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