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