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