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