tracers: Add tracepoint when a plugin feature it loaded
[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   GST_TRACER_PLUGIN_FEATURE_LOADED (real_feature);
128
129   return real_feature;
130
131   /* ERRORS */
132 load_failed:
133   {
134     GST_WARNING ("Failed to load plugin containing feature '%s'.",
135         GST_OBJECT_NAME (feature));
136     return NULL;
137   }
138 disappeared:
139   {
140     GST_INFO
141         ("Loaded plugin containing feature '%s', but feature disappeared.",
142         GST_OBJECT_NAME (feature));
143     return NULL;
144   }
145 not_found:
146   {
147     GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
148         "not found.", GST_OBJECT_NAME (real_feature));
149     return NULL;
150   }
151 }
152
153 /**
154  * gst_plugin_feature_set_rank:
155  * @feature: feature to rank
156  * @rank: rank value - higher number means more priority rank
157  *
158  * Specifies a rank for a plugin feature, so that autoplugging uses
159  * the most appropriate feature.
160  */
161 void
162 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
163 {
164   g_return_if_fail (feature != NULL);
165   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
166
167   feature->rank = rank;
168 }
169
170 /**
171  * gst_plugin_feature_get_rank:
172  * @feature: a feature
173  *
174  * Gets the rank of a plugin feature.
175  *
176  * Returns: The rank of the feature
177  */
178 guint
179 gst_plugin_feature_get_rank (GstPluginFeature * feature)
180 {
181   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
182
183   return feature->rank;
184 }
185
186 /**
187  * gst_plugin_feature_get_plugin:
188  * @feature: a feature
189  *
190  * Get the plugin that provides this feature.
191  *
192  * Returns: (transfer full) (nullable): the plugin that provides this
193  *     feature, or %NULL.  Unref with gst_object_unref() when no
194  *     longer needed.
195  */
196 GstPlugin *
197 gst_plugin_feature_get_plugin (GstPluginFeature * feature)
198 {
199   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
200
201   if (feature->plugin == NULL)
202     return NULL;
203
204   return (GstPlugin *) gst_object_ref (feature->plugin);
205 }
206
207 /**
208  * gst_plugin_feature_get_plugin_name:
209  * @feature: a feature
210  *
211  * Get the name of the plugin that provides this feature.
212  *
213  * Returns: (nullable): the name of the plugin that provides this
214  *     feature, or %NULL if the feature is not associated with a
215  *     plugin.
216  *
217  * Since: 1.2
218  */
219 const gchar *
220 gst_plugin_feature_get_plugin_name (GstPluginFeature * feature)
221 {
222   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
223
224   if (feature->plugin == NULL)
225     return NULL;
226
227   return gst_plugin_get_name (feature->plugin);
228 }
229
230 /**
231  * gst_plugin_feature_list_free:
232  * @list: (transfer full) (element-type Gst.PluginFeature): list
233  *     of #GstPluginFeature
234  *
235  * Unrefs each member of @list, then frees the list.
236  */
237 void
238 gst_plugin_feature_list_free (GList * list)
239 {
240   GList *g;
241
242   for (g = list; g; g = g->next) {
243     GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
244
245     gst_object_unref (feature);
246   }
247   g_list_free (list);
248 }
249
250 /**
251  * gst_plugin_feature_list_copy:
252  * @list: (transfer none) (element-type Gst.PluginFeature): list
253  *     of #GstPluginFeature
254  *
255  * Copies the list of features. Caller should call @gst_plugin_feature_list_free
256  * when done with the list.
257  *
258  * Returns: (transfer full) (element-type Gst.PluginFeature): a copy of @list,
259  *     with each feature's reference count incremented.
260  */
261 GList *
262 gst_plugin_feature_list_copy (GList * list)
263 {
264   GList *new_list = NULL;
265
266   if (G_LIKELY (list)) {
267     GList *last;
268
269     new_list = g_list_alloc ();
270     new_list->data = gst_object_ref (list->data);
271     new_list->prev = NULL;
272     last = new_list;
273     list = list->next;
274     while (list) {
275       last->next = g_list_alloc ();
276       last->next->prev = last;
277       last = last->next;
278       last->data = gst_object_ref (list->data);
279       list = list->next;
280     }
281     last->next = NULL;
282   }
283
284   return new_list;
285 }
286
287 /**
288  * gst_plugin_feature_list_debug:
289  * @list: (transfer none) (element-type Gst.PluginFeature): a #GList of
290  *     plugin features
291  *
292  * Debug the plugin feature names in @list.
293  */
294 void
295 gst_plugin_feature_list_debug (GList * list)
296 {
297 #ifndef GST_DISABLE_GST_DEBUG
298   while (list) {
299     GST_DEBUG ("%s",
300         gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
301     list = list->next;
302   }
303 #endif
304 }
305
306 /**
307  * gst_plugin_feature_check_version:
308  * @feature: a feature
309  * @min_major: minimum required major version
310  * @min_minor: minimum required minor version
311  * @min_micro: minimum required micro version
312  *
313  * Checks whether the given plugin feature is at least
314  *  the required version
315  *
316  * Returns: %TRUE if the plugin feature has at least
317  *  the required version, otherwise %FALSE.
318  */
319 gboolean
320 gst_plugin_feature_check_version (GstPluginFeature * feature,
321     guint min_major, guint min_minor, guint min_micro)
322 {
323   GstRegistry *registry;
324   GstPlugin *plugin;
325   gboolean ret = FALSE;
326
327   g_return_val_if_fail (feature != NULL, FALSE);
328   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
329
330   GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
331       feature->plugin_name, GST_OBJECT_NAME (feature));
332
333   registry = gst_registry_get ();
334   plugin = gst_registry_find_plugin (registry, feature->plugin_name);
335
336   if (plugin) {
337     const gchar *ver_str;
338     guint major, minor, micro, nano;
339     gint nscan;
340
341     ver_str = gst_plugin_get_version (plugin);
342     g_return_val_if_fail (ver_str != NULL, FALSE);
343
344     nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, &micro, &nano);
345     GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
346
347     if (nscan >= 3) {
348       if (major > min_major)
349         ret = TRUE;
350       else if (major < min_major)
351         ret = FALSE;
352       else if (minor > min_minor)
353         ret = TRUE;
354       else if (minor < min_minor)
355         ret = FALSE;
356       else if (micro > min_micro)
357         ret = TRUE;
358       /* micro is 1 smaller but we have a nano version, this is the upcoming
359        * release of the requested version and we're ok then */
360       else if (nscan == 4 && nano > 0 && (micro + 1 == min_micro))
361         ret = TRUE;
362       else
363         ret = (micro == min_micro);
364
365       GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
366           micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
367     } else {
368       GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
369           ver_str, feature->plugin_name);
370     }
371
372     gst_object_unref (plugin);
373   } else {
374     GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
375   }
376
377   return ret;
378 }
379
380 /**
381  * gst_plugin_feature_rank_compare_func:
382  * @p1: a #GstPluginFeature
383  * @p2: a #GstPluginFeature
384  *
385  * Compares the two given #GstPluginFeature instances. This function can be
386  * used as a #GCompareFunc when sorting by rank and then by name.
387  *
388  * Returns: negative value if the rank of p1 > the rank of p2 or the ranks are
389  * equal but the name of p1 comes before the name of p2; zero if the rank
390  * and names are equal; positive value if the rank of p1 < the rank of p2 or the
391  * ranks are equal but the name of p2 comes before the name of p1
392  */
393 gint
394 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
395 {
396   GstPluginFeature *f1, *f2;
397   gint diff;
398
399   f1 = (GstPluginFeature *) p1;
400   f2 = (GstPluginFeature *) p2;
401
402   diff = f2->rank - f1->rank;
403   if (diff != 0)
404     return diff;
405
406   diff = strcmp (GST_OBJECT_NAME (f1), GST_OBJECT_NAME (f2));
407
408   return diff;
409 }
410
411 static gboolean
412 parse_feature_name (gchar * str, const gchar ** feature)
413 {
414   if (!str)
415     return FALSE;
416
417   /* works in place */
418   g_strstrip (str);
419
420   if (str[0] != '\0') {
421     *feature = str;
422     return TRUE;
423   }
424
425   return FALSE;
426 }
427
428 static gboolean
429 parse_feature_rank (gchar * str, GstRank * rank)
430 {
431   if (!str)
432     return FALSE;
433
434   /* works in place */
435   g_strstrip (str);
436
437   if (g_ascii_isdigit (str[0])) {
438     unsigned long l;
439     char *endptr;
440     l = strtoul (str, &endptr, 10);
441     if (endptr > str && endptr[0] == 0) {
442       *rank = (GstRank) l;
443     } else {
444       return FALSE;
445     }
446   } else if (g_ascii_strcasecmp (str, "NONE") == 0) {
447     *rank = GST_RANK_NONE;
448   } else if (g_ascii_strcasecmp (str, "MARGINAL") == 0) {
449     *rank = GST_RANK_MARGINAL;
450   } else if (g_ascii_strcasecmp (str, "SECONDARY") == 0) {
451     *rank = GST_RANK_SECONDARY;
452   } else if (g_ascii_strcasecmp (str, "PRIMARY") == 0) {
453     *rank = GST_RANK_PRIMARY;
454   } else if (g_ascii_strcasecmp (str, "MAX") == 0) {
455     *rank = (GstRank) G_MAXINT;
456   } else {
457     return FALSE;
458   }
459
460   return TRUE;
461 }
462
463 void
464 _priv_gst_plugin_feature_rank_initialize (void)
465 {
466   const gchar *env;
467   gchar **split;
468   gchar **walk;
469
470   env = g_getenv ("GST_PLUGIN_FEATURE_RANK");
471
472   if (!env)
473     return;
474
475   split = g_strsplit (env, ",", 0);
476
477   for (walk = split; *walk; walk++) {
478     if (strchr (*walk, ':')) {
479       gchar **values = g_strsplit (*walk, ":", 2);
480
481       if (values[0] && values[1]) {
482         GstRank rank;
483         const gchar *str;
484
485         if (parse_feature_name (values[0], &str)
486             && parse_feature_rank (values[1], &rank)) {
487           GstPluginFeature *feature;
488
489           feature = gst_registry_find_feature (gst_registry_get (), str,
490               GST_TYPE_ELEMENT_FACTORY);
491           if (feature) {
492             gst_plugin_feature_set_rank (feature, rank);
493             GST_DEBUG ("Update rank of plugin feature \"%s\" to %d", str, rank);
494           }
495         }
496       }
497
498       g_strfreev (values);
499     }
500   }
501
502   g_strfreev (split);
503 }