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
26 * The #autoconvert element has one sink and one source pad. It will look for
27 * other elements that also have one sink and one source pad.
28 * It will then pick an element that matches the caps on both sides.
29 * If the caps change, it may change the selected element if the current one
30 * no longer matches the caps.
32 * The list of element it will look into can be specified in the
33 * #GstAutoConvert:factories property, otherwise it will look at all available
42 #include "gstautoconvert.h"
46 GST_DEBUG_CATEGORY (autoconvert_debug);
47 #define GST_CAT_DEFAULT (autoconvert_debug)
49 #define GST_AUTOCONVERT_LOCK(ac) GST_OBJECT_LOCK (ac)
50 #define GST_AUTOCONVERT_UNLOCK(ac) GST_OBJECT_UNLOCK (ac)
52 /* elementfactory information */
53 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
58 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
63 static GstStaticPadTemplate sink_internal_template =
64 GST_STATIC_PAD_TEMPLATE ("sink_internal",
69 static GstStaticPadTemplate src_internal_template =
70 GST_STATIC_PAD_TEMPLATE ("src_internal",
75 /* GstAutoConvert signals and args */
88 static void gst_auto_convert_set_property (GObject * object,
89 guint prop_id, const GValue * value, GParamSpec * pspec);
90 static void gst_auto_convert_get_property (GObject * object,
91 guint prop_id, GValue * value, GParamSpec * pspec);
92 static void gst_auto_convert_dispose (GObject * object);
94 static GstElement *gst_auto_convert_get_subelement (GstAutoConvert *
96 static GstPad *gst_auto_convert_get_internal_sinkpad (GstAutoConvert *
98 static GstPad *gst_auto_convert_get_internal_srcpad (GstAutoConvert *
101 static GstIterator *gst_auto_convert_iterate_internal_links (GstPad * pad,
104 static gboolean gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert,
106 static GstCaps *gst_auto_convert_getcaps (GstAutoConvert * autoconvert,
107 GstCaps * filter, GstPadDirection dir);
108 static GstFlowReturn gst_auto_convert_sink_chain (GstPad * pad,
109 GstObject * parent, GstBuffer * buffer);
110 static GstFlowReturn gst_auto_convert_sink_chain_list (GstPad * pad,
111 GstObject * parent, GstBufferList * list);
112 static gboolean gst_auto_convert_sink_event (GstPad * pad, GstObject * parent,
114 static gboolean gst_auto_convert_sink_query (GstPad * pad, GstObject * parent,
117 static gboolean gst_auto_convert_src_event (GstPad * pad, GstObject * parent,
119 static gboolean gst_auto_convert_src_query (GstPad * pad, GstObject * parent,
122 static GstFlowReturn gst_auto_convert_internal_sink_chain (GstPad * pad,
123 GstObject * parent, GstBuffer * buffer);
124 static GstFlowReturn gst_auto_convert_internal_sink_chain_list (GstPad * pad,
125 GstObject * parent, GstBufferList * list);
126 static gboolean gst_auto_convert_internal_sink_event (GstPad * pad,
127 GstObject * parent, GstEvent * event);
128 static gboolean gst_auto_convert_internal_sink_query (GstPad * pad,
129 GstObject * parent, GstQuery * query);
131 static gboolean gst_auto_convert_internal_src_event (GstPad * pad,
132 GstObject * parent, GstEvent * event);
133 static gboolean gst_auto_convert_internal_src_query (GstPad * pad,
134 GstObject * parent, GstQuery * query);
136 static GList *gst_auto_convert_load_factories (GstAutoConvert * autoconvert);
138 * gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert *
139 autoconvert, GstElementFactory * factory);
140 static gboolean gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
141 GstElement * element, GstCaps * caps);
143 static GQuark internal_srcpad_quark = 0;
144 static GQuark internal_sinkpad_quark = 0;
145 static GQuark parent_quark = 0;
147 G_DEFINE_TYPE (GstAutoConvert, gst_auto_convert, GST_TYPE_BIN);
150 gst_auto_convert_class_init (GstAutoConvertClass * klass)
152 GObjectClass *gobject_class = (GObjectClass *) klass;
153 GstElementClass *gstelement_class = (GstElementClass *) klass;
155 GST_DEBUG_CATEGORY_INIT (autoconvert_debug, "autoconvert", 0,
156 "Auto convert based on caps");
158 internal_srcpad_quark = g_quark_from_static_string ("internal_srcpad");
159 internal_sinkpad_quark = g_quark_from_static_string ("internal_sinkpad");
160 parent_quark = g_quark_from_static_string ("parent");
163 gst_element_class_add_static_pad_template (gstelement_class, &srctemplate);
164 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
166 gst_element_class_set_static_metadata (gstelement_class,
167 "Select converter based on caps", "Generic/Bin",
168 "Selects the right transform element based on the caps",
169 "Olivier Crete <olivier.crete@collabora.com>");
171 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_auto_convert_dispose);
173 gobject_class->set_property = gst_auto_convert_set_property;
174 gobject_class->get_property = gst_auto_convert_get_property;
176 g_object_class_install_property (gobject_class, PROP_FACTORIES,
177 g_param_spec_pointer ("factories",
178 "GList of GstElementFactory",
179 "GList of GstElementFactory objects to pick from (the element takes"
180 " ownership of the list (NULL means it will go through all possible"
181 " elements), can only be set once",
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 gst_auto_convert_init (GstAutoConvert * autoconvert)
188 autoconvert->sinkpad =
189 gst_pad_new_from_static_template (&sinktemplate, "sink");
190 autoconvert->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
192 gst_pad_set_chain_function (autoconvert->sinkpad,
193 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_chain));
194 gst_pad_set_chain_list_function (autoconvert->sinkpad,
195 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_chain_list));
196 gst_pad_set_event_function (autoconvert->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_event));
198 gst_pad_set_query_function (autoconvert->sinkpad,
199 GST_DEBUG_FUNCPTR (gst_auto_convert_sink_query));
200 gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
201 GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
203 gst_pad_set_event_function (autoconvert->srcpad,
204 GST_DEBUG_FUNCPTR (gst_auto_convert_src_event));
205 gst_pad_set_query_function (autoconvert->srcpad,
206 GST_DEBUG_FUNCPTR (gst_auto_convert_src_query));
207 gst_pad_set_iterate_internal_links_function (autoconvert->sinkpad,
208 GST_DEBUG_FUNCPTR (gst_auto_convert_iterate_internal_links));
210 gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->sinkpad);
211 gst_element_add_pad (GST_ELEMENT (autoconvert), autoconvert->srcpad);
215 gst_auto_convert_dispose (GObject * object)
217 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
219 g_clear_object (&autoconvert->current_subelement);
220 g_clear_object (&autoconvert->current_internal_sinkpad);
221 g_clear_object (&autoconvert->current_internal_srcpad);
224 GList *factories = g_atomic_pointer_get (&autoconvert->factories);
226 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
228 gst_plugin_feature_list_free (factories);
233 G_OBJECT_CLASS (gst_auto_convert_parent_class)->dispose (object);
237 gst_auto_convert_set_property (GObject * object,
238 guint prop_id, const GValue * value, GParamSpec * pspec)
240 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 if (g_atomic_pointer_get (&autoconvert->factories) == NULL) {
248 GList *factories = g_value_get_pointer (value);
249 factories = g_list_copy (factories);
250 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories,
252 g_list_foreach (factories, (GFunc) g_object_ref, NULL);
254 g_list_free (factories);
256 GST_WARNING_OBJECT (object, "Can not reset factories after they"
257 " have been set or auto-discovered");
264 gst_auto_convert_get_property (GObject * object,
265 guint prop_id, GValue * value, GParamSpec * pspec)
267 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (object);
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 g_value_set_pointer (value,
275 g_atomic_pointer_get (&autoconvert->factories));
282 gst_auto_convert_get_element_by_type (GstAutoConvert * autoconvert, GType type)
285 GstBin *bin = GST_BIN (autoconvert);
286 GstElement *element = NULL;
288 g_return_val_if_fail (type != 0, NULL);
290 GST_OBJECT_LOCK (autoconvert);
292 for (item = bin->children; item; item = item->next) {
293 if (G_TYPE_CHECK_INSTANCE_TYPE (item->data, type)) {
294 element = gst_object_ref (item->data);
299 GST_OBJECT_UNLOCK (autoconvert);
305 * get_pad_by_direction:
306 * @element: The Element
307 * @direction: The direction
309 * Gets a #GstPad that goes in the requested direction. I will return NULL
310 * if there is no pad or if there is more than one pad in this direction
314 get_pad_by_direction (GstElement * element, GstPadDirection direction)
316 GstIterator *iter = gst_element_iterate_pads (element);
317 GstPad *selected_pad = NULL;
319 GValue item = { 0, };
326 switch (gst_iterator_next (iter, &item)) {
327 case GST_ITERATOR_OK:
329 GstPad *pad = g_value_get_object (&item);
331 if (gst_pad_get_direction (pad) == direction) {
332 /* We check if there is more than one pad in this direction,
333 * if there is, we return NULL so that the element is refused
337 gst_object_unref (selected_pad);
340 selected_pad = g_object_ref (pad);
343 g_value_unset (&item);
346 case GST_ITERATOR_RESYNC:
348 gst_object_unref (selected_pad);
351 gst_iterator_resync (iter);
353 case GST_ITERATOR_ERROR:
354 GST_ERROR ("Error iterating pads of element %s",
355 GST_OBJECT_NAME (element));
356 gst_object_unref (selected_pad);
360 case GST_ITERATOR_DONE:
365 g_value_unset (&item);
366 gst_iterator_free (iter);
369 GST_ERROR ("Did not find pad of direction %d in %s",
370 direction, GST_OBJECT_NAME (element));
376 gst_auto_convert_get_subelement (GstAutoConvert * autoconvert)
378 GstElement *element = NULL;
380 GST_AUTOCONVERT_LOCK (autoconvert);
381 if (autoconvert->current_subelement)
382 element = gst_object_ref (autoconvert->current_subelement);
383 GST_AUTOCONVERT_UNLOCK (autoconvert);
389 gst_auto_convert_get_internal_sinkpad (GstAutoConvert * autoconvert)
393 GST_AUTOCONVERT_LOCK (autoconvert);
394 if (autoconvert->current_internal_sinkpad)
395 pad = gst_object_ref (autoconvert->current_internal_sinkpad);
396 GST_AUTOCONVERT_UNLOCK (autoconvert);
402 gst_auto_convert_get_internal_srcpad (GstAutoConvert * autoconvert)
406 GST_AUTOCONVERT_LOCK (autoconvert);
407 if (autoconvert->current_internal_srcpad)
408 pad = gst_object_ref (autoconvert->current_internal_srcpad);
409 GST_AUTOCONVERT_UNLOCK (autoconvert);
415 * This function creates and adds an element to the GstAutoConvert
416 * it then creates the internal pads and links them
421 gst_auto_convert_add_element (GstAutoConvert * autoconvert,
422 GstElementFactory * factory)
424 GstElement *element = NULL;
425 GstPad *internal_sinkpad = NULL;
426 GstPad *internal_srcpad = NULL;
427 GstPad *sinkpad = NULL;
428 GstPad *srcpad = NULL;
429 GstPadLinkReturn padlinkret;
431 GST_DEBUG_OBJECT (autoconvert, "Adding element %s to the autoconvert bin",
432 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)));
434 element = gst_element_factory_create (factory, NULL);
438 if (!gst_bin_add (GST_BIN (autoconvert), element)) {
439 GST_ERROR_OBJECT (autoconvert, "Could not add element %s to the bin",
440 GST_OBJECT_NAME (element));
441 gst_object_unref (element);
445 srcpad = get_pad_by_direction (element, GST_PAD_SRC);
447 GST_ERROR_OBJECT (autoconvert, "Could not find source in %s",
448 GST_OBJECT_NAME (element));
452 sinkpad = get_pad_by_direction (element, GST_PAD_SINK);
454 GST_ERROR_OBJECT (autoconvert, "Could not find sink in %s",
455 GST_OBJECT_NAME (element));
460 gst_pad_new_from_static_template (&sink_internal_template,
463 gst_pad_new_from_static_template (&src_internal_template, "src_internal");
465 if (!internal_sinkpad || !internal_srcpad) {
466 GST_ERROR_OBJECT (autoconvert, "Could not create internal pads");
468 gst_object_unref (internal_srcpad);
469 if (internal_sinkpad)
470 gst_object_unref (internal_sinkpad);
474 g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
476 g_object_weak_ref (G_OBJECT (element), (GWeakNotify) gst_object_unref,
479 gst_pad_set_active (internal_sinkpad, TRUE);
480 gst_pad_set_active (internal_srcpad, TRUE);
482 g_object_set_qdata (G_OBJECT (internal_srcpad), parent_quark, autoconvert);
483 g_object_set_qdata (G_OBJECT (internal_sinkpad), parent_quark, autoconvert);
485 gst_pad_set_chain_function (internal_sinkpad,
486 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_chain));
487 gst_pad_set_chain_list_function (internal_sinkpad,
488 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_chain_list));
489 gst_pad_set_event_function (internal_sinkpad,
490 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_event));
491 gst_pad_set_query_function (internal_sinkpad,
492 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_sink_query));
494 gst_pad_set_event_function (internal_srcpad,
495 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_event));
496 gst_pad_set_query_function (internal_srcpad,
497 GST_DEBUG_FUNCPTR (gst_auto_convert_internal_src_query));
499 padlinkret = gst_pad_link_full (internal_srcpad, sinkpad,
500 GST_PAD_LINK_CHECK_NOTHING);
501 if (GST_PAD_LINK_FAILED (padlinkret)) {
502 GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
504 GST_DEBUG_PAD_NAME (internal_srcpad),
505 GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
509 padlinkret = gst_pad_link_full (srcpad, internal_sinkpad,
510 GST_PAD_LINK_CHECK_NOTHING);
511 if (GST_PAD_LINK_FAILED (padlinkret)) {
512 GST_WARNING_OBJECT (autoconvert, "Could not links pad %s:%s to %s:%s"
514 GST_DEBUG_PAD_NAME (internal_srcpad),
515 GST_DEBUG_PAD_NAME (sinkpad), padlinkret);
519 g_object_set_qdata (G_OBJECT (element),
520 internal_srcpad_quark, internal_srcpad);
521 g_object_set_qdata (G_OBJECT (element),
522 internal_sinkpad_quark, internal_sinkpad);
525 gst_element_sync_state_with_parent (element);
527 /* Increment the reference count we will return to the caller */
528 gst_object_ref (element);
530 /* unref sink and src pad */
531 gst_object_unref (srcpad);
532 gst_object_unref (sinkpad);
536 gst_element_set_locked_state (element, TRUE);
537 gst_element_set_state (element, GST_STATE_NULL);
538 gst_bin_remove (GST_BIN (autoconvert), element);
541 gst_object_unref (srcpad);
543 gst_object_unref (sinkpad);
549 gst_auto_convert_get_or_make_element_from_factory (GstAutoConvert * autoconvert,
550 GstElementFactory * factory)
552 GstElement *element = NULL;
553 GstElementFactory *loaded_factory =
554 GST_ELEMENT_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
560 element = gst_auto_convert_get_element_by_type (autoconvert,
561 gst_element_factory_get_element_type (loaded_factory));
564 element = gst_auto_convert_add_element (autoconvert, loaded_factory);
567 gst_object_unref (loaded_factory);
573 * This function checks if there is one and only one pad template on the
574 * factory that can accept the given caps. If there is one and only one,
575 * it returns TRUE, otherwise, its FALSE
579 factory_can_intersect (GstAutoConvert * autoconvert,
580 GstElementFactory * factory, GstPadDirection direction, GstCaps * caps)
582 const GList *templates;
583 gint has_direction = FALSE;
584 gboolean ret = FALSE;
586 g_return_val_if_fail (factory != NULL, FALSE);
587 g_return_val_if_fail (caps != NULL, FALSE);
589 templates = gst_element_factory_get_static_pad_templates (factory);
592 GstStaticPadTemplate *template = (GstStaticPadTemplate *) templates->data;
594 if (template->direction == direction) {
595 GstCaps *tmpl_caps = NULL;
598 /* If there is more than one pad in this direction, we return FALSE
599 * Only transform elements (with one sink and one source pad)
603 GST_DEBUG_OBJECT (autoconvert, "Factory %p"
604 " has more than one static template with dir %d",
605 template, direction);
608 has_direction = TRUE;
610 tmpl_caps = gst_static_caps_get (&template->static_caps);
611 intersect = gst_caps_can_intersect (tmpl_caps, caps);
612 GST_DEBUG_OBJECT (autoconvert, "Factories %" GST_PTR_FORMAT
613 " static caps %" GST_PTR_FORMAT " and caps %" GST_PTR_FORMAT
614 " can%s intersect", factory, tmpl_caps, caps,
615 intersect ? "" : " not");
616 gst_caps_unref (tmpl_caps);
620 templates = g_list_next (templates);
627 sticky_event_push (GstPad * pad, GstEvent ** event, gpointer user_data)
629 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (user_data);
631 gst_event_ref (*event);
632 gst_pad_push_event (autoconvert->current_internal_srcpad, *event);
638 gst_auto_convert_activate_element (GstAutoConvert * autoconvert,
639 GstElement * element, GstCaps * caps)
641 GstPad *internal_srcpad = g_object_get_qdata (G_OBJECT (element),
642 internal_srcpad_quark);
643 GstPad *internal_sinkpad = g_object_get_qdata (G_OBJECT (element),
644 internal_sinkpad_quark);
647 /* check if the element can really accept said caps */
648 if (!gst_pad_peer_query_accept_caps (internal_srcpad, caps)) {
649 GST_DEBUG_OBJECT (autoconvert, "Could not set %s:%s to %"
650 GST_PTR_FORMAT, GST_DEBUG_PAD_NAME (internal_srcpad), caps);
655 GST_AUTOCONVERT_LOCK (autoconvert);
656 gst_object_replace ((GstObject **) & autoconvert->current_subelement,
657 GST_OBJECT (element));
658 gst_object_replace ((GstObject **) & autoconvert->current_internal_srcpad,
659 GST_OBJECT (internal_srcpad));
660 gst_object_replace ((GstObject **) & autoconvert->current_internal_sinkpad,
661 GST_OBJECT (internal_sinkpad));
662 GST_AUTOCONVERT_UNLOCK (autoconvert);
664 gst_pad_sticky_events_foreach (autoconvert->sinkpad, sticky_event_push,
667 gst_pad_push_event (autoconvert->sinkpad, gst_event_new_reconfigure ());
669 GST_INFO_OBJECT (autoconvert, "Selected element %s",
670 GST_OBJECT_NAME (GST_OBJECT (element)));
672 gst_object_unref (element);
678 gst_auto_convert_iterate_internal_links (GstPad * pad, GstObject * parent)
680 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
681 GstIterator *it = NULL;
684 if (pad == autoconvert->sinkpad)
685 internal = gst_auto_convert_get_internal_srcpad (autoconvert);
687 internal = gst_auto_convert_get_internal_sinkpad (autoconvert);
692 g_value_init (&val, GST_TYPE_PAD);
693 g_value_take_object (&val, internal);
695 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
696 g_value_unset (&val);
703 * If there is already an internal element, it will try to call set_caps on it
705 * If there isn't an internal element or if the set_caps() on the internal
706 * element failed, it will try to find another element where it would succeed
707 * and will change the internal element.
711 gst_auto_convert_sink_setcaps (GstAutoConvert * autoconvert, GstCaps * caps)
714 GstCaps *other_caps = NULL;
716 GstCaps *current_caps;
718 g_return_val_if_fail (autoconvert != NULL, FALSE);
720 current_caps = gst_pad_get_current_caps (autoconvert->sinkpad);
722 if (gst_caps_is_equal_fixed (caps, current_caps)) {
723 gst_caps_unref (current_caps);
726 gst_caps_unref (current_caps);
729 if (autoconvert->current_subelement) {
730 if (gst_pad_peer_query_accept_caps (autoconvert->current_internal_srcpad,
732 /* If we can set the new caps on the current element,
733 * then we just get out
735 GST_DEBUG_OBJECT (autoconvert, "Could set %s:%s to %" GST_PTR_FORMAT,
736 GST_DEBUG_PAD_NAME (autoconvert->current_internal_srcpad), caps);
739 /* If the current element doesn't work,
740 * then we remove the current element before finding a new one.
742 GST_AUTOCONVERT_LOCK (autoconvert);
743 g_clear_object (&autoconvert->current_subelement);
744 g_clear_object (&autoconvert->current_internal_sinkpad);
745 g_clear_object (&autoconvert->current_internal_srcpad);
746 GST_AUTOCONVERT_UNLOCK (autoconvert);
750 other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
752 factories = g_atomic_pointer_get (&autoconvert->factories);
755 factories = gst_auto_convert_load_factories (autoconvert);
757 for (elem = factories; elem; elem = g_list_next (elem)) {
758 GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
761 /* Lets first check if according to the static pad templates on the factory
762 * these caps have any chance of success
764 if (!factory_can_intersect (autoconvert, factory, GST_PAD_SINK, caps)) {
765 GST_LOG_OBJECT (autoconvert, "Factory %s does not accept sink caps %"
767 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)), caps);
770 if (other_caps != NULL) {
771 if (!factory_can_intersect (autoconvert, factory, GST_PAD_SRC,
773 GST_LOG_OBJECT (autoconvert,
774 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
775 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
781 /* The element had a chance of success, lets make it */
783 gst_auto_convert_get_or_make_element_from_factory (autoconvert,
788 /* And make it the current child */
789 if (gst_auto_convert_activate_element (autoconvert, element, caps))
792 gst_object_unref (element);
797 gst_caps_unref (other_caps);
799 if (autoconvert->current_subelement) {
802 GST_WARNING_OBJECT (autoconvert,
803 "Could not find a matching element for caps");
809 * This function filters the pad pad templates, taking only transform element
810 * (with one sink and one src pad)
814 gst_auto_convert_default_filter_func (GstPluginFeature * feature,
817 GstElementFactory *factory = NULL;
818 const GList *static_pad_templates, *tmp;
819 GstStaticPadTemplate *src = NULL, *sink = NULL;
821 if (!GST_IS_ELEMENT_FACTORY (feature))
824 factory = GST_ELEMENT_FACTORY (feature);
826 static_pad_templates = gst_element_factory_get_static_pad_templates (factory);
828 for (tmp = static_pad_templates; tmp; tmp = g_list_next (tmp)) {
829 GstStaticPadTemplate *template = tmp->data;
832 if (template->presence == GST_PAD_SOMETIMES)
835 if (template->presence != GST_PAD_ALWAYS)
838 switch (template->direction) {
853 caps = gst_static_pad_template_get_caps (template);
855 if (gst_caps_is_any (caps) || gst_caps_is_empty (caps))
865 /* function used to sort element features
866 * Copy-pasted from decodebin */
868 compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
871 const gchar *rname1, *rname2;
873 diff = gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
877 rname1 = gst_plugin_feature_get_name (f1);
878 rname2 = gst_plugin_feature_get_name (f2);
880 diff = strcmp (rname2, rname1);
886 gst_auto_convert_load_factories (GstAutoConvert * autoconvert)
888 GList *all_factories;
891 gst_registry_feature_filter (gst_registry_get (),
892 gst_auto_convert_default_filter_func, FALSE, NULL);
894 all_factories = g_list_sort (all_factories, (GCompareFunc) compare_ranks);
896 g_assert (all_factories);
898 if (g_atomic_pointer_compare_and_exchange (&autoconvert->factories, NULL,
900 gst_plugin_feature_list_free (all_factories);
903 return g_atomic_pointer_get (&autoconvert->factories);
906 /* In this case, we should almost always have an internal element, because
907 * set_caps() should have been called first
911 gst_auto_convert_sink_chain (GstPad * pad, GstObject * parent,
914 GstFlowReturn ret = GST_FLOW_NOT_NEGOTIATED;
915 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
917 if (autoconvert->current_internal_srcpad) {
918 ret = gst_pad_push (autoconvert->current_internal_srcpad, buffer);
919 if (ret != GST_FLOW_OK)
920 GST_DEBUG_OBJECT (autoconvert,
921 "Child element %" GST_PTR_FORMAT "returned flow %s",
922 autoconvert->current_subelement, gst_flow_get_name (ret));
924 GST_ERROR_OBJECT (autoconvert, "Got buffer without an negotiated element,"
925 " returning not-negotiated");
926 gst_buffer_unref (buffer);
933 gst_auto_convert_sink_chain_list (GstPad * pad, GstObject * parent,
934 GstBufferList * list)
936 GstFlowReturn ret = GST_FLOW_NOT_NEGOTIATED;
937 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
939 if (autoconvert->current_internal_srcpad) {
940 ret = gst_pad_push_list (autoconvert->current_internal_srcpad, list);
941 if (ret != GST_FLOW_OK)
942 GST_DEBUG_OBJECT (autoconvert,
943 "Child element %" GST_PTR_FORMAT "returned flow %s",
944 autoconvert->current_subelement, gst_flow_get_name (ret));
946 GST_ERROR_OBJECT (autoconvert, "Got buffer without an negotiated element,"
947 " returning not-negotiated");
948 gst_buffer_list_unref (list);
955 gst_auto_convert_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
958 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
959 GstPad *internal_srcpad;
961 if (GST_EVENT_TYPE (event) == GST_EVENT_CAPS) {
964 gst_event_parse_caps (event, &caps);
965 ret = gst_auto_convert_sink_setcaps (autoconvert, caps);
967 gst_event_unref (event);
972 internal_srcpad = gst_auto_convert_get_internal_srcpad (autoconvert);
973 if (internal_srcpad) {
974 ret = gst_pad_push_event (internal_srcpad, event);
975 gst_object_unref (internal_srcpad);
977 switch (GST_EVENT_TYPE (event)) {
978 case GST_EVENT_FLUSH_STOP:
979 case GST_EVENT_FLUSH_START:
980 ret = gst_pad_push_event (autoconvert->srcpad, event);
983 gst_event_unref (event);
992 /* TODO Properly test that this code works well for queries */
994 gst_auto_convert_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
997 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
998 GstElement *subelement;
1000 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1001 GstCaps *filter, *caps;
1003 gst_query_parse_caps (query, &filter);
1004 caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SINK);
1005 gst_query_set_caps_result (query, caps);
1006 gst_caps_unref (caps);
1011 subelement = gst_auto_convert_get_subelement (autoconvert);
1013 GstPad *sub_sinkpad = get_pad_by_direction (subelement, GST_PAD_SINK);
1015 ret = gst_pad_query (sub_sinkpad, query);
1017 gst_object_unref (sub_sinkpad);
1018 gst_object_unref (subelement);
1020 if (ret && GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
1022 gst_query_parse_accept_caps_result (query, &res);
1025 goto ignore_acceptcaps_failure;
1030 ignore_acceptcaps_failure:
1032 if (GST_QUERY_TYPE (query) == GST_QUERY_ACCEPT_CAPS) {
1034 GstCaps *accept_caps;
1036 gst_query_parse_accept_caps (query, &accept_caps);
1038 caps = gst_auto_convert_getcaps (autoconvert, accept_caps, GST_PAD_SINK);
1039 gst_query_set_accept_caps_result (query,
1040 gst_caps_can_intersect (caps, accept_caps));
1041 gst_caps_unref (caps);
1046 GST_WARNING_OBJECT (autoconvert, "Got query %s while no element was"
1047 " selected, letting through",
1048 gst_query_type_get_name (GST_QUERY_TYPE (query)));
1049 return gst_pad_peer_query (autoconvert->srcpad, query);
1053 * gst_auto_convert_getcaps:
1054 * @pad: the sink #GstPad
1056 * This function returns the union of the caps of all the possible element
1057 * factories, based on the static pad templates.
1058 * It also checks does a getcaps on the downstream element and ignores all
1059 * factories whose static caps can not satisfy it.
1061 * It does not try to use each elements getcaps() function
1065 gst_auto_convert_getcaps (GstAutoConvert * autoconvert, GstCaps * filter,
1066 GstPadDirection dir)
1068 GstCaps *caps = NULL, *other_caps = NULL;
1069 GList *elem, *factories;
1071 caps = gst_caps_new_empty ();
1073 if (dir == GST_PAD_SINK)
1074 other_caps = gst_pad_peer_query_caps (autoconvert->srcpad, NULL);
1076 other_caps = gst_pad_peer_query_caps (autoconvert->sinkpad, NULL);
1078 GST_DEBUG_OBJECT (autoconvert,
1079 "Lets find all the element that can fit here with src caps %"
1080 GST_PTR_FORMAT, other_caps);
1082 if (other_caps && gst_caps_is_empty (other_caps)) {
1086 factories = g_atomic_pointer_get (&autoconvert->factories);
1089 factories = gst_auto_convert_load_factories (autoconvert);
1091 for (elem = factories; elem; elem = g_list_next (elem)) {
1092 GstElementFactory *factory = GST_ELEMENT_FACTORY (elem->data);
1093 GstElement *element = NULL;
1094 GstCaps *element_caps;
1095 GstPad *internal_pad = NULL;
1098 if (!factory_can_intersect (autoconvert, factory, dir, filter)) {
1099 GST_LOG_OBJECT (autoconvert,
1100 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1101 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1107 if (other_caps != NULL) {
1108 if (!factory_can_intersect (autoconvert, factory,
1109 dir == GST_PAD_SINK ? GST_PAD_SRC : GST_PAD_SINK, other_caps)) {
1110 GST_LOG_OBJECT (autoconvert,
1111 "Factory %s does not accept src caps %" GST_PTR_FORMAT,
1112 gst_plugin_feature_get_name (GST_PLUGIN_FEATURE (factory)),
1117 element = gst_auto_convert_get_or_make_element_from_factory (autoconvert,
1119 if (element == NULL)
1122 if (dir == GST_PAD_SINK)
1123 internal_pad = g_object_get_qdata (G_OBJECT (element),
1124 internal_srcpad_quark);
1126 internal_pad = g_object_get_qdata (G_OBJECT (element),
1127 internal_sinkpad_quark);
1129 element_caps = gst_pad_peer_query_caps (internal_pad, filter);
1132 caps = gst_caps_merge (caps, element_caps);
1134 gst_object_unref (element);
1136 /* Early out, any is absorbing */
1137 if (gst_caps_is_any (caps))
1142 for (tmp = gst_element_factory_get_static_pad_templates (factory);
1143 tmp; tmp = g_list_next (tmp)) {
1144 GstStaticPadTemplate *template = tmp->data;
1146 if (GST_PAD_TEMPLATE_DIRECTION (template) == dir) {
1147 GstCaps *static_caps = gst_static_pad_template_get_caps (template);
1150 caps = gst_caps_merge (caps, static_caps);
1153 /* Early out, any is absorbing */
1154 if (gst_caps_is_any (caps))
1161 GST_DEBUG_OBJECT (autoconvert, "Returning unioned caps %" GST_PTR_FORMAT,
1167 gst_caps_unref (other_caps);
1175 gst_auto_convert_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
1177 gboolean ret = TRUE;
1178 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1179 GstPad *internal_sinkpad;
1181 if (GST_EVENT_TYPE (event) == GST_EVENT_RECONFIGURE)
1182 gst_pad_push_event (autoconvert->sinkpad, gst_event_ref (event));
1184 internal_sinkpad = gst_auto_convert_get_internal_sinkpad (autoconvert);
1185 if (internal_sinkpad) {
1186 ret = gst_pad_push_event (internal_sinkpad, event);
1187 gst_object_unref (internal_sinkpad);
1188 } else if (GST_EVENT_TYPE (event) != GST_EVENT_RECONFIGURE) {
1189 GST_WARNING_OBJECT (autoconvert,
1190 "Got upstream event while no element was selected," "forwarding.");
1191 ret = gst_pad_push_event (autoconvert->sinkpad, event);
1193 gst_event_unref (event);
1198 /* TODO Properly test that this code works well for queries */
1200 gst_auto_convert_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
1202 gboolean ret = TRUE;
1203 GstAutoConvert *autoconvert = GST_AUTO_CONVERT (parent);
1204 GstElement *subelement;
1206 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
1207 GstCaps *filter, *caps;
1209 gst_query_parse_caps (query, &filter);
1210 caps = gst_auto_convert_getcaps (autoconvert, filter, GST_PAD_SRC);
1211 gst_query_set_caps_result (query, caps);
1212 gst_caps_unref (caps);
1217 subelement = gst_auto_convert_get_subelement (autoconvert);
1219 GstPad *sub_srcpad = get_pad_by_direction (subelement, GST_PAD_SRC);
1221 ret = gst_pad_query (sub_srcpad, query);
1223 gst_object_unref (sub_srcpad);
1224 gst_object_unref (subelement);
1226 GST_WARNING_OBJECT (autoconvert,
1227 "Got upstream query of type %s while no element was selected,"
1228 " forwarding.", gst_query_type_get_name (GST_QUERY_TYPE (query)));
1229 ret = gst_pad_peer_query (autoconvert->sinkpad, query);
1235 static GstFlowReturn
1236 gst_auto_convert_internal_sink_chain (GstPad * pad, GstObject * parent,
1239 GstAutoConvert *autoconvert =
1240 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1243 return gst_pad_push (autoconvert->srcpad, buffer);
1246 static GstFlowReturn
1247 gst_auto_convert_internal_sink_chain_list (GstPad * pad, GstObject * parent,
1248 GstBufferList * list)
1250 GstAutoConvert *autoconvert =
1251 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1254 return gst_pad_push_list (autoconvert->srcpad, list);
1258 gst_auto_convert_internal_sink_event (GstPad * pad, GstObject * parent,
1261 GstAutoConvert *autoconvert =
1262 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1264 gboolean drop = FALSE;
1266 GST_AUTOCONVERT_LOCK (autoconvert);
1267 if (autoconvert->current_internal_sinkpad != pad) {
1270 GST_AUTOCONVERT_UNLOCK (autoconvert);
1273 gst_event_unref (event);
1277 return gst_pad_push_event (autoconvert->srcpad, event);
1281 gst_auto_convert_internal_sink_query (GstPad * pad, GstObject * parent,
1284 GstAutoConvert *autoconvert =
1285 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1288 if (!gst_pad_peer_query (autoconvert->srcpad, query)) {
1289 switch (GST_QUERY_TYPE (query)) {
1290 case GST_QUERY_CAPS:
1294 gst_query_parse_caps (query, &filter);
1296 gst_query_set_caps_result (query, filter);
1298 filter = gst_caps_new_any ();
1299 gst_query_set_caps_result (query, filter);
1300 gst_caps_unref (filter);
1304 case GST_QUERY_ACCEPT_CAPS:
1305 gst_query_set_accept_caps_result (query, TRUE);
1316 gst_auto_convert_internal_src_event (GstPad * pad, GstObject * parent,
1319 GstAutoConvert *autoconvert =
1320 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1322 gboolean drop = FALSE;
1324 GST_AUTOCONVERT_LOCK (autoconvert);
1325 if (autoconvert->current_internal_srcpad != pad) {
1328 GST_AUTOCONVERT_UNLOCK (autoconvert);
1331 GST_DEBUG_OBJECT (autoconvert, "Dropping event %" GST_PTR_FORMAT, event);
1332 gst_event_unref (event);
1336 return gst_pad_push_event (autoconvert->sinkpad, event);
1340 gst_auto_convert_internal_src_query (GstPad * pad, GstObject * parent,
1343 GstAutoConvert *autoconvert =
1344 GST_AUTO_CONVERT (g_object_get_qdata (G_OBJECT (pad),
1347 return gst_pad_peer_query (autoconvert->sinkpad, query);