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>
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.
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.
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.
27 * SECTION:element-input-selector
28 * @see_also: #GstOutputSelector
30 * Direct one out of N input streams to the output pad.
32 * The input pads are from a GstPad subclass and have additional
33 * properties, which users may find useful, namely:
37 * "running-time": Running time of stream on pad (#gint64)
40 * "tags": The currently active tags on the pad (#GstTagList, boxed type)
43 * "active": If the pad is currently active (#gboolean)
46 * "always-ok" : Make an inactive pads return #GST_FLOW_OK instead of
47 * #GST_FLOW_NOT_LINKED
58 #include "gstinputselector.h"
60 #define DEBUG_CACHED_BUFFERS 0
62 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
63 #define GST_CAT_DEFAULT input_selector_debug
65 #define GST_TYPE_INPUT_SELECTOR_SYNC_MODE (gst_input_selector_sync_mode_get_type())
67 gst_input_selector_sync_mode_get_type (void)
69 static GType type = 0;
70 static const GEnumValue data[] = {
71 {GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT,
72 "Sync using the current active segment",
74 {GST_INPUT_SELECTOR_SYNC_MODE_CLOCK, "Sync using the clock", "clock"},
79 type = g_enum_register_static ("GstInputSelectorSyncMode", data);
84 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (&((GstInputSelector*)(sel))->lock)
85 #define GST_INPUT_SELECTOR_GET_COND(sel) (&((GstInputSelector*)(sel))->cond)
86 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
87 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
88 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
89 GST_INPUT_SELECTOR_GET_LOCK(sel)))
90 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
92 static GstStaticPadTemplate gst_input_selector_sink_factory =
93 GST_STATIC_PAD_TEMPLATE ("sink_%u",
98 static GstStaticPadTemplate gst_input_selector_src_factory =
99 GST_STATIC_PAD_TEMPLATE ("src",
102 GST_STATIC_CAPS_ANY);
114 #define DEFAULT_SYNC_STREAMS TRUE
115 #define DEFAULT_SYNC_MODE GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT
116 #define DEFAULT_CACHE_BUFFERS FALSE
117 #define DEFAULT_PAD_ALWAYS_OK TRUE
122 PROP_PAD_RUNNING_TIME,
135 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
137 static void gst_input_selector_active_pad_changed (GstInputSelector * sel,
138 GParamSpec * pspec, gpointer user_data);
139 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
141 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
143 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
144 GstPad * pad, gboolean strict);
146 #define GST_TYPE_SELECTOR_PAD \
147 (gst_selector_pad_get_type())
148 #define GST_SELECTOR_PAD(obj) \
149 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
150 #define GST_SELECTOR_PAD_CLASS(klass) \
151 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
152 #define GST_IS_SELECTOR_PAD(obj) \
153 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
154 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
155 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
156 #define GST_SELECTOR_PAD_CAST(obj) \
157 ((GstSelectorPad *)(obj))
159 typedef struct _GstSelectorPad GstSelectorPad;
160 typedef struct _GstSelectorPadClass GstSelectorPadClass;
161 typedef struct _GstSelectorPadCachedBuffer GstSelectorPadCachedBuffer;
163 struct _GstSelectorPad
167 gboolean active; /* when buffer have passed the pad */
168 gboolean pushed; /* when buffer was pushed downstream since activation */
169 gboolean eos; /* when EOS has been received */
170 gboolean eos_sent; /* when EOS was sent downstream */
171 gboolean discont; /* after switching we create a discont */
172 gboolean flushing; /* set after flush-start and before flush-stop */
174 GstTagList *tags; /* last tags received on the pad */
176 GstSegment segment; /* the current segment on the pad */
177 guint32 segment_seqnum; /* sequence number of the current segment */
179 gboolean events_pending; /* TRUE if sticky events need to be updated */
181 gboolean sending_cached_buffers;
182 GQueue *cached_buffers;
185 struct _GstSelectorPadCachedBuffer
191 struct _GstSelectorPadClass
196 GType gst_selector_pad_get_type (void);
197 static void gst_selector_pad_finalize (GObject * object);
198 static void gst_selector_pad_get_property (GObject * object,
199 guint prop_id, GValue * value, GParamSpec * pspec);
200 static void gst_selector_pad_set_property (GObject * object,
201 guint prop_id, const GValue * value, GParamSpec * pspec);
203 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
204 static void gst_selector_pad_reset (GstSelectorPad * pad);
205 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
207 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
209 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
211 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
213 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
215 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
217 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
220 gst_selector_pad_class_init (GstSelectorPadClass * klass)
222 GObjectClass *gobject_class;
224 gobject_class = (GObjectClass *) klass;
226 gobject_class->finalize = gst_selector_pad_finalize;
228 gobject_class->get_property = gst_selector_pad_get_property;
229 gobject_class->set_property = gst_selector_pad_set_property;
231 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
232 g_param_spec_int64 ("running-time", "Running time",
233 "Running time of stream on pad", 0, G_MAXINT64, 0,
234 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
236 g_param_spec_boxed ("tags", "Tags",
237 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
238 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
240 g_param_spec_boolean ("active", "Active",
241 "If the pad is currently active", FALSE,
242 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
243 /* FIXME: better property name? */
244 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
245 g_param_spec_boolean ("always-ok", "Always OK",
246 "Make an inactive pad return OK instead of NOT_LINKED",
247 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 gst_selector_pad_init (GstSelectorPad * pad)
253 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
254 gst_selector_pad_reset (pad);
258 gst_selector_pad_finalize (GObject * object)
262 pad = GST_SELECTOR_PAD_CAST (object);
265 gst_tag_list_unref (pad->tags);
266 gst_selector_pad_free_cached_buffers (pad);
268 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
272 gst_selector_pad_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec)
275 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
278 case PROP_PAD_ALWAYS_OK:
279 GST_OBJECT_LOCK (object);
280 spad->always_ok = g_value_get_boolean (value);
281 GST_OBJECT_UNLOCK (object);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gst_selector_pad_get_property (GObject * object, guint prop_id,
291 GValue * value, GParamSpec * pspec)
293 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
296 case PROP_PAD_RUNNING_TIME:
297 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
300 GST_OBJECT_LOCK (object);
301 g_value_set_boxed (value, spad->tags);
302 GST_OBJECT_UNLOCK (object);
304 case PROP_PAD_ACTIVE:
306 GstInputSelector *sel;
308 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
310 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
311 GST_PAD_CAST (spad)));
312 gst_object_unref (sel);
314 g_value_set_boolean (value, FALSE);
318 case PROP_PAD_ALWAYS_OK:
319 GST_OBJECT_LOCK (object);
320 g_value_set_boolean (value, spad->always_ok);
321 GST_OBJECT_UNLOCK (object);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 gst_selector_pad_get_running_time (GstSelectorPad * pad)
334 GST_OBJECT_LOCK (pad);
336 GstFormat format = pad->segment.format;
339 gst_segment_to_running_time (&pad->segment, format,
340 pad->segment.position);
342 GST_OBJECT_UNLOCK (pad);
344 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
345 " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
350 /* must be called with the SELECTOR_LOCK */
352 gst_selector_pad_reset (GstSelectorPad * pad)
354 GST_OBJECT_LOCK (pad);
358 pad->eos_sent = FALSE;
359 pad->events_pending = FALSE;
360 pad->discont = FALSE;
361 pad->flushing = FALSE;
362 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
363 pad->sending_cached_buffers = FALSE;
364 gst_selector_pad_free_cached_buffers (pad);
365 GST_OBJECT_UNLOCK (pad);
368 static GstSelectorPadCachedBuffer *
369 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
371 GstSelectorPadCachedBuffer *cached_buffer =
372 g_slice_new (GstSelectorPadCachedBuffer);
373 cached_buffer->buffer = buffer;
374 cached_buffer->segment = selpad->segment;
375 return cached_buffer;
379 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
381 gst_buffer_unref (cached_buffer->buffer);
382 g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
385 /* must be called with the SELECTOR_LOCK */
387 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
389 if (selpad->segment.format != GST_FORMAT_TIME) {
390 GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
391 "not caching", buffer);
395 GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
396 if (!selpad->cached_buffers)
397 selpad->cached_buffers = g_queue_new ();
398 g_queue_push_tail (selpad->cached_buffers,
399 gst_selector_pad_new_cached_buffer (selpad, buffer));
402 /* must be called with the SELECTOR_LOCK */
404 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
406 GstSelectorPadCachedBuffer *cached_buffer;
408 if (!selpad->cached_buffers)
411 GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
412 while ((cached_buffer = g_queue_pop_head (selpad->cached_buffers)))
413 gst_selector_pad_free_cached_buffer (cached_buffer);
414 g_queue_free (selpad->cached_buffers);
415 selpad->cached_buffers = NULL;
418 /* strictly get the linked pad from the sinkpad. If the pad is active we return
419 * the srcpad else we return NULL */
421 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
423 GstInputSelector *sel;
425 GstIterator *it = NULL;
428 sel = GST_INPUT_SELECTOR (parent);
430 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
432 g_value_init (&val, GST_TYPE_PAD);
433 g_value_set_object (&val, otherpad);
434 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
435 g_value_unset (&val);
436 gst_object_unref (otherpad);
443 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
447 gboolean new_tags = FALSE;
448 GstInputSelector *sel;
449 GstSelectorPad *selpad;
450 GstPad *prev_active_sinkpad;
451 GstPad *active_sinkpad;
453 sel = GST_INPUT_SELECTOR (parent);
454 selpad = GST_SELECTOR_PAD_CAST (pad);
455 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
457 GST_INPUT_SELECTOR_LOCK (sel);
458 prev_active_sinkpad =
459 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
460 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
461 GST_INPUT_SELECTOR_UNLOCK (sel);
463 if (prev_active_sinkpad != active_sinkpad) {
464 if (prev_active_sinkpad)
465 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
466 g_object_notify (G_OBJECT (active_sinkpad), "active");
467 g_object_notify (G_OBJECT (sel), "active-pad");
469 if (prev_active_sinkpad)
470 gst_object_unref (prev_active_sinkpad);
472 GST_INPUT_SELECTOR_LOCK (sel);
473 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
475 /* only forward if we are dealing with the active sinkpad */
476 forward = (pad == active_sinkpad);
478 switch (GST_EVENT_TYPE (event)) {
479 case GST_EVENT_STREAM_START:{
482 if (!gst_event_parse_group_id (event, &group_id))
483 sel->have_group_id = FALSE;
486 case GST_EVENT_FLUSH_START:
487 /* Unblock the pad if it's waiting */
488 selpad->flushing = TRUE;
489 GST_INPUT_SELECTOR_BROADCAST (sel);
491 case GST_EVENT_FLUSH_STOP:
492 gst_selector_pad_reset (selpad);
494 case GST_EVENT_SEGMENT:
496 gst_event_copy_segment (event, &selpad->segment);
497 selpad->segment_seqnum = gst_event_get_seqnum (event);
499 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
505 GstTagList *tags, *oldtags, *newtags;
507 gst_event_parse_tag (event, &tags);
509 oldtags = selpad->tags;
511 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
512 selpad->tags = newtags;
514 gst_tag_list_unref (oldtags);
515 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
524 selpad->eos_sent = TRUE;
526 GstSelectorPad *active_selpad;
528 /* If the active sinkpad is in EOS state but EOS
529 * was not sent downstream this means that the pad
530 * got EOS before it was set as active pad and that
531 * the previously active pad got EOS after it was
534 active_selpad = GST_SELECTOR_PAD (active_sinkpad);
535 forward = (active_selpad->eos && !active_selpad->eos_sent);
536 active_selpad->eos_sent = TRUE;
538 GST_DEBUG_OBJECT (pad, "received EOS");
541 GstClockTime ts, dur;
543 GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
545 gst_event_parse_gap (event, &ts, &dur);
546 if (GST_CLOCK_TIME_IS_VALID (ts)) {
547 if (GST_CLOCK_TIME_IS_VALID (dur))
550 /* update the segment position */
551 GST_OBJECT_LOCK (pad);
552 selpad->segment.position = ts;
553 GST_OBJECT_UNLOCK (pad);
554 if (sel->sync_streams && active_sinkpad == pad)
555 GST_INPUT_SELECTOR_BROADCAST (sel);
563 GST_INPUT_SELECTOR_UNLOCK (sel);
565 g_object_notify (G_OBJECT (selpad), "tags");
567 GST_DEBUG_OBJECT (pad, "forwarding event");
568 res = gst_pad_push_event (sel->srcpad, event);
570 /* If we aren't forwarding the event because the pad is not the
571 * active_sinkpad, then set the flag on the pad
572 * that says a segment needs sending if/when that pad is activated.
573 * For all other cases, we send the event immediately, which makes
574 * sparse streams and other segment updates work correctly downstream.
576 if (GST_EVENT_IS_STICKY (event))
577 selpad->events_pending = TRUE;
578 gst_event_unref (event);
585 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
587 gboolean res = FALSE;
589 switch (GST_QUERY_TYPE (query)) {
590 case GST_QUERY_ALLOCATION:{
591 GstPad *active_sinkpad;
592 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
594 /* Only do the allocation query for the active sinkpad,
595 * after switching a reconfigure event is sent and upstream
596 * should reconfigure and do a new allocation query
598 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
599 GST_INPUT_SELECTOR_LOCK (sel);
600 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
601 GST_INPUT_SELECTOR_UNLOCK (sel);
603 if (pad != active_sinkpad) {
611 res = gst_pad_query_default (pad, parent, query);
619 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
620 * or return TRUE when flushing */
622 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
624 while (self->blocked && !self->flushing && !pad->flushing) {
625 /* we can be unlocked here when we are shutting down (flushing) or when we
627 GST_INPUT_SELECTOR_WAIT (self);
629 return self->flushing;
632 /* must be called without the SELECTOR_LOCK, will wait until the running time
633 * of the active pad is after this pad or return TRUE when flushing */
635 gst_input_selector_wait_running_time (GstInputSelector * sel,
636 GstSelectorPad * selpad, GstBuffer * buf)
640 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
642 /* If we have no valid timestamp we can't sync this buffer */
643 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
644 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
645 "invalid timestamp");
649 seg = &selpad->segment;
652 * a) this is the active pad
653 * b) the pad or the selector is flushing
654 * c) the selector is not blocked
655 * d) the buffer running time is before the current running time
656 * (either active-seg or clock, depending on sync-mode)
659 GST_INPUT_SELECTOR_LOCK (sel);
661 GstPad *active_sinkpad;
662 GstSelectorPad *active_selpad;
664 gint64 cur_running_time;
665 GstClockTime running_time;
668 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
669 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
671 if (seg->format != GST_FORMAT_TIME) {
672 GST_DEBUG_OBJECT (selpad,
673 "Not waiting because we don't have a TIME segment");
674 GST_INPUT_SELECTOR_UNLOCK (sel);
678 running_time = GST_BUFFER_TIMESTAMP (buf);
679 /* If possible try to get the running time at the end of the buffer */
680 if (GST_BUFFER_DURATION_IS_VALID (buf))
681 running_time += GST_BUFFER_DURATION (buf);
682 /* Only use the segment to convert to running time if the segment is
683 * in TIME format, otherwise do our best to try to sync */
684 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
685 if (running_time > seg->stop) {
686 running_time = seg->stop;
690 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
691 /* If this is outside the segment don't sync */
692 if (running_time == -1) {
693 GST_DEBUG_OBJECT (selpad,
694 "Not waiting because buffer is outside segment");
695 GST_INPUT_SELECTOR_UNLOCK (sel);
699 cur_running_time = GST_CLOCK_TIME_NONE;
700 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
701 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
703 GstClockTime base_time;
705 cur_running_time = gst_clock_get_time (clock);
706 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
707 if (base_time <= cur_running_time)
708 cur_running_time -= base_time;
710 cur_running_time = 0;
712 gst_object_unref (clock);
715 GstSegment *active_seg;
717 active_seg = &active_selpad->segment;
719 /* If the active segment is configured but not to time format
720 * we can't do any syncing at all */
721 if (active_seg->format != GST_FORMAT_TIME
722 && active_seg->format != GST_FORMAT_UNDEFINED) {
723 GST_DEBUG_OBJECT (selpad,
724 "Not waiting because active segment isn't in TIME format");
725 GST_INPUT_SELECTOR_UNLOCK (sel);
729 /* Get active pad's running time, if no configured segment yet keep at -1 */
730 if (active_seg->format == GST_FORMAT_TIME)
731 cur_running_time = gst_segment_to_running_time (active_seg,
732 GST_FORMAT_TIME, active_seg->position);
735 if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
736 (sel->blocked || cur_running_time == -1
737 || running_time >= cur_running_time)) {
739 GST_DEBUG_OBJECT (selpad,
740 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
741 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
742 GST_TIME_ARGS (cur_running_time));
744 GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
746 GST_INPUT_SELECTOR_WAIT (sel);
748 GST_INPUT_SELECTOR_UNLOCK (sel);
753 /* Return TRUE if the selector or the pad is flushing */
754 return (sel->flushing || selpad->flushing);
758 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
760 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
762 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
763 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
766 e = gst_event_new_segment (seg);
767 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
769 gst_pad_push_event (sel->srcpad, e);
770 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
771 && !sel->have_group_id) {
773 gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
775 /* Only push stream-start once if not all our streams have a stream-id */
777 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
779 gst_event_unref (tmp);
782 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
788 #if DEBUG_CACHED_BUFFERS
790 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
794 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
795 GstSelectorPad *selpad;
800 selpad = GST_SELECTOR_PAD_CAST (walk->data);
801 if (!selpad->cached_buffers) {
802 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
806 timestamps = g_string_new ("Cached buffers timestamps:");
807 for (i = 0; i < selpad->cached_buffers->length; ++i) {
808 GstSelectorPadCachedBuffer *cached_buffer;
810 cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
811 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
812 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (cached_buffer->buffer)));
814 str = g_string_free (timestamps, FALSE);
815 GST_DEBUG_OBJECT (selpad, "%s", str);
821 /* must be called with the SELECTOR_LOCK */
823 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
827 gint64 cur_running_time;
830 cur_running_time = GST_CLOCK_TIME_NONE;
831 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
832 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
834 GstClockTime base_time;
836 cur_running_time = gst_clock_get_time (clock);
837 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
838 if (base_time <= cur_running_time)
839 cur_running_time -= base_time;
841 cur_running_time = 0;
843 gst_object_unref (clock);
846 GstPad *active_sinkpad;
847 GstSelectorPad *active_selpad;
848 GstSegment *active_seg;
850 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
851 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
852 active_seg = &active_selpad->segment;
854 /* Get active pad's running time, if no configured segment yet keep at -1 */
855 if (active_seg->format == GST_FORMAT_TIME)
856 cur_running_time = gst_segment_to_running_time (active_seg,
857 GST_FORMAT_TIME, active_seg->position);
860 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
863 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
864 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
865 GstSelectorPad *selpad;
867 GstSelectorPadCachedBuffer *cached_buffer;
868 GSList *maybe_remove;
869 guint queue_position;
871 selpad = GST_SELECTOR_PAD_CAST (walk->data);
872 if (!selpad->cached_buffers)
875 seg = &selpad->segment;
879 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
881 GstBuffer *buffer = cached_buffer->buffer;
882 GstClockTime running_time;
885 /* If we have no valid timestamp we can't sync this buffer */
886 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
887 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
888 queue_position = g_slist_length (maybe_remove);
892 /* the buffer is still valid if its duration is valid and the
893 * timestamp + duration is >= time, or if its duration is invalid
894 * and the timestamp is >= time */
895 running_time = GST_BUFFER_TIMESTAMP (buffer);
896 /* If possible try to get the running time at the end of the buffer */
897 if (GST_BUFFER_DURATION_IS_VALID (buffer))
898 running_time += GST_BUFFER_DURATION (buffer);
899 /* Only use the segment to convert to running time if the segment is
900 * in TIME format, otherwise do our best to try to sync */
901 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
902 if (running_time > seg->stop) {
903 running_time = seg->stop;
907 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
909 GST_DEBUG_OBJECT (selpad,
910 "checking if buffer %p running time=%" GST_TIME_FORMAT
911 " >= stream time=%" GST_TIME_FORMAT, buffer,
912 GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
913 if (running_time >= cur_running_time) {
917 GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
918 g_queue_pop_nth (selpad->cached_buffers, queue_position);
919 gst_selector_pad_free_cached_buffer (cached_buffer);
921 for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
922 /* A buffer after some invalid buffers was removed, it means the invalid buffers
923 * are old, lets also remove them */
924 cached_buffer = l->data;
925 g_queue_remove (selpad->cached_buffers, cached_buffer);
926 gst_selector_pad_free_cached_buffer (cached_buffer);
929 g_slist_free (maybe_remove);
934 g_slist_free (maybe_remove);
937 if (g_queue_is_empty (selpad->cached_buffers)) {
938 g_queue_free (selpad->cached_buffers);
939 selpad->cached_buffers = NULL;
943 #if DEBUG_CACHED_BUFFERS
944 gst_input_selector_debug_cached_buffers (sel);
949 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
951 GstInputSelector *sel;
953 GstPad *active_sinkpad;
954 GstPad *prev_active_sinkpad = NULL;
955 GstSelectorPad *selpad;
956 GstClockTime start_time;
958 sel = GST_INPUT_SELECTOR (parent);
959 selpad = GST_SELECTOR_PAD_CAST (pad);
961 GST_DEBUG_OBJECT (selpad,
962 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
963 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
965 GST_INPUT_SELECTOR_LOCK (sel);
966 /* wait or check for flushing */
967 if (gst_input_selector_wait (sel, selpad)) {
968 GST_INPUT_SELECTOR_UNLOCK (sel);
972 GST_LOG_OBJECT (pad, "getting active pad");
974 prev_active_sinkpad =
975 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
976 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
978 /* In sync mode wait until the active pad has advanced
979 * after the running time of the current buffer */
980 if (sel->sync_streams) {
981 /* call chain for each cached buffer if we are not the active pad
982 * or if we are the active pad but didn't push anything yet. */
983 if (active_sinkpad != pad || !selpad->pushed) {
984 /* no need to check for sel->cache_buffers as selpad->cached_buffers
985 * will only be valid if cache_buffers is TRUE */
986 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
987 GstSelectorPadCachedBuffer *cached_buffer;
988 GstSegment saved_segment;
990 saved_segment = selpad->segment;
992 selpad->sending_cached_buffers = TRUE;
993 while (!sel->flushing && !selpad->flushing &&
994 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
995 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
996 "invoking chain for cached buffer %p", cached_buffer->buffer);
998 selpad->segment = cached_buffer->segment;
999 selpad->events_pending = TRUE;
1000 GST_INPUT_SELECTOR_UNLOCK (sel);
1001 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
1002 GST_INPUT_SELECTOR_LOCK (sel);
1004 /* we may have cleaned up the queue in the meantime because of
1006 if (!selpad->cached_buffers) {
1010 selpad->sending_cached_buffers = FALSE;
1012 /* all cached buffers sent, restore segment for current buffer */
1013 selpad->segment = saved_segment;
1014 selpad->events_pending = TRUE;
1016 /* Might have changed while calling chain for cached buffers */
1017 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1021 if (active_sinkpad != pad) {
1022 GST_INPUT_SELECTOR_UNLOCK (sel);
1023 if (gst_input_selector_wait_running_time (sel, selpad, buf))
1025 GST_INPUT_SELECTOR_LOCK (sel);
1028 /* Might have changed while waiting */
1029 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1032 /* update the segment on the srcpad */
1033 start_time = GST_BUFFER_TIMESTAMP (buf);
1034 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
1035 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1036 GST_TIME_ARGS (start_time));
1037 if (GST_BUFFER_DURATION_IS_VALID (buf))
1038 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1039 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1041 GST_OBJECT_LOCK (pad);
1042 selpad->segment.position = start_time;
1043 GST_OBJECT_UNLOCK (pad);
1046 /* Ignore buffers from pads except the selected one */
1047 if (pad != active_sinkpad)
1050 /* Tell all non-active pads that we advanced the running time */
1051 if (sel->sync_streams)
1052 GST_INPUT_SELECTOR_BROADCAST (sel);
1054 GST_INPUT_SELECTOR_UNLOCK (sel);
1056 if (prev_active_sinkpad != active_sinkpad) {
1057 if (prev_active_sinkpad)
1058 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1059 g_object_notify (G_OBJECT (active_sinkpad), "active");
1060 g_object_notify (G_OBJECT (sel), "active-pad");
1063 /* if we have a pending events, push them now */
1064 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1065 || selpad->events_pending)) {
1066 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1068 selpad->events_pending = FALSE;
1071 if (prev_active_sinkpad)
1072 gst_object_unref (prev_active_sinkpad);
1073 prev_active_sinkpad = NULL;
1075 if (selpad->discont) {
1076 buf = gst_buffer_make_writable (buf);
1078 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1079 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1080 selpad->discont = FALSE;
1084 GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1085 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1087 /* Only make the buffer read-only when necessary */
1088 if (sel->sync_streams && sel->cache_buffers)
1089 buf = gst_buffer_ref (buf);
1090 res = gst_pad_push (sel->srcpad, buf);
1091 GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1093 GST_INPUT_SELECTOR_LOCK (sel);
1095 if (sel->sync_streams && sel->cache_buffers) {
1096 /* Might have changed while pushing */
1097 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1098 /* only set pad to pushed if we are still the active pad */
1099 if (active_sinkpad == pad)
1100 selpad->pushed = TRUE;
1102 /* cache buffer as we may need it again if we change pads */
1103 gst_selector_pad_cache_buffer (selpad, buf);
1104 gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1106 selpad->pushed = TRUE;
1108 GST_INPUT_SELECTOR_UNLOCK (sel);
1112 if (prev_active_sinkpad)
1113 gst_object_unref (prev_active_sinkpad);
1114 prev_active_sinkpad = NULL;
1118 /* dropped buffers */
1121 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1123 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1124 /* when we drop a buffer, we're creating a discont on this pad */
1125 selpad->discont = TRUE;
1126 GST_INPUT_SELECTOR_UNLOCK (sel);
1127 gst_buffer_unref (buf);
1129 /* figure out what to return upstream */
1130 GST_OBJECT_LOCK (selpad);
1131 if (selpad->always_ok || !active_pad_pushed)
1134 res = GST_FLOW_NOT_LINKED;
1135 GST_OBJECT_UNLOCK (selpad);
1141 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1142 gst_buffer_unref (buf);
1143 res = GST_FLOW_FLUSHING;
1148 static void gst_input_selector_dispose (GObject * object);
1149 static void gst_input_selector_finalize (GObject * object);
1151 static void gst_input_selector_set_property (GObject * object,
1152 guint prop_id, const GValue * value, GParamSpec * pspec);
1153 static void gst_input_selector_get_property (GObject * object,
1154 guint prop_id, GValue * value, GParamSpec * pspec);
1156 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1157 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1158 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1160 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1161 element, GstStateChange transition);
1163 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1165 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1167 static gint64 gst_input_selector_block (GstInputSelector * self);
1170 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1171 "input-selector", 0, "An input stream selector element");
1172 #define gst_input_selector_parent_class parent_class
1173 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1177 gst_input_selector_class_init (GstInputSelectorClass * klass)
1179 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1180 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1182 gobject_class->dispose = gst_input_selector_dispose;
1183 gobject_class->finalize = gst_input_selector_finalize;
1185 gobject_class->set_property = gst_input_selector_set_property;
1186 gobject_class->get_property = gst_input_selector_get_property;
1188 g_object_class_install_property (gobject_class, PROP_N_PADS,
1189 g_param_spec_uint ("n-pads", "Number of Pads",
1190 "The number of sink pads", 0, G_MAXUINT, 0,
1191 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1193 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1194 g_param_spec_object ("active-pad", "Active pad",
1195 "The currently active sink pad", GST_TYPE_PAD,
1196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1199 * GstInputSelector:sync-streams
1201 * If set to %TRUE all inactive streams will be synced to the
1202 * running time of the active stream or to the current clock.
1204 * To make sure no buffers are dropped by input-selector
1205 * that might be needed when switching the active pad,
1206 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1208 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1209 g_param_spec_boolean ("sync-streams", "Sync Streams",
1210 "Synchronize inactive streams to the running time of the active "
1211 "stream or to the current clock",
1212 DEFAULT_SYNC_STREAMS,
1213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1214 GST_PARAM_MUTABLE_READY));
1217 * GstInputSelector:sync-mode
1219 * Select how input-selector will sync buffers when in sync-streams mode.
1221 * Note that when using the "active-segment" mode, the "active-segment" may
1222 * be ahead of current clock time when switching the active pad, as the current
1223 * active pad may have pushed more buffers than what was displayed/consumed,
1224 * which may cause delays and some missing buffers.
1226 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1227 g_param_spec_enum ("sync-mode", "Sync mode",
1228 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1230 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1231 GST_PARAM_MUTABLE_READY));
1234 * GstInputSelector:cache-buffers
1236 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1237 * the active pad will cache the buffers still considered valid (after current
1238 * running time, see sync-mode) to avoid missing frames if/when the pad is
1241 * The active pad may push more buffers than what is currently displayed/consumed
1242 * and when changing pads those buffers will be discarded and the only way to
1243 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1245 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1246 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1247 "Cache buffers for active-pad",
1248 DEFAULT_CACHE_BUFFERS,
1249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1250 GST_PARAM_MUTABLE_READY));
1253 * GstInputSelector::block:
1254 * @inputselector: the #GstInputSelector
1256 * Block all sink pads in preparation for a switch. Returns the stop time of
1257 * the current switch segment, as a running time, or 0 if there is no current
1258 * active pad or the current active pad never received data.
1260 gst_input_selector_signals[SIGNAL_BLOCK] =
1261 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1262 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1263 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1264 g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1266 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1267 "Generic", "N-to-1 input stream selector",
1268 "Julien Moutte <julien@moutte.net>, "
1269 "Jan Schmidt <thaytan@mad.scientist.com>, "
1270 "Wim Taymans <wim.taymans@gmail.com>");
1271 gst_element_class_add_pad_template (gstelement_class,
1272 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1273 gst_element_class_add_pad_template (gstelement_class,
1274 gst_static_pad_template_get (&gst_input_selector_src_factory));
1276 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1277 gstelement_class->release_pad = gst_input_selector_release_pad;
1278 gstelement_class->change_state = gst_input_selector_change_state;
1280 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1284 gst_input_selector_init (GstInputSelector * sel)
1286 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1287 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1288 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1289 gst_pad_set_query_function (sel->srcpad,
1290 GST_DEBUG_FUNCPTR (gst_input_selector_query));
1291 gst_pad_set_event_function (sel->srcpad,
1292 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1293 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1294 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1295 /* sinkpad management */
1296 sel->active_sinkpad = NULL;
1298 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1299 sel->have_group_id = TRUE;
1301 g_mutex_init (&sel->lock);
1302 g_cond_init (&sel->cond);
1303 sel->blocked = FALSE;
1305 /* lets give a change for downstream to do something on
1306 * active-pad change before we start pushing new buffers */
1307 g_signal_connect_data (sel, "notify::active-pad",
1308 (GCallback) gst_input_selector_active_pad_changed, NULL,
1309 NULL, G_CONNECT_AFTER);
1313 gst_input_selector_dispose (GObject * object)
1315 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1317 if (sel->active_sinkpad) {
1318 gst_object_unref (sel->active_sinkpad);
1319 sel->active_sinkpad = NULL;
1321 G_OBJECT_CLASS (parent_class)->dispose (object);
1325 gst_input_selector_finalize (GObject * object)
1327 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1329 g_mutex_clear (&sel->lock);
1330 g_cond_clear (&sel->cond);
1332 G_OBJECT_CLASS (parent_class)->finalize (object);
1335 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1336 * active pad changed. */
1338 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1340 GstSelectorPad *old, *new;
1341 GstPad **active_pad_p;
1343 if (pad == self->active_sinkpad)
1346 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1347 new = GST_SELECTOR_PAD_CAST (pad);
1349 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1350 GST_DEBUG_PAD_NAME (new));
1353 old->pushed = FALSE;
1355 new->pushed = FALSE;
1357 /* Send a new SEGMENT event on the new pad next */
1358 if (old != new && new)
1359 new->events_pending = TRUE;
1361 active_pad_p = &self->active_sinkpad;
1362 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1364 if (old && old != new)
1365 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1367 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1369 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1370 self->active_sinkpad);
1376 gst_input_selector_set_property (GObject * object, guint prop_id,
1377 const GValue * value, GParamSpec * pspec)
1379 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1382 case PROP_ACTIVE_PAD:
1386 pad = g_value_get_object (value);
1388 GST_INPUT_SELECTOR_LOCK (sel);
1390 #if DEBUG_CACHED_BUFFERS
1391 gst_input_selector_debug_cached_buffers (sel);
1394 gst_input_selector_set_active_pad (sel, pad);
1396 #if DEBUG_CACHED_BUFFERS
1397 gst_input_selector_debug_cached_buffers (sel);
1400 GST_INPUT_SELECTOR_UNLOCK (sel);
1403 case PROP_SYNC_STREAMS:
1404 GST_INPUT_SELECTOR_LOCK (sel);
1405 sel->sync_streams = g_value_get_boolean (value);
1406 GST_INPUT_SELECTOR_UNLOCK (sel);
1408 case PROP_SYNC_MODE:
1409 GST_INPUT_SELECTOR_LOCK (sel);
1410 sel->sync_mode = g_value_get_enum (value);
1411 GST_INPUT_SELECTOR_UNLOCK (sel);
1413 case PROP_CACHE_BUFFERS:
1414 GST_INPUT_SELECTOR_LOCK (object);
1415 sel->cache_buffers = g_value_get_boolean (value);
1416 GST_INPUT_SELECTOR_UNLOCK (object);
1419 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1425 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1426 GParamSpec * pspec, gpointer user_data)
1428 /* Wake up all non-active pads in sync mode, they might be
1429 * the active pad now */
1430 if (sel->sync_streams)
1431 GST_INPUT_SELECTOR_BROADCAST (sel);
1435 gst_input_selector_get_property (GObject * object, guint prop_id,
1436 GValue * value, GParamSpec * pspec)
1438 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1442 GST_INPUT_SELECTOR_LOCK (object);
1443 g_value_set_uint (value, sel->n_pads);
1444 GST_INPUT_SELECTOR_UNLOCK (object);
1446 case PROP_ACTIVE_PAD:
1447 GST_INPUT_SELECTOR_LOCK (object);
1448 g_value_set_object (value, sel->active_sinkpad);
1449 GST_INPUT_SELECTOR_UNLOCK (object);
1451 case PROP_SYNC_STREAMS:
1452 GST_INPUT_SELECTOR_LOCK (object);
1453 g_value_set_boolean (value, sel->sync_streams);
1454 GST_INPUT_SELECTOR_UNLOCK (object);
1456 case PROP_SYNC_MODE:
1457 GST_INPUT_SELECTOR_LOCK (object);
1458 g_value_set_enum (value, sel->sync_mode);
1459 GST_INPUT_SELECTOR_UNLOCK (object);
1461 case PROP_CACHE_BUFFERS:
1462 GST_INPUT_SELECTOR_LOCK (object);
1463 g_value_set_boolean (value, sel->cache_buffers);
1464 GST_INPUT_SELECTOR_UNLOCK (object);
1467 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1473 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1476 GstPad *otherpad = NULL;
1478 GST_INPUT_SELECTOR_LOCK (sel);
1479 if (pad == sel->srcpad)
1480 otherpad = sel->active_sinkpad;
1481 else if (pad == sel->active_sinkpad || !strict)
1482 otherpad = sel->srcpad;
1484 gst_object_ref (otherpad);
1485 GST_INPUT_SELECTOR_UNLOCK (sel);
1491 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1493 GstInputSelector *sel;
1494 gboolean result = FALSE;
1496 gboolean done = FALSE;
1497 GValue item = { 0, };
1499 GList *pushed_pads = NULL;
1501 sel = GST_INPUT_SELECTOR (parent);
1502 /* Send upstream events to all sinkpads */
1503 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1505 /* This is now essentially a copy of gst_pad_event_default_dispatch
1506 * with a different iterator */
1508 switch (gst_iterator_next (iter, &item)) {
1509 case GST_ITERATOR_OK:
1510 eventpad = g_value_get_object (&item);
1512 /* if already pushed, skip */
1513 if (g_list_find (pushed_pads, eventpad)) {
1514 g_value_reset (&item);
1518 gst_event_ref (event);
1519 result |= gst_pad_push_event (eventpad, event);
1521 g_value_reset (&item);
1523 case GST_ITERATOR_RESYNC:
1524 /* We don't reset the result here because we don't push the event
1525 * again on pads that got the event already and because we need
1526 * to consider the result of the previous pushes */
1527 gst_iterator_resync (iter);
1529 case GST_ITERATOR_ERROR:
1530 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1533 case GST_ITERATOR_DONE:
1538 g_value_unset (&item);
1539 gst_iterator_free (iter);
1541 g_list_free (pushed_pads);
1543 gst_event_unref (event);
1548 /* query on the srcpad. We override this function because by default it will
1549 * only forward the query to one random sinkpad */
1551 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1553 gboolean res = FALSE;
1554 GstInputSelector *sel;
1556 sel = GST_INPUT_SELECTOR (parent);
1558 switch (GST_QUERY_TYPE (query)) {
1559 case GST_QUERY_LATENCY:
1562 GstClockTime resmin, resmax;
1569 /* perform the query on all sinkpads and combine the results. We take the
1570 * max of min and the min of max for the result latency. */
1571 GST_INPUT_SELECTOR_LOCK (sel);
1572 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1573 walk = g_list_next (walk)) {
1574 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1576 if (gst_pad_peer_query (sinkpad, query)) {
1577 GstClockTime min, max;
1580 /* one query succeeded, we succeed too */
1583 gst_query_parse_latency (query, &live, &min, &max);
1585 GST_DEBUG_OBJECT (sinkpad,
1586 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1587 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1594 else if (max < resmax)
1596 if (reslive == FALSE)
1601 GST_INPUT_SELECTOR_UNLOCK (sel);
1603 gst_query_set_latency (query, reslive, resmin, resmax);
1605 GST_DEBUG_OBJECT (sel,
1606 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1607 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1614 res = gst_pad_query_default (pad, parent, query);
1621 /* check if the pad is the active sinkpad */
1622 static inline gboolean
1623 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1627 GST_INPUT_SELECTOR_LOCK (sel);
1628 res = (pad == sel->active_sinkpad);
1629 GST_INPUT_SELECTOR_UNLOCK (sel);
1634 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1636 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1638 GstPad *active_sinkpad;
1639 GstSelectorPad *selpad;
1641 selpad = GST_SELECTOR_PAD_CAST (pad);
1643 selpad->active = TRUE;
1644 active_sinkpad = sel->active_sinkpad;
1645 if (sel->active_sinkpad == NULL) {
1646 GValue item = G_VALUE_INIT;
1647 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1648 GstIteratorResult ires;
1650 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1651 gst_iterator_resync (iter);
1652 if (ires == GST_ITERATOR_OK) {
1653 /* If no pad is currently selected, we return the first usable pad to
1654 * guarantee consistency */
1656 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1657 g_value_reset (&item);
1658 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1659 GST_DEBUG_PAD_NAME (active_sinkpad));
1661 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1662 gst_iterator_free (iter);
1665 return active_sinkpad;
1669 gst_input_selector_request_new_pad (GstElement * element,
1670 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1672 GstInputSelector *sel;
1674 GstPad *sinkpad = NULL;
1676 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1678 sel = GST_INPUT_SELECTOR (element);
1680 GST_INPUT_SELECTOR_LOCK (sel);
1682 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1683 name = g_strdup_printf ("sink_%u", sel->padcount++);
1684 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1685 "name", name, "direction", templ->direction, "template", templ, NULL);
1690 gst_pad_set_event_function (sinkpad,
1691 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1692 gst_pad_set_query_function (sinkpad,
1693 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1694 gst_pad_set_chain_function (sinkpad,
1695 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1696 gst_pad_set_iterate_internal_links_function (sinkpad,
1697 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1699 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1700 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1701 gst_pad_set_active (sinkpad, TRUE);
1702 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1703 GST_INPUT_SELECTOR_UNLOCK (sel);
1709 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1711 GstInputSelector *sel;
1713 sel = GST_INPUT_SELECTOR (element);
1714 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1716 GST_INPUT_SELECTOR_LOCK (sel);
1717 /* if the pad was the active pad, makes us select a new one */
1718 if (sel->active_sinkpad == pad) {
1719 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1720 gst_object_unref (sel->active_sinkpad);
1721 sel->active_sinkpad = NULL;
1724 GST_INPUT_SELECTOR_UNLOCK (sel);
1726 gst_pad_set_active (pad, FALSE);
1727 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1731 gst_input_selector_reset (GstInputSelector * sel)
1735 GST_INPUT_SELECTOR_LOCK (sel);
1736 /* clear active pad */
1737 if (sel->active_sinkpad) {
1738 gst_object_unref (sel->active_sinkpad);
1739 sel->active_sinkpad = NULL;
1741 /* reset each of our sinkpads state */
1742 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1743 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1745 gst_selector_pad_reset (selpad);
1748 gst_tag_list_unref (selpad->tags);
1749 selpad->tags = NULL;
1752 sel->have_group_id = TRUE;
1753 GST_INPUT_SELECTOR_UNLOCK (sel);
1756 static GstStateChangeReturn
1757 gst_input_selector_change_state (GstElement * element,
1758 GstStateChange transition)
1760 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1761 GstStateChangeReturn result;
1763 switch (transition) {
1764 case GST_STATE_CHANGE_READY_TO_PAUSED:
1765 GST_INPUT_SELECTOR_LOCK (self);
1766 self->blocked = FALSE;
1767 self->flushing = FALSE;
1768 GST_INPUT_SELECTOR_UNLOCK (self);
1770 case GST_STATE_CHANGE_PAUSED_TO_READY:
1771 /* first unlock before we call the parent state change function, which
1772 * tries to acquire the stream lock when going to ready. */
1773 GST_INPUT_SELECTOR_LOCK (self);
1774 self->blocked = FALSE;
1775 self->flushing = TRUE;
1776 GST_INPUT_SELECTOR_BROADCAST (self);
1777 GST_INPUT_SELECTOR_UNLOCK (self);
1783 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1785 switch (transition) {
1786 case GST_STATE_CHANGE_PAUSED_TO_READY:
1787 gst_input_selector_reset (self);
1797 gst_input_selector_block (GstInputSelector * self)
1800 GstSelectorPad *spad;
1802 GST_INPUT_SELECTOR_LOCK (self);
1805 GST_WARNING_OBJECT (self, "switch already blocked");
1807 self->blocked = TRUE;
1808 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1811 ret = gst_selector_pad_get_running_time (spad);
1813 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1815 GST_INPUT_SELECTOR_UNLOCK (self);