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 =
194 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
195 gobject_class->set_property =
196 GST_DEBUG_FUNCPTR (gst_selector_pad_set_property);
198 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
199 g_param_spec_int64 ("running-time", "Running time",
200 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
201 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
202 g_param_spec_boxed ("tags", "Tags",
203 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
205 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
206 g_param_spec_boolean ("active", "Active",
207 "If the pad is currently active", FALSE, G_PARAM_READABLE));
208 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
209 g_param_spec_boolean ("always-ok", "Always OK",
210 "Make an inactive pad return OK instead of NOT_LINKED",
211 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE));
215 gst_selector_pad_init (GstSelectorPad * pad)
217 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
218 gst_selector_pad_reset (pad);
222 gst_selector_pad_finalize (GObject * object)
226 pad = GST_SELECTOR_PAD_CAST (object);
229 gst_tag_list_free (pad->tags);
231 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
235 gst_selector_pad_set_property (GObject * object, guint prop_id,
236 const GValue * value, GParamSpec * pspec)
238 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
241 case PROP_PAD_ALWAYS_OK:
242 GST_OBJECT_LOCK (object);
243 spad->always_ok = g_value_get_boolean (value);
244 GST_OBJECT_UNLOCK (object);
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
253 gst_selector_pad_get_property (GObject * object, guint prop_id,
254 GValue * value, GParamSpec * pspec)
256 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
259 case PROP_PAD_RUNNING_TIME:
260 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
263 GST_OBJECT_LOCK (object);
264 g_value_set_boxed (value, spad->tags);
265 GST_OBJECT_UNLOCK (object);
267 case PROP_PAD_ACTIVE:
269 GstInputSelector *sel;
271 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
272 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
273 GST_PAD_CAST (spad)));
274 gst_object_unref (sel);
277 case PROP_PAD_ALWAYS_OK:
278 GST_OBJECT_LOCK (object);
279 g_value_set_boolean (value, spad->always_ok);
280 GST_OBJECT_UNLOCK (object);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gst_selector_pad_get_running_time (GstSelectorPad * pad)
293 GST_OBJECT_LOCK (pad);
295 gint64 last_stop = pad->segment.last_stop;
298 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
301 GST_OBJECT_UNLOCK (pad);
303 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
304 GST_TIME_ARGS (ret));
310 gst_selector_pad_reset (GstSelectorPad * pad)
312 GST_OBJECT_LOCK (pad);
315 pad->segment_pending = FALSE;
316 pad->discont = FALSE;
317 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
318 GST_OBJECT_UNLOCK (pad);
321 /* strictly get the linked pad from the sinkpad. If the pad is active we return
322 * the srcpad else we return NULL */
325 gst_selector_pad_iterate_linked_pads (GstPad * pad)
327 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
328 GstPad *opad = gst_input_selector_get_linked_pad (pad, TRUE);
329 GstIterator *it = gst_iterator_new_single (GST_TYPE_PAD, opad,
330 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
333 gst_object_unref (opad);
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 =
767 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
768 gobject_class->get_property =
769 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
771 g_object_class_install_property (gobject_class, PROP_N_PADS,
772 g_param_spec_uint ("n-pads", "Number of Pads",
773 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
775 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
776 g_param_spec_object ("active-pad", "Active pad",
777 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
779 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
780 g_param_spec_boolean ("select-all", "Select all mode",
781 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
784 * GstInputSelector::block:
785 * @inputselector: the #GstInputSelector
787 * Block all sink pads in preparation for a switch. Returns the stop time of
788 * the current switch segment, as a running time, or 0 if there is no current
789 * active pad or the current active pad never received data.
791 gst_input_selector_signals[SIGNAL_BLOCK] =
792 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
793 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
794 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
795 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
797 * GstInputSelector::switch:
798 * @inputselector: the #GstInputSelector
799 * @pad: the pad to switch to
800 * @stop_time: running time at which to close the previous segment, or -1
801 * to use the running time of the previously active sink pad
802 * @start_time: running time at which to start the new segment, or -1 to
803 * use the running time of the newly active sink pad
805 * Switch to a new feed. The segment opened by the previously active pad, if
806 * any, will be closed, and a new segment opened before data flows again.
808 * This signal must be emitted when the element has been blocked via the <link
809 * linkend="GstInputSelector-block">block</link> signal.
811 * If you have a stream with only one switch element, such as an audio-only
812 * stream, a stream switch should be performed by first emitting the block
813 * signal, and then emitting the switch signal with -1 for the stop and start
816 * The intention of the @stop_time and @start_time arguments is to allow
817 * multiple switch elements to switch and maintain stream synchronization.
818 * When switching a stream with multiple feeds, you will need as many switch
819 * elements as you have feeds. For example, a feed with audio and video will
820 * have one switch element between the audio feeds and one for video.
822 * A switch over multiple switch elements should be performed as follows:
823 * First, emit the <link linkend="GstInputSelector-block">block</link>
824 * signal, collecting the returned values. The maximum running time returned
825 * by block should then be used as the time at which to close the previous
828 * Then, query the running times of the new audio and video pads that you will
829 * switch to. Naturally, these pads are on separate switch elements. Take the
830 * minimum running time for those streams and use it for the time at which to
831 * open the new segment.
833 * If @pad is the same as the current active pad, the element will cancel any
834 * previous block without adjusting segments.
837 * the signal changed from accepting the pad name to the pad object.
842 gst_input_selector_signals[SIGNAL_SWITCH] =
843 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
844 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
845 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
846 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
848 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
849 gstelement_class->release_pad = gst_input_selector_release_pad;
850 gstelement_class->change_state = gst_input_selector_change_state;
852 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
853 /* note the underscore because switch is a keyword otherwise */
854 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
858 gst_input_selector_init (GstInputSelector * sel)
860 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
861 gst_pad_set_iterate_internal_links_function (sel->srcpad,
862 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
863 gst_pad_set_getcaps_function (sel->srcpad,
864 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
865 gst_pad_set_query_function (sel->srcpad,
866 GST_DEBUG_FUNCPTR (gst_input_selector_query));
867 gst_pad_set_event_function (sel->srcpad,
868 GST_DEBUG_FUNCPTR (gst_input_selector_event));
869 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
870 /* sinkpad management */
871 sel->active_sinkpad = NULL;
873 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
875 sel->lock = g_mutex_new ();
876 sel->cond = g_cond_new ();
877 sel->blocked = FALSE;
879 sel->select_all = FALSE;
883 gst_input_selector_dispose (GObject * object)
885 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
887 if (sel->active_sinkpad) {
888 gst_object_unref (sel->active_sinkpad);
889 sel->active_sinkpad = NULL;
892 g_mutex_free (sel->lock);
896 g_cond_free (sel->cond);
900 G_OBJECT_CLASS (parent_class)->dispose (object);
903 /* Solve the following equation for B.timestamp, and set that as the segment
905 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
908 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
910 return (running_time - segment->accum) * segment->abs_rate + segment->start;
914 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
916 segment->stop = gst_segment_get_timestamp (segment, running_time);
917 segment->last_stop = -1;
921 gst_segment_set_start (GstSegment * segment, gint64 running_time)
923 gint64 new_start, duration;
925 new_start = gst_segment_get_timestamp (segment, running_time);
927 /* this is the duration we skipped */
928 duration = new_start - segment->start;
929 /* add the duration to the accumulated segment time */
930 segment->accum += duration;
931 /* move position in the segment */
932 segment->time += duration;
933 segment->start += duration;
936 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
937 * active pad changed. */
939 gst_input_selector_set_active_pad (GstInputSelector * self,
940 GstPad * pad, gint64 stop_time, gint64 start_time)
942 GstSelectorPad *old, *new;
943 GstPad **active_pad_p;
945 if (pad == self->active_sinkpad)
948 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
949 new = GST_SELECTOR_PAD_CAST (pad);
951 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
952 GST_DEBUG_PAD_NAME (new));
954 if (stop_time == -1 && old) {
955 /* no stop time given, get the latest running_time on the active pad to
956 * close and open the new segment */
957 stop_time = start_time = gst_selector_pad_get_running_time (old);
958 GST_DEBUG_OBJECT (self, "using start/stop of %" G_GINT64_FORMAT,
962 if (old && old->active && !self->pending_close && stop_time >= 0) {
963 /* schedule a last_stop update if one isn't already scheduled, and a
964 segment has been pushed before. */
965 memcpy (&self->segment, &old->segment, sizeof (self->segment));
967 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
969 gst_segment_set_stop (&self->segment, stop_time);
970 self->pending_close = TRUE;
973 if (new && new->active && start_time >= 0) {
974 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
976 /* schedule a new segment push */
977 gst_segment_set_start (&new->segment, start_time);
978 new->segment_pending = TRUE;
981 active_pad_p = &self->active_sinkpad;
982 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
983 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
984 self->active_sinkpad);
990 gst_input_selector_set_property (GObject * object, guint prop_id,
991 const GValue * value, GParamSpec * pspec)
993 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
996 case PROP_ACTIVE_PAD:
1000 pad = g_value_get_object (value);
1002 GST_INPUT_SELECTOR_LOCK (sel);
1003 gst_input_selector_set_active_pad (sel, pad,
1004 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1005 GST_INPUT_SELECTOR_UNLOCK (sel);
1008 case PROP_SELECT_ALL:
1009 GST_INPUT_SELECTOR_LOCK (object);
1010 sel->select_all = g_value_get_boolean (value);
1011 GST_INPUT_SELECTOR_UNLOCK (object);
1014 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020 gst_input_selector_get_property (GObject * object, guint prop_id,
1021 GValue * value, GParamSpec * pspec)
1023 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1027 GST_INPUT_SELECTOR_LOCK (object);
1028 g_value_set_uint (value, sel->n_pads);
1029 GST_INPUT_SELECTOR_UNLOCK (object);
1031 case PROP_ACTIVE_PAD:
1032 GST_INPUT_SELECTOR_LOCK (object);
1033 g_value_set_object (value, sel->active_sinkpad);
1034 GST_INPUT_SELECTOR_UNLOCK (object);
1036 case PROP_SELECT_ALL:
1037 GST_INPUT_SELECTOR_LOCK (object);
1038 g_value_set_boolean (value, sel->select_all);
1039 GST_INPUT_SELECTOR_UNLOCK (object);
1042 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1048 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1050 GstInputSelector *sel;
1051 GstPad *otherpad = NULL;
1053 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1055 GST_INPUT_SELECTOR_LOCK (sel);
1056 if (pad == sel->srcpad)
1057 otherpad = sel->active_sinkpad;
1058 else if (pad == sel->active_sinkpad || !strict)
1059 otherpad = sel->srcpad;
1061 gst_object_ref (otherpad);
1062 GST_INPUT_SELECTOR_UNLOCK (sel);
1064 gst_object_unref (sel);
1070 gst_input_selector_event (GstPad * pad, GstEvent * event)
1072 gboolean res = FALSE;
1075 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1078 res = gst_pad_push_event (otherpad, event);
1080 gst_object_unref (otherpad);
1082 gst_event_unref (event);
1086 /* query on the srcpad. We override this function because by default it will
1087 * only forward the query to one random sinkpad */
1089 gst_input_selector_query (GstPad * pad, GstQuery * query)
1091 gboolean res = TRUE;
1092 GstInputSelector *sel;
1095 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1097 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1099 switch (GST_QUERY_TYPE (query)) {
1100 case GST_QUERY_LATENCY:
1103 GstClockTime resmin, resmax;
1110 /* assume FALSE, we become TRUE if one query succeeds */
1113 /* perform the query on all sinkpads and combine the results. We take the
1114 * max of min and the min of max for the result latency. */
1115 GST_INPUT_SELECTOR_LOCK (sel);
1116 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1117 walk = g_list_next (walk)) {
1118 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1120 if (gst_pad_peer_query (sinkpad, query)) {
1121 GstClockTime min, max;
1124 /* one query succeeded, we succeed too */
1127 gst_query_parse_latency (query, &live, &min, &max);
1129 GST_DEBUG_OBJECT (sinkpad,
1130 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1131 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1138 else if (max < resmax)
1140 if (reslive == FALSE)
1145 GST_INPUT_SELECTOR_UNLOCK (sel);
1147 gst_query_set_latency (query, reslive, resmin, resmax);
1149 GST_DEBUG_OBJECT (sel,
1150 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1151 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1159 res = gst_pad_peer_query (otherpad, query);
1163 gst_object_unref (otherpad);
1164 gst_object_unref (sel);
1170 gst_input_selector_getcaps (GstPad * pad)
1176 parent = gst_object_get_parent (GST_OBJECT (pad));
1178 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1181 if (GST_INPUT_SELECTOR (parent)->select_all) {
1182 GST_DEBUG_OBJECT (parent,
1183 "Pad %s:%s not linked, returning merge of caps",
1184 GST_DEBUG_PAD_NAME (pad));
1185 caps = gst_pad_proxy_getcaps (pad);
1187 GST_DEBUG_OBJECT (parent,
1188 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
1189 caps = gst_caps_new_any ();
1192 GST_DEBUG_OBJECT (parent,
1193 "Pad %s:%s is linked (to %s:%s), returning peer caps",
1194 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
1195 /* if the peer has caps, use those. If the pad is not linked, this function
1196 * returns NULL and we return ANY */
1197 if (!(caps = gst_pad_peer_get_caps (otherpad)))
1198 caps = gst_caps_new_any ();
1199 gst_object_unref (otherpad);
1202 gst_object_unref (parent);
1206 /* check if the pad is the active sinkpad */
1208 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1210 GstSelectorPad *selpad;
1213 selpad = GST_SELECTOR_PAD_CAST (pad);
1215 GST_INPUT_SELECTOR_LOCK (sel);
1216 res = (pad == sel->active_sinkpad);
1217 GST_INPUT_SELECTOR_UNLOCK (sel);
1222 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1224 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1226 GstPad *active_sinkpad;
1227 GstSelectorPad *selpad;
1229 selpad = GST_SELECTOR_PAD_CAST (pad);
1231 selpad->active = TRUE;
1232 active_sinkpad = sel->active_sinkpad;
1233 if (active_sinkpad == NULL || sel->select_all) {
1234 /* first pad we get activity on becomes the activated pad by default, if we
1235 * select all, we also remember the last used pad. */
1236 if (sel->active_sinkpad)
1237 gst_object_unref (sel->active_sinkpad);
1238 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1239 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1242 return active_sinkpad;
1246 gst_input_selector_request_new_pad (GstElement * element,
1247 GstPadTemplate * templ, const gchar * unused)
1249 GstInputSelector *sel;
1251 GstPad *sinkpad = NULL;
1253 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1255 sel = GST_INPUT_SELECTOR (element);
1257 GST_INPUT_SELECTOR_LOCK (sel);
1259 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1260 name = g_strdup_printf ("sink%d", sel->padcount++);
1261 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1262 "name", name, "direction", templ->direction, "template", templ, NULL);
1267 gst_pad_set_event_function (sinkpad,
1268 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1269 gst_pad_set_getcaps_function (sinkpad,
1270 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1271 gst_pad_set_acceptcaps_function (sinkpad,
1272 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1273 gst_pad_set_chain_function (sinkpad,
1274 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1275 gst_pad_set_iterate_internal_links_function (sinkpad,
1276 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1277 gst_pad_set_bufferalloc_function (sinkpad,
1278 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1280 gst_pad_set_active (sinkpad, TRUE);
1281 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1282 GST_INPUT_SELECTOR_UNLOCK (sel);
1288 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1290 GstInputSelector *sel;
1292 sel = GST_INPUT_SELECTOR (element);
1293 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1295 GST_INPUT_SELECTOR_LOCK (sel);
1296 /* if the pad was the active pad, makes us select a new one */
1297 if (sel->active_sinkpad == pad) {
1298 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1299 gst_object_unref (sel->active_sinkpad);
1300 sel->active_sinkpad = NULL;
1304 gst_pad_set_active (pad, FALSE);
1305 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1306 GST_INPUT_SELECTOR_UNLOCK (sel);
1310 gst_input_selector_reset (GstInputSelector * sel)
1314 GST_INPUT_SELECTOR_LOCK (sel);
1315 /* clear active pad */
1316 if (sel->active_sinkpad) {
1317 gst_object_unref (sel->active_sinkpad);
1318 sel->active_sinkpad = NULL;
1321 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1322 sel->pending_close = FALSE;
1323 /* reset each of our sinkpads state */
1324 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1325 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1327 gst_selector_pad_reset (selpad);
1330 gst_tag_list_free (selpad->tags);
1331 selpad->tags = NULL;
1334 GST_INPUT_SELECTOR_UNLOCK (sel);
1337 static GstStateChangeReturn
1338 gst_input_selector_change_state (GstElement * element,
1339 GstStateChange transition)
1341 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1342 GstStateChangeReturn result;
1344 switch (transition) {
1345 case GST_STATE_CHANGE_READY_TO_PAUSED:
1346 GST_INPUT_SELECTOR_LOCK (self);
1347 self->blocked = FALSE;
1348 self->flushing = FALSE;
1349 GST_INPUT_SELECTOR_UNLOCK (self);
1351 case GST_STATE_CHANGE_PAUSED_TO_READY:
1352 /* first unlock before we call the parent state change function, which
1353 * tries to acquire the stream lock when going to ready. */
1354 GST_INPUT_SELECTOR_LOCK (self);
1355 self->blocked = FALSE;
1356 self->flushing = TRUE;
1357 GST_INPUT_SELECTOR_BROADCAST (self);
1358 GST_INPUT_SELECTOR_UNLOCK (self);
1364 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1366 switch (transition) {
1367 case GST_STATE_CHANGE_PAUSED_TO_READY:
1368 gst_input_selector_reset (self);
1378 gst_input_selector_block (GstInputSelector * self)
1381 GstSelectorPad *spad;
1383 GST_INPUT_SELECTOR_LOCK (self);
1386 GST_WARNING_OBJECT (self, "switch already blocked");
1388 self->blocked = TRUE;
1389 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1392 ret = gst_selector_pad_get_running_time (spad);
1394 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1396 GST_INPUT_SELECTOR_UNLOCK (self);
1401 /* stop_time and start_time are running times */
1403 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1404 gint64 stop_time, gint64 start_time)
1408 g_return_if_fail (self->blocked == TRUE);
1410 GST_INPUT_SELECTOR_LOCK (self);
1412 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1414 self->blocked = FALSE;
1415 GST_INPUT_SELECTOR_BROADCAST (self);
1416 GST_INPUT_SELECTOR_UNLOCK (self);
1419 g_object_notify (G_OBJECT (self), "active-pad");
1423 gst_input_selector_check_eos (GstElement * selector)
1425 GstIterator *it = gst_element_iterate_sink_pads (selector);
1426 GstIteratorResult ires;
1428 gboolean done = FALSE, is_eos = FALSE;
1429 GstSelectorPad *pad;
1432 ires = gst_iterator_next (it, &item);
1434 case GST_ITERATOR_DONE:
1435 GST_INFO_OBJECT (selector, "all sink pads have eos");
1439 case GST_ITERATOR_OK:
1440 pad = GST_SELECTOR_PAD_CAST (item);
1444 gst_object_unref (pad);
1446 case GST_ITERATOR_RESYNC:
1447 gst_iterator_resync (it);
1454 gst_iterator_free (it);