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