coreelements: GST_BOILERPLATE already sets parent_class
[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   gobject_class->dispose = gst_input_selector_dispose;
801
802   gobject_class->set_property = gst_input_selector_set_property;
803   gobject_class->get_property = gst_input_selector_get_property;
804
805   g_object_class_install_property (gobject_class, PROP_N_PADS,
806       g_param_spec_uint ("n-pads", "Number of Pads",
807           "The number of sink pads", 0, G_MAXUINT, 0,
808           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
809
810   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
811       g_param_spec_object ("active-pad", "Active pad",
812           "The currently active sink pad", GST_TYPE_PAD,
813           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814
815   g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
816       g_param_spec_boolean ("select-all", "Select all mode",
817           "Forwards data from all input pads", FALSE,
818           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
819
820   /**
821    * GstInputSelector::block:
822    * @inputselector: the #GstInputSelector
823    *
824    * Block all sink pads in preparation for a switch. Returns the stop time of
825    * the current switch segment, as a running time, or 0 if there is no current
826    * active pad or the current active pad never received data.
827    */
828   gst_input_selector_signals[SIGNAL_BLOCK] =
829       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
830       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
831       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
832       gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
833   /**
834    * GstInputSelector::switch:
835    * @inputselector: the #GstInputSelector
836    * @pad:            the pad to switch to
837    * @stop_time:      running time at which to close the previous segment, or -1
838    *                  to use the running time of the previously active sink pad
839    * @start_time:     running time at which to start the new segment, or -1 to
840    *                  use the running time of the newly active sink pad
841    *
842    * Switch to a new feed. The segment opened by the previously active pad, if
843    * any, will be closed, and a new segment opened before data flows again.
844    *
845    * This signal must be emitted when the element has been blocked via the <link
846    * linkend="GstInputSelector-block">block</link> signal.
847    *
848    * If you have a stream with only one switch element, such as an audio-only
849    * stream, a stream switch should be performed by first emitting the block
850    * signal, and then emitting the switch signal with -1 for the stop and start
851    * time values.
852    *
853    * The intention of the @stop_time and @start_time arguments is to allow
854    * multiple switch elements to switch and maintain stream synchronization.
855    * When switching a stream with multiple feeds, you will need as many switch
856    * elements as you have feeds. For example, a feed with audio and video will
857    * have one switch element between the audio feeds and one for video.
858    *
859    * A switch over multiple switch elements should be performed as follows:
860    * First, emit the <link linkend="GstInputSelector-block">block</link>
861    * signal, collecting the returned values. The maximum running time returned
862    * by block should then be used as the time at which to close the previous
863    * segment.
864    *
865    * Then, query the running times of the new audio and video pads that you will
866    * switch to. Naturally, these pads are on separate switch elements. Take the
867    * minimum running time for those streams and use it for the time at which to
868    * open the new segment.
869    *
870    * If @pad is the same as the current active pad, the element will cancel any
871    * previous block without adjusting segments.
872    *
873    * <note><simpara>
874    * the signal changed from accepting the pad name to the pad object.
875    * </simpara></note>
876    *
877    * Since: 0.10.7
878    */
879   gst_input_selector_signals[SIGNAL_SWITCH] =
880       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
881       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
882       NULL, NULL, gst_input_selector_marshal_VOID__OBJECT_INT64_INT64,
883       G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
884
885   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
886   gstelement_class->release_pad = gst_input_selector_release_pad;
887   gstelement_class->change_state = gst_input_selector_change_state;
888
889   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
890   /* note the underscore because switch is a keyword otherwise */
891   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
892 }
893
894 static void
895 gst_input_selector_init (GstInputSelector * sel,
896     GstInputSelectorClass * g_class)
897 {
898   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
899   gst_pad_set_iterate_internal_links_function (sel->srcpad,
900       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
901   gst_pad_set_getcaps_function (sel->srcpad,
902       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
903   gst_pad_set_query_function (sel->srcpad,
904       GST_DEBUG_FUNCPTR (gst_input_selector_query));
905   gst_pad_set_event_function (sel->srcpad,
906       GST_DEBUG_FUNCPTR (gst_input_selector_event));
907   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
908   /* sinkpad management */
909   sel->active_sinkpad = NULL;
910   sel->padcount = 0;
911   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
912
913   sel->lock = g_mutex_new ();
914   sel->cond = g_cond_new ();
915   sel->blocked = FALSE;
916
917   sel->select_all = FALSE;
918 }
919
920 static void
921 gst_input_selector_dispose (GObject * object)
922 {
923   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
924
925   if (sel->active_sinkpad) {
926     gst_object_unref (sel->active_sinkpad);
927     sel->active_sinkpad = NULL;
928   }
929   if (sel->lock) {
930     g_mutex_free (sel->lock);
931     sel->lock = NULL;
932   }
933   if (sel->cond) {
934     g_cond_free (sel->cond);
935     sel->cond = NULL;
936   }
937
938   G_OBJECT_CLASS (parent_class)->dispose (object);
939 }
940
941 /* Solve the following equation for B.timestamp, and set that as the segment
942  * stop:
943  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
944  */
945 static gint64
946 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
947 {
948   if (running_time <= segment->accum)
949     return segment->start;
950   else
951     return (running_time - segment->accum) * segment->abs_rate + segment->start;
952 }
953
954 static void
955 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
956 {
957   segment->stop = gst_segment_get_timestamp (segment, running_time);
958   segment->last_stop = -1;
959 }
960
961 static void
962 gst_segment_set_start (GstSegment * segment, gint64 running_time)
963 {
964   gint64 new_start, duration;
965
966   new_start = gst_segment_get_timestamp (segment, running_time);
967
968   /* this is the duration we skipped */
969   duration = new_start - segment->start;
970   /* add the duration to the accumulated segment time */
971   segment->accum += duration;
972   /* move position in the segment */
973   segment->time += duration;
974   segment->start += duration;
975 }
976
977 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
978  * active pad changed. */
979 static gboolean
980 gst_input_selector_set_active_pad (GstInputSelector * self,
981     GstPad * pad, gint64 stop_time, gint64 start_time)
982 {
983   GstSelectorPad *old, *new;
984   GstPad **active_pad_p;
985
986   if (pad == self->active_sinkpad)
987     return FALSE;
988
989   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
990   new = GST_SELECTOR_PAD_CAST (pad);
991
992   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
993       GST_DEBUG_PAD_NAME (new));
994
995   if (!GST_CLOCK_TIME_IS_VALID (stop_time) && old) {
996     /* no stop time given, get the latest running_time on the active pad to 
997      * close and open the new segment */
998     stop_time = start_time = gst_selector_pad_get_running_time (old);
999     GST_DEBUG_OBJECT (self, "using start/stop of %" GST_TIME_FORMAT,
1000         GST_TIME_ARGS (start_time));
1001   }
1002
1003   if (old && old->active && !self->pending_close && stop_time >= 0) {
1004     /* schedule a last_stop update if one isn't already scheduled, and a
1005        segment has been pushed before. */
1006     memcpy (&self->segment, &old->segment, sizeof (self->segment));
1007
1008     GST_DEBUG_OBJECT (self, "setting stop_time to %" GST_TIME_FORMAT,
1009         GST_TIME_ARGS (stop_time));
1010     gst_segment_set_stop (&self->segment, stop_time);
1011     self->pending_close = TRUE;
1012   }
1013
1014   if (new && new->active && start_time >= 0) {
1015     GST_DEBUG_OBJECT (self, "setting start_time to %" GST_TIME_FORMAT,
1016         GST_TIME_ARGS (start_time));
1017     /* schedule a new segment push */
1018     gst_segment_set_start (&new->segment, start_time);
1019     new->segment_pending = TRUE;
1020   }
1021
1022   active_pad_p = &self->active_sinkpad;
1023   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1024   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1025       self->active_sinkpad);
1026
1027   return TRUE;
1028 }
1029
1030 static void
1031 gst_input_selector_set_property (GObject * object, guint prop_id,
1032     const GValue * value, GParamSpec * pspec)
1033 {
1034   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1035
1036   switch (prop_id) {
1037     case PROP_ACTIVE_PAD:
1038     {
1039       GstPad *pad;
1040
1041       pad = g_value_get_object (value);
1042
1043       GST_INPUT_SELECTOR_LOCK (sel);
1044       gst_input_selector_set_active_pad (sel, pad,
1045           GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1046       GST_INPUT_SELECTOR_UNLOCK (sel);
1047       break;
1048     }
1049     case PROP_SELECT_ALL:
1050       GST_INPUT_SELECTOR_LOCK (object);
1051       sel->select_all = g_value_get_boolean (value);
1052       GST_INPUT_SELECTOR_UNLOCK (object);
1053       break;
1054     default:
1055       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1056       break;
1057   }
1058 }
1059
1060 static void
1061 gst_input_selector_get_property (GObject * object, guint prop_id,
1062     GValue * value, GParamSpec * pspec)
1063 {
1064   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1065
1066   switch (prop_id) {
1067     case PROP_N_PADS:
1068       GST_INPUT_SELECTOR_LOCK (object);
1069       g_value_set_uint (value, sel->n_pads);
1070       GST_INPUT_SELECTOR_UNLOCK (object);
1071       break;
1072     case PROP_ACTIVE_PAD:
1073       GST_INPUT_SELECTOR_LOCK (object);
1074       g_value_set_object (value, sel->active_sinkpad);
1075       GST_INPUT_SELECTOR_UNLOCK (object);
1076       break;
1077     case PROP_SELECT_ALL:
1078       GST_INPUT_SELECTOR_LOCK (object);
1079       g_value_set_boolean (value, sel->select_all);
1080       GST_INPUT_SELECTOR_UNLOCK (object);
1081       break;
1082     default:
1083       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1084       break;
1085   }
1086 }
1087
1088 static GstPad *
1089 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1090 {
1091   GstInputSelector *sel;
1092   GstPad *otherpad = NULL;
1093
1094   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1095
1096   GST_INPUT_SELECTOR_LOCK (sel);
1097   if (pad == sel->srcpad)
1098     otherpad = sel->active_sinkpad;
1099   else if (pad == sel->active_sinkpad || !strict)
1100     otherpad = sel->srcpad;
1101   if (otherpad)
1102     gst_object_ref (otherpad);
1103   GST_INPUT_SELECTOR_UNLOCK (sel);
1104
1105   gst_object_unref (sel);
1106
1107   return otherpad;
1108 }
1109
1110 static gboolean
1111 gst_input_selector_event (GstPad * pad, GstEvent * event)
1112 {
1113   gboolean res = FALSE;
1114   GstPad *otherpad;
1115
1116   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1117
1118   if (otherpad) {
1119     res = gst_pad_push_event (otherpad, event);
1120
1121     gst_object_unref (otherpad);
1122   } else
1123     gst_event_unref (event);
1124   return res;
1125 }
1126
1127 /* query on the srcpad. We override this function because by default it will
1128  * only forward the query to one random sinkpad */
1129 static gboolean
1130 gst_input_selector_query (GstPad * pad, GstQuery * query)
1131 {
1132   gboolean res = TRUE;
1133   GstInputSelector *sel;
1134   GstPad *otherpad;
1135
1136   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1137
1138   otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1139
1140   switch (GST_QUERY_TYPE (query)) {
1141     case GST_QUERY_LATENCY:
1142     {
1143       GList *walk;
1144       GstClockTime resmin, resmax;
1145       gboolean reslive;
1146
1147       resmin = 0;
1148       resmax = -1;
1149       reslive = FALSE;
1150
1151       /* assume FALSE, we become TRUE if one query succeeds */
1152       res = FALSE;
1153
1154       /* perform the query on all sinkpads and combine the results. We take the
1155        * max of min and the min of max for the result latency. */
1156       GST_INPUT_SELECTOR_LOCK (sel);
1157       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1158           walk = g_list_next (walk)) {
1159         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1160
1161         if (gst_pad_peer_query (sinkpad, query)) {
1162           GstClockTime min, max;
1163           gboolean live;
1164
1165           /* one query succeeded, we succeed too */
1166           res = TRUE;
1167
1168           gst_query_parse_latency (query, &live, &min, &max);
1169
1170           GST_DEBUG_OBJECT (sinkpad,
1171               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1172               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1173
1174           if (live) {
1175             if (min > resmin)
1176               resmin = min;
1177             if (resmax == -1)
1178               resmax = max;
1179             else if (max < resmax)
1180               resmax = max;
1181             if (reslive == FALSE)
1182               reslive = live;
1183           }
1184         }
1185       }
1186       GST_INPUT_SELECTOR_UNLOCK (sel);
1187       if (res) {
1188         gst_query_set_latency (query, reslive, resmin, resmax);
1189
1190         GST_DEBUG_OBJECT (sel,
1191             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1192             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1193             reslive);
1194       }
1195
1196       break;
1197     }
1198     default:
1199       if (otherpad)
1200         res = gst_pad_peer_query (otherpad, query);
1201       break;
1202   }
1203   if (otherpad)
1204     gst_object_unref (otherpad);
1205   gst_object_unref (sel);
1206
1207   return res;
1208 }
1209
1210 static GstCaps *
1211 gst_input_selector_getcaps (GstPad * pad)
1212 {
1213   GstPad *otherpad;
1214   GstObject *parent;
1215   GstCaps *caps;
1216
1217   parent = gst_object_get_parent (GST_OBJECT (pad));
1218
1219   otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1220
1221   if (!otherpad) {
1222     if (GST_INPUT_SELECTOR (parent)->select_all) {
1223       GST_DEBUG_OBJECT (parent,
1224           "Pad %s:%s not linked, returning merge of caps",
1225           GST_DEBUG_PAD_NAME (pad));
1226       caps = gst_pad_proxy_getcaps (pad);
1227     } else {
1228       GST_DEBUG_OBJECT (parent,
1229           "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1230       caps = gst_caps_new_any ();
1231     }
1232   } else {
1233     GST_DEBUG_OBJECT (parent,
1234         "Pad %s:%s is linked (to %s:%s), returning peer caps",
1235         GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1236     /* if the peer has caps, use those. If the pad is not linked, this function
1237      * returns NULL and we return ANY */
1238     if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1239       caps = gst_caps_new_any ();
1240     gst_object_unref (otherpad);
1241   }
1242
1243   gst_object_unref (parent);
1244   return caps;
1245 }
1246
1247 /* check if the pad is the active sinkpad */
1248 static inline gboolean
1249 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1250 {
1251   gboolean res;
1252
1253   GST_INPUT_SELECTOR_LOCK (sel);
1254   res = (pad == sel->active_sinkpad);
1255   GST_INPUT_SELECTOR_UNLOCK (sel);
1256
1257   return res;
1258 }
1259
1260 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1261 static GstPad *
1262 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1263 {
1264   GstPad *active_sinkpad;
1265   GstSelectorPad *selpad;
1266
1267   selpad = GST_SELECTOR_PAD_CAST (pad);
1268
1269   selpad->active = TRUE;
1270   active_sinkpad = sel->active_sinkpad;
1271   if (active_sinkpad == NULL || sel->select_all) {
1272     /* first pad we get activity on becomes the activated pad by default, if we
1273      * select all, we also remember the last used pad. */
1274     if (sel->active_sinkpad)
1275       gst_object_unref (sel->active_sinkpad);
1276     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1277     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1278   }
1279
1280   return active_sinkpad;
1281 }
1282
1283 static GstPad *
1284 gst_input_selector_request_new_pad (GstElement * element,
1285     GstPadTemplate * templ, const gchar * unused)
1286 {
1287   GstInputSelector *sel;
1288   gchar *name = NULL;
1289   GstPad *sinkpad = NULL;
1290
1291   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1292
1293   sel = GST_INPUT_SELECTOR (element);
1294
1295   GST_INPUT_SELECTOR_LOCK (sel);
1296
1297   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1298   name = g_strdup_printf ("sink%d", sel->padcount++);
1299   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1300       "name", name, "direction", templ->direction, "template", templ, NULL);
1301   g_free (name);
1302
1303   sel->n_pads++;
1304
1305   gst_pad_set_event_function (sinkpad,
1306       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1307   gst_pad_set_getcaps_function (sinkpad,
1308       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1309   gst_pad_set_acceptcaps_function (sinkpad,
1310       GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1311   gst_pad_set_chain_function (sinkpad,
1312       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1313   gst_pad_set_iterate_internal_links_function (sinkpad,
1314       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1315   gst_pad_set_bufferalloc_function (sinkpad,
1316       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1317
1318   gst_pad_set_active (sinkpad, TRUE);
1319   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1320   GST_INPUT_SELECTOR_UNLOCK (sel);
1321
1322   return sinkpad;
1323 }
1324
1325 static void
1326 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1327 {
1328   GstInputSelector *sel;
1329
1330   sel = GST_INPUT_SELECTOR (element);
1331   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1332
1333   GST_INPUT_SELECTOR_LOCK (sel);
1334   /* if the pad was the active pad, makes us select a new one */
1335   if (sel->active_sinkpad == pad) {
1336     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1337     gst_object_unref (sel->active_sinkpad);
1338     sel->active_sinkpad = NULL;
1339   }
1340   sel->n_pads--;
1341
1342   gst_pad_set_active (pad, FALSE);
1343   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1344   GST_INPUT_SELECTOR_UNLOCK (sel);
1345 }
1346
1347 static void
1348 gst_input_selector_reset (GstInputSelector * sel)
1349 {
1350   GList *walk;
1351
1352   GST_INPUT_SELECTOR_LOCK (sel);
1353   /* clear active pad */
1354   if (sel->active_sinkpad) {
1355     gst_object_unref (sel->active_sinkpad);
1356     sel->active_sinkpad = NULL;
1357   }
1358   /* reset segment */
1359   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1360   sel->pending_close = FALSE;
1361   /* reset each of our sinkpads state */
1362   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1363     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1364
1365     gst_selector_pad_reset (selpad);
1366
1367     if (selpad->tags) {
1368       gst_tag_list_free (selpad->tags);
1369       selpad->tags = NULL;
1370     }
1371   }
1372   GST_INPUT_SELECTOR_UNLOCK (sel);
1373 }
1374
1375 static GstStateChangeReturn
1376 gst_input_selector_change_state (GstElement * element,
1377     GstStateChange transition)
1378 {
1379   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1380   GstStateChangeReturn result;
1381
1382   switch (transition) {
1383     case GST_STATE_CHANGE_READY_TO_PAUSED:
1384       GST_INPUT_SELECTOR_LOCK (self);
1385       self->blocked = FALSE;
1386       self->flushing = FALSE;
1387       GST_INPUT_SELECTOR_UNLOCK (self);
1388       break;
1389     case GST_STATE_CHANGE_PAUSED_TO_READY:
1390       /* first unlock before we call the parent state change function, which
1391        * tries to acquire the stream lock when going to ready. */
1392       GST_INPUT_SELECTOR_LOCK (self);
1393       self->blocked = FALSE;
1394       self->flushing = TRUE;
1395       GST_INPUT_SELECTOR_BROADCAST (self);
1396       GST_INPUT_SELECTOR_UNLOCK (self);
1397       break;
1398     default:
1399       break;
1400   }
1401
1402   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1403
1404   switch (transition) {
1405     case GST_STATE_CHANGE_PAUSED_TO_READY:
1406       gst_input_selector_reset (self);
1407       break;
1408     default:
1409       break;
1410   }
1411
1412   return result;
1413 }
1414
1415 static gint64
1416 gst_input_selector_block (GstInputSelector * self)
1417 {
1418   gint64 ret = 0;
1419   GstSelectorPad *spad;
1420
1421   GST_INPUT_SELECTOR_LOCK (self);
1422
1423   if (self->blocked)
1424     GST_WARNING_OBJECT (self, "switch already blocked");
1425
1426   self->blocked = TRUE;
1427   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1428
1429   if (spad)
1430     ret = gst_selector_pad_get_running_time (spad);
1431   else
1432     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1433
1434   GST_INPUT_SELECTOR_UNLOCK (self);
1435
1436   return ret;
1437 }
1438
1439 /* stop_time and start_time are running times */
1440 static void
1441 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1442     gint64 stop_time, gint64 start_time)
1443 {
1444   gboolean changed;
1445
1446   g_return_if_fail (self->blocked == TRUE);
1447
1448   GST_INPUT_SELECTOR_LOCK (self);
1449   changed =
1450       gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1451
1452   self->blocked = FALSE;
1453   GST_INPUT_SELECTOR_BROADCAST (self);
1454   GST_INPUT_SELECTOR_UNLOCK (self);
1455
1456   if (changed)
1457     g_object_notify (G_OBJECT (self), "active-pad");
1458 }
1459
1460 static gboolean
1461 gst_input_selector_check_eos (GstElement * selector)
1462 {
1463   GstIterator *it = gst_element_iterate_sink_pads (selector);
1464   GstIteratorResult ires;
1465   gpointer item;
1466   gboolean done = FALSE, is_eos = FALSE;
1467   GstSelectorPad *pad;
1468
1469   while (!done) {
1470     ires = gst_iterator_next (it, &item);
1471     switch (ires) {
1472       case GST_ITERATOR_DONE:
1473         GST_INFO_OBJECT (selector, "all sink pads have eos");
1474         done = TRUE;
1475         is_eos = TRUE;
1476         break;
1477       case GST_ITERATOR_OK:
1478         pad = GST_SELECTOR_PAD_CAST (item);
1479         if (!pad->eos) {
1480           done = TRUE;
1481         }
1482         gst_object_unref (pad);
1483         break;
1484       case GST_ITERATOR_RESYNC:
1485         gst_iterator_resync (it);
1486         break;
1487       default:
1488         done = TRUE;
1489         break;
1490     }
1491   }
1492   gst_iterator_free (it);
1493
1494   return is_eos;
1495 }