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>)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-input-selector
27 * @see_also: #GstOutputSelector
29 * Direct one out of N input streams to the output pad.
38 #include "gstinputselector.h"
39 #include "gstselector-marshal.h"
41 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
42 #define GST_CAT_DEFAULT input_selector_debug
44 static GstStaticPadTemplate gst_input_selector_sink_factory =
45 GST_STATIC_PAD_TEMPLATE ("sink%d",
50 static GstStaticPadTemplate gst_input_selector_src_factory =
51 GST_STATIC_PAD_TEMPLATE ("src",
65 #define DEFAULT_PAD_ALWAYS_OK TRUE
70 PROP_PAD_RUNNING_TIME,
84 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
86 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
88 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
90 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
92 static gboolean gst_input_selector_check_eos (GstElement * selector);
94 #define GST_TYPE_SELECTOR_PAD \
95 (gst_selector_pad_get_type())
96 #define GST_SELECTOR_PAD(obj) \
97 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
98 #define GST_SELECTOR_PAD_CLASS(klass) \
99 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
100 #define GST_IS_SELECTOR_PAD(obj) \
101 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
102 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
103 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
104 #define GST_SELECTOR_PAD_CAST(obj) \
105 ((GstSelectorPad *)(obj))
107 typedef struct _GstSelectorPad GstSelectorPad;
108 typedef struct _GstSelectorPadClass GstSelectorPadClass;
110 struct _GstSelectorPad
114 gboolean active; /* when buffer have passed the pad */
115 gboolean eos; /* when EOS has been received */
116 gboolean discont; /* after switching we create a discont */
118 GstSegment segment; /* the current segment on the pad */
119 GstTagList *tags; /* last tags received on the pad */
121 gboolean segment_pending;
124 struct _GstSelectorPadClass
129 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
130 static void gst_selector_pad_init (GstSelectorPad * pad);
131 static void gst_selector_pad_finalize (GObject * object);
132 static void gst_selector_pad_get_property (GObject * object,
133 guint prop_id, GValue * value, GParamSpec * pspec);
134 static void gst_selector_pad_set_property (GObject * object,
135 guint prop_id, const GValue * value, GParamSpec * pspec);
137 static GstPadClass *selector_pad_parent_class = NULL;
139 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
140 static void gst_selector_pad_reset (GstSelectorPad * pad);
141 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
142 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
143 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
144 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad);
145 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
146 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
147 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
150 gst_selector_pad_get_type (void)
152 static GType selector_pad_type = 0;
154 if (!selector_pad_type) {
155 static const GTypeInfo selector_pad_info = {
156 sizeof (GstSelectorPadClass),
159 (GClassInitFunc) gst_selector_pad_class_init,
162 sizeof (GstSelectorPad),
164 (GInstanceInitFunc) gst_selector_pad_init,
168 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
169 &selector_pad_info, 0);
171 return selector_pad_type;
175 gst_selector_pad_class_init (GstSelectorPadClass * klass)
177 GObjectClass *gobject_class;
179 gobject_class = (GObjectClass *) klass;
181 selector_pad_parent_class = g_type_class_peek_parent (klass);
183 gobject_class->finalize = gst_selector_pad_finalize;
185 gobject_class->get_property = gst_selector_pad_get_property;
186 gobject_class->set_property = gst_selector_pad_set_property;
188 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
189 g_param_spec_int64 ("running-time", "Running time",
190 "Running time of stream on pad", 0, G_MAXINT64, 0,
191 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
193 g_param_spec_boxed ("tags", "Tags",
194 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
195 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
196 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
197 g_param_spec_boolean ("active", "Active",
198 "If the pad is currently active", FALSE,
199 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
200 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
201 g_param_spec_boolean ("always-ok", "Always OK",
202 "Make an inactive pad return OK instead of NOT_LINKED",
203 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207 gst_selector_pad_init (GstSelectorPad * pad)
209 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
210 gst_selector_pad_reset (pad);
214 gst_selector_pad_finalize (GObject * object)
218 pad = GST_SELECTOR_PAD_CAST (object);
221 gst_tag_list_free (pad->tags);
223 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
227 gst_selector_pad_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
230 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
233 case PROP_PAD_ALWAYS_OK:
234 GST_OBJECT_LOCK (object);
235 spad->always_ok = g_value_get_boolean (value);
236 GST_OBJECT_UNLOCK (object);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 gst_selector_pad_get_property (GObject * object, guint prop_id,
246 GValue * value, GParamSpec * pspec)
248 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
251 case PROP_PAD_RUNNING_TIME:
252 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
255 GST_OBJECT_LOCK (object);
256 g_value_set_boxed (value, spad->tags);
257 GST_OBJECT_UNLOCK (object);
259 case PROP_PAD_ACTIVE:
261 GstInputSelector *sel;
263 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
264 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
265 GST_PAD_CAST (spad)));
266 gst_object_unref (sel);
269 case PROP_PAD_ALWAYS_OK:
270 GST_OBJECT_LOCK (object);
271 g_value_set_boolean (value, spad->always_ok);
272 GST_OBJECT_UNLOCK (object);
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281 gst_selector_pad_get_running_time (GstSelectorPad * pad)
285 GST_OBJECT_LOCK (pad);
287 gint64 last_stop = pad->segment.last_stop;
290 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
293 GST_OBJECT_UNLOCK (pad);
295 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
296 GST_TIME_ARGS (ret));
302 gst_selector_pad_reset (GstSelectorPad * pad)
304 GST_OBJECT_LOCK (pad);
307 pad->segment_pending = FALSE;
308 pad->discont = FALSE;
309 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
310 GST_OBJECT_UNLOCK (pad);
313 /* strictly get the linked pad from the sinkpad. If the pad is active we return
314 * the srcpad else we return NULL */
316 gst_selector_pad_iterate_linked_pads (GstPad * pad)
318 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
322 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
323 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
324 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
327 gst_object_unref (otherpad);
328 gst_object_unref (sel);
334 gst_selector_pad_event (GstPad * pad, GstEvent * event)
337 gboolean forward = TRUE;
338 GstInputSelector *sel;
339 GstSelectorPad *selpad;
340 GstPad *prev_active_sinkpad;
341 GstPad *active_sinkpad;
343 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
344 selpad = GST_SELECTOR_PAD_CAST (pad);
346 GST_INPUT_SELECTOR_LOCK (sel);
347 prev_active_sinkpad = sel->active_sinkpad;
348 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
350 /* only forward if we are dealing with the active sinkpad or if select_all
352 if (pad != active_sinkpad && !sel->select_all)
354 GST_INPUT_SELECTOR_UNLOCK (sel);
356 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
357 g_object_notify (G_OBJECT (sel), "active-pad");
359 switch (GST_EVENT_TYPE (event)) {
360 case GST_EVENT_FLUSH_START:
361 /* FIXME, flush out the waiter */
363 case GST_EVENT_FLUSH_STOP:
364 GST_INPUT_SELECTOR_LOCK (sel);
365 gst_selector_pad_reset (selpad);
366 sel->pending_close = FALSE;
367 GST_INPUT_SELECTOR_UNLOCK (sel);
369 case GST_EVENT_NEWSEGMENT:
374 gint64 start, stop, time;
376 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
377 &start, &stop, &time);
379 GST_DEBUG_OBJECT (pad,
380 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
382 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
383 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
385 GST_INPUT_SELECTOR_LOCK (sel);
386 GST_OBJECT_LOCK (selpad);
387 gst_segment_set_newsegment_full (&selpad->segment, update,
388 rate, arate, format, start, stop, time);
389 GST_OBJECT_UNLOCK (selpad);
391 /* If we aren't forwarding the event (because the pad is not the
392 * active_sinkpad, and select_all is not set, then set the flag on the
393 * that says a segment needs sending if/when that pad is activated.
394 * For all other cases, we send the event immediately, which makes
395 * sparse streams and other segment updates work correctly downstream.
398 selpad->segment_pending = TRUE;
400 GST_INPUT_SELECTOR_UNLOCK (sel);
405 GstTagList *tags, *oldtags, *newtags;
407 gst_event_parse_tag (event, &tags);
409 GST_OBJECT_LOCK (selpad);
410 oldtags = selpad->tags;
412 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
413 selpad->tags = newtags;
415 gst_tag_list_free (oldtags);
416 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
417 GST_OBJECT_UNLOCK (selpad);
419 g_object_notify (G_OBJECT (selpad), "tags");
424 GST_DEBUG_OBJECT (pad, "received EOS");
425 /* don't forward eos in select_all mode until all sink pads have eos */
426 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
434 GST_DEBUG_OBJECT (pad, "forwarding event");
435 res = gst_pad_push_event (sel->srcpad, event);
437 gst_event_unref (event);
439 gst_object_unref (sel);
445 gst_selector_pad_getcaps (GstPad * pad)
447 GstInputSelector *sel;
450 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
452 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
453 caps = gst_pad_peer_get_caps_reffed (sel->srcpad);
455 caps = gst_caps_new_any ();
457 gst_object_unref (sel);
463 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
465 GstInputSelector *sel;
468 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
470 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
471 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
472 gst_object_unref (sel);
478 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
479 guint size, GstCaps * caps, GstBuffer ** buf)
481 GstInputSelector *sel;
482 GstFlowReturn result;
483 GstPad *active_sinkpad;
484 GstPad *prev_active_sinkpad;
485 GstSelectorPad *selpad;
487 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
488 selpad = GST_SELECTOR_PAD_CAST (pad);
490 GST_LOG_OBJECT (pad, "received alloc");
492 GST_INPUT_SELECTOR_LOCK (sel);
493 prev_active_sinkpad = sel->active_sinkpad;
494 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
496 if (pad != active_sinkpad)
499 GST_INPUT_SELECTOR_UNLOCK (sel);
501 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
502 g_object_notify (G_OBJECT (sel), "active-pad");
504 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
507 gst_object_unref (sel);
514 GST_INPUT_SELECTOR_UNLOCK (sel);
516 /* unselected pad, perform fallback alloc or return unlinked when
518 GST_OBJECT_LOCK (selpad);
519 if (selpad->always_ok) {
520 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
522 result = GST_FLOW_OK;
524 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
525 result = GST_FLOW_NOT_LINKED;
527 GST_OBJECT_UNLOCK (selpad);
533 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
534 * or return TRUE when flushing */
536 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
538 while (self->blocked && !self->flushing) {
539 /* we can be unlocked here when we are shutting down (flushing) or when we
541 GST_INPUT_SELECTOR_WAIT (self);
543 return self->flushing;
547 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
549 GstInputSelector *sel;
551 GstPad *active_sinkpad;
552 GstPad *prev_active_sinkpad;
553 GstSelectorPad *selpad;
554 GstClockTime start_time;
556 GstEvent *close_event = NULL, *start_event = NULL;
559 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
560 selpad = GST_SELECTOR_PAD_CAST (pad);
561 seg = &selpad->segment;
563 GST_INPUT_SELECTOR_LOCK (sel);
564 /* wait or check for flushing */
565 if (gst_input_selector_wait (sel, pad))
568 GST_LOG_OBJECT (pad, "getting active pad");
570 prev_active_sinkpad = sel->active_sinkpad;
571 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
573 /* update the segment on the srcpad */
574 start_time = GST_BUFFER_TIMESTAMP (buf);
575 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
576 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
577 GST_TIME_ARGS (start_time));
578 if (GST_BUFFER_DURATION_IS_VALID (buf))
579 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
580 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
582 GST_OBJECT_LOCK (pad);
583 gst_segment_set_last_stop (seg, seg->format, start_time);
584 GST_OBJECT_UNLOCK (pad);
587 /* Ignore buffers from pads except the selected one */
588 if (pad != active_sinkpad)
591 if (G_UNLIKELY (sel->pending_close)) {
592 GstSegment *cseg = &sel->segment;
594 GST_DEBUG_OBJECT (sel,
595 "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
597 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
598 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
599 cseg->start, cseg->stop, cseg->time);
601 /* create update segment */
602 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
603 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
605 sel->pending_close = FALSE;
607 /* if we have a pending segment, push it out now */
608 if (G_UNLIKELY (selpad->segment_pending)) {
609 GST_DEBUG_OBJECT (pad,
610 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
612 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
613 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
614 seg->start, seg->stop, seg->time);
616 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
617 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
619 selpad->segment_pending = FALSE;
621 GST_INPUT_SELECTOR_UNLOCK (sel);
623 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
624 g_object_notify (G_OBJECT (sel), "active-pad");
627 gst_pad_push_event (sel->srcpad, close_event);
630 gst_pad_push_event (sel->srcpad, start_event);
632 if (selpad->discont) {
633 buf = gst_buffer_make_metadata_writable (buf);
635 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
636 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
637 selpad->discont = FALSE;
641 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
643 if ((caps = GST_BUFFER_CAPS (buf))) {
644 if (GST_PAD_CAPS (sel->srcpad) != caps)
645 gst_pad_set_caps (sel->srcpad, caps);
648 res = gst_pad_push (sel->srcpad, buf);
651 gst_object_unref (sel);
654 /* dropped buffers */
657 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
658 /* when we drop a buffer, we're creating a discont on this pad */
659 selpad->discont = TRUE;
660 GST_INPUT_SELECTOR_UNLOCK (sel);
661 gst_buffer_unref (buf);
663 /* figure out what to return upstream */
664 GST_OBJECT_LOCK (selpad);
665 if (selpad->always_ok)
668 res = GST_FLOW_NOT_LINKED;
669 GST_OBJECT_UNLOCK (selpad);
675 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
676 GST_INPUT_SELECTOR_UNLOCK (sel);
677 gst_buffer_unref (buf);
678 res = GST_FLOW_WRONG_STATE;
683 static void gst_input_selector_dispose (GObject * object);
685 static void gst_input_selector_set_property (GObject * object,
686 guint prop_id, const GValue * value, GParamSpec * pspec);
687 static void gst_input_selector_get_property (GObject * object,
688 guint prop_id, GValue * value, GParamSpec * pspec);
690 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
691 GstPadTemplate * templ, const gchar * unused);
692 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
694 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
695 element, GstStateChange transition);
697 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
698 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
699 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
700 static gint64 gst_input_selector_block (GstInputSelector * self);
701 static void gst_input_selector_switch (GstInputSelector * self,
702 GstPad * pad, gint64 stop_time, gint64 start_time);
704 #define _do_init(bla) \
705 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
706 "input-selector", 0, "An input stream selector element");
708 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
709 GST_TYPE_ELEMENT, _do_init);
712 gst_input_selector_base_init (gpointer g_class)
714 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
716 gst_element_class_set_details_simple (element_class, "Input selector",
717 "Generic", "N-to-1 input stream selectoring",
718 "Julien Moutte <julien@moutte.net>, "
719 "Jan Schmidt <thaytan@mad.scientist.com>, "
720 "Wim Taymans <wim.taymans@gmail.com>");
721 gst_element_class_add_pad_template (element_class,
722 gst_static_pad_template_get (&gst_input_selector_sink_factory));
723 gst_element_class_add_pad_template (element_class,
724 gst_static_pad_template_get (&gst_input_selector_src_factory));
728 gst_input_selector_class_init (GstInputSelectorClass * klass)
730 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
731 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
733 parent_class = g_type_class_peek_parent (klass);
735 gobject_class->dispose = gst_input_selector_dispose;
737 gobject_class->set_property = gst_input_selector_set_property;
738 gobject_class->get_property = gst_input_selector_get_property;
740 g_object_class_install_property (gobject_class, PROP_N_PADS,
741 g_param_spec_uint ("n-pads", "Number of Pads",
742 "The number of sink pads", 0, G_MAXUINT, 0,
743 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
745 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
746 g_param_spec_object ("active-pad", "Active pad",
747 "The currently active sink pad", GST_TYPE_PAD,
748 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
750 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
751 g_param_spec_boolean ("select-all", "Select all mode",
752 "Forwards data from all input pads", FALSE,
753 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
756 * GstInputSelector::block:
757 * @inputselector: the #GstInputSelector
759 * Block all sink pads in preparation for a switch. Returns the stop time of
760 * the current switch segment, as a running time, or 0 if there is no current
761 * active pad or the current active pad never received data.
763 gst_input_selector_signals[SIGNAL_BLOCK] =
764 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
765 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
766 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
767 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
769 * GstInputSelector::switch:
770 * @inputselector: the #GstInputSelector
771 * @pad: the pad to switch to
772 * @stop_time: running time at which to close the previous segment, or -1
773 * to use the running time of the previously active sink pad
774 * @start_time: running time at which to start the new segment, or -1 to
775 * use the running time of the newly active sink pad
777 * Switch to a new feed. The segment opened by the previously active pad, if
778 * any, will be closed, and a new segment opened before data flows again.
780 * This signal must be emitted when the element has been blocked via the <link
781 * linkend="GstInputSelector-block">block</link> signal.
783 * If you have a stream with only one switch element, such as an audio-only
784 * stream, a stream switch should be performed by first emitting the block
785 * signal, and then emitting the switch signal with -1 for the stop and start
788 * The intention of the @stop_time and @start_time arguments is to allow
789 * multiple switch elements to switch and maintain stream synchronization.
790 * When switching a stream with multiple feeds, you will need as many switch
791 * elements as you have feeds. For example, a feed with audio and video will
792 * have one switch element between the audio feeds and one for video.
794 * A switch over multiple switch elements should be performed as follows:
795 * First, emit the <link linkend="GstInputSelector-block">block</link>
796 * signal, collecting the returned values. The maximum running time returned
797 * by block should then be used as the time at which to close the previous
800 * Then, query the running times of the new audio and video pads that you will
801 * switch to. Naturally, these pads are on separate switch elements. Take the
802 * minimum running time for those streams and use it for the time at which to
803 * open the new segment.
805 * If @pad is the same as the current active pad, the element will cancel any
806 * previous block without adjusting segments.
809 * the signal changed from accepting the pad name to the pad object.
814 gst_input_selector_signals[SIGNAL_SWITCH] =
815 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
816 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
817 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
818 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
820 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
821 gstelement_class->release_pad = gst_input_selector_release_pad;
822 gstelement_class->change_state = gst_input_selector_change_state;
824 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
825 /* note the underscore because switch is a keyword otherwise */
826 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
830 gst_input_selector_init (GstInputSelector * sel,
831 GstInputSelectorClass * g_class)
833 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
834 gst_pad_set_iterate_internal_links_function (sel->srcpad,
835 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
836 gst_pad_set_getcaps_function (sel->srcpad,
837 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
838 gst_pad_set_query_function (sel->srcpad,
839 GST_DEBUG_FUNCPTR (gst_input_selector_query));
840 gst_pad_set_event_function (sel->srcpad,
841 GST_DEBUG_FUNCPTR (gst_input_selector_event));
842 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
843 /* sinkpad management */
844 sel->active_sinkpad = NULL;
846 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
848 sel->lock = g_mutex_new ();
849 sel->cond = g_cond_new ();
850 sel->blocked = FALSE;
852 sel->select_all = FALSE;
856 gst_input_selector_dispose (GObject * object)
858 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
860 if (sel->active_sinkpad) {
861 gst_object_unref (sel->active_sinkpad);
862 sel->active_sinkpad = NULL;
865 g_mutex_free (sel->lock);
869 g_cond_free (sel->cond);
873 G_OBJECT_CLASS (parent_class)->dispose (object);
876 /* Solve the following equation for B.timestamp, and set that as the segment
878 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
881 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
883 if (running_time <= segment->accum)
884 return segment->start;
886 return (running_time - segment->accum) * segment->abs_rate + segment->start;
890 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
892 segment->stop = gst_segment_get_timestamp (segment, running_time);
893 segment->last_stop = -1;
897 gst_segment_set_start (GstSegment * segment, gint64 running_time)
899 gint64 new_start, duration;
901 new_start = gst_segment_get_timestamp (segment, running_time);
903 /* this is the duration we skipped */
904 duration = new_start - segment->start;
905 /* add the duration to the accumulated segment time */
906 segment->accum += duration;
907 /* move position in the segment */
908 segment->time += duration;
909 segment->start += duration;
912 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
913 * active pad changed. */
915 gst_input_selector_set_active_pad (GstInputSelector * self,
916 GstPad * pad, gint64 stop_time, gint64 start_time)
918 GstSelectorPad *old, *new;
919 GstPad **active_pad_p;
921 if (pad == self->active_sinkpad)
924 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
925 new = GST_SELECTOR_PAD_CAST (pad);
927 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
928 GST_DEBUG_PAD_NAME (new));
930 if (stop_time == -1 && old) {
931 /* no stop time given, get the latest running_time on the active pad to
932 * close and open the new segment */
933 stop_time = start_time = gst_selector_pad_get_running_time (old);
934 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
938 if (old && old->active && !self->pending_close && stop_time >= 0) {
939 /* schedule a last_stop update if one isn't already scheduled, and a
940 segment has been pushed before. */
941 memcpy (&self->segment, &old->segment, sizeof (self->segment));
943 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
945 gst_segment_set_stop (&self->segment, stop_time);
946 self->pending_close = TRUE;
949 if (new && new->active && start_time >= 0) {
950 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
952 /* schedule a new segment push */
953 gst_segment_set_start (&new->segment, start_time);
954 new->segment_pending = TRUE;
957 active_pad_p = &self->active_sinkpad;
958 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
959 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
960 self->active_sinkpad);
966 gst_input_selector_set_property (GObject * object, guint prop_id,
967 const GValue * value, GParamSpec * pspec)
969 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
972 case PROP_ACTIVE_PAD:
976 pad = g_value_get_object (value);
978 GST_INPUT_SELECTOR_LOCK (sel);
979 gst_input_selector_set_active_pad (sel, pad,
980 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
981 GST_INPUT_SELECTOR_UNLOCK (sel);
984 case PROP_SELECT_ALL:
985 GST_INPUT_SELECTOR_LOCK (object);
986 sel->select_all = g_value_get_boolean (value);
987 GST_INPUT_SELECTOR_UNLOCK (object);
990 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
996 gst_input_selector_get_property (GObject * object, guint prop_id,
997 GValue * value, GParamSpec * pspec)
999 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1003 GST_INPUT_SELECTOR_LOCK (object);
1004 g_value_set_uint (value, sel->n_pads);
1005 GST_INPUT_SELECTOR_UNLOCK (object);
1007 case PROP_ACTIVE_PAD:
1008 GST_INPUT_SELECTOR_LOCK (object);
1009 g_value_set_object (value, sel->active_sinkpad);
1010 GST_INPUT_SELECTOR_UNLOCK (object);
1012 case PROP_SELECT_ALL:
1013 GST_INPUT_SELECTOR_LOCK (object);
1014 g_value_set_boolean (value, sel->select_all);
1015 GST_INPUT_SELECTOR_UNLOCK (object);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1026 GstInputSelector *sel;
1027 GstPad *otherpad = NULL;
1029 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1031 GST_INPUT_SELECTOR_LOCK (sel);
1032 if (pad == sel->srcpad)
1033 otherpad = sel->active_sinkpad;
1034 else if (pad == sel->active_sinkpad || !strict)
1035 otherpad = sel->srcpad;
1037 gst_object_ref (otherpad);
1038 GST_INPUT_SELECTOR_UNLOCK (sel);
1040 gst_object_unref (sel);
1046 gst_input_selector_event (GstPad * pad, GstEvent * event)
1048 gboolean res = FALSE;
1051 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1054 res = gst_pad_push_event (otherpad, event);
1056 gst_object_unref (otherpad);
1058 gst_event_unref (event);
1062 /* query on the srcpad. We override this function because by default it will
1063 * only forward the query to one random sinkpad */
1065 gst_input_selector_query (GstPad * pad, GstQuery * query)
1067 gboolean res = TRUE;
1068 GstInputSelector *sel;
1071 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1073 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1075 switch (GST_QUERY_TYPE (query)) {
1076 case GST_QUERY_LATENCY:
1079 GstClockTime resmin, resmax;
1086 /* assume FALSE, we become TRUE if one query succeeds */
1089 /* perform the query on all sinkpads and combine the results. We take the
1090 * max of min and the min of max for the result latency. */
1091 GST_INPUT_SELECTOR_LOCK (sel);
1092 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1093 walk = g_list_next (walk)) {
1094 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1096 if (gst_pad_peer_query (sinkpad, query)) {
1097 GstClockTime min, max;
1100 /* one query succeeded, we succeed too */
1103 gst_query_parse_latency (query, &live, &min, &max);
1105 GST_DEBUG_OBJECT (sinkpad,
1106 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1107 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1114 else if (max < resmax)
1116 if (reslive == FALSE)
1121 GST_INPUT_SELECTOR_UNLOCK (sel);
1123 gst_query_set_latency (query, reslive, resmin, resmax);
1125 GST_DEBUG_OBJECT (sel,
1126 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1127 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1135 res = gst_pad_peer_query (otherpad, query);
1139 gst_object_unref (otherpad);
1140 gst_object_unref (sel);
1146 gst_input_selector_getcaps (GstPad * pad)
1152 parent = gst_object_get_parent (GST_OBJECT (pad));
1154 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1157 if (GST_INPUT_SELECTOR (parent)->select_all) {
1158 GST_DEBUG_OBJECT (parent,
1159 "Pad %s:%s not linked, returning merge of caps",
1160 GST_DEBUG_PAD_NAME (pad));
1161 caps = gst_pad_proxy_getcaps (pad);
1163 GST_DEBUG_OBJECT (parent,
1164 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1165 caps = gst_caps_new_any ();
1168 GST_DEBUG_OBJECT (parent,
1169 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1170 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1171 /* if the peer has caps, use those. If the pad is not linked, this function
1172 * returns NULL and we return ANY */
1173 if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1174 caps = gst_caps_new_any ();
1175 gst_object_unref (otherpad);
1178 gst_object_unref (parent);
1182 /* check if the pad is the active sinkpad */
1183 static inline gboolean
1184 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1188 GST_INPUT_SELECTOR_LOCK (sel);
1189 res = (pad == sel->active_sinkpad);
1190 GST_INPUT_SELECTOR_UNLOCK (sel);
1195 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1197 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1199 GstPad *active_sinkpad;
1200 GstSelectorPad *selpad;
1202 selpad = GST_SELECTOR_PAD_CAST (pad);
1204 selpad->active = TRUE;
1205 active_sinkpad = sel->active_sinkpad;
1206 if (active_sinkpad == NULL || sel->select_all) {
1207 /* first pad we get activity on becomes the activated pad by default, if we
1208 * select all, we also remember the last used pad. */
1209 if (sel->active_sinkpad)
1210 gst_object_unref (sel->active_sinkpad);
1211 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1212 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1215 return active_sinkpad;
1219 gst_input_selector_request_new_pad (GstElement * element,
1220 GstPadTemplate * templ, const gchar * unused)
1222 GstInputSelector *sel;
1224 GstPad *sinkpad = NULL;
1226 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1228 sel = GST_INPUT_SELECTOR (element);
1230 GST_INPUT_SELECTOR_LOCK (sel);
1232 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1233 name = g_strdup_printf ("sink%d", sel->padcount++);
1234 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1235 "name", name, "direction", templ->direction, "template", templ, NULL);
1240 gst_pad_set_event_function (sinkpad,
1241 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1242 gst_pad_set_getcaps_function (sinkpad,
1243 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1244 gst_pad_set_acceptcaps_function (sinkpad,
1245 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1246 gst_pad_set_chain_function (sinkpad,
1247 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1248 gst_pad_set_iterate_internal_links_function (sinkpad,
1249 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1250 gst_pad_set_bufferalloc_function (sinkpad,
1251 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1253 gst_pad_set_active (sinkpad, TRUE);
1254 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1255 GST_INPUT_SELECTOR_UNLOCK (sel);
1261 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1263 GstInputSelector *sel;
1265 sel = GST_INPUT_SELECTOR (element);
1266 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1268 GST_INPUT_SELECTOR_LOCK (sel);
1269 /* if the pad was the active pad, makes us select a new one */
1270 if (sel->active_sinkpad == pad) {
1271 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1272 gst_object_unref (sel->active_sinkpad);
1273 sel->active_sinkpad = NULL;
1277 gst_pad_set_active (pad, FALSE);
1278 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1279 GST_INPUT_SELECTOR_UNLOCK (sel);
1283 gst_input_selector_reset (GstInputSelector * sel)
1287 GST_INPUT_SELECTOR_LOCK (sel);
1288 /* clear active pad */
1289 if (sel->active_sinkpad) {
1290 gst_object_unref (sel->active_sinkpad);
1291 sel->active_sinkpad = NULL;
1294 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1295 sel->pending_close = FALSE;
1296 /* reset each of our sinkpads state */
1297 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1298 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1300 gst_selector_pad_reset (selpad);
1303 gst_tag_list_free (selpad->tags);
1304 selpad->tags = NULL;
1307 GST_INPUT_SELECTOR_UNLOCK (sel);
1310 static GstStateChangeReturn
1311 gst_input_selector_change_state (GstElement * element,
1312 GstStateChange transition)
1314 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1315 GstStateChangeReturn result;
1317 switch (transition) {
1318 case GST_STATE_CHANGE_READY_TO_PAUSED:
1319 GST_INPUT_SELECTOR_LOCK (self);
1320 self->blocked = FALSE;
1321 self->flushing = FALSE;
1322 GST_INPUT_SELECTOR_UNLOCK (self);
1324 case GST_STATE_CHANGE_PAUSED_TO_READY:
1325 /* first unlock before we call the parent state change function, which
1326 * tries to acquire the stream lock when going to ready. */
1327 GST_INPUT_SELECTOR_LOCK (self);
1328 self->blocked = FALSE;
1329 self->flushing = TRUE;
1330 GST_INPUT_SELECTOR_BROADCAST (self);
1331 GST_INPUT_SELECTOR_UNLOCK (self);
1337 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1339 switch (transition) {
1340 case GST_STATE_CHANGE_PAUSED_TO_READY:
1341 gst_input_selector_reset (self);
1351 gst_input_selector_block (GstInputSelector * self)
1354 GstSelectorPad *spad;
1356 GST_INPUT_SELECTOR_LOCK (self);
1359 GST_WARNING_OBJECT (self, "switch already blocked");
1361 self->blocked = TRUE;
1362 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1365 ret = gst_selector_pad_get_running_time (spad);
1367 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1369 GST_INPUT_SELECTOR_UNLOCK (self);
1374 /* stop_time and start_time are running times */
1376 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1377 gint64 stop_time, gint64 start_time)
1381 g_return_if_fail (self->blocked == TRUE);
1383 GST_INPUT_SELECTOR_LOCK (self);
1385 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1387 self->blocked = FALSE;
1388 GST_INPUT_SELECTOR_BROADCAST (self);
1389 GST_INPUT_SELECTOR_UNLOCK (self);
1392 g_object_notify (G_OBJECT (self), "active-pad");
1396 gst_input_selector_check_eos (GstElement * selector)
1398 GstIterator *it = gst_element_iterate_sink_pads (selector);
1399 GstIteratorResult ires;
1401 gboolean done = FALSE, is_eos = FALSE;
1402 GstSelectorPad *pad;
1405 ires = gst_iterator_next (it, &item);
1407 case GST_ITERATOR_DONE:
1408 GST_INFO_OBJECT (selector, "all sink pads have eos");
1412 case GST_ITERATOR_OK:
1413 pad = GST_SELECTOR_PAD_CAST (item);
1417 gst_object_unref (pad);
1419 case GST_ITERATOR_RESYNC:
1420 gst_iterator_resync (it);
1427 gst_iterator_free (it);