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 #include "gst/glib-compat-private.h"
64 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
65 #define GST_CAT_DEFAULT input_selector_debug
67 #if GLIB_CHECK_VERSION(2, 26, 0)
68 #define NOTIFY_MUTEX_LOCK()
69 #define NOTIFY_MUTEX_UNLOCK()
71 static GStaticRecMutex notify_mutex = G_STATIC_REC_MUTEX_INIT;
72 #define NOTIFY_MUTEX_LOCK() g_rec_mutex_lock (¬ify_mutex)
73 #define NOTIFY_MUTEX_UNLOCK() g_rec_mutex_unlock (¬ify_mutex)
76 static GstStaticPadTemplate gst_input_selector_sink_factory =
77 GST_STATIC_PAD_TEMPLATE ("sink_%u",
82 static GstStaticPadTemplate gst_input_selector_src_factory =
83 GST_STATIC_PAD_TEMPLATE ("src",
96 #define DEFAULT_SYNC_STREAMS FALSE
98 #define DEFAULT_PAD_ALWAYS_OK TRUE
103 PROP_PAD_RUNNING_TIME,
116 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
118 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
120 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
122 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
123 GstPad * pad, gboolean strict);
125 #define GST_TYPE_SELECTOR_PAD \
126 (gst_selector_pad_get_type())
127 #define GST_SELECTOR_PAD(obj) \
128 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
129 #define GST_SELECTOR_PAD_CLASS(klass) \
130 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
131 #define GST_IS_SELECTOR_PAD(obj) \
132 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
133 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
134 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
135 #define GST_SELECTOR_PAD_CAST(obj) \
136 ((GstSelectorPad *)(obj))
138 typedef struct _GstSelectorPad GstSelectorPad;
139 typedef struct _GstSelectorPadClass GstSelectorPadClass;
141 struct _GstSelectorPad
145 gboolean active; /* when buffer have passed the pad */
146 gboolean pushed; /* when buffer was pushed downstream since activation */
147 gboolean eos; /* when EOS has been received */
148 gboolean eos_sent; /* when EOS was sent downstream */
149 gboolean discont; /* after switching we create a discont */
150 gboolean flushing; /* set after flush-start and before flush-stop */
152 GstTagList *tags; /* last tags received on the pad */
154 GstClockTime position; /* the current position in the segment */
155 GstSegment segment; /* the current segment on the pad */
156 guint32 segment_seqnum; /* sequence number of the current segment */
158 gboolean segment_pending;
161 struct _GstSelectorPadClass
166 GType gst_selector_pad_get_type (void);
167 static void gst_selector_pad_finalize (GObject * object);
168 static void gst_selector_pad_get_property (GObject * object,
169 guint prop_id, GValue * value, GParamSpec * pspec);
170 static void gst_selector_pad_set_property (GObject * object,
171 guint prop_id, const GValue * value, GParamSpec * pspec);
173 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
174 static void gst_selector_pad_reset (GstSelectorPad * pad);
175 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
177 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
179 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
181 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
184 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
187 gst_selector_pad_class_init (GstSelectorPadClass * klass)
189 GObjectClass *gobject_class;
191 gobject_class = (GObjectClass *) klass;
193 gobject_class->finalize = gst_selector_pad_finalize;
195 gobject_class->get_property = gst_selector_pad_get_property;
196 gobject_class->set_property = 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,
201 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
203 g_param_spec_boxed ("tags", "Tags",
204 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
205 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
207 g_param_spec_boolean ("active", "Active",
208 "If the pad is currently active", FALSE,
209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
210 /* FIXME: better property name? */
211 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
212 g_param_spec_boolean ("always-ok", "Always OK",
213 "Make an inactive pad return OK instead of NOT_LINKED",
214 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 gst_selector_pad_init (GstSelectorPad * pad)
220 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
221 gst_selector_pad_reset (pad);
225 gst_selector_pad_finalize (GObject * object)
229 pad = GST_SELECTOR_PAD_CAST (object);
232 gst_tag_list_free (pad->tags);
234 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
238 gst_selector_pad_set_property (GObject * object, guint prop_id,
239 const GValue * value, GParamSpec * pspec)
241 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
244 case PROP_PAD_ALWAYS_OK:
245 GST_OBJECT_LOCK (object);
246 spad->always_ok = g_value_get_boolean (value);
247 GST_OBJECT_UNLOCK (object);
250 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 gst_selector_pad_get_property (GObject * object, guint prop_id,
257 GValue * value, GParamSpec * pspec)
259 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
262 case PROP_PAD_RUNNING_TIME:
263 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
266 GST_OBJECT_LOCK (object);
267 g_value_set_boxed (value, spad->tags);
268 GST_OBJECT_UNLOCK (object);
270 case PROP_PAD_ACTIVE:
272 GstInputSelector *sel;
274 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
275 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
276 GST_PAD_CAST (spad)));
277 gst_object_unref (sel);
280 case PROP_PAD_ALWAYS_OK:
281 GST_OBJECT_LOCK (object);
282 g_value_set_boolean (value, spad->always_ok);
283 GST_OBJECT_UNLOCK (object);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 gst_selector_pad_get_running_time (GstSelectorPad * pad)
296 GST_OBJECT_LOCK (pad);
298 guint64 position = pad->position;
299 GstFormat format = pad->segment.format;
301 ret = gst_segment_to_running_time (&pad->segment, format, position);
303 GST_OBJECT_UNLOCK (pad);
305 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
306 GST_TIME_ARGS (ret));
312 gst_selector_pad_reset (GstSelectorPad * pad)
314 GST_OBJECT_LOCK (pad);
318 pad->eos_sent = FALSE;
319 pad->segment_pending = FALSE;
320 pad->discont = FALSE;
321 pad->flushing = FALSE;
322 pad->position = GST_CLOCK_TIME_NONE;
323 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
324 GST_OBJECT_UNLOCK (pad);
327 /* strictly get the linked pad from the sinkpad. If the pad is active we return
328 * the srcpad else we return NULL */
330 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
332 GstInputSelector *sel;
334 GstIterator *it = NULL;
337 sel = GST_INPUT_SELECTOR (parent);
339 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
341 g_value_init (&val, GST_TYPE_PAD);
342 g_value_set_object (&val, otherpad);
343 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
344 g_value_unset (&val);
345 gst_object_unref (otherpad);
352 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
356 GstInputSelector *sel;
357 GstSelectorPad *selpad;
358 GstPad *prev_active_sinkpad;
359 GstPad *active_sinkpad;
361 sel = GST_INPUT_SELECTOR (parent);
362 selpad = GST_SELECTOR_PAD_CAST (pad);
364 GST_INPUT_SELECTOR_LOCK (sel);
365 prev_active_sinkpad = sel->active_sinkpad;
366 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
368 /* only forward if we are dealing with the active sinkpad */
369 forward = (pad == active_sinkpad);
370 GST_INPUT_SELECTOR_UNLOCK (sel);
372 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
373 NOTIFY_MUTEX_LOCK ();
374 g_object_notify (G_OBJECT (sel), "active-pad");
375 NOTIFY_MUTEX_UNLOCK ();
378 switch (GST_EVENT_TYPE (event)) {
379 case GST_EVENT_FLUSH_START:
380 /* Unblock the pad if it's waiting */
381 GST_INPUT_SELECTOR_LOCK (sel);
382 selpad->flushing = TRUE;
383 GST_INPUT_SELECTOR_BROADCAST (sel);
384 GST_INPUT_SELECTOR_UNLOCK (sel);
386 case GST_EVENT_FLUSH_STOP:
387 GST_INPUT_SELECTOR_LOCK (sel);
388 gst_selector_pad_reset (selpad);
389 GST_INPUT_SELECTOR_UNLOCK (sel);
391 case GST_EVENT_SEGMENT:
393 GST_INPUT_SELECTOR_LOCK (sel);
394 GST_OBJECT_LOCK (selpad);
395 gst_event_copy_segment (event, &selpad->segment);
396 selpad->segment_seqnum = gst_event_get_seqnum (event);
398 /* Update the position */
399 if (selpad->position == GST_CLOCK_TIME_NONE
400 || selpad->segment.position > selpad->position) {
401 selpad->position = selpad->segment.position;
402 } else if (selpad->position != GST_CLOCK_TIME_NONE
403 && selpad->position > selpad->segment.position) {
404 selpad->segment.position = selpad->position;
407 gst_event_unref (event);
408 event = gst_event_new_segment (&selpad->segment);
409 gst_event_set_seqnum (event, selpad->segment_seqnum);
412 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
415 /* If we aren't forwarding the event because the pad is not the
416 * active_sinkpad, then set the flag on the pad
417 * that says a segment needs sending if/when that pad is activated.
418 * For all other cases, we send the event immediately, which makes
419 * sparse streams and other segment updates work correctly downstream.
422 selpad->segment_pending = TRUE;
424 GST_OBJECT_UNLOCK (selpad);
425 GST_INPUT_SELECTOR_UNLOCK (sel);
430 GstTagList *tags, *oldtags, *newtags;
432 gst_event_parse_tag (event, &tags);
434 GST_OBJECT_LOCK (selpad);
435 oldtags = selpad->tags;
437 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
438 selpad->tags = newtags;
440 gst_tag_list_free (oldtags);
441 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
442 GST_OBJECT_UNLOCK (selpad);
444 g_object_notify (G_OBJECT (selpad), "tags");
451 selpad->eos_sent = TRUE;
455 /* If the active sinkpad is in EOS state but EOS
456 * was not sent downstream this means that the pad
457 * got EOS before it was set as active pad and that
458 * the previously active pad got EOS after it was
461 GST_INPUT_SELECTOR_LOCK (sel);
462 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
463 tmp = GST_SELECTOR_PAD (active_sinkpad);
464 forward = (tmp->eos && !tmp->eos_sent);
465 tmp->eos_sent = TRUE;
466 GST_INPUT_SELECTOR_UNLOCK (sel);
468 GST_DEBUG_OBJECT (pad, "received EOS");
474 GST_DEBUG_OBJECT (pad, "forwarding event");
475 res = gst_pad_push_event (sel->srcpad, event);
477 gst_event_unref (event);
483 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
485 gboolean res = FALSE;
487 switch (GST_QUERY_TYPE (query)) {
489 res = gst_pad_query_default (pad, parent, query);
496 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
497 * or return TRUE when flushing */
499 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
501 while (self->blocked && !self->flushing && !pad->flushing) {
502 /* we can be unlocked here when we are shutting down (flushing) or when we
504 GST_INPUT_SELECTOR_WAIT (self);
506 return self->flushing;
509 /* must be called with the SELECTOR_LOCK, will block until the running time
510 * of the active pad is after this pad or return TRUE when flushing */
512 gst_input_selector_wait_running_time (GstInputSelector * sel,
513 GstSelectorPad * pad, GstBuffer * buf)
515 GstPad *active_sinkpad;
516 GstSelectorPad *active_selpad;
517 GstSegment *seg, *active_seg;
518 GstClockTime running_time, active_running_time = GST_CLOCK_TIME_NONE;
523 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
524 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
525 active_seg = &active_selpad->segment;
527 /* We can only sync if the segments are in time format or
528 * if the active pad had no newsegment event yet */
529 if (seg->format != GST_FORMAT_TIME ||
530 (active_seg->format != GST_FORMAT_TIME
531 && active_seg->format != GST_FORMAT_UNDEFINED))
534 /* If we have no valid timestamp we can't sync this buffer */
535 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf))
538 running_time = GST_BUFFER_TIMESTAMP (buf);
539 /* If possible try to get the running time at the end of the buffer */
540 if (GST_BUFFER_DURATION_IS_VALID (buf))
541 running_time += GST_BUFFER_DURATION (buf);
542 if (running_time > seg->stop)
543 running_time = seg->stop;
545 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
546 /* If this is outside the segment don't sync */
547 if (running_time == -1)
550 /* Get active pad's running time, if no configured segment yet keep at -1 */
551 if (active_seg->format == GST_FORMAT_TIME)
552 active_running_time =
553 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
554 active_selpad->position);
557 * a) this is the active pad
558 * b) the pad or the selector is flushing
559 * c) the selector is not blocked
560 * d) the active pad has no running time or the active
561 * pad's running time is before this running time
562 * e) the active pad has a non-time segment
564 while (pad != active_selpad && !sel->flushing && !pad->flushing &&
565 (sel->blocked || active_running_time == -1
566 || running_time >= active_running_time)) {
568 GST_DEBUG_OBJECT (pad,
569 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
570 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
571 GST_TIME_ARGS (active_running_time));
573 GST_INPUT_SELECTOR_WAIT (sel);
575 /* Get new active pad, it might have changed */
577 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
578 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
579 active_seg = &active_selpad->segment;
581 /* If the active segment is configured but not to time format
582 * we can't do any syncing at all */
583 if (active_seg->format != GST_FORMAT_TIME
584 && active_seg->format != GST_FORMAT_UNDEFINED)
587 /* Get the new active pad running time */
588 if (active_seg->format == GST_FORMAT_TIME)
589 active_running_time =
590 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
591 active_selpad->position);
593 active_running_time = -1;
596 GST_DEBUG_OBJECT (pad,
597 "Waited for active streams to advance. %" GST_TIME_FORMAT " >= %"
598 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
599 GST_TIME_ARGS (active_running_time));
603 /* Return TRUE if the selector or the pad is flushing */
604 return (sel->flushing || pad->flushing);
609 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
611 GstInputSelector *sel;
613 GstPad *active_sinkpad;
614 GstPad *prev_active_sinkpad;
615 GstSelectorPad *selpad;
616 GstClockTime start_time;
618 GstEvent *start_event = NULL;
620 sel = GST_INPUT_SELECTOR (parent);
621 selpad = GST_SELECTOR_PAD_CAST (pad);
622 seg = &selpad->segment;
624 GST_INPUT_SELECTOR_LOCK (sel);
625 /* wait or check for flushing */
626 if (gst_input_selector_wait (sel, selpad))
629 GST_LOG_OBJECT (pad, "getting active pad");
631 prev_active_sinkpad = sel->active_sinkpad;
632 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
634 /* In sync mode wait until the active pad has advanced
635 * after the running time of the current buffer */
636 if (sel->sync_streams && active_sinkpad != pad) {
637 if (gst_input_selector_wait_running_time (sel, selpad, buf))
641 /* Might have changed while waiting */
642 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
644 /* update the segment on the srcpad */
645 start_time = GST_BUFFER_TIMESTAMP (buf);
646 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
647 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
648 GST_TIME_ARGS (start_time));
649 if (GST_BUFFER_DURATION_IS_VALID (buf))
650 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
651 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
653 GST_OBJECT_LOCK (pad);
654 selpad->position = start_time;
655 GST_OBJECT_UNLOCK (pad);
658 /* Ignore buffers from pads except the selected one */
659 if (pad != active_sinkpad)
662 /* Tell all non-active pads that we advanced the running time */
663 if (sel->sync_streams)
664 GST_INPUT_SELECTOR_BROADCAST (sel);
666 /* if we have a pending segment, push it out now */
667 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
668 || selpad->segment_pending)) {
669 if (G_UNLIKELY (seg->format == GST_FORMAT_UNDEFINED)) {
670 GST_ERROR_OBJECT (pad, "Buffers arrived before NEWSEGMENT event");
672 GST_DEBUG_OBJECT (pad,
673 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
674 "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
675 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
676 seg->start, seg->stop, seg->time);
678 start_event = gst_event_new_segment (seg);
679 gst_event_set_seqnum (start_event, selpad->segment_seqnum);
680 selpad->segment_pending = FALSE;
683 GST_INPUT_SELECTOR_UNLOCK (sel);
685 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
686 NOTIFY_MUTEX_LOCK ();
687 g_object_notify (G_OBJECT (sel), "active-pad");
688 NOTIFY_MUTEX_UNLOCK ();
692 gst_pad_push_event (sel->srcpad, start_event);
694 if (selpad->discont) {
695 buf = gst_buffer_make_writable (buf);
697 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
698 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
699 selpad->discont = FALSE;
703 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
705 res = gst_pad_push (sel->srcpad, buf);
706 selpad->pushed = TRUE;
711 /* dropped buffers */
714 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
716 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
717 /* when we drop a buffer, we're creating a discont on this pad */
718 selpad->discont = TRUE;
719 GST_INPUT_SELECTOR_UNLOCK (sel);
720 gst_buffer_unref (buf);
722 /* figure out what to return upstream */
723 GST_OBJECT_LOCK (selpad);
724 if (selpad->always_ok || !active_pad_pushed)
727 res = GST_FLOW_NOT_LINKED;
728 GST_OBJECT_UNLOCK (selpad);
734 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
735 GST_INPUT_SELECTOR_UNLOCK (sel);
736 gst_buffer_unref (buf);
737 res = GST_FLOW_WRONG_STATE;
742 static void gst_input_selector_dispose (GObject * object);
743 static void gst_input_selector_finalize (GObject * object);
745 static void gst_input_selector_set_property (GObject * object,
746 guint prop_id, const GValue * value, GParamSpec * pspec);
747 static void gst_input_selector_get_property (GObject * object,
748 guint prop_id, GValue * value, GParamSpec * pspec);
750 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
751 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
752 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
754 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
755 element, GstStateChange transition);
757 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
759 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
761 static gint64 gst_input_selector_block (GstInputSelector * self);
763 /* FIXME: create these marshallers using glib-genmarshal */
765 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
766 GValue * return_value G_GNUC_UNUSED,
767 guint n_param_values,
768 const GValue * param_values,
769 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
771 typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
772 register GMarshalFunc_INT64__VOID callback;
773 register GCClosure *cc = (GCClosure *) closure;
774 register gpointer data1, data2;
777 g_return_if_fail (return_value != NULL);
778 g_return_if_fail (n_param_values == 1);
780 if (G_CCLOSURE_SWAP_DATA (closure)) {
781 data1 = closure->data;
782 data2 = g_value_peek_pointer (param_values + 0);
784 data1 = g_value_peek_pointer (param_values + 0);
785 data2 = closure->data;
788 (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
790 v_return = callback (data1, data2);
792 g_value_set_int64 (return_value, v_return);
796 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
797 "input-selector", 0, "An input stream selector element");
798 #define gst_input_selector_parent_class parent_class
799 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
803 gst_input_selector_class_init (GstInputSelectorClass * klass)
805 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
806 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
808 gobject_class->dispose = gst_input_selector_dispose;
809 gobject_class->finalize = gst_input_selector_finalize;
811 gobject_class->set_property = gst_input_selector_set_property;
812 gobject_class->get_property = gst_input_selector_get_property;
814 g_object_class_install_property (gobject_class, PROP_N_PADS,
815 g_param_spec_uint ("n-pads", "Number of Pads",
816 "The number of sink pads", 0, G_MAXUINT, 0,
817 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
819 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
820 g_param_spec_object ("active-pad", "Active pad",
821 "The currently active sink pad", GST_TYPE_PAD,
822 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
825 * GstInputSelector:sync-streams
827 * If set to %TRUE all inactive streams will be synced to the
828 * running time of the active stream. This makes sure that no
829 * buffers are dropped by input-selector that might be needed
830 * when switching the active pad.
834 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
835 g_param_spec_boolean ("sync-streams", "Sync Streams",
836 "Synchronize inactive streams to the running time of the active stream",
837 DEFAULT_SYNC_STREAMS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
840 * GstInputSelector::block:
841 * @inputselector: the #GstInputSelector
843 * Block all sink pads in preparation for a switch. Returns the stop time of
844 * the current switch segment, as a running time, or 0 if there is no current
845 * active pad or the current active pad never received data.
847 gst_input_selector_signals[SIGNAL_BLOCK] =
848 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
849 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
850 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
851 gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
853 gst_element_class_set_details_simple (gstelement_class, "Input selector",
854 "Generic", "N-to-1 input stream selector",
855 "Julien Moutte <julien@moutte.net>, "
856 "Jan Schmidt <thaytan@mad.scientist.com>, "
857 "Wim Taymans <wim.taymans@gmail.com>");
858 gst_element_class_add_pad_template (gstelement_class,
859 gst_static_pad_template_get (&gst_input_selector_sink_factory));
860 gst_element_class_add_pad_template (gstelement_class,
861 gst_static_pad_template_get (&gst_input_selector_src_factory));
863 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
864 gstelement_class->release_pad = gst_input_selector_release_pad;
865 gstelement_class->change_state = gst_input_selector_change_state;
867 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
871 gst_input_selector_init (GstInputSelector * sel)
873 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
874 gst_pad_set_iterate_internal_links_function (sel->srcpad,
875 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
876 gst_pad_set_query_function (sel->srcpad,
877 GST_DEBUG_FUNCPTR (gst_input_selector_query));
878 gst_pad_set_event_function (sel->srcpad,
879 GST_DEBUG_FUNCPTR (gst_input_selector_event));
880 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
881 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
882 /* sinkpad management */
883 sel->active_sinkpad = NULL;
885 sel->sync_streams = DEFAULT_SYNC_STREAMS;
887 g_mutex_init (&sel->lock);
888 g_cond_init (&sel->cond);
889 sel->blocked = FALSE;
893 gst_input_selector_dispose (GObject * object)
895 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
897 if (sel->active_sinkpad) {
898 gst_object_unref (sel->active_sinkpad);
899 sel->active_sinkpad = NULL;
901 G_OBJECT_CLASS (parent_class)->dispose (object);
905 gst_input_selector_finalize (GObject * object)
907 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
909 g_mutex_clear (&sel->lock);
910 g_cond_clear (&sel->cond);
912 G_OBJECT_CLASS (parent_class)->finalize (object);
915 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
916 * active pad changed. */
918 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
920 GstSelectorPad *old, *new;
921 GstPad **active_pad_p;
923 if (pad == self->active_sinkpad)
926 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
927 new = GST_SELECTOR_PAD_CAST (pad);
929 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
930 GST_DEBUG_PAD_NAME (new));
937 /* Send a new SEGMENT event on the new pad next */
938 if (old != new && new)
939 new->segment_pending = TRUE;
941 active_pad_p = &self->active_sinkpad;
942 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
944 /* Wake up all non-active pads in sync mode, they might be
945 * the active pad now */
946 if (self->sync_streams)
947 GST_INPUT_SELECTOR_BROADCAST (self);
949 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
950 self->active_sinkpad);
956 gst_input_selector_set_property (GObject * object, guint prop_id,
957 const GValue * value, GParamSpec * pspec)
959 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
962 case PROP_ACTIVE_PAD:
966 pad = g_value_get_object (value);
968 GST_INPUT_SELECTOR_LOCK (sel);
969 gst_input_selector_set_active_pad (sel, pad);
970 GST_INPUT_SELECTOR_UNLOCK (sel);
973 case PROP_SYNC_STREAMS:
975 GST_INPUT_SELECTOR_LOCK (sel);
976 sel->sync_streams = g_value_get_boolean (value);
977 GST_INPUT_SELECTOR_UNLOCK (sel);
981 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
987 gst_input_selector_get_property (GObject * object, guint prop_id,
988 GValue * value, GParamSpec * pspec)
990 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
994 GST_INPUT_SELECTOR_LOCK (object);
995 g_value_set_uint (value, sel->n_pads);
996 GST_INPUT_SELECTOR_UNLOCK (object);
998 case PROP_ACTIVE_PAD:
999 GST_INPUT_SELECTOR_LOCK (object);
1000 g_value_set_object (value, sel->active_sinkpad);
1001 GST_INPUT_SELECTOR_UNLOCK (object);
1003 case PROP_SYNC_STREAMS:
1004 GST_INPUT_SELECTOR_LOCK (object);
1005 g_value_set_boolean (value, sel->sync_streams);
1006 GST_INPUT_SELECTOR_UNLOCK (object);
1009 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1015 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1018 GstPad *otherpad = NULL;
1020 GST_INPUT_SELECTOR_LOCK (sel);
1021 if (pad == sel->srcpad)
1022 otherpad = sel->active_sinkpad;
1023 else if (pad == sel->active_sinkpad || !strict)
1024 otherpad = sel->srcpad;
1026 gst_object_ref (otherpad);
1027 GST_INPUT_SELECTOR_UNLOCK (sel);
1033 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1035 GstInputSelector *sel;
1036 gboolean result = FALSE;
1038 gboolean done = FALSE;
1039 GValue item = { 0, };
1041 GList *pushed_pads = NULL;
1043 sel = GST_INPUT_SELECTOR (parent);
1044 /* Send upstream events to all sinkpads */
1045 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1047 /* This is now essentially a copy of gst_pad_event_default_dispatch
1048 * with a different iterator */
1050 switch (gst_iterator_next (iter, &item)) {
1051 case GST_ITERATOR_OK:
1052 eventpad = g_value_get_object (&item);
1054 /* if already pushed, skip */
1055 if (g_list_find (pushed_pads, eventpad)) {
1056 g_value_reset (&item);
1060 gst_event_ref (event);
1061 result |= gst_pad_push_event (eventpad, event);
1063 g_value_reset (&item);
1065 case GST_ITERATOR_RESYNC:
1066 /* We don't reset the result here because we don't push the event
1067 * again on pads that got the event already and because we need
1068 * to consider the result of the previous pushes */
1069 gst_iterator_resync (iter);
1071 case GST_ITERATOR_ERROR:
1072 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1075 case GST_ITERATOR_DONE:
1080 g_value_unset (&item);
1081 gst_iterator_free (iter);
1083 g_list_free (pushed_pads);
1085 gst_event_unref (event);
1090 /* query on the srcpad. We override this function because by default it will
1091 * only forward the query to one random sinkpad */
1093 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1095 gboolean res = FALSE;
1096 GstInputSelector *sel;
1098 sel = GST_INPUT_SELECTOR (parent);
1100 switch (GST_QUERY_TYPE (query)) {
1101 case GST_QUERY_LATENCY:
1104 GstClockTime resmin, resmax;
1111 /* perform the query on all sinkpads and combine the results. We take the
1112 * max of min and the min of max for the result latency. */
1113 GST_INPUT_SELECTOR_LOCK (sel);
1114 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1115 walk = g_list_next (walk)) {
1116 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1118 if (gst_pad_peer_query (sinkpad, query)) {
1119 GstClockTime min, max;
1122 /* one query succeeded, we succeed too */
1125 gst_query_parse_latency (query, &live, &min, &max);
1127 GST_DEBUG_OBJECT (sinkpad,
1128 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1129 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1136 else if (max < resmax)
1138 if (reslive == FALSE)
1143 GST_INPUT_SELECTOR_UNLOCK (sel);
1145 gst_query_set_latency (query, reslive, resmin, resmax);
1147 GST_DEBUG_OBJECT (sel,
1148 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1149 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1156 res = gst_pad_query_default (pad, parent, query);
1163 /* check if the pad is the active sinkpad */
1164 static inline gboolean
1165 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1169 GST_INPUT_SELECTOR_LOCK (sel);
1170 res = (pad == sel->active_sinkpad);
1171 GST_INPUT_SELECTOR_UNLOCK (sel);
1176 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1178 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1180 GstPad *active_sinkpad;
1181 GstSelectorPad *selpad;
1183 selpad = GST_SELECTOR_PAD_CAST (pad);
1185 selpad->active = TRUE;
1186 active_sinkpad = sel->active_sinkpad;
1187 if (active_sinkpad == NULL) {
1188 /* first pad we get activity on becomes the activated pad by default */
1189 if (sel->active_sinkpad)
1190 gst_object_unref (sel->active_sinkpad);
1191 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1192 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1195 return active_sinkpad;
1199 gst_input_selector_request_new_pad (GstElement * element,
1200 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1202 GstInputSelector *sel;
1204 GstPad *sinkpad = NULL;
1206 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1208 sel = GST_INPUT_SELECTOR (element);
1210 GST_INPUT_SELECTOR_LOCK (sel);
1212 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1213 name = g_strdup_printf ("sink_%u", sel->padcount++);
1214 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1215 "name", name, "direction", templ->direction, "template", templ, NULL);
1220 gst_pad_set_event_function (sinkpad,
1221 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1222 gst_pad_set_query_function (sinkpad,
1223 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1224 gst_pad_set_chain_function (sinkpad,
1225 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1226 gst_pad_set_iterate_internal_links_function (sinkpad,
1227 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1229 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1230 gst_pad_set_active (sinkpad, TRUE);
1231 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1232 GST_INPUT_SELECTOR_UNLOCK (sel);
1238 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1240 GstInputSelector *sel;
1242 sel = GST_INPUT_SELECTOR (element);
1243 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1245 GST_INPUT_SELECTOR_LOCK (sel);
1246 /* if the pad was the active pad, makes us select a new one */
1247 if (sel->active_sinkpad == pad) {
1248 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1249 gst_object_unref (sel->active_sinkpad);
1250 sel->active_sinkpad = NULL;
1254 gst_pad_set_active (pad, FALSE);
1255 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1256 GST_INPUT_SELECTOR_UNLOCK (sel);
1260 gst_input_selector_reset (GstInputSelector * sel)
1264 GST_INPUT_SELECTOR_LOCK (sel);
1265 /* clear active pad */
1266 if (sel->active_sinkpad) {
1267 gst_object_unref (sel->active_sinkpad);
1268 sel->active_sinkpad = NULL;
1270 /* reset each of our sinkpads state */
1271 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1272 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1274 gst_selector_pad_reset (selpad);
1277 gst_tag_list_free (selpad->tags);
1278 selpad->tags = NULL;
1281 GST_INPUT_SELECTOR_UNLOCK (sel);
1284 static GstStateChangeReturn
1285 gst_input_selector_change_state (GstElement * element,
1286 GstStateChange transition)
1288 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1289 GstStateChangeReturn result;
1291 switch (transition) {
1292 case GST_STATE_CHANGE_READY_TO_PAUSED:
1293 GST_INPUT_SELECTOR_LOCK (self);
1294 self->blocked = FALSE;
1295 self->flushing = FALSE;
1296 GST_INPUT_SELECTOR_UNLOCK (self);
1298 case GST_STATE_CHANGE_PAUSED_TO_READY:
1299 /* first unlock before we call the parent state change function, which
1300 * tries to acquire the stream lock when going to ready. */
1301 GST_INPUT_SELECTOR_LOCK (self);
1302 self->blocked = FALSE;
1303 self->flushing = TRUE;
1304 GST_INPUT_SELECTOR_BROADCAST (self);
1305 GST_INPUT_SELECTOR_UNLOCK (self);
1311 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1313 switch (transition) {
1314 case GST_STATE_CHANGE_PAUSED_TO_READY:
1315 gst_input_selector_reset (self);
1325 gst_input_selector_block (GstInputSelector * self)
1328 GstSelectorPad *spad;
1330 GST_INPUT_SELECTOR_LOCK (self);
1333 GST_WARNING_OBJECT (self, "switch already blocked");
1335 self->blocked = TRUE;
1336 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1339 ret = gst_selector_pad_get_running_time (spad);
1341 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1343 GST_INPUT_SELECTOR_UNLOCK (self);