plugins: Don't force 64-bit file/seek functions variants on android
[platform/upstream/gstreamer.git] / plugins / elements / gstconcat.c
1 /* GStreamer concat element
2  *
3  *  Copyright (c) 2014 Sebastian Dröge <sebastian@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21 /**
22  * SECTION:element-concat
23  * @title: concat
24  * @see_also: #GstFunnel
25  *
26  * Concatenates streams together to one continous stream.
27  *
28  * All streams but the current one are blocked until the current one
29  * finished with %GST_EVENT_EOS. Then the next stream is enabled, while
30  * keeping the running time continous for %GST_FORMAT_TIME segments or
31  * keeping the segment continous for %GST_FORMAT_BYTES segments.
32  *
33  * Streams are switched in the order in which the sinkpads were requested.
34  *
35  * By default, the stream segment's base values are adjusted to ensure
36  * the segment transitions between streams are continuous. In some cases,
37  * it may be desirable to turn off these adjustments (for example, because
38  * another downstream element like a streamsynchronizer adjusts the base
39  * values on its own). The adjust-base property can be used for this purpose.
40  *
41  * ## Example launch line
42  * |[
43  * gst-launch-1.0 concat name=c ! xvimagesink  videotestsrc num-buffers=100 ! c.   videotestsrc num-buffers=100 pattern=ball ! c.
44  * ]| Plays two video streams one after another.
45  *
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #include "config.h"
50 #endif
51
52 #include "gstconcat.h"
53
54 GST_DEBUG_CATEGORY_STATIC (gst_concat_debug);
55 #define GST_CAT_DEFAULT gst_concat_debug
56
57 G_GNUC_INTERNAL GType gst_concat_pad_get_type (void);
58
59 #define GST_TYPE_CONCAT_PAD (gst_concat_pad_get_type())
60 #define GST_CONCAT_PAD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_CONCAT_PAD, GstConcatPad))
61 #define GST_CONCAT_PAD_CAST(obj) ((GstConcatPad *)(obj))
62 #define GST_CONCAT_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_CONCAT_PAD, GstConcatPadClass))
63 #define GST_IS_CONCAT_PAD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_CONCAT_PAD))
64 #define GST_IS_CONCAT_PAD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_CONCAT_PAD))
65
66 typedef struct _GstConcatPad GstConcatPad;
67 typedef struct _GstConcatPadClass GstConcatPadClass;
68
69 struct _GstConcatPad
70 {
71   GstPad parent;
72
73   GstSegment segment;
74
75   /* Protected by the concat lock */
76   gboolean flushing;
77 };
78
79 struct _GstConcatPadClass
80 {
81   GstPadClass parent;
82 };
83
84 G_DEFINE_TYPE (GstConcatPad, gst_concat_pad, GST_TYPE_PAD);
85
86 static void
87 gst_concat_pad_class_init (GstConcatPadClass * klass)
88 {
89 }
90
91 static void
92 gst_concat_pad_init (GstConcatPad * self)
93 {
94   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
95   self->flushing = FALSE;
96 }
97
98 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
99     GST_PAD_SINK,
100     GST_PAD_REQUEST,
101     GST_STATIC_CAPS_ANY);
102
103 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
104     GST_PAD_SRC,
105     GST_PAD_ALWAYS,
106     GST_STATIC_CAPS_ANY);
107
108 enum
109 {
110   PROP_0,
111   PROP_ACTIVE_PAD,
112   PROP_ADJUST_BASE
113 };
114
115 #define DEFAULT_ADJUST_BASE TRUE
116
117 #define _do_init \
118   GST_DEBUG_CATEGORY_INIT (gst_concat_debug, "concat", 0, "concat element");
119 #define gst_concat_parent_class parent_class
120 G_DEFINE_TYPE_WITH_CODE (GstConcat, gst_concat, GST_TYPE_ELEMENT, _do_init);
121
122 static void gst_concat_dispose (GObject * object);
123 static void gst_concat_finalize (GObject * object);
124 static void gst_concat_get_property (GObject * object,
125     guint prop_id, GValue * value, GParamSpec * pspec);
126 static void gst_concat_set_property (GObject * object,
127     guint prop_id, const GValue * value, GParamSpec * pspec);
128
129 static GstStateChangeReturn gst_concat_change_state (GstElement * element,
130     GstStateChange transition);
131 static GstPad *gst_concat_request_new_pad (GstElement * element,
132     GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
133 static void gst_concat_release_pad (GstElement * element, GstPad * pad);
134
135 static GstFlowReturn gst_concat_sink_chain (GstPad * pad, GstObject * parent,
136     GstBuffer * buffer);
137 static gboolean gst_concat_sink_event (GstPad * pad, GstObject * parent,
138     GstEvent * event);
139 static gboolean gst_concat_sink_query (GstPad * pad, GstObject * parent,
140     GstQuery * query);
141
142 static gboolean gst_concat_src_event (GstPad * pad, GstObject * parent,
143     GstEvent * event);
144 static gboolean gst_concat_src_query (GstPad * pad, GstObject * parent,
145     GstQuery * query);
146
147 static gboolean gst_concat_switch_pad (GstConcat * self);
148
149 static void gst_concat_notify_active_pad (GstConcat * self);
150
151 static GParamSpec *pspec_active_pad = NULL;
152
153 static void
154 gst_concat_class_init (GstConcatClass * klass)
155 {
156   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
157   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
158
159   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_concat_dispose);
160   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_concat_finalize);
161
162   gobject_class->get_property = gst_concat_get_property;
163   gobject_class->set_property = gst_concat_set_property;
164
165   pspec_active_pad = g_param_spec_object ("active-pad", "Active pad",
166       "Currently active src pad", GST_TYPE_PAD, G_PARAM_READABLE |
167       G_PARAM_STATIC_STRINGS);
168   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
169       pspec_active_pad);
170   g_object_class_install_property (gobject_class, PROP_ADJUST_BASE,
171       g_param_spec_boolean ("adjust-base", "Adjust segment base",
172           "Adjust the base value of segments to ensure they are adjacent",
173           DEFAULT_ADJUST_BASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174
175   gst_element_class_set_static_metadata (gstelement_class,
176       "Concat", "Generic", "Concatenate multiple streams",
177       "Sebastian Dröge <sebastian@centricular.com>");
178
179   gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
180   gst_element_class_add_static_pad_template (gstelement_class, &src_template);
181
182   gstelement_class->request_new_pad =
183       GST_DEBUG_FUNCPTR (gst_concat_request_new_pad);
184   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_concat_release_pad);
185   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_concat_change_state);
186 }
187
188 static void
189 gst_concat_init (GstConcat * self)
190 {
191   g_mutex_init (&self->lock);
192   g_cond_init (&self->cond);
193
194   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
195   gst_pad_set_event_function (self->srcpad,
196       GST_DEBUG_FUNCPTR (gst_concat_src_event));
197   gst_pad_set_query_function (self->srcpad,
198       GST_DEBUG_FUNCPTR (gst_concat_src_query));
199   gst_pad_use_fixed_caps (self->srcpad);
200
201   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
202
203   self->adjust_base = DEFAULT_ADJUST_BASE;
204 }
205
206 static void
207 gst_concat_dispose (GObject * object)
208 {
209   GstConcat *self = GST_CONCAT (object);
210   GList *item;
211
212   gst_object_replace ((GstObject **) & self->current_sinkpad, NULL);
213
214 restart:
215   for (item = GST_ELEMENT_PADS (object); item; item = g_list_next (item)) {
216     GstPad *pad = GST_PAD (item->data);
217
218     if (GST_PAD_IS_SINK (pad)) {
219       gst_element_release_request_pad (GST_ELEMENT (object), pad);
220       goto restart;
221     }
222   }
223
224   G_OBJECT_CLASS (parent_class)->dispose (object);
225 }
226
227 static void
228 gst_concat_finalize (GObject * object)
229 {
230   GstConcat *self = GST_CONCAT (object);
231
232   g_mutex_clear (&self->lock);
233   g_cond_clear (&self->cond);
234
235   G_OBJECT_CLASS (parent_class)->finalize (object);
236 }
237
238 static void
239 gst_concat_get_property (GObject * object, guint prop_id, GValue * value,
240     GParamSpec * pspec)
241 {
242   GstConcat *self = GST_CONCAT (object);
243
244   switch (prop_id) {
245     case PROP_ACTIVE_PAD:{
246       g_mutex_lock (&self->lock);
247       g_value_set_object (value, self->current_sinkpad);
248       g_mutex_unlock (&self->lock);
249       break;
250     }
251     case PROP_ADJUST_BASE:{
252       g_mutex_lock (&self->lock);
253       g_value_set_boolean (value, self->adjust_base);
254       g_mutex_unlock (&self->lock);
255       break;
256     }
257     default:
258       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259       break;
260   }
261 }
262
263 static void
264 gst_concat_set_property (GObject * object, guint prop_id, const GValue * value,
265     GParamSpec * pspec)
266 {
267   GstConcat *self = GST_CONCAT (object);
268
269   switch (prop_id) {
270     case PROP_ADJUST_BASE:{
271       g_mutex_lock (&self->lock);
272       self->adjust_base = g_value_get_boolean (value);
273       g_mutex_unlock (&self->lock);
274       break;
275     }
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279   }
280 }
281
282 static GstPad *
283 gst_concat_request_new_pad (GstElement * element, GstPadTemplate * templ,
284     const gchar * name, const GstCaps * caps)
285 {
286   GstConcat *self = GST_CONCAT (element);
287   GstPad *sinkpad;
288   gchar *pad_name;
289   gboolean do_notify = FALSE;
290
291   GST_DEBUG_OBJECT (element, "requesting pad");
292
293   g_mutex_lock (&self->lock);
294   pad_name = g_strdup_printf ("sink_%u", self->pad_count);
295   self->pad_count++;
296   g_mutex_unlock (&self->lock);
297
298   sinkpad = GST_PAD_CAST (g_object_new (GST_TYPE_CONCAT_PAD,
299           "name", pad_name, "direction", templ->direction, "template", templ,
300           NULL));
301   g_free (pad_name);
302
303   gst_pad_set_chain_function (sinkpad,
304       GST_DEBUG_FUNCPTR (gst_concat_sink_chain));
305   gst_pad_set_event_function (sinkpad,
306       GST_DEBUG_FUNCPTR (gst_concat_sink_event));
307   gst_pad_set_query_function (sinkpad,
308       GST_DEBUG_FUNCPTR (gst_concat_sink_query));
309   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
310   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
311
312   gst_pad_set_active (sinkpad, TRUE);
313
314   g_mutex_lock (&self->lock);
315   self->sinkpads = g_list_prepend (self->sinkpads, gst_object_ref (sinkpad));
316   if (!self->current_sinkpad) {
317     do_notify = TRUE;
318     self->current_sinkpad = gst_object_ref (sinkpad);
319   }
320   g_mutex_unlock (&self->lock);
321
322   gst_element_add_pad (element, sinkpad);
323
324   if (do_notify)
325     gst_concat_notify_active_pad (self);
326
327   return sinkpad;
328 }
329
330 static void
331 gst_concat_release_pad (GstElement * element, GstPad * pad)
332 {
333   GstConcat *self = GST_CONCAT (element);
334   GstConcatPad *spad = GST_CONCAT_PAD_CAST (pad);
335   GList *l;
336   gboolean current_pad_removed = FALSE;
337   gboolean eos = FALSE;
338   gboolean do_notify = FALSE;
339
340   GST_DEBUG_OBJECT (self, "releasing pad");
341
342   g_mutex_lock (&self->lock);
343   spad->flushing = TRUE;
344   g_cond_broadcast (&self->cond);
345   g_mutex_unlock (&self->lock);
346
347   gst_pad_set_active (pad, FALSE);
348
349   /* Now the pad is definitely not running anymore */
350
351   g_mutex_lock (&self->lock);
352   if (self->current_sinkpad == GST_PAD_CAST (spad)) {
353     eos = !gst_concat_switch_pad (self);
354     current_pad_removed = TRUE;
355     do_notify = TRUE;
356   }
357
358   for (l = self->sinkpads; l; l = l->next) {
359     if ((gpointer) spad == l->data) {
360       gst_object_unref (spad);
361       self->sinkpads = g_list_delete_link (self->sinkpads, l);
362       break;
363     }
364   }
365   g_mutex_unlock (&self->lock);
366
367   gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
368
369   if (do_notify)
370     gst_concat_notify_active_pad (self);
371
372   if (GST_STATE (self) > GST_STATE_READY) {
373     if (current_pad_removed && !eos)
374       gst_element_post_message (GST_ELEMENT_CAST (self),
375           gst_message_new_duration_changed (GST_OBJECT_CAST (self)));
376
377     /* FIXME: Sending EOS from application thread */
378     if (eos)
379       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
380   }
381 }
382
383 /* Returns FALSE if flushing
384  * Must be called from the pad's streaming thread
385  */
386 static gboolean
387 gst_concat_pad_wait (GstConcatPad * spad, GstConcat * self)
388 {
389   g_mutex_lock (&self->lock);
390   if (spad->flushing) {
391     g_mutex_unlock (&self->lock);
392     GST_DEBUG_OBJECT (spad, "Flushing");
393     return FALSE;
394   }
395
396   while (spad != GST_CONCAT_PAD_CAST (self->current_sinkpad)) {
397     GST_TRACE_OBJECT (spad, "Not the current sinkpad - waiting");
398     if (self->current_sinkpad == NULL && g_list_length (self->sinkpads) == 1) {
399       GST_LOG_OBJECT (spad, "Sole pad waiting, switching");
400       /* If we are the only sinkpad, take active pad ownership */
401       self->current_sinkpad = gst_object_ref (self->sinkpads->data);
402       break;
403     }
404     g_cond_wait (&self->cond, &self->lock);
405     if (spad->flushing) {
406       g_mutex_unlock (&self->lock);
407       GST_DEBUG_OBJECT (spad, "Flushing");
408       return FALSE;
409     }
410   }
411   /* This pad can only become not the current sinkpad from
412    * a) This streaming thread (we hold the stream lock)
413    * b) Releasing the pad (takes the stream lock, see above)
414    *
415    * Unlocking here is thus safe and we can safely push
416    * serialized data to our srcpad
417    */
418   GST_DEBUG_OBJECT (spad, "Now the current sinkpad");
419   g_mutex_unlock (&self->lock);
420
421   return TRUE;
422 }
423
424 static GstFlowReturn
425 gst_concat_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
426 {
427   GstFlowReturn ret;
428   GstConcat *self = GST_CONCAT (parent);
429   GstConcatPad *spad = GST_CONCAT_PAD (pad);
430
431   GST_LOG_OBJECT (pad, "received buffer %p", buffer);
432
433   if (!gst_concat_pad_wait (spad, self))
434     return GST_FLOW_FLUSHING;
435
436   if (self->last_stop == GST_CLOCK_TIME_NONE)
437     self->last_stop = spad->segment.start;
438
439   if (self->format == GST_FORMAT_TIME) {
440     GstClockTime start_time = GST_BUFFER_TIMESTAMP (buffer);
441     GstClockTime end_time = GST_CLOCK_TIME_NONE;
442
443     if (start_time != GST_CLOCK_TIME_NONE)
444       end_time = start_time;
445     if (GST_BUFFER_DURATION_IS_VALID (buffer))
446       end_time += GST_BUFFER_DURATION (buffer);
447
448     if (end_time != GST_CLOCK_TIME_NONE && end_time > self->last_stop)
449       self->last_stop = end_time;
450   } else {
451     self->last_stop += gst_buffer_get_size (buffer);
452   }
453
454   ret = gst_pad_push (self->srcpad, buffer);
455
456   GST_LOG_OBJECT (pad, "handled buffer %s", gst_flow_get_name (ret));
457
458   return ret;
459 }
460
461 /* Returns FALSE if no further pad, must be called with concat lock */
462 static gboolean
463 gst_concat_switch_pad (GstConcat * self)
464 {
465   GList *l;
466   gboolean next;
467   GstSegment segment;
468   gint64 last_stop;
469
470   segment = GST_CONCAT_PAD (self->current_sinkpad)->segment;
471
472   last_stop = self->last_stop;
473   if (last_stop == GST_CLOCK_TIME_NONE)
474     last_stop = segment.stop;
475   if (last_stop == GST_CLOCK_TIME_NONE)
476     last_stop = segment.start;
477   g_assert (last_stop != GST_CLOCK_TIME_NONE);
478
479   if (last_stop > segment.stop)
480     last_stop = segment.stop;
481
482   if (segment.format == GST_FORMAT_TIME)
483     last_stop =
484         gst_segment_to_running_time (&segment, segment.format, last_stop);
485   else
486     last_stop += segment.start;
487
488   self->current_start_offset += last_stop;
489
490   for (l = self->sinkpads; l; l = l->next) {
491     if ((gpointer) self->current_sinkpad == l->data) {
492       l = l->prev;
493       GST_DEBUG_OBJECT (self,
494           "Switching from pad %" GST_PTR_FORMAT " to %" GST_PTR_FORMAT,
495           self->current_sinkpad, l ? l->data : NULL);
496       gst_object_unref (self->current_sinkpad);
497       self->current_sinkpad = l ? gst_object_ref (l->data) : NULL;
498       g_cond_broadcast (&self->cond);
499       break;
500     }
501   }
502
503   next = self->current_sinkpad != NULL;
504
505   self->last_stop = GST_CLOCK_TIME_NONE;
506
507   return next;
508 }
509
510 static void
511 gst_concat_notify_active_pad (GstConcat * self)
512 {
513   g_object_notify_by_pspec ((GObject *) self, pspec_active_pad);
514 }
515
516 static gboolean
517 gst_concat_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
518 {
519   GstConcat *self = GST_CONCAT (parent);
520   GstConcatPad *spad = GST_CONCAT_PAD_CAST (pad);
521   gboolean ret = TRUE;
522
523   GST_LOG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
524
525   switch (GST_EVENT_TYPE (event)) {
526     case GST_EVENT_STREAM_START:{
527       if (!gst_concat_pad_wait (spad, self)) {
528         ret = FALSE;
529         gst_event_unref (event);
530       } else {
531         ret = gst_pad_event_default (pad, parent, event);
532       }
533       break;
534     }
535     case GST_EVENT_SEGMENT:{
536       gboolean adjust_base;
537
538       /* Drop segment event, we create our own one */
539       gst_event_copy_segment (event, &spad->segment);
540       gst_event_unref (event);
541
542       g_mutex_lock (&self->lock);
543       adjust_base = self->adjust_base;
544       if (self->format == GST_FORMAT_UNDEFINED) {
545         if (spad->segment.format != GST_FORMAT_TIME
546             && spad->segment.format != GST_FORMAT_BYTES) {
547           g_mutex_unlock (&self->lock);
548           GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
549               ("Can only operate in TIME or BYTES format"));
550           ret = FALSE;
551           break;
552         }
553         self->format = spad->segment.format;
554         GST_DEBUG_OBJECT (self, "Operating in %s format",
555             gst_format_get_name (self->format));
556         g_mutex_unlock (&self->lock);
557       } else if (self->format != spad->segment.format) {
558         g_mutex_unlock (&self->lock);
559         GST_ELEMENT_ERROR (self, CORE, FAILED, (NULL),
560             ("Operating in %s format but new pad has %s",
561                 gst_format_get_name (self->format),
562                 gst_format_get_name (spad->segment.format)));
563         ret = FALSE;
564       } else {
565         g_mutex_unlock (&self->lock);
566       }
567
568       if (!gst_concat_pad_wait (spad, self)) {
569         ret = FALSE;
570       } else {
571         GstSegment segment = spad->segment;
572
573         if (adjust_base) {
574           /* We know no duration */
575           segment.duration = -1;
576
577           /* Update segment values to be continous with last stream */
578           if (self->format == GST_FORMAT_TIME) {
579             segment.base += self->current_start_offset;
580           } else {
581             /* Shift start/stop byte position */
582             segment.start += self->current_start_offset;
583             if (segment.stop != -1)
584               segment.stop += self->current_start_offset;
585           }
586         }
587
588         gst_pad_push_event (self->srcpad, gst_event_new_segment (&segment));
589       }
590       break;
591     }
592     case GST_EVENT_EOS:{
593       gst_event_unref (event);
594
595       if (!gst_concat_pad_wait (spad, self)) {
596         ret = FALSE;
597       } else {
598         gboolean next;
599
600         g_mutex_lock (&self->lock);
601         next = gst_concat_switch_pad (self);
602         g_mutex_unlock (&self->lock);
603         ret = TRUE;
604
605         gst_concat_notify_active_pad (self);
606
607         if (!next) {
608           gst_pad_push_event (self->srcpad, gst_event_new_eos ());
609         } else {
610           gst_element_post_message (GST_ELEMENT_CAST (self),
611               gst_message_new_duration_changed (GST_OBJECT_CAST (self)));
612         }
613       }
614       break;
615     }
616     case GST_EVENT_FLUSH_START:{
617       gboolean forward;
618
619       g_mutex_lock (&self->lock);
620       spad->flushing = TRUE;
621       g_cond_broadcast (&self->cond);
622       forward = (self->current_sinkpad == GST_PAD_CAST (spad));
623       if (!forward && g_list_length (self->sinkpads) == 1)
624         forward = TRUE;
625       g_mutex_unlock (&self->lock);
626
627       if (forward)
628         ret = gst_pad_event_default (pad, parent, event);
629       else
630         gst_event_unref (event);
631       break;
632     }
633     case GST_EVENT_FLUSH_STOP:{
634       gboolean forward;
635
636       gst_segment_init (&spad->segment, GST_FORMAT_UNDEFINED);
637       spad->flushing = FALSE;
638
639       g_mutex_lock (&self->lock);
640       forward = (self->current_sinkpad == GST_PAD_CAST (spad));
641       if (!forward && g_list_length (self->sinkpads) == 1)
642         forward = TRUE;
643       g_mutex_unlock (&self->lock);
644
645       if (forward) {
646         gboolean reset_time;
647
648         gst_event_parse_flush_stop (event, &reset_time);
649         if (reset_time) {
650           GST_DEBUG_OBJECT (self,
651               "resetting start offset to 0 after flushing with reset_time = TRUE");
652           self->current_start_offset = 0;
653         }
654         ret = gst_pad_event_default (pad, parent, event);
655       } else {
656         gst_event_unref (event);
657       }
658       break;
659     }
660     default:{
661       /* Wait for other serialized events before forwarding */
662       if (GST_EVENT_IS_SERIALIZED (event) && !gst_concat_pad_wait (spad, self)) {
663         gst_event_unref (event);
664         ret = FALSE;
665       } else {
666         ret = gst_pad_event_default (pad, parent, event);
667       }
668       break;
669     }
670   }
671
672   return ret;
673 }
674
675 static gboolean
676 gst_concat_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
677 {
678   GstConcat *self = GST_CONCAT (parent);
679   GstConcatPad *spad = GST_CONCAT_PAD_CAST (pad);
680   gboolean ret = TRUE;
681
682   GST_LOG_OBJECT (pad, "received query %" GST_PTR_FORMAT, query);
683
684   switch (GST_QUERY_TYPE (query)) {
685     default:
686       /* Wait for other serialized queries before forwarding */
687       if (GST_QUERY_IS_SERIALIZED (query) && !gst_concat_pad_wait (spad, self)) {
688         ret = FALSE;
689       } else {
690         ret = gst_pad_query_default (pad, parent, query);
691       }
692       break;
693   }
694
695   return ret;
696 }
697
698 static gboolean
699 gst_concat_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
700 {
701   GstConcat *self = GST_CONCAT (parent);
702   gboolean ret = TRUE;
703
704   GST_LOG_OBJECT (pad, "received event %" GST_PTR_FORMAT, event);
705
706   switch (GST_EVENT_TYPE (event)) {
707     case GST_EVENT_SEEK:{
708       GstPad *sinkpad = NULL;
709
710       g_mutex_lock (&self->lock);
711       if ((sinkpad = self->current_sinkpad))
712         gst_object_ref (sinkpad);
713       /* If no current active sinkpad but only one sinkpad, try reactivating that pad */
714       if (sinkpad == NULL && g_list_length (self->sinkpads) == 1) {
715         sinkpad = gst_object_ref (self->sinkpads->data);
716       }
717       g_mutex_unlock (&self->lock);
718       if (sinkpad) {
719         ret = gst_pad_push_event (sinkpad, event);
720         gst_object_unref (sinkpad);
721       } else {
722         gst_event_unref (event);
723         ret = FALSE;
724       }
725       break;
726     }
727     case GST_EVENT_QOS:{
728       GstQOSType type;
729       GstClockTimeDiff diff;
730       GstClockTime timestamp;
731       gdouble proportion;
732       GstPad *sinkpad = NULL;
733
734       g_mutex_lock (&self->lock);
735       if ((sinkpad = self->current_sinkpad))
736         gst_object_ref (sinkpad);
737       g_mutex_unlock (&self->lock);
738
739       if (sinkpad) {
740         gst_event_parse_qos (event, &type, &proportion, &diff, &timestamp);
741         gst_event_unref (event);
742
743         if (timestamp != GST_CLOCK_TIME_NONE
744             && timestamp > self->current_start_offset) {
745           timestamp -= self->current_start_offset;
746           event = gst_event_new_qos (type, proportion, diff, timestamp);
747           ret = gst_pad_push_event (self->current_sinkpad, event);
748         } else {
749           ret = FALSE;
750         }
751         gst_object_unref (sinkpad);
752       } else {
753         gst_event_unref (event);
754         ret = FALSE;
755       }
756       break;
757     }
758     case GST_EVENT_FLUSH_STOP:{
759       gboolean reset_time;
760
761       gst_event_parse_flush_stop (event, &reset_time);
762       if (reset_time) {
763         GST_DEBUG_OBJECT (self,
764             "resetting start offset to 0 after flushing with reset_time = TRUE");
765         self->current_start_offset = 0;
766       }
767
768       ret = gst_pad_event_default (pad, parent, event);
769       break;
770     }
771     default:
772       ret = gst_pad_event_default (pad, parent, event);
773       break;
774   }
775
776   return ret;
777 }
778
779 static gboolean
780 gst_concat_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
781 {
782   gboolean ret = TRUE;
783
784   GST_LOG_OBJECT (pad, "received query %" GST_PTR_FORMAT, query);
785
786   switch (GST_QUERY_TYPE (query)) {
787     default:
788       ret = gst_pad_query_default (pad, parent, query);
789       break;
790   }
791
792   return ret;
793 }
794
795 static void
796 reset_pad (const GValue * data, gpointer user_data)
797 {
798   GstPad *pad = g_value_get_object (data);
799   GstConcatPad *spad = GST_CONCAT_PAD_CAST (pad);
800
801   gst_segment_init (&spad->segment, GST_FORMAT_UNDEFINED);
802   spad->flushing = FALSE;
803 }
804
805 static void
806 unblock_pad (const GValue * data, gpointer user_data)
807 {
808   GstPad *pad = g_value_get_object (data);
809   GstConcatPad *spad = GST_CONCAT_PAD_CAST (pad);
810
811   spad->flushing = TRUE;
812 }
813
814 static GstStateChangeReturn
815 gst_concat_change_state (GstElement * element, GstStateChange transition)
816 {
817   GstConcat *self = GST_CONCAT (element);
818   GstStateChangeReturn ret;
819
820   switch (transition) {
821     case GST_STATE_CHANGE_READY_TO_PAUSED:{
822       GstIterator *iter = gst_element_iterate_sink_pads (element);
823       GstIteratorResult res;
824
825       self->format = GST_FORMAT_UNDEFINED;
826       self->current_start_offset = 0;
827       self->last_stop = GST_CLOCK_TIME_NONE;
828
829       while ((res =
830               gst_iterator_foreach (iter, reset_pad,
831                   NULL)) == GST_ITERATOR_RESYNC)
832         gst_iterator_resync (iter);
833       gst_iterator_free (iter);
834
835       if (res == GST_ITERATOR_ERROR)
836         return GST_STATE_CHANGE_FAILURE;
837       break;
838     }
839     case GST_STATE_CHANGE_PAUSED_TO_READY:{
840       GstIterator *iter = gst_element_iterate_sink_pads (element);
841       GstIteratorResult res;
842
843       g_mutex_lock (&self->lock);
844       while ((res =
845               gst_iterator_foreach (iter, unblock_pad,
846                   NULL)) == GST_ITERATOR_RESYNC)
847         gst_iterator_resync (iter);
848       gst_iterator_free (iter);
849       g_cond_broadcast (&self->cond);
850       g_mutex_unlock (&self->lock);
851
852       if (res == GST_ITERATOR_ERROR)
853         return GST_STATE_CHANGE_FAILURE;
854
855       break;
856     }
857     default:
858       break;
859   }
860
861   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
862
863   return ret;
864 }