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_static_rec_mutex_lock (¬ify_mutex)
73 #define NOTIFY_MUTEX_UNLOCK() g_static_rec_mutex_unlock (¬ify_mutex)
76 #define GST_INPUT_SELECTOR_GET_LOCK(sel) (((GstInputSelector*)(sel))->lock)
77 #define GST_INPUT_SELECTOR_GET_COND(sel) (((GstInputSelector*)(sel))->cond)
78 #define GST_INPUT_SELECTOR_LOCK(sel) (g_mutex_lock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
79 #define GST_INPUT_SELECTOR_UNLOCK(sel) (g_mutex_unlock (GST_INPUT_SELECTOR_GET_LOCK(sel)))
80 #define GST_INPUT_SELECTOR_WAIT(sel) (g_cond_wait (GST_INPUT_SELECTOR_GET_COND(sel), \
81 GST_INPUT_SELECTOR_GET_LOCK(sel)))
82 #define GST_INPUT_SELECTOR_BROADCAST(sel) (g_cond_broadcast (GST_INPUT_SELECTOR_GET_COND(sel)))
84 static GstStaticPadTemplate gst_input_selector_sink_factory =
85 GST_STATIC_PAD_TEMPLATE ("sink_%u",
90 static GstStaticPadTemplate gst_input_selector_src_factory =
91 GST_STATIC_PAD_TEMPLATE ("src",
104 #define DEFAULT_SYNC_STREAMS FALSE
106 #define DEFAULT_PAD_ALWAYS_OK TRUE
111 PROP_PAD_RUNNING_TIME,
124 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
126 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
128 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
130 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
131 GstPad * pad, gboolean strict);
133 #define GST_TYPE_SELECTOR_PAD \
134 (gst_selector_pad_get_type())
135 #define GST_SELECTOR_PAD(obj) \
136 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
137 #define GST_SELECTOR_PAD_CLASS(klass) \
138 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
139 #define GST_IS_SELECTOR_PAD(obj) \
140 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
141 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
142 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
143 #define GST_SELECTOR_PAD_CAST(obj) \
144 ((GstSelectorPad *)(obj))
146 typedef struct _GstSelectorPad GstSelectorPad;
147 typedef struct _GstSelectorPadClass GstSelectorPadClass;
149 struct _GstSelectorPad
153 gboolean active; /* when buffer have passed the pad */
154 gboolean pushed; /* when buffer was pushed downstream since activation */
155 gboolean eos; /* when EOS has been received */
156 gboolean eos_sent; /* when EOS was sent downstream */
157 gboolean discont; /* after switching we create a discont */
158 gboolean flushing; /* set after flush-start and before flush-stop */
160 GstTagList *tags; /* last tags received on the pad */
162 GstClockTime position; /* the current position in the segment */
163 GstSegment segment; /* the current segment on the pad */
164 guint32 segment_seqnum; /* sequence number of the current segment */
166 gboolean segment_pending;
169 struct _GstSelectorPadClass
174 GType gst_selector_pad_get_type (void);
175 static void gst_selector_pad_finalize (GObject * object);
176 static void gst_selector_pad_get_property (GObject * object,
177 guint prop_id, GValue * value, GParamSpec * pspec);
178 static void gst_selector_pad_set_property (GObject * object,
179 guint prop_id, const GValue * value, GParamSpec * pspec);
181 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
182 static void gst_selector_pad_reset (GstSelectorPad * pad);
183 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
185 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
187 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
189 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
192 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
195 gst_selector_pad_class_init (GstSelectorPadClass * klass)
197 GObjectClass *gobject_class;
199 gobject_class = (GObjectClass *) klass;
201 gobject_class->finalize = gst_selector_pad_finalize;
203 gobject_class->get_property = gst_selector_pad_get_property;
204 gobject_class->set_property = gst_selector_pad_set_property;
206 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
207 g_param_spec_int64 ("running-time", "Running time",
208 "Running time of stream on pad", 0, G_MAXINT64, 0,
209 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
211 g_param_spec_boxed ("tags", "Tags",
212 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
213 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
215 g_param_spec_boolean ("active", "Active",
216 "If the pad is currently active", FALSE,
217 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
218 /* FIXME: better property name? */
219 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
220 g_param_spec_boolean ("always-ok", "Always OK",
221 "Make an inactive pad return OK instead of NOT_LINKED",
222 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 gst_selector_pad_init (GstSelectorPad * pad)
228 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
229 gst_selector_pad_reset (pad);
233 gst_selector_pad_finalize (GObject * object)
237 pad = GST_SELECTOR_PAD_CAST (object);
240 gst_tag_list_free (pad->tags);
242 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
246 gst_selector_pad_set_property (GObject * object, guint prop_id,
247 const GValue * value, GParamSpec * pspec)
249 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
252 case PROP_PAD_ALWAYS_OK:
253 GST_OBJECT_LOCK (object);
254 spad->always_ok = g_value_get_boolean (value);
255 GST_OBJECT_UNLOCK (object);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 gst_selector_pad_get_property (GObject * object, guint prop_id,
265 GValue * value, GParamSpec * pspec)
267 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
270 case PROP_PAD_RUNNING_TIME:
271 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
274 GST_OBJECT_LOCK (object);
275 g_value_set_boxed (value, spad->tags);
276 GST_OBJECT_UNLOCK (object);
278 case PROP_PAD_ACTIVE:
280 GstInputSelector *sel;
282 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
283 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
284 GST_PAD_CAST (spad)));
285 gst_object_unref (sel);
288 case PROP_PAD_ALWAYS_OK:
289 GST_OBJECT_LOCK (object);
290 g_value_set_boolean (value, spad->always_ok);
291 GST_OBJECT_UNLOCK (object);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gst_selector_pad_get_running_time (GstSelectorPad * pad)
304 GST_OBJECT_LOCK (pad);
306 guint64 position = pad->position;
307 GstFormat format = pad->segment.format;
309 ret = gst_segment_to_running_time (&pad->segment, format, position);
311 GST_OBJECT_UNLOCK (pad);
313 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
314 GST_TIME_ARGS (ret));
320 gst_selector_pad_reset (GstSelectorPad * pad)
322 GST_OBJECT_LOCK (pad);
326 pad->eos_sent = FALSE;
327 pad->segment_pending = FALSE;
328 pad->discont = FALSE;
329 pad->flushing = FALSE;
330 pad->position = GST_CLOCK_TIME_NONE;
331 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
332 GST_OBJECT_UNLOCK (pad);
335 /* strictly get the linked pad from the sinkpad. If the pad is active we return
336 * the srcpad else we return NULL */
338 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
340 GstInputSelector *sel;
342 GstIterator *it = NULL;
345 sel = GST_INPUT_SELECTOR (parent);
347 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
349 g_value_init (&val, GST_TYPE_PAD);
350 g_value_set_object (&val, otherpad);
351 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
352 g_value_unset (&val);
353 gst_object_unref (otherpad);
360 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
364 GstInputSelector *sel;
365 GstSelectorPad *selpad;
366 GstPad *prev_active_sinkpad;
367 GstPad *active_sinkpad;
369 sel = GST_INPUT_SELECTOR (parent);
370 selpad = GST_SELECTOR_PAD_CAST (pad);
372 GST_INPUT_SELECTOR_LOCK (sel);
373 prev_active_sinkpad = sel->active_sinkpad;
374 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
376 /* only forward if we are dealing with the active sinkpad */
377 forward = (pad == active_sinkpad);
378 GST_INPUT_SELECTOR_UNLOCK (sel);
380 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
381 NOTIFY_MUTEX_LOCK ();
382 g_object_notify (G_OBJECT (sel), "active-pad");
383 NOTIFY_MUTEX_UNLOCK ();
386 switch (GST_EVENT_TYPE (event)) {
387 case GST_EVENT_FLUSH_START:
388 /* Unblock the pad if it's waiting */
389 GST_INPUT_SELECTOR_LOCK (sel);
390 selpad->flushing = TRUE;
391 GST_INPUT_SELECTOR_BROADCAST (sel);
392 GST_INPUT_SELECTOR_UNLOCK (sel);
394 case GST_EVENT_FLUSH_STOP:
395 GST_INPUT_SELECTOR_LOCK (sel);
396 gst_selector_pad_reset (selpad);
397 GST_INPUT_SELECTOR_UNLOCK (sel);
399 case GST_EVENT_SEGMENT:
401 GST_INPUT_SELECTOR_LOCK (sel);
402 GST_OBJECT_LOCK (selpad);
403 gst_event_copy_segment (event, &selpad->segment);
404 selpad->segment_seqnum = gst_event_get_seqnum (event);
406 /* Update the position */
407 if (selpad->position == GST_CLOCK_TIME_NONE
408 || selpad->segment.position > selpad->position) {
409 selpad->position = selpad->segment.position;
410 } else if (selpad->position != GST_CLOCK_TIME_NONE
411 && selpad->position > selpad->segment.position) {
412 selpad->segment.position = selpad->position;
415 gst_event_unref (event);
416 event = gst_event_new_segment (&selpad->segment);
417 gst_event_set_seqnum (event, selpad->segment_seqnum);
420 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
423 /* If we aren't forwarding the event because the pad is not the
424 * active_sinkpad, then set the flag on the pad
425 * that says a segment needs sending if/when that pad is activated.
426 * For all other cases, we send the event immediately, which makes
427 * sparse streams and other segment updates work correctly downstream.
430 selpad->segment_pending = TRUE;
432 GST_OBJECT_UNLOCK (selpad);
433 GST_INPUT_SELECTOR_UNLOCK (sel);
438 GstTagList *tags, *oldtags, *newtags;
440 gst_event_parse_tag (event, &tags);
442 GST_OBJECT_LOCK (selpad);
443 oldtags = selpad->tags;
445 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
446 selpad->tags = newtags;
448 gst_tag_list_free (oldtags);
449 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
450 GST_OBJECT_UNLOCK (selpad);
452 g_object_notify (G_OBJECT (selpad), "tags");
459 selpad->eos_sent = TRUE;
463 /* If the active sinkpad is in EOS state but EOS
464 * was not sent downstream this means that the pad
465 * got EOS before it was set as active pad and that
466 * the previously active pad got EOS after it was
469 GST_INPUT_SELECTOR_LOCK (sel);
470 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
471 tmp = GST_SELECTOR_PAD (active_sinkpad);
472 forward = (tmp->eos && !tmp->eos_sent);
473 tmp->eos_sent = TRUE;
474 GST_INPUT_SELECTOR_UNLOCK (sel);
476 GST_DEBUG_OBJECT (pad, "received EOS");
482 GST_DEBUG_OBJECT (pad, "forwarding event");
483 res = gst_pad_push_event (sel->srcpad, event);
485 gst_event_unref (event);
491 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
493 gboolean res = FALSE;
495 switch (GST_QUERY_TYPE (query)) {
497 res = gst_pad_query_default (pad, parent, query);
504 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
505 * or return TRUE when flushing */
507 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
509 while (self->blocked && !self->flushing && !pad->flushing) {
510 /* we can be unlocked here when we are shutting down (flushing) or when we
512 GST_INPUT_SELECTOR_WAIT (self);
514 return self->flushing;
517 /* must be called with the SELECTOR_LOCK, will block until the running time
518 * of the active pad is after this pad or return TRUE when flushing */
520 gst_input_selector_wait_running_time (GstInputSelector * sel,
521 GstSelectorPad * pad, GstBuffer * buf)
523 GstPad *active_sinkpad;
524 GstSelectorPad *active_selpad;
525 GstSegment *seg, *active_seg;
526 GstClockTime running_time, active_running_time = -1;
531 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
532 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
533 active_seg = &active_selpad->segment;
535 /* We can only sync if the segments are in time format or
536 * if the active pad had no newsegment event yet */
537 if (seg->format != GST_FORMAT_TIME ||
538 (active_seg->format != GST_FORMAT_TIME
539 && active_seg->format != GST_FORMAT_UNDEFINED))
542 /* If we have no valid timestamp we can't sync this buffer */
543 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf))
546 running_time = GST_BUFFER_TIMESTAMP (buf);
547 /* If possible try to get the running time at the end of the buffer */
548 if (GST_BUFFER_DURATION_IS_VALID (buf))
549 running_time += GST_BUFFER_DURATION (buf);
550 if (running_time > seg->stop)
551 running_time = seg->stop;
553 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
554 /* If this is outside the segment don't sync */
555 if (running_time == -1)
558 /* Get active pad's running time, if no configured segment yet keep at -1 */
559 if (active_seg->format == GST_FORMAT_TIME)
560 active_running_time =
561 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
562 active_selpad->position);
565 * a) this is the active pad
566 * b) the pad or the selector is flushing
567 * c) the selector is not blocked
568 * d) the active pad has no running time or the active
569 * pad's running time is before this running time
570 * e) the active pad has a non-time segment
572 while (pad != active_selpad && !sel->flushing && !pad->flushing &&
573 (sel->blocked || active_running_time == -1
574 || running_time >= active_running_time)) {
576 GST_DEBUG_OBJECT (pad,
577 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
578 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
579 GST_TIME_ARGS (active_running_time));
581 GST_INPUT_SELECTOR_WAIT (sel);
583 /* Get new active pad, it might have changed */
585 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
586 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
587 active_seg = &active_selpad->segment;
589 /* If the active segment is configured but not to time format
590 * we can't do any syncing at all */
591 if (active_seg->format != GST_FORMAT_TIME
592 && active_seg->format != GST_FORMAT_UNDEFINED)
595 /* Get the new active pad running time */
596 if (active_seg->format == GST_FORMAT_TIME)
597 active_running_time =
598 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
599 active_selpad->position);
601 active_running_time = -1;
604 GST_DEBUG_OBJECT (pad,
605 "Waited for active streams to advance. %" GST_TIME_FORMAT " >= %"
606 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
607 GST_TIME_ARGS (active_running_time));
611 /* Return TRUE if the selector or the pad is flushing */
612 return (sel->flushing || pad->flushing);
617 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
619 GstInputSelector *sel;
621 GstPad *active_sinkpad;
622 GstPad *prev_active_sinkpad;
623 GstSelectorPad *selpad;
624 GstClockTime start_time;
626 GstEvent *start_event = NULL;
628 sel = GST_INPUT_SELECTOR (parent);
629 selpad = GST_SELECTOR_PAD_CAST (pad);
630 seg = &selpad->segment;
632 GST_INPUT_SELECTOR_LOCK (sel);
633 /* wait or check for flushing */
634 if (gst_input_selector_wait (sel, selpad))
637 GST_LOG_OBJECT (pad, "getting active pad");
639 prev_active_sinkpad = sel->active_sinkpad;
640 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
642 /* In sync mode wait until the active pad has advanced
643 * after the running time of the current buffer */
644 if (sel->sync_streams && active_sinkpad != pad) {
645 if (gst_input_selector_wait_running_time (sel, selpad, buf))
649 /* Might have changed while waiting */
650 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
652 /* update the segment on the srcpad */
653 start_time = GST_BUFFER_TIMESTAMP (buf);
654 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
655 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
656 GST_TIME_ARGS (start_time));
657 if (GST_BUFFER_DURATION_IS_VALID (buf))
658 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
659 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
661 GST_OBJECT_LOCK (pad);
662 selpad->position = start_time;
663 GST_OBJECT_UNLOCK (pad);
666 /* Ignore buffers from pads except the selected one */
667 if (pad != active_sinkpad)
670 /* Tell all non-active pads that we advanced the running time */
671 if (sel->sync_streams)
672 GST_INPUT_SELECTOR_BROADCAST (sel);
674 /* if we have a pending segment, push it out now */
675 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
676 || selpad->segment_pending)) {
677 if (G_UNLIKELY (seg->format == GST_FORMAT_UNDEFINED)) {
678 GST_ERROR_OBJECT (pad, "Buffers arrived before NEWSEGMENT event");
680 GST_DEBUG_OBJECT (pad,
681 "pushing pending NEWSEGMENT update %d, rate %lf, applied rate %lf, "
682 "format %d, " "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
683 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
684 seg->start, seg->stop, seg->time);
686 start_event = gst_event_new_segment (seg);
687 gst_event_set_seqnum (start_event, selpad->segment_seqnum);
688 selpad->segment_pending = FALSE;
691 GST_INPUT_SELECTOR_UNLOCK (sel);
693 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
694 NOTIFY_MUTEX_LOCK ();
695 g_object_notify (G_OBJECT (sel), "active-pad");
696 NOTIFY_MUTEX_UNLOCK ();
700 gst_pad_push_event (sel->srcpad, start_event);
702 if (selpad->discont) {
703 buf = gst_buffer_make_writable (buf);
705 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
706 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
707 selpad->discont = FALSE;
711 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
713 res = gst_pad_push (sel->srcpad, buf);
714 selpad->pushed = TRUE;
719 /* dropped buffers */
722 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
724 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
725 /* when we drop a buffer, we're creating a discont on this pad */
726 selpad->discont = TRUE;
727 GST_INPUT_SELECTOR_UNLOCK (sel);
728 gst_buffer_unref (buf);
730 /* figure out what to return upstream */
731 GST_OBJECT_LOCK (selpad);
732 if (selpad->always_ok || !active_pad_pushed)
735 res = GST_FLOW_NOT_LINKED;
736 GST_OBJECT_UNLOCK (selpad);
742 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
743 GST_INPUT_SELECTOR_UNLOCK (sel);
744 gst_buffer_unref (buf);
745 res = GST_FLOW_WRONG_STATE;
750 static void gst_input_selector_dispose (GObject * object);
752 static void gst_input_selector_set_property (GObject * object,
753 guint prop_id, const GValue * value, GParamSpec * pspec);
754 static void gst_input_selector_get_property (GObject * object,
755 guint prop_id, GValue * value, GParamSpec * pspec);
757 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
758 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
759 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
761 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
762 element, GstStateChange transition);
764 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
766 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
768 static gint64 gst_input_selector_block (GstInputSelector * self);
770 /* FIXME: create these marshallers using glib-genmarshal */
772 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
773 GValue * return_value G_GNUC_UNUSED,
774 guint n_param_values,
775 const GValue * param_values,
776 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
778 typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
779 register GMarshalFunc_INT64__VOID callback;
780 register GCClosure *cc = (GCClosure *) closure;
781 register gpointer data1, data2;
784 g_return_if_fail (return_value != NULL);
785 g_return_if_fail (n_param_values == 1);
787 if (G_CCLOSURE_SWAP_DATA (closure)) {
788 data1 = closure->data;
789 data2 = g_value_peek_pointer (param_values + 0);
791 data1 = g_value_peek_pointer (param_values + 0);
792 data2 = closure->data;
795 (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
797 v_return = callback (data1, data2);
799 g_value_set_int64 (return_value, v_return);
803 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
804 "input-selector", 0, "An input stream selector element");
805 #define gst_input_selector_parent_class parent_class
806 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
810 gst_input_selector_class_init (GstInputSelectorClass * klass)
812 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
813 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
815 gobject_class->dispose = gst_input_selector_dispose;
817 gobject_class->set_property = gst_input_selector_set_property;
818 gobject_class->get_property = gst_input_selector_get_property;
820 g_object_class_install_property (gobject_class, PROP_N_PADS,
821 g_param_spec_uint ("n-pads", "Number of Pads",
822 "The number of sink pads", 0, G_MAXUINT, 0,
823 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
825 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
826 g_param_spec_object ("active-pad", "Active pad",
827 "The currently active sink pad", GST_TYPE_PAD,
828 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
831 * GstInputSelector:sync-streams
833 * If set to %TRUE all inactive streams will be synced to the
834 * running time of the active stream. This makes sure that no
835 * buffers are dropped by input-selector that might be needed
836 * when switching the active pad.
840 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
841 g_param_spec_boolean ("sync-streams", "Sync Streams",
842 "Synchronize inactive streams to the running time of the active stream",
843 DEFAULT_SYNC_STREAMS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
846 * GstInputSelector::block:
847 * @inputselector: the #GstInputSelector
849 * Block all sink pads in preparation for a switch. Returns the stop time of
850 * the current switch segment, as a running time, or 0 if there is no current
851 * active pad or the current active pad never received data.
853 gst_input_selector_signals[SIGNAL_BLOCK] =
854 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
855 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
856 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
857 gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
859 gst_element_class_set_details_simple (gstelement_class, "Input selector",
860 "Generic", "N-to-1 input stream selector",
861 "Julien Moutte <julien@moutte.net>, "
862 "Jan Schmidt <thaytan@mad.scientist.com>, "
863 "Wim Taymans <wim.taymans@gmail.com>");
864 gst_element_class_add_pad_template (gstelement_class,
865 gst_static_pad_template_get (&gst_input_selector_sink_factory));
866 gst_element_class_add_pad_template (gstelement_class,
867 gst_static_pad_template_get (&gst_input_selector_src_factory));
869 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
870 gstelement_class->release_pad = gst_input_selector_release_pad;
871 gstelement_class->change_state = gst_input_selector_change_state;
873 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
877 gst_input_selector_init (GstInputSelector * sel)
879 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
880 gst_pad_set_iterate_internal_links_function (sel->srcpad,
881 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
882 gst_pad_set_query_function (sel->srcpad,
883 GST_DEBUG_FUNCPTR (gst_input_selector_query));
884 gst_pad_set_event_function (sel->srcpad,
885 GST_DEBUG_FUNCPTR (gst_input_selector_event));
886 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
887 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
888 /* sinkpad management */
889 sel->active_sinkpad = NULL;
891 sel->sync_streams = DEFAULT_SYNC_STREAMS;
893 sel->lock = g_mutex_new ();
894 sel->cond = g_cond_new ();
895 sel->blocked = FALSE;
899 gst_input_selector_dispose (GObject * object)
901 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
903 if (sel->active_sinkpad) {
904 gst_object_unref (sel->active_sinkpad);
905 sel->active_sinkpad = NULL;
908 g_mutex_free (sel->lock);
912 g_cond_free (sel->cond);
916 G_OBJECT_CLASS (parent_class)->dispose (object);
919 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
920 * active pad changed. */
922 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
924 GstSelectorPad *old, *new;
925 GstPad **active_pad_p;
927 if (pad == self->active_sinkpad)
930 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
931 new = GST_SELECTOR_PAD_CAST (pad);
933 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
934 GST_DEBUG_PAD_NAME (new));
941 /* Send a new SEGMENT event on the new pad next */
942 if (old != new && new)
943 new->segment_pending = TRUE;
945 active_pad_p = &self->active_sinkpad;
946 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
948 /* Wake up all non-active pads in sync mode, they might be
949 * the active pad now */
950 if (self->sync_streams)
951 GST_INPUT_SELECTOR_BROADCAST (self);
953 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
954 self->active_sinkpad);
960 gst_input_selector_set_property (GObject * object, guint prop_id,
961 const GValue * value, GParamSpec * pspec)
963 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
966 case PROP_ACTIVE_PAD:
970 pad = g_value_get_object (value);
972 GST_INPUT_SELECTOR_LOCK (sel);
973 gst_input_selector_set_active_pad (sel, pad);
974 GST_INPUT_SELECTOR_UNLOCK (sel);
977 case PROP_SYNC_STREAMS:
979 GST_INPUT_SELECTOR_LOCK (sel);
980 sel->sync_streams = g_value_get_boolean (value);
981 GST_INPUT_SELECTOR_UNLOCK (sel);
985 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991 gst_input_selector_get_property (GObject * object, guint prop_id,
992 GValue * value, GParamSpec * pspec)
994 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
998 GST_INPUT_SELECTOR_LOCK (object);
999 g_value_set_uint (value, sel->n_pads);
1000 GST_INPUT_SELECTOR_UNLOCK (object);
1002 case PROP_ACTIVE_PAD:
1003 GST_INPUT_SELECTOR_LOCK (object);
1004 g_value_set_object (value, sel->active_sinkpad);
1005 GST_INPUT_SELECTOR_UNLOCK (object);
1007 case PROP_SYNC_STREAMS:
1008 GST_INPUT_SELECTOR_LOCK (object);
1009 g_value_set_boolean (value, sel->sync_streams);
1010 GST_INPUT_SELECTOR_UNLOCK (object);
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1019 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1022 GstPad *otherpad = NULL;
1024 GST_INPUT_SELECTOR_LOCK (sel);
1025 if (pad == sel->srcpad)
1026 otherpad = sel->active_sinkpad;
1027 else if (pad == sel->active_sinkpad || !strict)
1028 otherpad = sel->srcpad;
1030 gst_object_ref (otherpad);
1031 GST_INPUT_SELECTOR_UNLOCK (sel);
1037 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1039 GstInputSelector *sel;
1040 gboolean result = FALSE;
1042 gboolean done = FALSE;
1043 GValue item = { 0, };
1045 GList *pushed_pads = NULL;
1047 sel = GST_INPUT_SELECTOR (parent);
1048 /* Send upstream events to all sinkpads */
1049 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1051 /* This is now essentially a copy of gst_pad_event_default_dispatch
1052 * with a different iterator */
1054 switch (gst_iterator_next (iter, &item)) {
1055 case GST_ITERATOR_OK:
1056 eventpad = g_value_get_object (&item);
1058 /* if already pushed, skip */
1059 if (g_list_find (pushed_pads, eventpad)) {
1060 g_value_reset (&item);
1064 gst_event_ref (event);
1065 result |= gst_pad_push_event (eventpad, event);
1067 g_value_reset (&item);
1069 case GST_ITERATOR_RESYNC:
1070 /* We don't reset the result here because we don't push the event
1071 * again on pads that got the event already and because we need
1072 * to consider the result of the previous pushes */
1073 gst_iterator_resync (iter);
1075 case GST_ITERATOR_ERROR:
1076 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1079 case GST_ITERATOR_DONE:
1084 g_value_unset (&item);
1085 gst_iterator_free (iter);
1087 g_list_free (pushed_pads);
1089 gst_event_unref (event);
1094 /* query on the srcpad. We override this function because by default it will
1095 * only forward the query to one random sinkpad */
1097 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1099 gboolean res = FALSE;
1100 GstInputSelector *sel;
1102 sel = GST_INPUT_SELECTOR (parent);
1104 switch (GST_QUERY_TYPE (query)) {
1105 case GST_QUERY_LATENCY:
1108 GstClockTime resmin, resmax;
1115 /* perform the query on all sinkpads and combine the results. We take the
1116 * max of min and the min of max for the result latency. */
1117 GST_INPUT_SELECTOR_LOCK (sel);
1118 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1119 walk = g_list_next (walk)) {
1120 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1122 if (gst_pad_peer_query (sinkpad, query)) {
1123 GstClockTime min, max;
1126 /* one query succeeded, we succeed too */
1129 gst_query_parse_latency (query, &live, &min, &max);
1131 GST_DEBUG_OBJECT (sinkpad,
1132 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1133 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1140 else if (max < resmax)
1142 if (reslive == FALSE)
1147 GST_INPUT_SELECTOR_UNLOCK (sel);
1149 gst_query_set_latency (query, reslive, resmin, resmax);
1151 GST_DEBUG_OBJECT (sel,
1152 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1153 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1160 res = gst_pad_query_default (pad, parent, query);
1167 /* check if the pad is the active sinkpad */
1168 static inline gboolean
1169 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1173 GST_INPUT_SELECTOR_LOCK (sel);
1174 res = (pad == sel->active_sinkpad);
1175 GST_INPUT_SELECTOR_UNLOCK (sel);
1180 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1182 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1184 GstPad *active_sinkpad;
1185 GstSelectorPad *selpad;
1187 selpad = GST_SELECTOR_PAD_CAST (pad);
1189 selpad->active = TRUE;
1190 active_sinkpad = sel->active_sinkpad;
1191 if (active_sinkpad == NULL) {
1192 /* first pad we get activity on becomes the activated pad by default */
1193 if (sel->active_sinkpad)
1194 gst_object_unref (sel->active_sinkpad);
1195 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1196 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1199 return active_sinkpad;
1203 gst_input_selector_request_new_pad (GstElement * element,
1204 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1206 GstInputSelector *sel;
1208 GstPad *sinkpad = NULL;
1210 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1212 sel = GST_INPUT_SELECTOR (element);
1214 GST_INPUT_SELECTOR_LOCK (sel);
1216 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1217 name = g_strdup_printf ("sink_%u", sel->padcount++);
1218 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1219 "name", name, "direction", templ->direction, "template", templ, NULL);
1224 gst_pad_set_event_function (sinkpad,
1225 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1226 gst_pad_set_query_function (sinkpad,
1227 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1228 gst_pad_set_chain_function (sinkpad,
1229 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1230 gst_pad_set_iterate_internal_links_function (sinkpad,
1231 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1233 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1234 gst_pad_set_active (sinkpad, TRUE);
1235 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1236 GST_INPUT_SELECTOR_UNLOCK (sel);
1242 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1244 GstInputSelector *sel;
1246 sel = GST_INPUT_SELECTOR (element);
1247 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1249 GST_INPUT_SELECTOR_LOCK (sel);
1250 /* if the pad was the active pad, makes us select a new one */
1251 if (sel->active_sinkpad == pad) {
1252 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1253 gst_object_unref (sel->active_sinkpad);
1254 sel->active_sinkpad = NULL;
1258 gst_pad_set_active (pad, FALSE);
1259 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1260 GST_INPUT_SELECTOR_UNLOCK (sel);
1264 gst_input_selector_reset (GstInputSelector * sel)
1268 GST_INPUT_SELECTOR_LOCK (sel);
1269 /* clear active pad */
1270 if (sel->active_sinkpad) {
1271 gst_object_unref (sel->active_sinkpad);
1272 sel->active_sinkpad = NULL;
1274 /* reset each of our sinkpads state */
1275 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1276 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1278 gst_selector_pad_reset (selpad);
1281 gst_tag_list_free (selpad->tags);
1282 selpad->tags = NULL;
1285 GST_INPUT_SELECTOR_UNLOCK (sel);
1288 static GstStateChangeReturn
1289 gst_input_selector_change_state (GstElement * element,
1290 GstStateChange transition)
1292 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1293 GstStateChangeReturn result;
1295 switch (transition) {
1296 case GST_STATE_CHANGE_READY_TO_PAUSED:
1297 GST_INPUT_SELECTOR_LOCK (self);
1298 self->blocked = FALSE;
1299 self->flushing = FALSE;
1300 GST_INPUT_SELECTOR_UNLOCK (self);
1302 case GST_STATE_CHANGE_PAUSED_TO_READY:
1303 /* first unlock before we call the parent state change function, which
1304 * tries to acquire the stream lock when going to ready. */
1305 GST_INPUT_SELECTOR_LOCK (self);
1306 self->blocked = FALSE;
1307 self->flushing = TRUE;
1308 GST_INPUT_SELECTOR_BROADCAST (self);
1309 GST_INPUT_SELECTOR_UNLOCK (self);
1315 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1317 switch (transition) {
1318 case GST_STATE_CHANGE_PAUSED_TO_READY:
1319 gst_input_selector_reset (self);
1329 gst_input_selector_block (GstInputSelector * self)
1332 GstSelectorPad *spad;
1334 GST_INPUT_SELECTOR_LOCK (self);
1337 GST_WARNING_OBJECT (self, "switch already blocked");
1339 self->blocked = TRUE;
1340 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1343 ret = gst_selector_pad_get_running_time (spad);
1345 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1347 GST_INPUT_SELECTOR_UNLOCK (self);