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->pads = NULL;
263 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
265 G_OBJECT_CLASS (parent_class)->dispose (object);
269 gst_splitmux_src_finalize (GObject * object)
271 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
272 g_mutex_clear (&splitmux->lock);
273 g_mutex_clear (&splitmux->pads_lock);
274 g_free (splitmux->location);
276 G_OBJECT_CLASS (parent_class)->finalize (object);
280 gst_splitmux_src_set_property (GObject * object, guint prop_id,
281 const GValue * value, GParamSpec * pspec)
283 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
287 GST_OBJECT_LOCK (splitmux);
288 g_free (splitmux->location);
289 splitmux->location = g_value_dup_string (value);
290 GST_OBJECT_UNLOCK (splitmux);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gst_splitmux_src_get_property (GObject * object, guint prop_id,
301 GValue * value, GParamSpec * pspec)
303 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
307 GST_OBJECT_LOCK (splitmux);
308 g_value_set_string (value, splitmux->location);
309 GST_OBJECT_UNLOCK (splitmux);
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 static GstStateChangeReturn
318 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
320 GstStateChangeReturn ret;
321 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
323 switch (transition) {
324 case GST_STATE_CHANGE_NULL_TO_READY:{
327 case GST_STATE_CHANGE_READY_TO_PAUSED:{
328 if (!gst_splitmux_src_start (splitmux))
329 return GST_STATE_CHANGE_FAILURE;
332 case GST_STATE_CHANGE_PAUSED_TO_READY:
333 case GST_STATE_CHANGE_READY_TO_NULL:
334 /* Make sure the element will shut down */
335 if (!gst_splitmux_src_stop (splitmux))
336 return GST_STATE_CHANGE_FAILURE;
342 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
347 static GstSplitMuxPartReader *
348 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
350 GstSplitMuxPartReader *r;
352 r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
354 g_signal_connect (r, "prepared", (GCallback) gst_splitmux_part_prepared,
357 gst_splitmux_part_reader_set_callbacks (r, splitmux,
358 (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
359 gst_splitmux_part_reader_set_location (r, filename);
365 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
367 GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
375 gst_event_parse_caps (event, &newcaps);
377 GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
378 " and %" GST_PTR_FORMAT, curcaps, newcaps);
383 /* If caps are exactly equal exit early */
384 if (gst_caps_is_equal (curcaps, newcaps)) {
385 gst_caps_unref (curcaps);
389 /* More extensive check, ignore changes in framerate, because
390 * demuxers get that wrong */
391 tmpcaps = gst_caps_copy (newcaps);
392 s = gst_caps_get_structure (tmpcaps, 0);
393 gst_structure_remove_field (s, "framerate");
395 tmpcurcaps = gst_caps_copy (curcaps);
396 gst_caps_unref (curcaps);
397 s = gst_caps_get_structure (tmpcurcaps, 0);
398 gst_structure_remove_field (s, "framerate");
400 /* Now check if these filtered caps are equal */
401 if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
402 GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
406 gst_caps_unref (tmpcaps);
407 gst_caps_unref (tmpcurcaps);
412 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
413 SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
415 switch (GST_EVENT_TYPE (event)) {
416 case GST_EVENT_STREAM_START:{
417 if (splitpad->sent_stream_start)
419 splitpad->sent_stream_start = TRUE;
423 if (gst_splitmux_end_of_part (splitmux, splitpad))
424 // Continuing to next part, drop the EOS
428 case GST_EVENT_SEGMENT:{
431 gst_event_copy_segment (event, &seg);
433 splitpad->segment.position = seg.position;
435 if (splitpad->sent_segment)
436 goto drop_event; /* We already forwarded a segment event */
438 /* Calculate output segment */
439 GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
440 " got seg %" GST_SEGMENT_FORMAT
441 " play seg %" GST_SEGMENT_FORMAT,
442 &splitpad->segment, &seg, &splitmux->play_segment);
444 /* If playing forward, take the stop time from the overall
445 * seg or play_segment */
446 if (splitmux->play_segment.rate > 0.0) {
447 if (splitmux->play_segment.stop != -1)
448 seg.stop = splitmux->play_segment.stop;
450 seg.stop = splitpad->segment.stop;
452 /* Reverse playback from stop time to start time */
453 /* See if an end point was requested in the seek */
454 if (splitmux->play_segment.start != -1) {
455 seg.start = splitmux->play_segment.start;
456 seg.time = splitmux->play_segment.time;
458 seg.start = splitpad->segment.start;
459 seg.time = splitpad->segment.time;
463 GST_INFO_OBJECT (splitpad,
464 "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
466 gst_event_unref (event);
467 event = gst_event_new_segment (&seg);
468 splitpad->sent_segment = TRUE;
471 case GST_EVENT_CAPS:{
472 if (!gst_splitmux_check_new_caps (splitpad, event))
474 splitpad->sent_caps = TRUE;
481 gst_pad_push_event ((GstPad *) (splitpad), event);
484 gst_event_unref (event);
489 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
490 SplitMuxSrcPad * splitpad, GstBuffer * buf)
494 if (splitpad->clear_next_discont) {
495 GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
496 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
497 splitpad->clear_next_discont = FALSE;
499 if (splitpad->set_next_discont) {
500 GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
501 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
502 splitpad->set_next_discont = FALSE;
505 ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
507 GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
512 gst_splitmux_pad_loop (GstPad * pad)
514 /* Get one event/buffer from the associated part and push */
515 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
516 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
517 GstDataQueueItem *item = NULL;
518 GstSplitMuxPartReader *reader = splitpad->reader;
522 GST_OBJECT_LOCK (splitpad);
523 if (splitpad->part_pad == NULL) {
524 GST_OBJECT_UNLOCK (splitpad);
527 part_pad = gst_object_ref (splitpad->part_pad);
528 GST_OBJECT_UNLOCK (splitpad);
530 GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
531 " pad %" GST_PTR_FORMAT, reader, part_pad);
532 ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
533 if (ret == GST_FLOW_ERROR)
535 if (ret == GST_FLOW_FLUSHING || item == NULL)
538 GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
541 if (GST_IS_EVENT (item->object)) {
542 GstEvent *event = (GstEvent *) (item->object);
543 gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
545 GstBuffer *buf = (GstBuffer *) (item->object);
546 GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
547 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
548 /* Stop immediately on error or flushing */
549 GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
550 gst_pad_pause_task (pad);
551 if (ret <= GST_FLOW_EOS) {
552 const gchar *reason = gst_flow_get_name (ret);
553 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED,
554 (_("Internal data flow error.")),
555 ("streaming task paused, reason %s (%d)", reason, ret));
559 g_slice_free (GstDataQueueItem, item);
561 gst_object_unref (part_pad);
562 gst_object_unref (splitmux);
567 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
568 ("Error reading part file %s", GST_STR_NULL (reader->path)));
570 gst_pad_pause_task (pad);
571 gst_object_unref (part_pad);
572 gst_object_unref (splitmux);
577 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part)
581 GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
583 splitmux->cur_part = part;
584 if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
585 &splitmux->play_segment))
588 SPLITMUX_SRC_PADS_LOCK (splitmux);
589 for (cur = g_list_first (splitmux->pads);
590 cur != NULL; cur = g_list_next (cur)) {
591 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
592 splitpad->cur_part = part;
593 splitpad->reader = splitmux->parts[splitpad->cur_part];
594 if (splitpad->part_pad)
595 gst_object_unref (splitpad->part_pad);
597 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
598 (GstPad *) (splitpad));
600 /* Make sure we start with a DISCONT */
601 splitpad->set_next_discont = TRUE;
602 splitpad->clear_next_discont = FALSE;
604 gst_pad_start_task (GST_PAD (splitpad),
605 (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
607 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
613 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
615 gboolean ret = FALSE;
617 gchar *basename = NULL;
618 gchar *dirname = NULL;
620 GstClockTime next_offset = 0;
622 GstClockTime total_duration = 0;
624 GST_DEBUG_OBJECT (splitmux, "Starting");
626 g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
628 if (files == NULL || *files == NULL) {
629 GST_OBJECT_LOCK (splitmux);
630 if (splitmux->location != NULL && splitmux->location[0] != '\0') {
631 basename = g_path_get_basename (splitmux->location);
632 dirname = g_path_get_dirname (splitmux->location);
634 GST_OBJECT_UNLOCK (splitmux);
637 files = gst_split_util_find_files (dirname, basename, &err);
639 if (files == NULL || *files == NULL)
643 SPLITMUX_SRC_LOCK (splitmux);
644 splitmux->pads_complete = FALSE;
645 splitmux->running = TRUE;
646 SPLITMUX_SRC_UNLOCK (splitmux);
648 splitmux->num_parts = g_strv_length (files);
650 splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
652 for (i = 0; i < splitmux->num_parts; i++) {
653 splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
654 if (splitmux->parts[i] == NULL)
657 /* Figure out the next offset - the smallest one */
658 gst_splitmux_part_reader_set_start_offset (splitmux->parts[i], next_offset);
659 if (!gst_splitmux_part_reader_prepare (splitmux->parts[i])) {
660 GST_WARNING_OBJECT (splitmux,
661 "Failed to prepare file part %s. Cannot play past there.", files[i]);
662 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
663 ("Failed to prepare file part %s. Cannot play past there.",
665 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
666 g_object_unref (splitmux->parts[i]);
667 splitmux->parts[i] = NULL;
671 /* Extend our total duration to cover this part */
674 gst_splitmux_part_reader_get_duration (splitmux->parts[i]);
675 splitmux->play_segment.duration = total_duration;
677 next_offset = gst_splitmux_part_reader_get_end_offset (splitmux->parts[i]);
680 /* Update total_duration state variable */
681 GST_OBJECT_LOCK (splitmux);
682 splitmux->total_duration = total_duration;
683 GST_OBJECT_UNLOCK (splitmux);
685 /* Store how many parts we actually created */
686 splitmux->num_parts = i;
688 if (splitmux->num_parts < 1)
691 /* All done preparing, activate the first part */
692 GST_INFO_OBJECT (splitmux,
693 "All parts prepared. Total duration %" GST_TIME_FORMAT
694 " Activating first part", GST_TIME_ARGS (total_duration));
695 ret = gst_splitmux_src_activate_part (splitmux, 0);
697 goto failed_first_part;
710 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
711 ("Failed to find files in '%s' for pattern '%s'",
712 GST_STR_NULL (dirname), GST_STR_NULL (basename)));
717 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
718 ("Failed to open any files for reading"));
723 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
724 ("Failed to activate first part for playback"));
730 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
734 GList *cur, *pads_list;
736 SPLITMUX_SRC_LOCK (splitmux);
737 if (!splitmux->running)
740 GST_DEBUG_OBJECT (splitmux, "Stopping");
742 /* Stop and destroy all parts */
743 for (i = 0; i < splitmux->num_parts; i++) {
744 if (splitmux->parts[i] == NULL)
746 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
747 g_object_unref (splitmux->parts[i]);
748 splitmux->parts[i] = NULL;
751 SPLITMUX_SRC_PADS_LOCK (splitmux);
752 pads_list = splitmux->pads;
753 splitmux->pads = NULL;
754 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
756 for (cur = g_list_first (pads_list); cur != NULL; cur = g_list_next (cur)) {
757 SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
758 gst_pad_stop_task (GST_PAD (tmp));
759 gst_element_remove_pad (GST_ELEMENT (splitmux), GST_PAD (tmp));
761 g_list_free (pads_list);
763 g_free (splitmux->parts);
764 splitmux->parts = NULL;
765 splitmux->num_parts = 0;
766 splitmux->running = FALSE;
767 splitmux->total_duration = GST_CLOCK_TIME_NONE;
768 /* Reset playback segment */
769 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
771 SPLITMUX_SRC_UNLOCK (splitmux);
777 GstSplitMuxSrc *splitmux;
778 SplitMuxSrcPad *splitpad;
782 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
784 SplitMuxAndPad *splitmux_and_pad;
785 GstSplitMuxSrc *splitmux;
786 SplitMuxSrcPad *splitpad;
788 splitmux_and_pad = user_data;
789 splitmux = splitmux_and_pad->splitmux;
790 splitpad = splitmux_and_pad->splitpad;
792 GST_DEBUG_OBJECT (splitpad, "handle sticky event %" GST_PTR_FORMAT, *event);
793 gst_event_ref (*event);
794 gst_splitmux_handle_event (splitmux, splitpad, pad, *event);
800 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
801 GstSplitMuxSrc * splitmux)
804 gchar *pad_name = gst_pad_get_name (pad);
805 GstPad *target = NULL;
806 gboolean is_new_pad = FALSE;
808 SPLITMUX_SRC_LOCK (splitmux);
809 SPLITMUX_SRC_PADS_LOCK (splitmux);
810 for (cur = g_list_first (splitmux->pads);
811 cur != NULL; cur = g_list_next (cur)) {
812 GstPad *tmp = (GstPad *) (cur->data);
813 if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
819 if (target == NULL && !splitmux->pads_complete) {
820 SplitMuxAndPad splitmux_and_pad;
822 /* No pad found, create one */
823 target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
824 "name", pad_name, "direction", GST_PAD_SRC, NULL);
825 splitmux->pads = g_list_prepend (splitmux->pads, target);
827 gst_pad_set_active (target, TRUE);
829 splitmux_and_pad.splitmux = splitmux;
830 splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
831 gst_pad_sticky_events_foreach (pad, handle_sticky_events,
835 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
836 SPLITMUX_SRC_UNLOCK (splitmux);
844 gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
849 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
850 ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
856 gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
857 GstSplitMuxSrc * splitmux)
859 gboolean need_no_more_pads;
861 GST_LOG_OBJECT (splitmux, "Part %" GST_PTR_FORMAT " prepared", reader);
862 SPLITMUX_SRC_LOCK (splitmux);
863 need_no_more_pads = !splitmux->pads_complete;
864 splitmux->pads_complete = TRUE;
865 SPLITMUX_SRC_UNLOCK (splitmux);
867 if (need_no_more_pads) {
868 GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
869 gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
874 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
880 gst_event_set_seqnum (e, seqnum);
882 SPLITMUX_SRC_PADS_LOCK (splitmux);
883 for (cur = g_list_first (splitmux->pads);
884 cur != NULL; cur = g_list_next (cur)) {
885 GstPad *pad = GST_PAD_CAST (cur->data);
887 gst_pad_push_event (pad, e);
889 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
895 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
897 GstEvent *e = gst_event_new_flush_stop (TRUE);
901 gst_event_set_seqnum (e, seqnum);
903 SPLITMUX_SRC_PADS_LOCK (splitmux);
904 for (cur = g_list_first (splitmux->pads);
905 cur != NULL; cur = g_list_next (cur)) {
906 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
909 gst_pad_push_event (GST_PAD_CAST (target), e);
910 target->sent_caps = FALSE;
911 target->sent_stream_start = FALSE;
912 target->sent_segment = FALSE;
914 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
919 /* Callback for when a part finishes and we need to move to the next */
921 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
924 gint cur_part = splitpad->cur_part;
925 gboolean res = FALSE;
927 if (splitmux->play_segment.rate >= 0.0) {
928 if (cur_part + 1 < splitmux->num_parts)
929 next_part = cur_part + 1;
930 /* Make sure the transition is seamless */
931 splitpad->set_next_discont = FALSE;
932 splitpad->clear_next_discont = TRUE;
934 /* Reverse play - move to previous segment */
936 next_part = cur_part - 1;
937 /* Non-seamless transition in reverse */
938 splitpad->set_next_discont = TRUE;
939 splitpad->clear_next_discont = FALSE;
943 SPLITMUX_SRC_LOCK (splitmux);
945 /* If all pads are done with this part, deactivate it */
946 if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
947 gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
949 if (next_part != -1) {
950 GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
951 " moving to part %d", splitpad, next_part);
952 splitpad->cur_part = next_part;
953 splitpad->reader = splitmux->parts[splitpad->cur_part];
954 if (splitpad->part_pad)
955 gst_object_unref (splitpad->part_pad);
957 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
958 (GstPad *) (splitpad));
960 if (splitmux->cur_part != next_part) {
962 /* If moving backward into a new part, set stop
963 * to -1 to ensure we play the entire file - workaround
964 * a bug in qtdemux that misses bits at the end */
965 gst_segment_copy_into (&splitmux->play_segment, &tmp);
969 /* This is the first pad to move to the new part, activate it */
970 splitmux->cur_part = next_part;
971 GST_DEBUG_OBJECT (splitpad,
972 "First pad to change part. Activating part %d with seg %"
973 GST_SEGMENT_FORMAT, next_part, &tmp);
974 if (!gst_splitmux_part_reader_activate (splitpad->reader, &tmp))
980 SPLITMUX_SRC_UNLOCK (splitmux);
983 SPLITMUX_SRC_UNLOCK (splitmux);
984 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
985 ("Failed to activate part %d", splitmux->cur_part));
989 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
992 splitmux_src_pad_constructed (GObject * pad)
994 gst_pad_set_event_function (GST_PAD (pad),
995 GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
996 gst_pad_set_query_function (GST_PAD (pad),
997 GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
999 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1003 gst_splitmux_src_pad_dispose (GObject * object)
1005 SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1007 GST_OBJECT_LOCK (pad);
1008 if (pad->part_pad) {
1009 gst_object_unref (pad->part_pad);
1010 pad->part_pad = NULL;
1012 GST_OBJECT_UNLOCK (pad);
1014 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1018 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1020 GObjectClass *gobject_klass = (GObjectClass *) (klass);
1022 gobject_klass->constructed = splitmux_src_pad_constructed;
1023 gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1027 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1031 /* Event handler for source pads. Proxy events into the child
1035 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1037 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1038 gboolean ret = FALSE;
1040 GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1041 " on %" GST_PTR_FORMAT, event, pad);
1043 switch (GST_EVENT_TYPE (event)) {
1044 case GST_EVENT_SEEK:{
1048 GstSeekType start_type, stop_type;
1052 GstClockTime part_start, position;
1056 gst_event_parse_seek (event, &rate, &format, &flags,
1057 &start_type, &start, &stop_type, &stop);
1059 if (format != GST_FORMAT_TIME) {
1060 GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1063 /* FIXME: Support non-flushing seeks, which might never wake up */
1064 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1065 GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
1068 seqnum = gst_event_get_seqnum (event);
1070 SPLITMUX_SRC_LOCK (splitmux);
1071 if (!splitmux->running || splitmux->num_parts < 1) {
1072 /* Not started yet */
1073 SPLITMUX_SRC_UNLOCK (splitmux);
1077 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1079 if (!gst_segment_do_seek (&tmp, rate,
1080 format, flags, start_type, start, stop_type, stop, NULL)) {
1081 /* Invalid seek requested, ignore it */
1082 SPLITMUX_SRC_UNLOCK (splitmux);
1085 position = tmp.position;
1087 GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1088 GST_SEGMENT_FORMAT, &tmp);
1090 GST_DEBUG_OBJECT (splitmux,
1091 "Handling flushing seek. Sending flush start");
1093 /* Send flush_start */
1094 gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1096 /* Stop all parts, which will work because of the flush */
1097 SPLITMUX_SRC_PADS_LOCK (splitmux);
1098 SPLITMUX_SRC_UNLOCK (splitmux);
1099 for (cur = g_list_first (splitmux->pads);
1100 cur != NULL; cur = g_list_next (cur)) {
1101 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1102 GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1103 gst_splitmux_part_reader_deactivate (reader);
1106 /* Shut down pad tasks */
1107 GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1108 for (cur = g_list_first (splitmux->pads);
1109 cur != NULL; cur = g_list_next (cur)) {
1110 GstPad *splitpad = (GstPad *) (cur->data);
1111 gst_pad_pause_task (GST_PAD (splitpad));
1113 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1114 SPLITMUX_SRC_LOCK (splitmux);
1116 /* Send flush stop */
1117 GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1118 gst_splitmux_push_flush_stop (splitmux, seqnum);
1120 /* Everything is stopped, so update the play_segment */
1121 gst_segment_copy_into (&tmp, &splitmux->play_segment);
1123 /* Work out where to start from now */
1124 for (i = 0; i < splitmux->num_parts; i++) {
1125 GstSplitMuxPartReader *reader = splitmux->parts[i];
1126 GstClockTime part_end =
1127 gst_splitmux_part_reader_get_end_offset (reader);
1129 if (part_end > position)
1132 if (i == splitmux->num_parts)
1133 i = splitmux->num_parts - 1;
1136 gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1138 GST_DEBUG_OBJECT (splitmux,
1139 "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1140 GST_TIME_FORMAT, GST_TIME_ARGS (position),
1141 i, GST_TIME_ARGS (position - part_start));
1143 ret = gst_splitmux_src_activate_part (splitmux, i);
1144 SPLITMUX_SRC_UNLOCK (splitmux);
1150 gst_event_unref (event);
1156 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1158 /* Query handler for source pads. Proxy queries into the child
1161 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1162 gboolean ret = FALSE;
1164 GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1165 " on %" GST_PTR_FORMAT, query, pad);
1166 switch (GST_QUERY_TYPE (query)) {
1167 case GST_QUERY_CAPS:
1168 case GST_QUERY_POSITION:{
1169 GstSplitMuxPartReader *part;
1170 SplitMuxSrcPad *anypad;
1172 SPLITMUX_SRC_LOCK (splitmux);
1173 SPLITMUX_SRC_PADS_LOCK (splitmux);
1174 anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1175 part = splitmux->parts[anypad->cur_part];
1176 ret = gst_splitmux_part_reader_src_query (part, pad, query);
1177 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1178 SPLITMUX_SRC_UNLOCK (splitmux);
1181 case GST_QUERY_DURATION:{
1183 gst_query_parse_duration (query, &fmt, NULL);
1184 if (fmt != GST_FORMAT_TIME)
1187 GST_OBJECT_LOCK (splitmux);
1188 if (splitmux->total_duration > 0) {
1189 gst_query_set_duration (query, GST_FORMAT_TIME,
1190 splitmux->total_duration);
1193 GST_OBJECT_UNLOCK (splitmux);
1196 case GST_QUERY_SEEKING:{
1199 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1200 if (format != GST_FORMAT_TIME)
1203 GST_OBJECT_LOCK (splitmux);
1204 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1205 splitmux->total_duration);
1207 GST_OBJECT_UNLOCK (splitmux);
1219 register_splitmuxsrc (GstPlugin * plugin)
1221 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1222 "Split File Demuxing Source");
1224 return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1225 GST_TYPE_SPLITMUX_SRC);