plugins/elements/gstinputselector.c: Ensure we emit notify::active-pad when auto...
[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 GList *gst_selector_pad_get_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 static GList *
324 gst_selector_pad_get_linked_pads (GstPad * pad)
325 {
326   GstPad *otherpad;
327
328   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
329   if (!otherpad)
330     return NULL;
331
332   /* need to drop the ref, internal linked pads is not MT safe */
333   gst_object_unref (otherpad);
334
335   return g_list_append (NULL, otherpad);
336 }
337
338 static gboolean
339 gst_selector_pad_event (GstPad * pad, GstEvent * event)
340 {
341   gboolean res = TRUE;
342   gboolean forward = TRUE;
343   GstInputSelector *sel;
344   GstSelectorPad *selpad;
345
346   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
347   selpad = GST_SELECTOR_PAD_CAST (pad);
348
349   /* only forward if we are dealing with the active sinkpad */
350   forward = gst_input_selector_is_active_sinkpad (sel, pad);
351
352   /* forward all events in select_all mode by default */
353   if (sel->select_all) {
354     forward = TRUE;
355   }
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       /* we are not going to forward the segment, mark the segment as
389        * pending */
390       forward = FALSE;
391       selpad->segment_pending = TRUE;
392       GST_INPUT_SELECTOR_UNLOCK (sel);
393
394       break;
395     }
396     case GST_EVENT_TAG:
397     {
398       GstTagList *tags;
399
400       GST_OBJECT_LOCK (selpad);
401       if (selpad->tags)
402         gst_tag_list_free (selpad->tags);
403       gst_event_parse_tag (event, &tags);
404       if (tags)
405         tags = gst_tag_list_copy (tags);
406       selpad->tags = tags;
407       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, selpad->tags);
408       GST_OBJECT_UNLOCK (selpad);
409       break;
410     }
411     case GST_EVENT_EOS:
412       selpad->eos = TRUE;
413       GST_DEBUG_OBJECT (pad, "received EOS");
414       /* don't forward eos in select_all mode until all sink pads have eos */
415       if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
416         forward = FALSE;
417       }
418       break;
419     default:
420       break;
421   }
422   if (forward) {
423     GST_DEBUG_OBJECT (pad, "forwarding event");
424     res = gst_pad_push_event (sel->srcpad, event);
425   } else
426     gst_event_unref (event);
427
428   gst_object_unref (sel);
429
430   return res;
431 }
432
433 static GstCaps *
434 gst_selector_pad_getcaps (GstPad * pad)
435 {
436   GstInputSelector *sel;
437   GstCaps *caps;
438
439   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
440
441   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
442   caps = gst_pad_peer_get_caps (sel->srcpad);
443   if (caps == NULL)
444     caps = gst_caps_new_any ();
445
446   gst_object_unref (sel);
447
448   return caps;
449 }
450
451 static GstFlowReturn
452 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
453     guint size, GstCaps * caps, GstBuffer ** buf)
454 {
455   GstInputSelector *sel;
456   GstFlowReturn result;
457   GstPad *active_sinkpad;
458   GstPad *prev_active_sinkpad;
459   GstSelectorPad *selpad;
460
461   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
462   selpad = GST_SELECTOR_PAD_CAST (pad);
463
464   GST_DEBUG_OBJECT (pad, "received alloc");
465
466   GST_INPUT_SELECTOR_LOCK (sel);
467   prev_active_sinkpad = sel->active_sinkpad;
468   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
469
470   if (pad != active_sinkpad)
471     goto not_active;
472
473   GST_INPUT_SELECTOR_UNLOCK (sel);
474
475   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
476     g_object_notify (G_OBJECT (sel), "active-pad");
477
478   result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
479
480 done:
481   gst_object_unref (sel);
482
483   return result;
484
485   /* ERRORS */
486 not_active:
487   {
488     GST_INPUT_SELECTOR_UNLOCK (sel);
489
490     /* unselected pad, perform fallback alloc or return unlinked when
491      * asked */
492     GST_OBJECT_LOCK (selpad);
493     if (selpad->always_ok) {
494       GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
495       *buf = NULL;
496       result = GST_FLOW_OK;
497     } else {
498       GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
499       result = GST_FLOW_NOT_LINKED;
500     }
501     GST_OBJECT_UNLOCK (selpad);
502
503     goto done;
504   }
505 }
506
507 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
508  * or return TRUE when flushing */
509 static gboolean
510 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
511 {
512   while (self->blocked && !self->flushing) {
513     /* we can be unlocked here when we are shutting down (flushing) or when we
514      * get unblocked */
515     GST_INPUT_SELECTOR_WAIT (self);
516   }
517   return self->flushing;
518 }
519
520 static GstFlowReturn
521 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
522 {
523   GstInputSelector *sel;
524   GstFlowReturn res;
525   GstPad *active_sinkpad;
526   GstPad *prev_active_sinkpad;
527   GstSelectorPad *selpad;
528   GstClockTime end_time, duration;
529   GstSegment *seg;
530   GstEvent *close_event = NULL, *start_event = NULL;
531
532   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
533   selpad = GST_SELECTOR_PAD_CAST (pad);
534   seg = &selpad->segment;
535
536   GST_INPUT_SELECTOR_LOCK (sel);
537   /* wait or check for flushing */
538   if (gst_input_selector_wait (sel, pad))
539     goto flushing;
540
541   GST_DEBUG_OBJECT (pad, "getting active pad");
542
543   prev_active_sinkpad = sel->active_sinkpad;
544   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
545
546   /* update the segment on the srcpad */
547   end_time = GST_BUFFER_TIMESTAMP (buf);
548   if (GST_CLOCK_TIME_IS_VALID (end_time)) {
549     duration = GST_BUFFER_DURATION (buf);
550     if (GST_CLOCK_TIME_IS_VALID (duration))
551       end_time += duration;
552     GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
553         GST_TIME_ARGS (end_time));
554
555     GST_OBJECT_LOCK (pad);
556     gst_segment_set_last_stop (seg, seg->format, end_time);
557     GST_OBJECT_UNLOCK (pad);
558   }
559
560   /* Ignore buffers from pads except the selected one */
561   if (pad != active_sinkpad)
562     goto ignore;
563
564   if (G_UNLIKELY (sel->pending_close)) {
565     GstSegment *cseg = &sel->segment;
566
567     GST_DEBUG_OBJECT (sel,
568         "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
569         "format %d, "
570         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
571         G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
572         cseg->start, cseg->stop, cseg->time);
573
574     /* create update segment */
575     close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
576         cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
577
578     sel->pending_close = FALSE;
579   }
580   /* if we have a pending segment, push it out now */
581   if (G_UNLIKELY (selpad->segment_pending)) {
582     GST_DEBUG_OBJECT (pad,
583         "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
584         "format %d, "
585         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
586         G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
587         seg->start, seg->stop, seg->time);
588
589     start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
590         seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
591
592     selpad->segment_pending = FALSE;
593   }
594   GST_INPUT_SELECTOR_UNLOCK (sel);
595
596   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
597     g_object_notify (G_OBJECT (sel), "active-pad");
598
599   if (close_event)
600     gst_pad_push_event (sel->srcpad, close_event);
601
602   if (start_event)
603     gst_pad_push_event (sel->srcpad, start_event);
604
605   if (selpad->discont) {
606     buf = gst_buffer_make_metadata_writable (buf);
607
608     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
609     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
610     selpad->discont = FALSE;
611   }
612
613   /* forward */
614   GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
615       GST_DEBUG_PAD_NAME (pad));
616
617   res = gst_pad_push (sel->srcpad, buf);
618
619 done:
620   gst_object_unref (sel);
621   return res;
622
623   /* dropped buffers */
624 ignore:
625   {
626     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
627     /* when we drop a buffer, we're creating a discont on this pad */
628     selpad->discont = TRUE;
629     GST_INPUT_SELECTOR_UNLOCK (sel);
630     gst_buffer_unref (buf);
631
632     /* figure out what to return upstream */
633     GST_OBJECT_LOCK (selpad);
634     if (selpad->always_ok)
635       res = GST_FLOW_OK;
636     else
637       res = GST_FLOW_NOT_LINKED;
638     GST_OBJECT_UNLOCK (selpad);
639
640     goto done;
641   }
642 flushing:
643   {
644     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
645     GST_INPUT_SELECTOR_UNLOCK (sel);
646     gst_buffer_unref (buf);
647     res = GST_FLOW_WRONG_STATE;
648     goto done;
649   }
650 }
651
652 static void gst_input_selector_init (GstInputSelector * sel);
653 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
654 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
655
656 static void gst_input_selector_dispose (GObject * object);
657
658 static void gst_input_selector_set_property (GObject * object,
659     guint prop_id, const GValue * value, GParamSpec * pspec);
660 static void gst_input_selector_get_property (GObject * object,
661     guint prop_id, GValue * value, GParamSpec * pspec);
662
663 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
664     GstPadTemplate * templ, const gchar * unused);
665 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
666
667 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
668     element, GstStateChange transition);
669
670 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
671 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
672 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
673 static gint64 gst_input_selector_block (GstInputSelector * self);
674 static void gst_input_selector_switch (GstInputSelector * self,
675     GstPad * pad, gint64 stop_time, gint64 start_time);
676
677 static GstElementClass *parent_class = NULL;
678
679 GType
680 gst_input_selector_get_type (void)
681 {
682   static GType input_selector_type = 0;
683
684   if (!input_selector_type) {
685     static const GTypeInfo input_selector_info = {
686       sizeof (GstInputSelectorClass),
687       (GBaseInitFunc) gst_input_selector_base_init,
688       NULL,
689       (GClassInitFunc) gst_input_selector_class_init,
690       NULL,
691       NULL,
692       sizeof (GstInputSelector),
693       0,
694       (GInstanceInitFunc) gst_input_selector_init,
695     };
696     input_selector_type =
697         g_type_register_static (GST_TYPE_ELEMENT,
698         "GstInputSelector", &input_selector_info, 0);
699     GST_DEBUG_CATEGORY_INIT (input_selector_debug,
700         "input-selector", 0, "An input stream selector element");
701   }
702
703   return input_selector_type;
704 }
705
706 static void
707 gst_input_selector_base_init (GstInputSelectorClass * klass)
708 {
709   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
710
711   gst_element_class_set_details (element_class, &gst_input_selector_details);
712   gst_element_class_add_pad_template (element_class,
713       gst_static_pad_template_get (&gst_input_selector_sink_factory));
714   gst_element_class_add_pad_template (element_class,
715       gst_static_pad_template_get (&gst_input_selector_src_factory));
716 }
717
718 static void
719 gst_input_selector_class_init (GstInputSelectorClass * klass)
720 {
721   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
722   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
723
724   parent_class = g_type_class_peek_parent (klass);
725
726   gobject_class->dispose = gst_input_selector_dispose;
727
728   gobject_class->set_property =
729       GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
730   gobject_class->get_property =
731       GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
732
733   g_object_class_install_property (gobject_class, PROP_N_PADS,
734       g_param_spec_uint ("n-pads", "Number of Pads",
735           "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
736
737   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
738       g_param_spec_object ("active-pad", "Active pad",
739           "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
740
741   g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
742       g_param_spec_boolean ("select-all", "Select all mode",
743           "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
744
745   /**
746    * GstInputSelector::block:
747    * @inputselector: the #GstInputSelector
748    *
749    * Block all sink pads in preparation for a switch. Returns the stop time of
750    * the current switch segment, as a running time, or 0 if there is no current
751    * active pad or the current active pad never received data.
752    */
753   gst_input_selector_signals[SIGNAL_BLOCK] =
754       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
755       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
756       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
757       gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
758   /**
759    * GstInputSelector::switch:
760    * @inputselector: the #GstInputSelector
761    * @pad:            the pad to switch to
762    * @stop_time:      running time at which to close the previous segment, or -1
763    *                  to use the running time of the previously active sink pad
764    * @start_time:     running time at which to start the new segment, or -1 to
765    *                  use the running time of the newly active sink pad
766    *
767    * Switch to a new feed. The segment opened by the previously active pad, if
768    * any, will be closed, and a new segment opened before data flows again.
769    *
770    * This signal must be emitted when the element has been blocked via the <link
771    * linkend="GstInputSelector-block">block</link> signal.
772    *
773    * If you have a stream with only one switch element, such as an audio-only
774    * stream, a stream switch should be performed by first emitting the block
775    * signal, and then emitting the switch signal with -1 for the stop and start
776    * time values.
777    *
778    * The intention of the @stop_time and @start_time arguments is to allow
779    * multiple switch elements to switch and maintain stream synchronization.
780    * When switching a stream with multiple feeds, you will need as many switch
781    * elements as you have feeds. For example, a feed with audio and video will
782    * have one switch element between the audio feeds and one for video.
783    *
784    * A switch over multiple switch elements should be performed as follows:
785    * First, emit the <link linkend="GstInputSelector-block">block</link>
786    * signal, collecting the returned values. The maximum running time returned
787    * by block should then be used as the time at which to close the previous
788    * segment.
789    *
790    * Then, query the running times of the new audio and video pads that you will
791    * switch to. Naturally, these pads are on separate switch elements. Take the
792    * minimum running time for those streams and use it for the time at which to
793    * open the new segment.
794    *
795    * If @pad is the same as the current active pad, the element will cancel any
796    * previous block without adjusting segments.
797    *
798    * Since: 0.10.7 the signal changed from accepting the pad name to the pad
799    * object.
800    */
801   gst_input_selector_signals[SIGNAL_SWITCH] =
802       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
803       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
804       NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
805       G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
806
807   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
808   gstelement_class->release_pad = gst_input_selector_release_pad;
809   gstelement_class->change_state = gst_input_selector_change_state;
810
811   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
812   /* note the underscore because switch is a keyword otherwise */
813   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
814 }
815
816 static void
817 gst_input_selector_init (GstInputSelector * sel)
818 {
819   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
820   gst_pad_set_internal_link_function (sel->srcpad,
821       GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
822   gst_pad_set_getcaps_function (sel->srcpad,
823       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
824   gst_pad_set_query_function (sel->srcpad,
825       GST_DEBUG_FUNCPTR (gst_input_selector_query));
826   gst_pad_set_event_function (sel->srcpad,
827       GST_DEBUG_FUNCPTR (gst_input_selector_event));
828   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
829   /* sinkpad management */
830   sel->active_sinkpad = NULL;
831   sel->padcount = 0;
832   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
833
834   sel->lock = g_mutex_new ();
835   sel->cond = g_cond_new ();
836   sel->blocked = FALSE;
837
838   sel->select_all = FALSE;
839 }
840
841 static void
842 gst_input_selector_dispose (GObject * object)
843 {
844   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
845
846   if (sel->active_sinkpad) {
847     gst_object_unref (sel->active_sinkpad);
848     sel->active_sinkpad = NULL;
849   }
850   if (sel->lock) {
851     g_mutex_free (sel->lock);
852     sel->lock = NULL;
853   }
854   if (sel->cond) {
855     g_cond_free (sel->cond);
856     sel->cond = NULL;
857   }
858
859   G_OBJECT_CLASS (parent_class)->dispose (object);
860 }
861
862 /* Solve the following equation for B.timestamp, and set that as the segment
863  * stop:
864  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
865  */
866 static gint64
867 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
868 {
869   return (running_time - segment->accum) * segment->abs_rate + segment->start;
870 }
871
872 static void
873 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
874 {
875   segment->stop = gst_segment_get_timestamp (segment, running_time);
876   segment->last_stop = -1;
877 }
878
879 static void
880 gst_segment_set_start (GstSegment * segment, gint64 running_time)
881 {
882   gint64 new_start, duration;
883
884   new_start = gst_segment_get_timestamp (segment, running_time);
885
886   /* this is the duration we skipped */
887   duration = new_start - segment->start;
888   /* add the duration to the accumulated segment time */
889   segment->accum += duration;
890   /* move position in the segment */
891   segment->time += duration;
892   segment->start += duration;
893 }
894
895 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
896  * active pad changed. */
897 static gboolean
898 gst_input_selector_set_active_pad (GstInputSelector * self,
899     GstPad * pad, gint64 stop_time, gint64 start_time)
900 {
901   GstSelectorPad *old, *new;
902   GstPad **active_pad_p;
903
904   if (pad == self->active_sinkpad)
905     return FALSE;
906
907   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
908   new = GST_SELECTOR_PAD_CAST (pad);
909
910   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
911       GST_DEBUG_PAD_NAME (new));
912
913   if (stop_time == -1 && old) {
914     /* no stop time given, get the latest running_time on the active pad to 
915      * close and open the new segment */
916     stop_time = start_time = gst_selector_pad_get_running_time (old);
917     GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
918         start_time);
919   }
920
921   if (old && old->active && !self->pending_close && stop_time >= 0) {
922     /* schedule a last_stop update if one isn't already scheduled, and a
923        segment has been pushed before. */
924     memcpy (&self->segment, &old->segment, sizeof (self->segment));
925
926     GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
927         stop_time);
928     gst_segment_set_stop (&self->segment, stop_time);
929     self->pending_close = TRUE;
930   }
931
932   if (new && new->active && start_time >= 0) {
933     GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
934         start_time);
935     /* schedule a new segment push */
936     gst_segment_set_start (&new->segment, start_time);
937     new->segment_pending = TRUE;
938   }
939
940   active_pad_p = &self->active_sinkpad;
941   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
942   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
943       self->active_sinkpad);
944
945   return TRUE;
946 }
947
948 static void
949 gst_input_selector_set_property (GObject * object, guint prop_id,
950     const GValue * value, GParamSpec * pspec)
951 {
952   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
953
954   switch (prop_id) {
955     case PROP_ACTIVE_PAD:
956     {
957       GstPad *pad;
958
959       pad = g_value_get_object (value);
960
961       GST_INPUT_SELECTOR_LOCK (sel);
962       gst_input_selector_set_active_pad (sel, pad,
963           GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
964       GST_INPUT_SELECTOR_UNLOCK (sel);
965       break;
966     }
967     case PROP_SELECT_ALL:
968       GST_INPUT_SELECTOR_LOCK (object);
969       sel->select_all = g_value_get_boolean (value);
970       GST_INPUT_SELECTOR_UNLOCK (object);
971       break;
972     default:
973       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
974       break;
975   }
976 }
977
978 static void
979 gst_input_selector_get_property (GObject * object, guint prop_id,
980     GValue * value, GParamSpec * pspec)
981 {
982   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
983
984   switch (prop_id) {
985     case PROP_N_PADS:
986       GST_INPUT_SELECTOR_LOCK (object);
987       g_value_set_uint (value, sel->n_pads);
988       GST_INPUT_SELECTOR_UNLOCK (object);
989       break;
990     case PROP_ACTIVE_PAD:
991       GST_INPUT_SELECTOR_LOCK (object);
992       g_value_set_object (value, sel->active_sinkpad);
993       GST_INPUT_SELECTOR_UNLOCK (object);
994       break;
995     case PROP_SELECT_ALL:
996       GST_INPUT_SELECTOR_LOCK (object);
997       g_value_set_boolean (value, sel->select_all);
998       GST_INPUT_SELECTOR_UNLOCK (object);
999       break;
1000     default:
1001       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1002       break;
1003   }
1004 }
1005
1006 static GstPad *
1007 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1008 {
1009   GstInputSelector *sel;
1010   GstPad *otherpad = NULL;
1011
1012   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1013
1014   GST_INPUT_SELECTOR_LOCK (sel);
1015   if (pad == sel->srcpad)
1016     otherpad = sel->active_sinkpad;
1017   else if (pad == sel->active_sinkpad || !strict)
1018     otherpad = sel->srcpad;
1019   if (otherpad)
1020     gst_object_ref (otherpad);
1021   GST_INPUT_SELECTOR_UNLOCK (sel);
1022
1023   gst_object_unref (sel);
1024
1025   return otherpad;
1026 }
1027
1028 static gboolean
1029 gst_input_selector_event (GstPad * pad, GstEvent * event)
1030 {
1031   gboolean res = FALSE;
1032   GstPad *otherpad;
1033
1034   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1035
1036   if (otherpad) {
1037     res = gst_pad_push_event (otherpad, event);
1038
1039     gst_object_unref (otherpad);
1040   }
1041   return res;
1042 }
1043
1044 /* query on the srcpad. We override this function because by default it will
1045  * only forward the query to one random sinkpad */
1046 static gboolean
1047 gst_input_selector_query (GstPad * pad, GstQuery * query)
1048 {
1049   gboolean res = TRUE;
1050   GstInputSelector *sel;
1051   GstPad *otherpad;
1052
1053   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1054
1055   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1056
1057   switch (GST_QUERY_TYPE (query)) {
1058     case GST_QUERY_LATENCY:
1059     {
1060       GList *walk;
1061       GstClockTime resmin, resmax;
1062       gboolean reslive;
1063
1064       resmin = 0;
1065       resmax = -1;
1066       reslive = FALSE;
1067
1068       /* assume FALSE, we become TRUE if one query succeeds */
1069       res = FALSE;
1070
1071       /* perform the query on all sinkpads and combine the results. We take the
1072        * max of min and the min of max for the result latency. */
1073       GST_INPUT_SELECTOR_LOCK (sel);
1074       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1075           walk = g_list_next (walk)) {
1076         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1077
1078         if (gst_pad_peer_query (sinkpad, query)) {
1079           GstClockTime min, max;
1080           gboolean live;
1081
1082           /* one query succeeded, we succeed too */
1083           res = TRUE;
1084
1085           gst_query_parse_latency (query, &live, &min, &max);
1086
1087           GST_DEBUG_OBJECT (sinkpad,
1088               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1089               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1090
1091           if (live) {
1092             if (min > resmin)
1093               resmin = min;
1094             if (resmax == -1)
1095               resmax = max;
1096             else if (max < resmax)
1097               resmax = max;
1098             if (reslive == FALSE)
1099               reslive = live;
1100           }
1101         }
1102       }
1103       GST_INPUT_SELECTOR_UNLOCK (sel);
1104       if (res) {
1105         gst_query_set_latency (query, reslive, resmin, resmax);
1106
1107         GST_DEBUG_OBJECT (sel,
1108             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1109             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1110             reslive);
1111       }
1112
1113       break;
1114     }
1115     default:
1116       if (otherpad)
1117         res = gst_pad_peer_query (otherpad, query);
1118       break;
1119   }
1120   if (otherpad)
1121     gst_object_unref (otherpad);
1122   gst_object_unref (sel);
1123
1124   return res;
1125 }
1126
1127 static GstCaps *
1128 gst_input_selector_getcaps (GstPad * pad)
1129 {
1130   GstPad *otherpad;
1131   GstObject *parent;
1132   GstCaps *caps;
1133
1134   parent = gst_object_get_parent (GST_OBJECT (pad));
1135
1136   otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1137
1138   if (!otherpad) {
1139     if (GST_INPUT_SELECTOR (parent)->select_all) {
1140       GST_DEBUG_OBJECT (parent,
1141           "Pad %s:%s not linked, returning merge of caps",
1142           GST_DEBUG_PAD_NAME (pad));
1143       caps = gst_pad_proxy_getcaps (pad);
1144     } else {
1145       GST_DEBUG_OBJECT (parent,
1146           "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1147       caps = gst_caps_new_any ();
1148     }
1149   } else {
1150     GST_DEBUG_OBJECT (parent,
1151         "Pad %s:%s is linked (to %s:%s), returning peer caps",
1152         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1153     /* if the peer has caps, use those. If the pad is not linked, this function
1154      * returns NULL and we return ANY */
1155     if (!(caps = gst_pad_peer_get_caps (otherpad)))
1156       caps = gst_caps_new_any ();
1157     gst_object_unref (otherpad);
1158   }
1159
1160   gst_object_unref (parent);
1161   return caps;
1162 }
1163
1164 /* check if the pad is the active sinkpad */
1165 static gboolean
1166 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1167 {
1168   GstSelectorPad *selpad;
1169   gboolean res;
1170
1171   selpad = GST_SELECTOR_PAD_CAST (pad);
1172
1173   GST_INPUT_SELECTOR_LOCK (sel);
1174   res = (pad == sel->active_sinkpad);
1175   GST_INPUT_SELECTOR_UNLOCK (sel);
1176
1177   return res;
1178 }
1179
1180 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1181 static GstPad *
1182 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1183 {
1184   GstPad *active_sinkpad;
1185   GstSelectorPad *selpad;
1186
1187   selpad = GST_SELECTOR_PAD_CAST (pad);
1188
1189   selpad->active = TRUE;
1190   active_sinkpad = sel->active_sinkpad;
1191   if (active_sinkpad == NULL || sel->select_all) {
1192     /* first pad we get activity on becomes the activated pad by default, if we
1193      * select all, we also remember the last used pad. */
1194     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1195     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1196   }
1197
1198   return active_sinkpad;
1199 }
1200
1201 static GstPad *
1202 gst_input_selector_request_new_pad (GstElement * element,
1203     GstPadTemplate * templ, const gchar * unused)
1204 {
1205   GstInputSelector *sel;
1206   gchar *name = NULL;
1207   GstPad *sinkpad = NULL;
1208
1209   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1210
1211   sel = GST_INPUT_SELECTOR (element);
1212
1213   GST_INPUT_SELECTOR_LOCK (sel);
1214
1215   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1216   name = g_strdup_printf ("sink%d", sel->padcount++);
1217   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1218       "name", name, "direction", templ->direction, "template", templ, NULL);
1219   g_free (name);
1220
1221   sel->n_pads++;
1222
1223   gst_pad_set_event_function (sinkpad,
1224       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1225   gst_pad_set_getcaps_function (sinkpad,
1226       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1227   gst_pad_set_chain_function (sinkpad,
1228       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1229   gst_pad_set_internal_link_function (sinkpad,
1230       GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
1231   gst_pad_set_bufferalloc_function (sinkpad,
1232       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1233
1234   gst_pad_set_active (sinkpad, TRUE);
1235   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1236   GST_INPUT_SELECTOR_UNLOCK (sel);
1237
1238   return sinkpad;
1239 }
1240
1241 static void
1242 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1243 {
1244   GstInputSelector *sel;
1245
1246   sel = GST_INPUT_SELECTOR (element);
1247   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1248
1249   GST_INPUT_SELECTOR_LOCK (sel);
1250   /* if the pad was the active pad, makes us select a new one */
1251   if (sel->active_sinkpad == pad) {
1252     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1253     sel->active_sinkpad = NULL;
1254   }
1255   sel->n_pads--;
1256
1257   gst_pad_set_active (pad, FALSE);
1258   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1259   GST_INPUT_SELECTOR_UNLOCK (sel);
1260 }
1261
1262 static void
1263 gst_input_selector_reset (GstInputSelector * sel)
1264 {
1265   GList *walk;
1266
1267   GST_INPUT_SELECTOR_LOCK (sel);
1268   /* clear active pad */
1269   if (sel->active_sinkpad) {
1270     gst_object_unref (sel->active_sinkpad);
1271     sel->active_sinkpad = NULL;
1272   }
1273   /* reset segment */
1274   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1275   sel->pending_close = FALSE;
1276   /* reset each of our sinkpads state */
1277   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1278     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1279
1280     gst_selector_pad_reset (selpad);
1281
1282     if (selpad->tags) {
1283       gst_tag_list_free (selpad->tags);
1284       selpad->tags = NULL;
1285     }
1286   }
1287   GST_INPUT_SELECTOR_UNLOCK (sel);
1288 }
1289
1290 static GstStateChangeReturn
1291 gst_input_selector_change_state (GstElement * element,
1292     GstStateChange transition)
1293 {
1294   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1295   GstStateChangeReturn result;
1296
1297   switch (transition) {
1298     case GST_STATE_CHANGE_READY_TO_PAUSED:
1299       GST_INPUT_SELECTOR_LOCK (self);
1300       self->blocked = FALSE;
1301       self->flushing = FALSE;
1302       GST_INPUT_SELECTOR_UNLOCK (self);
1303       break;
1304     case GST_STATE_CHANGE_PAUSED_TO_READY:
1305       /* first unlock before we call the parent state change function, which
1306        * tries to acquire the stream lock when going to ready. */
1307       GST_INPUT_SELECTOR_LOCK (self);
1308       self->blocked = FALSE;
1309       self->flushing = TRUE;
1310       GST_INPUT_SELECTOR_BROADCAST (self);
1311       GST_INPUT_SELECTOR_UNLOCK (self);
1312       break;
1313     default:
1314       break;
1315   }
1316
1317   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1318
1319   switch (transition) {
1320     case GST_STATE_CHANGE_PAUSED_TO_READY:
1321       gst_input_selector_reset (self);
1322       break;
1323     default:
1324       break;
1325   }
1326
1327   return result;
1328 }
1329
1330 static gint64
1331 gst_input_selector_block (GstInputSelector * self)
1332 {
1333   gint64 ret = 0;
1334   GstSelectorPad *spad;
1335
1336   GST_INPUT_SELECTOR_LOCK (self);
1337
1338   if (self->blocked)
1339     GST_WARNING_OBJECT (self, "switch already blocked");
1340
1341   self->blocked = TRUE;
1342   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1343
1344   if (spad)
1345     ret = gst_selector_pad_get_running_time (spad);
1346   else
1347     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1348
1349   GST_INPUT_SELECTOR_UNLOCK (self);
1350
1351   return ret;
1352 }
1353
1354 /* stop_time and start_time are running times */
1355 static void
1356 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1357     gint64 stop_time, gint64 start_time)
1358 {
1359   gboolean changed;
1360
1361   g_return_if_fail (self->blocked == TRUE);
1362
1363   GST_INPUT_SELECTOR_LOCK (self);
1364   changed =
1365       gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1366
1367   self->blocked = FALSE;
1368   GST_INPUT_SELECTOR_BROADCAST (self);
1369   GST_INPUT_SELECTOR_UNLOCK (self);
1370
1371   if (changed)
1372     g_object_notify (G_OBJECT (self), "active-pad");
1373 }
1374
1375 static gboolean
1376 gst_input_selector_check_eos (GstElement * selector)
1377 {
1378   GstIterator *it = gst_element_iterate_sink_pads (selector);
1379   GstIteratorResult ires;
1380   gpointer item;
1381   gboolean done = FALSE, is_eos = FALSE;
1382   GstSelectorPad *pad;
1383
1384   while (!done) {
1385     ires = gst_iterator_next (it, &item);
1386     switch (ires) {
1387       case GST_ITERATOR_DONE:
1388         GST_INFO_OBJECT (selector, "all sink pads have eos");
1389         done = TRUE;
1390         is_eos = TRUE;
1391         break;
1392       case GST_ITERATOR_OK:
1393         pad = GST_SELECTOR_PAD_CAST (item);
1394         if (!pad->eos) {
1395           done = TRUE;
1396         }
1397         break;
1398       case GST_ITERATOR_RESYNC:
1399         gst_iterator_resync (it);
1400         break;
1401       default:
1402         done = TRUE;
1403         break;
1404     }
1405   }
1406   gst_iterator_free (it);
1407
1408   return is_eos;
1409 }