splitmuxsink: Allow time and bytes to reach their respective thresholds
[platform/upstream/gst-plugins-good.git] / gst / multifile / gstsplitmuxsrc.c
1 /* GStreamer Split Demuxer bin that recombines files created by
2  * the splitmuxsink element.
3  *
4  * Copyright (C) <2014> Jan Schmidt <jan@centricular.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 /**
23  * SECTION:element-splitmuxsrc
24  * @short_description: Split Demuxer bin that recombines files created by
25  * the splitmuxsink element.
26  *
27  * This element reads a set of input files created by the splitmuxsink element
28  * containing contiguous elementary streams split across multiple files.
29  *
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.
33  *
34  * <refsect2>
35  * <title>Example pipelines</title>
36  * |[
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
39  * |[
40  * gst-launch-1.0 playbin uri="splitmux://path/to/foo.mp4.*"
41  * ]| Play back a set of files created by splitmuxsink
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include <string.h>
50 #include "gstsplitmuxsrc.h"
51 #include "gstsplitutils.h"
52
53 #include "../../gst-libs/gst/gst-i18n-plugin.h"
54
55 GST_DEBUG_CATEGORY (splitmux_debug);
56 #define GST_CAT_DEFAULT splitmux_debug
57
58 enum
59 {
60   PROP_0,
61   PROP_LOCATION
62 };
63
64 enum
65 {
66   SIGNAL_FORMAT_LOCATION,
67   SIGNAL_LAST
68 };
69
70 static guint signals[SIGNAL_LAST];
71
72 static GstStaticPadTemplate video_src_template =
73 GST_STATIC_PAD_TEMPLATE ("video",
74     GST_PAD_SRC,
75     GST_PAD_SOMETIMES,
76     GST_STATIC_CAPS_ANY);
77
78 static GstStaticPadTemplate audio_src_template =
79 GST_STATIC_PAD_TEMPLATE ("audio_%u",
80     GST_PAD_SRC,
81     GST_PAD_SOMETIMES,
82     GST_STATIC_CAPS_ANY);
83
84 static GstStaticPadTemplate subtitle_src_template =
85 GST_STATIC_PAD_TEMPLATE ("subtitle_%u",
86     GST_PAD_SRC,
87     GST_PAD_SOMETIMES,
88     GST_STATIC_CAPS_ANY);
89
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,
102     GstEvent * event);
103 static gboolean splitmux_src_pad_query (GstPad * pad, GstObject * parent,
104     GstQuery * query);
105 static void splitmux_src_uri_handler_init (gpointer g_iface,
106     gpointer iface_data);
107
108
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,
116     GstEvent * event);
117
118 #define _do_init \
119     G_IMPLEMENT_INTERFACE(GST_TYPE_URI_HANDLER, splitmux_src_uri_handler_init);
120 #define gst_splitmux_src_parent_class parent_class
121
122 G_DEFINE_TYPE_EXTENDED (GstSplitMuxSrc, gst_splitmux_src, GST_TYPE_BIN, 0,
123     _do_init);
124
125 static GstURIType
126 splitmux_src_uri_get_type (GType type)
127 {
128   return GST_URI_SRC;
129 }
130
131 static const gchar *const *
132 splitmux_src_uri_get_protocols (GType type)
133 {
134   static const gchar *protocols[] = { "splitmux", NULL };
135
136   return protocols;
137 }
138
139 static gchar *
140 splitmux_src_uri_get_uri (GstURIHandler * handler)
141 {
142   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
143   gchar *ret = NULL;
144
145   GST_OBJECT_LOCK (splitmux);
146   if (splitmux->location)
147     ret = g_strdup_printf ("splitmux://%s", splitmux->location);
148   GST_OBJECT_UNLOCK (splitmux);
149   return ret;
150 }
151
152 static gboolean
153 splitmux_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
154     GError ** err)
155 {
156   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (handler);
157   gchar *protocol, *location;
158
159   protocol = gst_uri_get_protocol (uri);
160   if (protocol == NULL || !g_str_equal (protocol, "splitmux"))
161     goto wrong_uri;
162   g_free (protocol);
163
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);
169
170   return TRUE;
171
172 wrong_uri:
173   g_free (protocol);
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");
178   return FALSE;
179 }
180
181 static void
182 splitmux_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
183 {
184   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) (g_iface);
185
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;
190 }
191
192
193 static void
194 gst_splitmux_src_class_init (GstSplitMuxSrcClass * klass)
195 {
196   GObjectClass *gobject_class = (GObjectClass *) klass;
197   GstElementClass *gstelement_class = (GstElementClass *) klass;
198
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;
203
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>");
208
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);
215
216   gstelement_class->change_state =
217       GST_DEBUG_FUNCPTR (gst_splitmux_src_change_state);
218
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));
223
224   /**
225    * GstSplitMuxSrc::format-location:
226    * @splitmux: the #GstSplitMuxSrc
227    *
228    * Returns: A NULL-terminated sorted array of strings containing the
229    *   filenames of the input files. The array will be freed internally
230    *   using g_strfreev()
231    *
232    * Since: 1.8
233    */
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);
237 }
238
239 static void
240 gst_splitmux_src_init (GstSplitMuxSrc * splitmux)
241 {
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);
246 }
247
248 static void
249 gst_splitmux_src_dispose (GObject * object)
250 {
251   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
252   GList *cur;
253
254   SPLITMUX_SRC_PADS_LOCK (splitmux);
255
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);
260   }
261   g_list_free (splitmux->pads);
262   splitmux->pads = NULL;
263   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
264
265   G_OBJECT_CLASS (parent_class)->dispose (object);
266 }
267
268 static void
269 gst_splitmux_src_finalize (GObject * object)
270 {
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);
275
276   G_OBJECT_CLASS (parent_class)->finalize (object);
277 }
278
279 static void
280 gst_splitmux_src_set_property (GObject * object, guint prop_id,
281     const GValue * value, GParamSpec * pspec)
282 {
283   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
284
285   switch (prop_id) {
286     case PROP_LOCATION:{
287       GST_OBJECT_LOCK (splitmux);
288       g_free (splitmux->location);
289       splitmux->location = g_value_dup_string (value);
290       GST_OBJECT_UNLOCK (splitmux);
291       break;
292     }
293     default:
294       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295       break;
296   }
297 }
298
299 static void
300 gst_splitmux_src_get_property (GObject * object, guint prop_id,
301     GValue * value, GParamSpec * pspec)
302 {
303   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
304
305   switch (prop_id) {
306     case PROP_LOCATION:
307       GST_OBJECT_LOCK (splitmux);
308       g_value_set_string (value, splitmux->location);
309       GST_OBJECT_UNLOCK (splitmux);
310       break;
311     default:
312       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313       break;
314   }
315 }
316
317 static GstStateChangeReturn
318 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
319 {
320   GstStateChangeReturn ret;
321   GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
322
323   switch (transition) {
324     case GST_STATE_CHANGE_NULL_TO_READY:{
325       break;
326     }
327     case GST_STATE_CHANGE_READY_TO_PAUSED:{
328       if (!gst_splitmux_src_start (splitmux))
329         return GST_STATE_CHANGE_FAILURE;
330       break;
331     }
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;
337       break;
338     default:
339       break;
340   }
341
342   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
343
344   return ret;
345 }
346
347 static GstSplitMuxPartReader *
348 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
349 {
350   GstSplitMuxPartReader *r;
351
352   r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
353
354   g_signal_connect (r, "prepared", (GCallback) gst_splitmux_part_prepared,
355       splitmux);
356
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);
360
361   return r;
362 }
363
364 static gboolean
365 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
366 {
367   GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
368   GstCaps *newcaps;
369   GstCaps *tmpcaps;
370   GstCaps *tmpcurcaps;
371
372   GstStructure *s;
373   gboolean res = TRUE;
374
375   gst_event_parse_caps (event, &newcaps);
376
377   GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
378       " and %" GST_PTR_FORMAT, curcaps, newcaps);
379
380   if (curcaps == NULL)
381     return TRUE;
382
383   /* If caps are exactly equal exit early */
384   if (gst_caps_is_equal (curcaps, newcaps)) {
385     gst_caps_unref (curcaps);
386     return FALSE;
387   }
388
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");
394
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");
399
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");
403     res = FALSE;
404   }
405
406   gst_caps_unref (tmpcaps);
407   gst_caps_unref (tmpcurcaps);
408   return res;
409 }
410
411 static void
412 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
413     SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
414 {
415   switch (GST_EVENT_TYPE (event)) {
416     case GST_EVENT_STREAM_START:{
417       if (splitpad->sent_stream_start)
418         goto drop_event;
419       splitpad->sent_stream_start = TRUE;
420       break;
421     }
422     case GST_EVENT_EOS:{
423       if (gst_splitmux_end_of_part (splitmux, splitpad))
424         // Continuing to next part, drop the EOS
425         goto drop_event;
426       break;
427     }
428     case GST_EVENT_SEGMENT:{
429       GstSegment seg;
430
431       gst_event_copy_segment (event, &seg);
432
433       splitpad->segment.position = seg.position;
434
435       if (splitpad->sent_segment)
436         goto drop_event;        /* We already forwarded a segment event */
437
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);
443
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;
449         else
450           seg.stop = splitpad->segment.stop;
451       } else {
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;
457         } else {
458           seg.start = splitpad->segment.start;
459           seg.time = splitpad->segment.time;
460         }
461       }
462
463       GST_INFO_OBJECT (splitpad,
464           "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
465
466       gst_event_unref (event);
467       event = gst_event_new_segment (&seg);
468       splitpad->sent_segment = TRUE;
469       break;
470     }
471     case GST_EVENT_CAPS:{
472       if (!gst_splitmux_check_new_caps (splitpad, event))
473         goto drop_event;
474       splitpad->sent_caps = TRUE;
475       break;
476     }
477     default:
478       break;
479   }
480
481   gst_pad_push_event ((GstPad *) (splitpad), event);
482   return;
483 drop_event:
484   gst_event_unref (event);
485   return;
486 }
487
488 static GstFlowReturn
489 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
490     SplitMuxSrcPad * splitpad, GstBuffer * buf)
491 {
492   GstFlowReturn ret;
493
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;
498   }
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;
503   }
504
505   ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
506
507   GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
508   return ret;
509 }
510
511 static void
512 gst_splitmux_pad_loop (GstPad * pad)
513 {
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;
519   GstPad *part_pad;
520   GstFlowReturn ret;
521
522   GST_OBJECT_LOCK (splitpad);
523   if (splitpad->part_pad == NULL) {
524     GST_OBJECT_UNLOCK (splitpad);
525     return;
526   }
527   part_pad = gst_object_ref (splitpad->part_pad);
528   GST_OBJECT_UNLOCK (splitpad);
529
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)
534     goto error;
535   if (ret == GST_FLOW_FLUSHING || item == NULL)
536     goto flushing;
537
538   GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
539       item->object);
540
541   if (GST_IS_EVENT (item->object)) {
542     GstEvent *event = (GstEvent *) (item->object);
543     gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
544   } else {
545     GstBuffer *buf = (GstBuffer *) (item->object);
546     GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
547     if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
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));
556       }
557     }
558   }
559   g_slice_free (GstDataQueueItem, item);
560
561   gst_object_unref (part_pad);
562   gst_object_unref (splitmux);
563   return;
564
565 error:
566   /* Fall through */
567   GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
568       ("Error reading part file %s", GST_STR_NULL (reader->path)));
569 flushing:
570   gst_pad_pause_task (pad);
571   gst_object_unref (part_pad);
572   gst_object_unref (splitmux);
573   return;
574 }
575
576 static gboolean
577 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part)
578 {
579   GList *cur;
580
581   GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
582
583   splitmux->cur_part = part;
584   if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
585           &splitmux->play_segment))
586     return FALSE;
587
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);
596     splitpad->part_pad =
597         gst_splitmux_part_reader_lookup_pad (splitpad->reader,
598         (GstPad *) (splitpad));
599
600     /* Make sure we start with a DISCONT */
601     splitpad->set_next_discont = TRUE;
602     splitpad->clear_next_discont = FALSE;
603
604     gst_pad_start_task (GST_PAD (splitpad),
605         (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
606   }
607   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
608
609   return TRUE;
610 }
611
612 static gboolean
613 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
614 {
615   gboolean ret = FALSE;
616   GError *err = NULL;
617   gchar *basename = NULL;
618   gchar *dirname = NULL;
619   gchar **files;
620   GstClockTime next_offset = 0;
621   guint i;
622   GstClockTime total_duration = 0;
623
624   GST_DEBUG_OBJECT (splitmux, "Starting");
625
626   g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
627
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);
633     }
634     GST_OBJECT_UNLOCK (splitmux);
635
636     g_strfreev (files);
637     files = gst_split_util_find_files (dirname, basename, &err);
638
639     if (files == NULL || *files == NULL)
640       goto no_files;
641   }
642
643   SPLITMUX_SRC_LOCK (splitmux);
644   splitmux->pads_complete = FALSE;
645   splitmux->running = TRUE;
646   SPLITMUX_SRC_UNLOCK (splitmux);
647
648   splitmux->num_parts = g_strv_length (files);
649
650   splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
651
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)
655       break;
656
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.",
664               files[i]));
665       gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
666       g_object_unref (splitmux->parts[i]);
667       splitmux->parts[i] = NULL;
668       break;
669     }
670
671     /* Extend our total duration to cover this part */
672     total_duration =
673         next_offset +
674         gst_splitmux_part_reader_get_duration (splitmux->parts[i]);
675     splitmux->play_segment.duration = total_duration;
676
677     next_offset = gst_splitmux_part_reader_get_end_offset (splitmux->parts[i]);
678   }
679
680   /* Update total_duration state variable */
681   GST_OBJECT_LOCK (splitmux);
682   splitmux->total_duration = total_duration;
683   GST_OBJECT_UNLOCK (splitmux);
684
685   /* Store how many parts we actually created */
686   splitmux->num_parts = i;
687
688   if (splitmux->num_parts < 1)
689     goto failed_part;
690
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);
696   if (ret == FALSE)
697     goto failed_first_part;
698 done:
699   if (err != NULL)
700     g_error_free (err);
701   g_strfreev (files);
702   g_free (basename);
703   g_free (dirname);
704
705   return ret;
706
707 /* ERRORS */
708 no_files:
709   {
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)));
713     goto done;
714   }
715 failed_part:
716   {
717     GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
718         ("Failed to open any files for reading"));
719     goto done;
720   }
721 failed_first_part:
722   {
723     GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
724         ("Failed to activate first part for playback"));
725     goto done;
726   }
727 }
728
729 static gboolean
730 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
731 {
732   gboolean ret = TRUE;
733   guint i;
734   GList *cur, *pads_list;
735
736   SPLITMUX_SRC_LOCK (splitmux);
737   if (!splitmux->running)
738     goto out;
739
740   GST_DEBUG_OBJECT (splitmux, "Stopping");
741
742   /* Stop and destroy all parts  */
743   for (i = 0; i < splitmux->num_parts; i++) {
744     if (splitmux->parts[i] == NULL)
745       continue;
746     gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
747     g_object_unref (splitmux->parts[i]);
748     splitmux->parts[i] = NULL;
749   }
750
751   SPLITMUX_SRC_PADS_LOCK (splitmux);
752   pads_list = splitmux->pads;
753   splitmux->pads = NULL;
754   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
755
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));
760   }
761   g_list_free (pads_list);
762
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);
770 out:
771   SPLITMUX_SRC_UNLOCK (splitmux);
772   return ret;
773 }
774
775 typedef struct
776 {
777   GstSplitMuxSrc *splitmux;
778   SplitMuxSrcPad *splitpad;
779 } SplitMuxAndPad;
780
781 static gboolean
782 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
783 {
784   SplitMuxAndPad *splitmux_and_pad;
785   GstSplitMuxSrc *splitmux;
786   SplitMuxSrcPad *splitpad;
787
788   splitmux_and_pad = user_data;
789   splitmux = splitmux_and_pad->splitmux;
790   splitpad = splitmux_and_pad->splitpad;
791
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);
795
796   return TRUE;
797 }
798
799 static GstPad *
800 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
801     GstSplitMuxSrc * splitmux)
802 {
803   GList *cur;
804   gchar *pad_name = gst_pad_get_name (pad);
805   GstPad *target = NULL;
806   gboolean is_new_pad = FALSE;
807
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)) {
814       target = tmp;
815       break;
816     }
817   }
818
819   if (target == NULL && !splitmux->pads_complete) {
820     SplitMuxAndPad splitmux_and_pad;
821
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);
826
827     gst_pad_set_active (target, TRUE);
828
829     splitmux_and_pad.splitmux = splitmux;
830     splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
831     gst_pad_sticky_events_foreach (pad, handle_sticky_events,
832         &splitmux_and_pad);
833     is_new_pad = TRUE;
834   }
835   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
836   SPLITMUX_SRC_UNLOCK (splitmux);
837
838   g_free (pad_name);
839
840   if (target == NULL)
841     goto pad_not_found;
842
843   if (is_new_pad)
844     gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
845
846   return target;
847
848 pad_not_found:
849   GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
850       ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
851           part->path, pad));
852   return NULL;
853 }
854
855 static void
856 gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
857     GstSplitMuxSrc * splitmux)
858 {
859   gboolean need_no_more_pads;
860
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);
866
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));
870   }
871 }
872
873 static void
874 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
875     guint32 seqnum)
876 {
877   GList *cur;
878
879   if (seqnum)
880     gst_event_set_seqnum (e, seqnum);
881
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);
886     gst_event_ref (e);
887     gst_pad_push_event (pad, e);
888   }
889   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
890
891   gst_event_unref (e);
892 }
893
894 static void
895 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
896 {
897   GstEvent *e = gst_event_new_flush_stop (TRUE);
898   GList *cur;
899
900   if (seqnum)
901     gst_event_set_seqnum (e, seqnum);
902
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);
907
908     gst_event_ref (e);
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;
913   }
914   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
915
916   gst_event_unref (e);
917 }
918
919 /* Callback for when a part finishes and we need to move to the next */
920 static gboolean
921 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
922 {
923   gint next_part = -1;
924   gint cur_part = splitpad->cur_part;
925   gboolean res = FALSE;
926
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;
933   } else {
934     /* Reverse play - move to previous segment */
935     if (cur_part > 0) {
936       next_part = cur_part - 1;
937       /* Non-seamless transition in reverse */
938       splitpad->set_next_discont = TRUE;
939       splitpad->clear_next_discont = FALSE;
940     }
941   }
942
943   SPLITMUX_SRC_LOCK (splitmux);
944
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]);
948
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);
956     splitpad->part_pad =
957         gst_splitmux_part_reader_lookup_pad (splitpad->reader,
958         (GstPad *) (splitpad));
959
960     if (splitmux->cur_part != next_part) {
961       GstSegment tmp;
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);
966       if (tmp.rate < 0)
967         tmp.stop = -1;
968
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))
975         goto error;
976     }
977     res = TRUE;
978   }
979
980   SPLITMUX_SRC_UNLOCK (splitmux);
981   return res;
982 error:
983   SPLITMUX_SRC_UNLOCK (splitmux);
984   GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
985       ("Failed to activate part %d", splitmux->cur_part));
986   return FALSE;
987 }
988
989 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
990
991 static void
992 splitmux_src_pad_constructed (GObject * pad)
993 {
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));
998
999   G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1000 }
1001
1002 static void
1003 gst_splitmux_src_pad_dispose (GObject * object)
1004 {
1005   SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1006
1007   GST_OBJECT_LOCK (pad);
1008   if (pad->part_pad) {
1009     gst_object_unref (pad->part_pad);
1010     pad->part_pad = NULL;
1011   }
1012   GST_OBJECT_UNLOCK (pad);
1013
1014   G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1015 }
1016
1017 static void
1018 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1019 {
1020   GObjectClass *gobject_klass = (GObjectClass *) (klass);
1021
1022   gobject_klass->constructed = splitmux_src_pad_constructed;
1023   gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1024 }
1025
1026 static void
1027 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1028 {
1029 }
1030
1031 /* Event handler for source pads. Proxy events into the child
1032  * parts as needed
1033  */
1034 static gboolean
1035 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1036 {
1037   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1038   gboolean ret = FALSE;
1039
1040   GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1041       " on %" GST_PTR_FORMAT, event, pad);
1042
1043   switch (GST_EVENT_TYPE (event)) {
1044     case GST_EVENT_SEEK:{
1045       GstFormat format;
1046       gdouble rate;
1047       GstSeekFlags flags;
1048       GstSeekType start_type, stop_type;
1049       gint64 start, stop;
1050       guint32 seqnum;
1051       gint i;
1052       GstClockTime part_start, position;
1053       GList *cur;
1054       GstSegment tmp;
1055
1056       gst_event_parse_seek (event, &rate, &format, &flags,
1057           &start_type, &start, &stop_type, &stop);
1058
1059       if (format != GST_FORMAT_TIME) {
1060         GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1061         goto error;
1062       }
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");
1066         goto error;
1067       }
1068       seqnum = gst_event_get_seqnum (event);
1069
1070       SPLITMUX_SRC_LOCK (splitmux);
1071       if (!splitmux->running || splitmux->num_parts < 1) {
1072         /* Not started yet */
1073         SPLITMUX_SRC_UNLOCK (splitmux);
1074         goto error;
1075       }
1076
1077       gst_segment_copy_into (&splitmux->play_segment, &tmp);
1078
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);
1083         goto error;
1084       }
1085       position = tmp.position;
1086
1087       GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1088           GST_SEGMENT_FORMAT, &tmp);
1089
1090       GST_DEBUG_OBJECT (splitmux,
1091           "Handling flushing seek. Sending flush start");
1092
1093       /* Send flush_start */
1094       gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1095
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);
1104       }
1105
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));
1112       }
1113       SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1114       SPLITMUX_SRC_LOCK (splitmux);
1115
1116       /* Send flush stop */
1117       GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1118       gst_splitmux_push_flush_stop (splitmux, seqnum);
1119
1120       /* Everything is stopped, so update the play_segment */
1121       gst_segment_copy_into (&tmp, &splitmux->play_segment);
1122
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);
1128
1129         if (part_end > position)
1130           break;
1131       }
1132       if (i == splitmux->num_parts)
1133         i = splitmux->num_parts - 1;
1134
1135       part_start =
1136           gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1137
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));
1142
1143       ret = gst_splitmux_src_activate_part (splitmux, i);
1144       SPLITMUX_SRC_UNLOCK (splitmux);
1145     }
1146     default:
1147       break;
1148   }
1149
1150   gst_event_unref (event);
1151 error:
1152   return ret;
1153 }
1154
1155 static gboolean
1156 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1157 {
1158   /* Query handler for source pads. Proxy queries into the child
1159    * parts as needed
1160    */
1161   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1162   gboolean ret = FALSE;
1163
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;
1171
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);
1179       break;
1180     }
1181     case GST_QUERY_DURATION:{
1182       GstFormat fmt;
1183       gst_query_parse_duration (query, &fmt, NULL);
1184       if (fmt != GST_FORMAT_TIME)
1185         break;
1186
1187       GST_OBJECT_LOCK (splitmux);
1188       if (splitmux->total_duration > 0) {
1189         gst_query_set_duration (query, GST_FORMAT_TIME,
1190             splitmux->total_duration);
1191         ret = TRUE;
1192       }
1193       GST_OBJECT_UNLOCK (splitmux);
1194       break;
1195     }
1196     case GST_QUERY_SEEKING:{
1197       GstFormat format;
1198
1199       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1200       if (format != GST_FORMAT_TIME)
1201         break;
1202
1203       GST_OBJECT_LOCK (splitmux);
1204       gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1205           splitmux->total_duration);
1206       ret = TRUE;
1207       GST_OBJECT_UNLOCK (splitmux);
1208
1209       break;
1210     }
1211     default:
1212       break;
1213   }
1214   return ret;
1215 }
1216
1217
1218 gboolean
1219 register_splitmuxsrc (GstPlugin * plugin)
1220 {
1221   GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1222       "Split File Demuxing Source");
1223
1224   return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1225       GST_TYPE_SPLITMUX_SRC);
1226 }