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