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,
128 static void gst_input_selector_active_pad_changed (GstInputSelector * sel,
129 GParamSpec * pspec, gpointer user_data);
130 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
132 static GstPad *gst_input_selector_get_active_sinkpad (GstInputSelector * sel);
133 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
134 GstPad * pad, gboolean strict);
136 #define GST_TYPE_SELECTOR_PAD \
137 (gst_selector_pad_get_type())
138 #define GST_SELECTOR_PAD(obj) \
139 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
140 #define GST_SELECTOR_PAD_CLASS(klass) \
141 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
142 #define GST_IS_SELECTOR_PAD(obj) \
143 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
144 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
145 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
146 #define GST_SELECTOR_PAD_CAST(obj) \
147 ((GstSelectorPad *)(obj))
149 typedef struct _GstSelectorPad GstSelectorPad;
150 typedef struct _GstSelectorPadClass GstSelectorPadClass;
151 typedef struct _GstSelectorPadCachedBuffer GstSelectorPadCachedBuffer;
153 struct _GstSelectorPad
157 gboolean pushed; /* when buffer was pushed downstream since activation */
158 gboolean eos; /* when EOS has been received */
159 gboolean eos_sent; /* when EOS was sent downstream */
160 gboolean discont; /* after switching we create a discont */
161 gboolean flushing; /* set after flush-start and before flush-stop */
163 GstTagList *tags; /* last tags received on the pad */
165 GstSegment segment; /* the current segment on the pad */
166 guint32 segment_seqnum; /* sequence number of the current segment */
168 gboolean events_pending; /* TRUE if sticky events need to be updated */
170 gboolean sending_cached_buffers;
171 GQueue *cached_buffers;
174 struct _GstSelectorPadCachedBuffer
180 struct _GstSelectorPadClass
185 GType gst_selector_pad_get_type (void);
186 static void gst_selector_pad_finalize (GObject * object);
187 static void gst_selector_pad_get_property (GObject * object,
188 guint prop_id, GValue * value, GParamSpec * pspec);
189 static void gst_selector_pad_set_property (GObject * object,
190 guint prop_id, const GValue * value, GParamSpec * pspec);
192 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
193 static void gst_selector_pad_reset (GstSelectorPad * pad);
194 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
196 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
198 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
200 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
202 static void gst_selector_pad_cache_buffer (GstSelectorPad * selpad,
204 static void gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad);
206 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
209 gst_selector_pad_class_init (GstSelectorPadClass * klass)
211 GObjectClass *gobject_class;
213 gobject_class = (GObjectClass *) klass;
215 gobject_class->finalize = gst_selector_pad_finalize;
217 gobject_class->get_property = gst_selector_pad_get_property;
218 gobject_class->set_property = gst_selector_pad_set_property;
220 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
221 g_param_spec_int64 ("running-time", "Running time",
222 "Running time of stream on pad", 0, G_MAXINT64, 0,
223 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
225 g_param_spec_boxed ("tags", "Tags",
226 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
227 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
229 g_param_spec_boolean ("active", "Active",
230 "If the pad is currently active", FALSE,
231 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
232 /* FIXME: better property name? */
233 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
234 g_param_spec_boolean ("always-ok", "Always OK",
235 "Make an inactive pad return OK instead of NOT_LINKED",
236 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 gst_selector_pad_init (GstSelectorPad * pad)
242 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
243 gst_selector_pad_reset (pad);
247 gst_selector_pad_finalize (GObject * object)
251 pad = GST_SELECTOR_PAD_CAST (object);
254 gst_tag_list_unref (pad->tags);
255 gst_selector_pad_free_cached_buffers (pad);
257 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
261 gst_selector_pad_set_property (GObject * object, guint prop_id,
262 const GValue * value, GParamSpec * pspec)
264 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
267 case PROP_PAD_ALWAYS_OK:
268 GST_OBJECT_LOCK (object);
269 spad->always_ok = g_value_get_boolean (value);
270 GST_OBJECT_UNLOCK (object);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_selector_pad_get_property (GObject * object, guint prop_id,
280 GValue * value, GParamSpec * pspec)
282 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
285 case PROP_PAD_RUNNING_TIME:
286 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
289 GST_OBJECT_LOCK (object);
290 g_value_set_boxed (value, spad->tags);
291 GST_OBJECT_UNLOCK (object);
293 case PROP_PAD_ACTIVE:
295 GstInputSelector *sel;
297 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
299 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
300 GST_PAD_CAST (spad)));
301 gst_object_unref (sel);
303 g_value_set_boolean (value, FALSE);
307 case PROP_PAD_ALWAYS_OK:
308 GST_OBJECT_LOCK (object);
309 g_value_set_boolean (value, spad->always_ok);
310 GST_OBJECT_UNLOCK (object);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 gst_selector_pad_get_running_time (GstSelectorPad * pad)
323 GST_OBJECT_LOCK (pad);
324 if (pad->segment.format == GST_FORMAT_TIME) {
326 gst_segment_to_running_time (&pad->segment, pad->segment.format,
327 pad->segment.position);
329 GST_OBJECT_UNLOCK (pad);
331 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT
332 " segment: %" GST_SEGMENT_FORMAT, GST_TIME_ARGS (ret), &pad->segment);
337 /* must be called with the SELECTOR_LOCK */
339 gst_selector_pad_reset (GstSelectorPad * pad)
341 GST_OBJECT_LOCK (pad);
344 pad->eos_sent = FALSE;
345 pad->events_pending = FALSE;
346 pad->discont = FALSE;
347 pad->flushing = FALSE;
348 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
349 pad->sending_cached_buffers = FALSE;
350 gst_selector_pad_free_cached_buffers (pad);
351 GST_OBJECT_UNLOCK (pad);
354 static GstSelectorPadCachedBuffer *
355 gst_selector_pad_new_cached_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
357 GstSelectorPadCachedBuffer *cached_buffer =
358 g_slice_new (GstSelectorPadCachedBuffer);
359 cached_buffer->buffer = buffer;
360 cached_buffer->segment = selpad->segment;
361 return cached_buffer;
365 gst_selector_pad_free_cached_buffer (GstSelectorPadCachedBuffer * cached_buffer)
367 if (cached_buffer->buffer)
368 gst_buffer_unref (cached_buffer->buffer);
369 g_slice_free (GstSelectorPadCachedBuffer, cached_buffer);
372 /* must be called with the SELECTOR_LOCK */
374 gst_selector_pad_cache_buffer (GstSelectorPad * selpad, GstBuffer * buffer)
376 if (selpad->segment.format != GST_FORMAT_TIME) {
377 GST_DEBUG_OBJECT (selpad, "Buffer %p with segment not in time format, "
378 "not caching", buffer);
382 GST_DEBUG_OBJECT (selpad, "Caching buffer %p", buffer);
383 if (!selpad->cached_buffers)
384 selpad->cached_buffers = g_queue_new ();
385 g_queue_push_tail (selpad->cached_buffers,
386 gst_selector_pad_new_cached_buffer (selpad, buffer));
389 /* must be called with the SELECTOR_LOCK */
391 gst_selector_pad_free_cached_buffers (GstSelectorPad * selpad)
393 if (!selpad->cached_buffers)
396 GST_DEBUG_OBJECT (selpad, "Freeing cached buffers");
397 g_queue_free_full (selpad->cached_buffers,
398 (GDestroyNotify) gst_selector_pad_free_cached_buffer);
399 selpad->cached_buffers = NULL;
402 /* strictly get the linked pad from the sinkpad. If the pad is active we return
403 * the srcpad else we return NULL */
405 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
407 GstInputSelector *sel;
409 GstIterator *it = NULL;
412 sel = GST_INPUT_SELECTOR (parent);
414 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
416 g_value_init (&val, GST_TYPE_PAD);
417 g_value_set_object (&val, otherpad);
418 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
419 g_value_unset (&val);
420 gst_object_unref (otherpad);
427 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
429 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
431 GST_DEBUG_OBJECT (sinkpad, "forward sticky event %" GST_PTR_FORMAT, *event);
433 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
434 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
437 e = gst_event_new_segment (seg);
438 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
440 gst_pad_push_event (sel->srcpad, e);
441 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
442 && !sel->have_group_id) {
444 gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
446 /* Only push stream-start once if not all our streams have a stream-id */
448 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
450 gst_event_unref (tmp);
453 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
460 gst_input_selector_eos_wait (GstInputSelector * self, GstSelectorPad * pad,
461 GstEvent * eos_event)
463 while (!self->eos && !self->flushing && !pad->flushing) {
464 GstPad *active_sinkpad;
465 active_sinkpad = gst_input_selector_get_active_sinkpad (self);
466 if (pad == GST_SELECTOR_PAD_CAST (active_sinkpad) && pad->eos
468 GST_DEBUG_OBJECT (pad, "send EOS event");
469 GST_INPUT_SELECTOR_UNLOCK (self);
470 /* if we have a pending events, push them now */
471 if (pad->events_pending) {
472 gst_pad_sticky_events_foreach (GST_PAD_CAST (pad),
473 forward_sticky_events, self);
474 pad->events_pending = FALSE;
477 gst_pad_push_event (self->srcpad, gst_event_ref (eos_event));
478 GST_INPUT_SELECTOR_LOCK (self);
479 pad->eos_sent = TRUE;
481 /* we can be unlocked here when we are shutting down (flushing) or when we
483 GST_INPUT_SELECTOR_WAIT (self);
487 return self->flushing;
491 gst_input_selector_all_eos (GstInputSelector * sel)
495 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = walk->next) {
496 GstSelectorPad *selpad;
498 selpad = GST_SELECTOR_PAD_CAST (walk->data);
509 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
513 gboolean new_tags = FALSE;
514 GstInputSelector *sel;
515 GstSelectorPad *selpad;
516 GstPad *prev_active_sinkpad;
517 GstPad *active_sinkpad;
519 sel = GST_INPUT_SELECTOR (parent);
520 selpad = GST_SELECTOR_PAD_CAST (pad);
521 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
523 GST_INPUT_SELECTOR_LOCK (sel);
524 prev_active_sinkpad =
525 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
526 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
527 gst_object_ref (active_sinkpad);
528 GST_INPUT_SELECTOR_UNLOCK (sel);
530 if (prev_active_sinkpad != active_sinkpad) {
531 if (prev_active_sinkpad)
532 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
533 g_object_notify (G_OBJECT (active_sinkpad), "active");
534 g_object_notify (G_OBJECT (sel), "active-pad");
536 if (prev_active_sinkpad)
537 gst_object_unref (prev_active_sinkpad);
538 gst_object_unref (active_sinkpad);
540 GST_INPUT_SELECTOR_LOCK (sel);
541 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
543 /* only forward if we are dealing with the active sinkpad */
544 forward = (pad == active_sinkpad);
546 switch (GST_EVENT_TYPE (event)) {
547 case GST_EVENT_STREAM_START:{
550 if (!gst_event_parse_group_id (event, &group_id))
551 sel->have_group_id = FALSE;
554 case GST_EVENT_FLUSH_START:
555 /* Unblock the pad if it's waiting */
556 selpad->flushing = TRUE;
558 GST_INPUT_SELECTOR_BROADCAST (sel);
560 case GST_EVENT_FLUSH_STOP:
561 gst_selector_pad_reset (selpad);
563 case GST_EVENT_SEGMENT:
565 gst_event_copy_segment (event, &selpad->segment);
566 selpad->segment_seqnum = gst_event_get_seqnum (event);
568 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
574 GstTagList *tags, *oldtags, *newtags;
576 gst_event_parse_tag (event, &tags);
578 GST_OBJECT_LOCK (selpad);
579 oldtags = selpad->tags;
581 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
582 selpad->tags = newtags;
583 GST_OBJECT_UNLOCK (selpad);
586 gst_tag_list_unref (oldtags);
587 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
594 GST_DEBUG_OBJECT (pad, "received EOS");
595 if (gst_input_selector_all_eos (sel)) {
596 GST_DEBUG_OBJECT (pad, "All sink pad received EOS");
598 GST_INPUT_SELECTOR_BROADCAST (sel);
600 gst_input_selector_eos_wait (sel, selpad, event);
605 GstClockTime ts, dur;
607 GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
609 gst_event_parse_gap (event, &ts, &dur);
610 if (GST_CLOCK_TIME_IS_VALID (ts)) {
611 if (GST_CLOCK_TIME_IS_VALID (dur))
614 /* update the segment position */
615 GST_OBJECT_LOCK (pad);
616 selpad->segment.position = ts;
617 GST_OBJECT_UNLOCK (pad);
618 if (sel->sync_streams && active_sinkpad == pad)
619 GST_INPUT_SELECTOR_BROADCAST (sel);
627 GST_INPUT_SELECTOR_UNLOCK (sel);
629 g_object_notify (G_OBJECT (selpad), "tags");
632 GST_DEBUG_OBJECT (pad, "forwarding event");
633 res = gst_pad_push_event (sel->srcpad, event);
635 /* If we aren't forwarding the event because the pad is not the
636 * active_sinkpad, then set the flag on the pad
637 * that says a segment needs sending if/when that pad is activated.
638 * For all other cases, we send the event immediately, which makes
639 * sparse streams and other segment updates work correctly downstream.
641 if (GST_EVENT_IS_STICKY (event))
642 selpad->events_pending = TRUE;
643 gst_event_unref (event);
651 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
653 gboolean res = FALSE;
654 GstInputSelector *self = (GstInputSelector *) parent;
656 switch (GST_QUERY_TYPE (query)) {
658 /* always proxy caps query, regardless of active pad or not */
659 res = gst_pad_peer_query (self->srcpad, query);
661 case GST_QUERY_ALLOCATION:{
662 GstPad *active_sinkpad;
663 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
665 /* Only do the allocation query for the active sinkpad,
666 * after switching a reconfigure event is sent and upstream
667 * should reconfigure and do a new allocation query
669 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
670 GST_INPUT_SELECTOR_LOCK (sel);
671 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
672 GST_INPUT_SELECTOR_UNLOCK (sel);
674 if (pad != active_sinkpad) {
682 res = gst_pad_query_default (pad, parent, query);
691 gst_input_selector_get_clipped_running_time (GstSegment * seg, GstBuffer * buf)
693 GstClockTime running_time;
695 running_time = GST_BUFFER_PTS (buf);
696 /* If possible try to get the running time at the end of the buffer */
697 if (GST_BUFFER_DURATION_IS_VALID (buf))
698 running_time += GST_BUFFER_DURATION (buf);
699 /* Only use the segment to convert to running time if the segment is
700 * in TIME format, otherwise do our best to try to sync */
701 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
702 if (running_time > seg->stop) {
703 running_time = seg->stop;
706 return gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
709 /* must be called without the SELECTOR_LOCK, will wait until the running time
710 * of the active pad is after this pad or return TRUE when flushing */
712 gst_input_selector_wait_running_time (GstInputSelector * sel,
713 GstSelectorPad * selpad, GstBuffer * buf)
717 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
719 /* If we have no valid timestamp we can't sync this buffer */
720 if (!GST_BUFFER_PTS_IS_VALID (buf)) {
721 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
722 "invalid timestamp");
726 seg = &selpad->segment;
729 * a) this is the active pad
730 * b) the pad or the selector is flushing
731 * c) the buffer running time is before the current running time
732 * (either active-seg or clock, depending on sync-mode)
735 GST_INPUT_SELECTOR_LOCK (sel);
737 GstPad *active_sinkpad;
738 GstSelectorPad *active_selpad;
740 gint64 cur_running_time;
741 GstClockTime running_time;
743 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
744 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
746 if (seg->format != GST_FORMAT_TIME) {
747 GST_DEBUG_OBJECT (selpad,
748 "Not waiting because we don't have a TIME segment");
749 GST_INPUT_SELECTOR_UNLOCK (sel);
753 running_time = gst_input_selector_get_clipped_running_time (seg, buf);
754 /* If this is outside the segment don't sync */
755 if (running_time == -1) {
756 GST_DEBUG_OBJECT (selpad,
757 "Not waiting because buffer is outside segment");
758 GST_INPUT_SELECTOR_UNLOCK (sel);
762 cur_running_time = GST_CLOCK_TIME_NONE;
763 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
764 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
766 GstClockTime base_time;
768 cur_running_time = gst_clock_get_time (clock);
769 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
770 if (base_time <= cur_running_time)
771 cur_running_time -= base_time;
773 cur_running_time = 0;
775 gst_object_unref (clock);
778 GstSegment *active_seg;
780 active_seg = &active_selpad->segment;
782 /* If the active segment is configured but not to time format
783 * we can't do any syncing at all */
784 if ((active_seg->format != GST_FORMAT_TIME
785 && active_seg->format != GST_FORMAT_UNDEFINED)) {
786 GST_DEBUG_OBJECT (selpad,
787 "Not waiting because active segment isn't in TIME format");
788 GST_INPUT_SELECTOR_UNLOCK (sel);
792 /* Get active pad's running time, if no configured segment yet keep at -1 */
793 if (active_seg->format == GST_FORMAT_TIME)
794 cur_running_time = gst_segment_to_running_time (active_seg,
795 GST_FORMAT_TIME, active_seg->position);
798 if (selpad != active_selpad && !sel->eos && !sel->flushing
799 && !selpad->flushing && (cur_running_time == GST_CLOCK_TIME_NONE
800 || running_time >= cur_running_time)) {
801 GST_DEBUG_OBJECT (selpad,
802 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
803 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
804 GST_TIME_ARGS (cur_running_time));
805 GST_INPUT_SELECTOR_WAIT (sel);
807 GST_INPUT_SELECTOR_UNLOCK (sel);
812 /* Return TRUE if the selector or the pad is flushing */
813 return (sel->flushing || selpad->flushing);
816 #if DEBUG_CACHED_BUFFERS
818 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
822 if (gst_debug_category_get_threshold (input_selector_debug) < GST_LEVEL_DEBUG)
825 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = walk->next) {
826 GstSelectorPad *selpad;
830 selpad = GST_SELECTOR_PAD_CAST (walk->data);
831 if (!selpad->cached_buffers) {
832 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
836 timestamps = g_string_new ("Cached buffers timestamps:");
837 for (l = selpad->cached_buffers->head; l != NULL; l = l->next) {
838 GstSelectorPadCachedBuffer *cached_buffer = l->data;
840 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
841 GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
843 GST_DEBUG_OBJECT (selpad, "%s", timestamps->str);
844 g_string_free (timestamps, TRUE);
849 /* must be called with the SELECTOR_LOCK */
851 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
855 gint64 cur_running_time;
858 cur_running_time = GST_CLOCK_TIME_NONE;
859 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
860 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
862 GstClockTime base_time;
864 cur_running_time = gst_clock_get_time (clock);
865 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
866 if (base_time <= cur_running_time)
867 cur_running_time -= base_time;
869 cur_running_time = 0;
871 gst_object_unref (clock);
874 GstPad *active_sinkpad;
875 GstSelectorPad *active_selpad;
876 GstSegment *active_seg;
878 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
879 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
880 active_seg = &active_selpad->segment;
882 /* Get active pad's running time, if no configured segment yet keep at -1 */
883 if (active_seg->format == GST_FORMAT_TIME)
884 cur_running_time = gst_segment_to_running_time (active_seg,
885 GST_FORMAT_TIME, active_seg->position);
888 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
891 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
892 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
893 GstSelectorPad *selpad;
895 GstSelectorPadCachedBuffer *cached_buffer;
896 GSList *maybe_remove;
897 guint queue_position;
899 selpad = GST_SELECTOR_PAD_CAST (walk->data);
900 if (!selpad->cached_buffers)
903 seg = &selpad->segment;
907 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
909 GstBuffer *buffer = cached_buffer->buffer;
910 GstClockTime running_time;
913 /* If we have no valid timestamp we can't sync this buffer */
914 if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
915 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
916 queue_position = g_slist_length (maybe_remove);
920 /* the buffer is still valid if its duration is valid and the
921 * timestamp + duration is >= time, or if its duration is invalid
922 * and the timestamp is >= time */
923 running_time = gst_input_selector_get_clipped_running_time (seg, buffer);
924 GST_DEBUG_OBJECT (selpad,
925 "checking if buffer %p running time=%" GST_TIME_FORMAT
926 " >= stream time=%" GST_TIME_FORMAT, buffer,
927 GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
928 if (running_time >= cur_running_time) {
932 GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
933 g_queue_pop_nth (selpad->cached_buffers, queue_position);
934 gst_selector_pad_free_cached_buffer (cached_buffer);
936 for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
937 /* A buffer after some invalid buffers was removed, it means the invalid buffers
938 * are old, lets also remove them */
939 cached_buffer = l->data;
940 g_queue_remove (selpad->cached_buffers, cached_buffer);
941 gst_selector_pad_free_cached_buffer (cached_buffer);
944 g_slist_free (maybe_remove);
949 g_slist_free (maybe_remove);
952 if (g_queue_is_empty (selpad->cached_buffers)) {
953 g_queue_free (selpad->cached_buffers);
954 selpad->cached_buffers = NULL;
958 #if DEBUG_CACHED_BUFFERS
959 gst_input_selector_debug_cached_buffers (sel);
964 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
966 GstInputSelector *sel;
968 GstPad *active_sinkpad;
969 GstPad *prev_active_sinkpad = NULL;
970 GstSelectorPad *selpad;
972 sel = GST_INPUT_SELECTOR (parent);
973 selpad = GST_SELECTOR_PAD_CAST (pad);
975 GST_DEBUG_OBJECT (selpad,
976 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
977 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
979 GST_INPUT_SELECTOR_LOCK (sel);
982 GST_INPUT_SELECTOR_UNLOCK (sel);
986 GST_LOG_OBJECT (pad, "getting active pad");
988 prev_active_sinkpad =
989 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
990 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
992 /* In sync mode wait until the active pad has advanced
993 * after the running time of the current buffer */
994 if (sel->sync_streams) {
995 /* call chain for each cached buffer if we are not the active pad
996 * or if we are the active pad but didn't push anything yet. */
997 if (active_sinkpad != pad || !selpad->pushed) {
998 /* no need to check for sel->cache_buffers as selpad->cached_buffers
999 * will only be valid if cache_buffers is TRUE */
1000 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
1001 GstSelectorPadCachedBuffer *cached_buffer;
1002 GstSegment saved_segment;
1004 saved_segment = selpad->segment;
1006 selpad->sending_cached_buffers = TRUE;
1007 while (!sel->eos && !sel->flushing && !selpad->flushing &&
1008 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
1009 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
1010 "invoking chain for cached buffer %p", cached_buffer->buffer);
1012 selpad->segment = cached_buffer->segment;
1013 selpad->events_pending = TRUE;
1014 GST_INPUT_SELECTOR_UNLOCK (sel);
1015 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
1016 GST_INPUT_SELECTOR_LOCK (sel);
1018 /* We just passed the ownership of the buffer to the chain function */
1019 cached_buffer->buffer = NULL;
1020 gst_selector_pad_free_cached_buffer (cached_buffer);
1022 /* we may have cleaned up the queue in the meantime because of
1024 if (!selpad->cached_buffers) {
1028 selpad->sending_cached_buffers = FALSE;
1030 /* all cached buffers sent, restore segment for current buffer */
1031 selpad->segment = saved_segment;
1032 selpad->events_pending = TRUE;
1034 /* Might have changed while calling chain for cached buffers */
1035 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1039 if (active_sinkpad != pad) {
1040 GST_INPUT_SELECTOR_UNLOCK (sel);
1041 if (gst_input_selector_wait_running_time (sel, selpad, buf))
1043 GST_INPUT_SELECTOR_LOCK (sel);
1046 /* Might have changed while waiting */
1047 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1050 /* update the segment on the srcpad */
1051 if (GST_BUFFER_PTS_IS_VALID (buf)) {
1052 GstClockTime start_time = GST_BUFFER_PTS (buf);
1054 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1055 GST_TIME_ARGS (start_time));
1056 if (GST_BUFFER_DURATION_IS_VALID (buf))
1057 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1058 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1060 GST_OBJECT_LOCK (pad);
1061 selpad->segment.position = start_time;
1062 GST_OBJECT_UNLOCK (pad);
1065 /* Ignore buffers from pads except the selected one */
1066 if (pad != active_sinkpad)
1069 /* Tell all non-active pads that we advanced the running time */
1070 if (sel->sync_streams)
1071 GST_INPUT_SELECTOR_BROADCAST (sel);
1073 GST_INPUT_SELECTOR_UNLOCK (sel);
1075 if (prev_active_sinkpad != active_sinkpad) {
1076 if (prev_active_sinkpad)
1077 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1078 g_object_notify (G_OBJECT (active_sinkpad), "active");
1079 g_object_notify (G_OBJECT (sel), "active-pad");
1082 /* if we have a pending events, push them now */
1083 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1084 || selpad->events_pending)) {
1085 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1087 selpad->events_pending = FALSE;
1090 if (prev_active_sinkpad) {
1091 gst_object_unref (prev_active_sinkpad);
1092 prev_active_sinkpad = NULL;
1095 if (selpad->discont) {
1096 buf = gst_buffer_make_writable (buf);
1098 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1099 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1100 selpad->discont = FALSE;
1104 GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1105 buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1107 /* Only make the buffer read-only when necessary */
1108 if (sel->sync_streams && sel->cache_buffers)
1109 buf = gst_buffer_ref (buf);
1110 res = gst_pad_push (sel->srcpad, buf);
1111 GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1113 GST_INPUT_SELECTOR_LOCK (sel);
1115 if (sel->sync_streams && sel->cache_buffers) {
1116 /* Might have changed while pushing */
1117 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1118 /* only set pad to pushed if we are still the active pad */
1119 if (active_sinkpad == pad)
1120 selpad->pushed = TRUE;
1122 /* cache buffer as we may need it again if we change pads */
1123 gst_selector_pad_cache_buffer (selpad, buf);
1124 gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1126 selpad->pushed = TRUE;
1128 GST_INPUT_SELECTOR_UNLOCK (sel);
1132 if (prev_active_sinkpad)
1133 gst_object_unref (prev_active_sinkpad);
1134 prev_active_sinkpad = NULL;
1138 /* dropped buffers */
1141 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1143 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1144 /* when we drop a buffer, we're creating a discont on this pad */
1145 selpad->discont = TRUE;
1146 GST_INPUT_SELECTOR_UNLOCK (sel);
1147 gst_buffer_unref (buf);
1149 /* figure out what to return upstream */
1150 GST_OBJECT_LOCK (selpad);
1151 if (selpad->always_ok || !active_pad_pushed)
1154 res = GST_FLOW_NOT_LINKED;
1155 GST_OBJECT_UNLOCK (selpad);
1161 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1162 gst_buffer_unref (buf);
1163 res = GST_FLOW_FLUSHING;
1168 static void gst_input_selector_dispose (GObject * object);
1169 static void gst_input_selector_finalize (GObject * object);
1171 static void gst_input_selector_set_property (GObject * object,
1172 guint prop_id, const GValue * value, GParamSpec * pspec);
1173 static void gst_input_selector_get_property (GObject * object,
1174 guint prop_id, GValue * value, GParamSpec * pspec);
1176 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1177 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1178 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1180 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1181 element, GstStateChange transition);
1183 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1187 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1188 "input-selector", 0, "An input stream selector element");
1189 #define gst_input_selector_parent_class parent_class
1190 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1194 gst_input_selector_class_init (GstInputSelectorClass * klass)
1196 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1197 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1199 gobject_class->dispose = gst_input_selector_dispose;
1200 gobject_class->finalize = gst_input_selector_finalize;
1202 gobject_class->set_property = gst_input_selector_set_property;
1203 gobject_class->get_property = gst_input_selector_get_property;
1205 g_object_class_install_property (gobject_class, PROP_N_PADS,
1206 g_param_spec_uint ("n-pads", "Number of Pads",
1207 "The number of sink pads", 0, G_MAXUINT, 0,
1208 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1210 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1211 g_param_spec_object ("active-pad", "Active pad",
1212 "The currently active sink pad", GST_TYPE_PAD,
1213 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
1214 G_PARAM_STATIC_STRINGS));
1217 * GstInputSelector:sync-streams
1219 * If set to %TRUE all inactive streams will be synced to the
1220 * running time of the active stream or to the current clock.
1222 * To make sure no buffers are dropped by input-selector
1223 * that might be needed when switching the active pad,
1224 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1226 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1227 g_param_spec_boolean ("sync-streams", "Sync Streams",
1228 "Synchronize inactive streams to the running time of the active "
1229 "stream or to the current clock",
1230 DEFAULT_SYNC_STREAMS,
1231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1232 GST_PARAM_MUTABLE_READY));
1235 * GstInputSelector:sync-mode
1237 * Select how input-selector will sync buffers when in sync-streams mode.
1239 * Note that when using the "active-segment" mode, the "active-segment" may
1240 * be ahead of current clock time when switching the active pad, as the current
1241 * active pad may have pushed more buffers than what was displayed/consumed,
1242 * which may cause delays and some missing buffers.
1244 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1245 g_param_spec_enum ("sync-mode", "Sync mode",
1246 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1248 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1249 GST_PARAM_MUTABLE_READY));
1252 * GstInputSelector:cache-buffers
1254 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1255 * the active pad will cache the buffers still considered valid (after current
1256 * running time, see sync-mode) to avoid missing frames if/when the pad is
1259 * The active pad may push more buffers than what is currently displayed/consumed
1260 * and when changing pads those buffers will be discarded and the only way to
1261 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1263 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1264 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1265 "Cache buffers for active-pad",
1266 DEFAULT_CACHE_BUFFERS,
1267 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1268 GST_PARAM_MUTABLE_READY));
1270 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1271 "Generic", "N-to-1 input stream selector",
1272 "Julien Moutte <julien@moutte.net>, "
1273 "Jan Schmidt <thaytan@mad.scientist.com>, "
1274 "Wim Taymans <wim.taymans@gmail.com>");
1275 gst_element_class_add_pad_template (gstelement_class,
1276 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1277 gst_element_class_add_pad_template (gstelement_class,
1278 gst_static_pad_template_get (&gst_input_selector_src_factory));
1280 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1281 gstelement_class->release_pad = gst_input_selector_release_pad;
1282 gstelement_class->change_state = gst_input_selector_change_state;
1286 gst_input_selector_init (GstInputSelector * sel)
1288 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1289 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1290 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1291 gst_pad_set_event_function (sel->srcpad,
1292 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1293 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1294 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1295 /* sinkpad management */
1296 sel->active_sinkpad = NULL;
1298 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1299 sel->sync_mode = DEFAULT_SYNC_MODE;
1300 sel->have_group_id = TRUE;
1302 g_mutex_init (&sel->lock);
1303 g_cond_init (&sel->cond);
1306 /* lets give a change for downstream to do something on
1307 * active-pad change before we start pushing new buffers */
1308 g_signal_connect_data (sel, "notify::active-pad",
1309 (GCallback) gst_input_selector_active_pad_changed, NULL,
1310 NULL, G_CONNECT_AFTER);
1314 gst_input_selector_dispose (GObject * object)
1316 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1318 if (sel->active_sinkpad) {
1319 gst_object_unref (sel->active_sinkpad);
1320 sel->active_sinkpad = NULL;
1322 G_OBJECT_CLASS (parent_class)->dispose (object);
1326 gst_input_selector_finalize (GObject * object)
1328 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1330 g_mutex_clear (&sel->lock);
1331 g_cond_clear (&sel->cond);
1333 G_OBJECT_CLASS (parent_class)->finalize (object);
1336 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1337 * active pad changed. */
1339 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1341 GstSelectorPad *old, *new;
1342 GstPad **active_pad_p;
1344 if (pad == self->active_sinkpad)
1347 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1348 new = GST_SELECTOR_PAD_CAST (pad);
1350 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1351 GST_DEBUG_PAD_NAME (new));
1354 old->pushed = FALSE;
1356 new->pushed = FALSE;
1358 /* Send a new SEGMENT event on the new pad next */
1359 if (old != new && new)
1360 new->events_pending = TRUE;
1362 active_pad_p = &self->active_sinkpad;
1363 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1365 if (old && old != new)
1366 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1368 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1370 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1371 self->active_sinkpad);
1373 if (old != new && new && new->eos) {
1374 new->eos_sent = FALSE;
1375 GST_INPUT_SELECTOR_BROADCAST (self);
1382 gst_input_selector_set_property (GObject * object, guint prop_id,
1383 const GValue * value, GParamSpec * pspec)
1385 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1388 case PROP_ACTIVE_PAD:
1392 pad = g_value_get_object (value);
1394 GST_INPUT_SELECTOR_LOCK (sel);
1396 #if DEBUG_CACHED_BUFFERS
1397 gst_input_selector_debug_cached_buffers (sel);
1400 gst_input_selector_set_active_pad (sel, pad);
1402 #if DEBUG_CACHED_BUFFERS
1403 gst_input_selector_debug_cached_buffers (sel);
1406 GST_INPUT_SELECTOR_UNLOCK (sel);
1409 case PROP_SYNC_STREAMS:
1410 GST_INPUT_SELECTOR_LOCK (sel);
1411 sel->sync_streams = g_value_get_boolean (value);
1412 GST_INPUT_SELECTOR_UNLOCK (sel);
1414 case PROP_SYNC_MODE:
1415 GST_INPUT_SELECTOR_LOCK (sel);
1416 sel->sync_mode = g_value_get_enum (value);
1417 GST_INPUT_SELECTOR_UNLOCK (sel);
1419 case PROP_CACHE_BUFFERS:
1420 GST_INPUT_SELECTOR_LOCK (object);
1421 sel->cache_buffers = g_value_get_boolean (value);
1422 GST_INPUT_SELECTOR_UNLOCK (object);
1425 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1431 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1432 GParamSpec * pspec, gpointer user_data)
1434 /* Wake up all non-active pads in sync mode, they might be
1435 * the active pad now */
1436 if (sel->sync_streams)
1437 GST_INPUT_SELECTOR_BROADCAST (sel);
1441 gst_input_selector_get_property (GObject * object, guint prop_id,
1442 GValue * value, GParamSpec * pspec)
1444 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1448 GST_INPUT_SELECTOR_LOCK (object);
1449 g_value_set_uint (value, sel->n_pads);
1450 GST_INPUT_SELECTOR_UNLOCK (object);
1452 case PROP_ACTIVE_PAD:
1453 GST_INPUT_SELECTOR_LOCK (object);
1454 g_value_set_object (value, sel->active_sinkpad);
1455 GST_INPUT_SELECTOR_UNLOCK (object);
1457 case PROP_SYNC_STREAMS:
1458 GST_INPUT_SELECTOR_LOCK (object);
1459 g_value_set_boolean (value, sel->sync_streams);
1460 GST_INPUT_SELECTOR_UNLOCK (object);
1462 case PROP_SYNC_MODE:
1463 GST_INPUT_SELECTOR_LOCK (object);
1464 g_value_set_enum (value, sel->sync_mode);
1465 GST_INPUT_SELECTOR_UNLOCK (object);
1467 case PROP_CACHE_BUFFERS:
1468 GST_INPUT_SELECTOR_LOCK (object);
1469 g_value_set_boolean (value, sel->cache_buffers);
1470 GST_INPUT_SELECTOR_UNLOCK (object);
1473 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1479 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1482 GstPad *otherpad = NULL;
1484 GST_INPUT_SELECTOR_LOCK (sel);
1485 if (pad == sel->srcpad)
1486 otherpad = sel->active_sinkpad;
1487 else if (pad == sel->active_sinkpad || !strict)
1488 otherpad = sel->srcpad;
1490 gst_object_ref (otherpad);
1491 GST_INPUT_SELECTOR_UNLOCK (sel);
1497 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1499 GstInputSelector *sel;
1500 gboolean result = FALSE;
1502 gboolean done = FALSE;
1503 GValue item = { 0, };
1505 GList *pushed_pads = NULL;
1507 sel = GST_INPUT_SELECTOR (parent);
1508 /* Send upstream events to all sinkpads */
1509 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1511 /* This is now essentially a copy of gst_pad_event_default_dispatch
1512 * with a different iterator */
1514 switch (gst_iterator_next (iter, &item)) {
1515 case GST_ITERATOR_OK:
1516 eventpad = g_value_get_object (&item);
1518 /* if already pushed, skip */
1519 if (g_list_find (pushed_pads, eventpad)) {
1520 g_value_reset (&item);
1524 gst_event_ref (event);
1525 result |= gst_pad_push_event (eventpad, event);
1527 g_value_reset (&item);
1529 case GST_ITERATOR_RESYNC:
1530 /* We don't reset the result here because we don't push the event
1531 * again on pads that got the event already and because we need
1532 * to consider the result of the previous pushes */
1533 gst_iterator_resync (iter);
1535 case GST_ITERATOR_ERROR:
1536 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1539 case GST_ITERATOR_DONE:
1544 g_value_unset (&item);
1545 gst_iterator_free (iter);
1547 g_list_free (pushed_pads);
1549 gst_event_unref (event);
1554 /* check if the pad is the active sinkpad */
1555 static inline gboolean
1556 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1560 GST_INPUT_SELECTOR_LOCK (sel);
1561 res = (pad == sel->active_sinkpad);
1562 GST_INPUT_SELECTOR_UNLOCK (sel);
1567 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1569 gst_input_selector_get_active_sinkpad (GstInputSelector * sel)
1571 GstPad *active_sinkpad;
1573 active_sinkpad = sel->active_sinkpad;
1574 if (active_sinkpad == NULL) {
1575 GValue item = G_VALUE_INIT;
1576 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1577 GstIteratorResult ires;
1579 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1580 gst_iterator_resync (iter);
1581 if (ires == GST_ITERATOR_OK) {
1582 /* If no pad is currently selected, we return the first usable pad to
1583 * guarantee consistency */
1585 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1586 g_value_reset (&item);
1587 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1588 GST_DEBUG_PAD_NAME (active_sinkpad));
1590 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1591 gst_iterator_free (iter);
1594 return active_sinkpad;
1598 gst_input_selector_request_new_pad (GstElement * element,
1599 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1601 GstInputSelector *sel;
1603 GstPad *sinkpad = NULL;
1605 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1607 sel = GST_INPUT_SELECTOR (element);
1609 GST_INPUT_SELECTOR_LOCK (sel);
1611 GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1612 name = g_strdup_printf ("sink_%u", sel->padcount++);
1613 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1614 "name", name, "direction", templ->direction, "template", templ, NULL);
1619 gst_pad_set_event_function (sinkpad,
1620 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1621 gst_pad_set_query_function (sinkpad,
1622 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1623 gst_pad_set_chain_function (sinkpad,
1624 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1625 gst_pad_set_iterate_internal_links_function (sinkpad,
1626 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1628 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1629 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1630 gst_pad_set_active (sinkpad, TRUE);
1631 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1632 GST_INPUT_SELECTOR_UNLOCK (sel);
1638 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1640 GstInputSelector *sel;
1642 sel = GST_INPUT_SELECTOR (element);
1643 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1645 GST_INPUT_SELECTOR_LOCK (sel);
1646 /* if the pad was the active pad, makes us select a new one */
1647 if (sel->active_sinkpad == pad) {
1648 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1649 gst_object_unref (sel->active_sinkpad);
1650 sel->active_sinkpad = NULL;
1653 GST_INPUT_SELECTOR_UNLOCK (sel);
1655 gst_pad_set_active (pad, FALSE);
1656 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1660 gst_input_selector_reset (GstInputSelector * sel)
1664 GST_INPUT_SELECTOR_LOCK (sel);
1665 /* clear active pad */
1666 if (sel->active_sinkpad) {
1667 gst_object_unref (sel->active_sinkpad);
1668 sel->active_sinkpad = NULL;
1670 sel->eos_sent = FALSE;
1672 /* reset each of our sinkpads state */
1673 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1674 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1676 gst_selector_pad_reset (selpad);
1679 gst_tag_list_unref (selpad->tags);
1680 selpad->tags = NULL;
1683 sel->have_group_id = TRUE;
1684 GST_INPUT_SELECTOR_UNLOCK (sel);
1687 static GstStateChangeReturn
1688 gst_input_selector_change_state (GstElement * element,
1689 GstStateChange transition)
1691 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1692 GstStateChangeReturn result;
1694 switch (transition) {
1695 case GST_STATE_CHANGE_READY_TO_PAUSED:
1696 GST_INPUT_SELECTOR_LOCK (self);
1698 self->flushing = FALSE;
1699 GST_INPUT_SELECTOR_UNLOCK (self);
1701 case GST_STATE_CHANGE_PAUSED_TO_READY:
1702 /* first unlock before we call the parent state change function, which
1703 * tries to acquire the stream lock when going to ready. */
1704 GST_INPUT_SELECTOR_LOCK (self);
1706 self->flushing = TRUE;
1707 GST_INPUT_SELECTOR_BROADCAST (self);
1708 GST_INPUT_SELECTOR_UNLOCK (self);
1714 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1716 switch (transition) {
1717 case GST_STATE_CHANGE_PAUSED_TO_READY:
1718 gst_input_selector_reset (self);