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 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));
311 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
312 GST_PAD_CAST (spad)));
313 gst_object_unref (sel);
315 g_value_set_boolean (value, FALSE);
319 case PROP_PAD_ALWAYS_OK:
320 GST_OBJECT_LOCK (object);
321 g_value_set_boolean (value, spad->always_ok);
322 GST_OBJECT_UNLOCK (object);
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
331 gst_selector_pad_get_running_time (GstSelectorPad * pad)
335 GST_OBJECT_LOCK (pad);
337 guint64 position = pad->position;
338 GstFormat format = pad->segment.format;
340 ret = gst_segment_to_running_time (&pad->segment, format, 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 pad->position = GST_CLOCK_TIME_NONE;
363 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
364 pad->sending_cached_buffers = FALSE;
365 gst_selector_pad_free_cached_buffers (pad);
366 GST_OBJECT_UNLOCK (pad);
369 static GstSelectorPadCachedBuffer *
370 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
372 GstSelectorPadCachedBuffer *cached_buffer =
373 g_slice_new (GstSelectorPadCachedBuffer);
374 cached_buffer->buffer = buffer;
375 cached_buffer->segment = selpad->segment;
376 return cached_buffer;
380 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
382 gst_buffer_unref (cached_buffer->buffer);
383 g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
386 /* must be called with the SELECTOR_LOCK */
388 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
390 if (selpad->segment.format != GST_FORMAT_TIME) {
391 GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
392 "not caching", buffer);
396 GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
397 if (!selpad->cached_buffers)
398 selpad->cached_buffers = g_queue_new ();
399 g_queue_push_tail (selpad->cached_buffers,
400 gst_selector_pad_new_cached_buffer (selpad, buffer));
403 /* must be called with the SELECTOR_LOCK */
405 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
407 GstSelectorPadCachedBuffer *cached_buffer;
409 if (!selpad->cached_buffers)
412 GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
413 while ((cached_buffer = g_queue_pop_head (selpad->cached_buffers)))
414 gst_selector_pad_free_cached_buffer (cached_buffer);
415 g_queue_free (selpad->cached_buffers);
416 selpad->cached_buffers = NULL;
419 /* strictly get the linked pad from the sinkpad. If the pad is active we return
420 * the srcpad else we return NULL */
422 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
424 GstInputSelector *sel;
426 GstIterator *it = NULL;
429 sel = GST_INPUT_SELECTOR (parent);
431 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
433 g_value_init (&val, GST_TYPE_PAD);
434 g_value_set_object (&val, otherpad);
435 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
436 g_value_unset (&val);
437 gst_object_unref (otherpad);
444 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
448 gboolean new_tags = FALSE;
449 GstInputSelector *sel;
450 GstSelectorPad *selpad;
451 GstPad *prev_active_sinkpad;
452 GstPad *active_sinkpad;
454 sel = GST_INPUT_SELECTOR (parent);
455 selpad = GST_SELECTOR_PAD_CAST (pad);
456 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
458 GST_INPUT_SELECTOR_LOCK (sel);
459 prev_active_sinkpad =
460 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
461 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
462 GST_INPUT_SELECTOR_UNLOCK (sel);
464 if (prev_active_sinkpad != active_sinkpad) {
465 if (prev_active_sinkpad)
466 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
467 g_object_notify (G_OBJECT (active_sinkpad), "active");
468 g_object_notify (G_OBJECT (sel), "active-pad");
470 if (prev_active_sinkpad)
471 gst_object_unref (prev_active_sinkpad);
473 GST_INPUT_SELECTOR_LOCK (sel);
474 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
476 /* only forward if we are dealing with the active sinkpad */
477 forward = (pad == active_sinkpad);
479 switch (GST_EVENT_TYPE (event)) {
480 case GST_EVENT_STREAM_START:{
483 if (!gst_event_parse_group_id (event, &group_id))
484 sel->have_group_id = FALSE;
487 case GST_EVENT_FLUSH_START:
488 /* Unblock the pad if it's waiting */
489 selpad->flushing = TRUE;
490 GST_INPUT_SELECTOR_BROADCAST (sel);
492 case GST_EVENT_FLUSH_STOP:
493 gst_selector_pad_reset (selpad);
495 case GST_EVENT_SEGMENT:
497 gst_event_copy_segment (event, &selpad->segment);
498 selpad->segment_seqnum = gst_event_get_seqnum (event);
500 /* Update the position */
501 if (selpad->position == GST_CLOCK_TIME_NONE
502 || selpad->segment.position > selpad->position) {
503 selpad->position = selpad->segment.position;
504 } else if (selpad->position != GST_CLOCK_TIME_NONE
505 && selpad->position > selpad->segment.position) {
506 selpad->segment.position = selpad->position;
509 gst_event_unref (event);
510 event = gst_event_new_segment (&selpad->segment);
511 gst_event_set_seqnum (event, selpad->segment_seqnum);
515 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
521 GstTagList *tags, *oldtags, *newtags;
523 gst_event_parse_tag (event, &tags);
525 oldtags = selpad->tags;
527 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
528 selpad->tags = newtags;
530 gst_tag_list_unref (oldtags);
531 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
540 selpad->eos_sent = TRUE;
542 GstSelectorPad *active_selpad;
544 /* If the active sinkpad is in EOS state but EOS
545 * was not sent downstream this means that the pad
546 * got EOS before it was set as active pad and that
547 * the previously active pad got EOS after it was
550 active_selpad = GST_SELECTOR_PAD (active_sinkpad);
551 forward = (active_selpad->eos && !active_selpad->eos_sent);
552 active_selpad->eos_sent = TRUE;
554 GST_DEBUG_OBJECT (pad, "received EOS");
559 GST_INPUT_SELECTOR_UNLOCK (sel);
561 g_object_notify (G_OBJECT (selpad), "tags");
563 GST_DEBUG_OBJECT (pad, "forwarding event");
564 res = gst_pad_push_event (sel->srcpad, event);
566 /* If we aren't forwarding the event because the pad is not the
567 * active_sinkpad, then set the flag on the pad
568 * that says a segment needs sending if/when that pad is activated.
569 * For all other cases, we send the event immediately, which makes
570 * sparse streams and other segment updates work correctly downstream.
572 if (GST_EVENT_IS_STICKY (event))
573 selpad->events_pending = TRUE;
574 gst_event_unref (event);
581 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
583 gboolean res = FALSE;
585 switch (GST_QUERY_TYPE (query)) {
586 case GST_QUERY_ALLOCATION:{
587 GstPad *active_sinkpad;
588 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
590 /* Only do the allocation query for the active sinkpad,
591 * after switching a reconfigure event is sent and upstream
592 * should reconfigure and do a new allocation query
594 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
595 GST_INPUT_SELECTOR_LOCK (sel);
596 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
597 GST_INPUT_SELECTOR_UNLOCK (sel);
599 if (pad != active_sinkpad) {
607 res = gst_pad_query_default (pad, parent, query);
615 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
616 * or return TRUE when flushing */
618 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
620 while (self->blocked && !self->flushing && !pad->flushing) {
621 /* we can be unlocked here when we are shutting down (flushing) or when we
623 GST_INPUT_SELECTOR_WAIT (self);
625 return self->flushing;
628 /* must be called without the SELECTOR_LOCK, will wait until the running time
629 * of the active pad is after this pad or return TRUE when flushing */
631 gst_input_selector_wait_running_time (GstInputSelector * sel,
632 GstSelectorPad * selpad, GstBuffer * buf)
636 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
638 /* If we have no valid timestamp we can't sync this buffer */
639 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
640 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
641 "invalid timestamp");
645 seg = &selpad->segment;
648 * a) this is the active pad
649 * b) the pad or the selector is flushing
650 * c) the selector is not blocked
651 * d) the buffer running time is before the current running time
652 * (either active-seg or clock, depending on sync-mode)
655 GST_INPUT_SELECTOR_LOCK (sel);
657 GstPad *active_sinkpad;
658 GstSelectorPad *active_selpad;
660 gint64 cur_running_time;
661 GstClockTime running_time;
664 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
665 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
667 if (seg->format != GST_FORMAT_TIME) {
668 GST_DEBUG_OBJECT (selpad,
669 "Not waiting because we don't have a TIME segment");
670 GST_INPUT_SELECTOR_UNLOCK (sel);
674 running_time = GST_BUFFER_TIMESTAMP (buf);
675 /* If possible try to get the running time at the end of the buffer */
676 if (GST_BUFFER_DURATION_IS_VALID (buf))
677 running_time += GST_BUFFER_DURATION (buf);
678 /* Only use the segment to convert to running time if the segment is
679 * in TIME format, otherwise do our best to try to sync */
680 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
681 if (running_time > seg->stop) {
682 running_time = seg->stop;
686 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
687 /* If this is outside the segment don't sync */
688 if (running_time == -1) {
689 GST_DEBUG_OBJECT (selpad,
690 "Not waiting because buffer is outside segment");
691 GST_INPUT_SELECTOR_UNLOCK (sel);
695 cur_running_time = GST_CLOCK_TIME_NONE;
696 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
697 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
699 GstClockTime base_time;
701 cur_running_time = gst_clock_get_time (clock);
702 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
703 if (base_time <= cur_running_time)
704 cur_running_time -= base_time;
706 cur_running_time = 0;
708 gst_object_unref (clock);
711 GstSegment *active_seg;
713 active_seg = &active_selpad->segment;
715 /* If the active segment is configured but not to time format
716 * we can't do any syncing at all */
717 if (active_seg->format != GST_FORMAT_TIME
718 && active_seg->format != GST_FORMAT_UNDEFINED) {
719 GST_DEBUG_OBJECT (selpad,
720 "Not waiting because active segment isn't in TIME format");
721 GST_INPUT_SELECTOR_UNLOCK (sel);
725 /* Get active pad's running time, if no configured segment yet keep at -1 */
726 if (active_seg->format == GST_FORMAT_TIME)
727 cur_running_time = gst_segment_to_running_time (active_seg,
728 GST_FORMAT_TIME, active_seg->position);
731 if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
732 (sel->blocked || cur_running_time == -1
733 || running_time >= cur_running_time)) {
735 GST_DEBUG_OBJECT (selpad,
736 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
737 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
738 GST_TIME_ARGS (cur_running_time));
740 GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
742 GST_INPUT_SELECTOR_WAIT (sel);
744 GST_INPUT_SELECTOR_UNLOCK (sel);
749 /* Return TRUE if the selector or the pad is flushing */
750 return (sel->flushing || selpad->flushing);
754 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
756 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
758 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
759 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
762 e = gst_event_new_segment (seg);
763 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
765 gst_pad_push_event (sel->srcpad, e);
766 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
767 && !sel->have_group_id) {
769 gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
771 /* Only push stream-start once if not all our streams have a stream-id */
773 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
775 gst_event_unref (tmp);
778 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
784 #if DEBUG_CACHED_BUFFERS
786 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
790 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
791 GstSelectorPad *selpad;
796 selpad = GST_SELECTOR_PAD_CAST (walk->data);
797 if (!selpad->cached_buffers) {
798 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
802 timestamps = g_string_new ("Cached buffers timestamps:");
803 for (i = 0; i < selpad->cached_buffers->length; ++i) {
804 GstSelectorPadCachedBuffer *cached_buffer;
806 cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
807 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
808 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (cached_buffer->buffer)));
810 str = g_string_free (timestamps, FALSE);
811 GST_DEBUG_OBJECT (selpad, str);
817 /* must be called with the SELECTOR_LOCK */
819 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
823 gint64 cur_running_time;
826 cur_running_time = GST_CLOCK_TIME_NONE;
827 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
828 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
830 GstClockTime base_time;
832 cur_running_time = gst_clock_get_time (clock);
833 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
834 if (base_time <= cur_running_time)
835 cur_running_time -= base_time;
837 cur_running_time = 0;
839 gst_object_unref (clock);
842 GstPad *active_sinkpad;
843 GstSelectorPad *active_selpad;
844 GstSegment *active_seg;
846 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
847 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
848 active_seg = &active_selpad->segment;
850 /* Get active pad's running time, if no configured segment yet keep at -1 */
851 if (active_seg->format == GST_FORMAT_TIME)
852 cur_running_time = gst_segment_to_running_time (active_seg,
853 GST_FORMAT_TIME, active_seg->position);
856 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
859 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
860 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
861 GstSelectorPad *selpad;
863 GstSelectorPadCachedBuffer *cached_buffer;
864 GSList *maybe_remove;
865 guint queue_position;
867 selpad = GST_SELECTOR_PAD_CAST (walk->data);
868 if (!selpad->cached_buffers)
871 seg = &selpad->segment;
875 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
877 GstBuffer *buffer = cached_buffer->buffer;
878 GstClockTime running_time;
881 /* If we have no valid timestamp we can't sync this buffer */
882 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
883 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
884 queue_position = g_slist_length (maybe_remove);
888 /* the buffer is still valid if its duration is valid and the
889 * timestamp + duration is >= time, or if its duration is invalid
890 * and the timestamp is >= time */
891 running_time = GST_BUFFER_TIMESTAMP (buffer);
892 /* If possible try to get the running time at the end of the buffer */
893 if (GST_BUFFER_DURATION_IS_VALID (buffer))
894 running_time += GST_BUFFER_DURATION (buffer);
895 /* Only use the segment to convert to running time if the segment is
896 * in TIME format, otherwise do our best to try to sync */
897 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
898 if (running_time > seg->stop) {
899 running_time = seg->stop;
903 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
905 GST_DEBUG_OBJECT (selpad,
906 "checking if buffer %p running time=%" GST_TIME_FORMAT
907 " >= stream time=%" GST_TIME_FORMAT, buffer,
908 GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
909 if (running_time >= cur_running_time) {
913 GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
914 g_queue_pop_nth (selpad->cached_buffers, queue_position);
915 gst_selector_pad_free_cached_buffer (cached_buffer);
917 for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
918 /* A buffer after some invalid buffers was removed, it means the invalid buffers
919 * are old, lets also remove them */
920 cached_buffer = l->data;
921 g_queue_remove (selpad->cached_buffers, cached_buffer);
922 gst_selector_pad_free_cached_buffer (cached_buffer);
925 g_slist_free (maybe_remove);
930 g_slist_free (maybe_remove);
933 if (g_queue_is_empty (selpad->cached_buffers)) {
934 g_queue_free (selpad->cached_buffers);
935 selpad->cached_buffers = NULL;
939 #if DEBUG_CACHED_BUFFERS
940 gst_input_selector_debug_cached_buffers (sel);
945 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
947 GstInputSelector *sel;
949 GstPad *active_sinkpad;
950 GstPad *prev_active_sinkpad = NULL;
951 GstSelectorPad *selpad;
952 GstClockTime start_time;
954 sel = GST_INPUT_SELECTOR (parent);
955 selpad = GST_SELECTOR_PAD_CAST (pad);
957 GST_DEBUG_OBJECT (selpad,
958 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
959 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
961 GST_INPUT_SELECTOR_LOCK (sel);
962 /* wait or check for flushing */
963 if (gst_input_selector_wait (sel, selpad)) {
964 GST_INPUT_SELECTOR_UNLOCK (sel);
968 GST_LOG_OBJECT (pad, "getting active pad");
970 prev_active_sinkpad =
971 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
972 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
974 /* In sync mode wait until the active pad has advanced
975 * after the running time of the current buffer */
976 if (sel->sync_streams) {
977 /* call chain for each cached buffer if we are not the active pad
978 * or if we are the active pad but didn't push anything yet. */
979 if (active_sinkpad != pad || !selpad->pushed) {
980 /* no need to check for sel->cache_buffers as selpad->cached_buffers
981 * will only be valid if cache_buffers is TRUE */
982 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
983 GstSelectorPadCachedBuffer *cached_buffer;
984 GstSegment saved_segment;
986 saved_segment = selpad->segment;
988 selpad->sending_cached_buffers = TRUE;
989 while (!sel->flushing && !selpad->flushing &&
990 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
991 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
992 "invoking chain for cached buffer %p", cached_buffer->buffer);
994 selpad->segment = cached_buffer->segment;
995 selpad->events_pending = TRUE;
996 GST_INPUT_SELECTOR_UNLOCK (sel);
997 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
998 GST_INPUT_SELECTOR_LOCK (sel);
1000 /* we may have cleaned up the queue in the meantime because of
1002 if (!selpad->cached_buffers) {
1006 selpad->sending_cached_buffers = FALSE;
1008 /* all cached buffers sent, restore segment for current buffer */
1009 selpad->segment = saved_segment;
1010 selpad->events_pending = TRUE;
1012 /* Might have changed while calling chain for cached buffers */
1013 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1017 if (active_sinkpad != pad) {
1018 GST_INPUT_SELECTOR_UNLOCK (sel);
1019 if (gst_input_selector_wait_running_time (sel, selpad, buf))
1021 GST_INPUT_SELECTOR_LOCK (sel);
1024 /* Might have changed while waiting */
1025 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1028 /* update the segment on the srcpad */
1029 start_time = GST_BUFFER_TIMESTAMP (buf);
1030 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
1031 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1032 GST_TIME_ARGS (start_time));
1033 if (GST_BUFFER_DURATION_IS_VALID (buf))
1034 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1035 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1037 GST_OBJECT_LOCK (pad);
1038 selpad->position = start_time;
1039 selpad->segment.position = start_time;
1040 GST_OBJECT_UNLOCK (pad);
1043 /* Ignore buffers from pads except the selected one */
1044 if (pad != active_sinkpad)
1047 /* Tell all non-active pads that we advanced the running time */
1048 if (sel->sync_streams)
1049 GST_INPUT_SELECTOR_BROADCAST (sel);
1051 GST_INPUT_SELECTOR_UNLOCK (sel);
1053 if (prev_active_sinkpad != active_sinkpad) {
1054 if (prev_active_sinkpad)
1055 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1056 g_object_notify (G_OBJECT (active_sinkpad), "active");
1057 g_object_notify (G_OBJECT (sel), "active-pad");
1060 /* if we have a pending events, push them now */
1061 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1062 || selpad->events_pending)) {
1063 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1065 selpad->events_pending = FALSE;
1068 if (prev_active_sinkpad)
1069 gst_object_unref (prev_active_sinkpad);
1070 prev_active_sinkpad = NULL;
1072 if (selpad->discont) {
1073 buf = gst_buffer_make_writable (buf);
1075 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1076 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1077 selpad->discont = FALSE;
1081 GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1082 buf, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
1084 /* Only make the buffer read-only when necessary */
1085 if (sel->sync_streams && sel->cache_buffers)
1086 buf = gst_buffer_ref (buf);
1087 res = gst_pad_push (sel->srcpad, buf);
1088 GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1090 GST_INPUT_SELECTOR_LOCK (sel);
1092 if (sel->sync_streams && sel->cache_buffers) {
1093 /* Might have changed while pushing */
1094 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1095 /* only set pad to pushed if we are still the active pad */
1096 if (active_sinkpad == pad)
1097 selpad->pushed = TRUE;
1099 /* cache buffer as we may need it again if we change pads */
1100 gst_selector_pad_cache_buffer (selpad, buf);
1101 gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1103 selpad->pushed = TRUE;
1105 GST_INPUT_SELECTOR_UNLOCK (sel);
1109 if (prev_active_sinkpad)
1110 gst_object_unref (prev_active_sinkpad);
1111 prev_active_sinkpad = NULL;
1115 /* dropped buffers */
1118 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1120 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1121 /* when we drop a buffer, we're creating a discont on this pad */
1122 selpad->discont = TRUE;
1123 GST_INPUT_SELECTOR_UNLOCK (sel);
1124 gst_buffer_unref (buf);
1126 /* figure out what to return upstream */
1127 GST_OBJECT_LOCK (selpad);
1128 if (selpad->always_ok || !active_pad_pushed)
1131 res = GST_FLOW_NOT_LINKED;
1132 GST_OBJECT_UNLOCK (selpad);
1138 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1139 gst_buffer_unref (buf);
1140 res = GST_FLOW_FLUSHING;
1145 static void gst_input_selector_dispose (GObject * object);
1146 static void gst_input_selector_finalize (GObject * object);
1148 static void gst_input_selector_set_property (GObject * object,
1149 guint prop_id, const GValue * value, GParamSpec * pspec);
1150 static void gst_input_selector_get_property (GObject * object,
1151 guint prop_id, GValue * value, GParamSpec * pspec);
1153 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1154 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1155 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1157 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1158 element, GstStateChange transition);
1160 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1162 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
1164 static gint64 gst_input_selector_block (GstInputSelector * self);
1167 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1168 "input-selector", 0, "An input stream selector element");
1169 #define gst_input_selector_parent_class parent_class
1170 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1174 gst_input_selector_class_init (GstInputSelectorClass * klass)
1176 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1177 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1179 gobject_class->dispose = gst_input_selector_dispose;
1180 gobject_class->finalize = gst_input_selector_finalize;
1182 gobject_class->set_property = gst_input_selector_set_property;
1183 gobject_class->get_property = gst_input_selector_get_property;
1185 g_object_class_install_property (gobject_class, PROP_N_PADS,
1186 g_param_spec_uint ("n-pads", "Number of Pads",
1187 "The number of sink pads", 0, G_MAXUINT, 0,
1188 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1190 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1191 g_param_spec_object ("active-pad", "Active pad",
1192 "The currently active sink pad", GST_TYPE_PAD,
1193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1196 * GstInputSelector:sync-streams
1198 * If set to %TRUE all inactive streams will be synced to the
1199 * running time of the active stream or to the current clock.
1201 * To make sure no buffers are dropped by input-selector
1202 * that might be needed when switching the active pad,
1203 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1205 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1206 g_param_spec_boolean ("sync-streams", "Sync Streams",
1207 "Synchronize inactive streams to the running time of the active "
1208 "stream or to the current clock",
1209 DEFAULT_SYNC_STREAMS,
1210 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1211 GST_PARAM_MUTABLE_READY));
1214 * GstInputSelector:sync-mode
1216 * Select how input-selector will sync buffers when in sync-streams mode.
1218 * Note that when using the "active-segment" mode, the "active-segment" may
1219 * be ahead of current clock time when switching the active pad, as the current
1220 * active pad may have pushed more buffers than what was displayed/consumed,
1221 * which may cause delays and some missing buffers.
1223 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1224 g_param_spec_enum ("sync-mode", "Sync mode",
1225 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1227 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1228 GST_PARAM_MUTABLE_READY));
1231 * GstInputSelector:cache-buffers
1233 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1234 * the active pad will cache the buffers still considered valid (after current
1235 * running time, see sync-mode) to avoid missing frames if/when the pad is
1238 * The active pad may push more buffers than what is currently displayed/consumed
1239 * and when changing pads those buffers will be discarded and the only way to
1240 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1242 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1243 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1244 "Cache buffers for active-pad",
1245 DEFAULT_CACHE_BUFFERS,
1246 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1247 GST_PARAM_MUTABLE_READY));
1250 * GstInputSelector::block:
1251 * @inputselector: the #GstInputSelector
1253 * Block all sink pads in preparation for a switch. Returns the stop time of
1254 * the current switch segment, as a running time, or 0 if there is no current
1255 * active pad or the current active pad never received data.
1257 gst_input_selector_signals[SIGNAL_BLOCK] =
1258 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1259 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1260 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1261 g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1263 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1264 "Generic", "N-to-1 input stream selector",
1265 "Julien Moutte <julien@moutte.net>, "
1266 "Jan Schmidt <thaytan@mad.scientist.com>, "
1267 "Wim Taymans <wim.taymans@gmail.com>");
1268 gst_element_class_add_pad_template (gstelement_class,
1269 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1270 gst_element_class_add_pad_template (gstelement_class,
1271 gst_static_pad_template_get (&gst_input_selector_src_factory));
1273 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1274 gstelement_class->release_pad = gst_input_selector_release_pad;
1275 gstelement_class->change_state = gst_input_selector_change_state;
1277 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1281 gst_input_selector_init (GstInputSelector * sel)
1283 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1284 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1285 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1286 gst_pad_set_query_function (sel->srcpad,
1287 GST_DEBUG_FUNCPTR (gst_input_selector_query));
1288 gst_pad_set_event_function (sel->srcpad,
1289 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1290 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1291 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1292 /* sinkpad management */
1293 sel->active_sinkpad = NULL;
1295 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1296 sel->have_group_id = TRUE;
1298 g_mutex_init (&sel->lock);
1299 g_cond_init (&sel->cond);
1300 sel->blocked = FALSE;
1302 /* lets give a change for downstream to do something on
1303 * active-pad change before we start pushing new buffers */
1304 g_signal_connect_data (sel, "notify::active-pad",
1305 (GCallback) gst_input_selector_active_pad_changed, NULL,
1306 NULL, G_CONNECT_AFTER);
1310 gst_input_selector_dispose (GObject * object)
1312 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1314 if (sel->active_sinkpad) {
1315 gst_object_unref (sel->active_sinkpad);
1316 sel->active_sinkpad = NULL;
1318 G_OBJECT_CLASS (parent_class)->dispose (object);
1322 gst_input_selector_finalize (GObject * object)
1324 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1326 g_mutex_clear (&sel->lock);
1327 g_cond_clear (&sel->cond);
1329 G_OBJECT_CLASS (parent_class)->finalize (object);
1332 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1333 * active pad changed. */
1335 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1337 GstSelectorPad *old, *new;
1338 GstPad **active_pad_p;
1340 if (pad == self->active_sinkpad)
1343 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1344 new = GST_SELECTOR_PAD_CAST (pad);
1346 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1347 GST_DEBUG_PAD_NAME (new));
1350 old->pushed = FALSE;
1352 new->pushed = FALSE;
1354 /* Send a new SEGMENT event on the new pad next */
1355 if (old != new && new)
1356 new->events_pending = TRUE;
1358 active_pad_p = &self->active_sinkpad;
1359 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1361 if (old && old != new)
1362 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1364 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1366 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1367 self->active_sinkpad);
1373 gst_input_selector_set_property (GObject * object, guint prop_id,
1374 const GValue * value, GParamSpec * pspec)
1376 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1379 case PROP_ACTIVE_PAD:
1383 pad = g_value_get_object (value);
1385 GST_INPUT_SELECTOR_LOCK (sel);
1387 #if DEBUG_CACHED_BUFFERS
1388 gst_input_selector_debug_cached_buffers (sel);
1391 gst_input_selector_set_active_pad (sel, pad);
1393 #if DEBUG_CACHED_BUFFERS
1394 gst_input_selector_debug_cached_buffers (sel);
1397 GST_INPUT_SELECTOR_UNLOCK (sel);
1400 case PROP_SYNC_STREAMS:
1401 GST_INPUT_SELECTOR_LOCK (sel);
1402 sel->sync_streams = g_value_get_boolean (value);
1403 GST_INPUT_SELECTOR_UNLOCK (sel);
1405 case PROP_SYNC_MODE:
1406 GST_INPUT_SELECTOR_LOCK (sel);
1407 sel->sync_mode = g_value_get_enum (value);
1408 GST_INPUT_SELECTOR_UNLOCK (sel);
1410 case PROP_CACHE_BUFFERS:
1411 GST_INPUT_SELECTOR_LOCK (object);
1412 sel->cache_buffers = g_value_get_boolean (value);
1413 GST_INPUT_SELECTOR_UNLOCK (object);
1416 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1422 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1423 GParamSpec * pspec, gpointer user_data)
1425 /* Wake up all non-active pads in sync mode, they might be
1426 * the active pad now */
1427 if (sel->sync_streams)
1428 GST_INPUT_SELECTOR_BROADCAST (sel);
1432 gst_input_selector_get_property (GObject * object, guint prop_id,
1433 GValue * value, GParamSpec * pspec)
1435 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1439 GST_INPUT_SELECTOR_LOCK (object);
1440 g_value_set_uint (value, sel->n_pads);
1441 GST_INPUT_SELECTOR_UNLOCK (object);
1443 case PROP_ACTIVE_PAD:
1444 GST_INPUT_SELECTOR_LOCK (object);
1445 g_value_set_object (value, sel->active_sinkpad);
1446 GST_INPUT_SELECTOR_UNLOCK (object);
1448 case PROP_SYNC_STREAMS:
1449 GST_INPUT_SELECTOR_LOCK (object);
1450 g_value_set_boolean (value, sel->sync_streams);
1451 GST_INPUT_SELECTOR_UNLOCK (object);
1453 case PROP_SYNC_MODE:
1454 GST_INPUT_SELECTOR_LOCK (object);
1455 g_value_set_enum (value, sel->sync_mode);
1456 GST_INPUT_SELECTOR_UNLOCK (object);
1458 case PROP_CACHE_BUFFERS:
1459 GST_INPUT_SELECTOR_LOCK (object);
1460 g_value_set_boolean (value, sel->cache_buffers);
1461 GST_INPUT_SELECTOR_UNLOCK (object);
1464 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1470 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1473 GstPad *otherpad = NULL;
1475 GST_INPUT_SELECTOR_LOCK (sel);
1476 if (pad == sel->srcpad)
1477 otherpad = sel->active_sinkpad;
1478 else if (pad == sel->active_sinkpad || !strict)
1479 otherpad = sel->srcpad;
1481 gst_object_ref (otherpad);
1482 GST_INPUT_SELECTOR_UNLOCK (sel);
1488 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1490 GstInputSelector *sel;
1491 gboolean result = FALSE;
1493 gboolean done = FALSE;
1494 GValue item = { 0, };
1496 GList *pushed_pads = NULL;
1498 sel = GST_INPUT_SELECTOR (parent);
1499 /* Send upstream events to all sinkpads */
1500 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1502 /* This is now essentially a copy of gst_pad_event_default_dispatch
1503 * with a different iterator */
1505 switch (gst_iterator_next (iter, &item)) {
1506 case GST_ITERATOR_OK:
1507 eventpad = g_value_get_object (&item);
1509 /* if already pushed, skip */
1510 if (g_list_find (pushed_pads, eventpad)) {
1511 g_value_reset (&item);
1515 gst_event_ref (event);
1516 result |= gst_pad_push_event (eventpad, event);
1518 g_value_reset (&item);
1520 case GST_ITERATOR_RESYNC:
1521 /* We don't reset the result here because we don't push the event
1522 * again on pads that got the event already and because we need
1523 * to consider the result of the previous pushes */
1524 gst_iterator_resync (iter);
1526 case GST_ITERATOR_ERROR:
1527 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1530 case GST_ITERATOR_DONE:
1535 g_value_unset (&item);
1536 gst_iterator_free (iter);
1538 g_list_free (pushed_pads);
1540 gst_event_unref (event);
1545 /* query on the srcpad. We override this function because by default it will
1546 * only forward the query to one random sinkpad */
1548 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1550 gboolean res = FALSE;
1551 GstInputSelector *sel;
1553 sel = GST_INPUT_SELECTOR (parent);
1555 switch (GST_QUERY_TYPE (query)) {
1556 case GST_QUERY_LATENCY:
1559 GstClockTime resmin, resmax;
1566 /* perform the query on all sinkpads and combine the results. We take the
1567 * max of min and the min of max for the result latency. */
1568 GST_INPUT_SELECTOR_LOCK (sel);
1569 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1570 walk = g_list_next (walk)) {
1571 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1573 if (gst_pad_peer_query (sinkpad, query)) {
1574 GstClockTime min, max;
1577 /* one query succeeded, we succeed too */
1580 gst_query_parse_latency (query, &live, &min, &max);
1582 GST_DEBUG_OBJECT (sinkpad,
1583 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1584 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1591 else if (max < resmax)
1593 if (reslive == FALSE)
1598 GST_INPUT_SELECTOR_UNLOCK (sel);
1600 gst_query_set_latency (query, reslive, resmin, resmax);
1602 GST_DEBUG_OBJECT (sel,
1603 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1604 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1611 res = gst_pad_query_default (pad, parent, query);
1618 /* check if the pad is the active sinkpad */
1619 static inline gboolean
1620 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1624 GST_INPUT_SELECTOR_LOCK (sel);
1625 res = (pad == sel->active_sinkpad);
1626 GST_INPUT_SELECTOR_UNLOCK (sel);
1631 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1633 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1635 GstPad *active_sinkpad;
1636 GstSelectorPad *selpad;
1638 selpad = GST_SELECTOR_PAD_CAST (pad);
1640 selpad->active = TRUE;
1641 active_sinkpad = sel->active_sinkpad;
1642 if (sel->active_sinkpad == NULL) {
1643 GValue item = G_VALUE_INIT;
1644 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1645 GstIteratorResult ires;
1647 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1648 gst_iterator_resync (iter);
1649 if (ires == GST_ITERATOR_OK) {
1650 /* If no pad is currently selected, we return the first usable pad to
1651 * guarantee consistency */
1653 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1654 g_value_reset (&item);
1655 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1656 GST_DEBUG_PAD_NAME (active_sinkpad));
1658 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1659 gst_iterator_free (iter);
1662 return active_sinkpad;
1666 gst_input_selector_request_new_pad (GstElement * element,
1667 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1669 GstInputSelector *sel;
1671 GstPad *sinkpad = NULL;
1673 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1675 sel = GST_INPUT_SELECTOR (element);
1677 GST_INPUT_SELECTOR_LOCK (sel);
1679 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1680 name = g_strdup_printf ("sink_%u", sel->padcount++);
1681 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1682 "name", name, "direction", templ->direction, "template", templ, NULL);
1687 gst_pad_set_event_function (sinkpad,
1688 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1689 gst_pad_set_query_function (sinkpad,
1690 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1691 gst_pad_set_chain_function (sinkpad,
1692 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1693 gst_pad_set_iterate_internal_links_function (sinkpad,
1694 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1696 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1697 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1698 gst_pad_set_active (sinkpad, TRUE);
1699 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1700 GST_INPUT_SELECTOR_UNLOCK (sel);
1706 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1708 GstInputSelector *sel;
1710 sel = GST_INPUT_SELECTOR (element);
1711 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1713 GST_INPUT_SELECTOR_LOCK (sel);
1714 /* if the pad was the active pad, makes us select a new one */
1715 if (sel->active_sinkpad == pad) {
1716 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1717 gst_object_unref (sel->active_sinkpad);
1718 sel->active_sinkpad = NULL;
1721 GST_INPUT_SELECTOR_UNLOCK (sel);
1723 gst_pad_set_active (pad, FALSE);
1724 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1728 gst_input_selector_reset (GstInputSelector * sel)
1732 GST_INPUT_SELECTOR_LOCK (sel);
1733 /* clear active pad */
1734 if (sel->active_sinkpad) {
1735 gst_object_unref (sel->active_sinkpad);
1736 sel->active_sinkpad = NULL;
1738 /* reset each of our sinkpads state */
1739 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1740 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1742 gst_selector_pad_reset (selpad);
1745 gst_tag_list_unref (selpad->tags);
1746 selpad->tags = NULL;
1749 sel->have_group_id = TRUE;
1750 GST_INPUT_SELECTOR_UNLOCK (sel);
1753 static GstStateChangeReturn
1754 gst_input_selector_change_state (GstElement * element,
1755 GstStateChange transition)
1757 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1758 GstStateChangeReturn result;
1760 switch (transition) {
1761 case GST_STATE_CHANGE_READY_TO_PAUSED:
1762 GST_INPUT_SELECTOR_LOCK (self);
1763 self->blocked = FALSE;
1764 self->flushing = FALSE;
1765 GST_INPUT_SELECTOR_UNLOCK (self);
1767 case GST_STATE_CHANGE_PAUSED_TO_READY:
1768 /* first unlock before we call the parent state change function, which
1769 * tries to acquire the stream lock when going to ready. */
1770 GST_INPUT_SELECTOR_LOCK (self);
1771 self->blocked = FALSE;
1772 self->flushing = TRUE;
1773 GST_INPUT_SELECTOR_BROADCAST (self);
1774 GST_INPUT_SELECTOR_UNLOCK (self);
1780 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1782 switch (transition) {
1783 case GST_STATE_CHANGE_PAUSED_TO_READY:
1784 gst_input_selector_reset (self);
1794 gst_input_selector_block (GstInputSelector * self)
1797 GstSelectorPad *spad;
1799 GST_INPUT_SELECTOR_LOCK (self);
1802 GST_WARNING_OBJECT (self, "switch already blocked");
1804 self->blocked = TRUE;
1805 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1808 ret = gst_selector_pad_get_running_time (spad);
1810 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1812 GST_INPUT_SELECTOR_UNLOCK (self);