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 * @short_description: Base class for contents of a GstPlugin
26 * @see_also: #GstPlugin
28 * This is a base class for anything that can be added to a #GstPlugin.
31 #include "gst_private.h"
33 #include "gstpluginfeature.h"
34 #include "gstplugin.h"
35 #include "gstregistry.h"
41 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
43 static void gst_plugin_feature_finalize (GObject * object);
45 /* static guint gst_plugin_feature_signals[LAST_SIGNAL] = { 0 }; */
47 G_DEFINE_ABSTRACT_TYPE (GstPluginFeature, gst_plugin_feature, GST_TYPE_OBJECT);
50 gst_plugin_feature_class_init (GstPluginFeatureClass * klass)
52 G_OBJECT_CLASS (klass)->finalize = gst_plugin_feature_finalize;
56 gst_plugin_feature_init (GstPluginFeature * feature)
58 /* do nothing, needed because of G_DEFINE_TYPE */
62 gst_plugin_feature_finalize (GObject * object)
64 GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (object);
66 GST_DEBUG ("finalizing feature %p: '%s'", feature, GST_OBJECT_NAME (feature));
68 if (feature->plugin != NULL) {
69 g_object_remove_weak_pointer ((GObject *) feature->plugin,
70 (gpointer *) & feature->plugin);
73 G_OBJECT_CLASS (gst_plugin_feature_parent_class)->finalize (object);
77 * gst_plugin_feature_load:
78 * @feature: (transfer none): the plugin feature to check
80 * Loads the plugin containing @feature if it's not already loaded. @feature is
81 * unaffected; use the return value instead.
83 * Normally this function is used like this:
85 * GstPluginFeature *loaded_feature;
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;
93 * Returns: (transfer full) (nullable): a reference to the loaded
94 * feature, or %NULL on error
97 gst_plugin_feature_load (GstPluginFeature * feature)
100 GstPluginFeature *real_feature;
102 g_return_val_if_fail (feature != NULL, FALSE);
103 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
105 GST_DEBUG ("loading plugin for feature %p; '%s'", feature,
106 GST_OBJECT_NAME (feature));
108 return gst_object_ref (feature);
110 GST_DEBUG ("loading plugin %s", feature->plugin_name);
111 plugin = gst_plugin_load_by_name (feature->plugin_name);
115 GST_DEBUG ("loaded plugin %s", feature->plugin_name);
116 gst_object_unref (plugin);
118 real_feature = gst_registry_lookup_feature (gst_registry_get (),
119 GST_OBJECT_NAME (feature));
121 if (real_feature == NULL)
123 else if (!real_feature->loaded)
131 GST_WARNING ("Failed to load plugin containing feature '%s'.",
132 GST_OBJECT_NAME (feature));
138 ("Loaded plugin containing feature '%s', but feature disappeared.",
139 GST_OBJECT_NAME (feature));
144 GST_INFO ("Tried to load plugin containing feature '%s', but feature was "
145 "not found.", GST_OBJECT_NAME (real_feature));
151 * gst_plugin_feature_set_rank:
152 * @feature: feature to rank
153 * @rank: rank value - higher number means more priority rank
155 * Specifies a rank for a plugin feature, so that autoplugging uses
156 * the most appropriate feature.
159 gst_plugin_feature_set_rank (GstPluginFeature * feature, guint rank)
161 g_return_if_fail (feature != NULL);
162 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
164 feature->rank = rank;
168 * gst_plugin_feature_get_rank:
169 * @feature: a feature
171 * Gets the rank of a plugin feature.
173 * Returns: The rank of the feature
176 gst_plugin_feature_get_rank (GstPluginFeature * feature)
178 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), GST_RANK_NONE);
180 return feature->rank;
184 * gst_plugin_feature_get_plugin:
185 * @feature: a feature
187 * Get the plugin that provides this feature.
189 * Returns: (transfer full) (nullable): the plugin that provides this
190 * feature, or %NULL. Unref with gst_object_unref() when no
194 gst_plugin_feature_get_plugin (GstPluginFeature * feature)
196 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
198 if (feature->plugin == NULL)
201 return (GstPlugin *) gst_object_ref (feature->plugin);
205 * gst_plugin_feature_get_plugin_name:
206 * @feature: a feature
208 * Get the name of the plugin that provides this feature.
210 * Returns: (nullable): the name of the plugin that provides this
211 * feature, or %NULL if the feature is not associated with a
217 gst_plugin_feature_get_plugin_name (GstPluginFeature * feature)
219 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), NULL);
221 if (feature->plugin == NULL)
224 return gst_plugin_get_name (feature->plugin);
228 * gst_plugin_feature_list_free:
229 * @list: (transfer full) (element-type Gst.PluginFeature): list
230 * of #GstPluginFeature
232 * Unrefs each member of @list, then frees the list.
235 gst_plugin_feature_list_free (GList * list)
239 for (g = list; g; g = g->next) {
240 GstPluginFeature *feature = GST_PLUGIN_FEATURE_CAST (g->data);
242 gst_object_unref (feature);
248 * gst_plugin_feature_list_copy:
249 * @list: (transfer none) (element-type Gst.PluginFeature): list
250 * of #GstPluginFeature
252 * Copies the list of features. Caller should call @gst_plugin_feature_list_free
253 * when done with the list.
255 * Returns: (transfer full) (element-type Gst.PluginFeature): a copy of @list,
256 * with each feature's reference count incremented.
259 gst_plugin_feature_list_copy (GList * list)
261 GList *new_list = NULL;
263 if (G_LIKELY (list)) {
266 new_list = g_list_alloc ();
267 new_list->data = gst_object_ref (list->data);
268 new_list->prev = NULL;
272 last->next = g_list_alloc ();
273 last->next->prev = last;
275 last->data = gst_object_ref (list->data);
285 * gst_plugin_feature_list_debug:
286 * @list: (transfer none) (element-type Gst.PluginFeature): a #GList of
289 * Debug the plugin feature names in @list.
292 gst_plugin_feature_list_debug (GList * list)
294 #ifndef GST_DISABLE_GST_DEBUG
297 gst_plugin_feature_get_name ((GstPluginFeature *) list->data));
304 * gst_plugin_feature_check_version:
305 * @feature: a feature
306 * @min_major: minimum required major version
307 * @min_minor: minimum required minor version
308 * @min_micro: minimum required micro version
310 * Checks whether the given plugin feature is at least
311 * the required version
313 * Returns: %TRUE if the plugin feature has at least
314 * the required version, otherwise %FALSE.
317 gst_plugin_feature_check_version (GstPluginFeature * feature,
318 guint min_major, guint min_minor, guint min_micro)
320 GstRegistry *registry;
322 gboolean ret = FALSE;
324 g_return_val_if_fail (feature != NULL, FALSE);
325 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
327 GST_DEBUG ("Looking up plugin '%s' containing plugin feature '%s'",
328 feature->plugin_name, GST_OBJECT_NAME (feature));
330 registry = gst_registry_get ();
331 plugin = gst_registry_find_plugin (registry, feature->plugin_name);
334 const gchar *ver_str;
335 guint major, minor, micro, nano;
338 ver_str = gst_plugin_get_version (plugin);
339 g_return_val_if_fail (ver_str != NULL, FALSE);
341 nscan = sscanf (ver_str, "%u.%u.%u.%u", &major, &minor, µ, &nano);
342 GST_DEBUG ("version string '%s' parsed to %d values", ver_str, nscan);
345 if (major > min_major)
347 else if (major < min_major)
349 else if (minor > min_minor)
351 else if (minor < min_minor)
353 else if (micro > min_micro)
355 /* micro is 1 smaller but we have a nano version, this is the upcoming
356 * release of the requested version and we're ok then */
357 else if (nscan == 4 && nano > 0 && (micro + 1 == min_micro))
360 ret = (micro == min_micro);
362 GST_DEBUG ("Checking whether %u.%u.%u >= %u.%u.%u? %s", major, minor,
363 micro, min_major, min_minor, min_micro, (ret) ? "yes" : "no");
365 GST_WARNING ("Could not parse version string '%s' of plugin '%s'",
366 ver_str, feature->plugin_name);
369 gst_object_unref (plugin);
371 GST_DEBUG ("Could not find plugin '%s'", feature->plugin_name);
378 * gst_plugin_feature_rank_compare_func:
379 * @p1: a #GstPluginFeature
380 * @p2: a #GstPluginFeature
382 * Compares the two given #GstPluginFeature instances. This function can be
383 * used as a #GCompareFunc when sorting by rank and then by name.
385 * Returns: negative value if the rank of p1 > the rank of p2 or the ranks are
386 * equal but the name of p1 comes before the name of p2; zero if the rank
387 * and names are equal; positive value if the rank of p1 < the rank of p2 or the
388 * ranks are equal but the name of p2 comes before the name of p1
391 gst_plugin_feature_rank_compare_func (gconstpointer p1, gconstpointer p2)
393 GstPluginFeature *f1, *f2;
396 f1 = (GstPluginFeature *) p1;
397 f2 = (GstPluginFeature *) p2;
399 diff = f2->rank - f1->rank;
403 diff = strcmp (GST_OBJECT_NAME (f1), GST_OBJECT_NAME (f2));