splitmuxsink: Don't leak old muxer/sink in async mode
[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->n_pads = 0;
263   splitmux->pads = NULL;
264   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
265
266   G_OBJECT_CLASS (parent_class)->dispose (object);
267 }
268
269 static void
270 gst_splitmux_src_finalize (GObject * object)
271 {
272   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
273   g_mutex_clear (&splitmux->lock);
274   g_mutex_clear (&splitmux->pads_lock);
275   g_free (splitmux->location);
276
277   G_OBJECT_CLASS (parent_class)->finalize (object);
278 }
279
280 static void
281 gst_splitmux_src_set_property (GObject * object, guint prop_id,
282     const GValue * value, GParamSpec * pspec)
283 {
284   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
285
286   switch (prop_id) {
287     case PROP_LOCATION:{
288       GST_OBJECT_LOCK (splitmux);
289       g_free (splitmux->location);
290       splitmux->location = g_value_dup_string (value);
291       GST_OBJECT_UNLOCK (splitmux);
292       break;
293     }
294     default:
295       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296       break;
297   }
298 }
299
300 static void
301 gst_splitmux_src_get_property (GObject * object, guint prop_id,
302     GValue * value, GParamSpec * pspec)
303 {
304   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (object);
305
306   switch (prop_id) {
307     case PROP_LOCATION:
308       GST_OBJECT_LOCK (splitmux);
309       g_value_set_string (value, splitmux->location);
310       GST_OBJECT_UNLOCK (splitmux);
311       break;
312     default:
313       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314       break;
315   }
316 }
317
318 static GstStateChangeReturn
319 gst_splitmux_src_change_state (GstElement * element, GstStateChange transition)
320 {
321   GstStateChangeReturn ret;
322   GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) element;
323
324   switch (transition) {
325     case GST_STATE_CHANGE_NULL_TO_READY:{
326       break;
327     }
328     case GST_STATE_CHANGE_READY_TO_PAUSED:{
329       if (!gst_splitmux_src_start (splitmux))
330         return GST_STATE_CHANGE_FAILURE;
331       break;
332     }
333     case GST_STATE_CHANGE_PAUSED_TO_READY:
334     case GST_STATE_CHANGE_READY_TO_NULL:
335       /* Make sure the element will shut down */
336       if (!gst_splitmux_src_stop (splitmux))
337         return GST_STATE_CHANGE_FAILURE;
338       break;
339     default:
340       break;
341   }
342
343   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
344
345   return ret;
346 }
347
348 static GstSplitMuxPartReader *
349 gst_splitmux_part_create (GstSplitMuxSrc * splitmux, char *filename)
350 {
351   GstSplitMuxPartReader *r;
352
353   r = g_object_new (GST_TYPE_SPLITMUX_PART_READER, NULL);
354
355   g_signal_connect (r, "prepared", (GCallback) gst_splitmux_part_prepared,
356       splitmux);
357
358   gst_splitmux_part_reader_set_callbacks (r, splitmux,
359       (GstSplitMuxPartReaderPadCb) gst_splitmux_find_output_pad);
360   gst_splitmux_part_reader_set_location (r, filename);
361
362   return r;
363 }
364
365 static gboolean
366 gst_splitmux_check_new_caps (SplitMuxSrcPad * splitpad, GstEvent * event)
367 {
368   GstCaps *curcaps = gst_pad_get_current_caps ((GstPad *) (splitpad));
369   GstCaps *newcaps;
370   GstCaps *tmpcaps;
371   GstCaps *tmpcurcaps;
372
373   GstStructure *s;
374   gboolean res = TRUE;
375
376   gst_event_parse_caps (event, &newcaps);
377
378   GST_LOG_OBJECT (splitpad, "Comparing caps %" GST_PTR_FORMAT
379       " and %" GST_PTR_FORMAT, curcaps, newcaps);
380
381   if (curcaps == NULL)
382     return TRUE;
383
384   /* If caps are exactly equal exit early */
385   if (gst_caps_is_equal (curcaps, newcaps)) {
386     gst_caps_unref (curcaps);
387     return FALSE;
388   }
389
390   /* More extensive check, ignore changes in framerate, because
391    * demuxers get that wrong */
392   tmpcaps = gst_caps_copy (newcaps);
393   s = gst_caps_get_structure (tmpcaps, 0);
394   gst_structure_remove_field (s, "framerate");
395
396   tmpcurcaps = gst_caps_copy (curcaps);
397   gst_caps_unref (curcaps);
398   s = gst_caps_get_structure (tmpcurcaps, 0);
399   gst_structure_remove_field (s, "framerate");
400
401   /* Now check if these filtered caps are equal */
402   if (gst_caps_is_equal (tmpcurcaps, tmpcaps)) {
403     GST_INFO_OBJECT (splitpad, "Ignoring framerate-only caps change");
404     res = FALSE;
405   }
406
407   gst_caps_unref (tmpcaps);
408   gst_caps_unref (tmpcurcaps);
409   return res;
410 }
411
412 static void
413 gst_splitmux_handle_event (GstSplitMuxSrc * splitmux,
414     SplitMuxSrcPad * splitpad, GstPad * part_pad, GstEvent * event)
415 {
416   switch (GST_EVENT_TYPE (event)) {
417     case GST_EVENT_STREAM_START:{
418       if (splitpad->sent_stream_start)
419         goto drop_event;
420       splitpad->sent_stream_start = TRUE;
421       break;
422     }
423     case GST_EVENT_EOS:{
424       if (gst_splitmux_end_of_part (splitmux, splitpad))
425         // Continuing to next part, drop the EOS
426         goto drop_event;
427       if (splitmux->segment_seqnum) {
428         event = gst_event_make_writable (event);
429         gst_event_set_seqnum (event, splitmux->segment_seqnum);
430       }
431       break;
432     }
433     case GST_EVENT_SEGMENT:{
434       GstClockTime duration;
435       GstSegment seg;
436
437       gst_event_copy_segment (event, &seg);
438
439       splitpad->segment.position = seg.position;
440
441       if (splitpad->sent_segment)
442         goto drop_event;        /* We already forwarded a segment event */
443
444       /* Calculate output segment */
445       GST_LOG_OBJECT (splitpad, "Pad seg %" GST_SEGMENT_FORMAT
446           " got seg %" GST_SEGMENT_FORMAT
447           " play seg %" GST_SEGMENT_FORMAT,
448           &splitpad->segment, &seg, &splitmux->play_segment);
449
450       /* If playing forward, take the stop time from the overall
451        * seg or play_segment */
452       if (splitmux->play_segment.rate > 0.0) {
453         if (splitmux->play_segment.stop != -1)
454           seg.stop = splitmux->play_segment.stop;
455         else
456           seg.stop = splitpad->segment.stop;
457       } else {
458         /* Reverse playback from stop time to start time */
459         /* See if an end point was requested in the seek */
460         if (splitmux->play_segment.start != -1) {
461           seg.start = splitmux->play_segment.start;
462           seg.time = splitmux->play_segment.time;
463         } else {
464           seg.start = splitpad->segment.start;
465           seg.time = splitpad->segment.time;
466         }
467       }
468
469       GST_OBJECT_LOCK (splitmux);
470       duration = splitmux->total_duration;
471       GST_OBJECT_UNLOCK (splitmux);
472
473       if (duration > 0)
474         seg.duration = duration;
475       else
476         seg.duration = GST_CLOCK_TIME_NONE;
477
478       GST_INFO_OBJECT (splitpad,
479           "Forwarding segment %" GST_SEGMENT_FORMAT, &seg);
480
481       gst_event_unref (event);
482       event = gst_event_new_segment (&seg);
483       if (splitmux->segment_seqnum)
484         gst_event_set_seqnum (event, splitmux->segment_seqnum);
485       splitpad->sent_segment = TRUE;
486       break;
487     }
488     case GST_EVENT_CAPS:{
489       if (!gst_splitmux_check_new_caps (splitpad, event))
490         goto drop_event;
491       splitpad->sent_caps = TRUE;
492       break;
493     }
494     default:
495       break;
496   }
497
498   gst_pad_push_event ((GstPad *) (splitpad), event);
499   return;
500 drop_event:
501   gst_event_unref (event);
502   return;
503 }
504
505 static GstFlowReturn
506 gst_splitmux_handle_buffer (GstSplitMuxSrc * splitmux,
507     SplitMuxSrcPad * splitpad, GstBuffer * buf)
508 {
509   GstFlowReturn ret;
510
511   if (splitpad->clear_next_discont) {
512     GST_LOG_OBJECT (splitpad, "Clearing discont flag on buffer");
513     GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
514     splitpad->clear_next_discont = FALSE;
515   }
516   if (splitpad->set_next_discont) {
517     GST_LOG_OBJECT (splitpad, "Setting discont flag on buffer");
518     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
519     splitpad->set_next_discont = FALSE;
520   }
521
522   ret = gst_pad_push (GST_PAD_CAST (splitpad), buf);
523
524   GST_LOG_OBJECT (splitpad, "Pad push returned %d", ret);
525   return ret;
526 }
527
528 static guint
529 count_not_linked (GstSplitMuxSrc * splitmux)
530 {
531   GList *cur;
532   guint ret = 0;
533
534   for (cur = g_list_first (splitmux->pads);
535       cur != NULL; cur = g_list_next (cur)) {
536     SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
537     if (GST_PAD_LAST_FLOW_RETURN (splitpad) == GST_FLOW_NOT_LINKED)
538       ret++;
539   }
540
541   return ret;
542 }
543
544 static void
545 gst_splitmux_pad_loop (GstPad * pad)
546 {
547   /* Get one event/buffer from the associated part and push */
548   SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (pad);
549   GstSplitMuxSrc *splitmux = (GstSplitMuxSrc *) gst_pad_get_parent (pad);
550   GstDataQueueItem *item = NULL;
551   GstSplitMuxPartReader *reader = splitpad->reader;
552   GstPad *part_pad;
553   GstFlowReturn ret;
554
555   GST_OBJECT_LOCK (splitpad);
556   if (splitpad->part_pad == NULL) {
557     GST_OBJECT_UNLOCK (splitpad);
558     return;
559   }
560   part_pad = gst_object_ref (splitpad->part_pad);
561   GST_OBJECT_UNLOCK (splitpad);
562
563   GST_LOG_OBJECT (splitpad, "Popping data queue item from %" GST_PTR_FORMAT
564       " pad %" GST_PTR_FORMAT, reader, part_pad);
565   ret = gst_splitmux_part_reader_pop (reader, part_pad, &item);
566   if (ret == GST_FLOW_ERROR)
567     goto error;
568   if (ret == GST_FLOW_FLUSHING || item == NULL)
569     goto flushing;
570
571   GST_DEBUG_OBJECT (splitpad, "Got data queue item %" GST_PTR_FORMAT,
572       item->object);
573
574   if (GST_IS_EVENT (item->object)) {
575     GstEvent *event = (GstEvent *) (item->object);
576     gst_splitmux_handle_event (splitmux, splitpad, part_pad, event);
577   } else {
578     GstBuffer *buf = (GstBuffer *) (item->object);
579     GstFlowReturn ret = gst_splitmux_handle_buffer (splitmux, splitpad, buf);
580     if (G_UNLIKELY (ret != GST_FLOW_OK && ret != GST_FLOW_EOS)) {
581       /* Stop immediately on error or flushing */
582       GST_INFO_OBJECT (splitpad, "Stopping due to pad_push() result %d", ret);
583       gst_pad_pause_task (pad);
584       if (ret < GST_FLOW_EOS) {
585         GST_ELEMENT_FLOW_ERROR (splitmux, ret);
586       } else if (ret == GST_FLOW_NOT_LINKED) {
587         gboolean post_error;
588         guint n_notlinked;
589
590         /* Only post not-linked if all pads are not-linked */
591         SPLITMUX_SRC_PADS_LOCK (splitmux);
592         n_notlinked = count_not_linked (splitmux);
593         post_error = (splitmux->pads_complete
594             && n_notlinked == splitmux->n_pads);
595         SPLITMUX_SRC_PADS_UNLOCK (splitmux);
596
597         if (post_error)
598           GST_ELEMENT_FLOW_ERROR (splitmux, ret);
599       }
600     }
601   }
602   g_slice_free (GstDataQueueItem, item);
603
604   gst_object_unref (part_pad);
605   gst_object_unref (splitmux);
606   return;
607
608 error:
609   /* Fall through */
610   GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
611       ("Error reading part file %s", GST_STR_NULL (reader->path)));
612 flushing:
613   gst_pad_pause_task (pad);
614   gst_object_unref (part_pad);
615   gst_object_unref (splitmux);
616   return;
617 }
618
619 static gboolean
620 gst_splitmux_src_activate_part (GstSplitMuxSrc * splitmux, guint part,
621     GstSeekFlags extra_flags)
622 {
623   GList *cur;
624
625   GST_DEBUG_OBJECT (splitmux, "Activating part %d", part);
626
627   splitmux->cur_part = part;
628   if (!gst_splitmux_part_reader_activate (splitmux->parts[part],
629           &splitmux->play_segment, extra_flags))
630     return FALSE;
631
632   SPLITMUX_SRC_PADS_LOCK (splitmux);
633   for (cur = g_list_first (splitmux->pads);
634       cur != NULL; cur = g_list_next (cur)) {
635     SplitMuxSrcPad *splitpad = (SplitMuxSrcPad *) (cur->data);
636     splitpad->cur_part = part;
637     splitpad->reader = splitmux->parts[splitpad->cur_part];
638     if (splitpad->part_pad)
639       gst_object_unref (splitpad->part_pad);
640     splitpad->part_pad =
641         gst_splitmux_part_reader_lookup_pad (splitpad->reader,
642         (GstPad *) (splitpad));
643
644     /* Make sure we start with a DISCONT */
645     splitpad->set_next_discont = TRUE;
646     splitpad->clear_next_discont = FALSE;
647
648     gst_pad_start_task (GST_PAD (splitpad),
649         (GstTaskFunction) gst_splitmux_pad_loop, splitpad, NULL);
650   }
651   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
652
653   return TRUE;
654 }
655
656 static gboolean
657 gst_splitmux_src_start (GstSplitMuxSrc * splitmux)
658 {
659   gboolean ret = FALSE;
660   GError *err = NULL;
661   gchar *basename = NULL;
662   gchar *dirname = NULL;
663   gchar **files;
664   GstClockTime next_offset = 0;
665   guint i;
666   GstClockTime total_duration = 0;
667
668   GST_DEBUG_OBJECT (splitmux, "Starting");
669
670   g_signal_emit (splitmux, signals[SIGNAL_FORMAT_LOCATION], 0, &files);
671
672   if (files == NULL || *files == NULL) {
673     GST_OBJECT_LOCK (splitmux);
674     if (splitmux->location != NULL && splitmux->location[0] != '\0') {
675       basename = g_path_get_basename (splitmux->location);
676       dirname = g_path_get_dirname (splitmux->location);
677     }
678     GST_OBJECT_UNLOCK (splitmux);
679
680     g_strfreev (files);
681     files = gst_split_util_find_files (dirname, basename, &err);
682
683     if (files == NULL || *files == NULL)
684       goto no_files;
685   }
686
687   SPLITMUX_SRC_LOCK (splitmux);
688   splitmux->pads_complete = FALSE;
689   splitmux->running = TRUE;
690   SPLITMUX_SRC_UNLOCK (splitmux);
691
692   splitmux->num_parts = g_strv_length (files);
693
694   splitmux->parts = g_new0 (GstSplitMuxPartReader *, splitmux->num_parts);
695
696   for (i = 0; i < splitmux->num_parts; i++) {
697     splitmux->parts[i] = gst_splitmux_part_create (splitmux, files[i]);
698     if (splitmux->parts[i] == NULL)
699       break;
700
701     /* Figure out the next offset - the smallest one */
702     gst_splitmux_part_reader_set_start_offset (splitmux->parts[i], next_offset);
703     if (!gst_splitmux_part_reader_prepare (splitmux->parts[i])) {
704       GST_WARNING_OBJECT (splitmux,
705           "Failed to prepare file part %s. Cannot play past there.", files[i]);
706       GST_ELEMENT_WARNING (splitmux, RESOURCE, READ, (NULL),
707           ("Failed to prepare file part %s. Cannot play past there.",
708               files[i]));
709       gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
710       g_object_unref (splitmux->parts[i]);
711       splitmux->parts[i] = NULL;
712       break;
713     }
714
715     /* Extend our total duration to cover this part */
716     total_duration =
717         next_offset +
718         gst_splitmux_part_reader_get_duration (splitmux->parts[i]);
719     splitmux->play_segment.duration = total_duration;
720
721     next_offset = gst_splitmux_part_reader_get_end_offset (splitmux->parts[i]);
722   }
723
724   /* Update total_duration state variable */
725   GST_OBJECT_LOCK (splitmux);
726   splitmux->total_duration = total_duration;
727   GST_OBJECT_UNLOCK (splitmux);
728
729   /* Store how many parts we actually created */
730   splitmux->num_parts = i;
731
732   if (splitmux->num_parts < 1)
733     goto failed_part;
734
735   /* All done preparing, activate the first part */
736   GST_INFO_OBJECT (splitmux,
737       "All parts prepared. Total duration %" GST_TIME_FORMAT
738       " Activating first part", GST_TIME_ARGS (total_duration));
739   ret = gst_splitmux_src_activate_part (splitmux, 0, GST_SEEK_FLAG_NONE);
740   if (ret == FALSE)
741     goto failed_first_part;
742 done:
743   if (err != NULL)
744     g_error_free (err);
745   g_strfreev (files);
746   g_free (basename);
747   g_free (dirname);
748
749   return ret;
750
751 /* ERRORS */
752 no_files:
753   {
754     GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, ("%s", err->message),
755         ("Failed to find files in '%s' for pattern '%s'",
756             GST_STR_NULL (dirname), GST_STR_NULL (basename)));
757     goto done;
758   }
759 failed_part:
760   {
761     GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
762         ("Failed to open any files for reading"));
763     goto done;
764   }
765 failed_first_part:
766   {
767     GST_ELEMENT_ERROR (splitmux, RESOURCE, OPEN_READ, (NULL),
768         ("Failed to activate first part for playback"));
769     goto done;
770   }
771 }
772
773 static gboolean
774 gst_splitmux_src_stop (GstSplitMuxSrc * splitmux)
775 {
776   gboolean ret = TRUE;
777   guint i;
778   GList *cur, *pads_list;
779
780   SPLITMUX_SRC_LOCK (splitmux);
781   if (!splitmux->running)
782     goto out;
783
784   GST_DEBUG_OBJECT (splitmux, "Stopping");
785
786   /* Stop and destroy all parts  */
787   for (i = 0; i < splitmux->num_parts; i++) {
788     if (splitmux->parts[i] == NULL)
789       continue;
790     gst_splitmux_part_reader_unprepare (splitmux->parts[i]);
791     g_object_unref (splitmux->parts[i]);
792     splitmux->parts[i] = NULL;
793   }
794
795   SPLITMUX_SRC_PADS_LOCK (splitmux);
796   pads_list = splitmux->pads;
797   splitmux->pads = NULL;
798   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
799
800   SPLITMUX_SRC_UNLOCK (splitmux);
801   for (cur = g_list_first (pads_list); cur != NULL; cur = g_list_next (cur)) {
802     SplitMuxSrcPad *tmp = (SplitMuxSrcPad *) (cur->data);
803     gst_pad_stop_task (GST_PAD (tmp));
804     gst_element_remove_pad (GST_ELEMENT (splitmux), GST_PAD (tmp));
805   }
806   g_list_free (pads_list);
807   SPLITMUX_SRC_LOCK (splitmux);
808
809   g_free (splitmux->parts);
810   splitmux->parts = NULL;
811   splitmux->num_parts = 0;
812   splitmux->running = FALSE;
813   splitmux->total_duration = GST_CLOCK_TIME_NONE;
814   /* Reset playback segment */
815   gst_segment_init (&splitmux->play_segment, GST_FORMAT_TIME);
816 out:
817   SPLITMUX_SRC_UNLOCK (splitmux);
818   return ret;
819 }
820
821 typedef struct
822 {
823   GstSplitMuxSrc *splitmux;
824   SplitMuxSrcPad *splitpad;
825 } SplitMuxAndPad;
826
827 static gboolean
828 handle_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
829 {
830   SplitMuxAndPad *splitmux_and_pad;
831   GstSplitMuxSrc *splitmux;
832   SplitMuxSrcPad *splitpad;
833
834   splitmux_and_pad = user_data;
835   splitmux = splitmux_and_pad->splitmux;
836   splitpad = splitmux_and_pad->splitpad;
837
838   GST_DEBUG_OBJECT (splitpad, "handle sticky event %" GST_PTR_FORMAT, *event);
839   gst_event_ref (*event);
840   gst_splitmux_handle_event (splitmux, splitpad, pad, *event);
841
842   return TRUE;
843 }
844
845 static GstPad *
846 gst_splitmux_find_output_pad (GstSplitMuxPartReader * part, GstPad * pad,
847     GstSplitMuxSrc * splitmux)
848 {
849   GList *cur;
850   gchar *pad_name = gst_pad_get_name (pad);
851   GstPad *target = NULL;
852   gboolean is_new_pad = FALSE;
853
854   SPLITMUX_SRC_LOCK (splitmux);
855   SPLITMUX_SRC_PADS_LOCK (splitmux);
856   for (cur = g_list_first (splitmux->pads);
857       cur != NULL; cur = g_list_next (cur)) {
858     GstPad *tmp = (GstPad *) (cur->data);
859     if (g_str_equal (GST_PAD_NAME (tmp), pad_name)) {
860       target = tmp;
861       break;
862     }
863   }
864
865   if (target == NULL && !splitmux->pads_complete) {
866     SplitMuxAndPad splitmux_and_pad;
867
868     /* No pad found, create one */
869     target = g_object_new (SPLITMUX_TYPE_SRC_PAD,
870         "name", pad_name, "direction", GST_PAD_SRC, NULL);
871     splitmux->pads = g_list_prepend (splitmux->pads, target);
872     splitmux->n_pads++;
873
874     gst_pad_set_active (target, TRUE);
875
876     splitmux_and_pad.splitmux = splitmux;
877     splitmux_and_pad.splitpad = (SplitMuxSrcPad *) target;
878     gst_pad_sticky_events_foreach (pad, handle_sticky_events,
879         &splitmux_and_pad);
880     is_new_pad = TRUE;
881   }
882   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
883   SPLITMUX_SRC_UNLOCK (splitmux);
884
885   g_free (pad_name);
886
887   if (target == NULL)
888     goto pad_not_found;
889
890   if (is_new_pad)
891     gst_element_add_pad (GST_ELEMENT_CAST (splitmux), target);
892
893   return target;
894
895 pad_not_found:
896   GST_ELEMENT_ERROR (splitmux, STREAM, FAILED, (NULL),
897       ("Stream part %s contains extra unknown pad %" GST_PTR_FORMAT,
898           part->path, pad));
899   return NULL;
900 }
901
902 static void
903 gst_splitmux_part_prepared (GstSplitMuxPartReader * reader,
904     GstSplitMuxSrc * splitmux)
905 {
906   gboolean need_no_more_pads;
907
908   GST_LOG_OBJECT (splitmux, "Part %" GST_PTR_FORMAT " prepared", reader);
909   SPLITMUX_SRC_LOCK (splitmux);
910   need_no_more_pads = !splitmux->pads_complete;
911   splitmux->pads_complete = TRUE;
912   SPLITMUX_SRC_UNLOCK (splitmux);
913
914   if (need_no_more_pads) {
915     GST_DEBUG_OBJECT (splitmux, "Signalling no-more-pads");
916     gst_element_no_more_pads (GST_ELEMENT_CAST (splitmux));
917   }
918 }
919
920 static void
921 gst_splitmux_push_event (GstSplitMuxSrc * splitmux, GstEvent * e,
922     guint32 seqnum)
923 {
924   GList *cur;
925
926   if (seqnum) {
927     e = gst_event_make_writable (e);
928     gst_event_set_seqnum (e, seqnum);
929   }
930
931   SPLITMUX_SRC_PADS_LOCK (splitmux);
932   for (cur = g_list_first (splitmux->pads);
933       cur != NULL; cur = g_list_next (cur)) {
934     GstPad *pad = GST_PAD_CAST (cur->data);
935     gst_event_ref (e);
936     gst_pad_push_event (pad, e);
937   }
938   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
939
940   gst_event_unref (e);
941 }
942
943 static void
944 gst_splitmux_push_flush_stop (GstSplitMuxSrc * splitmux, guint32 seqnum)
945 {
946   GstEvent *e = gst_event_new_flush_stop (TRUE);
947   GList *cur;
948
949   if (seqnum) {
950     e = gst_event_make_writable (e);
951     gst_event_set_seqnum (e, seqnum);
952   }
953
954   SPLITMUX_SRC_PADS_LOCK (splitmux);
955   for (cur = g_list_first (splitmux->pads);
956       cur != NULL; cur = g_list_next (cur)) {
957     SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
958
959     gst_event_ref (e);
960     gst_pad_push_event (GST_PAD_CAST (target), e);
961     target->sent_caps = FALSE;
962     target->sent_stream_start = FALSE;
963     target->sent_segment = FALSE;
964   }
965   SPLITMUX_SRC_PADS_UNLOCK (splitmux);
966
967   gst_event_unref (e);
968 }
969
970 /* Callback for when a part finishes and we need to move to the next */
971 static gboolean
972 gst_splitmux_end_of_part (GstSplitMuxSrc * splitmux, SplitMuxSrcPad * splitpad)
973 {
974   gint next_part = -1;
975   gint cur_part = splitpad->cur_part;
976   gboolean res = FALSE;
977
978   if (splitmux->play_segment.rate >= 0.0) {
979     if (cur_part + 1 < splitmux->num_parts)
980       next_part = cur_part + 1;
981     /* Make sure the transition is seamless */
982     splitpad->set_next_discont = FALSE;
983     splitpad->clear_next_discont = TRUE;
984   } else {
985     /* Reverse play - move to previous segment */
986     if (cur_part > 0) {
987       next_part = cur_part - 1;
988       /* Non-seamless transition in reverse */
989       splitpad->set_next_discont = TRUE;
990       splitpad->clear_next_discont = FALSE;
991     }
992   }
993
994   SPLITMUX_SRC_LOCK (splitmux);
995
996   /* If all pads are done with this part, deactivate it */
997   if (gst_splitmux_part_is_eos (splitmux->parts[splitpad->cur_part]))
998     gst_splitmux_part_reader_deactivate (splitmux->parts[cur_part]);
999
1000   if (splitmux->play_segment.rate >= 0.0) {
1001     if (splitmux->play_segment.stop != -1) {
1002       GstClockTime part_end =
1003           gst_splitmux_part_reader_get_end_offset (splitmux->parts[cur_part]);
1004       if (part_end >= splitmux->play_segment.stop) {
1005         GST_DEBUG_OBJECT (splitmux,
1006             "Stop position was within that part. Finishing");
1007         next_part = -1;
1008       }
1009     }
1010   } else {
1011     if (splitmux->play_segment.start != -1) {
1012       GstClockTime part_start =
1013           gst_splitmux_part_reader_get_start_offset (splitmux->parts[cur_part]);
1014       if (part_start <= splitmux->play_segment.start) {
1015         GST_DEBUG_OBJECT (splitmux,
1016             "Start position %" GST_TIME_FORMAT
1017             " was within that part. Finishing",
1018             GST_TIME_ARGS (splitmux->play_segment.start));
1019         next_part = -1;
1020       }
1021     }
1022   }
1023
1024   if (next_part != -1) {
1025     GST_DEBUG_OBJECT (splitmux, "At EOS on pad %" GST_PTR_FORMAT
1026         " moving to part %d", splitpad, next_part);
1027     splitpad->cur_part = next_part;
1028     splitpad->reader = splitmux->parts[splitpad->cur_part];
1029     if (splitpad->part_pad)
1030       gst_object_unref (splitpad->part_pad);
1031     splitpad->part_pad =
1032         gst_splitmux_part_reader_lookup_pad (splitpad->reader,
1033         (GstPad *) (splitpad));
1034
1035     if (splitmux->cur_part != next_part) {
1036       if (!gst_splitmux_part_reader_is_active (splitpad->reader)) {
1037         GstSegment tmp;
1038         /* If moving backward into a new part, set stop
1039          * to -1 to ensure we play the entire file - workaround
1040          * a bug in qtdemux that misses bits at the end */
1041         gst_segment_copy_into (&splitmux->play_segment, &tmp);
1042         if (tmp.rate < 0)
1043           tmp.stop = -1;
1044
1045         /* This is the first pad to move to the new part, activate it */
1046         GST_DEBUG_OBJECT (splitpad,
1047             "First pad to change part. Activating part %d with seg %"
1048             GST_SEGMENT_FORMAT, next_part, &tmp);
1049         if (!gst_splitmux_part_reader_activate (splitpad->reader, &tmp,
1050                 GST_SEEK_FLAG_NONE))
1051           goto error;
1052       }
1053       splitmux->cur_part = next_part;
1054     }
1055     res = TRUE;
1056   }
1057
1058   SPLITMUX_SRC_UNLOCK (splitmux);
1059   return res;
1060 error:
1061   SPLITMUX_SRC_UNLOCK (splitmux);
1062   GST_ELEMENT_ERROR (splitmux, RESOURCE, READ, (NULL),
1063       ("Failed to activate part %d", splitmux->cur_part));
1064   return FALSE;
1065 }
1066
1067 G_DEFINE_TYPE (SplitMuxSrcPad, splitmux_src_pad, GST_TYPE_PAD);
1068
1069 static void
1070 splitmux_src_pad_constructed (GObject * pad)
1071 {
1072   gst_pad_set_event_function (GST_PAD (pad),
1073       GST_DEBUG_FUNCPTR (splitmux_src_pad_event));
1074   gst_pad_set_query_function (GST_PAD (pad),
1075       GST_DEBUG_FUNCPTR (splitmux_src_pad_query));
1076
1077   G_OBJECT_CLASS (splitmux_src_pad_parent_class)->constructed (pad);
1078 }
1079
1080 static void
1081 gst_splitmux_src_pad_dispose (GObject * object)
1082 {
1083   SplitMuxSrcPad *pad = (SplitMuxSrcPad *) (object);
1084
1085   GST_OBJECT_LOCK (pad);
1086   if (pad->part_pad) {
1087     gst_object_unref (pad->part_pad);
1088     pad->part_pad = NULL;
1089   }
1090   GST_OBJECT_UNLOCK (pad);
1091
1092   G_OBJECT_CLASS (splitmux_src_pad_parent_class)->dispose (object);
1093 }
1094
1095 static void
1096 splitmux_src_pad_class_init (SplitMuxSrcPadClass * klass)
1097 {
1098   GObjectClass *gobject_klass = (GObjectClass *) (klass);
1099
1100   gobject_klass->constructed = splitmux_src_pad_constructed;
1101   gobject_klass->dispose = gst_splitmux_src_pad_dispose;
1102 }
1103
1104 static void
1105 splitmux_src_pad_init (SplitMuxSrcPad * pad)
1106 {
1107 }
1108
1109 /* Event handler for source pads. Proxy events into the child
1110  * parts as needed
1111  */
1112 static gboolean
1113 splitmux_src_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
1114 {
1115   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1116   gboolean ret = FALSE;
1117
1118   GST_DEBUG_OBJECT (parent, "event %" GST_PTR_FORMAT
1119       " on %" GST_PTR_FORMAT, event, pad);
1120
1121   switch (GST_EVENT_TYPE (event)) {
1122     case GST_EVENT_SEEK:{
1123       GstFormat format;
1124       gdouble rate;
1125       GstSeekFlags flags;
1126       GstSeekType start_type, stop_type;
1127       gint64 start, stop;
1128       guint32 seqnum;
1129       gint i;
1130       GstClockTime part_start, position;
1131       GList *cur;
1132       GstSegment tmp;
1133
1134       gst_event_parse_seek (event, &rate, &format, &flags,
1135           &start_type, &start, &stop_type, &stop);
1136
1137       if (format != GST_FORMAT_TIME) {
1138         GST_DEBUG_OBJECT (splitmux, "can only seek on TIME");
1139         goto error;
1140       }
1141       /* FIXME: Support non-flushing seeks, which might never wake up */
1142       if (!(flags & GST_SEEK_FLAG_FLUSH)) {
1143         GST_DEBUG_OBJECT (splitmux, "Only flushing seeks supported");
1144         goto error;
1145       }
1146       seqnum = gst_event_get_seqnum (event);
1147
1148       SPLITMUX_SRC_LOCK (splitmux);
1149       if (!splitmux->running || splitmux->num_parts < 1) {
1150         /* Not started yet */
1151         SPLITMUX_SRC_UNLOCK (splitmux);
1152         goto error;
1153       }
1154
1155       gst_segment_copy_into (&splitmux->play_segment, &tmp);
1156
1157       if (!gst_segment_do_seek (&tmp, rate,
1158               format, flags, start_type, start, stop_type, stop, NULL)) {
1159         /* Invalid seek requested, ignore it */
1160         SPLITMUX_SRC_UNLOCK (splitmux);
1161         goto error;
1162       }
1163       position = tmp.position;
1164
1165       GST_DEBUG_OBJECT (splitmux, "Performing seek with seg %"
1166           GST_SEGMENT_FORMAT, &tmp);
1167
1168       GST_DEBUG_OBJECT (splitmux,
1169           "Handling flushing seek. Sending flush start");
1170
1171       /* Send flush_start */
1172       gst_splitmux_push_event (splitmux, gst_event_new_flush_start (), seqnum);
1173
1174       /* Stop all parts, which will work because of the flush */
1175       SPLITMUX_SRC_PADS_LOCK (splitmux);
1176       SPLITMUX_SRC_UNLOCK (splitmux);
1177       for (cur = g_list_first (splitmux->pads);
1178           cur != NULL; cur = g_list_next (cur)) {
1179         SplitMuxSrcPad *target = (SplitMuxSrcPad *) (cur->data);
1180         GstSplitMuxPartReader *reader = splitmux->parts[target->cur_part];
1181         gst_splitmux_part_reader_deactivate (reader);
1182       }
1183
1184       /* Shut down pad tasks */
1185       GST_DEBUG_OBJECT (splitmux, "Pausing pad tasks");
1186       for (cur = g_list_first (splitmux->pads);
1187           cur != NULL; cur = g_list_next (cur)) {
1188         GstPad *splitpad = (GstPad *) (cur->data);
1189         gst_pad_pause_task (GST_PAD (splitpad));
1190       }
1191       SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1192       SPLITMUX_SRC_LOCK (splitmux);
1193
1194       /* Send flush stop */
1195       GST_DEBUG_OBJECT (splitmux, "Sending flush stop");
1196       gst_splitmux_push_flush_stop (splitmux, seqnum);
1197
1198       /* Everything is stopped, so update the play_segment */
1199       gst_segment_copy_into (&tmp, &splitmux->play_segment);
1200       splitmux->segment_seqnum = seqnum;
1201
1202       /* Work out where to start from now */
1203       for (i = 0; i < splitmux->num_parts; i++) {
1204         GstSplitMuxPartReader *reader = splitmux->parts[i];
1205         GstClockTime part_end =
1206             gst_splitmux_part_reader_get_end_offset (reader);
1207
1208         if (part_end > position)
1209           break;
1210       }
1211       if (i == splitmux->num_parts)
1212         i = splitmux->num_parts - 1;
1213
1214       part_start =
1215           gst_splitmux_part_reader_get_start_offset (splitmux->parts[i]);
1216
1217       GST_DEBUG_OBJECT (splitmux,
1218           "Seek to time %" GST_TIME_FORMAT " landed in part %d offset %"
1219           GST_TIME_FORMAT, GST_TIME_ARGS (position),
1220           i, GST_TIME_ARGS (position - part_start));
1221
1222       ret = gst_splitmux_src_activate_part (splitmux, i, flags);
1223       SPLITMUX_SRC_UNLOCK (splitmux);
1224     }
1225     default:
1226       break;
1227   }
1228
1229   gst_event_unref (event);
1230 error:
1231   return ret;
1232 }
1233
1234 static gboolean
1235 splitmux_src_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
1236 {
1237   /* Query handler for source pads. Proxy queries into the child
1238    * parts as needed
1239    */
1240   GstSplitMuxSrc *splitmux = GST_SPLITMUX_SRC (parent);
1241   gboolean ret = FALSE;
1242
1243   GST_LOG_OBJECT (parent, "query %" GST_PTR_FORMAT
1244       " on %" GST_PTR_FORMAT, query, pad);
1245   switch (GST_QUERY_TYPE (query)) {
1246     case GST_QUERY_CAPS:
1247     case GST_QUERY_POSITION:{
1248       GstSplitMuxPartReader *part;
1249       SplitMuxSrcPad *anypad;
1250
1251       SPLITMUX_SRC_LOCK (splitmux);
1252       SPLITMUX_SRC_PADS_LOCK (splitmux);
1253       anypad = (SplitMuxSrcPad *) (splitmux->pads->data);
1254       part = splitmux->parts[anypad->cur_part];
1255       ret = gst_splitmux_part_reader_src_query (part, pad, query);
1256       SPLITMUX_SRC_PADS_UNLOCK (splitmux);
1257       SPLITMUX_SRC_UNLOCK (splitmux);
1258       break;
1259     }
1260     case GST_QUERY_DURATION:{
1261       GstClockTime duration;
1262       GstFormat fmt;
1263
1264       gst_query_parse_duration (query, &fmt, NULL);
1265       if (fmt != GST_FORMAT_TIME)
1266         break;
1267
1268       GST_OBJECT_LOCK (splitmux);
1269       duration = splitmux->total_duration;
1270       GST_OBJECT_UNLOCK (splitmux);
1271
1272       if (duration > 0 && duration != GST_CLOCK_TIME_NONE) {
1273         gst_query_set_duration (query, GST_FORMAT_TIME, duration);
1274         ret = TRUE;
1275       }
1276       break;
1277     }
1278     case GST_QUERY_SEEKING:{
1279       GstFormat format;
1280
1281       gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
1282       if (format != GST_FORMAT_TIME)
1283         break;
1284
1285       GST_OBJECT_LOCK (splitmux);
1286       gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0,
1287           splitmux->total_duration);
1288       ret = TRUE;
1289       GST_OBJECT_UNLOCK (splitmux);
1290
1291       break;
1292     }
1293     default:
1294       break;
1295   }
1296   return ret;
1297 }
1298
1299
1300 gboolean
1301 register_splitmuxsrc (GstPlugin * plugin)
1302 {
1303   GST_DEBUG_CATEGORY_INIT (splitmux_debug, "splitmuxsrc", 0,
1304       "Split File Demuxing Source");
1305
1306   return gst_element_register (plugin, "splitmuxsrc", GST_RANK_NONE,
1307       GST_TYPE_SPLITMUX_SRC);
1308 }