input-selector: small code cleanups
[platform/upstream/gstreamer.git] / plugins / elements / gstinputselector.c
1 /* GStreamer input selector
2  * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3  * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4  * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
5  * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
6  * Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
7  * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
8  * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 /**
27  * SECTION:element-input-selector
28  * @see_also: #GstOutputSelector
29  *
30  * Direct one out of N input streams to the output pad.
31  *
32  * The input pads are from a GstPad subclass and have additional
33  * properties, which users may find useful, namely:
34  *
35  * <itemizedlist>
36  * <listitem>
37  * "running-time": Running time of stream on pad (#gint64)
38  * </listitem>
39  * <listitem>
40  * "tags": The currently active tags on the pad (#GstTagList, boxed type)
41  * </listitem>
42  * <listitem>
43  * "active": If the pad is currently active (#gboolean)
44  * </listitem>
45  * <listitem>
46  * "always-ok" : Make an inactive pads return #GST_FLOW_OK instead of
47  * #GST_FLOW_NOT_LINKED
48  * </listitem>
49  * </itemizedlist>
50  */
51
52 #ifdef HAVE_CONFIG_H
53 #include "config.h"
54 #endif
55
56 #include <string.h>
57
58 #include "gstinputselector.h"
59
60 #define DEBUG_CACHED_BUFFERS 0
61
62 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
63 #define GST_CAT_DEFAULT input_selector_debug
64
65 #define GST_TYPE_INPUT_SELECTOR_SYNC_MODE (gst_input_selector_sync_mode_get_type())
66 static GType
67 gst_input_selector_sync_mode_get_type (void)
68 {
69   static GType type = 0;
70   static const GEnumValue data[] = {
71     {GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT,
72           "Sync using the current active segment",
73         "active-segment"},
74     {GST_INPUT_SELECTOR_SYNC_MODE_CLOCK, "Sync using the clock", "clock"},
75     {0, NULL, NULL},
76   };
77
78   if (!type) {
79     type = g_enum_register_static ("GstInputSelectorSyncMode", data);
80   }
81   return type;
82 }
83
84 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (&((GstInputSelector*)(sel))->lock)
85 #define GST_INPUT_SELECTOR_GET_COND(sel) (&((GstInputSelector*)(sel))->cond)
86 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
87 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
88 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
89                         GST_INPUT_SELECTOR_GET_LOCK(sel)))
90 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
91
92 static GstStaticPadTemplate gst_input_selector_sink_factory =
93 GST_STATIC_PAD_TEMPLATE ("sink_%u",
94     GST_PAD_SINK,
95     GST_PAD_REQUEST,
96     GST_STATIC_CAPS_ANY);
97
98 static GstStaticPadTemplate gst_input_selector_src_factory =
99 GST_STATIC_PAD_TEMPLATE ("src",
100     GST_PAD_SRC,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS_ANY);
103
104 enum
105 {
106   PROP_0,
107   PROP_N_PADS,
108   PROP_ACTIVE_PAD,
109   PROP_SYNC_STREAMS,
110   PROP_SYNC_MODE,
111   PROP_CACHE_BUFFERS
112 };
113
114 #define DEFAULT_SYNC_STREAMS TRUE
115 #define DEFAULT_SYNC_MODE GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT
116 #define DEFAULT_CACHE_BUFFERS FALSE
117 #define DEFAULT_PAD_ALWAYS_OK TRUE
118
119 enum
120 {
121   PROP_PAD_0,
122   PROP_PAD_RUNNING_TIME,
123   PROP_PAD_TAGS,
124   PROP_PAD_ACTIVE,
125   PROP_PAD_ALWAYS_OK
126 };
127
128 enum
129 {
130   /* methods */
131   SIGNAL_BLOCK,
132   SIGNAL_SWITCH,
133   LAST_SIGNAL
134 };
135 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
136
137 static void gst_input_selector_active_pad_changed (GstInputSelector * sel,
138     GParamSpec * pspec, gpointer user_data);
139 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
140     sel, GstPad * pad);
141 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
142     GstPad * pad);
143 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
144     GstPad * pad, gboolean strict);
145
146 #define GST_TYPE_SELECTOR_PAD \
147   (gst_selector_pad_get_type())
148 #define GST_SELECTOR_PAD(obj) \
149   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
150 #define GST_SELECTOR_PAD_CLASS(klass) \
151   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
152 #define GST_IS_SELECTOR_PAD(obj) \
153   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
154 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
155   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
156 #define GST_SELECTOR_PAD_CAST(obj) \
157   ((GstSelectorPad *)(obj))
158
159 typedef struct _GstSelectorPad GstSelectorPad;
160 typedef struct _GstSelectorPadClass GstSelectorPadClass;
161 typedef struct _GstSelectorPadCachedBuffer GstSelectorPadCachedBuffer;
162
163 struct _GstSelectorPad
164 {
165   GstPad parent;
166
167   gboolean active;              /* when buffer have passed the pad */
168   gboolean pushed;              /* when buffer was pushed downstream since activation */
169   gboolean eos;                 /* when EOS has been received */
170   gboolean eos_sent;            /* when EOS was sent downstream */
171   gboolean discont;             /* after switching we create a discont */
172   gboolean flushing;            /* set after flush-start and before flush-stop */
173   gboolean always_ok;
174   GstTagList *tags;             /* last tags received on the pad */
175
176   GstSegment segment;           /* the current segment on the pad */
177   guint32 segment_seqnum;       /* sequence number of the current segment */
178
179   gboolean events_pending;      /* TRUE if sticky events need to be updated */
180
181   gboolean sending_cached_buffers;
182   GQueue *cached_buffers;
183 };
184
185 struct _GstSelectorPadCachedBuffer
186 {
187   GstBuffer *buffer;
188   GstSegment segment;
189 };
190
191 struct _GstSelectorPadClass
192 {
193   GstPadClass parent;
194 };
195
196 GType gst_selector_pad_get_type (void);
197 static void gst_selector_pad_finalize (GObject * object);
198 static void gst_selector_pad_get_property (GObject * object,
199     guint prop_id, GValue * value, GParamSpec * pspec);
200 static void gst_selector_pad_set_property (GObject * object,
201     guint prop_id, const GValue * value, GParamSpec * pspec);
202
203 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
204 static void gst_selector_pad_reset (GstSelectorPad * pad);
205 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
206     GstEvent * event);
207 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
208     GstQuery * query);
209 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
210     GstObject * parent);
211 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
212     GstBuffer * buf);
213 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
214     GstBuffer * buffer);
215 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
216
217 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
218
219 static void
220 gst_selector_pad_class_init (GstSelectorPadClass * klass)
221 {
222   GObjectClass *gobject_class;
223
224   gobject_class = (GObjectClass *) klass;
225
226   gobject_class->finalize = gst_selector_pad_finalize;
227
228   gobject_class->get_property = gst_selector_pad_get_property;
229   gobject_class->set_property = gst_selector_pad_set_property;
230
231   g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
232       g_param_spec_int64 ("running-time", "Running time",
233           "Running time of stream on pad", 0, G_MAXINT64, 0,
234           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
235   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
236       g_param_spec_boxed ("tags", "Tags",
237           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
238           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
239   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
240       g_param_spec_boolean ("active", "Active",
241           "If the pad is currently active", FALSE,
242           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
243   /* FIXME: better property name? */
244   g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
245       g_param_spec_boolean ("always-ok", "Always OK",
246           "Make an inactive pad return OK instead of NOT_LINKED",
247           DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 }
249
250 static void
251 gst_selector_pad_init (GstSelectorPad * pad)
252 {
253   pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
254   gst_selector_pad_reset (pad);
255 }
256
257 static void
258 gst_selector_pad_finalize (GObject * object)
259 {
260   GstSelectorPad *pad;
261
262   pad = GST_SELECTOR_PAD_CAST (object);
263
264   if (pad->tags)
265     gst_tag_list_unref (pad->tags);
266   gst_selector_pad_free_cached_buffers (pad);
267
268   G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
269 }
270
271 static void
272 gst_selector_pad_set_property (GObject * object, guint prop_id,
273     const GValue * value, GParamSpec * pspec)
274 {
275   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
276
277   switch (prop_id) {
278     case PROP_PAD_ALWAYS_OK:
279       GST_OBJECT_LOCK (object);
280       spad->always_ok = g_value_get_boolean (value);
281       GST_OBJECT_UNLOCK (object);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286   }
287 }
288
289 static void
290 gst_selector_pad_get_property (GObject * object, guint prop_id,
291     GValue * value, GParamSpec * pspec)
292 {
293   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
294
295   switch (prop_id) {
296     case PROP_PAD_RUNNING_TIME:
297       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
298       break;
299     case PROP_PAD_TAGS:
300       GST_OBJECT_LOCK (object);
301       g_value_set_boxed (value, spad->tags);
302       GST_OBJECT_UNLOCK (object);
303       break;
304     case PROP_PAD_ACTIVE:
305     {
306       GstInputSelector *sel;
307
308       sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
309       if (sel) {
310         g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
311                 GST_PAD_CAST (spad)));
312         gst_object_unref (sel);
313       } else {
314         g_value_set_boolean (value, FALSE);
315       }
316       break;
317     }
318     case PROP_PAD_ALWAYS_OK:
319       GST_OBJECT_LOCK (object);
320       g_value_set_boolean (value, spad->always_ok);
321       GST_OBJECT_UNLOCK (object);
322       break;
323     default:
324       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
325       break;
326   }
327 }
328
329 static gint64
330 gst_selector_pad_get_running_time (GstSelectorPad * pad)
331 {
332   gint64 ret = 0;
333
334   GST_OBJECT_LOCK (pad);
335   if (pad->active) {
336     ret =
337         gst_segment_to_running_time (&pad->segment, pad->segment.format,
338         pad->segment.position);
339   }
340   GST_OBJECT_UNLOCK (pad);
341
342   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
343       " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
344
345   return ret;
346 }
347
348 /* must be called with the SELECTOR_LOCK */
349 static void
350 gst_selector_pad_reset (GstSelectorPad * pad)
351 {
352   GST_OBJECT_LOCK (pad);
353   pad->active = FALSE;
354   pad->pushed = FALSE;
355   pad->eos = FALSE;
356   pad->eos_sent = FALSE;
357   pad->events_pending = FALSE;
358   pad->discont = FALSE;
359   pad->flushing = FALSE;
360   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
361   pad->sending_cached_buffers = FALSE;
362   gst_selector_pad_free_cached_buffers (pad);
363   GST_OBJECT_UNLOCK (pad);
364 }
365
366 static GstSelectorPadCachedBuffer *
367 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
368 {
369   GstSelectorPadCachedBuffer *cached_buffer =
370       g_slice_new (GstSelectorPadCachedBuffer);
371   cached_buffer->buffer = buffer;
372   cached_buffer->segment = selpad->segment;
373   return cached_buffer;
374 }
375
376 static void
377 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
378 {
379   gst_buffer_unref (cached_buffer->buffer);
380   g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
381 }
382
383 /* must be called with the SELECTOR_LOCK */
384 static void
385 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
386 {
387   if (selpad->segment.format != GST_FORMAT_TIME) {
388     GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
389         "not caching", buffer);
390     return;
391   }
392
393   GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
394   if (!selpad->cached_buffers)
395     selpad->cached_buffers = g_queue_new ();
396   g_queue_push_tail (selpad->cached_buffers,
397       gst_selector_pad_new_cached_buffer (selpad, buffer));
398 }
399
400 /* must be called with the SELECTOR_LOCK */
401 static void
402 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
403 {
404   if (!selpad->cached_buffers)
405     return;
406
407   GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
408   g_queue_free_full (selpad->cached_buffers,
409       (GDestroyNotify) gst_selector_pad_free_cached_buffer);
410   selpad->cached_buffers = NULL;
411 }
412
413 /* strictly get the linked pad from the sinkpad. If the pad is active we return
414  * the srcpad else we return NULL */
415 static GstIterator *
416 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
417 {
418   GstInputSelector *sel;
419   GstPad *otherpad;
420   GstIterator *it = NULL;
421   GValue val = { 0, };
422
423   sel = GST_INPUT_SELECTOR (parent);
424
425   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
426   if (otherpad) {
427     g_value_init (&val, GST_TYPE_PAD);
428     g_value_set_object (&val, otherpad);
429     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
430     g_value_unset (&val);
431     gst_object_unref (otherpad);
432   }
433
434   return it;
435 }
436
437 static gboolean
438 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
439 {
440   gboolean res = TRUE;
441   gboolean forward;
442   gboolean new_tags = FALSE;
443   GstInputSelector *sel;
444   GstSelectorPad *selpad;
445   GstPad *prev_active_sinkpad;
446   GstPad *active_sinkpad;
447
448   sel = GST_INPUT_SELECTOR (parent);
449   selpad = GST_SELECTOR_PAD_CAST (pad);
450   GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
451
452   GST_INPUT_SELECTOR_LOCK (sel);
453   prev_active_sinkpad =
454       sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
455   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
456   GST_INPUT_SELECTOR_UNLOCK (sel);
457
458   if (prev_active_sinkpad != active_sinkpad) {
459     if (prev_active_sinkpad)
460       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
461     g_object_notify (G_OBJECT (active_sinkpad), "active");
462     g_object_notify (G_OBJECT (sel), "active-pad");
463   }
464   if (prev_active_sinkpad)
465     gst_object_unref (prev_active_sinkpad);
466
467   GST_INPUT_SELECTOR_LOCK (sel);
468
469   /* only forward if we are dealing with the active sinkpad */
470   forward = (pad == active_sinkpad);
471
472   switch (GST_EVENT_TYPE (event)) {
473     case GST_EVENT_STREAM_START:{
474       guint group_id;
475
476       if (!gst_event_parse_group_id (event, &group_id))
477         sel->have_group_id = FALSE;
478       break;
479     }
480     case GST_EVENT_FLUSH_START:
481       /* Unblock the pad if it's waiting */
482       selpad->flushing = TRUE;
483       GST_INPUT_SELECTOR_BROADCAST (sel);
484       break;
485     case GST_EVENT_FLUSH_STOP:
486       gst_selector_pad_reset (selpad);
487       break;
488     case GST_EVENT_SEGMENT:
489     {
490       gst_event_copy_segment (event, &selpad->segment);
491       selpad->segment_seqnum = gst_event_get_seqnum (event);
492
493       GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
494           &selpad->segment);
495       break;
496     }
497     case GST_EVENT_TAG:
498     {
499       GstTagList *tags, *oldtags, *newtags;
500
501       gst_event_parse_tag (event, &tags);
502
503       GST_OBJECT_LOCK (selpad);
504       oldtags = selpad->tags;
505
506       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
507       selpad->tags = newtags;
508       GST_OBJECT_UNLOCK (selpad);
509
510       if (oldtags)
511         gst_tag_list_unref (oldtags);
512       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
513
514       new_tags = TRUE;
515       break;
516     }
517     case GST_EVENT_EOS:
518       selpad->eos = TRUE;
519
520       if (forward) {
521         selpad->eos_sent = TRUE;
522       } else {
523         GstSelectorPad *active_selpad;
524
525         /* If the active sinkpad is in EOS state but EOS
526          * was not sent downstream this means that the pad
527          * got EOS before it was set as active pad and that
528          * the previously active pad got EOS after it was
529          * active
530          */
531         active_selpad = GST_SELECTOR_PAD (active_sinkpad);
532         forward = (active_selpad->eos && !active_selpad->eos_sent);
533         active_selpad->eos_sent = TRUE;
534       }
535       GST_DEBUG_OBJECT (pad, "received EOS");
536       break;
537     case GST_EVENT_GAP:{
538       GstClockTime ts, dur;
539
540       GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
541
542       gst_event_parse_gap (event, &ts, &dur);
543       if (GST_CLOCK_TIME_IS_VALID (ts)) {
544         if (GST_CLOCK_TIME_IS_VALID (dur))
545           ts += dur;
546
547         /* update the segment position */
548         GST_OBJECT_LOCK (pad);
549         selpad->segment.position = ts;
550         GST_OBJECT_UNLOCK (pad);
551         if (sel->sync_streams && active_sinkpad == pad)
552           GST_INPUT_SELECTOR_BROADCAST (sel);
553       }
554
555     }
556       break;
557     default:
558       break;
559   }
560   GST_INPUT_SELECTOR_UNLOCK (sel);
561   if (new_tags)
562     g_object_notify (G_OBJECT (selpad), "tags");
563   if (forward) {
564     GST_DEBUG_OBJECT (pad, "forwarding event");
565     res = gst_pad_push_event (sel->srcpad, event);
566   } else {
567     /* If we aren't forwarding the event because the pad is not the
568      * active_sinkpad, then set the flag on the pad
569      * that says a segment needs sending if/when that pad is activated.
570      * For all other cases, we send the event immediately, which makes
571      * sparse streams and other segment updates work correctly downstream.
572      */
573     if (GST_EVENT_IS_STICKY (event))
574       selpad->events_pending = TRUE;
575     gst_event_unref (event);
576   }
577
578   return res;
579 }
580
581 static gboolean
582 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
583 {
584   gboolean res = FALSE;
585   GstInputSelector *self = (GstInputSelector *) parent;
586
587   switch (GST_QUERY_TYPE (query)) {
588     case GST_QUERY_CAPS:
589       /* always proxy caps query, regardless of active pad or not */
590       res = gst_pad_peer_query (self->srcpad, query);
591       break;
592     case GST_QUERY_ALLOCATION:{
593       GstPad *active_sinkpad;
594       GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
595
596       /* Only do the allocation query for the active sinkpad,
597        * after switching a reconfigure event is sent and upstream
598        * should reconfigure and do a new allocation query
599        */
600       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
601         GST_INPUT_SELECTOR_LOCK (sel);
602         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
603         GST_INPUT_SELECTOR_UNLOCK (sel);
604
605         if (pad != active_sinkpad) {
606           res = FALSE;
607           goto done;
608         }
609       }
610     }
611       /* fall through */
612     default:
613       res = gst_pad_query_default (pad, parent, query);
614       break;
615   }
616
617 done:
618   return res;
619 }
620
621 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
622  * or return TRUE when flushing */
623 static gboolean
624 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
625 {
626   while (self->blocked && !self->flushing && !pad->flushing) {
627     /* we can be unlocked here when we are shutting down (flushing) or when we
628      * get unblocked */
629     GST_INPUT_SELECTOR_WAIT (self);
630   }
631   return self->flushing;
632 }
633
634 /* must be called without the SELECTOR_LOCK, will wait until the running time
635  * of the active pad is after this pad or return TRUE when flushing */
636 static gboolean
637 gst_input_selector_wait_running_time (GstInputSelector * sel,
638     GstSelectorPad * selpad, GstBuffer * buf)
639 {
640   GstSegment *seg;
641
642   GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
643
644   /* If we have no valid timestamp we can't sync this buffer */
645   if (!GST_BUFFER_PTS_IS_VALID (buf)) {
646     GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
647         "invalid timestamp");
648     return FALSE;
649   }
650
651   seg = &selpad->segment;
652
653   /* Wait until
654    *   a) this is the active pad
655    *   b) the pad or the selector is flushing
656    *   c) the selector is not blocked
657    *   d) the buffer running time is before the current running time
658    *      (either active-seg or clock, depending on sync-mode)
659    */
660
661   GST_INPUT_SELECTOR_LOCK (sel);
662   while (TRUE) {
663     GstPad *active_sinkpad;
664     GstSelectorPad *active_selpad;
665     GstClock *clock;
666     gint64 cur_running_time;
667     GstClockTime running_time;
668
669     active_sinkpad =
670         gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
671     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
672
673     if (seg->format != GST_FORMAT_TIME) {
674       GST_DEBUG_OBJECT (selpad,
675           "Not waiting because we don't have a TIME segment");
676       GST_INPUT_SELECTOR_UNLOCK (sel);
677       return FALSE;
678     }
679
680     running_time = GST_BUFFER_PTS (buf);
681     /* If possible try to get the running time at the end of the buffer */
682     if (GST_BUFFER_DURATION_IS_VALID (buf))
683       running_time += GST_BUFFER_DURATION (buf);
684     /* Only use the segment to convert to running time if the segment is
685      * in TIME format, otherwise do our best to try to sync */
686     if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
687       if (running_time > seg->stop) {
688         running_time = seg->stop;
689       }
690     }
691     running_time =
692         gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
693     /* If this is outside the segment don't sync */
694     if (running_time == -1) {
695       GST_DEBUG_OBJECT (selpad,
696           "Not waiting because buffer is outside segment");
697       GST_INPUT_SELECTOR_UNLOCK (sel);
698       return FALSE;
699     }
700
701     cur_running_time = GST_CLOCK_TIME_NONE;
702     if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
703       clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
704       if (clock) {
705         GstClockTime base_time;
706
707         cur_running_time = gst_clock_get_time (clock);
708         base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
709         if (base_time <= cur_running_time)
710           cur_running_time -= base_time;
711         else
712           cur_running_time = 0;
713
714         gst_object_unref (clock);
715       }
716     } else {
717       GstSegment *active_seg;
718
719       active_seg = &active_selpad->segment;
720
721       /* If the active segment is configured but not to time format
722        * we can't do any syncing at all */
723       if (active_seg->format != GST_FORMAT_TIME
724           && active_seg->format != GST_FORMAT_UNDEFINED) {
725         GST_DEBUG_OBJECT (selpad,
726             "Not waiting because active segment isn't in TIME format");
727         GST_INPUT_SELECTOR_UNLOCK (sel);
728         return FALSE;
729       }
730
731       /* Get active pad's running time, if no configured segment yet keep at -1 */
732       if (active_seg->format == GST_FORMAT_TIME)
733         cur_running_time = gst_segment_to_running_time (active_seg,
734             GST_FORMAT_TIME, active_seg->position);
735     }
736
737     if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
738         (sel->blocked || cur_running_time == -1
739             || running_time >= cur_running_time)) {
740       if (!sel->blocked) {
741         GST_DEBUG_OBJECT (selpad,
742             "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
743             GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
744             GST_TIME_ARGS (cur_running_time));
745       } else
746         GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
747
748       GST_INPUT_SELECTOR_WAIT (sel);
749     } else {
750       GST_INPUT_SELECTOR_UNLOCK (sel);
751       break;
752     }
753   }
754
755   /* Return TRUE if the selector or the pad is flushing */
756   return (sel->flushing || selpad->flushing);
757 }
758
759 static gboolean
760 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
761 {
762   GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
763
764   if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
765     GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
766     GstEvent *e;
767
768     e = gst_event_new_segment (seg);
769     gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
770
771     gst_pad_push_event (sel->srcpad, e);
772   } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
773       && !sel->have_group_id) {
774     GstEvent *tmp =
775         gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
776
777     /* Only push stream-start once if not all our streams have a stream-id */
778     if (!tmp) {
779       gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
780     } else {
781       gst_event_unref (tmp);
782     }
783   } else {
784     gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
785   }
786
787   return TRUE;
788 }
789
790 #if DEBUG_CACHED_BUFFERS
791 static void
792 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
793 {
794   GList *walk;
795
796   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
797     GstSelectorPad *selpad;
798     GString *timestamps;
799     gchar *str;
800     int i;
801
802     selpad = GST_SELECTOR_PAD_CAST (walk->data);
803     if (!selpad->cached_buffers) {
804       GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
805       continue;
806     }
807
808     timestamps = g_string_new ("Cached buffers timestamps:");
809     for (i = 0; i < selpad->cached_buffers->length; ++i) {
810       GstSelectorPadCachedBuffer *cached_buffer;
811
812       cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
813       g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
814           GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
815     }
816     str = g_string_free (timestamps, FALSE);
817     GST_DEBUG_OBJECT (selpad, "%s", str);
818     g_free (str);
819   }
820 }
821 #endif
822
823 /* must be called with the SELECTOR_LOCK */
824 static void
825 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
826     GstPad * pad)
827 {
828   GstClock *clock;
829   gint64 cur_running_time;
830   GList *walk;
831
832   cur_running_time = GST_CLOCK_TIME_NONE;
833   if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
834     clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
835     if (clock) {
836       GstClockTime base_time;
837
838       cur_running_time = gst_clock_get_time (clock);
839       base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
840       if (base_time <= cur_running_time)
841         cur_running_time -= base_time;
842       else
843         cur_running_time = 0;
844
845       gst_object_unref (clock);
846     }
847   } else {
848     GstPad *active_sinkpad;
849     GstSelectorPad *active_selpad;
850     GstSegment *active_seg;
851
852     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
853     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
854     active_seg = &active_selpad->segment;
855
856     /* Get active pad's running time, if no configured segment yet keep at -1 */
857     if (active_seg->format == GST_FORMAT_TIME)
858       cur_running_time = gst_segment_to_running_time (active_seg,
859           GST_FORMAT_TIME, active_seg->position);
860   }
861
862   if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
863     return;
864
865   GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
866   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
867     GstSelectorPad *selpad;
868     GstSegment *seg;
869     GstSelectorPadCachedBuffer *cached_buffer;
870     GSList *maybe_remove;
871     guint queue_position;
872
873     selpad = GST_SELECTOR_PAD_CAST (walk->data);
874     if (!selpad->cached_buffers)
875       continue;
876
877     seg = &selpad->segment;
878
879     maybe_remove = NULL;
880     queue_position = 0;
881     while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
882                 queue_position))) {
883       GstBuffer *buffer = cached_buffer->buffer;
884       GstClockTime running_time;
885       GSList *l;
886
887       /* If we have no valid timestamp we can't sync this buffer */
888       if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
889         maybe_remove = g_slist_append (maybe_remove, cached_buffer);
890         queue_position = g_slist_length (maybe_remove);
891         continue;
892       }
893
894       /* the buffer is still valid if its duration is valid and the
895        * timestamp + duration is >= time, or if its duration is invalid
896        * and the timestamp is >= time */
897       running_time = GST_BUFFER_PTS (buffer);
898       /* If possible try to get the running time at the end of the buffer */
899       if (GST_BUFFER_DURATION_IS_VALID (buffer))
900         running_time += GST_BUFFER_DURATION (buffer);
901       /* Only use the segment to convert to running time if the segment is
902        * in TIME format, otherwise do our best to try to sync */
903       if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
904         if (running_time > seg->stop) {
905           running_time = seg->stop;
906         }
907       }
908       running_time =
909           gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
910
911       GST_DEBUG_OBJECT (selpad,
912           "checking if buffer %p running time=%" GST_TIME_FORMAT
913           " >= stream time=%" GST_TIME_FORMAT, buffer,
914           GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
915       if (running_time >= cur_running_time) {
916         break;
917       }
918
919       GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
920       g_queue_pop_nth (selpad->cached_buffers, queue_position);
921       gst_selector_pad_free_cached_buffer (cached_buffer);
922
923       for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
924         /* A buffer after some invalid buffers was removed, it means the invalid buffers
925          * are old, lets also remove them */
926         cached_buffer = l->data;
927         g_queue_remove (selpad->cached_buffers, cached_buffer);
928         gst_selector_pad_free_cached_buffer (cached_buffer);
929       }
930
931       g_slist_free (maybe_remove);
932       maybe_remove = NULL;
933       queue_position = 0;
934     }
935
936     g_slist_free (maybe_remove);
937     maybe_remove = NULL;
938
939     if (g_queue_is_empty (selpad->cached_buffers)) {
940       g_queue_free (selpad->cached_buffers);
941       selpad->cached_buffers = NULL;
942     }
943   }
944
945 #if DEBUG_CACHED_BUFFERS
946   gst_input_selector_debug_cached_buffers (sel);
947 #endif
948 }
949
950 static GstFlowReturn
951 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
952 {
953   GstInputSelector *sel;
954   GstFlowReturn res;
955   GstPad *active_sinkpad;
956   GstPad *prev_active_sinkpad = NULL;
957   GstSelectorPad *selpad;
958
959   sel = GST_INPUT_SELECTOR (parent);
960   selpad = GST_SELECTOR_PAD_CAST (pad);
961
962   GST_DEBUG_OBJECT (selpad,
963       "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
964       GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
965
966   GST_INPUT_SELECTOR_LOCK (sel);
967   /* wait or check for flushing */
968   if (gst_input_selector_wait (sel, selpad)) {
969     GST_INPUT_SELECTOR_UNLOCK (sel);
970     goto flushing;
971   }
972
973   GST_LOG_OBJECT (pad, "getting active pad");
974
975   prev_active_sinkpad =
976       sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
977   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
978
979   /* In sync mode wait until the active pad has advanced
980    * after the running time of the current buffer */
981   if (sel->sync_streams) {
982     /* call chain for each cached buffer if we are not the active pad
983      * or if we are the active pad but didn't push anything yet. */
984     if (active_sinkpad != pad || !selpad->pushed) {
985       /* no need to check for sel->cache_buffers as selpad->cached_buffers
986        * will only be valid if cache_buffers is TRUE */
987       if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
988         GstSelectorPadCachedBuffer *cached_buffer;
989         GstSegment saved_segment;
990
991         saved_segment = selpad->segment;
992
993         selpad->sending_cached_buffers = TRUE;
994         while (!sel->flushing && !selpad->flushing &&
995             (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
996           GST_DEBUG_OBJECT (pad, "Cached buffers found, "
997               "invoking chain for cached buffer %p", cached_buffer->buffer);
998
999           selpad->segment = cached_buffer->segment;
1000           selpad->events_pending = TRUE;
1001           GST_INPUT_SELECTOR_UNLOCK (sel);
1002           gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
1003           GST_INPUT_SELECTOR_LOCK (sel);
1004
1005           /* we may have cleaned up the queue in the meantime because of
1006            * old buffers */
1007           if (!selpad->cached_buffers) {
1008             break;
1009           }
1010         }
1011         selpad->sending_cached_buffers = FALSE;
1012
1013         /* all cached buffers sent, restore segment for current buffer */
1014         selpad->segment = saved_segment;
1015         selpad->events_pending = TRUE;
1016
1017         /* Might have changed while calling chain for cached buffers */
1018         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1019       }
1020     }
1021
1022     if (active_sinkpad != pad) {
1023       GST_INPUT_SELECTOR_UNLOCK (sel);
1024       if (gst_input_selector_wait_running_time (sel, selpad, buf))
1025         goto flushing;
1026       GST_INPUT_SELECTOR_LOCK (sel);
1027     }
1028
1029     /* Might have changed while waiting */
1030     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1031   }
1032
1033   /* update the segment on the srcpad */
1034   if (GST_BUFFER_PTS_IS_VALID (buf)) {
1035     GstClockTime start_time = GST_BUFFER_PTS (buf);
1036
1037     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1038         GST_TIME_ARGS (start_time));
1039     if (GST_BUFFER_DURATION_IS_VALID (buf))
1040       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1041           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1042
1043     GST_OBJECT_LOCK (pad);
1044     selpad->segment.position = start_time;
1045     GST_OBJECT_UNLOCK (pad);
1046   }
1047
1048   /* Ignore buffers from pads except the selected one */
1049   if (pad != active_sinkpad)
1050     goto ignore;
1051
1052   /* Tell all non-active pads that we advanced the running time */
1053   if (sel->sync_streams)
1054     GST_INPUT_SELECTOR_BROADCAST (sel);
1055
1056   GST_INPUT_SELECTOR_UNLOCK (sel);
1057
1058   if (prev_active_sinkpad != active_sinkpad) {
1059     if (prev_active_sinkpad)
1060       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1061     g_object_notify (G_OBJECT (active_sinkpad), "active");
1062     g_object_notify (G_OBJECT (sel), "active-pad");
1063   }
1064
1065   /* if we have a pending events, push them now */
1066   if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1067           || selpad->events_pending)) {
1068     gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1069         sel);
1070     selpad->events_pending = FALSE;
1071   }
1072
1073   if (prev_active_sinkpad) {
1074     gst_object_unref (prev_active_sinkpad);
1075     prev_active_sinkpad = NULL;
1076   }
1077
1078   if (selpad->discont) {
1079     buf = gst_buffer_make_writable (buf);
1080
1081     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1082     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1083     selpad->discont = FALSE;
1084   }
1085
1086   /* forward */
1087   GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1088       buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1089
1090   /* Only make the buffer read-only when necessary */
1091   if (sel->sync_streams && sel->cache_buffers)
1092     buf = gst_buffer_ref (buf);
1093   res = gst_pad_push (sel->srcpad, buf);
1094   GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1095
1096   GST_INPUT_SELECTOR_LOCK (sel);
1097
1098   if (sel->sync_streams && sel->cache_buffers) {
1099     /* Might have changed while pushing */
1100     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1101     /* only set pad to pushed if we are still the active pad */
1102     if (active_sinkpad == pad)
1103       selpad->pushed = TRUE;
1104
1105     /* cache buffer as we may need it again if we change pads */
1106     gst_selector_pad_cache_buffer (selpad, buf);
1107     gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1108   } else {
1109     selpad->pushed = TRUE;
1110   }
1111   GST_INPUT_SELECTOR_UNLOCK (sel);
1112
1113 done:
1114
1115   if (prev_active_sinkpad)
1116     gst_object_unref (prev_active_sinkpad);
1117   prev_active_sinkpad = NULL;
1118
1119   return res;
1120
1121   /* dropped buffers */
1122 ignore:
1123   {
1124     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1125
1126     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1127     /* when we drop a buffer, we're creating a discont on this pad */
1128     selpad->discont = TRUE;
1129     GST_INPUT_SELECTOR_UNLOCK (sel);
1130     gst_buffer_unref (buf);
1131
1132     /* figure out what to return upstream */
1133     GST_OBJECT_LOCK (selpad);
1134     if (selpad->always_ok || !active_pad_pushed)
1135       res = GST_FLOW_OK;
1136     else
1137       res = GST_FLOW_NOT_LINKED;
1138     GST_OBJECT_UNLOCK (selpad);
1139
1140     goto done;
1141   }
1142 flushing:
1143   {
1144     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1145     gst_buffer_unref (buf);
1146     res = GST_FLOW_FLUSHING;
1147     goto done;
1148   }
1149 }
1150
1151 static void gst_input_selector_dispose (GObject * object);
1152 static void gst_input_selector_finalize (GObject * object);
1153
1154 static void gst_input_selector_set_property (GObject * object,
1155     guint prop_id, const GValue * value, GParamSpec * pspec);
1156 static void gst_input_selector_get_property (GObject * object,
1157     guint prop_id, GValue * value, GParamSpec * pspec);
1158
1159 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1160     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1161 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1162
1163 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1164     element, GstStateChange transition);
1165
1166 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1167     GstEvent * event);
1168 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1169     GstQuery * query);
1170 static gint64 gst_input_selector_block (GstInputSelector * self);
1171
1172 #define _do_init \
1173     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1174         "input-selector", 0, "An input stream selector element");
1175 #define gst_input_selector_parent_class parent_class
1176 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1177     _do_init);
1178
1179 static void
1180 gst_input_selector_class_init (GstInputSelectorClass * klass)
1181 {
1182   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1183   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1184
1185   gobject_class->dispose = gst_input_selector_dispose;
1186   gobject_class->finalize = gst_input_selector_finalize;
1187
1188   gobject_class->set_property = gst_input_selector_set_property;
1189   gobject_class->get_property = gst_input_selector_get_property;
1190
1191   g_object_class_install_property (gobject_class, PROP_N_PADS,
1192       g_param_spec_uint ("n-pads", "Number of Pads",
1193           "The number of sink pads", 0, G_MAXUINT, 0,
1194           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1195
1196   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1197       g_param_spec_object ("active-pad", "Active pad",
1198           "The currently active sink pad", GST_TYPE_PAD,
1199           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
1200           G_PARAM_STATIC_STRINGS));
1201
1202   /**
1203    * GstInputSelector:sync-streams
1204    *
1205    * If set to %TRUE all inactive streams will be synced to the
1206    * running time of the active stream or to the current clock.
1207    *
1208    * To make sure no buffers are dropped by input-selector
1209    * that might be needed when switching the active pad,
1210    * sync-mode should be set to "clock" and cache-buffers to TRUE.
1211    */
1212   g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1213       g_param_spec_boolean ("sync-streams", "Sync Streams",
1214           "Synchronize inactive streams to the running time of the active "
1215           "stream or to the current clock",
1216           DEFAULT_SYNC_STREAMS,
1217           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1218           GST_PARAM_MUTABLE_READY));
1219
1220   /**
1221    * GstInputSelector:sync-mode
1222    *
1223    * Select how input-selector will sync buffers when in sync-streams mode.
1224    *
1225    * Note that when using the "active-segment" mode, the "active-segment" may
1226    * be ahead of current clock time when switching the active pad, as the current
1227    * active pad may have pushed more buffers than what was displayed/consumed,
1228    * which may cause delays and some missing buffers.
1229    */
1230   g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1231       g_param_spec_enum ("sync-mode", "Sync mode",
1232           "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1233           DEFAULT_SYNC_MODE,
1234           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1235           GST_PARAM_MUTABLE_READY));
1236
1237   /**
1238    * GstInputSelector:cache-buffers
1239    *
1240    * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1241    * the active pad will cache the buffers still considered valid (after current
1242    * running time, see sync-mode) to avoid missing frames if/when the pad is
1243    * reactivated.
1244    *
1245    * The active pad may push more buffers than what is currently displayed/consumed
1246    * and when changing pads those buffers will be discarded and the only way to
1247    * reactivate that pad without loosing the already consumed buffers is to enable cache.
1248    */
1249   g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1250       g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1251           "Cache buffers for active-pad",
1252           DEFAULT_CACHE_BUFFERS,
1253           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1254           GST_PARAM_MUTABLE_READY));
1255
1256   /**
1257    * GstInputSelector::block:
1258    * @inputselector: the #GstInputSelector
1259    *
1260    * Block all sink pads in preparation for a switch. Returns the stop time of
1261    * the current switch segment, as a running time, or 0 if there is no current
1262    * active pad or the current active pad never received data.
1263    */
1264   gst_input_selector_signals[SIGNAL_BLOCK] =
1265       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1266       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1267       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1268       g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1269
1270   gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1271       "Generic", "N-to-1 input stream selector",
1272       "Julien Moutte <julien@moutte.net>, "
1273       "Jan Schmidt <thaytan@mad.scientist.com>, "
1274       "Wim Taymans <wim.taymans@gmail.com>");
1275   gst_element_class_add_pad_template (gstelement_class,
1276       gst_static_pad_template_get (&gst_input_selector_sink_factory));
1277   gst_element_class_add_pad_template (gstelement_class,
1278       gst_static_pad_template_get (&gst_input_selector_src_factory));
1279
1280   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1281   gstelement_class->release_pad = gst_input_selector_release_pad;
1282   gstelement_class->change_state = gst_input_selector_change_state;
1283
1284   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1285 }
1286
1287 static void
1288 gst_input_selector_init (GstInputSelector * sel)
1289 {
1290   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1291   gst_pad_set_iterate_internal_links_function (sel->srcpad,
1292       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1293   gst_pad_set_query_function (sel->srcpad,
1294       GST_DEBUG_FUNCPTR (gst_input_selector_query));
1295   gst_pad_set_event_function (sel->srcpad,
1296       GST_DEBUG_FUNCPTR (gst_input_selector_event));
1297   GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1298   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1299   /* sinkpad management */
1300   sel->active_sinkpad = NULL;
1301   sel->padcount = 0;
1302   sel->sync_streams = DEFAULT_SYNC_STREAMS;
1303   sel->have_group_id = TRUE;
1304
1305   g_mutex_init (&sel->lock);
1306   g_cond_init (&sel->cond);
1307   sel->blocked = FALSE;
1308
1309   /* lets give a change for downstream to do something on
1310    * active-pad change before we start pushing new buffers */
1311   g_signal_connect_data (sel, "notify::active-pad",
1312       (GCallback) gst_input_selector_active_pad_changed, NULL,
1313       NULL, G_CONNECT_AFTER);
1314 }
1315
1316 static void
1317 gst_input_selector_dispose (GObject * object)
1318 {
1319   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1320
1321   if (sel->active_sinkpad) {
1322     gst_object_unref (sel->active_sinkpad);
1323     sel->active_sinkpad = NULL;
1324   }
1325   G_OBJECT_CLASS (parent_class)->dispose (object);
1326 }
1327
1328 static void
1329 gst_input_selector_finalize (GObject * object)
1330 {
1331   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1332
1333   g_mutex_clear (&sel->lock);
1334   g_cond_clear (&sel->cond);
1335
1336   G_OBJECT_CLASS (parent_class)->finalize (object);
1337 }
1338
1339 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1340  * active pad changed. */
1341 static gboolean
1342 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1343 {
1344   GstSelectorPad *old, *new;
1345   GstPad **active_pad_p;
1346
1347   if (pad == self->active_sinkpad)
1348     return FALSE;
1349
1350   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1351   new = GST_SELECTOR_PAD_CAST (pad);
1352
1353   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1354       GST_DEBUG_PAD_NAME (new));
1355
1356   if (old)
1357     old->pushed = FALSE;
1358   if (new)
1359     new->pushed = FALSE;
1360
1361   /* Send a new SEGMENT event on the new pad next */
1362   if (old != new && new)
1363     new->events_pending = TRUE;
1364
1365   active_pad_p = &self->active_sinkpad;
1366   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1367
1368   if (old && old != new)
1369     gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1370   if (new)
1371     gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1372
1373   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1374       self->active_sinkpad);
1375
1376   return TRUE;
1377 }
1378
1379 static void
1380 gst_input_selector_set_property (GObject * object, guint prop_id,
1381     const GValue * value, GParamSpec * pspec)
1382 {
1383   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1384
1385   switch (prop_id) {
1386     case PROP_ACTIVE_PAD:
1387     {
1388       GstPad *pad;
1389
1390       pad = g_value_get_object (value);
1391
1392       GST_INPUT_SELECTOR_LOCK (sel);
1393
1394 #if DEBUG_CACHED_BUFFERS
1395       gst_input_selector_debug_cached_buffers (sel);
1396 #endif
1397
1398       gst_input_selector_set_active_pad (sel, pad);
1399
1400 #if DEBUG_CACHED_BUFFERS
1401       gst_input_selector_debug_cached_buffers (sel);
1402 #endif
1403
1404       GST_INPUT_SELECTOR_UNLOCK (sel);
1405       break;
1406     }
1407     case PROP_SYNC_STREAMS:
1408       GST_INPUT_SELECTOR_LOCK (sel);
1409       sel->sync_streams = g_value_get_boolean (value);
1410       GST_INPUT_SELECTOR_UNLOCK (sel);
1411       break;
1412     case PROP_SYNC_MODE:
1413       GST_INPUT_SELECTOR_LOCK (sel);
1414       sel->sync_mode = g_value_get_enum (value);
1415       GST_INPUT_SELECTOR_UNLOCK (sel);
1416       break;
1417     case PROP_CACHE_BUFFERS:
1418       GST_INPUT_SELECTOR_LOCK (object);
1419       sel->cache_buffers = g_value_get_boolean (value);
1420       GST_INPUT_SELECTOR_UNLOCK (object);
1421       break;
1422     default:
1423       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1424       break;
1425   }
1426 }
1427
1428 static void
1429 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1430     GParamSpec * pspec, gpointer user_data)
1431 {
1432   /* Wake up all non-active pads in sync mode, they might be
1433    * the active pad now */
1434   if (sel->sync_streams)
1435     GST_INPUT_SELECTOR_BROADCAST (sel);
1436 }
1437
1438 static void
1439 gst_input_selector_get_property (GObject * object, guint prop_id,
1440     GValue * value, GParamSpec * pspec)
1441 {
1442   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1443
1444   switch (prop_id) {
1445     case PROP_N_PADS:
1446       GST_INPUT_SELECTOR_LOCK (object);
1447       g_value_set_uint (value, sel->n_pads);
1448       GST_INPUT_SELECTOR_UNLOCK (object);
1449       break;
1450     case PROP_ACTIVE_PAD:
1451       GST_INPUT_SELECTOR_LOCK (object);
1452       g_value_set_object (value, sel->active_sinkpad);
1453       GST_INPUT_SELECTOR_UNLOCK (object);
1454       break;
1455     case PROP_SYNC_STREAMS:
1456       GST_INPUT_SELECTOR_LOCK (object);
1457       g_value_set_boolean (value, sel->sync_streams);
1458       GST_INPUT_SELECTOR_UNLOCK (object);
1459       break;
1460     case PROP_SYNC_MODE:
1461       GST_INPUT_SELECTOR_LOCK (object);
1462       g_value_set_enum (value, sel->sync_mode);
1463       GST_INPUT_SELECTOR_UNLOCK (object);
1464       break;
1465     case PROP_CACHE_BUFFERS:
1466       GST_INPUT_SELECTOR_LOCK (object);
1467       g_value_set_boolean (value, sel->cache_buffers);
1468       GST_INPUT_SELECTOR_UNLOCK (object);
1469       break;
1470     default:
1471       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1472       break;
1473   }
1474 }
1475
1476 static GstPad *
1477 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1478     gboolean strict)
1479 {
1480   GstPad *otherpad = NULL;
1481
1482   GST_INPUT_SELECTOR_LOCK (sel);
1483   if (pad == sel->srcpad)
1484     otherpad = sel->active_sinkpad;
1485   else if (pad == sel->active_sinkpad || !strict)
1486     otherpad = sel->srcpad;
1487   if (otherpad)
1488     gst_object_ref (otherpad);
1489   GST_INPUT_SELECTOR_UNLOCK (sel);
1490
1491   return otherpad;
1492 }
1493
1494 static gboolean
1495 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1496 {
1497   GstInputSelector *sel;
1498   gboolean result = FALSE;
1499   GstIterator *iter;
1500   gboolean done = FALSE;
1501   GValue item = { 0, };
1502   GstPad *eventpad;
1503   GList *pushed_pads = NULL;
1504
1505   sel = GST_INPUT_SELECTOR (parent);
1506   /* Send upstream events to all sinkpads */
1507   iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1508
1509   /* This is now essentially a copy of gst_pad_event_default_dispatch
1510    * with a different iterator */
1511   while (!done) {
1512     switch (gst_iterator_next (iter, &item)) {
1513       case GST_ITERATOR_OK:
1514         eventpad = g_value_get_object (&item);
1515
1516         /* if already pushed,  skip */
1517         if (g_list_find (pushed_pads, eventpad)) {
1518           g_value_reset (&item);
1519           break;
1520         }
1521
1522         gst_event_ref (event);
1523         result |= gst_pad_push_event (eventpad, event);
1524
1525         g_value_reset (&item);
1526         break;
1527       case GST_ITERATOR_RESYNC:
1528         /* We don't reset the result here because we don't push the event
1529          * again on pads that got the event already and because we need
1530          * to consider the result of the previous pushes */
1531         gst_iterator_resync (iter);
1532         break;
1533       case GST_ITERATOR_ERROR:
1534         GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1535         done = TRUE;
1536         break;
1537       case GST_ITERATOR_DONE:
1538         done = TRUE;
1539         break;
1540     }
1541   }
1542   g_value_unset (&item);
1543   gst_iterator_free (iter);
1544
1545   g_list_free (pushed_pads);
1546
1547   gst_event_unref (event);
1548
1549   return result;
1550 }
1551
1552 /* query on the srcpad. We override this function because by default it will
1553  * only forward the query to one random sinkpad */
1554 static gboolean
1555 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1556 {
1557   gboolean res = FALSE;
1558   GstInputSelector *sel;
1559
1560   sel = GST_INPUT_SELECTOR (parent);
1561
1562   switch (GST_QUERY_TYPE (query)) {
1563     case GST_QUERY_LATENCY:
1564     {
1565       GList *walk;
1566       GstClockTime resmin, resmax;
1567       gboolean reslive;
1568
1569       resmin = 0;
1570       resmax = -1;
1571       reslive = FALSE;
1572
1573       /* perform the query on all sinkpads and combine the results. We take the
1574        * max of min and the min of max for the result latency. */
1575       GST_INPUT_SELECTOR_LOCK (sel);
1576       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1577           walk = g_list_next (walk)) {
1578         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1579
1580         if (gst_pad_peer_query (sinkpad, query)) {
1581           GstClockTime min, max;
1582           gboolean live;
1583
1584           /* one query succeeded, we succeed too */
1585           res = TRUE;
1586
1587           gst_query_parse_latency (query, &live, &min, &max);
1588
1589           GST_DEBUG_OBJECT (sinkpad,
1590               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1591               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1592
1593           if (live) {
1594             if (min > resmin)
1595               resmin = min;
1596             if (resmax == -1)
1597               resmax = max;
1598             else if (max < resmax)
1599               resmax = max;
1600             if (reslive == FALSE)
1601               reslive = live;
1602           }
1603         }
1604       }
1605       GST_INPUT_SELECTOR_UNLOCK (sel);
1606       if (res) {
1607         gst_query_set_latency (query, reslive, resmin, resmax);
1608
1609         GST_DEBUG_OBJECT (sel,
1610             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1611             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1612             reslive);
1613       }
1614
1615       break;
1616     }
1617     default:
1618       res = gst_pad_query_default (pad, parent, query);
1619       break;
1620   }
1621
1622   return res;
1623 }
1624
1625 /* check if the pad is the active sinkpad */
1626 static inline gboolean
1627 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1628 {
1629   gboolean res;
1630
1631   GST_INPUT_SELECTOR_LOCK (sel);
1632   res = (pad == sel->active_sinkpad);
1633   GST_INPUT_SELECTOR_UNLOCK (sel);
1634
1635   return res;
1636 }
1637
1638 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1639 static GstPad *
1640 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1641 {
1642   GstPad *active_sinkpad;
1643   GstSelectorPad *selpad;
1644
1645   selpad = GST_SELECTOR_PAD_CAST (pad);
1646
1647   selpad->active = TRUE;
1648   active_sinkpad = sel->active_sinkpad;
1649   if (active_sinkpad == NULL) {
1650     GValue item = G_VALUE_INIT;
1651     GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1652     GstIteratorResult ires;
1653
1654     while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1655       gst_iterator_resync (iter);
1656     if (ires == GST_ITERATOR_OK) {
1657       /* If no pad is currently selected, we return the first usable pad to
1658        * guarantee consistency */
1659
1660       active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1661       g_value_reset (&item);
1662       GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1663           GST_DEBUG_PAD_NAME (active_sinkpad));
1664     } else
1665       GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1666     gst_iterator_free (iter);
1667   }
1668
1669   return active_sinkpad;
1670 }
1671
1672 static GstPad *
1673 gst_input_selector_request_new_pad (GstElement * element,
1674     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1675 {
1676   GstInputSelector *sel;
1677   gchar *name = NULL;
1678   GstPad *sinkpad = NULL;
1679
1680   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1681
1682   sel = GST_INPUT_SELECTOR (element);
1683
1684   GST_INPUT_SELECTOR_LOCK (sel);
1685
1686   GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1687   name = g_strdup_printf ("sink_%u", sel->padcount++);
1688   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1689       "name", name, "direction", templ->direction, "template", templ, NULL);
1690   g_free (name);
1691
1692   sel->n_pads++;
1693
1694   gst_pad_set_event_function (sinkpad,
1695       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1696   gst_pad_set_query_function (sinkpad,
1697       GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1698   gst_pad_set_chain_function (sinkpad,
1699       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1700   gst_pad_set_iterate_internal_links_function (sinkpad,
1701       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1702
1703   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1704   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1705   gst_pad_set_active (sinkpad, TRUE);
1706   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1707   GST_INPUT_SELECTOR_UNLOCK (sel);
1708
1709   return sinkpad;
1710 }
1711
1712 static void
1713 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1714 {
1715   GstInputSelector *sel;
1716
1717   sel = GST_INPUT_SELECTOR (element);
1718   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1719
1720   GST_INPUT_SELECTOR_LOCK (sel);
1721   /* if the pad was the active pad, makes us select a new one */
1722   if (sel->active_sinkpad == pad) {
1723     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1724     gst_object_unref (sel->active_sinkpad);
1725     sel->active_sinkpad = NULL;
1726   }
1727   sel->n_pads--;
1728   GST_INPUT_SELECTOR_UNLOCK (sel);
1729
1730   gst_pad_set_active (pad, FALSE);
1731   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1732 }
1733
1734 static void
1735 gst_input_selector_reset (GstInputSelector * sel)
1736 {
1737   GList *walk;
1738
1739   GST_INPUT_SELECTOR_LOCK (sel);
1740   /* clear active pad */
1741   if (sel->active_sinkpad) {
1742     gst_object_unref (sel->active_sinkpad);
1743     sel->active_sinkpad = NULL;
1744   }
1745   /* reset each of our sinkpads state */
1746   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1747     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1748
1749     gst_selector_pad_reset (selpad);
1750
1751     if (selpad->tags) {
1752       gst_tag_list_unref (selpad->tags);
1753       selpad->tags = NULL;
1754     }
1755   }
1756   sel->have_group_id = TRUE;
1757   GST_INPUT_SELECTOR_UNLOCK (sel);
1758 }
1759
1760 static GstStateChangeReturn
1761 gst_input_selector_change_state (GstElement * element,
1762     GstStateChange transition)
1763 {
1764   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1765   GstStateChangeReturn result;
1766
1767   switch (transition) {
1768     case GST_STATE_CHANGE_READY_TO_PAUSED:
1769       GST_INPUT_SELECTOR_LOCK (self);
1770       self->blocked = FALSE;
1771       self->flushing = FALSE;
1772       GST_INPUT_SELECTOR_UNLOCK (self);
1773       break;
1774     case GST_STATE_CHANGE_PAUSED_TO_READY:
1775       /* first unlock before we call the parent state change function, which
1776        * tries to acquire the stream lock when going to ready. */
1777       GST_INPUT_SELECTOR_LOCK (self);
1778       self->blocked = FALSE;
1779       self->flushing = TRUE;
1780       GST_INPUT_SELECTOR_BROADCAST (self);
1781       GST_INPUT_SELECTOR_UNLOCK (self);
1782       break;
1783     default:
1784       break;
1785   }
1786
1787   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1788
1789   switch (transition) {
1790     case GST_STATE_CHANGE_PAUSED_TO_READY:
1791       gst_input_selector_reset (self);
1792       break;
1793     default:
1794       break;
1795   }
1796
1797   return result;
1798 }
1799
1800 static gint64
1801 gst_input_selector_block (GstInputSelector * self)
1802 {
1803   gint64 ret = 0;
1804   GstSelectorPad *spad;
1805
1806   GST_INPUT_SELECTOR_LOCK (self);
1807
1808   if (self->blocked)
1809     GST_WARNING_OBJECT (self, "switch already blocked");
1810
1811   self->blocked = TRUE;
1812   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1813
1814   if (spad)
1815     ret = gst_selector_pad_get_running_time (spad);
1816   else
1817     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1818
1819   GST_INPUT_SELECTOR_UNLOCK (self);
1820
1821   return ret;
1822 }