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