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