3 * Copyright 2007-2012 Collabora Ltd
4 * @author: Olivier Crete <olivier.crete@collabora.com>
5 * Copyright 2007-2008 Nokia
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:element-autoconvert
25 * The #autoconvert element has one sink and one source pad. It will look for
26 * other elements that also have one sink and one source pad.
27 * It will then pick an element that matches the caps on both sides.
28 * If the caps change, it may change the selected element if the current one
29 * no longer matches the caps.
31 * The list of element it will look into can be specified in the
32 * #GstAutoConvert::factories property, otherwise it will look at all available
41 #include "gstautoconvert.h"
45 GST_DEBUG_CATEGORY (autoconvert_debug);
46 #define GST_CAT_DEFAULT (autoconvert_debug)
48 #define GST_AUTOCONVERT_LOCK(ac) GST_OBJECT_LOCK (ac)
49 #define GST_AUTOCONVERT_UNLOCK(ac) GST_OBJECT_UNLOCK (ac)
51 /* elementfactory information */
52 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
62 static GstStaticPadTemplate sink_internal_template =
63 GST_STATIC_PAD_TEMPLATE ("sink_internal",
68 static GstStaticPadTemplate src_internal_template =
69 GST_STATIC_PAD_TEMPLATE ("src_internal",
74 /* GstAutoConvert signals and args */
87 static void gst_auto_convert_set_property (GObject * object,
88 guint prop_id, const GValue * value, GParamSpec * pspec);
89 static void gst_auto_convert_get_property (GObject * object,
90 guint prop_id, GValue * value, GParamSpec * pspec);
91 static void gst_auto_convert_dispose (GObject * object);
93 static GstElement *gst_auto_convert_get_subelement (GstAutoConvert *
95 static GstPad *gst_auto_convert_get_internal_sinkpad (GstAutoConvert *
97 static GstPad *gst_auto_convert_get_internal_srcpad (GstAutoConvert *
100 static GstIterator *gst_auto_convert_iterate_internal_links (GstPad * pad,
103 static gboolean gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert,
105 static GstCaps *gst_auto_convert_getcaps (GstAutoConvert * autoconvert,
106 GstCaps * filter, GstPadDirection dir);
107 static GstFlowReturn gst_auto_convert_sink_chain (GstPad * pad,
108 GstObject * parent, GstBuffer * buffer);
109 static gboolean gst_auto_convert_sink_event (GstPad * pad, GstObject * parent,
111 static gboolean gst_auto_convert_sink_query (GstPad * pad, GstObject * parent,
114 static gboolean gst_auto_convert_src_event (GstPad * pad, GstObject * parent,
116 static gboolean gst_auto_convert_src_query (GstPad * pad, GstObject * parent,
119 static GstFlowReturn gst_auto_convert_internal_sink_chain (GstPad * pad,
120 GstObject * parent, GstBuffer * buffer);
121 static gboolean gst_auto_convert_internal_sink_event (GstPad * pad,
122 GstObject * parent, GstEvent * event);
123 static gboolean gst_auto_convert_internal_sink_query (GstPad * pad,
124 GstObject * parent, GstQuery * query);
126 static gboolean gst_auto_convert_internal_src_event (GstPad * pad,
127 GstObject * parent, GstEvent * event);
128 static gboolean gst_auto_convert_internal_src_query (GstPad * pad,
129 GstObject * parent, GstQuery * query);
131 static GList *gst_auto_convert_load_factories (GstAutoConvert * autoconvert);
133 * gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert *
134 autoconvert, GstElementFactory * factory);
135 static gboolean gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
136 GstElement * element, GstCaps * caps);
138 static GQuark internal_srcpad_quark = 0;
139 static GQuark internal_sinkpad_quark = 0;
140 static GQuark parent_quark = 0;
142 G_DEFINE_TYPE (GstAutoConvert, gst_auto_convert, GST_TYPE_BIN);
145 gst_auto_convert_class_init (GstAutoConvertClass * klass)
147 GObjectClass *gobject_class = (GObjectClass *) klass;
148 GstElementClass *gstelement_class = (GstElementClass *) klass;
150 GST_DEBUG_CATEGORY_INIT (autoconvert_debug, "autoconvert", 0,
151 "Auto convert based on caps");
153 internal_srcpad_quark = g_quark_from_static_string ("internal_srcpad");
154 internal_sinkpad_quark = g_quark_from_static_string ("internal_sinkpad");
155 parent_quark = g_quark_from_static_string ("parent");
158 gst_element_class_add_pad_template (gstelement_class,
159 gst_static_pad_template_get (&srctemplate));
160 gst_element_class_add_pad_template (gstelement_class,
161 gst_static_pad_template_get (&sinktemplate));
163 gst_element_class_set_static_metadata (gstelement_class,
164 "Select convertor based on caps", "Generic/Bin",
165 "Selects the right transform element based on the caps",
166 "Olivier Crete <olivier.crete@collabora.com>");
168 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_auto_convert_dispose);
170 gobject_class->set_property = gst_auto_convert_set_property;
171 gobject_class->get_property = gst_auto_convert_get_property;
173 g_object_class_install_property (gobject_class, PROP_FACTORIES,
174 g_param_spec_pointer ("factories",
175 "GList of GstElementFactory",
176 "GList of GstElementFactory objects to pick from (the element takes"
177 " ownership of the list (NULL means it will go through all possible"
178 " elements), can only be set once",
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 gst_auto_convert_init (GstAutoConvert * autoconvert)
185 autoconvert->sinkpad =
186 gst_pad_new_from_static_template (&sinktemplate, "sink");
187 autoconvert->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
189 gst_pad_set_chain_function (autoconvert->sinkpad,
190 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_chain));
191 gst_pad_set_event_function (autoconvert->sinkpad,
192 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_event));
193 gst_pad_set_query_function (autoconvert->sinkpad,
194 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_query));
195 gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
196 GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
198 gst_pad_set_event_function (autoconvert->srcpad,
199 GST_DEBUG_FUNCPTR (gst_auto_convert_src_event));
200 gst_pad_set_query_function (autoconvert->srcpad,
201 GST_DEBUG_FUNCPTR (gst_auto_convert_src_query));
202 gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
203 GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
205 gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->sinkpad);
206 gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->srcpad);
210 gst_auto_convert_dispose (GObject * object)
212 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
214 g_clear_object (&autoconvert->current_subelement);
215 g_clear_object (&autoconvert->current_internal_sinkpad);
216 g_clear_object (&autoconvert->current_internal_srcpad);
219 GList *factories = g_atomic_pointer_get (&autoconvert->factories);
221 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
223 gst_plugin_feature_list_free (factories);
228 G_OBJECT_CLASS (gst_auto_convert_parent_class)->dispose (object);
232 gst_auto_convert_set_property (GObject * object,
233 guint prop_id, const GValue * value, GParamSpec * pspec)
235 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 if (g_atomic_pointer_get (&autoconvert->factories) == NULL) {
243 GList *factories = g_value_get_pointer (value);
244 factories = g_list_copy (factories);
245 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
247 g_list_foreach (factories, (GFunc) g_object_ref, NULL);
249 g_list_free (factories);
251 GST_WARNING_OBJECT (object, "Can not reset factories after they"
252 " have been set or auto-discovered");
259 gst_auto_convert_get_property (GObject * object,
260 guint prop_id, GValue * value, GParamSpec * pspec)
262 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 g_value_set_pointer (value,
270 g_atomic_pointer_get (&autoconvert->factories));
277 gst_auto_convert_get_element_by_type (GstAutoConvert * autoconvert, GType type)
280 GstBin *bin = GST_BIN (autoconvert);
281 GstElement *element = NULL;
283 g_return_val_if_fail (type != 0, NULL);
285 GST_OBJECT_LOCK (autoconvert);
287 for (item = bin->children; item; item = item->next) {
288 if (G_TYPE_CHECK_INSTANCE_TYPE (item->data, type)) {
289 element = gst_object_ref (item->data);
294 GST_OBJECT_UNLOCK (autoconvert);
300 * get_pad_by_direction:
301 * @element: The Element
302 * @direction: The direction
304 * Gets a #GstPad that goes in the requested direction. I will return NULL
305 * if there is no pad or if there is more than one pad in this direction
309 get_pad_by_direction (GstElement * element, GstPadDirection direction)
311 GstIterator *iter = gst_element_iterate_pads (element);
312 GstPad *selected_pad = NULL;
314 GValue item = { 0, };
321 switch (gst_iterator_next (iter, &item)) {
322 case GST_ITERATOR_OK:
324 GstPad *pad = g_value_get_object (&item);
326 if (gst_pad_get_direction (pad) == direction) {
327 /* We check if there is more than one pad in this direction,
328 * if there is, we return NULL so that the element is refused
332 gst_object_unref (selected_pad);
335 selected_pad = g_object_ref (pad);
338 g_value_unset (&item);
341 case GST_ITERATOR_RESYNC:
343 gst_object_unref (selected_pad);
346 gst_iterator_resync (iter);
348 case GST_ITERATOR_ERROR:
349 GST_ERROR ("Error iterating pads of element %s",
350 GST_OBJECT_NAME (element));
351 gst_object_unref (selected_pad);
355 case GST_ITERATOR_DONE:
360 g_value_unset (&item);
361 gst_iterator_free (iter);
364 GST_ERROR ("Did not find pad of direction %d in %s",
365 direction, GST_OBJECT_NAME (element));
371 gst_auto_convert_get_subelement (GstAutoConvert * autoconvert)
373 GstElement *element = NULL;
375 GST_AUTOCONVERT_LOCK (autoconvert);
376 if (autoconvert->current_subelement)
377 element = gst_object_ref (autoconvert->current_subelement);
378 GST_AUTOCONVERT_UNLOCK (autoconvert);
384 gst_auto_convert_get_internal_sinkpad (GstAutoConvert * autoconvert)
388 GST_AUTOCONVERT_LOCK (autoconvert);
389 if (autoconvert->current_internal_sinkpad)
390 pad = gst_object_ref (autoconvert->current_internal_sinkpad);
391 GST_AUTOCONVERT_UNLOCK (autoconvert);
397 gst_auto_convert_get_internal_srcpad (GstAutoConvert * autoconvert)
401 GST_AUTOCONVERT_LOCK (autoconvert);
402 if (autoconvert->current_internal_srcpad)
403 pad = gst_object_ref (autoconvert->current_internal_srcpad);
404 GST_AUTOCONVERT_UNLOCK (autoconvert);
410 * This function creates and adds an element to the GstAutoConvert
411 * it then creates the internal pads and links them
416 gst_auto_convert_add_element (GstAutoConvert * autoconvert,
417 GstElementFactory * factory)
419 GstElement *element = NULL;
420 GstPad *internal_sinkpad = NULL;
421 GstPad *internal_srcpad = NULL;
422 GstPad *sinkpad = NULL;
423 GstPad *srcpad = NULL;
424 GstPadLinkReturn padlinkret;
426 GST_DEBUG_OBJECT (autoconvert, "Adding element %s to the autoconvert bin",
427 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
429 element = gst_element_factory_create (factory, NULL);
433 if (!gst_bin_add (GST_BIN (autoconvert), element)) {
434 GST_ERROR_OBJECT (autoconvert, "Could not add element %s to the bin",
435 GST_OBJECT_NAME (element));
436 gst_object_unref (element);
440 srcpad = get_pad_by_direction (element, GST_PAD_SRC);
442 GST_ERROR_OBJECT (autoconvert, "Could not find source in %s",
443 GST_OBJECT_NAME (element));
447 sinkpad = get_pad_by_direction (element, GST_PAD_SINK);
449 GST_ERROR_OBJECT (autoconvert, "Could not find sink in %s",
450 GST_OBJECT_NAME (element));
455 gst_pad_new_from_static_template (&sink_internal_template,
458 gst_pad_new_from_static_template (&src_internal_template, "src_internal");
460 if (!internal_sinkpad || !internal_srcpad) {
461 GST_ERROR_OBJECT (autoconvert, "Could not create internal pads");
463 gst_object_unref (internal_srcpad);
464 if (internal_sinkpad)
465 gst_object_unref (internal_sinkpad);
469 g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
471 g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
474 gst_pad_set_active (internal_sinkpad, TRUE);
475 gst_pad_set_active (internal_srcpad, TRUE);
477 g_object_set_qdata (G_OBJECT (internal_srcpad), parent_quark, autoconvert);
478 g_object_set_qdata (G_OBJECT (internal_sinkpad), parent_quark, autoconvert);
480 gst_pad_set_chain_function (internal_sinkpad,
481 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_chain));
482 gst_pad_set_event_function (internal_sinkpad,
483 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_event));
484 gst_pad_set_query_function (internal_sinkpad,
485 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_query));
487 gst_pad_set_event_function (internal_srcpad,
488 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_event));
489 gst_pad_set_query_function (internal_srcpad,
490 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_query));
492 padlinkret = gst_pad_link_full (internal_srcpad, sinkpad,
493 GST_PAD_LINK_CHECK_NOTHING);
494 if (GST_PAD_LINK_FAILED (padlinkret)) {
495 GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
497 GST_DEBUG_PAD_NAME (internal_srcpad),
498 GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
502 padlinkret = gst_pad_link_full (srcpad, internal_sinkpad,
503 GST_PAD_LINK_CHECK_NOTHING);
504 if (GST_PAD_LINK_FAILED (padlinkret)) {
505 GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
507 GST_DEBUG_PAD_NAME (internal_srcpad),
508 GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
512 g_object_set_qdata (G_OBJECT (element),
513 internal_srcpad_quark, internal_srcpad);
514 g_object_set_qdata (G_OBJECT (element),
515 internal_sinkpad_quark, internal_sinkpad);
518 gst_element_sync_state_with_parent (element);
520 /* Increment the reference count we will return to the caller */
521 gst_object_ref (element);
523 /* unref sink and src pad */
524 gst_object_unref (srcpad);
525 gst_object_unref (sinkpad);
529 gst_element_set_locked_state (element, TRUE);
530 gst_element_set_state (element, GST_STATE_NULL);
531 gst_bin_remove (GST_BIN (autoconvert), element);
534 gst_object_unref (srcpad);
536 gst_object_unref (sinkpad);
542 gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert * autoconvert,
543 GstElementFactory * factory)
545 GstElement *element = NULL;
546 GstElementFactory *loaded_factory =
547 GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
553 element = gst_auto_convert_get_element_by_type (autoconvert,
554 gst_element_factory_get_element_type (loaded_factory));
557 element = gst_auto_convert_add_element (autoconvert, loaded_factory);
560 gst_object_unref (loaded_factory);
566 * This function checks if there is one and only one pad template on the
567 * factory that can accept the given caps. If there is one and only one,
568 * it returns TRUE, otherwise, its FALSE
572 factory_can_intersect (GstAutoConvert * autoconvert,
573 GstElementFactory * factory, GstPadDirection direction, GstCaps * caps)
575 const GList *templates;
576 gint has_direction = FALSE;
577 gboolean ret = FALSE;
579 g_return_val_if_fail (factory != NULL, FALSE);
580 g_return_val_if_fail (caps != NULL, FALSE);
582 templates = gst_element_factory_get_static_pad_templates (factory);
585 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
587 if (template->direction == direction) {
588 GstCaps *tmpl_caps = NULL;
591 /* If there is more than one pad in this direction, we return FALSE
592 * Only transform elements (with one sink and one source pad)
596 GST_DEBUG_OBJECT (autoconvert, "Factory %p"
597 " has more than one static template with dir %d",
598 template, direction);
601 has_direction = TRUE;
603 tmpl_caps = gst_static_caps_get (&template->static_caps);
604 intersect = gst_caps_can_intersect (tmpl_caps, caps);
605 GST_DEBUG_OBJECT (autoconvert, "Factories %" GST_PTR_FORMAT
606 " static caps %" GST_PTR_FORMAT " and caps %" GST_PTR_FORMAT
607 " can%s intersect", factory, tmpl_caps, caps,
608 intersect ? "" : " not");
609 gst_caps_unref (tmpl_caps);
613 templates = g_list_next (templates);
620 sticky_event_push (GstPad * pad, GstEvent ** event, gpointer user_data)
622 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (user_data);
624 gst_event_ref (*event);
625 gst_pad_push_event (autoconvert->current_internal_srcpad, *event);
631 gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
632 GstElement * element, GstCaps * caps)
634 GstPad *internal_srcpad = g_object_get_qdata (G_OBJECT (element),
635 internal_srcpad_quark);
636 GstPad *internal_sinkpad = g_object_get_qdata (G_OBJECT (element),
637 internal_sinkpad_quark);
640 /* check if the element can really accept said caps */
641 if (!gst_pad_peer_query_accept_caps (internal_srcpad, caps)) {
642 GST_DEBUG_OBJECT (autoconvert, "Could not set %s:%s to %"
643 GST_PTR_FORMAT, GST_DEBUG_PAD_NAME (internal_srcpad), caps);
648 GST_AUTOCONVERT_LOCK (autoconvert);
649 gst_object_replace ((GstObject **) & autoconvert->current_subelement,
650 GST_OBJECT (element));
651 gst_object_replace ((GstObject **) & autoconvert->current_internal_srcpad,
652 GST_OBJECT (internal_srcpad));
653 gst_object_replace ((GstObject **) & autoconvert->current_internal_sinkpad,
654 GST_OBJECT (internal_sinkpad));
655 GST_AUTOCONVERT_UNLOCK (autoconvert);
657 gst_pad_sticky_events_foreach (autoconvert->sinkpad, sticky_event_push,
660 gst_pad_push_event (autoconvert->sinkpad, gst_event_new_reconfigure ());
662 GST_INFO_OBJECT (autoconvert, "Selected element %s",
663 GST_OBJECT_NAME (GST_OBJECT (element)));
665 gst_object_unref (element);
671 gst_auto_convert_iterate_internal_links (GstPad * pad, GstObject * parent)
673 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
674 GstIterator *it = NULL;
677 if (pad == autoconvert->sinkpad)
678 internal = gst_auto_convert_get_internal_srcpad (autoconvert);
680 internal = gst_auto_convert_get_internal_sinkpad (autoconvert);
685 g_value_init (&val, GST_TYPE_PAD);
686 g_value_take_object (&val, internal);
688 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
689 g_value_unset (&val);
696 * If there is already an internal element, it will try to call set_caps on it
698 * If there isn't an internal element or if the set_caps() on the internal
699 * element failed, it will try to find another element where it would succeed
700 * and will change the internal element.
704 gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert, GstCaps * caps)
707 GstCaps *other_caps = NULL;
709 GstCaps *current_caps;
711 g_return_val_if_fail (autoconvert != NULL, FALSE);
713 current_caps = gst_pad_get_current_caps (autoconvert->sinkpad);
715 if (gst_caps_is_equal_fixed (caps, current_caps)) {
716 gst_caps_unref (current_caps);
719 gst_caps_unref (current_caps);
722 if (autoconvert->current_subelement) {
723 if (gst_pad_peer_query_accept_caps (autoconvert->current_internal_srcpad,
725 /* If we can set the new caps on the current element,
726 * then we just get out
728 GST_DEBUG_OBJECT (autoconvert, "Could set %s:%s to %" GST_PTR_FORMAT,
729 GST_DEBUG_PAD_NAME (autoconvert->current_internal_srcpad), caps);
732 /* If the current element doesn't work,
733 * then we remove the current element before finding a new one.
735 GST_AUTOCONVERT_LOCK (autoconvert);
736 g_clear_object (&autoconvert->current_subelement);
737 g_clear_object (&autoconvert->current_internal_sinkpad);
738 g_clear_object (&autoconvert->current_internal_srcpad);
739 GST_AUTOCONVERT_UNLOCK (autoconvert);
743 other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
745 factories = g_atomic_pointer_get (&autoconvert->factories);
748 factories = gst_auto_convert_load_factories (autoconvert);
750 for (elem = factories; elem; elem = g_list_next (elem)) {
751 GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
754 /* Lets first check if according to the static pad templates on the factory
755 * these caps have any chance of success
757 if (!factory_can_intersect (autoconvert, factory, GST_PAD_SINK, caps)) {
758 GST_LOG_OBJECT (autoconvert, "Factory %s does not accept sink caps %"
760 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)), caps);
763 if (other_caps != NULL) {
764 if (!factory_can_intersect (autoconvert, factory, GST_PAD_SRC,
766 GST_LOG_OBJECT (autoconvert,
767 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
768 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
774 /* The element had a chance of success, lets make it */
776 gst_auto_convert_get_or_make_element_from_factory (autoconvert,
781 /* And make it the current child */
782 if (gst_auto_convert_activate_element (autoconvert, element, caps))
785 gst_object_unref (element);
790 gst_caps_unref (other_caps);
792 if (autoconvert->current_subelement) {
795 GST_WARNING_OBJECT (autoconvert,
796 "Could not find a matching element for caps");
802 * This function filters the pad pad templates, taking only transform element
803 * (with one sink and one src pad)
807 gst_auto_convert_default_filter_func (GstPluginFeature * feature,
810 GstElementFactory *factory = NULL;
811 const GList *static_pad_templates, *tmp;
812 GstStaticPadTemplate *src = NULL, *sink = NULL;
814 if (!GST_IS_ELEMENT_FACTORY (feature))
817 factory = GST_ELEMENT_FACTORY (feature);
819 static_pad_templates = gst_element_factory_get_static_pad_templates (factory);
821 for (tmp = static_pad_templates; tmp; tmp = g_list_next (tmp)) {
822 GstStaticPadTemplate *template = tmp->data;
825 if (template->presence == GST_PAD_SOMETIMES)
828 if (template->presence != GST_PAD_ALWAYS)
831 switch (template->direction) {
846 caps = gst_static_pad_template_get_caps (template);
848 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps))
858 /* function used to sort element features
859 * Copy-pasted from decodebin */
861 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
864 const gchar *rname1, *rname2;
866 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
870 rname1 = gst_plugin_feature_get_name (f1);
871 rname2 = gst_plugin_feature_get_name (f2);
873 diff = strcmp (rname2, rname1);
879 gst_auto_convert_load_factories (GstAutoConvert * autoconvert)
881 GList *all_factories;
884 gst_registry_feature_filter (gst_registry_get (),
885 gst_auto_convert_default_filter_func, FALSE, NULL);
887 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
889 g_assert (all_factories);
891 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories, NULL,
893 gst_plugin_feature_list_free (all_factories);
896 return g_atomic_pointer_get (&autoconvert->factories);
899 /* In this case, we should almost always have an internal element, because
900 * set_caps() should have been called first
904 gst_auto_convert_sink_chain (GstPad * pad, GstObject * parent,
907 GstFlowReturn ret = GST_FLOW_NOT_NEGOTIATED;
908 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
910 if (autoconvert->current_internal_srcpad) {
911 ret = gst_pad_push (autoconvert->current_internal_srcpad, buffer);
912 if (ret != GST_FLOW_OK)
913 GST_DEBUG_OBJECT (autoconvert,
914 "Child element %" GST_PTR_FORMAT "returned flow %s",
915 autoconvert->current_subelement, gst_flow_get_name (ret));
917 GST_ERROR_OBJECT (autoconvert, "Got buffer without an negotiated element,"
918 " returning not-negotiated");
925 gst_auto_convert_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
928 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
929 GstPad *internal_srcpad;
931 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
934 gst_event_parse_caps (event, &caps);
935 ret = gst_auto_convert_sink_setcaps (autoconvert, caps);
937 gst_event_unref (event);
942 internal_srcpad = gst_auto_convert_get_internal_srcpad (autoconvert);
943 if (internal_srcpad) {
944 ret = gst_pad_push_event (internal_srcpad, event);
945 gst_object_unref (internal_srcpad);
947 switch (GST_EVENT_TYPE (event)) {
948 case GST_EVENT_FLUSH_STOP:
949 case GST_EVENT_FLUSH_START:
950 ret = gst_pad_push_event (autoconvert->srcpad, event);
953 gst_event_unref (event);
962 /* TODO Properly test that this code works well for queries */
964 gst_auto_convert_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
967 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
968 GstElement *subelement;
970 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
971 GstCaps *filter, *caps;
973 gst_query_parse_caps (query, &filter);
974 caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SINK);
975 gst_query_set_caps_result (query, caps);
976 gst_caps_unref (caps);
981 subelement = gst_auto_convert_get_subelement (autoconvert);
983 GstPad *sub_sinkpad = get_pad_by_direction (subelement, GST_PAD_SINK);
985 ret = gst_pad_query (sub_sinkpad, query);
987 gst_object_unref (sub_sinkpad);
988 gst_object_unref (subelement);
990 if (ret && GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
992 gst_query_parse_accept_caps_result (query, &res);
995 goto ignore_acceptcaps_failure;
1000 ignore_acceptcaps_failure:
1002 if (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
1004 GstCaps *accept_caps;
1006 gst_query_parse_accept_caps (query, &accept_caps);
1008 caps = gst_auto_convert_getcaps (autoconvert, accept_caps, GST_PAD_SINK);
1009 gst_query_set_accept_caps_result (query,
1010 gst_caps_can_intersect (caps, accept_caps));
1011 gst_caps_unref (caps);
1016 GST_WARNING_OBJECT (autoconvert, "Got query %s while no element was"
1017 " selected, letting through",
1018 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1019 return gst_pad_peer_query (autoconvert->srcpad, query);
1023 * gst_auto_convert_getcaps:
1024 * @pad: the sink #GstPad
1026 * This function returns the union of the caps of all the possible element
1027 * factories, based on the static pad templates.
1028 * It also checks does a getcaps on the downstream element and ignores all
1029 * factories whose static caps can not satisfy it.
1031 * It does not try to use each elements getcaps() function
1035 gst_auto_convert_getcaps (GstAutoConvert * autoconvert, GstCaps * filter,
1036 GstPadDirection dir)
1038 GstCaps *caps = NULL, *other_caps = NULL;
1039 GList *elem, *factories;
1041 caps = gst_caps_new_empty ();
1043 if (dir == GST_PAD_SINK)
1044 other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
1046 other_caps = gst_pad_peer_query_caps (autoconvert->sinkpad, NULL);
1048 GST_DEBUG_OBJECT (autoconvert,
1049 "Lets find all the element that can fit here with src caps %"
1050 GST_PTR_FORMAT, other_caps);
1052 if (other_caps && gst_caps_is_empty (other_caps)) {
1056 factories = g_atomic_pointer_get (&autoconvert->factories);
1059 factories = gst_auto_convert_load_factories (autoconvert);
1061 for (elem = factories; elem; elem = g_list_next (elem)) {
1062 GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
1063 GstElement *element = NULL;
1064 GstCaps *element_caps;
1065 GstPad *internal_pad = NULL;
1068 if (!factory_can_intersect (autoconvert, factory, dir, filter)) {
1069 GST_LOG_OBJECT (autoconvert,
1070 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1071 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1077 if (other_caps != NULL) {
1078 if (!factory_can_intersect (autoconvert, factory,
1079 dir == GST_PAD_SINK ? GST_PAD_SRC : GST_PAD_SINK, other_caps)) {
1080 GST_LOG_OBJECT (autoconvert,
1081 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1082 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1087 element = gst_auto_convert_get_or_make_element_from_factory (autoconvert,
1089 if (element == NULL)
1092 if (dir == GST_PAD_SINK)
1093 internal_pad = g_object_get_qdata (G_OBJECT (element),
1094 internal_srcpad_quark);
1096 internal_pad = g_object_get_qdata (G_OBJECT (element),
1097 internal_sinkpad_quark);
1099 element_caps = gst_pad_peer_query_caps (internal_pad, filter);
1102 caps = gst_caps_merge (caps, element_caps);
1104 gst_object_unref (element);
1106 /* Early out, any is absorbing */
1107 if (gst_caps_is_any (caps))
1112 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1113 tmp; tmp = g_list_next (tmp)) {
1114 GstStaticPadTemplate *template = tmp->data;
1116 if (GST_PAD_TEMPLATE_DIRECTION (template) == dir) {
1117 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1120 caps = gst_caps_merge (caps, static_caps);
1123 /* Early out, any is absorbing */
1124 if (gst_caps_is_any (caps))
1131 GST_DEBUG_OBJECT (autoconvert, "Returning unioned caps %" GST_PTR_FORMAT,
1137 gst_caps_unref (other_caps);
1145 gst_auto_convert_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1147 gboolean ret = TRUE;
1148 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1149 GstPad *internal_sinkpad;
1151 if (GST_EVENT_TYPE (event) == GST_EVENT_RECONFIGURE)
1152 gst_pad_push_event (autoconvert->sinkpad, gst_event_ref (event));
1154 internal_sinkpad = gst_auto_convert_get_internal_sinkpad (autoconvert);
1155 if (internal_sinkpad) {
1156 ret = gst_pad_push_event (internal_sinkpad, event);
1157 gst_object_unref (internal_sinkpad);
1158 } else if (GST_EVENT_TYPE (event) != GST_EVENT_RECONFIGURE) {
1159 GST_WARNING_OBJECT (autoconvert,
1160 "Got upstream event while no element was selected," "forwarding.");
1161 ret = gst_pad_push_event (autoconvert->sinkpad, event);
1167 /* TODO Properly test that this code works well for queries */
1169 gst_auto_convert_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1171 gboolean ret = TRUE;
1172 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1173 GstElement *subelement;
1175 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1176 GstCaps *filter, *caps;
1178 gst_query_parse_caps (query, &filter);
1179 caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SRC);
1180 gst_query_set_caps_result (query, caps);
1181 gst_caps_unref (caps);
1186 subelement = gst_auto_convert_get_subelement (autoconvert);
1188 GstPad *sub_srcpad = get_pad_by_direction (subelement, GST_PAD_SRC);
1190 ret = gst_pad_query (sub_srcpad, query);
1192 gst_object_unref (sub_srcpad);
1193 gst_object_unref (subelement);
1195 GST_WARNING_OBJECT (autoconvert,
1196 "Got upstream query of type %s while no element was selected,"
1197 " forwarding.", gst_query_type_get_name (GST_QUERY_TYPE (query)));
1198 ret = gst_pad_peer_query (autoconvert->sinkpad, query);
1204 static GstFlowReturn
1205 gst_auto_convert_internal_sink_chain (GstPad * pad, GstObject * parent,
1208 GstAutoConvert *autoconvert =
1209 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1212 return gst_pad_push (autoconvert->srcpad, buffer);
1216 gst_auto_convert_internal_sink_event (GstPad * pad, GstObject * parent,
1219 GstAutoConvert *autoconvert =
1220 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1222 gboolean drop = FALSE;
1224 GST_AUTOCONVERT_LOCK (autoconvert);
1225 if (autoconvert->current_internal_sinkpad != pad) {
1228 GST_AUTOCONVERT_UNLOCK (autoconvert);
1231 gst_event_unref (event);
1235 return gst_pad_push_event (autoconvert->srcpad, event);
1239 gst_auto_convert_internal_sink_query (GstPad * pad, GstObject * parent,
1242 GstAutoConvert *autoconvert =
1243 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1246 return gst_pad_peer_query (autoconvert->srcpad, query);
1250 gst_auto_convert_internal_src_event (GstPad * pad, GstObject * parent,
1253 GstAutoConvert *autoconvert =
1254 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1256 gboolean drop = FALSE;
1258 GST_AUTOCONVERT_LOCK (autoconvert);
1259 if (autoconvert->current_internal_srcpad != pad) {
1262 GST_AUTOCONVERT_UNLOCK (autoconvert);
1265 GST_DEBUG_OBJECT (autoconvert, "Dropping event %" GST_PTR_FORMAT, event);
1266 gst_event_unref (event);
1270 return gst_pad_push_event (autoconvert->sinkpad, event);
1274 gst_auto_convert_internal_src_query (GstPad * pad, GstObject * parent,
1277 GstAutoConvert *autoconvert =
1278 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1281 return gst_pad_peer_query (autoconvert->sinkpad, query);