1 /* GStreamer input selector
2 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4 * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
5 * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
6 * Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
7 * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
8 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
27 * SECTION:element-input-selector
28 * @see_also: #GstOutputSelector
30 * Direct one out of N input streams to the output pad.
32 * The input pads are from a GstPad subclass and have additional
33 * properties, which users may find useful, namely:
37 * "running-time": Running time of stream on pad (#gint64)
40 * "tags": The currently active tags on the pad (#GstTagList, boxed type)
43 * "active": If the pad is currently active (#gboolean)
46 * "always-ok" : Make an inactive pads return #GST_FLOW_OK instead of
47 * #GST_FLOW_NOT_LINKED
58 #include "gstinputselector.h"
60 #define DEBUG_CACHED_BUFFERS 0
62 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
63 #define GST_CAT_DEFAULT input_selector_debug
65 #define GST_TYPE_INPUT_SELECTOR_SYNC_MODE (gst_input_selector_sync_mode_get_type())
67 gst_input_selector_sync_mode_get_type (void)
69 static GType type = 0;
70 static const GEnumValue data[] = {
71 {GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT,
72 "Sync using the current active segment",
74 {GST_INPUT_SELECTOR_SYNC_MODE_CLOCK, "Sync using the clock", "clock"},
79 type = g_enum_register_static ("GstInputSelectorSyncMode", data);
84 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (&((GstInputSelector*)(sel))->lock)
85 #define GST_INPUT_SELECTOR_GET_COND(sel) (&((GstInputSelector*)(sel))->cond)
86 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
87 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
88 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
89 GST_INPUT_SELECTOR_GET_LOCK(sel)))
90 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
92 static GstStaticPadTemplate gst_input_selector_sink_factory =
93 GST_STATIC_PAD_TEMPLATE ("sink_%u",
98 static GstStaticPadTemplate gst_input_selector_src_factory =
99 GST_STATIC_PAD_TEMPLATE ("src",
102 GST_STATIC_CAPS_ANY);
114 #define DEFAULT_SYNC_STREAMS TRUE
115 #define DEFAULT_SYNC_MODE GST_INPUT_SELECTOR_SYNC_MODE_ACTIVE_SEGMENT
116 #define DEFAULT_CACHE_BUFFERS FALSE
117 #define DEFAULT_PAD_ALWAYS_OK TRUE
122 PROP_PAD_RUNNING_TIME,
135 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
137 static void gst_input_selector_active_pad_changed (GstInputSelector * sel,
138 GParamSpec * pspec, gpointer user_data);
139 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
141 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
143 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
144 GstPad * pad, gboolean strict);
146 #define GST_TYPE_SELECTOR_PAD \
147 (gst_selector_pad_get_type())
148 #define GST_SELECTOR_PAD(obj) \
149 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
150 #define GST_SELECTOR_PAD_CLASS(klass) \
151 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
152 #define GST_IS_SELECTOR_PAD(obj) \
153 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
154 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
155 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
156 #define GST_SELECTOR_PAD_CAST(obj) \
157 ((GstSelectorPad *)(obj))
159 typedef struct _GstSelectorPad GstSelectorPad;
160 typedef struct _GstSelectorPadClass GstSelectorPadClass;
161 typedef struct _GstSelectorPadCachedBuffer GstSelectorPadCachedBuffer;
163 struct _GstSelectorPad
167 gboolean active; /* when buffer have passed the pad */
168 gboolean pushed; /* when buffer was pushed downstream since activation */
169 gboolean eos; /* when EOS has been received */
170 gboolean eos_sent; /* when EOS was sent downstream */
171 gboolean discont; /* after switching we create a discont */
172 gboolean flushing; /* set after flush-start and before flush-stop */
174 GstTagList *tags; /* last tags received on the pad */
176 GstSegment segment; /* the current segment on the pad */
177 guint32 segment_seqnum; /* sequence number of the current segment */
179 gboolean events_pending; /* TRUE if sticky events need to be updated */
181 gboolean sending_cached_buffers;
182 GQueue *cached_buffers;
185 struct _GstSelectorPadCachedBuffer
191 struct _GstSelectorPadClass
196 GType gst_selector_pad_get_type (void);
197 static void gst_selector_pad_finalize (GObject * object);
198 static void gst_selector_pad_get_property (GObject * object,
199 guint prop_id, GValue * value, GParamSpec * pspec);
200 static void gst_selector_pad_set_property (GObject * object,
201 guint prop_id, const GValue * value, GParamSpec * pspec);
203 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
204 static void gst_selector_pad_reset (GstSelectorPad * pad);
205 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
207 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
209 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
211 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
213 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
215 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
217 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
220 gst_selector_pad_class_init (GstSelectorPadClass * klass)
222 GObjectClass *gobject_class;
224 gobject_class = (GObjectClass *) klass;
226 gobject_class->finalize = gst_selector_pad_finalize;
228 gobject_class->get_property = gst_selector_pad_get_property;
229 gobject_class->set_property = gst_selector_pad_set_property;
231 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
232 g_param_spec_int64 ("running-time", "Running time",
233 "Running time of stream on pad", 0, G_MAXINT64, 0,
234 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
235 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
236 g_param_spec_boxed ("tags", "Tags",
237 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
238 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
240 g_param_spec_boolean ("active", "Active",
241 "If the pad is currently active", FALSE,
242 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
243 /* FIXME: better property name? */
244 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
245 g_param_spec_boolean ("always-ok", "Always OK",
246 "Make an inactive pad return OK instead of NOT_LINKED",
247 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
251 gst_selector_pad_init (GstSelectorPad * pad)
253 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
254 gst_selector_pad_reset (pad);
258 gst_selector_pad_finalize (GObject * object)
262 pad = GST_SELECTOR_PAD_CAST (object);
265 gst_tag_list_unref (pad->tags);
266 gst_selector_pad_free_cached_buffers (pad);
268 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
272 gst_selector_pad_set_property (GObject * object, guint prop_id,
273 const GValue * value, GParamSpec * pspec)
275 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
278 case PROP_PAD_ALWAYS_OK:
279 GST_OBJECT_LOCK (object);
280 spad->always_ok = g_value_get_boolean (value);
281 GST_OBJECT_UNLOCK (object);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gst_selector_pad_get_property (GObject * object, guint prop_id,
291 GValue * value, GParamSpec * pspec)
293 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
296 case PROP_PAD_RUNNING_TIME:
297 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
300 GST_OBJECT_LOCK (object);
301 g_value_set_boxed (value, spad->tags);
302 GST_OBJECT_UNLOCK (object);
304 case PROP_PAD_ACTIVE:
306 GstInputSelector *sel;
308 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
310 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
311 GST_PAD_CAST (spad)));
312 gst_object_unref (sel);
314 g_value_set_boolean (value, FALSE);
318 case PROP_PAD_ALWAYS_OK:
319 GST_OBJECT_LOCK (object);
320 g_value_set_boolean (value, spad->always_ok);
321 GST_OBJECT_UNLOCK (object);
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330 gst_selector_pad_get_running_time (GstSelectorPad * pad)
334 GST_OBJECT_LOCK (pad);
337 gst_segment_to_running_time (&pad->segment, pad->segment.format,
338 pad->segment.position);
340 GST_OBJECT_UNLOCK (pad);
342 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
343 " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
348 /* must be called with the SELECTOR_LOCK */
350 gst_selector_pad_reset (GstSelectorPad * pad)
352 GST_OBJECT_LOCK (pad);
356 pad->eos_sent = FALSE;
357 pad->events_pending = FALSE;
358 pad->discont = FALSE;
359 pad->flushing = FALSE;
360 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
361 pad->sending_cached_buffers = FALSE;
362 gst_selector_pad_free_cached_buffers (pad);
363 GST_OBJECT_UNLOCK (pad);
366 static GstSelectorPadCachedBuffer *
367 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
369 GstSelectorPadCachedBuffer *cached_buffer =
370 g_slice_new (GstSelectorPadCachedBuffer);
371 cached_buffer->buffer = buffer;
372 cached_buffer->segment = selpad->segment;
373 return cached_buffer;
377 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
379 gst_buffer_unref (cached_buffer->buffer);
380 g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
383 /* must be called with the SELECTOR_LOCK */
385 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
387 if (selpad->segment.format != GST_FORMAT_TIME) {
388 GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
389 "not caching", buffer);
393 GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
394 if (!selpad->cached_buffers)
395 selpad->cached_buffers = g_queue_new ();
396 g_queue_push_tail (selpad->cached_buffers,
397 gst_selector_pad_new_cached_buffer (selpad, buffer));
400 /* must be called with the SELECTOR_LOCK */
402 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
404 if (!selpad->cached_buffers)
407 GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
408 g_queue_free_full (selpad->cached_buffers,
409 (GDestroyNotify) gst_selector_pad_free_cached_buffer);
410 selpad->cached_buffers = NULL;
413 /* strictly get the linked pad from the sinkpad. If the pad is active we return
414 * the srcpad else we return NULL */
416 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
418 GstInputSelector *sel;
420 GstIterator *it = NULL;
423 sel = GST_INPUT_SELECTOR (parent);
425 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
427 g_value_init (&val, GST_TYPE_PAD);
428 g_value_set_object (&val, otherpad);
429 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
430 g_value_unset (&val);
431 gst_object_unref (otherpad);
438 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
442 gboolean new_tags = FALSE;
443 GstInputSelector *sel;
444 GstSelectorPad *selpad;
445 GstPad *prev_active_sinkpad;
446 GstPad *active_sinkpad;
448 sel = GST_INPUT_SELECTOR (parent);
449 selpad = GST_SELECTOR_PAD_CAST (pad);
450 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
452 GST_INPUT_SELECTOR_LOCK (sel);
453 prev_active_sinkpad =
454 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
455 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
456 GST_INPUT_SELECTOR_UNLOCK (sel);
458 if (prev_active_sinkpad != active_sinkpad) {
459 if (prev_active_sinkpad)
460 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
461 g_object_notify (G_OBJECT (active_sinkpad), "active");
462 g_object_notify (G_OBJECT (sel), "active-pad");
464 if (prev_active_sinkpad)
465 gst_object_unref (prev_active_sinkpad);
467 GST_INPUT_SELECTOR_LOCK (sel);
469 /* only forward if we are dealing with the active sinkpad */
470 forward = (pad == active_sinkpad);
472 switch (GST_EVENT_TYPE (event)) {
473 case GST_EVENT_STREAM_START:{
476 if (!gst_event_parse_group_id (event, &group_id))
477 sel->have_group_id = FALSE;
480 case GST_EVENT_FLUSH_START:
481 /* Unblock the pad if it's waiting */
482 selpad->flushing = TRUE;
483 GST_INPUT_SELECTOR_BROADCAST (sel);
485 case GST_EVENT_FLUSH_STOP:
486 gst_selector_pad_reset (selpad);
488 case GST_EVENT_SEGMENT:
490 gst_event_copy_segment (event, &selpad->segment);
491 selpad->segment_seqnum = gst_event_get_seqnum (event);
493 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
499 GstTagList *tags, *oldtags, *newtags;
501 gst_event_parse_tag (event, &tags);
503 GST_OBJECT_LOCK (selpad);
504 oldtags = selpad->tags;
506 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
507 selpad->tags = newtags;
508 GST_OBJECT_UNLOCK (selpad);
511 gst_tag_list_unref (oldtags);
512 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
521 selpad->eos_sent = TRUE;
523 GstSelectorPad *active_selpad;
525 /* If the active sinkpad is in EOS state but EOS
526 * was not sent downstream this means that the pad
527 * got EOS before it was set as active pad and that
528 * the previously active pad got EOS after it was
531 active_selpad = GST_SELECTOR_PAD (active_sinkpad);
532 forward = (active_selpad->eos && !active_selpad->eos_sent);
533 active_selpad->eos_sent = TRUE;
535 GST_DEBUG_OBJECT (pad, "received EOS");
538 GstClockTime ts, dur;
540 GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
542 gst_event_parse_gap (event, &ts, &dur);
543 if (GST_CLOCK_TIME_IS_VALID (ts)) {
544 if (GST_CLOCK_TIME_IS_VALID (dur))
547 /* update the segment position */
548 GST_OBJECT_LOCK (pad);
549 selpad->segment.position = ts;
550 GST_OBJECT_UNLOCK (pad);
551 if (sel->sync_streams && active_sinkpad == pad)
552 GST_INPUT_SELECTOR_BROADCAST (sel);
560 GST_INPUT_SELECTOR_UNLOCK (sel);
562 g_object_notify (G_OBJECT (selpad), "tags");
564 GST_DEBUG_OBJECT (pad, "forwarding event");
565 res = gst_pad_push_event (sel->srcpad, event);
567 /* If we aren't forwarding the event because the pad is not the
568 * active_sinkpad, then set the flag on the pad
569 * that says a segment needs sending if/when that pad is activated.
570 * For all other cases, we send the event immediately, which makes
571 * sparse streams and other segment updates work correctly downstream.
573 if (GST_EVENT_IS_STICKY (event))
574 selpad->events_pending = TRUE;
575 gst_event_unref (event);
582 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
584 gboolean res = FALSE;
585 GstInputSelector *self = (GstInputSelector *) parent;
587 switch (GST_QUERY_TYPE (query)) {
589 /* always proxy caps query, regardless of active pad or not */
590 res = gst_pad_peer_query (self->srcpad, query);
592 case GST_QUERY_ALLOCATION:{
593 GstPad *active_sinkpad;
594 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
596 /* Only do the allocation query for the active sinkpad,
597 * after switching a reconfigure event is sent and upstream
598 * should reconfigure and do a new allocation query
600 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
601 GST_INPUT_SELECTOR_LOCK (sel);
602 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
603 GST_INPUT_SELECTOR_UNLOCK (sel);
605 if (pad != active_sinkpad) {
613 res = gst_pad_query_default (pad, parent, query);
621 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
622 * or return TRUE when flushing */
624 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
626 while (self->blocked && !self->flushing && !pad->flushing) {
627 /* we can be unlocked here when we are shutting down (flushing) or when we
629 GST_INPUT_SELECTOR_WAIT (self);
631 return self->flushing;
635 gst_input_selector_get_clipped_running_time (GstSegment * seg, GstBuffer * buf)
637 GstClockTime running_time;
639 running_time = GST_BUFFER_PTS (buf);
640 /* If possible try to get the running time at the end of the buffer */
641 if (GST_BUFFER_DURATION_IS_VALID (buf))
642 running_time += GST_BUFFER_DURATION (buf);
643 /* Only use the segment to convert to running time if the segment is
644 * in TIME format, otherwise do our best to try to sync */
645 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
646 if (running_time > seg->stop) {
647 running_time = seg->stop;
650 return gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
653 /* must be called without the SELECTOR_LOCK, will wait until the running time
654 * of the active pad is after this pad or return TRUE when flushing */
656 gst_input_selector_wait_running_time (GstInputSelector * sel,
657 GstSelectorPad * selpad, GstBuffer * buf)
661 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
663 /* If we have no valid timestamp we can't sync this buffer */
664 if (!GST_BUFFER_PTS_IS_VALID (buf)) {
665 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
666 "invalid timestamp");
670 seg = &selpad->segment;
673 * a) this is the active pad
674 * b) the pad or the selector is flushing
675 * c) the selector is not blocked
676 * d) the buffer running time is before the current running time
677 * (either active-seg or clock, depending on sync-mode)
680 GST_INPUT_SELECTOR_LOCK (sel);
682 GstPad *active_sinkpad;
683 GstSelectorPad *active_selpad;
685 gint64 cur_running_time;
686 GstClockTime running_time;
689 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (selpad));
690 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
692 if (seg->format != GST_FORMAT_TIME) {
693 GST_DEBUG_OBJECT (selpad,
694 "Not waiting because we don't have a TIME segment");
695 GST_INPUT_SELECTOR_UNLOCK (sel);
699 running_time = gst_input_selector_get_clipped_running_time (seg, buf);
700 /* If this is outside the segment don't sync */
701 if (running_time == -1) {
702 GST_DEBUG_OBJECT (selpad,
703 "Not waiting because buffer is outside segment");
704 GST_INPUT_SELECTOR_UNLOCK (sel);
708 cur_running_time = GST_CLOCK_TIME_NONE;
709 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
710 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
712 GstClockTime base_time;
714 cur_running_time = gst_clock_get_time (clock);
715 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
716 if (base_time <= cur_running_time)
717 cur_running_time -= base_time;
719 cur_running_time = 0;
721 gst_object_unref (clock);
724 GstSegment *active_seg;
726 active_seg = &active_selpad->segment;
728 /* If the active segment is configured but not to time format
729 * we can't do any syncing at all */
730 if (active_seg->format != GST_FORMAT_TIME
731 && active_seg->format != GST_FORMAT_UNDEFINED) {
732 GST_DEBUG_OBJECT (selpad,
733 "Not waiting because active segment isn't in TIME format");
734 GST_INPUT_SELECTOR_UNLOCK (sel);
738 /* Get active pad's running time, if no configured segment yet keep at -1 */
739 if (active_seg->format == GST_FORMAT_TIME)
740 cur_running_time = gst_segment_to_running_time (active_seg,
741 GST_FORMAT_TIME, active_seg->position);
744 if (selpad != active_selpad && !sel->flushing && !selpad->flushing &&
745 (sel->blocked || cur_running_time == GST_CLOCK_TIME_NONE
746 || running_time >= cur_running_time)) {
748 GST_DEBUG_OBJECT (selpad,
749 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
750 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
751 GST_TIME_ARGS (cur_running_time));
753 GST_DEBUG_OBJECT (selpad, "Waiting for selector to unblock");
755 GST_INPUT_SELECTOR_WAIT (sel);
757 GST_INPUT_SELECTOR_UNLOCK (sel);
762 /* Return TRUE if the selector or the pad is flushing */
763 return (sel->flushing || selpad->flushing);
767 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
769 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
771 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
772 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
775 e = gst_event_new_segment (seg);
776 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
778 gst_pad_push_event (sel->srcpad, e);
779 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
780 && !sel->have_group_id) {
782 gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
784 /* Only push stream-start once if not all our streams have a stream-id */
786 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
788 gst_event_unref (tmp);
791 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
797 #if DEBUG_CACHED_BUFFERS
799 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
803 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
804 GstSelectorPad *selpad;
809 selpad = GST_SELECTOR_PAD_CAST (walk->data);
810 if (!selpad->cached_buffers) {
811 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
815 timestamps = g_string_new ("Cached buffers timestamps:");
816 for (i = 0; i < selpad->cached_buffers->length; ++i) {
817 GstSelectorPadCachedBuffer *cached_buffer;
819 cached_buffer = g_queue_peek_nth (selpad->cached_buffers, i);
820 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
821 GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
823 str = g_string_free (timestamps, FALSE);
824 GST_DEBUG_OBJECT (selpad, "%s", str);
830 /* must be called with the SELECTOR_LOCK */
832 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
836 gint64 cur_running_time;
839 cur_running_time = GST_CLOCK_TIME_NONE;
840 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
841 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
843 GstClockTime base_time;
845 cur_running_time = gst_clock_get_time (clock);
846 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
847 if (base_time <= cur_running_time)
848 cur_running_time -= base_time;
850 cur_running_time = 0;
852 gst_object_unref (clock);
855 GstPad *active_sinkpad;
856 GstSelectorPad *active_selpad;
857 GstSegment *active_seg;
859 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
860 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
861 active_seg = &active_selpad->segment;
863 /* Get active pad's running time, if no configured segment yet keep at -1 */
864 if (active_seg->format == GST_FORMAT_TIME)
865 cur_running_time = gst_segment_to_running_time (active_seg,
866 GST_FORMAT_TIME, active_seg->position);
869 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
872 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
873 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
874 GstSelectorPad *selpad;
876 GstSelectorPadCachedBuffer *cached_buffer;
877 GSList *maybe_remove;
878 guint queue_position;
880 selpad = GST_SELECTOR_PAD_CAST (walk->data);
881 if (!selpad->cached_buffers)
884 seg = &selpad->segment;
888 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
890 GstBuffer *buffer = cached_buffer->buffer;
891 GstClockTime running_time;
894 /* If we have no valid timestamp we can't sync this buffer */
895 if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
896 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
897 queue_position = g_slist_length (maybe_remove);
901 /* the buffer is still valid if its duration is valid and the
902 * timestamp + duration is >= time, or if its duration is invalid
903 * and the timestamp is >= time */
904 running_time = gst_input_selector_get_clipped_running_time (seg, buffer);
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;
953 sel = GST_INPUT_SELECTOR (parent);
954 selpad = GST_SELECTOR_PAD_CAST (pad);
956 GST_DEBUG_OBJECT (selpad,
957 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
958 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
960 GST_INPUT_SELECTOR_LOCK (sel);
961 /* wait or check for flushing */
962 if (gst_input_selector_wait (sel, selpad)) {
963 GST_INPUT_SELECTOR_UNLOCK (sel);
967 GST_LOG_OBJECT (pad, "getting active pad");
969 prev_active_sinkpad =
970 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
971 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
973 /* In sync mode wait until the active pad has advanced
974 * after the running time of the current buffer */
975 if (sel->sync_streams) {
976 /* call chain for each cached buffer if we are not the active pad
977 * or if we are the active pad but didn't push anything yet. */
978 if (active_sinkpad != pad || !selpad->pushed) {
979 /* no need to check for sel->cache_buffers as selpad->cached_buffers
980 * will only be valid if cache_buffers is TRUE */
981 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
982 GstSelectorPadCachedBuffer *cached_buffer;
983 GstSegment saved_segment;
985 saved_segment = selpad->segment;
987 selpad->sending_cached_buffers = TRUE;
988 while (!sel->flushing && !selpad->flushing &&
989 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
990 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
991 "invoking chain for cached buffer %p", cached_buffer->buffer);
993 selpad->segment = cached_buffer->segment;
994 selpad->events_pending = TRUE;
995 GST_INPUT_SELECTOR_UNLOCK (sel);
996 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
997 GST_INPUT_SELECTOR_LOCK (sel);
999 /* we may have cleaned up the queue in the meantime because of
1001 if (!selpad->cached_buffers) {
1005 selpad->sending_cached_buffers = FALSE;
1007 /* all cached buffers sent, restore segment for current buffer */
1008 selpad->segment = saved_segment;
1009 selpad->events_pending = TRUE;
1011 /* Might have changed while calling chain for cached buffers */
1012 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1016 if (active_sinkpad != pad) {
1017 GST_INPUT_SELECTOR_UNLOCK (sel);
1018 if (gst_input_selector_wait_running_time (sel, selpad, buf))
1020 GST_INPUT_SELECTOR_LOCK (sel);
1023 /* Might have changed while waiting */
1024 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
1027 /* update the segment on the srcpad */
1028 if (GST_BUFFER_PTS_IS_VALID (buf)) {
1029 GstClockTime start_time = GST_BUFFER_PTS (buf);
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->segment.position = start_time;
1039 GST_OBJECT_UNLOCK (pad);
1042 /* Ignore buffers from pads except the selected one */
1043 if (pad != active_sinkpad)
1046 /* Tell all non-active pads that we advanced the running time */
1047 if (sel->sync_streams)
1048 GST_INPUT_SELECTOR_BROADCAST (sel);
1050 GST_INPUT_SELECTOR_UNLOCK (sel);
1052 if (prev_active_sinkpad != active_sinkpad) {
1053 if (prev_active_sinkpad)
1054 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1055 g_object_notify (G_OBJECT (active_sinkpad), "active");
1056 g_object_notify (G_OBJECT (sel), "active-pad");
1059 /* if we have a pending events, push them now */
1060 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1061 || selpad->events_pending)) {
1062 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1064 selpad->events_pending = FALSE;
1067 if (prev_active_sinkpad) {
1068 gst_object_unref (prev_active_sinkpad);
1069 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_PTS (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 | GST_PARAM_MUTABLE_PLAYING |
1194 G_PARAM_STATIC_STRINGS));
1197 * GstInputSelector:sync-streams
1199 * If set to %TRUE all inactive streams will be synced to the
1200 * running time of the active stream or to the current clock.
1202 * To make sure no buffers are dropped by input-selector
1203 * that might be needed when switching the active pad,
1204 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1206 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1207 g_param_spec_boolean ("sync-streams", "Sync Streams",
1208 "Synchronize inactive streams to the running time of the active "
1209 "stream or to the current clock",
1210 DEFAULT_SYNC_STREAMS,
1211 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1212 GST_PARAM_MUTABLE_READY));
1215 * GstInputSelector:sync-mode
1217 * Select how input-selector will sync buffers when in sync-streams mode.
1219 * Note that when using the "active-segment" mode, the "active-segment" may
1220 * be ahead of current clock time when switching the active pad, as the current
1221 * active pad may have pushed more buffers than what was displayed/consumed,
1222 * which may cause delays and some missing buffers.
1224 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1225 g_param_spec_enum ("sync-mode", "Sync mode",
1226 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1228 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1229 GST_PARAM_MUTABLE_READY));
1232 * GstInputSelector:cache-buffers
1234 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1235 * the active pad will cache the buffers still considered valid (after current
1236 * running time, see sync-mode) to avoid missing frames if/when the pad is
1239 * The active pad may push more buffers than what is currently displayed/consumed
1240 * and when changing pads those buffers will be discarded and the only way to
1241 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1243 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1244 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1245 "Cache buffers for active-pad",
1246 DEFAULT_CACHE_BUFFERS,
1247 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1248 GST_PARAM_MUTABLE_READY));
1251 * GstInputSelector::block:
1252 * @inputselector: the #GstInputSelector
1254 * Block all sink pads in preparation for a switch. Returns the stop time of
1255 * the current switch segment, as a running time, or 0 if there is no current
1256 * active pad or the current active pad never received data.
1258 gst_input_selector_signals[SIGNAL_BLOCK] =
1259 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
1260 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1261 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
1262 g_cclosure_marshal_generic, G_TYPE_INT64, 0);
1264 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1265 "Generic", "N-to-1 input stream selector",
1266 "Julien Moutte <julien@moutte.net>, "
1267 "Jan Schmidt <thaytan@mad.scientist.com>, "
1268 "Wim Taymans <wim.taymans@gmail.com>");
1269 gst_element_class_add_pad_template (gstelement_class,
1270 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1271 gst_element_class_add_pad_template (gstelement_class,
1272 gst_static_pad_template_get (&gst_input_selector_src_factory));
1274 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1275 gstelement_class->release_pad = gst_input_selector_release_pad;
1276 gstelement_class->change_state = gst_input_selector_change_state;
1278 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
1282 gst_input_selector_init (GstInputSelector * sel)
1284 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1285 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1286 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1287 gst_pad_set_query_function (sel->srcpad,
1288 GST_DEBUG_FUNCPTR (gst_input_selector_query));
1289 gst_pad_set_event_function (sel->srcpad,
1290 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1291 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1292 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1293 /* sinkpad management */
1294 sel->active_sinkpad = NULL;
1296 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1297 sel->have_group_id = TRUE;
1299 g_mutex_init (&sel->lock);
1300 g_cond_init (&sel->cond);
1301 sel->blocked = FALSE;
1303 /* lets give a change for downstream to do something on
1304 * active-pad change before we start pushing new buffers */
1305 g_signal_connect_data (sel, "notify::active-pad",
1306 (GCallback) gst_input_selector_active_pad_changed, NULL,
1307 NULL, G_CONNECT_AFTER);
1311 gst_input_selector_dispose (GObject * object)
1313 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1315 if (sel->active_sinkpad) {
1316 gst_object_unref (sel->active_sinkpad);
1317 sel->active_sinkpad = NULL;
1319 G_OBJECT_CLASS (parent_class)->dispose (object);
1323 gst_input_selector_finalize (GObject * object)
1325 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1327 g_mutex_clear (&sel->lock);
1328 g_cond_clear (&sel->cond);
1330 G_OBJECT_CLASS (parent_class)->finalize (object);
1333 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1334 * active pad changed. */
1336 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1338 GstSelectorPad *old, *new;
1339 GstPad **active_pad_p;
1341 if (pad == self->active_sinkpad)
1344 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1345 new = GST_SELECTOR_PAD_CAST (pad);
1347 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1348 GST_DEBUG_PAD_NAME (new));
1351 old->pushed = FALSE;
1353 new->pushed = FALSE;
1355 /* Send a new SEGMENT event on the new pad next */
1356 if (old != new && new)
1357 new->events_pending = TRUE;
1359 active_pad_p = &self->active_sinkpad;
1360 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1362 if (old && old != new)
1363 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1365 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1367 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1368 self->active_sinkpad);
1374 gst_input_selector_set_property (GObject * object, guint prop_id,
1375 const GValue * value, GParamSpec * pspec)
1377 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1380 case PROP_ACTIVE_PAD:
1384 pad = g_value_get_object (value);
1386 GST_INPUT_SELECTOR_LOCK (sel);
1388 #if DEBUG_CACHED_BUFFERS
1389 gst_input_selector_debug_cached_buffers (sel);
1392 gst_input_selector_set_active_pad (sel, pad);
1394 #if DEBUG_CACHED_BUFFERS
1395 gst_input_selector_debug_cached_buffers (sel);
1398 GST_INPUT_SELECTOR_UNLOCK (sel);
1401 case PROP_SYNC_STREAMS:
1402 GST_INPUT_SELECTOR_LOCK (sel);
1403 sel->sync_streams = g_value_get_boolean (value);
1404 GST_INPUT_SELECTOR_UNLOCK (sel);
1406 case PROP_SYNC_MODE:
1407 GST_INPUT_SELECTOR_LOCK (sel);
1408 sel->sync_mode = g_value_get_enum (value);
1409 GST_INPUT_SELECTOR_UNLOCK (sel);
1411 case PROP_CACHE_BUFFERS:
1412 GST_INPUT_SELECTOR_LOCK (object);
1413 sel->cache_buffers = g_value_get_boolean (value);
1414 GST_INPUT_SELECTOR_UNLOCK (object);
1417 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1423 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1424 GParamSpec * pspec, gpointer user_data)
1426 /* Wake up all non-active pads in sync mode, they might be
1427 * the active pad now */
1428 if (sel->sync_streams)
1429 GST_INPUT_SELECTOR_BROADCAST (sel);
1433 gst_input_selector_get_property (GObject * object, guint prop_id,
1434 GValue * value, GParamSpec * pspec)
1436 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1440 GST_INPUT_SELECTOR_LOCK (object);
1441 g_value_set_uint (value, sel->n_pads);
1442 GST_INPUT_SELECTOR_UNLOCK (object);
1444 case PROP_ACTIVE_PAD:
1445 GST_INPUT_SELECTOR_LOCK (object);
1446 g_value_set_object (value, sel->active_sinkpad);
1447 GST_INPUT_SELECTOR_UNLOCK (object);
1449 case PROP_SYNC_STREAMS:
1450 GST_INPUT_SELECTOR_LOCK (object);
1451 g_value_set_boolean (value, sel->sync_streams);
1452 GST_INPUT_SELECTOR_UNLOCK (object);
1454 case PROP_SYNC_MODE:
1455 GST_INPUT_SELECTOR_LOCK (object);
1456 g_value_set_enum (value, sel->sync_mode);
1457 GST_INPUT_SELECTOR_UNLOCK (object);
1459 case PROP_CACHE_BUFFERS:
1460 GST_INPUT_SELECTOR_LOCK (object);
1461 g_value_set_boolean (value, sel->cache_buffers);
1462 GST_INPUT_SELECTOR_UNLOCK (object);
1465 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1471 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1474 GstPad *otherpad = NULL;
1476 GST_INPUT_SELECTOR_LOCK (sel);
1477 if (pad == sel->srcpad)
1478 otherpad = sel->active_sinkpad;
1479 else if (pad == sel->active_sinkpad || !strict)
1480 otherpad = sel->srcpad;
1482 gst_object_ref (otherpad);
1483 GST_INPUT_SELECTOR_UNLOCK (sel);
1489 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1491 GstInputSelector *sel;
1492 gboolean result = FALSE;
1494 gboolean done = FALSE;
1495 GValue item = { 0, };
1497 GList *pushed_pads = NULL;
1499 sel = GST_INPUT_SELECTOR (parent);
1500 /* Send upstream events to all sinkpads */
1501 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1503 /* This is now essentially a copy of gst_pad_event_default_dispatch
1504 * with a different iterator */
1506 switch (gst_iterator_next (iter, &item)) {
1507 case GST_ITERATOR_OK:
1508 eventpad = g_value_get_object (&item);
1510 /* if already pushed, skip */
1511 if (g_list_find (pushed_pads, eventpad)) {
1512 g_value_reset (&item);
1516 gst_event_ref (event);
1517 result |= gst_pad_push_event (eventpad, event);
1519 g_value_reset (&item);
1521 case GST_ITERATOR_RESYNC:
1522 /* We don't reset the result here because we don't push the event
1523 * again on pads that got the event already and because we need
1524 * to consider the result of the previous pushes */
1525 gst_iterator_resync (iter);
1527 case GST_ITERATOR_ERROR:
1528 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1531 case GST_ITERATOR_DONE:
1536 g_value_unset (&item);
1537 gst_iterator_free (iter);
1539 g_list_free (pushed_pads);
1541 gst_event_unref (event);
1546 /* query on the srcpad. We override this function because by default it will
1547 * only forward the query to one random sinkpad */
1549 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1551 gboolean res = FALSE;
1552 GstInputSelector *sel;
1554 sel = GST_INPUT_SELECTOR (parent);
1556 switch (GST_QUERY_TYPE (query)) {
1557 case GST_QUERY_LATENCY:
1560 GstClockTime resmin, resmax;
1567 /* perform the query on all sinkpads and combine the results. We take the
1568 * max of min and the min of max for the result latency. */
1569 GST_INPUT_SELECTOR_LOCK (sel);
1570 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1571 walk = g_list_next (walk)) {
1572 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1574 if (gst_pad_peer_query (sinkpad, query)) {
1575 GstClockTime min, max;
1578 /* one query succeeded, we succeed too */
1581 gst_query_parse_latency (query, &live, &min, &max);
1583 GST_DEBUG_OBJECT (sinkpad,
1584 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1585 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1592 else if (max < resmax)
1594 if (reslive == FALSE)
1599 GST_INPUT_SELECTOR_UNLOCK (sel);
1601 gst_query_set_latency (query, reslive, resmin, resmax);
1603 GST_DEBUG_OBJECT (sel,
1604 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1605 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1612 res = gst_pad_query_default (pad, parent, query);
1619 /* check if the pad is the active sinkpad */
1620 static inline gboolean
1621 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1625 GST_INPUT_SELECTOR_LOCK (sel);
1626 res = (pad == sel->active_sinkpad);
1627 GST_INPUT_SELECTOR_UNLOCK (sel);
1632 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1634 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1636 GstPad *active_sinkpad;
1637 GstSelectorPad *selpad;
1639 selpad = GST_SELECTOR_PAD_CAST (pad);
1641 selpad->active = TRUE;
1642 active_sinkpad = sel->active_sinkpad;
1643 if (active_sinkpad == NULL) {
1644 GValue item = G_VALUE_INIT;
1645 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1646 GstIteratorResult ires;
1648 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1649 gst_iterator_resync (iter);
1650 if (ires == GST_ITERATOR_OK) {
1651 /* If no pad is currently selected, we return the first usable pad to
1652 * guarantee consistency */
1654 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1655 g_value_reset (&item);
1656 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1657 GST_DEBUG_PAD_NAME (active_sinkpad));
1659 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1660 gst_iterator_free (iter);
1663 return active_sinkpad;
1667 gst_input_selector_request_new_pad (GstElement * element,
1668 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1670 GstInputSelector *sel;
1672 GstPad *sinkpad = NULL;
1674 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1676 sel = GST_INPUT_SELECTOR (element);
1678 GST_INPUT_SELECTOR_LOCK (sel);
1680 GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1681 name = g_strdup_printf ("sink_%u", sel->padcount++);
1682 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1683 "name", name, "direction", templ->direction, "template", templ, NULL);
1688 gst_pad_set_event_function (sinkpad,
1689 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1690 gst_pad_set_query_function (sinkpad,
1691 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1692 gst_pad_set_chain_function (sinkpad,
1693 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1694 gst_pad_set_iterate_internal_links_function (sinkpad,
1695 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1697 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1698 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1699 gst_pad_set_active (sinkpad, TRUE);
1700 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1701 GST_INPUT_SELECTOR_UNLOCK (sel);
1707 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1709 GstInputSelector *sel;
1711 sel = GST_INPUT_SELECTOR (element);
1712 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1714 GST_INPUT_SELECTOR_LOCK (sel);
1715 /* if the pad was the active pad, makes us select a new one */
1716 if (sel->active_sinkpad == pad) {
1717 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1718 gst_object_unref (sel->active_sinkpad);
1719 sel->active_sinkpad = NULL;
1722 GST_INPUT_SELECTOR_UNLOCK (sel);
1724 gst_pad_set_active (pad, FALSE);
1725 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1729 gst_input_selector_reset (GstInputSelector * sel)
1733 GST_INPUT_SELECTOR_LOCK (sel);
1734 /* clear active pad */
1735 if (sel->active_sinkpad) {
1736 gst_object_unref (sel->active_sinkpad);
1737 sel->active_sinkpad = NULL;
1739 /* reset each of our sinkpads state */
1740 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1741 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1743 gst_selector_pad_reset (selpad);
1746 gst_tag_list_unref (selpad->tags);
1747 selpad->tags = NULL;
1750 sel->have_group_id = TRUE;
1751 GST_INPUT_SELECTOR_UNLOCK (sel);
1754 static GstStateChangeReturn
1755 gst_input_selector_change_state (GstElement * element,
1756 GstStateChange transition)
1758 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1759 GstStateChangeReturn result;
1761 switch (transition) {
1762 case GST_STATE_CHANGE_READY_TO_PAUSED:
1763 GST_INPUT_SELECTOR_LOCK (self);
1764 self->blocked = FALSE;
1765 self->flushing = FALSE;
1766 GST_INPUT_SELECTOR_UNLOCK (self);
1768 case GST_STATE_CHANGE_PAUSED_TO_READY:
1769 /* first unlock before we call the parent state change function, which
1770 * tries to acquire the stream lock when going to ready. */
1771 GST_INPUT_SELECTOR_LOCK (self);
1772 self->blocked = FALSE;
1773 self->flushing = TRUE;
1774 GST_INPUT_SELECTOR_BROADCAST (self);
1775 GST_INPUT_SELECTOR_UNLOCK (self);
1781 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1783 switch (transition) {
1784 case GST_STATE_CHANGE_PAUSED_TO_READY:
1785 gst_input_selector_reset (self);
1795 gst_input_selector_block (GstInputSelector * self)
1798 GstSelectorPad *spad;
1800 GST_INPUT_SELECTOR_LOCK (self);
1803 GST_WARNING_OBJECT (self, "switch already blocked");
1805 self->blocked = TRUE;
1806 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1809 ret = gst_selector_pad_get_running_time (spad);
1811 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1813 GST_INPUT_SELECTOR_UNLOCK (self);