GstElementFactory: Add listing features
[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 <stdio.h>
39 #include <string.h>
40
41 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
42
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 static GstObjectClass *parent_class = NULL;
49
50 static void
51 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
52 {
53   parent_class = g_type_class_peek_parent (klass);
54
55   G_OBJECT_CLASS (klass)->finalize = 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
73   G_OBJECT_CLASS (parent_class)->finalize (object);
74 }
75
76 /**
77  * gst_plugin_feature_load:
78  * @feature: the plugin feature to check
79  *
80  * Loads the plugin containing @feature if it's not already loaded. @feature is
81  * unaffected; use the return value instead.
82  *
83  * Normally this function is used like this:
84  * |[
85  * GstPluginFeature *loaded_feature;
86  * 
87  * loaded_feature = gst_plugin_feature_load (feature);
88  * // presumably, we're no longer interested in the potentially-unloaded feature
89  * gst_object_unref (feature);
90  * feature = loaded_feature;
91  * ]|
92  *
93  * Returns: A reference to the loaded feature, or NULL on error.
94  */
95 GstPluginFeature *
96 gst_plugin_feature_load (GstPluginFeature * feature)
97 {
98   GstPlugin *plugin;
99   GstPluginFeature *real_feature;
100
101   g_return_val_if_fail (feature != NULL, FALSE);
102   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
103
104   GST_DEBUG ("loading plugin for feature %p; '%s'", feature,
105       GST_PLUGIN_FEATURE_NAME (feature));
106   if (feature->loaded)
107     return gst_object_ref (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     goto load_failed;
113
114   GST_DEBUG ("loaded plugin %s", feature->plugin_name);
115   gst_object_unref (plugin);
116
117   real_feature =
118       gst_registry_lookup_feature (gst_registry_get_default (), feature->name);
119
120   if (real_feature == NULL)
121     goto disappeared;
122   else if (!real_feature->loaded)
123     goto not_found;
124
125   return real_feature;
126
127   /* ERRORS */
128 load_failed:
129   {
130     GST_WARNING ("Failed to load plugin containing feature '%s'.",
131         GST_PLUGIN_FEATURE_NAME (feature));
132     return NULL;
133   }
134 disappeared:
135   {
136     GST_INFO
137         ("Loaded plugin containing feature '%s', but feature disappeared.",
138         feature->name);
139     return NULL;
140   }
141 not_found:
142   {
143     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
144         "not found.", real_feature->name);
145     return NULL;
146   }
147 }
148
149 /**
150  * gst_plugin_feature_type_name_filter:
151  * @feature: the #GstPluginFeature
152  * @data: the type and name to check against
153  *
154  * Compares type and name of plugin feature. Can be used with gst_filter_run().
155  *
156  * Returns: TRUE if equal.
157  */
158 gboolean
159 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
160     GstTypeNameData * data)
161 {
162   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
163
164   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
165       (data->name == NULL
166           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
167 }
168
169 /**
170  * gst_plugin_feature_set_name:
171  * @feature: a feature
172  * @name: the name to set
173  *
174  * Sets the name of a plugin feature. The name uniquely identifies a feature
175  * within all features of the same type. Renaming a plugin feature is not
176  * allowed. A copy is made of the name so you should free the supplied @name
177  * after calling this function.
178  */
179 void
180 gst_plugin_feature_set_name (GstPluginFeature * feature, const gchar * name)
181 {
182   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
183   g_return_if_fail (name != NULL);
184
185   if (feature->name) {
186     g_return_if_fail (strcmp (feature->name, name) == 0);
187   } else {
188     feature->name = g_strdup (name);
189   }
190   gst_object_set_name (GST_OBJECT_CAST (feature), feature->name);
191 }
192
193 /**
194  * gst_plugin_feature_get_name:
195  * @feature: a feature
196  *
197  * Gets the name of a plugin feature.
198  *
199  * Returns: the name
200  */
201 G_CONST_RETURN gchar *
202 gst_plugin_feature_get_name (GstPluginFeature * feature)
203 {
204   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
205
206   return feature->name;
207 }
208
209 /**
210  * gst_plugin_feature_set_rank:
211  * @feature: feature to rank
212  * @rank: rank value - higher number means more priority rank
213  *
214  * Specifies a rank for a plugin feature, so that autoplugging uses
215  * the most appropriate feature.
216  */
217 void
218 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
219 {
220   g_return_if_fail (feature != NULL);
221   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
222
223   feature->rank = rank;
224 }
225
226 /**
227  * gst_plugin_feature_get_rank:
228  * @feature: a feature
229  *
230  * Gets the rank of a plugin feature.
231  *
232  * Returns: The rank of the feature
233  */
234 guint
235 gst_plugin_feature_get_rank (GstPluginFeature * feature)
236 {
237   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
238
239   return feature->rank;
240 }
241
242 /**
243  * gst_plugin_feature_list_free:
244  * @list: list of #GstPluginFeature
245  *
246  * Unrefs each member of @list, then frees the list.
247  */
248 void
249 gst_plugin_feature_list_free (GList * list)
250 {
251   GList *g;
252
253   for (g = list; g; g = g->next) {
254     GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
255
256     gst_object_unref (feature);
257   }
258   g_list_free (list);
259 }
260
261 /**
262  * gst_plugin_feature_list_copy:
263  * @list: list of #GstPluginFeature
264  *
265  * Copies the list of features. Caller should call @gst_plugin_feature_list_free
266  * when done with the list.
267  *
268  * Returns: a copy of @list, with each feature's reference count incremented.
269  *
270  * Since: 0.10.26
271  */
272 GList *
273 gst_plugin_feature_list_copy (GList * list)
274 {
275   GList *new_list = NULL;
276
277   if (G_LIKELY (list)) {
278     GList *last;
279
280     new_list = g_list_alloc ();
281     new_list->data = g_object_ref ((GObject *) list->data);
282     new_list->prev = NULL;
283     last = new_list;
284     list = list->next;
285     while (list) {
286       last->next = g_list_alloc ();
287       last->next->prev = last;
288       last = last->next;
289       last->data = g_object_ref ((GObject *) list->data);
290       list = list->next;
291     }
292     last->next = NULL;
293   }
294
295   return new_list;
296 }
297
298 /**
299  * gst_plugin_feature_list_debug:
300  * @list: a #GList of plugin features
301  *
302  * Debug the plugin feature names in @list.
303  *
304  * Since: 0.10.31
305  */
306 void
307 gst_plugin_feature_list_debug (GList * list)
308 {
309 #ifndef GST_DISABLE_GST_DEBUG
310   while (list) {
311     GST_DEBUG ("%s",
312         gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
313     list = list->next;
314   }
315 #endif
316 }
317
318 /**
319  * gst_plugin_feature_check_version:
320  * @feature: a feature
321  * @min_major: minimum required major version
322  * @min_minor: minimum required minor version
323  * @min_micro: minimum required micro version
324  *
325  * Checks whether the given plugin feature is at least
326  *  the required version
327  *
328  * Returns: #TRUE if the plugin feature has at least
329  *  the required version, otherwise #FALSE.
330  */
331 gboolean
332 gst_plugin_feature_check_version (GstPluginFeature * feature,
333     guint min_major, guint min_minor, guint min_micro)
334 {
335   GstRegistry *registry;
336   GstPlugin *plugin;
337   gboolean ret = FALSE;
338
339   g_return_val_if_fail (feature != NULL, FALSE);
340   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
341
342   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
343       feature->plugin_name, feature->name);
344
345   registry = gst_registry_get_default ();
346   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
347
348   if (plugin) {
349     const gchar *ver_str;
350     guint major, minor, micro, nano;
351     gint nscan;
352
353     ver_str = gst_plugin_get_version (plugin);
354     g_return_val_if_fail (ver_str != NULL, FALSE);
355
356     nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, &micro, &nano);
357     GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
358
359     if (nscan >= 3) {
360       if (major > min_major)
361         ret = TRUE;
362       else if (major < min_major)
363         ret = FALSE;
364       else if (minor > min_minor)
365         ret = TRUE;
366       else if (minor < min_minor)
367         ret = FALSE;
368       else if (micro > min_micro)
369         ret = TRUE;
370       /* micro is 1 smaller but we have a nano version, this is the upcomming
371        * release of the requested version and we're ok then */
372       else if (nscan == 4 && nano > 0 && (micro + 1 == min_micro))
373         ret = TRUE;
374       else
375         ret = (micro == min_micro);
376
377       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
378           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
379     } else {
380       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
381           ver_str, feature->plugin_name);
382     }
383
384     gst_object_unref (plugin);
385   } else {
386     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
387   }
388
389   return ret;
390 }
391
392 /**
393  * gst_plugin_feature_rank_compare_func:
394  * @p1: a #GstPluginFeature
395  * @p2: a #GstPluginFeature
396  *
397  * Compares the two given #GstPluginFeature instances. This function can be
398  * used as a #GCompareFunc when sorting by rank and then by name.
399  *
400  * Returns: negative value if the rank of p1 > the rank of p2 or the ranks are
401  * equal but the name of p1 comes before the name of p2; zero if the rank
402  * and names are equal; positive value if the rank of p1 < the rank of p2 or the
403  * ranks are equal but the name of p2 comes after the name of p1
404  *
405  * Since: 0.10.31
406  */
407 gint
408 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
409 {
410   GstPluginFeature *f1, *f2;
411   gint diff;
412
413   f1 = (GstPluginFeature *) p1;
414   f2 = (GstPluginFeature *) p2;
415
416   diff = f2->rank - f1->rank;
417   if (diff != 0)
418     return diff;
419
420   diff = strcmp (f2->name, f1->name);
421
422   return diff;
423 }