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
64 static GstStaticPadTemplate video_src_template =
65 GST_STATIC_PAD_TEMPLATE ("video",
70 static GstStaticPadTemplate audio_src_template =
71 GST_STATIC_PAD_TEMPLATE ("audio_%u",
76 static GstStaticPadTemplate subtitle_src_template =
77 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
82 static GstStateChangeReturn gst_splitmux_src_change_state (GstElement *
83 element, GstStateChange transition);
84 static void gst_splitmux_src_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_splitmux_src_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
88 static void gst_splitmux_src_dispose (GObject * object);
89 static void gst_splitmux_src_finalize (GObject * object);
90 static gboolean gst_splitmux_src_start (GstSplitMuxSrc * splitmux);
91 static gboolean gst_splitmux_src_stop (GstSplitMuxSrc * splitmux);
92 static void splitmux_src_pad_constructed (GObject * pad);
93 static gboolean splitmux_src_pad_event (GstPad * pad, GstObject * parent,
95 static gboolean splitmux_src_pad_query (GstPad * pad, GstObject * parent,
97 static void splitmux_src_uri_handler_init (gpointer g_iface,
101 static GstPad *gst_splitmux_find_output_pad (GstSplitMuxPartReader * part,
102 GstPad * pad, GstSplitMuxSrc * splitmux);
103 static void gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
104 GstSplitMuxSrc * splitmux);
105 static gboolean gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux,
106 SplitMuxSrcPad * pad);
107 static gboolean gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad,
111 G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, splitmux_src_uri_handler_init);
112 #define gst_splitmux_src_parent_class parent_class
114 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSrc, gst_splitmux_src, GST_TYPE_BIN, 0,
118 splitmux_src_uri_get_type (GType type)
123 static const gchar *const *
124 splitmux_src_uri_get_protocols (GType type)
126 static const gchar *protocols[] = { "splitmux", NULL };
132 splitmux_src_uri_get_uri (GstURIHandler * handler)
134 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
137 GST_OBJECT_LOCK (splitmux);
138 if (splitmux->location)
139 ret = g_strdup_printf ("splitmux://%s", splitmux->location);
140 GST_OBJECT_UNLOCK (splitmux);
145 splitmux_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
148 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
149 gchar *protocol, *location;
151 protocol = gst_uri_get_protocol (uri);
152 if (protocol == NULL || !g_str_equal (protocol, "splitmux"))
156 location = gst_uri_get_location (uri);
157 GST_OBJECT_LOCK (splitmux);
158 g_free (splitmux->location);
159 splitmux->location = location;
160 GST_OBJECT_UNLOCK (splitmux);
166 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
167 ("Error parsing uri %s", uri));
168 g_set_error_literal (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
169 "Could not parse splitmux URI");
174 splitmux_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
176 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) (g_iface);
178 iface->get_type = splitmux_src_uri_get_type;
179 iface->get_protocols = splitmux_src_uri_get_protocols;
180 iface->set_uri = splitmux_src_uri_set_uri;
181 iface->get_uri = splitmux_src_uri_get_uri;
186 gst_splitmux_src_class_init (GstSplitMuxSrcClass * klass)
188 GObjectClass *gobject_class = (GObjectClass *) klass;
189 GstElementClass *gstelement_class = (GstElementClass *) klass;
191 gobject_class->set_property = gst_splitmux_src_set_property;
192 gobject_class->get_property = gst_splitmux_src_get_property;
193 gobject_class->dispose = gst_splitmux_src_dispose;
194 gobject_class->finalize = gst_splitmux_src_finalize;
196 gst_element_class_set_static_metadata (gstelement_class,
197 "Split File Demuxing Bin", "Generic/Bin/Demuxer",
198 "Source that reads a set of files created by splitmuxsink",
199 "Jan Schmidt <jan@centricular.com>");
201 gst_element_class_add_pad_template (gstelement_class,
202 gst_static_pad_template_get (&video_src_template));
203 gst_element_class_add_pad_template (gstelement_class,
204 gst_static_pad_template_get (&audio_src_template));
205 gst_element_class_add_pad_template (gstelement_class,
206 gst_static_pad_template_get (&subtitle_src_template));
208 gstelement_class->change_state =
209 GST_DEBUG_FUNCPTR (gst_splitmux_src_change_state);
211 g_object_class_install_property (gobject_class, PROP_LOCATION,
212 g_param_spec_string ("location", "File Input Pattern",
213 "Glob pattern for the location of the files to read", NULL,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 gst_splitmux_src_init (GstSplitMuxSrc * splitmux)
220 g_mutex_init (&splitmux->lock);
221 g_mutex_init (&splitmux->pads_lock);
222 splitmux->total_duration = GST_CLOCK_TIME_NONE;
223 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
227 gst_splitmux_src_dispose (GObject * object)
229 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
232 SPLITMUX_SRC_PADS_LOCK (splitmux);
234 for (cur = g_list_first (splitmux->pads);
235 cur != NULL; cur = g_list_next (cur)) {
236 GstPad *pad = GST_PAD (cur->data);
237 gst_element_remove_pad (GST_ELEMENT (splitmux), pad);
239 g_list_free (splitmux->pads);
240 splitmux->pads = NULL;
241 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
243 G_OBJECT_CLASS (parent_class)->dispose (object);
247 gst_splitmux_src_finalize (GObject * object)
249 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
250 g_mutex_clear (&splitmux->lock);
251 g_mutex_clear (&splitmux->pads_lock);
252 g_free (splitmux->location);
254 G_OBJECT_CLASS (parent_class)->finalize (object);
258 gst_splitmux_src_set_property (GObject * object, guint prop_id,
259 const GValue * value, GParamSpec * pspec)
261 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
265 GST_OBJECT_LOCK (splitmux);
266 g_free (splitmux->location);
267 splitmux->location = g_value_dup_string (value);
268 GST_OBJECT_UNLOCK (splitmux);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_splitmux_src_get_property (GObject * object, guint prop_id,
279 GValue * value, GParamSpec * pspec)
281 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
285 GST_OBJECT_LOCK (splitmux);
286 g_value_set_string (value, splitmux->location);
287 GST_OBJECT_UNLOCK (splitmux);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 static GstStateChangeReturn
296 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
298 GstStateChangeReturn ret;
299 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
301 switch (transition) {
302 case GST_STATE_CHANGE_NULL_TO_READY:{
305 case GST_STATE_CHANGE_READY_TO_PAUSED:{
306 if (!gst_splitmux_src_start (splitmux))
307 return GST_STATE_CHANGE_FAILURE;
310 case GST_STATE_CHANGE_PAUSED_TO_READY:
311 case GST_STATE_CHANGE_READY_TO_NULL:
312 /* Make sure the element will shut down */
313 if (!gst_splitmux_src_stop (splitmux))
314 return GST_STATE_CHANGE_FAILURE;
320 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
325 static GstSplitMuxPartReader *
326 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
328 GstSplitMuxPartReader *r;
330 r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
332 g_signal_connect (r, "prepared", (GCallback) gst_splitmux_part_prepared,
335 gst_splitmux_part_reader_set_callbacks (r, splitmux,
336 (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
337 gst_splitmux_part_reader_set_location (r, filename);
343 resend_sticky (GstPad * pad, GstEvent ** event, GstPad * target)
345 switch (GST_EVENT_TYPE (*event)) {
347 if (!gst_splitmux_check_new_caps (SPLITMUX_SRC_PAD (target), *event))
349 return gst_pad_push_event (target, gst_event_ref (*event));
350 case GST_EVENT_STREAM_START:
351 return gst_pad_push_event (target, gst_event_ref (*event));
360 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
362 GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
370 gst_event_parse_caps (event, &newcaps);
372 GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
373 " and %" GST_PTR_FORMAT, curcaps, newcaps);
378 /* If caps are exactly equal exit early */
379 if (gst_caps_is_equal (curcaps, newcaps)) {
380 gst_caps_unref (curcaps);
384 /* More extensive check, ignore changes in framerate, because
385 * demuxers get that wrong */
386 tmpcaps = gst_caps_copy (newcaps);
387 s = gst_caps_get_structure (tmpcaps, 0);
388 gst_structure_remove_field (s, "framerate");
390 tmpcurcaps = gst_caps_copy (curcaps);
391 gst_caps_unref (curcaps);
392 s = gst_caps_get_structure (tmpcurcaps, 0);
393 gst_structure_remove_field (s, "framerate");
395 /* Now check if these filtered caps are equal */
396 if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
397 GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
401 gst_caps_unref (tmpcaps);
402 gst_caps_unref (tmpcurcaps);
407 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
408 SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
410 switch (GST_EVENT_TYPE (event)) {
411 case GST_EVENT_STREAM_START:{
412 if (splitpad->sent_stream_start)
414 splitpad->sent_stream_start = TRUE;
418 if (gst_splitmux_end_of_part (splitmux, splitpad))
419 // Continuing to next part, drop the EOS
423 case GST_EVENT_SEGMENT:{
426 gst_event_copy_segment (event, &seg);
428 splitpad->segment.position = seg.position;
430 if (splitpad->sent_segment)
431 goto drop_event; /* We already forwarded a segment event */
433 /* Calculate output segment */
434 GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
435 " got seg %" GST_SEGMENT_FORMAT
436 " play seg %" GST_SEGMENT_FORMAT,
437 &splitpad->segment, &seg, &splitmux->play_segment);
439 /* If playing forward, take the stop time from the overall
440 * seg or play_segment */
441 if (splitmux->play_segment.rate > 0.0) {
442 if (splitmux->play_segment.stop != -1)
443 seg.stop = splitmux->play_segment.stop;
445 seg.stop = splitpad->segment.stop;
447 /* Reverse playback from stop time to start time */
448 /* See if an end point was requested in the seek */
449 if (splitmux->play_segment.start != -1) {
450 seg.start = splitmux->play_segment.start;
451 seg.time = splitmux->play_segment.time;
453 seg.start = splitpad->segment.start;
454 seg.time = splitpad->segment.time;
458 GST_INFO_OBJECT (splitpad,
459 "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
461 gst_event_unref (event);
462 event = gst_event_new_segment (&seg);
463 splitpad->sent_segment = TRUE;
466 case GST_EVENT_CAPS:{
467 if (!gst_splitmux_check_new_caps (splitpad, event))
469 splitpad->sent_caps = TRUE;
476 /* Make sure to send sticky events - from the part_pad directly */
477 if (splitpad->sent_caps == FALSE || splitpad->sent_stream_start == FALSE) {
478 gst_pad_sticky_events_foreach (GST_PAD_CAST (part_pad),
479 (GstPadStickyEventsForeachFunction) (resend_sticky), splitpad);
480 splitpad->sent_caps = splitpad->sent_stream_start = TRUE;
483 gst_pad_push_event ((GstPad *) (splitpad), event);
486 gst_event_unref (event);
491 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
492 SplitMuxSrcPad * splitpad, GstBuffer * buf)
496 if (splitpad->clear_next_discont) {
497 GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
498 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
499 splitpad->clear_next_discont = FALSE;
501 if (splitpad->set_next_discont) {
502 GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
503 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
504 splitpad->set_next_discont = FALSE;
507 ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
509 GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
514 gst_splitmux_pad_loop (GstPad * pad)
516 /* Get one event/buffer from the associated part and push */
517 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
518 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
519 GstDataQueueItem *item = NULL;
520 GstSplitMuxPartReader *reader = splitpad->reader;
521 GstPad *part_pad = splitpad->part_pad;
524 GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
525 " pad %" GST_PTR_FORMAT, reader, part_pad);
526 ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
527 if (ret == GST_FLOW_ERROR)
529 if (ret == GST_FLOW_FLUSHING || item == NULL)
532 GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
535 if (GST_IS_EVENT (item->object)) {
536 GstEvent *event = (GstEvent *) (item->object);
537 gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
539 GstBuffer *buf = (GstBuffer *) (item->object);
540 GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
541 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
542 /* Stop immediately on error or flushing */
543 GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
544 gst_pad_pause_task (pad);
545 if (ret <= GST_FLOW_EOS) {
546 const gchar *reason = gst_flow_get_name (ret);
547 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED,
548 (_("Internal data flow error.")),
549 ("streaming task paused, reason %s (%d)", reason, ret));
553 g_slice_free (GstDataQueueItem, item);
555 gst_object_unref (splitmux);
560 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
561 ("Error reading part file %s", GST_STR_NULL (reader->path)));
563 gst_pad_pause_task (pad);
564 gst_object_unref (splitmux);
569 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part)
573 GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
575 splitmux->cur_part = part;
576 gst_splitmux_part_reader_activate (splitmux->parts[part],
577 &splitmux->play_segment);
579 SPLITMUX_SRC_PADS_LOCK (splitmux);
580 for (cur = g_list_first (splitmux->pads);
581 cur != NULL; cur = g_list_next (cur)) {
582 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
583 splitpad->cur_part = part;
584 splitpad->reader = splitmux->parts[splitpad->cur_part];
586 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
587 (GstPad *) (splitpad));
589 /* Make sure we start with a DISCONT */
590 splitpad->set_next_discont = TRUE;
591 splitpad->clear_next_discont = FALSE;
593 gst_pad_start_task (GST_PAD (splitpad),
594 (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
596 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
600 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
602 gboolean ret = FALSE;
604 gchar *basename = NULL;
605 gchar *dirname = NULL;
607 GstClockTime next_offset = 0;
609 GstClockTime total_duration = 0;
611 GST_DEBUG_OBJECT (splitmux, "Starting");
613 GST_OBJECT_LOCK (splitmux);
614 if (splitmux->location != NULL && splitmux->location[0] != '\0') {
615 basename = g_path_get_basename (splitmux->location);
616 dirname = g_path_get_dirname (splitmux->location);
618 GST_OBJECT_UNLOCK (splitmux);
620 files = gst_split_util_find_files (dirname, basename, &err);
622 if (files == NULL || *files == NULL)
625 SPLITMUX_SRC_LOCK (splitmux);
626 splitmux->running = TRUE;
627 SPLITMUX_SRC_UNLOCK (splitmux);
629 splitmux->num_parts = g_strv_length (files);
631 splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
633 for (i = 0; i < splitmux->num_parts; i++) {
634 splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
635 if (splitmux->parts[i] == NULL)
638 /* Figure out the next offset - the smallest one */
639 gst_splitmux_part_reader_set_start_offset (splitmux->parts[i], next_offset);
640 if (!gst_splitmux_part_reader_prepare (splitmux->parts[i])) {
641 GST_WARNING_OBJECT (splitmux,
642 "Failed to prepare file part %s. Cannot play past there.", files[i]);
643 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
644 ("Failed to prepare file part %s. Cannot play past there.",
646 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
647 g_object_unref (splitmux->parts[i]);
648 splitmux->parts[i] = NULL;
652 /* Extend our total duration to cover this part */
655 gst_splitmux_part_reader_get_duration (splitmux->parts[i]);
656 splitmux->play_segment.duration = total_duration;
658 next_offset = gst_splitmux_part_reader_get_end_offset (splitmux->parts[i]);
661 /* Update total_duration state variable */
662 GST_OBJECT_LOCK (splitmux);
663 splitmux->total_duration = total_duration;
664 GST_OBJECT_UNLOCK (splitmux);
666 /* Store how many parts we actually created */
667 splitmux->num_parts = i;
669 if (splitmux->num_parts < 1)
672 /* All done preparing, activate the first part */
673 GST_INFO_OBJECT (splitmux,
674 "All parts prepared. Total duration %" GST_TIME_FORMAT
675 " Activating first part", GST_TIME_ARGS (total_duration));
676 gst_splitmux_src_activate_part (splitmux, 0);
691 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
692 ("Failed to find files in '%s' for pattern '%s'",
693 GST_STR_NULL (dirname), GST_STR_NULL (basename)));
698 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
699 ("Failed to open any files for reading"));
705 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
711 SPLITMUX_SRC_LOCK (splitmux);
712 if (!splitmux->running)
715 GST_DEBUG_OBJECT (splitmux, "Stopping");
717 /* Stop and destroy all parts */
718 for (i = 0; i < splitmux->num_parts; i++) {
719 if (splitmux->parts[i] == NULL)
721 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
722 g_object_unref (splitmux->parts[i]);
723 splitmux->parts[i] = NULL;
726 SPLITMUX_SRC_PADS_LOCK (splitmux);
727 for (cur = g_list_first (splitmux->pads);
728 cur != NULL; cur = g_list_next (cur)) {
729 SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
730 gst_pad_stop_task (GST_PAD (tmp));
732 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
734 g_free (splitmux->parts);
735 splitmux->parts = NULL;
736 splitmux->num_parts = 0;
737 splitmux->running = FALSE;
738 splitmux->total_duration = GST_CLOCK_TIME_NONE;
739 /* Reset playback segment */
740 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
742 SPLITMUX_SRC_UNLOCK (splitmux);
747 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
748 GstSplitMuxSrc * splitmux)
751 gchar *pad_name = gst_pad_get_name (pad);
752 GstPad *target = NULL;
753 gboolean is_new_pad = FALSE;
755 SPLITMUX_SRC_LOCK (splitmux);
756 SPLITMUX_SRC_PADS_LOCK (splitmux);
757 for (cur = g_list_first (splitmux->pads);
758 cur != NULL; cur = g_list_next (cur)) {
759 GstPad *tmp = (GstPad *) (cur->data);
760 if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
766 if (target == NULL && !splitmux->pads_complete) {
767 /* No pad found, create one */
768 target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
769 "name", pad_name, "direction", GST_PAD_SRC, NULL);
770 splitmux->pads = g_list_prepend (splitmux->pads, target);
772 gst_pad_set_active (target, TRUE);
775 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
776 SPLITMUX_SRC_UNLOCK (splitmux);
784 gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
789 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
790 ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
796 gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
797 GstSplitMuxSrc * splitmux)
799 gboolean need_no_more_pads;
801 SPLITMUX_SRC_LOCK (splitmux);
802 need_no_more_pads = !splitmux->pads_complete;
803 splitmux->pads_complete = TRUE;
804 SPLITMUX_SRC_UNLOCK (splitmux);
806 if (need_no_more_pads) {
807 GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
808 gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
813 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
819 gst_event_set_seqnum (e, seqnum);
821 SPLITMUX_SRC_PADS_LOCK (splitmux);
822 for (cur = g_list_first (splitmux->pads);
823 cur != NULL; cur = g_list_next (cur)) {
824 GstPad *pad = GST_PAD_CAST (cur->data);
826 gst_pad_push_event (pad, e);
828 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
834 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
836 GstEvent *e = gst_event_new_flush_stop (TRUE);
840 gst_event_set_seqnum (e, seqnum);
842 SPLITMUX_SRC_PADS_LOCK (splitmux);
843 for (cur = g_list_first (splitmux->pads);
844 cur != NULL; cur = g_list_next (cur)) {
845 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
848 gst_pad_push_event (GST_PAD_CAST (target), e);
849 target->sent_caps = FALSE;
850 target->sent_stream_start = FALSE;
851 target->sent_segment = FALSE;
853 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
858 /* Callback for when a part finishes and we need to move to the next */
860 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
863 gint cur_part = splitpad->cur_part;
864 gboolean res = FALSE;
866 if (splitmux->play_segment.rate >= 0.0) {
867 if (cur_part + 1 < splitmux->num_parts)
868 next_part = cur_part + 1;
869 /* Make sure the transition is seamless */
870 splitpad->set_next_discont = FALSE;
871 splitpad->clear_next_discont = TRUE;
873 /* Reverse play - move to previous segment */
875 next_part = cur_part - 1;
876 /* Non-seamless transition in reverse */
877 splitpad->set_next_discont = TRUE;
878 splitpad->clear_next_discont = FALSE;
882 SPLITMUX_SRC_LOCK (splitmux);
884 /* If all pads are done with this part, deactivate it */
885 if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
886 gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
888 if (next_part != -1) {
889 GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
890 " moving to part %d", splitpad, next_part);
891 splitpad->cur_part = next_part;
892 splitpad->reader = splitmux->parts[splitpad->cur_part];
894 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
895 (GstPad *) (splitpad));
897 if (splitmux->cur_part != next_part) {
899 /* If moving backward into a new part, set stop
900 * to -1 to ensure we play the entire file - workaround
901 * a bug in qtdemux that misses bits at the end */
902 gst_segment_copy_into (&splitmux->play_segment, &tmp);
906 /* This is the first pad to move to the new part, activate it */
907 splitmux->cur_part = next_part;
908 GST_DEBUG_OBJECT (splitpad,
909 "First pad to change part. Activating part %d with seg %"
910 GST_SEGMENT_FORMAT, next_part, &tmp);
911 gst_splitmux_part_reader_activate (splitpad->reader, &tmp);
916 SPLITMUX_SRC_UNLOCK (splitmux);
920 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
923 splitmux_src_pad_constructed (GObject * pad)
925 gst_pad_set_event_function (GST_PAD (pad),
926 GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
927 gst_pad_set_query_function (GST_PAD (pad),
928 GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
930 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
934 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
936 GObjectClass *gobject_klass = (GObjectClass *) (klass);
938 gobject_klass->constructed = splitmux_src_pad_constructed;
942 splitmux_src_pad_init (SplitMuxSrcPad * pad)
946 /* Event handler for source pads. Proxy events into the child
950 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
952 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
953 gboolean ret = FALSE;
955 GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
956 " on %" GST_PTR_FORMAT, event, pad);
958 switch (GST_EVENT_TYPE (event)) {
959 case GST_EVENT_SEEK:{
963 GstSeekType start_type, stop_type;
967 GstClockTime part_start, position;
971 gst_event_parse_seek (event, &rate, &format, &flags,
972 &start_type, &start, &stop_type, &stop);
974 if (format != GST_FORMAT_TIME) {
975 GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
978 /* FIXME: Support non-flushing seeks, which might never wake up */
979 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
980 GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
983 seqnum = gst_event_get_seqnum (event);
985 SPLITMUX_SRC_LOCK (splitmux);
986 if (!splitmux->running || splitmux->num_parts < 1) {
987 /* Not started yet */
988 SPLITMUX_SRC_UNLOCK (splitmux);
992 gst_segment_copy_into (&splitmux->play_segment, &tmp);
994 if (!gst_segment_do_seek (&tmp, rate,
995 format, flags, start_type, start, stop_type, stop, NULL)) {
996 /* Invalid seek requested, ignore it */
997 SPLITMUX_SRC_UNLOCK (splitmux);
1000 position = tmp.position;
1002 GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1003 GST_SEGMENT_FORMAT, &tmp);
1005 GST_DEBUG_OBJECT (splitmux,
1006 "Handling flushing seek. Sending flush start");
1008 /* Send flush_start */
1009 gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1011 /* Stop all parts, which will work because of the flush */
1012 SPLITMUX_SRC_PADS_LOCK (splitmux);
1013 SPLITMUX_SRC_UNLOCK (splitmux);
1014 for (cur = g_list_first (splitmux->pads);
1015 cur != NULL; cur = g_list_next (cur)) {
1016 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1017 GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1018 gst_splitmux_part_reader_deactivate (reader);
1021 /* Shut down pad tasks */
1022 GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1023 for (cur = g_list_first (splitmux->pads);
1024 cur != NULL; cur = g_list_next (cur)) {
1025 GstPad *splitpad = (GstPad *) (cur->data);
1026 gst_pad_pause_task (GST_PAD (splitpad));
1028 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1029 SPLITMUX_SRC_LOCK (splitmux);
1031 /* Send flush stop */
1032 GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1033 gst_splitmux_push_flush_stop (splitmux, seqnum);
1035 /* Everything is stopped, so update the play_segment */
1036 gst_segment_copy_into (&tmp, &splitmux->play_segment);
1038 /* Work out where to start from now */
1039 for (i = 0; i < splitmux->num_parts; i++) {
1040 GstSplitMuxPartReader *reader = splitmux->parts[i];
1041 GstClockTime part_end =
1042 gst_splitmux_part_reader_get_end_offset (reader);
1044 if (part_end > position)
1047 if (i == splitmux->num_parts)
1048 i = splitmux->num_parts - 1;
1051 gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1053 GST_DEBUG_OBJECT (splitmux,
1054 "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1055 GST_TIME_FORMAT, GST_TIME_ARGS (position),
1056 i, GST_TIME_ARGS (position - part_start));
1058 gst_splitmux_src_activate_part (splitmux, i);
1061 SPLITMUX_SRC_UNLOCK (splitmux);
1067 gst_event_unref (event);
1073 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1075 /* Query handler for source pads. Proxy queries into the child
1078 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1079 gboolean ret = FALSE;
1081 GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1082 " on %" GST_PTR_FORMAT, query, pad);
1083 switch (GST_QUERY_TYPE (query)) {
1084 case GST_QUERY_CAPS:
1085 case GST_QUERY_POSITION:{
1086 GstSplitMuxPartReader *part;
1087 SplitMuxSrcPad *anypad;
1089 SPLITMUX_SRC_LOCK (splitmux);
1090 SPLITMUX_SRC_PADS_LOCK (splitmux);
1091 anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1092 part = splitmux->parts[anypad->cur_part];
1093 ret = gst_splitmux_part_reader_src_query (part, pad, query);
1094 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1095 SPLITMUX_SRC_UNLOCK (splitmux);
1098 case GST_QUERY_DURATION:{
1100 gst_query_parse_duration (query, &fmt, NULL);
1101 if (fmt != GST_FORMAT_TIME)
1104 GST_OBJECT_LOCK (splitmux);
1105 if (splitmux->total_duration > 0) {
1106 gst_query_set_duration (query, GST_FORMAT_TIME,
1107 splitmux->total_duration);
1110 GST_OBJECT_UNLOCK (splitmux);
1113 case GST_QUERY_SEEKING:{
1116 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1117 if (format != GST_FORMAT_TIME)
1120 GST_OBJECT_LOCK (splitmux);
1121 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1122 splitmux->total_duration);
1124 GST_OBJECT_UNLOCK (splitmux);
1136 register_splitmuxsrc (GstPlugin * plugin)
1138 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1139 "Split File Demuxing Source");
1141 return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1142 GST_TYPE_SPLITMUX_SRC);