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