gst/: Use _CAST macros to avoid unneeded type checking.
[platform/upstream/gstreamer.git] / gst / gstpluginfeature.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstpluginfeature.c: Abstract base class for all plugin features
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gstpluginfeature
25  * @short_description: Base class for contents of a GstPlugin
26  * @see_also: #GstPlugin
27  *
28  * This is a base class for anything that can be added to a #GstPlugin.
29  */
30
31 #include "gst_private.h"
32
33 #include "gstpluginfeature.h"
34 #include "gstplugin.h"
35 #include "gstregistry.h"
36 #include "gstinfo.h"
37
38 #include <string.h>
39
40 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
41
42 static void gst_plugin_feature_class_init (GstPluginFeatureClass * klass);
43 static void gst_plugin_feature_init (GstPluginFeature * feature);
44 static void gst_plugin_feature_finalize (GObject * object);
45
46 /* static guint gst_plugin_feature_signals[LAST_SIGNAL] = { 0 }; */
47
48 G_DEFINE_ABSTRACT_TYPE (GstPluginFeature, gst_plugin_feature, GST_TYPE_OBJECT);
49 static GstObjectClass *parent_class = NULL;
50
51 static void
52 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
53 {
54   parent_class = g_type_class_peek_parent (klass);
55
56   G_OBJECT_CLASS (klass)->finalize =
57       GST_DEBUG_FUNCPTR (gst_plugin_feature_finalize);
58 }
59
60 static void
61 gst_plugin_feature_init (GstPluginFeature * feature)
62 {
63
64 }
65
66 static void
67 gst_plugin_feature_finalize (GObject * object)
68 {
69   GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (object);
70
71   GST_DEBUG ("finalizing feature %p: '%s'", feature,
72       GST_PLUGIN_FEATURE_NAME (feature));
73   g_free (feature->name);
74   g_free (feature->plugin_name);
75
76   G_OBJECT_CLASS (parent_class)->finalize (object);
77 }
78
79 /**
80  * gst_plugin_feature_load:
81  * @feature: the plugin feature to check
82  *
83  * Loads the plugin containing @feature if it's not already loaded. @feature is
84  * unaffected; use the return value instead.
85  *
86  * Normally this function is used like this:
87  *
88  * <programlisting>
89  * GstPluginFeature *loaded_feature;
90  * loaded_feature = gst_plugin_feature_load (feature);
91  * // presumably, we're no longer interested in the potentially-unloaded feature
92  * gst_object_unref (feature);
93  * feature = loaded_feature;
94  * </programlisting>
95  *
96  * Returns: A reference to the loaded feature, or NULL on error.
97  */
98 GstPluginFeature *
99 gst_plugin_feature_load (GstPluginFeature * feature)
100 {
101   GstPlugin *plugin;
102   GstPluginFeature *real_feature;
103
104   g_return_val_if_fail (feature != NULL, FALSE);
105   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
106
107   GST_DEBUG ("loading plugin for feature %p; '%s'", feature,
108       GST_PLUGIN_FEATURE_NAME (feature));
109   if (feature->loaded)
110     return feature;
111
112   GST_DEBUG ("loading plugin %s", feature->plugin_name);
113   plugin = gst_plugin_load_by_name (feature->plugin_name);
114   if (!plugin)
115     goto load_failed;
116
117   GST_DEBUG ("loaded plugin %s", feature->plugin_name);
118   gst_object_unref (plugin);
119
120   real_feature =
121       gst_registry_lookup_feature (gst_registry_get_default (), feature->name);
122
123   if (real_feature == NULL)
124     goto disappeared;
125   else if (!real_feature->loaded)
126     goto not_found;
127
128   return real_feature;
129
130   /* ERRORS */
131 load_failed:
132   {
133     GST_WARNING ("Failed to load plugin containing feature '%s'.",
134         GST_PLUGIN_FEATURE_NAME (feature));
135     return NULL;
136   }
137 disappeared:
138   {
139     GST_INFO
140         ("Loaded plugin containing feature '%s', but feature disappeared.",
141         feature->name);
142     return NULL;
143   }
144 not_found:
145   {
146     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
147         "not found.", real_feature->name);
148     return NULL;
149   }
150 }
151
152 /**
153  * gst_plugin_feature_type_name_filter:
154  * @feature: the #GstPluginFeature
155  * @data: the type and name to check against
156  *
157  * Compares type and name of plugin feature. Can be used with gst_filter_run().
158  *
159  * Returns: TRUE if equal.
160  */
161 gboolean
162 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
163     GstTypeNameData * data)
164 {
165   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
166       (data->name == NULL
167           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
168 }
169
170 /**
171  * gst_plugin_feature_set_name:
172  * @feature: a feature
173  * @name: the name to set
174  *
175  * Sets the name of a plugin feature. The name uniquely identifies a feature
176  * within all features of the same type. Renaming a plugin feature is not
177  * allowed. A copy is made of the name so you should free the supplied @name
178  * after calling this function.
179  */
180 void
181 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
182 {
183   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
184   g_return_if_fail (name != NULL);
185
186   if (feature->name) {
187     g_return_if_fail (strcmp (feature->name, name) == 0);
188   } else {
189     feature->name = g_strdup (name);
190   }
191   gst_object_set_name (GST_OBJECT (feature), feature->name);
192 }
193
194 /**
195  * gst_plugin_feature_get_name:
196  * @feature: a feature
197  *
198  * Gets the name of a plugin feature.
199  *
200  * Returns: the name
201  */
202 G_CONST_RETURN gchar *
203 gst_plugin_feature_get_name (GstPluginFeature * feature)
204 {
205   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
206
207   return feature->name;
208 }
209
210 /**
211  * gst_plugin_feature_set_rank:
212  * @feature: feature to rank
213  * @rank: rank value - higher number means more priority rank
214  *
215  * Specifies a rank for a plugin feature, so that autoplugging uses
216  * the most appropriate feature.
217  */
218 void
219 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
220 {
221   g_return_if_fail (feature != NULL);
222   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
223
224   feature->rank = rank;
225 }
226
227 /**
228  * gst_plugin_feature_get_rank:
229  * @feature: a feature
230  *
231  * Gets the rank of a plugin feature.
232  *
233  * Returns: The rank of the feature
234  */
235 guint
236 gst_plugin_feature_get_rank (GstPluginFeature * feature)
237 {
238   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
239
240   return feature->rank;
241 }
242
243 /**
244  * gst_plugin_feature_list_free:
245  * @list: list of #GstPluginFeature
246  *
247  * Unrefs each member of @list, then frees the list.
248  */
249 void
250 gst_plugin_feature_list_free (GList * list)
251 {
252   GList *g;
253
254   for (g = list; g; g = g->next) {
255     GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
256
257     gst_object_unref (feature);
258   }
259   g_list_free (list);
260 }
261
262 /**
263  * gst_plugin_feature_check_version:
264  * @feature: a feature
265  * @min_major: minimum required major version
266  * @min_minor: minimum required minor version
267  * @min_micro: minimum required micro version
268  *
269  * Checks whether the given plugin feature is at least
270  *  the required version
271  *
272  * Returns: #TRUE if the plugin feature has at least
273  *  the required version, otherwise #FALSE.
274  */
275 gboolean
276 gst_plugin_feature_check_version (GstPluginFeature * feature,
277     guint min_major, guint min_minor, guint min_micro)
278 {
279   GstRegistry *registry;
280   GstPlugin *plugin;
281   gboolean ret = FALSE;
282
283   g_return_val_if_fail (feature != NULL, FALSE);
284   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
285
286   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
287       feature->plugin_name, feature->name);
288
289   registry = gst_registry_get_default ();
290   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
291
292   if (plugin) {
293     const gchar *ver_str;
294     guint major, minor, micro;
295
296     ver_str = gst_plugin_get_version (plugin);
297     g_return_val_if_fail (ver_str != NULL, FALSE);
298
299     if (sscanf (ver_str, "%u.%u.%u", &major, &minor, &micro) == 3) {
300       if (major > min_major)
301         ret = TRUE;
302       else if (major < min_major)
303         ret = FALSE;
304       else if (minor > min_minor)
305         ret = TRUE;
306       else if (minor < min_minor)
307         ret = FALSE;
308       else if (micro > min_micro)
309         ret = TRUE;
310       else
311         ret = (micro == min_micro);
312
313       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
314           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
315     } else {
316       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
317           ver_str, feature->plugin_name);
318     }
319
320     gst_object_unref (plugin);
321   } else {
322     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
323   }
324
325   return ret;
326 }