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