• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 
26 /**
27  * SECTION:element-input-selector
28  * @title: input-selector
29  * @see_also: #GstOutputSelector
30  *
31  * Direct one out of N input streams to the output pad.
32  *
33  * The input pads are from a GstPad subclass and have additional
34  * properties, which users may find useful, namely:
35  *
36  * * "running-time": Running time of stream on pad (#gint64)
37  * * "tags": The currently active tags on the pad (#GstTagList, boxed type)
38  * * "active": If the pad is currently active (#gboolean)
39  * * "always-ok" : Make an inactive pads return #GST_FLOW_OK instead of
40  * #GST_FLOW_NOT_LINKED
41  *
42  */
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <string.h>
49 
50 #include "gstinputselector.h"
51 #include "gstcoreelementselements.h"
52 
53 #define DEBUG_CACHED_BUFFERS 0
54 
55 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
56 #define GST_CAT_DEFAULT input_selector_debug
57 
58 #define GST_TYPE_INPUT_SELECTOR_SYNC_MODE (gst_input_selector_sync_mode_get_type())
59 static GType
gst_input_selector_sync_mode_get_type(void)60 gst_input_selector_sync_mode_get_type (void)
61 {
62   static GType type = 0;
63   static const GEnumValue data[] = {
64     {GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT,
65           "Sync using the current active segment",
66         "active-segment"},
67     {GST_INPUT_SELECTOR_SYNC_MODE_CLOCK, "Sync using the clock", "clock"},
68     {0, NULL, NULL},
69   };
70 
71   if (!type) {
72     type = g_enum_register_static ("GstInputSelectorSyncMode", data);
73   }
74   return type;
75 }
76 
77 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (&((GstInputSelector*)(sel))->lock)
78 #define GST_INPUT_SELECTOR_GET_COND(sel) (&((GstInputSelector*)(sel))->cond)
79 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
80 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
81 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
82 			GST_INPUT_SELECTOR_GET_LOCK(sel)))
83 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
84 
85 static GstStaticPadTemplate gst_input_selector_sink_factory =
86 GST_STATIC_PAD_TEMPLATE ("sink_%u",
87     GST_PAD_SINK,
88     GST_PAD_REQUEST,
89     GST_STATIC_CAPS_ANY);
90 
91 static GstStaticPadTemplate gst_input_selector_src_factory =
92 GST_STATIC_PAD_TEMPLATE ("src",
93     GST_PAD_SRC,
94     GST_PAD_ALWAYS,
95     GST_STATIC_CAPS_ANY);
96 
97 enum
98 {
99   PROP_0,
100   PROP_N_PADS,
101   PROP_ACTIVE_PAD,
102   PROP_SYNC_STREAMS,
103   PROP_SYNC_MODE,
104   PROP_CACHE_BUFFERS
105 };
106 
107 #define DEFAULT_SYNC_STREAMS TRUE
108 #define DEFAULT_SYNC_MODE GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT
109 #define DEFAULT_CACHE_BUFFERS FALSE
110 #define DEFAULT_PAD_ALWAYS_OK TRUE
111 
112 enum
113 {
114   PROP_PAD_0,
115   PROP_PAD_RUNNING_TIME,
116   PROP_PAD_TAGS,
117   PROP_PAD_ACTIVE,
118   PROP_PAD_ALWAYS_OK
119 };
120 
121 static void gst_input_selector_active_pad_changed (GstInputSelector * sel,
122     GParamSpec * pspec, gpointer user_data);
123 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
124     sel, GstPad * pad);
125 static GstPad *gst_input_selector_get_active_sinkpad (GstInputSelector * sel);
126 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
127     GstPad * pad, gboolean strict);
128 
129 #define GST_TYPE_SELECTOR_PAD \
130   (gst_selector_pad_get_type())
131 #define GST_SELECTOR_PAD(obj) \
132   (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
133 #define GST_SELECTOR_PAD_CLASS(klass) \
134   (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
135 #define GST_IS_SELECTOR_PAD(obj) \
136   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
137 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
138   (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
139 #define GST_SELECTOR_PAD_CAST(obj) \
140   ((GstSelectorPad *)(obj))
141 
142 typedef struct _GstSelectorPad GstSelectorPad;
143 typedef struct _GstSelectorPadClass GstSelectorPadClass;
144 typedef struct _GstSelectorPadCachedBuffer GstSelectorPadCachedBuffer;
145 
146 struct _GstSelectorPad
147 {
148   GstPad parent;
149 
150   gboolean pushed;              /* when buffer was pushed downstream since activation */
151   guint group_id;               /* Group ID from the last stream-start */
152   gboolean group_done;          /* when Stream Group Done has been
153                                    received */
154   gboolean eos;                 /* when EOS has been received */
155   gboolean eos_sent;            /* when EOS was sent downstream */
156   gboolean discont;             /* after switching we create a discont */
157   gboolean flushing;            /* set after flush-start and before flush-stop */
158   gboolean always_ok;
159   GstTagList *tags;             /* last tags received on the pad */
160 
161   GstSegment segment;           /* the current segment on the pad */
162   guint32 segment_seqnum;       /* sequence number of the current segment */
163 
164   gboolean events_pending;      /* TRUE if sticky events need to be updated */
165 
166   gboolean sending_cached_buffers;
167   GQueue *cached_buffers;
168 };
169 
170 struct _GstSelectorPadCachedBuffer
171 {
172   GstBuffer *buffer;
173   GstSegment segment;
174 };
175 
176 struct _GstSelectorPadClass
177 {
178   GstPadClass parent;
179 };
180 
181 GType gst_selector_pad_get_type (void);
182 static void gst_selector_pad_finalize (GObject * object);
183 static void gst_selector_pad_get_property (GObject * object,
184     guint prop_id, GValue * value, GParamSpec * pspec);
185 static void gst_selector_pad_set_property (GObject * object,
186     guint prop_id, const GValue * value, GParamSpec * pspec);
187 
188 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
189 static void gst_selector_pad_reset (GstSelectorPad * pad);
190 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
191     GstEvent * event);
192 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
193     GstQuery * query);
194 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
195     GstObject * parent);
196 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
197     GstBuffer * buf);
198 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
199     GstBuffer * buffer);
200 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
201 
202 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
203 
204 static void
gst_selector_pad_class_init(GstSelectorPadClass * klass)205 gst_selector_pad_class_init (GstSelectorPadClass * klass)
206 {
207   GObjectClass *gobject_class;
208 
209   gobject_class = (GObjectClass *) klass;
210 
211   gobject_class->finalize = gst_selector_pad_finalize;
212 
213   gobject_class->get_property = gst_selector_pad_get_property;
214   gobject_class->set_property = gst_selector_pad_set_property;
215 
216   g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
217       g_param_spec_int64 ("running-time", "Running time",
218           "Running time of stream on pad", 0, G_MAXINT64, 0,
219           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
220   g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
221       g_param_spec_boxed ("tags", "Tags",
222           "The currently active tags on the pad", GST_TYPE_TAG_LIST,
223           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
224   g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
225       g_param_spec_boolean ("active", "Active",
226           "If the pad is currently active", FALSE,
227           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
228   /* FIXME: better property name? */
229   g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
230       g_param_spec_boolean ("always-ok", "Always OK",
231           "Make an inactive pad return OK instead of NOT_LINKED",
232           DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 }
234 
235 static void
gst_selector_pad_init(GstSelectorPad * pad)236 gst_selector_pad_init (GstSelectorPad * pad)
237 {
238   pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
239   gst_selector_pad_reset (pad);
240 }
241 
242 static void
gst_selector_pad_finalize(GObject * object)243 gst_selector_pad_finalize (GObject * object)
244 {
245   GstSelectorPad *pad;
246 
247   pad = GST_SELECTOR_PAD_CAST (object);
248 
249   if (pad->tags)
250     gst_tag_list_unref (pad->tags);
251   gst_selector_pad_free_cached_buffers (pad);
252 
253   G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
254 }
255 
256 static void
gst_selector_pad_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)257 gst_selector_pad_set_property (GObject * object, guint prop_id,
258     const GValue * value, GParamSpec * pspec)
259 {
260   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
261 
262   switch (prop_id) {
263     case PROP_PAD_ALWAYS_OK:
264       GST_OBJECT_LOCK (object);
265       spad->always_ok = g_value_get_boolean (value);
266       GST_OBJECT_UNLOCK (object);
267       break;
268     default:
269       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270       break;
271   }
272 }
273 
274 static void
gst_selector_pad_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)275 gst_selector_pad_get_property (GObject * object, guint prop_id,
276     GValue * value, GParamSpec * pspec)
277 {
278   GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
279 
280   switch (prop_id) {
281     case PROP_PAD_RUNNING_TIME:
282       g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
283       break;
284     case PROP_PAD_TAGS:
285       GST_OBJECT_LOCK (object);
286       g_value_set_boxed (value, spad->tags);
287       GST_OBJECT_UNLOCK (object);
288       break;
289     case PROP_PAD_ACTIVE:
290     {
291       GstInputSelector *sel;
292 
293       sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
294       if (sel) {
295         g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
296                 GST_PAD_CAST (spad)));
297         gst_object_unref (sel);
298       } else {
299         g_value_set_boolean (value, FALSE);
300       }
301       break;
302     }
303     case PROP_PAD_ALWAYS_OK:
304       GST_OBJECT_LOCK (object);
305       g_value_set_boolean (value, spad->always_ok);
306       GST_OBJECT_UNLOCK (object);
307       break;
308     default:
309       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310       break;
311   }
312 }
313 
314 static gint64
gst_selector_pad_get_running_time(GstSelectorPad * pad)315 gst_selector_pad_get_running_time (GstSelectorPad * pad)
316 {
317   gint64 ret = 0;
318 
319   GST_OBJECT_LOCK (pad);
320   if (pad->segment.format == GST_FORMAT_TIME) {
321     ret =
322         gst_segment_to_running_time (&pad->segment, pad->segment.format,
323         pad->segment.position);
324   }
325   GST_OBJECT_UNLOCK (pad);
326 
327   GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
328       " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
329 
330   return ret;
331 }
332 
333 /* must be called with the SELECTOR_LOCK */
334 static void
gst_selector_pad_reset(GstSelectorPad * pad)335 gst_selector_pad_reset (GstSelectorPad * pad)
336 {
337   GST_OBJECT_LOCK (pad);
338   pad->pushed = FALSE;
339   pad->group_done = FALSE;
340   pad->eos = FALSE;
341   pad->eos_sent = FALSE;
342   pad->events_pending = FALSE;
343   pad->discont = FALSE;
344   pad->flushing = FALSE;
345   gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
346   pad->sending_cached_buffers = FALSE;
347   gst_selector_pad_free_cached_buffers (pad);
348   GST_OBJECT_UNLOCK (pad);
349 }
350 
351 static GstSelectorPadCachedBuffer *
gst_selector_pad_new_cached_buffer(GstSelectorPad * selpad,GstBuffer * buffer)352 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
353 {
354   GstSelectorPadCachedBuffer *cached_buffer =
355       g_slice_new (GstSelectorPadCachedBuffer);
356   cached_buffer->buffer = buffer;
357   cached_buffer->segment = selpad->segment;
358   return cached_buffer;
359 }
360 
361 static void
gst_selector_pad_free_cached_buffer(GstSelectorPadCachedBuffer * cached_buffer)362 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
363 {
364   if (cached_buffer->buffer)
365     gst_buffer_unref (cached_buffer->buffer);
366   g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
367 }
368 
369 /* must be called with the SELECTOR_LOCK */
370 static void
gst_selector_pad_cache_buffer(GstSelectorPad * selpad,GstBuffer * buffer)371 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
372 {
373   if (selpad->segment.format != GST_FORMAT_TIME) {
374     GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
375         "not caching", buffer);
376     gst_buffer_unref (buffer);
377     return;
378   }
379 
380   GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
381   if (!selpad->cached_buffers)
382     selpad->cached_buffers = g_queue_new ();
383   g_queue_push_tail (selpad->cached_buffers,
384       gst_selector_pad_new_cached_buffer (selpad, buffer));
385 }
386 
387 /* must be called with the SELECTOR_LOCK */
388 static void
gst_selector_pad_free_cached_buffers(GstSelectorPad * selpad)389 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
390 {
391   if (!selpad->cached_buffers)
392     return;
393 
394   GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
395   g_queue_free_full (selpad->cached_buffers,
396       (GDestroyNotify) gst_selector_pad_free_cached_buffer);
397   selpad->cached_buffers = NULL;
398 }
399 
400 /* strictly get the linked pad from the sinkpad. If the pad is active we return
401  * the srcpad else we return NULL */
402 static GstIterator *
gst_selector_pad_iterate_linked_pads(GstPad * pad,GstObject * parent)403 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
404 {
405   GstInputSelector *sel;
406   GstPad *otherpad;
407   GstIterator *it = NULL;
408   GValue val = { 0, };
409 
410   sel = GST_INPUT_SELECTOR (parent);
411 
412   otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
413   if (otherpad) {
414     g_value_init (&val, GST_TYPE_PAD);
415     g_value_set_object (&val, otherpad);
416     it = gst_iterator_new_single (GST_TYPE_PAD, &val);
417     g_value_unset (&val);
418     gst_object_unref (otherpad);
419   }
420 
421   return it;
422 }
423 
424 static gboolean
forward_sticky_events(GstPad * sinkpad,GstEvent ** event,gpointer user_data)425 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
426 {
427   GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
428 
429   GST_DEBUG_OBJECT (sinkpad, "forward sticky event %" GST_PTR_FORMAT, *event);
430 
431   if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
432     GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
433     GstEvent *e;
434 
435     e = gst_event_new_segment (seg);
436     gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
437 
438     gst_pad_push_event (sel->srcpad, e);
439   } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
440       && !sel->have_group_id) {
441     GstEvent *tmp =
442         gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
443 
444     /* Only push stream-start once if not all our streams have a stream-id */
445     if (!tmp) {
446       gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
447     } else {
448       gst_event_unref (tmp);
449     }
450   } else {
451     gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
452   }
453 
454   return TRUE;
455 }
456 
457 static gboolean
gst_input_selector_eos_wait(GstInputSelector * self,GstSelectorPad * pad,GstEvent * eos_event)458 gst_input_selector_eos_wait (GstInputSelector * self, GstSelectorPad * pad,
459     GstEvent * eos_event)
460 {
461   while (!self->eos && !self->flushing && !pad->flushing) {
462     GstPad *active_sinkpad;
463     active_sinkpad = gst_input_selector_get_active_sinkpad (self);
464     if (pad == GST_SELECTOR_PAD_CAST (active_sinkpad) && pad->eos
465         && !pad->eos_sent) {
466       GST_DEBUG_OBJECT (pad, "send EOS event");
467       GST_INPUT_SELECTOR_UNLOCK (self);
468       /* if we have a pending events, push them now */
469       if (pad->events_pending) {
470         gst_pad_sticky_events_foreach (GST_PAD_CAST (pad),
471             forward_sticky_events, self);
472         pad->events_pending = FALSE;
473       }
474 
475       gst_pad_push_event (self->srcpad, gst_event_ref (eos_event));
476       GST_INPUT_SELECTOR_LOCK (self);
477       /* Wake up other pads so they can continue when syncing to
478        * running time, as this pad just switched to EOS and
479        * may enable others to progress */
480       GST_INPUT_SELECTOR_BROADCAST (self);
481       pad->eos_sent = TRUE;
482     } else {
483       /* we can be unlocked here when we are shutting down (flushing) or when we
484        * get unblocked */
485       GST_INPUT_SELECTOR_WAIT (self);
486     }
487   }
488 
489   return self->flushing;
490 }
491 
492 static gboolean
gst_input_selector_all_eos(GstInputSelector * sel)493 gst_input_selector_all_eos (GstInputSelector * sel)
494 {
495   GList *walk;
496 
497   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = walk->next) {
498     GstSelectorPad *selpad;
499 
500     selpad = GST_SELECTOR_PAD_CAST (walk->data);
501     if (!selpad->eos) {
502       return FALSE;
503     }
504 
505   }
506 
507   return TRUE;
508 }
509 
510 static gboolean
gst_selector_pad_event(GstPad * pad,GstObject * parent,GstEvent * event)511 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
512 {
513   gboolean res = TRUE;
514   gboolean forward;
515   gboolean new_tags = FALSE;
516   GstInputSelector *sel;
517   GstSelectorPad *selpad;
518   GstPad *prev_active_sinkpad;
519   GstPad *active_sinkpad;
520 
521   sel = GST_INPUT_SELECTOR (parent);
522   selpad = GST_SELECTOR_PAD_CAST (pad);
523   GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
524 
525   GST_INPUT_SELECTOR_LOCK (sel);
526   prev_active_sinkpad =
527       sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
528   active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
529   gst_object_ref (active_sinkpad);
530   GST_INPUT_SELECTOR_UNLOCK (sel);
531 
532   if (prev_active_sinkpad != active_sinkpad) {
533     if (prev_active_sinkpad)
534       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
535     g_object_notify (G_OBJECT (active_sinkpad), "active");
536     g_object_notify (G_OBJECT (sel), "active-pad");
537   }
538   if (prev_active_sinkpad)
539     gst_object_unref (prev_active_sinkpad);
540   gst_object_unref (active_sinkpad);
541 
542   GST_INPUT_SELECTOR_LOCK (sel);
543   active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
544 
545   /* only forward if we are dealing with the active sinkpad */
546   forward = (pad == active_sinkpad);
547 
548   switch (GST_EVENT_TYPE (event)) {
549     case GST_EVENT_STREAM_START:{
550       if (!gst_event_parse_group_id (event, &selpad->group_id)) {
551         sel->have_group_id = FALSE;
552         selpad->group_id = 0;
553       }
554       break;
555     }
556     case GST_EVENT_FLUSH_START:
557       /* Unblock the pad if it's waiting */
558       selpad->flushing = TRUE;
559       sel->eos = FALSE;
560       selpad->group_done = FALSE;
561       GST_INPUT_SELECTOR_BROADCAST (sel);
562       break;
563     case GST_EVENT_FLUSH_STOP:
564       gst_selector_pad_reset (selpad);
565       break;
566     case GST_EVENT_SEGMENT:
567     {
568       gst_event_copy_segment (event, &selpad->segment);
569       selpad->segment_seqnum = gst_event_get_seqnum (event);
570 
571       GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
572           &selpad->segment);
573       break;
574     }
575     case GST_EVENT_TAG:
576     {
577       GstTagList *tags, *oldtags, *newtags;
578 
579       gst_event_parse_tag (event, &tags);
580 
581       GST_OBJECT_LOCK (selpad);
582       oldtags = selpad->tags;
583 
584       newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
585       selpad->tags = newtags;
586       GST_OBJECT_UNLOCK (selpad);
587 
588       if (oldtags)
589         gst_tag_list_unref (oldtags);
590       GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
591 
592       new_tags = TRUE;
593       break;
594     }
595     case GST_EVENT_EOS:
596       selpad->eos = TRUE;
597       GST_DEBUG_OBJECT (pad, "received EOS");
598       if (gst_input_selector_all_eos (sel)) {
599         GST_DEBUG_OBJECT (pad, "All sink pad received EOS");
600         sel->eos = TRUE;
601         GST_INPUT_SELECTOR_BROADCAST (sel);
602       } else {
603         gst_input_selector_eos_wait (sel, selpad, event);
604         forward = FALSE;
605       }
606       break;
607     case GST_EVENT_GAP:{
608       GstClockTime ts, dur;
609 
610       GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
611 
612       gst_event_parse_gap (event, &ts, &dur);
613       if (GST_CLOCK_TIME_IS_VALID (ts)) {
614         if (GST_CLOCK_TIME_IS_VALID (dur))
615           ts += dur;
616 
617         /* update the segment position */
618         GST_OBJECT_LOCK (pad);
619         selpad->segment.position = ts;
620         GST_OBJECT_UNLOCK (pad);
621         if (sel->sync_streams && active_sinkpad == pad)
622           GST_INPUT_SELECTOR_BROADCAST (sel);
623       }
624 
625     }
626       break;
627     case GST_EVENT_STREAM_GROUP_DONE:{
628       GST_DEBUG_OBJECT (sel, "Stream group-done in inputselector pad %s",
629           GST_OBJECT_NAME (selpad));
630       gst_event_parse_stream_group_done (event, &selpad->group_id);
631       selpad->group_done = TRUE;
632       if (sel->sync_streams && active_sinkpad == pad)
633         GST_INPUT_SELECTOR_BROADCAST (sel);
634       break;
635     }
636     default:
637       break;
638   }
639   GST_INPUT_SELECTOR_UNLOCK (sel);
640   if (new_tags)
641     g_object_notify (G_OBJECT (selpad), "tags");
642   if (forward) {
643     GST_DEBUG_OBJECT (pad, "forwarding event");
644     res = gst_pad_push_event (sel->srcpad, event);
645   } else {
646     /* If we aren't forwarding the event because the pad is not the
647      * active_sinkpad, then set the flag on the pad
648      * that says a segment needs sending if/when that pad is activated.
649      * For all other cases, we send the event immediately, which makes
650      * sparse streams and other segment updates work correctly downstream.
651      */
652     if (GST_EVENT_IS_STICKY (event))
653       selpad->events_pending = TRUE;
654     gst_event_unref (event);
655   }
656 
657   return res;
658 }
659 
660 static gboolean
gst_selector_pad_query(GstPad * pad,GstObject * parent,GstQuery * query)661 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
662 {
663   gboolean res = FALSE;
664   GstInputSelector *self = (GstInputSelector *) parent;
665 
666   switch (GST_QUERY_TYPE (query)) {
667     case GST_QUERY_CAPS:
668     case GST_QUERY_POSITION:
669     case GST_QUERY_DURATION:
670     case GST_QUERY_CONTEXT:
671       /* always proxy caps/position/duration/context queries, regardless of active pad or not
672        * See https://bugzilla.gnome.org/show_bug.cgi?id=775445 */
673       res = gst_pad_peer_query (self->srcpad, query);
674       break;
675     case GST_QUERY_ALLOCATION:{
676       GstPad *active_sinkpad;
677       GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
678 
679       /* Only do the allocation query for the active sinkpad,
680        * after switching a reconfigure event is sent and upstream
681        * should reconfigure and do a new allocation query
682        */
683       if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
684         GST_INPUT_SELECTOR_LOCK (sel);
685         active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
686         GST_INPUT_SELECTOR_UNLOCK (sel);
687 
688         if (pad != active_sinkpad) {
689           res = FALSE;
690           goto done;
691         }
692       }
693     }
694       /* fall through */
695     default:
696       res = gst_pad_query_default (pad, parent, query);
697       break;
698   }
699 
700 done:
701   return res;
702 }
703 
704 static GstClockTime
gst_input_selector_get_clipped_running_time(GstSegment * seg,GstBuffer * buf)705 gst_input_selector_get_clipped_running_time (GstSegment * seg, GstBuffer * buf)
706 {
707   GstClockTime running_time;
708 
709   running_time = GST_BUFFER_PTS (buf);
710   /* If possible try to get the running time at the end of the buffer */
711   if (GST_BUFFER_DURATION_IS_VALID (buf))
712     running_time += GST_BUFFER_DURATION (buf);
713   /* Only use the segment to convert to running time if the segment is
714    * in TIME format, otherwise do our best to try to sync */
715   if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
716     if (running_time > seg->stop) {
717       running_time = seg->stop;
718     }
719   }
720   return gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
721 }
722 
723 /* must be called without the SELECTOR_LOCK, will wait until the running time
724  * of the active pad is after this pad or return TRUE when flushing */
725 static gboolean
gst_input_selector_wait_running_time(GstInputSelector * sel,GstSelectorPad * selpad,GstBuffer * buf)726 gst_input_selector_wait_running_time (GstInputSelector * sel,
727     GstSelectorPad * selpad, GstBuffer * buf)
728 {
729   GstSegment *seg;
730 
731   GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
732 
733   /* If we have no valid timestamp we can't sync this buffer */
734   if (!GST_BUFFER_PTS_IS_VALID (buf)) {
735     GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
736         "invalid timestamp");
737     return FALSE;
738   }
739 
740   seg = &selpad->segment;
741 
742   /* Wait until
743    *   a) this is the active pad
744    *   b) the pad or the selector is flushing
745    *   c) the buffer running time is before the current running time
746    *      (either active-seg or clock, depending on sync-mode)
747    */
748 
749   GST_INPUT_SELECTOR_LOCK (sel);
750   while (TRUE) {
751     GstPad *active_sinkpad;
752     GstSelectorPad *active_selpad;
753     GstClock *clock;
754     gint64 cur_running_time;
755     GstClockTime running_time;
756 
757     active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
758     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
759 
760     if (seg->format != GST_FORMAT_TIME) {
761       GST_DEBUG_OBJECT (selpad,
762           "Not waiting because we don't have a TIME segment");
763       GST_INPUT_SELECTOR_UNLOCK (sel);
764       return FALSE;
765     }
766 
767     running_time = gst_input_selector_get_clipped_running_time (seg, buf);
768     /* If this is outside the segment don't sync */
769     if (running_time == -1) {
770       GST_DEBUG_OBJECT (selpad,
771           "Not waiting because buffer is outside segment");
772       GST_INPUT_SELECTOR_UNLOCK (sel);
773       return FALSE;
774     }
775 
776     cur_running_time = GST_CLOCK_TIME_NONE;
777     if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
778       clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
779       if (clock) {
780         GstClockTime base_time;
781 
782         cur_running_time = gst_clock_get_time (clock);
783         base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
784         if (base_time <= cur_running_time)
785           cur_running_time -= base_time;
786         else
787           cur_running_time = 0;
788 
789         gst_object_unref (clock);
790       }
791     } else {
792       GstSegment *active_seg;
793 
794       active_seg = &active_selpad->segment;
795 
796       /* If the active segment is configured but not to time format
797        * we can't do any syncing at all */
798       if ((active_seg->format != GST_FORMAT_TIME
799               && active_seg->format != GST_FORMAT_UNDEFINED)) {
800         GST_DEBUG_OBJECT (selpad,
801             "Not waiting because active segment isn't in TIME format");
802         GST_INPUT_SELECTOR_UNLOCK (sel);
803         return FALSE;
804       }
805 
806       /* Get active pad's running time, if no configured segment yet keep at -1 */
807       if (active_seg->format == GST_FORMAT_TIME)
808         cur_running_time = gst_segment_to_running_time (active_seg,
809             GST_FORMAT_TIME, active_seg->position);
810     }
811 
812     /* Don't wait if the group is finished on the active pad,
813      * as the running time won't progress now */
814     if (selpad != active_selpad && active_selpad->group_done &&
815         selpad->group_id == active_selpad->group_id) {
816       GST_DEBUG_OBJECT (selpad, "Active pad received group-done. Unblocking");
817       GST_INPUT_SELECTOR_UNLOCK (sel);
818       break;
819     }
820 
821     if (selpad != active_selpad && !sel->eos && !sel->flushing
822         && !selpad->flushing && (cur_running_time == GST_CLOCK_TIME_NONE
823             || running_time >= cur_running_time)) {
824       GST_DEBUG_OBJECT (selpad,
825           "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
826           GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
827           GST_TIME_ARGS (cur_running_time));
828       GST_INPUT_SELECTOR_WAIT (sel);
829     } else {
830       GST_INPUT_SELECTOR_UNLOCK (sel);
831       break;
832     }
833   }
834 
835   /* Return TRUE if the selector or the pad is flushing */
836   return (sel->flushing || selpad->flushing);
837 }
838 
839 #if DEBUG_CACHED_BUFFERS
840 static void
gst_input_selector_debug_cached_buffers(GstInputSelector * sel)841 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
842 {
843   GList *walk;
844 
845   if (gst_debug_category_get_threshold (input_selector_debug) < GST_LEVEL_DEBUG)
846     return;
847 
848   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = walk->next) {
849     GstSelectorPad *selpad;
850     GString *timestamps;
851     GList *l;
852 
853     selpad = GST_SELECTOR_PAD_CAST (walk->data);
854     if (!selpad->cached_buffers) {
855       GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
856       continue;
857     }
858 
859     timestamps = g_string_new ("Cached buffers timestamps:");
860     for (l = selpad->cached_buffers->head; l != NULL; l = l->next) {
861       GstSelectorPadCachedBuffer *cached_buffer = l->data;
862 
863       g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
864           GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
865     }
866     GST_DEBUG_OBJECT (selpad, "%s", timestamps->str);
867     g_string_free (timestamps, TRUE);
868   }
869 }
870 #endif
871 
872 /* must be called with the SELECTOR_LOCK */
873 static void
gst_input_selector_cleanup_old_cached_buffers(GstInputSelector * sel,GstPad * pad)874 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
875     GstPad * pad)
876 {
877   GstClock *clock;
878   gint64 cur_running_time;
879   GList *walk;
880 
881   cur_running_time = GST_CLOCK_TIME_NONE;
882   if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
883     clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
884     if (clock) {
885       GstClockTime base_time;
886 
887       cur_running_time = gst_clock_get_time (clock);
888       base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
889       if (base_time <= cur_running_time)
890         cur_running_time -= base_time;
891       else
892         cur_running_time = 0;
893 
894       gst_object_unref (clock);
895     }
896   } else {
897     GstPad *active_sinkpad;
898     GstSelectorPad *active_selpad;
899     GstSegment *active_seg;
900 
901     active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
902     active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
903     active_seg = &active_selpad->segment;
904 
905     /* Get active pad's running time, if no configured segment yet keep at -1 */
906     if (active_seg->format == GST_FORMAT_TIME)
907       cur_running_time = gst_segment_to_running_time (active_seg,
908           GST_FORMAT_TIME, active_seg->position);
909   }
910 
911   if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
912     return;
913 
914   GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
915   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
916     GstSelectorPad *selpad;
917     GstSelectorPadCachedBuffer *cached_buffer;
918     GSList *maybe_remove;
919     guint queue_position;
920 
921     selpad = GST_SELECTOR_PAD_CAST (walk->data);
922     if (!selpad->cached_buffers)
923       continue;
924 
925     maybe_remove = NULL;
926     queue_position = 0;
927     while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
928                 queue_position))) {
929       GstBuffer *buffer = cached_buffer->buffer;
930       GstSegment *seg = &cached_buffer->segment;
931       GstClockTime running_time;
932       GSList *l;
933 
934       /* If we have no valid timestamp we can't sync this buffer */
935       if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
936         maybe_remove = g_slist_append (maybe_remove, cached_buffer);
937         queue_position = g_slist_length (maybe_remove);
938         continue;
939       }
940 
941       /* the buffer is still valid if its duration is valid and the
942        * timestamp + duration is >= time, or if its duration is invalid
943        * and the timestamp is >= time */
944       running_time = gst_input_selector_get_clipped_running_time (seg, buffer);
945       GST_DEBUG_OBJECT (selpad,
946           "checking if buffer %p running time=%" GST_TIME_FORMAT
947           " >= stream time=%" GST_TIME_FORMAT, buffer,
948           GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
949       if (running_time >= cur_running_time) {
950         break;
951       }
952 
953       GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
954       g_queue_pop_nth (selpad->cached_buffers, queue_position);
955       gst_selector_pad_free_cached_buffer (cached_buffer);
956 
957       for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
958         /* A buffer after some invalid buffers was removed, it means the invalid buffers
959          * are old, lets also remove them */
960         cached_buffer = l->data;
961         g_queue_remove (selpad->cached_buffers, cached_buffer);
962         gst_selector_pad_free_cached_buffer (cached_buffer);
963       }
964 
965       g_slist_free (maybe_remove);
966       maybe_remove = NULL;
967       queue_position = 0;
968     }
969 
970     g_slist_free (maybe_remove);
971     maybe_remove = NULL;
972 
973     if (g_queue_is_empty (selpad->cached_buffers)) {
974       g_queue_free (selpad->cached_buffers);
975       selpad->cached_buffers = NULL;
976     }
977   }
978 
979 #if DEBUG_CACHED_BUFFERS
980   gst_input_selector_debug_cached_buffers (sel);
981 #endif
982 }
983 
984 static GstFlowReturn
gst_selector_pad_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)985 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
986 {
987   GstInputSelector *sel;
988   GstFlowReturn res;
989   GstPad *active_sinkpad;
990   GstPad *prev_active_sinkpad = NULL;
991   GstSelectorPad *selpad;
992 
993   sel = GST_INPUT_SELECTOR (parent);
994   selpad = GST_SELECTOR_PAD_CAST (pad);
995 
996   GST_DEBUG_OBJECT (selpad,
997       "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
998       GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
999 
1000   GST_INPUT_SELECTOR_LOCK (sel);
1001 
1002   if (sel->flushing) {
1003     GST_INPUT_SELECTOR_UNLOCK (sel);
1004     goto flushing;
1005   }
1006 
1007   GST_LOG_OBJECT (pad, "getting active pad");
1008 
1009   prev_active_sinkpad =
1010       sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
1011   active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1012 
1013   /* In sync mode wait until the active pad has advanced
1014    * after the running time of the current buffer */
1015   if (sel->sync_streams) {
1016     /* call chain for each cached buffer if we are not the active pad
1017      * or if we are the active pad but didn't push anything yet. */
1018     if (active_sinkpad != pad || !selpad->pushed) {
1019       /* no need to check for sel->cache_buffers as selpad->cached_buffers
1020        * will only be valid if cache_buffers is TRUE */
1021       if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
1022         GstSelectorPadCachedBuffer *cached_buffer;
1023         GstSegment saved_segment;
1024 
1025         saved_segment = selpad->segment;
1026 
1027         selpad->sending_cached_buffers = TRUE;
1028         while (!sel->eos && !sel->flushing && !selpad->flushing &&
1029             (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
1030           GST_DEBUG_OBJECT (pad, "Cached buffers found, "
1031               "invoking chain for cached buffer %p", cached_buffer->buffer);
1032 
1033           selpad->segment = cached_buffer->segment;
1034           selpad->events_pending = TRUE;
1035           GST_INPUT_SELECTOR_UNLOCK (sel);
1036           gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
1037           GST_INPUT_SELECTOR_LOCK (sel);
1038 
1039           /* We just passed the ownership of the buffer to the chain function */
1040           cached_buffer->buffer = NULL;
1041           gst_selector_pad_free_cached_buffer (cached_buffer);
1042 
1043           /* we may have cleaned up the queue in the meantime because of
1044            * old buffers */
1045           if (!selpad->cached_buffers) {
1046             break;
1047           }
1048         }
1049         selpad->sending_cached_buffers = FALSE;
1050 
1051         /* all cached buffers sent, restore segment for current buffer */
1052         selpad->segment = saved_segment;
1053         selpad->events_pending = TRUE;
1054 
1055         /* Might have changed while calling chain for cached buffers */
1056         active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1057       }
1058     }
1059 
1060     if (active_sinkpad != pad) {
1061       GST_INPUT_SELECTOR_UNLOCK (sel);
1062       if (gst_input_selector_wait_running_time (sel, selpad, buf))
1063         goto flushing;
1064       GST_INPUT_SELECTOR_LOCK (sel);
1065     }
1066 
1067     /* Might have changed while waiting */
1068     active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1069   }
1070 
1071   /* update the segment on the srcpad */
1072   if (GST_BUFFER_PTS_IS_VALID (buf)) {
1073     GstClockTime start_time = GST_BUFFER_PTS (buf);
1074 
1075     GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1076         GST_TIME_ARGS (start_time));
1077     if (GST_BUFFER_DURATION_IS_VALID (buf))
1078       GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1079           GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1080 
1081     GST_OBJECT_LOCK (pad);
1082     selpad->segment.position = start_time;
1083     GST_OBJECT_UNLOCK (pad);
1084   }
1085 
1086   /* Ignore buffers from pads except the selected one */
1087   if (pad != active_sinkpad)
1088     goto ignore;
1089 
1090   /* Tell all non-active pads that we advanced the running time */
1091   if (sel->sync_streams)
1092     GST_INPUT_SELECTOR_BROADCAST (sel);
1093 
1094   GST_INPUT_SELECTOR_UNLOCK (sel);
1095 
1096   if (prev_active_sinkpad != active_sinkpad) {
1097     if (prev_active_sinkpad)
1098       g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1099     g_object_notify (G_OBJECT (active_sinkpad), "active");
1100     g_object_notify (G_OBJECT (sel), "active-pad");
1101   }
1102 
1103   /* if we have a pending events, push them now */
1104   if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1105           || selpad->events_pending)) {
1106     gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1107         sel);
1108     selpad->events_pending = FALSE;
1109   }
1110 
1111   if (prev_active_sinkpad) {
1112     gst_object_unref (prev_active_sinkpad);
1113     prev_active_sinkpad = NULL;
1114   }
1115 
1116   if (selpad->discont) {
1117     buf = gst_buffer_make_writable (buf);
1118 
1119     GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1120     GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1121     selpad->discont = FALSE;
1122   }
1123 
1124   /* forward */
1125   GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1126       buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1127 
1128   /* Only make the buffer read-only when necessary */
1129   if (sel->sync_streams && sel->cache_buffers)
1130     buf = gst_buffer_ref (buf);
1131   res = gst_pad_push (sel->srcpad, buf);
1132   GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1133 
1134   GST_INPUT_SELECTOR_LOCK (sel);
1135 
1136   if (sel->sync_streams && sel->cache_buffers) {
1137     /* Might have changed while pushing */
1138     active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1139     /* only set pad to pushed if we are still the active pad */
1140     if (active_sinkpad == pad)
1141       selpad->pushed = TRUE;
1142 
1143     /* cache buffer as we may need it again if we change pads */
1144     gst_selector_pad_cache_buffer (selpad, buf);
1145     gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1146   } else {
1147     selpad->pushed = TRUE;
1148   }
1149   GST_INPUT_SELECTOR_UNLOCK (sel);
1150 
1151 done:
1152 
1153   if (prev_active_sinkpad)
1154     gst_object_unref (prev_active_sinkpad);
1155   prev_active_sinkpad = NULL;
1156 
1157   return res;
1158 
1159   /* dropped buffers */
1160 ignore:
1161   {
1162     gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1163 
1164     GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1165     /* when we drop a buffer, we're creating a discont on this pad */
1166     selpad->discont = TRUE;
1167     GST_INPUT_SELECTOR_UNLOCK (sel);
1168     gst_buffer_unref (buf);
1169 
1170     /* figure out what to return upstream */
1171     GST_OBJECT_LOCK (selpad);
1172     if (selpad->always_ok || !active_pad_pushed)
1173       res = GST_FLOW_OK;
1174     else
1175       res = GST_FLOW_NOT_LINKED;
1176     GST_OBJECT_UNLOCK (selpad);
1177 
1178     goto done;
1179   }
1180 flushing:
1181   {
1182     GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1183     gst_buffer_unref (buf);
1184     res = GST_FLOW_FLUSHING;
1185     goto done;
1186   }
1187 }
1188 
1189 static void gst_input_selector_dispose (GObject * object);
1190 static void gst_input_selector_finalize (GObject * object);
1191 
1192 static void gst_input_selector_set_property (GObject * object,
1193     guint prop_id, const GValue * value, GParamSpec * pspec);
1194 static void gst_input_selector_get_property (GObject * object,
1195     guint prop_id, GValue * value, GParamSpec * pspec);
1196 
1197 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1198     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1199 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1200 
1201 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1202     element, GstStateChange transition);
1203 
1204 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1205     GstEvent * event);
1206 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1207     GstQuery * query);
1208 
1209 #define _do_init \
1210     GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1211         "input-selector", 0, "An input stream selector element");
1212 #define gst_input_selector_parent_class parent_class
1213 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1214     _do_init);
1215 GST_ELEMENT_REGISTER_DEFINE (input_selector, "input-selector", GST_RANK_NONE,
1216     GST_TYPE_INPUT_SELECTOR);
1217 
1218 static void
gst_input_selector_class_init(GstInputSelectorClass * klass)1219 gst_input_selector_class_init (GstInputSelectorClass * klass)
1220 {
1221   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1222   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1223 
1224   gobject_class->dispose = gst_input_selector_dispose;
1225   gobject_class->finalize = gst_input_selector_finalize;
1226 
1227   gobject_class->set_property = gst_input_selector_set_property;
1228   gobject_class->get_property = gst_input_selector_get_property;
1229 
1230   g_object_class_install_property (gobject_class, PROP_N_PADS,
1231       g_param_spec_uint ("n-pads", "Number of Pads",
1232           "The number of sink pads", 0, G_MAXUINT, 0,
1233           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1234 
1235   g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1236       g_param_spec_object ("active-pad", "Active pad",
1237           "The currently active sink pad", GST_TYPE_PAD,
1238           G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
1239           G_PARAM_STATIC_STRINGS));
1240 
1241   /**
1242    * GstInputSelector:sync-streams
1243    *
1244    * If set to %TRUE all inactive streams will be synced to the
1245    * running time of the active stream or to the current clock.
1246    *
1247    * To make sure no buffers are dropped by input-selector
1248    * that might be needed when switching the active pad,
1249    * sync-mode should be set to "clock" and cache-buffers to TRUE.
1250    */
1251   g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1252       g_param_spec_boolean ("sync-streams", "Sync Streams",
1253           "Synchronize inactive streams to the running time of the active "
1254           "stream or to the current clock",
1255           DEFAULT_SYNC_STREAMS,
1256           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1257           GST_PARAM_MUTABLE_READY));
1258 
1259   /**
1260    * GstInputSelector:sync-mode
1261    *
1262    * Select how input-selector will sync buffers when in sync-streams mode.
1263    *
1264    * Note that when using the "active-segment" mode, the "active-segment" may
1265    * be ahead of current clock time when switching the active pad, as the current
1266    * active pad may have pushed more buffers than what was displayed/consumed,
1267    * which may cause delays and some missing buffers.
1268    */
1269   g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1270       g_param_spec_enum ("sync-mode", "Sync mode",
1271           "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1272           DEFAULT_SYNC_MODE,
1273           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1274           GST_PARAM_MUTABLE_READY));
1275 
1276   /**
1277    * GstInputSelector:cache-buffers
1278    *
1279    * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1280    * the active pad will cache the buffers still considered valid (after current
1281    * running time, see sync-mode) to avoid missing frames if/when the pad is
1282    * reactivated.
1283    *
1284    * The active pad may push more buffers than what is currently displayed/consumed
1285    * and when changing pads those buffers will be discarded and the only way to
1286    * reactivate that pad without losing the already consumed buffers is to enable cache.
1287    */
1288   g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1289       g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1290           "Cache buffers for active-pad",
1291           DEFAULT_CACHE_BUFFERS,
1292           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1293           GST_PARAM_MUTABLE_READY));
1294 
1295   gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1296       "Generic", "N-to-1 input stream selector",
1297       "Julien Moutte <julien@moutte.net>, "
1298       "Jan Schmidt <thaytan@mad.scientist.com>, "
1299       "Wim Taymans <wim.taymans@gmail.com>");
1300   gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
1301       &gst_input_selector_sink_factory, GST_TYPE_SELECTOR_PAD);
1302   gst_element_class_add_static_pad_template (gstelement_class,
1303       &gst_input_selector_src_factory);
1304 
1305   gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1306   gstelement_class->release_pad = gst_input_selector_release_pad;
1307   gstelement_class->change_state = gst_input_selector_change_state;
1308 
1309   gst_type_mark_as_plugin_api (GST_TYPE_SELECTOR_PAD, 0);
1310   gst_type_mark_as_plugin_api (GST_TYPE_INPUT_SELECTOR_SYNC_MODE, 0);
1311 }
1312 
1313 static void
gst_input_selector_init(GstInputSelector * sel)1314 gst_input_selector_init (GstInputSelector * sel)
1315 {
1316   sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1317   gst_pad_set_iterate_internal_links_function (sel->srcpad,
1318       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1319   gst_pad_set_event_function (sel->srcpad,
1320       GST_DEBUG_FUNCPTR (gst_input_selector_event));
1321   gst_pad_set_query_function (sel->srcpad,
1322       GST_DEBUG_FUNCPTR (gst_input_selector_query));
1323   GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1324   gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1325   /* sinkpad management */
1326   sel->active_sinkpad = NULL;
1327   sel->padcount = 0;
1328   sel->sync_streams = DEFAULT_SYNC_STREAMS;
1329   sel->sync_mode = DEFAULT_SYNC_MODE;
1330   sel->have_group_id = TRUE;
1331 
1332   g_mutex_init (&sel->lock);
1333   g_cond_init (&sel->cond);
1334   sel->eos = FALSE;
1335 
1336   /* lets give a change for downstream to do something on
1337    * active-pad change before we start pushing new buffers */
1338   g_signal_connect_data (sel, "notify::active-pad",
1339       (GCallback) gst_input_selector_active_pad_changed, NULL,
1340       NULL, G_CONNECT_AFTER);
1341 }
1342 
1343 static void
gst_input_selector_dispose(GObject * object)1344 gst_input_selector_dispose (GObject * object)
1345 {
1346   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1347 
1348   if (sel->active_sinkpad) {
1349     gst_object_unref (sel->active_sinkpad);
1350     sel->active_sinkpad = NULL;
1351   }
1352   G_OBJECT_CLASS (parent_class)->dispose (object);
1353 }
1354 
1355 static void
gst_input_selector_finalize(GObject * object)1356 gst_input_selector_finalize (GObject * object)
1357 {
1358   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1359 
1360   g_mutex_clear (&sel->lock);
1361   g_cond_clear (&sel->cond);
1362 
1363   G_OBJECT_CLASS (parent_class)->finalize (object);
1364 }
1365 
1366 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1367  * active pad changed. */
1368 static gboolean
gst_input_selector_set_active_pad(GstInputSelector * self,GstPad * pad)1369 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1370 {
1371   GstSelectorPad *old, *new;
1372   GstPad **active_pad_p;
1373 
1374   if (pad == self->active_sinkpad)
1375     return FALSE;
1376 
1377   /* guard against users setting a src pad or foreign pad as active pad */
1378   if (pad != NULL) {
1379     g_return_val_if_fail (GST_PAD_IS_SINK (pad), FALSE);
1380     g_return_val_if_fail (GST_IS_SELECTOR_PAD (pad), FALSE);
1381     g_return_val_if_fail (GST_PAD_PARENT (pad) == GST_ELEMENT_CAST (self),
1382         FALSE);
1383   }
1384 
1385   old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1386   new = GST_SELECTOR_PAD_CAST (pad);
1387 
1388   GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1389       GST_DEBUG_PAD_NAME (new));
1390 
1391   if (old)
1392     old->pushed = FALSE;
1393   if (new)
1394     new->pushed = FALSE;
1395 
1396   /* Send a new SEGMENT event on the new pad next */
1397   if (old != new && new)
1398     new->events_pending = TRUE;
1399 
1400   active_pad_p = &self->active_sinkpad;
1401   gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1402 
1403   if (old && old != new)
1404     gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1405   if (new)
1406     gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1407 
1408   GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1409       self->active_sinkpad);
1410 
1411   if (old != new && new && new->eos) {
1412     new->eos_sent = FALSE;
1413     GST_INPUT_SELECTOR_BROADCAST (self);
1414   }
1415 
1416   return TRUE;
1417 }
1418 
1419 static void
gst_input_selector_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1420 gst_input_selector_set_property (GObject * object, guint prop_id,
1421     const GValue * value, GParamSpec * pspec)
1422 {
1423   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1424 
1425   switch (prop_id) {
1426     case PROP_ACTIVE_PAD:
1427     {
1428       GstPad *pad;
1429 
1430       pad = g_value_get_object (value);
1431 
1432       GST_INPUT_SELECTOR_LOCK (sel);
1433 
1434       sel->active_sinkpad_from_user = ! !pad;
1435 #if DEBUG_CACHED_BUFFERS
1436       gst_input_selector_debug_cached_buffers (sel);
1437 #endif
1438 
1439       gst_input_selector_set_active_pad (sel, pad);
1440 
1441 #if DEBUG_CACHED_BUFFERS
1442       gst_input_selector_debug_cached_buffers (sel);
1443 #endif
1444 
1445       GST_INPUT_SELECTOR_UNLOCK (sel);
1446       break;
1447     }
1448     case PROP_SYNC_STREAMS:
1449       GST_INPUT_SELECTOR_LOCK (sel);
1450       sel->sync_streams = g_value_get_boolean (value);
1451       GST_INPUT_SELECTOR_UNLOCK (sel);
1452       break;
1453     case PROP_SYNC_MODE:
1454       GST_INPUT_SELECTOR_LOCK (sel);
1455       sel->sync_mode = g_value_get_enum (value);
1456       GST_INPUT_SELECTOR_UNLOCK (sel);
1457       break;
1458     case PROP_CACHE_BUFFERS:
1459       GST_INPUT_SELECTOR_LOCK (object);
1460       sel->cache_buffers = g_value_get_boolean (value);
1461       GST_INPUT_SELECTOR_UNLOCK (object);
1462       break;
1463     default:
1464       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1465       break;
1466   }
1467 }
1468 
1469 static void
gst_input_selector_active_pad_changed(GstInputSelector * sel,GParamSpec * pspec,gpointer user_data)1470 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1471     GParamSpec * pspec, gpointer user_data)
1472 {
1473   /* Wake up all non-active pads in sync mode, they might be
1474    * the active pad now */
1475   if (sel->sync_streams)
1476     GST_INPUT_SELECTOR_BROADCAST (sel);
1477 }
1478 
1479 static void
gst_input_selector_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1480 gst_input_selector_get_property (GObject * object, guint prop_id,
1481     GValue * value, GParamSpec * pspec)
1482 {
1483   GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1484 
1485   switch (prop_id) {
1486     case PROP_N_PADS:
1487       GST_INPUT_SELECTOR_LOCK (object);
1488       g_value_set_uint (value, sel->n_pads);
1489       GST_INPUT_SELECTOR_UNLOCK (object);
1490       break;
1491     case PROP_ACTIVE_PAD:
1492       GST_INPUT_SELECTOR_LOCK (object);
1493       g_value_set_object (value, sel->active_sinkpad);
1494       GST_INPUT_SELECTOR_UNLOCK (object);
1495       break;
1496     case PROP_SYNC_STREAMS:
1497       GST_INPUT_SELECTOR_LOCK (object);
1498       g_value_set_boolean (value, sel->sync_streams);
1499       GST_INPUT_SELECTOR_UNLOCK (object);
1500       break;
1501     case PROP_SYNC_MODE:
1502       GST_INPUT_SELECTOR_LOCK (object);
1503       g_value_set_enum (value, sel->sync_mode);
1504       GST_INPUT_SELECTOR_UNLOCK (object);
1505       break;
1506     case PROP_CACHE_BUFFERS:
1507       GST_INPUT_SELECTOR_LOCK (object);
1508       g_value_set_boolean (value, sel->cache_buffers);
1509       GST_INPUT_SELECTOR_UNLOCK (object);
1510       break;
1511     default:
1512       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1513       break;
1514   }
1515 }
1516 
1517 static GstPad *
gst_input_selector_get_linked_pad(GstInputSelector * sel,GstPad * pad,gboolean strict)1518 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1519     gboolean strict)
1520 {
1521   GstPad *otherpad = NULL;
1522 
1523   GST_INPUT_SELECTOR_LOCK (sel);
1524   if (pad == sel->srcpad)
1525     otherpad = sel->active_sinkpad;
1526   else if (pad == sel->active_sinkpad || !strict)
1527     otherpad = sel->srcpad;
1528   if (otherpad)
1529     gst_object_ref (otherpad);
1530   GST_INPUT_SELECTOR_UNLOCK (sel);
1531 
1532   return otherpad;
1533 }
1534 
1535 static gboolean
gst_input_selector_event(GstPad * pad,GstObject * parent,GstEvent * event)1536 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1537 {
1538   GstInputSelector *sel;
1539   gboolean result = FALSE;
1540   GstIterator *iter;
1541   gboolean done = FALSE;
1542   GValue item = { 0, };
1543   GstPad *eventpad;
1544   GList *pushed_pads = NULL;
1545 
1546   sel = GST_INPUT_SELECTOR (parent);
1547   /* Send upstream events to all sinkpads */
1548   iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1549 
1550   GST_INPUT_SELECTOR_LOCK (sel);
1551   if (sel->active_sinkpad) {
1552     eventpad = gst_object_ref (sel->active_sinkpad);
1553     GST_INPUT_SELECTOR_UNLOCK (sel);
1554 
1555     gst_event_ref (event);
1556     result |= gst_pad_push_event (eventpad, event);
1557     pushed_pads = g_list_append (pushed_pads, eventpad);
1558     gst_object_unref (eventpad);
1559   } else {
1560     GST_INPUT_SELECTOR_UNLOCK (sel);
1561   }
1562 
1563   /* This is now essentially a copy of gst_pad_event_default_dispatch
1564    * with a different iterator */
1565   while (!done) {
1566     switch (gst_iterator_next (iter, &item)) {
1567       case GST_ITERATOR_OK:
1568         eventpad = g_value_get_object (&item);
1569 
1570         /* if already pushed,  skip */
1571         if (g_list_find (pushed_pads, eventpad)) {
1572           g_value_reset (&item);
1573           break;
1574         }
1575 
1576         gst_event_ref (event);
1577         result |= gst_pad_push_event (eventpad, event);
1578         pushed_pads = g_list_append (pushed_pads, eventpad);
1579 
1580         g_value_reset (&item);
1581         break;
1582       case GST_ITERATOR_RESYNC:
1583         /* We don't reset the result here because we don't push the event
1584          * again on pads that got the event already and because we need
1585          * to consider the result of the previous pushes */
1586         gst_iterator_resync (iter);
1587         break;
1588       case GST_ITERATOR_ERROR:
1589         GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1590         done = TRUE;
1591         break;
1592       case GST_ITERATOR_DONE:
1593         done = TRUE;
1594         break;
1595     }
1596   }
1597   g_value_unset (&item);
1598   gst_iterator_free (iter);
1599 
1600   g_list_free (pushed_pads);
1601 
1602   gst_event_unref (event);
1603 
1604   return result;
1605 }
1606 
1607 typedef struct
1608 {
1609   gboolean live;
1610   GstClockTime min, max;
1611 } LatencyFoldData;
1612 
1613 static gboolean
query_latency_default_fold(const GValue * item,GValue * ret,gpointer user_data)1614 query_latency_default_fold (const GValue * item, GValue * ret,
1615     gpointer user_data)
1616 {
1617   GstPad *pad = g_value_get_object (item), *peer;
1618   LatencyFoldData *fold_data = user_data;
1619   GstQuery *query;
1620   gboolean res = FALSE;
1621 
1622   query = gst_query_new_latency ();
1623 
1624   peer = gst_pad_get_peer (pad);
1625   if (peer) {
1626     res = gst_pad_peer_query (pad, query);
1627   } else {
1628     GST_LOG_OBJECT (pad, "No peer pad found, ignoring this pad");
1629   }
1630 
1631   if (res) {
1632     gboolean live;
1633     GstClockTime min, max;
1634 
1635     gst_query_parse_latency (query, &live, &min, &max);
1636 
1637     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1638         " max:%" G_GINT64_FORMAT, live ? "true" : "false", min, max);
1639 
1640     if (live) {
1641       if (min > fold_data->min)
1642         fold_data->min = min;
1643 
1644       if (fold_data->max == GST_CLOCK_TIME_NONE)
1645         fold_data->max = max;
1646       else if (max < fold_data->max)
1647         fold_data->max = max;
1648       fold_data->live = live;
1649     }
1650   } else if (peer) {
1651     GST_DEBUG_OBJECT (pad, "latency query failed");
1652     g_value_set_boolean (ret, FALSE);
1653   }
1654 
1655   gst_query_unref (query);
1656   if (peer)
1657     gst_object_unref (peer);
1658 
1659   return TRUE;
1660 }
1661 
1662 static gboolean
gst_input_selector_query_latency(GstInputSelector * sel,GstPad * pad,GstQuery * query)1663 gst_input_selector_query_latency (GstInputSelector * sel, GstPad * pad,
1664     GstQuery * query)
1665 {
1666   GstIterator *it;
1667   GstIteratorResult res;
1668   GValue ret = G_VALUE_INIT;
1669   gboolean query_ret;
1670   LatencyFoldData fold_data;
1671 
1672   /* This is basically gst_pad_query_latency_default() but with a different
1673    * iterator. We query all sinkpads! */
1674   it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1675   if (!it) {
1676     GST_DEBUG_OBJECT (pad, "Can't iterate internal links");
1677     return FALSE;
1678   }
1679 
1680   g_value_init (&ret, G_TYPE_BOOLEAN);
1681 
1682 retry:
1683   fold_data.live = FALSE;
1684   fold_data.min = 0;
1685   fold_data.max = GST_CLOCK_TIME_NONE;
1686 
1687   g_value_set_boolean (&ret, TRUE);
1688   res = gst_iterator_fold (it, query_latency_default_fold, &ret, &fold_data);
1689   switch (res) {
1690     case GST_ITERATOR_OK:
1691       g_assert_not_reached ();
1692       break;
1693     case GST_ITERATOR_DONE:
1694       break;
1695     case GST_ITERATOR_ERROR:
1696       g_value_set_boolean (&ret, FALSE);
1697       break;
1698     case GST_ITERATOR_RESYNC:
1699       gst_iterator_resync (it);
1700       goto retry;
1701     default:
1702       g_assert_not_reached ();
1703       break;
1704   }
1705   gst_iterator_free (it);
1706 
1707   query_ret = g_value_get_boolean (&ret);
1708   if (query_ret) {
1709     GST_LOG_OBJECT (pad, "got latency live:%s min:%" G_GINT64_FORMAT
1710         " max:%" G_GINT64_FORMAT, fold_data.live ? "true" : "false",
1711         fold_data.min, fold_data.max);
1712 
1713     if (fold_data.min > fold_data.max) {
1714       GST_ERROR_OBJECT (pad, "minimum latency bigger than maximum latency");
1715     }
1716 
1717     gst_query_set_latency (query, fold_data.live, fold_data.min, fold_data.max);
1718   } else {
1719     GST_LOG_OBJECT (pad, "latency query failed");
1720   }
1721 
1722   return query_ret;
1723 }
1724 
1725 static gboolean
gst_input_selector_query(GstPad * pad,GstObject * parent,GstQuery * query)1726 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1727 {
1728   GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
1729 
1730   switch (GST_QUERY_TYPE (query)) {
1731     case GST_QUERY_LATENCY:
1732       /* Query all sink pads for the latency, not just the active one */
1733       return gst_input_selector_query_latency (sel, pad, query);
1734     default:
1735       return gst_pad_query_default (pad, parent, query);
1736   }
1737 }
1738 
1739 /* check if the pad is the active sinkpad */
1740 static inline gboolean
gst_input_selector_is_active_sinkpad(GstInputSelector * sel,GstPad * pad)1741 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1742 {
1743   gboolean res;
1744 
1745   GST_INPUT_SELECTOR_LOCK (sel);
1746   res = (pad == sel->active_sinkpad);
1747   GST_INPUT_SELECTOR_UNLOCK (sel);
1748 
1749   return res;
1750 }
1751 
1752 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1753 static GstPad *
gst_input_selector_get_active_sinkpad(GstInputSelector * sel)1754 gst_input_selector_get_active_sinkpad (GstInputSelector * sel)
1755 {
1756   GstPad *active_sinkpad;
1757 
1758   active_sinkpad = sel->active_sinkpad;
1759   if (active_sinkpad == NULL) {
1760     GValue item = G_VALUE_INIT;
1761     GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1762     GstIteratorResult ires;
1763 
1764     while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1765       gst_iterator_resync (iter);
1766     if (ires == GST_ITERATOR_OK) {
1767       /* If no pad is currently selected, we return the first usable pad to
1768        * guarantee consistency */
1769 
1770       active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1771       g_value_reset (&item);
1772       GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1773           GST_DEBUG_PAD_NAME (active_sinkpad));
1774     } else
1775       GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1776     gst_iterator_free (iter);
1777   }
1778 
1779   return active_sinkpad;
1780 }
1781 
1782 static GstPad *
gst_input_selector_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * unused,const GstCaps * caps)1783 gst_input_selector_request_new_pad (GstElement * element,
1784     GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1785 {
1786   GstInputSelector *sel;
1787   gchar *name = NULL;
1788   GstPad *sinkpad = NULL;
1789 
1790   g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1791 
1792   sel = GST_INPUT_SELECTOR (element);
1793 
1794   GST_INPUT_SELECTOR_LOCK (sel);
1795 
1796   GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1797   name = g_strdup_printf ("sink_%u", sel->padcount++);
1798   sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1799       "name", name, "direction", templ->direction, "template", templ, NULL);
1800   g_free (name);
1801 
1802   sel->n_pads++;
1803 
1804   gst_pad_set_event_function (sinkpad,
1805       GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1806   gst_pad_set_query_function (sinkpad,
1807       GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1808   gst_pad_set_chain_function (sinkpad,
1809       GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1810   gst_pad_set_iterate_internal_links_function (sinkpad,
1811       GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1812 
1813   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1814   GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1815   gst_pad_set_active (sinkpad, TRUE);
1816   GST_INPUT_SELECTOR_UNLOCK (sel);
1817   gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1818 
1819   return sinkpad;
1820 }
1821 
1822 static void
gst_input_selector_release_pad(GstElement * element,GstPad * pad)1823 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1824 {
1825   GstSelectorPad *selpad;
1826   GstInputSelector *sel;
1827 
1828   sel = GST_INPUT_SELECTOR (element);
1829   selpad = GST_SELECTOR_PAD (pad);
1830   GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1831 
1832   GST_INPUT_SELECTOR_LOCK (sel);
1833   /* if the pad was the active pad, makes us select a new one */
1834   if (sel->active_sinkpad == pad) {
1835     GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1836     gst_object_unref (sel->active_sinkpad);
1837     sel->active_sinkpad = NULL;
1838     sel->active_sinkpad_from_user = FALSE;
1839   }
1840 
1841   /* wake up the pad if it's currently waiting for EOS or a running time to be
1842    * reached. Otherwise we'll deadlock on the streaming thread further below
1843    * when deactivating the pad. */
1844   selpad->flushing = TRUE;
1845   GST_INPUT_SELECTOR_BROADCAST (sel);
1846 
1847   sel->n_pads--;
1848   GST_INPUT_SELECTOR_UNLOCK (sel);
1849 
1850   gst_pad_set_active (pad, FALSE);
1851   gst_element_remove_pad (GST_ELEMENT (sel), pad);
1852 }
1853 
1854 static void
gst_input_selector_reset(GstInputSelector * sel)1855 gst_input_selector_reset (GstInputSelector * sel)
1856 {
1857   GList *walk;
1858 
1859   GST_INPUT_SELECTOR_LOCK (sel);
1860   /* clear active pad */
1861   if (sel->active_sinkpad && !sel->active_sinkpad_from_user) {
1862     gst_object_unref (sel->active_sinkpad);
1863     sel->active_sinkpad = NULL;
1864   }
1865   sel->eos_sent = FALSE;
1866 
1867   /* reset each of our sinkpads state */
1868   for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1869     GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1870 
1871     gst_selector_pad_reset (selpad);
1872 
1873     if (selpad->tags) {
1874       gst_tag_list_unref (selpad->tags);
1875       selpad->tags = NULL;
1876     }
1877   }
1878   sel->have_group_id = TRUE;
1879   GST_INPUT_SELECTOR_UNLOCK (sel);
1880 }
1881 
1882 static GstStateChangeReturn
gst_input_selector_change_state(GstElement * element,GstStateChange transition)1883 gst_input_selector_change_state (GstElement * element,
1884     GstStateChange transition)
1885 {
1886   GstInputSelector *self = GST_INPUT_SELECTOR (element);
1887   GstStateChangeReturn result;
1888 
1889   switch (transition) {
1890     case GST_STATE_CHANGE_READY_TO_PAUSED:
1891       GST_INPUT_SELECTOR_LOCK (self);
1892       self->eos = FALSE;
1893       self->flushing = FALSE;
1894       GST_INPUT_SELECTOR_UNLOCK (self);
1895       break;
1896     case GST_STATE_CHANGE_PAUSED_TO_READY:
1897       /* first unlock before we call the parent state change function, which
1898        * tries to acquire the stream lock when going to ready. */
1899       GST_INPUT_SELECTOR_LOCK (self);
1900       self->eos = TRUE;
1901       self->flushing = TRUE;
1902       GST_INPUT_SELECTOR_BROADCAST (self);
1903       GST_INPUT_SELECTOR_UNLOCK (self);
1904       break;
1905     default:
1906       break;
1907   }
1908 
1909   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1910 
1911   switch (transition) {
1912     case GST_STATE_CHANGE_PAUSED_TO_READY:
1913       gst_input_selector_reset (self);
1914       break;
1915     default:
1916       break;
1917   }
1918 
1919   return result;
1920 }
1921