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",
78 PROP_PAD_RUNNING_TIME,
91 static guint gst_input_selector_signals[LAST_SIGNAL] = { 0 };
93 static gboolean gst_input_selector_is_active_sinkpad (GstInputSelector * sel,
95 static GstPad *gst_input_selector_activate_sinkpad (GstInputSelector * sel,
97 static GstPad *gst_input_selector_get_linked_pad (GstPad * pad,
99 static void gst_input_selector_push_pending_stop (GstInputSelector * self);
100 static gboolean gst_input_selector_check_eos (GstElement * selector);
102 #define GST_TYPE_SELECTOR_PAD \
103 (gst_selector_pad_get_type())
104 #define GST_SELECTOR_PAD(obj) \
105 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
106 #define GST_SELECTOR_PAD_CLASS(klass) \
107 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
108 #define GST_IS_SELECTOR_PAD(obj) \
109 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
110 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
111 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
112 #define GST_SELECTOR_PAD_CAST(obj) \
113 ((GstSelectorPad *)(obj))
115 typedef struct _GstSelectorPad GstSelectorPad;
116 typedef struct _GstSelectorPadClass GstSelectorPadClass;
118 struct _GstSelectorPad
122 gboolean active; /* when buffer have passed the pad */
123 gboolean eos; /* when EOS has been received */
124 GstSegment segment; /* the current segment on the pad */
125 GstTagList *tags; /* last tags received on the pad */
127 gboolean segment_pending;
130 struct _GstSelectorPadClass
135 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
136 static void gst_selector_pad_init (GstSelectorPad * pad);
137 static void gst_selector_pad_finalize (GObject * object);
138 static void gst_selector_pad_get_property (GObject * object,
139 guint prop_id, GValue * value, GParamSpec * pspec);
141 static GstPadClass *selector_pad_parent_class = NULL;
143 static gint64 gst_selector_pad_get_running_time (GstSelectorPad * pad);
144 static void gst_selector_pad_reset (GstSelectorPad * pad);
145 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
146 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
147 static GList *gst_selector_pad_get_linked_pads (GstPad * pad);
148 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
149 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
150 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
153 gst_selector_pad_get_type (void)
155 static GType selector_pad_type = 0;
157 if (!selector_pad_type) {
158 static const GTypeInfo selector_pad_info = {
159 sizeof (GstSelectorPadClass),
162 (GClassInitFunc) gst_selector_pad_class_init,
165 sizeof (GstSelectorPad),
167 (GInstanceInitFunc) gst_selector_pad_init,
171 g_type_register_static (GST_TYPE_PAD, "GstSelectorPad",
172 &selector_pad_info, 0);
174 return selector_pad_type;
178 gst_selector_pad_class_init (GstSelectorPadClass * klass)
180 GObjectClass *gobject_class;
182 gobject_class = (GObjectClass *) klass;
184 selector_pad_parent_class = g_type_class_peek_parent (klass);
186 gobject_class->finalize = gst_selector_pad_finalize;
188 gobject_class->get_property =
189 GST_DEBUG_FUNCPTR (gst_selector_pad_get_property);
191 g_object_class_install_property (gobject_class, PROP_PAD_RUNNING_TIME,
192 g_param_spec_int64 ("running-time", "Running time",
193 "Running time of stream on pad", 0, G_MAXINT64, 0, G_PARAM_READABLE));
194 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
195 g_param_spec_boxed ("tags", "Tags",
196 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
198 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
199 g_param_spec_boolean ("active", "Active",
200 "If the pad is currently active", FALSE, G_PARAM_READABLE));
204 gst_selector_pad_init (GstSelectorPad * pad)
206 gst_selector_pad_reset (pad);
210 gst_selector_pad_finalize (GObject * object)
214 pad = GST_SELECTOR_PAD_CAST (object);
217 gst_tag_list_free (pad->tags);
219 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
223 gst_selector_pad_get_property (GObject * object, guint prop_id,
224 GValue * value, GParamSpec * pspec)
226 GstSelectorPad *spad = GST_SELECTOR_PAD_CAST (object);
229 case PROP_PAD_RUNNING_TIME:
230 g_value_set_int64 (value, gst_selector_pad_get_running_time (spad));
233 GST_OBJECT_LOCK (object);
234 g_value_set_boxed (value, spad->tags);
235 GST_OBJECT_UNLOCK (object);
237 case PROP_PAD_ACTIVE:
239 GstInputSelector *sel;
241 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (spad));
242 g_value_set_boolean (value, gst_input_selector_is_active_sinkpad (sel,
243 GST_PAD_CAST (spad)));
244 gst_object_unref (sel);
248 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
254 gst_selector_pad_get_running_time (GstSelectorPad * pad)
258 GST_OBJECT_LOCK (pad);
261 gint64 last_stop = pad->segment.last_stop;
264 ret = gst_segment_to_running_time (&pad->segment, GST_FORMAT_TIME,
268 GST_OBJECT_UNLOCK (pad);
270 GST_DEBUG_OBJECT (pad, "running time: %" GST_TIME_FORMAT,
271 GST_TIME_ARGS (ret));
277 gst_selector_pad_reset (GstSelectorPad * pad)
281 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
284 /* strictly get the linked pad from the sinkpad. If the pad is active we return
285 * the srcpad else we return NULL */
287 gst_selector_pad_get_linked_pads (GstPad * pad)
291 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
295 /* need to drop the ref, internal linked pads is not MT safe */
296 gst_object_unref (otherpad);
298 return g_list_append (NULL, otherpad);
302 gst_selector_pad_event (GstPad * pad, GstEvent * event)
305 gboolean forward = TRUE;
306 GstInputSelector *sel;
307 GstSelectorPad *selpad;
309 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
310 selpad = GST_SELECTOR_PAD_CAST (pad);
312 /* only forward if we are dealing with the active sinkpad */
313 forward = gst_input_selector_is_active_sinkpad (sel, pad);
315 /* forward all events in select_all mode by default */
316 if (sel->select_all) {
320 switch (GST_EVENT_TYPE (event)) {
321 case GST_EVENT_FLUSH_STOP:
322 gst_selector_pad_reset (selpad);
324 case GST_EVENT_NEWSEGMENT:
329 gint64 start, stop, time;
331 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
332 &start, &stop, &time);
334 GST_DEBUG_OBJECT (pad,
335 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
337 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
338 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
340 gst_segment_set_newsegment_full (&selpad->segment, update,
341 rate, arate, format, start, stop, time);
342 /* if we are not going to forward the segment, mark the segment as
345 selpad->segment_pending = TRUE;
352 GST_OBJECT_LOCK (selpad);
354 gst_tag_list_free (selpad->tags);
355 gst_event_parse_tag (event, &tags);
357 tags = gst_tag_list_copy (tags);
359 GST_DEBUG_OBJECT (pad, "received tags %" GST_PTR_FORMAT, selpad->tags);
360 GST_OBJECT_UNLOCK (selpad);
365 GST_DEBUG_OBJECT (pad, "received EOS");
366 /* don't forward eos in select_all mode until all sink pads have eos */
367 if (sel->select_all && !gst_input_selector_check_eos (GST_ELEMENT (sel))) {
375 GST_DEBUG_OBJECT (pad, "forwarding event");
376 res = gst_pad_push_event (sel->srcpad, event);
378 gst_event_unref (event);
380 gst_object_unref (sel);
386 gst_selector_pad_getcaps (GstPad * pad)
388 GstInputSelector *sel;
391 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
393 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
394 caps = gst_pad_peer_get_caps (sel->srcpad);
396 caps = gst_caps_new_any ();
398 gst_object_unref (sel);
404 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
405 guint size, GstCaps * caps, GstBuffer ** buf)
407 GstInputSelector *sel;
408 GstFlowReturn result;
409 GstPad *active_sinkpad;
411 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
413 GST_DEBUG_OBJECT (pad, "received alloc");
415 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
417 /* Fallback allocation for buffers from pads except the selected one */
418 if (pad != active_sinkpad && !sel->select_all) {
419 GST_DEBUG_OBJECT (sel,
420 "Pad %s:%s is not selected. Performing fallback allocation",
421 GST_DEBUG_PAD_NAME (pad));
424 result = GST_FLOW_OK;
426 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
428 /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
429 * allocation. This should NOT be necessary, because playbin should
430 * properly block the source pad from running until it's finished hooking
431 * everything up, but playbin needs refactoring first. */
432 if (result == GST_FLOW_NOT_LINKED) {
433 GST_DEBUG_OBJECT (sel,
434 "No peer pad yet - performing fallback allocation for pad %s:%s",
435 GST_DEBUG_PAD_NAME (pad));
438 result = GST_FLOW_OK;
442 gst_object_unref (sel);
448 gst_input_selector_wait (GstInputSelector * self, GstPad * pad)
452 GST_OBJECT_LOCK (self);
454 while (self->blocked)
455 g_cond_wait (self->blocked_cond, GST_OBJECT_GET_LOCK (self));
457 GST_OBJECT_UNLOCK (self);
459 GST_OBJECT_LOCK (pad);
460 flushing = GST_PAD_IS_FLUSHING (pad);
461 GST_OBJECT_UNLOCK (pad);
467 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
469 GstInputSelector *sel;
471 GstPad *active_sinkpad;
472 GstSelectorPad *selpad;
473 GstClockTime end_time, duration;
476 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
477 selpad = GST_SELECTOR_PAD_CAST (pad);
478 seg = &selpad->segment;
480 if (gst_input_selector_wait (sel, pad))
483 GST_DEBUG_OBJECT (pad, "chain, getting active pad");
485 active_sinkpad = gst_input_selector_activate_sinkpad (sel, pad);
487 end_time = GST_BUFFER_TIMESTAMP (buf);
488 if (GST_CLOCK_TIME_IS_VALID (end_time)) {
489 duration = GST_BUFFER_DURATION (buf);
490 if (GST_CLOCK_TIME_IS_VALID (duration))
491 end_time += duration;
492 GST_DEBUG_OBJECT (pad, "received end time %" GST_TIME_FORMAT,
493 GST_TIME_ARGS (end_time));
494 gst_segment_set_last_stop (seg, seg->format, end_time);
497 /* Ignore buffers from pads except the selected one */
498 if (pad != active_sinkpad && !sel->select_all)
501 gst_input_selector_push_pending_stop (sel);
503 /* if we have a pending segment, push it out now */
504 if (selpad->segment_pending) {
506 GST_DEBUG_OBJECT (pad,
507 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
509 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
510 G_GINT64_FORMAT, FALSE, seg->rate, seg->applied_rate, seg->format,
511 seg->start, seg->stop, seg->time);
513 gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
514 seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
517 selpad->segment_pending = FALSE;
521 GST_DEBUG_OBJECT (pad, "Forwarding buffer %p from pad %s:%s", buf,
522 GST_DEBUG_PAD_NAME (pad));
523 res = gst_pad_push (sel->srcpad, buf);
525 gst_object_unref (sel);
527 /* dropped buffers */
530 GST_DEBUG_OBJECT (pad, "Ignoring buffer %p", buf);
531 gst_buffer_unref (buf);
537 static void gst_input_selector_dispose (GObject * object);
538 static void gst_input_selector_init (GstInputSelector * sel);
539 static void gst_input_selector_base_init (GstInputSelectorClass * klass);
540 static void gst_input_selector_class_init (GstInputSelectorClass * klass);
541 static void gst_input_selector_set_property (GObject * object,
542 guint prop_id, const GValue * value, GParamSpec * pspec);
543 static void gst_input_selector_get_property (GObject * object,
544 guint prop_id, GValue * value, GParamSpec * pspec);
545 static GstPad *gst_input_selector_request_new_pad (GstElement * element,
546 GstPadTemplate * templ, const gchar * unused);
547 static void gst_input_selector_release_pad (GstElement * element, GstPad * pad);
548 static GstStateChangeReturn gst_input_selector_change_state (GstElement *
549 element, GstStateChange transition);
550 static GList *gst_input_selector_get_linked_pads (GstPad * pad);
551 static GstCaps *gst_input_selector_getcaps (GstPad * pad);
552 static gint64 gst_input_selector_block (GstInputSelector * self);
553 static void gst_input_selector_switch (GstInputSelector * self,
554 GstPad * pad, gint64 stop_time, gint64 start_time);
556 static GstElementClass *parent_class = NULL;
559 gst_input_selector_get_type (void)
561 static GType input_selector_type = 0;
563 if (!input_selector_type) {
564 static const GTypeInfo input_selector_info = {
565 sizeof (GstInputSelectorClass),
566 (GBaseInitFunc) gst_input_selector_base_init,
568 (GClassInitFunc) gst_input_selector_class_init,
571 sizeof (GstInputSelector),
573 (GInstanceInitFunc) gst_input_selector_init,
575 input_selector_type =
576 g_type_register_static (GST_TYPE_ELEMENT,
577 "GstInputSelector", &input_selector_info, 0);
578 GST_DEBUG_CATEGORY_INIT (input_selector_debug,
579 "input-selector", 0, "An input stream selector element");
582 return input_selector_type;
586 gst_input_selector_base_init (GstInputSelectorClass * klass)
588 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
590 gst_element_class_set_details (element_class, &gst_input_selector_details);
591 gst_element_class_add_pad_template (element_class,
592 gst_static_pad_template_get (&gst_input_selector_sink_factory));
593 gst_element_class_add_pad_template (element_class,
594 gst_static_pad_template_get (&gst_input_selector_src_factory));
598 gst_input_selector_class_init (GstInputSelectorClass * klass)
600 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
601 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
603 parent_class = g_type_class_peek_parent (klass);
605 gobject_class->dispose = gst_input_selector_dispose;
607 gobject_class->set_property =
608 GST_DEBUG_FUNCPTR (gst_input_selector_set_property);
609 gobject_class->get_property =
610 GST_DEBUG_FUNCPTR (gst_input_selector_get_property);
612 g_object_class_install_property (gobject_class, PROP_N_PADS,
613 g_param_spec_uint ("n-pads", "Number of Pads",
614 "The number of sink pads", 0, G_MAXUINT, 0, G_PARAM_READABLE));
616 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
617 g_param_spec_object ("active-pad", "Active pad",
618 "The currently active sink pad", GST_TYPE_PAD, G_PARAM_READWRITE));
620 g_object_class_install_property (gobject_class, PROP_SELECT_ALL,
621 g_param_spec_boolean ("select-all", "Select all mode",
622 "Forwards data from all input pads", FALSE, G_PARAM_READWRITE));
625 * GstInputSelector::block:
626 * @inputselector: the #GstInputSelector
628 * Block all sink pads in preparation for a switch. Returns the stop time of
629 * the current switch segment, as a running time, or 0 if there is no current
630 * active pad or the current active pad never received data.
632 gst_input_selector_signals[SIGNAL_BLOCK] =
633 g_signal_new ("block", G_TYPE_FROM_CLASS (klass),
634 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
635 G_STRUCT_OFFSET (GstInputSelectorClass, block), NULL, NULL,
636 gst_selector_marshal_INT64__VOID, G_TYPE_INT64, 0);
638 * GstInputSelector::switch:
639 * @inputselector: the #GstInputSelector
640 * @pad: the pad to switch to
641 * @stop_time: running time at which to close the previous segment, or -1
642 * to use the running time of the previously active sink pad
643 * @start_time: running time at which to start the new segment, or -1 to
644 * use the running time of the newly active sink pad
646 * Switch to a new feed. The segment opened by the previously active pad, if
647 * any, will be closed, and a new segment opened before data flows again.
649 * This signal must be emitted when the element has been blocked via the <link
650 * linkend="GstInputSelector-block">block</link> signal.
652 * If you have a stream with only one switch element, such as an audio-only
653 * stream, a stream switch should be performed by first emitting the block
654 * signal, and then emitting the switch signal with -1 for the stop and start
657 * The intention of the @stop_time and @start_time arguments is to allow
658 * multiple switch elements to switch and maintain stream synchronization.
659 * When switching a stream with multiple feeds, you will need as many switch
660 * elements as you have feeds. For example, a feed with audio and video will
661 * have one switch element between the audio feeds and one for video.
663 * A switch over multiple switch elements should be performed as follows:
664 * First, emit the <link linkend="GstInputSelector-block">block</link>
665 * signal, collecting the returned values. The maximum running time returned
666 * by block should then be used as the time at which to close the previous
669 * Then, query the running times of the new audio and video pads that you will
670 * switch to. Naturally, these pads are on separate switch elements. Take the
671 * minimum running time for those streams and use it for the time at which to
672 * open the new segment.
674 * If @pad is the same as the current active pad, the element will cancel any
675 * previous block without adjusting segments.
677 gst_input_selector_signals[SIGNAL_SWITCH] =
678 g_signal_new ("switch", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
679 G_STRUCT_OFFSET (GstInputSelectorClass, switch_),
680 NULL, NULL, gst_selector_marshal_VOID__OBJECT_INT64_INT64,
681 G_TYPE_NONE, 3, GST_TYPE_PAD, G_TYPE_INT64, G_TYPE_INT64);
683 gstelement_class->request_new_pad = gst_input_selector_request_new_pad;
684 gstelement_class->release_pad = gst_input_selector_release_pad;
685 gstelement_class->change_state = gst_input_selector_change_state;
687 klass->block = GST_DEBUG_FUNCPTR (gst_input_selector_block);
688 /* note the underscore because switch is a keyword otherwise */
689 klass->switch_ = GST_DEBUG_FUNCPTR (gst_input_selector_switch);
693 gst_input_selector_init (GstInputSelector * sel)
695 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
696 gst_pad_set_internal_link_function (sel->srcpad,
697 GST_DEBUG_FUNCPTR (gst_input_selector_get_linked_pads));
698 gst_pad_set_getcaps_function (sel->srcpad,
699 GST_DEBUG_FUNCPTR (gst_input_selector_getcaps));
700 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
701 /* sinkpad management */
702 sel->active_sinkpad = NULL;
704 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
706 sel->blocked_cond = g_cond_new ();
707 sel->blocked = FALSE;
709 sel->select_all = FALSE;
713 gst_input_selector_dispose (GObject * object)
715 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
717 if (sel->active_sinkpad) {
718 gst_object_unref (sel->active_sinkpad);
719 sel->active_sinkpad = NULL;
722 if (sel->blocked_cond) {
723 g_cond_free (sel->blocked_cond);
724 sel->blocked_cond = NULL;
727 G_OBJECT_CLASS (parent_class)->dispose (object);
730 /* Solve the following equation for B.timestamp, and set that as the segment
732 * B.running_time = (B.timestamp - NS.start) / NS.abs_rate + NS.accum
735 gst_segment_get_timestamp (GstSegment * segment, gint64 running_time)
737 return (running_time - segment->accum) * segment->abs_rate + segment->start;
741 gst_segment_set_stop (GstSegment * segment, gint64 running_time)
743 segment->stop = gst_segment_get_timestamp (segment, running_time);
744 segment->last_stop = -1;
748 gst_segment_set_start (GstSegment * segment, gint64 running_time)
750 segment->start = gst_segment_get_timestamp (segment, running_time);
754 gst_input_selector_set_active_pad (GstInputSelector * self,
755 GstPad * pad, gint64 stop_time, gint64 start_time)
757 GstSelectorPad *old, *new;
758 GstPad **active_pad_p;
760 GST_OBJECT_LOCK (self);
762 if (pad == self->active_sinkpad)
765 old = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
766 new = GST_SELECTOR_PAD_CAST (pad);
768 if (old && old->active && !self->pending_stop && stop_time >= 0) {
769 /* schedule a last_stop update if one isn't already scheduled, and a
770 segment has been pushed before. */
771 memcpy (&self->pending_stop_segment, &old->segment,
772 sizeof (self->pending_stop_segment));
773 GST_DEBUG_OBJECT (self, "setting stop_time to %" G_GINT64_FORMAT,
775 gst_segment_set_stop (&self->pending_stop_segment, stop_time);
776 self->pending_stop = TRUE;
779 if (new && new->active && start_time >= 0) {
780 GST_DEBUG_OBJECT (self, "setting start_time to %" G_GINT64_FORMAT,
782 /* schedule a new segment push */
783 gst_segment_set_start (&new->segment, start_time);
784 new->segment_pending = TRUE;
787 active_pad_p = &self->active_sinkpad;
788 gst_object_replace ((GstObject **) active_pad_p, GST_OBJECT_CAST (pad));
789 GST_DEBUG_OBJECT (self, "New active pad is %" GST_PTR_FORMAT,
790 self->active_sinkpad);
793 GST_OBJECT_UNLOCK (self);
797 gst_input_selector_set_property (GObject * object, guint prop_id,
798 const GValue * value, GParamSpec * pspec)
800 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
803 case PROP_ACTIVE_PAD:
807 pad = g_value_get_object (value);
809 gst_input_selector_set_active_pad (sel, pad,
810 GST_CLOCK_TIME_NONE, GST_CLOCK_TIME_NONE);
813 case PROP_SELECT_ALL:
814 sel->select_all = g_value_get_boolean (value);
817 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
823 gst_input_selector_get_property (GObject * object, guint prop_id,
824 GValue * value, GParamSpec * pspec)
826 GstInputSelector *sel = GST_INPUT_SELECTOR (object);
830 GST_OBJECT_LOCK (object);
831 g_value_set_uint (value, sel->n_pads);
832 GST_OBJECT_UNLOCK (object);
834 case PROP_ACTIVE_PAD:
835 GST_OBJECT_LOCK (object);
836 g_value_set_object (value, sel->active_sinkpad);
837 GST_OBJECT_UNLOCK (object);
839 case PROP_SELECT_ALL:
840 g_value_set_boolean (value, sel->select_all);
843 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
849 gst_input_selector_get_linked_pad (GstPad * pad, gboolean strict)
851 GstInputSelector *sel;
852 GstPad *otherpad = NULL;
854 sel = GST_INPUT_SELECTOR (gst_pad_get_parent (pad));
855 GST_OBJECT_LOCK (sel);
856 if (pad == sel->srcpad)
857 otherpad = sel->active_sinkpad;
858 else if (pad == sel->active_sinkpad || !strict)
859 otherpad = sel->srcpad;
861 gst_object_ref (otherpad);
862 GST_OBJECT_UNLOCK (sel);
863 gst_object_unref (sel);
868 gst_input_selector_getcaps (GstPad * pad)
874 parent = gst_object_get_parent (GST_OBJECT (pad));
875 if (GST_INPUT_SELECTOR (parent)->select_all) {
876 caps = gst_pad_proxy_getcaps (pad);
880 otherpad = gst_input_selector_get_linked_pad (pad, FALSE);
882 GST_DEBUG_OBJECT (parent,
883 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
884 caps = gst_caps_new_any ();
886 GST_DEBUG_OBJECT (parent,
887 "Pad %s:%s is linked (to %s:%s), returning peer caps",
888 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
889 /* if the peer has caps, use those. If the pad is not linked, this function
890 * returns NULL and we return ANY */
891 if (!(caps = gst_pad_peer_get_caps (otherpad)))
892 caps = gst_caps_new_any ();
893 gst_object_unref (otherpad);
897 gst_object_unref (parent);
901 /* check if the pad is the active sinkpad */
903 gst_input_selector_is_active_sinkpad (GstInputSelector * sel, GstPad * pad)
905 GstSelectorPad *selpad;
908 selpad = GST_SELECTOR_PAD_CAST (pad);
910 GST_OBJECT_LOCK (sel);
911 res = (pad == sel->active_sinkpad);
912 GST_OBJECT_UNLOCK (sel);
917 /* Get or create the active sinkpad */
919 gst_input_selector_activate_sinkpad (GstInputSelector * sel, GstPad * pad)
921 GstPad *active_sinkpad;
922 GstSelectorPad *selpad;
924 selpad = GST_SELECTOR_PAD_CAST (pad);
926 GST_OBJECT_LOCK (sel);
927 selpad->active = TRUE;
928 active_sinkpad = sel->active_sinkpad;
929 if (active_sinkpad == NULL) {
930 /* first pad we get an alloc on becomes the activated pad by default */
931 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
932 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
934 GST_OBJECT_UNLOCK (sel);
936 return active_sinkpad;
940 gst_input_selector_get_linked_pads (GstPad * pad)
944 otherpad = gst_input_selector_get_linked_pad (pad, TRUE);
947 /* need to drop the ref, internal linked pads is not MT safe */
948 gst_object_unref (otherpad);
949 return g_list_append (NULL, otherpad);
953 gst_input_selector_request_new_pad (GstElement * element,
954 GstPadTemplate * templ, const gchar * unused)
956 GstInputSelector *sel;
958 GstPad *sinkpad = NULL;
960 sel = GST_INPUT_SELECTOR (element);
961 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
962 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
963 GST_OBJECT_LOCK (sel);
964 name = g_strdup_printf ("sink%d", sel->padcount++);
965 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
966 "name", name, "direction", templ->direction, "template", templ, NULL);
969 GST_OBJECT_UNLOCK (sel);
971 gst_pad_set_event_function (sinkpad,
972 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
973 gst_pad_set_getcaps_function (sinkpad,
974 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
975 gst_pad_set_chain_function (sinkpad,
976 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
977 gst_pad_set_internal_link_function (sinkpad,
978 GST_DEBUG_FUNCPTR (gst_selector_pad_get_linked_pads));
979 gst_pad_set_bufferalloc_function (sinkpad,
980 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
982 gst_pad_set_active (sinkpad, TRUE);
983 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
989 gst_input_selector_release_pad (GstElement * element, GstPad * pad)
991 GstInputSelector *sel;
993 sel = GST_INPUT_SELECTOR (element);
994 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
996 GST_OBJECT_LOCK (sel);
997 /* if the pad was the active pad, makes us select a new one */
998 if (sel->active_sinkpad == pad) {
999 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1000 sel->active_sinkpad = NULL;
1003 GST_OBJECT_UNLOCK (sel);
1005 gst_pad_set_active (pad, FALSE);
1006 gst_element_remove_pad (GST_ELEMENT (sel), pad);
1009 static GstStateChangeReturn
1010 gst_input_selector_change_state (GstElement * element,
1011 GstStateChange transition)
1013 GstInputSelector *self = GST_INPUT_SELECTOR (element);
1014 GstStateChangeReturn result;
1016 result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1018 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) {
1019 GST_OBJECT_LOCK (self);
1020 self->blocked = FALSE;
1021 g_cond_broadcast (self->blocked_cond);
1022 GST_OBJECT_UNLOCK (self);
1029 gst_input_selector_block (GstInputSelector * self)
1032 GstSelectorPad *spad;
1034 GST_OBJECT_LOCK (self);
1037 GST_WARNING_OBJECT (self, "switch already blocked");
1039 self->blocked = TRUE;
1040 spad = GST_SELECTOR_PAD_CAST (self->active_sinkpad);
1042 if (self->active_sinkpad)
1043 ret = gst_selector_pad_get_running_time (spad);
1045 GST_DEBUG_OBJECT (self, "no active pad while blocking");
1047 GST_OBJECT_UNLOCK (self);
1053 gst_input_selector_push_pending_stop (GstInputSelector * self)
1055 GstEvent *event = NULL;
1057 GST_OBJECT_LOCK (self);
1059 if (G_UNLIKELY (self->pending_stop)) {
1060 GstSegment *seg = &self->pending_stop_segment;
1062 GST_DEBUG_OBJECT (self,
1063 "pushing NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1065 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1066 G_GINT64_FORMAT, TRUE, seg->rate, seg->applied_rate, seg->format,
1067 seg->start, seg->stop, seg->time);
1069 event = gst_event_new_new_segment_full (TRUE, seg->rate,
1070 seg->applied_rate, seg->format, seg->start, seg->stop, seg->time);
1072 self->pending_stop = FALSE;
1075 GST_OBJECT_UNLOCK (self);
1078 gst_pad_push_event (self->srcpad, event);
1081 /* stop_time and start_time are running times */
1083 gst_input_selector_switch (GstInputSelector * self, GstPad * pad,
1084 gint64 stop_time, gint64 start_time)
1086 g_return_if_fail (self->blocked == TRUE);
1088 gst_input_selector_set_active_pad (self, pad, stop_time, start_time);
1090 GST_OBJECT_LOCK (self);
1091 self->blocked = FALSE;
1092 g_cond_broadcast (self->blocked_cond);
1093 GST_OBJECT_UNLOCK (self);
1097 gst_input_selector_check_eos (GstElement * selector)
1099 GstIterator *it = gst_element_iterate_sink_pads (selector);
1100 GstIteratorResult ires;
1102 gboolean done = FALSE, is_eos = FALSE;
1103 GstSelectorPad *pad;
1106 ires = gst_iterator_next (it, &item);
1108 case GST_ITERATOR_DONE:
1109 GST_INFO_OBJECT (selector, "all sink pads have eos");
1113 case GST_ITERATOR_OK:
1114 pad = GST_SELECTOR_PAD_CAST (item);
1119 case GST_ITERATOR_RESYNC:
1120 gst_iterator_resync (it);
1127 gst_iterator_free (it);