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_object_unref (sel);
322 gst_selector_pad_getcaps (GstPad * pad)
324 GstInputSelector *sel;
327 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
329 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
330 caps = gst_pad_peer_get_caps (sel->srcpad);
332 caps = gst_caps_new_any ();
334 gst_object_unref (sel);
340 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
341 guint size, GstCaps * caps, GstBuffer ** buf)
343 GstInputSelector *sel;
344 GstFlowReturn result;
345 GstPad *active_sinkpad;
347 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
349 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
351 /* Fallback allocation for buffers from pads except the selected one */
352 if (pad != active_sinkpad) {
353 GST_DEBUG_OBJECT (sel,
354 "Pad %s:%s is not selected. Performing fallback allocation",
355 GST_DEBUG_PAD_NAME (pad));
358 result = GST_FLOW_OK;
360 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
362 /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
363 * allocation. This should NOT be necessary, because playbin should
364 * properly block the source pad from running until it's finished hooking
365 * everything up, but playbin needs refactoring first. */
366 if (result == GST_FLOW_NOT_LINKED) {
367 GST_DEBUG_OBJECT (sel,
368 "No peer pad yet - performing fallback allocation for pad %s:%s",
369 GST_DEBUG_PAD_NAME (pad));
372 result = GST_FLOW_OK;
376 gst_object_unref (sel);
382 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
386 GST_OBJECT_LOCK (self);
388 while (self->blocked)
389 g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
391 GST_OBJECT_UNLOCK (self);
393 GST_OBJECT_LOCK (pad);
394 flushing = GST_PAD_IS_FLUSHING (pad);
395 GST_OBJECT_UNLOCK (pad);
401 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
403 GstInputSelector *sel;
405 GstPad *active_sinkpad;
406 GstSelectorPad *selpad;
407 GstClockTime end_time, duration;
410 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
411 selpad = GST_SELECTOR_PAD_CAST (pad);
412 seg = &selpad->segment;
414 if (gst_input_selector_wait (sel, pad))
417 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
419 end_time = GST_BUFFER_TIMESTAMP (buf);
420 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
421 duration = GST_BUFFER_DURATION (buf);
422 if (GST_CLOCK_TIME_IS_VALID (duration))
423 end_time += duration;
424 GST_DEBUG_OBJECT (sel, "received end time %" GST_TIME_FORMAT,
425 GST_TIME_ARGS (end_time));
426 gst_segment_set_last_stop (seg, seg->format, end_time);
429 /* Ignore buffers from pads except the selected one */
430 if (pad != active_sinkpad)
433 gst_input_selector_push_pending_stop (sel);
435 /* if we have a pending segment, push it out now */
436 if (selpad->segment_pending) {
437 gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
438 seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
441 selpad->segment_pending = FALSE;
445 GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
446 GST_DEBUG_PAD_NAME (pad));
447 res = gst_pad_push (sel->srcpad, buf);
449 gst_object_unref (sel);
451 /* dropped buffers */
454 GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
455 buf, GST_DEBUG_PAD_NAME (pad));
456 gst_buffer_unref (buf);
462 static void gst_input_selector_dispose (GObject * object);
463 static void gst_input_selector_init (GstInputSelector * sel);
464 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
465 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
466 static void gst_input_selector_set_property (GObject * object,
467 guint prop_id, const GValue * value, GParamSpec * pspec);
468 static void gst_input_selector_get_property (GObject * object,
469 guint prop_id, GValue * value, GParamSpec * pspec);
470 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
471 GstPadTemplate * templ, const gchar * unused);
472 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
473 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
474 element, GstStateChange transition);
475 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
476 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
477 static gint64 gst_input_selector_block (GstInputSelector * self);
478 static void gst_input_selector_switch (GstInputSelector * self,
479 const gchar * pad_name, gint64 stop_time, gint64 start_time);
481 static GstElementClass *parent_class = NULL;
484 gst_input_selector_get_type (void)
486 static GType input_selector_type = 0;
488 if (!input_selector_type) {
489 static const GTypeInfo input_selector_info = {
490 sizeof (GstInputSelectorClass),
491 (GBaseInitFunc) gst_input_selector_base_init,
493 (GClassInitFunc) gst_input_selector_class_init,
496 sizeof (GstInputSelector),
498 (GInstanceInitFunc) gst_input_selector_init,
500 input_selector_type =
501 g_type_register_static (GST_TYPE_ELEMENT,
502 "GstInputSelector", &input_selector_info, 0);
503 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
504 "input-selector", 0, "An input stream selector element");
507 return input_selector_type;
511 gst_input_selector_base_init (GstInputSelectorClass * klass)
513 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
515 gst_element_class_set_details (element_class, &gst_input_selector_details);
516 gst_element_class_add_pad_template (element_class,
517 gst_static_pad_template_get (&gst_input_selector_sink_factory));
518 gst_element_class_add_pad_template (element_class,
519 gst_static_pad_template_get (&gst_input_selector_src_factory));
523 gst_input_selector_class_init (GstInputSelectorClass * klass)
525 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
526 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
528 parent_class = g_type_class_peek_parent (klass);
529 gobject_class->set_property =
530 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
531 gobject_class->get_property =
532 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
533 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
534 g_param_spec_string ("active-pad", "Active pad",
535 "Name of the currently" " active sink pad", NULL, G_PARAM_READWRITE));
536 gobject_class->dispose = gst_input_selector_dispose;
537 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
538 gstelement_class->release_pad = gst_input_selector_release_pad;
539 gstelement_class->change_state = gst_input_selector_change_state;
542 * GstInputSelector::block:
543 * @inputselector: the #GstInputSelector
545 * Block all sink pads in preparation for a switch. Returns the stop time of
546 * the current switch segment, as a running time, or 0 if there is no current
547 * active pad or the current active pad never received data.
549 gst_input_selector_signals[SIGNAL_BLOCK] =
550 g_signal_new ("block", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
551 G_STRUCT_OFFSET (GstInputSelectorClass, block),
552 NULL, NULL, gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
554 * GstInputSelector::switch:
555 * @inputselector: the #GstInputSelector
556 * @pad: name of pad to switch to
557 * @stop_time: running time at which to close the previous segment, or -1
558 * to use the running time of the previously active sink pad
559 * @start_time: running time at which to start the new segment, or -1 to
560 * use the running time of the newly active sink pad
562 * Switch to a new feed. The segment opened by the previously active pad, if
563 * any, will be closed, and a new segment opened before data flows again.
565 * This signal must be emitted when the element has been blocked via the <link
566 * linkend="GstInputSelector-block">block</link> signal.
568 * If you have a stream with only one switch element, such as an audio-only
569 * stream, a stream switch should be performed by first emitting the block
570 * signal, and then emitting the switch signal with -1 for the stop and start
573 * The intention of the @stop_time and @start_time arguments is to allow
574 * multiple switch elements to switch and maintain stream synchronization.
575 * When switching a stream with multiple feeds, you will need as many switch
576 * elements as you have feeds. For example, a feed with audio and video will
577 * have one switch element between the audio feeds and one for video.
579 * A switch over multiple switch elements should be performed as follows:
580 * First, emit the <link linkend="GstInputSelector-block">block</link>
581 * signal, collecting the returned values. The maximum running time returned
582 * by block should then be used as the time at which to close the previous
585 * Then, query the running times of the new audio and video pads that you will
586 * switch to. Naturally, these pads are on separate switch elements. Take the
587 * minimum running time for those streams and use it for the time at which to
588 * open the new segment.
590 * If @pad is the same as the current active pad, the element will cancel any
591 * previous block without adjusting segments.
593 gst_input_selector_signals[SIGNAL_SWITCH] =
594 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
595 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
596 NULL, NULL, gst_selector_marshal_VOID__STRING_INT64_INT64,
597 G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
599 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
600 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
604 gst_input_selector_init (GstInputSelector * sel)
606 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
607 gst_pad_set_internal_link_function (sel->srcpad,
608 GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
609 gst_pad_set_getcaps_function (sel->srcpad,
610 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
611 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
612 /* sinkpad management */
613 sel->active_sinkpad = NULL;
614 sel->nb_sinkpads = 0;
615 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
617 sel->blocked_cond = g_cond_new ();
618 sel->blocked = FALSE;
622 gst_input_selector_dispose (GObject * object)
624 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
626 if (sel->active_sinkpad) {
627 gst_object_unref (sel->active_sinkpad);
628 sel->active_sinkpad = NULL;
631 if (sel->blocked_cond) {
632 g_cond_free (sel->blocked_cond);
633 sel->blocked_cond = NULL;
636 G_OBJECT_CLASS (parent_class)->dispose (object);
639 /* Solve the following equation for B.timestamp, and set that as the segment
641 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
644 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
646 return (running_time - segment->accum) * segment->abs_rate + segment->start;
650 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
652 segment->stop = gst_segment_get_timestamp (segment, running_time);
653 segment->last_stop = -1;
657 gst_segment_set_start (GstSegment * segment, gint64 running_time)
659 segment->start = gst_segment_get_timestamp (segment, running_time);
663 gst_input_selector_set_active_pad (GstInputSelector * self,
664 const gchar * pad_name, gint64 stop_time, gint64 start_time)
667 GstSelectorPad *old, *new;
668 GstPad **active_pad_p;
670 if (strcmp (pad_name, "") != 0)
671 pad = gst_element_get_pad (GST_ELEMENT (self), pad_name);
675 GST_OBJECT_LOCK (self);
677 if (pad == self->active_sinkpad)
680 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
681 new = GST_SELECTOR_PAD_CAST (pad);
683 if (old && old->active && !self->pending_stop && stop_time >= 0) {
684 /* schedule a last_stop update if one isn't already scheduled, and a
685 segment has been pushed before. */
686 memcpy (&self->pending_stop_segment, &old->segment,
687 sizeof (self->pending_stop_segment));
688 gst_segment_set_stop (&self->pending_stop_segment, stop_time);
689 self->pending_stop = TRUE;
692 if (new && new->active && start_time >= 0) {
693 /* schedule a new segment push */
694 gst_segment_set_start (&new->segment, start_time);
695 new->segment_pending = TRUE;
698 active_pad_p = &self->active_sinkpad;
699 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
700 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
701 self->active_sinkpad);
704 GST_OBJECT_UNLOCK (self);
707 gst_object_unref (pad);
712 gst_input_selector_set_property (GObject * object, guint prop_id,
713 const GValue * value, GParamSpec * pspec)
715 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
718 case PROP_ACTIVE_PAD:
719 gst_input_selector_set_active_pad (sel,
720 g_value_get_string (value), GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
723 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
729 gst_input_selector_get_property (GObject * object, guint prop_id,
730 GValue * value, GParamSpec * pspec)
732 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
735 case PROP_ACTIVE_PAD:{
736 GST_OBJECT_LOCK (object);
737 if (sel->active_sinkpad != NULL) {
738 g_value_take_string (value, gst_pad_get_name (sel->active_sinkpad));
740 g_value_set_string (value, "");
742 GST_OBJECT_UNLOCK (object);
746 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
752 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
754 GstInputSelector *sel;
755 GstPad *otherpad = NULL;
757 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
758 GST_OBJECT_LOCK (sel);
759 if (pad == sel->srcpad)
760 otherpad = sel->active_sinkpad;
761 else if (pad == sel->active_sinkpad || !strict)
762 otherpad = sel->srcpad;
764 gst_object_ref (otherpad);
765 GST_OBJECT_UNLOCK (sel);
766 gst_object_unref (sel);
771 gst_input_selector_getcaps (GstPad * pad)
777 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
778 parent = gst_object_get_parent (GST_OBJECT (pad));
780 GST_DEBUG_OBJECT (parent,
781 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
782 caps = gst_caps_new_any ();
784 GST_DEBUG_OBJECT (parent,
785 "Pad %s:%s is linked (to %s:%s), returning peer caps",
786 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
787 /* if the peer has caps, use those. If the pad is not linked, this function
788 * returns NULL and we return ANY */
789 if (!(caps = gst_pad_peer_get_caps (otherpad)))
790 caps = gst_caps_new_any ();
791 gst_object_unref (otherpad);
794 gst_object_unref (parent);
798 /* check if the pad is the active sinkpad */
800 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
802 GstSelectorPad *selpad;
805 selpad = GST_SELECTOR_PAD_CAST (pad);
807 GST_OBJECT_LOCK (sel);
808 res = (pad == sel->active_sinkpad);
809 GST_OBJECT_UNLOCK (sel);
814 /* Get or create the active sinkpad */
816 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
818 GstPad *active_sinkpad;
819 GstSelectorPad *selpad;
821 selpad = GST_SELECTOR_PAD_CAST (pad);
823 GST_OBJECT_LOCK (sel);
824 selpad->active = TRUE;
825 active_sinkpad = sel->active_sinkpad;
826 if (active_sinkpad == NULL) {
827 /* first pad we get an alloc on becomes the activated pad by default */
828 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
829 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
831 GST_OBJECT_UNLOCK (sel);
833 return active_sinkpad;
837 gst_input_selector_get_linked_pads (GstPad * pad)
841 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
844 /* need to drop the ref, internal linked pads is not MT safe */
845 gst_object_unref (otherpad);
846 return g_list_append (NULL, otherpad);
850 gst_input_selector_request_new_pad (GstElement * element,
851 GstPadTemplate * templ, const gchar * unused)
853 GstInputSelector *sel;
855 GstPad *sinkpad = NULL;
857 sel = GST_INPUT_SELECTOR (element);
858 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
859 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->nb_sinkpads);
860 GST_OBJECT_LOCK (sel);
861 name = g_strdup_printf ("sink%d", sel->nb_sinkpads++);
862 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
863 "name", name, "direction", templ->direction, "template", templ, NULL);
865 GST_OBJECT_UNLOCK (sel);
867 gst_pad_set_event_function (sinkpad,
868 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
869 gst_pad_set_getcaps_function (sinkpad,
870 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
871 gst_pad_set_chain_function (sinkpad,
872 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
873 gst_pad_set_internal_link_function (sinkpad,
874 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
875 gst_pad_set_bufferalloc_function (sinkpad,
876 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
878 gst_pad_set_active (sinkpad, TRUE);
879 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
884 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
886 GstInputSelector *sel;
888 sel = GST_INPUT_SELECTOR (element);
889 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
891 GST_OBJECT_LOCK (sel);
892 /* if the pad was the active pad, makes us select a new one */
893 if (sel->active_sinkpad == pad) {
894 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
895 sel->active_sinkpad = NULL;
897 GST_OBJECT_UNLOCK (sel);
899 gst_pad_set_active (pad, FALSE);
900 gst_element_remove_pad (GST_ELEMENT (sel), pad);
903 static GstStateChangeReturn
904 gst_input_selector_change_state (GstElement * element,
905 GstStateChange transition)
907 GstInputSelector *self = GST_INPUT_SELECTOR (element);
908 GstStateChangeReturn result;
910 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
912 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
913 GST_OBJECT_LOCK (self);
914 self->blocked = FALSE;
915 g_cond_broadcast (self->blocked_cond);
916 GST_OBJECT_UNLOCK (self);
923 gst_input_selector_block (GstInputSelector * self)
926 GstSelectorPad *spad;
928 GST_OBJECT_LOCK (self);
931 GST_WARNING_OBJECT (self, "switch already blocked");
933 self->blocked = TRUE;
934 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
936 if (self->active_sinkpad)
937 ret = gst_selector_pad_get_running_time (spad);
939 GST_DEBUG_OBJECT (self, "no active pad while blocking");
941 GST_OBJECT_UNLOCK (self);
947 gst_input_selector_push_pending_stop (GstInputSelector * self)
949 GstEvent *event = NULL;
951 GST_OBJECT_LOCK (self);
953 if (G_UNLIKELY (self->pending_stop)) {
954 GstSegment *seg = &self->pending_stop_segment;
956 event = gst_event_new_new_segment_full (TRUE, seg->rate,
957 seg->applied_rate, seg->format, seg->start, seg->stop, seg->stop);
959 self->pending_stop = FALSE;
962 GST_OBJECT_UNLOCK (self);
965 gst_pad_push_event (self->srcpad, event);
968 /* stop_time and start_time are running times */
970 gst_input_selector_switch (GstInputSelector * self, const gchar * pad_name,
971 gint64 stop_time, gint64 start_time)
973 g_return_if_fail (self->blocked == TRUE);
975 gst_input_selector_set_active_pad (self, pad_name, stop_time, start_time);
977 GST_OBJECT_LOCK (self);
978 self->blocked = FALSE;
979 g_cond_broadcast (self->blocked_cond);
980 GST_OBJECT_UNLOCK (self);