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