gst/gstpluginfeature.c: This shouldn't issue a g_warning since it returns NULL if...
[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_ref (GST_TYPE_OBJECT);
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 (object);
70
71   GST_DEBUG ("finalizing feature %p", feature);
72   g_free (feature->name);
73   g_free (feature->plugin_name);
74
75   G_OBJECT_CLASS (parent_class)->finalize (object);
76 }
77
78 /**
79  * gst_plugin_feature_load:
80  * @feature: the plugin feature to check
81  *
82  * Loads the plugin containing @feature if it's not already loaded. @feature is
83  * unaffected; use the return value instead.
84  *
85  * Normally this function is used like this:
86  *
87  * <programlisting>
88  * GstPluginFeature *loaded_feature;
89  * loaded_feature = gst_plugin_feature_load (feature);
90  * // presumably, we're no longer interested in the potentially-unloaded feature
91  * gst_object_unref (feature);
92  * feature = loaded_feature;
93  * </programlisting>
94  *
95  * Returns: A reference to the loaded feature, or NULL on error.
96  */
97 GstPluginFeature *
98 gst_plugin_feature_load (GstPluginFeature * feature)
99 {
100   GstPlugin *plugin;
101   GstPluginFeature *real_feature;
102
103   g_return_val_if_fail (feature != NULL, FALSE);
104   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
105
106   GST_DEBUG ("loading plugin for feature %p", feature);
107   if (feature->loaded)
108     return 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     GST_WARNING ("Failed to load plugin containing feature '%s'.",
114         GST_PLUGIN_FEATURE_NAME (feature));
115     return NULL;
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     GST_INFO
125         ("Loaded plugin containing feature '%s', but feature disappeared.",
126         feature->name);
127   } else if (!real_feature->loaded) {
128     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
129         "not found.", real_feature->name);
130     return NULL;
131   }
132
133   return real_feature;
134 }
135
136 /**
137  * gst_plugin_feature_type_name_filter:
138  * @feature: the #GstPluginFeature
139  * @data: the type and name to check against
140  *
141  * Compares type and name of plugin feature. Can be used with gst_filter_run().
142  *
143  * Returns: TRUE if equal.
144  */
145 gboolean
146 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
147     GstTypeNameData * data)
148 {
149   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
150       (data->name == NULL
151           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
152 }
153
154 /**
155  * gst_plugin_feature_set_name:
156  * @feature: a feature
157  * @name: the name to set
158  *
159  * Sets the name of a plugin feature. The name uniquely identifies a feature
160  * within all features of the same type. Renaming a plugin feature is not
161  * allowed. A copy is made of the name so you should free the supplied @name
162  * after calling this function.
163  */
164 void
165 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
166 {
167   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
168   g_return_if_fail (name != NULL);
169
170   if (feature->name) {
171     g_return_if_fail (strcmp (feature->name, name) == 0);
172   } else {
173     feature->name = g_strdup (name);
174   }
175   gst_object_set_name (GST_OBJECT (feature), feature->name);
176 }
177
178 /**
179  * gst_plugin_feature_get_name:
180  * @feature: a feature
181  *
182  * Gets the name of a plugin feature.
183  *
184  * Returns: the name
185  */
186 G_CONST_RETURN gchar *
187 gst_plugin_feature_get_name (GstPluginFeature * feature)
188 {
189   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
190
191   return feature->name;
192 }
193
194 /**
195  * gst_plugin_feature_set_rank:
196  * @feature: feature to rank
197  * @rank: rank value - higher number means more priority rank
198  *
199  * Specifies a rank for a plugin feature, so that autoplugging uses
200  * the most appropriate feature.
201  */
202 void
203 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
204 {
205   g_return_if_fail (feature != NULL);
206   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
207
208   feature->rank = rank;
209 }
210
211 /**
212  * gst_plugin_feature_get_rank:
213  * @feature: a feature
214  *
215  * Gets the rank of a plugin feature.
216  *
217  * Returns: The rank of the feature
218  */
219 guint
220 gst_plugin_feature_get_rank (GstPluginFeature * feature)
221 {
222   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
223
224   return feature->rank;
225 }
226
227 /**
228  * gst_plugin_feature_list_free:
229  * @list: list of #GstPluginFeature
230  *
231  * Unrefs each member of @list, then frees the list.
232  */
233 void
234 gst_plugin_feature_list_free (GList * list)
235 {
236   GList *g;
237
238   for (g = list; g; g = g->next) {
239     GstPluginFeature *feature = GST_PLUGIN_FEATURE (g->data);
240
241     gst_object_unref (feature);
242   }
243   g_list_free (list);
244 }
245
246 /**
247  * gst_plugin_feature_check_version:
248  * @feature: a feature
249  * @min_major: minimum required major version
250  * @min_minor: minimum required minor version
251  * @min_micro: minimum required micro version
252  *
253  * Checks whether the given plugin feature is at least
254  *  the required version
255  *
256  * Returns: #TRUE if the plugin feature has at least
257  *  the required version, otherwise #FALSE.
258  */
259 gboolean
260 gst_plugin_feature_check_version (GstPluginFeature * feature,
261     guint min_major, guint min_minor, guint min_micro)
262 {
263   GstRegistry *registry;
264   GstPlugin *plugin;
265   gboolean ret = FALSE;
266
267   g_return_val_if_fail (feature != NULL, FALSE);
268   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
269
270   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
271       feature->plugin_name, feature->name);
272
273   registry = gst_registry_get_default ();
274   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
275
276   if (plugin) {
277     const gchar *ver_str;
278     guint major, minor, micro;
279
280     ver_str = gst_plugin_get_version (plugin);
281     g_return_val_if_fail (ver_str != NULL, FALSE);
282
283     if (sscanf (ver_str, "%u.%u.%u", &major, &minor, &micro) == 3) {
284       if (major > min_major)
285         ret = TRUE;
286       else if (major < min_major)
287         ret = FALSE;
288       else if (minor > min_minor)
289         ret = TRUE;
290       else if (minor < min_minor)
291         ret = FALSE;
292       else if (micro > min_micro)
293         ret = TRUE;
294       else
295         ret = (micro == min_micro);
296
297       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
298           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
299     } else {
300       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
301           ver_str, feature->plugin_name);
302     }
303
304     gst_object_unref (plugin);
305   } else {
306     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
307   }
308
309   return ret;
310 }