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)
132 GST_WARNING ("Failed to load plugin containing feature '%s'.",
133 GST_OBJECT_NAME (feature));
139 ("Loaded plugin containing feature '%s', but feature disappeared.",
140 GST_OBJECT_NAME (feature));
145 GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
146 "not found.", GST_OBJECT_NAME (real_feature));
152 * gst_plugin_feature_set_rank:
153 * @feature: feature to rank
154 * @rank: rank value - higher number means more priority rank
156 * Specifies a rank for a plugin feature, so that autoplugging uses
157 * the most appropriate feature.
160 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
162 g_return_if_fail (feature != NULL);
163 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
165 feature->rank = rank;
169 * gst_plugin_feature_get_rank:
170 * @feature: a feature
172 * Gets the rank of a plugin feature.
174 * Returns: The rank of the feature
177 gst_plugin_feature_get_rank (GstPluginFeature * feature)
179 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
181 return feature->rank;
185 * gst_plugin_feature_get_plugin:
186 * @feature: a feature
188 * Get the plugin that provides this feature.
190 * Returns: (transfer full) (nullable): the plugin that provides this
191 * feature, or %NULL. Unref with gst_object_unref() when no
195 gst_plugin_feature_get_plugin (GstPluginFeature * feature)
197 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
199 if (feature->plugin == NULL)
202 return (GstPlugin *) gst_object_ref (feature->plugin);
206 * gst_plugin_feature_get_plugin_name:
207 * @feature: a feature
209 * Get the name of the plugin that provides this feature.
211 * Returns: (nullable): the name of the plugin that provides this
212 * feature, or %NULL if the feature is not associated with a
218 gst_plugin_feature_get_plugin_name (GstPluginFeature * feature)
220 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
222 if (feature->plugin == NULL)
225 return gst_plugin_get_name (feature->plugin);
229 * gst_plugin_feature_list_free:
230 * @list: (transfer full) (element-type Gst.PluginFeature): list
231 * of #GstPluginFeature
233 * Unrefs each member of @list, then frees the list.
236 gst_plugin_feature_list_free (GList * list)
240 for (g = list; g; g = g->next) {
241 GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
243 gst_object_unref (feature);
249 * gst_plugin_feature_list_copy:
250 * @list: (transfer none) (element-type Gst.PluginFeature): list
251 * of #GstPluginFeature
253 * Copies the list of features. Caller should call @gst_plugin_feature_list_free
254 * when done with the list.
256 * Returns: (transfer full) (element-type Gst.PluginFeature): a copy of @list,
257 * with each feature's reference count incremented.
260 gst_plugin_feature_list_copy (GList * list)
262 GList *new_list = NULL;
264 if (G_LIKELY (list)) {
267 new_list = g_list_alloc ();
268 new_list->data = gst_object_ref (list->data);
269 new_list->prev = NULL;
273 last->next = g_list_alloc ();
274 last->next->prev = last;
276 last->data = gst_object_ref (list->data);
286 * gst_plugin_feature_list_debug:
287 * @list: (transfer none) (element-type Gst.PluginFeature): a #GList of
290 * Debug the plugin feature names in @list.
293 gst_plugin_feature_list_debug (GList * list)
295 #ifndef GST_DISABLE_GST_DEBUG
298 gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
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
311 * Checks whether the given plugin feature is at least
312 * the required version
314 * Returns: %TRUE if the plugin feature has at least
315 * the required version, otherwise %FALSE.
318 gst_plugin_feature_check_version (GstPluginFeature * feature,
319 guint min_major, guint min_minor, guint min_micro)
321 GstRegistry *registry;
323 gboolean ret = FALSE;
325 g_return_val_if_fail (feature != NULL, FALSE);
326 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
328 GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
329 feature->plugin_name, GST_OBJECT_NAME (feature));
331 registry = gst_registry_get ();
332 plugin = gst_registry_find_plugin (registry, feature->plugin_name);
335 const gchar *ver_str;
336 guint major, minor, micro, nano;
339 ver_str = gst_plugin_get_version (plugin);
340 g_return_val_if_fail (ver_str != NULL, FALSE);
342 nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, µ, &nano);
343 GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
346 if (major > min_major)
348 else if (major < min_major)
350 else if (minor > min_minor)
352 else if (minor < min_minor)
354 else if (micro > min_micro)
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))
361 ret = (micro == min_micro);
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");
366 GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
367 ver_str, feature->plugin_name);
370 gst_object_unref (plugin);
372 GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
379 * gst_plugin_feature_rank_compare_func:
380 * @p1: a #GstPluginFeature
381 * @p2: a #GstPluginFeature
383 * Compares the two given #GstPluginFeature instances. This function can be
384 * used as a #GCompareFunc when sorting by rank and then by name.
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
392 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
394 GstPluginFeature *f1, *f2;
397 f1 = (GstPluginFeature *) p1;
398 f2 = (GstPluginFeature *) p2;
400 diff = f2->rank - f1->rank;
404 diff = strcmp (GST_OBJECT_NAME (f1), GST_OBJECT_NAME (f2));