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>)
8 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
27 * SECTION:element-input-selector
28 * @see_also: #GstOutputSelector
30 * Direct one out of N input streams to the output pad.
32 * The input pads are from a GstPad subclass and have additional
33 * properties, which users may find useful, namely:
37 * "running-time": Running time of stream on pad (#gint64)
40 * "tags": The currently active tags on the pad (#GstTagList, boxed type)
43 * "active": If the pad is currently active (#gboolean)
46 * "always-ok" : Make an inactive pads return #GST_FLOW_OK instead of
47 * #GST_FLOW_NOT_LINKED
60 #include "gstinputselector.h"
62 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
63 #define GST_CAT_DEFAULT input_selector_debug
65 #if GLIB_CHECK_VERSION(2, 26, 0)
66 #define NOTIFY_MUTEX_LOCK()
67 #define NOTIFY_MUTEX_UNLOCK()
69 static GStaticRecMutex notify_mutex = G_STATIC_REC_MUTEX_INIT;
70 #define NOTIFY_MUTEX_LOCK() g_static_rec_mutex_lock (¬ify_mutex)
71 #define NOTIFY_MUTEX_UNLOCK() g_static_rec_mutex_unlock (¬ify_mutex)
74 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (((GstInputSelector*)(sel))->lock)
75 #define GST_INPUT_SELECTOR_GET_COND(sel) (((GstInputSelector*)(sel))->cond)
76 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
77 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
78 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
79 GST_INPUT_SELECTOR_GET_LOCK(sel)))
80 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
82 static GstStaticPadTemplate gst_input_selector_sink_factory =
83 GST_STATIC_PAD_TEMPLATE ("sink%d",
88 static GstStaticPadTemplate gst_input_selector_src_factory =
89 GST_STATIC_PAD_TEMPLATE ("src",
101 #define DEFAULT_PAD_ALWAYS_OK TRUE
106 PROP_PAD_RUNNING_TIME,
119 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
121 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
123 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
125 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
128 #define GST_TYPE_SELECTOR_PAD \
129 (gst_selector_pad_get_type())
130 #define GST_SELECTOR_PAD(obj) \
131 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
132 #define GST_SELECTOR_PAD_CLASS(klass) \
133 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
134 #define GST_IS_SELECTOR_PAD(obj) \
135 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
136 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
137 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
138 #define GST_SELECTOR_PAD_CAST(obj) \
139 ((GstSelectorPad *)(obj))
141 typedef struct _GstSelectorPad GstSelectorPad;
142 typedef struct _GstSelectorPadClass GstSelectorPadClass;
144 struct _GstSelectorPad
148 gboolean active; /* when buffer have passed the pad */
149 gboolean pushed; /* when buffer was pushed downstream since activation */
150 gboolean eos; /* when EOS has been received */
151 gboolean eos_sent; /* when EOS was sent downstream */
152 gboolean discont; /* after switching we create a discont */
153 gboolean flushing; /* set after flush-start and before flush-stop */
155 GstSegment segment; /* the current segment on the pad */
156 GstTagList *tags; /* last tags received on the pad */
158 gboolean segment_pending;
161 struct _GstSelectorPadClass
166 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
167 static void gst_selector_pad_init (GstSelectorPad * pad);
168 static void gst_selector_pad_finalize (GObject * object);
169 static void gst_selector_pad_get_property (GObject * object,
170 guint prop_id, GValue * value, GParamSpec * pspec);
171 static void gst_selector_pad_set_property (GObject * object,
172 guint prop_id, const GValue * value, GParamSpec * pspec);
174 static GstPadClass *selector_pad_parent_class = NULL;
176 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
177 static void gst_selector_pad_reset (GstSelectorPad * pad);
178 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
179 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
180 static gboolean gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps);
181 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad);
182 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
183 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
184 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
187 gst_selector_pad_get_type (void)
189 static volatile gsize selector_pad_type = 0;
190 static const GTypeInfo selector_pad_info = {
191 sizeof (GstSelectorPadClass),
194 (GClassInitFunc) gst_selector_pad_class_init,
197 sizeof (GstSelectorPad),
199 (GInstanceInitFunc) gst_selector_pad_init,
202 if (g_once_init_enter (&selector_pad_type)) {
203 GType tmp = g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
204 &selector_pad_info, 0);
205 g_once_init_leave (&selector_pad_type, tmp);
208 return (GType) selector_pad_type;
212 gst_selector_pad_class_init (GstSelectorPadClass * klass)
214 GObjectClass *gobject_class;
216 gobject_class = (GObjectClass *) klass;
218 selector_pad_parent_class = g_type_class_peek_parent (klass);
220 gobject_class->finalize = gst_selector_pad_finalize;
222 gobject_class->get_property = gst_selector_pad_get_property;
223 gobject_class->set_property = gst_selector_pad_set_property;
225 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
226 g_param_spec_int64 ("running-time", "Running time",
227 "Running time of stream on pad", 0, G_MAXINT64, 0,
228 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
230 g_param_spec_boxed ("tags", "Tags",
231 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
232 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
234 g_param_spec_boolean ("active", "Active",
235 "If the pad is currently active", FALSE,
236 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
237 /* FIXME: better property name? */
238 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
239 g_param_spec_boolean ("always-ok", "Always OK",
240 "Make an inactive pad return OK instead of NOT_LINKED",
241 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245 gst_selector_pad_init (GstSelectorPad * pad)
247 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
248 gst_selector_pad_reset (pad);
252 gst_selector_pad_finalize (GObject * object)
256 pad = GST_SELECTOR_PAD_CAST (object);
259 gst_tag_list_free (pad->tags);
261 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
265 gst_selector_pad_set_property (GObject * object, guint prop_id,
266 const GValue * value, GParamSpec * pspec)
268 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
271 case PROP_PAD_ALWAYS_OK:
272 GST_OBJECT_LOCK (object);
273 spad->always_ok = g_value_get_boolean (value);
274 GST_OBJECT_UNLOCK (object);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gst_selector_pad_get_property (GObject * object, guint prop_id,
284 GValue * value, GParamSpec * pspec)
286 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
289 case PROP_PAD_RUNNING_TIME:
290 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
293 GST_OBJECT_LOCK (object);
294 g_value_set_boxed (value, spad->tags);
295 GST_OBJECT_UNLOCK (object);
297 case PROP_PAD_ACTIVE:
299 GstInputSelector *sel;
301 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
302 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
303 GST_PAD_CAST (spad)));
304 gst_object_unref (sel);
307 case PROP_PAD_ALWAYS_OK:
308 GST_OBJECT_LOCK (object);
309 g_value_set_boolean (value, spad->always_ok);
310 GST_OBJECT_UNLOCK (object);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 gst_selector_pad_get_running_time (GstSelectorPad * pad)
323 GST_OBJECT_LOCK (pad);
325 gint64 last_stop = pad->segment.last_stop;
328 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
331 GST_OBJECT_UNLOCK (pad);
333 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
334 GST_TIME_ARGS (ret));
340 gst_selector_pad_reset (GstSelectorPad * pad)
342 GST_OBJECT_LOCK (pad);
346 pad->eos_sent = FALSE;
347 pad->segment_pending = FALSE;
348 pad->discont = FALSE;
349 pad->flushing = FALSE;
350 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
351 GST_OBJECT_UNLOCK (pad);
354 /* strictly get the linked pad from the sinkpad. If the pad is active we return
355 * the srcpad else we return NULL */
357 gst_selector_pad_iterate_linked_pads (GstPad * pad)
359 GstInputSelector *sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
363 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
364 it = gst_iterator_new_single (GST_TYPE_PAD, otherpad,
365 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
368 gst_object_unref (otherpad);
369 gst_object_unref (sel);
375 gst_selector_pad_event (GstPad * pad, GstEvent * event)
379 GstInputSelector *sel;
380 GstSelectorPad *selpad;
381 GstPad *prev_active_sinkpad;
382 GstPad *active_sinkpad;
384 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
385 selpad = GST_SELECTOR_PAD_CAST (pad);
387 GST_INPUT_SELECTOR_LOCK (sel);
388 prev_active_sinkpad = sel->active_sinkpad;
389 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
391 /* only forward if we are dealing with the active sinkpad */
392 forward = (pad == active_sinkpad);
393 GST_INPUT_SELECTOR_UNLOCK (sel);
395 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
396 NOTIFY_MUTEX_LOCK ();
397 g_object_notify (G_OBJECT (sel), "active-pad");
398 NOTIFY_MUTEX_UNLOCK ();
401 switch (GST_EVENT_TYPE (event)) {
402 case GST_EVENT_FLUSH_START:
403 /* Unblock the pad if it's waiting */
404 GST_INPUT_SELECTOR_LOCK (sel);
405 selpad->flushing = TRUE;
406 GST_INPUT_SELECTOR_BROADCAST (sel);
407 GST_INPUT_SELECTOR_UNLOCK (sel);
409 case GST_EVENT_FLUSH_STOP:
410 GST_INPUT_SELECTOR_LOCK (sel);
411 gst_selector_pad_reset (selpad);
412 sel->pending_close = FALSE;
413 GST_INPUT_SELECTOR_UNLOCK (sel);
415 case GST_EVENT_NEWSEGMENT:
420 gint64 start, stop, time;
422 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
423 &start, &stop, &time);
425 GST_DEBUG_OBJECT (pad,
426 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
428 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
429 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
431 GST_INPUT_SELECTOR_LOCK (sel);
432 GST_OBJECT_LOCK (selpad);
433 gst_segment_set_newsegment_full (&selpad->segment, update,
434 rate, arate, format, start, stop, time);
435 GST_OBJECT_UNLOCK (selpad);
437 /* If we aren't forwarding the event because the pad is not the
438 * active_sinkpad, then set the flag on the pad
439 * that says a segment needs sending if/when that pad is activated.
440 * For all other cases, we send the event immediately, which makes
441 * sparse streams and other segment updates work correctly downstream.
444 selpad->segment_pending = TRUE;
446 GST_INPUT_SELECTOR_UNLOCK (sel);
451 GstTagList *tags, *oldtags, *newtags;
453 gst_event_parse_tag (event, &tags);
455 GST_OBJECT_LOCK (selpad);
456 oldtags = selpad->tags;
458 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
459 selpad->tags = newtags;
461 gst_tag_list_free (oldtags);
462 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
463 GST_OBJECT_UNLOCK (selpad);
465 g_object_notify (G_OBJECT (selpad), "tags");
472 selpad->eos_sent = TRUE;
476 /* If the active sinkpad is in EOS state but EOS
477 * was not sent downstream this means that the pad
478 * got EOS before it was set as active pad and that
479 * the previously active pad got EOS after it was
482 GST_INPUT_SELECTOR_LOCK (sel);
483 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
484 tmp = GST_SELECTOR_PAD (active_sinkpad);
485 forward = (tmp->eos && !tmp->eos_sent);
486 tmp->eos_sent = TRUE;
487 GST_INPUT_SELECTOR_UNLOCK (sel);
489 GST_DEBUG_OBJECT (pad, "received EOS");
495 GST_DEBUG_OBJECT (pad, "forwarding event");
496 res = gst_pad_push_event (sel->srcpad, event);
498 gst_event_unref (event);
500 gst_object_unref (sel);
506 gst_selector_pad_getcaps (GstPad * pad)
508 GstInputSelector *sel;
511 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
513 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
514 caps = gst_pad_peer_get_caps_reffed (sel->srcpad);
516 caps = gst_caps_new_any ();
518 gst_object_unref (sel);
524 gst_selector_pad_acceptcaps (GstPad * pad, GstCaps * caps)
526 GstInputSelector *sel;
529 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
531 GST_DEBUG_OBJECT (sel, "Checking acceptcaps of srcpad peer");
532 res = gst_pad_peer_accept_caps (sel->srcpad, caps);
533 gst_object_unref (sel);
539 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
540 guint size, GstCaps * caps, GstBuffer ** buf)
542 GstInputSelector *sel;
543 GstFlowReturn result;
544 GstPad *active_sinkpad;
545 GstPad *prev_active_sinkpad;
546 GstSelectorPad *selpad;
548 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
549 selpad = GST_SELECTOR_PAD_CAST (pad);
551 GST_LOG_OBJECT (pad, "received alloc");
553 GST_INPUT_SELECTOR_LOCK (sel);
554 prev_active_sinkpad = sel->active_sinkpad;
555 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
557 if (pad != active_sinkpad)
560 GST_INPUT_SELECTOR_UNLOCK (sel);
562 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
563 NOTIFY_MUTEX_LOCK ();
564 g_object_notify (G_OBJECT (sel), "active-pad");
565 NOTIFY_MUTEX_UNLOCK ();
568 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
571 gst_object_unref (sel);
578 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
580 GST_INPUT_SELECTOR_UNLOCK (sel);
582 /* unselected pad, perform fallback alloc or return unlinked when
584 GST_OBJECT_LOCK (selpad);
585 if (selpad->always_ok || !active_pad_pushed) {
586 GST_DEBUG_OBJECT (pad, "Not selected, performing fallback allocation");
588 result = GST_FLOW_OK;
590 GST_DEBUG_OBJECT (pad, "Not selected, return NOT_LINKED");
591 result = GST_FLOW_NOT_LINKED;
593 GST_OBJECT_UNLOCK (selpad);
599 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
600 * or return TRUE when flushing */
602 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
604 while (self->blocked && !self->flushing && !pad->flushing) {
605 /* we can be unlocked here when we are shutting down (flushing) or when we
607 GST_INPUT_SELECTOR_WAIT (self);
609 return self->flushing;
613 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
615 GstInputSelector *sel;
617 GstPad *active_sinkpad;
618 GstPad *prev_active_sinkpad;
619 GstSelectorPad *selpad;
620 GstClockTime start_time;
622 GstEvent *close_event = NULL, *start_event = NULL;
625 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
626 selpad = GST_SELECTOR_PAD_CAST (pad);
627 seg = &selpad->segment;
629 GST_INPUT_SELECTOR_LOCK (sel);
630 /* wait or check for flushing */
631 if (gst_input_selector_wait (sel, selpad))
634 GST_LOG_OBJECT (pad, "getting active pad");
636 prev_active_sinkpad = sel->active_sinkpad;
637 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
639 /* update the segment on the srcpad */
640 start_time = GST_BUFFER_TIMESTAMP (buf);
641 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
642 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
643 GST_TIME_ARGS (start_time));
644 if (GST_BUFFER_DURATION_IS_VALID (buf))
645 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
646 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
648 GST_OBJECT_LOCK (pad);
649 gst_segment_set_last_stop (seg, seg->format, start_time);
650 GST_OBJECT_UNLOCK (pad);
653 /* Ignore buffers from pads except the selected one */
654 if (pad != active_sinkpad)
657 if (G_UNLIKELY (sel->pending_close)) {
658 GstSegment *cseg = &sel->segment;
660 GST_DEBUG_OBJECT (sel,
661 "pushing close NEWSEGMENT update %d, rate %lf, applied rate %lf, "
663 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
664 G_GINT64_FORMAT, TRUE, cseg->rate, cseg->applied_rate, cseg->format,
665 cseg->start, cseg->stop, cseg->time);
667 /* create update segment */
668 close_event = gst_event_new_new_segment_full (TRUE, cseg->rate,
669 cseg->applied_rate, cseg->format, cseg->start, cseg->stop, cseg->time);
671 sel->pending_close = FALSE;
673 /* if we have a pending segment, push it out now */
674 if (G_UNLIKELY (selpad->segment_pending)) {
675 GST_DEBUG_OBJECT (pad,
676 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
678 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
679 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
680 seg->start, seg->stop, seg->time);
682 start_event = gst_event_new_new_segment_full (FALSE, seg->rate,
683 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
685 selpad->segment_pending = FALSE;
687 GST_INPUT_SELECTOR_UNLOCK (sel);
689 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
690 NOTIFY_MUTEX_LOCK ();
691 g_object_notify (G_OBJECT (sel), "active-pad");
692 NOTIFY_MUTEX_UNLOCK ();
696 gst_pad_push_event (sel->srcpad, close_event);
699 gst_pad_push_event (sel->srcpad, start_event);
701 if (selpad->discont) {
702 buf = gst_buffer_make_writable (buf);
704 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
705 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
706 selpad->discont = FALSE;
710 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
712 if ((caps = GST_BUFFER_CAPS (buf))) {
713 if (GST_PAD_CAPS (sel->srcpad) != caps)
714 gst_pad_set_caps (sel->srcpad, caps);
717 res = gst_pad_push (sel->srcpad, buf);
718 selpad->pushed = TRUE;
721 gst_object_unref (sel);
724 /* dropped buffers */
727 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
729 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
730 /* when we drop a buffer, we're creating a discont on this pad */
731 selpad->discont = TRUE;
732 GST_INPUT_SELECTOR_UNLOCK (sel);
733 gst_buffer_unref (buf);
735 /* figure out what to return upstream */
736 GST_OBJECT_LOCK (selpad);
737 if (selpad->always_ok || !active_pad_pushed)
740 res = GST_FLOW_NOT_LINKED;
741 GST_OBJECT_UNLOCK (selpad);
747 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
748 GST_INPUT_SELECTOR_UNLOCK (sel);
749 gst_buffer_unref (buf);
750 res = GST_FLOW_WRONG_STATE;
755 static void gst_input_selector_dispose (GObject * object);
757 static void gst_input_selector_set_property (GObject * object,
758 guint prop_id, const GValue * value, GParamSpec * pspec);
759 static void gst_input_selector_get_property (GObject * object,
760 guint prop_id, GValue * value, GParamSpec * pspec);
762 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
763 GstPadTemplate * templ, const gchar * unused);
764 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
766 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
767 element, GstStateChange transition);
769 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
770 static gboolean gst_input_selector_event (GstPad * pad, GstEvent * event);
771 static gboolean gst_input_selector_query (GstPad * pad, GstQuery * query);
772 static gint64 gst_input_selector_block (GstInputSelector * self);
773 static void gst_input_selector_switch (GstInputSelector * self,
774 GstPad * pad, gint64 stop_time, gint64 start_time);
776 /* FIXME: create these marshallers using glib-genmarshal */
777 #define g_marshal_value_peek_object(v) (v)->data[0].v_pointer
778 #define g_marshal_value_peek_int64(v) (v)->data[0].v_int64
781 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
782 GValue * return_value G_GNUC_UNUSED,
783 guint n_param_values,
784 const GValue * param_values,
785 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
787 typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
788 register GMarshalFunc_INT64__VOID callback;
789 register GCClosure *cc = (GCClosure *) closure;
790 register gpointer data1, data2;
793 g_return_if_fail (return_value != NULL);
794 g_return_if_fail (n_param_values == 1);
796 if (G_CCLOSURE_SWAP_DATA (closure)) {
797 data1 = closure->data;
798 data2 = g_value_peek_pointer (param_values + 0);
800 data1 = g_value_peek_pointer (param_values + 0);
801 data2 = closure->data;
804 (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
806 v_return = callback (data1, data2);
808 g_value_set_int64 (return_value, v_return);
812 gst_input_selector_marshal_VOID__OBJECT_INT64_INT64 (GClosure * closure,
813 GValue * return_value G_GNUC_UNUSED,
814 guint n_param_values,
815 const GValue * param_values,
816 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
818 typedef void (*GMarshalFunc_VOID__OBJECT_INT64_INT64) (gpointer data1,
819 gpointer arg_1, gint64 arg_2, gint64 arg_3, gpointer data2);
820 register GMarshalFunc_VOID__OBJECT_INT64_INT64 callback;
821 register GCClosure *cc = (GCClosure *) closure;
822 register gpointer data1, data2;
824 g_return_if_fail (n_param_values == 4);
826 if (G_CCLOSURE_SWAP_DATA (closure)) {
827 data1 = closure->data;
828 data2 = g_value_peek_pointer (param_values + 0);
830 data1 = g_value_peek_pointer (param_values + 0);
831 data2 = closure->data;
834 (GMarshalFunc_VOID__OBJECT_INT64_INT64) (marshal_data ? marshal_data :
838 g_marshal_value_peek_object (param_values + 1),
839 g_marshal_value_peek_int64 (param_values + 2),
840 g_marshal_value_peek_int64 (param_values + 3), data2);
843 #define _do_init(bla) \
844 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
845 "input-selector", 0, "An input stream selector element");
847 GST_BOILERPLATE_FULL (GstInputSelector, gst_input_selector, GstElement,
848 GST_TYPE_ELEMENT, _do_init);
851 gst_input_selector_base_init (gpointer g_class)
853 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
855 gst_element_class_set_details_simple (element_class, "Input selector",
856 "Generic", "N-to-1 input stream selector",
857 "Julien Moutte <julien@moutte.net>, "
858 "Jan Schmidt <thaytan@mad.scientist.com>, "
859 "Wim Taymans <wim.taymans@gmail.com>");
860 gst_element_class_add_pad_template (element_class,
861 gst_static_pad_template_get (&gst_input_selector_sink_factory));
862 gst_element_class_add_pad_template (element_class,
863 gst_static_pad_template_get (&gst_input_selector_src_factory));
867 gst_input_selector_class_init (GstInputSelectorClass * klass)
869 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
870 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
872 gobject_class->dispose = gst_input_selector_dispose;
874 gobject_class->set_property = gst_input_selector_set_property;
875 gobject_class->get_property = gst_input_selector_get_property;
877 g_object_class_install_property (gobject_class, PROP_N_PADS,
878 g_param_spec_uint ("n-pads", "Number of Pads",
879 "The number of sink pads", 0, G_MAXUINT, 0,
880 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
882 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
883 g_param_spec_object ("active-pad", "Active pad",
884 "The currently active sink pad", GST_TYPE_PAD,
885 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
888 * GstInputSelector::block:
889 * @inputselector: the #GstInputSelector
891 * Block all sink pads in preparation for a switch. Returns the stop time of
892 * the current switch segment, as a running time, or 0 if there is no current
893 * active pad or the current active pad never received data.
895 gst_input_selector_signals[SIGNAL_BLOCK] =
896 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
897 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
898 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
899 gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
901 * GstInputSelector::switch:
902 * @inputselector: the #GstInputSelector
903 * @pad: the pad to switch to
904 * @stop_time: running time at which to close the previous segment, or -1
905 * to use the running time of the previously active sink pad
906 * @start_time: running time at which to start the new segment, or -1 to
907 * use the running time of the newly active sink pad
909 * Switch to a new feed. The segment opened by the previously active pad, if
910 * any, will be closed, and a new segment opened before data flows again.
912 * This signal must be emitted when the element has been blocked via the <link
913 * linkend="GstInputSelector-block">block</link> signal.
915 * If you have a stream with only one switch element, such as an audio-only
916 * stream, a stream switch should be performed by first emitting the block
917 * signal, and then emitting the switch signal with -1 for the stop and start
920 * The intention of the @stop_time and @start_time arguments is to allow
921 * multiple switch elements to switch and maintain stream synchronization.
922 * When switching a stream with multiple feeds, you will need as many switch
923 * elements as you have feeds. For example, a feed with audio and video will
924 * have one switch element between the audio feeds and one for video.
926 * A switch over multiple switch elements should be performed as follows:
927 * First, emit the <link linkend="GstInputSelector-block">block</link>
928 * signal, collecting the returned values. The maximum running time returned
929 * by block should then be used as the time at which to close the previous
932 * Then, query the running times of the new audio and video pads that you will
933 * switch to. Naturally, these pads are on separate switch elements. Take the
934 * minimum running time for those streams and use it for the time at which to
935 * open the new segment.
937 * If @pad is the same as the current active pad, the element will cancel any
938 * previous block without adjusting segments.
941 * the signal changed from accepting the pad name to the pad object.
946 gst_input_selector_signals[SIGNAL_SWITCH] =
947 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
948 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
949 NULL, NULL, gst_input_selector_marshal_VOID__OBJECT_INT64_INT64,
950 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
952 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
953 gstelement_class->release_pad = gst_input_selector_release_pad;
954 gstelement_class->change_state = gst_input_selector_change_state;
956 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
957 /* note the underscore because switch is a keyword otherwise */
958 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
962 gst_input_selector_init (GstInputSelector * sel,
963 GstInputSelectorClass * g_class)
965 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
966 gst_pad_set_iterate_internal_links_function (sel->srcpad,
967 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
968 gst_pad_set_getcaps_function (sel->srcpad,
969 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
970 gst_pad_set_query_function (sel->srcpad,
971 GST_DEBUG_FUNCPTR (gst_input_selector_query));
972 gst_pad_set_event_function (sel->srcpad,
973 GST_DEBUG_FUNCPTR (gst_input_selector_event));
974 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
975 /* sinkpad management */
976 sel->active_sinkpad = NULL;
978 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
980 sel->lock = g_mutex_new ();
981 sel->cond = g_cond_new ();
982 sel->blocked = FALSE;
986 gst_input_selector_dispose (GObject * object)
988 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
990 if (sel->active_sinkpad) {
991 gst_object_unref (sel->active_sinkpad);
992 sel->active_sinkpad = NULL;
995 g_mutex_free (sel->lock);
999 g_cond_free (sel->cond);
1003 G_OBJECT_CLASS (parent_class)->dispose (object);
1006 /* Solve the following equation for B.timestamp, and set that as the segment
1008 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
1011 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
1013 if (running_time <= segment->accum)
1014 return segment->start;
1016 return (running_time - segment->accum) * segment->abs_rate + segment->start;
1020 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
1022 segment->stop = gst_segment_get_timestamp (segment, running_time);
1023 segment->last_stop = -1;
1027 gst_segment_set_start (GstSegment * segment, gint64 running_time)
1029 gint64 new_start, duration;
1031 new_start = gst_segment_get_timestamp (segment, running_time);
1033 /* this is the duration we skipped */
1034 duration = new_start - segment->start;
1035 /* add the duration to the accumulated segment time */
1036 segment->accum += duration;
1037 /* move position in the segment */
1038 segment->time += duration;
1039 segment->start += duration;
1042 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
1043 * active pad changed. */
1045 gst_input_selector_set_active_pad (GstInputSelector * self,
1046 GstPad * pad, gint64 stop_time, gint64 start_time)
1048 GstSelectorPad *old, *new;
1049 GstPad **active_pad_p;
1051 if (pad == self->active_sinkpad)
1054 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1055 new = GST_SELECTOR_PAD_CAST (pad);
1057 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
1058 GST_DEBUG_PAD_NAME (new));
1060 if (!GST_CLOCK_TIME_IS_VALID (stop_time) && old) {
1061 /* no stop time given, get the latest running_time on the active pad to
1062 * close and open the new segment */
1063 stop_time = start_time = gst_selector_pad_get_running_time (old);
1064 GST_DEBUG_OBJECT (self, "using start/stop of %" GST_TIME_FORMAT,
1065 GST_TIME_ARGS (start_time));
1068 if (old && old->active && !self->pending_close && stop_time >= 0) {
1069 /* schedule a last_stop update if one isn't already scheduled, and a
1070 segment has been pushed before. */
1071 memcpy (&self->segment, &old->segment, sizeof (self->segment));
1073 GST_DEBUG_OBJECT (self, "setting stop_time to %" GST_TIME_FORMAT,
1074 GST_TIME_ARGS (stop_time));
1075 gst_segment_set_stop (&self->segment, stop_time);
1076 self->pending_close = TRUE;
1079 old->pushed = FALSE;
1081 if (new && new->active && start_time >= 0) {
1082 GST_DEBUG_OBJECT (self, "setting start_time to %" GST_TIME_FORMAT,
1083 GST_TIME_ARGS (start_time));
1084 /* schedule a new segment push */
1085 gst_segment_set_start (&new->segment, start_time);
1086 new->segment_pending = TRUE;
1089 new->pushed = FALSE;
1091 active_pad_p = &self->active_sinkpad;
1092 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
1093 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
1094 self->active_sinkpad);
1100 gst_input_selector_set_property (GObject * object, guint prop_id,
1101 const GValue * value, GParamSpec * pspec)
1103 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1106 case PROP_ACTIVE_PAD:
1110 pad = g_value_get_object (value);
1112 GST_INPUT_SELECTOR_LOCK (sel);
1113 gst_input_selector_set_active_pad (sel, pad,
1114 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
1115 GST_INPUT_SELECTOR_UNLOCK (sel);
1119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1125 gst_input_selector_get_property (GObject * object, guint prop_id,
1126 GValue * value, GParamSpec * pspec)
1128 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
1132 GST_INPUT_SELECTOR_LOCK (object);
1133 g_value_set_uint (value, sel->n_pads);
1134 GST_INPUT_SELECTOR_UNLOCK (object);
1136 case PROP_ACTIVE_PAD:
1137 GST_INPUT_SELECTOR_LOCK (object);
1138 g_value_set_object (value, sel->active_sinkpad);
1139 GST_INPUT_SELECTOR_UNLOCK (object);
1142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1148 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
1150 GstInputSelector *sel;
1151 GstPad *otherpad = NULL;
1153 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1155 GST_INPUT_SELECTOR_LOCK (sel);
1156 if (pad == sel->srcpad)
1157 otherpad = sel->active_sinkpad;
1158 else if (pad == sel->active_sinkpad || !strict)
1159 otherpad = sel->srcpad;
1161 gst_object_ref (otherpad);
1162 GST_INPUT_SELECTOR_UNLOCK (sel);
1164 gst_object_unref (sel);
1170 gst_input_selector_event (GstPad * pad, GstEvent * event)
1172 gboolean res = FALSE;
1175 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1178 res = gst_pad_push_event (otherpad, event);
1180 gst_object_unref (otherpad);
1182 gst_event_unref (event);
1186 /* query on the srcpad. We override this function because by default it will
1187 * only forward the query to one random sinkpad */
1189 gst_input_selector_query (GstPad * pad, GstQuery * query)
1191 gboolean res = TRUE;
1192 GstInputSelector *sel;
1195 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
1197 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
1199 switch (GST_QUERY_TYPE (query)) {
1200 case GST_QUERY_LATENCY:
1203 GstClockTime resmin, resmax;
1210 /* assume FALSE, we become TRUE if one query succeeds */
1213 /* perform the query on all sinkpads and combine the results. We take the
1214 * max of min and the min of max for the result latency. */
1215 GST_INPUT_SELECTOR_LOCK (sel);
1216 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1217 walk = g_list_next (walk)) {
1218 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1220 if (gst_pad_peer_query (sinkpad, query)) {
1221 GstClockTime min, max;
1224 /* one query succeeded, we succeed too */
1227 gst_query_parse_latency (query, &live, &min, &max);
1229 GST_DEBUG_OBJECT (sinkpad,
1230 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1231 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1238 else if (max < resmax)
1240 if (reslive == FALSE)
1245 GST_INPUT_SELECTOR_UNLOCK (sel);
1247 gst_query_set_latency (query, reslive, resmin, resmax);
1249 GST_DEBUG_OBJECT (sel,
1250 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1251 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1259 res = gst_pad_peer_query (otherpad, query);
1263 gst_object_unref (otherpad);
1264 gst_object_unref (sel);
1270 gst_input_selector_getcaps (GstPad * pad)
1276 parent = gst_object_get_parent (GST_OBJECT (pad));
1278 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
1281 GST_DEBUG_OBJECT (pad, "Pad not linked, returning ANY");
1282 caps = gst_caps_new_any ();
1284 GST_DEBUG_OBJECT (pad, "Pad is linked (to %s:%s), returning peer caps",
1285 GST_DEBUG_PAD_NAME (otherpad));
1286 /* if the peer has caps, use those. If the pad is not linked, this function
1287 * returns NULL and we return ANY */
1288 if (!(caps = gst_pad_peer_get_caps_reffed (otherpad)))
1289 caps = gst_caps_new_any ();
1290 gst_object_unref (otherpad);
1293 gst_object_unref (parent);
1297 /* check if the pad is the active sinkpad */
1298 static inline gboolean
1299 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1303 GST_INPUT_SELECTOR_LOCK (sel);
1304 res = (pad == sel->active_sinkpad);
1305 GST_INPUT_SELECTOR_UNLOCK (sel);
1310 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1312 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1314 GstPad *active_sinkpad;
1315 GstSelectorPad *selpad;
1317 selpad = GST_SELECTOR_PAD_CAST (pad);
1319 selpad->active = TRUE;
1320 active_sinkpad = sel->active_sinkpad;
1321 if (active_sinkpad == NULL) {
1322 /* first pad we get activity on becomes the activated pad by default */
1323 if (sel->active_sinkpad)
1324 gst_object_unref (sel->active_sinkpad);
1325 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1326 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1329 return active_sinkpad;
1333 gst_input_selector_request_new_pad (GstElement * element,
1334 GstPadTemplate * templ, const gchar * unused)
1336 GstInputSelector *sel;
1338 GstPad *sinkpad = NULL;
1340 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1342 sel = GST_INPUT_SELECTOR (element);
1344 GST_INPUT_SELECTOR_LOCK (sel);
1346 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1347 name = g_strdup_printf ("sink%d", sel->padcount++);
1348 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1349 "name", name, "direction", templ->direction, "template", templ, NULL);
1354 gst_pad_set_event_function (sinkpad,
1355 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1356 gst_pad_set_getcaps_function (sinkpad,
1357 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
1358 gst_pad_set_acceptcaps_function (sinkpad,
1359 GST_DEBUG_FUNCPTR (gst_selector_pad_acceptcaps));
1360 gst_pad_set_chain_function (sinkpad,
1361 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1362 gst_pad_set_iterate_internal_links_function (sinkpad,
1363 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1364 gst_pad_set_bufferalloc_function (sinkpad,
1365 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
1367 gst_pad_set_active (sinkpad, TRUE);
1368 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1369 GST_INPUT_SELECTOR_UNLOCK (sel);
1375 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1377 GstInputSelector *sel;
1379 sel = GST_INPUT_SELECTOR (element);
1380 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1382 GST_INPUT_SELECTOR_LOCK (sel);
1383 /* if the pad was the active pad, makes us select a new one */
1384 if (sel->active_sinkpad == pad) {
1385 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1386 gst_object_unref (sel->active_sinkpad);
1387 sel->active_sinkpad = NULL;
1391 gst_pad_set_active (pad, FALSE);
1392 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1393 GST_INPUT_SELECTOR_UNLOCK (sel);
1397 gst_input_selector_reset (GstInputSelector * sel)
1401 GST_INPUT_SELECTOR_LOCK (sel);
1402 /* clear active pad */
1403 if (sel->active_sinkpad) {
1404 gst_object_unref (sel->active_sinkpad);
1405 sel->active_sinkpad = NULL;
1408 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
1409 sel->pending_close = FALSE;
1410 /* reset each of our sinkpads state */
1411 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1412 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1414 gst_selector_pad_reset (selpad);
1417 gst_tag_list_free (selpad->tags);
1418 selpad->tags = NULL;
1421 GST_INPUT_SELECTOR_UNLOCK (sel);
1424 static GstStateChangeReturn
1425 gst_input_selector_change_state (GstElement * element,
1426 GstStateChange transition)
1428 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1429 GstStateChangeReturn result;
1431 switch (transition) {
1432 case GST_STATE_CHANGE_READY_TO_PAUSED:
1433 GST_INPUT_SELECTOR_LOCK (self);
1434 self->blocked = FALSE;
1435 self->flushing = FALSE;
1436 GST_INPUT_SELECTOR_UNLOCK (self);
1438 case GST_STATE_CHANGE_PAUSED_TO_READY:
1439 /* first unlock before we call the parent state change function, which
1440 * tries to acquire the stream lock when going to ready. */
1441 GST_INPUT_SELECTOR_LOCK (self);
1442 self->blocked = FALSE;
1443 self->flushing = TRUE;
1444 GST_INPUT_SELECTOR_BROADCAST (self);
1445 GST_INPUT_SELECTOR_UNLOCK (self);
1451 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1453 switch (transition) {
1454 case GST_STATE_CHANGE_PAUSED_TO_READY:
1455 gst_input_selector_reset (self);
1465 gst_input_selector_block (GstInputSelector * self)
1468 GstSelectorPad *spad;
1470 GST_INPUT_SELECTOR_LOCK (self);
1473 GST_WARNING_OBJECT (self, "switch already blocked");
1475 self->blocked = TRUE;
1476 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1479 ret = gst_selector_pad_get_running_time (spad);
1481 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1483 GST_INPUT_SELECTOR_UNLOCK (self);
1488 /* stop_time and start_time are running times */
1490 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1491 gint64 stop_time, gint64 start_time)
1495 g_return_if_fail (self->blocked == TRUE);
1497 GST_INPUT_SELECTOR_LOCK (self);
1499 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1501 self->blocked = FALSE;
1502 GST_INPUT_SELECTOR_BROADCAST (self);
1503 GST_INPUT_SELECTOR_UNLOCK (self);
1506 NOTIFY_MUTEX_LOCK ();
1507 g_object_notify (G_OBJECT (self), "active-pad");
1508 NOTIFY_MUTEX_UNLOCK ();