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"
33 /* list of loaded modules and its sequence number */
35 gint _gst_modules_seqno;
36 /* global list of plugins and its sequence number */
38 gint _gst_plugins_seqno;
39 gint _gst_plugin_elementfactories = 0;
40 gint _gst_plugin_types = 0;
41 /* list of paths to check for plugins */
42 GList *_gst_plugin_paths;
44 GList *_gst_libraries;
45 gint _gst_libraries_seqno;
47 /* whether or not to spew library load issues */
48 gboolean _gst_plugin_spew = FALSE;
50 /* whether or not to warn if registry needs rebuild (gstreamer-register sets
52 gboolean _gst_warn_old_registry = TRUE;
54 static gboolean plugin_times_older_than(time_t regtime);
55 static time_t get_time(const char * path);
58 _gst_plugin_initialize (void)
62 _gst_modules_seqno = 0;
64 _gst_plugins_seqno = 0;
65 _gst_plugin_paths = NULL;
66 _gst_libraries = NULL;
67 _gst_libraries_seqno = 0;
70 /* if this is set, we add build-directory paths to the list */
71 #ifdef PLUGINS_USE_SRCDIR
72 /* the catch-all plugins directory */
73 _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
74 PLUGINS_SRCDIR "/plugins");
75 /* the libreary directory */
76 _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
77 PLUGINS_SRCDIR "/libs");
78 /* location libgstelements.so */
79 _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
80 PLUGINS_SRCDIR "/gst/elements");
81 _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
82 PLUGINS_SRCDIR "/gst/types");
83 #endif /* PLUGINS_USE_SRCDIR */
85 /* add the main (installed) library path */
86 _gst_plugin_paths = g_list_prepend (_gst_plugin_paths, PLUGINS_DIR);
88 doc = xmlParseFile (GST_CONFIG_DIR"/reg.xml");
90 if (!doc || strcmp (doc->xmlRootNode->name, "GST-PluginRegistry") ||
91 !plugin_times_older_than(get_time(GST_CONFIG_DIR"/reg.xml"))) {
92 if (_gst_warn_old_registry)
93 g_warning ("gstplugin: registry needs rebuild\n");
94 gst_plugin_load_all ();
97 gst_plugin_load_thyself (doc->xmlRootNode);
103 get_time(const char * path)
106 if (stat(path, &statbuf)) return 0;
107 if (statbuf.st_mtime > statbuf.st_ctime) return statbuf.st_mtime;
108 return statbuf.st_ctime;
112 plugin_times_older_than_recurse(gchar *path, time_t regtime)
115 struct dirent *dirent;
118 time_t pathtime = get_time(path);
120 if (pathtime > regtime) {
121 GST_INFO (GST_CAT_PLUGIN_LOADING,
122 "time for %s was %ld; more recent than registry time of %ld\n",
123 path, (long)pathtime, (long)regtime);
129 while ((dirent = readdir(dir))) {
130 /* don't want to recurse in place or backwards */
131 if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
132 pluginname = g_strjoin("/",path,dirent->d_name,NULL);
133 if (!plugin_times_older_than_recurse(pluginname , regtime)) {
147 plugin_times_older_than(time_t regtime)
149 // return true iff regtime is more recent than the times of all the files
150 // in the plugin dirs.
152 path = _gst_plugin_paths;
153 while (path != NULL) {
154 GST_DEBUG (GST_CAT_PLUGIN_LOADING,
155 "comparing plugin times from %s with %ld\n",
156 (gchar *)path->data, (long) regtime);
157 if(!plugin_times_older_than_recurse(path->data, regtime))
159 path = g_list_next(path);
165 gst_plugin_load_recurse (gchar *directory, gchar *name)
168 struct dirent *dirent;
169 gboolean loaded = FALSE;
172 //g_print("recursive load of '%s' in '%s'\n", name, directory);
173 dir = opendir(directory);
175 while ((dirent = readdir(dir))) {
176 /* don't want to recurse in place or backwards */
177 if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
178 dirname = g_strjoin("/",directory,dirent->d_name,NULL);
179 loaded = gst_plugin_load_recurse(dirname,name);
181 if (loaded && name) {
189 if (strstr(directory,".so")) {
192 if ((temp = strstr(directory,name)) &&
193 (!strcmp(temp,name))) {
194 loaded = gst_plugin_load_absolute(directory);
196 } else if ((temp = strstr(directory,".so")) &&
197 (!strcmp(temp,".so"))) {
198 loaded = gst_plugin_load_absolute(directory);
206 * gst_plugin_load_all:
208 * Load all plugins in the path.
211 gst_plugin_load_all(void)
215 path = _gst_plugin_paths;
216 while (path != NULL) {
217 GST_DEBUG (GST_CAT_PLUGIN_LOADING,"loading plugins from %s\n",(gchar *)path->data);
218 gst_plugin_load_recurse(path->data,NULL);
219 path = g_list_next(path);
221 GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded %d plugins with %d elements and %d types",
222 _gst_plugins_seqno,_gst_plugin_elementfactories,_gst_plugin_types);
227 * @name: name of library to load
229 * Load the named library. Name should be given as
230 * "liblibrary.so".
232 * Returns: whether the library was loaded or not
235 gst_library_load (const gchar *name)
238 GList *libraries = _gst_libraries;
241 if (!strcmp((gchar *)libraries->data, name)) return TRUE;
243 libraries = g_list_next(libraries);
246 // for now this is the same
247 res = gst_plugin_load(name);
250 _gst_libraries = g_list_prepend(_gst_libraries, g_strdup (name));
257 gst_plugin_remove (GstPlugin *plugin)
261 factories = plugin->elements;
263 gst_elementfactory_destroy ((GstElementFactory*)(factories->data));
264 factories = g_list_next(factories);
267 _gst_plugins = g_list_remove(_gst_plugins, plugin);
269 // don't free the stuct because someone can have a handle to it
274 * @name: name of plugin to load
276 * Load the named plugin. Name should be given as
277 * "libplugin.so".
279 * Returns: whether the plugin was loaded or not
282 gst_plugin_load (const gchar *name)
289 //g_print("attempting to load plugin '%s'\n",name);
291 plugin = gst_plugin_find (name);
293 if (plugin && plugin->loaded) return TRUE;
295 path = _gst_plugin_paths;
296 while (path != NULL) {
297 pluginname = g_module_build_path(path->data,name);
298 if (gst_plugin_load_absolute(pluginname)) {
303 libspath = g_strconcat(path->data,"/.libs",NULL);
304 //g_print("trying to load '%s'\n",g_module_build_path(libspath,name));
305 pluginname = g_module_build_path(libspath,name);
307 if (gst_plugin_load_absolute(pluginname)) {
312 //g_print("trying to load '%s' from '%s'\n",name,path->data);
313 pluginname = g_module_build_path("",name);
314 if (gst_plugin_load_recurse(path->data,pluginname)) {
319 path = g_list_next(path);
325 * gst_plugin_load_absolute:
326 * @name: name of plugin to load
328 * Returns: whether or not the plugin loaded
331 gst_plugin_load_absolute (const gchar *name)
334 GstPluginInitFunc initfunc;
336 struct stat file_status;
338 if (g_module_supported() == FALSE) {
339 g_warning("gstplugin: wow, you built this on a platform without dynamic loading???\n");
343 if (stat(name,&file_status)) {
344 // g_print("problem opening file %s\n",name);
348 module = g_module_open(name,G_MODULE_BIND_LAZY);
349 if (module != NULL) {
350 if (g_module_symbol(module,"plugin_init",(gpointer *)&initfunc)) {
351 if ((plugin = (initfunc)(module))) {
352 GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" loaded: %d elements, %d types",
353 plugin->name,plugin->numelements,plugin->numtypes);
354 plugin->filename = g_strdup(name);
355 plugin->loaded = TRUE;
356 _gst_modules = g_list_prepend(_gst_modules,module);
357 _gst_modules_seqno++;
358 _gst_plugins = g_list_prepend(_gst_plugins,plugin);
359 _gst_plugins_seqno++;
360 _gst_plugin_elementfactories += plugin->numelements;
361 _gst_plugin_types += plugin->numtypes;
366 } else if (_gst_plugin_spew) {
367 gst_info("error loading plugin: %s, reason: %s\n", name, g_module_error());
375 * @name: name of new plugin
377 * Create a new plugin with given name.
379 * Returns: new plugin
382 gst_plugin_new (const gchar *name)
386 // return NULL if the plugin is allready loaded
387 plugin = gst_plugin_find (name);
388 if (plugin) return NULL;
390 plugin = (GstPlugin *)g_malloc(sizeof(GstPlugin));
392 plugin->name = g_strdup(name);
393 plugin->longname = NULL;
394 plugin->elements = NULL;
395 plugin->numelements = 0;
396 plugin->types = NULL;
397 plugin->numtypes = 0;
398 plugin->loaded = TRUE;
404 * gst_plugin_get_name:
405 * @plugin: plugin to get the name of
407 * Get the short name of the plugin
409 * Returns: the name of the plugin
412 gst_plugin_get_name (GstPlugin *plugin)
414 g_return_val_if_fail (plugin != NULL, NULL);
420 * gst_plugin_set_name:
421 * @plugin: plugin to set name of
424 * Sets the name (should be short) of the plugin.
427 gst_plugin_set_name (GstPlugin *plugin, const gchar *name)
429 g_return_if_fail (plugin != NULL);
432 g_free (plugin->name);
434 plugin->name = g_strdup (name);
438 * gst_plugin_set_longname:
439 * @plugin: plugin to set long name of
440 * @longname: new long name
442 * Sets the long name (should be descriptive) of the plugin.
445 gst_plugin_set_longname (GstPlugin *plugin, const gchar *longname)
447 g_return_if_fail(plugin != NULL);
449 if (plugin->longname)
450 g_free(plugin->longname);
452 plugin->longname = g_strdup(longname);
456 * gst_plugin_get_longname:
457 * @plugin: plugin to get long name of
459 * Get the long descriptive name of the plugin
461 * Returns: the long name of the plugin
464 gst_plugin_get_longname (GstPlugin *plugin)
466 g_return_val_if_fail (plugin != NULL, NULL);
468 return plugin->longname;
472 * gst_plugin_get_filename:
473 * @plugin: plugin to get the filename of
475 * get the filename of the plugin
477 * Returns: the filename of the plugin
480 gst_plugin_get_filename (GstPlugin *plugin)
482 g_return_val_if_fail (plugin != NULL, NULL);
484 return plugin->filename;
488 * gst_plugin_is_loaded:
489 * @plugin: plugin to query
491 * queries if the plugin is loaded into memory
493 * Returns: TRUE is loaded, FALSE otherwise
496 gst_plugin_is_loaded (GstPlugin *plugin)
498 g_return_val_if_fail (plugin != NULL, FALSE);
500 return plugin->loaded;
506 * @name: name of plugin to find
508 * Search the list of registered plugins for one of the given name
510 * Returns: pointer to the #GstPlugin if found, NULL otherwise
513 gst_plugin_find (const gchar *name)
515 GList *plugins = _gst_plugins;
517 g_return_val_if_fail(name != NULL, NULL);
520 GstPlugin *plugin = (GstPlugin *)plugins->data;
521 // g_print("plugin name is '%s'\n",plugin->name);
523 if (!strcmp(plugin->name,name)) {
527 plugins = g_list_next(plugins);
533 * gst_plugin_find_elementfactory:
534 * @name: name of elementfactory to find
536 * Find a registered elementfactory by name.
538 * Returns: @GstElementFactory if found, NULL if not
541 gst_plugin_find_elementfactory (const gchar *name)
543 GList *plugins, *factories;
544 GstElementFactory *factory;
546 g_return_val_if_fail(name != NULL, NULL);
548 plugins = _gst_plugins;
550 factories = ((GstPlugin *)(plugins->data))->elements;
552 factory = (GstElementFactory*)(factories->data);
553 if (!strcmp(factory->name, name))
554 return (GstElementFactory*)(factory);
555 factories = g_list_next(factories);
557 plugins = g_list_next(plugins);
564 * gst_plugin_load_elementfactory:
565 * @name: name of elementfactory to load
567 * Load a registered elementfactory by name.
569 * Returns: @GstElementFactory if loaded, NULL if not
572 gst_plugin_load_elementfactory (const gchar *name)
574 GList *plugins, *factories;
575 GstElementFactory *factory = NULL;
578 g_return_val_if_fail(name != NULL, NULL);
580 plugins = _gst_plugins;
582 plugin = (GstPlugin *)plugins->data;
583 factories = plugin->elements;
586 factory = (GstElementFactory*)(factories->data);
588 if (!strcmp(factory->name,name)) {
589 if (!plugin->loaded) {
590 gchar *filename = g_strdup (plugin->filename);
591 gchar *pluginname = g_strdup (plugin->name);
593 GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded elementfactory %s from plugin %s",name,plugin->name);
594 gst_plugin_remove(plugin);
595 if (!gst_plugin_load_absolute(filename)) {
596 GST_DEBUG (0,"gstplugin: error loading element factory %s from plugin %s\n", name, pluginname);
601 factory = gst_plugin_find_elementfactory(name);
604 factories = g_list_next(factories);
606 plugins = g_list_next(plugins);
613 * gst_plugin_load_typefactory:
614 * @mime: name of typefactory to load
616 * Load a registered typefactory by mime type.
619 gst_plugin_load_typefactory (const gchar *mime)
621 GList *plugins, *factories;
622 GstTypeFactory *factory;
625 g_return_if_fail (mime != NULL);
627 plugins = g_list_copy (_gst_plugins);
629 plugin = (GstPlugin *)plugins->data;
630 factories = g_list_copy (plugin->types);
633 factory = (GstTypeFactory*)(factories->data);
635 if (!strcmp(factory->mime,mime)) {
636 if (!plugin->loaded) {
637 gchar *filename = g_strdup (plugin->filename);
638 gchar *pluginname = g_strdup (plugin->name);
640 GST_INFO (GST_CAT_PLUGIN_LOADING,"loading type factory for \"%s\" from plugin %s",mime,plugin->name);
641 plugin->loaded = TRUE;
642 gst_plugin_remove(plugin);
643 if (!gst_plugin_load_absolute(filename)) {
644 GST_DEBUG (0,"gstplugin: error loading type factory \"%s\" from plugin %s\n", mime, pluginname);
651 factories = g_list_next(factories);
654 g_list_free (factories);
655 plugins = g_list_next(plugins);
657 g_list_free (plugins);
663 * gst_plugin_add_factory:
664 * @plugin: plugin to add factory to
665 * @factory: factory to add
667 * Add factory to the list of those provided by the plugin.
670 gst_plugin_add_factory (GstPlugin *plugin, GstElementFactory *factory)
672 g_return_if_fail (plugin != NULL);
673 g_return_if_fail (factory != NULL);
675 // g_print("adding factory to plugin\n");
676 plugin->elements = g_list_prepend (plugin->elements, factory);
677 plugin->numelements++;
681 * gst_plugin_add_type:
682 * @plugin: plugin to add type to
683 * @factory: the typefactory to add
685 * Add a typefactory to the list of those provided by the plugin.
688 gst_plugin_add_type (GstPlugin *plugin, GstTypeFactory *factory)
690 g_return_if_fail (plugin != NULL);
691 g_return_if_fail (factory != NULL);
693 // g_print("adding factory to plugin\n");
694 plugin->types = g_list_prepend (plugin->types, factory);
696 gst_type_register (factory);
700 * gst_plugin_get_list:
702 * get the currently loaded plugins
704 * Returns; a GList of GstPlugin elements
707 gst_plugin_get_list(void)
713 * gst_plugin_save_thyself:
714 * @parent: the parent node to save the plugin to
716 * saves the plugin into an XML representation
718 * Returns: the new XML node
721 gst_plugin_save_thyself (xmlNodePtr parent)
723 xmlNodePtr tree, subtree;
724 GList *plugins = NULL, *elements = NULL, *types = NULL;
726 plugins = gst_plugin_get_list();
728 GstPlugin *plugin = (GstPlugin *)plugins->data;
729 tree = xmlNewChild(parent,NULL,"plugin",NULL);
730 xmlNewChild(tree,NULL,"name",plugin->name);
731 xmlNewChild(tree,NULL,"longname",plugin->longname);
732 xmlNewChild(tree,NULL,"filename",plugin->filename);
733 types = plugin->types;
735 GstTypeFactory *factory = (GstTypeFactory *)types->data;
736 subtree = xmlNewChild(tree,NULL,"typefactory",NULL);
738 gst_typefactory_save_thyself(factory, subtree);
740 types = g_list_next(types);
742 elements = plugin->elements;
744 GstElementFactory *factory = (GstElementFactory *)elements->data;
745 subtree = xmlNewChild(tree,NULL,"elementfactory",NULL);
747 gst_elementfactory_save_thyself(factory, subtree);
749 elements = g_list_next(elements);
751 plugins = g_list_next(plugins);
757 * gst_plugin_load_thyself:
758 * @parent: the parent node to load the plugin from
760 * load the plugin from an XML representation
763 gst_plugin_load_thyself (xmlNodePtr parent)
766 gint elementcount = 0;
770 kinderen = parent->xmlChildrenNode; // Dutch invasion :-)
772 if (!strcmp(kinderen->name, "plugin")) {
773 xmlNodePtr field = kinderen->xmlChildrenNode;
774 GstPlugin *plugin = g_new0 (GstPlugin, 1);
775 plugin->elements = NULL;
776 plugin->types = NULL;
777 plugin->loaded = FALSE;
780 if (!strcmp(field->name, "name")) {
781 pluginname = xmlNodeGetContent(field);
782 if (gst_plugin_find(pluginname)) {
788 plugin->name = pluginname;
791 else if (!strcmp(field->name, "longname")) {
792 plugin->longname = xmlNodeGetContent(field);
794 else if (!strcmp(field->name, "filename")) {
795 plugin->filename = xmlNodeGetContent(field);
797 else if (!strcmp(field->name, "elementfactory")) {
798 GstElementFactory *factory = gst_elementfactory_load_thyself(field);
799 gst_plugin_add_factory (plugin, factory);
802 else if (!strcmp(field->name, "typefactory")) {
803 GstTypeFactory *factory = gst_typefactory_load_thyself(field);
804 gst_plugin_add_type (plugin, factory);
813 _gst_plugins = g_list_prepend(_gst_plugins, plugin);
817 kinderen = kinderen->next;
819 GST_INFO (GST_CAT_PLUGIN_LOADING,"added %d registered factories and %d types",elementcount,typecount);
824 * gst_plugin_get_factory_list:
825 * @plugin: the plugin to get the factories from
827 * get a list of all the factories that this plugin provides
829 * Returns: a GList of factories
832 gst_plugin_get_factory_list (GstPlugin *plugin)
834 g_return_val_if_fail (plugin != NULL, NULL);
836 return plugin->elements;
840 * gst_plugin_get_type_list:
841 * @plugin: the plugin to get the typefactories from
843 * get a list of all the typefactories that this plugin provides
845 * Returns: a GList of factories
848 gst_plugin_get_type_list (GstPlugin *plugin)
850 g_return_val_if_fail (plugin != NULL, NULL);
852 return plugin->types;