fix doc build fix autogen
[platform/upstream/gstreamer.git] / gst / gstregistry.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstregistry.c: handle registry
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 <glib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "gst_private.h"
32
33 #include "gstinfo.h"
34 #include "gstregistry.h"
35 #include "gstlog.h"
36 #include "gstmarshal.h"
37 #include "gstfilter.h"
38
39 /* Element signals and args */
40 enum {
41   PLUGIN_ADDED,
42   LAST_SIGNAL
43 };
44
45 static void             gst_registry_class_init           (GstRegistryClass *klass);
46 static void             gst_registry_init                 (GstRegistry *registry);
47
48 static GObjectClass *parent_class = NULL;
49 static guint gst_registry_signals[LAST_SIGNAL] = { 0 }; 
50
51 GType
52 gst_registry_get_type (void)
53 {
54   static GType registry_type = 0;
55
56   if (!registry_type) {
57     static const GTypeInfo registry_info = {
58       sizeof (GstRegistryClass),
59       NULL,
60       NULL,
61       (GClassInitFunc) gst_registry_class_init,
62       NULL,
63       NULL,
64       sizeof (GstRegistry),
65       32,
66       (GInstanceInitFunc) gst_registry_init,
67       NULL
68     };
69     registry_type = g_type_register_static (G_TYPE_OBJECT, "GstRegistry",
70                                             &registry_info, G_TYPE_FLAG_ABSTRACT);
71   }
72   return registry_type;
73 }
74
75 static void
76 gst_registry_class_init (GstRegistryClass *klass)
77 {
78   GObjectClass *gobject_class;
79
80   gobject_class = (GObjectClass*) klass;
81
82   parent_class = g_type_class_ref (G_TYPE_OBJECT);
83
84   gst_registry_signals[PLUGIN_ADDED] =
85     g_signal_new ("plugin_added", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
86                   G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL, NULL,
87                   gst_marshal_VOID__POINTER, G_TYPE_NONE, 1,
88                   G_TYPE_POINTER);
89
90   gobject_class->dispose = NULL;
91 }
92
93 static void
94 gst_registry_init (GstRegistry *registry)
95 {
96   registry->priority = 0;
97   registry->loaded = FALSE;
98   registry->paths = NULL;
99 }
100
101 /**
102  * gst_registry_load:
103  * @registry: the registry to load
104  *
105  * Load the given registry
106  *
107  * Returns: TRUE on success.
108  */
109 gboolean
110 gst_registry_load (GstRegistry *registry)
111 {
112   GstRegistryClass *rclass;
113
114   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
115
116   rclass = GST_REGISTRY_GET_CLASS (registry);
117
118   if (rclass->load)
119     return rclass->load (registry);
120
121   return FALSE;
122 }
123
124 /**
125  * gst_registry_is_loaded:
126  * @registry: the registry to check
127  *
128  * Check if the given registry is loaded
129  *
130  * Returns: TRUE if loaded.
131  */
132 gboolean
133 gst_registry_is_loaded (GstRegistry *registry)
134 {
135   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
136
137   return registry->loaded;
138 }
139
140 /**
141  * gst_registry_save:
142  * @registry: the registry to save
143  *
144  * Save the contents of the given registry
145  *
146  * Returns: TRUE on success
147  */
148 gboolean
149 gst_registry_save (GstRegistry *registry)
150 {
151   GstRegistryClass *rclass;
152
153   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
154
155   rclass = GST_REGISTRY_GET_CLASS (registry);
156
157   if (rclass->save)
158     return rclass->save (registry);
159
160   return FALSE;
161 }
162
163 /**
164  * gst_registry_rebuild:
165  * @registry: the registry to rebuild
166  *
167  * Rebuild the given registry
168  *
169  * Returns: TRUE on success
170  */
171 gboolean
172 gst_registry_rebuild (GstRegistry *registry)
173 {
174   GstRegistryClass *rclass;
175
176   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
177
178   rclass = GST_REGISTRY_GET_CLASS (registry);
179
180   if (rclass->rebuild)
181     return rclass->rebuild (registry);
182
183   return FALSE;
184 }
185
186 /**
187  * gst_registry_unload:
188  * @registry: the registry to unload
189  *
190  * Unload the given registry
191  *
192  * Returns: TRUE on success
193  */
194 gboolean
195 gst_registry_unload (GstRegistry *registry)
196 {
197   GstRegistryClass *rclass;
198
199   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
200
201   rclass = GST_REGISTRY_GET_CLASS (registry);
202
203   if (rclass->unload)
204     return rclass->unload (registry);
205
206   return FALSE;
207 }
208
209 /**
210  * gst_registry_add_path:
211  * @registry: the registry to add the path to
212  * @path: the path to add to the registry 
213  *
214  * Add the given path to the registry. The syntax of the
215  * path is specific to the registry. If the path has already been
216  * added, do nothing.
217  */
218 void
219 gst_registry_add_path (GstRegistry *registry, const gchar *path)
220 {
221   g_return_if_fail (GST_IS_REGISTRY (registry));
222   g_return_if_fail (path != NULL);
223
224   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp)) {
225     g_warning ("path %s already added to registry", path);        
226     return;
227   }
228
229   registry->paths = g_list_append (registry->paths, g_strdup (path));
230 }
231
232 /**
233  * gst_registry_get_path_list:
234  * @registry: the registry to get the pathlist of
235  *
236  * Get the list of paths for the given registry.
237  *
238  * Returns: A Glist of paths as strings. g_list_free after use.
239  */
240 GList*
241 gst_registry_get_path_list (GstRegistry *registry)
242 {
243   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
244
245   return g_list_copy (registry->paths);
246 }
247
248
249 /**
250  * gst_registry_clear_paths:
251  * @registry: the registry to clear the paths of
252  *
253  * Clear the paths of the given registry
254  */
255 void
256 gst_registry_clear_paths (GstRegistry *registry)
257 {
258   g_return_if_fail (GST_IS_REGISTRY (registry));
259
260   g_list_foreach (registry->paths, (GFunc) g_free, NULL);
261   g_list_free (registry->paths);
262
263   registry->paths = NULL;
264 }
265
266 /**
267  * gst_registry_add_plugin:
268  * @registry: the registry to add the plugin to
269  * @plugin: the plugin to add
270  *
271  * Add the plugin to the registry. The plugin-added signal 
272  * will be emitted.
273  *
274  * Returns: TRUE on success.
275  */
276 gboolean 
277 gst_registry_add_plugin (GstRegistry *registry, GstPlugin *plugin)
278 {
279   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
280   
281   plugin->manager = registry;
282   registry->plugins = g_list_prepend (registry->plugins, plugin);
283
284   g_signal_emit (G_OBJECT (registry), gst_registry_signals[PLUGIN_ADDED], 0, plugin);
285
286   return TRUE;
287 }
288
289 /**
290  * gst_registry_remove_plugin:
291  * @registry: the registry to remove the plugin from
292  * @plugin: the plugin to remove
293  *
294  * Remove the plugin from the registry.
295  */
296 void
297 gst_registry_remove_plugin (GstRegistry *registry, GstPlugin *plugin)
298 {
299   g_return_if_fail (GST_IS_REGISTRY (registry));
300
301   registry->plugins = g_list_remove (registry->plugins, plugin);
302 }
303
304 /**
305  * gst_registry_plugin_filter:
306  * @registry: registry to query
307  * @filter: the filter to use
308  * @first: only return first match
309  * @user_data: user data passed to the filter function
310  *
311  * Runs a filter against all plugins in the registry and returns a GList with
312  * the results. If the first flag is set, only the first match is 
313  * returned (as a list with a single object).
314  *
315  * Returns: a GList of plugins, g_list_free after use.
316  */
317 GList*
318 gst_registry_plugin_filter (GstRegistry *registry, 
319                             GstPluginFilter filter, 
320                             gboolean first,
321                             gpointer user_data)
322 {
323   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
324
325   return gst_filter_run (registry->plugins, (GstFilterFunc) filter, first, user_data);
326 }
327
328 /**
329  * gst_registry_feature_filter:
330  * @registry: registry to query
331  * @filter: the filter to use
332  * @first: only return first match
333  * @user_data: user data passed to the filter function
334  *
335  * Runs a filter against all features of the plugins in the registry 
336  * and returns a GList with the results. 
337  * If the first flag is set, only the first match is 
338  * returned (as a list with a single object).
339  *
340  * Returns: a GList of plugin features, g_list_free after use.
341  */
342 GList*
343 gst_registry_feature_filter (GstRegistry *registry,
344                              GstPluginFeatureFilter filter,
345                              gboolean first,
346                              gpointer user_data)
347 {
348   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
349
350   return gst_plugin_list_feature_filter (registry->plugins, filter, first, user_data);
351 }
352
353 /**
354  * gst_registry_find_plugin:
355  * @registry: the registry to search
356  * @name: the plugin name to find
357  *
358  * Find the plugin with the given name in the registry.
359  *
360  * Returns: The plugin with the given name or NULL if the plugin was not found.
361  */
362 GstPlugin*
363 gst_registry_find_plugin (GstRegistry *registry, const gchar *name)
364 {
365   GList *walk;
366   GstPlugin *result = NULL;
367
368   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
369   g_return_val_if_fail (name != NULL, NULL);
370
371   walk = gst_registry_plugin_filter (registry, 
372                                      (GstPluginFilter) gst_plugin_name_filter, 
373                                      TRUE, 
374                                      (gpointer) name);
375   if (walk) 
376     result = GST_PLUGIN (walk->data);
377
378   g_list_free (walk);
379
380   return result;
381 }
382
383 /**
384  * gst_registry_find_feature:
385  * @registry: the registry to search
386  * @name: the pluginfeature name to find
387  * @type: the pluginfeature type to find
388  *
389  * Find the pluginfeature with the given name and type in the registry.
390  *
391  * Returns: The pluginfeature with the given name and type or NULL 
392  * if the plugin was not found.
393  */
394 GstPluginFeature*
395 gst_registry_find_feature (GstRegistry *registry, const gchar *name, GType type)
396 {
397   GstPluginFeature *feature = NULL;
398   GList *walk;
399   GstTypeNameData data;
400
401   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
402   g_return_val_if_fail (name != NULL, NULL);
403
404   data.name = name;
405   data.type = type;
406
407   walk = gst_registry_feature_filter (registry, 
408                                       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
409                                       TRUE,
410                                       &data);
411
412   if (walk) 
413     feature = GST_PLUGIN_FEATURE (walk->data);
414
415   g_list_free (walk);
416
417   return feature;
418 }
419
420
421 /**
422  * gst_registry_load_plugin:
423  * @registry: the registry to load the plugin from
424  * @plugin: the plugin to load
425  *
426  * Bring the plugin from the registry into memory.
427  *
428  * Returns: a value indicating the result 
429  */
430 GstRegistryReturn
431 gst_registry_load_plugin (GstRegistry *registry, GstPlugin *plugin)
432 {
433   GstRegistryClass *rclass;
434
435   g_return_val_if_fail (GST_IS_REGISTRY (registry), GST_REGISTRY_PLUGIN_LOAD_ERROR);
436
437   rclass = GST_REGISTRY_GET_CLASS (registry);
438
439   if (rclass->load_plugin)
440     return rclass->load_plugin (registry, plugin);
441
442   return GST_REGISTRY_PLUGIN_LOAD_ERROR;
443 }
444
445 /**
446  * gst_registry_unload_plugin:
447  * @registry: the registry to unload the plugin from
448  * @plugin: the plugin to unload
449  *
450  * Unload the plugin from the given registry.
451  *
452  * Returns: a value indicating the result 
453  */
454 GstRegistryReturn
455 gst_registry_unload_plugin (GstRegistry *registry, GstPlugin *plugin)
456 {
457   GstRegistryClass *rclass;
458
459   g_return_val_if_fail (GST_IS_REGISTRY (registry), GST_REGISTRY_PLUGIN_LOAD_ERROR);
460
461   rclass = GST_REGISTRY_GET_CLASS (registry);
462
463   if (rclass->unload_plugin)
464     return rclass->unload_plugin (registry, plugin);
465
466   return GST_REGISTRY_PLUGIN_LOAD_ERROR;
467 }
468
469 /**
470  * gst_registry_update_plugin:
471  * @registry: the registry to update
472  * @plugin: the plugin to update
473  *
474  * Update the plugin in the given registry.
475  *
476  * Returns: a value indicating the result 
477  */
478 GstRegistryReturn
479 gst_registry_update_plugin (GstRegistry *registry, GstPlugin *plugin)
480 {
481   GstRegistryClass *rclass;
482
483   g_return_val_if_fail (GST_IS_REGISTRY (registry), GST_REGISTRY_PLUGIN_LOAD_ERROR);
484
485   rclass = GST_REGISTRY_GET_CLASS (registry);
486
487   if (rclass->update_plugin)
488     return rclass->update_plugin (registry, plugin);
489
490   return GST_REGISTRY_PLUGIN_LOAD_ERROR;
491 }