]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - plugins/elements/gstinputselector.c
Bump git version after unplanned 0.10.35 release
[glsdk/gstreamer0-10.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  */
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  */
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
58 #include <string.h>
60 #include "gstinputselector.h"
62 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
63 #define GST_CAT_DEFAULT input_selector_debug
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
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)))
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);
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);
94 enum
95 {
96   PROP_0,
97   PROP_N_PADS,
98   PROP_ACTIVE_PAD,
99   PROP_SYNC_STREAMS
100 };
102 #define DEFAULT_SYNC_STREAMS FALSE
104 #define DEFAULT_PAD_ALWAYS_OK TRUE
106 enum
108   PROP_PAD_0,
109   PROP_PAD_RUNNING_TIME,
110   PROP_PAD_TAGS,
111   PROP_PAD_ACTIVE,
112   PROP_PAD_ALWAYS_OK
113 };
115 enum
117   /* methods */
118   SIGNAL_BLOCK,
119   SIGNAL_SWITCH,
120   LAST_SIGNAL
121 };
122 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
124 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
125     sel, GstPad * pad);
126 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
127     GstPad * pad);
128 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
129     GstPad * pad, gboolean strict);
131 #define GST_TYPE_SELECTOR_PAD \
132   (gst_selector_pad_get_type())
133 #define GST_SELECTOR_PAD(obj) \
134   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
135 #define GST_SELECTOR_PAD_CLASS(klass) \
136   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
137 #define GST_IS_SELECTOR_PAD(obj) \
138   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
139 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
140   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
141 #define GST_SELECTOR_PAD_CAST(obj) \
142   ((GstSelectorPad *)(obj))
144 typedef struct _GstSelectorPad GstSelectorPad;
145 typedef struct _GstSelectorPadClass GstSelectorPadClass;
147 struct _GstSelectorPad
149   GstPad parent;
151   gboolean active;              /* when buffer have passed the pad */
152   gboolean pushed;              /* when buffer was pushed downstream since activation */
153   gboolean eos;                 /* when EOS has been received */
154   gboolean eos_sent;            /* when EOS was sent downstream */
155   gboolean discont;             /* after switching we create a discont */
156   gboolean flushing;            /* set after flush-start and before flush-stop */
157   gboolean always_ok;
158   GstSegment segment;           /* the current segment on the pad */
159   GstTagList *tags;             /* last tags received on the pad */
161   gboolean segment_pending;
162 };
164 struct _GstSelectorPadClass
166   GstPadClass parent;
167 };
169 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
170 static void gst_selector_pad_init (GstSelectorPad * pad);
171 static void gst_selector_pad_finalize (GObject * object);
172 static void gst_selector_pad_get_property (GObject * object,
173     guint prop_id, GValue * value, GParamSpec * pspec);
174 static void gst_selector_pad_set_property (GObject * object,
175     guint prop_id, const GValue * value, GParamSpec * pspec);
177 static GstPadClass *selector_pad_parent_class = NULL;
179 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
180 static void gst_selector_pad_reset (GstSelectorPad * pad);
181 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
182 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
183 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
184 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad);
185 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
186 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
187     guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
189 static GType
190 gst_selector_pad_get_type (void)
192   static volatile gsize selector_pad_type = 0;
193   static const GTypeInfo selector_pad_info = {
194     sizeof (GstSelectorPadClass),
195     NULL,
196     NULL,
197     (GClassInitFunc) gst_selector_pad_class_init,
198     NULL,
199     NULL,
200     sizeof (GstSelectorPad),
201     0,
202     (GInstanceInitFunc) gst_selector_pad_init,
203   };
205   if (g_once_init_enter (&selector_pad_type)) {
206     GType tmp = g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
207         &selector_pad_info, 0);
208     g_once_init_leave (&selector_pad_type, tmp);
209   }
211   return (GType) selector_pad_type;
214 static void
215 gst_selector_pad_class_init (GstSelectorPadClass * klass)
217   GObjectClass *gobject_class;
219   gobject_class = (GObjectClass *) klass;
221   selector_pad_parent_class = g_type_class_peek_parent (klass);
223   gobject_class->finalize = gst_selector_pad_finalize;
225   gobject_class->get_property = gst_selector_pad_get_property;
226   gobject_class->set_property = gst_selector_pad_set_property;
228   g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
229       g_param_spec_int64 ("running-time", "Running time",
230           "Running time of stream on pad", 0, G_MAXINT64, 0,
231           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
232   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
233       g_param_spec_boxed ("tags", "Tags",
234           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
235           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
236   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
237       g_param_spec_boolean ("active", "Active",
238           "If the pad is currently active", FALSE,
239           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
240   /* FIXME: better property name? */
241   g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
242       g_param_spec_boolean ("always-ok", "Always OK",
243           "Make an inactive pad return OK instead of NOT_LINKED",
244           DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 static void
248 gst_selector_pad_init (GstSelectorPad * pad)
250   pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
251   gst_selector_pad_reset (pad);
254 static void
255 gst_selector_pad_finalize (GObject * object)
257   GstSelectorPad *pad;
259   pad = GST_SELECTOR_PAD_CAST (object);
261   if (pad->tags)
262     gst_tag_list_free (pad->tags);
264   G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
267 static void
268 gst_selector_pad_set_property (GObject * object, guint prop_id,
269     const GValue * value, GParamSpec * pspec)
271   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
273   switch (prop_id) {
274     case PROP_PAD_ALWAYS_OK:
275       GST_OBJECT_LOCK (object);
276       spad->always_ok = g_value_get_boolean (value);
277       GST_OBJECT_UNLOCK (object);
278       break;
279     default:
280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281       break;
282   }
285 static void
286 gst_selector_pad_get_property (GObject * object, guint prop_id,
287     GValue * value, GParamSpec * pspec)
289   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
291   switch (prop_id) {
292     case PROP_PAD_RUNNING_TIME:
293       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
294       break;
295     case PROP_PAD_TAGS:
296       GST_OBJECT_LOCK (object);
297       g_value_set_boxed (value, spad->tags);
298       GST_OBJECT_UNLOCK (object);
299       break;
300     case PROP_PAD_ACTIVE:
301     {
302       GstInputSelector *sel;
304       sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
305       g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
306               GST_PAD_CAST (spad)));
307       gst_object_unref (sel);
308       break;
309     }
310     case PROP_PAD_ALWAYS_OK:
311       GST_OBJECT_LOCK (object);
312       g_value_set_boolean (value, spad->always_ok);
313       GST_OBJECT_UNLOCK (object);
314       break;
315     default:
316       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317       break;
318   }
321 static gint64
322 gst_selector_pad_get_running_time (GstSelectorPad * pad)
324   gint64 ret = 0;
326   GST_OBJECT_LOCK (pad);
327   if (pad->active) {
328     gint64 last_stop = pad->segment.last_stop;
330     if (last_stop >= 0)
331       ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
332           last_stop);
333   }
334   GST_OBJECT_UNLOCK (pad);
336   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
337       GST_TIME_ARGS (ret));
339   return ret;
342 static void
343 gst_selector_pad_reset (GstSelectorPad * pad)
345   GST_OBJECT_LOCK (pad);
346   pad->active = FALSE;
347   pad->pushed = FALSE;
348   pad->eos = FALSE;
349   pad->eos_sent = FALSE;
350   pad->segment_pending = FALSE;
351   pad->discont = FALSE;
352   pad->flushing = FALSE;
353   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
354   GST_OBJECT_UNLOCK (pad);
357 /* strictly get the linked pad from the sinkpad. If the pad is active we return
358  * the srcpad else we return NULL */
359 static GstIterator *
360 gst_selector_pad_iterate_linked_pads (GstPad * pad)
362   GstInputSelector *sel;
363   GstPad *otherpad;
364   GstIterator *it;
366   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
367   if (G_UNLIKELY (sel == NULL))
368     return NULL;
370   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
371   it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
372       (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
374   if (otherpad)
375     gst_object_unref (otherpad);
376   gst_object_unref (sel);
378   return it;
381 static gboolean
382 gst_selector_pad_event (GstPad * pad, GstEvent * event)
384   gboolean res = TRUE;
385   gboolean forward;
386   GstInputSelector *sel;
387   GstSelectorPad *selpad;
388   GstPad *prev_active_sinkpad;
389   GstPad *active_sinkpad;
391   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
392   if (G_UNLIKELY (sel == NULL)) {
393     gst_event_unref (event);
394     return FALSE;
395   }
396   selpad = GST_SELECTOR_PAD_CAST (pad);
398   GST_INPUT_SELECTOR_LOCK (sel);
399   prev_active_sinkpad = sel->active_sinkpad;
400   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
402   /* only forward if we are dealing with the active sinkpad */
403   forward = (pad == active_sinkpad);
404   GST_INPUT_SELECTOR_UNLOCK (sel);
406   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
407     NOTIFY_MUTEX_LOCK ();
408     g_object_notify (G_OBJECT (sel), "active-pad");
409     NOTIFY_MUTEX_UNLOCK ();
410   }
412   switch (GST_EVENT_TYPE (event)) {
413     case GST_EVENT_FLUSH_START:
414       /* Unblock the pad if it's waiting */
415       GST_INPUT_SELECTOR_LOCK (sel);
416       selpad->flushing = TRUE;
417       GST_INPUT_SELECTOR_BROADCAST (sel);
418       GST_INPUT_SELECTOR_UNLOCK (sel);
419       break;
420     case GST_EVENT_FLUSH_STOP:
421       GST_INPUT_SELECTOR_LOCK (sel);
422       gst_selector_pad_reset (selpad);
423       sel->pending_close = FALSE;
424       GST_INPUT_SELECTOR_UNLOCK (sel);
425       break;
426     case GST_EVENT_NEWSEGMENT:
427     {
428       gboolean update;
429       GstFormat format;
430       gdouble rate, arate;
431       gint64 start, stop, time;
433       gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
434           &start, &stop, &time);
436       GST_DEBUG_OBJECT (pad,
437           "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
438           "format %d, "
439           "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
440           G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
442       GST_INPUT_SELECTOR_LOCK (sel);
443       GST_OBJECT_LOCK (selpad);
444       gst_segment_set_newsegment_full (&selpad->segment, update,
445           rate, arate, format, start, stop, time);
446       GST_OBJECT_UNLOCK (selpad);
448       /* If we aren't forwarding the event because the pad is not the
449        * active_sinkpad, then set the flag on the pad
450        * that says a segment needs sending if/when that pad is activated.
451        * For all other cases, we send the event immediately, which makes
452        * sparse streams and other segment updates work correctly downstream.
453        */
454       if (!forward)
455         selpad->segment_pending = TRUE;
457       GST_INPUT_SELECTOR_UNLOCK (sel);
458       break;
459     }
460     case GST_EVENT_TAG:
461     {
462       GstTagList *tags, *oldtags, *newtags;
464       gst_event_parse_tag (event, &tags);
466       GST_OBJECT_LOCK (selpad);
467       oldtags = selpad->tags;
469       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
470       selpad->tags = newtags;
471       if (oldtags)
472         gst_tag_list_free (oldtags);
473       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
474       GST_OBJECT_UNLOCK (selpad);
476       g_object_notify (G_OBJECT (selpad), "tags");
477       break;
478     }
479     case GST_EVENT_EOS:
480       selpad->eos = TRUE;
482       if (forward) {
483         selpad->eos_sent = TRUE;
484       } else {
485         GstSelectorPad *tmp;
487         /* If the active sinkpad is in EOS state but EOS
488          * was not sent downstream this means that the pad
489          * got EOS before it was set as active pad and that
490          * the previously active pad got EOS after it was
491          * active
492          */
493         GST_INPUT_SELECTOR_LOCK (sel);
494         active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
495         tmp = GST_SELECTOR_PAD (active_sinkpad);
496         forward = (tmp->eos && !tmp->eos_sent);
497         tmp->eos_sent = TRUE;
498         GST_INPUT_SELECTOR_UNLOCK (sel);
499       }
500       GST_DEBUG_OBJECT (pad, "received EOS");
501       break;
502     default:
503       break;
504   }
505   if (forward) {
506     GST_DEBUG_OBJECT (pad, "forwarding event");
507     res = gst_pad_push_event (sel->srcpad, event);
508   } else
509     gst_event_unref (event);
511   gst_object_unref (sel);
513   return res;
516 static GstCaps *
517 gst_selector_pad_getcaps (GstPad * pad)
519   GstInputSelector *sel;
520   GstCaps *caps;
522   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
523   if (G_UNLIKELY (sel == NULL))
524     return gst_caps_new_any ();
526   GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
527   caps = gst_pad_peer_get_caps_reffed (sel->srcpad);
528   if (caps == NULL)
529     caps = gst_caps_new_any ();
531   gst_object_unref (sel);
533   return caps;
536 static gboolean
537 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
539   GstInputSelector *sel;
540   gboolean res;
542   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
543   if (G_UNLIKELY (sel == NULL))
544     return FALSE;
546   GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
547   res = gst_pad_peer_accept_caps (sel->srcpad, caps);
548   gst_object_unref (sel);
550   return res;
553 static GstFlowReturn
554 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
555     guint size, GstCaps * caps, GstBuffer ** buf)
557   GstInputSelector *sel;
558   GstFlowReturn result;
559   GstPad *active_sinkpad;
560   GstPad *prev_active_sinkpad;
561   GstSelectorPad *selpad;
563   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
564   if (G_UNLIKELY (sel == NULL))
565     return GST_FLOW_WRONG_STATE;
567   selpad = GST_SELECTOR_PAD_CAST (pad);
569   GST_LOG_OBJECT (pad, "received alloc");
571   GST_INPUT_SELECTOR_LOCK (sel);
572   prev_active_sinkpad = sel->active_sinkpad;
573   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
575   if (pad != active_sinkpad)
576     goto not_active;
578   GST_INPUT_SELECTOR_UNLOCK (sel);
580   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
581     NOTIFY_MUTEX_LOCK ();
582     g_object_notify (G_OBJECT (sel), "active-pad");
583     NOTIFY_MUTEX_UNLOCK ();
584   }
586   result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
588 done:
589   gst_object_unref (sel);
591   return result;
593   /* ERRORS */
594 not_active:
595   {
596     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
598     GST_INPUT_SELECTOR_UNLOCK (sel);
600     /* unselected pad, perform fallback alloc or return unlinked when
601      * asked */
602     GST_OBJECT_LOCK (selpad);
603     if (selpad->always_ok || !active_pad_pushed) {
604       GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
605       *buf = NULL;
606       result = GST_FLOW_OK;
607     } else {
608       GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
609       result = GST_FLOW_NOT_LINKED;
610     }
611     GST_OBJECT_UNLOCK (selpad);
613     goto done;
614   }
617 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked 
618  * or return TRUE when flushing */
619 static gboolean
620 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
622   while (self->blocked && !self->flushing && !pad->flushing) {
623     /* we can be unlocked here when we are shutting down (flushing) or when we
624      * get unblocked */
625     GST_INPUT_SELECTOR_WAIT (self);
626   }
627   return self->flushing;
630 /* must be called with the SELECTOR_LOCK, will block until the running time
631  * of the active pad is after this pad or return TRUE when flushing */
632 static gboolean
633 gst_input_selector_wait_running_time (GstInputSelector * sel,
634     GstSelectorPad * pad, GstBuffer * buf)
636   GstPad *active_sinkpad;
637   GstSelectorPad *active_selpad;
638   GstSegment *seg, *active_seg;
639   GstClockTime running_time, active_running_time = -1;
641   seg = &pad->segment;
643   active_sinkpad =
644       gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
645   active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
646   active_seg = &active_selpad->segment;
648   /* We can only sync if the segments are in time format or
649    * if the active pad had no newsegment event yet */
650   if (seg->format != GST_FORMAT_TIME ||
651       (active_seg->format != GST_FORMAT_TIME
652           && active_seg->format != GST_FORMAT_UNDEFINED))
653     return FALSE;
655   /* If we have no valid timestamp we can't sync this buffer */
656   if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf))
657     return FALSE;
659   running_time = GST_BUFFER_TIMESTAMP (buf);
660   /* If possible try to get the running time at the end of the buffer */
661   if (GST_BUFFER_DURATION_IS_VALID (buf))
662     running_time += GST_BUFFER_DURATION (buf);
663   if (running_time > seg->stop)
664     running_time = seg->stop;
665   running_time =
666       gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
667   /* If this is outside the segment don't sync */
668   if (running_time == -1)
669     return FALSE;
671   /* Get active pad's running time, if no configured segment yet keep at -1 */
672   if (active_seg->format == GST_FORMAT_TIME)
673     active_running_time =
674         gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
675         active_seg->last_stop);
677   /* Wait until
678    *   a) this is the active pad
679    *   b) the pad or the selector is flushing
680    *   c) the selector is not blocked
681    *   d) the active pad has no running time or the active
682    *      pad's running time is before this running time
683    *   e) the active pad has a non-time segment
684    */
685   while (pad != active_selpad && !sel->flushing && !pad->flushing &&
686       (sel->blocked || active_running_time == -1
687           || running_time >= active_running_time)) {
688     if (!sel->blocked)
689       GST_DEBUG_OBJECT (pad,
690           "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
691           GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
692           GST_TIME_ARGS (active_running_time));
694     GST_INPUT_SELECTOR_WAIT (sel);
696     /* Get new active pad, it might have changed */
697     active_sinkpad =
698         gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
699     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
700     active_seg = &active_selpad->segment;
702     /* If the active segment is configured but not to time format
703      * we can't do any syncing at all */
704     if (active_seg->format != GST_FORMAT_TIME
705         && active_seg->format != GST_FORMAT_UNDEFINED)
706       break;
708     /* Get the new active pad running time */
709     if (active_seg->format == GST_FORMAT_TIME)
710       active_running_time =
711           gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
712           active_seg->last_stop);
713     else
714       active_running_time = -1;
716     if (!sel->blocked)
717       GST_DEBUG_OBJECT (pad,
718           "Waited for active streams to advance. %" GST_TIME_FORMAT " >= %"
719           GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
720           GST_TIME_ARGS (active_running_time));
722   }
724   /* Return TRUE if the selector or the pad is flushing */
725   return (sel->flushing || pad->flushing);
729 static GstFlowReturn
730 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
732   GstInputSelector *sel;
733   GstFlowReturn res;
734   GstPad *active_sinkpad;
735   GstPad *prev_active_sinkpad;
736   GstSelectorPad *selpad;
737   GstClockTime start_time;
738   GstSegment *seg;
739   GstEvent *close_event = NULL, *start_event = NULL;
740   GstCaps *caps;
742   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
743   selpad = GST_SELECTOR_PAD_CAST (pad);
744   seg = &selpad->segment;
746   GST_INPUT_SELECTOR_LOCK (sel);
747   /* wait or check for flushing */
748   if (gst_input_selector_wait (sel, selpad))
749     goto flushing;
751   GST_LOG_OBJECT (pad, "getting active pad");
753   prev_active_sinkpad = sel->active_sinkpad;
754   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
756   /* In sync mode wait until the active pad has advanced
757    * after the running time of the current buffer */
758   if (sel->sync_streams && active_sinkpad != pad) {
759     if (gst_input_selector_wait_running_time (sel, selpad, buf))
760       goto flushing;
761   }
763   /* Might have changed while waiting */
764   active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
766   /* update the segment on the srcpad */
767   start_time = GST_BUFFER_TIMESTAMP (buf);
768   if (GST_CLOCK_TIME_IS_VALID (start_time)) {
769     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
770         GST_TIME_ARGS (start_time));
771     if (GST_BUFFER_DURATION_IS_VALID (buf))
772       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
773           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
775     GST_OBJECT_LOCK (pad);
776     gst_segment_set_last_stop (seg, seg->format, start_time);
777     GST_OBJECT_UNLOCK (pad);
778   }
780   /* Ignore buffers from pads except the selected one */
781   if (pad != active_sinkpad)
782     goto ignore;
784   /* Tell all non-active pads that we advanced the running time */
785   if (sel->sync_streams)
786     GST_INPUT_SELECTOR_BROADCAST (sel);
788   if (G_UNLIKELY (sel->pending_close)) {
789     GstSegment *cseg = &sel->segment;
791     GST_DEBUG_OBJECT (sel,
792         "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
793         "format %d, "
794         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
795         G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
796         cseg->start, cseg->stop, cseg->time);
798     /* create update segment */
799     close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
800         cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
802     sel->pending_close = FALSE;
803   }
804   /* if we have a pending segment, push it out now */
805   if (G_UNLIKELY (selpad->segment_pending)) {
806     GST_DEBUG_OBJECT (pad,
807         "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
808         "format %d, "
809         "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
810         G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
811         seg->start, seg->stop, seg->time);
813     start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
814         seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
816     selpad->segment_pending = FALSE;
817   }
818   GST_INPUT_SELECTOR_UNLOCK (sel);
820   if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
821     NOTIFY_MUTEX_LOCK ();
822     g_object_notify (G_OBJECT (sel), "active-pad");
823     NOTIFY_MUTEX_UNLOCK ();
824   }
826   if (close_event)
827     gst_pad_push_event (sel->srcpad, close_event);
829   if (start_event)
830     gst_pad_push_event (sel->srcpad, start_event);
832   if (selpad->discont) {
833     buf = gst_buffer_make_metadata_writable (buf);
835     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
836     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
837     selpad->discont = FALSE;
838   }
840   /* forward */
841   GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
843   if ((caps = GST_BUFFER_CAPS (buf))) {
844     if (GST_PAD_CAPS (sel->srcpad) != caps)
845       gst_pad_set_caps (sel->srcpad, caps);
846   }
848   res = gst_pad_push (sel->srcpad, buf);
849   selpad->pushed = TRUE;
851 done:
852   gst_object_unref (sel);
853   return res;
855   /* dropped buffers */
856 ignore:
857   {
858     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
860     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
861     /* when we drop a buffer, we're creating a discont on this pad */
862     selpad->discont = TRUE;
863     GST_INPUT_SELECTOR_UNLOCK (sel);
864     gst_buffer_unref (buf);
866     /* figure out what to return upstream */
867     GST_OBJECT_LOCK (selpad);
868     if (selpad->always_ok || !active_pad_pushed)
869       res = GST_FLOW_OK;
870     else
871       res = GST_FLOW_NOT_LINKED;
872     GST_OBJECT_UNLOCK (selpad);
874     goto done;
875   }
876 flushing:
877   {
878     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
879     GST_INPUT_SELECTOR_UNLOCK (sel);
880     gst_buffer_unref (buf);
881     res = GST_FLOW_WRONG_STATE;
882     goto done;
883   }
886 static void gst_input_selector_dispose (GObject * object);
888 static void gst_input_selector_set_property (GObject * object,
889     guint prop_id, const GValue * value, GParamSpec * pspec);
890 static void gst_input_selector_get_property (GObject * object,
891     guint prop_id, GValue * value, GParamSpec * pspec);
893 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
894     GstPadTemplate * templ, const gchar * unused);
895 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
897 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
898     element, GstStateChange transition);
900 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
901 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
902 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
903 static gint64 gst_input_selector_block (GstInputSelector * self);
904 static void gst_input_selector_switch (GstInputSelector * self,
905     GstPad * pad, gint64 stop_time, gint64 start_time);
907 /* FIXME: create these marshallers using glib-genmarshal */
908 #define g_marshal_value_peek_object(v)   (v)->data[0].v_pointer
909 #define g_marshal_value_peek_int64(v)    (v)->data[0].v_int64
911 static void
912 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
913     GValue * return_value G_GNUC_UNUSED,
914     guint n_param_values,
915     const GValue * param_values,
916     gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
918   typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
919   register GMarshalFunc_INT64__VOID callback;
920   register GCClosure *cc = (GCClosure *) closure;
921   register gpointer data1, data2;
922   gint64 v_return;
924   g_return_if_fail (return_value != NULL);
925   g_return_if_fail (n_param_values == 1);
927   if (G_CCLOSURE_SWAP_DATA (closure)) {
928     data1 = closure->data;
929     data2 = g_value_peek_pointer (param_values + 0);
930   } else {
931     data1 = g_value_peek_pointer (param_values + 0);
932     data2 = closure->data;
933   }
934   callback =
935       (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
937   v_return = callback (data1, data2);
939   g_value_set_int64 (return_value, v_return);
942 static void
943 gst_input_selector_marshal_VOID__OBJECT_INT64_INT64 (GClosure * closure,
944     GValue * return_value G_GNUC_UNUSED,
945     guint n_param_values,
946     const GValue * param_values,
947     gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
949   typedef void (*GMarshalFunc_VOID__OBJECT_INT64_INT64) (gpointer data1,
950       gpointer arg_1, gint64 arg_2, gint64 arg_3, gpointer data2);
951   register GMarshalFunc_VOID__OBJECT_INT64_INT64 callback;
952   register GCClosure *cc = (GCClosure *) closure;
953   register gpointer data1, data2;
955   g_return_if_fail (n_param_values == 4);
957   if (G_CCLOSURE_SWAP_DATA (closure)) {
958     data1 = closure->data;
959     data2 = g_value_peek_pointer (param_values + 0);
960   } else {
961     data1 = g_value_peek_pointer (param_values + 0);
962     data2 = closure->data;
963   }
964   callback =
965       (GMarshalFunc_VOID__OBJECT_INT64_INT64) (marshal_data ? marshal_data :
966       cc->callback);
968   callback (data1,
969       g_marshal_value_peek_object (param_values + 1),
970       g_marshal_value_peek_int64 (param_values + 2),
971       g_marshal_value_peek_int64 (param_values + 3), data2);
974 #define _do_init(bla) \
975     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
976         "input-selector", 0, "An input stream selector element");
978 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
979     GST_TYPE_ELEMENT, _do_init);
981 static void
982 gst_input_selector_base_init (gpointer g_class)
984   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
986   gst_element_class_set_details_simple (element_class, "Input selector",
987       "Generic", "N-to-1 input stream selector",
988       "Julien Moutte <julien@moutte.net>, "
989       "Jan Schmidt <thaytan@mad.scientist.com>, "
990       "Wim Taymans <wim.taymans@gmail.com>");
991   gst_element_class_add_pad_template (element_class,
992       gst_static_pad_template_get (&gst_input_selector_sink_factory));
993   gst_element_class_add_pad_template (element_class,
994       gst_static_pad_template_get (&gst_input_selector_src_factory));
997 static void
998 gst_input_selector_class_init (GstInputSelectorClass * klass)
1000   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1001   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1003   gobject_class->dispose = gst_input_selector_dispose;
1005   gobject_class->set_property = gst_input_selector_set_property;
1006   gobject_class->get_property = gst_input_selector_get_property;
1008   g_object_class_install_property (gobject_class, PROP_N_PADS,
1009       g_param_spec_uint ("n-pads", "Number of Pads",
1010           "The number of sink pads", 0, G_MAXUINT, 0,
1011           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1013   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1014       g_param_spec_object ("active-pad", "Active pad",
1015           "The currently active sink pad", GST_TYPE_PAD,
1016           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1018   /**
1019    * GstInputSelector:sync-streams
1020    *
1021    * If set to %TRUE all inactive streams will be synced to the
1022    * running time of the active stream. This makes sure that no
1023    * buffers are dropped by input-selector that might be needed
1024    * when switching the active pad.
1025    *
1026    * Since: 0.10.35
1027    */
1028   g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1029       g_param_spec_boolean ("sync-streams", "Sync Streams",
1030           "Synchronize inactive streams to the running time of the active stream",
1031           DEFAULT_SYNC_STREAMS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1033   /**
1034    * GstInputSelector::block:
1035    * @inputselector: the #GstInputSelector
1036    *
1037    * Block all sink pads in preparation for a switch. Returns the stop time of
1038    * the current switch segment, as a running time, or 0 if there is no current
1039    * active pad or the current active pad never received data.
1040    */
1041   gst_input_selector_signals[SIGNAL_BLOCK] =
1042       g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1043       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1044       G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1045       gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
1046   /**
1047    * GstInputSelector::switch:
1048    * @inputselector: the #GstInputSelector
1049    * @pad:            the pad to switch to
1050    * @stop_time:      running time at which to close the previous segment, or -1
1051    *                  to use the running time of the previously active sink pad
1052    * @start_time:     running time at which to start the new segment, or -1 to
1053    *                  use the running time of the newly active sink pad
1054    *
1055    * Switch to a new feed. The segment opened by the previously active pad, if
1056    * any, will be closed, and a new segment opened before data flows again.
1057    *
1058    * This signal must be emitted when the element has been blocked via the <link
1059    * linkend="GstInputSelector-block">block</link> signal.
1060    *
1061    * If you have a stream with only one switch element, such as an audio-only
1062    * stream, a stream switch should be performed by first emitting the block
1063    * signal, and then emitting the switch signal with -1 for the stop and start
1064    * time values.
1065    *
1066    * The intention of the @stop_time and @start_time arguments is to allow
1067    * multiple switch elements to switch and maintain stream synchronization.
1068    * When switching a stream with multiple feeds, you will need as many switch
1069    * elements as you have feeds. For example, a feed with audio and video will
1070    * have one switch element between the audio feeds and one for video.
1071    *
1072    * A switch over multiple switch elements should be performed as follows:
1073    * First, emit the <link linkend="GstInputSelector-block">block</link>
1074    * signal, collecting the returned values. The maximum running time returned
1075    * by block should then be used as the time at which to close the previous
1076    * segment.
1077    *
1078    * Then, query the running times of the new audio and video pads that you will
1079    * switch to. Naturally, these pads are on separate switch elements. Take the
1080    * minimum running time for those streams and use it for the time at which to
1081    * open the new segment.
1082    *
1083    * If @pad is the same as the current active pad, the element will cancel any
1084    * previous block without adjusting segments.
1085    *
1086    * <note><simpara>
1087    * the signal changed from accepting the pad name to the pad object.
1088    * </simpara></note>
1089    *
1090    * Since: 0.10.7
1091    */
1092   gst_input_selector_signals[SIGNAL_SWITCH] =
1093       g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
1094       G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
1095       NULL, NULL, gst_input_selector_marshal_VOID__OBJECT_INT64_INT64,
1096       G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
1098   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1099   gstelement_class->release_pad = gst_input_selector_release_pad;
1100   gstelement_class->change_state = gst_input_selector_change_state;
1102   klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1103   /* note the underscore because switch is a keyword otherwise */
1104   klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
1107 static void
1108 gst_input_selector_init (GstInputSelector * sel,
1109     GstInputSelectorClass * g_class)
1111   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1112   gst_pad_set_iterate_internal_links_function (sel->srcpad,
1113       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1114   gst_pad_set_getcaps_function (sel->srcpad,
1115       GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
1116   gst_pad_set_query_function (sel->srcpad,
1117       GST_DEBUG_FUNCPTR (gst_input_selector_query));
1118   gst_pad_set_event_function (sel->srcpad,
1119       GST_DEBUG_FUNCPTR (gst_input_selector_event));
1120   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1121   /* sinkpad management */
1122   sel->active_sinkpad = NULL;
1123   sel->padcount = 0;
1124   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1125   sel->sync_streams = DEFAULT_SYNC_STREAMS;
1127   sel->lock = g_mutex_new ();
1128   sel->cond = g_cond_new ();
1129   sel->blocked = FALSE;
1132 static void
1133 gst_input_selector_dispose (GObject * object)
1135   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1137   if (sel->active_sinkpad) {
1138     gst_object_unref (sel->active_sinkpad);
1139     sel->active_sinkpad = NULL;
1140   }
1141   if (sel->lock) {
1142     g_mutex_free (sel->lock);
1143     sel->lock = NULL;
1144   }
1145   if (sel->cond) {
1146     g_cond_free (sel->cond);
1147     sel->cond = NULL;
1148   }
1150   G_OBJECT_CLASS (parent_class)->dispose (object);
1153 /* Solve the following equation for B.timestamp, and set that as the segment
1154  * stop:
1155  * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
1156  */
1157 static gint64
1158 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
1160   if (running_time <= segment->accum)
1161     return segment->start;
1162   else
1163     return (running_time - segment->accum) * segment->abs_rate + segment->start;
1166 static void
1167 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
1169   segment->stop = gst_segment_get_timestamp (segment, running_time);
1170   segment->last_stop = -1;
1173 static void
1174 gst_segment_set_start (GstSegment * segment, gint64 running_time)
1176   gint64 new_start, duration;
1178   new_start = gst_segment_get_timestamp (segment, running_time);
1180   /* this is the duration we skipped */
1181   duration = new_start - segment->start;
1182   /* add the duration to the accumulated segment time */
1183   segment->accum += duration;
1184   /* move position in the segment */
1185   segment->time += duration;
1186   segment->start += duration;
1189 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1190  * active pad changed. */
1191 static gboolean
1192 gst_input_selector_set_active_pad (GstInputSelector * self,
1193     GstPad * pad, gint64 stop_time, gint64 start_time)
1195   GstSelectorPad *old, *new;
1196   GstPad **active_pad_p;
1198   if (pad == self->active_sinkpad)
1199     return FALSE;
1201   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1202   new = GST_SELECTOR_PAD_CAST (pad);
1204   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1205       GST_DEBUG_PAD_NAME (new));
1207   if (!GST_CLOCK_TIME_IS_VALID (stop_time) && old) {
1208     /* no stop time given, get the latest running_time on the active pad to 
1209      * close and open the new segment */
1210     stop_time = start_time = gst_selector_pad_get_running_time (old);
1211     GST_DEBUG_OBJECT (self, "using start/stop of %" GST_TIME_FORMAT,
1212         GST_TIME_ARGS (start_time));
1213   }
1215   if (old && old->active && !self->pending_close && stop_time >= 0) {
1216     /* schedule a last_stop update if one isn't already scheduled, and a
1217        segment has been pushed before. */
1218     memcpy (&self->segment, &old->segment, sizeof (self->segment));
1220     GST_DEBUG_OBJECT (self, "setting stop_time to %" GST_TIME_FORMAT,
1221         GST_TIME_ARGS (stop_time));
1222     gst_segment_set_stop (&self->segment, stop_time);
1223     self->pending_close = TRUE;
1224   }
1225   if (old)
1226     old->pushed = FALSE;
1228   if (new && new->active && start_time >= 0) {
1229     GST_DEBUG_OBJECT (self, "setting start_time to %" GST_TIME_FORMAT,
1230         GST_TIME_ARGS (start_time));
1231     /* schedule a new segment push */
1232     gst_segment_set_start (&new->segment, start_time);
1233     new->segment_pending = TRUE;
1234   }
1235   if (new)
1236     new->pushed = FALSE;
1238   active_pad_p = &self->active_sinkpad;
1239   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1241   /* Wake up all non-active pads in sync mode, they might be
1242    * the active pad now */
1243   if (self->sync_streams)
1244     GST_INPUT_SELECTOR_BROADCAST (self);
1246   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1247       self->active_sinkpad);
1249   return TRUE;
1252 static void
1253 gst_input_selector_set_property (GObject * object, guint prop_id,
1254     const GValue * value, GParamSpec * pspec)
1256   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1258   switch (prop_id) {
1259     case PROP_ACTIVE_PAD:
1260     {
1261       GstPad *pad;
1263       pad = g_value_get_object (value);
1265       GST_INPUT_SELECTOR_LOCK (sel);
1266       gst_input_selector_set_active_pad (sel, pad,
1267           GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1268       GST_INPUT_SELECTOR_UNLOCK (sel);
1269       break;
1270     }
1271     case PROP_SYNC_STREAMS:
1272     {
1273       GST_INPUT_SELECTOR_LOCK (sel);
1274       sel->sync_streams = g_value_get_boolean (value);
1275       GST_INPUT_SELECTOR_UNLOCK (sel);
1276       break;
1277     }
1278     default:
1279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1280       break;
1281   }
1284 static void
1285 gst_input_selector_get_property (GObject * object, guint prop_id,
1286     GValue * value, GParamSpec * pspec)
1288   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1290   switch (prop_id) {
1291     case PROP_N_PADS:
1292       GST_INPUT_SELECTOR_LOCK (object);
1293       g_value_set_uint (value, sel->n_pads);
1294       GST_INPUT_SELECTOR_UNLOCK (object);
1295       break;
1296     case PROP_ACTIVE_PAD:
1297       GST_INPUT_SELECTOR_LOCK (object);
1298       g_value_set_object (value, sel->active_sinkpad);
1299       GST_INPUT_SELECTOR_UNLOCK (object);
1300       break;
1301     case PROP_SYNC_STREAMS:
1302       GST_INPUT_SELECTOR_LOCK (object);
1303       g_value_set_boolean (value, sel->sync_streams);
1304       GST_INPUT_SELECTOR_UNLOCK (object);
1305       break;
1306     default:
1307       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1308       break;
1309   }
1312 static GstPad *
1313 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1314     gboolean strict)
1316   GstPad *otherpad = NULL;
1318   GST_INPUT_SELECTOR_LOCK (sel);
1319   if (pad == sel->srcpad)
1320     otherpad = sel->active_sinkpad;
1321   else if (pad == sel->active_sinkpad || !strict)
1322     otherpad = sel->srcpad;
1323   if (otherpad)
1324     gst_object_ref (otherpad);
1325   GST_INPUT_SELECTOR_UNLOCK (sel);
1327   return otherpad;
1330 static gboolean
1331 gst_input_selector_event (GstPad * pad, GstEvent * event)
1333   GstInputSelector *sel;
1334   gboolean res = FALSE;
1335   GstPad *otherpad;
1337   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1338   if (G_UNLIKELY (sel == NULL)) {
1339     gst_event_unref (event);
1340     return FALSE;
1341   }
1343   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
1344   if (otherpad) {
1345     res = gst_pad_push_event (otherpad, event);
1347     gst_object_unref (otherpad);
1348   } else
1349     gst_event_unref (event);
1351   gst_object_unref (sel);
1352   return res;
1355 /* query on the srcpad. We override this function because by default it will
1356  * only forward the query to one random sinkpad */
1357 static gboolean
1358 gst_input_selector_query (GstPad * pad, GstQuery * query)
1360   gboolean res = TRUE;
1361   GstInputSelector *sel;
1362   GstPad *otherpad;
1364   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1365   if (G_UNLIKELY (sel == NULL))
1366     return FALSE;
1368   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
1370   switch (GST_QUERY_TYPE (query)) {
1371     case GST_QUERY_LATENCY:
1372     {
1373       GList *walk;
1374       GstClockTime resmin, resmax;
1375       gboolean reslive;
1377       resmin = 0;
1378       resmax = -1;
1379       reslive = FALSE;
1381       /* assume FALSE, we become TRUE if one query succeeds */
1382       res = FALSE;
1384       /* perform the query on all sinkpads and combine the results. We take the
1385        * max of min and the min of max for the result latency. */
1386       GST_INPUT_SELECTOR_LOCK (sel);
1387       for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1388           walk = g_list_next (walk)) {
1389         GstPad *sinkpad = GST_PAD_CAST (walk->data);
1391         if (gst_pad_peer_query (sinkpad, query)) {
1392           GstClockTime min, max;
1393           gboolean live;
1395           /* one query succeeded, we succeed too */
1396           res = TRUE;
1398           gst_query_parse_latency (query, &live, &min, &max);
1400           GST_DEBUG_OBJECT (sinkpad,
1401               "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1402               ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1404           if (live) {
1405             if (min > resmin)
1406               resmin = min;
1407             if (resmax == -1)
1408               resmax = max;
1409             else if (max < resmax)
1410               resmax = max;
1411             if (reslive == FALSE)
1412               reslive = live;
1413           }
1414         }
1415       }
1416       GST_INPUT_SELECTOR_UNLOCK (sel);
1417       if (res) {
1418         gst_query_set_latency (query, reslive, resmin, resmax);
1420         GST_DEBUG_OBJECT (sel,
1421             "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1422             ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1423             reslive);
1424       }
1426       break;
1427     }
1428     default:
1429       if (otherpad)
1430         res = gst_pad_peer_query (otherpad, query);
1431       break;
1432   }
1433   if (otherpad)
1434     gst_object_unref (otherpad);
1435   gst_object_unref (sel);
1437   return res;
1440 static GstCaps *
1441 gst_input_selector_getcaps (GstPad * pad)
1443   GstPad *otherpad;
1444   GstInputSelector *sel;
1445   GstCaps *caps;
1447   sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1448   if (G_UNLIKELY (sel == NULL))
1449     return gst_caps_new_any ();
1451   otherpad = gst_input_selector_get_linked_pad (sel, pad, FALSE);
1453   if (!otherpad) {
1454     GST_DEBUG_OBJECT (pad, "Pad not linked, returning ANY");
1455     caps = gst_caps_new_any ();
1456   } else {
1457     GST_DEBUG_OBJECT (pad, "Pad is linked (to %s:%s), returning peer caps",
1458         GST_DEBUG_PAD_NAME (otherpad));
1459     /* if the peer has caps, use those. If the pad is not linked, this function
1460      * returns NULL and we return ANY */
1461     if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1462       caps = gst_caps_new_any ();
1463     gst_object_unref (otherpad);
1464   }
1466   gst_object_unref (sel);
1467   return caps;
1470 /* check if the pad is the active sinkpad */
1471 static inline gboolean
1472 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1474   gboolean res;
1476   GST_INPUT_SELECTOR_LOCK (sel);
1477   res = (pad == sel->active_sinkpad);
1478   GST_INPUT_SELECTOR_UNLOCK (sel);
1480   return res;
1483 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1484 static GstPad *
1485 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1487   GstPad *active_sinkpad;
1488   GstSelectorPad *selpad;
1490   selpad = GST_SELECTOR_PAD_CAST (pad);
1492   selpad->active = TRUE;
1493   active_sinkpad = sel->active_sinkpad;
1494   if (active_sinkpad == NULL) {
1495     /* first pad we get activity on becomes the activated pad by default */
1496     if (sel->active_sinkpad)
1497       gst_object_unref (sel->active_sinkpad);
1498     active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1499     GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1500   }
1502   return active_sinkpad;
1505 static GstPad *
1506 gst_input_selector_request_new_pad (GstElement * element,
1507     GstPadTemplate * templ, const gchar * unused)
1509   GstInputSelector *sel;
1510   gchar *name = NULL;
1511   GstPad *sinkpad = NULL;
1513   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1515   sel = GST_INPUT_SELECTOR (element);
1517   GST_INPUT_SELECTOR_LOCK (sel);
1519   GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1520   name = g_strdup_printf ("sink%d", sel->padcount++);
1521   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1522       "name", name, "direction", templ->direction, "template", templ, NULL);
1523   g_free (name);
1525   sel->n_pads++;
1527   gst_pad_set_event_function (sinkpad,
1528       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1529   gst_pad_set_getcaps_function (sinkpad,
1530       GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1531   gst_pad_set_acceptcaps_function (sinkpad,
1532       GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1533   gst_pad_set_chain_function (sinkpad,
1534       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1535   gst_pad_set_iterate_internal_links_function (sinkpad,
1536       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1537   gst_pad_set_bufferalloc_function (sinkpad,
1538       GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1540   gst_pad_set_active (sinkpad, TRUE);
1541   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1542   GST_INPUT_SELECTOR_UNLOCK (sel);
1544   return sinkpad;
1547 static void
1548 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1550   GstInputSelector *sel;
1552   sel = GST_INPUT_SELECTOR (element);
1553   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1555   GST_INPUT_SELECTOR_LOCK (sel);
1556   /* if the pad was the active pad, makes us select a new one */
1557   if (sel->active_sinkpad == pad) {
1558     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1559     gst_object_unref (sel->active_sinkpad);
1560     sel->active_sinkpad = NULL;
1561   }
1562   sel->n_pads--;
1564   gst_pad_set_active (pad, FALSE);
1565   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1566   GST_INPUT_SELECTOR_UNLOCK (sel);
1569 static void
1570 gst_input_selector_reset (GstInputSelector * sel)
1572   GList *walk;
1574   GST_INPUT_SELECTOR_LOCK (sel);
1575   /* clear active pad */
1576   if (sel->active_sinkpad) {
1577     gst_object_unref (sel->active_sinkpad);
1578     sel->active_sinkpad = NULL;
1579   }
1580   /* reset segment */
1581   gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1582   sel->pending_close = FALSE;
1583   /* reset each of our sinkpads state */
1584   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1585     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1587     gst_selector_pad_reset (selpad);
1589     if (selpad->tags) {
1590       gst_tag_list_free (selpad->tags);
1591       selpad->tags = NULL;
1592     }
1593   }
1594   GST_INPUT_SELECTOR_UNLOCK (sel);
1597 static GstStateChangeReturn
1598 gst_input_selector_change_state (GstElement * element,
1599     GstStateChange transition)
1601   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1602   GstStateChangeReturn result;
1604   switch (transition) {
1605     case GST_STATE_CHANGE_READY_TO_PAUSED:
1606       GST_INPUT_SELECTOR_LOCK (self);
1607       self->blocked = FALSE;
1608       self->flushing = FALSE;
1609       GST_INPUT_SELECTOR_UNLOCK (self);
1610       break;
1611     case GST_STATE_CHANGE_PAUSED_TO_READY:
1612       /* first unlock before we call the parent state change function, which
1613        * tries to acquire the stream lock when going to ready. */
1614       GST_INPUT_SELECTOR_LOCK (self);
1615       self->blocked = FALSE;
1616       self->flushing = TRUE;
1617       GST_INPUT_SELECTOR_BROADCAST (self);
1618       GST_INPUT_SELECTOR_UNLOCK (self);
1619       break;
1620     default:
1621       break;
1622   }
1624   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1626   switch (transition) {
1627     case GST_STATE_CHANGE_PAUSED_TO_READY:
1628       gst_input_selector_reset (self);
1629       break;
1630     default:
1631       break;
1632   }
1634   return result;
1637 static gint64
1638 gst_input_selector_block (GstInputSelector * self)
1640   gint64 ret = 0;
1641   GstSelectorPad *spad;
1643   GST_INPUT_SELECTOR_LOCK (self);
1645   if (self->blocked)
1646     GST_WARNING_OBJECT (self, "switch already blocked");
1648   self->blocked = TRUE;
1649   spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1651   if (spad)
1652     ret = gst_selector_pad_get_running_time (spad);
1653   else
1654     GST_DEBUG_OBJECT (self, "no active pad while blocking");
1656   GST_INPUT_SELECTOR_UNLOCK (self);
1658   return ret;
1661 /* stop_time and start_time are running times */
1662 static void
1663 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1664     gint64 stop_time, gint64 start_time)
1666   gboolean changed;
1668   g_return_if_fail (self->blocked == TRUE);
1670   GST_INPUT_SELECTOR_LOCK (self);
1671   changed =
1672       gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1674   self->blocked = FALSE;
1675   GST_INPUT_SELECTOR_BROADCAST (self);
1676   GST_INPUT_SELECTOR_UNLOCK (self);
1678   if (changed) {
1679     NOTIFY_MUTEX_LOCK ();
1680     g_object_notify (G_OBJECT (self), "active-pad");
1681     NOTIFY_MUTEX_UNLOCK ();
1682   }