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