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, G_PARAM_READABLE));
191 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
192 g_param_spec_boxed ("tags", "Tags",
193 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
195 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
196 g_param_spec_boolean ("active", "Active",
197 "If the pad is currently active", FALSE, G_PARAM_READABLE));
198 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
199 g_param_spec_boolean ("always-ok", "Always OK",
200 "Make an inactive pad return OK instead of NOT_LINKED",
201 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE));
205 gst_selector_pad_init (GstSelectorPad * pad)
207 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
208 gst_selector_pad_reset (pad);
212 gst_selector_pad_finalize (GObject * object)
216 pad = GST_SELECTOR_PAD_CAST (object);
219 gst_tag_list_free (pad->tags);
221 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
225 gst_selector_pad_set_property (GObject * object, guint prop_id,
226 const GValue * value, GParamSpec * pspec)
228 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
231 case PROP_PAD_ALWAYS_OK:
232 GST_OBJECT_LOCK (object);
233 spad->always_ok = g_value_get_boolean (value);
234 GST_OBJECT_UNLOCK (object);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gst_selector_pad_get_property (GObject * object, guint prop_id,
244 GValue * value, GParamSpec * pspec)
246 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
249 case PROP_PAD_RUNNING_TIME:
250 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
253 GST_OBJECT_LOCK (object);
254 g_value_set_boxed (value, spad->tags);
255 GST_OBJECT_UNLOCK (object);
257 case PROP_PAD_ACTIVE:
259 GstInputSelector *sel;
261 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
262 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
263 GST_PAD_CAST (spad)));
264 gst_object_unref (sel);
267 case PROP_PAD_ALWAYS_OK:
268 GST_OBJECT_LOCK (object);
269 g_value_set_boolean (value, spad->always_ok);
270 GST_OBJECT_UNLOCK (object);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279 gst_selector_pad_get_running_time (GstSelectorPad * pad)
283 GST_OBJECT_LOCK (pad);
285 gint64 last_stop = pad->segment.last_stop;
288 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
291 GST_OBJECT_UNLOCK (pad);
293 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
294 GST_TIME_ARGS (ret));
300 gst_selector_pad_reset (GstSelectorPad * pad)
302 GST_OBJECT_LOCK (pad);
305 pad->segment_pending = FALSE;
306 pad->discont = FALSE;
307 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
308 GST_OBJECT_UNLOCK (pad);
311 /* strictly get the linked pad from the sinkpad. If the pad is active we return
312 * the srcpad else we return NULL */
314 gst_selector_pad_iterate_linked_pads (GstPad * pad)
316 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
320 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
321 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
322 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
325 gst_object_unref (otherpad);
326 gst_object_unref (sel);
332 gst_selector_pad_event (GstPad * pad, GstEvent * event)
335 gboolean forward = TRUE;
336 GstInputSelector *sel;
337 GstSelectorPad *selpad;
338 GstPad *prev_active_sinkpad;
339 GstPad *active_sinkpad;
341 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
342 selpad = GST_SELECTOR_PAD_CAST (pad);
344 GST_INPUT_SELECTOR_LOCK (sel);
345 prev_active_sinkpad = sel->active_sinkpad;
346 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
348 /* only forward if we are dealing with the active sinkpad or if select_all
350 if (pad != active_sinkpad && !sel->select_all)
352 GST_INPUT_SELECTOR_UNLOCK (sel);
354 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
355 g_object_notify (G_OBJECT (sel), "active-pad");
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);
389 /* If we aren't forwarding the event (because the pad is not the
390 * active_sinkpad, and select_all is not set, then set the flag on the
391 * that says a segment needs sending if/when that pad is activated.
392 * For all other cases, we send the event immediately, which makes
393 * sparse streams and other segment updates work correctly downstream.
396 selpad->segment_pending = TRUE;
398 GST_INPUT_SELECTOR_UNLOCK (sel);
403 GstTagList *tags, *oldtags, *newtags;
405 gst_event_parse_tag (event, &tags);
407 GST_OBJECT_LOCK (selpad);
408 oldtags = selpad->tags;
410 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
411 selpad->tags = newtags;
413 gst_tag_list_free (oldtags);
414 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
415 GST_OBJECT_UNLOCK (selpad);
417 g_object_notify (G_OBJECT (selpad), "tags");
422 GST_DEBUG_OBJECT (pad, "received EOS");
423 /* don't forward eos in select_all mode until all sink pads have eos */
424 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
432 GST_DEBUG_OBJECT (pad, "forwarding event");
433 res = gst_pad_push_event (sel->srcpad, event);
435 gst_event_unref (event);
437 gst_object_unref (sel);
443 gst_selector_pad_getcaps (GstPad * pad)
445 GstInputSelector *sel;
448 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
450 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
451 caps = gst_pad_peer_get_caps (sel->srcpad);
453 caps = gst_caps_new_any ();
455 gst_object_unref (sel);
461 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
463 GstInputSelector *sel;
466 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
468 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
469 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
470 gst_object_unref (sel);
476 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
477 guint size, GstCaps * caps, GstBuffer ** buf)
479 GstInputSelector *sel;
480 GstFlowReturn result;
481 GstPad *active_sinkpad;
482 GstPad *prev_active_sinkpad;
483 GstSelectorPad *selpad;
485 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
486 selpad = GST_SELECTOR_PAD_CAST (pad);
488 GST_DEBUG_OBJECT (pad, "received alloc");
490 GST_INPUT_SELECTOR_LOCK (sel);
491 prev_active_sinkpad = sel->active_sinkpad;
492 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
494 if (pad != active_sinkpad)
497 GST_INPUT_SELECTOR_UNLOCK (sel);
499 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
500 g_object_notify (G_OBJECT (sel), "active-pad");
502 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
505 gst_object_unref (sel);
512 GST_INPUT_SELECTOR_UNLOCK (sel);
514 /* unselected pad, perform fallback alloc or return unlinked when
516 GST_OBJECT_LOCK (selpad);
517 if (selpad->always_ok) {
518 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
520 result = GST_FLOW_OK;
522 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
523 result = GST_FLOW_NOT_LINKED;
525 GST_OBJECT_UNLOCK (selpad);
531 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
532 * or return TRUE when flushing */
534 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
536 while (self->blocked && !self->flushing) {
537 /* we can be unlocked here when we are shutting down (flushing) or when we
539 GST_INPUT_SELECTOR_WAIT (self);
541 return self->flushing;
545 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
547 GstInputSelector *sel;
549 GstPad *active_sinkpad;
550 GstPad *prev_active_sinkpad;
551 GstSelectorPad *selpad;
552 GstClockTime start_time;
554 GstEvent *close_event = NULL, *start_event = NULL;
557 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
558 selpad = GST_SELECTOR_PAD_CAST (pad);
559 seg = &selpad->segment;
561 GST_INPUT_SELECTOR_LOCK (sel);
562 /* wait or check for flushing */
563 if (gst_input_selector_wait (sel, pad))
566 GST_DEBUG_OBJECT (pad, "getting active pad");
568 prev_active_sinkpad = sel->active_sinkpad;
569 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
571 /* update the segment on the srcpad */
572 start_time = GST_BUFFER_TIMESTAMP (buf);
573 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
574 GST_DEBUG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
575 GST_TIME_ARGS (start_time));
576 if (GST_BUFFER_DURATION_IS_VALID (buf))
577 GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
578 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
580 GST_OBJECT_LOCK (pad);
581 gst_segment_set_last_stop (seg, seg->format, start_time);
582 GST_OBJECT_UNLOCK (pad);
585 /* Ignore buffers from pads except the selected one */
586 if (pad != active_sinkpad)
589 if (G_UNLIKELY (sel->pending_close)) {
590 GstSegment *cseg = &sel->segment;
592 GST_DEBUG_OBJECT (sel,
593 "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
595 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
596 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
597 cseg->start, cseg->stop, cseg->time);
599 /* create update segment */
600 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
601 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
603 sel->pending_close = FALSE;
605 /* if we have a pending segment, push it out now */
606 if (G_UNLIKELY (selpad->segment_pending)) {
607 GST_DEBUG_OBJECT (pad,
608 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
610 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
611 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
612 seg->start, seg->stop, seg->time);
614 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
615 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
617 selpad->segment_pending = FALSE;
619 GST_INPUT_SELECTOR_UNLOCK (sel);
621 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
622 g_object_notify (G_OBJECT (sel), "active-pad");
625 gst_pad_push_event (sel->srcpad, close_event);
628 gst_pad_push_event (sel->srcpad, start_event);
630 if (selpad->discont) {
631 buf = gst_buffer_make_metadata_writable (buf);
633 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
634 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
635 selpad->discont = FALSE;
639 GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
640 GST_DEBUG_PAD_NAME (pad));
642 if ((caps = GST_BUFFER_CAPS (buf))) {
643 if (GST_PAD_CAPS (sel->srcpad) != caps)
644 gst_pad_set_caps (sel->srcpad, caps);
647 res = gst_pad_push (sel->srcpad, buf);
650 gst_object_unref (sel);
653 /* dropped buffers */
656 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
657 /* when we drop a buffer, we're creating a discont on this pad */
658 selpad->discont = TRUE;
659 GST_INPUT_SELECTOR_UNLOCK (sel);
660 gst_buffer_unref (buf);
662 /* figure out what to return upstream */
663 GST_OBJECT_LOCK (selpad);
664 if (selpad->always_ok)
667 res = GST_FLOW_NOT_LINKED;
668 GST_OBJECT_UNLOCK (selpad);
674 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
675 GST_INPUT_SELECTOR_UNLOCK (sel);
676 gst_buffer_unref (buf);
677 res = GST_FLOW_WRONG_STATE;
682 static void gst_input_selector_dispose (GObject * object);
684 static void gst_input_selector_set_property (GObject * object,
685 guint prop_id, const GValue * value, GParamSpec * pspec);
686 static void gst_input_selector_get_property (GObject * object,
687 guint prop_id, GValue * value, GParamSpec * pspec);
689 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
690 GstPadTemplate * templ, const gchar * unused);
691 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
693 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
694 element, GstStateChange transition);
696 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
697 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
698 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
699 static gint64 gst_input_selector_block (GstInputSelector * self);
700 static void gst_input_selector_switch (GstInputSelector * self,
701 GstPad * pad, gint64 stop_time, gint64 start_time);
703 #define _do_init(bla) \
704 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
705 "input-selector", 0, "An input stream selector element");
707 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
708 GST_TYPE_ELEMENT, _do_init);
711 gst_input_selector_base_init (gpointer g_class)
713 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
715 gst_element_class_set_details_simple (element_class, "Input selector",
716 "Generic", "N-to-1 input stream selectoring",
717 "Julien Moutte <julien@moutte.net>, "
718 "Jan Schmidt <thaytan@mad.scientist.com>, "
719 "Wim Taymans <wim.taymans@gmail.com>");
720 gst_element_class_add_pad_template (element_class,
721 gst_static_pad_template_get (&gst_input_selector_sink_factory));
722 gst_element_class_add_pad_template (element_class,
723 gst_static_pad_template_get (&gst_input_selector_src_factory));
727 gst_input_selector_class_init (GstInputSelectorClass * klass)
729 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
730 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
732 parent_class = g_type_class_peek_parent (klass);
734 gobject_class->dispose = gst_input_selector_dispose;
736 gobject_class->set_property = gst_input_selector_set_property;
737 gobject_class->get_property = gst_input_selector_get_property;
739 g_object_class_install_property (gobject_class, PROP_N_PADS,
740 g_param_spec_uint ("n-pads", "Number of Pads",
741 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
743 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
744 g_param_spec_object ("active-pad", "Active pad",
745 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
747 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
748 g_param_spec_boolean ("select-all", "Select all mode",
749 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
752 * GstInputSelector::block:
753 * @inputselector: the #GstInputSelector
755 * Block all sink pads in preparation for a switch. Returns the stop time of
756 * the current switch segment, as a running time, or 0 if there is no current
757 * active pad or the current active pad never received data.
759 gst_input_selector_signals[SIGNAL_BLOCK] =
760 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
761 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
762 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
763 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
765 * GstInputSelector::switch:
766 * @inputselector: the #GstInputSelector
767 * @pad: the pad to switch to
768 * @stop_time: running time at which to close the previous segment, or -1
769 * to use the running time of the previously active sink pad
770 * @start_time: running time at which to start the new segment, or -1 to
771 * use the running time of the newly active sink pad
773 * Switch to a new feed. The segment opened by the previously active pad, if
774 * any, will be closed, and a new segment opened before data flows again.
776 * This signal must be emitted when the element has been blocked via the <link
777 * linkend="GstInputSelector-block">block</link> signal.
779 * If you have a stream with only one switch element, such as an audio-only
780 * stream, a stream switch should be performed by first emitting the block
781 * signal, and then emitting the switch signal with -1 for the stop and start
784 * The intention of the @stop_time and @start_time arguments is to allow
785 * multiple switch elements to switch and maintain stream synchronization.
786 * When switching a stream with multiple feeds, you will need as many switch
787 * elements as you have feeds. For example, a feed with audio and video will
788 * have one switch element between the audio feeds and one for video.
790 * A switch over multiple switch elements should be performed as follows:
791 * First, emit the <link linkend="GstInputSelector-block">block</link>
792 * signal, collecting the returned values. The maximum running time returned
793 * by block should then be used as the time at which to close the previous
796 * Then, query the running times of the new audio and video pads that you will
797 * switch to. Naturally, these pads are on separate switch elements. Take the
798 * minimum running time for those streams and use it for the time at which to
799 * open the new segment.
801 * If @pad is the same as the current active pad, the element will cancel any
802 * previous block without adjusting segments.
805 * the signal changed from accepting the pad name to the pad object.
810 gst_input_selector_signals[SIGNAL_SWITCH] =
811 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
812 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
813 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
814 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
816 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
817 gstelement_class->release_pad = gst_input_selector_release_pad;
818 gstelement_class->change_state = gst_input_selector_change_state;
820 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
821 /* note the underscore because switch is a keyword otherwise */
822 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
826 gst_input_selector_init (GstInputSelector * sel,
827 GstInputSelectorClass * g_class)
829 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
830 gst_pad_set_iterate_internal_links_function (sel->srcpad,
831 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
832 gst_pad_set_getcaps_function (sel->srcpad,
833 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
834 gst_pad_set_query_function (sel->srcpad,
835 GST_DEBUG_FUNCPTR (gst_input_selector_query));
836 gst_pad_set_event_function (sel->srcpad,
837 GST_DEBUG_FUNCPTR (gst_input_selector_event));
838 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
839 /* sinkpad management */
840 sel->active_sinkpad = NULL;
842 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
844 sel->lock = g_mutex_new ();
845 sel->cond = g_cond_new ();
846 sel->blocked = FALSE;
848 sel->select_all = FALSE;
852 gst_input_selector_dispose (GObject * object)
854 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
856 if (sel->active_sinkpad) {
857 gst_object_unref (sel->active_sinkpad);
858 sel->active_sinkpad = NULL;
861 g_mutex_free (sel->lock);
865 g_cond_free (sel->cond);
869 G_OBJECT_CLASS (parent_class)->dispose (object);
872 /* Solve the following equation for B.timestamp, and set that as the segment
874 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
877 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
879 if (running_time <= segment->accum)
880 return segment->start;
882 return (running_time - segment->accum) * segment->abs_rate + segment->start;
886 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
888 segment->stop = gst_segment_get_timestamp (segment, running_time);
889 segment->last_stop = -1;
893 gst_segment_set_start (GstSegment * segment, gint64 running_time)
895 gint64 new_start, duration;
897 new_start = gst_segment_get_timestamp (segment, running_time);
899 /* this is the duration we skipped */
900 duration = new_start - segment->start;
901 /* add the duration to the accumulated segment time */
902 segment->accum += duration;
903 /* move position in the segment */
904 segment->time += duration;
905 segment->start += duration;
908 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
909 * active pad changed. */
911 gst_input_selector_set_active_pad (GstInputSelector * self,
912 GstPad * pad, gint64 stop_time, gint64 start_time)
914 GstSelectorPad *old, *new;
915 GstPad **active_pad_p;
917 if (pad == self->active_sinkpad)
920 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
921 new = GST_SELECTOR_PAD_CAST (pad);
923 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
924 GST_DEBUG_PAD_NAME (new));
926 if (stop_time == -1 && old) {
927 /* no stop time given, get the latest running_time on the active pad to
928 * close and open the new segment */
929 stop_time = start_time = gst_selector_pad_get_running_time (old);
930 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
934 if (old && old->active && !self->pending_close && stop_time >= 0) {
935 /* schedule a last_stop update if one isn't already scheduled, and a
936 segment has been pushed before. */
937 memcpy (&self->segment, &old->segment, sizeof (self->segment));
939 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
941 gst_segment_set_stop (&self->segment, stop_time);
942 self->pending_close = TRUE;
945 if (new && new->active && start_time >= 0) {
946 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
948 /* schedule a new segment push */
949 gst_segment_set_start (&new->segment, start_time);
950 new->segment_pending = TRUE;
953 active_pad_p = &self->active_sinkpad;
954 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
955 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
956 self->active_sinkpad);
962 gst_input_selector_set_property (GObject * object, guint prop_id,
963 const GValue * value, GParamSpec * pspec)
965 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
968 case PROP_ACTIVE_PAD:
972 pad = g_value_get_object (value);
974 GST_INPUT_SELECTOR_LOCK (sel);
975 gst_input_selector_set_active_pad (sel, pad,
976 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
977 GST_INPUT_SELECTOR_UNLOCK (sel);
980 case PROP_SELECT_ALL:
981 GST_INPUT_SELECTOR_LOCK (object);
982 sel->select_all = g_value_get_boolean (value);
983 GST_INPUT_SELECTOR_UNLOCK (object);
986 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
992 gst_input_selector_get_property (GObject * object, guint prop_id,
993 GValue * value, GParamSpec * pspec)
995 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
999 GST_INPUT_SELECTOR_LOCK (object);
1000 g_value_set_uint (value, sel->n_pads);
1001 GST_INPUT_SELECTOR_UNLOCK (object);
1003 case PROP_ACTIVE_PAD:
1004 GST_INPUT_SELECTOR_LOCK (object);
1005 g_value_set_object (value, sel->active_sinkpad);
1006 GST_INPUT_SELECTOR_UNLOCK (object);
1008 case PROP_SELECT_ALL:
1009 GST_INPUT_SELECTOR_LOCK (object);
1010 g_value_set_boolean (value, sel->select_all);
1011 GST_INPUT_SELECTOR_UNLOCK (object);
1014 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1022 GstInputSelector *sel;
1023 GstPad *otherpad = NULL;
1025 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1027 GST_INPUT_SELECTOR_LOCK (sel);
1028 if (pad == sel->srcpad)
1029 otherpad = sel->active_sinkpad;
1030 else if (pad == sel->active_sinkpad || !strict)
1031 otherpad = sel->srcpad;
1033 gst_object_ref (otherpad);
1034 GST_INPUT_SELECTOR_UNLOCK (sel);
1036 gst_object_unref (sel);
1042 gst_input_selector_event (GstPad * pad, GstEvent * event)
1044 gboolean res = FALSE;
1047 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1050 res = gst_pad_push_event (otherpad, event);
1052 gst_object_unref (otherpad);
1054 gst_event_unref (event);
1058 /* query on the srcpad. We override this function because by default it will
1059 * only forward the query to one random sinkpad */
1061 gst_input_selector_query (GstPad * pad, GstQuery * query)
1063 gboolean res = TRUE;
1064 GstInputSelector *sel;
1067 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1069 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1071 switch (GST_QUERY_TYPE (query)) {
1072 case GST_QUERY_LATENCY:
1075 GstClockTime resmin, resmax;
1082 /* assume FALSE, we become TRUE if one query succeeds */
1085 /* perform the query on all sinkpads and combine the results. We take the
1086 * max of min and the min of max for the result latency. */
1087 GST_INPUT_SELECTOR_LOCK (sel);
1088 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1089 walk = g_list_next (walk)) {
1090 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1092 if (gst_pad_peer_query (sinkpad, query)) {
1093 GstClockTime min, max;
1096 /* one query succeeded, we succeed too */
1099 gst_query_parse_latency (query, &live, &min, &max);
1101 GST_DEBUG_OBJECT (sinkpad,
1102 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1103 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1110 else if (max < resmax)
1112 if (reslive == FALSE)
1117 GST_INPUT_SELECTOR_UNLOCK (sel);
1119 gst_query_set_latency (query, reslive, resmin, resmax);
1121 GST_DEBUG_OBJECT (sel,
1122 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1123 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1131 res = gst_pad_peer_query (otherpad, query);
1135 gst_object_unref (otherpad);
1136 gst_object_unref (sel);
1142 gst_input_selector_getcaps (GstPad * pad)
1148 parent = gst_object_get_parent (GST_OBJECT (pad));
1150 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1153 if (GST_INPUT_SELECTOR (parent)->select_all) {
1154 GST_DEBUG_OBJECT (parent,
1155 "Pad %s:%s not linked, returning merge of caps",
1156 GST_DEBUG_PAD_NAME (pad));
1157 caps = gst_pad_proxy_getcaps (pad);
1159 GST_DEBUG_OBJECT (parent,
1160 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1161 caps = gst_caps_new_any ();
1164 GST_DEBUG_OBJECT (parent,
1165 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1166 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1167 /* if the peer has caps, use those. If the pad is not linked, this function
1168 * returns NULL and we return ANY */
1169 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1170 caps = gst_caps_new_any ();
1171 gst_object_unref (otherpad);
1174 gst_object_unref (parent);
1178 /* check if the pad is the active sinkpad */
1179 static inline gboolean
1180 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1184 GST_INPUT_SELECTOR_LOCK (sel);
1185 res = (pad == sel->active_sinkpad);
1186 GST_INPUT_SELECTOR_UNLOCK (sel);
1191 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1193 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1195 GstPad *active_sinkpad;
1196 GstSelectorPad *selpad;
1198 selpad = GST_SELECTOR_PAD_CAST (pad);
1200 selpad->active = TRUE;
1201 active_sinkpad = sel->active_sinkpad;
1202 if (active_sinkpad == NULL || sel->select_all) {
1203 /* first pad we get activity on becomes the activated pad by default, if we
1204 * select all, we also remember the last used pad. */
1205 if (sel->active_sinkpad)
1206 gst_object_unref (sel->active_sinkpad);
1207 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1208 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1211 return active_sinkpad;
1215 gst_input_selector_request_new_pad (GstElement * element,
1216 GstPadTemplate * templ, const gchar * unused)
1218 GstInputSelector *sel;
1220 GstPad *sinkpad = NULL;
1222 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1224 sel = GST_INPUT_SELECTOR (element);
1226 GST_INPUT_SELECTOR_LOCK (sel);
1228 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1229 name = g_strdup_printf ("sink%d", sel->padcount++);
1230 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1231 "name", name, "direction", templ->direction, "template", templ, NULL);
1236 gst_pad_set_event_function (sinkpad,
1237 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1238 gst_pad_set_getcaps_function (sinkpad,
1239 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1240 gst_pad_set_acceptcaps_function (sinkpad,
1241 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1242 gst_pad_set_chain_function (sinkpad,
1243 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1244 gst_pad_set_iterate_internal_links_function (sinkpad,
1245 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1246 gst_pad_set_bufferalloc_function (sinkpad,
1247 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1249 gst_pad_set_active (sinkpad, TRUE);
1250 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1251 GST_INPUT_SELECTOR_UNLOCK (sel);
1257 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1259 GstInputSelector *sel;
1261 sel = GST_INPUT_SELECTOR (element);
1262 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1264 GST_INPUT_SELECTOR_LOCK (sel);
1265 /* if the pad was the active pad, makes us select a new one */
1266 if (sel->active_sinkpad == pad) {
1267 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1268 gst_object_unref (sel->active_sinkpad);
1269 sel->active_sinkpad = NULL;
1273 gst_pad_set_active (pad, FALSE);
1274 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1275 GST_INPUT_SELECTOR_UNLOCK (sel);
1279 gst_input_selector_reset (GstInputSelector * sel)
1283 GST_INPUT_SELECTOR_LOCK (sel);
1284 /* clear active pad */
1285 if (sel->active_sinkpad) {
1286 gst_object_unref (sel->active_sinkpad);
1287 sel->active_sinkpad = NULL;
1290 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1291 sel->pending_close = FALSE;
1292 /* reset each of our sinkpads state */
1293 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1294 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1296 gst_selector_pad_reset (selpad);
1299 gst_tag_list_free (selpad->tags);
1300 selpad->tags = NULL;
1303 GST_INPUT_SELECTOR_UNLOCK (sel);
1306 static GstStateChangeReturn
1307 gst_input_selector_change_state (GstElement * element,
1308 GstStateChange transition)
1310 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1311 GstStateChangeReturn result;
1313 switch (transition) {
1314 case GST_STATE_CHANGE_READY_TO_PAUSED:
1315 GST_INPUT_SELECTOR_LOCK (self);
1316 self->blocked = FALSE;
1317 self->flushing = FALSE;
1318 GST_INPUT_SELECTOR_UNLOCK (self);
1320 case GST_STATE_CHANGE_PAUSED_TO_READY:
1321 /* first unlock before we call the parent state change function, which
1322 * tries to acquire the stream lock when going to ready. */
1323 GST_INPUT_SELECTOR_LOCK (self);
1324 self->blocked = FALSE;
1325 self->flushing = TRUE;
1326 GST_INPUT_SELECTOR_BROADCAST (self);
1327 GST_INPUT_SELECTOR_UNLOCK (self);
1333 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1335 switch (transition) {
1336 case GST_STATE_CHANGE_PAUSED_TO_READY:
1337 gst_input_selector_reset (self);
1347 gst_input_selector_block (GstInputSelector * self)
1350 GstSelectorPad *spad;
1352 GST_INPUT_SELECTOR_LOCK (self);
1355 GST_WARNING_OBJECT (self, "switch already blocked");
1357 self->blocked = TRUE;
1358 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1361 ret = gst_selector_pad_get_running_time (spad);
1363 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1365 GST_INPUT_SELECTOR_UNLOCK (self);
1370 /* stop_time and start_time are running times */
1372 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1373 gint64 stop_time, gint64 start_time)
1377 g_return_if_fail (self->blocked == TRUE);
1379 GST_INPUT_SELECTOR_LOCK (self);
1381 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1383 self->blocked = FALSE;
1384 GST_INPUT_SELECTOR_BROADCAST (self);
1385 GST_INPUT_SELECTOR_UNLOCK (self);
1388 g_object_notify (G_OBJECT (self), "active-pad");
1392 gst_input_selector_check_eos (GstElement * selector)
1394 GstIterator *it = gst_element_iterate_sink_pads (selector);
1395 GstIteratorResult ires;
1397 gboolean done = FALSE, is_eos = FALSE;
1398 GstSelectorPad *pad;
1401 ires = gst_iterator_next (it, &item);
1403 case GST_ITERATOR_DONE:
1404 GST_INFO_OBJECT (selector, "all sink pads have eos");
1408 case GST_ITERATOR_OK:
1409 pad = GST_SELECTOR_PAD_CAST (item);
1413 gst_object_unref (pad);
1415 case GST_ITERATOR_RESYNC:
1416 gst_iterator_resync (it);
1423 gst_iterator_free (it);