2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
27 #include <gst/gstplugin.h>
30 /* list of loaded modules and its sequence number */
32 gint _gst_modules_seqno;
33 /* global list of plugins and its sequence number */
35 gint _gst_plugins_seqno;
36 /* list of paths to check for plugins */
37 GList *_gst_plugin_paths;
39 /* whether or not to spew library load issues */
40 gboolean _gst_plugin_spew = FALSE;
43 void _gst_plugin_initialize() {
45 _gst_modules_seqno = 0;
47 _gst_plugins_seqno = 0;
48 _gst_plugin_paths = NULL;
50 /* add the main (installed) library path */
51 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,PLUGINS_DIR);
53 /* if this is set, we add build-directory paths to the list */
54 #ifdef PLUGINS_USE_SRCDIR
55 /* the catch-all plugins directory */
56 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
57 PLUGINS_SRCDIR "/plugins");
58 /* the libreary directory */
59 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
60 PLUGINS_SRCDIR "/libs");
61 /* location libgstelements.so */
62 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
63 PLUGINS_SRCDIR "/gst/elements");
64 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
65 PLUGINS_SRCDIR "/gst/types");
66 #endif /* PLUGINS_USE_SRCDIR */
69 static gboolean gst_plugin_load_recurse(gchar *directory,gchar *name) {
71 struct dirent *dirent;
72 gboolean loaded = FALSE;
74 //g_print("recursive load of '%s' in '%s'\n", name, directory);
75 dir = opendir(directory);
77 while ((dirent = readdir(dir))) {
78 /* don't want to recurse in place or backwards */
79 if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
80 loaded = gst_plugin_load_recurse(g_strjoin("/",directory,dirent->d_name,
82 if (loaded && name) return TRUE;
87 if (strstr(directory,".so")) {
90 if ((temp = strstr(directory,name)) &&
91 (!strcmp(temp,name))) {
92 loaded = gst_plugin_load_absolute(directory);
95 } else if ((temp = strstr(directory,".so")) &&
96 (!strcmp(temp,".so"))) {
97 loaded = gst_plugin_load_absolute(directory);
106 * gst_plugin_load_all:
108 * Load all plugins in the path.
110 void gst_plugin_load_all() {
113 path = _gst_plugin_paths;
114 while (path != NULL) {
115 gst_plugin_load_recurse(path->data,NULL);
116 path = g_list_next(path);
122 * @name: name of liabrary to load
124 * Load the named liabrary. Name should be given as
125 * "libliabrary.so".
127 * Returns: whether the liabrary was loaded or not
129 gboolean gst_library_load(gchar *name) {
130 // for now this is the same
131 return gst_plugin_load(name);
135 * @name: name of plugin to load
137 * Load the named plugin. Name should be given as
138 * "libplugin.so".
140 * Returns: whether the plugin was loaded or not
142 gboolean gst_plugin_load(gchar *name) {
146 // g_print("attempting to load plugin '%s'\n",name);
148 path = _gst_plugin_paths;
149 while (path != NULL) {
150 if (gst_plugin_load_absolute(g_module_build_path(path->data,name)))
152 libspath = g_strconcat(path->data,"/.libs",NULL);
153 //g_print("trying to load '%s'\n",g_module_build_path(libspath,name));
154 if (gst_plugin_load_absolute(g_module_build_path(libspath,name))) {
159 //g_print("trying to load '%s' from '%s'\n",name,path->data);
160 if (gst_plugin_load_recurse(path->data,g_module_build_path("",name))) {
163 path = g_list_next(path);
169 * gst_plugin_load_absolute:
170 * @name: name of plugin to load
172 * Returns: whether or not the plugin loaded
174 gboolean gst_plugin_load_absolute(gchar *name) {
176 GstPluginInitFunc initfunc;
179 //g_print("trying to absolute load '%s\n",name);
181 if (g_module_supported() == FALSE) {
182 g_print("wow, you built this on a platform without dynamic loading???\n");
186 module = g_module_open(name,G_MODULE_BIND_LAZY);
187 if (module != NULL) {
188 if (g_module_symbol(module,"plugin_init",(gpointer *)&initfunc)) {
189 if ((plugin = (initfunc)(module))) {
191 plugin->filename = g_strdup(name);
192 _gst_modules = g_list_append(_gst_modules,module);
193 _gst_modules_seqno++;
194 _gst_plugins = g_list_append(_gst_plugins,plugin);
195 _gst_plugins_seqno++;
196 factories = plugin->elements;
198 gst_elementfactory_register((GstElementFactory*)(factories->data));
199 factories = g_list_next(factories);
205 } else if (_gst_plugin_spew) {
206 // if (strstr(g_module_error(),"No such") == NULL)
207 gst_info("error loading plugin: %s\n",g_module_error());
215 * @name: name of new plugin
217 * Create a new plugin with given name.
219 * Returns: new plugin
221 GstPlugin *gst_plugin_new(gchar *name) {
222 GstPlugin *plugin = (GstPlugin *)g_malloc(sizeof(GstPlugin));
224 plugin->name = g_strdup(name);
225 plugin->longname = NULL;
226 plugin->types = NULL;
227 plugin->elements = NULL;
233 * gst_plugin_set_longname:
234 * @plugin: plugin to set long name of
235 * @longname: new long name
237 * Sets the long name (should be descriptive) of the plugin.
239 void gst_plugin_set_longname(GstPlugin *plugin,gchar *longname) {
240 g_return_if_fail(plugin != NULL);
242 if (plugin->longname) g_free(plugin->longname);
243 plugin->longname = g_strdup(longname);
248 * @name: name of plugin to find
250 * Search the list of registered plugins for one of the given name
252 * Returns: pointer to the #GstPlugin if found, NULL otherwise
254 GstPlugin *gst_plugin_find(gchar *name) {
255 GList *plugins = _gst_plugins;
257 g_return_val_if_fail(name != NULL, NULL);
260 GstPlugin *plugin = (GstPlugin *)plugins->data;
261 // g_print("plugin name is '%s'\n",plugin->name);
263 if (!strcmp(plugin->name,name))
266 plugins = g_list_next(plugins);
272 * gst_plugin_find_elementfactory:
273 * @name: name of elementfactory to find
275 * Find a registered elementfactory by name.
277 * Returns: @GstElementFactory if found, NULL if not
279 GstElementFactory *gst_plugin_find_elementfactory(gchar *name) {
280 GList *plugins, *factories;
281 GstElementFactory *factory;
283 g_return_val_if_fail(name != NULL, NULL);
285 plugins = _gst_plugins;
287 factories = ((GstPlugin *)(plugins->data))->elements;
289 factory = (GstElementFactory*)(factories->data);
290 if (!strcmp(gst_element_get_name(GST_ELEMENT(factory)),name))
291 return (GstElementFactory*)(factory);
292 factories = g_list_next(factories);
294 plugins = g_list_next(plugins);
301 * gst_plugin_add_factory:
302 * @plugin: plugin to add factory to
303 * @factory: factory to add
305 * Add factory to the list of those provided by the element.
307 void gst_plugin_add_factory(GstPlugin *plugin,GstElementFactory *factory) {
308 g_return_if_fail(plugin != NULL);
309 g_return_if_fail(factory != NULL);
311 // g_print("adding factory to plugin\n");
312 plugin->elements = g_list_append(plugin->elements,factory);
315 GList *gst_plugin_get_list() {