gst/gstpluginfeature.c: more meaningful debug output
[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: '%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     GST_WARNING ("Failed to load plugin containing feature '%s'.",
116         GST_PLUGIN_FEATURE_NAME (feature));
117     return NULL;
118   }
119   GST_DEBUG ("loaded plugin %s", feature->plugin_name);
120   gst_object_unref (plugin);
121
122   real_feature =
123       gst_registry_lookup_feature (gst_registry_get_default (), feature->name);
124
125   if (real_feature == NULL) {
126     GST_INFO
127         ("Loaded plugin containing feature '%s', but feature disappeared.",
128         feature->name);
129   } else if (!real_feature->loaded) {
130     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
131         "not found.", real_feature->name);
132     return NULL;
133   }
134
135   return real_feature;
136 }
137
138 /**
139  * gst_plugin_feature_type_name_filter:
140  * @feature: the #GstPluginFeature
141  * @data: the type and name to check against
142  *
143  * Compares type and name of plugin feature. Can be used with gst_filter_run().
144  *
145  * Returns: TRUE if equal.
146  */
147 gboolean
148 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
149     GstTypeNameData * data)
150 {
151   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
152       (data->name == NULL
153           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
154 }
155
156 /**
157  * gst_plugin_feature_set_name:
158  * @feature: a feature
159  * @name: the name to set
160  *
161  * Sets the name of a plugin feature. The name uniquely identifies a feature
162  * within all features of the same type. Renaming a plugin feature is not
163  * allowed. A copy is made of the name so you should free the supplied @name
164  * after calling this function.
165  */
166 void
167 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
168 {
169   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
170   g_return_if_fail (name != NULL);
171
172   if (feature->name) {
173     g_return_if_fail (strcmp (feature->name, name) == 0);
174   } else {
175     feature->name = g_strdup (name);
176   }
177   gst_object_set_name (GST_OBJECT (feature), feature->name);
178 }
179
180 /**
181  * gst_plugin_feature_get_name:
182  * @feature: a feature
183  *
184  * Gets the name of a plugin feature.
185  *
186  * Returns: the name
187  */
188 G_CONST_RETURN gchar *
189 gst_plugin_feature_get_name (GstPluginFeature * feature)
190 {
191   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
192
193   return feature->name;
194 }
195
196 /**
197  * gst_plugin_feature_set_rank:
198  * @feature: feature to rank
199  * @rank: rank value - higher number means more priority rank
200  *
201  * Specifies a rank for a plugin feature, so that autoplugging uses
202  * the most appropriate feature.
203  */
204 void
205 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
206 {
207   g_return_if_fail (feature != NULL);
208   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
209
210   feature->rank = rank;
211 }
212
213 /**
214  * gst_plugin_feature_get_rank:
215  * @feature: a feature
216  *
217  * Gets the rank of a plugin feature.
218  *
219  * Returns: The rank of the feature
220  */
221 guint
222 gst_plugin_feature_get_rank (GstPluginFeature * feature)
223 {
224   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
225
226   return feature->rank;
227 }
228
229 /**
230  * gst_plugin_feature_list_free:
231  * @list: list of #GstPluginFeature
232  *
233  * Unrefs each member of @list, then frees the list.
234  */
235 void
236 gst_plugin_feature_list_free (GList * list)
237 {
238   GList *g;
239
240   for (g = list; g; g = g->next) {
241     GstPluginFeature *feature = GST_PLUGIN_FEATURE (g->data);
242
243     gst_object_unref (feature);
244   }
245   g_list_free (list);
246 }
247
248 /**
249  * gst_plugin_feature_check_version:
250  * @feature: a feature
251  * @min_major: minimum required major version
252  * @min_minor: minimum required minor version
253  * @min_micro: minimum required micro version
254  *
255  * Checks whether the given plugin feature is at least
256  *  the required version
257  *
258  * Returns: #TRUE if the plugin feature has at least
259  *  the required version, otherwise #FALSE.
260  */
261 gboolean
262 gst_plugin_feature_check_version (GstPluginFeature * feature,
263     guint min_major, guint min_minor, guint min_micro)
264 {
265   GstRegistry *registry;
266   GstPlugin *plugin;
267   gboolean ret = FALSE;
268
269   g_return_val_if_fail (feature != NULL, FALSE);
270   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
271
272   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
273       feature->plugin_name, feature->name);
274
275   registry = gst_registry_get_default ();
276   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
277
278   if (plugin) {
279     const gchar *ver_str;
280     guint major, minor, micro;
281
282     ver_str = gst_plugin_get_version (plugin);
283     g_return_val_if_fail (ver_str != NULL, FALSE);
284
285     if (sscanf (ver_str, "%u.%u.%u", &major, &minor, &micro) == 3) {
286       if (major > min_major)
287         ret = TRUE;
288       else if (major < min_major)
289         ret = FALSE;
290       else if (minor > min_minor)
291         ret = TRUE;
292       else if (minor < min_minor)
293         ret = FALSE;
294       else if (micro > min_micro)
295         ret = TRUE;
296       else
297         ret = (micro == min_micro);
298
299       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
300           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
301     } else {
302       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
303           ver_str, feature->plugin_name);
304     }
305
306     gst_object_unref (plugin);
307   } else {
308     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
309   }
310
311   return ret;
312 }