2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstplugin.c: Plugin subsystem for loading elements, types, and libs
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
28 #include "gst_private.h"
29 #include "gstplugin.h"
30 #include "gstversion.h"
31 #include "gstregistrypool.h"
34 #include "gstfilter.h"
36 static GModule *main_module = NULL;
37 static GList *_gst_plugin_static = NULL;
39 static GstPlugin* gst_plugin_register_func (GstPluginDesc *desc, GstPlugin *plugin,
42 gst_plugin_error_quark (void)
44 static GQuark quark = 0;
46 quark = g_quark_from_static_string ("gst_plugin_error");
50 /* this function can be called in the GCC constructor extension, before
51 * the _gst_plugin_initialize() was called. In that case, we store the
52 * plugin description in a list to initialize it when we open the main
54 * When the main module is known, we can register the plugin right away.
57 _gst_plugin_register_static (GstPluginDesc *desc)
59 if (main_module == NULL) {
60 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
65 plugin = g_new0 (GstPlugin, 1);
66 plugin->filename = NULL;
67 plugin->module = NULL;
68 plugin = gst_plugin_register_func (desc, plugin, main_module);
71 plugin->module = main_module;
72 gst_registry_pool_add_plugin (plugin);
78 _gst_plugin_initialize (void)
80 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
82 /* now register all static plugins */
83 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static, NULL);
87 gst_plugin_check_version (gint major, gint minor)
89 /* return NULL if the major and minor version numbers are not compatible */
91 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
98 gst_plugin_register_func (GstPluginDesc *desc, GstPlugin *plugin, GModule *module)
100 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
101 GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" has incompatible version, not loading",
106 g_free (plugin->name);
107 plugin->name = g_strdup(desc->name);
109 if (!((desc->plugin_init) (module, plugin))) {
110 GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" failed to initialise",
114 GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
121 * @filename: The filename of the plugin
123 * Creates a plugin from the given filename
125 * Returns: A new GstPlugin object
128 gst_plugin_new (const gchar *filename)
130 GstPlugin *plugin = g_new0 (GstPlugin, 1);
131 plugin->filename = g_strdup (filename);
137 * gst_plugin_load_plugin:
138 * @plugin: The plugin to load
139 * @error: Pointer to a NULL-valued GError.
141 * Load the given plugin.
143 * Returns: whether or not the plugin loaded. Sets @error as appropriate.
146 gst_plugin_load_plugin (GstPlugin *plugin, GError **error)
150 struct stat file_status;
153 g_return_val_if_fail (plugin != NULL, FALSE);
158 filename = plugin->filename;
160 GST_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"", filename);
162 if (g_module_supported () == FALSE) {
165 GST_PLUGIN_ERROR_MODULE,
166 "Dynamic loading not supported");
170 if (stat (filename, &file_status)) {
173 GST_PLUGIN_ERROR_MODULE,
174 "Problem opening file %s (plugin %s)\n",
175 filename, plugin->name);
179 module = g_module_open (filename, G_MODULE_BIND_LAZY);
181 if (module != NULL) {
182 if (g_module_symbol (module, "plugin_desc", (gpointer *)&desc)) {
183 GST_DEBUG (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded, called entry function...", filename);
185 plugin->filename = g_strdup (filename);
186 plugin = gst_plugin_register_func (desc, plugin, module);
188 if (plugin != NULL) {
189 GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded", plugin->filename);
190 plugin->module = module;
197 GST_PLUGIN_ERROR_MODULE,
198 "gst_plugin_register_func failed for plugin \"%s\"",
206 GST_PLUGIN_ERROR_MODULE,
207 "Could not find plugin_desc in \"%s\"",
215 GST_PLUGIN_ERROR_MODULE,
216 "Error loading plugin %s, reason: %s\n",
217 filename, g_module_error());
224 * gst_plugin_unload_plugin:
225 * @plugin: The plugin to unload
227 * Unload the given plugin.
229 * Returns: whether or not the plugin unloaded
232 gst_plugin_unload_plugin (GstPlugin *plugin)
234 g_return_val_if_fail (plugin != NULL, FALSE);
239 if (g_module_close (plugin->module)) {
240 plugin->module = NULL;
241 GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded", plugin->filename);
245 GST_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"", plugin->filename);
251 * gst_plugin_get_name:
252 * @plugin: plugin to get the name of
254 * Get the short name of the plugin
256 * Returns: the name of the plugin
259 gst_plugin_get_name (GstPlugin *plugin)
261 g_return_val_if_fail (plugin != NULL, NULL);
267 * gst_plugin_set_name:
268 * @plugin: plugin to set name of
271 * Sets the name (should be short) of the plugin.
274 gst_plugin_set_name (GstPlugin *plugin, const gchar *name)
276 g_return_if_fail (plugin != NULL);
278 g_free (plugin->name);
280 plugin->name = g_strdup (name);
284 * gst_plugin_set_longname:
285 * @plugin: plugin to set long name of
286 * @longname: new long name
288 * Sets the long name (should be descriptive) of the plugin.
291 gst_plugin_set_longname (GstPlugin *plugin, const gchar *longname)
293 g_return_if_fail(plugin != NULL);
295 g_free(plugin->longname);
297 plugin->longname = g_strdup(longname);
301 * gst_plugin_get_longname:
302 * @plugin: plugin to get long name of
304 * Get the long descriptive name of the plugin
306 * Returns: the long name of the plugin
309 gst_plugin_get_longname (GstPlugin *plugin)
311 g_return_val_if_fail (plugin != NULL, NULL);
313 return plugin->longname;
317 * gst_plugin_get_filename:
318 * @plugin: plugin to get the filename of
320 * get the filename of the plugin
322 * Returns: the filename of the plugin
325 gst_plugin_get_filename (GstPlugin *plugin)
327 g_return_val_if_fail (plugin != NULL, NULL);
329 return plugin->filename;
333 * gst_plugin_is_loaded:
334 * @plugin: plugin to query
336 * queries if the plugin is loaded into memory
338 * Returns: TRUE is loaded, FALSE otherwise
341 gst_plugin_is_loaded (GstPlugin *plugin)
343 g_return_val_if_fail (plugin != NULL, FALSE);
345 return (plugin->module != NULL);
349 gst_plugin_feature_filter (GstPlugin *plugin,
350 GstPluginFeatureFilter filter,
354 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first, user_data);
359 GstPluginFeatureFilter filter;
366 _feature_filter (GstPlugin *plugin, gpointer user_data)
369 FeatureFilterData *data = (FeatureFilterData *) user_data;
371 result = gst_plugin_feature_filter (plugin, data->filter, data->first, data->user_data);
373 data->result = g_list_concat (data->result, result);
380 gst_plugin_list_feature_filter (GList *list,
381 GstPluginFeatureFilter filter,
385 FeatureFilterData data;
388 data.filter = filter;
390 data.user_data = user_data;
393 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
394 g_list_free (result);
401 gst_plugin_name_filter (GstPlugin *plugin, const gchar *name)
403 return (plugin->name && !strcmp (plugin->name, name));
407 * gst_plugin_find_feature:
408 * @plugin: plugin to get the feature from
409 * @name: The name of the feature to find
410 * @type: The type of the feature to find
412 * Find a feature of the given name and type in the given plugin.
414 * Returns: a GstPluginFeature or NULL if the feature was not found.
417 gst_plugin_find_feature (GstPlugin *plugin, const gchar *name, GType type)
420 GstPluginFeature *result = NULL;
421 GstTypeNameData data;
423 g_return_val_if_fail (name != NULL, NULL);
428 walk = gst_filter_run (plugin->features,
429 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE,
433 result = GST_PLUGIN_FEATURE (walk->data);
439 * gst_plugin_add_feature:
440 * @plugin: plugin to add feature to
441 * @feature: feature to add
443 * Add feature to the list of those provided by the plugin.
444 * There is a separate namespace for each plugin feature type.
445 * See #gst_plugin_get_feature_list
448 gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
450 GstPluginFeature *oldfeature;
452 g_return_if_fail (plugin != NULL);
453 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
454 g_return_if_fail (feature != NULL);
456 oldfeature = gst_plugin_find_feature (plugin,
457 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
460 feature->manager = plugin;
461 plugin->features = g_list_prepend (plugin->features, feature);
462 plugin->numfeatures++;
467 * gst_plugin_get_feature_list:
468 * @plugin: the plugin to get the features from
470 * get a list of all the features that this plugin provides
472 * Returns: a GList of features, use g_list_free to free the list.
475 gst_plugin_get_feature_list (GstPlugin *plugin)
477 g_return_val_if_fail (plugin != NULL, NULL);
479 return g_list_copy (plugin->features);
484 * @name: name of plugin to load
486 * Load the named plugin.
488 * Returns: whether the plugin was loaded or not
491 gst_plugin_load (const gchar *name)
494 GError *error = NULL;
496 plugin = gst_registry_pool_find_plugin (name);
498 gboolean result = gst_plugin_load_plugin (plugin, &error);
500 GST_DEBUG (GST_CAT_PLUGIN_LOADING, "load_plugin error: %s\n",
502 g_error_free (error);
507 GST_DEBUG (GST_CAT_PLUGIN_LOADING, "Could not find %s in registry pool",
515 * @name: name of library to load
517 * Load the named library. Name should be given as
518 * "liblibrary.so".
520 * Returns: whether the library was loaded or not
523 gst_library_load (const gchar *name)
527 /* for now this is the same */
528 res = gst_plugin_load (name);