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