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