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