2 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
3 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
4 * Copyright (C) 2005 Jan Schmidt <thaytan@mad.scientist.com>
5 * Copyright (C) 2007 Wim Taymans <wim.taymans@gmail.com>
6 * Copyright (C) 2007 Andy Wingo <wingo@pobox.com>
7 * Copyright (C) 2008 Nokia Corporation. (contact <stefan.kost@nokia.com>)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * SECTION:element-input-selector
27 * @short_description: N-to-1 stream selectoring
28 * @see_also: #GstOutputSelector
30 * Direct one out of N input streams to the output pad.
39 #include "gstinputselector.h"
40 #include "gstselector-marshal.h"
42 GST_DEBUG_CATEGORY_STATIC (input_selector_debug);
43 #define GST_CAT_DEFAULT input_selector_debug
45 static const GstElementDetails gst_input_selector_details =
46 GST_ELEMENT_DETAILS ("Input selector",
48 "N-to-1 input stream selectoring",
49 "Julien Moutte <julien@moutte.net>\n"
50 "Ronald S. Bultje <rbultje@ronald.bitfreak.net>\n"
51 "Jan Schmidt <thaytan@mad.scientist.com>\n"
52 "Wim Taymans <wim.taymans@gmail.com>");
54 static GstStaticPadTemplate gst_input_selector_sink_factory =
55 GST_STATIC_PAD_TEMPLATE ("sink%d",
60 static GstStaticPadTemplate gst_input_selector_src_factory =
61 GST_STATIC_PAD_TEMPLATE ("src",
74 PAD_PROP_RUNNING_TIME = 1
84 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
86 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
88 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
90 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
92 static void gst_input_selector_push_pending_stop (GstInputSelector * self);
93 static gboolean gst_input_selector_check_eos (GstElement * selector);
95 #define GST_TYPE_SELECTOR_PAD \
96 (gst_selector_pad_get_type())
97 #define GST_SELECTOR_PAD(obj) \
98 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
99 #define GST_SELECTOR_PAD_CLASS(klass) \
100 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
101 #define GST_IS_SELECTOR_PAD(obj) \
102 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
103 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
104 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
105 #define GST_SELECTOR_PAD_CAST(obj) \
106 ((GstSelectorPad *)(obj))
108 typedef struct _GstSelectorPad GstSelectorPad;
109 typedef struct _GstSelectorPadClass GstSelectorPadClass;
111 struct _GstSelectorPad
117 gboolean segment_pending;
121 struct _GstSelectorPadClass
126 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
127 static void gst_selector_pad_init (GstSelectorPad * pad);
128 static void gst_selector_pad_finalize (GObject * object);
129 static void gst_selector_pad_get_property (GObject * object,
130 guint prop_id, GValue * value, GParamSpec * pspec);
132 static GstPadClass *selector_pad_parent_class = NULL;
134 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
135 static void gst_selector_pad_reset (GstSelectorPad * pad);
136 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
137 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
138 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
139 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
140 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
141 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
144 gst_selector_pad_get_type (void)
146 static GType selector_pad_type = 0;
148 if (!selector_pad_type) {
149 static const GTypeInfo selector_pad_info = {
150 sizeof (GstSelectorPadClass),
153 (GClassInitFunc) gst_selector_pad_class_init,
156 sizeof (GstSelectorPad),
158 (GInstanceInitFunc) gst_selector_pad_init,
162 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
163 &selector_pad_info, 0);
165 return selector_pad_type;
169 gst_selector_pad_class_init (GstSelectorPadClass * klass)
171 GObjectClass *gobject_class;
173 gobject_class = (GObjectClass *) klass;
175 selector_pad_parent_class = g_type_class_peek_parent (klass);
177 gobject_class->get_property =
178 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
179 g_object_class_install_property (gobject_class, PAD_PROP_RUNNING_TIME,
180 g_param_spec_int64 ("running-time", "Running time",
181 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
183 gobject_class->finalize = gst_selector_pad_finalize;
187 gst_selector_pad_init (GstSelectorPad * pad)
189 gst_selector_pad_reset (pad);
193 gst_selector_pad_finalize (GObject * object)
197 pad = GST_SELECTOR_PAD_CAST (object);
199 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
203 gst_selector_pad_get_property (GObject * object, guint prop_id,
204 GValue * value, GParamSpec * pspec)
206 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
209 case PAD_PROP_RUNNING_TIME:
210 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gst_selector_pad_get_running_time (GstSelectorPad * pad)
224 GST_OBJECT_LOCK (pad);
227 gint64 last_stop = pad->segment.last_stop;
230 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
234 GST_OBJECT_UNLOCK (pad);
236 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
237 GST_TIME_ARGS (ret));
243 gst_selector_pad_reset (GstSelectorPad * pad)
247 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
250 /* strictly get the linked pad from the sinkpad. If the pad is active we return
251 * the srcpad else we return NULL */
253 gst_selector_pad_get_linked_pads (GstPad * pad)
257 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
261 /* need to drop the ref, internal linked pads is not MT safe */
262 gst_object_unref (otherpad);
264 return g_list_append (NULL, otherpad);
268 gst_selector_pad_event (GstPad * pad, GstEvent * event)
271 gboolean forward = TRUE;
272 GstInputSelector *sel;
273 GstSelectorPad *selpad;
275 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
276 selpad = GST_SELECTOR_PAD_CAST (pad);
278 /* only forward if we are dealing with the active sinkpad */
279 forward = gst_input_selector_is_active_sinkpad (sel, pad);
281 /* forward all events in select_all mode by default */
282 if (sel->select_all) {
286 switch (GST_EVENT_TYPE (event)) {
287 case GST_EVENT_FLUSH_STOP:
288 gst_selector_pad_reset (selpad);
290 case GST_EVENT_NEWSEGMENT:
295 gint64 start, stop, time;
297 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
298 &start, &stop, &time);
300 GST_DEBUG_OBJECT (sel,
301 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
303 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
304 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
306 gst_segment_set_newsegment_full (&selpad->segment, update,
307 rate, arate, format, start, stop, time);
308 /* if we are not going to forward the segment, mark the segment as
311 selpad->segment_pending = TRUE;
316 /* don't forward eos in select_all mode until all sink pads have eos */
317 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
325 res = gst_pad_push_event (sel->srcpad, event);
327 gst_event_unref (event);
329 gst_object_unref (sel);
335 gst_selector_pad_getcaps (GstPad * pad)
337 GstInputSelector *sel;
340 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
342 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
343 caps = gst_pad_peer_get_caps (sel->srcpad);
345 caps = gst_caps_new_any ();
347 gst_object_unref (sel);
353 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
354 guint size, GstCaps * caps, GstBuffer ** buf)
356 GstInputSelector *sel;
357 GstFlowReturn result;
358 GstPad *active_sinkpad;
360 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
362 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
364 /* Fallback allocation for buffers from pads except the selected one */
365 if (pad != active_sinkpad && !sel->select_all) {
366 GST_DEBUG_OBJECT (sel,
367 "Pad %s:%s is not selected. Performing fallback allocation",
368 GST_DEBUG_PAD_NAME (pad));
371 result = GST_FLOW_OK;
373 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
375 /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
376 * allocation. This should NOT be necessary, because playbin should
377 * properly block the source pad from running until it's finished hooking
378 * everything up, but playbin needs refactoring first. */
379 if (result == GST_FLOW_NOT_LINKED) {
380 GST_DEBUG_OBJECT (sel,
381 "No peer pad yet - performing fallback allocation for pad %s:%s",
382 GST_DEBUG_PAD_NAME (pad));
385 result = GST_FLOW_OK;
389 gst_object_unref (sel);
395 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
399 GST_OBJECT_LOCK (self);
401 while (self->blocked)
402 g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
404 GST_OBJECT_UNLOCK (self);
406 GST_OBJECT_LOCK (pad);
407 flushing = GST_PAD_IS_FLUSHING (pad);
408 GST_OBJECT_UNLOCK (pad);
414 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
416 GstInputSelector *sel;
418 GstPad *active_sinkpad;
419 GstSelectorPad *selpad;
420 GstClockTime end_time, duration;
423 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
424 selpad = GST_SELECTOR_PAD_CAST (pad);
425 seg = &selpad->segment;
427 if (gst_input_selector_wait (sel, pad))
430 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
432 end_time = GST_BUFFER_TIMESTAMP (buf);
433 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
434 duration = GST_BUFFER_DURATION (buf);
435 if (GST_CLOCK_TIME_IS_VALID (duration))
436 end_time += duration;
437 GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
438 GST_TIME_ARGS (end_time));
439 gst_segment_set_last_stop (seg, seg->format, end_time);
442 /* Ignore buffers from pads except the selected one */
443 if (pad != active_sinkpad && !sel->select_all)
446 gst_input_selector_push_pending_stop (sel);
448 /* if we have a pending segment, push it out now */
449 if (selpad->segment_pending) {
450 gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
451 seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
454 selpad->segment_pending = FALSE;
458 GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
459 GST_DEBUG_PAD_NAME (pad));
460 res = gst_pad_push (sel->srcpad, buf);
462 gst_object_unref (sel);
464 /* dropped buffers */
467 GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
468 buf, GST_DEBUG_PAD_NAME (pad));
469 gst_buffer_unref (buf);
475 static void gst_input_selector_dispose (GObject * object);
476 static void gst_input_selector_init (GstInputSelector * sel);
477 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
478 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
479 static void gst_input_selector_set_property (GObject * object,
480 guint prop_id, const GValue * value, GParamSpec * pspec);
481 static void gst_input_selector_get_property (GObject * object,
482 guint prop_id, GValue * value, GParamSpec * pspec);
483 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
484 GstPadTemplate * templ, const gchar * unused);
485 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
486 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
487 element, GstStateChange transition);
488 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
489 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
490 static gint64 gst_input_selector_block (GstInputSelector * self);
491 static void gst_input_selector_switch (GstInputSelector * self,
492 const gchar * pad_name, gint64 stop_time, gint64 start_time);
494 static GstElementClass *parent_class = NULL;
497 gst_input_selector_get_type (void)
499 static GType input_selector_type = 0;
501 if (!input_selector_type) {
502 static const GTypeInfo input_selector_info = {
503 sizeof (GstInputSelectorClass),
504 (GBaseInitFunc) gst_input_selector_base_init,
506 (GClassInitFunc) gst_input_selector_class_init,
509 sizeof (GstInputSelector),
511 (GInstanceInitFunc) gst_input_selector_init,
513 input_selector_type =
514 g_type_register_static (GST_TYPE_ELEMENT,
515 "GstInputSelector", &input_selector_info, 0);
516 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
517 "input-selector", 0, "An input stream selector element");
520 return input_selector_type;
524 gst_input_selector_base_init (GstInputSelectorClass * klass)
526 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
528 gst_element_class_set_details (element_class, &gst_input_selector_details);
529 gst_element_class_add_pad_template (element_class,
530 gst_static_pad_template_get (&gst_input_selector_sink_factory));
531 gst_element_class_add_pad_template (element_class,
532 gst_static_pad_template_get (&gst_input_selector_src_factory));
536 gst_input_selector_class_init (GstInputSelectorClass * klass)
538 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
539 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
541 parent_class = g_type_class_peek_parent (klass);
542 gobject_class->set_property =
543 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
544 gobject_class->get_property =
545 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
546 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
547 g_param_spec_string ("active-pad", "Active pad",
548 "Name of the currently" " active sink pad", NULL, G_PARAM_READWRITE));
549 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
550 g_param_spec_boolean ("select-all", "Select all mode",
551 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
552 gobject_class->dispose = gst_input_selector_dispose;
553 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
554 gstelement_class->release_pad = gst_input_selector_release_pad;
555 gstelement_class->change_state = gst_input_selector_change_state;
558 * GstInputSelector::block:
559 * @inputselector: the #GstInputSelector
561 * Block all sink pads in preparation for a switch. Returns the stop time of
562 * the current switch segment, as a running time, or 0 if there is no current
563 * active pad or the current active pad never received data.
565 gst_input_selector_signals[SIGNAL_BLOCK] =
566 g_signal_new ("block", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
567 G_STRUCT_OFFSET (GstInputSelectorClass, block),
568 NULL, NULL, gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
570 * GstInputSelector::switch:
571 * @inputselector: the #GstInputSelector
572 * @pad: name of pad to switch to
573 * @stop_time: running time at which to close the previous segment, or -1
574 * to use the running time of the previously active sink pad
575 * @start_time: running time at which to start the new segment, or -1 to
576 * use the running time of the newly active sink pad
578 * Switch to a new feed. The segment opened by the previously active pad, if
579 * any, will be closed, and a new segment opened before data flows again.
581 * This signal must be emitted when the element has been blocked via the <link
582 * linkend="GstInputSelector-block">block</link> signal.
584 * If you have a stream with only one switch element, such as an audio-only
585 * stream, a stream switch should be performed by first emitting the block
586 * signal, and then emitting the switch signal with -1 for the stop and start
589 * The intention of the @stop_time and @start_time arguments is to allow
590 * multiple switch elements to switch and maintain stream synchronization.
591 * When switching a stream with multiple feeds, you will need as many switch
592 * elements as you have feeds. For example, a feed with audio and video will
593 * have one switch element between the audio feeds and one for video.
595 * A switch over multiple switch elements should be performed as follows:
596 * First, emit the <link linkend="GstInputSelector-block">block</link>
597 * signal, collecting the returned values. The maximum running time returned
598 * by block should then be used as the time at which to close the previous
601 * Then, query the running times of the new audio and video pads that you will
602 * switch to. Naturally, these pads are on separate switch elements. Take the
603 * minimum running time for those streams and use it for the time at which to
604 * open the new segment.
606 * If @pad is the same as the current active pad, the element will cancel any
607 * previous block without adjusting segments.
609 gst_input_selector_signals[SIGNAL_SWITCH] =
610 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
611 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
612 NULL, NULL, gst_selector_marshal_VOID__STRING_INT64_INT64,
613 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
615 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
616 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
620 gst_input_selector_init (GstInputSelector * sel)
622 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
623 gst_pad_set_internal_link_function (sel->srcpad,
624 GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
625 gst_pad_set_getcaps_function (sel->srcpad,
626 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
627 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
628 /* sinkpad management */
629 sel->active_sinkpad = NULL;
630 sel->nb_sinkpads = 0;
631 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
633 sel->blocked_cond = g_cond_new ();
634 sel->blocked = FALSE;
636 sel->select_all = FALSE;
640 gst_input_selector_dispose (GObject * object)
642 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
644 if (sel->active_sinkpad) {
645 gst_object_unref (sel->active_sinkpad);
646 sel->active_sinkpad = NULL;
649 if (sel->blocked_cond) {
650 g_cond_free (sel->blocked_cond);
651 sel->blocked_cond = NULL;
654 G_OBJECT_CLASS (parent_class)->dispose (object);
657 /* Solve the following equation for B.timestamp, and set that as the segment
659 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
662 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
664 return (running_time - segment->accum) * segment->abs_rate + segment->start;
668 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
670 segment->stop = gst_segment_get_timestamp (segment, running_time);
671 segment->last_stop = -1;
675 gst_segment_set_start (GstSegment * segment, gint64 running_time)
677 segment->start = gst_segment_get_timestamp (segment, running_time);
681 gst_input_selector_set_active_pad (GstInputSelector * self,
682 const gchar * pad_name, gint64 stop_time, gint64 start_time)
685 GstSelectorPad *old, *new;
686 GstPad **active_pad_p;
688 if (strcmp (pad_name, "") != 0)
689 pad = gst_element_get_pad (GST_ELEMENT (self), pad_name);
693 GST_OBJECT_LOCK (self);
695 if (pad == self->active_sinkpad)
698 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
699 new = GST_SELECTOR_PAD_CAST (pad);
701 if (old && old->active && !self->pending_stop && stop_time >= 0) {
702 /* schedule a last_stop update if one isn't already scheduled, and a
703 segment has been pushed before. */
704 memcpy (&self->pending_stop_segment, &old->segment,
705 sizeof (self->pending_stop_segment));
706 gst_segment_set_stop (&self->pending_stop_segment, stop_time);
707 self->pending_stop = TRUE;
710 if (new && new->active && start_time >= 0) {
711 /* schedule a new segment push */
712 gst_segment_set_start (&new->segment, start_time);
713 new->segment_pending = TRUE;
716 active_pad_p = &self->active_sinkpad;
717 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
718 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
719 self->active_sinkpad);
722 GST_OBJECT_UNLOCK (self);
725 gst_object_unref (pad);
730 gst_input_selector_set_property (GObject * object, guint prop_id,
731 const GValue * value, GParamSpec * pspec)
733 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
736 case PROP_ACTIVE_PAD:
737 gst_input_selector_set_active_pad (sel,
738 g_value_get_string (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
740 case PROP_SELECT_ALL:
741 sel->select_all = g_value_get_boolean (value);
744 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
750 gst_input_selector_get_property (GObject * object, guint prop_id,
751 GValue * value, GParamSpec * pspec)
753 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
756 case PROP_ACTIVE_PAD:{
757 GST_OBJECT_LOCK (object);
758 if (sel->active_sinkpad != NULL) {
759 g_value_take_string (value, gst_pad_get_name (sel->active_sinkpad));
761 g_value_set_string (value, "");
763 GST_OBJECT_UNLOCK (object);
766 case PROP_SELECT_ALL:
767 g_value_set_boolean (value, sel->select_all);
770 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
776 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
778 GstInputSelector *sel;
779 GstPad *otherpad = NULL;
781 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
782 GST_OBJECT_LOCK (sel);
783 if (pad == sel->srcpad)
784 otherpad = sel->active_sinkpad;
785 else if (pad == sel->active_sinkpad || !strict)
786 otherpad = sel->srcpad;
788 gst_object_ref (otherpad);
789 GST_OBJECT_UNLOCK (sel);
790 gst_object_unref (sel);
795 gst_input_selector_getcaps (GstPad * pad)
801 parent = gst_object_get_parent (GST_OBJECT (pad));
802 if (GST_INPUT_SELECTOR (parent)->select_all) {
803 caps = gst_pad_proxy_getcaps (pad);
807 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
809 GST_DEBUG_OBJECT (parent,
810 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
811 caps = gst_caps_new_any ();
813 GST_DEBUG_OBJECT (parent,
814 "Pad %s:%s is linked (to %s:%s), returning peer caps",
815 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
816 /* if the peer has caps, use those. If the pad is not linked, this function
817 * returns NULL and we return ANY */
818 if (!(caps = gst_pad_peer_get_caps (otherpad)))
819 caps = gst_caps_new_any ();
820 gst_object_unref (otherpad);
824 gst_object_unref (parent);
828 /* check if the pad is the active sinkpad */
830 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
832 GstSelectorPad *selpad;
835 selpad = GST_SELECTOR_PAD_CAST (pad);
837 GST_OBJECT_LOCK (sel);
838 res = (pad == sel->active_sinkpad);
839 GST_OBJECT_UNLOCK (sel);
844 /* Get or create the active sinkpad */
846 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
848 GstPad *active_sinkpad;
849 GstSelectorPad *selpad;
851 selpad = GST_SELECTOR_PAD_CAST (pad);
853 GST_OBJECT_LOCK (sel);
854 selpad->active = TRUE;
855 active_sinkpad = sel->active_sinkpad;
856 if (active_sinkpad == NULL) {
857 /* first pad we get an alloc on becomes the activated pad by default */
858 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
859 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
861 GST_OBJECT_UNLOCK (sel);
863 return active_sinkpad;
867 gst_input_selector_get_linked_pads (GstPad * pad)
871 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
874 /* need to drop the ref, internal linked pads is not MT safe */
875 gst_object_unref (otherpad);
876 return g_list_append (NULL, otherpad);
880 gst_input_selector_request_new_pad (GstElement * element,
881 GstPadTemplate * templ, const gchar * unused)
883 GstInputSelector *sel;
885 GstPad *sinkpad = NULL;
887 sel = GST_INPUT_SELECTOR (element);
888 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
889 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->nb_sinkpads);
890 GST_OBJECT_LOCK (sel);
891 name = g_strdup_printf ("sink%d", sel->nb_sinkpads++);
892 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
893 "name", name, "direction", templ->direction, "template", templ, NULL);
895 GST_OBJECT_UNLOCK (sel);
897 gst_pad_set_event_function (sinkpad,
898 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
899 gst_pad_set_getcaps_function (sinkpad,
900 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
901 gst_pad_set_chain_function (sinkpad,
902 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
903 gst_pad_set_internal_link_function (sinkpad,
904 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
905 gst_pad_set_bufferalloc_function (sinkpad,
906 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
908 gst_pad_set_active (sinkpad, TRUE);
909 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
914 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
916 GstInputSelector *sel;
918 sel = GST_INPUT_SELECTOR (element);
919 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
921 GST_OBJECT_LOCK (sel);
922 /* if the pad was the active pad, makes us select a new one */
923 if (sel->active_sinkpad == pad) {
924 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
925 sel->active_sinkpad = NULL;
927 GST_OBJECT_UNLOCK (sel);
929 gst_pad_set_active (pad, FALSE);
930 gst_element_remove_pad (GST_ELEMENT (sel), pad);
933 static GstStateChangeReturn
934 gst_input_selector_change_state (GstElement * element,
935 GstStateChange transition)
937 GstInputSelector *self = GST_INPUT_SELECTOR (element);
938 GstStateChangeReturn result;
940 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
942 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
943 GST_OBJECT_LOCK (self);
944 self->blocked = FALSE;
945 g_cond_broadcast (self->blocked_cond);
946 GST_OBJECT_UNLOCK (self);
953 gst_input_selector_block (GstInputSelector * self)
956 GstSelectorPad *spad;
958 GST_OBJECT_LOCK (self);
961 GST_WARNING_OBJECT (self, "switch already blocked");
963 self->blocked = TRUE;
964 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
966 if (self->active_sinkpad)
967 ret = gst_selector_pad_get_running_time (spad);
969 GST_DEBUG_OBJECT (self, "no active pad while blocking");
971 GST_OBJECT_UNLOCK (self);
977 gst_input_selector_push_pending_stop (GstInputSelector * self)
979 GstEvent *event = NULL;
981 GST_OBJECT_LOCK (self);
983 if (G_UNLIKELY (self->pending_stop)) {
984 GstSegment *seg = &self->pending_stop_segment;
986 event = gst_event_new_new_segment_full (TRUE, seg->rate,
987 seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
989 self->pending_stop = FALSE;
992 GST_OBJECT_UNLOCK (self);
995 gst_pad_push_event (self->srcpad, event);
998 /* stop_time and start_time are running times */
1000 gst_input_selector_switch (GstInputSelector * self, const gchar * pad_name,
1001 gint64 stop_time, gint64 start_time)
1003 g_return_if_fail (self->blocked == TRUE);
1005 gst_input_selector_set_active_pad (self, pad_name, stop_time, start_time);
1007 GST_OBJECT_LOCK (self);
1008 self->blocked = FALSE;
1009 g_cond_broadcast (self->blocked_cond);
1010 GST_OBJECT_UNLOCK (self);
1014 gst_input_selector_check_eos (GstElement * selector)
1016 GstIterator *it = gst_element_iterate_sink_pads (selector);
1017 GstIteratorResult ires;
1019 gboolean done = FALSE, is_eos = FALSE;
1020 GstSelectorPad *pad;
1023 ires = gst_iterator_next (it, &item);
1025 case GST_ITERATOR_DONE:
1026 GST_INFO_OBJECT (selector, "all sink pads have eos");
1030 case GST_ITERATOR_OK:
1031 pad = GST_SELECTOR_PAD_CAST (item);
1036 case GST_ITERATOR_RESYNC:
1037 gst_iterator_resync (it);
1044 gst_iterator_free (it);