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