added better error reporting for plugin loading
[platform/upstream/gstreamer.git] / gst / gstplugin.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstplugin.c: Plugin subsystem for loading elements, types, and libs
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #ifdef HAVE_DIRENT_H
29 #include <dirent.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <signal.h>
35 #include <errno.h>
36
37 #include "gst_private.h"
38
39 #include "gstplugin.h"
40 #include "gstversion.h"
41 #include "gstregistrypool.h"
42 #include "gstinfo.h"
43 #include "gstfilter.h"
44
45
46 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
47
48 static GModule *main_module = NULL;
49 static GList *_gst_plugin_static = NULL;
50
51 /* static variables for segfault handling of plugin loading */
52 static char *_gst_plugin_fault_handler_filename = NULL;
53 extern gboolean *_gst_disable_segtrap;  /* see gst.c */
54
55 #ifndef HAVE_WIN32
56 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
57 #endif
58
59 /* list of valid licenses.
60  * One of these must be specified or the plugin won't be loaded 
61  * Contact gstreamer-devel@lists.sourceforge.net if your license should be 
62  * added.
63  *
64  * GPL: http://www.gnu.org/copyleft/gpl.html
65  * LGPL: http://www.gnu.org/copyleft/lesser.html
66  * QPL: http://www.trolltech.com/licenses/qpl.html
67  */
68 static gchar *valid_licenses[] = {
69   "LGPL",                       /* GNU Lesser General Public License */
70   "GPL",                        /* GNU General Public License */
71   "QPL",                        /* Trolltech Qt Public License */
72   "GPL/QPL",                    /* Combi-license of GPL + QPL */
73   GST_LICENSE_UNKNOWN,          /* some other license */
74   NULL
75 };
76
77 static void gst_plugin_desc_copy (GstPluginDesc * dest,
78     const GstPluginDesc * src);
79
80 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
81     GModule * module, GstPluginDesc * desc);
82
83 static GstPlugin *
84 gst_plugin_copy (GstPlugin * plugin)
85 {
86   return g_memdup (plugin, sizeof (*plugin));
87 }
88
89 GType
90 gst_plugin_get_type (void)
91 {
92   static GType plugin_type;
93
94   if (plugin_type == 0) {
95     plugin_type = g_boxed_type_register_static ("GstPlugin",
96         (GBoxedCopyFunc) gst_plugin_copy, g_free);
97   }
98
99   return plugin_type;
100 }
101
102 GQuark
103 gst_plugin_error_quark (void)
104 {
105   static GQuark quark = 0;
106
107   if (!quark)
108     quark = g_quark_from_static_string ("gst_plugin_error");
109   return quark;
110 }
111
112 /* this function can be called in the GCC constructor extension, before
113  * the _gst_plugin_initialize() was called. In that case, we store the 
114  * plugin description in a list to initialize it when we open the main
115  * module later on.
116  * When the main module is known, we can register the plugin right away.
117  * */
118 void
119 _gst_plugin_register_static (GstPluginDesc * desc)
120 {
121   if (main_module == NULL) {
122     if (GST_CAT_DEFAULT)
123       GST_LOG ("queueing static plugin \"%s\" for loading later on",
124           desc->name);
125     _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
126   } else {
127     GstPlugin *plugin;
128
129     if (GST_CAT_DEFAULT)
130       GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
131     plugin = g_new0 (GstPlugin, 1);
132     if (gst_plugin_register_func (plugin, main_module, desc)) {
133       if (GST_CAT_DEFAULT)
134         GST_INFO ("loaded static plugin \"%s\"", desc->name);
135       gst_registry_pool_add_plugin (plugin);
136     }
137   }
138 }
139
140 void
141 _gst_plugin_initialize (void)
142 {
143   main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
144
145   /* now register all static plugins */
146   g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
147       NULL);
148 }
149
150 /* this function could be extended to check if the plugin license matches the 
151  * applications license (would require the app to register its license somehow).
152  * We'll wait for someone who's interested in it to code it :)
153  */
154 static gboolean
155 gst_plugin_check_license (const gchar * license)
156 {
157   gchar **check_license = valid_licenses;
158
159   g_assert (check_license);
160
161   while (*check_license) {
162     if (strcmp (license, *check_license) == 0)
163       return TRUE;
164     check_license++;
165   }
166   return FALSE;
167 }
168
169 static gboolean
170 gst_plugin_check_version (gint major, gint minor)
171 {
172   /* return NULL if the major and minor version numbers are not compatible */
173   /* with ours. */
174   if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
175     return FALSE;
176
177   return TRUE;
178 }
179
180 static GstPlugin *
181 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
182     GstPluginDesc * desc)
183 {
184   g_assert (plugin->module == NULL);
185
186   if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
187     if (GST_CAT_DEFAULT)
188       GST_INFO ("plugin \"%s\" has incompatible version, not loading",
189           plugin->filename);
190     return FALSE;
191   }
192
193   if (!desc->license || !desc->description || !desc->package || !desc->origin) {
194     if (GST_CAT_DEFAULT)
195       GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
196           plugin->filename);
197     return FALSE;
198   }
199
200   if (!gst_plugin_check_license (desc->license)) {
201     if (GST_CAT_DEFAULT)
202       GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
203           plugin->filename, desc->license);
204     return FALSE;
205   }
206
207   gst_plugin_desc_copy (&plugin->desc, desc);
208   plugin->module = module;
209
210   if (!((desc->plugin_init) (plugin))) {
211     if (GST_CAT_DEFAULT)
212       GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
213     plugin->module = NULL;
214     return FALSE;
215   }
216
217   if (GST_CAT_DEFAULT)
218     GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
219
220   return plugin;
221 }
222
223 #ifndef HAVE_WIN32
224 /*
225  * _gst_plugin_fault_handler_restore:
226  * segfault handler restorer
227  */
228 static void
229 _gst_plugin_fault_handler_restore (void)
230 {
231   struct sigaction action;
232
233   memset (&action, 0, sizeof (action));
234   action.sa_handler = SIG_DFL;
235
236   sigaction (SIGSEGV, &action, NULL);
237 }
238
239 /*
240  * _gst_plugin_fault_handler_sighandler:
241  * segfault handler implementation
242  */
243 static void
244 _gst_plugin_fault_handler_sighandler (int signum)
245 {
246   /* We need to restore the fault handler or we'll keep getting it */
247   _gst_plugin_fault_handler_restore ();
248
249   switch (signum) {
250     case SIGSEGV:
251       g_print ("\nERROR: ");
252       g_print ("Caught a segmentation fault while loading plugin file:\n");
253       g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
254       g_print ("Please either:\n");
255       g_print ("- remove it and restart.\n");
256       g_print ("- run with --gst-disable-segtrap and debug.\n");
257       exit (-1);
258       break;
259     default:
260       g_print ("Caught unhandled signal on plugin loading\n");
261       break;
262   }
263 }
264
265 /*
266  * _gst_plugin_fault_handler_setup:
267  * sets up the segfault handler
268  */
269 static void
270 _gst_plugin_fault_handler_setup (void)
271 {
272   struct sigaction action;
273
274   /* if asked to leave segfaults alone, just return */
275   if (_gst_disable_segtrap)
276     return;
277
278   if (_gst_plugin_fault_handler_is_setup)
279     return;
280
281   memset (&action, 0, sizeof (action));
282   action.sa_handler = _gst_plugin_fault_handler_sighandler;
283
284   sigaction (SIGSEGV, &action, NULL);
285 }
286 #else
287 static void
288 _gst_plugin_fault_handler_restore (void)
289 {
290 }
291
292 static void
293 _gst_plugin_fault_handler_setup (void)
294 {
295 }
296 #endif
297
298 static void _gst_plugin_fault_handler_setup ();
299
300 /**
301  * gst_plugin_check_file:
302  * @filename: the plugin filename to check for pluginness
303  * @error: pointer to a NULL-valued GError
304  *
305  * Checks if the given path represents a GStreamer plugin.
306  *
307  * Returns: TRUE if the given path is a GStreamer plugin.
308  */
309 gboolean
310 gst_plugin_check_file (const gchar * filename, GError ** error)
311 {
312   GModule *module;
313   struct stat file_status;
314   gpointer ptr;
315
316   g_return_val_if_fail (filename != NULL, FALSE);
317
318   if (g_module_supported () == FALSE) {
319     g_set_error (error,
320         GST_PLUGIN_ERROR,
321         GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
322     return FALSE;
323   }
324
325   if (stat (filename, &file_status)) {
326     g_set_error (error,
327         GST_PLUGIN_ERROR,
328         GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
329         strerror (errno));
330     return FALSE;
331   }
332
333   module = g_module_open (filename, G_MODULE_BIND_LAZY);
334
335   if (module == NULL) {
336     GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename,
337         g_module_error ());
338     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
339         "Error loading plugin %s, reason: %s\n", filename, g_module_error ());
340     return FALSE;
341   }
342
343   if (!g_module_symbol (module, "gst_plugin_desc", &ptr)) {
344     GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
345     g_set_error (error,
346         GST_PLUGIN_ERROR,
347         GST_PLUGIN_ERROR_MODULE,
348         "Could not find plugin entry point in \"%s\"", filename);
349     g_module_close (module);
350     return FALSE;
351   }
352   /* it's a plugin */
353   GST_INFO ("looks like a gst plugin \"%s\"", filename);
354   g_module_close (module);
355   return TRUE;
356 }
357
358 /**
359  * gst_plugin_load_file:
360  * @filename: the plugin filename to load
361  * @error: pointer to a NULL-valued GError
362  *
363  * Loads the given plugin.
364  *
365  * Returns: a new GstPlugin or NULL, if an error occurred.
366  */
367 GstPlugin *
368 gst_plugin_load_file (const gchar * filename, GError ** error)
369 {
370   GstPlugin *plugin;
371   GModule *module;
372   GstPluginDesc *desc;
373   gboolean free_plugin;
374   gpointer ptr;
375
376   g_return_val_if_fail (filename != NULL, NULL);
377
378   GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
379       filename);
380
381   if (!gst_plugin_check_file (filename, error))
382     return NULL;
383
384   module = g_module_open (filename, G_MODULE_BIND_LAZY);
385
386   if (module == NULL)
387     goto load_error;
388
389   if (!g_module_symbol (module, "gst_plugin_desc", &ptr))
390     goto load_error;
391
392   desc = (GstPluginDesc *) ptr;
393
394   plugin = gst_registry_pool_find_plugin (desc->name);
395   if (!plugin) {
396     free_plugin = TRUE;
397     plugin = g_new0 (GstPlugin, 1);
398     plugin->filename = g_strdup (filename);
399     GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin, filename);
400   } else {
401     free_plugin = FALSE;
402     if (gst_plugin_is_loaded (plugin)) {
403       if (plugin->filename && strcmp (plugin->filename, filename) != 0) {
404         GST_WARNING
405             ("plugin %p from file \"%s\" with same name %s is already "
406             "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
407             plugin->desc.name, filename);
408         g_set_error (error, GST_PLUGIN_ERROR,
409             GST_PLUGIN_ERROR_NAME_MISMATCH,
410             "plugin %p from file \"%s\" with same name %s is already "
411             "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
412             plugin->desc.name, filename);
413         if (free_plugin)
414           g_free (plugin);
415         return NULL;
416       }
417       GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
418           plugin, filename);
419       return plugin;
420     }
421   }
422   GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
423       plugin, filename);
424
425   if (g_module_symbol (module, "plugin_init", &ptr)) {
426     g_print
427         ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
428         plugin, plugin->filename);
429     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
430         "plugin \"%s\" exports a symbol named plugin_init", desc->name);
431   }
432
433   /* this is where we load the actual .so, so let's trap SIGSEGV */
434   _gst_plugin_fault_handler_setup ();
435   _gst_plugin_fault_handler_filename = plugin->filename;
436
437   if (gst_plugin_register_func (plugin, module, desc)) {
438     /* remove signal handler */
439     _gst_plugin_fault_handler_restore ();
440     _gst_plugin_fault_handler_filename = NULL;
441     GST_INFO ("plugin \"%s\" loaded", plugin->filename);
442     return plugin;
443   } else {
444     /* remove signal handler */
445     _gst_plugin_fault_handler_restore ();
446     GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
447     /* plugin == NULL */
448     g_set_error (error,
449         GST_PLUGIN_ERROR,
450         GST_PLUGIN_ERROR_MODULE,
451         "gst_plugin_register_func failed for plugin \"%s\"", filename);
452     if (free_plugin)
453       g_free (plugin);
454     return NULL;
455   }
456 load_error:
457   g_set_error (error,
458       GST_PLUGIN_ERROR,
459       GST_PLUGIN_ERROR_MODULE, "generic load error for \"%s\"", filename);
460   return NULL;
461 }
462
463 static void
464 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
465 {
466   dest->major_version = src->major_version;
467   dest->minor_version = src->minor_version;
468   g_free (dest->name);
469   dest->name = g_strdup (src->name);
470   g_free (dest->description);
471   dest->description = g_strdup (src->description);
472   dest->plugin_init = src->plugin_init;
473   dest->plugin_exit = src->plugin_exit;
474   g_free (dest->version);
475   dest->version = g_strdup (src->version);
476   g_free (dest->license);
477   dest->license = g_strdup (src->license);
478   g_free (dest->package);
479   dest->package = g_strdup (src->package);
480   g_free (dest->origin);
481   dest->origin = g_strdup (src->origin);
482 }
483
484 #if 0
485 /* unused */
486 static void
487 gst_plugin_desc_free (GstPluginDesc * desc)
488 {
489   g_free (desc->name);
490   g_free (desc->description);
491   g_free (desc->version);
492   g_free (desc->license);
493   g_free (desc->package);
494   g_free (desc->origin);
495
496   memset (desc, 0, sizeof (GstPluginDesc));
497 }
498 #endif
499 /**
500  * gst_plugin_unload_plugin:
501  * @plugin: The plugin to unload
502  *
503  * Unload the given plugin.
504  *
505  * Returns: whether or not the plugin unloaded
506  */
507 gboolean
508 gst_plugin_unload_plugin (GstPlugin * plugin)
509 {
510   g_return_val_if_fail (plugin != NULL, FALSE);
511
512   if (!plugin->module)
513     return TRUE;
514
515   if (g_module_close (plugin->module)) {
516     plugin->module = NULL;
517     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
518         plugin->filename);
519     return TRUE;
520   } else {
521     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
522         plugin->filename);
523     return FALSE;
524   }
525 }
526
527 /**
528  * gst_plugin_get_name:
529  * @plugin: plugin to get the name of
530  *
531  * Get the short name of the plugin
532  *
533  * Returns: the name of the plugin
534  */
535 const gchar *
536 gst_plugin_get_name (GstPlugin * plugin)
537 {
538   g_return_val_if_fail (plugin != NULL, NULL);
539
540   return plugin->desc.name;
541 }
542
543 /**
544  * gst_plugin_get_description:
545  * @plugin: plugin to get long name of
546  *
547  * Get the long descriptive name of the plugin
548  *
549  * Returns: the long name of the plugin
550  */
551 G_CONST_RETURN gchar *
552 gst_plugin_get_description (GstPlugin * plugin)
553 {
554   g_return_val_if_fail (plugin != NULL, NULL);
555
556   return plugin->desc.description;
557 }
558
559 /**
560  * gst_plugin_get_filename:
561  * @plugin: plugin to get the filename of
562  *
563  * get the filename of the plugin
564  *
565  * Returns: the filename of the plugin
566  */
567 G_CONST_RETURN gchar *
568 gst_plugin_get_filename (GstPlugin * plugin)
569 {
570   g_return_val_if_fail (plugin != NULL, NULL);
571
572   return plugin->filename;
573 }
574
575 /**
576  * gst_plugin_get_license:
577  * @plugin: plugin to get the license of
578  *
579  * get the license of the plugin
580  *
581  * Returns: the license of the plugin
582  */
583 G_CONST_RETURN gchar *
584 gst_plugin_get_license (GstPlugin * plugin)
585 {
586   g_return_val_if_fail (plugin != NULL, NULL);
587
588   return plugin->desc.license;
589 }
590
591 /**
592  * gst_plugin_get_package:
593  * @plugin: plugin to get the package of
594  *
595  * get the package the plugin belongs to.
596  *
597  * Returns: the package of the plugin
598  */
599 G_CONST_RETURN gchar *
600 gst_plugin_get_package (GstPlugin * plugin)
601 {
602   g_return_val_if_fail (plugin != NULL, NULL);
603
604   return plugin->desc.package;
605 }
606
607 /**
608  * gst_plugin_get_origin:
609  * @plugin: plugin to get the origin of
610  *
611  * get the URL where the plugin comes from
612  *
613  * Returns: the origin of the plugin
614  */
615 G_CONST_RETURN gchar *
616 gst_plugin_get_origin (GstPlugin * plugin)
617 {
618   g_return_val_if_fail (plugin != NULL, NULL);
619
620   return plugin->desc.origin;
621 }
622
623 /**
624  * gst_plugin_get_module:
625  * @plugin: plugin to query
626  *
627  * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is 
628  * returned.
629  *
630  * Returns: module belonging to the plugin or NULL if the plugin isn't 
631  *          loaded yet.
632  */
633 GModule *
634 gst_plugin_get_module (GstPlugin * plugin)
635 {
636   g_return_val_if_fail (plugin != NULL, FALSE);
637
638   return plugin->module;
639 }
640
641 /**
642  * gst_plugin_is_loaded:
643  * @plugin: plugin to query
644  *
645  * queries if the plugin is loaded into memory
646  *
647  * Returns: TRUE is loaded, FALSE otherwise
648  */
649 gboolean
650 gst_plugin_is_loaded (GstPlugin * plugin)
651 {
652   g_return_val_if_fail (plugin != NULL, FALSE);
653
654   return (plugin->module != NULL);
655 }
656
657 /**
658  * gst_plugin_feature_list:
659  * @plugin: plugin to query
660  * @filter: the filter to use
661  * @first: only return first match
662  * @user_data: user data passed to the filter function
663  *
664  * Runs a filter against all plugin features and returns a GList with
665  * the results. If the first flag is set, only the first match is 
666  * returned (as a list with a single object).
667  *
668  * Returns: a GList of features, g_list_free after use.
669  */
670 GList *
671 gst_plugin_feature_filter (GstPlugin * plugin,
672     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
673 {
674   return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
675       user_data);
676 }
677
678 typedef struct
679 {
680   GstPluginFeatureFilter filter;
681   gboolean first;
682   gpointer user_data;
683   GList *result;
684 }
685 FeatureFilterData;
686
687 static gboolean
688 _feature_filter (GstPlugin * plugin, gpointer user_data)
689 {
690   GList *result;
691   FeatureFilterData *data = (FeatureFilterData *) user_data;
692
693   result =
694       gst_plugin_feature_filter (plugin, data->filter, data->first,
695       data->user_data);
696   if (result) {
697     data->result = g_list_concat (data->result, result);
698     return TRUE;
699   }
700   return FALSE;
701 }
702
703 /**
704  * gst_plugin_list_feature_list:
705  * @list: a list of plugins to query
706  * @filter: the filter to use
707  * @first: only return first match
708  * @user_data: user data passed to the filter function
709  *
710  * Runs a filter against all plugin features of the plugins in the given
711  * list and returns a GList with the results. 
712  * If the first flag is set, only the first match is 
713  * returned (as a list with a single object).
714  *
715  * Returns: a GList of features, g_list_free after use.
716  */
717 GList *
718 gst_plugin_list_feature_filter (GList * list,
719     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
720 {
721   FeatureFilterData data;
722   GList *result;
723
724   data.filter = filter;
725   data.first = first;
726   data.user_data = user_data;
727   data.result = NULL;
728
729   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
730   g_list_free (result);
731
732   return data.result;
733 }
734
735 /**
736  * gst_plugin_name_filter:
737  * @plugin: the plugin to check
738  * @name: the name of the plugin
739  *
740  * A standard filter that returns TRUE when the plugin is of the
741  * given name.
742  *
743  * Returns: TRUE if the plugin is of the given name.
744  */
745 gboolean
746 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
747 {
748   return (plugin->desc.name && !strcmp (plugin->desc.name, name));
749 }
750
751 /**
752  * gst_plugin_find_feature:
753  * @plugin: plugin to get the feature from
754  * @name: The name of the feature to find
755  * @type: The type of the feature to find
756  *
757  * Find a feature of the given name and type in the given plugin.
758  *
759  * Returns: a GstPluginFeature or NULL if the feature was not found.
760  */
761 GstPluginFeature *
762 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
763 {
764   GList *walk;
765   GstPluginFeature *result = NULL;
766   GstTypeNameData data;
767
768   g_return_val_if_fail (name != NULL, NULL);
769
770   data.type = type;
771   data.name = name;
772
773   walk = gst_filter_run (plugin->features,
774       (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
775
776   if (walk)
777     result = GST_PLUGIN_FEATURE (walk->data);
778
779   return result;
780 }
781
782 /**
783  * gst_plugin_add_feature:
784  * @plugin: plugin to add feature to
785  * @feature: feature to add
786  *
787  * Add feature to the list of those provided by the plugin.
788  * There is a separate namespace for each plugin feature type.
789  * See #gst_plugin_get_feature_list
790  */
791 void
792 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
793 {
794   GstPluginFeature *oldfeature;
795
796   /* FIXME 0.9: get reference counting somewhat right in here,
797    * GstPluginFeatures should probably be GstObjects that are sinked when
798    * adding them to a plugin */
799   g_return_if_fail (plugin != NULL);
800   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
801   g_return_if_fail (feature != NULL);
802
803   oldfeature = gst_plugin_find_feature (plugin,
804       GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
805
806   if (oldfeature == feature) {
807     GST_WARNING ("feature %s has already been added",
808         GST_PLUGIN_FEATURE_NAME (feature));
809     /* g_object_unref (feature); */
810   } else if (oldfeature) {
811     GST_WARNING ("feature %s already present in plugin",
812         GST_PLUGIN_FEATURE_NAME (feature));
813     /* g_object_unref (feature); */
814   } else {
815     feature->manager = plugin;
816     plugin->features = g_list_prepend (plugin->features, feature);
817     plugin->numfeatures++;
818   }
819 }
820
821 /**
822  * gst_plugin_get_feature_list:
823  * @plugin: the plugin to get the features from
824  *
825  * get a list of all the features that this plugin provides
826  *
827  * Returns: a GList of features, use g_list_free to free the list.
828  */
829 GList *
830 gst_plugin_get_feature_list (GstPlugin * plugin)
831 {
832   g_return_val_if_fail (plugin != NULL, NULL);
833
834   return g_list_copy (plugin->features);
835 }
836
837 /**
838  * gst_plugin_load:
839  * @name: name of plugin to load
840  *
841  * Load the named plugin.  
842  *
843  * Returns: whether the plugin was loaded or not
844  */
845 gboolean
846 gst_plugin_load (const gchar * name)
847 {
848   GstPlugin *plugin;
849   GError *error = NULL;
850
851   plugin = gst_registry_pool_find_plugin (name);
852   if (plugin) {
853     gst_plugin_load_file (plugin->filename, &error);
854     if (error) {
855       GST_WARNING ("load_plugin error: %s\n", error->message);
856       g_error_free (error);
857       return FALSE;
858     }
859     return TRUE;;
860   }
861
862   GST_DEBUG ("Could not find %s in registry pool", name);
863   return FALSE;
864 }
865
866 /**
867  * gst_library_load:
868  * @name: name of library to load
869  *
870  * Load the named library.  Name should be given as
871  * &quot;liblibrary.so&quot;.
872  *
873  * Returns: whether the library was loaded or not
874  */
875 gboolean
876 gst_library_load (const gchar * name)
877 {
878   gboolean res;
879
880   /* for now this is the same */
881   res = gst_plugin_load (name);
882
883   return res;
884 }