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