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