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 static GstStaticPadTemplate gst_input_selector_sink_factory =
68 GST_STATIC_PAD_TEMPLATE ("sink_%u",
73 static GstStaticPadTemplate gst_input_selector_src_factory =
74 GST_STATIC_PAD_TEMPLATE ("src",
87 #define DEFAULT_SYNC_STREAMS TRUE
89 #define DEFAULT_PAD_ALWAYS_OK TRUE
94 PROP_PAD_RUNNING_TIME,
107 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
109 static inline gboolean gst_input_selector_is_active_sinkpad (GstInputSelector *
111 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
113 static GstPad *gst_input_selector_get_linked_pad (GstInputSelector * sel,
114 GstPad * pad, gboolean strict);
116 #define GST_TYPE_SELECTOR_PAD \
117 (gst_selector_pad_get_type())
118 #define GST_SELECTOR_PAD(obj) \
119 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
120 #define GST_SELECTOR_PAD_CLASS(klass) \
121 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
122 #define GST_IS_SELECTOR_PAD(obj) \
123 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
124 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
125 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
126 #define GST_SELECTOR_PAD_CAST(obj) \
127 ((GstSelectorPad *)(obj))
129 typedef struct _GstSelectorPad GstSelectorPad;
130 typedef struct _GstSelectorPadClass GstSelectorPadClass;
132 struct _GstSelectorPad
136 gboolean active; /* when buffer have passed the pad */
137 gboolean pushed; /* when buffer was pushed downstream since activation */
138 gboolean eos; /* when EOS has been received */
139 gboolean eos_sent; /* when EOS was sent downstream */
140 gboolean discont; /* after switching we create a discont */
141 gboolean flushing; /* set after flush-start and before flush-stop */
143 GstTagList *tags; /* last tags received on the pad */
145 GstClockTime position; /* the current position in the segment */
146 GstSegment segment; /* the current segment on the pad */
147 guint32 segment_seqnum; /* sequence number of the current segment */
149 gboolean events_pending; /* TRUE if sticky events need to be updated */
152 struct _GstSelectorPadClass
157 GType gst_selector_pad_get_type (void);
158 static void gst_selector_pad_finalize (GObject * object);
159 static void gst_selector_pad_get_property (GObject * object,
160 guint prop_id, GValue * value, GParamSpec * pspec);
161 static void gst_selector_pad_set_property (GObject * object,
162 guint prop_id, const GValue * value, GParamSpec * pspec);
164 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
165 static void gst_selector_pad_reset (GstSelectorPad * pad);
166 static gboolean gst_selector_pad_event (GstPad * pad, GstObject * parent,
168 static gboolean gst_selector_pad_query (GstPad * pad, GstObject * parent,
170 static GstIterator *gst_selector_pad_iterate_linked_pads (GstPad * pad,
172 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstObject * parent,
175 G_DEFINE_TYPE (GstSelectorPad, gst_selector_pad, GST_TYPE_PAD);
178 gst_selector_pad_class_init (GstSelectorPadClass * klass)
180 GObjectClass *gobject_class;
182 gobject_class = (GObjectClass *) klass;
184 gobject_class->finalize = gst_selector_pad_finalize;
186 gobject_class->get_property = gst_selector_pad_get_property;
187 gobject_class->set_property = gst_selector_pad_set_property;
189 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
190 g_param_spec_int64 ("running-time", "Running time",
191 "Running time of stream on pad", 0, G_MAXINT64, 0,
192 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
194 g_param_spec_boxed ("tags", "Tags",
195 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
196 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
198 g_param_spec_boolean ("active", "Active",
199 "If the pad is currently active", FALSE,
200 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
201 /* FIXME: better property name? */
202 g_object_class_install_property (gobject_class, PROP_PAD_ALWAYS_OK,
203 g_param_spec_boolean ("always-ok", "Always OK",
204 "Make an inactive pad return OK instead of NOT_LINKED",
205 DEFAULT_PAD_ALWAYS_OK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 gst_selector_pad_init (GstSelectorPad * pad)
211 pad->always_ok = DEFAULT_PAD_ALWAYS_OK;
212 gst_selector_pad_reset (pad);
216 gst_selector_pad_finalize (GObject * object)
220 pad = GST_SELECTOR_PAD_CAST (object);
223 gst_tag_list_free (pad->tags);
225 G_OBJECT_CLASS (gst_selector_pad_parent_class)->finalize (object);
229 gst_selector_pad_set_property (GObject * object, guint prop_id,
230 const GValue * value, GParamSpec * pspec)
232 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
235 case PROP_PAD_ALWAYS_OK:
236 GST_OBJECT_LOCK (object);
237 spad->always_ok = g_value_get_boolean (value);
238 GST_OBJECT_UNLOCK (object);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gst_selector_pad_get_property (GObject * object, guint prop_id,
248 GValue * value, GParamSpec * pspec)
250 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
253 case PROP_PAD_RUNNING_TIME:
254 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
257 GST_OBJECT_LOCK (object);
258 g_value_set_boxed (value, spad->tags);
259 GST_OBJECT_UNLOCK (object);
261 case PROP_PAD_ACTIVE:
263 GstInputSelector *sel;
265 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
266 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
267 GST_PAD_CAST (spad)));
268 gst_object_unref (sel);
271 case PROP_PAD_ALWAYS_OK:
272 GST_OBJECT_LOCK (object);
273 g_value_set_boolean (value, spad->always_ok);
274 GST_OBJECT_UNLOCK (object);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gst_selector_pad_get_running_time (GstSelectorPad * pad)
287 GST_OBJECT_LOCK (pad);
289 guint64 position = pad->position;
290 GstFormat format = pad->segment.format;
292 ret = gst_segment_to_running_time (&pad->segment, format, position);
294 GST_OBJECT_UNLOCK (pad);
296 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
297 GST_TIME_ARGS (ret));
303 gst_selector_pad_reset (GstSelectorPad * pad)
305 GST_OBJECT_LOCK (pad);
309 pad->eos_sent = FALSE;
310 pad->events_pending = FALSE;
311 pad->discont = FALSE;
312 pad->flushing = FALSE;
313 pad->position = GST_CLOCK_TIME_NONE;
314 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
315 GST_OBJECT_UNLOCK (pad);
318 /* strictly get the linked pad from the sinkpad. If the pad is active we return
319 * the srcpad else we return NULL */
321 gst_selector_pad_iterate_linked_pads (GstPad * pad, GstObject * parent)
323 GstInputSelector *sel;
325 GstIterator *it = NULL;
328 sel = GST_INPUT_SELECTOR (parent);
330 otherpad = gst_input_selector_get_linked_pad (sel, pad, TRUE);
332 g_value_init (&val, GST_TYPE_PAD);
333 g_value_set_object (&val, otherpad);
334 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
335 g_value_unset (&val);
336 gst_object_unref (otherpad);
343 gst_selector_pad_event (GstPad * pad, GstObject * parent, GstEvent * event)
347 GstInputSelector *sel;
348 GstSelectorPad *selpad;
349 GstPad *prev_active_sinkpad;
350 GstPad *active_sinkpad;
352 sel = GST_INPUT_SELECTOR (parent);
353 selpad = GST_SELECTOR_PAD_CAST (pad);
355 GST_INPUT_SELECTOR_LOCK (sel);
356 prev_active_sinkpad = sel->active_sinkpad;
357 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
359 /* only forward if we are dealing with the active sinkpad */
360 forward = (pad == active_sinkpad);
361 GST_INPUT_SELECTOR_UNLOCK (sel);
363 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
364 g_object_notify (G_OBJECT (sel), "active-pad");
367 switch (GST_EVENT_TYPE (event)) {
368 case GST_EVENT_FLUSH_START:
369 /* Unblock the pad if it's waiting */
370 GST_INPUT_SELECTOR_LOCK (sel);
371 selpad->flushing = TRUE;
372 GST_INPUT_SELECTOR_BROADCAST (sel);
373 GST_INPUT_SELECTOR_UNLOCK (sel);
375 case GST_EVENT_FLUSH_STOP:
376 GST_INPUT_SELECTOR_LOCK (sel);
377 gst_selector_pad_reset (selpad);
378 GST_INPUT_SELECTOR_UNLOCK (sel);
380 case GST_EVENT_SEGMENT:
382 GST_INPUT_SELECTOR_LOCK (sel);
383 GST_OBJECT_LOCK (selpad);
384 gst_event_copy_segment (event, &selpad->segment);
385 selpad->segment_seqnum = gst_event_get_seqnum (event);
387 /* Update the position */
388 if (selpad->position == GST_CLOCK_TIME_NONE
389 || selpad->segment.position > selpad->position) {
390 selpad->position = selpad->segment.position;
391 } else if (selpad->position != GST_CLOCK_TIME_NONE
392 && selpad->position > selpad->segment.position) {
393 selpad->segment.position = selpad->position;
396 gst_event_unref (event);
397 event = gst_event_new_segment (&selpad->segment);
398 gst_event_set_seqnum (event, selpad->segment_seqnum);
401 GST_DEBUG_OBJECT (pad, "configured SEGMENT %" GST_SEGMENT_FORMAT,
404 GST_OBJECT_UNLOCK (selpad);
405 GST_INPUT_SELECTOR_UNLOCK (sel);
410 GstTagList *tags, *oldtags, *newtags;
412 gst_event_parse_tag (event, &tags);
414 GST_OBJECT_LOCK (selpad);
415 oldtags = selpad->tags;
417 newtags = gst_tag_list_merge (oldtags, tags, GST_TAG_MERGE_REPLACE);
418 selpad->tags = newtags;
420 gst_tag_list_free (oldtags);
421 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, newtags);
422 GST_OBJECT_UNLOCK (selpad);
424 g_object_notify (G_OBJECT (selpad), "tags");
431 selpad->eos_sent = TRUE;
435 /* If the active sinkpad is in EOS state but EOS
436 * was not sent downstream this means that the pad
437 * got EOS before it was set as active pad and that
438 * the previously active pad got EOS after it was
441 GST_INPUT_SELECTOR_LOCK (sel);
442 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
443 tmp = GST_SELECTOR_PAD (active_sinkpad);
444 forward = (tmp->eos && !tmp->eos_sent);
445 tmp->eos_sent = TRUE;
446 GST_INPUT_SELECTOR_UNLOCK (sel);
448 GST_DEBUG_OBJECT (pad, "received EOS");
454 GST_DEBUG_OBJECT (pad, "forwarding event");
455 res = gst_pad_push_event (sel->srcpad, event);
457 /* If we aren't forwarding the event because the pad is not the
458 * active_sinkpad, then set the flag on the pad
459 * that says a segment needs sending if/when that pad is activated.
460 * For all other cases, we send the event immediately, which makes
461 * sparse streams and other segment updates work correctly downstream.
463 if (GST_EVENT_IS_STICKY (event))
464 selpad->events_pending = TRUE;
465 gst_event_unref (event);
472 gst_selector_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
474 gboolean res = FALSE;
476 switch (GST_QUERY_TYPE (query)) {
478 res = gst_pad_query_default (pad, parent, query);
485 /* must be called with the SELECTOR_LOCK, will block while the pad is blocked
486 * or return TRUE when flushing */
488 gst_input_selector_wait (GstInputSelector * self, GstSelectorPad * pad)
490 while (self->blocked && !self->flushing && !pad->flushing) {
491 /* we can be unlocked here when we are shutting down (flushing) or when we
493 GST_INPUT_SELECTOR_WAIT (self);
495 return self->flushing;
498 /* must be called with the SELECTOR_LOCK, will block until the running time
499 * of the active pad is after this pad or return TRUE when flushing */
501 gst_input_selector_wait_running_time (GstInputSelector * sel,
502 GstSelectorPad * pad, GstBuffer * buf)
504 GstPad *active_sinkpad;
505 GstSelectorPad *active_selpad;
506 GstSegment *seg, *active_seg;
507 GstClockTime running_time, active_running_time = GST_CLOCK_TIME_NONE;
512 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
513 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
514 active_seg = &active_selpad->segment;
516 /* We can only sync if the segments are in time format or
517 * if the active pad had no newsegment event yet */
518 if (seg->format != GST_FORMAT_TIME ||
519 (active_seg->format != GST_FORMAT_TIME
520 && active_seg->format != GST_FORMAT_UNDEFINED))
523 /* If we have no valid timestamp we can't sync this buffer */
524 if (!GST_BUFFER_TIMESTAMP_IS_VALID (buf))
527 running_time = GST_BUFFER_TIMESTAMP (buf);
528 /* If possible try to get the running time at the end of the buffer */
529 if (GST_BUFFER_DURATION_IS_VALID (buf))
530 running_time += GST_BUFFER_DURATION (buf);
531 if (running_time > seg->stop)
532 running_time = seg->stop;
534 gst_segment_to_running_time (seg, GST_FORMAT_TIME, running_time);
535 /* If this is outside the segment don't sync */
536 if (running_time == -1)
539 /* Get active pad's running time, if no configured segment yet keep at -1 */
540 if (active_seg->format == GST_FORMAT_TIME)
541 active_running_time =
542 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
543 active_selpad->position);
546 * a) this is the active pad
547 * b) the pad or the selector is flushing
548 * c) the selector is not blocked
549 * d) the active pad has no running time or the active
550 * pad's running time is before this running time
551 * e) the active pad has a non-time segment
552 * f) the active pad changed and has not pushed anything
554 while (pad != active_selpad && !sel->flushing && !pad->flushing
555 && active_selpad->pushed && (sel->blocked || active_running_time == -1
556 || running_time >= active_running_time)) {
558 GST_DEBUG_OBJECT (pad,
559 "Waiting for active streams to advance. %" GST_TIME_FORMAT " >= %"
560 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
561 GST_TIME_ARGS (active_running_time));
563 GST_INPUT_SELECTOR_WAIT (sel);
565 /* Get new active pad, it might have changed */
567 gst_input_selector_activate_sinkpad (sel, GST_PAD_CAST (pad));
568 active_selpad = GST_SELECTOR_PAD_CAST (active_sinkpad);
569 active_seg = &active_selpad->segment;
571 /* If the active segment is configured but not to time format
572 * we can't do any syncing at all */
573 if (active_seg->format != GST_FORMAT_TIME
574 && active_seg->format != GST_FORMAT_UNDEFINED)
577 /* Get the new active pad running time */
578 if (active_seg->format == GST_FORMAT_TIME)
579 active_running_time =
580 gst_segment_to_running_time (active_seg, GST_FORMAT_TIME,
581 active_selpad->position);
583 active_running_time = -1;
586 GST_DEBUG_OBJECT (pad,
587 "Waited for active streams to advance. %" GST_TIME_FORMAT " >= %"
588 GST_TIME_FORMAT, GST_TIME_ARGS (running_time),
589 GST_TIME_ARGS (active_running_time));
593 /* Return TRUE if the selector or the pad is flushing */
594 return (sel->flushing || pad->flushing);
598 forward_sticky_events (GstPad * sinkpad, GstEvent ** event, gpointer user_data)
600 GstInputSelector *sel = GST_INPUT_SELECTOR (user_data);
602 if (GST_EVENT_TYPE (*event) == GST_EVENT_SEGMENT) {
603 GstSegment *seg = &GST_SELECTOR_PAD (sinkpad)->segment;
605 gst_pad_push_event (sel->srcpad, gst_event_new_segment (seg));
607 gst_pad_push_event (sel->srcpad, gst_event_ref (*event));
614 gst_selector_pad_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
616 GstInputSelector *sel;
618 GstPad *active_sinkpad;
619 GstPad *prev_active_sinkpad;
620 GstSelectorPad *selpad;
621 GstClockTime start_time;
623 sel = GST_INPUT_SELECTOR (parent);
624 selpad = GST_SELECTOR_PAD_CAST (pad);
626 GST_INPUT_SELECTOR_LOCK (sel);
627 /* wait or check for flushing */
628 if (gst_input_selector_wait (sel, selpad))
631 GST_LOG_OBJECT (pad, "getting active pad");
633 prev_active_sinkpad = sel->active_sinkpad;
634 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
636 /* In sync mode wait until the active pad has advanced
637 * after the running time of the current buffer */
638 if (sel->sync_streams && active_sinkpad != pad) {
639 if (gst_input_selector_wait_running_time (sel, selpad, buf))
643 /* Might have changed while waiting */
644 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
646 /* update the segment on the srcpad */
647 start_time = GST_BUFFER_TIMESTAMP (buf);
648 if (GST_CLOCK_TIME_IS_VALID (start_time)) {
649 GST_LOG_OBJECT (pad, "received start time %" GST_TIME_FORMAT,
650 GST_TIME_ARGS (start_time));
651 if (GST_BUFFER_DURATION_IS_VALID (buf))
652 GST_LOG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
653 GST_TIME_ARGS (start_time + GST_BUFFER_DURATION (buf)));
655 GST_OBJECT_LOCK (pad);
656 selpad->position = start_time;
657 selpad->segment.position = start_time;
658 GST_OBJECT_UNLOCK (pad);
661 /* Ignore buffers from pads except the selected one */
662 if (pad != active_sinkpad)
665 /* Tell all non-active pads that we advanced the running time */
666 if (sel->sync_streams)
667 GST_INPUT_SELECTOR_BROADCAST (sel);
669 GST_INPUT_SELECTOR_UNLOCK (sel);
671 if (prev_active_sinkpad != active_sinkpad && pad == active_sinkpad) {
672 g_object_notify (G_OBJECT (sel), "active-pad");
675 /* if we have a pending events, push them now */
676 if (G_UNLIKELY (prev_active_sinkpad != active_sinkpad
677 || selpad->events_pending)) {
678 gst_pad_sticky_events_foreach (GST_PAD_CAST (selpad), forward_sticky_events,
680 selpad->events_pending = FALSE;
683 if (selpad->discont) {
684 buf = gst_buffer_make_writable (buf);
686 GST_DEBUG_OBJECT (pad, "Marking discont buffer %p", buf);
687 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
688 selpad->discont = FALSE;
692 GST_LOG_OBJECT (pad, "Forwarding buffer %p", buf);
694 res = gst_pad_push (sel->srcpad, buf);
695 selpad->pushed = TRUE;
700 /* dropped buffers */
703 gboolean active_pad_pushed = GST_SELECTOR_PAD_CAST (active_sinkpad)->pushed;
705 GST_DEBUG_OBJECT (pad, "Pad not active, discard buffer %p", buf);
706 /* when we drop a buffer, we're creating a discont on this pad */
707 selpad->discont = TRUE;
708 GST_INPUT_SELECTOR_UNLOCK (sel);
709 gst_buffer_unref (buf);
711 /* figure out what to return upstream */
712 GST_OBJECT_LOCK (selpad);
713 if (selpad->always_ok || !active_pad_pushed)
716 res = GST_FLOW_NOT_LINKED;
717 GST_OBJECT_UNLOCK (selpad);
723 GST_DEBUG_OBJECT (pad, "We are flushing, discard buffer %p", buf);
724 GST_INPUT_SELECTOR_UNLOCK (sel);
725 gst_buffer_unref (buf);
726 res = GST_FLOW_FLUSHING;
731 static void gst_input_selector_dispose (GObject * object);
732 static void gst_input_selector_finalize (GObject * object);
734 static void gst_input_selector_set_property (GObject * object,
735 guint prop_id, const GValue * value, GParamSpec * pspec);
736 static void gst_input_selector_get_property (GObject * object,
737 guint prop_id, GValue * value, GParamSpec * pspec);
739 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
740 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps);
741 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
743 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
744 element, GstStateChange transition);
746 static gboolean gst_input_selector_event (GstPad * pad, GstObject * parent,
748 static gboolean gst_input_selector_query (GstPad * pad, GstObject * parent,
750 static gint64 gst_input_selector_block (GstInputSelector * self);
752 /* FIXME: create these marshallers using glib-genmarshal */
754 gst_input_selector_marshal_INT64__VOID (GClosure * closure,
755 GValue * return_value G_GNUC_UNUSED,
756 guint n_param_values,
757 const GValue * param_values,
758 gpointer invocation_hint G_GNUC_UNUSED, gpointer marshal_data)
760 typedef gint64 (*GMarshalFunc_INT64__VOID) (gpointer data1, gpointer data2);
761 register GMarshalFunc_INT64__VOID callback;
762 register GCClosure *cc = (GCClosure *) closure;
763 register gpointer data1, data2;
766 g_return_if_fail (return_value != NULL);
767 g_return_if_fail (n_param_values == 1);
769 if (G_CCLOSURE_SWAP_DATA (closure)) {
770 data1 = closure->data;
771 data2 = g_value_peek_pointer (param_values + 0);
773 data1 = g_value_peek_pointer (param_values + 0);
774 data2 = closure->data;
777 (GMarshalFunc_INT64__VOID) (marshal_data ? marshal_data : cc->callback);
779 v_return = callback (data1, data2);
781 g_value_set_int64 (return_value, v_return);
785 GST_DEBUG_CATEGORY_INIT (input_selector_debug, \
786 "input-selector", 0, "An input stream selector element");
787 #define gst_input_selector_parent_class parent_class
788 G_DEFINE_TYPE_WITH_CODE (GstInputSelector, gst_input_selector, GST_TYPE_ELEMENT,
792 gst_input_selector_class_init (GstInputSelectorClass * klass)
794 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
795 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
797 gobject_class->dispose = gst_input_selector_dispose;
798 gobject_class->finalize = gst_input_selector_finalize;
800 gobject_class->set_property = gst_input_selector_set_property;
801 gobject_class->get_property = gst_input_selector_get_property;
803 g_object_class_install_property (gobject_class, PROP_N_PADS,
804 g_param_spec_uint ("n-pads", "Number of Pads",
805 "The number of sink pads", 0, G_MAXUINT, 0,
806 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
808 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
809 g_param_spec_object ("active-pad", "Active pad",
810 "The currently active sink pad", GST_TYPE_PAD,
811 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814 * GstInputSelector:sync-streams
816 * If set to %TRUE all inactive streams will be synced to the
817 * running time of the active stream. This makes sure that no
818 * buffers are dropped by input-selector that might be needed
819 * when switching the active pad.
823 g_object_class_install_property (gobject_class, PROP_SYNC_STREAMS,
824 g_param_spec_boolean ("sync-streams", "Sync Streams",
825 "Synchronize inactive streams to the running time of the active stream",
826 DEFAULT_SYNC_STREAMS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
829 * GstInputSelector::block:
830 * @inputselector: the #GstInputSelector
832 * Block all sink pads in preparation for a switch. Returns the stop time of
833 * the current switch segment, as a running time, or 0 if there is no current
834 * active pad or the current active pad never received data.
836 gst_input_selector_signals[SIGNAL_BLOCK] =
837 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
838 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
839 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
840 gst_input_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
842 gst_element_class_set_static_metadata (gstelement_class, "Input selector",
843 "Generic", "N-to-1 input stream selector",
844 "Julien Moutte <julien@moutte.net>, "
845 "Jan Schmidt <thaytan@mad.scientist.com>, "
846 "Wim Taymans <wim.taymans@gmail.com>");
847 gst_element_class_add_pad_template (gstelement_class,
848 gst_static_pad_template_get (&gst_input_selector_sink_factory));
849 gst_element_class_add_pad_template (gstelement_class,
850 gst_static_pad_template_get (&gst_input_selector_src_factory));
852 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
853 gstelement_class->release_pad = gst_input_selector_release_pad;
854 gstelement_class->change_state = gst_input_selector_change_state;
856 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
860 gst_input_selector_init (GstInputSelector * sel)
862 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
863 gst_pad_set_iterate_internal_links_function (sel->srcpad,
864 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
865 gst_pad_set_query_function (sel->srcpad,
866 GST_DEBUG_FUNCPTR (gst_input_selector_query));
867 gst_pad_set_event_function (sel->srcpad,
868 GST_DEBUG_FUNCPTR (gst_input_selector_event));
869 GST_OBJECT_FLAG_SET (sel->srcpad, GST_PAD_FLAG_PROXY_CAPS);
870 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
871 /* sinkpad management */
872 sel->active_sinkpad = NULL;
874 sel->sync_streams = DEFAULT_SYNC_STREAMS;
876 g_mutex_init (&sel->lock);
877 g_cond_init (&sel->cond);
878 sel->blocked = FALSE;
882 gst_input_selector_dispose (GObject * object)
884 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
886 if (sel->active_sinkpad) {
887 gst_object_unref (sel->active_sinkpad);
888 sel->active_sinkpad = NULL;
890 G_OBJECT_CLASS (parent_class)->dispose (object);
894 gst_input_selector_finalize (GObject * object)
896 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
898 g_mutex_clear (&sel->lock);
899 g_cond_clear (&sel->cond);
901 G_OBJECT_CLASS (parent_class)->finalize (object);
904 /* this function must be called with the SELECTOR_LOCK. It returns TRUE when the
905 * active pad changed. */
907 gst_input_selector_set_active_pad (GstInputSelector * self, GstPad * pad)
909 GstSelectorPad *old, *new;
910 GstPad **active_pad_p;
912 if (pad == self->active_sinkpad)
915 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
916 new = GST_SELECTOR_PAD_CAST (pad);
918 GST_DEBUG_OBJECT (self, "setting active pad to %s:%s",
919 GST_DEBUG_PAD_NAME (new));
926 /* Send a new SEGMENT event on the new pad next */
927 if (old != new && new)
928 new->events_pending = TRUE;
930 active_pad_p = &self->active_sinkpad;
931 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
933 gst_pad_push_event (pad, gst_event_new_reconfigure ());
935 /* Wake up all non-active pads in sync mode, they might be
936 * the active pad now */
937 if (self->sync_streams)
938 GST_INPUT_SELECTOR_BROADCAST (self);
940 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
941 self->active_sinkpad);
947 gst_input_selector_set_property (GObject * object, guint prop_id,
948 const GValue * value, GParamSpec * pspec)
950 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
953 case PROP_ACTIVE_PAD:
957 pad = g_value_get_object (value);
959 GST_INPUT_SELECTOR_LOCK (sel);
960 gst_input_selector_set_active_pad (sel, pad);
961 GST_INPUT_SELECTOR_UNLOCK (sel);
964 case PROP_SYNC_STREAMS:
966 GST_INPUT_SELECTOR_LOCK (sel);
967 sel->sync_streams = g_value_get_boolean (value);
968 GST_INPUT_SELECTOR_UNLOCK (sel);
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 gst_input_selector_get_property (GObject * object, guint prop_id,
979 GValue * value, GParamSpec * pspec)
981 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
985 GST_INPUT_SELECTOR_LOCK (object);
986 g_value_set_uint (value, sel->n_pads);
987 GST_INPUT_SELECTOR_UNLOCK (object);
989 case PROP_ACTIVE_PAD:
990 GST_INPUT_SELECTOR_LOCK (object);
991 g_value_set_object (value, sel->active_sinkpad);
992 GST_INPUT_SELECTOR_UNLOCK (object);
994 case PROP_SYNC_STREAMS:
995 GST_INPUT_SELECTOR_LOCK (object);
996 g_value_set_boolean (value, sel->sync_streams);
997 GST_INPUT_SELECTOR_UNLOCK (object);
1000 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1006 gst_input_selector_get_linked_pad (GstInputSelector * sel, GstPad * pad,
1009 GstPad *otherpad = NULL;
1011 GST_INPUT_SELECTOR_LOCK (sel);
1012 if (pad == sel->srcpad)
1013 otherpad = sel->active_sinkpad;
1014 else if (pad == sel->active_sinkpad || !strict)
1015 otherpad = sel->srcpad;
1017 gst_object_ref (otherpad);
1018 GST_INPUT_SELECTOR_UNLOCK (sel);
1024 gst_input_selector_event (GstPad * pad, GstObject * parent, GstEvent * event)
1026 GstInputSelector *sel;
1027 gboolean result = FALSE;
1029 gboolean done = FALSE;
1030 GValue item = { 0, };
1032 GList *pushed_pads = NULL;
1034 sel = GST_INPUT_SELECTOR (parent);
1035 /* Send upstream events to all sinkpads */
1036 iter = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (sel));
1038 /* This is now essentially a copy of gst_pad_event_default_dispatch
1039 * with a different iterator */
1041 switch (gst_iterator_next (iter, &item)) {
1042 case GST_ITERATOR_OK:
1043 eventpad = g_value_get_object (&item);
1045 /* if already pushed, skip */
1046 if (g_list_find (pushed_pads, eventpad)) {
1047 g_value_reset (&item);
1051 gst_event_ref (event);
1052 result |= gst_pad_push_event (eventpad, event);
1054 g_value_reset (&item);
1056 case GST_ITERATOR_RESYNC:
1057 /* We don't reset the result here because we don't push the event
1058 * again on pads that got the event already and because we need
1059 * to consider the result of the previous pushes */
1060 gst_iterator_resync (iter);
1062 case GST_ITERATOR_ERROR:
1063 GST_ERROR_OBJECT (pad, "Could not iterate over sinkpads");
1066 case GST_ITERATOR_DONE:
1071 g_value_unset (&item);
1072 gst_iterator_free (iter);
1074 g_list_free (pushed_pads);
1076 gst_event_unref (event);
1081 /* query on the srcpad. We override this function because by default it will
1082 * only forward the query to one random sinkpad */
1084 gst_input_selector_query (GstPad * pad, GstObject * parent, GstQuery * query)
1086 gboolean res = FALSE;
1087 GstInputSelector *sel;
1089 sel = GST_INPUT_SELECTOR (parent);
1091 switch (GST_QUERY_TYPE (query)) {
1092 case GST_QUERY_LATENCY:
1095 GstClockTime resmin, resmax;
1102 /* perform the query on all sinkpads and combine the results. We take the
1103 * max of min and the min of max for the result latency. */
1104 GST_INPUT_SELECTOR_LOCK (sel);
1105 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk;
1106 walk = g_list_next (walk)) {
1107 GstPad *sinkpad = GST_PAD_CAST (walk->data);
1109 if (gst_pad_peer_query (sinkpad, query)) {
1110 GstClockTime min, max;
1113 /* one query succeeded, we succeed too */
1116 gst_query_parse_latency (query, &live, &min, &max);
1118 GST_DEBUG_OBJECT (sinkpad,
1119 "peer latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1120 ", live %d", GST_TIME_ARGS (min), GST_TIME_ARGS (max), live);
1127 else if (max < resmax)
1129 if (reslive == FALSE)
1134 GST_INPUT_SELECTOR_UNLOCK (sel);
1136 gst_query_set_latency (query, reslive, resmin, resmax);
1138 GST_DEBUG_OBJECT (sel,
1139 "total latency min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT
1140 ", live %d", GST_TIME_ARGS (resmin), GST_TIME_ARGS (resmax),
1147 res = gst_pad_query_default (pad, parent, query);
1154 /* check if the pad is the active sinkpad */
1155 static inline gboolean
1156 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
1160 GST_INPUT_SELECTOR_LOCK (sel);
1161 res = (pad == sel->active_sinkpad);
1162 GST_INPUT_SELECTOR_UNLOCK (sel);
1167 /* Get or create the active sinkpad, must be called with SELECTOR_LOCK */
1169 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
1171 GstPad *active_sinkpad;
1172 GstSelectorPad *selpad;
1174 selpad = GST_SELECTOR_PAD_CAST (pad);
1176 selpad->active = TRUE;
1177 active_sinkpad = sel->active_sinkpad;
1178 if (active_sinkpad == NULL) {
1179 /* first pad we get activity on becomes the activated pad by default */
1180 if (sel->active_sinkpad)
1181 gst_object_unref (sel->active_sinkpad);
1182 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
1183 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1186 return active_sinkpad;
1190 gst_input_selector_request_new_pad (GstElement * element,
1191 GstPadTemplate * templ, const gchar * unused, const GstCaps * caps)
1193 GstInputSelector *sel;
1195 GstPad *sinkpad = NULL;
1197 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
1199 sel = GST_INPUT_SELECTOR (element);
1201 GST_INPUT_SELECTOR_LOCK (sel);
1203 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
1204 name = g_strdup_printf ("sink_%u", sel->padcount++);
1205 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
1206 "name", name, "direction", templ->direction, "template", templ, NULL);
1211 gst_pad_set_event_function (sinkpad,
1212 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
1213 gst_pad_set_query_function (sinkpad,
1214 GST_DEBUG_FUNCPTR (gst_selector_pad_query));
1215 gst_pad_set_chain_function (sinkpad,
1216 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
1217 gst_pad_set_iterate_internal_links_function (sinkpad,
1218 GST_DEBUG_FUNCPTR (gst_selector_pad_iterate_linked_pads));
1220 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_CAPS);
1221 GST_OBJECT_FLAG_SET (sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
1222 gst_pad_set_active (sinkpad, TRUE);
1223 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
1224 GST_INPUT_SELECTOR_UNLOCK (sel);
1230 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
1232 GstInputSelector *sel;
1234 sel = GST_INPUT_SELECTOR (element);
1235 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1237 GST_INPUT_SELECTOR_LOCK (sel);
1238 /* if the pad was the active pad, makes us select a new one */
1239 if (sel->active_sinkpad == pad) {
1240 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1241 gst_object_unref (sel->active_sinkpad);
1242 sel->active_sinkpad = NULL;
1246 gst_pad_set_active (pad, FALSE);
1247 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1248 GST_INPUT_SELECTOR_UNLOCK (sel);
1252 gst_input_selector_reset (GstInputSelector * sel)
1256 GST_INPUT_SELECTOR_LOCK (sel);
1257 /* clear active pad */
1258 if (sel->active_sinkpad) {
1259 gst_object_unref (sel->active_sinkpad);
1260 sel->active_sinkpad = NULL;
1262 /* reset each of our sinkpads state */
1263 for (walk = GST_ELEMENT_CAST (sel)->sinkpads; walk; walk = g_list_next (walk)) {
1264 GstSelectorPad *selpad = GST_SELECTOR_PAD_CAST (walk->data);
1266 gst_selector_pad_reset (selpad);
1269 gst_tag_list_free (selpad->tags);
1270 selpad->tags = NULL;
1273 GST_INPUT_SELECTOR_UNLOCK (sel);
1276 static GstStateChangeReturn
1277 gst_input_selector_change_state (GstElement * element,
1278 GstStateChange transition)
1280 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1281 GstStateChangeReturn result;
1283 switch (transition) {
1284 case GST_STATE_CHANGE_READY_TO_PAUSED:
1285 GST_INPUT_SELECTOR_LOCK (self);
1286 self->blocked = FALSE;
1287 self->flushing = FALSE;
1288 GST_INPUT_SELECTOR_UNLOCK (self);
1290 case GST_STATE_CHANGE_PAUSED_TO_READY:
1291 /* first unlock before we call the parent state change function, which
1292 * tries to acquire the stream lock when going to ready. */
1293 GST_INPUT_SELECTOR_LOCK (self);
1294 self->blocked = FALSE;
1295 self->flushing = TRUE;
1296 GST_INPUT_SELECTOR_BROADCAST (self);
1297 GST_INPUT_SELECTOR_UNLOCK (self);
1303 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1305 switch (transition) {
1306 case GST_STATE_CHANGE_PAUSED_TO_READY:
1307 gst_input_selector_reset (self);
1317 gst_input_selector_block (GstInputSelector * self)
1320 GstSelectorPad *spad;
1322 GST_INPUT_SELECTOR_LOCK (self);
1325 GST_WARNING_OBJECT (self, "switch already blocked");
1327 self->blocked = TRUE;
1328 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1331 ret = gst_selector_pad_get_running_time (spad);
1333 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1335 GST_INPUT_SELECTOR_UNLOCK (self);