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 "Jan Schmidt <thaytan@mad.scientist.com>\n"
50 "Wim Taymans <wim.taymans@gmail.com>");
52 static GstStaticPadTemplate gst_input_selector_sink_factory =
53 GST_STATIC_PAD_TEMPLATE ("sink%d",
58 static GstStaticPadTemplate gst_input_selector_src_factory =
59 GST_STATIC_PAD_TEMPLATE ("src",
73 #define DEFAULT_PAD_ALWAYS_OK TRUE
78 PROP_PAD_RUNNING_TIME,
92 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
94 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
96 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
98 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
100 static gboolean gst_input_selector_check_eos (GstElement * selector);
102 #define GST_TYPE_SELECTOR_PAD \
103 (gst_selector_pad_get_type())
104 #define GST_SELECTOR_PAD(obj) \
105 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
106 #define GST_SELECTOR_PAD_CLASS(klass) \
107 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
108 #define GST_IS_SELECTOR_PAD(obj) \
109 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
110 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
111 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
112 #define GST_SELECTOR_PAD_CAST(obj) \
113 ((GstSelectorPad *)(obj))
115 typedef struct _GstSelectorPad GstSelectorPad;
116 typedef struct _GstSelectorPadClass GstSelectorPadClass;
118 struct _GstSelectorPad
122 gboolean active; /* when buffer have passed the pad */
123 gboolean eos; /* when EOS has been received */
124 gboolean discont; /* after switching we create a discont */
126 GstSegment segment; /* the current segment on the pad */
127 GstTagList *tags; /* last tags received on the pad */
129 gboolean segment_pending;
132 struct _GstSelectorPadClass
137 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
138 static void gst_selector_pad_init (GstSelectorPad * pad);
139 static void gst_selector_pad_finalize (GObject * object);
140 static void gst_selector_pad_get_property (GObject * object,
141 guint prop_id, GValue * value, GParamSpec * pspec);
142 static void gst_selector_pad_set_property (GObject * object,
143 guint prop_id, const GValue * value, GParamSpec * pspec);
145 static GstPadClass *selector_pad_parent_class = NULL;
147 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
148 static void gst_selector_pad_reset (GstSelectorPad * pad);
149 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
150 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
151 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
152 static GstIterator *gst_selector_pad_iterate_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 = gst_selector_pad_get_property;
194 gobject_class->set_property = gst_selector_pad_set_property;
196 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
197 g_param_spec_int64 ("running-time", "Running time",
198 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
199 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
200 g_param_spec_boxed ("tags", "Tags",
201 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
203 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
204 g_param_spec_boolean ("active", "Active",
205 "If the pad is currently active", FALSE, G_PARAM_READABLE));
206 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
207 g_param_spec_boolean ("always-ok", "Always OK",
208 "Make an inactive pad return OK instead of NOT_LINKED",
209 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE));
213 gst_selector_pad_init (GstSelectorPad * pad)
215 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
216 gst_selector_pad_reset (pad);
220 gst_selector_pad_finalize (GObject * object)
224 pad = GST_SELECTOR_PAD_CAST (object);
227 gst_tag_list_free (pad->tags);
229 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
233 gst_selector_pad_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
236 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
239 case PROP_PAD_ALWAYS_OK:
240 GST_OBJECT_LOCK (object);
241 spad->always_ok = g_value_get_boolean (value);
242 GST_OBJECT_UNLOCK (object);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 gst_selector_pad_get_property (GObject * object, guint prop_id,
252 GValue * value, GParamSpec * pspec)
254 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
257 case PROP_PAD_RUNNING_TIME:
258 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
261 GST_OBJECT_LOCK (object);
262 g_value_set_boxed (value, spad->tags);
263 GST_OBJECT_UNLOCK (object);
265 case PROP_PAD_ACTIVE:
267 GstInputSelector *sel;
269 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
270 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
271 GST_PAD_CAST (spad)));
272 gst_object_unref (sel);
275 case PROP_PAD_ALWAYS_OK:
276 GST_OBJECT_LOCK (object);
277 g_value_set_boolean (value, spad->always_ok);
278 GST_OBJECT_UNLOCK (object);
281 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
287 gst_selector_pad_get_running_time (GstSelectorPad * pad)
291 GST_OBJECT_LOCK (pad);
293 gint64 last_stop = pad->segment.last_stop;
296 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
299 GST_OBJECT_UNLOCK (pad);
301 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
302 GST_TIME_ARGS (ret));
308 gst_selector_pad_reset (GstSelectorPad * pad)
310 GST_OBJECT_LOCK (pad);
313 pad->segment_pending = FALSE;
314 pad->discont = FALSE;
315 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
316 GST_OBJECT_UNLOCK (pad);
319 /* strictly get the linked pad from the sinkpad. If the pad is active we return
320 * the srcpad else we return NULL */
322 gst_selector_pad_iterate_linked_pads (GstPad * pad)
324 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
328 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
329 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
330 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
333 gst_object_unref (otherpad);
334 gst_object_unref (sel);
340 gst_selector_pad_event (GstPad * pad, GstEvent * event)
343 gboolean forward = TRUE;
344 GstInputSelector *sel;
345 GstSelectorPad *selpad;
346 GstPad *prev_active_sinkpad;
347 GstPad *active_sinkpad;
349 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
350 selpad = GST_SELECTOR_PAD_CAST (pad);
352 GST_INPUT_SELECTOR_LOCK (sel);
353 prev_active_sinkpad = sel->active_sinkpad;
354 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
356 /* only forward if we are dealing with the active sinkpad or if select_all
358 if (pad != active_sinkpad && !sel->select_all)
360 GST_INPUT_SELECTOR_UNLOCK (sel);
362 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
363 g_object_notify (G_OBJECT (sel), "active-pad");
365 switch (GST_EVENT_TYPE (event)) {
366 case GST_EVENT_FLUSH_START:
367 /* FIXME, flush out the waiter */
369 case GST_EVENT_FLUSH_STOP:
370 GST_INPUT_SELECTOR_LOCK (sel);
371 gst_selector_pad_reset (selpad);
372 sel->pending_close = FALSE;
373 GST_INPUT_SELECTOR_UNLOCK (sel);
375 case GST_EVENT_NEWSEGMENT:
380 gint64 start, stop, time;
382 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
383 &start, &stop, &time);
385 GST_DEBUG_OBJECT (pad,
386 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
388 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
389 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
391 GST_INPUT_SELECTOR_LOCK (sel);
392 GST_OBJECT_LOCK (selpad);
393 gst_segment_set_newsegment_full (&selpad->segment, update,
394 rate, arate, format, start, stop, time);
395 GST_OBJECT_UNLOCK (selpad);
397 /* If we aren't forwarding the event (because the pad is not the
398 * active_sinkpad, and select_all is not set, then set the flag on the
399 * that says a segment needs sending if/when that pad is activated.
400 * For all other cases, we send the event immediately, which makes
401 * sparse streams and other segment updates work correctly downstream.
404 selpad->segment_pending = TRUE;
406 GST_INPUT_SELECTOR_UNLOCK (sel);
411 GstTagList *tags, *oldtags, *newtags;
413 gst_event_parse_tag (event, &tags);
415 GST_OBJECT_LOCK (selpad);
416 oldtags = selpad->tags;
418 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
419 selpad->tags = newtags;
421 gst_tag_list_free (oldtags);
422 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
423 GST_OBJECT_UNLOCK (selpad);
425 g_object_notify (G_OBJECT (selpad), "tags");
430 GST_DEBUG_OBJECT (pad, "received EOS");
431 /* don't forward eos in select_all mode until all sink pads have eos */
432 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
440 GST_DEBUG_OBJECT (pad, "forwarding event");
441 res = gst_pad_push_event (sel->srcpad, event);
443 gst_event_unref (event);
445 gst_object_unref (sel);
451 gst_selector_pad_getcaps (GstPad * pad)
453 GstInputSelector *sel;
456 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
458 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
459 caps = gst_pad_peer_get_caps (sel->srcpad);
461 caps = gst_caps_new_any ();
463 gst_object_unref (sel);
469 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
471 GstInputSelector *sel;
474 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
476 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
477 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
478 gst_object_unref (sel);
484 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
485 guint size, GstCaps * caps, GstBuffer ** buf)
487 GstInputSelector *sel;
488 GstFlowReturn result;
489 GstPad *active_sinkpad;
490 GstPad *prev_active_sinkpad;
491 GstSelectorPad *selpad;
493 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
494 selpad = GST_SELECTOR_PAD_CAST (pad);
496 GST_DEBUG_OBJECT (pad, "received alloc");
498 GST_INPUT_SELECTOR_LOCK (sel);
499 prev_active_sinkpad = sel->active_sinkpad;
500 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
502 if (pad != active_sinkpad)
505 GST_INPUT_SELECTOR_UNLOCK (sel);
507 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad)
508 g_object_notify (G_OBJECT (sel), "active-pad");
510 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
513 gst_object_unref (sel);
520 GST_INPUT_SELECTOR_UNLOCK (sel);
522 /* unselected pad, perform fallback alloc or return unlinked when
524 GST_OBJECT_LOCK (selpad);
525 if (selpad->always_ok) {
526 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
528 result = GST_FLOW_OK;
530 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
531 result = GST_FLOW_NOT_LINKED;
533 GST_OBJECT_UNLOCK (selpad);
539 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
540 * or return TRUE when flushing */
542 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
544 while (self->blocked && !self->flushing) {
545 /* we can be unlocked here when we are shutting down (flushing) or when we
547 GST_INPUT_SELECTOR_WAIT (self);
549 return self->flushing;
553 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
555 GstInputSelector *sel;
557 GstPad *active_sinkpad;
558 GstPad *prev_active_sinkpad;
559 GstSelectorPad *selpad;
560 GstClockTime end_time, duration;
562 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 if ((caps = GST_BUFFER_CAPS (buf))) {
651 if (GST_PAD_CAPS (sel->srcpad) != caps)
652 gst_pad_set_caps (sel->srcpad, caps);
655 res = gst_pad_push (sel->srcpad, buf);
658 gst_object_unref (sel);
661 /* dropped buffers */
664 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
665 /* when we drop a buffer, we're creating a discont on this pad */
666 selpad->discont = TRUE;
667 GST_INPUT_SELECTOR_UNLOCK (sel);
668 gst_buffer_unref (buf);
670 /* figure out what to return upstream */
671 GST_OBJECT_LOCK (selpad);
672 if (selpad->always_ok)
675 res = GST_FLOW_NOT_LINKED;
676 GST_OBJECT_UNLOCK (selpad);
682 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
683 GST_INPUT_SELECTOR_UNLOCK (sel);
684 gst_buffer_unref (buf);
685 res = GST_FLOW_WRONG_STATE;
690 static void gst_input_selector_init (GstInputSelector * sel);
691 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
692 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
694 static void gst_input_selector_dispose (GObject * object);
696 static void gst_input_selector_set_property (GObject * object,
697 guint prop_id, const GValue * value, GParamSpec * pspec);
698 static void gst_input_selector_get_property (GObject * object,
699 guint prop_id, GValue * value, GParamSpec * pspec);
701 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
702 GstPadTemplate * templ, const gchar * unused);
703 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
705 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
706 element, GstStateChange transition);
708 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
709 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
710 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
711 static gint64 gst_input_selector_block (GstInputSelector * self);
712 static void gst_input_selector_switch (GstInputSelector * self,
713 GstPad * pad, gint64 stop_time, gint64 start_time);
715 static GstElementClass *parent_class = NULL;
718 gst_input_selector_get_type (void)
720 static GType input_selector_type = 0;
722 if (!input_selector_type) {
723 static const GTypeInfo input_selector_info = {
724 sizeof (GstInputSelectorClass),
725 (GBaseInitFunc) gst_input_selector_base_init,
727 (GClassInitFunc) gst_input_selector_class_init,
730 sizeof (GstInputSelector),
732 (GInstanceInitFunc) gst_input_selector_init,
734 input_selector_type =
735 g_type_register_static (GST_TYPE_ELEMENT,
736 "GstInputSelector", &input_selector_info, 0);
737 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
738 "input-selector", 0, "An input stream selector element");
741 return input_selector_type;
745 gst_input_selector_base_init (GstInputSelectorClass * klass)
747 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
749 gst_element_class_set_details (element_class, &gst_input_selector_details);
750 gst_element_class_add_pad_template (element_class,
751 gst_static_pad_template_get (&gst_input_selector_sink_factory));
752 gst_element_class_add_pad_template (element_class,
753 gst_static_pad_template_get (&gst_input_selector_src_factory));
757 gst_input_selector_class_init (GstInputSelectorClass * klass)
759 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
760 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
762 parent_class = g_type_class_peek_parent (klass);
764 gobject_class->dispose = gst_input_selector_dispose;
766 gobject_class->set_property = gst_input_selector_set_property;
767 gobject_class->get_property = gst_input_selector_get_property;
769 g_object_class_install_property (gobject_class, PROP_N_PADS,
770 g_param_spec_uint ("n-pads", "Number of Pads",
771 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
773 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
774 g_param_spec_object ("active-pad", "Active pad",
775 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
777 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
778 g_param_spec_boolean ("select-all", "Select all mode",
779 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
782 * GstInputSelector::block:
783 * @inputselector: the #GstInputSelector
785 * Block all sink pads in preparation for a switch. Returns the stop time of
786 * the current switch segment, as a running time, or 0 if there is no current
787 * active pad or the current active pad never received data.
789 gst_input_selector_signals[SIGNAL_BLOCK] =
790 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
791 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
792 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
793 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
795 * GstInputSelector::switch:
796 * @inputselector: the #GstInputSelector
797 * @pad: the pad to switch to
798 * @stop_time: running time at which to close the previous segment, or -1
799 * to use the running time of the previously active sink pad
800 * @start_time: running time at which to start the new segment, or -1 to
801 * use the running time of the newly active sink pad
803 * Switch to a new feed. The segment opened by the previously active pad, if
804 * any, will be closed, and a new segment opened before data flows again.
806 * This signal must be emitted when the element has been blocked via the <link
807 * linkend="GstInputSelector-block">block</link> signal.
809 * If you have a stream with only one switch element, such as an audio-only
810 * stream, a stream switch should be performed by first emitting the block
811 * signal, and then emitting the switch signal with -1 for the stop and start
814 * The intention of the @stop_time and @start_time arguments is to allow
815 * multiple switch elements to switch and maintain stream synchronization.
816 * When switching a stream with multiple feeds, you will need as many switch
817 * elements as you have feeds. For example, a feed with audio and video will
818 * have one switch element between the audio feeds and one for video.
820 * A switch over multiple switch elements should be performed as follows:
821 * First, emit the <link linkend="GstInputSelector-block">block</link>
822 * signal, collecting the returned values. The maximum running time returned
823 * by block should then be used as the time at which to close the previous
826 * Then, query the running times of the new audio and video pads that you will
827 * switch to. Naturally, these pads are on separate switch elements. Take the
828 * minimum running time for those streams and use it for the time at which to
829 * open the new segment.
831 * If @pad is the same as the current active pad, the element will cancel any
832 * previous block without adjusting segments.
835 * the signal changed from accepting the pad name to the pad object.
840 gst_input_selector_signals[SIGNAL_SWITCH] =
841 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
842 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
843 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
844 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
846 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
847 gstelement_class->release_pad = gst_input_selector_release_pad;
848 gstelement_class->change_state = gst_input_selector_change_state;
850 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
851 /* note the underscore because switch is a keyword otherwise */
852 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
856 gst_input_selector_init (GstInputSelector * sel)
858 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
859 gst_pad_set_iterate_internal_links_function (sel->srcpad,
860 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
861 gst_pad_set_getcaps_function (sel->srcpad,
862 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
863 gst_pad_set_query_function (sel->srcpad,
864 GST_DEBUG_FUNCPTR (gst_input_selector_query));
865 gst_pad_set_event_function (sel->srcpad,
866 GST_DEBUG_FUNCPTR (gst_input_selector_event));
867 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
868 /* sinkpad management */
869 sel->active_sinkpad = NULL;
871 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
873 sel->lock = g_mutex_new ();
874 sel->cond = g_cond_new ();
875 sel->blocked = FALSE;
877 sel->select_all = FALSE;
881 gst_input_selector_dispose (GObject * object)
883 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
885 if (sel->active_sinkpad) {
886 gst_object_unref (sel->active_sinkpad);
887 sel->active_sinkpad = NULL;
890 g_mutex_free (sel->lock);
894 g_cond_free (sel->cond);
898 G_OBJECT_CLASS (parent_class)->dispose (object);
901 /* Solve the following equation for B.timestamp, and set that as the segment
903 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
906 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
908 return (running_time - segment->accum) * segment->abs_rate + segment->start;
912 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
914 segment->stop = gst_segment_get_timestamp (segment, running_time);
915 segment->last_stop = -1;
919 gst_segment_set_start (GstSegment * segment, gint64 running_time)
921 gint64 new_start, duration;
923 new_start = gst_segment_get_timestamp (segment, running_time);
925 /* this is the duration we skipped */
926 duration = new_start - segment->start;
927 /* add the duration to the accumulated segment time */
928 segment->accum += duration;
929 /* move position in the segment */
930 segment->time += duration;
931 segment->start += duration;
934 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
935 * active pad changed. */
937 gst_input_selector_set_active_pad (GstInputSelector * self,
938 GstPad * pad, gint64 stop_time, gint64 start_time)
940 GstSelectorPad *old, *new;
941 GstPad **active_pad_p;
943 if (pad == self->active_sinkpad)
946 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
947 new = GST_SELECTOR_PAD_CAST (pad);
949 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
950 GST_DEBUG_PAD_NAME (new));
952 if (stop_time == -1 && old) {
953 /* no stop time given, get the latest running_time on the active pad to
954 * close and open the new segment */
955 stop_time = start_time = gst_selector_pad_get_running_time (old);
956 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
960 if (old && old->active && !self->pending_close && stop_time >= 0) {
961 /* schedule a last_stop update if one isn't already scheduled, and a
962 segment has been pushed before. */
963 memcpy (&self->segment, &old->segment, sizeof (self->segment));
965 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
967 gst_segment_set_stop (&self->segment, stop_time);
968 self->pending_close = TRUE;
971 if (new && new->active && start_time >= 0) {
972 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
974 /* schedule a new segment push */
975 gst_segment_set_start (&new->segment, start_time);
976 new->segment_pending = TRUE;
979 active_pad_p = &self->active_sinkpad;
980 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
981 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
982 self->active_sinkpad);
988 gst_input_selector_set_property (GObject * object, guint prop_id,
989 const GValue * value, GParamSpec * pspec)
991 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
994 case PROP_ACTIVE_PAD:
998 pad = g_value_get_object (value);
1000 GST_INPUT_SELECTOR_LOCK (sel);
1001 gst_input_selector_set_active_pad (sel, pad,
1002 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1003 GST_INPUT_SELECTOR_UNLOCK (sel);
1006 case PROP_SELECT_ALL:
1007 GST_INPUT_SELECTOR_LOCK (object);
1008 sel->select_all = g_value_get_boolean (value);
1009 GST_INPUT_SELECTOR_UNLOCK (object);
1012 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1018 gst_input_selector_get_property (GObject * object, guint prop_id,
1019 GValue * value, GParamSpec * pspec)
1021 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1025 GST_INPUT_SELECTOR_LOCK (object);
1026 g_value_set_uint (value, sel->n_pads);
1027 GST_INPUT_SELECTOR_UNLOCK (object);
1029 case PROP_ACTIVE_PAD:
1030 GST_INPUT_SELECTOR_LOCK (object);
1031 g_value_set_object (value, sel->active_sinkpad);
1032 GST_INPUT_SELECTOR_UNLOCK (object);
1034 case PROP_SELECT_ALL:
1035 GST_INPUT_SELECTOR_LOCK (object);
1036 g_value_set_boolean (value, sel->select_all);
1037 GST_INPUT_SELECTOR_UNLOCK (object);
1040 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1046 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1048 GstInputSelector *sel;
1049 GstPad *otherpad = NULL;
1051 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1053 GST_INPUT_SELECTOR_LOCK (sel);
1054 if (pad == sel->srcpad)
1055 otherpad = sel->active_sinkpad;
1056 else if (pad == sel->active_sinkpad || !strict)
1057 otherpad = sel->srcpad;
1059 gst_object_ref (otherpad);
1060 GST_INPUT_SELECTOR_UNLOCK (sel);
1062 gst_object_unref (sel);
1068 gst_input_selector_event (GstPad * pad, GstEvent * event)
1070 gboolean res = FALSE;
1073 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1076 res = gst_pad_push_event (otherpad, event);
1078 gst_object_unref (otherpad);
1080 gst_event_unref (event);
1084 /* query on the srcpad. We override this function because by default it will
1085 * only forward the query to one random sinkpad */
1087 gst_input_selector_query (GstPad * pad, GstQuery * query)
1089 gboolean res = TRUE;
1090 GstInputSelector *sel;
1093 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1095 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1097 switch (GST_QUERY_TYPE (query)) {
1098 case GST_QUERY_LATENCY:
1101 GstClockTime resmin, resmax;
1108 /* assume FALSE, we become TRUE if one query succeeds */
1111 /* perform the query on all sinkpads and combine the results. We take the
1112 * max of min and the min of max for the result latency. */
1113 GST_INPUT_SELECTOR_LOCK (sel);
1114 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1115 walk = g_list_next (walk)) {
1116 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1118 if (gst_pad_peer_query (sinkpad, query)) {
1119 GstClockTime min, max;
1122 /* one query succeeded, we succeed too */
1125 gst_query_parse_latency (query, &live, &min, &max);
1127 GST_DEBUG_OBJECT (sinkpad,
1128 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1129 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1136 else if (max < resmax)
1138 if (reslive == FALSE)
1143 GST_INPUT_SELECTOR_UNLOCK (sel);
1145 gst_query_set_latency (query, reslive, resmin, resmax);
1147 GST_DEBUG_OBJECT (sel,
1148 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1149 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1157 res = gst_pad_peer_query (otherpad, query);
1161 gst_object_unref (otherpad);
1162 gst_object_unref (sel);
1168 gst_input_selector_getcaps (GstPad * pad)
1174 parent = gst_object_get_parent (GST_OBJECT (pad));
1176 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1179 if (GST_INPUT_SELECTOR (parent)->select_all) {
1180 GST_DEBUG_OBJECT (parent,
1181 "Pad %s:%s not linked, returning merge of caps",
1182 GST_DEBUG_PAD_NAME (pad));
1183 caps = gst_pad_proxy_getcaps (pad);
1185 GST_DEBUG_OBJECT (parent,
1186 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1187 caps = gst_caps_new_any ();
1190 GST_DEBUG_OBJECT (parent,
1191 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1192 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1193 /* if the peer has caps, use those. If the pad is not linked, this function
1194 * returns NULL and we return ANY */
1195 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1196 caps = gst_caps_new_any ();
1197 gst_object_unref (otherpad);
1200 gst_object_unref (parent);
1204 /* check if the pad is the active sinkpad */
1206 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1208 GstSelectorPad *selpad;
1211 selpad = GST_SELECTOR_PAD_CAST (pad);
1213 GST_INPUT_SELECTOR_LOCK (sel);
1214 res = (pad == sel->active_sinkpad);
1215 GST_INPUT_SELECTOR_UNLOCK (sel);
1220 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1222 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1224 GstPad *active_sinkpad;
1225 GstSelectorPad *selpad;
1227 selpad = GST_SELECTOR_PAD_CAST (pad);
1229 selpad->active = TRUE;
1230 active_sinkpad = sel->active_sinkpad;
1231 if (active_sinkpad == NULL || sel->select_all) {
1232 /* first pad we get activity on becomes the activated pad by default, if we
1233 * select all, we also remember the last used pad. */
1234 if (sel->active_sinkpad)
1235 gst_object_unref (sel->active_sinkpad);
1236 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1237 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1240 return active_sinkpad;
1244 gst_input_selector_request_new_pad (GstElement * element,
1245 GstPadTemplate * templ, const gchar * unused)
1247 GstInputSelector *sel;
1249 GstPad *sinkpad = NULL;
1251 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1253 sel = GST_INPUT_SELECTOR (element);
1255 GST_INPUT_SELECTOR_LOCK (sel);
1257 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1258 name = g_strdup_printf ("sink%d", sel->padcount++);
1259 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1260 "name", name, "direction", templ->direction, "template", templ, NULL);
1265 gst_pad_set_event_function (sinkpad,
1266 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1267 gst_pad_set_getcaps_function (sinkpad,
1268 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1269 gst_pad_set_acceptcaps_function (sinkpad,
1270 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1271 gst_pad_set_chain_function (sinkpad,
1272 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1273 gst_pad_set_iterate_internal_links_function (sinkpad,
1274 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1275 gst_pad_set_bufferalloc_function (sinkpad,
1276 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1278 gst_pad_set_active (sinkpad, TRUE);
1279 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1280 GST_INPUT_SELECTOR_UNLOCK (sel);
1286 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1288 GstInputSelector *sel;
1290 sel = GST_INPUT_SELECTOR (element);
1291 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1293 GST_INPUT_SELECTOR_LOCK (sel);
1294 /* if the pad was the active pad, makes us select a new one */
1295 if (sel->active_sinkpad == pad) {
1296 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1297 gst_object_unref (sel->active_sinkpad);
1298 sel->active_sinkpad = NULL;
1302 gst_pad_set_active (pad, FALSE);
1303 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1304 GST_INPUT_SELECTOR_UNLOCK (sel);
1308 gst_input_selector_reset (GstInputSelector * sel)
1312 GST_INPUT_SELECTOR_LOCK (sel);
1313 /* clear active pad */
1314 if (sel->active_sinkpad) {
1315 gst_object_unref (sel->active_sinkpad);
1316 sel->active_sinkpad = NULL;
1319 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1320 sel->pending_close = FALSE;
1321 /* reset each of our sinkpads state */
1322 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1323 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1325 gst_selector_pad_reset (selpad);
1328 gst_tag_list_free (selpad->tags);
1329 selpad->tags = NULL;
1332 GST_INPUT_SELECTOR_UNLOCK (sel);
1335 static GstStateChangeReturn
1336 gst_input_selector_change_state (GstElement * element,
1337 GstStateChange transition)
1339 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1340 GstStateChangeReturn result;
1342 switch (transition) {
1343 case GST_STATE_CHANGE_READY_TO_PAUSED:
1344 GST_INPUT_SELECTOR_LOCK (self);
1345 self->blocked = FALSE;
1346 self->flushing = FALSE;
1347 GST_INPUT_SELECTOR_UNLOCK (self);
1349 case GST_STATE_CHANGE_PAUSED_TO_READY:
1350 /* first unlock before we call the parent state change function, which
1351 * tries to acquire the stream lock when going to ready. */
1352 GST_INPUT_SELECTOR_LOCK (self);
1353 self->blocked = FALSE;
1354 self->flushing = TRUE;
1355 GST_INPUT_SELECTOR_BROADCAST (self);
1356 GST_INPUT_SELECTOR_UNLOCK (self);
1362 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1364 switch (transition) {
1365 case GST_STATE_CHANGE_PAUSED_TO_READY:
1366 gst_input_selector_reset (self);
1376 gst_input_selector_block (GstInputSelector * self)
1379 GstSelectorPad *spad;
1381 GST_INPUT_SELECTOR_LOCK (self);
1384 GST_WARNING_OBJECT (self, "switch already blocked");
1386 self->blocked = TRUE;
1387 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1390 ret = gst_selector_pad_get_running_time (spad);
1392 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1394 GST_INPUT_SELECTOR_UNLOCK (self);
1399 /* stop_time and start_time are running times */
1401 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1402 gint64 stop_time, gint64 start_time)
1406 g_return_if_fail (self->blocked == TRUE);
1408 GST_INPUT_SELECTOR_LOCK (self);
1410 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1412 self->blocked = FALSE;
1413 GST_INPUT_SELECTOR_BROADCAST (self);
1414 GST_INPUT_SELECTOR_UNLOCK (self);
1417 g_object_notify (G_OBJECT (self), "active-pad");
1421 gst_input_selector_check_eos (GstElement * selector)
1423 GstIterator *it = gst_element_iterate_sink_pads (selector);
1424 GstIteratorResult ires;
1426 gboolean done = FALSE, is_eos = FALSE;
1427 GstSelectorPad *pad;
1430 ires = gst_iterator_next (it, &item);
1432 case GST_ITERATOR_DONE:
1433 GST_INFO_OBJECT (selector, "all sink pads have eos");
1437 case GST_ITERATOR_OK:
1438 pad = GST_SELECTOR_PAD_CAST (item);
1442 gst_object_unref (pad);
1444 case GST_ITERATOR_RESYNC:
1445 gst_iterator_resync (it);
1452 gst_iterator_free (it);