gst_caps_to_string: print NULL caps correctly
[platform/upstream/gstreamer.git] / gst / gstplugin.c
index d957049..6113e49 100644 (file)
 #include <dirent.h>
 #include <unistd.h>
 
-#undef RTLD_GLOBAL
-
 #include "gst_private.h"
 #include "gstplugin.h"
 #include "gstversion.h"
+#include "gstregistrypool.h"
+#include "gstlog.h"
 #include "config.h"
+#include "gstfilter.h"
 
+static GModule *main_module = NULL;
+static GList *_gst_plugin_static = NULL;
 
-/* list of loaded modules and its sequence number */
-GList *_gst_modules;
-gint _gst_modules_seqno;
-/* global list of plugins and its sequence number */
-GList *_gst_plugins;
-gint _gst_plugins_seqno;
-gint _gst_plugin_elementfactories = 0;
-gint _gst_plugin_types = 0;
-/* list of paths to check for plugins */
-GList *_gst_plugin_paths;
-
-GList *_gst_libraries;
-gint _gst_libraries_seqno;
-
-/* whether or not to spew library load issues */
-gboolean _gst_plugin_spew = FALSE;
-
-/* whether or not to warn if registry needs rebuild (gstreamer-register sets
- * this to false.) */
-gboolean _gst_warn_old_registry = TRUE;
-
-static gboolean plugin_times_older_than(time_t regtime);
-static time_t get_time(const char * path);
-
-void
-_gst_plugin_initialize (void)
+static GstPlugin*      gst_plugin_register_func        (GstPluginDesc *desc, GstPlugin *plugin, 
+                                                        GModule *module);
+GQuark 
+gst_plugin_error_quark (void)
 {
-  xmlDocPtr doc;
-  _gst_modules = NULL;
-  _gst_modules_seqno = 0;
-  _gst_plugins = NULL;
-  _gst_plugins_seqno = 0;
-  _gst_plugin_paths = NULL;
-  _gst_libraries = NULL;
-  _gst_libraries_seqno = 0;
-
-  /* add the main (installed) library path */
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths, PLUGINS_DIR);
-
-  /* if this is set, we add build-directory paths to the list */
-#ifdef PLUGINS_USE_SRCDIR
-  /* the catch-all plugins directory */
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
-                                      PLUGINS_SRCDIR "/plugins");
-  /* the libreary directory */
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
-                                      PLUGINS_SRCDIR "/libs");
-  /* location libgstelements.so */
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
-                                      PLUGINS_SRCDIR "/gst/elements");
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
-                                      PLUGINS_SRCDIR "/gst/types");
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,
-                                      PLUGINS_SRCDIR "/gst/autoplug");
-#endif /* PLUGINS_USE_SRCDIR */
-
-  doc = xmlParseFile (GST_CONFIG_DIR"/reg.xml");
-
-  if (!doc || 
-      !doc->xmlRootNode ||
-      doc->xmlRootNode->name == 0 ||
-      strcmp (doc->xmlRootNode->name, "GST-PluginRegistry") ||
-      !plugin_times_older_than(get_time(GST_CONFIG_DIR"/reg.xml"))) {
-    if (_gst_warn_old_registry)
-       g_warning ("gstplugin: registry needs rebuild: run gstreamer-register\n");
-    gst_plugin_load_all ();
-    return;
-  }
-  gst_plugin_load_thyself (doc->xmlRootNode);
-
-  xmlFreeDoc (doc);
+  static GQuark quark = 0;
+  if (!quark)
+    quark = g_quark_from_static_string ("gst_plugin_error");
+  return quark;
 }
 
-/**
- * gst_plugin_add_path:
- * @path: the directory to add to the search path
- *
- * Add a directory to the path searched for plugins.
- */
+/* this function can be called in the GCC constructor extension, before
+ * the _gst_plugin_initialize() was called. In that case, we store the 
+ * plugin description in a list to initialize it when we open the main
+ * module later on.
+ * When the main module is known, we can register the plugin right away.
+ * */
 void
-gst_plugin_add_path (const gchar *path)
-{
-  _gst_plugin_paths = g_list_prepend (_gst_plugin_paths,g_strdup(path));
-}
-
-static time_t
-get_time(const char * path)
+_gst_plugin_register_static (GstPluginDesc *desc)
 {
-  struct stat statbuf;
-  if (stat(path, &statbuf)) return 0;
-  if (statbuf.st_mtime > statbuf.st_ctime) return statbuf.st_mtime;
-  return statbuf.st_ctime;
-}
-
-static gboolean
-plugin_times_older_than_recurse(gchar *path, time_t regtime)
-{
-  DIR *dir;
-  struct dirent *dirent;
-  gchar *pluginname;
-
-  time_t pathtime = get_time(path);
-
-  if (pathtime > regtime) {
-    GST_INFO (GST_CAT_PLUGIN_LOADING,
-              "time for %s was %ld; more recent than registry time of %ld\n",
-              path, (long)pathtime, (long)regtime);
-    return FALSE;
+  if (main_module == NULL) {
+    _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
   }
+  else {
+    GstPlugin *plugin;
 
-  dir = opendir(path);
-  if (dir) {
-    while ((dirent = readdir(dir))) {
-      /* don't want to recurse in place or backwards */
-      if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
-       pluginname = g_strjoin("/",path,dirent->d_name,NULL);
-       if (!plugin_times_older_than_recurse(pluginname , regtime)) {
-          g_free (pluginname);
-          closedir(dir);
-          return FALSE;
-        }
-        g_free (pluginname);
-      }
-    }
-    closedir(dir);
-  }
-  return TRUE;
-}
+    plugin = g_new0 (GstPlugin, 1);
+    plugin->filename = NULL;
+    plugin->module = NULL;
+    plugin = gst_plugin_register_func (desc, plugin, main_module);
 
-static gboolean
-plugin_times_older_than(time_t regtime)
-{
-  // return true iff regtime is more recent than the times of all the files
-  // in the plugin dirs.
-  GList *path;
-  path = _gst_plugin_paths;
-  while (path != NULL) {
-    GST_DEBUG (GST_CAT_PLUGIN_LOADING,
-              "comparing plugin times from %s with %ld\n",
-              (gchar *)path->data, (long) regtime);
-    if(!plugin_times_older_than_recurse(path->data, regtime))
-       return FALSE;
-    path = g_list_next(path);
-  }
-  return TRUE;
-}
-
-static gboolean
-gst_plugin_load_recurse (gchar *directory, gchar *name)
-{
-  DIR *dir;
-  struct dirent *dirent;
-  gboolean loaded = FALSE;
-  gchar *dirname;
-
-  //g_print("recursive load of '%s' in '%s'\n", name, directory);
-  dir = opendir(directory);
-  if (dir) {
-    while ((dirent = readdir(dir))) {
-      /* don't want to recurse in place or backwards */
-      if (strcmp(dirent->d_name,".") && strcmp(dirent->d_name,"..")) {
-        dirname = g_strjoin("/",directory,dirent->d_name,NULL);
-        loaded = gst_plugin_load_recurse(dirname,name);
-        g_free(dirname);
-       if (loaded && name) {
-          closedir(dir);
-          return TRUE;
-        }
-      }
-    }
-    closedir(dir);
-  } else {
-    if (strstr(directory,".so")) {
-      gchar *temp;
-      if (name) {
-        if ((temp = strstr(directory,name)) &&
-            (!strcmp(temp,name))) {
-          loaded = gst_plugin_load_absolute(directory);
-        }
-      } else if ((temp = strstr(directory,".so")) &&
-                 (!strcmp(temp,".so"))) {
-        loaded = gst_plugin_load_absolute(directory);
-      }
+    if (plugin) {
+      plugin->module = main_module;
+      gst_registry_pool_add_plugin (plugin);
     }
   }
-  return loaded;
 }
 
-/**
- * gst_plugin_load_all:
- *
- * Load all plugins in the path.
- */
 void
-gst_plugin_load_all(void)
+_gst_plugin_initialize (void)
 {
-  GList *path;
+  main_module =  g_module_open (NULL, G_MODULE_BIND_LAZY);
 
-  path = _gst_plugin_paths;
-  while (path != NULL) {
-    GST_INFO (GST_CAT_PLUGIN_LOADING,"loading plugins from %s",(gchar *)path->data);
-    gst_plugin_load_recurse(path->data,NULL);
-    path = g_list_next(path);
-  }
-  GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded %d plugins with %d elements and %d types",
-       _gst_plugins_seqno,_gst_plugin_elementfactories,_gst_plugin_types);
+  /* now register all static plugins */
+  g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static, NULL);
 }
 
-/**
- * gst_library_load:
- * @name: name of library to load
- *
- * Load the named library.  Name should be given as
- * &quot;liblibrary.so&quot;.
- *
- * Returns: whether the library was loaded or not
- */
-gboolean
-gst_library_load (const gchar *name)
+static gboolean
+gst_plugin_check_version (gint major, gint minor)
 {
-  gboolean res;
-  GList *libraries = _gst_libraries;
-
-  while (libraries) {
-    if (!strcmp((gchar *)libraries->data, name)) return TRUE;
-
-    libraries = g_list_next(libraries);
-  }
-
-  // for now this is the same
-  res = gst_plugin_load(name);
-
-  if (res) {
-    _gst_libraries = g_list_prepend(_gst_libraries, g_strdup (name));
-  }
+  /* return NULL if the major and minor version numbers are not compatible */
+  /* with ours. */
+  if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR) 
+    return FALSE;
 
-  return res;
+  return TRUE;
 }
 
-static void
-gst_plugin_remove (GstPlugin *plugin)
+static GstPlugin*
+gst_plugin_register_func (GstPluginDesc *desc, GstPlugin *plugin, GModule *module)
 {
-  GList *factories;
-
-  factories = plugin->elements;
-  while (factories) {
-    gst_elementfactory_destroy ((GstElementFactory*)(factories->data));
-    factories = g_list_next(factories);
+  if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
+    GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" has incompatible version, not loading",
+       plugin->filename);
+    return NULL;
   }
 
-  _gst_plugins = g_list_remove(_gst_plugins, plugin);
+  g_free (plugin->name);
+  plugin->name = g_strdup(desc->name);
+
+  if (!((desc->plugin_init) (module, plugin))) {
+    GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" failed to initialise",
+       plugin->filename);
+    return NULL;
+  }
+  GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
 
-  // don't free the stuct because someone can have a handle to it
+  return plugin;
 }
 
 /**
- * gst_plugin_load:
- * @name: name of plugin to load
+ * gst_plugin_new:
+ * @filename: The filename of the plugin
  *
- * Load the named plugin.  Name should be given as
- * &quot;libplugin.so&quot;.
+ * Creates a plugin from the given filename
  *
- * Returns: whether the plugin was loaded or not
+ * Returns: A new GstPlugin object
  */
-gboolean
-gst_plugin_load (const gchar *name)
+GstPlugin*
+gst_plugin_new (const gchar *filename)
 {
-  GList *path;
-  gchar *libspath;
-  GstPlugin *plugin;
-  gchar *pluginname;
-
-  //g_print("attempting to load plugin '%s'\n",name);
-
-  plugin = gst_plugin_find (name);
+  GstPlugin *plugin = g_new0 (GstPlugin, 1);
+  plugin->filename = g_strdup (filename);
 
-  if (plugin && plugin->loaded) return TRUE;
-
-  path = _gst_plugin_paths;
-  while (path != NULL) {
-    pluginname = g_module_build_path(path->data,name);
-    if (gst_plugin_load_absolute(pluginname)) {
-      g_free(pluginname);
-      return TRUE;
-    }
-    g_free(pluginname);
-    libspath = g_strconcat(path->data,"/.libs",NULL);
-    //g_print("trying to load '%s'\n",g_module_build_path(libspath,name));
-    pluginname = g_module_build_path(libspath,name);
-    g_free(libspath);
-    if (gst_plugin_load_absolute(pluginname)) {
-      g_free(pluginname);
-      return TRUE;
-    }
-    g_free(pluginname);
-    //g_print("trying to load '%s' from '%s'\n",name,path->data);
-    pluginname = g_module_build_path("",name);
-    if (gst_plugin_load_recurse(path->data,pluginname)) {
-      g_free(pluginname);
-      return TRUE;
-    }
-    g_free(pluginname);
-    path = g_list_next(path);
-  }
-  return FALSE;
+  return plugin;
 }
 
 /**
- * gst_plugin_load_absolute:
- * @name: name of plugin to load
+ * gst_plugin_load_plugin:
+ * @plugin: The plugin to load
+ * @error: Pointer to a NULL-valued GError.
  *
- * Returns: whether or not the plugin loaded
+ * Load the given plugin.
+ *
+ * Returns: whether or not the plugin loaded. Sets @error as appropriate.
  */
 gboolean
-gst_plugin_load_absolute (const gchar *name)
+gst_plugin_load_plugin (GstPlugin *plugin, GError **error)
 {
   GModule *module;
   GstPluginDesc *desc;
-  GstPlugin *plugin;
   struct stat file_status;
+  gchar *filename;
 
-  GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" loading", name);
+  g_return_val_if_fail (plugin != NULL, FALSE);
 
-  if (g_module_supported() == FALSE) {
-    g_warning("gstplugin: wow, you built this on a platform without dynamic loading???\n");
+  if (plugin->module) 
+    return TRUE;
+
+  filename = plugin->filename;
+
+  GST_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"", filename);
+
+  if (g_module_supported () == FALSE) {
+    g_set_error (error,
+                 GST_PLUGIN_ERROR,
+                 GST_PLUGIN_ERROR_MODULE,
+                 "Dynamic loading not supported");
     return FALSE;
   }
 
-  if (stat(name,&file_status)) {
-    //g_print("problem opening file %s\n",name);
+  if (stat (filename, &file_status)) {
+    g_set_error (error,
+                 GST_PLUGIN_ERROR,
+                 GST_PLUGIN_ERROR_MODULE,
+                 "Problem opening file %s (plugin %s)\n",
+                 filename, plugin->name); 
     return FALSE;
   }
 
-  module = g_module_open(name,G_MODULE_BIND_LAZY);
+  module = g_module_open (filename, G_MODULE_BIND_LAZY);
+
   if (module != NULL) {
-    if (g_module_symbol(module,"plugin_desc",(gpointer *)&desc)) {
-      GST_INFO (GST_CAT_PLUGIN_LOADING,"loading plugin \"%s\"...", name);
-      plugin = gst_plugin_new(desc->name, desc->major_version, desc->minor_version);
-      if (plugin != NULL) {
-        plugin->filename = g_strdup(name);
-       if (!((desc->plugin_init)(module, plugin))) {
-          GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" failed to initialise",
-             plugin->name);
-         g_free(plugin);
-         plugin = NULL;
-       }
-      }
+    gpointer ptr;
+
+    if (g_module_symbol (module, "plugin_desc", &ptr)) {
+      desc = (GstPluginDesc *)ptr;
+
+      GST_DEBUG (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded, called entry function...", filename);
+
+      plugin->filename = g_strdup (filename);
+      plugin = gst_plugin_register_func (desc, plugin, module);
 
       if (plugin != NULL) {
-        GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" loaded: %d elements, %d types",
-             plugin->name,plugin->numelements,plugin->numtypes);
-        plugin->loaded = TRUE;
-        _gst_modules = g_list_prepend(_gst_modules,module);
-        _gst_modules_seqno++;
-        _gst_plugins = g_list_prepend(_gst_plugins,plugin);
-        _gst_plugins_seqno++;
-        _gst_plugin_elementfactories += plugin->numelements;
-        _gst_plugin_types += plugin->numtypes;
+        GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded", plugin->filename);
+        plugin->module = module;
         return TRUE;
       }
+      else {
+       /* plugin == NULL */
+        g_set_error (error,
+                     GST_PLUGIN_ERROR,
+                     GST_PLUGIN_ERROR_MODULE,
+                     "gst_plugin_register_func failed for plugin \"%s\"",
+                     filename);
+        return FALSE;
+      }
     }
-    return TRUE;
-  } else if (_gst_plugin_spew) {
-    // FIXME this should be some standard gst mechanism!!!
-    g_printerr ("error loading plugin %s, reason: %s\n", name, g_module_error());
-  }
+    else {
+      g_set_error (error,
+                   GST_PLUGIN_ERROR,
+                   GST_PLUGIN_ERROR_MODULE,
+                   "Could not find plugin_desc in \"%s\"",
+                   filename);
+    }
+    return FALSE;
+  } 
   else {
-    GST_INFO (GST_CAT_PLUGIN_LOADING, "error loading plugin %s, reason: %s\n", name, g_module_error());
+    g_set_error (error,
+                 GST_PLUGIN_ERROR,
+                 GST_PLUGIN_ERROR_MODULE,
+                 "Error loading plugin %s, reason: %s\n",
+                 filename, g_module_error());
+    return FALSE;
   }
-
-  return FALSE;
 }
 
+
 /**
- * gst_plugin_new:
- * @name: name of new plugin
- * @major: major version number of core that plugin is compatible with
- * @minor: minor version number of core that plugin is compatible with
+ * gst_plugin_unload_plugin:
+ * @plugin: The plugin to unload
  *
- * Create a new plugin with given name.
+ * Unload the given plugin.
  *
- * Returns: new plugin, or NULL if plugin couldn't be created, due to
- * incompatible version number, or name already being allocated)
+ * Returns: whether or not the plugin unloaded
  */
-GstPlugin*
-gst_plugin_new (const gchar *name, gint major, gint minor)
+gboolean
+gst_plugin_unload_plugin (GstPlugin *plugin)
 {
-  GstPlugin *plugin;
-
-  // return NULL if the major and minor version numbers are not compatible
-  // with ours.
-  if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR) return NULL;
-
-  // return NULL if the plugin is allready loaded
-  plugin = gst_plugin_find (name);
-  if (plugin) return NULL;
-
-  plugin = (GstPlugin *)g_malloc(sizeof(GstPlugin));
+  g_return_val_if_fail (plugin != NULL, FALSE);
 
-  plugin->name = g_strdup(name);
-  plugin->longname = NULL;
-  plugin->elements = NULL;
-  plugin->numelements = 0;
-  plugin->types = NULL;
-  plugin->numtypes = 0;
-  plugin->autopluggers = NULL;
-  plugin->numautopluggers = 0;
-  plugin->loaded = TRUE;
+  if (!plugin->module) 
+    return TRUE;
 
-  return plugin;
+  if (g_module_close (plugin->module)) {
+    plugin->module = NULL;
+    GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded", plugin->filename);
+    return TRUE;
+  }
+  else {
+    GST_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"", plugin->filename);
+    return FALSE;
+  }
 }
 
 /**
@@ -473,8 +279,7 @@ gst_plugin_set_name (GstPlugin *plugin, const gchar *name)
 {
   g_return_if_fail (plugin != NULL);
 
-  if (plugin->name)
-    g_free (plugin->name);
+  g_free (plugin->name);
 
   plugin->name = g_strdup (name);
 }
@@ -491,8 +296,7 @@ gst_plugin_set_longname (GstPlugin *plugin, const gchar *longname)
 {
   g_return_if_fail(plugin != NULL);
 
-  if (plugin->longname)
-    g_free(plugin->longname);
+  g_free(plugin->longname);
 
   plugin->longname = g_strdup(longname);
 }
@@ -542,473 +346,226 @@ gst_plugin_is_loaded (GstPlugin *plugin)
 {
   g_return_val_if_fail (plugin != NULL, FALSE);
 
-  return plugin->loaded;
+  return (plugin->module != NULL);
 }
 
-
 /**
- * gst_plugin_find:
- * @name: name of plugin to find
- *
- * Search the list of registered plugins for one of the given name
- *
- * Returns: pointer to the #GstPlugin if found, NULL otherwise
- */
-GstPlugin*
-gst_plugin_find (const gchar *name)
-{
-  GList *plugins = _gst_plugins;
-
-  g_return_val_if_fail(name != NULL, NULL);
-
-  while (plugins) {
-    GstPlugin *plugin = (GstPlugin *)plugins->data;
-//    g_print("plugin name is '%s'\n",plugin->name);
-    if (plugin->name) {
-      if (!strcmp(plugin->name,name)) {
-        return plugin;
-      }
-    }
-    plugins = g_list_next(plugins);
-  }
-  return NULL;
-}
-
-static GstElementFactory*
-gst_plugin_find_elementfactory (const gchar *name)
-{
-  GList *plugins, *factories;
-  GstElementFactory *factory;
-
-  g_return_val_if_fail(name != NULL, NULL);
-
-  plugins = _gst_plugins;
-  while (plugins) {
-    factories = ((GstPlugin *)(plugins->data))->elements;
-    while (factories) {
-      factory = (GstElementFactory*)(factories->data);
-      if (!strcmp(factory->name, name))
-        return (GstElementFactory*)(factory);
-      factories = g_list_next(factories);
-    }
-    plugins = g_list_next(plugins);
-  }
-
-  return NULL;
-}
-
-/**
- * gst_plugin_load_elementfactory:
- * @name: name of elementfactory to load
+ * gst_plugin_feature_list:
+ * @plugin: plugin to query
+ * @filter: the filter to use
+ * @first: only return first match
+ * @user_data: user data passed to the filter function
  *
- * Load a registered elementfactory by name.
+ * Runs a filter against all plugin features and returns a GList with
+ * the results. If the first flag is set, only the first match is 
+ * returned (as a list with a single object).
  *
- * Returns: @GstElementFactory if loaded, NULL if not
+ * Returns: a GList of features, g_list_free after use.
  */
-GstElementFactory*
-gst_plugin_load_elementfactory (const gchar *name)
-{
-  GList *plugins, *factories;
-  GstElementFactory *factory = NULL;
-  GstPlugin *plugin;
-
-  g_return_val_if_fail(name != NULL, NULL);
-
-  plugins = _gst_plugins;
-  while (plugins) {
-    plugin = (GstPlugin *)plugins->data;
-    factories = plugin->elements;
-
-    while (factories) {
-      factory = (GstElementFactory*)(factories->data);
-
-      if (!strcmp(factory->name,name)) {
-       if (!plugin->loaded) {
-          gchar *filename = g_strdup (plugin->filename);
-         gchar *pluginname = g_strdup (plugin->name);
-
-          GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded elementfactory %s from plugin %s",name,plugin->name);
-         gst_plugin_remove(plugin);
-         if (!gst_plugin_load_absolute(filename)) {
-           GST_DEBUG (0,"gstplugin: error loading element factory %s from plugin %s\n", name, pluginname);
-         }
-         g_free (pluginname);
-         g_free (filename);
-       }
-       factory = gst_plugin_find_elementfactory(name);
-        return factory;
-      }
-      factories = g_list_next(factories);
-    }
-    plugins = g_list_next(plugins);
-  }
-
-  return factory;
-}
-
-static GstAutoplugFactory*
-gst_plugin_find_autoplugfactory (const gchar *name)
+GList*
+gst_plugin_feature_filter (GstPlugin *plugin,
+                          GstPluginFeatureFilter filter,
+                          gboolean first,
+                          gpointer user_data)
 {
-  GList *plugins, *factories;
-  GstAutoplugFactory *factory;
-
-  g_return_val_if_fail(name != NULL, NULL);
-
-  plugins = _gst_plugins;
-  while (plugins) {
-    factories = ((GstPlugin *)(plugins->data))->autopluggers;
-    while (factories) {
-      factory = (GstAutoplugFactory*)(factories->data);
-      if (!strcmp(factory->name, name))
-        return (GstAutoplugFactory*)(factory);
-      factories = g_list_next(factories);
-    }
-    plugins = g_list_next(plugins);
-  }
-
-  return NULL;
+  return gst_filter_run (plugin->features, (GstFilterFunc) filter, first, user_data);
 }
-/**
- * gst_plugin_load_autoplugfactory:
- * @name: name of autoplugfactory to load
- *
- * Load a registered autoplugfactory by name.
- *
- * Returns: @GstAutoplugFactory if loaded, NULL if not
- */
-GstAutoplugFactory*
-gst_plugin_load_autoplugfactory (const gchar *name)
-{
-  GList *plugins, *factories;
-  GstAutoplugFactory *factory = NULL;
-  GstPlugin *plugin;
-
-  g_return_val_if_fail(name != NULL, NULL);
-
-  plugins = _gst_plugins;
-  while (plugins) {
-    plugin = (GstPlugin *)plugins->data;
-    factories = plugin->autopluggers;
-
-    while (factories) {
-      factory = (GstAutoplugFactory*)(factories->data);
-
-      if (!strcmp(factory->name,name)) {
-       if (!plugin->loaded) {
-          gchar *filename = g_strdup (plugin->filename);
-         gchar *pluginname = g_strdup (plugin->name);
-
-          GST_INFO (GST_CAT_PLUGIN_LOADING,"loaded autoplugfactory %s from plugin %s",name,plugin->name);
-         gst_plugin_remove(plugin);
-         if (!gst_plugin_load_absolute(filename)) {
-           GST_DEBUG (0,"gstplugin: error loading autoplug factory %s from plugin %s\n", name, pluginname);
-         }
-         g_free (pluginname);
-         g_free (filename);
-       }
-       factory = gst_plugin_find_autoplugfactory(name);
-        return factory;
-      }
-      factories = g_list_next(factories);
-    }
-    plugins = g_list_next(plugins);
-  }
 
-  return factory;
-}
+typedef struct
+{ 
+  GstPluginFeatureFilter filter;
+  gboolean               first;
+  gpointer               user_data;
+  GList                 *result;
+} FeatureFilterData;
 
-/**
- * gst_plugin_load_typefactory:
- * @mime: name of typefactory to load
- *
- * Load a registered typefactory by mime type.
- */
-void
-gst_plugin_load_typefactory (const gchar *mime)
+static gboolean
+_feature_filter (GstPlugin *plugin, gpointer user_data)
 {
-  GList *plugins, *factories;
-  GstTypeFactory *factory;
-  GstPlugin *plugin;
-
-  g_return_if_fail (mime != NULL);
-
-  plugins = g_list_copy (_gst_plugins);
-  while (plugins) {
-    plugin = (GstPlugin *)plugins->data;
-    factories = g_list_copy (plugin->types);
-
-    while (factories) {
-      factory = (GstTypeFactory*)(factories->data);
-
-      if (!strcmp(factory->mime,mime)) {
-       if (!plugin->loaded) {
-          gchar *filename = g_strdup (plugin->filename);
-         gchar *pluginname = g_strdup (plugin->name);
-
-          GST_INFO (GST_CAT_PLUGIN_LOADING,"loading type factory for \"%s\" from plugin %s",mime,plugin->name);
-         plugin->loaded = TRUE;
-         gst_plugin_remove(plugin);
-         if (!gst_plugin_load_absolute(filename)) {
-           GST_DEBUG (0,"gstplugin: error loading type factory \"%s\" from plugin %s\n", mime, pluginname);
-         }
-         g_free (filename);
-         g_free (pluginname);
-       }
-       //return;
-      }
-      factories = g_list_next(factories);
-    }
+  GList *result;
+  FeatureFilterData *data = (FeatureFilterData *) user_data;
 
-    g_list_free (factories);
-    plugins = g_list_next(plugins);
+  result = gst_plugin_feature_filter (plugin, data->filter, data->first, data->user_data);
+  if (result) {
+    data->result = g_list_concat (data->result, result);
+    return TRUE;
   }
-  g_list_free (plugins);
-
-  return;
-}
-
-/**
- * gst_plugin_add_factory:
- * @plugin: plugin to add factory to
- * @factory: factory to add
- *
- * Add factory to the list of those provided by the plugin.
- */
-void
-gst_plugin_add_factory (GstPlugin *plugin, GstElementFactory *factory)
-{
-  g_return_if_fail (plugin != NULL);
-  g_return_if_fail (factory != NULL);
-
-//  g_print("adding factory to plugin\n");
-  plugin->elements = g_list_prepend (plugin->elements, factory);
-  plugin->numelements++;
+  return FALSE;
 }
 
 /**
- * gst_plugin_add_type:
- * @plugin: plugin to add type to
- * @factory: the typefactory to add
- *
- * Add a typefactory to the list of those provided by the plugin.
+ * gst_plugin_list_feature_list:
+ * @list: a list of plugins to query
+ * @filter: the filter to use
+ * @first: only return first match
+ * @user_data: user data passed to the filter function
+ *
+ * Runs a filter against all plugin features of the plugins in the given
+ * list and returns a GList with the results. 
+ * If the first flag is set, only the first match is 
+ * returned (as a list with a single object).
+ *
+ * Returns: a GList of features, g_list_free after use.
  */
-void
-gst_plugin_add_type (GstPlugin *plugin, GstTypeFactory *factory)
+GList*
+gst_plugin_list_feature_filter  (GList *list, 
+                                GstPluginFeatureFilter filter,
+                                gboolean first,
+                                gpointer user_data)
 {
-  g_return_if_fail (plugin != NULL);
-  g_return_if_fail (factory != NULL);
+  FeatureFilterData data;
+  GList *result;
 
-//  g_print("adding factory to plugin\n");
-  plugin->types = g_list_prepend (plugin->types, factory);
-  plugin->numtypes++;
-  gst_type_register (factory);
-}
+  data.filter = filter;
+  data.first = first;
+  data.user_data = user_data;
+  data.result = NULL;
 
-/**
- * gst_plugin_add_autoplugger:
- * @plugin: plugin to add the autoplugger to
- * @factory: the autoplugfactory to add
- *
- * Add an autoplugfactory to the list of those provided by the plugin.
- */
-void
-gst_plugin_add_autoplugger (GstPlugin *plugin, GstAutoplugFactory *factory)
-{
-  g_return_if_fail (plugin != NULL);
-  g_return_if_fail (factory != NULL);
+  result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
+  g_list_free (result);
 
-//  g_print("adding factory to plugin\n");
-  plugin->autopluggers = g_list_prepend (plugin->autopluggers, factory);
-  plugin->numautopluggers++;
+  return data.result;
 }
 
 /**
- * gst_plugin_get_list:
+ * gst_plugin_name_filter:
+ * @plugin: the plugin to check
+ * @name: the name of the plugin
  *
- * get the currently loaded plugins
+ * A standard filterthat returns TRUE when the plugin is of the
+ * given name.
  *
- * Returns; a GList of GstPlugin elements
+ * Returns: TRUE if the plugin is of the given name.
  */
-GList*
-gst_plugin_get_list (void)
+gboolean
+gst_plugin_name_filter (GstPlugin *plugin, const gchar *name)
 {
-  return _gst_plugins;
+  return (plugin->name && !strcmp (plugin->name, name));
 }
 
 /**
- * gst_plugin_save_thyself:
- * @parent: the parent node to save the plugin to
+ * gst_plugin_find_feature:
+ * @plugin: plugin to get the feature from
+ * @name: The name of the feature to find
+ * @type: The type of the feature to find
  *
- * saves the plugin into an XML representation
+ * Find a feature of the given name and type in the given plugin.
  *
- * Returns: the new XML node
+ * Returns: a GstPluginFeature or NULL if the feature was not found.
  */
-xmlNodePtr
-gst_plugin_save_thyself (xmlNodePtr parent)
+GstPluginFeature*
+gst_plugin_find_feature (GstPlugin *plugin, const gchar *name, GType type)
 {
-  xmlNodePtr tree, subtree;
-  GList *plugins = NULL, *elements = NULL, *types = NULL, *autopluggers = NULL;
+  GList *walk;
+  GstPluginFeature *result = NULL;
+  GstTypeNameData data;
 
-  plugins = gst_plugin_get_list ();
-  while (plugins) {
-    GstPlugin *plugin = (GstPlugin *)plugins->data;
+  g_return_val_if_fail (name != NULL, NULL);
 
-    tree = xmlNewChild (parent, NULL, "plugin", NULL);
-    xmlNewChild (tree, NULL, "name", plugin->name);
-    xmlNewChild (tree, NULL, "longname", plugin->longname);
-    xmlNewChild (tree, NULL, "filename", plugin->filename);
+  data.type = type;
+  data.name = name;
+  
+  walk = gst_filter_run (plugin->features, 
+                        (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE,
+                        &data);
 
-    types = plugin->types;
-    while (types) {
-      GstTypeFactory *factory = (GstTypeFactory *)types->data;
-      subtree = xmlNewChild(tree, NULL, "typefactory", NULL);
+  if (walk) 
+    result = GST_PLUGIN_FEATURE (walk->data);
 
-      gst_typefactory_save_thyself (factory, subtree);
-
-      types = g_list_next (types);
-    }
-    elements = plugin->elements;
-    while (elements) {
-      GstElementFactory *factory = (GstElementFactory *)elements->data;
-      subtree = xmlNewChild (tree, NULL, "elementfactory", NULL);
-
-      gst_elementfactory_save_thyself (factory, subtree);
-
-      elements = g_list_next (elements);
-    }
-    autopluggers = plugin->autopluggers;
-    while (autopluggers) {
-      GstAutoplugFactory *factory = (GstAutoplugFactory *)autopluggers->data;
-      subtree = xmlNewChild (tree, NULL, "autoplugfactory", NULL);
-
-      gst_autoplugfactory_save_thyself (factory, subtree);
-
-      autopluggers = g_list_next (autopluggers);
-    }
-    plugins = g_list_next (plugins);
-  }
-  return parent;
+  return result;
 }
 
 /**
- * gst_plugin_load_thyself:
- * @parent: the parent node to load the plugin from
+ * gst_plugin_add_feature:
+ * @plugin: plugin to add feature to
+ * @feature: feature to add
  *
- * load the plugin from an XML representation
+ * Add feature to the list of those provided by the plugin.
+ * There is a separate namespace for each plugin feature type.
+ * See #gst_plugin_get_feature_list
  */
 void
-gst_plugin_load_thyself (xmlNodePtr parent)
+gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
 {
-  xmlNodePtr kinderen;
-  gint elementcount = 0;
-  gint autoplugcount = 0;
-  gint typecount = 0;
-  gchar *pluginname;
-
-  kinderen = parent->xmlChildrenNode; // Dutch invasion :-)
-  while (kinderen) {
-    if (!strcmp (kinderen->name, "plugin")) {
-      xmlNodePtr field = kinderen->xmlChildrenNode;
-      GstPlugin *plugin = g_new0 (GstPlugin, 1);
-
-      plugin->elements = NULL;
-      plugin->types = NULL;
-      plugin->loaded = FALSE;
-
-      while (field) {
-       if (!strcmp (field->name, "name")) {
-          pluginname = xmlNodeGetContent (field);
-         if (gst_plugin_find (pluginname)) {
-            g_free (pluginname);
-            g_free (plugin);
-           plugin = NULL;
-            break;
-         } else {
-           plugin->name = pluginname;
-         }
-       }
-       else if (!strcmp (field->name, "longname")) {
-         plugin->longname = xmlNodeGetContent (field);
-       }
-       else if (!strcmp (field->name, "filename")) {
-         plugin->filename = xmlNodeGetContent (field);
-       }
-       else if (!strcmp (field->name, "elementfactory")) {
-         GstElementFactory *factory = gst_elementfactory_load_thyself (field);
-         gst_plugin_add_factory (plugin, factory);
-         elementcount++;
-       }
-       else if (!strcmp (field->name, "autoplugfactory")) {
-         GstAutoplugFactory *factory = gst_autoplugfactory_load_thyself (field);
-         gst_plugin_add_autoplugger (plugin, factory);
-         autoplugcount++;
-       }
-       else if (!strcmp (field->name, "typefactory")) {
-         GstTypeFactory *factory = gst_typefactory_load_thyself (field);
-         gst_plugin_add_type (plugin, factory);
-         elementcount++;
-         typecount++;
-       }
-
-       field = field->next;
-      }
+  GstPluginFeature *oldfeature;
 
-      if (plugin) {
-        _gst_plugins = g_list_prepend (_gst_plugins, plugin);
-      }
-    }
+  g_return_if_fail (plugin != NULL);
+  g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
+  g_return_if_fail (feature != NULL);
 
-    kinderen = kinderen->next;
+  oldfeature = gst_plugin_find_feature (plugin, 
+                 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
+
+  if (!oldfeature) {
+    feature->manager = plugin;
+    plugin->features = g_list_prepend (plugin->features, feature);
+    plugin->numfeatures++;
   }
-  GST_INFO (GST_CAT_PLUGIN_LOADING, "added %d registered factories, %d autopluggers and %d types",
-                 elementcount, autoplugcount, typecount);
 }
 
-
 /**
- * gst_plugin_get_factory_list:
- * @plugin: the plugin to get the factories from
+ * gst_plugin_get_feature_list:
+ * @plugin: the plugin to get the features from
  *
- * get a list of all the factories that this plugin provides
+ * get a list of all the features that this plugin provides
  *
- * Returns: a GList of factories
+ * Returns: a GList of features, use g_list_free to free the list.
  */
 GList*
-gst_plugin_get_factory_list (GstPlugin *plugin)
+gst_plugin_get_feature_list (GstPlugin *plugin)
 {
   g_return_val_if_fail (plugin != NULL, NULL);
 
-  return plugin->elements;
+  return g_list_copy (plugin->features);
 }
 
 /**
- * gst_plugin_get_type_list:
- * @plugin: the plugin to get the typefactories from
+ * gst_plugin_load:
+ * @name: name of plugin to load
  *
- * get a list of all the typefactories that this plugin provides
+ * Load the named plugin.  
  *
- * Returns: a GList of factories
+ * Returns: whether the plugin was loaded or not
  */
-GList*
-gst_plugin_get_type_list (GstPlugin *plugin)
+gboolean
+gst_plugin_load (const gchar *name)
 {
-  g_return_val_if_fail (plugin != NULL, NULL);
+  GstPlugin *plugin;
+  GError *error = NULL;
+
+  plugin = gst_registry_pool_find_plugin (name);
+  if (plugin) {
+    gboolean result = gst_plugin_load_plugin (plugin, &error);
+    if (error) {
+      GST_DEBUG (GST_CAT_PLUGIN_LOADING, "load_plugin error: %s\n",
+                error->message);
+      g_error_free (error);
+    }
+    return result;
+  }
+
+  GST_DEBUG (GST_CAT_PLUGIN_LOADING, "Could not find %s in registry pool",
+             name);
 
-  return plugin->types;
+  return FALSE;
 }
 
 /**
- * gst_plugin_get_autoplug_list:
- * @plugin: the plugin to get the autoplugfactories from
+ * gst_library_load:
+ * @name: name of library to load
  *
- * get a list of all the autoplugfactories that this plugin provides
+ * Load the named library.  Name should be given as
+ * &quot;liblibrary.so&quot;.
  *
- * Returns: a GList of factories
+ * Returns: whether the library was loaded or not
  */
-GList*
-gst_plugin_get_autoplug_list (GstPlugin *plugin)
+gboolean
+gst_library_load (const gchar *name)
 {
-  g_return_val_if_fail (plugin != NULL, NULL);
+  gboolean res;
+
+  /* for now this is the same */
+  res = gst_plugin_load (name);
 
-  return plugin->autopluggers;
+  return res;
 }