Policy element integration
[profile/ivi/gst-plugins-base.git] / gst / policy / gstautopolicy.c
1 /* GStreamer
2  * Copyright (C) <2012> Intel Corporation
3  *
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.
8  *
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.
13  *
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.
18  */
19
20
21 /**
22  * SECTION:element-autopolicy
23  * @see_also: defaultpolicy
24  *
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.
30  *
31  * <refsect2>
32  * <title>Example launch line (with policy-specific debugging)</title>
33  * |[
34  * gst-launch -v -m --gst-debug=policy:5 fakesrc ! autopolicy actual-policy::role=testrole ! fakesink silent=TRUE
35  * ]|
36  * </refsect2>
37  * <refsect2>
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.
42  * 
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".
46  * </refsect2>
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #  include <config.h>
51 #endif
52
53 #include <gst/gst.h>
54 #include <string.h>
55
56 #include "gstautopolicy.h"
57 #include "gstpolicyplugin.h"
58
59 GST_DEBUG_CATEGORY_EXTERN (gst_policy_debug);
60 #define GST_CAT_DEFAULT gst_policy_debug
61
62 enum
63 {
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   PROP_0
70 };
71
72 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
73     GST_PAD_SINK,
74     GST_PAD_REQUEST,
75     GST_STATIC_CAPS ("ANY")
76     );
77
78 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src_%d",
79     GST_PAD_SRC,
80     GST_PAD_SOMETIMES,
81     GST_STATIC_CAPS ("ANY")
82     );
83
84 #define gst_auto_policy_parent_class parent_class
85 G_DEFINE_TYPE (GstAutoPolicy, gst_auto_policy, GST_TYPE_BIN);
86
87 static GstPad *
88 gst_auto_policy_request_new_pad (GstElement * element,
89     GstPadTemplate * templ, const gchar * name/*, const GstCaps * caps*/)
90 {
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);
97
98   kid_sink_pad = gst_element_get_request_pad (self->kid, "sink_%d");
99   it = gst_pad_iterate_internal_links (kid_sink_pad);
100   if (!it) {
101       gst_object_unref(kid_sink_pad);
102       return NULL;
103   }
104   gst_iterator_next (it, (gpointer *)&kid_src_pad);
105   gst_iterator_free (it);
106
107   sinkpad = gst_ghost_pad_new (name, kid_sink_pad);
108   srcpad = gst_ghost_pad_new (NULL, kid_src_pad);
109
110   gst_pad_set_element_private (sinkpad, srcpad);
111   gst_pad_set_element_private (srcpad, sinkpad);
112
113   gst_pad_set_active (srcpad, TRUE);
114   gst_pad_set_active (sinkpad, TRUE);
115
116   gst_element_add_pad (GST_ELEMENT (self), sinkpad);
117   gst_element_add_pad (GST_ELEMENT (self), srcpad);
118
119   gst_object_unref (kid_sink_pad);
120   gst_object_unref (kid_src_pad);
121
122   return sinkpad;
123 }
124
125 static void
126 gst_auto_policy_release_pad (GstElement * element, GstPad * pad)
127 {
128   GstAutoPolicy *self = GST_AUTO_POLICY (element);
129   GstPad *srcpad = gst_pad_get_element_private (pad);
130
131   gst_pad_set_element_private (pad, NULL);
132   gst_pad_set_element_private (srcpad, NULL);
133
134   gst_element_remove_pad (GST_ELEMENT_CAST (self), srcpad);
135   gst_element_remove_pad (GST_ELEMENT_CAST (self), pad);
136 }
137
138 static void
139 gst_auto_policy_dispose (GstAutoPolicy * auto_policy)
140 {
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;
144
145   G_OBJECT_CLASS (parent_class)->dispose ((GObject *) auto_policy);
146 }
147
148 /* initialize the policy's class */
149 static void
150 gst_auto_policy_class_init (GstAutoPolicyClass * klass)
151 {
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>" 
158   };
159
160   gobject_class = (GObjectClass *) klass;
161   gstelement_class = (GstElementClass *) klass;
162
163   gobject_class->dispose = (GObjectFinalizeFunc) gst_auto_policy_dispose;
164
165   gst_element_class_set_details(gstelement_class, &details);
166
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));
171
172   gstelement_class->request_new_pad = gst_auto_policy_request_new_pad;
173   gstelement_class->release_pad = gst_auto_policy_release_pad;
174 }
175
176 static gboolean
177 gst_auto_policy_factory_filter (GstPluginFeature * feature, gpointer data)
178 {
179   guint rank;
180   const gchar *klass;
181
182   /* we only care about element factories */
183   if (!GST_IS_ELEMENT_FACTORY (feature))
184     return FALSE;
185
186   /* policy elements */
187   klass = gst_element_factory_get_klass (GST_ELEMENT_FACTORY (feature));
188   if (strstr (klass, "Policy") == NULL)
189     return FALSE;
190
191   /* only select elements with autoplugging rank */
192   rank = gst_plugin_feature_get_rank (feature);
193   if (rank < GST_RANK_MARGINAL)
194     return FALSE;
195
196   return TRUE;
197 }
198
199 static gint
200 gst_auto_policy_compare_ranks (GstPluginFeature * f1, GstPluginFeature * f2)
201 {
202   return gst_plugin_feature_get_rank (f2) - gst_plugin_feature_get_rank (f1);
203 }
204
205 static GstElement *
206 gst_auto_policy_discover_actual_policy (GstAutoPolicy * auto_policy)
207 {
208   GList *list, *item;
209   GstElement *choice = NULL;
210
211   list = gst_registry_feature_filter (gst_registry_get_default (),
212       (GstPluginFeatureFilter) gst_auto_policy_factory_filter, FALSE,
213       auto_policy);
214   list = g_list_sort (list, (GCompareFunc) gst_auto_policy_compare_ranks);
215
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");
220     if (choice) {
221       GST_DEBUG_OBJECT (auto_policy, "Selected %s", GST_OBJECT_NAME (f));
222       break;
223     }
224   }
225   gst_plugin_feature_list_free (list);
226   return choice;
227 }
228
229 static void
230 gst_auto_policy_init (GstAutoPolicy * auto_policy)
231 {
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);
236
237 }