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