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