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 gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
153 static GstIterator *gst_selector_pad_iterate_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 pad->discont = FALSE;
318 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
319 GST_OBJECT_UNLOCK (pad);
322 /* strictly get the linked pad from the sinkpad. If the pad is active we return
323 * the srcpad else we return NULL */
326 gst_selector_pad_iterate_linked_pads (GstPad * pad)
328 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
329 GstPad *opad = gst_input_selector_get_linked_pad (pad, TRUE);
330 GstIterator *it = gst_iterator_new_single (GST_TYPE_PAD, opad,
331 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
334 gst_object_unref (opad);
335 gst_object_unref (sel);
341 gst_selector_pad_event (GstPad * pad, GstEvent * event)
344 gboolean forward = TRUE;
345 GstInputSelector *sel;
346 GstSelectorPad *selpad;
347 GstPad *prev_active_sinkpad;
348 GstPad *active_sinkpad;
350 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
351 selpad = GST_SELECTOR_PAD_CAST (pad);
353 GST_INPUT_SELECTOR_LOCK (sel);
354 prev_active_sinkpad = sel->active_sinkpad;
355 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
357 /* only forward if we are dealing with the active sinkpad or if select_all
359 if (pad != active_sinkpad && !sel->select_all)
361 GST_INPUT_SELECTOR_UNLOCK (sel);
363 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
364 g_object_notify (G_OBJECT (sel), "active-pad");
366 switch (GST_EVENT_TYPE (event)) {
367 case GST_EVENT_FLUSH_START:
368 /* FIXME, flush out the waiter */
370 case GST_EVENT_FLUSH_STOP:
371 GST_INPUT_SELECTOR_LOCK (sel);
372 gst_selector_pad_reset (selpad);
373 sel->pending_close = FALSE;
374 GST_INPUT_SELECTOR_UNLOCK (sel);
376 case GST_EVENT_NEWSEGMENT:
381 gint64 start, stop, time;
383 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
384 &start, &stop, &time);
386 GST_DEBUG_OBJECT (pad,
387 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
389 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
390 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
392 GST_INPUT_SELECTOR_LOCK (sel);
393 GST_OBJECT_LOCK (selpad);
394 gst_segment_set_newsegment_full (&selpad->segment, update,
395 rate, arate, format, start, stop, time);
396 GST_OBJECT_UNLOCK (selpad);
398 /* If we aren't forwarding the event (because the pad is not the
399 * active_sinkpad, and select_all is not set, then set the flag on the
400 * that says a segment needs sending if/when that pad is activated.
401 * For all other cases, we send the event immediately, which makes
402 * sparse streams and other segment updates work correctly downstream.
405 selpad->segment_pending = TRUE;
407 GST_INPUT_SELECTOR_UNLOCK (sel);
412 GstTagList *tags, *oldtags, *newtags;
414 gst_event_parse_tag (event, &tags);
416 GST_OBJECT_LOCK (selpad);
417 oldtags = selpad->tags;
419 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
420 selpad->tags = newtags;
422 gst_tag_list_free (oldtags);
423 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
424 GST_OBJECT_UNLOCK (selpad);
426 g_object_notify (G_OBJECT (selpad), "tags");
431 GST_DEBUG_OBJECT (pad, "received EOS");
432 /* don't forward eos in select_all mode until all sink pads have eos */
433 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
441 GST_DEBUG_OBJECT (pad, "forwarding event");
442 res = gst_pad_push_event (sel->srcpad, event);
444 gst_event_unref (event);
446 gst_object_unref (sel);
452 gst_selector_pad_getcaps (GstPad * pad)
454 GstInputSelector *sel;
457 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
459 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
460 caps = gst_pad_peer_get_caps (sel->srcpad);
462 caps = gst_caps_new_any ();
464 gst_object_unref (sel);
470 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
472 GstInputSelector *sel;
475 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
477 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
478 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
479 gst_object_unref (sel);
485 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
486 guint size, GstCaps * caps, GstBuffer ** buf)
488 GstInputSelector *sel;
489 GstFlowReturn result;
490 GstPad *active_sinkpad;
491 GstPad *prev_active_sinkpad;
492 GstSelectorPad *selpad;
494 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
495 selpad = GST_SELECTOR_PAD_CAST (pad);
497 GST_DEBUG_OBJECT (pad, "received alloc");
499 GST_INPUT_SELECTOR_LOCK (sel);
500 prev_active_sinkpad = sel->active_sinkpad;
501 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
503 if (pad != active_sinkpad)
506 GST_INPUT_SELECTOR_UNLOCK (sel);
508 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
509 g_object_notify (G_OBJECT (sel), "active-pad");
511 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
514 gst_object_unref (sel);
521 GST_INPUT_SELECTOR_UNLOCK (sel);
523 /* unselected pad, perform fallback alloc or return unlinked when
525 GST_OBJECT_LOCK (selpad);
526 if (selpad->always_ok) {
527 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
529 result = GST_FLOW_OK;
531 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
532 result = GST_FLOW_NOT_LINKED;
534 GST_OBJECT_UNLOCK (selpad);
540 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
541 * or return TRUE when flushing */
543 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
545 while (self->blocked && !self->flushing) {
546 /* we can be unlocked here when we are shutting down (flushing) or when we
548 GST_INPUT_SELECTOR_WAIT (self);
550 return self->flushing;
554 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
556 GstInputSelector *sel;
558 GstPad *active_sinkpad;
559 GstPad *prev_active_sinkpad;
560 GstSelectorPad *selpad;
561 GstClockTime end_time, duration;
563 GstEvent *close_event = NULL, *start_event = NULL;
565 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
566 selpad = GST_SELECTOR_PAD_CAST (pad);
567 seg = &selpad->segment;
569 GST_INPUT_SELECTOR_LOCK (sel);
570 /* wait or check for flushing */
571 if (gst_input_selector_wait (sel, pad))
574 GST_DEBUG_OBJECT (pad, "getting active pad");
576 prev_active_sinkpad = sel->active_sinkpad;
577 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
579 /* update the segment on the srcpad */
580 end_time = GST_BUFFER_TIMESTAMP (buf);
581 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
582 duration = GST_BUFFER_DURATION (buf);
583 if (GST_CLOCK_TIME_IS_VALID (duration))
584 end_time += duration;
585 GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
586 GST_TIME_ARGS (end_time));
588 GST_OBJECT_LOCK (pad);
589 gst_segment_set_last_stop (seg, seg->format, end_time);
590 GST_OBJECT_UNLOCK (pad);
593 /* Ignore buffers from pads except the selected one */
594 if (pad != active_sinkpad)
597 if (G_UNLIKELY (sel->pending_close)) {
598 GstSegment *cseg = &sel->segment;
600 GST_DEBUG_OBJECT (sel,
601 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
603 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
604 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
605 cseg->start, cseg->stop, cseg->time);
607 /* create update segment */
608 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
609 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
611 sel->pending_close = FALSE;
613 /* if we have a pending segment, push it out now */
614 if (G_UNLIKELY (selpad->segment_pending)) {
615 GST_DEBUG_OBJECT (pad,
616 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
618 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
619 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
620 seg->start, seg->stop, seg->time);
622 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
623 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
625 selpad->segment_pending = FALSE;
627 GST_INPUT_SELECTOR_UNLOCK (sel);
629 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
630 g_object_notify (G_OBJECT (sel), "active-pad");
633 gst_pad_push_event (sel->srcpad, close_event);
636 gst_pad_push_event (sel->srcpad, start_event);
638 if (selpad->discont) {
639 buf = gst_buffer_make_metadata_writable (buf);
641 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
642 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
643 selpad->discont = FALSE;
647 GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
648 GST_DEBUG_PAD_NAME (pad));
650 res = gst_pad_push (sel->srcpad, buf);
653 gst_object_unref (sel);
656 /* dropped buffers */
659 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
660 /* when we drop a buffer, we're creating a discont on this pad */
661 selpad->discont = TRUE;
662 GST_INPUT_SELECTOR_UNLOCK (sel);
663 gst_buffer_unref (buf);
665 /* figure out what to return upstream */
666 GST_OBJECT_LOCK (selpad);
667 if (selpad->always_ok)
670 res = GST_FLOW_NOT_LINKED;
671 GST_OBJECT_UNLOCK (selpad);
677 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
678 GST_INPUT_SELECTOR_UNLOCK (sel);
679 gst_buffer_unref (buf);
680 res = GST_FLOW_WRONG_STATE;
685 static void gst_input_selector_init (GstInputSelector * sel);
686 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
687 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
689 static void gst_input_selector_dispose (GObject * object);
691 static void gst_input_selector_set_property (GObject * object,
692 guint prop_id, const GValue * value, GParamSpec * pspec);
693 static void gst_input_selector_get_property (GObject * object,
694 guint prop_id, GValue * value, GParamSpec * pspec);
696 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
697 GstPadTemplate * templ, const gchar * unused);
698 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
700 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
701 element, GstStateChange transition);
703 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
704 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
705 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
706 static gint64 gst_input_selector_block (GstInputSelector * self);
707 static void gst_input_selector_switch (GstInputSelector * self,
708 GstPad * pad, gint64 stop_time, gint64 start_time);
710 static GstElementClass *parent_class = NULL;
713 gst_input_selector_get_type (void)
715 static GType input_selector_type = 0;
717 if (!input_selector_type) {
718 static const GTypeInfo input_selector_info = {
719 sizeof (GstInputSelectorClass),
720 (GBaseInitFunc) gst_input_selector_base_init,
722 (GClassInitFunc) gst_input_selector_class_init,
725 sizeof (GstInputSelector),
727 (GInstanceInitFunc) gst_input_selector_init,
729 input_selector_type =
730 g_type_register_static (GST_TYPE_ELEMENT,
731 "GstInputSelector", &input_selector_info, 0);
732 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
733 "input-selector", 0, "An input stream selector element");
736 return input_selector_type;
740 gst_input_selector_base_init (GstInputSelectorClass * klass)
742 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
744 gst_element_class_set_details (element_class, &gst_input_selector_details);
745 gst_element_class_add_pad_template (element_class,
746 gst_static_pad_template_get (&gst_input_selector_sink_factory));
747 gst_element_class_add_pad_template (element_class,
748 gst_static_pad_template_get (&gst_input_selector_src_factory));
752 gst_input_selector_class_init (GstInputSelectorClass * klass)
754 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
755 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
757 parent_class = g_type_class_peek_parent (klass);
759 gobject_class->dispose = gst_input_selector_dispose;
761 gobject_class->set_property =
762 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
763 gobject_class->get_property =
764 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
766 g_object_class_install_property (gobject_class, PROP_N_PADS,
767 g_param_spec_uint ("n-pads", "Number of Pads",
768 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
770 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
771 g_param_spec_object ("active-pad", "Active pad",
772 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
774 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
775 g_param_spec_boolean ("select-all", "Select all mode",
776 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
779 * GstInputSelector::block:
780 * @inputselector: the #GstInputSelector
782 * Block all sink pads in preparation for a switch. Returns the stop time of
783 * the current switch segment, as a running time, or 0 if there is no current
784 * active pad or the current active pad never received data.
786 gst_input_selector_signals[SIGNAL_BLOCK] =
787 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
788 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
789 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
790 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
792 * GstInputSelector::switch:
793 * @inputselector: the #GstInputSelector
794 * @pad: the pad to switch to
795 * @stop_time: running time at which to close the previous segment, or -1
796 * to use the running time of the previously active sink pad
797 * @start_time: running time at which to start the new segment, or -1 to
798 * use the running time of the newly active sink pad
800 * Switch to a new feed. The segment opened by the previously active pad, if
801 * any, will be closed, and a new segment opened before data flows again.
803 * This signal must be emitted when the element has been blocked via the <link
804 * linkend="GstInputSelector-block">block</link> signal.
806 * If you have a stream with only one switch element, such as an audio-only
807 * stream, a stream switch should be performed by first emitting the block
808 * signal, and then emitting the switch signal with -1 for the stop and start
811 * The intention of the @stop_time and @start_time arguments is to allow
812 * multiple switch elements to switch and maintain stream synchronization.
813 * When switching a stream with multiple feeds, you will need as many switch
814 * elements as you have feeds. For example, a feed with audio and video will
815 * have one switch element between the audio feeds and one for video.
817 * A switch over multiple switch elements should be performed as follows:
818 * First, emit the <link linkend="GstInputSelector-block">block</link>
819 * signal, collecting the returned values. The maximum running time returned
820 * by block should then be used as the time at which to close the previous
823 * Then, query the running times of the new audio and video pads that you will
824 * switch to. Naturally, these pads are on separate switch elements. Take the
825 * minimum running time for those streams and use it for the time at which to
826 * open the new segment.
828 * If @pad is the same as the current active pad, the element will cancel any
829 * previous block without adjusting segments.
832 * the signal changed from accepting the pad name to the pad object.
837 gst_input_selector_signals[SIGNAL_SWITCH] =
838 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
839 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
840 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
841 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
843 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
844 gstelement_class->release_pad = gst_input_selector_release_pad;
845 gstelement_class->change_state = gst_input_selector_change_state;
847 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
848 /* note the underscore because switch is a keyword otherwise */
849 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
853 gst_input_selector_init (GstInputSelector * sel)
855 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
856 gst_pad_set_iterate_internal_links_function (sel->srcpad,
857 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
858 gst_pad_set_getcaps_function (sel->srcpad,
859 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
860 gst_pad_set_query_function (sel->srcpad,
861 GST_DEBUG_FUNCPTR (gst_input_selector_query));
862 gst_pad_set_event_function (sel->srcpad,
863 GST_DEBUG_FUNCPTR (gst_input_selector_event));
864 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
865 /* sinkpad management */
866 sel->active_sinkpad = NULL;
868 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
870 sel->lock = g_mutex_new ();
871 sel->cond = g_cond_new ();
872 sel->blocked = FALSE;
874 sel->select_all = FALSE;
878 gst_input_selector_dispose (GObject * object)
880 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
882 if (sel->active_sinkpad) {
883 gst_object_unref (sel->active_sinkpad);
884 sel->active_sinkpad = NULL;
887 g_mutex_free (sel->lock);
891 g_cond_free (sel->cond);
895 G_OBJECT_CLASS (parent_class)->dispose (object);
898 /* Solve the following equation for B.timestamp, and set that as the segment
900 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
903 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
905 return (running_time - segment->accum) * segment->abs_rate + segment->start;
909 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
911 segment->stop = gst_segment_get_timestamp (segment, running_time);
912 segment->last_stop = -1;
916 gst_segment_set_start (GstSegment * segment, gint64 running_time)
918 gint64 new_start, duration;
920 new_start = gst_segment_get_timestamp (segment, running_time);
922 /* this is the duration we skipped */
923 duration = new_start - segment->start;
924 /* add the duration to the accumulated segment time */
925 segment->accum += duration;
926 /* move position in the segment */
927 segment->time += duration;
928 segment->start += duration;
931 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
932 * active pad changed. */
934 gst_input_selector_set_active_pad (GstInputSelector * self,
935 GstPad * pad, gint64 stop_time, gint64 start_time)
937 GstSelectorPad *old, *new;
938 GstPad **active_pad_p;
940 if (pad == self->active_sinkpad)
943 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
944 new = GST_SELECTOR_PAD_CAST (pad);
946 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
947 GST_DEBUG_PAD_NAME (new));
949 if (stop_time == -1 && old) {
950 /* no stop time given, get the latest running_time on the active pad to
951 * close and open the new segment */
952 stop_time = start_time = gst_selector_pad_get_running_time (old);
953 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
957 if (old && old->active && !self->pending_close && stop_time >= 0) {
958 /* schedule a last_stop update if one isn't already scheduled, and a
959 segment has been pushed before. */
960 memcpy (&self->segment, &old->segment, sizeof (self->segment));
962 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
964 gst_segment_set_stop (&self->segment, stop_time);
965 self->pending_close = TRUE;
968 if (new && new->active && start_time >= 0) {
969 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
971 /* schedule a new segment push */
972 gst_segment_set_start (&new->segment, start_time);
973 new->segment_pending = TRUE;
976 active_pad_p = &self->active_sinkpad;
977 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
978 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
979 self->active_sinkpad);
985 gst_input_selector_set_property (GObject * object, guint prop_id,
986 const GValue * value, GParamSpec * pspec)
988 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
991 case PROP_ACTIVE_PAD:
995 pad = g_value_get_object (value);
997 GST_INPUT_SELECTOR_LOCK (sel);
998 gst_input_selector_set_active_pad (sel, pad,
999 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1000 GST_INPUT_SELECTOR_UNLOCK (sel);
1003 case PROP_SELECT_ALL:
1004 GST_INPUT_SELECTOR_LOCK (object);
1005 sel->select_all = g_value_get_boolean (value);
1006 GST_INPUT_SELECTOR_UNLOCK (object);
1009 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1015 gst_input_selector_get_property (GObject * object, guint prop_id,
1016 GValue * value, GParamSpec * pspec)
1018 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1022 GST_INPUT_SELECTOR_LOCK (object);
1023 g_value_set_uint (value, sel->n_pads);
1024 GST_INPUT_SELECTOR_UNLOCK (object);
1026 case PROP_ACTIVE_PAD:
1027 GST_INPUT_SELECTOR_LOCK (object);
1028 g_value_set_object (value, sel->active_sinkpad);
1029 GST_INPUT_SELECTOR_UNLOCK (object);
1031 case PROP_SELECT_ALL:
1032 GST_INPUT_SELECTOR_LOCK (object);
1033 g_value_set_boolean (value, sel->select_all);
1034 GST_INPUT_SELECTOR_UNLOCK (object);
1037 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1043 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1045 GstInputSelector *sel;
1046 GstPad *otherpad = NULL;
1048 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1050 GST_INPUT_SELECTOR_LOCK (sel);
1051 if (pad == sel->srcpad)
1052 otherpad = sel->active_sinkpad;
1053 else if (pad == sel->active_sinkpad || !strict)
1054 otherpad = sel->srcpad;
1056 gst_object_ref (otherpad);
1057 GST_INPUT_SELECTOR_UNLOCK (sel);
1059 gst_object_unref (sel);
1065 gst_input_selector_event (GstPad * pad, GstEvent * event)
1067 gboolean res = FALSE;
1070 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1073 res = gst_pad_push_event (otherpad, event);
1075 gst_object_unref (otherpad);
1077 gst_event_unref (event);
1081 /* query on the srcpad. We override this function because by default it will
1082 * only forward the query to one random sinkpad */
1084 gst_input_selector_query (GstPad * pad, GstQuery * query)
1086 gboolean res = TRUE;
1087 GstInputSelector *sel;
1090 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1092 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1094 switch (GST_QUERY_TYPE (query)) {
1095 case GST_QUERY_LATENCY:
1098 GstClockTime resmin, resmax;
1105 /* assume FALSE, we become TRUE if one query succeeds */
1108 /* perform the query on all sinkpads and combine the results. We take the
1109 * max of min and the min of max for the result latency. */
1110 GST_INPUT_SELECTOR_LOCK (sel);
1111 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1112 walk = g_list_next (walk)) {
1113 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1115 if (gst_pad_peer_query (sinkpad, query)) {
1116 GstClockTime min, max;
1119 /* one query succeeded, we succeed too */
1122 gst_query_parse_latency (query, &live, &min, &max);
1124 GST_DEBUG_OBJECT (sinkpad,
1125 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1126 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1133 else if (max < resmax)
1135 if (reslive == FALSE)
1140 GST_INPUT_SELECTOR_UNLOCK (sel);
1142 gst_query_set_latency (query, reslive, resmin, resmax);
1144 GST_DEBUG_OBJECT (sel,
1145 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1146 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1154 res = gst_pad_peer_query (otherpad, query);
1158 gst_object_unref (otherpad);
1159 gst_object_unref (sel);
1165 gst_input_selector_getcaps (GstPad * pad)
1171 parent = gst_object_get_parent (GST_OBJECT (pad));
1173 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1176 if (GST_INPUT_SELECTOR (parent)->select_all) {
1177 GST_DEBUG_OBJECT (parent,
1178 "Pad %s:%s not linked, returning merge of caps",
1179 GST_DEBUG_PAD_NAME (pad));
1180 caps = gst_pad_proxy_getcaps (pad);
1182 GST_DEBUG_OBJECT (parent,
1183 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1184 caps = gst_caps_new_any ();
1187 GST_DEBUG_OBJECT (parent,
1188 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1189 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1190 /* if the peer has caps, use those. If the pad is not linked, this function
1191 * returns NULL and we return ANY */
1192 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1193 caps = gst_caps_new_any ();
1194 gst_object_unref (otherpad);
1197 gst_object_unref (parent);
1201 /* check if the pad is the active sinkpad */
1203 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1205 GstSelectorPad *selpad;
1208 selpad = GST_SELECTOR_PAD_CAST (pad);
1210 GST_INPUT_SELECTOR_LOCK (sel);
1211 res = (pad == sel->active_sinkpad);
1212 GST_INPUT_SELECTOR_UNLOCK (sel);
1217 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1219 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1221 GstPad *active_sinkpad;
1222 GstSelectorPad *selpad;
1224 selpad = GST_SELECTOR_PAD_CAST (pad);
1226 selpad->active = TRUE;
1227 active_sinkpad = sel->active_sinkpad;
1228 if (active_sinkpad == NULL || sel->select_all) {
1229 /* first pad we get activity on becomes the activated pad by default, if we
1230 * select all, we also remember the last used pad. */
1231 if (sel->active_sinkpad)
1232 gst_object_unref (sel->active_sinkpad);
1233 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1234 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1237 return active_sinkpad;
1241 gst_input_selector_request_new_pad (GstElement * element,
1242 GstPadTemplate * templ, const gchar * unused)
1244 GstInputSelector *sel;
1246 GstPad *sinkpad = NULL;
1248 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1250 sel = GST_INPUT_SELECTOR (element);
1252 GST_INPUT_SELECTOR_LOCK (sel);
1254 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1255 name = g_strdup_printf ("sink%d", sel->padcount++);
1256 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1257 "name", name, "direction", templ->direction, "template", templ, NULL);
1262 gst_pad_set_event_function (sinkpad,
1263 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1264 gst_pad_set_getcaps_function (sinkpad,
1265 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1266 gst_pad_set_acceptcaps_function (sinkpad,
1267 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1268 gst_pad_set_chain_function (sinkpad,
1269 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1270 gst_pad_set_iterate_internal_links_function (sinkpad,
1271 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1272 gst_pad_set_bufferalloc_function (sinkpad,
1273 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1275 gst_pad_set_active (sinkpad, TRUE);
1276 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1277 GST_INPUT_SELECTOR_UNLOCK (sel);
1283 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1285 GstInputSelector *sel;
1287 sel = GST_INPUT_SELECTOR (element);
1288 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1290 GST_INPUT_SELECTOR_LOCK (sel);
1291 /* if the pad was the active pad, makes us select a new one */
1292 if (sel->active_sinkpad == pad) {
1293 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1294 gst_object_unref (sel->active_sinkpad);
1295 sel->active_sinkpad = NULL;
1299 gst_pad_set_active (pad, FALSE);
1300 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1301 GST_INPUT_SELECTOR_UNLOCK (sel);
1305 gst_input_selector_reset (GstInputSelector * sel)
1309 GST_INPUT_SELECTOR_LOCK (sel);
1310 /* clear active pad */
1311 if (sel->active_sinkpad) {
1312 gst_object_unref (sel->active_sinkpad);
1313 sel->active_sinkpad = NULL;
1316 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1317 sel->pending_close = FALSE;
1318 /* reset each of our sinkpads state */
1319 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1320 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1322 gst_selector_pad_reset (selpad);
1325 gst_tag_list_free (selpad->tags);
1326 selpad->tags = NULL;
1329 GST_INPUT_SELECTOR_UNLOCK (sel);
1332 static GstStateChangeReturn
1333 gst_input_selector_change_state (GstElement * element,
1334 GstStateChange transition)
1336 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1337 GstStateChangeReturn result;
1339 switch (transition) {
1340 case GST_STATE_CHANGE_READY_TO_PAUSED:
1341 GST_INPUT_SELECTOR_LOCK (self);
1342 self->blocked = FALSE;
1343 self->flushing = FALSE;
1344 GST_INPUT_SELECTOR_UNLOCK (self);
1346 case GST_STATE_CHANGE_PAUSED_TO_READY:
1347 /* first unlock before we call the parent state change function, which
1348 * tries to acquire the stream lock when going to ready. */
1349 GST_INPUT_SELECTOR_LOCK (self);
1350 self->blocked = FALSE;
1351 self->flushing = TRUE;
1352 GST_INPUT_SELECTOR_BROADCAST (self);
1353 GST_INPUT_SELECTOR_UNLOCK (self);
1359 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1361 switch (transition) {
1362 case GST_STATE_CHANGE_PAUSED_TO_READY:
1363 gst_input_selector_reset (self);
1373 gst_input_selector_block (GstInputSelector * self)
1376 GstSelectorPad *spad;
1378 GST_INPUT_SELECTOR_LOCK (self);
1381 GST_WARNING_OBJECT (self, "switch already blocked");
1383 self->blocked = TRUE;
1384 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1387 ret = gst_selector_pad_get_running_time (spad);
1389 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1391 GST_INPUT_SELECTOR_UNLOCK (self);
1396 /* stop_time and start_time are running times */
1398 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1399 gint64 stop_time, gint64 start_time)
1403 g_return_if_fail (self->blocked == TRUE);
1405 GST_INPUT_SELECTOR_LOCK (self);
1407 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1409 self->blocked = FALSE;
1410 GST_INPUT_SELECTOR_BROADCAST (self);
1411 GST_INPUT_SELECTOR_UNLOCK (self);
1414 g_object_notify (G_OBJECT (self), "active-pad");
1418 gst_input_selector_check_eos (GstElement * selector)
1420 GstIterator *it = gst_element_iterate_sink_pads (selector);
1421 GstIteratorResult ires;
1423 gboolean done = FALSE, is_eos = FALSE;
1424 GstSelectorPad *pad;
1427 ires = gst_iterator_next (it, &item);
1429 case GST_ITERATOR_DONE:
1430 GST_INFO_OBJECT (selector, "all sink pads have eos");
1434 case GST_ITERATOR_OK:
1435 pad = GST_SELECTOR_PAD_CAST (item);
1439 gst_object_unref (pad);
1441 case GST_ITERATOR_RESYNC:
1442 gst_iterator_resync (it);
1449 gst_iterator_free (it);