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