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_DEBUG_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_DEBUG_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_DEBUG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
577 GST_TIME_ARGS (start_time));
578 if (GST_BUFFER_DURATION_IS_VALID (buf))
579 GST_DEBUG_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_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
642 GST_DEBUG_PAD_NAME (pad));
644 if ((caps = GST_BUFFER_CAPS (buf))) {
645 if (GST_PAD_CAPS (sel->srcpad) != caps)
646 gst_pad_set_caps (sel->srcpad, caps);
649 res = gst_pad_push (sel->srcpad, buf);
652 gst_object_unref (sel);
655 /* dropped buffers */
658 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
659 /* when we drop a buffer, we're creating a discont on this pad */
660 selpad->discont = TRUE;
661 GST_INPUT_SELECTOR_UNLOCK (sel);
662 gst_buffer_unref (buf);
664 /* figure out what to return upstream */
665 GST_OBJECT_LOCK (selpad);
666 if (selpad->always_ok)
669 res = GST_FLOW_NOT_LINKED;
670 GST_OBJECT_UNLOCK (selpad);
676 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
677 GST_INPUT_SELECTOR_UNLOCK (sel);
678 gst_buffer_unref (buf);
679 res = GST_FLOW_WRONG_STATE;
684 static void gst_input_selector_dispose (GObject * object);
686 static void gst_input_selector_set_property (GObject * object,
687 guint prop_id, const GValue * value, GParamSpec * pspec);
688 static void gst_input_selector_get_property (GObject * object,
689 guint prop_id, GValue * value, GParamSpec * pspec);
691 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
692 GstPadTemplate * templ, const gchar * unused);
693 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
695 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
696 element, GstStateChange transition);
698 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
699 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
700 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
701 static gint64 gst_input_selector_block (GstInputSelector * self);
702 static void gst_input_selector_switch (GstInputSelector * self,
703 GstPad * pad, gint64 stop_time, gint64 start_time);
705 #define _do_init(bla) \
706 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
707 "input-selector", 0, "An input stream selector element");
709 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
710 GST_TYPE_ELEMENT, _do_init);
713 gst_input_selector_base_init (gpointer g_class)
715 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
717 gst_element_class_set_details_simple (element_class, "Input selector",
718 "Generic", "N-to-1 input stream selectoring",
719 "Julien Moutte <julien@moutte.net>, "
720 "Jan Schmidt <thaytan@mad.scientist.com>, "
721 "Wim Taymans <wim.taymans@gmail.com>");
722 gst_element_class_add_pad_template (element_class,
723 gst_static_pad_template_get (&gst_input_selector_sink_factory));
724 gst_element_class_add_pad_template (element_class,
725 gst_static_pad_template_get (&gst_input_selector_src_factory));
729 gst_input_selector_class_init (GstInputSelectorClass * klass)
731 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
732 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
734 parent_class = g_type_class_peek_parent (klass);
736 gobject_class->dispose = gst_input_selector_dispose;
738 gobject_class->set_property = gst_input_selector_set_property;
739 gobject_class->get_property = gst_input_selector_get_property;
741 g_object_class_install_property (gobject_class, PROP_N_PADS,
742 g_param_spec_uint ("n-pads", "Number of Pads",
743 "The number of sink pads", 0, G_MAXUINT, 0,
744 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
746 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
747 g_param_spec_object ("active-pad", "Active pad",
748 "The currently active sink pad", GST_TYPE_PAD,
749 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
751 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
752 g_param_spec_boolean ("select-all", "Select all mode",
753 "Forwards data from all input pads", FALSE,
754 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
757 * GstInputSelector::block:
758 * @inputselector: the #GstInputSelector
760 * Block all sink pads in preparation for a switch. Returns the stop time of
761 * the current switch segment, as a running time, or 0 if there is no current
762 * active pad or the current active pad never received data.
764 gst_input_selector_signals[SIGNAL_BLOCK] =
765 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
766 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
767 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
768 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
770 * GstInputSelector::switch:
771 * @inputselector: the #GstInputSelector
772 * @pad: the pad to switch to
773 * @stop_time: running time at which to close the previous segment, or -1
774 * to use the running time of the previously active sink pad
775 * @start_time: running time at which to start the new segment, or -1 to
776 * use the running time of the newly active sink pad
778 * Switch to a new feed. The segment opened by the previously active pad, if
779 * any, will be closed, and a new segment opened before data flows again.
781 * This signal must be emitted when the element has been blocked via the <link
782 * linkend="GstInputSelector-block">block</link> signal.
784 * If you have a stream with only one switch element, such as an audio-only
785 * stream, a stream switch should be performed by first emitting the block
786 * signal, and then emitting the switch signal with -1 for the stop and start
789 * The intention of the @stop_time and @start_time arguments is to allow
790 * multiple switch elements to switch and maintain stream synchronization.
791 * When switching a stream with multiple feeds, you will need as many switch
792 * elements as you have feeds. For example, a feed with audio and video will
793 * have one switch element between the audio feeds and one for video.
795 * A switch over multiple switch elements should be performed as follows:
796 * First, emit the <link linkend="GstInputSelector-block">block</link>
797 * signal, collecting the returned values. The maximum running time returned
798 * by block should then be used as the time at which to close the previous
801 * Then, query the running times of the new audio and video pads that you will
802 * switch to. Naturally, these pads are on separate switch elements. Take the
803 * minimum running time for those streams and use it for the time at which to
804 * open the new segment.
806 * If @pad is the same as the current active pad, the element will cancel any
807 * previous block without adjusting segments.
810 * the signal changed from accepting the pad name to the pad object.
815 gst_input_selector_signals[SIGNAL_SWITCH] =
816 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
817 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
818 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
819 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
821 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
822 gstelement_class->release_pad = gst_input_selector_release_pad;
823 gstelement_class->change_state = gst_input_selector_change_state;
825 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
826 /* note the underscore because switch is a keyword otherwise */
827 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
831 gst_input_selector_init (GstInputSelector * sel,
832 GstInputSelectorClass * g_class)
834 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
835 gst_pad_set_iterate_internal_links_function (sel->srcpad,
836 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
837 gst_pad_set_getcaps_function (sel->srcpad,
838 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
839 gst_pad_set_query_function (sel->srcpad,
840 GST_DEBUG_FUNCPTR (gst_input_selector_query));
841 gst_pad_set_event_function (sel->srcpad,
842 GST_DEBUG_FUNCPTR (gst_input_selector_event));
843 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
844 /* sinkpad management */
845 sel->active_sinkpad = NULL;
847 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
849 sel->lock = g_mutex_new ();
850 sel->cond = g_cond_new ();
851 sel->blocked = FALSE;
853 sel->select_all = FALSE;
857 gst_input_selector_dispose (GObject * object)
859 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
861 if (sel->active_sinkpad) {
862 gst_object_unref (sel->active_sinkpad);
863 sel->active_sinkpad = NULL;
866 g_mutex_free (sel->lock);
870 g_cond_free (sel->cond);
874 G_OBJECT_CLASS (parent_class)->dispose (object);
877 /* Solve the following equation for B.timestamp, and set that as the segment
879 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
882 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
884 if (running_time <= segment->accum)
885 return segment->start;
887 return (running_time - segment->accum) * segment->abs_rate + segment->start;
891 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
893 segment->stop = gst_segment_get_timestamp (segment, running_time);
894 segment->last_stop = -1;
898 gst_segment_set_start (GstSegment * segment, gint64 running_time)
900 gint64 new_start, duration;
902 new_start = gst_segment_get_timestamp (segment, running_time);
904 /* this is the duration we skipped */
905 duration = new_start - segment->start;
906 /* add the duration to the accumulated segment time */
907 segment->accum += duration;
908 /* move position in the segment */
909 segment->time += duration;
910 segment->start += duration;
913 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
914 * active pad changed. */
916 gst_input_selector_set_active_pad (GstInputSelector * self,
917 GstPad * pad, gint64 stop_time, gint64 start_time)
919 GstSelectorPad *old, *new;
920 GstPad **active_pad_p;
922 if (pad == self->active_sinkpad)
925 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
926 new = GST_SELECTOR_PAD_CAST (pad);
928 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
929 GST_DEBUG_PAD_NAME (new));
931 if (stop_time == -1 && old) {
932 /* no stop time given, get the latest running_time on the active pad to
933 * close and open the new segment */
934 stop_time = start_time = gst_selector_pad_get_running_time (old);
935 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
939 if (old && old->active && !self->pending_close && stop_time >= 0) {
940 /* schedule a last_stop update if one isn't already scheduled, and a
941 segment has been pushed before. */
942 memcpy (&self->segment, &old->segment, sizeof (self->segment));
944 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
946 gst_segment_set_stop (&self->segment, stop_time);
947 self->pending_close = TRUE;
950 if (new && new->active && start_time >= 0) {
951 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
953 /* schedule a new segment push */
954 gst_segment_set_start (&new->segment, start_time);
955 new->segment_pending = TRUE;
958 active_pad_p = &self->active_sinkpad;
959 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
960 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
961 self->active_sinkpad);
967 gst_input_selector_set_property (GObject * object, guint prop_id,
968 const GValue * value, GParamSpec * pspec)
970 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
973 case PROP_ACTIVE_PAD:
977 pad = g_value_get_object (value);
979 GST_INPUT_SELECTOR_LOCK (sel);
980 gst_input_selector_set_active_pad (sel, pad,
981 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
982 GST_INPUT_SELECTOR_UNLOCK (sel);
985 case PROP_SELECT_ALL:
986 GST_INPUT_SELECTOR_LOCK (object);
987 sel->select_all = g_value_get_boolean (value);
988 GST_INPUT_SELECTOR_UNLOCK (object);
991 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
997 gst_input_selector_get_property (GObject * object, guint prop_id,
998 GValue * value, GParamSpec * pspec)
1000 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1004 GST_INPUT_SELECTOR_LOCK (object);
1005 g_value_set_uint (value, sel->n_pads);
1006 GST_INPUT_SELECTOR_UNLOCK (object);
1008 case PROP_ACTIVE_PAD:
1009 GST_INPUT_SELECTOR_LOCK (object);
1010 g_value_set_object (value, sel->active_sinkpad);
1011 GST_INPUT_SELECTOR_UNLOCK (object);
1013 case PROP_SELECT_ALL:
1014 GST_INPUT_SELECTOR_LOCK (object);
1015 g_value_set_boolean (value, sel->select_all);
1016 GST_INPUT_SELECTOR_UNLOCK (object);
1019 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1027 GstInputSelector *sel;
1028 GstPad *otherpad = NULL;
1030 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1032 GST_INPUT_SELECTOR_LOCK (sel);
1033 if (pad == sel->srcpad)
1034 otherpad = sel->active_sinkpad;
1035 else if (pad == sel->active_sinkpad || !strict)
1036 otherpad = sel->srcpad;
1038 gst_object_ref (otherpad);
1039 GST_INPUT_SELECTOR_UNLOCK (sel);
1041 gst_object_unref (sel);
1047 gst_input_selector_event (GstPad * pad, GstEvent * event)
1049 gboolean res = FALSE;
1052 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1055 res = gst_pad_push_event (otherpad, event);
1057 gst_object_unref (otherpad);
1059 gst_event_unref (event);
1063 /* query on the srcpad. We override this function because by default it will
1064 * only forward the query to one random sinkpad */
1066 gst_input_selector_query (GstPad * pad, GstQuery * query)
1068 gboolean res = TRUE;
1069 GstInputSelector *sel;
1072 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1074 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1076 switch (GST_QUERY_TYPE (query)) {
1077 case GST_QUERY_LATENCY:
1080 GstClockTime resmin, resmax;
1087 /* assume FALSE, we become TRUE if one query succeeds */
1090 /* perform the query on all sinkpads and combine the results. We take the
1091 * max of min and the min of max for the result latency. */
1092 GST_INPUT_SELECTOR_LOCK (sel);
1093 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1094 walk = g_list_next (walk)) {
1095 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1097 if (gst_pad_peer_query (sinkpad, query)) {
1098 GstClockTime min, max;
1101 /* one query succeeded, we succeed too */
1104 gst_query_parse_latency (query, &live, &min, &max);
1106 GST_DEBUG_OBJECT (sinkpad,
1107 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1108 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1115 else if (max < resmax)
1117 if (reslive == FALSE)
1122 GST_INPUT_SELECTOR_UNLOCK (sel);
1124 gst_query_set_latency (query, reslive, resmin, resmax);
1126 GST_DEBUG_OBJECT (sel,
1127 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1128 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1136 res = gst_pad_peer_query (otherpad, query);
1140 gst_object_unref (otherpad);
1141 gst_object_unref (sel);
1147 gst_input_selector_getcaps (GstPad * pad)
1153 parent = gst_object_get_parent (GST_OBJECT (pad));
1155 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1158 if (GST_INPUT_SELECTOR (parent)->select_all) {
1159 GST_DEBUG_OBJECT (parent,
1160 "Pad %s:%s not linked, returning merge of caps",
1161 GST_DEBUG_PAD_NAME (pad));
1162 caps = gst_pad_proxy_getcaps (pad);
1164 GST_DEBUG_OBJECT (parent,
1165 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1166 caps = gst_caps_new_any ();
1169 GST_DEBUG_OBJECT (parent,
1170 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1171 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1172 /* if the peer has caps, use those. If the pad is not linked, this function
1173 * returns NULL and we return ANY */
1174 if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1175 caps = gst_caps_new_any ();
1176 gst_object_unref (otherpad);
1179 gst_object_unref (parent);
1183 /* check if the pad is the active sinkpad */
1184 static inline gboolean
1185 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1189 GST_INPUT_SELECTOR_LOCK (sel);
1190 res = (pad == sel->active_sinkpad);
1191 GST_INPUT_SELECTOR_UNLOCK (sel);
1196 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1198 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1200 GstPad *active_sinkpad;
1201 GstSelectorPad *selpad;
1203 selpad = GST_SELECTOR_PAD_CAST (pad);
1205 selpad->active = TRUE;
1206 active_sinkpad = sel->active_sinkpad;
1207 if (active_sinkpad == NULL || sel->select_all) {
1208 /* first pad we get activity on becomes the activated pad by default, if we
1209 * select all, we also remember the last used pad. */
1210 if (sel->active_sinkpad)
1211 gst_object_unref (sel->active_sinkpad);
1212 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1213 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1216 return active_sinkpad;
1220 gst_input_selector_request_new_pad (GstElement * element,
1221 GstPadTemplate * templ, const gchar * unused)
1223 GstInputSelector *sel;
1225 GstPad *sinkpad = NULL;
1227 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1229 sel = GST_INPUT_SELECTOR (element);
1231 GST_INPUT_SELECTOR_LOCK (sel);
1233 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1234 name = g_strdup_printf ("sink%d", sel->padcount++);
1235 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1236 "name", name, "direction", templ->direction, "template", templ, NULL);
1241 gst_pad_set_event_function (sinkpad,
1242 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1243 gst_pad_set_getcaps_function (sinkpad,
1244 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1245 gst_pad_set_acceptcaps_function (sinkpad,
1246 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1247 gst_pad_set_chain_function (sinkpad,
1248 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1249 gst_pad_set_iterate_internal_links_function (sinkpad,
1250 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1251 gst_pad_set_bufferalloc_function (sinkpad,
1252 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1254 gst_pad_set_active (sinkpad, TRUE);
1255 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1256 GST_INPUT_SELECTOR_UNLOCK (sel);
1262 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1264 GstInputSelector *sel;
1266 sel = GST_INPUT_SELECTOR (element);
1267 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1269 GST_INPUT_SELECTOR_LOCK (sel);
1270 /* if the pad was the active pad, makes us select a new one */
1271 if (sel->active_sinkpad == pad) {
1272 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1273 gst_object_unref (sel->active_sinkpad);
1274 sel->active_sinkpad = NULL;
1278 gst_pad_set_active (pad, FALSE);
1279 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1280 GST_INPUT_SELECTOR_UNLOCK (sel);
1284 gst_input_selector_reset (GstInputSelector * sel)
1288 GST_INPUT_SELECTOR_LOCK (sel);
1289 /* clear active pad */
1290 if (sel->active_sinkpad) {
1291 gst_object_unref (sel->active_sinkpad);
1292 sel->active_sinkpad = NULL;
1295 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1296 sel->pending_close = FALSE;
1297 /* reset each of our sinkpads state */
1298 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1299 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1301 gst_selector_pad_reset (selpad);
1304 gst_tag_list_free (selpad->tags);
1305 selpad->tags = NULL;
1308 GST_INPUT_SELECTOR_UNLOCK (sel);
1311 static GstStateChangeReturn
1312 gst_input_selector_change_state (GstElement * element,
1313 GstStateChange transition)
1315 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1316 GstStateChangeReturn result;
1318 switch (transition) {
1319 case GST_STATE_CHANGE_READY_TO_PAUSED:
1320 GST_INPUT_SELECTOR_LOCK (self);
1321 self->blocked = FALSE;
1322 self->flushing = FALSE;
1323 GST_INPUT_SELECTOR_UNLOCK (self);
1325 case GST_STATE_CHANGE_PAUSED_TO_READY:
1326 /* first unlock before we call the parent state change function, which
1327 * tries to acquire the stream lock when going to ready. */
1328 GST_INPUT_SELECTOR_LOCK (self);
1329 self->blocked = FALSE;
1330 self->flushing = TRUE;
1331 GST_INPUT_SELECTOR_BROADCAST (self);
1332 GST_INPUT_SELECTOR_UNLOCK (self);
1338 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1340 switch (transition) {
1341 case GST_STATE_CHANGE_PAUSED_TO_READY:
1342 gst_input_selector_reset (self);
1352 gst_input_selector_block (GstInputSelector * self)
1355 GstSelectorPad *spad;
1357 GST_INPUT_SELECTOR_LOCK (self);
1360 GST_WARNING_OBJECT (self, "switch already blocked");
1362 self->blocked = TRUE;
1363 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1366 ret = gst_selector_pad_get_running_time (spad);
1368 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1370 GST_INPUT_SELECTOR_UNLOCK (self);
1375 /* stop_time and start_time are running times */
1377 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1378 gint64 stop_time, gint64 start_time)
1382 g_return_if_fail (self->blocked == TRUE);
1384 GST_INPUT_SELECTOR_LOCK (self);
1386 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1388 self->blocked = FALSE;
1389 GST_INPUT_SELECTOR_BROADCAST (self);
1390 GST_INPUT_SELECTOR_UNLOCK (self);
1393 g_object_notify (G_OBJECT (self), "active-pad");
1397 gst_input_selector_check_eos (GstElement * selector)
1399 GstIterator *it = gst_element_iterate_sink_pads (selector);
1400 GstIteratorResult ires;
1402 gboolean done = FALSE, is_eos = FALSE;
1403 GstSelectorPad *pad;
1406 ires = gst_iterator_next (it, &item);
1408 case GST_ITERATOR_DONE:
1409 GST_INFO_OBJECT (selector, "all sink pads have eos");
1413 case GST_ITERATOR_OK:
1414 pad = GST_SELECTOR_PAD_CAST (item);
1418 gst_object_unref (pad);
1420 case GST_ITERATOR_RESYNC:
1421 gst_iterator_resync (it);
1428 gst_iterator_free (it);