gst/gstpluginfeature.c: Revert patch from #459466 until after the release and we...
[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_finalize (GObject * object);
43
44 /* static guint gst_plugin_feature_signals[LAST_SIGNAL] = { 0 }; */
45
46 G_DEFINE_ABSTRACT_TYPE (GstPluginFeature, gst_plugin_feature, GST_TYPE_OBJECT);
47 static GstObjectClass *parent_class = NULL;
48
49 static void
50 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
51 {
52   parent_class = g_type_class_peek_parent (klass);
53
54   G_OBJECT_CLASS (klass)->finalize =
55       GST_DEBUG_FUNCPTR (gst_plugin_feature_finalize);
56 }
57
58 static void
59 gst_plugin_feature_init (GstPluginFeature * feature)
60 {
61   /* do nothing, needed because of G_DEFINE_TYPE */
62 }
63
64 static void
65 gst_plugin_feature_finalize (GObject * object)
66 {
67   GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (object);
68
69   GST_DEBUG ("finalizing feature %p: '%s'", feature,
70       GST_PLUGIN_FEATURE_NAME (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; '%s'", feature,
106       GST_PLUGIN_FEATURE_NAME (feature));
107   if (feature->loaded)
108     return gst_object_ref (feature);
109
110   GST_DEBUG ("loading plugin %s", feature->plugin_name);
111   plugin = gst_plugin_load_by_name (feature->plugin_name);
112   if (!plugin)
113     goto load_failed;
114
115   GST_DEBUG ("loaded plugin %s", feature->plugin_name);
116   gst_object_unref (plugin);
117
118   real_feature =
119       gst_registry_lookup_feature (gst_registry_get_default (), feature->name);
120
121   if (real_feature == NULL)
122     goto disappeared;
123   else if (!real_feature->loaded)
124     goto not_found;
125
126   return real_feature;
127
128   /* ERRORS */
129 load_failed:
130   {
131     GST_WARNING ("Failed to load plugin containing feature '%s'.",
132         GST_PLUGIN_FEATURE_NAME (feature));
133     return NULL;
134   }
135 disappeared:
136   {
137     GST_INFO
138         ("Loaded plugin containing feature '%s', but feature disappeared.",
139         feature->name);
140     return NULL;
141   }
142 not_found:
143   {
144     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
145         "not found.", real_feature->name);
146     return NULL;
147   }
148 }
149
150 /**
151  * gst_plugin_feature_type_name_filter:
152  * @feature: the #GstPluginFeature
153  * @data: the type and name to check against
154  *
155  * Compares type and name of plugin feature. Can be used with gst_filter_run().
156  *
157  * Returns: TRUE if equal.
158  */
159 gboolean
160 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
161     GstTypeNameData * data)
162 {
163   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
164       (data->name == NULL
165           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
166 }
167
168 /**
169  * gst_plugin_feature_set_name:
170  * @feature: a feature
171  * @name: the name to set
172  *
173  * Sets the name of a plugin feature. The name uniquely identifies a feature
174  * within all features of the same type. Renaming a plugin feature is not
175  * allowed. A copy is made of the name so you should free the supplied @name
176  * after calling this function.
177  */
178 void
179 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
180 {
181   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
182   g_return_if_fail (name != NULL);
183
184   if (feature->name) {
185     g_return_if_fail (strcmp (feature->name, name) == 0);
186   } else {
187     feature->name = g_strdup (name);
188   }
189   gst_object_set_name (GST_OBJECT (feature), feature->name);
190 }
191
192 /**
193  * gst_plugin_feature_get_name:
194  * @feature: a feature
195  *
196  * Gets the name of a plugin feature.
197  *
198  * Returns: the name
199  */
200 G_CONST_RETURN gchar *
201 gst_plugin_feature_get_name (GstPluginFeature * feature)
202 {
203   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
204
205   return feature->name;
206 }
207
208 /**
209  * gst_plugin_feature_set_rank:
210  * @feature: feature to rank
211  * @rank: rank value - higher number means more priority rank
212  *
213  * Specifies a rank for a plugin feature, so that autoplugging uses
214  * the most appropriate feature.
215  */
216 void
217 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
218 {
219   g_return_if_fail (feature != NULL);
220   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
221
222   feature->rank = rank;
223 }
224
225 /**
226  * gst_plugin_feature_get_rank:
227  * @feature: a feature
228  *
229  * Gets the rank of a plugin feature.
230  *
231  * Returns: The rank of the feature
232  */
233 guint
234 gst_plugin_feature_get_rank (GstPluginFeature * feature)
235 {
236   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
237
238   return feature->rank;
239 }
240
241 /**
242  * gst_plugin_feature_list_free:
243  * @list: list of #GstPluginFeature
244  *
245  * Unrefs each member of @list, then frees the list.
246  */
247 void
248 gst_plugin_feature_list_free (GList * list)
249 {
250   GList *g;
251
252   for (g = list; g; g = g->next) {
253     GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
254
255     gst_object_unref (feature);
256   }
257   g_list_free (list);
258 }
259
260 /**
261  * gst_plugin_feature_check_version:
262  * @feature: a feature
263  * @min_major: minimum required major version
264  * @min_minor: minimum required minor version
265  * @min_micro: minimum required micro version
266  *
267  * Checks whether the given plugin feature is at least
268  *  the required version
269  *
270  * Returns: #TRUE if the plugin feature has at least
271  *  the required version, otherwise #FALSE.
272  */
273 gboolean
274 gst_plugin_feature_check_version (GstPluginFeature * feature,
275     guint min_major, guint min_minor, guint min_micro)
276 {
277   GstRegistry *registry;
278   GstPlugin *plugin;
279   gboolean ret = FALSE;
280
281   g_return_val_if_fail (feature != NULL, FALSE);
282   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
283
284   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
285       feature->plugin_name, feature->name);
286
287   registry = gst_registry_get_default ();
288   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
289
290   if (plugin) {
291     const gchar *ver_str;
292     guint major, minor, micro;
293
294     ver_str = gst_plugin_get_version (plugin);
295     g_return_val_if_fail (ver_str != NULL, FALSE);
296
297     if (sscanf (ver_str, "%u.%u.%u", &major, &minor, &micro) == 3) {
298       if (major > min_major)
299         ret = TRUE;
300       else if (major < min_major)
301         ret = FALSE;
302       else if (minor > min_minor)
303         ret = TRUE;
304       else if (minor < min_minor)
305         ret = FALSE;
306       else if (micro > min_micro)
307         ret = TRUE;
308       else
309         ret = (micro == min_micro);
310
311       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
312           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
313     } else {
314       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
315           ver_str, feature->plugin_name);
316     }
317
318     gst_object_unref (plugin);
319   } else {
320     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
321   }
322
323   return ret;
324 }