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