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