gst-indent run on core
[platform/upstream/gstreamer.git] / gst / gstregistrypool.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 <string.h>
24
25 #include "gst_private.h"
26
27 #include "gstinfo.h"
28 #include "gstregistrypool.h"
29 #include "gstlog.h"
30 #include "gstfilter.h"
31
32 /* list of registries in the pool */
33 static GList *_gst_registry_pool = NULL;
34
35 /* list of plugins without a registry, like statically linked
36  * plugins */
37 static GList *_gst_registry_pool_plugins = NULL;
38
39 /**
40  * gst_registry_pool_list:
41  *
42  * Get a list of all registries in the pool
43  *
44  * Returns: a Glist of GstRegistries, g_list_free after use.
45  */
46 GList *
47 gst_registry_pool_list (void)
48 {
49   return g_list_copy (_gst_registry_pool);
50 }
51
52 #ifndef GST_DISABLE_REGISTRY
53 static gint
54 gst_registry_compare_func (gconstpointer a, gconstpointer b)
55 {
56   return GST_REGISTRY (a)->priority - GST_REGISTRY (b)->priority;
57 }
58
59 /**
60  * gst_registry_pool_add:
61  * @registry: the registry to add
62  * @priority: the priority of the registry
63  *
64  * Add the registry to the pool with the given priority.
65  */
66 void
67 gst_registry_pool_add (GstRegistry * registry, guint priority)
68 {
69   g_return_if_fail (GST_IS_REGISTRY (registry));
70
71   registry->priority = priority;
72
73   _gst_registry_pool =
74       g_list_insert_sorted (_gst_registry_pool, registry,
75       gst_registry_compare_func);
76 }
77
78 /**
79  * gst_registry_pool_remove:
80  * @registry: the registry to remove
81  *
82  * Remove the registry from the pool.
83  */
84 void
85 gst_registry_pool_remove (GstRegistry * registry)
86 {
87   g_return_if_fail (GST_IS_REGISTRY (registry));
88
89   _gst_registry_pool = g_list_remove (_gst_registry_pool, registry);
90 }
91 #endif /* GST_DISABLE_REGISTRY */
92
93 /**
94  * gst_registry_pool_add_plugin:
95  * @plugin: the plugin to add
96  *
97  * Add the plugin to the global pool of plugins.
98  */
99 void
100 gst_registry_pool_add_plugin (GstPlugin * plugin)
101 {
102   _gst_registry_pool_plugins =
103       g_list_prepend (_gst_registry_pool_plugins, plugin);
104 }
105
106 #ifndef GST_DISABLE_REGISTRY
107 static void
108 _registry_load_func (GstRegistry * registry, gpointer user_data)
109 {
110   if (!(registry->flags & GST_REGISTRY_DELAYED_LOADING)) {
111     gst_registry_load (registry);
112   }
113 }
114 #endif /* GST_DISABLE_REGISTRY */
115
116 /**
117  * gst_registry_pool_load_all:
118  *
119  * Load all the registries in the pool. Registries with the
120  * GST_REGISTRY_DELAYED_LOADING will not be loaded.
121  */
122 void
123 gst_registry_pool_load_all (void)
124 {
125 #ifndef GST_DISABLE_REGISTRY
126   g_list_foreach (_gst_registry_pool, (GFunc) _registry_load_func, NULL);
127 #endif /* GST_DISABLE_REGISTRY */
128 }
129
130 /**
131  * gst_registry_pool_plugin_list:
132  *
133  * Get a list of all plugins in the pool.
134  * 
135  * Returns: a GList of plugins, g_list_free after use.
136  */
137 GList *
138 gst_registry_pool_plugin_list (void)
139 {
140   return gst_registry_pool_plugin_filter (NULL, FALSE, NULL);
141 }
142
143 /**
144  * gst_registry_pool_plugin_filter:
145  * @filter: the filter to use
146  * @first: only return first match
147  * @user_data: user data passed to the filter function
148  *
149  * Runs a filter against all plugins in all registries and returns a GList with
150  * the results. If the first flag is set, only the first match is 
151  * returned (as a list with a single object).
152  *
153  * Returns: a GList of plugins, g_list_free after use.
154  */
155 GList *
156 gst_registry_pool_plugin_filter (GstPluginFilter filter, gboolean first,
157     gpointer user_data)
158 {
159   GList *result = NULL;
160   GList *temp;
161
162 #ifndef GST_DISABLE_REGISTRY
163   GList *walk;
164
165   walk = _gst_registry_pool;
166
167   while (walk) {
168     GstRegistry *registry = GST_REGISTRY (walk->data);
169
170     temp = gst_registry_plugin_filter (registry, filter, first, user_data);
171     if (temp && first)
172       return temp;
173
174     result = g_list_concat (result, temp);
175
176     walk = g_list_next (walk);
177   }
178 #endif /* GST_DISABLE_REGISTRY */
179
180   temp =
181       gst_filter_run (_gst_registry_pool_plugins, (GstFilterFunc) filter, first,
182       user_data);
183
184   result = g_list_concat (result, temp);
185
186   return result;
187 }
188
189 /**
190  * gst_registry_pool_feature_list:
191  * @type: the type of the features to list.
192  *
193  * Get a list of all pluginfeatures of the given type in the pool.
194  * 
195  * Returns: a GList of pluginfeatures, g_list_free after use.
196  */
197 GList *
198 gst_registry_pool_feature_list (GType type)
199 {
200   GstTypeNameData data;
201
202   data.name = NULL;
203   data.type = type;
204
205   return gst_registry_pool_feature_filter (
206       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
207       FALSE, &data);
208 }
209
210 /**
211  * gst_registry_pool_feature_filter:
212  * @filter: the filter to apply to the feature list
213  * @first: return the first matching feature
214  * @user_data: data passed to the filter function
215  *
216  * Apply the filter function to all features and return a list
217  * of those features that satisfy the filter. If the first flag
218  * is TRUE, only the first match is returned in a GList with
219  * one element.
220  * 
221  * Returns: a GList of pluginfeatures, g_list_free after use.
222  */
223 GList *
224 gst_registry_pool_feature_filter (GstPluginFeatureFilter filter, gboolean first,
225     gpointer user_data)
226 {
227   GList *result = NULL;
228   GList *temp;
229
230 #ifndef GST_DISABLE_REGISTRY
231   GList *walk;
232
233   walk = _gst_registry_pool;
234
235   while (walk) {
236     GstRegistry *registry = GST_REGISTRY (walk->data);
237
238     temp = gst_registry_feature_filter (registry, filter, first, user_data);
239     if (temp && first)
240       return temp;
241
242     result = g_list_concat (result, temp);
243
244     walk = g_list_next (walk);
245   }
246 #endif /* GST_DISABLE_REGISTRY */
247
248   temp =
249       gst_plugin_list_feature_filter (_gst_registry_pool_plugins, filter, first,
250       user_data);
251
252   result = g_list_concat (result, temp);
253
254   return result;
255 }
256
257 /**
258  * gst_registry_pool_find_plugin:
259  * @name: the name of the plugin to find
260  *
261  * Get the named plugin from the registry pool
262  * 
263  * Returns: The plugin with the given name or NULL if the plugin 
264  * was not found.
265  */
266 GstPlugin *
267 gst_registry_pool_find_plugin (const gchar * name)
268 {
269   GstPlugin *result = NULL;
270   GList *walk;
271
272   g_return_val_if_fail (name != NULL, NULL);
273
274   walk =
275       gst_registry_pool_plugin_filter ((GstPluginFilter) gst_plugin_name_filter,
276       TRUE, (gpointer) name);
277
278   if (walk)
279     result = GST_PLUGIN (walk->data);
280
281   g_list_free (walk);
282
283   return result;
284 }
285
286 /**
287  * gst_registry_pool_find_feature:
288  * @name: the name of the pluginfeature to find
289  * @type: the type of the pluginfeature to find
290  *
291  * Get the pluginfeature with the given name and type from the pool of
292  * registries.
293  * 
294  * Returns: A pluginfeature with the given name and type or NULL if the feature
295  * was not found.
296  */
297 GstPluginFeature *
298 gst_registry_pool_find_feature (const gchar * name, GType type)
299 {
300   GstPluginFeature *result = NULL;
301   GList *walk;
302   GstTypeNameData data;
303
304   g_return_val_if_fail (name != NULL, NULL);
305
306   data.type = type;
307   data.name = name;
308
309   walk =
310       gst_registry_pool_feature_filter ((GstPluginFeatureFilter)
311       gst_plugin_feature_type_name_filter, TRUE, &data);
312
313   if (walk)
314     result = GST_PLUGIN_FEATURE (walk->data);
315
316   g_list_free (walk);
317
318   return result;
319 }
320
321 /**
322  * gst_registry_pool_get_prefered:
323  * @flags: The flags for the prefered registry
324  *
325  * Get the prefered registry with the given flags
326  * 
327  * Returns: The registry with the flags.
328  */
329 GstRegistry *
330 gst_registry_pool_get_prefered (GstRegistryFlags flags)
331 {
332 #ifndef GST_DISABLE_REGISTRY
333   GList *walk = _gst_registry_pool;
334
335   while (walk) {
336     GstRegistry *registry = GST_REGISTRY (walk->data);
337
338     if (registry->flags & flags)
339       return registry;
340
341     walk = g_list_next (walk);
342   }
343 #endif /* GST_DISABLE_REGISTRY */
344   return NULL;
345 }