2 * Copyright (C) <2012> Intel Corporation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 * SECTION:element-autopolicy
23 * @see_also: defaultpolicy
25 * Autopolicy is a wrapper element for platform-specific policy elements.
26 * It automatically detects an appropriate policy element to use by scanning
27 * the registry for all elements that have <quote>Policy</quote> in the class
28 * field of their element information. The list is sorted by rank and only
29 * elements with a non-zero autoplugging rank are included.
32 * <title>Example launch line (with policy-specific debugging)</title>
34 * gst-launch -v -m --gst-debug=policy:5 fakesrc ! autopolicy actual-policy::role=testrole ! fakesink silent=TRUE
38 * <title>Usage</title>
39 * Autopolicy provides an unlimited number of request sink pads. You can obtain
40 * the corresponding source pad by listening to a "pad-added" signal while
41 * requesting the sink pad.
43 * You can also get and set the stream role property #GstPolicy:role by using
44 * #GstChildProxy interface that autopolicy implements. The name of the role
45 * property is "actual-policy::role".
56 #include "gstautopolicy.h"
57 #include "gstpolicyplugin.h"
59 GST_DEBUG_CATEGORY_EXTERN (gst_policy_debug);
60 #define GST_CAT_DEFAULT gst_policy_debug
72 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
75 GST_STATIC_CAPS ("ANY")
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src_%d",
81 GST_STATIC_CAPS ("ANY")
84 #define gst_auto_policy_parent_class parent_class
85 G_DEFINE_TYPE (GstAutoPolicy, gst_auto_policy, GST_TYPE_BIN);
88 gst_auto_policy_request_new_pad (GstElement * element,
89 GstPadTemplate * templ, const gchar * name/*, const GstCaps * caps*/)
91 GstPad *srcpad = NULL;
92 GstPad *sinkpad = NULL;
93 GstPad *kid_sink_pad = NULL;
94 GstPad *kid_src_pad = NULL;
95 GstIterator *it = NULL;
96 GstAutoPolicy *self = GST_AUTO_POLICY (element);
98 kid_sink_pad = gst_element_get_request_pad (self->kid, "sink_%d");
99 it = gst_pad_iterate_internal_links (kid_sink_pad);
101 gst_object_unref(kid_sink_pad);
104 gst_iterator_next (it, (gpointer *)&kid_src_pad);
105 gst_iterator_free (it);
107 sinkpad = gst_ghost_pad_new (name, kid_sink_pad);
108 srcpad = gst_ghost_pad_new (NULL, kid_src_pad);
110 gst_pad_set_element_private (sinkpad, srcpad);
111 gst_pad_set_element_private (srcpad, sinkpad);
113 gst_pad_set_active (srcpad, TRUE);
114 gst_pad_set_active (sinkpad, TRUE);
116 gst_element_add_pad (GST_ELEMENT (self), sinkpad);
117 gst_element_add_pad (GST_ELEMENT (self), srcpad);
119 gst_object_unref (kid_sink_pad);
120 gst_object_unref (kid_src_pad);
126 gst_auto_policy_release_pad (GstElement * element, GstPad * pad)
128 GstAutoPolicy *self = GST_AUTO_POLICY (element);
129 GstPad *srcpad = gst_pad_get_element_private (pad);
131 gst_pad_set_element_private (pad, NULL);
132 gst_pad_set_element_private (srcpad, NULL);
134 gst_element_remove_pad (GST_ELEMENT_CAST (self), srcpad);
135 gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
139 gst_auto_policy_dispose (GstAutoPolicy * auto_policy)
141 gst_element_set_state (auto_policy->kid, GST_STATE_NULL);
142 gst_bin_remove (GST_BIN (auto_policy), auto_policy->kid);
143 auto_policy->kid = NULL;
145 G_OBJECT_CLASS (parent_class)->dispose ((GObject *) auto_policy);
148 /* initialize the policy's class */
150 gst_auto_policy_class_init (GstAutoPolicyClass * klass)
152 GObjectClass *gobject_class;
153 GstElementClass *gstelement_class;
154 const GstElementDetails details = {
155 (gchar *)"Auto policy", (gchar *)"Policy",
156 (gchar *)"Wrapper element for automatically detected policy element",
157 (gchar *)"Alexander Kanavin <alexander.kanavin@intel.com>"
160 gobject_class = (GObjectClass *) klass;
161 gstelement_class = (GstElementClass *) klass;
163 gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_policy_dispose;
165 gst_element_class_set_details(gstelement_class, &details);
167 gst_element_class_add_pad_template (gstelement_class,
168 gst_static_pad_template_get (&src_factory));
169 gst_element_class_add_pad_template (gstelement_class,
170 gst_static_pad_template_get (&sink_factory));
172 gstelement_class->request_new_pad = gst_auto_policy_request_new_pad;
173 gstelement_class->release_pad = gst_auto_policy_release_pad;
177 gst_auto_policy_factory_filter (GstPluginFeature * feature, gpointer data)
182 /* we only care about element factories */
183 if (!GST_IS_ELEMENT_FACTORY (feature))
186 /* policy elements */
187 klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
188 if (strstr (klass, "Policy") == NULL)
191 /* only select elements with autoplugging rank */
192 rank = gst_plugin_feature_get_rank (feature);
193 if (rank < GST_RANK_MARGINAL)
200 gst_auto_policy_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
202 return gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
206 gst_auto_policy_discover_actual_policy (GstAutoPolicy * auto_policy)
209 GstElement *choice = NULL;
211 list = gst_registry_feature_filter (gst_registry_get_default (),
212 (GstPluginFeatureFilter) gst_auto_policy_factory_filter, FALSE,
214 list = g_list_sort (list, (GCompareFunc) gst_auto_policy_compare_ranks);
216 for (item = list; item != NULL; item = item->next) {
217 GstElementFactory *f = GST_ELEMENT_FACTORY (item->data);
218 GST_DEBUG_OBJECT (auto_policy, "Testing %s", GST_OBJECT_NAME (f));
219 choice = gst_element_factory_create (f, "actual-policy");
221 GST_DEBUG_OBJECT (auto_policy, "Selected %s", GST_OBJECT_NAME (f));
225 gst_plugin_feature_list_free (list);
230 gst_auto_policy_init (GstAutoPolicy * auto_policy)
232 auto_policy->kid = gst_auto_policy_discover_actual_policy (auto_policy);
233 //At least default policy should always be available
234 g_assert (auto_policy->kid);
235 gst_bin_add (GST_BIN (auto_policy), auto_policy->kid);