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., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, 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 GstClockTime position; /* the current position in the segment */
177 GstSegment segment; /* the current segment on the pad */
178 guint32 segment_seqnum; /* sequence number of the current segment */
180 gboolean events_pending; /* TRUE if sticky events need to be updated */
182 gboolean sending_cached_buffers;
183 GQueue *cached_buffers;
186 struct _GstSelectorPadCachedBuffer
192 struct _GstSelectorPadClass
197 GType gst_selector_pad_get_type (void);
198 static void gst_selector_pad_finalize (GObject * object);
199 static void gst_selector_pad_get_property (GObject * object,
200 guint prop_id, GValue * value, GParamSpec * pspec);
201 static void gst_selector_pad_set_property (GObject * object,
202 guint prop_id, const GValue * value, GParamSpec * pspec);
204 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
205 static void gst_selector_pad_reset (GstSelectorPad * pad);
206 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
208 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
210 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
212 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
214 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
216 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
218 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
221 gst_selector_pad_class_init (GstSelectorPadClass * klass)
223 GObjectClass *gobject_class;
225 gobject_class = (GObjectClass *) klass;
227 gobject_class->finalize = gst_selector_pad_finalize;
229 gobject_class->get_property = gst_selector_pad_get_property;
230 gobject_class->set_property = gst_selector_pad_set_property;
232 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
233 g_param_spec_int64 ("running-time", "Running time",
234 "Running time of stream on pad", 0, G_MAXINT64, 0,
235 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
237 g_param_spec_boxed ("tags", "Tags",
238 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
239 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
241 g_param_spec_boolean ("active", "Active",
242 "If the pad is currently active", FALSE,
243 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
244 /* FIXME: better property name? */
245 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
246 g_param_spec_boolean ("always-ok", "Always OK",
247 "Make an inactive pad return OK instead of NOT_LINKED",
248 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
252 gst_selector_pad_init (GstSelectorPad * pad)
254 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
255 gst_selector_pad_reset (pad);
259 gst_selector_pad_finalize (GObject * object)
263 pad = GST_SELECTOR_PAD_CAST (object);
266 gst_tag_list_unref (pad->tags);
267 gst_selector_pad_free_cached_buffers (pad);
269 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
273 gst_selector_pad_set_property (GObject * object, guint prop_id,
274 const GValue * value, GParamSpec * pspec)
276 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
279 case PROP_PAD_ALWAYS_OK:
280 GST_OBJECT_LOCK (object);
281 spad->always_ok = g_value_get_boolean (value);
282 GST_OBJECT_UNLOCK (object);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 gst_selector_pad_get_property (GObject * object, guint prop_id,
292 GValue * value, GParamSpec * pspec)
294 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
297 case PROP_PAD_RUNNING_TIME:
298 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
301 GST_OBJECT_LOCK (object);
302 g_value_set_boxed (value, spad->tags);
303 GST_OBJECT_UNLOCK (object);
305 case PROP_PAD_ACTIVE:
307 GstInputSelector *sel;
309 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);
315 case PROP_PAD_ALWAYS_OK:
316 GST_OBJECT_LOCK (object);
317 g_value_set_boolean (value, spad->always_ok);
318 GST_OBJECT_UNLOCK (object);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gst_selector_pad_get_running_time (GstSelectorPad * pad)
331 GST_OBJECT_LOCK (pad);
333 guint64 position = pad->position;
334 GstFormat format = pad->segment.format;
336 ret = gst_segment_to_running_time (&pad->segment, format, position);
338 GST_OBJECT_UNLOCK (pad);
340 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
341 " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
346 /* must be called with the SELECTOR_LOCK */
348 gst_selector_pad_reset (GstSelectorPad * pad)
350 GST_OBJECT_LOCK (pad);
354 pad->eos_sent = FALSE;
355 pad->events_pending = FALSE;
356 pad->discont = FALSE;
357 pad->flushing = FALSE;
358 pad->position = GST_CLOCK_TIME_NONE;
359 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
360 pad->sending_cached_buffers = FALSE;
361 gst_selector_pad_free_cached_buffers (pad);
362 GST_OBJECT_UNLOCK (pad);
365 static GstSelectorPadCachedBuffer *
366 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
368 GstSelectorPadCachedBuffer *cached_buffer =
369 g_slice_new (GstSelectorPadCachedBuffer);
370 cached_buffer->buffer = buffer;
371 cached_buffer->segment = selpad->segment;
372 return cached_buffer;
376 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
378 gst_buffer_unref (cached_buffer->buffer);
379 g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
382 /* must be called with the SELECTOR_LOCK */
384 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
386 if (selpad->segment.format != GST_FORMAT_TIME) {
387 GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
388 "not caching", buffer);
392 GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
393 if (!selpad->cached_buffers)
394 selpad->cached_buffers = g_queue_new ();
395 g_queue_push_tail (selpad->cached_buffers,
396 gst_selector_pad_new_cached_buffer (selpad, buffer));
399 /* must be called with the SELECTOR_LOCK */
401 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
403 GstSelectorPadCachedBuffer *cached_buffer;
405 if (!selpad->cached_buffers)
408 GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
409 while ((cached_buffer = g_queue_pop_head (selpad->cached_buffers)))
410 gst_selector_pad_free_cached_buffer (cached_buffer);
411 g_queue_free (selpad->cached_buffers);
412 selpad->cached_buffers = NULL;
415 /* strictly get the linked pad from the sinkpad. If the pad is active we return
416 * the srcpad else we return NULL */
418 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
420 GstInputSelector *sel;
422 GstIterator *it = NULL;
425 sel = GST_INPUT_SELECTOR (parent);
427 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
429 g_value_init (&val, GST_TYPE_PAD);
430 g_value_set_object (&val, otherpad);
431 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
432 g_value_unset (&val);
433 gst_object_unref (otherpad);
440 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
444 gboolean new_tags = FALSE;
445 GstInputSelector *sel;
446 GstSelectorPad *selpad;
447 GstPad *prev_active_sinkpad;
448 GstPad *active_sinkpad;
450 sel = GST_INPUT_SELECTOR (parent);
451 selpad = GST_SELECTOR_PAD_CAST (pad);
452 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
454 GST_INPUT_SELECTOR_LOCK (sel);
455 prev_active_sinkpad = sel->active_sinkpad;
456 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
457 GST_INPUT_SELECTOR_UNLOCK (sel);
459 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
460 g_object_notify (G_OBJECT (sel), "active-pad");
463 GST_INPUT_SELECTOR_LOCK (sel);
464 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
466 /* only forward if we are dealing with the active sinkpad */
467 forward = (pad == active_sinkpad);
469 switch (GST_EVENT_TYPE (event)) {
470 case GST_EVENT_FLUSH_START:
471 /* Unblock the pad if it's waiting */
472 selpad->flushing = TRUE;
473 GST_INPUT_SELECTOR_BROADCAST (sel);
475 case GST_EVENT_FLUSH_STOP:
476 gst_selector_pad_reset (selpad);
478 case GST_EVENT_SEGMENT:
480 gst_event_copy_segment (event, &selpad->segment);
481 selpad->segment_seqnum = gst_event_get_seqnum (event);
483 /* Update the position */
484 if (selpad->position == GST_CLOCK_TIME_NONE
485 || selpad->segment.position > selpad->position) {
486 selpad->position = selpad->segment.position;
487 } else if (selpad->position != GST_CLOCK_TIME_NONE
488 && selpad->position > selpad->segment.position) {
489 selpad->segment.position = selpad->position;
492 gst_event_unref (event);
493 event = gst_event_new_segment (&selpad->segment);
494 gst_event_set_seqnum (event, selpad->segment_seqnum);
497 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
503 GstTagList *tags, *oldtags, *newtags;
505 gst_event_parse_tag (event, &tags);
507 oldtags = selpad->tags;
509 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
510 selpad->tags = newtags;
512 gst_tag_list_unref (oldtags);
513 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
522 selpad->eos_sent = TRUE;
524 GstSelectorPad *active_selpad;
526 /* If the active sinkpad is in EOS state but EOS
527 * was not sent downstream this means that the pad
528 * got EOS before it was set as active pad and that
529 * the previously active pad got EOS after it was
532 active_selpad = GST_SELECTOR_PAD (active_sinkpad);
533 forward = (active_selpad->eos && !active_selpad->eos_sent);
534 active_selpad->eos_sent = TRUE;
536 GST_DEBUG_OBJECT (pad, "received EOS");
541 GST_INPUT_SELECTOR_UNLOCK (sel);
543 g_object_notify (G_OBJECT (selpad), "tags");
545 GST_DEBUG_OBJECT (pad, "forwarding event");
546 res = gst_pad_push_event (sel->srcpad, event);
548 /* If we aren't forwarding the event because the pad is not the
549 * active_sinkpad, then set the flag on the pad
550 * that says a segment needs sending if/when that pad is activated.
551 * For all other cases, we send the event immediately, which makes
552 * sparse streams and other segment updates work correctly downstream.
554 if (GST_EVENT_IS_STICKY (event))
555 selpad->events_pending = TRUE;
556 gst_event_unref (event);
563 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
565 gboolean res = FALSE;
567 switch (GST_QUERY_TYPE (query)) {
568 case GST_QUERY_ALLOCATION:{
569 GstPad *active_sinkpad;
570 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
572 /* Only do the allocation query for the active sinkpad,
573 * after switching a reconfigure event is sent and upstream
574 * should reconfigure and do a new allocation query
576 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
577 GST_INPUT_SELECTOR_LOCK (sel);
578 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
579 GST_INPUT_SELECTOR_UNLOCK (sel);
581 if (pad != active_sinkpad) {
589 res = gst_pad_query_default (pad, parent, query);
597 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
598 * or return TRUE when flushing */
600 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
602 while (self->blocked && !self->flushing && !pad->flushing) {
603 /* we can be unlocked here when we are shutting down (flushing) or when we
605 GST_INPUT_SELECTOR_WAIT (self);
607 return self->flushing;
610 /* must be called without the SELECTOR_LOCK, will wait until the running time
611 * of the active pad is after this pad or return TRUE when flushing */
613 gst_input_selector_wait_running_time (GstInputSelector * sel,
614 GstSelectorPad * selpad, GstBuffer * buf)
618 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
620 /* If we have no valid timestamp we can't sync this buffer */
621 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
622 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
623 "invalid timestamp");
627 seg = &selpad->segment;
630 * a) this is the active pad
631 * b) the pad or the selector is flushing
632 * c) the selector is not blocked
633 * d) the buffer running time is before the current running time
634 * (either active-seg or clock, depending on sync-mode)
637 GST_INPUT_SELECTOR_LOCK (sel);
639 GstPad *active_sinkpad;
640 GstSelectorPad *active_selpad;
642 gint64 cur_running_time;
643 GstClockTime running_time;
646 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
647 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
649 if (seg->format != GST_FORMAT_TIME) {
650 GST_DEBUG_OBJECT (selpad,
651 "Not waiting because we don't have a TIME segment");
652 GST_INPUT_SELECTOR_UNLOCK (sel);
656 running_time = GST_BUFFER_TIMESTAMP (buf);
657 /* If possible try to get the running time at the end of the buffer */
658 if (GST_BUFFER_DURATION_IS_VALID (buf))
659 running_time += GST_BUFFER_DURATION (buf);
660 /* Only use the segment to convert to running time if the segment is
661 * in TIME format, otherwise do our best to try to sync */
662 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
663 if (running_time > seg->stop) {
664 running_time = seg->stop;
668 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
669 /* If this is outside the segment don't sync */
670 if (running_time == -1) {
671 GST_DEBUG_OBJECT (selpad,
672 "Not waiting because buffer is outside segment");
673 GST_INPUT_SELECTOR_UNLOCK (sel);
677 cur_running_time = GST_CLOCK_TIME_NONE;
678 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
679 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
681 GstClockTime base_time;
683 cur_running_time = gst_clock_get_time (clock);
684 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
685 if (base_time <= cur_running_time)
686 cur_running_time -= base_time;
688 cur_running_time = 0;
691 GstSegment *active_seg;
693 active_seg = &active_selpad->segment;
695 /* If the active segment is configured but not to time format
696 * we can't do any syncing at all */
697 if (active_seg->format != GST_FORMAT_TIME
698 && active_seg->format != GST_FORMAT_UNDEFINED) {
699 GST_DEBUG_OBJECT (selpad,
700 "Not waiting because active segment isn't in TIME format");
701 GST_INPUT_SELECTOR_UNLOCK (sel);
705 /* Get active pad's running time, if no configured segment yet keep at -1 */
706 if (active_seg->format == GST_FORMAT_TIME)
707 cur_running_time = gst_segment_to_running_time (active_seg,
708 GST_FORMAT_TIME, active_seg->position);
711 if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
712 (sel->blocked || cur_running_time == -1
713 || running_time >= cur_running_time)) {
715 GST_DEBUG_OBJECT (selpad,
716 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
717 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
718 GST_TIME_ARGS (cur_running_time));
720 GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
722 GST_INPUT_SELECTOR_WAIT (sel);
724 GST_INPUT_SELECTOR_UNLOCK (sel);
729 /* Return TRUE if the selector or the pad is flushing */
730 return (sel->flushing || selpad->flushing);
734 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
736 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
738 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
739 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
742 e = gst_event_new_segment (seg);
743 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
745 gst_pad_push_event (sel->srcpad, e);
746 } else if (GST_EVENT_TYPE (*event) != GST_EVENT_STREAM_START) {
747 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
753 #if DEBUG_CACHED_BUFFERS
755 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
759 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
760 GstSelectorPad *selpad;
765 selpad = GST_SELECTOR_PAD_CAST (walk->data);
766 if (!selpad->cached_buffers) {
767 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
771 timestamps = g_string_new ("Cached buffers timestamps:");
772 for (i = 0; i < selpad->cached_buffers->length; ++i) {
773 GstSelectorPadCachedBuffer *cached_buffer;
775 cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
776 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
777 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (cached_buffer->buffer)));
779 str = g_string_free (timestamps, FALSE);
780 GST_DEBUG_OBJECT (selpad, str);
786 /* must be called with the SELECTOR_LOCK */
788 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
792 gint64 cur_running_time;
795 cur_running_time = GST_CLOCK_TIME_NONE;
796 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
797 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
799 GstClockTime base_time;
801 cur_running_time = gst_clock_get_time (clock);
802 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
803 if (base_time <= cur_running_time)
804 cur_running_time -= base_time;
806 cur_running_time = 0;
808 gst_object_unref (clock);
811 GstPad *active_sinkpad;
812 GstSelectorPad *active_selpad;
813 GstSegment *active_seg;
815 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
816 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
817 active_seg = &active_selpad->segment;
819 /* Get active pad's running time, if no configured segment yet keep at -1 */
820 if (active_seg->format == GST_FORMAT_TIME)
821 cur_running_time = gst_segment_to_running_time (active_seg,
822 GST_FORMAT_TIME, active_seg->position);
825 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
828 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
829 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
830 GstSelectorPad *selpad;
832 GstSelectorPadCachedBuffer *cached_buffer;
833 GSList *maybe_remove;
834 guint queue_position;
836 selpad = GST_SELECTOR_PAD_CAST (walk->data);
837 if (!selpad->cached_buffers)
840 seg = &selpad->segment;
844 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
846 GstBuffer *buffer = cached_buffer->buffer;
847 GstClockTime running_time;
850 /* If we have no valid timestamp we can't sync this buffer */
851 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
852 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
853 queue_position = g_slist_length (maybe_remove);
857 /* the buffer is still valid if its duration is valid and the
858 * timestamp + duration is >= time, or if its duration is invalid
859 * and the timestamp is >= time */
860 running_time = GST_BUFFER_TIMESTAMP (buffer);
861 /* If possible try to get the running time at the end of the buffer */
862 if (GST_BUFFER_DURATION_IS_VALID (buffer))
863 running_time += GST_BUFFER_DURATION (buffer);
864 /* Only use the segment to convert to running time if the segment is
865 * in TIME format, otherwise do our best to try to sync */
866 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
867 if (running_time > seg->stop) {
868 running_time = seg->stop;
872 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
874 GST_DEBUG_OBJECT (selpad,
875 "checking if buffer %p running time=%" GST_TIME_FORMAT
876 " >= stream time=%" GST_TIME_FORMAT, buffer,
877 GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
878 if (running_time >= cur_running_time) {
882 GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
883 g_queue_pop_nth (selpad->cached_buffers, queue_position);
884 gst_selector_pad_free_cached_buffer (cached_buffer);
886 for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
887 /* A buffer after some invalid buffers was removed, it means the invalid buffers
888 * are old, lets also remove them */
889 cached_buffer = l->data;
890 g_queue_remove (selpad->cached_buffers, cached_buffer);
891 gst_selector_pad_free_cached_buffer (cached_buffer);
894 g_slist_free (maybe_remove);
899 g_slist_free (maybe_remove);
902 if (g_queue_is_empty (selpad->cached_buffers)) {
903 g_queue_free (selpad->cached_buffers);
904 selpad->cached_buffers = NULL;
908 #if DEBUG_CACHED_BUFFERS
909 gst_input_selector_debug_cached_buffers (sel);
914 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
916 GstInputSelector *sel;
918 GstPad *active_sinkpad;
919 GstPad *prev_active_sinkpad;
920 GstSelectorPad *selpad;
921 GstClockTime start_time;
923 sel = GST_INPUT_SELECTOR (parent);
924 selpad = GST_SELECTOR_PAD_CAST (pad);
926 GST_DEBUG_OBJECT (selpad,
927 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
928 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
930 GST_INPUT_SELECTOR_LOCK (sel);
931 /* wait or check for flushing */
932 if (gst_input_selector_wait (sel, selpad)) {
933 GST_INPUT_SELECTOR_UNLOCK (sel);
937 GST_LOG_OBJECT (pad, "getting active pad");
939 prev_active_sinkpad = sel->active_sinkpad;
940 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
942 /* In sync mode wait until the active pad has advanced
943 * after the running time of the current buffer */
944 if (sel->sync_streams) {
945 /* call chain for each cached buffer if we are not the active pad
946 * or if we are the active pad but didn't push anything yet. */
947 if (active_sinkpad != pad || !selpad->pushed) {
948 /* no need to check for sel->cache_buffers as selpad->cached_buffers
949 * will only be valid if cache_buffers is TRUE */
950 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
951 GstSelectorPadCachedBuffer *cached_buffer;
952 GstSegment saved_segment;
954 saved_segment = selpad->segment;
956 selpad->sending_cached_buffers = TRUE;
957 while (!sel->flushing && !selpad->flushing &&
958 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
959 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
960 "invoking chain for cached buffer %p", cached_buffer->buffer);
962 selpad->segment = cached_buffer->segment;
963 selpad->events_pending = TRUE;
964 GST_INPUT_SELECTOR_UNLOCK (sel);
965 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
966 GST_INPUT_SELECTOR_LOCK (sel);
968 /* we may have cleaned up the queue in the meantime because of
970 if (!selpad->cached_buffers) {
974 selpad->sending_cached_buffers = FALSE;
976 /* all cached buffers sent, restore segment for current buffer */
977 selpad->segment = saved_segment;
978 selpad->events_pending = TRUE;
980 /* Might have changed while calling chain for cached buffers */
981 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
985 if (active_sinkpad != pad) {
986 GST_INPUT_SELECTOR_UNLOCK (sel);
987 if (gst_input_selector_wait_running_time (sel, selpad, buf))
989 GST_INPUT_SELECTOR_LOCK (sel);
992 /* Might have changed while waiting */
993 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
996 /* update the segment on the srcpad */
997 start_time = GST_BUFFER_TIMESTAMP (buf);
998 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
999 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1000 GST_TIME_ARGS (start_time));
1001 if (GST_BUFFER_DURATION_IS_VALID (buf))
1002 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1003 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1005 GST_OBJECT_LOCK (pad);
1006 selpad->position = start_time;
1007 selpad->segment.position = start_time;
1008 GST_OBJECT_UNLOCK (pad);
1011 /* Ignore buffers from pads except the selected one */
1012 if (pad != active_sinkpad)
1015 /* Tell all non-active pads that we advanced the running time */
1016 if (sel->sync_streams)
1017 GST_INPUT_SELECTOR_BROADCAST (sel);
1019 GST_INPUT_SELECTOR_UNLOCK (sel);
1021 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
1022 g_object_notify (G_OBJECT (sel), "active-pad");
1025 /* if we have a pending events, push them now */
1026 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1027 || selpad->events_pending)) {
1028 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1030 selpad->events_pending = FALSE;
1033 if (selpad->discont) {
1034 buf = gst_buffer_make_writable (buf);
1036 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1037 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1038 selpad->discont = FALSE;
1042 GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1043 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1045 res = gst_pad_push (sel->srcpad, gst_buffer_ref (buf));
1046 GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1048 GST_INPUT_SELECTOR_LOCK (sel);
1050 if (sel->sync_streams && sel->cache_buffers) {
1051 /* Might have changed while pushing */
1052 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1053 /* only set pad to pushed if we are still the active pad */
1054 if (active_sinkpad == pad)
1055 selpad->pushed = TRUE;
1057 /* cache buffer as we may need it again if we change pads */
1058 gst_selector_pad_cache_buffer (selpad, buf);
1059 gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1061 selpad->pushed = TRUE;
1062 gst_buffer_unref (buf);
1064 GST_INPUT_SELECTOR_UNLOCK (sel);
1069 /* dropped buffers */
1072 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1074 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1075 /* when we drop a buffer, we're creating a discont on this pad */
1076 selpad->discont = TRUE;
1077 GST_INPUT_SELECTOR_UNLOCK (sel);
1078 gst_buffer_unref (buf);
1080 /* figure out what to return upstream */
1081 GST_OBJECT_LOCK (selpad);
1082 if (selpad->always_ok || !active_pad_pushed)
1085 res = GST_FLOW_NOT_LINKED;
1086 GST_OBJECT_UNLOCK (selpad);
1092 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1093 gst_buffer_unref (buf);
1094 res = GST_FLOW_FLUSHING;
1099 static void gst_input_selector_dispose (GObject * object);
1100 static void gst_input_selector_finalize (GObject * object);
1102 static void gst_input_selector_set_property (GObject * object,
1103 guint prop_id, const GValue * value, GParamSpec * pspec);
1104 static void gst_input_selector_get_property (GObject * object,
1105 guint prop_id, GValue * value, GParamSpec * pspec);
1107 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1108 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1109 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1111 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1112 element, GstStateChange transition);
1114 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1116 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1118 static gint64 gst_input_selector_block (GstInputSelector * self);
1121 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1122 "input-selector", 0, "An input stream selector element");
1123 #define gst_input_selector_parent_class parent_class
1124 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1128 gst_input_selector_class_init (GstInputSelectorClass * klass)
1130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1131 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1133 gobject_class->dispose = gst_input_selector_dispose;
1134 gobject_class->finalize = gst_input_selector_finalize;
1136 gobject_class->set_property = gst_input_selector_set_property;
1137 gobject_class->get_property = gst_input_selector_get_property;
1139 g_object_class_install_property (gobject_class, PROP_N_PADS,
1140 g_param_spec_uint ("n-pads", "Number of Pads",
1141 "The number of sink pads", 0, G_MAXUINT, 0,
1142 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1144 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1145 g_param_spec_object ("active-pad", "Active pad",
1146 "The currently active sink pad", GST_TYPE_PAD,
1147 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1150 * GstInputSelector:sync-streams
1152 * If set to %TRUE all inactive streams will be synced to the
1153 * running time of the active stream or to the current clock.
1155 * To make sure no buffers are dropped by input-selector
1156 * that might be needed when switching the active pad,
1157 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1159 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1160 g_param_spec_boolean ("sync-streams", "Sync Streams",
1161 "Synchronize inactive streams to the running time of the active "
1162 "stream or to the current clock",
1163 DEFAULT_SYNC_STREAMS,
1164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1165 GST_PARAM_MUTABLE_READY));
1168 * GstInputSelector:sync-mode
1170 * Select how input-selector will sync buffers when in sync-streams mode.
1172 * Note that when using the "active-segment" mode, the "active-segment" may
1173 * be ahead of current clock time when switching the active pad, as the current
1174 * active pad may have pushed more buffers than what was displayed/consumed,
1175 * which may cause delays and some missing buffers.
1177 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1178 g_param_spec_enum ("sync-mode", "Sync mode",
1179 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1182 GST_PARAM_MUTABLE_READY));
1185 * GstInputSelector:cache-buffers
1187 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1188 * the active pad will cache the buffers still considered valid (after current
1189 * running time, see sync-mode) to avoid missing frames if/when the pad is
1192 * The active pad may push more buffers than what is currently displayed/consumed
1193 * and when changing pads those buffers will be discarded and the only way to
1194 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1196 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1197 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1198 "Cache buffers for active-pad",
1199 DEFAULT_CACHE_BUFFERS,
1200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1201 GST_PARAM_MUTABLE_READY));
1204 * GstInputSelector::block:
1205 * @inputselector: the #GstInputSelector
1207 * Block all sink pads in preparation for a switch. Returns the stop time of
1208 * the current switch segment, as a running time, or 0 if there is no current
1209 * active pad or the current active pad never received data.
1211 gst_input_selector_signals[SIGNAL_BLOCK] =
1212 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1213 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1214 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1215 g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1217 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1218 "Generic", "N-to-1 input stream selector",
1219 "Julien Moutte <julien@moutte.net>, "
1220 "Jan Schmidt <thaytan@mad.scientist.com>, "
1221 "Wim Taymans <wim.taymans@gmail.com>");
1222 gst_element_class_add_pad_template (gstelement_class,
1223 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1224 gst_element_class_add_pad_template (gstelement_class,
1225 gst_static_pad_template_get (&gst_input_selector_src_factory));
1227 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1228 gstelement_class->release_pad = gst_input_selector_release_pad;
1229 gstelement_class->change_state = gst_input_selector_change_state;
1231 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1235 gst_input_selector_init (GstInputSelector * sel)
1237 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1238 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1239 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1240 gst_pad_set_query_function (sel->srcpad,
1241 GST_DEBUG_FUNCPTR (gst_input_selector_query));
1242 gst_pad_set_event_function (sel->srcpad,
1243 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1244 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1245 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1246 /* sinkpad management */
1247 sel->active_sinkpad = NULL;
1249 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1251 g_mutex_init (&sel->lock);
1252 g_cond_init (&sel->cond);
1253 sel->blocked = FALSE;
1255 /* lets give a change for downstream to do something on
1256 * active-pad change before we start pushing new buffers */
1257 g_signal_connect_data (sel, "notify::active-pad",
1258 (GCallback) gst_input_selector_active_pad_changed, NULL,
1259 NULL, G_CONNECT_AFTER);
1263 gst_input_selector_dispose (GObject * object)
1265 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1267 if (sel->active_sinkpad) {
1268 gst_object_unref (sel->active_sinkpad);
1269 sel->active_sinkpad = NULL;
1271 G_OBJECT_CLASS (parent_class)->dispose (object);
1275 gst_input_selector_finalize (GObject * object)
1277 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1279 g_mutex_clear (&sel->lock);
1280 g_cond_clear (&sel->cond);
1282 G_OBJECT_CLASS (parent_class)->finalize (object);
1285 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1286 * active pad changed. */
1288 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1290 GstSelectorPad *old, *new;
1291 GstPad **active_pad_p;
1293 if (pad == self->active_sinkpad)
1296 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1297 new = GST_SELECTOR_PAD_CAST (pad);
1299 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1300 GST_DEBUG_PAD_NAME (new));
1303 old->pushed = FALSE;
1305 new->pushed = FALSE;
1307 /* Send a new SEGMENT event on the new pad next */
1308 if (old != new && new)
1309 new->events_pending = TRUE;
1311 active_pad_p = &self->active_sinkpad;
1312 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1314 if (old && old != new)
1315 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1317 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1319 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1320 self->active_sinkpad);
1326 gst_input_selector_set_property (GObject * object, guint prop_id,
1327 const GValue * value, GParamSpec * pspec)
1329 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1332 case PROP_ACTIVE_PAD:
1336 pad = g_value_get_object (value);
1338 GST_INPUT_SELECTOR_LOCK (sel);
1340 #if DEBUG_CACHED_BUFFERS
1341 gst_input_selector_debug_cached_buffers (sel);
1344 gst_input_selector_set_active_pad (sel, pad);
1346 #if DEBUG_CACHED_BUFFERS
1347 gst_input_selector_debug_cached_buffers (sel);
1350 GST_INPUT_SELECTOR_UNLOCK (sel);
1353 case PROP_SYNC_STREAMS:
1354 GST_INPUT_SELECTOR_LOCK (sel);
1355 sel->sync_streams = g_value_get_boolean (value);
1356 GST_INPUT_SELECTOR_UNLOCK (sel);
1358 case PROP_SYNC_MODE:
1359 GST_INPUT_SELECTOR_LOCK (sel);
1360 sel->sync_mode = g_value_get_enum (value);
1361 GST_INPUT_SELECTOR_UNLOCK (sel);
1363 case PROP_CACHE_BUFFERS:
1364 GST_INPUT_SELECTOR_LOCK (object);
1365 sel->cache_buffers = g_value_get_boolean (value);
1366 GST_INPUT_SELECTOR_UNLOCK (object);
1369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1375 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1376 GParamSpec * pspec, gpointer user_data)
1378 /* Wake up all non-active pads in sync mode, they might be
1379 * the active pad now */
1380 if (sel->sync_streams)
1381 GST_INPUT_SELECTOR_BROADCAST (sel);
1385 gst_input_selector_get_property (GObject * object, guint prop_id,
1386 GValue * value, GParamSpec * pspec)
1388 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1392 GST_INPUT_SELECTOR_LOCK (object);
1393 g_value_set_uint (value, sel->n_pads);
1394 GST_INPUT_SELECTOR_UNLOCK (object);
1396 case PROP_ACTIVE_PAD:
1397 GST_INPUT_SELECTOR_LOCK (object);
1398 g_value_set_object (value, sel->active_sinkpad);
1399 GST_INPUT_SELECTOR_UNLOCK (object);
1401 case PROP_SYNC_STREAMS:
1402 GST_INPUT_SELECTOR_LOCK (object);
1403 g_value_set_boolean (value, sel->sync_streams);
1404 GST_INPUT_SELECTOR_UNLOCK (object);
1406 case PROP_SYNC_MODE:
1407 GST_INPUT_SELECTOR_LOCK (object);
1408 g_value_set_enum (value, sel->sync_mode);
1409 GST_INPUT_SELECTOR_UNLOCK (object);
1411 case PROP_CACHE_BUFFERS:
1412 GST_INPUT_SELECTOR_LOCK (object);
1413 g_value_set_boolean (value, sel->cache_buffers);
1414 GST_INPUT_SELECTOR_UNLOCK (object);
1417 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1423 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1426 GstPad *otherpad = NULL;
1428 GST_INPUT_SELECTOR_LOCK (sel);
1429 if (pad == sel->srcpad)
1430 otherpad = sel->active_sinkpad;
1431 else if (pad == sel->active_sinkpad || !strict)
1432 otherpad = sel->srcpad;
1434 gst_object_ref (otherpad);
1435 GST_INPUT_SELECTOR_UNLOCK (sel);
1441 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1443 GstInputSelector *sel;
1444 gboolean result = FALSE;
1446 gboolean done = FALSE;
1447 GValue item = { 0, };
1449 GList *pushed_pads = NULL;
1451 sel = GST_INPUT_SELECTOR (parent);
1452 /* Send upstream events to all sinkpads */
1453 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1455 /* This is now essentially a copy of gst_pad_event_default_dispatch
1456 * with a different iterator */
1458 switch (gst_iterator_next (iter, &item)) {
1459 case GST_ITERATOR_OK:
1460 eventpad = g_value_get_object (&item);
1462 /* if already pushed, skip */
1463 if (g_list_find (pushed_pads, eventpad)) {
1464 g_value_reset (&item);
1468 gst_event_ref (event);
1469 result |= gst_pad_push_event (eventpad, event);
1471 g_value_reset (&item);
1473 case GST_ITERATOR_RESYNC:
1474 /* We don't reset the result here because we don't push the event
1475 * again on pads that got the event already and because we need
1476 * to consider the result of the previous pushes */
1477 gst_iterator_resync (iter);
1479 case GST_ITERATOR_ERROR:
1480 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1483 case GST_ITERATOR_DONE:
1488 g_value_unset (&item);
1489 gst_iterator_free (iter);
1491 g_list_free (pushed_pads);
1493 gst_event_unref (event);
1498 /* query on the srcpad. We override this function because by default it will
1499 * only forward the query to one random sinkpad */
1501 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1503 gboolean res = FALSE;
1504 GstInputSelector *sel;
1506 sel = GST_INPUT_SELECTOR (parent);
1508 switch (GST_QUERY_TYPE (query)) {
1509 case GST_QUERY_LATENCY:
1512 GstClockTime resmin, resmax;
1519 /* perform the query on all sinkpads and combine the results. We take the
1520 * max of min and the min of max for the result latency. */
1521 GST_INPUT_SELECTOR_LOCK (sel);
1522 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1523 walk = g_list_next (walk)) {
1524 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1526 if (gst_pad_peer_query (sinkpad, query)) {
1527 GstClockTime min, max;
1530 /* one query succeeded, we succeed too */
1533 gst_query_parse_latency (query, &live, &min, &max);
1535 GST_DEBUG_OBJECT (sinkpad,
1536 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1537 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1544 else if (max < resmax)
1546 if (reslive == FALSE)
1551 GST_INPUT_SELECTOR_UNLOCK (sel);
1553 gst_query_set_latency (query, reslive, resmin, resmax);
1555 GST_DEBUG_OBJECT (sel,
1556 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1557 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1564 res = gst_pad_query_default (pad, parent, query);
1571 /* check if the pad is the active sinkpad */
1572 static inline gboolean
1573 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1577 GST_INPUT_SELECTOR_LOCK (sel);
1578 res = (pad == sel->active_sinkpad);
1579 GST_INPUT_SELECTOR_UNLOCK (sel);
1584 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1586 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1588 GstPad *active_sinkpad;
1589 GstSelectorPad *selpad;
1591 selpad = GST_SELECTOR_PAD_CAST (pad);
1593 selpad->active = TRUE;
1594 active_sinkpad = sel->active_sinkpad;
1595 if (sel->active_sinkpad == NULL) {
1596 GValue item = G_VALUE_INIT;
1597 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1598 GstIteratorResult ires;
1600 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1601 gst_iterator_resync (iter);
1602 if (ires == GST_ITERATOR_OK) {
1603 /* If no pad is currently selected, we return the first usable pad to
1604 * guarantee consistency */
1606 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1607 g_value_reset (&item);
1608 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1609 GST_DEBUG_PAD_NAME (active_sinkpad));
1611 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1612 gst_iterator_free (iter);
1615 return active_sinkpad;
1619 gst_input_selector_request_new_pad (GstElement * element,
1620 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1622 GstInputSelector *sel;
1624 GstPad *sinkpad = NULL;
1626 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1628 sel = GST_INPUT_SELECTOR (element);
1630 GST_INPUT_SELECTOR_LOCK (sel);
1632 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1633 name = g_strdup_printf ("sink_%u", sel->padcount++);
1634 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1635 "name", name, "direction", templ->direction, "template", templ, NULL);
1640 gst_pad_set_event_function (sinkpad,
1641 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1642 gst_pad_set_query_function (sinkpad,
1643 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1644 gst_pad_set_chain_function (sinkpad,
1645 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1646 gst_pad_set_iterate_internal_links_function (sinkpad,
1647 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1649 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1650 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1651 gst_pad_set_active (sinkpad, TRUE);
1652 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1653 GST_INPUT_SELECTOR_UNLOCK (sel);
1659 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1661 GstInputSelector *sel;
1663 sel = GST_INPUT_SELECTOR (element);
1664 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1666 GST_INPUT_SELECTOR_LOCK (sel);
1667 /* if the pad was the active pad, makes us select a new one */
1668 if (sel->active_sinkpad == pad) {
1669 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1670 gst_object_unref (sel->active_sinkpad);
1671 sel->active_sinkpad = NULL;
1675 gst_pad_set_active (pad, FALSE);
1676 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1677 GST_INPUT_SELECTOR_UNLOCK (sel);
1681 gst_input_selector_reset (GstInputSelector * sel)
1685 GST_INPUT_SELECTOR_LOCK (sel);
1686 /* clear active pad */
1687 if (sel->active_sinkpad) {
1688 gst_object_unref (sel->active_sinkpad);
1689 sel->active_sinkpad = NULL;
1691 /* reset each of our sinkpads state */
1692 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1693 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1695 gst_selector_pad_reset (selpad);
1698 gst_tag_list_unref (selpad->tags);
1699 selpad->tags = NULL;
1702 GST_INPUT_SELECTOR_UNLOCK (sel);
1705 static GstStateChangeReturn
1706 gst_input_selector_change_state (GstElement * element,
1707 GstStateChange transition)
1709 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1710 GstStateChangeReturn result;
1712 switch (transition) {
1713 case GST_STATE_CHANGE_READY_TO_PAUSED:
1714 GST_INPUT_SELECTOR_LOCK (self);
1715 self->blocked = FALSE;
1716 self->flushing = FALSE;
1717 GST_INPUT_SELECTOR_UNLOCK (self);
1719 case GST_STATE_CHANGE_PAUSED_TO_READY:
1720 /* first unlock before we call the parent state change function, which
1721 * tries to acquire the stream lock when going to ready. */
1722 GST_INPUT_SELECTOR_LOCK (self);
1723 self->blocked = FALSE;
1724 self->flushing = TRUE;
1725 GST_INPUT_SELECTOR_BROADCAST (self);
1726 GST_INPUT_SELECTOR_UNLOCK (self);
1732 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1734 switch (transition) {
1735 case GST_STATE_CHANGE_PAUSED_TO_READY:
1736 gst_input_selector_reset (self);
1746 gst_input_selector_block (GstInputSelector * self)
1749 GstSelectorPad *spad;
1751 GST_INPUT_SELECTOR_LOCK (self);
1754 GST_WARNING_OBJECT (self, "switch already blocked");
1756 self->blocked = TRUE;
1757 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1760 ret = gst_selector_pad_get_running_time (spad);
1762 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1764 GST_INPUT_SELECTOR_UNLOCK (self);