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