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