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