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