1 /* GStreamer Split Demuxer bin that recombines files created by
2 * the splitmuxsink element.
4 * Copyright (C) <2014> Jan Schmidt <jan@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-splitmuxsrc
24 * @short_description: Split Demuxer bin that recombines files created by
25 * the splitmuxsink element.
27 * This element reads a set of input files created by the splitmuxsink element
28 * containing contiguous elementary streams split across multiple files.
30 * This element is similar to splitfilesrc, except that it recombines the
31 * streams in each file part at the demuxed elementary level, rather than
32 * as a single larger bytestream.
35 * <title>Example pipelines</title>
37 * gst-launch-1.0 splitmuxsrc location=video*.mov ! decodebin ! xvimagesink
38 * ]| Demux each file part and output the video stream as one continuous stream
40 * gst-launch-1.0 playbin uri="splitmux://path/to/foo.mp4.*"
41 * ]| Play back a set of files created by splitmuxsink
50 #include "gstsplitmuxsrc.h"
51 #include "gstsplitutils.h"
53 #include "../../gst-libs/gst/gst-i18n-plugin.h"
55 GST_DEBUG_CATEGORY (splitmux_debug);
56 #define GST_CAT_DEFAULT splitmux_debug
66 SIGNAL_FORMAT_LOCATION,
70 static guint signals[SIGNAL_LAST];
72 static GstStaticPadTemplate video_src_template =
73 GST_STATIC_PAD_TEMPLATE ("video",
78 static GstStaticPadTemplate audio_src_template =
79 GST_STATIC_PAD_TEMPLATE ("audio_%u",
84 static GstStaticPadTemplate subtitle_src_template =
85 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
90 static GstStateChangeReturn gst_splitmux_src_change_state (GstElement *
91 element, GstStateChange transition);
92 static void gst_splitmux_src_set_property (GObject * object, guint prop_id,
93 const GValue * value, GParamSpec * pspec);
94 static void gst_splitmux_src_get_property (GObject * object, guint prop_id,
95 GValue * value, GParamSpec * pspec);
96 static void gst_splitmux_src_dispose (GObject * object);
97 static void gst_splitmux_src_finalize (GObject * object);
98 static gboolean gst_splitmux_src_start (GstSplitMuxSrc * splitmux);
99 static gboolean gst_splitmux_src_stop (GstSplitMuxSrc * splitmux);
100 static void splitmux_src_pad_constructed (GObject * pad);
101 static gboolean splitmux_src_pad_event (GstPad * pad, GstObject * parent,
103 static gboolean splitmux_src_pad_query (GstPad * pad, GstObject * parent,
105 static void splitmux_src_uri_handler_init (gpointer g_iface,
106 gpointer iface_data);
109 static GstPad *gst_splitmux_find_output_pad (GstSplitMuxPartReader * part,
110 GstPad * pad, GstSplitMuxSrc * splitmux);
111 static void gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
112 GstSplitMuxSrc * splitmux);
113 static gboolean gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux,
114 SplitMuxSrcPad * pad);
115 static gboolean gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad,
119 G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, splitmux_src_uri_handler_init);
120 #define gst_splitmux_src_parent_class parent_class
122 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSrc, gst_splitmux_src, GST_TYPE_BIN, 0,
126 splitmux_src_uri_get_type (GType type)
131 static const gchar *const *
132 splitmux_src_uri_get_protocols (GType type)
134 static const gchar *protocols[] = { "splitmux", NULL };
140 splitmux_src_uri_get_uri (GstURIHandler * handler)
142 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
145 GST_OBJECT_LOCK (splitmux);
146 if (splitmux->location)
147 ret = g_strdup_printf ("splitmux://%s", splitmux->location);
148 GST_OBJECT_UNLOCK (splitmux);
153 splitmux_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
156 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
157 gchar *protocol, *location;
159 protocol = gst_uri_get_protocol (uri);
160 if (protocol == NULL || !g_str_equal (protocol, "splitmux"))
164 location = gst_uri_get_location (uri);
165 GST_OBJECT_LOCK (splitmux);
166 g_free (splitmux->location);
167 splitmux->location = location;
168 GST_OBJECT_UNLOCK (splitmux);
174 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
175 ("Error parsing uri %s", uri));
176 g_set_error_literal (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
177 "Could not parse splitmux URI");
182 splitmux_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
184 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) (g_iface);
186 iface->get_type = splitmux_src_uri_get_type;
187 iface->get_protocols = splitmux_src_uri_get_protocols;
188 iface->set_uri = splitmux_src_uri_set_uri;
189 iface->get_uri = splitmux_src_uri_get_uri;
194 gst_splitmux_src_class_init (GstSplitMuxSrcClass * klass)
196 GObjectClass *gobject_class = (GObjectClass *) klass;
197 GstElementClass *gstelement_class = (GstElementClass *) klass;
199 gobject_class->set_property = gst_splitmux_src_set_property;
200 gobject_class->get_property = gst_splitmux_src_get_property;
201 gobject_class->dispose = gst_splitmux_src_dispose;
202 gobject_class->finalize = gst_splitmux_src_finalize;
204 gst_element_class_set_static_metadata (gstelement_class,
205 "Split File Demuxing Bin", "Generic/Bin/Demuxer",
206 "Source that reads a set of files created by splitmuxsink",
207 "Jan Schmidt <jan@centricular.com>");
209 gst_element_class_add_static_pad_template (gstelement_class,
210 &video_src_template);
211 gst_element_class_add_static_pad_template (gstelement_class,
212 &audio_src_template);
213 gst_element_class_add_static_pad_template (gstelement_class,
214 &subtitle_src_template);
216 gstelement_class->change_state =
217 GST_DEBUG_FUNCPTR (gst_splitmux_src_change_state);
219 g_object_class_install_property (gobject_class, PROP_LOCATION,
220 g_param_spec_string ("location", "File Input Pattern",
221 "Glob pattern for the location of the files to read", NULL,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 * GstSplitMuxSrc::format-location:
226 * @splitmux: the #GstSplitMuxSrc
228 * Returns: A NULL-terminated sorted array of strings containing the
229 * filenames of the input files. The array will be freed internally
234 signals[SIGNAL_FORMAT_LOCATION] =
235 g_signal_new ("format-location", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_STRV, 0);
240 gst_splitmux_src_init (GstSplitMuxSrc * splitmux)
242 g_mutex_init (&splitmux->lock);
243 g_mutex_init (&splitmux->pads_lock);
244 splitmux->total_duration = GST_CLOCK_TIME_NONE;
245 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
249 gst_splitmux_src_dispose (GObject * object)
251 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
254 SPLITMUX_SRC_PADS_LOCK (splitmux);
256 for (cur = g_list_first (splitmux->pads);
257 cur != NULL; cur = g_list_next (cur)) {
258 GstPad *pad = GST_PAD (cur->data);
259 gst_element_remove_pad (GST_ELEMENT (splitmux), pad);
261 g_list_free (splitmux->pads);
262 splitmux->n_pads = 0;
263 splitmux->pads = NULL;
264 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
266 G_OBJECT_CLASS (parent_class)->dispose (object);
270 gst_splitmux_src_finalize (GObject * object)
272 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
273 g_mutex_clear (&splitmux->lock);
274 g_mutex_clear (&splitmux->pads_lock);
275 g_free (splitmux->location);
277 G_OBJECT_CLASS (parent_class)->finalize (object);
281 gst_splitmux_src_set_property (GObject * object, guint prop_id,
282 const GValue * value, GParamSpec * pspec)
284 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
288 GST_OBJECT_LOCK (splitmux);
289 g_free (splitmux->location);
290 splitmux->location = g_value_dup_string (value);
291 GST_OBJECT_UNLOCK (splitmux);
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 gst_splitmux_src_get_property (GObject * object, guint prop_id,
302 GValue * value, GParamSpec * pspec)
304 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
308 GST_OBJECT_LOCK (splitmux);
309 g_value_set_string (value, splitmux->location);
310 GST_OBJECT_UNLOCK (splitmux);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
318 static GstStateChangeReturn
319 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
321 GstStateChangeReturn ret;
322 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
324 switch (transition) {
325 case GST_STATE_CHANGE_NULL_TO_READY:{
328 case GST_STATE_CHANGE_READY_TO_PAUSED:{
329 if (!gst_splitmux_src_start (splitmux))
330 return GST_STATE_CHANGE_FAILURE;
333 case GST_STATE_CHANGE_PAUSED_TO_READY:
334 case GST_STATE_CHANGE_READY_TO_NULL:
335 /* Make sure the element will shut down */
336 if (!gst_splitmux_src_stop (splitmux))
337 return GST_STATE_CHANGE_FAILURE;
343 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
348 static GstSplitMuxPartReader *
349 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
351 GstSplitMuxPartReader *r;
353 r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
355 g_signal_connect (r, "prepared", (GCallback) gst_splitmux_part_prepared,
358 gst_splitmux_part_reader_set_callbacks (r, splitmux,
359 (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
360 gst_splitmux_part_reader_set_location (r, filename);
366 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
368 GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
376 gst_event_parse_caps (event, &newcaps);
378 GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
379 " and %" GST_PTR_FORMAT, curcaps, newcaps);
384 /* If caps are exactly equal exit early */
385 if (gst_caps_is_equal (curcaps, newcaps)) {
386 gst_caps_unref (curcaps);
390 /* More extensive check, ignore changes in framerate, because
391 * demuxers get that wrong */
392 tmpcaps = gst_caps_copy (newcaps);
393 s = gst_caps_get_structure (tmpcaps, 0);
394 gst_structure_remove_field (s, "framerate");
396 tmpcurcaps = gst_caps_copy (curcaps);
397 gst_caps_unref (curcaps);
398 s = gst_caps_get_structure (tmpcurcaps, 0);
399 gst_structure_remove_field (s, "framerate");
401 /* Now check if these filtered caps are equal */
402 if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
403 GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
407 gst_caps_unref (tmpcaps);
408 gst_caps_unref (tmpcurcaps);
413 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
414 SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
416 switch (GST_EVENT_TYPE (event)) {
417 case GST_EVENT_STREAM_START:{
418 if (splitpad->sent_stream_start)
420 splitpad->sent_stream_start = TRUE;
424 if (gst_splitmux_end_of_part (splitmux, splitpad))
425 // Continuing to next part, drop the EOS
427 if (splitmux->segment_seqnum) {
428 event = gst_event_make_writable (event);
429 gst_event_set_seqnum (event, splitmux->segment_seqnum);
433 case GST_EVENT_SEGMENT:{
434 GstClockTime duration;
437 gst_event_copy_segment (event, &seg);
439 splitpad->segment.position = seg.position;
441 if (splitpad->sent_segment)
442 goto drop_event; /* We already forwarded a segment event */
444 /* Calculate output segment */
445 GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
446 " got seg %" GST_SEGMENT_FORMAT
447 " play seg %" GST_SEGMENT_FORMAT,
448 &splitpad->segment, &seg, &splitmux->play_segment);
450 /* If playing forward, take the stop time from the overall
451 * seg or play_segment */
452 if (splitmux->play_segment.rate > 0.0) {
453 if (splitmux->play_segment.stop != -1)
454 seg.stop = splitmux->play_segment.stop;
456 seg.stop = splitpad->segment.stop;
458 /* Reverse playback from stop time to start time */
459 /* See if an end point was requested in the seek */
460 if (splitmux->play_segment.start != -1) {
461 seg.start = splitmux->play_segment.start;
462 seg.time = splitmux->play_segment.time;
464 seg.start = splitpad->segment.start;
465 seg.time = splitpad->segment.time;
469 GST_OBJECT_LOCK (splitmux);
470 duration = splitmux->total_duration;
471 GST_OBJECT_UNLOCK (splitmux);
474 seg.duration = duration;
476 seg.duration = GST_CLOCK_TIME_NONE;
478 GST_INFO_OBJECT (splitpad,
479 "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
481 gst_event_unref (event);
482 event = gst_event_new_segment (&seg);
483 if (splitmux->segment_seqnum)
484 gst_event_set_seqnum (event, splitmux->segment_seqnum);
485 splitpad->sent_segment = TRUE;
488 case GST_EVENT_CAPS:{
489 if (!gst_splitmux_check_new_caps (splitpad, event))
491 splitpad->sent_caps = TRUE;
498 gst_pad_push_event ((GstPad *) (splitpad), event);
501 gst_event_unref (event);
506 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
507 SplitMuxSrcPad * splitpad, GstBuffer * buf)
511 if (splitpad->clear_next_discont) {
512 GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
513 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
514 splitpad->clear_next_discont = FALSE;
516 if (splitpad->set_next_discont) {
517 GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
518 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
519 splitpad->set_next_discont = FALSE;
522 ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
524 GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
529 count_not_linked (GstSplitMuxSrc * splitmux)
534 for (cur = g_list_first (splitmux->pads);
535 cur != NULL; cur = g_list_next (cur)) {
536 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
537 if (GST_PAD_LAST_FLOW_RETURN (splitpad) == GST_FLOW_NOT_LINKED)
545 gst_splitmux_pad_loop (GstPad * pad)
547 /* Get one event/buffer from the associated part and push */
548 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
549 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
550 GstDataQueueItem *item = NULL;
551 GstSplitMuxPartReader *reader = splitpad->reader;
555 GST_OBJECT_LOCK (splitpad);
556 if (splitpad->part_pad == NULL) {
557 GST_OBJECT_UNLOCK (splitpad);
560 part_pad = gst_object_ref (splitpad->part_pad);
561 GST_OBJECT_UNLOCK (splitpad);
563 GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
564 " pad %" GST_PTR_FORMAT, reader, part_pad);
565 ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
566 if (ret == GST_FLOW_ERROR)
568 if (ret == GST_FLOW_FLUSHING || item == NULL)
571 GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
574 if (GST_IS_EVENT (item->object)) {
575 GstEvent *event = (GstEvent *) (item->object);
576 gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
578 GstBuffer *buf = (GstBuffer *) (item->object);
579 GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
580 if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
581 /* Stop immediately on error or flushing */
582 GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
583 gst_pad_pause_task (pad);
584 if (ret < GST_FLOW_EOS) {
585 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
586 } else if (ret == GST_FLOW_NOT_LINKED) {
590 /* Only post not-linked if all pads are not-linked */
591 SPLITMUX_SRC_PADS_LOCK (splitmux);
592 n_notlinked = count_not_linked (splitmux);
593 post_error = (splitmux->pads_complete
594 && n_notlinked == splitmux->n_pads);
595 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
598 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
602 g_slice_free (GstDataQueueItem, item);
604 gst_object_unref (part_pad);
605 gst_object_unref (splitmux);
610 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
611 ("Error reading part file %s", GST_STR_NULL (reader->path)));
613 gst_pad_pause_task (pad);
614 gst_object_unref (part_pad);
615 gst_object_unref (splitmux);
620 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part,
621 GstSeekFlags extra_flags)
625 GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
627 splitmux->cur_part = part;
628 if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
629 &splitmux->play_segment, extra_flags))
632 SPLITMUX_SRC_PADS_LOCK (splitmux);
633 for (cur = g_list_first (splitmux->pads);
634 cur != NULL; cur = g_list_next (cur)) {
635 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
636 splitpad->cur_part = part;
637 splitpad->reader = splitmux->parts[splitpad->cur_part];
638 if (splitpad->part_pad)
639 gst_object_unref (splitpad->part_pad);
641 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
642 (GstPad *) (splitpad));
644 /* Make sure we start with a DISCONT */
645 splitpad->set_next_discont = TRUE;
646 splitpad->clear_next_discont = FALSE;
648 gst_pad_start_task (GST_PAD (splitpad),
649 (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
651 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
657 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
659 gboolean ret = FALSE;
661 gchar *basename = NULL;
662 gchar *dirname = NULL;
664 GstClockTime next_offset = 0;
666 GstClockTime total_duration = 0;
668 GST_DEBUG_OBJECT (splitmux, "Starting");
670 g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
672 if (files == NULL || *files == NULL) {
673 GST_OBJECT_LOCK (splitmux);
674 if (splitmux->location != NULL && splitmux->location[0] != '\0') {
675 basename = g_path_get_basename (splitmux->location);
676 dirname = g_path_get_dirname (splitmux->location);
678 GST_OBJECT_UNLOCK (splitmux);
681 files = gst_split_util_find_files (dirname, basename, &err);
683 if (files == NULL || *files == NULL)
687 SPLITMUX_SRC_LOCK (splitmux);
688 splitmux->pads_complete = FALSE;
689 splitmux->running = TRUE;
690 SPLITMUX_SRC_UNLOCK (splitmux);
692 splitmux->num_parts = g_strv_length (files);
694 splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
696 for (i = 0; i < splitmux->num_parts; i++) {
697 splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
698 if (splitmux->parts[i] == NULL)
701 /* Figure out the next offset - the smallest one */
702 gst_splitmux_part_reader_set_start_offset (splitmux->parts[i], next_offset);
703 if (!gst_splitmux_part_reader_prepare (splitmux->parts[i])) {
704 GST_WARNING_OBJECT (splitmux,
705 "Failed to prepare file part %s. Cannot play past there.", files[i]);
706 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
707 ("Failed to prepare file part %s. Cannot play past there.",
709 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
710 g_object_unref (splitmux->parts[i]);
711 splitmux->parts[i] = NULL;
715 /* Extend our total duration to cover this part */
718 gst_splitmux_part_reader_get_duration (splitmux->parts[i]);
719 splitmux->play_segment.duration = total_duration;
721 next_offset = gst_splitmux_part_reader_get_end_offset (splitmux->parts[i]);
724 /* Update total_duration state variable */
725 GST_OBJECT_LOCK (splitmux);
726 splitmux->total_duration = total_duration;
727 GST_OBJECT_UNLOCK (splitmux);
729 /* Store how many parts we actually created */
730 splitmux->num_parts = i;
732 if (splitmux->num_parts < 1)
735 /* All done preparing, activate the first part */
736 GST_INFO_OBJECT (splitmux,
737 "All parts prepared. Total duration %" GST_TIME_FORMAT
738 " Activating first part", GST_TIME_ARGS (total_duration));
739 ret = gst_splitmux_src_activate_part (splitmux, 0, GST_SEEK_FLAG_NONE);
741 goto failed_first_part;
754 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
755 ("Failed to find files in '%s' for pattern '%s'",
756 GST_STR_NULL (dirname), GST_STR_NULL (basename)));
761 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
762 ("Failed to open any files for reading"));
767 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
768 ("Failed to activate first part for playback"));
774 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
778 GList *cur, *pads_list;
780 SPLITMUX_SRC_LOCK (splitmux);
781 if (!splitmux->running)
784 GST_DEBUG_OBJECT (splitmux, "Stopping");
786 /* Stop and destroy all parts */
787 for (i = 0; i < splitmux->num_parts; i++) {
788 if (splitmux->parts[i] == NULL)
790 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
791 g_object_unref (splitmux->parts[i]);
792 splitmux->parts[i] = NULL;
795 SPLITMUX_SRC_PADS_LOCK (splitmux);
796 pads_list = splitmux->pads;
797 splitmux->pads = NULL;
798 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
800 SPLITMUX_SRC_UNLOCK (splitmux);
801 for (cur = g_list_first (pads_list); cur != NULL; cur = g_list_next (cur)) {
802 SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
803 gst_pad_stop_task (GST_PAD (tmp));
804 gst_element_remove_pad (GST_ELEMENT (splitmux), GST_PAD (tmp));
806 g_list_free (pads_list);
807 SPLITMUX_SRC_LOCK (splitmux);
809 g_free (splitmux->parts);
810 splitmux->parts = NULL;
811 splitmux->num_parts = 0;
812 splitmux->running = FALSE;
813 splitmux->total_duration = GST_CLOCK_TIME_NONE;
814 /* Reset playback segment */
815 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
817 SPLITMUX_SRC_UNLOCK (splitmux);
823 GstSplitMuxSrc *splitmux;
824 SplitMuxSrcPad *splitpad;
828 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
830 SplitMuxAndPad *splitmux_and_pad;
831 GstSplitMuxSrc *splitmux;
832 SplitMuxSrcPad *splitpad;
834 splitmux_and_pad = user_data;
835 splitmux = splitmux_and_pad->splitmux;
836 splitpad = splitmux_and_pad->splitpad;
838 GST_DEBUG_OBJECT (splitpad, "handle sticky event %" GST_PTR_FORMAT, *event);
839 gst_event_ref (*event);
840 gst_splitmux_handle_event (splitmux, splitpad, pad, *event);
846 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
847 GstSplitMuxSrc * splitmux)
850 gchar *pad_name = gst_pad_get_name (pad);
851 GstPad *target = NULL;
852 gboolean is_new_pad = FALSE;
854 SPLITMUX_SRC_LOCK (splitmux);
855 SPLITMUX_SRC_PADS_LOCK (splitmux);
856 for (cur = g_list_first (splitmux->pads);
857 cur != NULL; cur = g_list_next (cur)) {
858 GstPad *tmp = (GstPad *) (cur->data);
859 if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
865 if (target == NULL && !splitmux->pads_complete) {
866 SplitMuxAndPad splitmux_and_pad;
868 /* No pad found, create one */
869 target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
870 "name", pad_name, "direction", GST_PAD_SRC, NULL);
871 splitmux->pads = g_list_prepend (splitmux->pads, target);
874 gst_pad_set_active (target, TRUE);
876 splitmux_and_pad.splitmux = splitmux;
877 splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
878 gst_pad_sticky_events_foreach (pad, handle_sticky_events,
882 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
883 SPLITMUX_SRC_UNLOCK (splitmux);
891 gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
896 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
897 ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
903 gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
904 GstSplitMuxSrc * splitmux)
906 gboolean need_no_more_pads;
908 GST_LOG_OBJECT (splitmux, "Part %" GST_PTR_FORMAT " prepared", reader);
909 SPLITMUX_SRC_LOCK (splitmux);
910 need_no_more_pads = !splitmux->pads_complete;
911 splitmux->pads_complete = TRUE;
912 SPLITMUX_SRC_UNLOCK (splitmux);
914 if (need_no_more_pads) {
915 GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
916 gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
921 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
927 e = gst_event_make_writable (e);
928 gst_event_set_seqnum (e, seqnum);
931 SPLITMUX_SRC_PADS_LOCK (splitmux);
932 for (cur = g_list_first (splitmux->pads);
933 cur != NULL; cur = g_list_next (cur)) {
934 GstPad *pad = GST_PAD_CAST (cur->data);
936 gst_pad_push_event (pad, e);
938 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
944 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
946 GstEvent *e = gst_event_new_flush_stop (TRUE);
950 e = gst_event_make_writable (e);
951 gst_event_set_seqnum (e, seqnum);
954 SPLITMUX_SRC_PADS_LOCK (splitmux);
955 for (cur = g_list_first (splitmux->pads);
956 cur != NULL; cur = g_list_next (cur)) {
957 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
960 gst_pad_push_event (GST_PAD_CAST (target), e);
961 target->sent_caps = FALSE;
962 target->sent_stream_start = FALSE;
963 target->sent_segment = FALSE;
965 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
970 /* Callback for when a part finishes and we need to move to the next */
972 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
975 gint cur_part = splitpad->cur_part;
976 gboolean res = FALSE;
978 if (splitmux->play_segment.rate >= 0.0) {
979 if (cur_part + 1 < splitmux->num_parts)
980 next_part = cur_part + 1;
981 /* Make sure the transition is seamless */
982 splitpad->set_next_discont = FALSE;
983 splitpad->clear_next_discont = TRUE;
985 /* Reverse play - move to previous segment */
987 next_part = cur_part - 1;
988 /* Non-seamless transition in reverse */
989 splitpad->set_next_discont = TRUE;
990 splitpad->clear_next_discont = FALSE;
994 SPLITMUX_SRC_LOCK (splitmux);
996 /* If all pads are done with this part, deactivate it */
997 if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
998 gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
1000 if (splitmux->play_segment.rate >= 0.0) {
1001 if (splitmux->play_segment.stop != -1) {
1002 GstClockTime part_end =
1003 gst_splitmux_part_reader_get_end_offset (splitmux->parts[cur_part]);
1004 if (part_end >= splitmux->play_segment.stop) {
1005 GST_DEBUG_OBJECT (splitmux,
1006 "Stop position was within that part. Finishing");
1011 if (splitmux->play_segment.start != -1) {
1012 GstClockTime part_start =
1013 gst_splitmux_part_reader_get_start_offset (splitmux->parts[cur_part]);
1014 if (part_start <= splitmux->play_segment.start) {
1015 GST_DEBUG_OBJECT (splitmux,
1016 "Start position %" GST_TIME_FORMAT
1017 " was within that part. Finishing",
1018 GST_TIME_ARGS (splitmux->play_segment.start));
1024 if (next_part != -1) {
1025 GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
1026 " moving to part %d", splitpad, next_part);
1027 splitpad->cur_part = next_part;
1028 splitpad->reader = splitmux->parts[splitpad->cur_part];
1029 if (splitpad->part_pad)
1030 gst_object_unref (splitpad->part_pad);
1031 splitpad->part_pad =
1032 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
1033 (GstPad *) (splitpad));
1035 if (splitmux->cur_part != next_part) {
1036 if (!gst_splitmux_part_reader_is_active (splitpad->reader)) {
1038 /* If moving backward into a new part, set stop
1039 * to -1 to ensure we play the entire file - workaround
1040 * a bug in qtdemux that misses bits at the end */
1041 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1045 /* This is the first pad to move to the new part, activate it */
1046 GST_DEBUG_OBJECT (splitpad,
1047 "First pad to change part. Activating part %d with seg %"
1048 GST_SEGMENT_FORMAT, next_part, &tmp);
1049 if (!gst_splitmux_part_reader_activate (splitpad->reader, &tmp,
1050 GST_SEEK_FLAG_NONE))
1053 splitmux->cur_part = next_part;
1058 SPLITMUX_SRC_UNLOCK (splitmux);
1061 SPLITMUX_SRC_UNLOCK (splitmux);
1062 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
1063 ("Failed to activate part %d", splitmux->cur_part));
1067 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
1070 splitmux_src_pad_constructed (GObject * pad)
1072 gst_pad_set_event_function (GST_PAD (pad),
1073 GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
1074 gst_pad_set_query_function (GST_PAD (pad),
1075 GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
1077 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1081 gst_splitmux_src_pad_dispose (GObject * object)
1083 SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1085 GST_OBJECT_LOCK (pad);
1086 if (pad->part_pad) {
1087 gst_object_unref (pad->part_pad);
1088 pad->part_pad = NULL;
1090 GST_OBJECT_UNLOCK (pad);
1092 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1096 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1098 GObjectClass *gobject_klass = (GObjectClass *) (klass);
1100 gobject_klass->constructed = splitmux_src_pad_constructed;
1101 gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1105 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1109 /* Event handler for source pads. Proxy events into the child
1113 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1115 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1116 gboolean ret = FALSE;
1118 GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1119 " on %" GST_PTR_FORMAT, event, pad);
1121 switch (GST_EVENT_TYPE (event)) {
1122 case GST_EVENT_SEEK:{
1126 GstSeekType start_type, stop_type;
1130 GstClockTime part_start, position;
1134 gst_event_parse_seek (event, &rate, &format, &flags,
1135 &start_type, &start, &stop_type, &stop);
1137 if (format != GST_FORMAT_TIME) {
1138 GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1141 /* FIXME: Support non-flushing seeks, which might never wake up */
1142 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1143 GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
1146 seqnum = gst_event_get_seqnum (event);
1148 SPLITMUX_SRC_LOCK (splitmux);
1149 if (!splitmux->running || splitmux->num_parts < 1) {
1150 /* Not started yet */
1151 SPLITMUX_SRC_UNLOCK (splitmux);
1155 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1157 if (!gst_segment_do_seek (&tmp, rate,
1158 format, flags, start_type, start, stop_type, stop, NULL)) {
1159 /* Invalid seek requested, ignore it */
1160 SPLITMUX_SRC_UNLOCK (splitmux);
1163 position = tmp.position;
1165 GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1166 GST_SEGMENT_FORMAT, &tmp);
1168 GST_DEBUG_OBJECT (splitmux,
1169 "Handling flushing seek. Sending flush start");
1171 /* Send flush_start */
1172 gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1174 /* Stop all parts, which will work because of the flush */
1175 SPLITMUX_SRC_PADS_LOCK (splitmux);
1176 SPLITMUX_SRC_UNLOCK (splitmux);
1177 for (cur = g_list_first (splitmux->pads);
1178 cur != NULL; cur = g_list_next (cur)) {
1179 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1180 GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1181 gst_splitmux_part_reader_deactivate (reader);
1184 /* Shut down pad tasks */
1185 GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1186 for (cur = g_list_first (splitmux->pads);
1187 cur != NULL; cur = g_list_next (cur)) {
1188 GstPad *splitpad = (GstPad *) (cur->data);
1189 gst_pad_pause_task (GST_PAD (splitpad));
1191 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1192 SPLITMUX_SRC_LOCK (splitmux);
1194 /* Send flush stop */
1195 GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1196 gst_splitmux_push_flush_stop (splitmux, seqnum);
1198 /* Everything is stopped, so update the play_segment */
1199 gst_segment_copy_into (&tmp, &splitmux->play_segment);
1200 splitmux->segment_seqnum = seqnum;
1202 /* Work out where to start from now */
1203 for (i = 0; i < splitmux->num_parts; i++) {
1204 GstSplitMuxPartReader *reader = splitmux->parts[i];
1205 GstClockTime part_end =
1206 gst_splitmux_part_reader_get_end_offset (reader);
1208 if (part_end > position)
1211 if (i == splitmux->num_parts)
1212 i = splitmux->num_parts - 1;
1215 gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1217 GST_DEBUG_OBJECT (splitmux,
1218 "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1219 GST_TIME_FORMAT, GST_TIME_ARGS (position),
1220 i, GST_TIME_ARGS (position - part_start));
1222 ret = gst_splitmux_src_activate_part (splitmux, i, flags);
1223 SPLITMUX_SRC_UNLOCK (splitmux);
1229 gst_event_unref (event);
1235 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1237 /* Query handler for source pads. Proxy queries into the child
1240 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1241 gboolean ret = FALSE;
1243 GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1244 " on %" GST_PTR_FORMAT, query, pad);
1245 switch (GST_QUERY_TYPE (query)) {
1246 case GST_QUERY_CAPS:
1247 case GST_QUERY_POSITION:{
1248 GstSplitMuxPartReader *part;
1249 SplitMuxSrcPad *anypad;
1251 SPLITMUX_SRC_LOCK (splitmux);
1252 SPLITMUX_SRC_PADS_LOCK (splitmux);
1253 anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1254 part = splitmux->parts[anypad->cur_part];
1255 ret = gst_splitmux_part_reader_src_query (part, pad, query);
1256 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1257 SPLITMUX_SRC_UNLOCK (splitmux);
1260 case GST_QUERY_DURATION:{
1261 GstClockTime duration;
1264 gst_query_parse_duration (query, &fmt, NULL);
1265 if (fmt != GST_FORMAT_TIME)
1268 GST_OBJECT_LOCK (splitmux);
1269 duration = splitmux->total_duration;
1270 GST_OBJECT_UNLOCK (splitmux);
1272 if (duration > 0 && duration != GST_CLOCK_TIME_NONE) {
1273 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1278 case GST_QUERY_SEEKING:{
1281 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1282 if (format != GST_FORMAT_TIME)
1285 GST_OBJECT_LOCK (splitmux);
1286 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1287 splitmux->total_duration);
1289 GST_OBJECT_UNLOCK (splitmux);
1301 register_splitmuxsrc (GstPlugin * plugin)
1303 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1304 "Split File Demuxing Source");
1306 return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1307 GST_TYPE_SPLITMUX_SRC);