1 /* GStreamer input selector
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.
40 #include "gstinputselector.h"
42 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
43 #define GST_CAT_DEFAULT input_selector_debug
45 #if GLIB_CHECK_VERSION(2, 26, 0)
46 #define NOTIFY_MUTEX_LOCK()
47 #define NOTIFY_MUTEX_UNLOCK()
49 static GStaticRecMutex notify_mutex = G_STATIC_REC_MUTEX_INIT;
50 #define NOTIFY_MUTEX_LOCK() g_static_rec_mutex_lock (¬ify_mutex)
51 #define NOTIFY_MUTEX_UNLOCK() g_static_rec_mutex_unlock (¬ify_mutex)
54 static GstStaticPadTemplate gst_input_selector_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink%d",
60 static GstStaticPadTemplate gst_input_selector_src_factory =
61 GST_STATIC_PAD_TEMPLATE ("src",
73 #define DEFAULT_PAD_ALWAYS_OK TRUE
78 PROP_PAD_RUNNING_TIME,
91 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
93 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
95 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
97 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
100 #define GST_TYPE_SELECTOR_PAD \
101 (gst_selector_pad_get_type())
102 #define GST_SELECTOR_PAD(obj) \
103 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
104 #define GST_SELECTOR_PAD_CLASS(klass) \
105 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
106 #define GST_IS_SELECTOR_PAD(obj) \
107 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
108 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
109 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
110 #define GST_SELECTOR_PAD_CAST(obj) \
111 ((GstSelectorPad *)(obj))
113 typedef struct _GstSelectorPad GstSelectorPad;
114 typedef struct _GstSelectorPadClass GstSelectorPadClass;
116 struct _GstSelectorPad
120 gboolean active; /* when buffer have passed the pad */
121 gboolean eos; /* when EOS has been received */
122 gboolean discont; /* after switching we create a discont */
124 GstSegment segment; /* the current segment on the pad */
125 GstTagList *tags; /* last tags received on the pad */
127 gboolean segment_pending;
130 struct _GstSelectorPadClass
135 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
136 static void gst_selector_pad_init (GstSelectorPad * pad);
137 static void gst_selector_pad_finalize (GObject * object);
138 static void gst_selector_pad_get_property (GObject * object,
139 guint prop_id, GValue * value, GParamSpec * pspec);
140 static void gst_selector_pad_set_property (GObject * object,
141 guint prop_id, const GValue * value, GParamSpec * pspec);
143 static GstPadClass *selector_pad_parent_class = NULL;
145 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
146 static void gst_selector_pad_reset (GstSelectorPad * pad);
147 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
148 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
149 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
150 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad);
151 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
152 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
153 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
156 gst_selector_pad_get_type (void)
158 static volatile gsize selector_pad_type = 0;
159 static const GTypeInfo selector_pad_info = {
160 sizeof (GstSelectorPadClass),
163 (GClassInitFunc) gst_selector_pad_class_init,
166 sizeof (GstSelectorPad),
168 (GInstanceInitFunc) gst_selector_pad_init,
171 if (g_once_init_enter (&selector_pad_type)) {
172 GType tmp = g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
173 &selector_pad_info, 0);
174 g_once_init_leave (&selector_pad_type, tmp);
177 return (GType) selector_pad_type;
181 gst_selector_pad_class_init (GstSelectorPadClass * klass)
183 GObjectClass *gobject_class;
185 gobject_class = (GObjectClass *) klass;
187 selector_pad_parent_class = g_type_class_peek_parent (klass);
189 gobject_class->finalize = gst_selector_pad_finalize;
191 gobject_class->get_property = gst_selector_pad_get_property;
192 gobject_class->set_property = gst_selector_pad_set_property;
194 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
195 g_param_spec_int64 ("running-time", "Running time",
196 "Running time of stream on pad", 0, G_MAXINT64, 0,
197 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
199 g_param_spec_boxed ("tags", "Tags",
200 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
201 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
203 g_param_spec_boolean ("active", "Active",
204 "If the pad is currently active", FALSE,
205 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
206 /* FIXME: better property name? */
207 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
208 g_param_spec_boolean ("always-ok", "Always OK",
209 "Make an inactive pad return OK instead of NOT_LINKED",
210 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 gst_selector_pad_init (GstSelectorPad * pad)
216 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
217 gst_selector_pad_reset (pad);
221 gst_selector_pad_finalize (GObject * object)
225 pad = GST_SELECTOR_PAD_CAST (object);
228 gst_tag_list_free (pad->tags);
230 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
234 gst_selector_pad_set_property (GObject * object, guint prop_id,
235 const GValue * value, GParamSpec * pspec)
237 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
240 case PROP_PAD_ALWAYS_OK:
241 GST_OBJECT_LOCK (object);
242 spad->always_ok = g_value_get_boolean (value);
243 GST_OBJECT_UNLOCK (object);
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 gst_selector_pad_get_property (GObject * object, guint prop_id,
253 GValue * value, GParamSpec * pspec)
255 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
258 case PROP_PAD_RUNNING_TIME:
259 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
262 GST_OBJECT_LOCK (object);
263 g_value_set_boxed (value, spad->tags);
264 GST_OBJECT_UNLOCK (object);
266 case PROP_PAD_ACTIVE:
268 GstInputSelector *sel;
270 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
271 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
272 GST_PAD_CAST (spad)));
273 gst_object_unref (sel);
276 case PROP_PAD_ALWAYS_OK:
277 GST_OBJECT_LOCK (object);
278 g_value_set_boolean (value, spad->always_ok);
279 GST_OBJECT_UNLOCK (object);
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 gst_selector_pad_get_running_time (GstSelectorPad * pad)
292 GST_OBJECT_LOCK (pad);
294 gint64 last_stop = pad->segment.last_stop;
297 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
300 GST_OBJECT_UNLOCK (pad);
302 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
303 GST_TIME_ARGS (ret));
309 gst_selector_pad_reset (GstSelectorPad * pad)
311 GST_OBJECT_LOCK (pad);
314 pad->segment_pending = FALSE;
315 pad->discont = FALSE;
316 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
317 GST_OBJECT_UNLOCK (pad);
320 /* strictly get the linked pad from the sinkpad. If the pad is active we return
321 * the srcpad else we return NULL */
323 gst_selector_pad_iterate_linked_pads (GstPad * pad)
325 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
329 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
330 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
331 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
334 gst_object_unref (otherpad);
335 gst_object_unref (sel);
341 gst_selector_pad_event (GstPad * pad, GstEvent * event)
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 */
358 forward = (pad == active_sinkpad);
359 GST_INPUT_SELECTOR_UNLOCK (sel);
361 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
362 NOTIFY_MUTEX_LOCK ();
363 g_object_notify (G_OBJECT (sel), "active-pad");
364 NOTIFY_MUTEX_UNLOCK ();
367 switch (GST_EVENT_TYPE (event)) {
368 case GST_EVENT_FLUSH_START:
369 /* FIXME, flush out the waiter */
371 case GST_EVENT_FLUSH_STOP:
372 GST_INPUT_SELECTOR_LOCK (sel);
373 gst_selector_pad_reset (selpad);
374 sel->pending_close = FALSE;
375 GST_INPUT_SELECTOR_UNLOCK (sel);
377 case GST_EVENT_NEWSEGMENT:
382 gint64 start, stop, time;
384 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
385 &start, &stop, &time);
387 GST_DEBUG_OBJECT (pad,
388 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
390 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
391 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
393 GST_INPUT_SELECTOR_LOCK (sel);
394 GST_OBJECT_LOCK (selpad);
395 gst_segment_set_newsegment_full (&selpad->segment, update,
396 rate, arate, format, start, stop, time);
397 GST_OBJECT_UNLOCK (selpad);
399 /* If we aren't forwarding the event because the pad is not the
400 * active_sinkpad, then set the flag on the pad
401 * that says a segment needs sending if/when that pad is activated.
402 * For all other cases, we send the event immediately, which makes
403 * sparse streams and other segment updates work correctly downstream.
406 selpad->segment_pending = TRUE;
408 GST_INPUT_SELECTOR_UNLOCK (sel);
413 GstTagList *tags, *oldtags, *newtags;
415 gst_event_parse_tag (event, &tags);
417 GST_OBJECT_LOCK (selpad);
418 oldtags = selpad->tags;
420 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
421 selpad->tags = newtags;
423 gst_tag_list_free (oldtags);
424 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
425 GST_OBJECT_UNLOCK (selpad);
427 g_object_notify (G_OBJECT (selpad), "tags");
432 GST_DEBUG_OBJECT (pad, "received EOS");
438 GST_DEBUG_OBJECT (pad, "forwarding event");
439 res = gst_pad_push_event (sel->srcpad, event);
441 gst_event_unref (event);
443 gst_object_unref (sel);
449 gst_selector_pad_getcaps (GstPad * pad)
451 GstInputSelector *sel;
454 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
456 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
457 caps = gst_pad_peer_get_caps_reffed (sel->srcpad);
459 caps = gst_caps_new_any ();
461 gst_object_unref (sel);
467 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
469 GstInputSelector *sel;
472 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
474 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
475 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
476 gst_object_unref (sel);
482 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
483 guint size, GstCaps * caps, GstBuffer ** buf)
485 GstInputSelector *sel;
486 GstFlowReturn result;
487 GstPad *active_sinkpad;
488 GstPad *prev_active_sinkpad;
489 GstSelectorPad *selpad;
491 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
492 selpad = GST_SELECTOR_PAD_CAST (pad);
494 GST_LOG_OBJECT (pad, "received alloc");
496 GST_INPUT_SELECTOR_LOCK (sel);
497 prev_active_sinkpad = sel->active_sinkpad;
498 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
500 if (pad != active_sinkpad)
503 GST_INPUT_SELECTOR_UNLOCK (sel);
505 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
506 NOTIFY_MUTEX_LOCK ();
507 g_object_notify (G_OBJECT (sel), "active-pad");
508 NOTIFY_MUTEX_UNLOCK ();
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 start_time;
563 GstEvent *close_event = NULL, *start_event = NULL;
566 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
567 selpad = GST_SELECTOR_PAD_CAST (pad);
568 seg = &selpad->segment;
570 GST_INPUT_SELECTOR_LOCK (sel);
571 /* wait or check for flushing */
572 if (gst_input_selector_wait (sel, pad))
575 GST_LOG_OBJECT (pad, "getting active pad");
577 prev_active_sinkpad = sel->active_sinkpad;
578 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
580 /* update the segment on the srcpad */
581 start_time = GST_BUFFER_TIMESTAMP (buf);
582 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
583 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
584 GST_TIME_ARGS (start_time));
585 if (GST_BUFFER_DURATION_IS_VALID (buf))
586 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
587 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
589 GST_OBJECT_LOCK (pad);
590 gst_segment_set_last_stop (seg, seg->format, start_time);
591 GST_OBJECT_UNLOCK (pad);
594 /* Ignore buffers from pads except the selected one */
595 if (pad != active_sinkpad)
598 if (G_UNLIKELY (sel->pending_close)) {
599 GstSegment *cseg = &sel->segment;
601 GST_DEBUG_OBJECT (sel,
602 "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
604 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
605 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
606 cseg->start, cseg->stop, cseg->time);
608 /* create update segment */
609 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
610 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
612 sel->pending_close = FALSE;
614 /* if we have a pending segment, push it out now */
615 if (G_UNLIKELY (selpad->segment_pending)) {
616 GST_DEBUG_OBJECT (pad,
617 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
619 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
620 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
621 seg->start, seg->stop, seg->time);
623 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
624 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
626 selpad->segment_pending = FALSE;
628 GST_INPUT_SELECTOR_UNLOCK (sel);
630 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
631 NOTIFY_MUTEX_LOCK ();
632 g_object_notify (G_OBJECT (sel), "active-pad");
633 NOTIFY_MUTEX_UNLOCK ();
637 gst_pad_push_event (sel->srcpad, close_event);
640 gst_pad_push_event (sel->srcpad, start_event);
642 if (selpad->discont) {
643 buf = gst_buffer_make_metadata_writable (buf);
645 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
646 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
647 selpad->discont = FALSE;
651 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
653 if ((caps = GST_BUFFER_CAPS (buf))) {
654 if (GST_PAD_CAPS (sel->srcpad) != caps)
655 gst_pad_set_caps (sel->srcpad, caps);
658 res = gst_pad_push (sel->srcpad, buf);
661 gst_object_unref (sel);
664 /* dropped buffers */
667 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
668 /* when we drop a buffer, we're creating a discont on this pad */
669 selpad->discont = TRUE;
670 GST_INPUT_SELECTOR_UNLOCK (sel);
671 gst_buffer_unref (buf);
673 /* figure out what to return upstream */
674 GST_OBJECT_LOCK (selpad);
675 if (selpad->always_ok)
678 res = GST_FLOW_NOT_LINKED;
679 GST_OBJECT_UNLOCK (selpad);
685 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
686 GST_INPUT_SELECTOR_UNLOCK (sel);
687 gst_buffer_unref (buf);
688 res = GST_FLOW_WRONG_STATE;
693 static void gst_input_selector_dispose (GObject * object);
695 static void gst_input_selector_set_property (GObject * object,
696 guint prop_id, const GValue * value, GParamSpec * pspec);
697 static void gst_input_selector_get_property (GObject * object,
698 guint prop_id, GValue * value, GParamSpec * pspec);
700 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
701 GstPadTemplate * templ, const gchar * unused);
702 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
704 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
705 element, GstStateChange transition);
707 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
708 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
709 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
710 static gint64 gst_input_selector_block (GstInputSelector * self);
711 static void gst_input_selector_switch (GstInputSelector * self,
712 GstPad * pad, gint64 stop_time, gint64 start_time);
714 /* FIXME: create these marshallers using glib-genmarshal */
715 #define g_marshal_value_peek_object(v) (v)->data[0].v_pointer
716 #define g_marshal_value_peek_int64(v) (v)->data[0].v_int64
719 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
720 GValue * return_value G_GNUC_UNUSED,
721 guint n_param_values,
722 const GValue * param_values,
723 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
725 typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
726 register GMarshalFunc_INT64__VOID callback;
727 register GCClosure *cc = (GCClosure *) closure;
728 register gpointer data1, data2;
731 g_return_if_fail (return_value != NULL);
732 g_return_if_fail (n_param_values == 1);
734 if (G_CCLOSURE_SWAP_DATA (closure)) {
735 data1 = closure->data;
736 data2 = g_value_peek_pointer (param_values + 0);
738 data1 = g_value_peek_pointer (param_values + 0);
739 data2 = closure->data;
742 (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
744 v_return = callback (data1, data2);
746 g_value_set_int64 (return_value, v_return);
750 gst_input_selector_marshal_VOID__OBJECT_INT64_INT64 (GClosure * closure,
751 GValue * return_value G_GNUC_UNUSED,
752 guint n_param_values,
753 const GValue * param_values,
754 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
756 typedef void (*GMarshalFunc_VOID__OBJECT_INT64_INT64) (gpointer data1,
757 gpointer arg_1, gint64 arg_2, gint64 arg_3, gpointer data2);
758 register GMarshalFunc_VOID__OBJECT_INT64_INT64 callback;
759 register GCClosure *cc = (GCClosure *) closure;
760 register gpointer data1, data2;
762 g_return_if_fail (n_param_values == 4);
764 if (G_CCLOSURE_SWAP_DATA (closure)) {
765 data1 = closure->data;
766 data2 = g_value_peek_pointer (param_values + 0);
768 data1 = g_value_peek_pointer (param_values + 0);
769 data2 = closure->data;
772 (GMarshalFunc_VOID__OBJECT_INT64_INT64) (marshal_data ? marshal_data :
776 g_marshal_value_peek_object (param_values + 1),
777 g_marshal_value_peek_int64 (param_values + 2),
778 g_marshal_value_peek_int64 (param_values + 3), data2);
781 #define _do_init(bla) \
782 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
783 "input-selector", 0, "An input stream selector element");
785 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
786 GST_TYPE_ELEMENT, _do_init);
789 gst_input_selector_base_init (gpointer g_class)
791 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
793 gst_element_class_set_details_simple (element_class, "Input selector",
794 "Generic", "N-to-1 input stream selector",
795 "Julien Moutte <julien@moutte.net>, "
796 "Jan Schmidt <thaytan@mad.scientist.com>, "
797 "Wim Taymans <wim.taymans@gmail.com>");
798 gst_element_class_add_pad_template (element_class,
799 gst_static_pad_template_get (&gst_input_selector_sink_factory));
800 gst_element_class_add_pad_template (element_class,
801 gst_static_pad_template_get (&gst_input_selector_src_factory));
805 gst_input_selector_class_init (GstInputSelectorClass * klass)
807 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
808 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
810 gobject_class->dispose = gst_input_selector_dispose;
812 gobject_class->set_property = gst_input_selector_set_property;
813 gobject_class->get_property = gst_input_selector_get_property;
815 g_object_class_install_property (gobject_class, PROP_N_PADS,
816 g_param_spec_uint ("n-pads", "Number of Pads",
817 "The number of sink pads", 0, G_MAXUINT, 0,
818 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
820 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
821 g_param_spec_object ("active-pad", "Active pad",
822 "The currently active sink pad", GST_TYPE_PAD,
823 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
826 * GstInputSelector::block:
827 * @inputselector: the #GstInputSelector
829 * Block all sink pads in preparation for a switch. Returns the stop time of
830 * the current switch segment, as a running time, or 0 if there is no current
831 * active pad or the current active pad never received data.
833 gst_input_selector_signals[SIGNAL_BLOCK] =
834 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
835 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
836 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
837 gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
839 * GstInputSelector::switch:
840 * @inputselector: the #GstInputSelector
841 * @pad: the pad to switch to
842 * @stop_time: running time at which to close the previous segment, or -1
843 * to use the running time of the previously active sink pad
844 * @start_time: running time at which to start the new segment, or -1 to
845 * use the running time of the newly active sink pad
847 * Switch to a new feed. The segment opened by the previously active pad, if
848 * any, will be closed, and a new segment opened before data flows again.
850 * This signal must be emitted when the element has been blocked via the <link
851 * linkend="GstInputSelector-block">block</link> signal.
853 * If you have a stream with only one switch element, such as an audio-only
854 * stream, a stream switch should be performed by first emitting the block
855 * signal, and then emitting the switch signal with -1 for the stop and start
858 * The intention of the @stop_time and @start_time arguments is to allow
859 * multiple switch elements to switch and maintain stream synchronization.
860 * When switching a stream with multiple feeds, you will need as many switch
861 * elements as you have feeds. For example, a feed with audio and video will
862 * have one switch element between the audio feeds and one for video.
864 * A switch over multiple switch elements should be performed as follows:
865 * First, emit the <link linkend="GstInputSelector-block">block</link>
866 * signal, collecting the returned values. The maximum running time returned
867 * by block should then be used as the time at which to close the previous
870 * Then, query the running times of the new audio and video pads that you will
871 * switch to. Naturally, these pads are on separate switch elements. Take the
872 * minimum running time for those streams and use it for the time at which to
873 * open the new segment.
875 * If @pad is the same as the current active pad, the element will cancel any
876 * previous block without adjusting segments.
879 * the signal changed from accepting the pad name to the pad object.
884 gst_input_selector_signals[SIGNAL_SWITCH] =
885 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
886 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
887 NULL, NULL, gst_input_selector_marshal_VOID__OBJECT_INT64_INT64,
888 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
890 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
891 gstelement_class->release_pad = gst_input_selector_release_pad;
892 gstelement_class->change_state = gst_input_selector_change_state;
894 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
895 /* note the underscore because switch is a keyword otherwise */
896 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
900 gst_input_selector_init (GstInputSelector * sel,
901 GstInputSelectorClass * g_class)
903 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
904 gst_pad_set_iterate_internal_links_function (sel->srcpad,
905 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
906 gst_pad_set_getcaps_function (sel->srcpad,
907 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
908 gst_pad_set_query_function (sel->srcpad,
909 GST_DEBUG_FUNCPTR (gst_input_selector_query));
910 gst_pad_set_event_function (sel->srcpad,
911 GST_DEBUG_FUNCPTR (gst_input_selector_event));
912 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
913 /* sinkpad management */
914 sel->active_sinkpad = NULL;
916 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
918 sel->lock = g_mutex_new ();
919 sel->cond = g_cond_new ();
920 sel->blocked = FALSE;
924 gst_input_selector_dispose (GObject * object)
926 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
928 if (sel->active_sinkpad) {
929 gst_object_unref (sel->active_sinkpad);
930 sel->active_sinkpad = NULL;
933 g_mutex_free (sel->lock);
937 g_cond_free (sel->cond);
941 G_OBJECT_CLASS (parent_class)->dispose (object);
944 /* Solve the following equation for B.timestamp, and set that as the segment
946 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
949 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
951 if (running_time <= segment->accum)
952 return segment->start;
954 return (running_time - segment->accum) * segment->abs_rate + segment->start;
958 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
960 segment->stop = gst_segment_get_timestamp (segment, running_time);
961 segment->last_stop = -1;
965 gst_segment_set_start (GstSegment * segment, gint64 running_time)
967 gint64 new_start, duration;
969 new_start = gst_segment_get_timestamp (segment, running_time);
971 /* this is the duration we skipped */
972 duration = new_start - segment->start;
973 /* add the duration to the accumulated segment time */
974 segment->accum += duration;
975 /* move position in the segment */
976 segment->time += duration;
977 segment->start += duration;
980 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
981 * active pad changed. */
983 gst_input_selector_set_active_pad (GstInputSelector * self,
984 GstPad * pad, gint64 stop_time, gint64 start_time)
986 GstSelectorPad *old, *new;
987 GstPad **active_pad_p;
989 if (pad == self->active_sinkpad)
992 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
993 new = GST_SELECTOR_PAD_CAST (pad);
995 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
996 GST_DEBUG_PAD_NAME (new));
998 if (!GST_CLOCK_TIME_IS_VALID (stop_time) && old) {
999 /* no stop time given, get the latest running_time on the active pad to
1000 * close and open the new segment */
1001 stop_time = start_time = gst_selector_pad_get_running_time (old);
1002 GST_DEBUG_OBJECT (self, "using start/stop of %" GST_TIME_FORMAT,
1003 GST_TIME_ARGS (start_time));
1006 if (old && old->active && !self->pending_close && stop_time >= 0) {
1007 /* schedule a last_stop update if one isn't already scheduled, and a
1008 segment has been pushed before. */
1009 memcpy (&self->segment, &old->segment, sizeof (self->segment));
1011 GST_DEBUG_OBJECT (self, "setting stop_time to %" GST_TIME_FORMAT,
1012 GST_TIME_ARGS (stop_time));
1013 gst_segment_set_stop (&self->segment, stop_time);
1014 self->pending_close = TRUE;
1017 if (new && new->active && start_time >= 0) {
1018 GST_DEBUG_OBJECT (self, "setting start_time to %" GST_TIME_FORMAT,
1019 GST_TIME_ARGS (start_time));
1020 /* schedule a new segment push */
1021 gst_segment_set_start (&new->segment, start_time);
1022 new->segment_pending = TRUE;
1025 active_pad_p = &self->active_sinkpad;
1026 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1027 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1028 self->active_sinkpad);
1034 gst_input_selector_set_property (GObject * object, guint prop_id,
1035 const GValue * value, GParamSpec * pspec)
1037 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1040 case PROP_ACTIVE_PAD:
1044 pad = g_value_get_object (value);
1046 GST_INPUT_SELECTOR_LOCK (sel);
1047 gst_input_selector_set_active_pad (sel, pad,
1048 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1049 GST_INPUT_SELECTOR_UNLOCK (sel);
1053 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1059 gst_input_selector_get_property (GObject * object, guint prop_id,
1060 GValue * value, GParamSpec * pspec)
1062 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1066 GST_INPUT_SELECTOR_LOCK (object);
1067 g_value_set_uint (value, sel->n_pads);
1068 GST_INPUT_SELECTOR_UNLOCK (object);
1070 case PROP_ACTIVE_PAD:
1071 GST_INPUT_SELECTOR_LOCK (object);
1072 g_value_set_object (value, sel->active_sinkpad);
1073 GST_INPUT_SELECTOR_UNLOCK (object);
1076 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1082 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1084 GstInputSelector *sel;
1085 GstPad *otherpad = NULL;
1087 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1089 GST_INPUT_SELECTOR_LOCK (sel);
1090 if (pad == sel->srcpad)
1091 otherpad = sel->active_sinkpad;
1092 else if (pad == sel->active_sinkpad || !strict)
1093 otherpad = sel->srcpad;
1095 gst_object_ref (otherpad);
1096 GST_INPUT_SELECTOR_UNLOCK (sel);
1098 gst_object_unref (sel);
1104 gst_input_selector_event (GstPad * pad, GstEvent * event)
1106 gboolean res = FALSE;
1109 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1112 res = gst_pad_push_event (otherpad, event);
1114 gst_object_unref (otherpad);
1116 gst_event_unref (event);
1120 /* query on the srcpad. We override this function because by default it will
1121 * only forward the query to one random sinkpad */
1123 gst_input_selector_query (GstPad * pad, GstQuery * query)
1125 gboolean res = TRUE;
1126 GstInputSelector *sel;
1129 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1131 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1133 switch (GST_QUERY_TYPE (query)) {
1134 case GST_QUERY_LATENCY:
1137 GstClockTime resmin, resmax;
1144 /* assume FALSE, we become TRUE if one query succeeds */
1147 /* perform the query on all sinkpads and combine the results. We take the
1148 * max of min and the min of max for the result latency. */
1149 GST_INPUT_SELECTOR_LOCK (sel);
1150 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1151 walk = g_list_next (walk)) {
1152 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1154 if (gst_pad_peer_query (sinkpad, query)) {
1155 GstClockTime min, max;
1158 /* one query succeeded, we succeed too */
1161 gst_query_parse_latency (query, &live, &min, &max);
1163 GST_DEBUG_OBJECT (sinkpad,
1164 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1165 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1172 else if (max < resmax)
1174 if (reslive == FALSE)
1179 GST_INPUT_SELECTOR_UNLOCK (sel);
1181 gst_query_set_latency (query, reslive, resmin, resmax);
1183 GST_DEBUG_OBJECT (sel,
1184 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1185 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1193 res = gst_pad_peer_query (otherpad, query);
1197 gst_object_unref (otherpad);
1198 gst_object_unref (sel);
1204 gst_input_selector_getcaps (GstPad * pad)
1210 parent = gst_object_get_parent (GST_OBJECT (pad));
1212 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1215 GST_DEBUG_OBJECT (pad, "Pad not linked, returning ANY");
1216 caps = gst_caps_new_any ();
1218 GST_DEBUG_OBJECT (pad, "Pad is linked (to %s:%s), returning peer caps",
1219 GST_DEBUG_PAD_NAME (otherpad));
1220 /* if the peer has caps, use those. If the pad is not linked, this function
1221 * returns NULL and we return ANY */
1222 if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1223 caps = gst_caps_new_any ();
1224 gst_object_unref (otherpad);
1227 gst_object_unref (parent);
1231 /* check if the pad is the active sinkpad */
1232 static inline gboolean
1233 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1237 GST_INPUT_SELECTOR_LOCK (sel);
1238 res = (pad == sel->active_sinkpad);
1239 GST_INPUT_SELECTOR_UNLOCK (sel);
1244 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1246 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1248 GstPad *active_sinkpad;
1249 GstSelectorPad *selpad;
1251 selpad = GST_SELECTOR_PAD_CAST (pad);
1253 selpad->active = TRUE;
1254 active_sinkpad = sel->active_sinkpad;
1255 if (active_sinkpad == NULL) {
1256 /* first pad we get activity on becomes the activated pad by default */
1257 if (sel->active_sinkpad)
1258 gst_object_unref (sel->active_sinkpad);
1259 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1260 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1263 return active_sinkpad;
1267 gst_input_selector_request_new_pad (GstElement * element,
1268 GstPadTemplate * templ, const gchar * unused)
1270 GstInputSelector *sel;
1272 GstPad *sinkpad = NULL;
1274 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1276 sel = GST_INPUT_SELECTOR (element);
1278 GST_INPUT_SELECTOR_LOCK (sel);
1280 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1281 name = g_strdup_printf ("sink%d", sel->padcount++);
1282 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1283 "name", name, "direction", templ->direction, "template", templ, NULL);
1288 gst_pad_set_event_function (sinkpad,
1289 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1290 gst_pad_set_getcaps_function (sinkpad,
1291 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1292 gst_pad_set_acceptcaps_function (sinkpad,
1293 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1294 gst_pad_set_chain_function (sinkpad,
1295 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1296 gst_pad_set_iterate_internal_links_function (sinkpad,
1297 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1298 gst_pad_set_bufferalloc_function (sinkpad,
1299 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1301 gst_pad_set_active (sinkpad, TRUE);
1302 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1303 GST_INPUT_SELECTOR_UNLOCK (sel);
1309 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1311 GstInputSelector *sel;
1313 sel = GST_INPUT_SELECTOR (element);
1314 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1316 GST_INPUT_SELECTOR_LOCK (sel);
1317 /* if the pad was the active pad, makes us select a new one */
1318 if (sel->active_sinkpad == pad) {
1319 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1320 gst_object_unref (sel->active_sinkpad);
1321 sel->active_sinkpad = NULL;
1325 gst_pad_set_active (pad, FALSE);
1326 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1327 GST_INPUT_SELECTOR_UNLOCK (sel);
1331 gst_input_selector_reset (GstInputSelector * sel)
1335 GST_INPUT_SELECTOR_LOCK (sel);
1336 /* clear active pad */
1337 if (sel->active_sinkpad) {
1338 gst_object_unref (sel->active_sinkpad);
1339 sel->active_sinkpad = NULL;
1342 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1343 sel->pending_close = FALSE;
1344 /* reset each of our sinkpads state */
1345 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1346 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1348 gst_selector_pad_reset (selpad);
1351 gst_tag_list_free (selpad->tags);
1352 selpad->tags = NULL;
1355 GST_INPUT_SELECTOR_UNLOCK (sel);
1358 static GstStateChangeReturn
1359 gst_input_selector_change_state (GstElement * element,
1360 GstStateChange transition)
1362 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1363 GstStateChangeReturn result;
1365 switch (transition) {
1366 case GST_STATE_CHANGE_READY_TO_PAUSED:
1367 GST_INPUT_SELECTOR_LOCK (self);
1368 self->blocked = FALSE;
1369 self->flushing = FALSE;
1370 GST_INPUT_SELECTOR_UNLOCK (self);
1372 case GST_STATE_CHANGE_PAUSED_TO_READY:
1373 /* first unlock before we call the parent state change function, which
1374 * tries to acquire the stream lock when going to ready. */
1375 GST_INPUT_SELECTOR_LOCK (self);
1376 self->blocked = FALSE;
1377 self->flushing = TRUE;
1378 GST_INPUT_SELECTOR_BROADCAST (self);
1379 GST_INPUT_SELECTOR_UNLOCK (self);
1385 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1387 switch (transition) {
1388 case GST_STATE_CHANGE_PAUSED_TO_READY:
1389 gst_input_selector_reset (self);
1399 gst_input_selector_block (GstInputSelector * self)
1402 GstSelectorPad *spad;
1404 GST_INPUT_SELECTOR_LOCK (self);
1407 GST_WARNING_OBJECT (self, "switch already blocked");
1409 self->blocked = TRUE;
1410 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1413 ret = gst_selector_pad_get_running_time (spad);
1415 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1417 GST_INPUT_SELECTOR_UNLOCK (self);
1422 /* stop_time and start_time are running times */
1424 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1425 gint64 stop_time, gint64 start_time)
1429 g_return_if_fail (self->blocked == TRUE);
1431 GST_INPUT_SELECTOR_LOCK (self);
1433 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1435 self->blocked = FALSE;
1436 GST_INPUT_SELECTOR_BROADCAST (self);
1437 GST_INPUT_SELECTOR_UNLOCK (self);
1440 NOTIFY_MUTEX_LOCK ();
1441 g_object_notify (G_OBJECT (self), "active-pad");
1442 NOTIFY_MUTEX_UNLOCK ();