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