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",
73 PAD_PROP_RUNNING_TIME = 1
83 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
85 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
87 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
89 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
91 static void gst_input_selector_push_pending_stop (GstInputSelector * self);
93 #define GST_TYPE_SELECTOR_PAD \
94 (gst_selector_pad_get_type())
95 #define GST_SELECTOR_PAD(obj) \
96 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
97 #define GST_SELECTOR_PAD_CLASS(klass) \
98 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
99 #define GST_IS_SELECTOR_PAD(obj) \
100 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
101 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
102 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
103 #define GST_SELECTOR_PAD_CAST(obj) \
104 ((GstSelectorPad *)(obj))
106 typedef struct _GstSelectorPad GstSelectorPad;
107 typedef struct _GstSelectorPadClass GstSelectorPadClass;
109 struct _GstSelectorPad
115 gboolean segment_pending;
119 struct _GstSelectorPadClass
124 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
125 static void gst_selector_pad_init (GstSelectorPad * pad);
126 static void gst_selector_pad_finalize (GObject * object);
127 static void gst_selector_pad_get_property (GObject * object,
128 guint prop_id, GValue * value, GParamSpec * pspec);
130 static GstPadClass *selector_pad_parent_class = NULL;
132 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
133 static void gst_selector_pad_reset (GstSelectorPad * pad);
134 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
135 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
136 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
137 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
138 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
139 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
142 gst_selector_pad_get_type (void)
144 static GType selector_pad_type = 0;
146 if (!selector_pad_type) {
147 static const GTypeInfo selector_pad_info = {
148 sizeof (GstSelectorPadClass),
151 (GClassInitFunc) gst_selector_pad_class_init,
154 sizeof (GstSelectorPad),
156 (GInstanceInitFunc) gst_selector_pad_init,
160 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
161 &selector_pad_info, 0);
163 return selector_pad_type;
167 gst_selector_pad_class_init (GstSelectorPadClass * klass)
169 GObjectClass *gobject_class;
171 gobject_class = (GObjectClass *) klass;
173 selector_pad_parent_class = g_type_class_peek_parent (klass);
175 gobject_class->get_property =
176 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
177 g_object_class_install_property (gobject_class, PAD_PROP_RUNNING_TIME,
178 g_param_spec_int64 ("running-time", "Running time",
179 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
181 gobject_class->finalize = gst_selector_pad_finalize;
185 gst_selector_pad_init (GstSelectorPad * pad)
187 gst_selector_pad_reset (pad);
191 gst_selector_pad_finalize (GObject * object)
195 pad = GST_SELECTOR_PAD_CAST (object);
197 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
201 gst_selector_pad_get_property (GObject * object, guint prop_id,
202 GValue * value, GParamSpec * pspec)
204 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
207 case PAD_PROP_RUNNING_TIME:
208 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_selector_pad_get_running_time (GstSelectorPad * pad)
222 GST_OBJECT_LOCK (pad);
225 gint64 last_stop = pad->segment.last_stop;
228 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
232 GST_OBJECT_UNLOCK (pad);
234 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
235 GST_TIME_ARGS (ret));
241 gst_selector_pad_reset (GstSelectorPad * pad)
245 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
248 /* strictly get the linked pad from the sinkpad. If the pad is active we return
249 * the srcpad else we return NULL */
251 gst_selector_pad_get_linked_pads (GstPad * pad)
255 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
259 /* need to drop the ref, internal linked pads is not MT safe */
260 gst_object_unref (otherpad);
262 return g_list_append (NULL, otherpad);
266 gst_selector_pad_event (GstPad * pad, GstEvent * event)
269 gboolean forward = TRUE;
270 GstInputSelector *sel;
271 GstSelectorPad *selpad;
273 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
274 selpad = GST_SELECTOR_PAD_CAST (pad);
276 /* only forward if we are dealing with the active sinkpad */
277 forward = gst_input_selector_is_active_sinkpad (sel, pad);
279 switch (GST_EVENT_TYPE (event)) {
280 case GST_EVENT_FLUSH_STOP:
281 gst_selector_pad_reset (selpad);
283 case GST_EVENT_NEWSEGMENT:
288 gint64 start, stop, time;
290 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
291 &start, &stop, &time);
293 GST_DEBUG_OBJECT (sel,
294 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
296 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
297 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
299 gst_segment_set_newsegment_full (&selpad->segment, update,
300 rate, arate, format, start, stop, time);
301 /* if we are not going to forward the segment, mark the segment as
304 selpad->segment_pending = TRUE;
314 res = gst_pad_push_event (sel->srcpad, event);
316 gst_event_unref (event);
318 gst_object_unref (sel);
324 gst_selector_pad_getcaps (GstPad * pad)
326 GstInputSelector *sel;
329 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
331 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
332 caps = gst_pad_peer_get_caps (sel->srcpad);
334 caps = gst_caps_new_any ();
336 gst_object_unref (sel);
342 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
343 guint size, GstCaps * caps, GstBuffer ** buf)
345 GstInputSelector *sel;
346 GstFlowReturn result;
347 GstPad *active_sinkpad;
349 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
351 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
353 /* Fallback allocation for buffers from pads except the selected one */
354 if (pad != active_sinkpad) {
355 GST_DEBUG_OBJECT (sel,
356 "Pad %s:%s is not selected. Performing fallback allocation",
357 GST_DEBUG_PAD_NAME (pad));
360 result = GST_FLOW_OK;
362 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
364 /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
365 * allocation. This should NOT be necessary, because playbin should
366 * properly block the source pad from running until it's finished hooking
367 * everything up, but playbin needs refactoring first. */
368 if (result == GST_FLOW_NOT_LINKED) {
369 GST_DEBUG_OBJECT (sel,
370 "No peer pad yet - performing fallback allocation for pad %s:%s",
371 GST_DEBUG_PAD_NAME (pad));
374 result = GST_FLOW_OK;
378 gst_object_unref (sel);
384 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
388 GST_OBJECT_LOCK (self);
390 while (self->blocked)
391 g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
393 GST_OBJECT_UNLOCK (self);
395 GST_OBJECT_LOCK (pad);
396 flushing = GST_PAD_IS_FLUSHING (pad);
397 GST_OBJECT_UNLOCK (pad);
403 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
405 GstInputSelector *sel;
407 GstPad *active_sinkpad;
408 GstSelectorPad *selpad;
409 GstClockTime end_time, duration;
412 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
413 selpad = GST_SELECTOR_PAD_CAST (pad);
414 seg = &selpad->segment;
416 if (gst_input_selector_wait (sel, pad))
419 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
421 end_time = GST_BUFFER_TIMESTAMP (buf);
422 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
423 duration = GST_BUFFER_DURATION (buf);
424 if (GST_CLOCK_TIME_IS_VALID (duration))
425 end_time += duration;
426 GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
427 GST_TIME_ARGS (end_time));
428 gst_segment_set_last_stop (seg, seg->format, end_time);
431 /* Ignore buffers from pads except the selected one */
432 if (pad != active_sinkpad)
435 gst_input_selector_push_pending_stop (sel);
437 /* if we have a pending segment, push it out now */
438 if (selpad->segment_pending) {
439 gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
440 seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
443 selpad->segment_pending = FALSE;
447 GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
448 GST_DEBUG_PAD_NAME (pad));
449 res = gst_pad_push (sel->srcpad, buf);
451 gst_object_unref (sel);
453 /* dropped buffers */
456 GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
457 buf, GST_DEBUG_PAD_NAME (pad));
458 gst_buffer_unref (buf);
464 static void gst_input_selector_dispose (GObject * object);
465 static void gst_input_selector_init (GstInputSelector * sel);
466 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
467 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
468 static void gst_input_selector_set_property (GObject * object,
469 guint prop_id, const GValue * value, GParamSpec * pspec);
470 static void gst_input_selector_get_property (GObject * object,
471 guint prop_id, GValue * value, GParamSpec * pspec);
472 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
473 GstPadTemplate * templ, const gchar * unused);
474 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
475 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
476 element, GstStateChange transition);
477 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
478 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
479 static gint64 gst_input_selector_block (GstInputSelector * self);
480 static void gst_input_selector_switch (GstInputSelector * self,
481 const gchar * pad_name, gint64 stop_time, gint64 start_time);
483 static GstElementClass *parent_class = NULL;
486 gst_input_selector_get_type (void)
488 static GType input_selector_type = 0;
490 if (!input_selector_type) {
491 static const GTypeInfo input_selector_info = {
492 sizeof (GstInputSelectorClass),
493 (GBaseInitFunc) gst_input_selector_base_init,
495 (GClassInitFunc) gst_input_selector_class_init,
498 sizeof (GstInputSelector),
500 (GInstanceInitFunc) gst_input_selector_init,
502 input_selector_type =
503 g_type_register_static (GST_TYPE_ELEMENT,
504 "GstInputSelector", &input_selector_info, 0);
505 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
506 "input-selector", 0, "An input stream selector element");
509 return input_selector_type;
513 gst_input_selector_base_init (GstInputSelectorClass * klass)
515 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
517 gst_element_class_set_details (element_class, &gst_input_selector_details);
518 gst_element_class_add_pad_template (element_class,
519 gst_static_pad_template_get (&gst_input_selector_sink_factory));
520 gst_element_class_add_pad_template (element_class,
521 gst_static_pad_template_get (&gst_input_selector_src_factory));
525 gst_input_selector_class_init (GstInputSelectorClass * klass)
527 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
528 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
530 parent_class = g_type_class_peek_parent (klass);
531 gobject_class->set_property =
532 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
533 gobject_class->get_property =
534 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
535 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
536 g_param_spec_string ("active-pad", "Active pad",
537 "Name of the currently" " active sink pad", NULL, G_PARAM_READWRITE));
538 gobject_class->dispose = gst_input_selector_dispose;
539 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
540 gstelement_class->release_pad = gst_input_selector_release_pad;
541 gstelement_class->change_state = gst_input_selector_change_state;
544 * GstInputSelector::block:
545 * @inputselector: the #GstInputSelector
547 * Block all sink pads in preparation for a switch. Returns the stop time of
548 * the current switch segment, as a running time, or 0 if there is no current
549 * active pad or the current active pad never received data.
551 gst_input_selector_signals[SIGNAL_BLOCK] =
552 g_signal_new ("block", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
553 G_STRUCT_OFFSET (GstInputSelectorClass, block),
554 NULL, NULL, gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
556 * GstInputSelector::switch:
557 * @inputselector: the #GstInputSelector
558 * @pad: name of pad to switch to
559 * @stop_time: running time at which to close the previous segment, or -1
560 * to use the running time of the previously active sink pad
561 * @start_time: running time at which to start the new segment, or -1 to
562 * use the running time of the newly active sink pad
564 * Switch to a new feed. The segment opened by the previously active pad, if
565 * any, will be closed, and a new segment opened before data flows again.
567 * This signal must be emitted when the element has been blocked via the <link
568 * linkend="GstInputSelector-block">block</link> signal.
570 * If you have a stream with only one switch element, such as an audio-only
571 * stream, a stream switch should be performed by first emitting the block
572 * signal, and then emitting the switch signal with -1 for the stop and start
575 * The intention of the @stop_time and @start_time arguments is to allow
576 * multiple switch elements to switch and maintain stream synchronization.
577 * When switching a stream with multiple feeds, you will need as many switch
578 * elements as you have feeds. For example, a feed with audio and video will
579 * have one switch element between the audio feeds and one for video.
581 * A switch over multiple switch elements should be performed as follows:
582 * First, emit the <link linkend="GstInputSelector-block">block</link>
583 * signal, collecting the returned values. The maximum running time returned
584 * by block should then be used as the time at which to close the previous
587 * Then, query the running times of the new audio and video pads that you will
588 * switch to. Naturally, these pads are on separate switch elements. Take the
589 * minimum running time for those streams and use it for the time at which to
590 * open the new segment.
592 * If @pad is the same as the current active pad, the element will cancel any
593 * previous block without adjusting segments.
595 gst_input_selector_signals[SIGNAL_SWITCH] =
596 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
597 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
598 NULL, NULL, gst_selector_marshal_VOID__STRING_INT64_INT64,
599 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
601 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
602 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
606 gst_input_selector_init (GstInputSelector * sel)
608 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
609 gst_pad_set_internal_link_function (sel->srcpad,
610 GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
611 gst_pad_set_getcaps_function (sel->srcpad,
612 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
613 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
614 /* sinkpad management */
615 sel->active_sinkpad = NULL;
616 sel->nb_sinkpads = 0;
617 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
619 sel->blocked_cond = g_cond_new ();
620 sel->blocked = FALSE;
624 gst_input_selector_dispose (GObject * object)
626 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
628 if (sel->active_sinkpad) {
629 gst_object_unref (sel->active_sinkpad);
630 sel->active_sinkpad = NULL;
633 if (sel->blocked_cond) {
634 g_cond_free (sel->blocked_cond);
635 sel->blocked_cond = NULL;
638 G_OBJECT_CLASS (parent_class)->dispose (object);
641 /* Solve the following equation for B.timestamp, and set that as the segment
643 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
646 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
648 return (running_time - segment->accum) * segment->abs_rate + segment->start;
652 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
654 segment->stop = gst_segment_get_timestamp (segment, running_time);
655 segment->last_stop = -1;
659 gst_segment_set_start (GstSegment * segment, gint64 running_time)
661 segment->start = gst_segment_get_timestamp (segment, running_time);
665 gst_input_selector_set_active_pad (GstInputSelector * self,
666 const gchar * pad_name, gint64 stop_time, gint64 start_time)
669 GstSelectorPad *old, *new;
670 GstPad **active_pad_p;
672 if (strcmp (pad_name, "") != 0)
673 pad = gst_element_get_pad (GST_ELEMENT (self), pad_name);
677 GST_OBJECT_LOCK (self);
679 if (pad == self->active_sinkpad)
682 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
683 new = GST_SELECTOR_PAD_CAST (pad);
685 if (old && old->active && !self->pending_stop && stop_time >= 0) {
686 /* schedule a last_stop update if one isn't already scheduled, and a
687 segment has been pushed before. */
688 memcpy (&self->pending_stop_segment, &old->segment,
689 sizeof (self->pending_stop_segment));
690 gst_segment_set_stop (&self->pending_stop_segment, stop_time);
691 self->pending_stop = TRUE;
694 if (new && new->active && start_time >= 0) {
695 /* schedule a new segment push */
696 gst_segment_set_start (&new->segment, start_time);
697 new->segment_pending = TRUE;
700 active_pad_p = &self->active_sinkpad;
701 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
702 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
703 self->active_sinkpad);
706 GST_OBJECT_UNLOCK (self);
709 gst_object_unref (pad);
714 gst_input_selector_set_property (GObject * object, guint prop_id,
715 const GValue * value, GParamSpec * pspec)
717 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
720 case PROP_ACTIVE_PAD:
721 gst_input_selector_set_active_pad (sel,
722 g_value_get_string (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
725 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
731 gst_input_selector_get_property (GObject * object, guint prop_id,
732 GValue * value, GParamSpec * pspec)
734 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
737 case PROP_ACTIVE_PAD:{
738 GST_OBJECT_LOCK (object);
739 if (sel->active_sinkpad != NULL) {
740 g_value_take_string (value, gst_pad_get_name (sel->active_sinkpad));
742 g_value_set_string (value, "");
744 GST_OBJECT_UNLOCK (object);
748 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
756 GstInputSelector *sel;
757 GstPad *otherpad = NULL;
759 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
760 GST_OBJECT_LOCK (sel);
761 if (pad == sel->srcpad)
762 otherpad = sel->active_sinkpad;
763 else if (pad == sel->active_sinkpad || !strict)
764 otherpad = sel->srcpad;
766 gst_object_ref (otherpad);
767 GST_OBJECT_UNLOCK (sel);
768 gst_object_unref (sel);
773 gst_input_selector_getcaps (GstPad * pad)
779 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
780 parent = gst_object_get_parent (GST_OBJECT (pad));
782 GST_DEBUG_OBJECT (parent,
783 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
784 caps = gst_caps_new_any ();
786 GST_DEBUG_OBJECT (parent,
787 "Pad %s:%s is linked (to %s:%s), returning peer caps",
788 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
789 /* if the peer has caps, use those. If the pad is not linked, this function
790 * returns NULL and we return ANY */
791 if (!(caps = gst_pad_peer_get_caps (otherpad)))
792 caps = gst_caps_new_any ();
793 gst_object_unref (otherpad);
796 gst_object_unref (parent);
800 /* check if the pad is the active sinkpad */
802 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
804 GstSelectorPad *selpad;
807 selpad = GST_SELECTOR_PAD_CAST (pad);
809 GST_OBJECT_LOCK (sel);
810 res = (pad == sel->active_sinkpad);
811 GST_OBJECT_UNLOCK (sel);
816 /* Get or create the active sinkpad */
818 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
820 GstPad *active_sinkpad;
821 GstSelectorPad *selpad;
823 selpad = GST_SELECTOR_PAD_CAST (pad);
825 GST_OBJECT_LOCK (sel);
826 selpad->active = TRUE;
827 active_sinkpad = sel->active_sinkpad;
828 if (active_sinkpad == NULL) {
829 /* first pad we get an alloc on becomes the activated pad by default */
830 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
831 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
833 GST_OBJECT_UNLOCK (sel);
835 return active_sinkpad;
839 gst_input_selector_get_linked_pads (GstPad * pad)
843 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
846 /* need to drop the ref, internal linked pads is not MT safe */
847 gst_object_unref (otherpad);
848 return g_list_append (NULL, otherpad);
852 gst_input_selector_request_new_pad (GstElement * element,
853 GstPadTemplate * templ, const gchar * unused)
855 GstInputSelector *sel;
857 GstPad *sinkpad = NULL;
859 sel = GST_INPUT_SELECTOR (element);
860 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
861 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->nb_sinkpads);
862 GST_OBJECT_LOCK (sel);
863 name = g_strdup_printf ("sink%d", sel->nb_sinkpads++);
864 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
865 "name", name, "direction", templ->direction, "template", templ, NULL);
867 GST_OBJECT_UNLOCK (sel);
869 gst_pad_set_event_function (sinkpad,
870 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
871 gst_pad_set_getcaps_function (sinkpad,
872 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
873 gst_pad_set_chain_function (sinkpad,
874 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
875 gst_pad_set_internal_link_function (sinkpad,
876 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
877 gst_pad_set_bufferalloc_function (sinkpad,
878 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
880 gst_pad_set_active (sinkpad, TRUE);
881 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
886 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
888 GstInputSelector *sel;
890 sel = GST_INPUT_SELECTOR (element);
891 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
893 GST_OBJECT_LOCK (sel);
894 /* if the pad was the active pad, makes us select a new one */
895 if (sel->active_sinkpad == pad) {
896 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
897 sel->active_sinkpad = NULL;
899 GST_OBJECT_UNLOCK (sel);
901 gst_pad_set_active (pad, FALSE);
902 gst_element_remove_pad (GST_ELEMENT (sel), pad);
905 static GstStateChangeReturn
906 gst_input_selector_change_state (GstElement * element,
907 GstStateChange transition)
909 GstInputSelector *self = GST_INPUT_SELECTOR (element);
910 GstStateChangeReturn result;
912 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
914 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
915 GST_OBJECT_LOCK (self);
916 self->blocked = FALSE;
917 g_cond_broadcast (self->blocked_cond);
918 GST_OBJECT_UNLOCK (self);
925 gst_input_selector_block (GstInputSelector * self)
928 GstSelectorPad *spad;
930 GST_OBJECT_LOCK (self);
933 GST_WARNING_OBJECT (self, "switch already blocked");
935 self->blocked = TRUE;
936 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
938 if (self->active_sinkpad)
939 ret = gst_selector_pad_get_running_time (spad);
941 GST_DEBUG_OBJECT (self, "no active pad while blocking");
943 GST_OBJECT_UNLOCK (self);
949 gst_input_selector_push_pending_stop (GstInputSelector * self)
951 GstEvent *event = NULL;
953 GST_OBJECT_LOCK (self);
955 if (G_UNLIKELY (self->pending_stop)) {
956 GstSegment *seg = &self->pending_stop_segment;
958 event = gst_event_new_new_segment_full (TRUE, seg->rate,
959 seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
961 self->pending_stop = FALSE;
964 GST_OBJECT_UNLOCK (self);
967 gst_pad_push_event (self->srcpad, event);
970 /* stop_time and start_time are running times */
972 gst_input_selector_switch (GstInputSelector * self, const gchar * pad_name,
973 gint64 stop_time, gint64 start_time)
975 g_return_if_fail (self->blocked == TRUE);
977 gst_input_selector_set_active_pad (self, pad_name, stop_time, start_time);
979 GST_OBJECT_LOCK (self);
980 self->blocked = FALSE;
981 g_cond_broadcast (self->blocked_cond);
982 GST_OBJECT_UNLOCK (self);