2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstpluginfeature.c: Abstract base class for all plugin features
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.
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.
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.
24 * SECTION:gstpluginfeature
25 * @title: GstPluginfeature
26 * @short_description: Base class for contents of a GstPlugin
27 * @see_also: #GstPlugin
29 * This is a base class for anything that can be added to a #GstPlugin.
32 #include "gst_private.h"
34 #include "gstpluginfeature.h"
35 #include "gstplugin.h"
36 #include "gstregistry.h"
42 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
44 static void gst_plugin_feature_finalize (GObject * object);
46 /* static guint gst_plugin_feature_signals[LAST_SIGNAL] = { 0 }; */
48 G_DEFINE_ABSTRACT_TYPE (GstPluginFeature, gst_plugin_feature, GST_TYPE_OBJECT);
51 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
53 G_OBJECT_CLASS (klass)->finalize = gst_plugin_feature_finalize;
57 gst_plugin_feature_init (GstPluginFeature * feature)
59 /* do nothing, needed because of G_DEFINE_TYPE */
63 gst_plugin_feature_finalize (GObject * object)
65 GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (object);
67 GST_DEBUG ("finalizing feature %p: '%s'", feature, GST_OBJECT_NAME (feature));
69 if (feature->plugin != NULL) {
70 g_object_remove_weak_pointer ((GObject *) feature->plugin,
71 (gpointer *) & feature->plugin);
74 G_OBJECT_CLASS (gst_plugin_feature_parent_class)->finalize (object);
78 * gst_plugin_feature_load:
79 * @feature: (transfer none): the plugin feature to check
81 * Loads the plugin containing @feature if it's not already loaded. @feature is
82 * unaffected; use the return value instead.
84 * Normally this function is used like this:
85 * |[<!-- language="C" -->
86 * GstPluginFeature *loaded_feature;
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;
94 * Returns: (transfer full) (nullable): a reference to the loaded
95 * feature, or %NULL on error
98 gst_plugin_feature_load (GstPluginFeature * feature)
101 GstPluginFeature *real_feature;
103 g_return_val_if_fail (feature != NULL, FALSE);
104 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
106 GST_DEBUG ("loading plugin for feature %p; '%s'", feature,
107 GST_OBJECT_NAME (feature));
109 return gst_object_ref (feature);
111 GST_DEBUG ("loading plugin %s", feature->plugin_name);
112 plugin = gst_plugin_load_by_name (feature->plugin_name);
116 GST_DEBUG ("loaded plugin %s", feature->plugin_name);
117 gst_object_unref (plugin);
119 real_feature = gst_registry_lookup_feature (gst_registry_get (),
120 GST_OBJECT_NAME (feature));
122 if (real_feature == NULL)
124 else if (!real_feature->loaded)
127 GST_TRACER_PLUGIN_FEATURE_LOADED (real_feature);
134 GST_WARNING ("Failed to load plugin containing feature '%s'.",
135 GST_OBJECT_NAME (feature));
141 ("Loaded plugin containing feature '%s', but feature disappeared.",
142 GST_OBJECT_NAME (feature));
147 GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
148 "not found.", GST_OBJECT_NAME (real_feature));
154 * gst_plugin_feature_set_rank:
155 * @feature: feature to rank
156 * @rank: rank value - higher number means more priority rank
158 * Specifies a rank for a plugin feature, so that autoplugging uses
159 * the most appropriate feature.
162 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
164 g_return_if_fail (feature != NULL);
165 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
167 feature->rank = rank;
171 * gst_plugin_feature_get_rank:
172 * @feature: a feature
174 * Gets the rank of a plugin feature.
176 * Returns: The rank of the feature
179 gst_plugin_feature_get_rank (GstPluginFeature * feature)
181 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
183 return feature->rank;
187 * gst_plugin_feature_get_plugin:
188 * @feature: a feature
190 * Get the plugin that provides this feature.
192 * Returns: (transfer full) (nullable): the plugin that provides this
193 * feature, or %NULL. Unref with gst_object_unref() when no
197 gst_plugin_feature_get_plugin (GstPluginFeature * feature)
199 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
201 if (feature->plugin == NULL)
204 return (GstPlugin *) gst_object_ref (feature->plugin);
208 * gst_plugin_feature_get_plugin_name:
209 * @feature: a feature
211 * Get the name of the plugin that provides this feature.
213 * Returns: (nullable): the name of the plugin that provides this
214 * feature, or %NULL if the feature is not associated with a
220 gst_plugin_feature_get_plugin_name (GstPluginFeature * feature)
222 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
224 if (feature->plugin == NULL)
227 return gst_plugin_get_name (feature->plugin);
231 * gst_plugin_feature_list_free:
232 * @list: (transfer full) (element-type Gst.PluginFeature): list
233 * of #GstPluginFeature
235 * Unrefs each member of @list, then frees the list.
238 gst_plugin_feature_list_free (GList * list)
242 for (g = list; g; g = g->next) {
243 GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
245 gst_object_unref (feature);
251 * gst_plugin_feature_list_copy:
252 * @list: (transfer none) (element-type Gst.PluginFeature): list
253 * of #GstPluginFeature
255 * Copies the list of features. Caller should call @gst_plugin_feature_list_free
256 * when done with the list.
258 * Returns: (transfer full) (element-type Gst.PluginFeature): a copy of @list,
259 * with each feature's reference count incremented.
262 gst_plugin_feature_list_copy (GList * list)
264 GList *new_list = NULL;
266 if (G_LIKELY (list)) {
269 new_list = g_list_alloc ();
270 new_list->data = gst_object_ref (list->data);
271 new_list->prev = NULL;
275 last->next = g_list_alloc ();
276 last->next->prev = last;
278 last->data = gst_object_ref (list->data);
288 * gst_plugin_feature_list_debug:
289 * @list: (transfer none) (element-type Gst.PluginFeature): a #GList of
292 * Debug the plugin feature names in @list.
295 gst_plugin_feature_list_debug (GList * list)
297 #ifndef GST_DISABLE_GST_DEBUG
300 gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
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
313 * Checks whether the given plugin feature is at least
314 * the required version
316 * Returns: %TRUE if the plugin feature has at least
317 * the required version, otherwise %FALSE.
320 gst_plugin_feature_check_version (GstPluginFeature * feature,
321 guint min_major, guint min_minor, guint min_micro)
323 GstRegistry *registry;
325 gboolean ret = FALSE;
327 g_return_val_if_fail (feature != NULL, FALSE);
328 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
330 GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
331 feature->plugin_name, GST_OBJECT_NAME (feature));
333 registry = gst_registry_get ();
334 plugin = gst_registry_find_plugin (registry, feature->plugin_name);
337 const gchar *ver_str;
338 guint major, minor, micro, nano;
341 ver_str = gst_plugin_get_version (plugin);
342 g_return_val_if_fail (ver_str != NULL, FALSE);
344 nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, µ, &nano);
345 GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
348 if (major > min_major)
350 else if (major < min_major)
352 else if (minor > min_minor)
354 else if (minor < min_minor)
356 else if (micro > min_micro)
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))
363 ret = (micro == min_micro);
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");
368 GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
369 ver_str, feature->plugin_name);
372 gst_object_unref (plugin);
374 GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
381 * gst_plugin_feature_rank_compare_func:
382 * @p1: a #GstPluginFeature
383 * @p2: a #GstPluginFeature
385 * Compares the two given #GstPluginFeature instances. This function can be
386 * used as a #GCompareFunc when sorting by rank and then by name.
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
394 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
396 GstPluginFeature *f1, *f2;
399 f1 = (GstPluginFeature *) p1;
400 f2 = (GstPluginFeature *) p2;
402 diff = f2->rank - f1->rank;
406 diff = strcmp (GST_OBJECT_NAME (f1), GST_OBJECT_NAME (f2));
412 parse_feature_name (gchar * str, const gchar ** feature)
420 if (str[0] != '\0') {
429 parse_feature_rank (gchar * str, GstRank * rank)
437 if (g_ascii_isdigit (str[0])) {
440 l = strtoul (str, &endptr, 10);
441 if (endptr > str && endptr[0] == 0) {
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;
464 _priv_gst_plugin_feature_rank_initialize (void)
470 env = g_getenv ("GST_PLUGIN_FEATURE_RANK");
475 split = g_strsplit (env, ",", 0);
477 for (walk = split; *walk; walk++) {
478 if (strchr (*walk, ':')) {
479 gchar **values = g_strsplit (*walk, ":", 2);
481 if (values[0] && values[1]) {
485 if (parse_feature_name (values[0], &str)
486 && parse_feature_rank (values[1], &rank)) {
487 GstPluginFeature *feature;
489 feature = gst_registry_find_feature (gst_registry_get (), str,
490 GST_TYPE_ELEMENT_FACTORY);
492 gst_plugin_feature_set_rank (feature, rank);
493 GST_DEBUG ("Update rank of plugin feature \"%s\" to %d", str, rank);
494 gst_object_unref (feature);