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