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