62a5aaa16ace06d985dfdccbb1e09a6cbb409d5a
[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., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, 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  * Since: 0.10.32
52  */
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #include <string.h>
59
60 #include "gstinputselector.h"
61
62 #include "gst/glib-compat-private.h"
63
64 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
65 #define GST_CAT_DEFAULT input_selector_debug
66
67 static GstStaticPadTemplate gst_input_selector_sink_factory =
68 GST_STATIC_PAD_TEMPLATE ("sink_%u",
69     GST_PAD_SINK,
70     GST_PAD_REQUEST,
71     GST_STATIC_CAPS_ANY);
72
73 static GstStaticPadTemplate gst_input_selector_src_factory =
74 GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS_ANY);
78
79 enum
80 {
81   PROP_0,
82   PROP_N_PADS,
83   PROP_ACTIVE_PAD,
84   PROP_SYNC_STREAMS
85 };
86
87 #define DEFAULT_SYNC_STREAMS TRUE
88
89 #define DEFAULT_PAD_ALWAYS_OK TRUE
90
91 enum
92 {
93   PROP_PAD_0,
94   PROP_PAD_RUNNING_TIME,
95   PROP_PAD_TAGS,
96   PROP_PAD_ACTIVE,
97   PROP_PAD_ALWAYS_OK
98 };
99
100 enum
101 {
102   /* methods */
103   SIGNAL_BLOCK,
104   SIGNAL_SWITCH,
105   LAST_SIGNAL
106 };
107 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
108
109 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
110     sel, GstPad * pad);
111 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
112     GstPad * pad);
113 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
114     GstPad * pad, gboolean strict);
115
116 #define GST_TYPE_SELECTOR_PAD \
117   (gst_selector_pad_get_type())
118 #define GST_SELECTOR_PAD(obj) \
119   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
120 #define GST_SELECTOR_PAD_CLASS(klass) \
121   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
122 #define GST_IS_SELECTOR_PAD(obj) \
123   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
124 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
125   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
126 #define GST_SELECTOR_PAD_CAST(obj) \
127   ((GstSelectorPad *)(obj))
128
129 typedef struct _GstSelectorPad GstSelectorPad;
130 typedef struct _GstSelectorPadClass GstSelectorPadClass;
131
132 struct _GstSelectorPad
133 {
134   GstPad parent;
135
136   gboolean active;              /* when buffer have passed the pad */
137   gboolean pushed;              /* when buffer was pushed downstream since activation */
138   gboolean eos;                 /* when EOS has been received */
139   gboolean eos_sent;            /* when EOS was sent downstream */
140   gboolean discont;             /* after switching we create a discont */
141   gboolean flushing;            /* set after flush-start and before flush-stop */
142   gboolean always_ok;
143   GstTagList *tags;             /* last tags received on the pad */
144
145   GstClockTime position;        /* the current position in the segment */
146   GstSegment segment;           /* the current segment on the pad */
147   guint32 segment_seqnum;       /* sequence number of the current segment */
148
149   gboolean events_pending;      /* TRUE if sticky events need to be updated */
150 };
151
152 struct _GstSelectorPadClass
153 {
154   GstPadClass parent;
155 };
156
157 GType gst_selector_pad_get_type (void);
158 static void gst_selector_pad_finalize (GObject * object);
159 static void gst_selector_pad_get_property (GObject * object,
160     guint prop_id, GValue * value, GParamSpec * pspec);
161 static void gst_selector_pad_set_property (GObject * object,
162     guint prop_id, const GValue * value, GParamSpec * pspec);
163
164 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
165 static void gst_selector_pad_reset (GstSelectorPad * pad);
166 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
167     GstEvent * event);
168 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
169     GstQuery * query);
170 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
171     GstObject * parent);
172 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
173     GstBuffer * buf);
174
175 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
176
177 static void
178 gst_selector_pad_class_init (GstSelectorPadClass * klass)
179 {
180   GObjectClass *gobject_class;
181
182   gobject_class = (GObjectClass *) klass;
183
184   gobject_class->finalize = gst_selector_pad_finalize;
185
186   gobject_class->get_property = gst_selector_pad_get_property;
187   gobject_class->set_property = gst_selector_pad_set_property;
188
189   g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
190       g_param_spec_int64 ("running-time", "Running time",
191           "Running time of stream on pad", 0, G_MAXINT64, 0,
192           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
194       g_param_spec_boxed ("tags", "Tags",
195           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
196           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
197   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
198       g_param_spec_boolean ("active", "Active",
199           "If the pad is currently active", FALSE,
200           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201   /* FIXME: better property name? */
202   g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
203       g_param_spec_boolean ("always-ok", "Always OK",
204           "Make an inactive pad return OK instead of NOT_LINKED",
205           DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 }
207
208 static void
209 gst_selector_pad_init (GstSelectorPad * pad)
210 {
211   pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
212   gst_selector_pad_reset (pad);
213 }
214
215 static void
216 gst_selector_pad_finalize (GObject * object)
217 {
218   GstSelectorPad *pad;
219
220   pad = GST_SELECTOR_PAD_CAST (object);
221
222   if (pad->tags)
223     gst_tag_list_unref (pad->tags);
224
225   G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
226 }
227
228 static void
229 gst_selector_pad_set_property (GObject * object, guint prop_id,
230     const GValue * value, GParamSpec * pspec)
231 {
232   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
233
234   switch (prop_id) {
235     case PROP_PAD_ALWAYS_OK:
236       GST_OBJECT_LOCK (object);
237       spad->always_ok = g_value_get_boolean (value);
238       GST_OBJECT_UNLOCK (object);
239       break;
240     default:
241       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242       break;
243   }
244 }
245
246 static void
247 gst_selector_pad_get_property (GObject * object, guint prop_id,
248     GValue * value, GParamSpec * pspec)
249 {
250   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
251
252   switch (prop_id) {
253     case PROP_PAD_RUNNING_TIME:
254       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
255       break;
256     case PROP_PAD_TAGS:
257       GST_OBJECT_LOCK (object);
258       g_value_set_boxed (value, spad->tags);
259       GST_OBJECT_UNLOCK (object);
260       break;
261     case PROP_PAD_ACTIVE:
262     {
263       GstInputSelector *sel;
264
265       sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
266       g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
267               GST_PAD_CAST (spad)));
268       gst_object_unref (sel);
269       break;
270     }
271     case PROP_PAD_ALWAYS_OK:
272       GST_OBJECT_LOCK (object);
273       g_value_set_boolean (value, spad->always_ok);
274       GST_OBJECT_UNLOCK (object);
275       break;
276     default:
277       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278       break;
279   }
280 }
281
282 static gint64
283 gst_selector_pad_get_running_time (GstSelectorPad * pad)
284 {
285   gint64 ret = 0;
286
287   GST_OBJECT_LOCK (pad);
288   if (pad->active) {
289     guint64 position = pad->position;
290     GstFormat format = pad->segment.format;
291
292     ret = gst_segment_to_running_time (&pad->segment, format, position);
293   }
294   GST_OBJECT_UNLOCK (pad);
295
296   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
297       GST_TIME_ARGS (ret));
298
299   return ret;
300 }
301
302 static void
303 gst_selector_pad_reset (GstSelectorPad * pad)
304 {
305   GST_OBJECT_LOCK (pad);
306   pad->active = FALSE;
307   pad->pushed = FALSE;
308   pad->eos = FALSE;
309   pad->eos_sent = FALSE;
310   pad->events_pending = FALSE;
311   pad->discont = FALSE;
312   pad->flushing = FALSE;
313   pad->position = GST_CLOCK_TIME_NONE;
314   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
315   GST_OBJECT_UNLOCK (pad);
316 }
317
318 /* strictly get the linked pad from the sinkpad. If the pad is active we return
319  * the srcpad else we return NULL */
320 static GstIterator *
321 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
322 {
323   GstInputSelector *sel;
324   GstPad *otherpad;
325   GstIterator *it = NULL;
326   GValue val = { 0, };
327
328   sel = GST_INPUT_SELECTOR (parent);
329
330   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
331   if (otherpad) {
332     g_value_init (&val, GST_TYPE_PAD);
333     g_value_set_object (&val, otherpad);
334     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
335     g_value_unset (&val);
336     gst_object_unref (otherpad);
337   }
338
339   return it;
340 }
341
342 static gboolean
343 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
344 {
345   gboolean res = TRUE;
346   gboolean forward;
347   GstInputSelector *sel;
348   GstSelectorPad *selpad;
349   GstPad *prev_active_sinkpad;
350   GstPad *active_sinkpad;
351
352   sel = GST_INPUT_SELECTOR (parent);
353   selpad = GST_SELECTOR_PAD_CAST (pad);
354
355   GST_INPUT_SELECTOR_LOCK (sel);
356   prev_active_sinkpad = sel->active_sinkpad;
357   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
358
359   /* only forward if we are dealing with the active sinkpad */
360   forward = (pad == active_sinkpad);
361   GST_INPUT_SELECTOR_UNLOCK (sel);
362
363   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
364     g_object_notify (G_OBJECT (sel), "active-pad");
365   }
366
367   switch (GST_EVENT_TYPE (event)) {
368     case GST_EVENT_FLUSH_START:
369       /* Unblock the pad if it's waiting */
370       GST_INPUT_SELECTOR_LOCK (sel);
371       selpad->flushing = TRUE;
372       GST_INPUT_SELECTOR_BROADCAST (sel);
373       GST_INPUT_SELECTOR_UNLOCK (sel);
374       break;
375     case GST_EVENT_FLUSH_STOP:
376       GST_INPUT_SELECTOR_LOCK (sel);
377       gst_selector_pad_reset (selpad);
378       GST_INPUT_SELECTOR_UNLOCK (sel);
379       break;
380     case GST_EVENT_SEGMENT:
381     {
382       GST_INPUT_SELECTOR_LOCK (sel);
383       GST_OBJECT_LOCK (selpad);
384       gst_event_copy_segment (event, &selpad->segment);
385       selpad->segment_seqnum = gst_event_get_seqnum (event);
386
387       /* Update the position */
388       if (selpad->position == GST_CLOCK_TIME_NONE
389           || selpad->segment.position > selpad->position) {
390         selpad->position = selpad->segment.position;
391       } else if (selpad->position != GST_CLOCK_TIME_NONE
392           && selpad->position > selpad->segment.position) {
393         selpad->segment.position = selpad->position;
394
395         if (forward) {
396           gst_event_unref (event);
397           event = gst_event_new_segment (&selpad->segment);
398           gst_event_set_seqnum (event, selpad->segment_seqnum);
399         }
400       }
401       GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
402           &selpad->segment);
403
404       GST_OBJECT_UNLOCK (selpad);
405       GST_INPUT_SELECTOR_UNLOCK (sel);
406       break;
407     }
408     case GST_EVENT_TAG:
409     {
410       GstTagList *tags, *oldtags, *newtags;
411
412       gst_event_parse_tag (event, &tags);
413
414       GST_OBJECT_LOCK (selpad);
415       oldtags = selpad->tags;
416
417       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
418       selpad->tags = newtags;
419       if (oldtags)
420         gst_tag_list_unref (oldtags);
421       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
422       GST_OBJECT_UNLOCK (selpad);
423
424       g_object_notify (G_OBJECT (selpad), "tags");
425       break;
426     }
427     case GST_EVENT_EOS:
428       selpad->eos = TRUE;
429
430       if (forward) {
431         selpad->eos_sent = TRUE;
432       } else {
433         GstSelectorPad *tmp;
434
435         /* If the active sinkpad is in EOS state but EOS
436          * was not sent downstream this means that the pad
437          * got EOS before it was set as active pad and that
438          * the previously active pad got EOS after it was
439          * active
440          */
441         GST_INPUT_SELECTOR_LOCK (sel);
442         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
443         tmp = GST_SELECTOR_PAD (active_sinkpad);
444         forward = (tmp->eos && !tmp->eos_sent);
445         tmp->eos_sent = TRUE;
446         GST_INPUT_SELECTOR_UNLOCK (sel);
447       }
448       GST_DEBUG_OBJECT (pad, "received EOS");
449       break;
450     default:
451       break;
452   }
453   if (forward) {
454     GST_DEBUG_OBJECT (pad, "forwarding event");
455     res = gst_pad_push_event (sel->srcpad, event);
456   } else {
457     /* If we aren't forwarding the event because the pad is not the
458      * active_sinkpad, then set the flag on the pad
459      * that says a segment needs sending if/when that pad is activated.
460      * For all other cases, we send the event immediately, which makes
461      * sparse streams and other segment updates work correctly downstream.
462      */
463     if (GST_EVENT_IS_STICKY (event))
464       selpad->events_pending = TRUE;
465     gst_event_unref (event);
466   }
467
468   return res;
469 }
470
471 static gboolean
472 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
473 {
474   gboolean res = FALSE;
475
476   switch (GST_QUERY_TYPE (query)) {
477     default:
478       res = gst_pad_query_default (pad, parent, query);
479       break;
480   }
481
482   return res;
483 }
484
485 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
486  * or return TRUE when flushing */
487 static gboolean
488 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
489 {
490   while (self->blocked && !self->flushing && !pad->flushing) {
491     /* we can be unlocked here when we are shutting down (flushing) or when we
492      * get unblocked */
493     GST_INPUT_SELECTOR_WAIT (self);
494   }
495   return self->flushing;
496 }
497
498 /* must be called with the SELECTOR_LOCK, will block until the running time
499  * of the active pad is after this pad or return TRUE when flushing */
500 static gboolean
501 gst_input_selector_wait_running_time (GstInputSelector * sel,
502     GstSelectorPad * pad, GstBuffer * buf)
503 {
504   GstPad *active_sinkpad;
505   GstSelectorPad *active_selpad;
506   GstSegment *seg, *active_seg;
507   GstClockTime running_time, active_running_time = GST_CLOCK_TIME_NONE;
508
509   seg = &pad->segment;
510
511   active_sinkpad =
512       gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
513   active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
514   active_seg = &active_selpad->segment;
515
516   /* We can only sync if the segments are in time format or
517    * if the active pad had no newsegment event yet */
518   if (seg->format != GST_FORMAT_TIME ||
519       (active_seg->format != GST_FORMAT_TIME
520           && active_seg->format != GST_FORMAT_UNDEFINED))
521     return FALSE;
522
523   /* If we have no valid timestamp we can't sync this buffer */
524   if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf))
525     return FALSE;
526
527   running_time = GST_BUFFER_TIMESTAMP (buf);
528   /* If possible try to get the running time at the end of the buffer */
529   if (GST_BUFFER_DURATION_IS_VALID (buf))
530     running_time += GST_BUFFER_DURATION (buf);
531   if (running_time > seg->stop)
532     running_time = seg->stop;
533   running_time =
534       gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
535   /* If this is outside the segment don't sync */
536   if (running_time == -1)
537     return FALSE;
538
539   /* Get active pad's running time, if no configured segment yet keep at -1 */
540   if (active_seg->format == GST_FORMAT_TIME)
541     active_running_time =
542         gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
543         active_selpad->position);
544
545   /* Wait until
546    *   a) this is the active pad
547    *   b) the pad or the selector is flushing
548    *   c) the selector is not blocked
549    *   d) the active pad has no running time or the active
550    *      pad's running time is before this running time
551    *   e) the active pad has a non-time segment
552    *   f) the active pad changed and has not pushed anything
553    */
554   while (pad != active_selpad && !sel->flushing && !pad->flushing
555       && active_selpad->pushed && (sel->blocked || active_running_time == -1
556           || running_time >= active_running_time)) {
557     if (!sel->blocked)
558       GST_DEBUG_OBJECT (pad,
559           "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
560           GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
561           GST_TIME_ARGS (active_running_time));
562
563     GST_INPUT_SELECTOR_WAIT (sel);
564
565     /* Get new active pad, it might have changed */
566     active_sinkpad =
567         gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
568     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
569     active_seg = &active_selpad->segment;
570
571     /* If the active segment is configured but not to time format
572      * we can't do any syncing at all */
573     if (active_seg->format != GST_FORMAT_TIME
574         && active_seg->format != GST_FORMAT_UNDEFINED)
575       break;
576
577     /* Get the new active pad running time */
578     if (active_seg->format == GST_FORMAT_TIME)
579       active_running_time =
580           gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
581           active_selpad->position);
582     else
583       active_running_time = -1;
584
585     if (!sel->blocked)
586       GST_DEBUG_OBJECT (pad,
587           "Waited for active streams to advance. %" GST_TIME_FORMAT " >= %"
588           GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
589           GST_TIME_ARGS (active_running_time));
590
591   }
592
593   /* Return TRUE if the selector or the pad is flushing */
594   return (sel->flushing || pad->flushing);
595 }
596
597 static gboolean
598 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
599 {
600   GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
601
602   if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
603     GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
604     GstEvent *e;
605
606     e = gst_event_new_segment (seg);
607     gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
608
609     gst_pad_push_event (sel->srcpad, e);
610   } else {
611     gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
612   }
613
614   return TRUE;
615 }
616
617 static GstFlowReturn
618 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
619 {
620   GstInputSelector *sel;
621   GstFlowReturn res;
622   GstPad *active_sinkpad;
623   GstPad *prev_active_sinkpad;
624   GstSelectorPad *selpad;
625   GstClockTime start_time;
626
627   sel = GST_INPUT_SELECTOR (parent);
628   selpad = GST_SELECTOR_PAD_CAST (pad);
629
630   GST_INPUT_SELECTOR_LOCK (sel);
631   /* wait or check for flushing */
632   if (gst_input_selector_wait (sel, selpad))
633     goto flushing;
634
635   GST_LOG_OBJECT (pad, "getting active pad");
636
637   prev_active_sinkpad = sel->active_sinkpad;
638   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
639
640   /* In sync mode wait until the active pad has advanced
641    * after the running time of the current buffer */
642   if (sel->sync_streams && active_sinkpad != pad) {
643     if (gst_input_selector_wait_running_time (sel, selpad, buf))
644       goto flushing;
645   }
646
647   /* Might have changed while waiting */
648   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
649
650   /* update the segment on the srcpad */
651   start_time = GST_BUFFER_TIMESTAMP (buf);
652   if (GST_CLOCK_TIME_IS_VALID (start_time)) {
653     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
654         GST_TIME_ARGS (start_time));
655     if (GST_BUFFER_DURATION_IS_VALID (buf))
656       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
657           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
658
659     GST_OBJECT_LOCK (pad);
660     selpad->position = start_time;
661     selpad->segment.position = start_time;
662     GST_OBJECT_UNLOCK (pad);
663   }
664
665   /* Ignore buffers from pads except the selected one */
666   if (pad != active_sinkpad)
667     goto ignore;
668
669   /* Tell all non-active pads that we advanced the running time */
670   if (sel->sync_streams)
671     GST_INPUT_SELECTOR_BROADCAST (sel);
672
673   GST_INPUT_SELECTOR_UNLOCK (sel);
674
675   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
676     g_object_notify (G_OBJECT (sel), "active-pad");
677   }
678
679   /* if we have a pending events, push them now */
680   if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
681           || selpad->events_pending)) {
682     gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
683         sel);
684     selpad->events_pending = FALSE;
685   }
686
687   if (selpad->discont) {
688     buf = gst_buffer_make_writable (buf);
689
690     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
691     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
692     selpad->discont = FALSE;
693   }
694
695   /* forward */
696   GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
697
698   res = gst_pad_push (sel->srcpad, buf);
699   selpad->pushed = TRUE;
700
701 done:
702   return res;
703
704   /* dropped buffers */
705 ignore:
706   {
707     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
708
709     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
710     /* when we drop a buffer, we're creating a discont on this pad */
711     selpad->discont = TRUE;
712     GST_INPUT_SELECTOR_UNLOCK (sel);
713     gst_buffer_unref (buf);
714
715     /* figure out what to return upstream */
716     GST_OBJECT_LOCK (selpad);
717     if (selpad->always_ok || !active_pad_pushed)
718       res = GST_FLOW_OK;
719     else
720       res = GST_FLOW_NOT_LINKED;
721     GST_OBJECT_UNLOCK (selpad);
722
723     goto done;
724   }
725 flushing:
726   {
727     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
728     GST_INPUT_SELECTOR_UNLOCK (sel);
729     gst_buffer_unref (buf);
730     res = GST_FLOW_FLUSHING;
731     goto done;
732   }
733 }
734
735 static void gst_input_selector_dispose (GObject * object);
736 static void gst_input_selector_finalize (GObject * object);
737
738 static void gst_input_selector_set_property (GObject * object,
739     guint prop_id, const GValue * value, GParamSpec * pspec);
740 static void gst_input_selector_get_property (GObject * object,
741     guint prop_id, GValue * value, GParamSpec * pspec);
742
743 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
744     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
745 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
746
747 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
748     element, GstStateChange transition);
749
750 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
751     GstEvent * event);
752 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
753     GstQuery * query);
754 static gint64 gst_input_selector_block (GstInputSelector * self);
755
756 /* FIXME: create these marshallers using glib-genmarshal */
757 static void
758 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
759     GValue * return_value G_GNUC_UNUSED,
760     guint n_param_values,
761     const GValue * param_values,
762     gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
763 {
764   typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
765   register GMarshalFunc_INT64__VOID callback;
766   register GCClosure *cc = (GCClosure *) closure;
767   register gpointer data1, data2;
768   gint64 v_return;
769
770   g_return_if_fail (return_value != NULL);
771   g_return_if_fail (n_param_values == 1);
772
773   if (G_CCLOSURE_SWAP_DATA (closure)) {
774     data1 = closure->data;
775     data2 = g_value_peek_pointer (param_values + 0);
776   } else {
777     data1 = g_value_peek_pointer (param_values + 0);
778     data2 = closure->data;
779   }
780   callback =
781       (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
782
783   v_return = callback (data1, data2);
784
785   g_value_set_int64 (return_value, v_return);
786 }
787
788 #define _do_init \
789     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
790         "input-selector", 0, "An input stream selector element");
791 #define gst_input_selector_parent_class parent_class
792 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
793     _do_init);
794
795 static void
796 gst_input_selector_class_init (GstInputSelectorClass * klass)
797 {
798   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
799   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
800
801   gobject_class->dispose = gst_input_selector_dispose;
802   gobject_class->finalize = gst_input_selector_finalize;
803
804   gobject_class->set_property = gst_input_selector_set_property;
805   gobject_class->get_property = gst_input_selector_get_property;
806
807   g_object_class_install_property (gobject_class, PROP_N_PADS,
808       g_param_spec_uint ("n-pads", "Number of Pads",
809           "The number of sink pads", 0, G_MAXUINT, 0,
810           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
811
812   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
813       g_param_spec_object ("active-pad", "Active pad",
814           "The currently active sink pad", GST_TYPE_PAD,
815           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
816
817   /**
818    * GstInputSelector:sync-streams
819    *
820    * If set to %TRUE all inactive streams will be synced to the
821    * running time of the active stream. This makes sure that no
822    * buffers are dropped by input-selector that might be needed
823    * when switching the active pad.
824    *
825    * Since: 0.10.36
826    */
827   g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
828       g_param_spec_boolean ("sync-streams", "Sync Streams",
829           "Synchronize inactive streams to the running time of the active stream",
830           DEFAULT_SYNC_STREAMS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
831
832   /**
833    * GstInputSelector::block:
834    * @inputselector: the #GstInputSelector
835    *
836    * Block all sink pads in preparation for a switch. Returns the stop time of
837    * the current switch segment, as a running time, or 0 if there is no current
838    * active pad or the current active pad never received data.
839    */
840   gst_input_selector_signals[SIGNAL_BLOCK] =
841       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
842       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
843       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
844       gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
845
846   gst_element_class_set_static_metadata (gstelement_class, "Input selector",
847       "Generic", "N-to-1 input stream selector",
848       "Julien Moutte <julien@moutte.net>, "
849       "Jan Schmidt <thaytan@mad.scientist.com>, "
850       "Wim Taymans <wim.taymans@gmail.com>");
851   gst_element_class_add_pad_template (gstelement_class,
852       gst_static_pad_template_get (&gst_input_selector_sink_factory));
853   gst_element_class_add_pad_template (gstelement_class,
854       gst_static_pad_template_get (&gst_input_selector_src_factory));
855
856   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
857   gstelement_class->release_pad = gst_input_selector_release_pad;
858   gstelement_class->change_state = gst_input_selector_change_state;
859
860   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
861 }
862
863 static void
864 gst_input_selector_init (GstInputSelector * sel)
865 {
866   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
867   gst_pad_set_iterate_internal_links_function (sel->srcpad,
868       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
869   gst_pad_set_query_function (sel->srcpad,
870       GST_DEBUG_FUNCPTR (gst_input_selector_query));
871   gst_pad_set_event_function (sel->srcpad,
872       GST_DEBUG_FUNCPTR (gst_input_selector_event));
873   GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
874   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
875   /* sinkpad management */
876   sel->active_sinkpad = NULL;
877   sel->padcount = 0;
878   sel->sync_streams = DEFAULT_SYNC_STREAMS;
879
880   g_mutex_init (&sel->lock);
881   g_cond_init (&sel->cond);
882   sel->blocked = FALSE;
883 }
884
885 static void
886 gst_input_selector_dispose (GObject * object)
887 {
888   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
889
890   if (sel->active_sinkpad) {
891     gst_object_unref (sel->active_sinkpad);
892     sel->active_sinkpad = NULL;
893   }
894   G_OBJECT_CLASS (parent_class)->dispose (object);
895 }
896
897 static void
898 gst_input_selector_finalize (GObject * object)
899 {
900   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
901
902   g_mutex_clear (&sel->lock);
903   g_cond_clear (&sel->cond);
904
905   G_OBJECT_CLASS (parent_class)->finalize (object);
906 }
907
908 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
909  * active pad changed. */
910 static gboolean
911 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
912 {
913   GstSelectorPad *old, *new;
914   GstPad **active_pad_p;
915
916   if (pad == self->active_sinkpad)
917     return FALSE;
918
919   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
920   new = GST_SELECTOR_PAD_CAST (pad);
921
922   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
923       GST_DEBUG_PAD_NAME (new));
924
925   if (old)
926     old->pushed = FALSE;
927   if (new)
928     new->pushed = FALSE;
929
930   /* Send a new SEGMENT event on the new pad next */
931   if (old != new && new)
932     new->events_pending = TRUE;
933
934   active_pad_p = &self->active_sinkpad;
935   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
936
937   gst_pad_push_event (pad, gst_event_new_reconfigure ());
938
939   /* Wake up all non-active pads in sync mode, they might be
940    * the active pad now */
941   if (self->sync_streams)
942     GST_INPUT_SELECTOR_BROADCAST (self);
943
944   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
945       self->active_sinkpad);
946
947   return TRUE;
948 }
949
950 static void
951 gst_input_selector_set_property (GObject * object, guint prop_id,
952     const GValue * value, GParamSpec * pspec)
953 {
954   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
955
956   switch (prop_id) {
957     case PROP_ACTIVE_PAD:
958     {
959       GstPad *pad;
960
961       pad = g_value_get_object (value);
962
963       GST_INPUT_SELECTOR_LOCK (sel);
964       gst_input_selector_set_active_pad (sel, pad);
965       GST_INPUT_SELECTOR_UNLOCK (sel);
966       break;
967     }
968     case PROP_SYNC_STREAMS:
969     {
970       GST_INPUT_SELECTOR_LOCK (sel);
971       sel->sync_streams = g_value_get_boolean (value);
972       GST_INPUT_SELECTOR_UNLOCK (sel);
973       break;
974     }
975     default:
976       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
977       break;
978   }
979 }
980
981 static void
982 gst_input_selector_get_property (GObject * object, guint prop_id,
983     GValue * value, GParamSpec * pspec)
984 {
985   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
986
987   switch (prop_id) {
988     case PROP_N_PADS:
989       GST_INPUT_SELECTOR_LOCK (object);
990       g_value_set_uint (value, sel->n_pads);
991       GST_INPUT_SELECTOR_UNLOCK (object);
992       break;
993     case PROP_ACTIVE_PAD:
994       GST_INPUT_SELECTOR_LOCK (object);
995       g_value_set_object (value, sel->active_sinkpad);
996       GST_INPUT_SELECTOR_UNLOCK (object);
997       break;
998     case PROP_SYNC_STREAMS:
999       GST_INPUT_SELECTOR_LOCK (object);
1000       g_value_set_boolean (value, sel->sync_streams);
1001       GST_INPUT_SELECTOR_UNLOCK (object);
1002       break;
1003     default:
1004       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1005       break;
1006   }
1007 }
1008
1009 static GstPad *
1010 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1011     gboolean strict)
1012 {
1013   GstPad *otherpad = NULL;
1014
1015   GST_INPUT_SELECTOR_LOCK (sel);
1016   if (pad == sel->srcpad)
1017     otherpad = sel->active_sinkpad;
1018   else if (pad == sel->active_sinkpad || !strict)
1019     otherpad = sel->srcpad;
1020   if (otherpad)
1021     gst_object_ref (otherpad);
1022   GST_INPUT_SELECTOR_UNLOCK (sel);
1023
1024   return otherpad;
1025 }
1026
1027 static gboolean
1028 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1029 {
1030   GstInputSelector *sel;
1031   gboolean result = FALSE;
1032   GstIterator *iter;
1033   gboolean done = FALSE;
1034   GValue item = { 0, };
1035   GstPad *eventpad;
1036   GList *pushed_pads = NULL;
1037
1038   sel = GST_INPUT_SELECTOR (parent);
1039   /* Send upstream events to all sinkpads */
1040   iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1041
1042   /* This is now essentially a copy of gst_pad_event_default_dispatch
1043    * with a different iterator */
1044   while (!done) {
1045     switch (gst_iterator_next (iter, &item)) {
1046       case GST_ITERATOR_OK:
1047         eventpad = g_value_get_object (&item);
1048
1049         /* if already pushed,  skip */
1050         if (g_list_find (pushed_pads, eventpad)) {
1051           g_value_reset (&item);
1052           break;
1053         }
1054
1055         gst_event_ref (event);
1056         result |= gst_pad_push_event (eventpad, event);
1057
1058         g_value_reset (&item);
1059         break;
1060       case GST_ITERATOR_RESYNC:
1061         /* We don't reset the result here because we don't push the event
1062          * again on pads that got the event already and because we need
1063          * to consider the result of the previous pushes */
1064         gst_iterator_resync (iter);
1065         break;
1066       case GST_ITERATOR_ERROR:
1067         GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1068         done = TRUE;
1069         break;
1070       case GST_ITERATOR_DONE:
1071         done = TRUE;
1072         break;
1073     }
1074   }
1075   g_value_unset (&item);
1076   gst_iterator_free (iter);
1077
1078   g_list_free (pushed_pads);
1079
1080   gst_event_unref (event);
1081
1082   return result;
1083 }
1084
1085 /* query on the srcpad. We override this function because by default it will
1086  * only forward the query to one random sinkpad */
1087 static gboolean
1088 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1089 {
1090   gboolean res = FALSE;
1091   GstInputSelector *sel;
1092
1093   sel = GST_INPUT_SELECTOR (parent);
1094
1095   switch (GST_QUERY_TYPE (query)) {
1096     case GST_QUERY_LATENCY:
1097     {
1098       GList *walk;
1099       GstClockTime resmin, resmax;
1100       gboolean reslive;
1101
1102       resmin = 0;
1103       resmax = -1;
1104       reslive = FALSE;
1105
1106       /* perform the query on all sinkpads and combine the results. We take the
1107        * max of min and the min of max for the result latency. */
1108       GST_INPUT_SELECTOR_LOCK (sel);
1109       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1110           walk = g_list_next (walk)) {
1111         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1112
1113         if (gst_pad_peer_query (sinkpad, query)) {
1114           GstClockTime min, max;
1115           gboolean live;
1116
1117           /* one query succeeded, we succeed too */
1118           res = TRUE;
1119
1120           gst_query_parse_latency (query, &live, &min, &max);
1121
1122           GST_DEBUG_OBJECT (sinkpad,
1123               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1124               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1125
1126           if (live) {
1127             if (min > resmin)
1128               resmin = min;
1129             if (resmax == -1)
1130               resmax = max;
1131             else if (max < resmax)
1132               resmax = max;
1133             if (reslive == FALSE)
1134               reslive = live;
1135           }
1136         }
1137       }
1138       GST_INPUT_SELECTOR_UNLOCK (sel);
1139       if (res) {
1140         gst_query_set_latency (query, reslive, resmin, resmax);
1141
1142         GST_DEBUG_OBJECT (sel,
1143             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1144             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1145             reslive);
1146       }
1147
1148       break;
1149     }
1150     default:
1151       res = gst_pad_query_default (pad, parent, query);
1152       break;
1153   }
1154
1155   return res;
1156 }
1157
1158 /* check if the pad is the active sinkpad */
1159 static inline gboolean
1160 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1161 {
1162   gboolean res;
1163
1164   GST_INPUT_SELECTOR_LOCK (sel);
1165   res = (pad == sel->active_sinkpad);
1166   GST_INPUT_SELECTOR_UNLOCK (sel);
1167
1168   return res;
1169 }
1170
1171 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1172 static GstPad *
1173 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1174 {
1175   GstPad *active_sinkpad;
1176   GstSelectorPad *selpad;
1177
1178   selpad = GST_SELECTOR_PAD_CAST (pad);
1179
1180   selpad->active = TRUE;
1181   active_sinkpad = sel->active_sinkpad;
1182   if (active_sinkpad == NULL) {
1183     /* first pad we get activity on becomes the activated pad by default */
1184     if (sel->active_sinkpad)
1185       gst_object_unref (sel->active_sinkpad);
1186     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1187     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1188   }
1189
1190   return active_sinkpad;
1191 }
1192
1193 static GstPad *
1194 gst_input_selector_request_new_pad (GstElement * element,
1195     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1196 {
1197   GstInputSelector *sel;
1198   gchar *name = NULL;
1199   GstPad *sinkpad = NULL;
1200
1201   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1202
1203   sel = GST_INPUT_SELECTOR (element);
1204
1205   GST_INPUT_SELECTOR_LOCK (sel);
1206
1207   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1208   name = g_strdup_printf ("sink_%u", sel->padcount++);
1209   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1210       "name", name, "direction", templ->direction, "template", templ, NULL);
1211   g_free (name);
1212
1213   sel->n_pads++;
1214
1215   gst_pad_set_event_function (sinkpad,
1216       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1217   gst_pad_set_query_function (sinkpad,
1218       GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1219   gst_pad_set_chain_function (sinkpad,
1220       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1221   gst_pad_set_iterate_internal_links_function (sinkpad,
1222       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1223
1224   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1225   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1226   gst_pad_set_active (sinkpad, TRUE);
1227   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1228   GST_INPUT_SELECTOR_UNLOCK (sel);
1229
1230   return sinkpad;
1231 }
1232
1233 static void
1234 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1235 {
1236   GstInputSelector *sel;
1237
1238   sel = GST_INPUT_SELECTOR (element);
1239   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1240
1241   GST_INPUT_SELECTOR_LOCK (sel);
1242   /* if the pad was the active pad, makes us select a new one */
1243   if (sel->active_sinkpad == pad) {
1244     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1245     gst_object_unref (sel->active_sinkpad);
1246     sel->active_sinkpad = NULL;
1247   }
1248   sel->n_pads--;
1249
1250   gst_pad_set_active (pad, FALSE);
1251   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1252   GST_INPUT_SELECTOR_UNLOCK (sel);
1253 }
1254
1255 static void
1256 gst_input_selector_reset (GstInputSelector * sel)
1257 {
1258   GList *walk;
1259
1260   GST_INPUT_SELECTOR_LOCK (sel);
1261   /* clear active pad */
1262   if (sel->active_sinkpad) {
1263     gst_object_unref (sel->active_sinkpad);
1264     sel->active_sinkpad = NULL;
1265   }
1266   /* reset each of our sinkpads state */
1267   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1268     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1269
1270     gst_selector_pad_reset (selpad);
1271
1272     if (selpad->tags) {
1273       gst_tag_list_unref (selpad->tags);
1274       selpad->tags = NULL;
1275     }
1276   }
1277   GST_INPUT_SELECTOR_UNLOCK (sel);
1278 }
1279
1280 static GstStateChangeReturn
1281 gst_input_selector_change_state (GstElement * element,
1282     GstStateChange transition)
1283 {
1284   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1285   GstStateChangeReturn result;
1286
1287   switch (transition) {
1288     case GST_STATE_CHANGE_READY_TO_PAUSED:
1289       GST_INPUT_SELECTOR_LOCK (self);
1290       self->blocked = FALSE;
1291       self->flushing = FALSE;
1292       GST_INPUT_SELECTOR_UNLOCK (self);
1293       break;
1294     case GST_STATE_CHANGE_PAUSED_TO_READY:
1295       /* first unlock before we call the parent state change function, which
1296        * tries to acquire the stream lock when going to ready. */
1297       GST_INPUT_SELECTOR_LOCK (self);
1298       self->blocked = FALSE;
1299       self->flushing = TRUE;
1300       GST_INPUT_SELECTOR_BROADCAST (self);
1301       GST_INPUT_SELECTOR_UNLOCK (self);
1302       break;
1303     default:
1304       break;
1305   }
1306
1307   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1308
1309   switch (transition) {
1310     case GST_STATE_CHANGE_PAUSED_TO_READY:
1311       gst_input_selector_reset (self);
1312       break;
1313     default:
1314       break;
1315   }
1316
1317   return result;
1318 }
1319
1320 static gint64
1321 gst_input_selector_block (GstInputSelector * self)
1322 {
1323   gint64 ret = 0;
1324   GstSelectorPad *spad;
1325
1326   GST_INPUT_SELECTOR_LOCK (self);
1327
1328   if (self->blocked)
1329     GST_WARNING_OBJECT (self, "switch already blocked");
1330
1331   self->blocked = TRUE;
1332   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1333
1334   if (spad)
1335     ret = gst_selector_pad_get_running_time (spad);
1336   else
1337     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1338
1339   GST_INPUT_SELECTOR_UNLOCK (self);
1340
1341   return ret;
1342 }