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 const GstElementDetails gst_input_selector_details =
45 GST_ELEMENT_DETAILS ("Input selector",
47 "N-to-1 input stream selectoring",
48 "Julien Moutte <julien@moutte.net>\n"
49 "Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
50 "Jan Schmidt <thaytan@mad.scientist.com>\n"
51 "Wim Taymans <wim.taymans@gmail.com>");
53 static GstStaticPadTemplate gst_input_selector_sink_factory =
54 GST_STATIC_PAD_TEMPLATE ("sink%d",
59 static GstStaticPadTemplate gst_input_selector_src_factory =
60 GST_STATIC_PAD_TEMPLATE ("src",
74 #define DEFAULT_PAD_ALWAYS_OK TRUE
79 PROP_PAD_RUNNING_TIME,
93 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
95 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
97 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
99 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
101 static gboolean gst_input_selector_check_eos (GstElement * selector);
103 #define GST_TYPE_SELECTOR_PAD \
104 (gst_selector_pad_get_type())
105 #define GST_SELECTOR_PAD(obj) \
106 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
107 #define GST_SELECTOR_PAD_CLASS(klass) \
108 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
109 #define GST_IS_SELECTOR_PAD(obj) \
110 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
111 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
112 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
113 #define GST_SELECTOR_PAD_CAST(obj) \
114 ((GstSelectorPad *)(obj))
116 typedef struct _GstSelectorPad GstSelectorPad;
117 typedef struct _GstSelectorPadClass GstSelectorPadClass;
119 struct _GstSelectorPad
123 gboolean active; /* when buffer have passed the pad */
124 gboolean eos; /* when EOS has been received */
125 gboolean discont; /* after switching we create a discont */
127 GstSegment segment; /* the current segment on the pad */
128 GstTagList *tags; /* last tags received on the pad */
130 gboolean segment_pending;
133 struct _GstSelectorPadClass
138 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
139 static void gst_selector_pad_init (GstSelectorPad * pad);
140 static void gst_selector_pad_finalize (GObject * object);
141 static void gst_selector_pad_get_property (GObject * object,
142 guint prop_id, GValue * value, GParamSpec * pspec);
143 static void gst_selector_pad_set_property (GObject * object,
144 guint prop_id, const GValue * value, GParamSpec * pspec);
146 static GstPadClass *selector_pad_parent_class = NULL;
148 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
149 static void gst_selector_pad_reset (GstSelectorPad * pad);
150 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
151 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
152 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
153 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
154 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
155 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
158 gst_selector_pad_get_type (void)
160 static GType selector_pad_type = 0;
162 if (!selector_pad_type) {
163 static const GTypeInfo selector_pad_info = {
164 sizeof (GstSelectorPadClass),
167 (GClassInitFunc) gst_selector_pad_class_init,
170 sizeof (GstSelectorPad),
172 (GInstanceInitFunc) gst_selector_pad_init,
176 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
177 &selector_pad_info, 0);
179 return selector_pad_type;
183 gst_selector_pad_class_init (GstSelectorPadClass * klass)
185 GObjectClass *gobject_class;
187 gobject_class = (GObjectClass *) klass;
189 selector_pad_parent_class = g_type_class_peek_parent (klass);
191 gobject_class->finalize = gst_selector_pad_finalize;
193 gobject_class->get_property =
194 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
195 gobject_class->set_property =
196 GST_DEBUG_FUNCPTR (gst_selector_pad_set_property);
198 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
199 g_param_spec_int64 ("running-time", "Running time",
200 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
201 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
202 g_param_spec_boxed ("tags", "Tags",
203 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
205 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
206 g_param_spec_boolean ("active", "Active",
207 "If the pad is currently active", FALSE, G_PARAM_READABLE));
208 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
209 g_param_spec_boolean ("always-ok", "Always OK",
210 "Make an inactive pad return OK instead of NOT_LINKED",
211 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE));
215 gst_selector_pad_init (GstSelectorPad * pad)
217 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
218 gst_selector_pad_reset (pad);
222 gst_selector_pad_finalize (GObject * object)
226 pad = GST_SELECTOR_PAD_CAST (object);
229 gst_tag_list_free (pad->tags);
231 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
235 gst_selector_pad_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec)
238 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
241 case PROP_PAD_ALWAYS_OK:
242 GST_OBJECT_LOCK (object);
243 spad->always_ok = g_value_get_boolean (value);
244 GST_OBJECT_UNLOCK (object);
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 gst_selector_pad_get_property (GObject * object, guint prop_id,
254 GValue * value, GParamSpec * pspec)
256 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
259 case PROP_PAD_RUNNING_TIME:
260 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
263 GST_OBJECT_LOCK (object);
264 g_value_set_boxed (value, spad->tags);
265 GST_OBJECT_UNLOCK (object);
267 case PROP_PAD_ACTIVE:
269 GstInputSelector *sel;
271 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
272 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
273 GST_PAD_CAST (spad)));
274 gst_object_unref (sel);
277 case PROP_PAD_ALWAYS_OK:
278 GST_OBJECT_LOCK (object);
279 g_value_set_boolean (value, spad->always_ok);
280 GST_OBJECT_UNLOCK (object);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gst_selector_pad_get_running_time (GstSelectorPad * pad)
293 GST_OBJECT_LOCK (pad);
295 gint64 last_stop = pad->segment.last_stop;
298 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
301 GST_OBJECT_UNLOCK (pad);
303 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
304 GST_TIME_ARGS (ret));
310 gst_selector_pad_reset (GstSelectorPad * pad)
312 GST_OBJECT_LOCK (pad);
315 pad->segment_pending = FALSE;
316 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
317 GST_OBJECT_UNLOCK (pad);
320 /* strictly get the linked pad from the sinkpad. If the pad is active we return
321 * the srcpad else we return NULL */
323 gst_selector_pad_get_linked_pads (GstPad * pad)
327 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
331 /* need to drop the ref, internal linked pads is not MT safe */
332 gst_object_unref (otherpad);
334 return g_list_append (NULL, otherpad);
338 gst_selector_pad_event (GstPad * pad, GstEvent * event)
341 gboolean forward = TRUE;
342 GstInputSelector *sel;
343 GstSelectorPad *selpad;
345 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
346 selpad = GST_SELECTOR_PAD_CAST (pad);
348 /* only forward if we are dealing with the active sinkpad */
349 forward = gst_input_selector_is_active_sinkpad (sel, pad);
351 /* forward all events in select_all mode by default */
352 if (sel->select_all) {
356 switch (GST_EVENT_TYPE (event)) {
357 case GST_EVENT_FLUSH_START:
358 /* FIXME, flush out the waiter */
360 case GST_EVENT_FLUSH_STOP:
361 GST_INPUT_SELECTOR_LOCK (sel);
362 gst_selector_pad_reset (selpad);
363 sel->pending_close = FALSE;
364 GST_INPUT_SELECTOR_UNLOCK (sel);
366 case GST_EVENT_NEWSEGMENT:
371 gint64 start, stop, time;
373 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
374 &start, &stop, &time);
376 GST_DEBUG_OBJECT (pad,
377 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
379 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
380 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
382 GST_INPUT_SELECTOR_LOCK (sel);
383 GST_OBJECT_LOCK (selpad);
384 gst_segment_set_newsegment_full (&selpad->segment, update,
385 rate, arate, format, start, stop, time);
386 GST_OBJECT_UNLOCK (selpad);
387 /* we are not going to forward the segment, mark the segment as
390 selpad->segment_pending = TRUE;
391 GST_INPUT_SELECTOR_UNLOCK (sel);
399 GST_OBJECT_LOCK (selpad);
401 gst_tag_list_free (selpad->tags);
402 gst_event_parse_tag (event, &tags);
404 tags = gst_tag_list_copy (tags);
406 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, selpad->tags);
407 GST_OBJECT_UNLOCK (selpad);
412 GST_DEBUG_OBJECT (pad, "received EOS");
413 /* don't forward eos in select_all mode until all sink pads have eos */
414 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
422 GST_DEBUG_OBJECT (pad, "forwarding event");
423 res = gst_pad_push_event (sel->srcpad, event);
425 gst_event_unref (event);
427 gst_object_unref (sel);
433 gst_selector_pad_getcaps (GstPad * pad)
435 GstInputSelector *sel;
438 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
440 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
441 caps = gst_pad_peer_get_caps (sel->srcpad);
443 caps = gst_caps_new_any ();
445 gst_object_unref (sel);
451 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
452 guint size, GstCaps * caps, GstBuffer ** buf)
454 GstInputSelector *sel;
455 GstFlowReturn result;
456 GstPad *active_sinkpad;
457 GstSelectorPad *selpad;
459 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
460 selpad = GST_SELECTOR_PAD_CAST (pad);
462 GST_DEBUG_OBJECT (pad, "received alloc");
464 GST_INPUT_SELECTOR_LOCK (sel);
465 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
467 if (pad != active_sinkpad)
470 GST_INPUT_SELECTOR_UNLOCK (sel);
472 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
475 gst_object_unref (sel);
482 GST_INPUT_SELECTOR_UNLOCK (sel);
484 /* unselected pad, perform fallback alloc or return unlinked when
486 GST_OBJECT_LOCK (selpad);
487 if (selpad->always_ok) {
488 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
490 result = GST_FLOW_OK;
492 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
493 result = GST_FLOW_NOT_LINKED;
495 GST_OBJECT_UNLOCK (selpad);
501 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
502 * or return TRUE when flushing */
504 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
506 while (self->blocked && !self->flushing) {
507 /* we can be unlocked here when we are shutting down (flushing) or when we
509 GST_INPUT_SELECTOR_WAIT (self);
511 return self->flushing;
515 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
517 GstInputSelector *sel;
519 GstPad *active_sinkpad;
520 GstSelectorPad *selpad;
521 GstClockTime end_time, duration;
523 GstEvent *close_event = NULL, *start_event = NULL;
525 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
526 selpad = GST_SELECTOR_PAD_CAST (pad);
527 seg = &selpad->segment;
529 GST_INPUT_SELECTOR_LOCK (sel);
530 /* wait or check for flushing */
531 if (gst_input_selector_wait (sel, pad))
534 GST_DEBUG_OBJECT (pad, "getting active pad");
536 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
538 /* update the segment on the srcpad */
539 end_time = GST_BUFFER_TIMESTAMP (buf);
540 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
541 duration = GST_BUFFER_DURATION (buf);
542 if (GST_CLOCK_TIME_IS_VALID (duration))
543 end_time += duration;
544 GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
545 GST_TIME_ARGS (end_time));
547 GST_OBJECT_LOCK (pad);
548 gst_segment_set_last_stop (seg, seg->format, end_time);
549 GST_OBJECT_UNLOCK (pad);
552 /* Ignore buffers from pads except the selected one */
553 if (pad != active_sinkpad)
556 if (G_UNLIKELY (sel->pending_close)) {
557 GstSegment *cseg = &sel->segment;
559 GST_DEBUG_OBJECT (sel,
560 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
562 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
563 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
564 cseg->start, cseg->stop, cseg->time);
566 /* create update segment */
567 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
568 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
570 sel->pending_close = FALSE;
572 /* if we have a pending segment, push it out now */
573 if (G_UNLIKELY (selpad->segment_pending)) {
574 GST_DEBUG_OBJECT (pad,
575 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
577 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
578 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
579 seg->start, seg->stop, seg->time);
581 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
582 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
584 selpad->segment_pending = FALSE;
586 GST_INPUT_SELECTOR_UNLOCK (sel);
589 gst_pad_push_event (sel->srcpad, close_event);
592 gst_pad_push_event (sel->srcpad, start_event);
594 if (selpad->discont) {
595 buf = gst_buffer_make_metadata_writable (buf);
597 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
598 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
599 selpad->discont = FALSE;
603 GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
604 GST_DEBUG_PAD_NAME (pad));
606 res = gst_pad_push (sel->srcpad, buf);
609 gst_object_unref (sel);
612 /* dropped buffers */
615 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
616 /* when we drop a buffer, we're creating a discont on this pad */
617 selpad->discont = TRUE;
618 GST_INPUT_SELECTOR_UNLOCK (sel);
619 gst_buffer_unref (buf);
621 /* figure out what to return upstream */
622 GST_OBJECT_LOCK (selpad);
623 if (selpad->always_ok)
626 res = GST_FLOW_NOT_LINKED;
627 GST_OBJECT_UNLOCK (selpad);
633 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
634 GST_INPUT_SELECTOR_UNLOCK (sel);
635 gst_buffer_unref (buf);
636 res = GST_FLOW_WRONG_STATE;
641 static void gst_input_selector_init (GstInputSelector * sel);
642 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
643 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
645 static void gst_input_selector_dispose (GObject * object);
647 static void gst_input_selector_set_property (GObject * object,
648 guint prop_id, const GValue * value, GParamSpec * pspec);
649 static void gst_input_selector_get_property (GObject * object,
650 guint prop_id, GValue * value, GParamSpec * pspec);
652 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
653 GstPadTemplate * templ, const gchar * unused);
654 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
656 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
657 element, GstStateChange transition);
659 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
660 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
661 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
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_pad_set_query_function (sel->srcpad,
814 GST_DEBUG_FUNCPTR (gst_input_selector_query));
815 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
816 /* sinkpad management */
817 sel->active_sinkpad = NULL;
819 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
821 sel->lock = g_mutex_new ();
822 sel->cond = g_cond_new ();
823 sel->blocked = FALSE;
825 sel->select_all = FALSE;
829 gst_input_selector_dispose (GObject * object)
831 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
833 if (sel->active_sinkpad) {
834 gst_object_unref (sel->active_sinkpad);
835 sel->active_sinkpad = NULL;
838 g_mutex_free (sel->lock);
842 g_cond_free (sel->cond);
846 G_OBJECT_CLASS (parent_class)->dispose (object);
849 /* Solve the following equation for B.timestamp, and set that as the segment
851 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
854 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
856 return (running_time - segment->accum) * segment->abs_rate + segment->start;
860 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
862 segment->stop = gst_segment_get_timestamp (segment, running_time);
863 segment->last_stop = -1;
867 gst_segment_set_start (GstSegment * segment, gint64 running_time)
869 gint64 new_start, duration;
871 new_start = gst_segment_get_timestamp (segment, running_time);
873 /* this is the duration we skipped */
874 duration = new_start - segment->start;
875 /* add the duration to the accumulated segment time */
876 segment->accum += duration;
877 /* move position in the segment */
878 segment->time += duration;
879 segment->start += duration;
882 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
883 * active pad changed. */
885 gst_input_selector_set_active_pad (GstInputSelector * self,
886 GstPad * pad, gint64 stop_time, gint64 start_time)
888 GstSelectorPad *old, *new;
889 GstPad **active_pad_p;
891 if (pad == self->active_sinkpad)
894 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
895 new = GST_SELECTOR_PAD_CAST (pad);
897 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
898 GST_DEBUG_PAD_NAME (new));
900 if (stop_time == -1 && old) {
901 /* no stop time given, get the latest running_time on the active pad to
902 * close and open the new segment */
903 stop_time = start_time = gst_selector_pad_get_running_time (old);
904 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
908 if (old && old->active && !self->pending_close && stop_time >= 0) {
909 /* schedule a last_stop update if one isn't already scheduled, and a
910 segment has been pushed before. */
911 memcpy (&self->segment, &old->segment, sizeof (self->segment));
912 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
914 gst_segment_set_stop (&self->segment, stop_time);
915 self->pending_close = TRUE;
918 if (new && new->active && start_time >= 0) {
919 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
921 /* schedule a new segment push */
922 gst_segment_set_start (&new->segment, start_time);
923 new->segment_pending = TRUE;
926 active_pad_p = &self->active_sinkpad;
927 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
928 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
929 self->active_sinkpad);
935 gst_input_selector_set_property (GObject * object, guint prop_id,
936 const GValue * value, GParamSpec * pspec)
938 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
941 case PROP_ACTIVE_PAD:
945 pad = g_value_get_object (value);
947 GST_INPUT_SELECTOR_LOCK (sel);
948 gst_input_selector_set_active_pad (sel, pad,
949 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
950 GST_INPUT_SELECTOR_UNLOCK (sel);
953 case PROP_SELECT_ALL:
954 GST_INPUT_SELECTOR_LOCK (object);
955 sel->select_all = g_value_get_boolean (value);
956 GST_INPUT_SELECTOR_UNLOCK (object);
959 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
965 gst_input_selector_get_property (GObject * object, guint prop_id,
966 GValue * value, GParamSpec * pspec)
968 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
972 GST_INPUT_SELECTOR_LOCK (object);
973 g_value_set_uint (value, sel->n_pads);
974 GST_INPUT_SELECTOR_UNLOCK (object);
976 case PROP_ACTIVE_PAD:
977 GST_INPUT_SELECTOR_LOCK (object);
978 g_value_set_object (value, sel->active_sinkpad);
979 GST_INPUT_SELECTOR_UNLOCK (object);
981 case PROP_SELECT_ALL:
982 GST_INPUT_SELECTOR_LOCK (object);
983 g_value_set_boolean (value, sel->select_all);
984 GST_INPUT_SELECTOR_UNLOCK (object);
987 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
993 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
995 GstInputSelector *sel;
996 GstPad *otherpad = NULL;
998 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1000 GST_INPUT_SELECTOR_LOCK (sel);
1001 if (pad == sel->srcpad)
1002 otherpad = sel->active_sinkpad;
1003 else if (pad == sel->active_sinkpad || !strict)
1004 otherpad = sel->srcpad;
1006 gst_object_ref (otherpad);
1007 GST_INPUT_SELECTOR_UNLOCK (sel);
1009 gst_object_unref (sel);
1014 /* query on the srcpad. We override this function because by default it will
1015 * only forward the query to one random sinkpad */
1017 gst_input_selector_query (GstPad * pad, GstQuery * query)
1020 GstInputSelector *sel;
1022 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1024 switch (GST_QUERY_TYPE (query)) {
1025 case GST_QUERY_LATENCY:
1028 GstClockTime resmin, resmax;
1035 /* assume FALSE, we become TRUE if one query succeeds */
1038 /* perform the query on all sinkpads and combine the results. We take the
1039 * max of min and the min of max for the result latency. */
1040 GST_INPUT_SELECTOR_LOCK (sel);
1041 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1042 walk = g_list_next (walk)) {
1043 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1045 if (gst_pad_peer_query (sinkpad, query)) {
1046 GstClockTime min, max;
1049 /* one query succeeded, we succeed too */
1052 gst_query_parse_latency (query, &live, &min, &max);
1054 GST_DEBUG_OBJECT (sinkpad,
1055 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1056 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1063 else if (max < resmax)
1065 if (reslive == FALSE)
1070 GST_INPUT_SELECTOR_UNLOCK (sel);
1072 gst_query_set_latency (query, reslive, resmin, resmax);
1074 GST_DEBUG_OBJECT (sel,
1075 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1076 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1083 res = gst_pad_query_default (pad, query);
1086 gst_object_unref (sel);
1092 gst_input_selector_getcaps (GstPad * pad)
1098 parent = gst_object_get_parent (GST_OBJECT (pad));
1100 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1103 if (GST_INPUT_SELECTOR (parent)->select_all) {
1104 GST_DEBUG_OBJECT (parent,
1105 "Pad %s:%s not linked, returning merge of caps",
1106 GST_DEBUG_PAD_NAME (pad));
1107 caps = gst_pad_proxy_getcaps (pad);
1109 GST_DEBUG_OBJECT (parent,
1110 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1111 caps = gst_caps_new_any ();
1114 GST_DEBUG_OBJECT (parent,
1115 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1116 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1117 /* if the peer has caps, use those. If the pad is not linked, this function
1118 * returns NULL and we return ANY */
1119 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1120 caps = gst_caps_new_any ();
1121 gst_object_unref (otherpad);
1124 gst_object_unref (parent);
1128 /* check if the pad is the active sinkpad */
1130 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1132 GstSelectorPad *selpad;
1135 selpad = GST_SELECTOR_PAD_CAST (pad);
1137 GST_INPUT_SELECTOR_LOCK (sel);
1138 res = (pad == sel->active_sinkpad);
1139 GST_INPUT_SELECTOR_UNLOCK (sel);
1144 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1146 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1148 GstPad *active_sinkpad;
1149 GstSelectorPad *selpad;
1151 selpad = GST_SELECTOR_PAD_CAST (pad);
1153 selpad->active = TRUE;
1154 active_sinkpad = sel->active_sinkpad;
1155 if (active_sinkpad == NULL || sel->select_all) {
1156 /* first pad we get activity on becomes the activated pad by default, if we
1157 * select all, we also remember the last used pad. */
1158 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1159 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1162 return active_sinkpad;
1166 gst_input_selector_get_linked_pads (GstPad * pad)
1170 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1173 /* need to drop the ref, internal linked pads is not MT safe */
1174 gst_object_unref (otherpad);
1175 return g_list_append (NULL, otherpad);
1179 gst_input_selector_request_new_pad (GstElement * element,
1180 GstPadTemplate * templ, const gchar * unused)
1182 GstInputSelector *sel;
1184 GstPad *sinkpad = NULL;
1186 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1188 sel = GST_INPUT_SELECTOR (element);
1190 GST_INPUT_SELECTOR_LOCK (sel);
1192 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1193 name = g_strdup_printf ("sink%d", sel->padcount++);
1194 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1195 "name", name, "direction", templ->direction, "template", templ, NULL);
1200 gst_pad_set_event_function (sinkpad,
1201 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1202 gst_pad_set_getcaps_function (sinkpad,
1203 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1204 gst_pad_set_chain_function (sinkpad,
1205 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1206 gst_pad_set_internal_link_function (sinkpad,
1207 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
1208 gst_pad_set_bufferalloc_function (sinkpad,
1209 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1211 gst_pad_set_active (sinkpad, TRUE);
1212 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1213 GST_INPUT_SELECTOR_UNLOCK (sel);
1219 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1221 GstInputSelector *sel;
1223 sel = GST_INPUT_SELECTOR (element);
1224 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1226 GST_INPUT_SELECTOR_LOCK (sel);
1227 /* if the pad was the active pad, makes us select a new one */
1228 if (sel->active_sinkpad == pad) {
1229 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1230 sel->active_sinkpad = NULL;
1234 gst_pad_set_active (pad, FALSE);
1235 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1236 GST_INPUT_SELECTOR_UNLOCK (sel);
1239 static GstStateChangeReturn
1240 gst_input_selector_change_state (GstElement * element,
1241 GstStateChange transition)
1243 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1244 GstStateChangeReturn result;
1246 switch (transition) {
1247 case GST_STATE_CHANGE_READY_TO_PAUSED:
1248 GST_INPUT_SELECTOR_LOCK (self);
1249 self->blocked = FALSE;
1250 self->flushing = FALSE;
1251 GST_INPUT_SELECTOR_UNLOCK (self);
1253 case GST_STATE_CHANGE_PAUSED_TO_READY:
1254 /* first unlock before we call the parent state change function, which
1255 * tries to acquire the stream lock when going to ready. */
1256 GST_INPUT_SELECTOR_LOCK (self);
1257 self->blocked = FALSE;
1258 self->flushing = TRUE;
1259 GST_INPUT_SELECTOR_BROADCAST (self);
1260 GST_INPUT_SELECTOR_UNLOCK (self);
1266 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1272 gst_input_selector_block (GstInputSelector * self)
1275 GstSelectorPad *spad;
1277 GST_INPUT_SELECTOR_LOCK (self);
1280 GST_WARNING_OBJECT (self, "switch already blocked");
1282 self->blocked = TRUE;
1283 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1286 ret = gst_selector_pad_get_running_time (spad);
1288 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1290 GST_INPUT_SELECTOR_UNLOCK (self);
1295 /* stop_time and start_time are running times */
1297 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1298 gint64 stop_time, gint64 start_time)
1302 g_return_if_fail (self->blocked == TRUE);
1304 GST_INPUT_SELECTOR_LOCK (self);
1306 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1308 self->blocked = FALSE;
1309 GST_INPUT_SELECTOR_BROADCAST (self);
1310 GST_INPUT_SELECTOR_UNLOCK (self);
1313 g_object_notify (G_OBJECT (self), "active-pad");
1317 gst_input_selector_check_eos (GstElement * selector)
1319 GstIterator *it = gst_element_iterate_sink_pads (selector);
1320 GstIteratorResult ires;
1322 gboolean done = FALSE, is_eos = FALSE;
1323 GstSelectorPad *pad;
1326 ires = gst_iterator_next (it, &item);
1328 case GST_ITERATOR_DONE:
1329 GST_INFO_OBJECT (selector, "all sink pads have eos");
1333 case GST_ITERATOR_OK:
1334 pad = GST_SELECTOR_PAD_CAST (item);
1339 case GST_ITERATOR_RESYNC:
1340 gst_iterator_resync (it);
1347 gst_iterator_free (it);