gst/gstplugin.c: apply patch from #172526 to make register work on MacOSX
[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 NULL;
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 NULL;
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 NULL;
205   }
206
207   if (GST_CAT_DEFAULT)
208     GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
209
210   gst_plugin_desc_copy (&plugin->desc, desc);
211   plugin->module = module;
212
213   if (!((desc->plugin_init) (plugin))) {
214     if (GST_CAT_DEFAULT)
215       GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
216     plugin->module = NULL;
217     return NULL;
218   }
219
220   if (GST_CAT_DEFAULT)
221     GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
222
223   return plugin;
224 }
225
226 #ifndef HAVE_WIN32
227 /*
228  * _gst_plugin_fault_handler_restore:
229  * segfault handler restorer
230  */
231 static void
232 _gst_plugin_fault_handler_restore (void)
233 {
234   struct sigaction action;
235
236   memset (&action, 0, sizeof (action));
237   action.sa_handler = SIG_DFL;
238
239   sigaction (SIGSEGV, &action, NULL);
240 }
241
242 /*
243  * _gst_plugin_fault_handler_sighandler:
244  * segfault handler implementation
245  */
246 static void
247 _gst_plugin_fault_handler_sighandler (int signum)
248 {
249   /* We need to restore the fault handler or we'll keep getting it */
250   _gst_plugin_fault_handler_restore ();
251
252   switch (signum) {
253     case SIGSEGV:
254       g_print ("\nERROR: ");
255       g_print ("Caught a segmentation fault while loading plugin file:\n");
256       g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
257       g_print ("Please either:\n");
258       g_print ("- remove it and restart.\n");
259       g_print ("- run with --gst-disable-segtrap and debug.\n");
260       exit (-1);
261       break;
262     default:
263       g_print ("Caught unhandled signal on plugin loading\n");
264       break;
265   }
266 }
267
268 /*
269  * _gst_plugin_fault_handler_setup:
270  * sets up the segfault handler
271  */
272 static void
273 _gst_plugin_fault_handler_setup (void)
274 {
275   struct sigaction action;
276
277   /* if asked to leave segfaults alone, just return */
278   if (_gst_disable_segtrap)
279     return;
280
281   if (_gst_plugin_fault_handler_is_setup)
282     return;
283
284   memset (&action, 0, sizeof (action));
285   action.sa_handler = _gst_plugin_fault_handler_sighandler;
286
287   sigaction (SIGSEGV, &action, NULL);
288 }
289 #else
290 static void
291 _gst_plugin_fault_handler_restore (void)
292 {
293 }
294
295 static void
296 _gst_plugin_fault_handler_setup (void)
297 {
298 }
299 #endif
300
301 static void _gst_plugin_fault_handler_setup ();
302
303 /**
304  * gst_plugin_check_module:
305  * @module: GModule handle to check for pluginness
306  * @error: pointer to a NULL-valued GError
307  * @pptr: pointer to a gpointer used to return the gst_plugin_desc symbol
308  *        (can be NULL)
309  *
310  * Checks if the given module is a GStreamer plugin
311  *
312  * Returns: TRUE if the given module is a GStreamer plugin
313  */
314 static gboolean
315 gst_plugin_check_module (GModule * module, const char *filename,
316     GError ** error, gpointer * pptr)
317 {
318   gpointer ptr;
319
320   if (pptr == NULL)
321     pptr = &ptr;
322
323   if (module == NULL) {
324     GST_DEBUG ("Error loading plugin %s, reason: %s", filename,
325         g_module_error ());
326     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
327         "Error loading plugin %s, reason: %s", filename, g_module_error ());
328     return FALSE;
329   }
330
331   if (!g_module_symbol (module, "gst_plugin_desc", pptr)) {
332     GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
333     g_set_error (error,
334         GST_PLUGIN_ERROR,
335         GST_PLUGIN_ERROR_MODULE,
336         "Could not find plugin entry point in \"%s\"", filename);
337     return FALSE;
338   }
339
340   return TRUE;
341 }
342
343 /**
344  * gst_plugin_check_file:
345  * @filename: the plugin filename to check for pluginness
346  * @error: pointer to a NULL-valued GError
347  *
348  * Checks if the given path represents a GStreamer plugin.
349  *
350  * Returns: TRUE if the given path is a GStreamer plugin.
351  */
352 gboolean
353 gst_plugin_check_file (const gchar * filename, GError ** error)
354 {
355   GModule *module;
356   struct stat file_status;
357   gboolean check;
358
359   g_return_val_if_fail (filename != NULL, FALSE);
360
361   if (g_module_supported () == FALSE) {
362     g_set_error (error,
363         GST_PLUGIN_ERROR,
364         GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
365     return FALSE;
366   }
367
368   if (stat (filename, &file_status)) {
369     g_set_error (error,
370         GST_PLUGIN_ERROR,
371         GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
372         strerror (errno));
373     return FALSE;
374   }
375
376   module = g_module_open (filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
377   check = gst_plugin_check_module (module, filename, error, NULL);
378   g_module_close (module);
379
380   GST_INFO ("file \"%s\" %s look like a gst plugin", filename,
381       check ? "does" : "doesn't");
382   return check;
383
384 }
385
386 /**
387  * gst_plugin_load_file:
388  * @filename: the plugin filename to load
389  * @error: pointer to a NULL-valued GError
390  *
391  * Loads the given plugin.
392  *
393  * Returns: a new GstPlugin or NULL, if an error occurred.
394  */
395 GstPlugin *
396 gst_plugin_load_file (const gchar * filename, GError ** error)
397 {
398   GstPlugin *plugin;
399   GModule *module;
400   GstPluginDesc *desc;
401   gboolean free_plugin;
402   gpointer ptr;
403
404   g_return_val_if_fail (filename != NULL, NULL);
405
406   GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
407       filename);
408
409   module = g_module_open (filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
410
411   /* handle module == NULL case */
412   if (!gst_plugin_check_module (module, filename, error, &ptr))
413     return NULL;
414
415   desc = (GstPluginDesc *) ptr;
416
417   plugin = gst_registry_pool_find_plugin (desc->name);
418   if (!plugin) {
419     free_plugin = TRUE;
420     plugin = g_new0 (GstPlugin, 1);
421     plugin->filename = g_strdup (filename);
422     GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin, filename);
423   } else {
424     free_plugin = FALSE;
425     if (gst_plugin_is_loaded (plugin)) {
426       g_module_close (module);
427       if (plugin->filename && strcmp (plugin->filename, filename) != 0) {
428         GST_WARNING
429             ("plugin %p from file \"%s\" with same name %s is already "
430             "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
431             plugin->desc.name, filename);
432         g_set_error (error, GST_PLUGIN_ERROR,
433             GST_PLUGIN_ERROR_NAME_MISMATCH,
434             "plugin %p from file \"%s\" with same name %s is already "
435             "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
436             plugin->desc.name, filename);
437         if (free_plugin)
438           g_free (plugin);
439         return NULL;
440       }
441       GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
442           plugin, filename);
443       return plugin;
444     }
445   }
446   GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
447       plugin, filename);
448
449   if (g_module_symbol (module, "plugin_init", &ptr)) {
450     GST_WARNING
451         ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
452         plugin, plugin->filename);
453     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
454         "plugin \"%s\" exports a symbol named plugin_init", desc->name);
455   }
456
457   /* this is where we load the actual .so, so let's trap SIGSEGV */
458   _gst_plugin_fault_handler_setup ();
459   _gst_plugin_fault_handler_filename = plugin->filename;
460
461   GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
462       plugin, filename);
463
464   if (gst_plugin_register_func (plugin, module, desc)) {
465     /* remove signal handler */
466     _gst_plugin_fault_handler_restore ();
467     _gst_plugin_fault_handler_filename = NULL;
468     GST_INFO ("plugin \"%s\" loaded", plugin->filename);
469     return plugin;
470   } else {
471     /* remove signal handler */
472     _gst_plugin_fault_handler_restore ();
473     GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
474     /* plugin == NULL */
475     g_set_error (error,
476         GST_PLUGIN_ERROR,
477         GST_PLUGIN_ERROR_MODULE,
478         "gst_plugin_register_func failed for plugin \"%s\"", filename);
479     g_module_close (module);
480     if (free_plugin)
481       g_free (plugin);
482     return NULL;
483   }
484 }
485
486 static void
487 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
488 {
489   dest->major_version = src->major_version;
490   dest->minor_version = src->minor_version;
491   g_free (dest->name);
492   dest->name = g_strdup (src->name);
493   g_free (dest->description);
494   dest->description = g_strdup (src->description);
495   dest->plugin_init = src->plugin_init;
496   dest->plugin_exit = src->plugin_exit;
497   g_free (dest->version);
498   dest->version = g_strdup (src->version);
499   g_free (dest->license);
500   dest->license = g_strdup (src->license);
501   g_free (dest->package);
502   dest->package = g_strdup (src->package);
503   g_free (dest->origin);
504   dest->origin = g_strdup (src->origin);
505 }
506
507 #if 0
508 /* unused */
509 static void
510 gst_plugin_desc_free (GstPluginDesc * desc)
511 {
512   g_free (desc->name);
513   g_free (desc->description);
514   g_free (desc->version);
515   g_free (desc->license);
516   g_free (desc->package);
517   g_free (desc->origin);
518
519   memset (desc, 0, sizeof (GstPluginDesc));
520 }
521 #endif
522 /**
523  * gst_plugin_unload_plugin:
524  * @plugin: The plugin to unload
525  *
526  * Unload the given plugin.
527  *
528  * Returns: whether or not the plugin unloaded
529  */
530 gboolean
531 gst_plugin_unload_plugin (GstPlugin * plugin)
532 {
533   g_return_val_if_fail (plugin != NULL, FALSE);
534
535   if (!plugin->module)
536     return TRUE;
537
538   if (g_module_close (plugin->module)) {
539     plugin->module = NULL;
540     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
541         plugin->filename);
542     return TRUE;
543   } else {
544     GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
545         plugin->filename);
546     return FALSE;
547   }
548 }
549
550 /**
551  * gst_plugin_get_name:
552  * @plugin: plugin to get the name of
553  *
554  * Get the short name of the plugin
555  *
556  * Returns: the name of the plugin
557  */
558 const gchar *
559 gst_plugin_get_name (GstPlugin * plugin)
560 {
561   g_return_val_if_fail (plugin != NULL, NULL);
562
563   return plugin->desc.name;
564 }
565
566 /**
567  * gst_plugin_get_description:
568  * @plugin: plugin to get long name of
569  *
570  * Get the long descriptive name of the plugin
571  *
572  * Returns: the long name of the plugin
573  */
574 G_CONST_RETURN gchar *
575 gst_plugin_get_description (GstPlugin * plugin)
576 {
577   g_return_val_if_fail (plugin != NULL, NULL);
578
579   return plugin->desc.description;
580 }
581
582 /**
583  * gst_plugin_get_filename:
584  * @plugin: plugin to get the filename of
585  *
586  * get the filename of the plugin
587  *
588  * Returns: the filename of the plugin
589  */
590 G_CONST_RETURN gchar *
591 gst_plugin_get_filename (GstPlugin * plugin)
592 {
593   g_return_val_if_fail (plugin != NULL, NULL);
594
595   return plugin->filename;
596 }
597
598 /**
599  * gst_plugin_get_version:
600  * @plugin: plugin to get the version of
601  *
602  * get the version of the plugin
603  *
604  * Returns: the version of the plugin
605  */
606 G_CONST_RETURN gchar *
607 gst_plugin_get_version (GstPlugin * plugin)
608 {
609   g_return_val_if_fail (plugin != NULL, NULL);
610
611   return plugin->desc.version;
612 }
613
614 /**
615  * gst_plugin_get_license:
616  * @plugin: plugin to get the license of
617  *
618  * get the license of the plugin
619  *
620  * Returns: the license of the plugin
621  */
622 G_CONST_RETURN gchar *
623 gst_plugin_get_license (GstPlugin * plugin)
624 {
625   g_return_val_if_fail (plugin != NULL, NULL);
626
627   return plugin->desc.license;
628 }
629
630 /**
631  * gst_plugin_get_package:
632  * @plugin: plugin to get the package of
633  *
634  * get the package the plugin belongs to.
635  *
636  * Returns: the package of the plugin
637  */
638 G_CONST_RETURN gchar *
639 gst_plugin_get_package (GstPlugin * plugin)
640 {
641   g_return_val_if_fail (plugin != NULL, NULL);
642
643   return plugin->desc.package;
644 }
645
646 /**
647  * gst_plugin_get_origin:
648  * @plugin: plugin to get the origin of
649  *
650  * get the URL where the plugin comes from
651  *
652  * Returns: the origin of the plugin
653  */
654 G_CONST_RETURN gchar *
655 gst_plugin_get_origin (GstPlugin * plugin)
656 {
657   g_return_val_if_fail (plugin != NULL, NULL);
658
659   return plugin->desc.origin;
660 }
661
662 /**
663  * gst_plugin_get_module:
664  * @plugin: plugin to query
665  *
666  * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is 
667  * returned.
668  *
669  * Returns: module belonging to the plugin or NULL if the plugin isn't 
670  *          loaded yet.
671  */
672 GModule *
673 gst_plugin_get_module (GstPlugin * plugin)
674 {
675   g_return_val_if_fail (plugin != NULL, NULL);
676
677   return plugin->module;
678 }
679
680 /**
681  * gst_plugin_is_loaded:
682  * @plugin: plugin to query
683  *
684  * queries if the plugin is loaded into memory
685  *
686  * Returns: TRUE is loaded, FALSE otherwise
687  */
688 gboolean
689 gst_plugin_is_loaded (GstPlugin * plugin)
690 {
691   g_return_val_if_fail (plugin != NULL, FALSE);
692
693   return (plugin->module != NULL);
694 }
695
696 /**
697  * gst_plugin_feature_list:
698  * @plugin: plugin to query
699  * @filter: the filter to use
700  * @first: only return first match
701  * @user_data: user data passed to the filter function
702  *
703  * Runs a filter against all plugin features and returns a GList with
704  * the results. If the first flag is set, only the first match is 
705  * returned (as a list with a single object).
706  *
707  * Returns: a GList of features, g_list_free after use.
708  */
709 GList *
710 gst_plugin_feature_filter (GstPlugin * plugin,
711     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
712 {
713   return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
714       user_data);
715 }
716
717 typedef struct
718 {
719   GstPluginFeatureFilter filter;
720   gboolean first;
721   gpointer user_data;
722   GList *result;
723 }
724 FeatureFilterData;
725
726 static gboolean
727 _feature_filter (GstPlugin * plugin, gpointer user_data)
728 {
729   GList *result;
730   FeatureFilterData *data = (FeatureFilterData *) user_data;
731
732   result =
733       gst_plugin_feature_filter (plugin, data->filter, data->first,
734       data->user_data);
735   if (result) {
736     data->result = g_list_concat (data->result, result);
737     return TRUE;
738   }
739   return FALSE;
740 }
741
742 /**
743  * gst_plugin_list_feature_list:
744  * @list: a list of plugins to query
745  * @filter: the filter to use
746  * @first: only return first match
747  * @user_data: user data passed to the filter function
748  *
749  * Runs a filter against all plugin features of the plugins in the given
750  * list and returns a GList with the results. 
751  * If the first flag is set, only the first match is 
752  * returned (as a list with a single object).
753  *
754  * Returns: a GList of features, g_list_free after use.
755  */
756 GList *
757 gst_plugin_list_feature_filter (GList * list,
758     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
759 {
760   FeatureFilterData data;
761   GList *result;
762
763   data.filter = filter;
764   data.first = first;
765   data.user_data = user_data;
766   data.result = NULL;
767
768   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
769   g_list_free (result);
770
771   return data.result;
772 }
773
774 /**
775  * gst_plugin_name_filter:
776  * @plugin: the plugin to check
777  * @name: the name of the plugin
778  *
779  * A standard filter that returns TRUE when the plugin is of the
780  * given name.
781  *
782  * Returns: TRUE if the plugin is of the given name.
783  */
784 gboolean
785 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
786 {
787   return (plugin->desc.name && !strcmp (plugin->desc.name, name));
788 }
789
790 /**
791  * gst_plugin_find_feature:
792  * @plugin: plugin to get the feature from
793  * @name: The name of the feature to find
794  * @type: The type of the feature to find
795  *
796  * Find a feature of the given name and type in the given plugin.
797  *
798  * Returns: a GstPluginFeature or NULL if the feature was not found.
799  */
800 GstPluginFeature *
801 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
802 {
803   GList *walk;
804   GstPluginFeature *result = NULL;
805   GstTypeNameData data;
806
807   g_return_val_if_fail (name != NULL, NULL);
808
809   data.type = type;
810   data.name = name;
811
812   walk = gst_filter_run (plugin->features,
813       (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
814
815   if (walk)
816     result = GST_PLUGIN_FEATURE (walk->data);
817
818   return result;
819 }
820
821 /**
822  * gst_plugin_add_feature:
823  * @plugin: plugin to add feature to
824  * @feature: feature to add
825  *
826  * Add feature to the list of those provided by the plugin.
827  * There is a separate namespace for each plugin feature type.
828  * See #gst_plugin_get_feature_list
829  */
830 void
831 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
832 {
833   GstPluginFeature *oldfeature;
834
835   /* FIXME 0.9: get reference counting somewhat right in here,
836    * GstPluginFeatures should probably be GstObjects that are sinked when
837    * adding them to a plugin */
838   g_return_if_fail (plugin != NULL);
839   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
840   g_return_if_fail (feature != NULL);
841
842   oldfeature = gst_plugin_find_feature (plugin,
843       GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
844
845   if (oldfeature == feature) {
846     GST_WARNING ("feature %s has already been added",
847         GST_PLUGIN_FEATURE_NAME (feature));
848     /* g_object_unref (feature); */
849   } else if (oldfeature) {
850     GST_WARNING ("feature %s already present in plugin",
851         GST_PLUGIN_FEATURE_NAME (feature));
852     /* g_object_unref (feature); */
853   } else {
854     feature->manager = plugin;
855     plugin->features = g_list_prepend (plugin->features, feature);
856     plugin->numfeatures++;
857   }
858 }
859
860 /**
861  * gst_plugin_get_feature_list:
862  * @plugin: the plugin to get the features from
863  *
864  * get a list of all the features that this plugin provides
865  *
866  * Returns: a GList of features, use g_list_free to free the list.
867  */
868 GList *
869 gst_plugin_get_feature_list (GstPlugin * plugin)
870 {
871   g_return_val_if_fail (plugin != NULL, NULL);
872
873   return g_list_copy (plugin->features);
874 }
875
876 /**
877  * gst_plugin_load:
878  * @name: name of plugin to load
879  *
880  * Load the named plugin.  
881  *
882  * Returns: whether the plugin was loaded or not
883  */
884 gboolean
885 gst_plugin_load (const gchar * name)
886 {
887   GstPlugin *plugin;
888   GError *error = NULL;
889
890   plugin = gst_registry_pool_find_plugin (name);
891   if (plugin) {
892     plugin = gst_plugin_load_file (plugin->filename, &error);
893     if (!plugin) {
894       GST_WARNING ("load_plugin error: %s\n", error->message);
895       g_error_free (error);
896       return FALSE;
897     }
898     return TRUE;;
899   }
900
901   GST_DEBUG ("Could not find %s in registry pool", name);
902   return FALSE;
903 }