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 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
431 gboolean new_tags = FALSE;
432 GstInputSelector *sel;
433 GstSelectorPad *selpad;
434 GstPad *prev_active_sinkpad;
435 GstPad *active_sinkpad;
437 sel = GST_INPUT_SELECTOR (parent);
438 selpad = GST_SELECTOR_PAD_CAST (pad);
439 GST_DEBUG_OBJECT (selpad, "received event %" GST_PTR_FORMAT, event);
441 GST_INPUT_SELECTOR_LOCK (sel);
442 prev_active_sinkpad =
443 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
444 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
445 gst_object_ref (active_sinkpad);
446 GST_INPUT_SELECTOR_UNLOCK (sel);
448 if (prev_active_sinkpad != active_sinkpad) {
449 if (prev_active_sinkpad)
450 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
451 g_object_notify (G_OBJECT (active_sinkpad), "active");
452 g_object_notify (G_OBJECT (sel), "active-pad");
454 if (prev_active_sinkpad)
455 gst_object_unref (prev_active_sinkpad);
456 gst_object_unref (active_sinkpad);
458 GST_INPUT_SELECTOR_LOCK (sel);
459 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
461 /* only forward if we are dealing with the active sinkpad */
462 forward = (pad == active_sinkpad);
464 switch (GST_EVENT_TYPE (event)) {
465 case GST_EVENT_STREAM_START:{
468 if (!gst_event_parse_group_id (event, &group_id))
469 sel->have_group_id = FALSE;
472 case GST_EVENT_FLUSH_START:
473 /* Unblock the pad if it's waiting */
474 selpad->flushing = TRUE;
476 GST_INPUT_SELECTOR_BROADCAST (sel);
478 case GST_EVENT_FLUSH_STOP:
479 gst_selector_pad_reset (selpad);
481 case GST_EVENT_SEGMENT:
483 gst_event_copy_segment (event, &selpad->segment);
484 selpad->segment_seqnum = gst_event_get_seqnum (event);
486 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
492 GstTagList *tags, *oldtags, *newtags;
494 gst_event_parse_tag (event, &tags);
496 GST_OBJECT_LOCK (selpad);
497 oldtags = selpad->tags;
499 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
500 selpad->tags = newtags;
501 GST_OBJECT_UNLOCK (selpad);
504 gst_tag_list_unref (oldtags);
505 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
515 /* Wait until we're the active sink pad or we're flushing */
516 while (!sel->eos && !sel->flushing && !selpad->flushing)
517 GST_INPUT_SELECTOR_WAIT (sel);
519 /* Notify all waiting pads about going EOS now */
521 GST_INPUT_SELECTOR_BROADCAST (sel);
524 selpad->eos_sent = TRUE;
525 GST_DEBUG_OBJECT (pad, "received EOS");
528 GstClockTime ts, dur;
530 GST_DEBUG_OBJECT (pad, "Received gap event: %" GST_PTR_FORMAT, event);
532 gst_event_parse_gap (event, &ts, &dur);
533 if (GST_CLOCK_TIME_IS_VALID (ts)) {
534 if (GST_CLOCK_TIME_IS_VALID (dur))
537 /* update the segment position */
538 GST_OBJECT_LOCK (pad);
539 selpad->segment.position = ts;
540 GST_OBJECT_UNLOCK (pad);
541 if (sel->sync_streams && active_sinkpad == pad)
542 GST_INPUT_SELECTOR_BROADCAST (sel);
550 GST_INPUT_SELECTOR_UNLOCK (sel);
552 g_object_notify (G_OBJECT (selpad), "tags");
554 GST_DEBUG_OBJECT (pad, "forwarding event");
555 res = gst_pad_push_event (sel->srcpad, event);
557 /* If we aren't forwarding the event because the pad is not the
558 * active_sinkpad, then set the flag on the pad
559 * that says a segment needs sending if/when that pad is activated.
560 * For all other cases, we send the event immediately, which makes
561 * sparse streams and other segment updates work correctly downstream.
563 if (GST_EVENT_IS_STICKY (event))
564 selpad->events_pending = TRUE;
565 gst_event_unref (event);
572 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
574 gboolean res = FALSE;
575 GstInputSelector *self = (GstInputSelector *) parent;
577 switch (GST_QUERY_TYPE (query)) {
579 /* always proxy caps query, regardless of active pad or not */
580 res = gst_pad_peer_query (self->srcpad, query);
582 case GST_QUERY_ALLOCATION:{
583 GstPad *active_sinkpad;
584 GstInputSelector *sel = GST_INPUT_SELECTOR (parent);
586 /* Only do the allocation query for the active sinkpad,
587 * after switching a reconfigure event is sent and upstream
588 * should reconfigure and do a new allocation query
590 if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
591 GST_INPUT_SELECTOR_LOCK (sel);
592 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
593 GST_INPUT_SELECTOR_UNLOCK (sel);
595 if (pad != active_sinkpad) {
603 res = gst_pad_query_default (pad, parent, query);
612 gst_input_selector_get_clipped_running_time (GstSegment * seg, GstBuffer * buf)
614 GstClockTime running_time;
616 running_time = GST_BUFFER_PTS (buf);
617 /* If possible try to get the running time at the end of the buffer */
618 if (GST_BUFFER_DURATION_IS_VALID (buf))
619 running_time += GST_BUFFER_DURATION (buf);
620 /* Only use the segment to convert to running time if the segment is
621 * in TIME format, otherwise do our best to try to sync */
622 if (GST_CLOCK_TIME_IS_VALID (seg->stop)) {
623 if (running_time > seg->stop) {
624 running_time = seg->stop;
627 return gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
630 /* must be called without the SELECTOR_LOCK, will wait until the running time
631 * of the active pad is after this pad or return TRUE when flushing */
633 gst_input_selector_wait_running_time (GstInputSelector * sel,
634 GstSelectorPad * selpad, GstBuffer * buf)
638 GST_DEBUG_OBJECT (selpad, "entering wait for buffer %p", buf);
640 /* If we have no valid timestamp we can't sync this buffer */
641 if (!GST_BUFFER_PTS_IS_VALID (buf)) {
642 GST_DEBUG_OBJECT (selpad, "leaving wait for buffer with "
643 "invalid timestamp");
647 seg = &selpad->segment;
650 * a) this is the active pad
651 * b) the pad or the selector is flushing
652 * c) the buffer running time is before the current running time
653 * (either active-seg or clock, depending on sync-mode)
656 GST_INPUT_SELECTOR_LOCK (sel);
658 GstPad *active_sinkpad;
659 GstSelectorPad *active_selpad;
661 gint64 cur_running_time;
662 GstClockTime running_time;
664 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
665 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
667 if (seg->format != GST_FORMAT_TIME) {
668 GST_DEBUG_OBJECT (selpad,
669 "Not waiting because we don't have a TIME segment");
670 GST_INPUT_SELECTOR_UNLOCK (sel);
674 running_time = gst_input_selector_get_clipped_running_time (seg, buf);
675 /* If this is outside the segment don't sync */
676 if (running_time == -1) {
677 GST_DEBUG_OBJECT (selpad,
678 "Not waiting because buffer is outside segment");
679 GST_INPUT_SELECTOR_UNLOCK (sel);
683 cur_running_time = GST_CLOCK_TIME_NONE;
684 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
685 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
687 GstClockTime base_time;
689 cur_running_time = gst_clock_get_time (clock);
690 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
691 if (base_time <= cur_running_time)
692 cur_running_time -= base_time;
694 cur_running_time = 0;
696 gst_object_unref (clock);
699 GstSegment *active_seg;
701 active_seg = &active_selpad->segment;
703 /* If the active segment is configured but not to time format
704 * we can't do any syncing at all */
705 if (active_seg->format != GST_FORMAT_TIME
706 && active_seg->format != GST_FORMAT_UNDEFINED) {
707 GST_DEBUG_OBJECT (selpad,
708 "Not waiting because active segment isn't in TIME format");
709 GST_INPUT_SELECTOR_UNLOCK (sel);
713 /* Get active pad's running time, if no configured segment yet keep at -1 */
714 if (active_seg->format == GST_FORMAT_TIME)
715 cur_running_time = gst_segment_to_running_time (active_seg,
716 GST_FORMAT_TIME, active_seg->position);
719 if (selpad != active_selpad && !sel->eos && !sel->flushing
720 && !selpad->flushing && (cur_running_time == GST_CLOCK_TIME_NONE
721 || running_time >= cur_running_time)) {
722 GST_DEBUG_OBJECT (selpad,
723 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
724 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
725 GST_TIME_ARGS (cur_running_time));
726 GST_INPUT_SELECTOR_WAIT (sel);
728 GST_INPUT_SELECTOR_UNLOCK (sel);
733 /* Return TRUE if the selector or the pad is flushing */
734 return (sel->flushing || selpad->flushing);
738 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
740 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
742 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
743 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
746 e = gst_event_new_segment (seg);
747 gst_event_set_seqnum (e, GST_SELECTOR_PAD_CAST (sinkpad)->segment_seqnum);
749 gst_pad_push_event (sel->srcpad, e);
750 } else if (GST_EVENT_TYPE (*event) == GST_EVENT_STREAM_START
751 && !sel->have_group_id) {
753 gst_pad_get_sticky_event (sel->srcpad, GST_EVENT_STREAM_START, 0);
755 /* Only push stream-start once if not all our streams have a stream-id */
757 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
759 gst_event_unref (tmp);
762 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
768 #if DEBUG_CACHED_BUFFERS
770 gst_input_selector_debug_cached_buffers (GstInputSelector * sel)
774 if (gst_debug_category_get_threshold (input_selector_debug) < GST_LEVEL_DEBUG)
777 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = walk->next) {
778 GstSelectorPad *selpad;
782 selpad = GST_SELECTOR_PAD_CAST (walk->data);
783 if (!selpad->cached_buffers) {
784 GST_DEBUG_OBJECT (selpad, "Cached buffers timestamps: <none>");
788 timestamps = g_string_new ("Cached buffers timestamps:");
789 for (l = selpad->cached_buffers->head; l != NULL; l = l->next) {
790 GstSelectorPadCachedBuffer *cached_buffer = l->data;
792 g_string_append_printf (timestamps, " %" GST_TIME_FORMAT,
793 GST_TIME_ARGS (GST_BUFFER_PTS (cached_buffer->buffer)));
795 GST_DEBUG_OBJECT (selpad, "%s", timestamps->str);
796 g_string_free (timestamps, TRUE);
801 /* must be called with the SELECTOR_LOCK */
803 gst_input_selector_cleanup_old_cached_buffers (GstInputSelector * sel,
807 gint64 cur_running_time;
810 cur_running_time = GST_CLOCK_TIME_NONE;
811 if (sel->sync_mode == GST_INPUT_SELECTOR_SYNC_MODE_CLOCK) {
812 clock = gst_element_get_clock (GST_ELEMENT_CAST (sel));
814 GstClockTime base_time;
816 cur_running_time = gst_clock_get_time (clock);
817 base_time = gst_element_get_base_time (GST_ELEMENT_CAST (sel));
818 if (base_time <= cur_running_time)
819 cur_running_time -= base_time;
821 cur_running_time = 0;
823 gst_object_unref (clock);
826 GstPad *active_sinkpad;
827 GstSelectorPad *active_selpad;
828 GstSegment *active_seg;
830 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
831 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
832 active_seg = &active_selpad->segment;
834 /* Get active pad's running time, if no configured segment yet keep at -1 */
835 if (active_seg->format == GST_FORMAT_TIME)
836 cur_running_time = gst_segment_to_running_time (active_seg,
837 GST_FORMAT_TIME, active_seg->position);
840 if (!GST_CLOCK_TIME_IS_VALID (cur_running_time))
843 GST_DEBUG_OBJECT (sel, "Cleaning up old cached buffers");
844 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
845 GstSelectorPad *selpad;
847 GstSelectorPadCachedBuffer *cached_buffer;
848 GSList *maybe_remove;
849 guint queue_position;
851 selpad = GST_SELECTOR_PAD_CAST (walk->data);
852 if (!selpad->cached_buffers)
855 seg = &selpad->segment;
859 while ((cached_buffer = g_queue_peek_nth (selpad->cached_buffers,
861 GstBuffer *buffer = cached_buffer->buffer;
862 GstClockTime running_time;
865 /* If we have no valid timestamp we can't sync this buffer */
866 if (!GST_BUFFER_PTS_IS_VALID (buffer)) {
867 maybe_remove = g_slist_append (maybe_remove, cached_buffer);
868 queue_position = g_slist_length (maybe_remove);
872 /* the buffer is still valid if its duration is valid and the
873 * timestamp + duration is >= time, or if its duration is invalid
874 * and the timestamp is >= time */
875 running_time = gst_input_selector_get_clipped_running_time (seg, buffer);
876 GST_DEBUG_OBJECT (selpad,
877 "checking if buffer %p running time=%" GST_TIME_FORMAT
878 " >= stream time=%" GST_TIME_FORMAT, buffer,
879 GST_TIME_ARGS (running_time), GST_TIME_ARGS (cur_running_time));
880 if (running_time >= cur_running_time) {
884 GST_DEBUG_OBJECT (selpad, "Removing old cached buffer %p", buffer);
885 g_queue_pop_nth (selpad->cached_buffers, queue_position);
886 gst_selector_pad_free_cached_buffer (cached_buffer);
888 for (l = maybe_remove; l != NULL; l = g_slist_next (l)) {
889 /* A buffer after some invalid buffers was removed, it means the invalid buffers
890 * are old, lets also remove them */
891 cached_buffer = l->data;
892 g_queue_remove (selpad->cached_buffers, cached_buffer);
893 gst_selector_pad_free_cached_buffer (cached_buffer);
896 g_slist_free (maybe_remove);
901 g_slist_free (maybe_remove);
904 if (g_queue_is_empty (selpad->cached_buffers)) {
905 g_queue_free (selpad->cached_buffers);
906 selpad->cached_buffers = NULL;
910 #if DEBUG_CACHED_BUFFERS
911 gst_input_selector_debug_cached_buffers (sel);
916 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
918 GstInputSelector *sel;
920 GstPad *active_sinkpad;
921 GstPad *prev_active_sinkpad = NULL;
922 GstSelectorPad *selpad;
924 sel = GST_INPUT_SELECTOR (parent);
925 selpad = GST_SELECTOR_PAD_CAST (pad);
927 GST_DEBUG_OBJECT (selpad,
928 "entering chain for buf %p with timestamp %" GST_TIME_FORMAT, buf,
929 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
931 GST_INPUT_SELECTOR_LOCK (sel);
934 GST_INPUT_SELECTOR_UNLOCK (sel);
939 GST_INPUT_SELECTOR_UNLOCK (sel);
943 GST_LOG_OBJECT (pad, "getting active pad");
945 prev_active_sinkpad =
946 sel->active_sinkpad ? gst_object_ref (sel->active_sinkpad) : NULL;
947 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
949 /* In sync mode wait until the active pad has advanced
950 * after the running time of the current buffer */
951 if (sel->sync_streams) {
952 /* call chain for each cached buffer if we are not the active pad
953 * or if we are the active pad but didn't push anything yet. */
954 if (active_sinkpad != pad || !selpad->pushed) {
955 /* no need to check for sel->cache_buffers as selpad->cached_buffers
956 * will only be valid if cache_buffers is TRUE */
957 if (selpad->cached_buffers && !selpad->sending_cached_buffers) {
958 GstSelectorPadCachedBuffer *cached_buffer;
959 GstSegment saved_segment;
961 saved_segment = selpad->segment;
963 selpad->sending_cached_buffers = TRUE;
964 while (!sel->eos && !sel->flushing && !selpad->flushing &&
965 (cached_buffer = g_queue_pop_head (selpad->cached_buffers))) {
966 GST_DEBUG_OBJECT (pad, "Cached buffers found, "
967 "invoking chain for cached buffer %p", cached_buffer->buffer);
969 selpad->segment = cached_buffer->segment;
970 selpad->events_pending = TRUE;
971 GST_INPUT_SELECTOR_UNLOCK (sel);
972 gst_selector_pad_chain (pad, parent, cached_buffer->buffer);
973 GST_INPUT_SELECTOR_LOCK (sel);
975 /* We just passed the ownership of the buffer to the chain function */
976 cached_buffer->buffer = NULL;
977 gst_selector_pad_free_cached_buffer (cached_buffer);
979 /* we may have cleaned up the queue in the meantime because of
981 if (!selpad->cached_buffers) {
985 selpad->sending_cached_buffers = FALSE;
987 /* all cached buffers sent, restore segment for current buffer */
988 selpad->segment = saved_segment;
989 selpad->events_pending = TRUE;
991 /* Might have changed while calling chain for cached buffers */
992 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
996 if (active_sinkpad != pad) {
997 GST_INPUT_SELECTOR_UNLOCK (sel);
998 if (gst_input_selector_wait_running_time (sel, selpad, buf))
1000 GST_INPUT_SELECTOR_LOCK (sel);
1003 /* Might have changed while waiting */
1004 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1008 GST_INPUT_SELECTOR_UNLOCK (sel);
1012 /* update the segment on the srcpad */
1013 if (GST_BUFFER_PTS_IS_VALID (buf)) {
1014 GstClockTime start_time = GST_BUFFER_PTS (buf);
1016 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
1017 GST_TIME_ARGS (start_time));
1018 if (GST_BUFFER_DURATION_IS_VALID (buf))
1019 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
1020 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
1022 GST_OBJECT_LOCK (pad);
1023 selpad->segment.position = start_time;
1024 GST_OBJECT_UNLOCK (pad);
1027 /* Ignore buffers from pads except the selected one */
1028 if (pad != active_sinkpad)
1031 /* Tell all non-active pads that we advanced the running time */
1032 if (sel->sync_streams)
1033 GST_INPUT_SELECTOR_BROADCAST (sel);
1035 GST_INPUT_SELECTOR_UNLOCK (sel);
1037 if (prev_active_sinkpad != active_sinkpad) {
1038 if (prev_active_sinkpad)
1039 g_object_notify (G_OBJECT (prev_active_sinkpad), "active");
1040 g_object_notify (G_OBJECT (active_sinkpad), "active");
1041 g_object_notify (G_OBJECT (sel), "active-pad");
1044 /* if we have a pending events, push them now */
1045 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
1046 || selpad->events_pending)) {
1047 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
1049 selpad->events_pending = FALSE;
1052 if (prev_active_sinkpad) {
1053 gst_object_unref (prev_active_sinkpad);
1054 prev_active_sinkpad = NULL;
1057 if (selpad->discont) {
1058 buf = gst_buffer_make_writable (buf);
1060 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
1061 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1062 selpad->discont = FALSE;
1066 GST_LOG_OBJECT (pad, "Forwarding buffer %p with timestamp %" GST_TIME_FORMAT,
1067 buf, GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
1069 /* Only make the buffer read-only when necessary */
1070 if (sel->sync_streams && sel->cache_buffers)
1071 buf = gst_buffer_ref (buf);
1072 res = gst_pad_push (sel->srcpad, buf);
1073 GST_LOG_OBJECT (pad, "Buffer %p forwarded result=%d", buf, res);
1075 GST_INPUT_SELECTOR_LOCK (sel);
1077 if (sel->sync_streams && sel->cache_buffers) {
1078 /* Might have changed while pushing */
1079 active_sinkpad = gst_input_selector_get_active_sinkpad (sel);
1080 /* only set pad to pushed if we are still the active pad */
1081 if (active_sinkpad == pad)
1082 selpad->pushed = TRUE;
1084 /* cache buffer as we may need it again if we change pads */
1085 gst_selector_pad_cache_buffer (selpad, buf);
1086 gst_input_selector_cleanup_old_cached_buffers (sel, pad);
1088 selpad->pushed = TRUE;
1090 GST_INPUT_SELECTOR_UNLOCK (sel);
1094 if (prev_active_sinkpad)
1095 gst_object_unref (prev_active_sinkpad);
1096 prev_active_sinkpad = NULL;
1100 /* dropped buffers */
1103 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
1105 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
1106 /* when we drop a buffer, we're creating a discont on this pad */
1107 selpad->discont = TRUE;
1108 GST_INPUT_SELECTOR_UNLOCK (sel);
1109 gst_buffer_unref (buf);
1111 /* figure out what to return upstream */
1112 GST_OBJECT_LOCK (selpad);
1113 if (selpad->always_ok || !active_pad_pushed)
1116 res = GST_FLOW_NOT_LINKED;
1117 GST_OBJECT_UNLOCK (selpad);
1123 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
1124 gst_buffer_unref (buf);
1125 res = GST_FLOW_FLUSHING;
1130 GST_DEBUG_OBJECT (pad, "We are eos, discard buffer %p", buf);
1131 gst_buffer_unref (buf);
1137 static void gst_input_selector_dispose (GObject * object);
1138 static void gst_input_selector_finalize (GObject * object);
1140 static void gst_input_selector_set_property (GObject * object,
1141 guint prop_id, const GValue * value, GParamSpec * pspec);
1142 static void gst_input_selector_get_property (GObject * object,
1143 guint prop_id, GValue * value, GParamSpec * pspec);
1145 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
1146 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
1147 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
1149 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
1150 element, GstStateChange transition);
1152 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
1156 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
1157 "input-selector", 0, "An input stream selector element");
1158 #define gst_input_selector_parent_class parent_class
1159 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
1163 gst_input_selector_class_init (GstInputSelectorClass * klass)
1165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1166 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
1168 gobject_class->dispose = gst_input_selector_dispose;
1169 gobject_class->finalize = gst_input_selector_finalize;
1171 gobject_class->set_property = gst_input_selector_set_property;
1172 gobject_class->get_property = gst_input_selector_get_property;
1174 g_object_class_install_property (gobject_class, PROP_N_PADS,
1175 g_param_spec_uint ("n-pads", "Number of Pads",
1176 "The number of sink pads", 0, G_MAXUINT, 0,
1177 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1179 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
1180 g_param_spec_object ("active-pad", "Active pad",
1181 "The currently active sink pad", GST_TYPE_PAD,
1182 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
1183 G_PARAM_STATIC_STRINGS));
1186 * GstInputSelector:sync-streams
1188 * If set to %TRUE all inactive streams will be synced to the
1189 * running time of the active stream or to the current clock.
1191 * To make sure no buffers are dropped by input-selector
1192 * that might be needed when switching the active pad,
1193 * sync-mode should be set to "clock" and cache-buffers to TRUE.
1195 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
1196 g_param_spec_boolean ("sync-streams", "Sync Streams",
1197 "Synchronize inactive streams to the running time of the active "
1198 "stream or to the current clock",
1199 DEFAULT_SYNC_STREAMS,
1200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1201 GST_PARAM_MUTABLE_READY));
1204 * GstInputSelector:sync-mode
1206 * Select how input-selector will sync buffers when in sync-streams mode.
1208 * Note that when using the "active-segment" mode, the "active-segment" may
1209 * be ahead of current clock time when switching the active pad, as the current
1210 * active pad may have pushed more buffers than what was displayed/consumed,
1211 * which may cause delays and some missing buffers.
1213 g_object_class_install_property (gobject_class, PROP_SYNC_MODE,
1214 g_param_spec_enum ("sync-mode", "Sync mode",
1215 "Behavior in sync-streams mode", GST_TYPE_INPUT_SELECTOR_SYNC_MODE,
1217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1218 GST_PARAM_MUTABLE_READY));
1221 * GstInputSelector:cache-buffers
1223 * If set to %TRUE and GstInputSelector:sync-streams is also set to %TRUE,
1224 * the active pad will cache the buffers still considered valid (after current
1225 * running time, see sync-mode) to avoid missing frames if/when the pad is
1228 * The active pad may push more buffers than what is currently displayed/consumed
1229 * and when changing pads those buffers will be discarded and the only way to
1230 * reactivate that pad without loosing the already consumed buffers is to enable cache.
1232 g_object_class_install_property (gobject_class, PROP_CACHE_BUFFERS,
1233 g_param_spec_boolean ("cache-buffers", "Cache Buffers",
1234 "Cache buffers for active-pad",
1235 DEFAULT_CACHE_BUFFERS,
1236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
1237 GST_PARAM_MUTABLE_READY));
1239 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
1240 "Generic", "N-to-1 input stream selector",
1241 "Julien Moutte <julien@moutte.net>, "
1242 "Jan Schmidt <thaytan@mad.scientist.com>, "
1243 "Wim Taymans <wim.taymans@gmail.com>");
1244 gst_element_class_add_pad_template (gstelement_class,
1245 gst_static_pad_template_get (&gst_input_selector_sink_factory));
1246 gst_element_class_add_pad_template (gstelement_class,
1247 gst_static_pad_template_get (&gst_input_selector_src_factory));
1249 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
1250 gstelement_class->release_pad = gst_input_selector_release_pad;
1251 gstelement_class->change_state = gst_input_selector_change_state;
1255 gst_input_selector_init (GstInputSelector * sel)
1257 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
1258 gst_pad_set_iterate_internal_links_function (sel->srcpad,
1259 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1260 gst_pad_set_event_function (sel->srcpad,
1261 GST_DEBUG_FUNCPTR (gst_input_selector_event));
1262 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
1263 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
1264 /* sinkpad management */
1265 sel->active_sinkpad = NULL;
1267 sel->sync_streams = DEFAULT_SYNC_STREAMS;
1268 sel->have_group_id = TRUE;
1270 g_mutex_init (&sel->lock);
1271 g_cond_init (&sel->cond);
1274 /* lets give a change for downstream to do something on
1275 * active-pad change before we start pushing new buffers */
1276 g_signal_connect_data (sel, "notify::active-pad",
1277 (GCallback) gst_input_selector_active_pad_changed, NULL,
1278 NULL, G_CONNECT_AFTER);
1282 gst_input_selector_dispose (GObject * object)
1284 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1286 if (sel->active_sinkpad) {
1287 gst_object_unref (sel->active_sinkpad);
1288 sel->active_sinkpad = NULL;
1290 G_OBJECT_CLASS (parent_class)->dispose (object);
1294 gst_input_selector_finalize (GObject * object)
1296 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1298 g_mutex_clear (&sel->lock);
1299 g_cond_clear (&sel->cond);
1301 G_OBJECT_CLASS (parent_class)->finalize (object);
1304 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1305 * active pad changed. */
1307 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
1309 GstSelectorPad *old, *new;
1310 GstPad **active_pad_p;
1312 if (pad == self->active_sinkpad)
1315 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1316 new = GST_SELECTOR_PAD_CAST (pad);
1318 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1319 GST_DEBUG_PAD_NAME (new));
1322 old->pushed = FALSE;
1324 new->pushed = FALSE;
1326 /* Send a new SEGMENT event on the new pad next */
1327 if (old != new && new)
1328 new->events_pending = TRUE;
1330 active_pad_p = &self->active_sinkpad;
1331 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1333 if (old && old != new)
1334 gst_pad_push_event (GST_PAD_CAST (old), gst_event_new_reconfigure ());
1336 gst_pad_push_event (GST_PAD_CAST (new), gst_event_new_reconfigure ());
1338 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1339 self->active_sinkpad);
1341 if (old != new && new && new->eos && !new->eos_sent) {
1343 GST_INPUT_SELECTOR_BROADCAST (self);
1350 gst_input_selector_set_property (GObject * object, guint prop_id,
1351 const GValue * value, GParamSpec * pspec)
1353 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1356 case PROP_ACTIVE_PAD:
1360 pad = g_value_get_object (value);
1362 GST_INPUT_SELECTOR_LOCK (sel);
1364 #if DEBUG_CACHED_BUFFERS
1365 gst_input_selector_debug_cached_buffers (sel);
1368 gst_input_selector_set_active_pad (sel, pad);
1370 #if DEBUG_CACHED_BUFFERS
1371 gst_input_selector_debug_cached_buffers (sel);
1374 GST_INPUT_SELECTOR_UNLOCK (sel);
1377 case PROP_SYNC_STREAMS:
1378 GST_INPUT_SELECTOR_LOCK (sel);
1379 sel->sync_streams = g_value_get_boolean (value);
1380 GST_INPUT_SELECTOR_UNLOCK (sel);
1382 case PROP_SYNC_MODE:
1383 GST_INPUT_SELECTOR_LOCK (sel);
1384 sel->sync_mode = g_value_get_enum (value);
1385 GST_INPUT_SELECTOR_UNLOCK (sel);
1387 case PROP_CACHE_BUFFERS:
1388 GST_INPUT_SELECTOR_LOCK (object);
1389 sel->cache_buffers = g_value_get_boolean (value);
1390 GST_INPUT_SELECTOR_UNLOCK (object);
1393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1399 gst_input_selector_active_pad_changed (GstInputSelector * sel,
1400 GParamSpec * pspec, gpointer user_data)
1402 /* Wake up all non-active pads in sync mode, they might be
1403 * the active pad now */
1404 if (sel->sync_streams)
1405 GST_INPUT_SELECTOR_BROADCAST (sel);
1409 gst_input_selector_get_property (GObject * object, guint prop_id,
1410 GValue * value, GParamSpec * pspec)
1412 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1416 GST_INPUT_SELECTOR_LOCK (object);
1417 g_value_set_uint (value, sel->n_pads);
1418 GST_INPUT_SELECTOR_UNLOCK (object);
1420 case PROP_ACTIVE_PAD:
1421 GST_INPUT_SELECTOR_LOCK (object);
1422 g_value_set_object (value, sel->active_sinkpad);
1423 GST_INPUT_SELECTOR_UNLOCK (object);
1425 case PROP_SYNC_STREAMS:
1426 GST_INPUT_SELECTOR_LOCK (object);
1427 g_value_set_boolean (value, sel->sync_streams);
1428 GST_INPUT_SELECTOR_UNLOCK (object);
1430 case PROP_SYNC_MODE:
1431 GST_INPUT_SELECTOR_LOCK (object);
1432 g_value_set_enum (value, sel->sync_mode);
1433 GST_INPUT_SELECTOR_UNLOCK (object);
1435 case PROP_CACHE_BUFFERS:
1436 GST_INPUT_SELECTOR_LOCK (object);
1437 g_value_set_boolean (value, sel->cache_buffers);
1438 GST_INPUT_SELECTOR_UNLOCK (object);
1441 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1447 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1450 GstPad *otherpad = NULL;
1452 GST_INPUT_SELECTOR_LOCK (sel);
1453 if (pad == sel->srcpad)
1454 otherpad = sel->active_sinkpad;
1455 else if (pad == sel->active_sinkpad || !strict)
1456 otherpad = sel->srcpad;
1458 gst_object_ref (otherpad);
1459 GST_INPUT_SELECTOR_UNLOCK (sel);
1465 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1467 GstInputSelector *sel;
1468 gboolean result = FALSE;
1470 gboolean done = FALSE;
1471 GValue item = { 0, };
1473 GList *pushed_pads = NULL;
1475 sel = GST_INPUT_SELECTOR (parent);
1476 /* Send upstream events to all sinkpads */
1477 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1479 /* This is now essentially a copy of gst_pad_event_default_dispatch
1480 * with a different iterator */
1482 switch (gst_iterator_next (iter, &item)) {
1483 case GST_ITERATOR_OK:
1484 eventpad = g_value_get_object (&item);
1486 /* if already pushed, skip */
1487 if (g_list_find (pushed_pads, eventpad)) {
1488 g_value_reset (&item);
1492 gst_event_ref (event);
1493 result |= gst_pad_push_event (eventpad, event);
1495 g_value_reset (&item);
1497 case GST_ITERATOR_RESYNC:
1498 /* We don't reset the result here because we don't push the event
1499 * again on pads that got the event already and because we need
1500 * to consider the result of the previous pushes */
1501 gst_iterator_resync (iter);
1503 case GST_ITERATOR_ERROR:
1504 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1507 case GST_ITERATOR_DONE:
1512 g_value_unset (&item);
1513 gst_iterator_free (iter);
1515 g_list_free (pushed_pads);
1517 gst_event_unref (event);
1522 /* check if the pad is the active sinkpad */
1523 static inline gboolean
1524 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1528 GST_INPUT_SELECTOR_LOCK (sel);
1529 res = (pad == sel->active_sinkpad);
1530 GST_INPUT_SELECTOR_UNLOCK (sel);
1535 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1537 gst_input_selector_get_active_sinkpad (GstInputSelector * sel)
1539 GstPad *active_sinkpad;
1541 active_sinkpad = sel->active_sinkpad;
1542 if (active_sinkpad == NULL) {
1543 GValue item = G_VALUE_INIT;
1544 GstIterator *iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1545 GstIteratorResult ires;
1547 while ((ires = gst_iterator_next (iter, &item)) == GST_ITERATOR_RESYNC)
1548 gst_iterator_resync (iter);
1549 if (ires == GST_ITERATOR_OK) {
1550 /* If no pad is currently selected, we return the first usable pad to
1551 * guarantee consistency */
1553 active_sinkpad = sel->active_sinkpad = g_value_dup_object (&item);
1554 g_value_reset (&item);
1555 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s",
1556 GST_DEBUG_PAD_NAME (active_sinkpad));
1558 GST_WARNING_OBJECT (sel, "Couldn't find a default sink pad");
1559 gst_iterator_free (iter);
1562 return active_sinkpad;
1566 gst_input_selector_request_new_pad (GstElement * element,
1567 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1569 GstInputSelector *sel;
1571 GstPad *sinkpad = NULL;
1573 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1575 sel = GST_INPUT_SELECTOR (element);
1577 GST_INPUT_SELECTOR_LOCK (sel);
1579 GST_LOG_OBJECT (sel, "Creating new pad sink_%u", sel->padcount);
1580 name = g_strdup_printf ("sink_%u", sel->padcount++);
1581 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1582 "name", name, "direction", templ->direction, "template", templ, NULL);
1587 gst_pad_set_event_function (sinkpad,
1588 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1589 gst_pad_set_query_function (sinkpad,
1590 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1591 gst_pad_set_chain_function (sinkpad,
1592 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1593 gst_pad_set_iterate_internal_links_function (sinkpad,
1594 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1596 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1597 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1598 gst_pad_set_active (sinkpad, TRUE);
1599 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1600 GST_INPUT_SELECTOR_UNLOCK (sel);
1606 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1608 GstInputSelector *sel;
1610 sel = GST_INPUT_SELECTOR (element);
1611 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1613 GST_INPUT_SELECTOR_LOCK (sel);
1614 /* if the pad was the active pad, makes us select a new one */
1615 if (sel->active_sinkpad == pad) {
1616 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1617 gst_object_unref (sel->active_sinkpad);
1618 sel->active_sinkpad = NULL;
1621 GST_INPUT_SELECTOR_UNLOCK (sel);
1623 gst_pad_set_active (pad, FALSE);
1624 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1628 gst_input_selector_reset (GstInputSelector * sel)
1632 GST_INPUT_SELECTOR_LOCK (sel);
1633 /* clear active pad */
1634 if (sel->active_sinkpad) {
1635 gst_object_unref (sel->active_sinkpad);
1636 sel->active_sinkpad = NULL;
1638 /* reset each of our sinkpads state */
1639 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1640 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1642 gst_selector_pad_reset (selpad);
1645 gst_tag_list_unref (selpad->tags);
1646 selpad->tags = NULL;
1649 sel->have_group_id = TRUE;
1650 GST_INPUT_SELECTOR_UNLOCK (sel);
1653 static GstStateChangeReturn
1654 gst_input_selector_change_state (GstElement * element,
1655 GstStateChange transition)
1657 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1658 GstStateChangeReturn result;
1660 switch (transition) {
1661 case GST_STATE_CHANGE_READY_TO_PAUSED:
1662 GST_INPUT_SELECTOR_LOCK (self);
1664 self->flushing = FALSE;
1665 GST_INPUT_SELECTOR_UNLOCK (self);
1667 case GST_STATE_CHANGE_PAUSED_TO_READY:
1668 /* first unlock before we call the parent state change function, which
1669 * tries to acquire the stream lock when going to ready. */
1670 GST_INPUT_SELECTOR_LOCK (self);
1672 self->flushing = TRUE;
1673 GST_INPUT_SELECTOR_BROADCAST (self);
1674 GST_INPUT_SELECTOR_UNLOCK (self);
1680 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1682 switch (transition) {
1683 case GST_STATE_CHANGE_PAUSED_TO_READY:
1684 gst_input_selector_reset (self);