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