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