inputselector: Keep a ref of the currently active sinkpad around
[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_object_ref (active_sinkpad);
457   GST_INPUT_SELECTOR_UNLOCK (sel);
458
459   if (prev_active_sinkpad != active_sinkpad) {
460     if (prev_active_sinkpad)
461       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
462     g_object_notify (G_OBJECT (active_sinkpad), "active");
463     g_object_notify (G_OBJECT (sel), "active-pad");
464   }
465   if (prev_active_sinkpad)
466     gst_object_unref (prev_active_sinkpad);
467   gst_object_unref (active_sinkpad);
468
469   GST_INPUT_SELECTOR_LOCK (sel);
470   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
471
472   /* only forward if we are dealing with the active sinkpad */
473   forward = (pad == active_sinkpad);
474
475   switch (GST_EVENT_TYPE (event)) {
476     case GST_EVENT_STREAM_START:{
477       guint group_id;
478
479       if (!gst_event_parse_group_id (event, &group_id))
480         sel->have_group_id = FALSE;
481       break;
482     }
483     case GST_EVENT_FLUSH_START:
484       /* Unblock the pad if it's waiting */
485       selpad->flushing = TRUE;
486       GST_INPUT_SELECTOR_BROADCAST (sel);
487       break;
488     case GST_EVENT_FLUSH_STOP:
489       gst_selector_pad_reset (selpad);
490       break;
491     case GST_EVENT_SEGMENT:
492     {
493       gst_event_copy_segment (event, &selpad->segment);
494       selpad->segment_seqnum = gst_event_get_seqnum (event);
495
496       GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
497           &selpad->segment);
498       break;
499     }
500     case GST_EVENT_TAG:
501     {
502       GstTagList *tags, *oldtags, *newtags;
503
504       gst_event_parse_tag (event, &tags);
505
506       GST_OBJECT_LOCK (selpad);
507       oldtags = selpad->tags;
508
509       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
510       selpad->tags = newtags;
511       GST_OBJECT_UNLOCK (selpad);
512
513       if (oldtags)
514         gst_tag_list_unref (oldtags);
515       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
516
517       new_tags = TRUE;
518       break;
519     }
520     case GST_EVENT_EOS:
521       selpad->eos = TRUE;
522
523       if (forward) {
524         selpad->eos_sent = TRUE;
525       } else {
526         GstSelectorPad *active_selpad;
527
528         /* If the active sinkpad is in EOS state but EOS
529          * was not sent downstream this means that the pad
530          * got EOS before it was set as active pad and that
531          * the previously active pad got EOS after it was
532          * active
533          */
534         active_selpad = GST_SELECTOR_PAD (active_sinkpad);
535         forward = (active_selpad->eos && !active_selpad->eos_sent);
536         active_selpad->eos_sent = TRUE;
537       }
538       GST_DEBUG_OBJECT (pad, "received EOS");
539       break;
540     case GST_EVENT_GAP:{
541       GstClockTime ts, dur;
542
543       GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
544
545       gst_event_parse_gap (event, &ts, &dur);
546       if (GST_CLOCK_TIME_IS_VALID (ts)) {
547         if (GST_CLOCK_TIME_IS_VALID (dur))
548           ts += dur;
549
550         /* update the segment position */
551         GST_OBJECT_LOCK (pad);
552         selpad->segment.position = ts;
553         GST_OBJECT_UNLOCK (pad);
554         if (sel->sync_streams && active_sinkpad == pad)
555           GST_INPUT_SELECTOR_BROADCAST (sel);
556       }
557
558     }
559       break;
560     default:
561       break;
562   }
563   GST_INPUT_SELECTOR_UNLOCK (sel);
564   if (new_tags)
565     g_object_notify (G_OBJECT (selpad), "tags");
566   if (forward) {
567     GST_DEBUG_OBJECT (pad, "forwarding event");
568     res = gst_pad_push_event (sel->srcpad, event);
569   } else {
570     /* If we aren't forwarding the event because the pad is not the
571      * active_sinkpad, then set the flag on the pad
572      * that says a segment needs sending if/when that pad is activated.
573      * For all other cases, we send the event immediately, which makes
574      * sparse streams and other segment updates work correctly downstream.
575      */
576     if (GST_EVENT_IS_STICKY (event))
577       selpad->events_pending = TRUE;
578     gst_event_unref (event);
579   }
580
581   return res;
582 }
583
584 static gboolean
585 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
586 {
587   gboolean res = FALSE;
588   GstInputSelector *self = (GstInputSelector *) parent;
589
590   switch (GST_QUERY_TYPE (query)) {
591     case GST_QUERY_CAPS:
592       /* always proxy caps query, regardless of active pad or not */
593       res = gst_pad_peer_query (self->srcpad, query);
594       break;
595     case GST_QUERY_ALLOCATION:{
596       GstPad *active_sinkpad;
597       GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
598
599       /* Only do the allocation query for the active sinkpad,
600        * after switching a reconfigure event is sent and upstream
601        * should reconfigure and do a new allocation query
602        */
603       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
604         GST_INPUT_SELECTOR_LOCK (sel);
605         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
606         GST_INPUT_SELECTOR_UNLOCK (sel);
607
608         if (pad != active_sinkpad) {
609           res = FALSE;
610           goto done;
611         }
612       }
613     }
614       /* fall through */
615     default:
616       res = gst_pad_query_default (pad, parent, query);
617       break;
618   }
619
620 done:
621   return res;
622 }
623
624 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
625  * or return TRUE when flushing */
626 static gboolean
627 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
628 {
629   while (self->blocked && !self->flushing && !pad->flushing) {
630     /* we can be unlocked here when we are shutting down (flushing) or when we
631      * get unblocked */
632     GST_INPUT_SELECTOR_WAIT (self);
633   }
634   return self->flushing;
635 }
636
637 static GstClockTime
638 gst_input_selector_get_clipped_running_time (GstSegment * seg, GstBuffer * buf)
639 {
640   GstClockTime running_time;
641
642   running_time = GST_BUFFER_PTS (buf);
643   /* If possible try to get the running time at the end of the buffer */
644   if (GST_BUFFER_DURATION_IS_VALID (buf))
645     running_time += GST_BUFFER_DURATION (buf);
646   /* Only use the segment to convert to running time if the segment is
647    * in TIME format, otherwise do our best to try to sync */
648   if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
649     if (running_time > seg->stop) {
650       running_time = seg->stop;
651     }
652   }
653   return gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
654 }
655
656 /* must be called without the SELECTOR_LOCK, will wait until the running time
657  * of the active pad is after this pad or return TRUE when flushing */
658 static gboolean
659 gst_input_selector_wait_running_time (GstInputSelector * sel,
660     GstSelectorPad * selpad, GstBuffer * buf)
661 {
662   GstSegment *seg;
663
664   GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
665
666   /* If we have no valid timestamp we can't sync this buffer */
667   if (!GST_BUFFER_PTS_IS_VALID (buf)) {
668     GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
669         "invalid timestamp");
670     return FALSE;
671   }
672
673   seg = &selpad->segment;
674
675   /* Wait until
676    *   a) this is the active pad
677    *   b) the pad or the selector is flushing
678    *   c) the selector is not blocked
679    *   d) the buffer running time is before the current running time
680    *      (either active-seg or clock, depending on sync-mode)
681    */
682
683   GST_INPUT_SELECTOR_LOCK (sel);
684   while (TRUE) {
685     GstPad *active_sinkpad;
686     GstSelectorPad *active_selpad;
687     GstClock *clock;
688     gint64 cur_running_time;
689     GstClockTime running_time;
690
691     active_sinkpad =
692         gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
693     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
694
695     if (seg->format != GST_FORMAT_TIME) {
696       GST_DEBUG_OBJECT (selpad,
697           "Not waiting because we don't have a TIME segment");
698       GST_INPUT_SELECTOR_UNLOCK (sel);
699       return FALSE;
700     }
701
702     running_time = gst_input_selector_get_clipped_running_time (seg, buf);
703     /* If this is outside the segment don't sync */
704     if (running_time == -1) {
705       GST_DEBUG_OBJECT (selpad,
706           "Not waiting because buffer is outside segment");
707       GST_INPUT_SELECTOR_UNLOCK (sel);
708       return FALSE;
709     }
710
711     cur_running_time = GST_CLOCK_TIME_NONE;
712     if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
713       clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
714       if (clock) {
715         GstClockTime base_time;
716
717         cur_running_time = gst_clock_get_time (clock);
718         base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
719         if (base_time <= cur_running_time)
720           cur_running_time -= base_time;
721         else
722           cur_running_time = 0;
723
724         gst_object_unref (clock);
725       }
726     } else {
727       GstSegment *active_seg;
728
729       active_seg = &active_selpad->segment;
730
731       /* If the active segment is configured but not to time format
732        * we can't do any syncing at all */
733       if (active_seg->format != GST_FORMAT_TIME
734           && active_seg->format != GST_FORMAT_UNDEFINED) {
735         GST_DEBUG_OBJECT (selpad,
736             "Not waiting because active segment isn't in TIME format");
737         GST_INPUT_SELECTOR_UNLOCK (sel);
738         return FALSE;
739       }
740
741       /* Get active pad's running time, if no configured segment yet keep at -1 */
742       if (active_seg->format == GST_FORMAT_TIME)
743         cur_running_time = gst_segment_to_running_time (active_seg,
744             GST_FORMAT_TIME, active_seg->position);
745     }
746
747     if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
748         (sel->blocked || cur_running_time == GST_CLOCK_TIME_NONE
749             || running_time >= cur_running_time)) {
750       if (!sel->blocked) {
751         GST_DEBUG_OBJECT (selpad,
752             "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
753             GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
754             GST_TIME_ARGS (cur_running_time));
755       } else
756         GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
757
758       GST_INPUT_SELECTOR_WAIT (sel);
759     } else {
760       GST_INPUT_SELECTOR_UNLOCK (sel);
761       break;
762     }
763   }
764
765   /* Return TRUE if the selector or the pad is flushing */
766   return (sel->flushing || selpad->flushing);
767 }
768
769 static gboolean
770 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
771 {
772   GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
773
774   if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
775     GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
776     GstEvent *e;
777
778     e = gst_event_new_segment (seg);
779     gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
780
781     gst_pad_push_event (sel->srcpad, e);
782   } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
783       && !sel->have_group_id) {
784     GstEvent *tmp =
785         gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
786
787     /* Only push stream-start once if not all our streams have a stream-id */
788     if (!tmp) {
789       gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
790     } else {
791       gst_event_unref (tmp);
792     }
793   } else {
794     gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
795   }
796
797   return TRUE;
798 }
799
800 #if DEBUG_CACHED_BUFFERS
801 static void
802 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
803 {
804   GList *walk;
805
806   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
807     GstSelectorPad *selpad;
808     GString *timestamps;
809     gchar *str;
810     int i;
811
812     selpad = GST_SELECTOR_PAD_CAST (walk->data);
813     if (!selpad->cached_buffers) {
814       GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
815       continue;
816     }
817
818     timestamps = g_string_new ("Cached buffers timestamps:");
819     for (i = 0; i < selpad->cached_buffers->length; ++i) {
820       GstSelectorPadCachedBuffer *cached_buffer;
821
822       cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
823       g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
824           GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
825     }
826     str = g_string_free (timestamps, FALSE);
827     GST_DEBUG_OBJECT (selpad, "%s", str);
828     g_free (str);
829   }
830 }
831 #endif
832
833 /* must be called with the SELECTOR_LOCK */
834 static void
835 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
836     GstPad * pad)
837 {
838   GstClock *clock;
839   gint64 cur_running_time;
840   GList *walk;
841
842   cur_running_time = GST_CLOCK_TIME_NONE;
843   if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
844     clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
845     if (clock) {
846       GstClockTime base_time;
847
848       cur_running_time = gst_clock_get_time (clock);
849       base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
850       if (base_time <= cur_running_time)
851         cur_running_time -= base_time;
852       else
853         cur_running_time = 0;
854
855       gst_object_unref (clock);
856     }
857   } else {
858     GstPad *active_sinkpad;
859     GstSelectorPad *active_selpad;
860     GstSegment *active_seg;
861
862     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
863     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
864     active_seg = &active_selpad->segment;
865
866     /* Get active pad's running time, if no configured segment yet keep at -1 */
867     if (active_seg->format == GST_FORMAT_TIME)
868       cur_running_time = gst_segment_to_running_time (active_seg,
869           GST_FORMAT_TIME, active_seg->position);
870   }
871
872   if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
873     return;
874
875   GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
876   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
877     GstSelectorPad *selpad;
878     GstSegment *seg;
879     GstSelectorPadCachedBuffer *cached_buffer;
880     GSList *maybe_remove;
881     guint queue_position;
882
883     selpad = GST_SELECTOR_PAD_CAST (walk->data);
884     if (!selpad->cached_buffers)
885       continue;
886
887     seg = &selpad->segment;
888
889     maybe_remove = NULL;
890     queue_position = 0;
891     while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
892                 queue_position))) {
893       GstBuffer *buffer = cached_buffer->buffer;
894       GstClockTime running_time;
895       GSList *l;
896
897       /* If we have no valid timestamp we can't sync this buffer */
898       if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
899         maybe_remove = g_slist_append (maybe_remove, cached_buffer);
900         queue_position = g_slist_length (maybe_remove);
901         continue;
902       }
903
904       /* the buffer is still valid if its duration is valid and the
905        * timestamp + duration is >= time, or if its duration is invalid
906        * and the timestamp is >= time */
907       running_time = gst_input_selector_get_clipped_running_time (seg, buffer);
908       GST_DEBUG_OBJECT (selpad,
909           "checking if buffer %p running time=%" GST_TIME_FORMAT
910           " >= stream time=%" GST_TIME_FORMAT, buffer,
911           GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
912       if (running_time >= cur_running_time) {
913         break;
914       }
915
916       GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
917       g_queue_pop_nth (selpad->cached_buffers, queue_position);
918       gst_selector_pad_free_cached_buffer (cached_buffer);
919
920       for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
921         /* A buffer after some invalid buffers was removed, it means the invalid buffers
922          * are old, lets also remove them */
923         cached_buffer = l->data;
924         g_queue_remove (selpad->cached_buffers, cached_buffer);
925         gst_selector_pad_free_cached_buffer (cached_buffer);
926       }
927
928       g_slist_free (maybe_remove);
929       maybe_remove = NULL;
930       queue_position = 0;
931     }
932
933     g_slist_free (maybe_remove);
934     maybe_remove = NULL;
935
936     if (g_queue_is_empty (selpad->cached_buffers)) {
937       g_queue_free (selpad->cached_buffers);
938       selpad->cached_buffers = NULL;
939     }
940   }
941
942 #if DEBUG_CACHED_BUFFERS
943   gst_input_selector_debug_cached_buffers (sel);
944 #endif
945 }
946
947 static GstFlowReturn
948 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
949 {
950   GstInputSelector *sel;
951   GstFlowReturn res;
952   GstPad *active_sinkpad;
953   GstPad *prev_active_sinkpad = NULL;
954   GstSelectorPad *selpad;
955
956   sel = GST_INPUT_SELECTOR (parent);
957   selpad = GST_SELECTOR_PAD_CAST (pad);
958
959   GST_DEBUG_OBJECT (selpad,
960       "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
961       GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
962
963   GST_INPUT_SELECTOR_LOCK (sel);
964   /* wait or check for flushing */
965   if (gst_input_selector_wait (sel, selpad)) {
966     GST_INPUT_SELECTOR_UNLOCK (sel);
967     goto flushing;
968   }
969
970   GST_LOG_OBJECT (pad, "getting active pad");
971
972   prev_active_sinkpad =
973       sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
974   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
975
976   /* In sync mode wait until the active pad has advanced
977    * after the running time of the current buffer */
978   if (sel->sync_streams) {
979     /* call chain for each cached buffer if we are not the active pad
980      * or if we are the active pad but didn't push anything yet. */
981     if (active_sinkpad != pad || !selpad->pushed) {
982       /* no need to check for sel->cache_buffers as selpad->cached_buffers
983        * will only be valid if cache_buffers is TRUE */
984       if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
985         GstSelectorPadCachedBuffer *cached_buffer;
986         GstSegment saved_segment;
987
988         saved_segment = selpad->segment;
989
990         selpad->sending_cached_buffers = TRUE;
991         while (!sel->flushing && !selpad->flushing &&
992             (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
993           GST_DEBUG_OBJECT (pad, "Cached buffers found, "
994               "invoking chain for cached buffer %p", cached_buffer->buffer);
995
996           selpad->segment = cached_buffer->segment;
997           selpad->events_pending = TRUE;
998           GST_INPUT_SELECTOR_UNLOCK (sel);
999           gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
1000           GST_INPUT_SELECTOR_LOCK (sel);
1001
1002           /* we may have cleaned up the queue in the meantime because of
1003            * old buffers */
1004           if (!selpad->cached_buffers) {
1005             break;
1006           }
1007         }
1008         selpad->sending_cached_buffers = FALSE;
1009
1010         /* all cached buffers sent, restore segment for current buffer */
1011         selpad->segment = saved_segment;
1012         selpad->events_pending = TRUE;
1013
1014         /* Might have changed while calling chain for cached buffers */
1015         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1016       }
1017     }
1018
1019     if (active_sinkpad != pad) {
1020       GST_INPUT_SELECTOR_UNLOCK (sel);
1021       if (gst_input_selector_wait_running_time (sel, selpad, buf))
1022         goto flushing;
1023       GST_INPUT_SELECTOR_LOCK (sel);
1024     }
1025
1026     /* Might have changed while waiting */
1027     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1028   }
1029
1030   /* update the segment on the srcpad */
1031   if (GST_BUFFER_PTS_IS_VALID (buf)) {
1032     GstClockTime start_time = GST_BUFFER_PTS (buf);
1033
1034     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1035         GST_TIME_ARGS (start_time));
1036     if (GST_BUFFER_DURATION_IS_VALID (buf))
1037       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1038           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1039
1040     GST_OBJECT_LOCK (pad);
1041     selpad->segment.position = start_time;
1042     GST_OBJECT_UNLOCK (pad);
1043   }
1044
1045   /* Ignore buffers from pads except the selected one */
1046   if (pad != active_sinkpad)
1047     goto ignore;
1048
1049   /* Tell all non-active pads that we advanced the running time */
1050   if (sel->sync_streams)
1051     GST_INPUT_SELECTOR_BROADCAST (sel);
1052
1053   GST_INPUT_SELECTOR_UNLOCK (sel);
1054
1055   if (prev_active_sinkpad != active_sinkpad) {
1056     if (prev_active_sinkpad)
1057       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1058     g_object_notify (G_OBJECT (active_sinkpad), "active");
1059     g_object_notify (G_OBJECT (sel), "active-pad");
1060   }
1061
1062   /* if we have a pending events, push them now */
1063   if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1064           || selpad->events_pending)) {
1065     gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1066         sel);
1067     selpad->events_pending = FALSE;
1068   }
1069
1070   if (prev_active_sinkpad) {
1071     gst_object_unref (prev_active_sinkpad);
1072     prev_active_sinkpad = NULL;
1073   }
1074
1075   if (selpad->discont) {
1076     buf = gst_buffer_make_writable (buf);
1077
1078     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1079     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1080     selpad->discont = FALSE;
1081   }
1082
1083   /* forward */
1084   GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1085       buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1086
1087   /* Only make the buffer read-only when necessary */
1088   if (sel->sync_streams && sel->cache_buffers)
1089     buf = gst_buffer_ref (buf);
1090   res = gst_pad_push (sel->srcpad, buf);
1091   GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1092
1093   GST_INPUT_SELECTOR_LOCK (sel);
1094
1095   if (sel->sync_streams && sel->cache_buffers) {
1096     /* Might have changed while pushing */
1097     active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1098     /* only set pad to pushed if we are still the active pad */
1099     if (active_sinkpad == pad)
1100       selpad->pushed = TRUE;
1101
1102     /* cache buffer as we may need it again if we change pads */
1103     gst_selector_pad_cache_buffer (selpad, buf);
1104     gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1105   } else {
1106     selpad->pushed = TRUE;
1107   }
1108   GST_INPUT_SELECTOR_UNLOCK (sel);
1109
1110 done:
1111
1112   if (prev_active_sinkpad)
1113     gst_object_unref (prev_active_sinkpad);
1114   prev_active_sinkpad = NULL;
1115
1116   return res;
1117
1118   /* dropped buffers */
1119 ignore:
1120   {
1121     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1122
1123     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1124     /* when we drop a buffer, we're creating a discont on this pad */
1125     selpad->discont = TRUE;
1126     GST_INPUT_SELECTOR_UNLOCK (sel);
1127     gst_buffer_unref (buf);
1128
1129     /* figure out what to return upstream */
1130     GST_OBJECT_LOCK (selpad);
1131     if (selpad->always_ok || !active_pad_pushed)
1132       res = GST_FLOW_OK;
1133     else
1134       res = GST_FLOW_NOT_LINKED;
1135     GST_OBJECT_UNLOCK (selpad);
1136
1137     goto done;
1138   }
1139 flushing:
1140   {
1141     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1142     gst_buffer_unref (buf);
1143     res = GST_FLOW_FLUSHING;
1144     goto done;
1145   }
1146 }
1147
1148 static void gst_input_selector_dispose (GObject * object);
1149 static void gst_input_selector_finalize (GObject * object);
1150
1151 static void gst_input_selector_set_property (GObject * object,
1152     guint prop_id, const GValue * value, GParamSpec * pspec);
1153 static void gst_input_selector_get_property (GObject * object,
1154     guint prop_id, GValue * value, GParamSpec * pspec);
1155
1156 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1157     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1158 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1159
1160 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1161     element, GstStateChange transition);
1162
1163 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1164     GstEvent * event);
1165 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1166     GstQuery * query);
1167 static gint64 gst_input_selector_block (GstInputSelector * self);
1168
1169 #define _do_init \
1170     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1171         "input-selector", 0, "An input stream selector element");
1172 #define gst_input_selector_parent_class parent_class
1173 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1174     _do_init);
1175
1176 static void
1177 gst_input_selector_class_init (GstInputSelectorClass * klass)
1178 {
1179   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1180   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1181
1182   gobject_class->dispose = gst_input_selector_dispose;
1183   gobject_class->finalize = gst_input_selector_finalize;
1184
1185   gobject_class->set_property = gst_input_selector_set_property;
1186   gobject_class->get_property = gst_input_selector_get_property;
1187
1188   g_object_class_install_property (gobject_class, PROP_N_PADS,
1189       g_param_spec_uint ("n-pads", "Number of Pads",
1190           "The number of sink pads", 0, G_MAXUINT, 0,
1191           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1192
1193   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1194       g_param_spec_object ("active-pad", "Active pad",
1195           "The currently active sink pad", GST_TYPE_PAD,
1196           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
1197           G_PARAM_STATIC_STRINGS));
1198
1199   /**
1200    * GstInputSelector:sync-streams
1201    *
1202    * If set to %TRUE all inactive streams will be synced to the
1203    * running time of the active stream or to the current clock.
1204    *
1205    * To make sure no buffers are dropped by input-selector
1206    * that might be needed when switching the active pad,
1207    * sync-mode should be set to "clock" and cache-buffers to TRUE.
1208    */
1209   g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1210       g_param_spec_boolean ("sync-streams", "Sync Streams",
1211           "Synchronize inactive streams to the running time of the active "
1212           "stream or to the current clock",
1213           DEFAULT_SYNC_STREAMS,
1214           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1215           GST_PARAM_MUTABLE_READY));
1216
1217   /**
1218    * GstInputSelector:sync-mode
1219    *
1220    * Select how input-selector will sync buffers when in sync-streams mode.
1221    *
1222    * Note that when using the "active-segment" mode, the "active-segment" may
1223    * be ahead of current clock time when switching the active pad, as the current
1224    * active pad may have pushed more buffers than what was displayed/consumed,
1225    * which may cause delays and some missing buffers.
1226    */
1227   g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1228       g_param_spec_enum ("sync-mode", "Sync mode",
1229           "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1230           DEFAULT_SYNC_MODE,
1231           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1232           GST_PARAM_MUTABLE_READY));
1233
1234   /**
1235    * GstInputSelector:cache-buffers
1236    *
1237    * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1238    * the active pad will cache the buffers still considered valid (after current
1239    * running time, see sync-mode) to avoid missing frames if/when the pad is
1240    * reactivated.
1241    *
1242    * The active pad may push more buffers than what is currently displayed/consumed
1243    * and when changing pads those buffers will be discarded and the only way to
1244    * reactivate that pad without loosing the already consumed buffers is to enable cache.
1245    */
1246   g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1247       g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1248           "Cache buffers for active-pad",
1249           DEFAULT_CACHE_BUFFERS,
1250           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1251           GST_PARAM_MUTABLE_READY));
1252
1253   /**
1254    * GstInputSelector::block:
1255    * @inputselector: the #GstInputSelector
1256    *
1257    * Block all sink pads in preparation for a switch. Returns the stop time of
1258    * the current switch segment, as a running time, or 0 if there is no current
1259    * active pad or the current active pad never received data.
1260    */
1261   gst_input_selector_signals[SIGNAL_BLOCK] =
1262       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1263       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1264       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1265       g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1266
1267   gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1268       "Generic", "N-to-1 input stream selector",
1269       "Julien Moutte <julien@moutte.net>, "
1270       "Jan Schmidt <thaytan@mad.scientist.com>, "
1271       "Wim Taymans <wim.taymans@gmail.com>");
1272   gst_element_class_add_pad_template (gstelement_class,
1273       gst_static_pad_template_get (&gst_input_selector_sink_factory));
1274   gst_element_class_add_pad_template (gstelement_class,
1275       gst_static_pad_template_get (&gst_input_selector_src_factory));
1276
1277   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1278   gstelement_class->release_pad = gst_input_selector_release_pad;
1279   gstelement_class->change_state = gst_input_selector_change_state;
1280
1281   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1282 }
1283
1284 static void
1285 gst_input_selector_init (GstInputSelector * sel)
1286 {
1287   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1288   gst_pad_set_iterate_internal_links_function (sel->srcpad,
1289       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1290   gst_pad_set_query_function (sel->srcpad,
1291       GST_DEBUG_FUNCPTR (gst_input_selector_query));
1292   gst_pad_set_event_function (sel->srcpad,
1293       GST_DEBUG_FUNCPTR (gst_input_selector_event));
1294   GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1295   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1296   /* sinkpad management */
1297   sel->active_sinkpad = NULL;
1298   sel->padcount = 0;
1299   sel->sync_streams = DEFAULT_SYNC_STREAMS;
1300   sel->have_group_id = TRUE;
1301
1302   g_mutex_init (&sel->lock);
1303   g_cond_init (&sel->cond);
1304   sel->blocked = FALSE;
1305
1306   /* lets give a change for downstream to do something on
1307    * active-pad change before we start pushing new buffers */
1308   g_signal_connect_data (sel, "notify::active-pad",
1309       (GCallback) gst_input_selector_active_pad_changed, NULL,
1310       NULL, G_CONNECT_AFTER);
1311 }
1312
1313 static void
1314 gst_input_selector_dispose (GObject * object)
1315 {
1316   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1317
1318   if (sel->active_sinkpad) {
1319     gst_object_unref (sel->active_sinkpad);
1320     sel->active_sinkpad = NULL;
1321   }
1322   G_OBJECT_CLASS (parent_class)->dispose (object);
1323 }
1324
1325 static void
1326 gst_input_selector_finalize (GObject * object)
1327 {
1328   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1329
1330   g_mutex_clear (&sel->lock);
1331   g_cond_clear (&sel->cond);
1332
1333   G_OBJECT_CLASS (parent_class)->finalize (object);
1334 }
1335
1336 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1337  * active pad changed. */
1338 static gboolean
1339 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1340 {
1341   GstSelectorPad *old, *new;
1342   GstPad **active_pad_p;
1343
1344   if (pad == self->active_sinkpad)
1345     return FALSE;
1346
1347   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1348   new = GST_SELECTOR_PAD_CAST (pad);
1349
1350   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1351       GST_DEBUG_PAD_NAME (new));
1352
1353   if (old)
1354     old->pushed = FALSE;
1355   if (new)
1356     new->pushed = FALSE;
1357
1358   /* Send a new SEGMENT event on the new pad next */
1359   if (old != new && new)
1360     new->events_pending = TRUE;
1361
1362   active_pad_p = &self->active_sinkpad;
1363   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1364
1365   if (old && old != new)
1366     gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1367   if (new)
1368     gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1369
1370   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1371       self->active_sinkpad);
1372
1373   return TRUE;
1374 }
1375
1376 static void
1377 gst_input_selector_set_property (GObject * object, guint prop_id,
1378     const GValue * value, GParamSpec * pspec)
1379 {
1380   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1381
1382   switch (prop_id) {
1383     case PROP_ACTIVE_PAD:
1384     {
1385       GstPad *pad;
1386
1387       pad = g_value_get_object (value);
1388
1389       GST_INPUT_SELECTOR_LOCK (sel);
1390
1391 #if DEBUG_CACHED_BUFFERS
1392       gst_input_selector_debug_cached_buffers (sel);
1393 #endif
1394
1395       gst_input_selector_set_active_pad (sel, pad);
1396
1397 #if DEBUG_CACHED_BUFFERS
1398       gst_input_selector_debug_cached_buffers (sel);
1399 #endif
1400
1401       GST_INPUT_SELECTOR_UNLOCK (sel);
1402       break;
1403     }
1404     case PROP_SYNC_STREAMS:
1405       GST_INPUT_SELECTOR_LOCK (sel);
1406       sel->sync_streams = g_value_get_boolean (value);
1407       GST_INPUT_SELECTOR_UNLOCK (sel);
1408       break;
1409     case PROP_SYNC_MODE:
1410       GST_INPUT_SELECTOR_LOCK (sel);
1411       sel->sync_mode = g_value_get_enum (value);
1412       GST_INPUT_SELECTOR_UNLOCK (sel);
1413       break;
1414     case PROP_CACHE_BUFFERS:
1415       GST_INPUT_SELECTOR_LOCK (object);
1416       sel->cache_buffers = g_value_get_boolean (value);
1417       GST_INPUT_SELECTOR_UNLOCK (object);
1418       break;
1419     default:
1420       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1421       break;
1422   }
1423 }
1424
1425 static void
1426 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1427     GParamSpec * pspec, gpointer user_data)
1428 {
1429   /* Wake up all non-active pads in sync mode, they might be
1430    * the active pad now */
1431   if (sel->sync_streams)
1432     GST_INPUT_SELECTOR_BROADCAST (sel);
1433 }
1434
1435 static void
1436 gst_input_selector_get_property (GObject * object, guint prop_id,
1437     GValue * value, GParamSpec * pspec)
1438 {
1439   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1440
1441   switch (prop_id) {
1442     case PROP_N_PADS:
1443       GST_INPUT_SELECTOR_LOCK (object);
1444       g_value_set_uint (value, sel->n_pads);
1445       GST_INPUT_SELECTOR_UNLOCK (object);
1446       break;
1447     case PROP_ACTIVE_PAD:
1448       GST_INPUT_SELECTOR_LOCK (object);
1449       g_value_set_object (value, sel->active_sinkpad);
1450       GST_INPUT_SELECTOR_UNLOCK (object);
1451       break;
1452     case PROP_SYNC_STREAMS:
1453       GST_INPUT_SELECTOR_LOCK (object);
1454       g_value_set_boolean (value, sel->sync_streams);
1455       GST_INPUT_SELECTOR_UNLOCK (object);
1456       break;
1457     case PROP_SYNC_MODE:
1458       GST_INPUT_SELECTOR_LOCK (object);
1459       g_value_set_enum (value, sel->sync_mode);
1460       GST_INPUT_SELECTOR_UNLOCK (object);
1461       break;
1462     case PROP_CACHE_BUFFERS:
1463       GST_INPUT_SELECTOR_LOCK (object);
1464       g_value_set_boolean (value, sel->cache_buffers);
1465       GST_INPUT_SELECTOR_UNLOCK (object);
1466       break;
1467     default:
1468       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1469       break;
1470   }
1471 }
1472
1473 static GstPad *
1474 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1475     gboolean strict)
1476 {
1477   GstPad *otherpad = NULL;
1478
1479   GST_INPUT_SELECTOR_LOCK (sel);
1480   if (pad == sel->srcpad)
1481     otherpad = sel->active_sinkpad;
1482   else if (pad == sel->active_sinkpad || !strict)
1483     otherpad = sel->srcpad;
1484   if (otherpad)
1485     gst_object_ref (otherpad);
1486   GST_INPUT_SELECTOR_UNLOCK (sel);
1487
1488   return otherpad;
1489 }
1490
1491 static gboolean
1492 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1493 {
1494   GstInputSelector *sel;
1495   gboolean result = FALSE;
1496   GstIterator *iter;
1497   gboolean done = FALSE;
1498   GValue item = { 0, };
1499   GstPad *eventpad;
1500   GList *pushed_pads = NULL;
1501
1502   sel = GST_INPUT_SELECTOR (parent);
1503   /* Send upstream events to all sinkpads */
1504   iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1505
1506   /* This is now essentially a copy of gst_pad_event_default_dispatch
1507    * with a different iterator */
1508   while (!done) {
1509     switch (gst_iterator_next (iter, &item)) {
1510       case GST_ITERATOR_OK:
1511         eventpad = g_value_get_object (&item);
1512
1513         /* if already pushed,  skip */
1514         if (g_list_find (pushed_pads, eventpad)) {
1515           g_value_reset (&item);
1516           break;
1517         }
1518
1519         gst_event_ref (event);
1520         result |= gst_pad_push_event (eventpad, event);
1521
1522         g_value_reset (&item);
1523         break;
1524       case GST_ITERATOR_RESYNC:
1525         /* We don't reset the result here because we don't push the event
1526          * again on pads that got the event already and because we need
1527          * to consider the result of the previous pushes */
1528         gst_iterator_resync (iter);
1529         break;
1530       case GST_ITERATOR_ERROR:
1531         GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1532         done = TRUE;
1533         break;
1534       case GST_ITERATOR_DONE:
1535         done = TRUE;
1536         break;
1537     }
1538   }
1539   g_value_unset (&item);
1540   gst_iterator_free (iter);
1541
1542   g_list_free (pushed_pads);
1543
1544   gst_event_unref (event);
1545
1546   return result;
1547 }
1548
1549 /* query on the srcpad. We override this function because by default it will
1550  * only forward the query to one random sinkpad */
1551 static gboolean
1552 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1553 {
1554   gboolean res = FALSE;
1555   GstInputSelector *sel;
1556
1557   sel = GST_INPUT_SELECTOR (parent);
1558
1559   switch (GST_QUERY_TYPE (query)) {
1560     case GST_QUERY_LATENCY:
1561     {
1562       GList *walk;
1563       GstClockTime resmin, resmax;
1564       gboolean reslive;
1565
1566       resmin = 0;
1567       resmax = -1;
1568       reslive = FALSE;
1569
1570       /* perform the query on all sinkpads and combine the results. We take the
1571        * max of min and the min of max for the result latency. */
1572       GST_INPUT_SELECTOR_LOCK (sel);
1573       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1574           walk = g_list_next (walk)) {
1575         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1576
1577         if (gst_pad_peer_query (sinkpad, query)) {
1578           GstClockTime min, max;
1579           gboolean live;
1580
1581           /* one query succeeded, we succeed too */
1582           res = TRUE;
1583
1584           gst_query_parse_latency (query, &live, &min, &max);
1585
1586           GST_DEBUG_OBJECT (sinkpad,
1587               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1588               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1589
1590           if (live) {
1591             if (min > resmin)
1592               resmin = min;
1593             if (resmax == -1)
1594               resmax = max;
1595             else if (max < resmax)
1596               resmax = max;
1597             if (!reslive)
1598               reslive = live;
1599           }
1600         }
1601       }
1602       GST_INPUT_SELECTOR_UNLOCK (sel);
1603       if (res) {
1604         gst_query_set_latency (query, reslive, resmin, resmax);
1605
1606         GST_DEBUG_OBJECT (sel,
1607             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1608             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1609             reslive);
1610       }
1611
1612       break;
1613     }
1614     default:
1615       res = gst_pad_query_default (pad, parent, query);
1616       break;
1617   }
1618
1619   return res;
1620 }
1621
1622 /* check if the pad is the active sinkpad */
1623 static inline gboolean
1624 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1625 {
1626   gboolean res;
1627
1628   GST_INPUT_SELECTOR_LOCK (sel);
1629   res = (pad == sel->active_sinkpad);
1630   GST_INPUT_SELECTOR_UNLOCK (sel);
1631
1632   return res;
1633 }
1634
1635 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1636 static GstPad *
1637 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1638 {
1639   GstPad *active_sinkpad;
1640   GstSelectorPad *selpad;
1641
1642   selpad = GST_SELECTOR_PAD_CAST (pad);
1643
1644   selpad->active = TRUE;
1645   active_sinkpad = sel->active_sinkpad;
1646   if (active_sinkpad == NULL) {
1647     GValue item = G_VALUE_INIT;
1648     GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1649     GstIteratorResult ires;
1650
1651     while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1652       gst_iterator_resync (iter);
1653     if (ires == GST_ITERATOR_OK) {
1654       /* If no pad is currently selected, we return the first usable pad to
1655        * guarantee consistency */
1656
1657       active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1658       g_value_reset (&item);
1659       GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1660           GST_DEBUG_PAD_NAME (active_sinkpad));
1661     } else
1662       GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1663     gst_iterator_free (iter);
1664   }
1665
1666   return active_sinkpad;
1667 }
1668
1669 static GstPad *
1670 gst_input_selector_request_new_pad (GstElement * element,
1671     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1672 {
1673   GstInputSelector *sel;
1674   gchar *name = NULL;
1675   GstPad *sinkpad = NULL;
1676
1677   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1678
1679   sel = GST_INPUT_SELECTOR (element);
1680
1681   GST_INPUT_SELECTOR_LOCK (sel);
1682
1683   GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1684   name = g_strdup_printf ("sink_%u", sel->padcount++);
1685   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1686       "name", name, "direction", templ->direction, "template", templ, NULL);
1687   g_free (name);
1688
1689   sel->n_pads++;
1690
1691   gst_pad_set_event_function (sinkpad,
1692       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1693   gst_pad_set_query_function (sinkpad,
1694       GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1695   gst_pad_set_chain_function (sinkpad,
1696       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1697   gst_pad_set_iterate_internal_links_function (sinkpad,
1698       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1699
1700   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1701   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1702   gst_pad_set_active (sinkpad, TRUE);
1703   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1704   GST_INPUT_SELECTOR_UNLOCK (sel);
1705
1706   return sinkpad;
1707 }
1708
1709 static void
1710 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1711 {
1712   GstInputSelector *sel;
1713
1714   sel = GST_INPUT_SELECTOR (element);
1715   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1716
1717   GST_INPUT_SELECTOR_LOCK (sel);
1718   /* if the pad was the active pad, makes us select a new one */
1719   if (sel->active_sinkpad == pad) {
1720     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1721     gst_object_unref (sel->active_sinkpad);
1722     sel->active_sinkpad = NULL;
1723   }
1724   sel->n_pads--;
1725   GST_INPUT_SELECTOR_UNLOCK (sel);
1726
1727   gst_pad_set_active (pad, FALSE);
1728   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1729 }
1730
1731 static void
1732 gst_input_selector_reset (GstInputSelector * sel)
1733 {
1734   GList *walk;
1735
1736   GST_INPUT_SELECTOR_LOCK (sel);
1737   /* clear active pad */
1738   if (sel->active_sinkpad) {
1739     gst_object_unref (sel->active_sinkpad);
1740     sel->active_sinkpad = NULL;
1741   }
1742   /* reset each of our sinkpads state */
1743   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1744     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1745
1746     gst_selector_pad_reset (selpad);
1747
1748     if (selpad->tags) {
1749       gst_tag_list_unref (selpad->tags);
1750       selpad->tags = NULL;
1751     }
1752   }
1753   sel->have_group_id = TRUE;
1754   GST_INPUT_SELECTOR_UNLOCK (sel);
1755 }
1756
1757 static GstStateChangeReturn
1758 gst_input_selector_change_state (GstElement * element,
1759     GstStateChange transition)
1760 {
1761   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1762   GstStateChangeReturn result;
1763
1764   switch (transition) {
1765     case GST_STATE_CHANGE_READY_TO_PAUSED:
1766       GST_INPUT_SELECTOR_LOCK (self);
1767       self->blocked = FALSE;
1768       self->flushing = FALSE;
1769       GST_INPUT_SELECTOR_UNLOCK (self);
1770       break;
1771     case GST_STATE_CHANGE_PAUSED_TO_READY:
1772       /* first unlock before we call the parent state change function, which
1773        * tries to acquire the stream lock when going to ready. */
1774       GST_INPUT_SELECTOR_LOCK (self);
1775       self->blocked = FALSE;
1776       self->flushing = TRUE;
1777       GST_INPUT_SELECTOR_BROADCAST (self);
1778       GST_INPUT_SELECTOR_UNLOCK (self);
1779       break;
1780     default:
1781       break;
1782   }
1783
1784   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1785
1786   switch (transition) {
1787     case GST_STATE_CHANGE_PAUSED_TO_READY:
1788       gst_input_selector_reset (self);
1789       break;
1790     default:
1791       break;
1792   }
1793
1794   return result;
1795 }
1796
1797 static gint64
1798 gst_input_selector_block (GstInputSelector * self)
1799 {
1800   gint64 ret = 0;
1801   GstSelectorPad *spad;
1802
1803   GST_INPUT_SELECTOR_LOCK (self);
1804
1805   if (self->blocked)
1806     GST_WARNING_OBJECT (self, "switch already blocked");
1807
1808   self->blocked = TRUE;
1809   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1810
1811   if (spad)
1812     ret = gst_selector_pad_get_running_time (spad);
1813   else
1814     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1815
1816   GST_INPUT_SELECTOR_UNLOCK (self);
1817
1818   return ret;
1819 }