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>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 #include "gststreamselector.h"
31 GST_DEBUG_CATEGORY_STATIC (stream_selector_debug);
32 #define GST_CAT_DEFAULT stream_selector_debug
34 static GstStaticPadTemplate gst_stream_selector_sink_factory =
35 GST_STATIC_PAD_TEMPLATE ("sink%d",
40 static GstStaticPadTemplate gst_stream_selector_src_factory =
41 GST_STATIC_PAD_TEMPLATE ("src",
54 static gboolean gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel,
56 static GstPad *gst_stream_selector_activate_sinkpad (GstStreamSelector * sel,
58 static GstPad *gst_stream_selector_get_linked_pad (GstPad * pad,
61 #define GST_TYPE_SELECTOR_PAD \
62 (gst_selector_pad_get_type())
63 #define GST_SELECTOR_PAD(obj) \
64 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_SELECTOR_PAD, GstSelectorPad))
65 #define GST_SELECTOR_PAD_CLASS(klass) \
66 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_SELECTOR_PAD, GstSelectorPadClass))
67 #define GST_IS_SELECTOR_PAD(obj) \
68 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_SELECTOR_PAD))
69 #define GST_IS_SELECTOR_PAD_CLASS(klass) \
70 (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_SELECTOR_PAD))
71 #define GST_SELECTOR_PAD_CAST(obj) \
72 ((GstSelectorPad *)(obj))
74 typedef struct _GstSelectorPad GstSelectorPad;
75 typedef struct _GstSelectorPadClass GstSelectorPadClass;
77 struct _GstSelectorPad
83 gboolean segment_pending;
88 struct _GstSelectorPadClass
93 static void gst_selector_pad_class_init (GstSelectorPadClass * klass);
94 static void gst_selector_pad_init (GstSelectorPad * pad);
95 static void gst_selector_pad_finalize (GObject * object);
97 static void gst_selector_pad_get_property (GObject * object,
98 guint prop_id, GValue * value, GParamSpec * pspec);
100 static GstPadClass *selector_pad_parent_class = NULL;
102 static void gst_selector_pad_reset (GstSelectorPad * pad);
103 static gboolean gst_selector_pad_event (GstPad * pad, GstEvent * event);
104 static GstCaps *gst_selector_pad_getcaps (GstPad * pad);
105 static GstFlowReturn gst_selector_pad_chain (GstPad * pad, GstBuffer * buf);
106 static GstFlowReturn gst_selector_pad_bufferalloc (GstPad * pad,
107 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
118 gst_selector_pad_get_type (void)
120 static GType selector_pad_type = 0;
122 if (!selector_pad_type) {
123 static const GTypeInfo selector_pad_info = {
124 sizeof (GstSelectorPadClass),
127 (GClassInitFunc) gst_selector_pad_class_init,
130 sizeof (GstSelectorPad),
132 (GInstanceInitFunc) gst_selector_pad_init,
136 g_type_register_static (GST_TYPE_PAD, "GstPlaybinSelectorPad",
137 &selector_pad_info, 0);
139 return selector_pad_type;
143 gst_selector_pad_class_init (GstSelectorPadClass * klass)
145 GObjectClass *gobject_class;
147 gobject_class = (GObjectClass *) klass;
149 selector_pad_parent_class = g_type_class_peek_parent (klass);
151 gobject_class->finalize = gst_selector_pad_finalize;
152 gobject_class->get_property = gst_selector_pad_get_property;
154 g_object_class_install_property (gobject_class, PROP_PAD_TAGS,
155 g_param_spec_boxed ("tags", "Tags",
156 "The currently active tags on the pad", GST_TYPE_TAG_LIST,
157 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_PAD_ACTIVE,
160 g_param_spec_boolean ("active", "Active",
161 "If the pad is currently active", FALSE,
162 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
166 gst_selector_pad_init (GstSelectorPad * pad)
168 gst_selector_pad_reset (pad);
172 gst_selector_pad_finalize (GObject * object)
176 pad = GST_SELECTOR_PAD_CAST (object);
179 gst_tag_list_free (pad->tags);
181 G_OBJECT_CLASS (selector_pad_parent_class)->finalize (object);
185 gst_selector_pad_get_property (GObject * object,
186 guint prop_id, GValue * value, GParamSpec * pspec)
190 pad = GST_SELECTOR_PAD (object);
194 GST_OBJECT_LOCK (object);
195 g_value_set_boxed (value, pad->tags);
196 GST_OBJECT_UNLOCK (object);
198 case PROP_PAD_ACTIVE:
200 GstStreamSelector *sel;
202 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
203 g_value_set_boolean (value, gst_stream_selector_is_active_sinkpad (sel,
204 GST_PAD_CAST (pad)));
205 gst_object_unref (sel);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_selector_pad_reset (GstSelectorPad * pad)
219 gst_segment_init (&pad->segment, GST_FORMAT_UNDEFINED);
223 gst_selector_pad_event (GstPad * pad, GstEvent * event)
226 gboolean forward = TRUE;
227 GstStreamSelector *sel;
228 GstSelectorPad *selpad;
229 GstPad *active_sinkpad;
231 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
232 selpad = GST_SELECTOR_PAD_CAST (pad);
234 /* only forward if we are dealing with the active sinkpad */
235 active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
236 forward = (active_sinkpad == pad);
238 switch (GST_EVENT_TYPE (event)) {
239 case GST_EVENT_FLUSH_STOP:
240 gst_selector_pad_reset (selpad);
242 case GST_EVENT_NEWSEGMENT:
247 gint64 start, stop, time;
249 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
250 &start, &stop, &time);
252 GST_DEBUG_OBJECT (selpad,
253 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
255 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
256 G_GINT64_FORMAT, update, rate, arate, format, start, stop, time);
258 gst_segment_set_newsegment_full (&selpad->segment, update,
259 rate, arate, format, start, stop, time);
260 /* if we are not going to forward the segment, mark the segment as
263 selpad->segment_pending = TRUE;
270 GST_OBJECT_LOCK (selpad);
272 gst_tag_list_free (selpad->tags);
273 gst_event_parse_tag (event, &tags);
275 tags = gst_tag_list_copy (tags);
277 GST_DEBUG_OBJECT (sel, "received tags %" GST_PTR_FORMAT, selpad->tags);
278 GST_OBJECT_UNLOCK (selpad);
288 res = gst_pad_push_event (sel->srcpad, event);
290 gst_event_unref (event);
292 gst_object_unref (sel);
298 gst_selector_pad_getcaps (GstPad * pad)
300 GstStreamSelector *sel;
303 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
305 GST_DEBUG_OBJECT (sel, "Getting caps of srcpad peer");
306 caps = gst_pad_peer_get_caps (sel->srcpad);
308 caps = gst_caps_new_any ();
310 gst_object_unref (sel);
316 gst_selector_pad_bufferalloc (GstPad * pad, guint64 offset,
317 guint size, GstCaps * caps, GstBuffer ** buf)
319 GstStreamSelector *sel;
320 GstFlowReturn result;
321 GstPad *active_sinkpad;
323 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
325 active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
327 /* Fallback allocation for buffers from pads except the selected one */
328 if (pad != active_sinkpad) {
329 GST_DEBUG_OBJECT (sel,
330 "Pad %s:%s is not selected. Performing fallback allocation",
331 GST_DEBUG_PAD_NAME (pad));
334 result = GST_FLOW_OK;
336 result = gst_pad_alloc_buffer (sel->srcpad, offset, size, caps, buf);
338 /* FIXME: HACK. If buffer alloc returns not-linked, perform a fallback
339 * allocation. This should NOT be necessary, because playbin should
340 * properly block the source pad from running until it's finished hooking
341 * everything up, but playbin needs refactoring first. */
342 if (result == GST_FLOW_NOT_LINKED) {
343 GST_DEBUG_OBJECT (sel,
344 "No peer pad yet - performing fallback allocation for pad %s:%s",
345 GST_DEBUG_PAD_NAME (pad));
348 result = GST_FLOW_OK;
352 gst_object_unref (sel);
358 gst_selector_pad_chain (GstPad * pad, GstBuffer * buf)
360 GstStreamSelector *sel;
362 GstPad *active_sinkpad;
363 GstSelectorPad *selpad;
364 GstClockTime timestamp;
367 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
368 selpad = GST_SELECTOR_PAD_CAST (pad);
369 seg = &selpad->segment;
371 active_sinkpad = gst_stream_selector_activate_sinkpad (sel, pad);
373 timestamp = GST_BUFFER_TIMESTAMP (buf);
374 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
375 GST_DEBUG_OBJECT (sel, "received timestamp %" GST_TIME_FORMAT,
376 GST_TIME_ARGS (timestamp));
377 gst_segment_set_last_stop (seg, seg->format, timestamp);
380 /* Ignore buffers from pads except the selected one */
381 if (pad != active_sinkpad)
384 /* if we have a pending segment, push it out now */
385 if (selpad->segment_pending) {
386 gst_pad_push_event (sel->srcpad, gst_event_new_new_segment_full (FALSE,
387 seg->rate, seg->applied_rate, seg->format, seg->start, seg->stop,
390 selpad->segment_pending = FALSE;
394 GST_DEBUG_OBJECT (sel, "Forwarding buffer %p from pad %s:%s", buf,
395 GST_DEBUG_PAD_NAME (pad));
396 res = gst_pad_push (sel->srcpad, buf);
398 gst_object_unref (sel);
400 /* dropped buffers */
403 GST_DEBUG_OBJECT (sel, "Ignoring buffer %p from pad %s:%s",
404 buf, GST_DEBUG_PAD_NAME (pad));
405 gst_buffer_unref (buf);
406 res = GST_FLOW_NOT_LINKED;
411 static void gst_stream_selector_dispose (GObject * object);
413 static void gst_stream_selector_init (GstStreamSelector * sel);
414 static void gst_stream_selector_base_init (GstStreamSelectorClass * klass);
415 static void gst_stream_selector_class_init (GstStreamSelectorClass * klass);
417 static void gst_stream_selector_set_property (GObject * object,
418 guint prop_id, const GValue * value, GParamSpec * pspec);
419 static void gst_stream_selector_get_property (GObject * object,
420 guint prop_id, GValue * value, GParamSpec * pspec);
422 static GstPad *gst_stream_selector_request_new_pad (GstElement * element,
423 GstPadTemplate * templ, const gchar * unused);
424 static void gst_stream_selector_release_pad (GstElement * element,
426 static GstIterator *gst_stream_selector_pad_iterate_linked_pads (GstPad * pad);
427 static GstCaps *gst_stream_selector_getcaps (GstPad * pad);
429 static GstElementClass *parent_class = NULL;
432 gst_stream_selector_get_type (void)
434 static GType stream_selector_type = 0;
436 if (!stream_selector_type) {
437 static const GTypeInfo stream_selector_info = {
438 sizeof (GstStreamSelectorClass),
439 (GBaseInitFunc) gst_stream_selector_base_init,
441 (GClassInitFunc) gst_stream_selector_class_init,
444 sizeof (GstStreamSelector),
446 (GInstanceInitFunc) gst_stream_selector_init,
448 stream_selector_type =
449 g_type_register_static (GST_TYPE_ELEMENT,
450 "GstStreamSelector", &stream_selector_info, 0);
451 GST_DEBUG_CATEGORY_INIT (stream_selector_debug,
452 "streamselector", 0, "A stream-selector element");
455 return stream_selector_type;
459 gst_stream_selector_base_init (GstStreamSelectorClass * klass)
461 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
463 gst_element_class_set_details_simple (element_class,
464 "StreamSelector", "Generic",
465 "N-to-1 input stream_selectoring",
466 "Julien Moutte <julien@moutte.net>, "
467 "Jan Schmidt <thaytan@mad.scientist.com>, "
468 "Wim Taymans <wim.taymans@gmail.com>");
469 gst_element_class_add_pad_template (element_class,
470 gst_static_pad_template_get (&gst_stream_selector_sink_factory));
471 gst_element_class_add_pad_template (element_class,
472 gst_static_pad_template_get (&gst_stream_selector_src_factory));
476 gst_stream_selector_class_init (GstStreamSelectorClass * klass)
478 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
479 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
481 parent_class = g_type_class_peek_parent (klass);
483 gobject_class->dispose = gst_stream_selector_dispose;
485 gobject_class->set_property = gst_stream_selector_set_property;
486 gobject_class->get_property = gst_stream_selector_get_property;
488 g_object_class_install_property (gobject_class, PROP_N_PADS,
489 g_param_spec_uint ("n-pads", "Number of Pads",
490 "The number of sink pads", 0, G_MAXUINT, 0,
491 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
492 g_object_class_install_property (gobject_class, PROP_ACTIVE_PAD,
493 g_param_spec_object ("active-pad", "Active Pad",
494 "The currently active sink pad", GST_TYPE_PAD,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
497 gstelement_class->request_new_pad = gst_stream_selector_request_new_pad;
498 gstelement_class->release_pad = gst_stream_selector_release_pad;
502 gst_stream_selector_init (GstStreamSelector * sel)
504 sel->srcpad = gst_pad_new ("src", GST_PAD_SRC);
505 gst_pad_set_iterate_internal_links_function (sel->srcpad,
506 GST_DEBUG_FUNCPTR (gst_stream_selector_pad_iterate_linked_pads));
507 gst_pad_set_getcaps_function (sel->srcpad,
508 GST_DEBUG_FUNCPTR (gst_stream_selector_getcaps));
509 gst_element_add_pad (GST_ELEMENT (sel), sel->srcpad);
510 /* sinkpad management */
512 sel->active_sinkpad = NULL;
513 gst_segment_init (&sel->segment, GST_FORMAT_UNDEFINED);
517 gst_stream_selector_dispose (GObject * object)
519 GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
521 if (sel->active_sinkpad) {
522 gst_object_unref (sel->active_sinkpad);
523 sel->active_sinkpad = NULL;
526 G_OBJECT_CLASS (parent_class)->dispose (object);
530 gst_stream_selector_set_property (GObject * object, guint prop_id,
531 const GValue * value, GParamSpec * pspec)
533 GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
536 case PROP_ACTIVE_PAD:
539 GstPad **active_pad_p;
541 pad = g_value_get_object (value);
543 GST_OBJECT_LOCK (object);
544 if (pad != sel->active_sinkpad) {
545 GstSelectorPad *selpad;
547 selpad = GST_SELECTOR_PAD_CAST (pad);
548 /* we can only activate pads that have data received */
549 if (selpad && !selpad->active) {
550 GST_DEBUG_OBJECT (sel, "No data received on pad %" GST_PTR_FORMAT,
553 active_pad_p = &sel->active_sinkpad;
554 gst_object_replace ((GstObject **) active_pad_p,
555 GST_OBJECT_CAST (pad));
556 GST_DEBUG_OBJECT (sel, "New active pad is %" GST_PTR_FORMAT,
557 sel->active_sinkpad);
560 GST_OBJECT_UNLOCK (object);
564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
570 gst_stream_selector_get_property (GObject * object, guint prop_id,
571 GValue * value, GParamSpec * pspec)
573 GstStreamSelector *sel = GST_STREAM_SELECTOR (object);
577 GST_OBJECT_LOCK (object);
578 g_value_set_uint (value, sel->n_pads);
579 GST_OBJECT_UNLOCK (object);
581 case PROP_ACTIVE_PAD:{
582 GST_OBJECT_LOCK (object);
583 g_value_set_object (value, sel->active_sinkpad);
584 GST_OBJECT_UNLOCK (object);
588 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594 gst_stream_selector_get_linked_pad (GstPad * pad, gboolean strict)
596 GstStreamSelector *sel;
597 GstPad *otherpad = NULL;
599 sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
601 GST_OBJECT_LOCK (sel);
602 if (pad == sel->srcpad)
603 otherpad = sel->active_sinkpad;
604 else if (pad == sel->active_sinkpad || !strict)
605 otherpad = sel->srcpad;
607 gst_object_ref (otherpad);
608 GST_OBJECT_UNLOCK (sel);
609 gst_object_unref (sel);
614 gst_stream_selector_getcaps (GstPad * pad)
620 otherpad = gst_stream_selector_get_linked_pad (pad, FALSE);
621 parent = gst_object_get_parent (GST_OBJECT (pad));
623 GST_DEBUG_OBJECT (parent,
624 "Pad %s:%s not linked, returning ANY", GST_DEBUG_PAD_NAME (pad));
625 caps = gst_caps_new_any ();
627 GST_DEBUG_OBJECT (parent,
628 "Pad %s:%s is linked (to %s:%s), returning peer caps",
629 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (otherpad));
630 /* if the peer has caps, use those. If the pad is not linked, this function
631 * returns NULL and we return ANY */
632 if (!(caps = gst_pad_peer_get_caps (otherpad)))
633 caps = gst_caps_new_any ();
634 gst_object_unref (otherpad);
637 gst_object_unref (parent);
641 /* check if the pad is the active sinkpad */
643 gst_stream_selector_is_active_sinkpad (GstStreamSelector * sel, GstPad * pad)
647 GST_OBJECT_LOCK (sel);
648 res = (pad == sel->active_sinkpad);
649 GST_OBJECT_UNLOCK (sel);
654 /* Get or create the active sinkpad */
656 gst_stream_selector_activate_sinkpad (GstStreamSelector * sel, GstPad * pad)
658 GstPad *active_sinkpad;
659 GstSelectorPad *selpad;
661 selpad = GST_SELECTOR_PAD_CAST (pad);
663 GST_OBJECT_LOCK (sel);
664 selpad->active = TRUE;
665 active_sinkpad = sel->active_sinkpad;
666 if (active_sinkpad == NULL) {
667 /* first pad we get an alloc on becomes the activated pad by default */
668 active_sinkpad = sel->active_sinkpad = gst_object_ref (pad);
669 GST_DEBUG_OBJECT (sel, "Activating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
671 GST_OBJECT_UNLOCK (sel);
673 return active_sinkpad;
677 gst_stream_selector_pad_iterate_linked_pads (GstPad * pad)
679 GstStreamSelector *sel = GST_STREAM_SELECTOR (gst_pad_get_parent (pad));
683 otherpad = gst_stream_selector_get_linked_pad (pad, TRUE);
685 gst_iterator_new_single (GST_TYPE_PAD, otherpad,
686 (GstCopyFunction) gst_object_ref, (GFreeFunc) gst_object_unref);
689 gst_object_unref (otherpad);
690 gst_object_unref (sel);
696 gst_stream_selector_request_new_pad (GstElement * element,
697 GstPadTemplate * templ, const gchar * unused)
699 GstStreamSelector *sel;
701 GstPad *sinkpad = NULL;
703 sel = GST_STREAM_SELECTOR (element);
704 g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
705 GST_LOG_OBJECT (sel, "Creating new pad %d", sel->padcount);
706 GST_OBJECT_LOCK (sel);
707 name = g_strdup_printf ("sink%d", sel->padcount++);
708 sinkpad = g_object_new (GST_TYPE_SELECTOR_PAD,
709 "name", name, "direction", templ->direction, "template", templ, NULL);
712 GST_OBJECT_UNLOCK (sel);
714 gst_pad_set_event_function (sinkpad,
715 GST_DEBUG_FUNCPTR (gst_selector_pad_event));
716 gst_pad_set_getcaps_function (sinkpad,
717 GST_DEBUG_FUNCPTR (gst_selector_pad_getcaps));
718 gst_pad_set_chain_function (sinkpad,
719 GST_DEBUG_FUNCPTR (gst_selector_pad_chain));
720 gst_pad_set_iterate_internal_links_function (sinkpad,
721 GST_DEBUG_FUNCPTR (gst_stream_selector_pad_iterate_linked_pads));
722 gst_pad_set_bufferalloc_function (sinkpad,
723 GST_DEBUG_FUNCPTR (gst_selector_pad_bufferalloc));
725 gst_pad_set_active (sinkpad, TRUE);
726 gst_element_add_pad (GST_ELEMENT (sel), sinkpad);
731 gst_stream_selector_release_pad (GstElement * element, GstPad * pad)
733 GstStreamSelector *sel;
735 sel = GST_STREAM_SELECTOR (element);
736 GST_LOG_OBJECT (sel, "Releasing pad %s:%s", GST_DEBUG_PAD_NAME (pad));
738 GST_OBJECT_LOCK (sel);
739 /* if the pad was the active pad, makes us select a new one */
740 if (sel->active_sinkpad == pad) {
741 GST_DEBUG_OBJECT (sel, "Deactivating pad %s:%s", GST_DEBUG_PAD_NAME (pad));
742 sel->active_sinkpad = NULL;
745 GST_OBJECT_UNLOCK (sel);
747 gst_pad_set_active (pad, FALSE);
748 gst_element_remove_pad (GST_ELEMENT (sel), pad);