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