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 /* location libgstelements.so */
59 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
60 PLUGINS_SRCDIR "/gst/elements");
61 _gst_plugin_paths = g_list_prepend(_gst_plugin_paths,
62 PLUGINS_SRCDIR "/gst/types");
63 #endif /* PLUGINS_USE_SRCDIR */
66 static gboolean gst_plugin_load_recurse(gchar *directory,gchar *name) {
68 struct dirent *dirent;
69 gboolean loaded = FALSE;
71 dir = opendir(directory);
73 while (dirent = readdir(dir)) {
74 /* don't want to recurse in place or backwards */
75 if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
76 gst_plugin_load_recurse(g_strjoin("/",directory,dirent->d_name,
82 if (strstr(directory,".so")) {
85 if ((temp = strstr(directory,name)) &&
86 (!strcmp(temp,name))) {
87 gst_plugin_load_absolute(directory);
90 } else if ((temp = strstr(directory,".so")) &&
91 (!strcmp(temp,".so"))) {
92 gst_plugin_load_absolute(directory);
101 * gst_plugin_load_all:
103 * Load all plugins in the path.
105 void gst_plugin_load_all() {
108 path = _gst_plugin_paths;
109 while (path != NULL) {
110 gst_plugin_load_recurse(path->data,NULL);
111 path = g_list_next(path);
117 * @name: name of plugin to load
119 * Load the named plugin. Name should be given as
120 * "libplugin.so".
122 * Returns: whether the plugin was loaded or not
124 gboolean gst_plugin_load(gchar *name) {
128 // g_print("attempting to load plugin '%s'\n",name);
130 path = _gst_plugin_paths;
131 while (path != NULL) {
132 if (gst_plugin_load_absolute(g_module_build_path(path->data,name)))
134 libspath = g_strconcat(path->data,"/.libs",NULL);
135 // g_print("trying to load '%s'\n",g_module_build_path(libspath,name));
136 if (gst_plugin_load_absolute(g_module_build_path(libspath,name))) {
141 // g_print("trying to load '%s' from '%s'\n",name,path->data);
142 if (gst_plugin_load_recurse(path->data,name)) {
145 path = g_list_next(path);
151 * gst_plugin_load_absolute:
152 * @name: name of plugin to load
154 * Returns: whether or not the plugin loaded
156 gboolean gst_plugin_load_absolute(gchar *name) {
158 GstPluginInitFunc initfunc;
161 // g_print("trying to load '%s\n",name);
163 if (g_module_supported() == FALSE) {
164 g_print("wow, you built this on a platform without dynamic loading???\n");
168 module = g_module_open(name,0);
169 if (module != NULL) {
170 if (g_module_symbol(module,"plugin_init",(gpointer *)&initfunc)) {
171 if (plugin = (initfunc)(module)) {
173 plugin->filename = g_strdup(name);
174 _gst_modules = g_list_append(_gst_modules,module);
175 _gst_modules_seqno++;
176 _gst_plugins = g_list_append(_gst_plugins,plugin);
177 _gst_plugins_seqno++;
178 factories = plugin->elements;
180 gst_elementfactory_register((GstElementFactory*)(factories->data));
181 factories = g_list_next(factories);
186 } else if (_gst_plugin_spew) {
187 // if (strstr(g_module_error(),"No such") == NULL)
188 gst_info("error loading plugin: %s\n",g_module_error());
196 * @name: name of new plugin
198 * Create a new plugin with given name.
200 * Returns: new plugin
202 GstPlugin *gst_plugin_new(gchar *name) {
203 GstPlugin *plugin = (GstPlugin *)malloc(sizeof(GstPlugin));
205 plugin->name = g_strdup(name);
206 plugin->longname = NULL;
207 plugin->types = NULL;
208 plugin->elements = NULL;
214 * gst_plugin_set_longname:
215 * @plugin: plugin to set long name of
216 * @longname: new long name
218 * Sets the long name (should be descriptive) of the plugin.
220 void gst_plugin_set_longname(GstPlugin *plugin,gchar *longname) {
221 g_return_if_fail(plugin != NULL);
223 if (plugin->longname) g_free(plugin->longname);
224 plugin->longname = g_strdup(longname);
229 * @name: name of plugin to find
231 * Search the list of registered plugins for one of the given name
233 * Returns: pointer to the #GstPlugin if found, NULL otherwise
235 GstPlugin *gst_plugin_find(gchar *name) {
236 GList *plugins = _gst_plugins;
238 g_return_if_fail(name != NULL);
241 GstPlugin *plugin = (GstPlugin *)plugins->data;
242 // g_print("plugin name is '%s'\n",plugin->name);
244 if (!strcmp(plugin->name,name))
247 plugins = g_list_next(plugins);
253 * gst_plugin_find_elementfactory:
254 * @name: name of elementfactory to find
256 * Find a registered elementfactory by name.
258 * Returns: @GstElementFactory if found, NULL if not
260 GstElementFactory *gst_plugin_find_elementfactory(gchar *name) {
261 GList *plugins, *factories;
262 GstElementFactory *factory;
264 g_return_if_fail(name != NULL);
266 plugins = _gst_plugins;
268 factories = ((GstPlugin *)(plugins->data))->elements;
270 factory = (GstElementFactory*)(factories->data);
271 if (!strcmp(gst_element_get_name(GST_ELEMENT(factory)),name))
272 return (GstElementFactory*)(factory);
273 factories = g_list_next(factories);
275 plugins = g_list_next(plugins);
282 * gst_plugin_add_factory:
283 * @plugin: plugin to add factory to
284 * @factory: factory to add
286 * Add factory to the list of those provided by the element.
288 void gst_plugin_add_factory(GstPlugin *plugin,GstElementFactory *factory) {
289 g_return_if_fail(plugin != NULL);
290 g_return_if_fail(factory != NULL);
292 // g_print("adding factory to plugin\n");
293 plugin->elements = g_list_append(plugin->elements,factory);
296 GList *gst_plugin_get_list() {