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 * @short_description: N-to-1 stream selectoring
28 * @see_also: #GstOutputSelector
30 * Direct one out of N input streams to the output pad.
39 #include "gstinputselector.h"
40 #include "gstselector-marshal.h"
42 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
43 #define GST_CAT_DEFAULT input_selector_debug
45 static const GstElementDetails gst_input_selector_details =
46 GST_ELEMENT_DETAILS ("Input selector",
48 "N-to-1 input stream selectoring",
49 "Julien Moutte <julien@moutte.net>\n"
50 "Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
51 "Jan Schmidt <thaytan@mad.scientist.com>\n"
52 "Wim Taymans <wim.taymans@gmail.com>");
54 static GstStaticPadTemplate gst_input_selector_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink%d",
60 static GstStaticPadTemplate gst_input_selector_src_factory =
61 GST_STATIC_PAD_TEMPLATE ("src",
75 #define DEFAULT_PAD_ALWAYS_OK TRUE
80 PROP_PAD_RUNNING_TIME,
94 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
96 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
98 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
100 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
102 static gboolean gst_input_selector_check_eos (GstElement * selector);
104 #define GST_TYPE_SELECTOR_PAD \
105 (gst_selector_pad_get_type())
106 #define GST_SELECTOR_PAD(obj) \
107 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
108 #define GST_SELECTOR_PAD_CLASS(klass) \
109 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
110 #define GST_IS_SELECTOR_PAD(obj) \
111 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
112 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
113 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
114 #define GST_SELECTOR_PAD_CAST(obj) \
115 ((GstSelectorPad *)(obj))
117 typedef struct _GstSelectorPad GstSelectorPad;
118 typedef struct _GstSelectorPadClass GstSelectorPadClass;
120 struct _GstSelectorPad
124 gboolean active; /* when buffer have passed the pad */
125 gboolean eos; /* when EOS has been received */
126 gboolean discont; /* after switching we create a discont */
128 GstSegment segment; /* the current segment on the pad */
129 GstTagList *tags; /* last tags received on the pad */
131 gboolean segment_pending;
134 struct _GstSelectorPadClass
139 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
140 static void gst_selector_pad_init (GstSelectorPad * pad);
141 static void gst_selector_pad_finalize (GObject * object);
142 static void gst_selector_pad_get_property (GObject * object,
143 guint prop_id, GValue * value, GParamSpec * pspec);
144 static void gst_selector_pad_set_property (GObject * object,
145 guint prop_id, const GValue * value, GParamSpec * pspec);
147 static GstPadClass *selector_pad_parent_class = NULL;
149 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
150 static void gst_selector_pad_reset (GstSelectorPad * pad);
151 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
152 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
153 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
154 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
155 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
156 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
159 gst_selector_pad_get_type (void)
161 static GType selector_pad_type = 0;
163 if (!selector_pad_type) {
164 static const GTypeInfo selector_pad_info = {
165 sizeof (GstSelectorPadClass),
168 (GClassInitFunc) gst_selector_pad_class_init,
171 sizeof (GstSelectorPad),
173 (GInstanceInitFunc) gst_selector_pad_init,
177 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
178 &selector_pad_info, 0);
180 return selector_pad_type;
184 gst_selector_pad_class_init (GstSelectorPadClass * klass)
186 GObjectClass *gobject_class;
188 gobject_class = (GObjectClass *) klass;
190 selector_pad_parent_class = g_type_class_peek_parent (klass);
192 gobject_class->finalize = gst_selector_pad_finalize;
194 gobject_class->get_property =
195 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
196 gobject_class->set_property =
197 GST_DEBUG_FUNCPTR (gst_selector_pad_set_property);
199 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
200 g_param_spec_int64 ("running-time", "Running time",
201 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
202 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
203 g_param_spec_boxed ("tags", "Tags",
204 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
206 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
207 g_param_spec_boolean ("active", "Active",
208 "If the pad is currently active", FALSE, G_PARAM_READABLE));
209 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
210 g_param_spec_boolean ("always-ok", "Always OK",
211 "Make an inactive pad return OK instead of NOT_LINKED",
212 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE));
216 gst_selector_pad_init (GstSelectorPad * pad)
218 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
219 gst_selector_pad_reset (pad);
223 gst_selector_pad_finalize (GObject * object)
227 pad = GST_SELECTOR_PAD_CAST (object);
230 gst_tag_list_free (pad->tags);
232 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
236 gst_selector_pad_set_property (GObject * object, guint prop_id,
237 const GValue * value, GParamSpec * pspec)
239 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
242 case PROP_PAD_ALWAYS_OK:
243 GST_OBJECT_LOCK (object);
244 spad->always_ok = g_value_get_boolean (value);
245 GST_OBJECT_UNLOCK (object);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_selector_pad_get_property (GObject * object, guint prop_id,
255 GValue * value, GParamSpec * pspec)
257 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
260 case PROP_PAD_RUNNING_TIME:
261 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
264 GST_OBJECT_LOCK (object);
265 g_value_set_boxed (value, spad->tags);
266 GST_OBJECT_UNLOCK (object);
268 case PROP_PAD_ACTIVE:
270 GstInputSelector *sel;
272 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
273 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
274 GST_PAD_CAST (spad)));
275 gst_object_unref (sel);
278 case PROP_PAD_ALWAYS_OK:
279 GST_OBJECT_LOCK (object);
280 g_value_set_boolean (value, spad->always_ok);
281 GST_OBJECT_UNLOCK (object);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gst_selector_pad_get_running_time (GstSelectorPad * pad)
294 GST_OBJECT_LOCK (pad);
296 gint64 last_stop = pad->segment.last_stop;
299 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
302 GST_OBJECT_UNLOCK (pad);
304 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
305 GST_TIME_ARGS (ret));
311 gst_selector_pad_reset (GstSelectorPad * pad)
313 GST_OBJECT_LOCK (pad);
316 pad->segment_pending = FALSE;
317 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
318 GST_OBJECT_UNLOCK (pad);
321 /* strictly get the linked pad from the sinkpad. If the pad is active we return
322 * the srcpad else we return NULL */
324 gst_selector_pad_get_linked_pads (GstPad * pad)
328 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
332 /* need to drop the ref, internal linked pads is not MT safe */
333 gst_object_unref (otherpad);
335 return g_list_append (NULL, otherpad);
339 gst_selector_pad_event (GstPad * pad, GstEvent * event)
342 gboolean forward = TRUE;
343 GstInputSelector *sel;
344 GstSelectorPad *selpad;
346 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
347 selpad = GST_SELECTOR_PAD_CAST (pad);
349 /* only forward if we are dealing with the active sinkpad */
350 forward = gst_input_selector_is_active_sinkpad (sel, pad);
352 /* forward all events in select_all mode by default */
353 if (sel->select_all) {
357 switch (GST_EVENT_TYPE (event)) {
358 case GST_EVENT_FLUSH_START:
359 /* FIXME, flush out the waiter */
361 case GST_EVENT_FLUSH_STOP:
362 GST_INPUT_SELECTOR_LOCK (sel);
363 gst_selector_pad_reset (selpad);
364 sel->pending_close = FALSE;
365 GST_INPUT_SELECTOR_UNLOCK (sel);
367 case GST_EVENT_NEWSEGMENT:
372 gint64 start, stop, time;
374 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
375 &start, &stop, &time);
377 GST_DEBUG_OBJECT (pad,
378 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
380 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
381 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
383 GST_INPUT_SELECTOR_LOCK (sel);
384 GST_OBJECT_LOCK (selpad);
385 gst_segment_set_newsegment_full (&selpad->segment, update,
386 rate, arate, format, start, stop, time);
387 GST_OBJECT_UNLOCK (selpad);
388 /* we are not going to forward the segment, mark the segment as
391 selpad->segment_pending = TRUE;
392 GST_INPUT_SELECTOR_UNLOCK (sel);
400 GST_OBJECT_LOCK (selpad);
402 gst_tag_list_free (selpad->tags);
403 gst_event_parse_tag (event, &tags);
405 tags = gst_tag_list_copy (tags);
407 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, selpad->tags);
408 GST_OBJECT_UNLOCK (selpad);
413 GST_DEBUG_OBJECT (pad, "received EOS");
414 /* don't forward eos in select_all mode until all sink pads have eos */
415 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
423 GST_DEBUG_OBJECT (pad, "forwarding event");
424 res = gst_pad_push_event (sel->srcpad, event);
426 gst_event_unref (event);
428 gst_object_unref (sel);
434 gst_selector_pad_getcaps (GstPad * pad)
436 GstInputSelector *sel;
439 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
441 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
442 caps = gst_pad_peer_get_caps (sel->srcpad);
444 caps = gst_caps_new_any ();
446 gst_object_unref (sel);
452 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
453 guint size, GstCaps * caps, GstBuffer ** buf)
455 GstInputSelector *sel;
456 GstFlowReturn result;
457 GstPad *active_sinkpad;
458 GstSelectorPad *selpad;
460 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
461 selpad = GST_SELECTOR_PAD_CAST (pad);
463 GST_DEBUG_OBJECT (pad, "received alloc");
465 GST_INPUT_SELECTOR_LOCK (sel);
466 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
468 if (pad != active_sinkpad && !sel->select_all)
471 GST_INPUT_SELECTOR_UNLOCK (sel);
473 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
476 gst_object_unref (sel);
483 GST_INPUT_SELECTOR_UNLOCK (sel);
485 /* unselected pad, perform fallback alloc or return unlinked when
487 GST_OBJECT_LOCK (selpad);
488 if (selpad->always_ok) {
489 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
491 result = GST_FLOW_OK;
493 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
494 result = GST_FLOW_NOT_LINKED;
496 GST_OBJECT_UNLOCK (selpad);
502 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
503 * or return TRUE when flushing */
505 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
507 while (self->blocked && !self->flushing) {
508 /* we can be unlocked here when we are shutting down (flushing) or when we
510 GST_INPUT_SELECTOR_WAIT (self);
512 return self->flushing;
516 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
518 GstInputSelector *sel;
520 GstPad *active_sinkpad;
521 GstSelectorPad *selpad;
522 GstClockTime end_time, duration;
524 GstEvent *close_event = NULL, *start_event = NULL;
526 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
527 selpad = GST_SELECTOR_PAD_CAST (pad);
528 seg = &selpad->segment;
530 GST_INPUT_SELECTOR_LOCK (sel);
531 /* wait or check for flushing */
532 if (gst_input_selector_wait (sel, pad))
535 GST_DEBUG_OBJECT (pad, "getting active pad");
537 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
539 /* update the segment on the srcpad */
540 end_time = GST_BUFFER_TIMESTAMP (buf);
541 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
542 duration = GST_BUFFER_DURATION (buf);
543 if (GST_CLOCK_TIME_IS_VALID (duration))
544 end_time += duration;
545 GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
546 GST_TIME_ARGS (end_time));
548 GST_OBJECT_LOCK (pad);
549 gst_segment_set_last_stop (seg, seg->format, end_time);
550 GST_OBJECT_UNLOCK (pad);
553 /* Ignore buffers from pads except the selected one */
554 if (pad != active_sinkpad && !sel->select_all)
557 if (G_UNLIKELY (sel->pending_close)) {
558 GstSegment *cseg = &sel->segment;
560 GST_DEBUG_OBJECT (sel,
561 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
563 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
564 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
565 cseg->start, cseg->stop, cseg->time);
567 /* create update segment */
568 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
569 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
571 sel->pending_close = FALSE;
573 /* if we have a pending segment, push it out now */
574 if (G_UNLIKELY (selpad->segment_pending)) {
575 GST_DEBUG_OBJECT (pad,
576 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
578 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
579 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
580 seg->start, seg->stop, seg->time);
582 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
583 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
585 selpad->segment_pending = FALSE;
587 GST_INPUT_SELECTOR_UNLOCK (sel);
590 gst_pad_push_event (sel->srcpad, close_event);
593 gst_pad_push_event (sel->srcpad, start_event);
595 if (selpad->discont) {
596 buf = gst_buffer_make_metadata_writable (buf);
598 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
599 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
600 selpad->discont = FALSE;
604 GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
605 GST_DEBUG_PAD_NAME (pad));
607 res = gst_pad_push (sel->srcpad, buf);
610 gst_object_unref (sel);
613 /* dropped buffers */
616 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
617 /* when we drop a buffer, we're creating a discont on this pad */
618 selpad->discont = TRUE;
619 GST_INPUT_SELECTOR_UNLOCK (sel);
620 gst_buffer_unref (buf);
622 /* figure out what to return upstream */
623 GST_OBJECT_LOCK (selpad);
624 if (selpad->always_ok)
627 res = GST_FLOW_NOT_LINKED;
628 GST_OBJECT_UNLOCK (selpad);
634 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
635 GST_INPUT_SELECTOR_UNLOCK (sel);
636 gst_buffer_unref (buf);
637 res = GST_FLOW_WRONG_STATE;
642 static void gst_input_selector_init (GstInputSelector * sel);
643 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
644 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
646 static void gst_input_selector_dispose (GObject * object);
648 static void gst_input_selector_set_property (GObject * object,
649 guint prop_id, const GValue * value, GParamSpec * pspec);
650 static void gst_input_selector_get_property (GObject * object,
651 guint prop_id, GValue * value, GParamSpec * pspec);
653 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
654 GstPadTemplate * templ, const gchar * unused);
655 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
657 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
658 element, GstStateChange transition);
660 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
661 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
662 static gint64 gst_input_selector_block (GstInputSelector * self);
663 static void gst_input_selector_switch (GstInputSelector * self,
664 GstPad * pad, gint64 stop_time, gint64 start_time);
666 static GstElementClass *parent_class = NULL;
669 gst_input_selector_get_type (void)
671 static GType input_selector_type = 0;
673 if (!input_selector_type) {
674 static const GTypeInfo input_selector_info = {
675 sizeof (GstInputSelectorClass),
676 (GBaseInitFunc) gst_input_selector_base_init,
678 (GClassInitFunc) gst_input_selector_class_init,
681 sizeof (GstInputSelector),
683 (GInstanceInitFunc) gst_input_selector_init,
685 input_selector_type =
686 g_type_register_static (GST_TYPE_ELEMENT,
687 "GstInputSelector", &input_selector_info, 0);
688 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
689 "input-selector", 0, "An input stream selector element");
692 return input_selector_type;
696 gst_input_selector_base_init (GstInputSelectorClass * klass)
698 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
700 gst_element_class_set_details (element_class, &gst_input_selector_details);
701 gst_element_class_add_pad_template (element_class,
702 gst_static_pad_template_get (&gst_input_selector_sink_factory));
703 gst_element_class_add_pad_template (element_class,
704 gst_static_pad_template_get (&gst_input_selector_src_factory));
708 gst_input_selector_class_init (GstInputSelectorClass * klass)
710 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
711 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
713 parent_class = g_type_class_peek_parent (klass);
715 gobject_class->dispose = gst_input_selector_dispose;
717 gobject_class->set_property =
718 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
719 gobject_class->get_property =
720 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
722 g_object_class_install_property (gobject_class, PROP_N_PADS,
723 g_param_spec_uint ("n-pads", "Number of Pads",
724 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
726 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
727 g_param_spec_object ("active-pad", "Active pad",
728 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
730 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
731 g_param_spec_boolean ("select-all", "Select all mode",
732 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
735 * GstInputSelector::block:
736 * @inputselector: the #GstInputSelector
738 * Block all sink pads in preparation for a switch. Returns the stop time of
739 * the current switch segment, as a running time, or 0 if there is no current
740 * active pad or the current active pad never received data.
742 gst_input_selector_signals[SIGNAL_BLOCK] =
743 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
744 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
745 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
746 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
748 * GstInputSelector::switch:
749 * @inputselector: the #GstInputSelector
750 * @pad: the pad to switch to
751 * @stop_time: running time at which to close the previous segment, or -1
752 * to use the running time of the previously active sink pad
753 * @start_time: running time at which to start the new segment, or -1 to
754 * use the running time of the newly active sink pad
756 * Switch to a new feed. The segment opened by the previously active pad, if
757 * any, will be closed, and a new segment opened before data flows again.
759 * This signal must be emitted when the element has been blocked via the <link
760 * linkend="GstInputSelector-block">block</link> signal.
762 * If you have a stream with only one switch element, such as an audio-only
763 * stream, a stream switch should be performed by first emitting the block
764 * signal, and then emitting the switch signal with -1 for the stop and start
767 * The intention of the @stop_time and @start_time arguments is to allow
768 * multiple switch elements to switch and maintain stream synchronization.
769 * When switching a stream with multiple feeds, you will need as many switch
770 * elements as you have feeds. For example, a feed with audio and video will
771 * have one switch element between the audio feeds and one for video.
773 * A switch over multiple switch elements should be performed as follows:
774 * First, emit the <link linkend="GstInputSelector-block">block</link>
775 * signal, collecting the returned values. The maximum running time returned
776 * by block should then be used as the time at which to close the previous
779 * Then, query the running times of the new audio and video pads that you will
780 * switch to. Naturally, these pads are on separate switch elements. Take the
781 * minimum running time for those streams and use it for the time at which to
782 * open the new segment.
784 * If @pad is the same as the current active pad, the element will cancel any
785 * previous block without adjusting segments.
787 * Since: 0.10.7 the signal changed from accepting the pad name to the pad
790 gst_input_selector_signals[SIGNAL_SWITCH] =
791 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
792 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
793 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
794 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
796 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
797 gstelement_class->release_pad = gst_input_selector_release_pad;
798 gstelement_class->change_state = gst_input_selector_change_state;
800 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
801 /* note the underscore because switch is a keyword otherwise */
802 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
806 gst_input_selector_init (GstInputSelector * sel)
808 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
809 gst_pad_set_internal_link_function (sel->srcpad,
810 GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
811 gst_pad_set_getcaps_function (sel->srcpad,
812 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
813 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
814 /* sinkpad management */
815 sel->active_sinkpad = NULL;
817 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
819 sel->lock = g_mutex_new ();
820 sel->cond = g_cond_new ();
821 sel->blocked = FALSE;
823 sel->select_all = FALSE;
827 gst_input_selector_dispose (GObject * object)
829 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
831 if (sel->active_sinkpad) {
832 gst_object_unref (sel->active_sinkpad);
833 sel->active_sinkpad = NULL;
836 g_mutex_free (sel->lock);
840 g_cond_free (sel->cond);
844 G_OBJECT_CLASS (parent_class)->dispose (object);
847 /* Solve the following equation for B.timestamp, and set that as the segment
849 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
852 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
854 return (running_time - segment->accum) * segment->abs_rate + segment->start;
858 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
860 segment->stop = gst_segment_get_timestamp (segment, running_time);
861 segment->last_stop = -1;
865 gst_segment_set_start (GstSegment * segment, gint64 running_time)
867 gint64 new_start, duration;
869 new_start = gst_segment_get_timestamp (segment, running_time);
871 /* this is the duration we skipped */
872 duration = new_start - segment->start;
873 /* add the duration to the accumulated segment time */
874 segment->accum += duration;
875 /* move position in the segment */
876 segment->time += duration;
877 segment->start += duration;
880 /* this function must be called with the SELECTOR_LOCK. */
882 gst_input_selector_set_active_pad (GstInputSelector * self,
883 GstPad * pad, gint64 stop_time, gint64 start_time)
885 GstSelectorPad *old, *new;
886 GstPad **active_pad_p;
888 if (pad == self->active_sinkpad)
891 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
892 new = GST_SELECTOR_PAD_CAST (pad);
894 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
895 GST_DEBUG_PAD_NAME (new));
897 if (stop_time == -1 && old) {
898 /* no stop time given, get the latest running_time on the active pad to
899 * close and open the new segment */
900 stop_time = start_time = gst_selector_pad_get_running_time (old);
901 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
905 if (old && old->active && !self->pending_close && stop_time >= 0) {
906 /* schedule a last_stop update if one isn't already scheduled, and a
907 segment has been pushed before. */
908 memcpy (&self->segment, &old->segment, sizeof (self->segment));
909 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
911 gst_segment_set_stop (&self->segment, stop_time);
912 self->pending_close = TRUE;
915 if (new && new->active && start_time >= 0) {
916 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
918 /* schedule a new segment push */
919 gst_segment_set_start (&new->segment, start_time);
920 new->segment_pending = TRUE;
923 active_pad_p = &self->active_sinkpad;
924 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
925 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
926 self->active_sinkpad);
928 g_object_notify (G_OBJECT (self), "active-pad");
932 gst_input_selector_set_property (GObject * object, guint prop_id,
933 const GValue * value, GParamSpec * pspec)
935 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
938 case PROP_ACTIVE_PAD:
942 pad = g_value_get_object (value);
944 GST_INPUT_SELECTOR_LOCK (sel);
945 gst_input_selector_set_active_pad (sel, pad,
946 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
947 GST_INPUT_SELECTOR_UNLOCK (sel);
950 case PROP_SELECT_ALL:
951 GST_INPUT_SELECTOR_LOCK (object);
952 sel->select_all = g_value_get_boolean (value);
953 GST_INPUT_SELECTOR_UNLOCK (object);
956 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
962 gst_input_selector_get_property (GObject * object, guint prop_id,
963 GValue * value, GParamSpec * pspec)
965 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
969 GST_INPUT_SELECTOR_LOCK (object);
970 g_value_set_uint (value, sel->n_pads);
971 GST_INPUT_SELECTOR_UNLOCK (object);
973 case PROP_ACTIVE_PAD:
974 GST_INPUT_SELECTOR_LOCK (object);
975 g_value_set_object (value, sel->active_sinkpad);
976 GST_INPUT_SELECTOR_UNLOCK (object);
978 case PROP_SELECT_ALL:
979 GST_INPUT_SELECTOR_LOCK (object);
980 g_value_set_boolean (value, sel->select_all);
981 GST_INPUT_SELECTOR_UNLOCK (object);
984 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
990 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
992 GstInputSelector *sel;
993 GstPad *otherpad = NULL;
995 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
997 GST_INPUT_SELECTOR_LOCK (sel);
998 if (pad == sel->srcpad)
999 otherpad = sel->active_sinkpad;
1000 else if (pad == sel->active_sinkpad || !strict)
1001 otherpad = sel->srcpad;
1003 gst_object_ref (otherpad);
1004 GST_INPUT_SELECTOR_UNLOCK (sel);
1006 gst_object_unref (sel);
1012 gst_input_selector_getcaps (GstPad * pad)
1018 parent = gst_object_get_parent (GST_OBJECT (pad));
1019 if (GST_INPUT_SELECTOR (parent)->select_all) {
1020 caps = gst_pad_proxy_getcaps (pad);
1024 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1026 GST_DEBUG_OBJECT (parent,
1027 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1028 caps = gst_caps_new_any ();
1030 GST_DEBUG_OBJECT (parent,
1031 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1032 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1033 /* if the peer has caps, use those. If the pad is not linked, this function
1034 * returns NULL and we return ANY */
1035 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1036 caps = gst_caps_new_any ();
1037 gst_object_unref (otherpad);
1041 gst_object_unref (parent);
1045 /* check if the pad is the active sinkpad */
1047 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1049 GstSelectorPad *selpad;
1052 selpad = GST_SELECTOR_PAD_CAST (pad);
1054 GST_INPUT_SELECTOR_LOCK (sel);
1055 res = (pad == sel->active_sinkpad);
1056 GST_INPUT_SELECTOR_UNLOCK (sel);
1061 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1063 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1065 GstPad *active_sinkpad;
1066 GstSelectorPad *selpad;
1068 selpad = GST_SELECTOR_PAD_CAST (pad);
1070 selpad->active = TRUE;
1071 active_sinkpad = sel->active_sinkpad;
1072 if (active_sinkpad == NULL) {
1073 /* first pad we get activity on becomes the activated pad by default */
1074 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1075 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1078 return active_sinkpad;
1082 gst_input_selector_get_linked_pads (GstPad * pad)
1086 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1089 /* need to drop the ref, internal linked pads is not MT safe */
1090 gst_object_unref (otherpad);
1091 return g_list_append (NULL, otherpad);
1095 gst_input_selector_request_new_pad (GstElement * element,
1096 GstPadTemplate * templ, const gchar * unused)
1098 GstInputSelector *sel;
1100 GstPad *sinkpad = NULL;
1102 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1104 sel = GST_INPUT_SELECTOR (element);
1106 GST_INPUT_SELECTOR_LOCK (sel);
1108 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1109 name = g_strdup_printf ("sink%d", sel->padcount++);
1110 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1111 "name", name, "direction", templ->direction, "template", templ, NULL);
1116 gst_pad_set_event_function (sinkpad,
1117 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1118 gst_pad_set_getcaps_function (sinkpad,
1119 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1120 gst_pad_set_chain_function (sinkpad,
1121 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1122 gst_pad_set_internal_link_function (sinkpad,
1123 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
1124 gst_pad_set_bufferalloc_function (sinkpad,
1125 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1127 gst_pad_set_active (sinkpad, TRUE);
1128 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1129 GST_INPUT_SELECTOR_UNLOCK (sel);
1135 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1137 GstInputSelector *sel;
1139 sel = GST_INPUT_SELECTOR (element);
1140 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1142 GST_INPUT_SELECTOR_LOCK (sel);
1143 /* if the pad was the active pad, makes us select a new one */
1144 if (sel->active_sinkpad == pad) {
1145 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1146 sel->active_sinkpad = NULL;
1150 gst_pad_set_active (pad, FALSE);
1151 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1152 GST_INPUT_SELECTOR_UNLOCK (sel);
1155 static GstStateChangeReturn
1156 gst_input_selector_change_state (GstElement * element,
1157 GstStateChange transition)
1159 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1160 GstStateChangeReturn result;
1162 switch (transition) {
1163 case GST_STATE_CHANGE_READY_TO_PAUSED:
1164 GST_INPUT_SELECTOR_LOCK (self);
1165 self->blocked = FALSE;
1166 self->flushing = FALSE;
1167 GST_INPUT_SELECTOR_UNLOCK (self);
1169 case GST_STATE_CHANGE_PAUSED_TO_READY:
1170 /* first unlock before we call the parent state change function, which
1171 * tries to acquire the stream lock when going to ready. */
1172 GST_INPUT_SELECTOR_LOCK (self);
1173 self->blocked = FALSE;
1174 self->flushing = TRUE;
1175 GST_INPUT_SELECTOR_BROADCAST (self);
1176 GST_INPUT_SELECTOR_UNLOCK (self);
1182 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1188 gst_input_selector_block (GstInputSelector * self)
1191 GstSelectorPad *spad;
1193 GST_INPUT_SELECTOR_LOCK (self);
1196 GST_WARNING_OBJECT (self, "switch already blocked");
1198 self->blocked = TRUE;
1199 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1202 ret = gst_selector_pad_get_running_time (spad);
1204 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1206 GST_INPUT_SELECTOR_UNLOCK (self);
1211 /* stop_time and start_time are running times */
1213 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1214 gint64 stop_time, gint64 start_time)
1216 g_return_if_fail (self->blocked == TRUE);
1218 GST_INPUT_SELECTOR_LOCK (self);
1219 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1221 self->blocked = FALSE;
1222 GST_INPUT_SELECTOR_BROADCAST (self);
1223 GST_INPUT_SELECTOR_UNLOCK (self);
1227 gst_input_selector_check_eos (GstElement * selector)
1229 GstIterator *it = gst_element_iterate_sink_pads (selector);
1230 GstIteratorResult ires;
1232 gboolean done = FALSE, is_eos = FALSE;
1233 GstSelectorPad *pad;
1236 ires = gst_iterator_next (it, &item);
1238 case GST_ITERATOR_DONE:
1239 GST_INFO_OBJECT (selector, "all sink pads have eos");
1243 case GST_ITERATOR_OK:
1244 pad = GST_SELECTOR_PAD_CAST (item);
1249 case GST_ITERATOR_RESYNC:
1250 gst_iterator_resync (it);
1257 gst_iterator_free (it);