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 gboolean gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux,
112 SplitMuxSrcPad * pad);
113 static gboolean gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad,
115 static gboolean gst_splitmux_src_prepare_next_part (GstSplitMuxSrc * splitmux);
116 static gboolean gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux,
117 guint part, GstSeekFlags extra_flags);
120 G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, splitmux_src_uri_handler_init);
121 #define gst_splitmux_src_parent_class parent_class
123 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSrc, gst_splitmux_src, GST_TYPE_BIN, 0,
127 splitmux_src_uri_get_type (GType type)
132 static const gchar *const *
133 splitmux_src_uri_get_protocols (GType type)
135 static const gchar *protocols[] = { "splitmux", NULL };
141 splitmux_src_uri_get_uri (GstURIHandler * handler)
143 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
146 GST_OBJECT_LOCK (splitmux);
147 if (splitmux->location)
148 ret = g_strdup_printf ("splitmux://%s", splitmux->location);
149 GST_OBJECT_UNLOCK (splitmux);
154 splitmux_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
157 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
158 gchar *protocol, *location;
160 protocol = gst_uri_get_protocol (uri);
161 if (protocol == NULL || !g_str_equal (protocol, "splitmux"))
165 location = gst_uri_get_location (uri);
166 GST_OBJECT_LOCK (splitmux);
167 g_free (splitmux->location);
168 splitmux->location = location;
169 GST_OBJECT_UNLOCK (splitmux);
175 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
176 ("Error parsing uri %s", uri));
177 g_set_error_literal (err, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
178 "Could not parse splitmux URI");
183 splitmux_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
185 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) (g_iface);
187 iface->get_type = splitmux_src_uri_get_type;
188 iface->get_protocols = splitmux_src_uri_get_protocols;
189 iface->set_uri = splitmux_src_uri_set_uri;
190 iface->get_uri = splitmux_src_uri_get_uri;
195 gst_splitmux_src_class_init (GstSplitMuxSrcClass * klass)
197 GObjectClass *gobject_class = (GObjectClass *) klass;
198 GstElementClass *gstelement_class = (GstElementClass *) klass;
200 gobject_class->set_property = gst_splitmux_src_set_property;
201 gobject_class->get_property = gst_splitmux_src_get_property;
202 gobject_class->dispose = gst_splitmux_src_dispose;
203 gobject_class->finalize = gst_splitmux_src_finalize;
205 gst_element_class_set_static_metadata (gstelement_class,
206 "Split File Demuxing Bin", "Generic/Bin/Demuxer",
207 "Source that reads a set of files created by splitmuxsink",
208 "Jan Schmidt <jan@centricular.com>");
210 gst_element_class_add_static_pad_template (gstelement_class,
211 &video_src_template);
212 gst_element_class_add_static_pad_template (gstelement_class,
213 &audio_src_template);
214 gst_element_class_add_static_pad_template (gstelement_class,
215 &subtitle_src_template);
217 gstelement_class->change_state =
218 GST_DEBUG_FUNCPTR (gst_splitmux_src_change_state);
220 g_object_class_install_property (gobject_class, PROP_LOCATION,
221 g_param_spec_string ("location", "File Input Pattern",
222 "Glob pattern for the location of the files to read", NULL,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 * GstSplitMuxSrc::format-location:
227 * @splitmux: the #GstSplitMuxSrc
229 * Returns: A NULL-terminated sorted array of strings containing the
230 * filenames of the input files. The array will be freed internally
235 signals[SIGNAL_FORMAT_LOCATION] =
236 g_signal_new ("format-location", G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_STRV, 0);
241 gst_splitmux_src_init (GstSplitMuxSrc * splitmux)
243 g_mutex_init (&splitmux->lock);
244 g_mutex_init (&splitmux->pads_lock);
245 splitmux->total_duration = GST_CLOCK_TIME_NONE;
246 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
250 gst_splitmux_src_dispose (GObject * object)
252 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
255 SPLITMUX_SRC_PADS_LOCK (splitmux);
257 for (cur = g_list_first (splitmux->pads);
258 cur != NULL; cur = g_list_next (cur)) {
259 GstPad *pad = GST_PAD (cur->data);
260 gst_element_remove_pad (GST_ELEMENT (splitmux), pad);
262 g_list_free (splitmux->pads);
263 splitmux->n_pads = 0;
264 splitmux->pads = NULL;
265 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
267 G_OBJECT_CLASS (parent_class)->dispose (object);
271 gst_splitmux_src_finalize (GObject * object)
273 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
274 g_mutex_clear (&splitmux->lock);
275 g_mutex_clear (&splitmux->pads_lock);
276 g_free (splitmux->location);
278 G_OBJECT_CLASS (parent_class)->finalize (object);
282 gst_splitmux_src_set_property (GObject * object, guint prop_id,
283 const GValue * value, GParamSpec * pspec)
285 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
289 GST_OBJECT_LOCK (splitmux);
290 g_free (splitmux->location);
291 splitmux->location = g_value_dup_string (value);
292 GST_OBJECT_UNLOCK (splitmux);
296 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
302 gst_splitmux_src_get_property (GObject * object, guint prop_id,
303 GValue * value, GParamSpec * pspec)
305 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
309 GST_OBJECT_LOCK (splitmux);
310 g_value_set_string (value, splitmux->location);
311 GST_OBJECT_UNLOCK (splitmux);
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
320 do_async_start (GstSplitMuxSrc * splitmux)
324 GST_STATE_LOCK (splitmux);
325 splitmux->async_pending = TRUE;
327 message = gst_message_new_async_start (GST_OBJECT_CAST (splitmux));
328 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (splitmux),
330 GST_STATE_UNLOCK (splitmux);
334 do_async_done (GstSplitMuxSrc * splitmux)
338 GST_STATE_LOCK (splitmux);
339 if (splitmux->async_pending) {
341 gst_message_new_async_done (GST_OBJECT_CAST (splitmux),
342 GST_CLOCK_TIME_NONE);
343 GST_BIN_CLASS (parent_class)->handle_message (GST_BIN_CAST (splitmux),
346 splitmux->async_pending = FALSE;
348 GST_STATE_UNLOCK (splitmux);
351 static GstStateChangeReturn
352 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
354 GstStateChangeReturn ret;
355 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
357 switch (transition) {
358 case GST_STATE_CHANGE_NULL_TO_READY:{
361 case GST_STATE_CHANGE_READY_TO_PAUSED:{
362 do_async_start (splitmux);
364 if (!gst_splitmux_src_start (splitmux)) {
365 do_async_done (splitmux);
366 return GST_STATE_CHANGE_FAILURE;
370 case GST_STATE_CHANGE_PAUSED_TO_READY:
371 case GST_STATE_CHANGE_READY_TO_NULL:
372 /* Make sure the element will shut down */
373 if (!gst_splitmux_src_stop (splitmux))
374 return GST_STATE_CHANGE_FAILURE;
380 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
381 if (ret == GST_STATE_CHANGE_FAILURE) {
382 do_async_done (splitmux);
386 switch (transition) {
387 case GST_STATE_CHANGE_READY_TO_PAUSED:
388 ret = GST_STATE_CHANGE_ASYNC;
399 gst_splitmux_src_activate_first_part (GstSplitMuxSrc * splitmux)
401 if (!gst_splitmux_src_activate_part (splitmux, 0, GST_SEEK_FLAG_NONE)) {
402 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
403 ("Failed to activate first part for playback"));
407 static GstBusSyncReply
408 gst_splitmux_part_bus_handler (GstBus * bus, GstMessage * msg,
411 GstSplitMuxSrc *splitmux = user_data;
413 switch (GST_MESSAGE_TYPE (msg)) {
414 case GST_MESSAGE_ASYNC_DONE:{
415 guint idx = splitmux->num_prepared_parts;
416 gboolean need_no_more_pads;
418 if (idx >= splitmux->num_parts) {
419 /* Shouldn't really happen! */
420 do_async_done (splitmux);
421 g_warn_if_reached ();
425 GST_DEBUG_OBJECT (splitmux, "Prepared file part %s (%u)",
426 splitmux->parts[idx]->path, idx);
428 /* signal no-more-pads as we have all pads at this point now */
429 SPLITMUX_SRC_LOCK (splitmux);
430 need_no_more_pads = !splitmux->pads_complete;
431 splitmux->pads_complete = TRUE;
432 SPLITMUX_SRC_UNLOCK (splitmux);
434 if (need_no_more_pads) {
435 GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
436 gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
439 /* Extend our total duration to cover this part */
440 GST_OBJECT_LOCK (splitmux);
441 splitmux->total_duration +=
442 gst_splitmux_part_reader_get_duration (splitmux->parts[idx]);
443 splitmux->play_segment.duration = splitmux->total_duration;
444 GST_OBJECT_UNLOCK (splitmux);
446 splitmux->end_offset =
447 gst_splitmux_part_reader_get_end_offset (splitmux->parts[idx]);
449 GST_DEBUG_OBJECT (splitmux,
450 "Duration %" GST_TIME_FORMAT ", total duration now: %" GST_TIME_FORMAT
451 " and end offset %" GST_TIME_FORMAT,
452 GST_TIME_ARGS (gst_splitmux_part_reader_get_duration (splitmux->parts
453 [idx])), GST_TIME_ARGS (splitmux->total_duration),
454 GST_TIME_ARGS (splitmux->end_offset));
456 splitmux->num_prepared_parts++;
458 /* If we're done or preparing the next part fails, finish here */
459 if (splitmux->num_prepared_parts >= splitmux->num_parts
460 || !gst_splitmux_src_prepare_next_part (splitmux)) {
461 /* Store how many parts we actually prepared in the end */
462 splitmux->num_parts = splitmux->num_prepared_parts;
463 do_async_done (splitmux);
465 /* All done preparing, activate the first part */
466 GST_INFO_OBJECT (splitmux,
467 "All parts prepared. Total duration %" GST_TIME_FORMAT
468 " Activating first part", GST_TIME_ARGS (splitmux->total_duration));
469 gst_element_call_async (GST_ELEMENT_CAST (splitmux),
470 (GstElementCallAsyncFunc) gst_splitmux_src_activate_first_part,
476 case GST_MESSAGE_ERROR:{
477 GST_ERROR_OBJECT (splitmux,
478 "Got error message from part %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT,
479 GST_MESSAGE_SRC (msg), msg);
480 if (splitmux->num_prepared_parts < splitmux->num_parts) {
481 guint idx = splitmux->num_prepared_parts;
484 GST_ERROR_OBJECT (splitmux,
485 "Failed to prepare first file part %s for playback",
486 splitmux->parts[idx]->path);
487 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
488 ("Failed to prepare first file part %s for playback",
489 splitmux->parts[idx]->path));
491 GST_WARNING_OBJECT (splitmux,
492 "Failed to prepare file part %s. Cannot play past there.",
493 splitmux->parts[idx]->path);
494 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
495 ("Failed to prepare file part %s. Cannot play past there.",
496 splitmux->parts[idx]->path));
499 /* Store how many parts we actually prepared in the end */
500 splitmux->num_parts = splitmux->num_prepared_parts;
501 do_async_done (splitmux);
504 /* All done preparing, activate the first part */
505 GST_INFO_OBJECT (splitmux,
506 "All parts prepared. Total duration %" GST_TIME_FORMAT
507 " Activating first part",
508 GST_TIME_ARGS (splitmux->total_duration));
509 gst_element_call_async (GST_ELEMENT_CAST (splitmux),
510 (GstElementCallAsyncFunc) gst_splitmux_src_activate_first_part,
514 /* Need to update the message source so that it's part of the element
515 * hierarchy the application would expect */
516 msg = gst_message_copy (msg);
517 gst_object_replace ((GstObject **) & msg->src, (GstObject *) splitmux);
518 gst_element_post_message (GST_ELEMENT_CAST (splitmux), msg);
529 static GstSplitMuxPartReader *
530 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
532 GstSplitMuxPartReader *r;
535 r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
537 gst_splitmux_part_reader_set_callbacks (r, splitmux,
538 (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
539 gst_splitmux_part_reader_set_location (r, filename);
541 bus = gst_element_get_bus (GST_ELEMENT_CAST (r));
542 gst_bus_set_sync_handler (bus, gst_splitmux_part_bus_handler, splitmux, NULL);
543 gst_object_unref (bus);
549 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
551 GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
559 gst_event_parse_caps (event, &newcaps);
561 GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
562 " and %" GST_PTR_FORMAT, curcaps, newcaps);
567 /* If caps are exactly equal exit early */
568 if (gst_caps_is_equal (curcaps, newcaps)) {
569 gst_caps_unref (curcaps);
573 /* More extensive check, ignore changes in framerate, because
574 * demuxers get that wrong */
575 tmpcaps = gst_caps_copy (newcaps);
576 s = gst_caps_get_structure (tmpcaps, 0);
577 gst_structure_remove_field (s, "framerate");
579 tmpcurcaps = gst_caps_copy (curcaps);
580 gst_caps_unref (curcaps);
581 s = gst_caps_get_structure (tmpcurcaps, 0);
582 gst_structure_remove_field (s, "framerate");
584 /* Now check if these filtered caps are equal */
585 if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
586 GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
590 gst_caps_unref (tmpcaps);
591 gst_caps_unref (tmpcurcaps);
596 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
597 SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
599 switch (GST_EVENT_TYPE (event)) {
600 case GST_EVENT_STREAM_START:{
601 if (splitpad->sent_stream_start)
603 splitpad->sent_stream_start = TRUE;
607 if (gst_splitmux_end_of_part (splitmux, splitpad))
608 // Continuing to next part, drop the EOS
610 if (splitmux->segment_seqnum) {
611 event = gst_event_make_writable (event);
612 gst_event_set_seqnum (event, splitmux->segment_seqnum);
616 case GST_EVENT_SEGMENT:{
617 GstClockTime duration;
620 gst_event_copy_segment (event, &seg);
622 splitpad->segment.position = seg.position;
624 if (splitpad->sent_segment)
625 goto drop_event; /* We already forwarded a segment event */
627 /* Calculate output segment */
628 GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
629 " got seg %" GST_SEGMENT_FORMAT
630 " play seg %" GST_SEGMENT_FORMAT,
631 &splitpad->segment, &seg, &splitmux->play_segment);
633 /* If playing forward, take the stop time from the overall
634 * seg or play_segment */
635 if (splitmux->play_segment.rate > 0.0) {
636 if (splitmux->play_segment.stop != -1)
637 seg.stop = splitmux->play_segment.stop;
639 seg.stop = splitpad->segment.stop;
641 /* Reverse playback from stop time to start time */
642 /* See if an end point was requested in the seek */
643 if (splitmux->play_segment.start != -1) {
644 seg.start = splitmux->play_segment.start;
645 seg.time = splitmux->play_segment.time;
647 seg.start = splitpad->segment.start;
648 seg.time = splitpad->segment.time;
652 GST_OBJECT_LOCK (splitmux);
653 duration = splitmux->total_duration;
654 GST_OBJECT_UNLOCK (splitmux);
657 seg.duration = duration;
659 seg.duration = GST_CLOCK_TIME_NONE;
661 GST_INFO_OBJECT (splitpad,
662 "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
664 gst_event_unref (event);
665 event = gst_event_new_segment (&seg);
666 if (splitmux->segment_seqnum)
667 gst_event_set_seqnum (event, splitmux->segment_seqnum);
668 splitpad->sent_segment = TRUE;
671 case GST_EVENT_CAPS:{
672 if (!gst_splitmux_check_new_caps (splitpad, event))
674 splitpad->sent_caps = TRUE;
681 gst_pad_push_event ((GstPad *) (splitpad), event);
684 gst_event_unref (event);
689 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
690 SplitMuxSrcPad * splitpad, GstBuffer * buf)
694 if (splitpad->clear_next_discont) {
695 GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
696 GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
697 splitpad->clear_next_discont = FALSE;
699 if (splitpad->set_next_discont) {
700 GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
701 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
702 splitpad->set_next_discont = FALSE;
705 ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
707 GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
712 count_not_linked (GstSplitMuxSrc * splitmux)
717 for (cur = g_list_first (splitmux->pads);
718 cur != NULL; cur = g_list_next (cur)) {
719 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
720 if (GST_PAD_LAST_FLOW_RETURN (splitpad) == GST_FLOW_NOT_LINKED)
728 gst_splitmux_pad_loop (GstPad * pad)
730 /* Get one event/buffer from the associated part and push */
731 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
732 GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
733 GstDataQueueItem *item = NULL;
734 GstSplitMuxPartReader *reader = splitpad->reader;
738 GST_OBJECT_LOCK (splitpad);
739 if (splitpad->part_pad == NULL) {
740 GST_OBJECT_UNLOCK (splitpad);
743 part_pad = gst_object_ref (splitpad->part_pad);
744 GST_OBJECT_UNLOCK (splitpad);
746 GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
747 " pad %" GST_PTR_FORMAT, reader, part_pad);
748 ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
749 if (ret == GST_FLOW_ERROR)
751 if (ret == GST_FLOW_FLUSHING || item == NULL)
754 GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
757 if (GST_IS_EVENT (item->object)) {
758 GstEvent *event = (GstEvent *) (item->object);
759 gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
761 GstBuffer *buf = (GstBuffer *) (item->object);
762 GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
763 if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
764 /* Stop immediately on error or flushing */
765 GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
766 gst_pad_pause_task (pad);
767 if (ret < GST_FLOW_EOS) {
768 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
769 } else if (ret == GST_FLOW_NOT_LINKED) {
773 /* Only post not-linked if all pads are not-linked */
774 SPLITMUX_SRC_PADS_LOCK (splitmux);
775 n_notlinked = count_not_linked (splitmux);
776 post_error = (splitmux->pads_complete
777 && n_notlinked == splitmux->n_pads);
778 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
781 GST_ELEMENT_FLOW_ERROR (splitmux, ret);
785 g_slice_free (GstDataQueueItem, item);
787 gst_object_unref (part_pad);
788 gst_object_unref (splitmux);
793 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
794 ("Error reading part file %s", GST_STR_NULL (reader->path)));
796 gst_pad_pause_task (pad);
797 gst_object_unref (part_pad);
798 gst_object_unref (splitmux);
803 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part,
804 GstSeekFlags extra_flags)
808 GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
810 splitmux->cur_part = part;
811 if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
812 &splitmux->play_segment, extra_flags))
815 SPLITMUX_SRC_PADS_LOCK (splitmux);
816 for (cur = g_list_first (splitmux->pads);
817 cur != NULL; cur = g_list_next (cur)) {
818 SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
819 splitpad->cur_part = part;
820 splitpad->reader = splitmux->parts[splitpad->cur_part];
821 if (splitpad->part_pad)
822 gst_object_unref (splitpad->part_pad);
824 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
825 (GstPad *) (splitpad));
827 /* Make sure we start with a DISCONT */
828 splitpad->set_next_discont = TRUE;
829 splitpad->clear_next_discont = FALSE;
831 gst_pad_start_task (GST_PAD (splitpad),
832 (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
834 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
840 gst_splitmux_src_prepare_next_part (GstSplitMuxSrc * splitmux)
842 guint idx = splitmux->num_prepared_parts;
844 g_assert (idx < splitmux->num_parts);
846 GST_DEBUG_OBJECT (splitmux, "Preparing file part %s (%u)",
847 splitmux->parts[idx]->path, idx);
849 gst_splitmux_part_reader_set_start_offset (splitmux->parts[idx],
850 splitmux->end_offset);
851 if (!gst_splitmux_part_reader_prepare (splitmux->parts[idx])) {
852 GST_WARNING_OBJECT (splitmux,
853 "Failed to prepare file part %s. Cannot play past there.",
854 splitmux->parts[idx]->path);
855 GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
856 ("Failed to prepare file part %s. Cannot play past there.",
857 splitmux->parts[idx]->path));
858 gst_splitmux_part_reader_unprepare (splitmux->parts[idx]);
859 g_object_unref (splitmux->parts[idx]);
860 splitmux->parts[idx] = NULL;
868 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
870 gboolean ret = FALSE;
872 gchar *basename = NULL;
873 gchar *dirname = NULL;
877 GST_DEBUG_OBJECT (splitmux, "Starting");
879 g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
881 if (files == NULL || *files == NULL) {
882 GST_OBJECT_LOCK (splitmux);
883 if (splitmux->location != NULL && splitmux->location[0] != '\0') {
884 basename = g_path_get_basename (splitmux->location);
885 dirname = g_path_get_dirname (splitmux->location);
887 GST_OBJECT_UNLOCK (splitmux);
890 files = gst_split_util_find_files (dirname, basename, &err);
892 if (files == NULL || *files == NULL)
896 SPLITMUX_SRC_LOCK (splitmux);
897 splitmux->pads_complete = FALSE;
898 splitmux->running = TRUE;
899 SPLITMUX_SRC_UNLOCK (splitmux);
901 splitmux->num_parts = g_strv_length (files);
903 splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
905 /* Create all part pipelines */
906 for (i = 0; i < splitmux->num_parts; i++) {
907 splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
908 if (splitmux->parts[i] == NULL)
912 /* Store how many parts we actually created */
913 splitmux->num_created_parts = splitmux->num_parts = i;
914 splitmux->num_prepared_parts = 0;
916 /* Update total_duration state variable */
917 GST_OBJECT_LOCK (splitmux);
918 splitmux->total_duration = 0;
919 splitmux->end_offset = 0;
920 GST_OBJECT_UNLOCK (splitmux);
922 /* Then start the first: it will asynchronously go to PAUSED
923 * or error out and then we can proceed with the next one
925 if (!gst_splitmux_src_prepare_next_part (splitmux) || splitmux->num_parts < 1)
928 /* All good now: we have to wait for all parts to be asynchronously
929 * prepared to know the total duration we can play */
944 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
945 ("Failed to find files in '%s' for pattern '%s'",
946 GST_STR_NULL (dirname), GST_STR_NULL (basename)));
951 GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
952 ("Failed to open any files for reading"));
958 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
962 GList *cur, *pads_list;
964 SPLITMUX_SRC_LOCK (splitmux);
965 if (!splitmux->running)
968 GST_DEBUG_OBJECT (splitmux, "Stopping");
970 /* Stop and destroy all parts */
971 for (i = 0; i < splitmux->num_created_parts; i++) {
972 if (splitmux->parts[i] == NULL)
974 gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
975 g_object_unref (splitmux->parts[i]);
976 splitmux->parts[i] = NULL;
979 SPLITMUX_SRC_PADS_LOCK (splitmux);
980 pads_list = splitmux->pads;
981 splitmux->pads = NULL;
982 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
984 SPLITMUX_SRC_UNLOCK (splitmux);
985 for (cur = g_list_first (pads_list); cur != NULL; cur = g_list_next (cur)) {
986 SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
987 gst_pad_stop_task (GST_PAD (tmp));
988 gst_element_remove_pad (GST_ELEMENT (splitmux), GST_PAD (tmp));
990 g_list_free (pads_list);
991 SPLITMUX_SRC_LOCK (splitmux);
993 g_free (splitmux->parts);
994 splitmux->parts = NULL;
995 splitmux->num_parts = 0;
996 splitmux->num_prepared_parts = 0;
997 splitmux->num_created_parts = 0;
998 splitmux->running = FALSE;
999 splitmux->total_duration = GST_CLOCK_TIME_NONE;
1000 /* Reset playback segment */
1001 gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
1003 SPLITMUX_SRC_UNLOCK (splitmux);
1009 GstSplitMuxSrc *splitmux;
1010 SplitMuxSrcPad *splitpad;
1014 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
1016 SplitMuxAndPad *splitmux_and_pad;
1017 GstSplitMuxSrc *splitmux;
1018 SplitMuxSrcPad *splitpad;
1020 splitmux_and_pad = user_data;
1021 splitmux = splitmux_and_pad->splitmux;
1022 splitpad = splitmux_and_pad->splitpad;
1024 GST_DEBUG_OBJECT (splitpad, "handle sticky event %" GST_PTR_FORMAT, *event);
1025 gst_event_ref (*event);
1026 gst_splitmux_handle_event (splitmux, splitpad, pad, *event);
1032 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
1033 GstSplitMuxSrc * splitmux)
1036 gchar *pad_name = gst_pad_get_name (pad);
1037 GstPad *target = NULL;
1038 gboolean is_new_pad = FALSE;
1040 SPLITMUX_SRC_LOCK (splitmux);
1041 SPLITMUX_SRC_PADS_LOCK (splitmux);
1042 for (cur = g_list_first (splitmux->pads);
1043 cur != NULL; cur = g_list_next (cur)) {
1044 GstPad *tmp = (GstPad *) (cur->data);
1045 if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
1051 if (target == NULL && !splitmux->pads_complete) {
1052 SplitMuxAndPad splitmux_and_pad;
1054 /* No pad found, create one */
1055 target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
1056 "name", pad_name, "direction", GST_PAD_SRC, NULL);
1057 splitmux->pads = g_list_prepend (splitmux->pads, target);
1060 gst_pad_set_active (target, TRUE);
1062 splitmux_and_pad.splitmux = splitmux;
1063 splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
1064 gst_pad_sticky_events_foreach (pad, handle_sticky_events,
1068 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1069 SPLITMUX_SRC_UNLOCK (splitmux);
1077 gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
1082 GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
1083 ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
1089 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
1095 e = gst_event_make_writable (e);
1096 gst_event_set_seqnum (e, seqnum);
1099 SPLITMUX_SRC_PADS_LOCK (splitmux);
1100 for (cur = g_list_first (splitmux->pads);
1101 cur != NULL; cur = g_list_next (cur)) {
1102 GstPad *pad = GST_PAD_CAST (cur->data);
1104 gst_pad_push_event (pad, e);
1106 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1108 gst_event_unref (e);
1112 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
1114 GstEvent *e = gst_event_new_flush_stop (TRUE);
1118 e = gst_event_make_writable (e);
1119 gst_event_set_seqnum (e, seqnum);
1122 SPLITMUX_SRC_PADS_LOCK (splitmux);
1123 for (cur = g_list_first (splitmux->pads);
1124 cur != NULL; cur = g_list_next (cur)) {
1125 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1128 gst_pad_push_event (GST_PAD_CAST (target), e);
1129 target->sent_caps = FALSE;
1130 target->sent_stream_start = FALSE;
1131 target->sent_segment = FALSE;
1133 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1135 gst_event_unref (e);
1138 /* Callback for when a part finishes and we need to move to the next */
1140 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
1142 gint next_part = -1;
1143 gint cur_part = splitpad->cur_part;
1144 gboolean res = FALSE;
1146 if (splitmux->play_segment.rate >= 0.0) {
1147 if (cur_part + 1 < splitmux->num_parts)
1148 next_part = cur_part + 1;
1149 /* Make sure the transition is seamless */
1150 splitpad->set_next_discont = FALSE;
1151 splitpad->clear_next_discont = TRUE;
1153 /* Reverse play - move to previous segment */
1155 next_part = cur_part - 1;
1156 /* Non-seamless transition in reverse */
1157 splitpad->set_next_discont = TRUE;
1158 splitpad->clear_next_discont = FALSE;
1162 SPLITMUX_SRC_LOCK (splitmux);
1164 /* If all pads are done with this part, deactivate it */
1165 if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
1166 gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
1168 if (splitmux->play_segment.rate >= 0.0) {
1169 if (splitmux->play_segment.stop != -1) {
1170 GstClockTime part_end =
1171 gst_splitmux_part_reader_get_end_offset (splitmux->parts[cur_part]);
1172 if (part_end >= splitmux->play_segment.stop) {
1173 GST_DEBUG_OBJECT (splitmux,
1174 "Stop position was within that part. Finishing");
1179 if (splitmux->play_segment.start != -1) {
1180 GstClockTime part_start =
1181 gst_splitmux_part_reader_get_start_offset (splitmux->parts[cur_part]);
1182 if (part_start <= splitmux->play_segment.start) {
1183 GST_DEBUG_OBJECT (splitmux,
1184 "Start position %" GST_TIME_FORMAT
1185 " was within that part. Finishing",
1186 GST_TIME_ARGS (splitmux->play_segment.start));
1192 if (next_part != -1) {
1193 GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
1194 " moving to part %d", splitpad, next_part);
1195 splitpad->cur_part = next_part;
1196 splitpad->reader = splitmux->parts[splitpad->cur_part];
1197 if (splitpad->part_pad)
1198 gst_object_unref (splitpad->part_pad);
1199 splitpad->part_pad =
1200 gst_splitmux_part_reader_lookup_pad (splitpad->reader,
1201 (GstPad *) (splitpad));
1203 if (splitmux->cur_part != next_part) {
1204 if (!gst_splitmux_part_reader_is_active (splitpad->reader)) {
1206 /* If moving backward into a new part, set stop
1207 * to -1 to ensure we play the entire file - workaround
1208 * a bug in qtdemux that misses bits at the end */
1209 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1213 /* This is the first pad to move to the new part, activate it */
1214 GST_DEBUG_OBJECT (splitpad,
1215 "First pad to change part. Activating part %d with seg %"
1216 GST_SEGMENT_FORMAT, next_part, &tmp);
1217 if (!gst_splitmux_part_reader_activate (splitpad->reader, &tmp,
1218 GST_SEEK_FLAG_NONE))
1221 splitmux->cur_part = next_part;
1226 SPLITMUX_SRC_UNLOCK (splitmux);
1229 SPLITMUX_SRC_UNLOCK (splitmux);
1230 GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
1231 ("Failed to activate part %d", splitmux->cur_part));
1235 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
1238 splitmux_src_pad_constructed (GObject * pad)
1240 gst_pad_set_event_function (GST_PAD (pad),
1241 GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
1242 gst_pad_set_query_function (GST_PAD (pad),
1243 GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
1245 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1249 gst_splitmux_src_pad_dispose (GObject * object)
1251 SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1253 GST_OBJECT_LOCK (pad);
1254 if (pad->part_pad) {
1255 gst_object_unref (pad->part_pad);
1256 pad->part_pad = NULL;
1258 GST_OBJECT_UNLOCK (pad);
1260 G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1264 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1266 GObjectClass *gobject_klass = (GObjectClass *) (klass);
1268 gobject_klass->constructed = splitmux_src_pad_constructed;
1269 gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1273 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1277 /* Event handler for source pads. Proxy events into the child
1281 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1283 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1284 gboolean ret = FALSE;
1286 GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1287 " on %" GST_PTR_FORMAT, event, pad);
1289 switch (GST_EVENT_TYPE (event)) {
1290 case GST_EVENT_SEEK:{
1294 GstSeekType start_type, stop_type;
1298 GstClockTime part_start, position;
1302 gst_event_parse_seek (event, &rate, &format, &flags,
1303 &start_type, &start, &stop_type, &stop);
1305 if (format != GST_FORMAT_TIME) {
1306 GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1309 /* FIXME: Support non-flushing seeks, which might never wake up */
1310 if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1311 GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
1314 seqnum = gst_event_get_seqnum (event);
1316 SPLITMUX_SRC_LOCK (splitmux);
1317 if (!splitmux->running || splitmux->num_parts < 1) {
1318 /* Not started yet */
1319 SPLITMUX_SRC_UNLOCK (splitmux);
1323 gst_segment_copy_into (&splitmux->play_segment, &tmp);
1325 if (!gst_segment_do_seek (&tmp, rate,
1326 format, flags, start_type, start, stop_type, stop, NULL)) {
1327 /* Invalid seek requested, ignore it */
1328 SPLITMUX_SRC_UNLOCK (splitmux);
1331 position = tmp.position;
1333 GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1334 GST_SEGMENT_FORMAT, &tmp);
1336 GST_DEBUG_OBJECT (splitmux,
1337 "Handling flushing seek. Sending flush start");
1339 /* Send flush_start */
1340 gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1342 /* Stop all parts, which will work because of the flush */
1343 SPLITMUX_SRC_PADS_LOCK (splitmux);
1344 SPLITMUX_SRC_UNLOCK (splitmux);
1345 for (cur = g_list_first (splitmux->pads);
1346 cur != NULL; cur = g_list_next (cur)) {
1347 SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1348 GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1349 gst_splitmux_part_reader_deactivate (reader);
1352 /* Shut down pad tasks */
1353 GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1354 for (cur = g_list_first (splitmux->pads);
1355 cur != NULL; cur = g_list_next (cur)) {
1356 GstPad *splitpad = (GstPad *) (cur->data);
1357 gst_pad_pause_task (GST_PAD (splitpad));
1359 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1360 SPLITMUX_SRC_LOCK (splitmux);
1362 /* Send flush stop */
1363 GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1364 gst_splitmux_push_flush_stop (splitmux, seqnum);
1366 /* Everything is stopped, so update the play_segment */
1367 gst_segment_copy_into (&tmp, &splitmux->play_segment);
1368 splitmux->segment_seqnum = seqnum;
1370 /* Work out where to start from now */
1371 for (i = 0; i < splitmux->num_parts; i++) {
1372 GstSplitMuxPartReader *reader = splitmux->parts[i];
1373 GstClockTime part_end =
1374 gst_splitmux_part_reader_get_end_offset (reader);
1376 if (part_end > position)
1379 if (i == splitmux->num_parts)
1380 i = splitmux->num_parts - 1;
1383 gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1385 GST_DEBUG_OBJECT (splitmux,
1386 "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1387 GST_TIME_FORMAT, GST_TIME_ARGS (position),
1388 i, GST_TIME_ARGS (position - part_start));
1390 ret = gst_splitmux_src_activate_part (splitmux, i, flags);
1391 SPLITMUX_SRC_UNLOCK (splitmux);
1397 gst_event_unref (event);
1403 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1405 /* Query handler for source pads. Proxy queries into the child
1408 GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1409 gboolean ret = FALSE;
1411 GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1412 " on %" GST_PTR_FORMAT, query, pad);
1413 switch (GST_QUERY_TYPE (query)) {
1414 case GST_QUERY_CAPS:
1415 case GST_QUERY_POSITION:{
1416 GstSplitMuxPartReader *part;
1417 SplitMuxSrcPad *anypad;
1419 SPLITMUX_SRC_LOCK (splitmux);
1420 SPLITMUX_SRC_PADS_LOCK (splitmux);
1421 anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1422 part = splitmux->parts[anypad->cur_part];
1423 ret = gst_splitmux_part_reader_src_query (part, pad, query);
1424 SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1425 SPLITMUX_SRC_UNLOCK (splitmux);
1428 case GST_QUERY_DURATION:{
1429 GstClockTime duration;
1432 gst_query_parse_duration (query, &fmt, NULL);
1433 if (fmt != GST_FORMAT_TIME)
1436 GST_OBJECT_LOCK (splitmux);
1437 duration = splitmux->total_duration;
1438 GST_OBJECT_UNLOCK (splitmux);
1440 if (duration > 0 && duration != GST_CLOCK_TIME_NONE) {
1441 gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1446 case GST_QUERY_SEEKING:{
1449 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1450 if (format != GST_FORMAT_TIME)
1453 GST_OBJECT_LOCK (splitmux);
1454 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1455 splitmux->total_duration);
1457 GST_OBJECT_UNLOCK (splitmux);
1469 register_splitmuxsrc (GstPlugin * plugin)
1471 GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1472 "Split File Demuxing Source");
1474 return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1475 GST_TYPE_SPLITMUX_SRC);