Fix aliasing warning from gcc-3.3
[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 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <dirent.h>
26 #include <unistd.h>
27
28 #include "gst_private.h"
29 #include "gstplugin.h"
30 #include "gstversion.h"
31 #include "gstregistrypool.h"
32 #include "gstlog.h"
33 #include "config.h"
34 #include "gstfilter.h"
35
36 static GModule *main_module = NULL;
37 static GList *_gst_plugin_static = NULL;
38
39 static GstPlugin*       gst_plugin_register_func        (GstPluginDesc *desc, GstPlugin *plugin, 
40                                                          GModule *module);
41 GQuark 
42 gst_plugin_error_quark (void)
43 {
44   static GQuark quark = 0;
45   if (!quark)
46     quark = g_quark_from_static_string ("gst_plugin_error");
47   return quark;
48 }
49
50 /* this function can be called in the GCC constructor extension, before
51  * the _gst_plugin_initialize() was called. In that case, we store the 
52  * plugin description in a list to initialize it when we open the main
53  * module later on.
54  * When the main module is known, we can register the plugin right away.
55  * */
56 void
57 _gst_plugin_register_static (GstPluginDesc *desc)
58 {
59   if (main_module == NULL) {
60     _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
61   }
62   else {
63     GstPlugin *plugin;
64
65     plugin = g_new0 (GstPlugin, 1);
66     plugin->filename = NULL;
67     plugin->module = NULL;
68     plugin = gst_plugin_register_func (desc, plugin, main_module);
69
70     if (plugin) {
71       plugin->module = main_module;
72       gst_registry_pool_add_plugin (plugin);
73     }
74   }
75 }
76
77 void
78 _gst_plugin_initialize (void)
79 {
80   main_module =  g_module_open (NULL, G_MODULE_BIND_LAZY);
81
82   /* now register all static plugins */
83   g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static, NULL);
84 }
85
86 static gboolean
87 gst_plugin_check_version (gint major, gint minor)
88 {
89   /* return NULL if the major and minor version numbers are not compatible */
90   /* with ours. */
91   if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR) 
92     return FALSE;
93
94   return TRUE;
95 }
96
97 static GstPlugin*
98 gst_plugin_register_func (GstPluginDesc *desc, GstPlugin *plugin, GModule *module)
99 {
100   if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
101     GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" has incompatible version, not loading",
102        plugin->filename);
103     return NULL;
104   }
105
106   g_free (plugin->name);
107   plugin->name = g_strdup(desc->name);
108
109   if (!((desc->plugin_init) (module, plugin))) {
110     GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" failed to initialise",
111        plugin->filename);
112     return NULL;
113   }
114   GST_INFO (GST_CAT_PLUGIN_LOADING,"plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
115
116   return plugin;
117 }
118
119 /**
120  * gst_plugin_new:
121  * @filename: The filename of the plugin
122  *
123  * Creates a plugin from the given filename
124  *
125  * Returns: A new GstPlugin object
126  */
127 GstPlugin*
128 gst_plugin_new (const gchar *filename)
129 {
130   GstPlugin *plugin = g_new0 (GstPlugin, 1);
131   plugin->filename = g_strdup (filename);
132
133   return plugin;
134 }
135
136 /**
137  * gst_plugin_load_plugin:
138  * @plugin: The plugin to load
139  * @error: Pointer to a NULL-valued GError.
140  *
141  * Load the given plugin.
142  *
143  * Returns: whether or not the plugin loaded. Sets @error as appropriate.
144  */
145 gboolean
146 gst_plugin_load_plugin (GstPlugin *plugin, GError **error)
147 {
148   GModule *module;
149   GstPluginDesc *desc;
150   struct stat file_status;
151   gchar *filename;
152
153   g_return_val_if_fail (plugin != NULL, FALSE);
154
155   if (plugin->module) 
156     return TRUE;
157
158   filename = plugin->filename;
159
160   GST_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"", filename);
161
162   if (g_module_supported () == FALSE) {
163     g_set_error (error,
164                  GST_PLUGIN_ERROR,
165                  GST_PLUGIN_ERROR_MODULE,
166                  "Dynamic loading not supported");
167     return FALSE;
168   }
169
170   if (stat (filename, &file_status)) {
171     g_set_error (error,
172                  GST_PLUGIN_ERROR,
173                  GST_PLUGIN_ERROR_MODULE,
174                  "Problem opening file %s (plugin %s)\n",
175                  filename, plugin->name); 
176     return FALSE;
177   }
178
179   module = g_module_open (filename, G_MODULE_BIND_LAZY);
180
181   if (module != NULL) {
182     gpointer ptr;
183
184     if (g_module_symbol (module, "plugin_desc", &ptr)) {
185       desc = (GstPluginDesc *)ptr;
186
187       GST_DEBUG (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded, called entry function...", filename);
188
189       plugin->filename = g_strdup (filename);
190       plugin = gst_plugin_register_func (desc, plugin, module);
191
192       if (plugin != NULL) {
193         GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" loaded", plugin->filename);
194         plugin->module = module;
195         return TRUE;
196       }
197       else {
198         /* plugin == NULL */
199         g_set_error (error,
200                      GST_PLUGIN_ERROR,
201                      GST_PLUGIN_ERROR_MODULE,
202                      "gst_plugin_register_func failed for plugin \"%s\"",
203                      filename);
204         return FALSE;
205       }
206     }
207     else {
208       g_set_error (error,
209                    GST_PLUGIN_ERROR,
210                    GST_PLUGIN_ERROR_MODULE,
211                    "Could not find plugin_desc in \"%s\"",
212                    filename);
213     }
214     return FALSE;
215   } 
216   else {
217     g_set_error (error,
218                  GST_PLUGIN_ERROR,
219                  GST_PLUGIN_ERROR_MODULE,
220                  "Error loading plugin %s, reason: %s\n",
221                  filename, g_module_error());
222     return FALSE;
223   }
224 }
225
226
227 /**
228  * gst_plugin_unload_plugin:
229  * @plugin: The plugin to unload
230  *
231  * Unload the given plugin.
232  *
233  * Returns: whether or not the plugin unloaded
234  */
235 gboolean
236 gst_plugin_unload_plugin (GstPlugin *plugin)
237 {
238   g_return_val_if_fail (plugin != NULL, FALSE);
239
240   if (!plugin->module) 
241     return TRUE;
242
243   if (g_module_close (plugin->module)) {
244     plugin->module = NULL;
245     GST_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded", plugin->filename);
246     return TRUE;
247   }
248   else {
249     GST_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"", plugin->filename);
250     return FALSE;
251   }
252 }
253
254 /**
255  * gst_plugin_get_name:
256  * @plugin: plugin to get the name of
257  *
258  * Get the short name of the plugin
259  *
260  * Returns: the name of the plugin
261  */
262 const gchar*
263 gst_plugin_get_name (GstPlugin *plugin)
264 {
265   g_return_val_if_fail (plugin != NULL, NULL);
266
267   return plugin->name;
268 }
269
270 /**
271  * gst_plugin_set_name:
272  * @plugin: plugin to set name of
273  * @name: new name
274  *
275  * Sets the name (should be short) of the plugin.
276  */
277 void
278 gst_plugin_set_name (GstPlugin *plugin, const gchar *name)
279 {
280   g_return_if_fail (plugin != NULL);
281
282   g_free (plugin->name);
283
284   plugin->name = g_strdup (name);
285 }
286
287 /**
288  * gst_plugin_set_longname:
289  * @plugin: plugin to set long name of
290  * @longname: new long name
291  *
292  * Sets the long name (should be descriptive) of the plugin.
293  */
294 void
295 gst_plugin_set_longname (GstPlugin *plugin, const gchar *longname)
296 {
297   g_return_if_fail(plugin != NULL);
298
299   g_free(plugin->longname);
300
301   plugin->longname = g_strdup(longname);
302 }
303
304 /**
305  * gst_plugin_get_longname:
306  * @plugin: plugin to get long name of
307  *
308  * Get the long descriptive name of the plugin
309  *
310  * Returns: the long name of the plugin
311  */
312 const gchar*
313 gst_plugin_get_longname (GstPlugin *plugin)
314 {
315   g_return_val_if_fail (plugin != NULL, NULL);
316
317   return plugin->longname;
318 }
319
320 /**
321  * gst_plugin_get_filename:
322  * @plugin: plugin to get the filename of
323  *
324  * get the filename of the plugin
325  *
326  * Returns: the filename of the plugin
327  */
328 const gchar*
329 gst_plugin_get_filename (GstPlugin *plugin)
330 {
331   g_return_val_if_fail (plugin != NULL, NULL);
332
333   return plugin->filename;
334 }
335
336 /**
337  * gst_plugin_is_loaded:
338  * @plugin: plugin to query
339  *
340  * queries if the plugin is loaded into memory
341  *
342  * Returns: TRUE is loaded, FALSE otherwise
343  */
344 gboolean
345 gst_plugin_is_loaded (GstPlugin *plugin)
346 {
347   g_return_val_if_fail (plugin != NULL, FALSE);
348
349   return (plugin->module != NULL);
350 }
351
352 /**
353  * gst_plugin_feature_list:
354  * @plugin: plugin to query
355  * @filter: the filter to use
356  * @first: only return first match
357  * @user_data: user data passed to the filter function
358  *
359  * Runs a filter against all plugin features and returns a GList with
360  * the results. If the first flag is set, only the first match is 
361  * returned (as a list with a single object).
362  *
363  * Returns: a GList of features, g_list_free after use.
364  */
365 GList*
366 gst_plugin_feature_filter (GstPlugin *plugin,
367                            GstPluginFeatureFilter filter,
368                            gboolean first,
369                            gpointer user_data)
370 {
371   return gst_filter_run (plugin->features, (GstFilterFunc) filter, first, user_data);
372 }
373
374 typedef struct
375
376   GstPluginFeatureFilter filter;
377   gboolean               first;
378   gpointer               user_data;
379   GList                 *result;
380 } FeatureFilterData;
381
382 static gboolean
383 _feature_filter (GstPlugin *plugin, gpointer user_data)
384 {
385   GList *result;
386   FeatureFilterData *data = (FeatureFilterData *) user_data;
387
388   result = gst_plugin_feature_filter (plugin, data->filter, data->first, data->user_data);
389   if (result) {
390     data->result = g_list_concat (data->result, result);
391     return TRUE;
392   }
393   return FALSE;
394 }
395
396 /**
397  * gst_plugin_list_feature_list:
398  * @list: a list of plugins to query
399  * @filter: the filter to use
400  * @first: only return first match
401  * @user_data: user data passed to the filter function
402  *
403  * Runs a filter against all plugin features of the plugins in the given
404  * list and returns a GList with the results. 
405  * If the first flag is set, only the first match is 
406  * returned (as a list with a single object).
407  *
408  * Returns: a GList of features, g_list_free after use.
409  */
410 GList*
411 gst_plugin_list_feature_filter  (GList *list, 
412                                  GstPluginFeatureFilter filter,
413                                  gboolean first,
414                                  gpointer user_data)
415 {
416   FeatureFilterData data;
417   GList *result;
418
419   data.filter = filter;
420   data.first = first;
421   data.user_data = user_data;
422   data.result = NULL;
423
424   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
425   g_list_free (result);
426
427   return data.result;
428 }
429
430 /**
431  * gst_plugin_name_filter:
432  * @plugin: the plugin to check
433  * @name: the name of the plugin
434  *
435  * A standard filterthat returns TRUE when the plugin is of the
436  * given name.
437  *
438  * Returns: TRUE if the plugin is of the given name.
439  */
440 gboolean
441 gst_plugin_name_filter (GstPlugin *plugin, const gchar *name)
442 {
443   return (plugin->name && !strcmp (plugin->name, name));
444 }
445
446 /**
447  * gst_plugin_find_feature:
448  * @plugin: plugin to get the feature from
449  * @name: The name of the feature to find
450  * @type: The type of the feature to find
451  *
452  * Find a feature of the given name and type in the given plugin.
453  *
454  * Returns: a GstPluginFeature or NULL if the feature was not found.
455  */
456 GstPluginFeature*
457 gst_plugin_find_feature (GstPlugin *plugin, const gchar *name, GType type)
458 {
459   GList *walk;
460   GstPluginFeature *result = NULL;
461   GstTypeNameData data;
462
463   g_return_val_if_fail (name != NULL, NULL);
464
465   data.type = type;
466   data.name = name;
467   
468   walk = gst_filter_run (plugin->features, 
469                          (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE,
470                          &data);
471
472   if (walk) 
473     result = GST_PLUGIN_FEATURE (walk->data);
474
475   return result;
476 }
477
478 /**
479  * gst_plugin_add_feature:
480  * @plugin: plugin to add feature to
481  * @feature: feature to add
482  *
483  * Add feature to the list of those provided by the plugin.
484  * There is a separate namespace for each plugin feature type.
485  * See #gst_plugin_get_feature_list
486  */
487 void
488 gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
489 {
490   GstPluginFeature *oldfeature;
491
492   g_return_if_fail (plugin != NULL);
493   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
494   g_return_if_fail (feature != NULL);
495
496   oldfeature = gst_plugin_find_feature (plugin, 
497                   GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
498
499   if (!oldfeature) {
500     feature->manager = plugin;
501     plugin->features = g_list_prepend (plugin->features, feature);
502     plugin->numfeatures++;
503   }
504 }
505
506 /**
507  * gst_plugin_get_feature_list:
508  * @plugin: the plugin to get the features from
509  *
510  * get a list of all the features that this plugin provides
511  *
512  * Returns: a GList of features, use g_list_free to free the list.
513  */
514 GList*
515 gst_plugin_get_feature_list (GstPlugin *plugin)
516 {
517   g_return_val_if_fail (plugin != NULL, NULL);
518
519   return g_list_copy (plugin->features);
520 }
521
522 /**
523  * gst_plugin_load:
524  * @name: name of plugin to load
525  *
526  * Load the named plugin.  
527  *
528  * Returns: whether the plugin was loaded or not
529  */
530 gboolean
531 gst_plugin_load (const gchar *name)
532 {
533   GstPlugin *plugin;
534   GError *error = NULL;
535
536   plugin = gst_registry_pool_find_plugin (name);
537   if (plugin) {
538     gboolean result = gst_plugin_load_plugin (plugin, &error);
539     if (error) {
540       GST_DEBUG (GST_CAT_PLUGIN_LOADING, "load_plugin error: %s\n",
541                  error->message);
542       g_error_free (error);
543     }
544     return result;
545   }
546
547   GST_DEBUG (GST_CAT_PLUGIN_LOADING, "Could not find %s in registry pool",
548              name);
549
550   return FALSE;
551 }
552
553 /**
554  * gst_library_load:
555  * @name: name of library to load
556  *
557  * Load the named library.  Name should be given as
558  * &quot;liblibrary.so&quot;.
559  *
560  * Returns: whether the library was loaded or not
561  */
562 gboolean
563 gst_library_load (const gchar *name)
564 {
565   gboolean res;
566
567   /* for now this is the same */
568   res = gst_plugin_load (name);
569
570   return res;
571 }