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