gst-indent run on core
[platform/upstream/gstreamer.git] / gst / gsttypefind.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefind.h: typefinding subsystem
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "gstinfo.h"
23 #include "gsttypefind.h"
24 #include "gstregistrypool.h"
25
26 GST_DEBUG_CATEGORY_STATIC (gst_type_find_debug);
27 #define GST_CAT_DEFAULT gst_type_find_debug
28
29 static void gst_type_find_factory_class_init (gpointer g_class,
30     gpointer class_data);
31 static void gst_type_find_factory_init (GTypeInstance * instance,
32     gpointer g_class);
33 static void gst_type_find_factory_dispose (GObject * object);
34
35 static void gst_type_find_factory_unload_thyself (GstPluginFeature * feature);
36
37 static void gst_type_find_load_plugin (GstTypeFind * find, gpointer data);
38
39 static GstPluginFeatureClass *parent_class = NULL;
40
41 GType
42 gst_type_find_factory_get_type (void)
43 {
44   static GType typefind_type = 0;
45
46   if (!typefind_type) {
47     static const GTypeInfo typefind_info = {
48       sizeof (GstTypeFindFactoryClass),
49       NULL,
50       NULL,
51       gst_type_find_factory_class_init,
52       NULL,
53       NULL,
54       sizeof (GstTypeFindFactory),
55       0,
56       gst_type_find_factory_init,
57       NULL
58     };
59     typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
60         "GstTypeFindFactory", &typefind_info, 0);
61     GST_DEBUG_CATEGORY_INIT (gst_type_find_debug, "GST_TYPEFIND",
62         GST_DEBUG_FG_GREEN, "typefinding subsystem");
63   }
64
65   return typefind_type;
66 }
67 static void
68 gst_type_find_factory_class_init (gpointer g_class, gpointer class_data)
69 {
70   GstPluginFeatureClass *gstpluginfeature_class =
71       GST_PLUGIN_FEATURE_CLASS (g_class);
72   GObjectClass *object_class = G_OBJECT_CLASS (g_class);
73
74   parent_class = g_type_class_peek_parent (g_class);
75
76   object_class->dispose = gst_type_find_factory_dispose;
77
78   gstpluginfeature_class->unload_thyself =
79       GST_DEBUG_FUNCPTR (gst_type_find_factory_unload_thyself);
80 }
81 static void
82 gst_type_find_factory_init (GTypeInstance * instance, gpointer g_class)
83 {
84   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
85
86   factory->user_data = factory;
87   factory->function = gst_type_find_load_plugin;
88 }
89 static void
90 gst_type_find_factory_dispose (GObject * object)
91 {
92   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
93
94   if (factory->caps) {
95     gst_caps_free (factory->caps);
96     factory->caps = NULL;
97   }
98   if (factory->extensions) {
99     g_strfreev (factory->extensions);
100     factory->extensions = NULL;
101   }
102 }
103 static void
104 gst_type_find_factory_unload_thyself (GstPluginFeature * feature)
105 {
106   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
107
108   factory->function = gst_type_find_load_plugin;
109   factory->user_data = factory;
110 }
111 static void
112 gst_type_find_load_plugin (GstTypeFind * find, gpointer data)
113 {
114   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (data);
115
116   GST_DEBUG_OBJECT (factory, "need to load typefind function %s",
117       GST_PLUGIN_FEATURE_NAME (factory));
118
119   if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
120     if (factory->function == gst_type_find_load_plugin) {
121       /* looks like we didn't get a real typefind function */
122       g_warning ("could not load valid typefind function for feature '%s'\n",
123           GST_PLUGIN_FEATURE_NAME (factory));
124     } else {
125       g_assert (factory->function);
126       gst_type_find_factory_call_function (factory, find);
127     }
128   }
129 }
130
131 /**
132  * gst_type_find_factory_get_list:
133  *
134  * Gets the list of all registered typefind factories. You must free the
135  * list using g_list_free.
136  * 
137  * Returns: the list of all registered typefind factories
138  */
139 GList *
140 gst_type_find_factory_get_list (void)
141 {
142   return gst_registry_pool_feature_list (GST_TYPE_TYPE_FIND_FACTORY);
143 }
144
145 /**
146  * gst_type_find_factory_get_caps:
147  * @factory: a factory
148  * 
149  * Gets the caps associated with a typefind factory.
150  *
151  * Returns: the #GstCaps associated with this factory
152  */
153 const GstCaps *
154 gst_type_find_factory_get_caps (const GstTypeFindFactory * factory)
155 {
156   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
157
158   return factory->caps;
159 }
160
161 /**
162  * gst_type_find_factory_get_extensions:
163  * @factory: a factory
164  * 
165  * Gets the extensions associated with a typefind factory. The returned
166  * array should not be changed. If you need to change stuff in it, you should
167  * copy it using g_stdupv().  This function may return NULL to indicate
168  * a 0-length list.
169  *
170  * Returns: a NULL-terminated array of extensions associated with this factory
171  */
172 gchar **
173 gst_type_find_factory_get_extensions (const GstTypeFindFactory * factory)
174 {
175   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
176
177   return factory->extensions;
178 }
179
180 /**
181  * gst_type_find_factory_call_function:
182  * @factory: a factory
183  * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
184  *        members must be set.
185  * 
186  * Calls the typefinding function associated with this factory.
187  */
188 void
189 gst_type_find_factory_call_function (const GstTypeFindFactory * factory,
190     GstTypeFind * find)
191 {
192   g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
193   g_return_if_fail (find != NULL);
194   g_return_if_fail (find->peek != NULL);
195   g_return_if_fail (find->suggest != NULL);
196
197   /* should never happen */
198   g_assert (factory->function != NULL);
199
200   factory->function (find, factory->user_data);
201 }
202
203 /**
204  * gst_type_find_register:
205  * @plugin: the GstPlugin to register with
206  * @name: the name for registering
207  * @rank: rank (or importance) of this typefind function
208  * @func: the function to use for typefinding
209  * @extensions: optional extensions that could belong to this type
210  * @possible_caps: optionally the caps that could be returned when typefinding succeeds
211  * @data: optional user data. This user data must be available until the plugin 
212  *        is unloaded.
213  *
214  * Registers a new typefind function to be used for typefinding. After 
215  * registering this function will be available for typefinding.
216  * This function is typically called during an element's plugin initialization.
217  *
218  * Returns: TRUE on success, FALSE otherwise
219  */
220 gboolean
221 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
222     GstTypeFindFunction func, gchar ** extensions,
223     const GstCaps * possible_caps, gpointer data)
224 {
225   GstTypeFindFactory *factory;
226
227   g_return_val_if_fail (plugin != NULL, FALSE);
228   g_return_val_if_fail (name != NULL, FALSE);
229   g_return_val_if_fail (func != NULL, FALSE);
230
231   GST_INFO ("registering typefind function for %s", name);
232   factory =
233       GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name,
234           GST_TYPE_TYPE_FIND_FACTORY));
235   if (!factory) {
236     factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
237     GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
238     g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
239     gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
240   } else {
241     GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
242   }
243
244   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
245   if (factory->extensions)
246     g_strfreev (factory->extensions);
247
248   factory->extensions = g_strdupv (extensions);
249   gst_caps_replace (&factory->caps, gst_caps_copy (possible_caps));
250   factory->function = func;
251   factory->user_data = data;
252
253   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
254
255   return TRUE;
256 }
257
258 /*** typefind function interface **********************************************/
259
260 /**
261  * gst_type_find_peek:
262  * @find: the find object the function was called with
263  * @offset: the offset
264  * @size: the number of bytes to return
265  *
266  * Returns size bytes of the stream to identify beginning at offset. If offset 
267  * is a positive number, the offset is relative to the beginning of the stream,
268  * if offset is a negative number the offset is relative to the end of the 
269  * stream. The returned memory is valid until the typefinding function returns
270  * and must not be freed.
271  * If NULL is returned, that data is not available.
272  *
273  * Returns: the requested data or NULL if that data is not available.
274  */
275 guint8 *
276 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
277 {
278   g_return_val_if_fail (find->peek != NULL, NULL);
279
280   return find->peek (find->data, offset, size);
281 }
282
283 /**
284  * gst_type_find_suggest:
285  * @find: the find object the function was called with
286  * @probability: the probability in percent that the suggestion is right
287  * @caps: the fixed caps to suggest
288  *
289  * If a typefind function calls this function it suggests the caps with the
290  * given probability. A typefind function may supply different suggestions
291  * in one call.
292  * It is up to the caller of the typefind function to interpret these values.
293  */
294 void
295 gst_type_find_suggest (GstTypeFind * find, guint probability,
296     const GstCaps * caps)
297 {
298   g_return_if_fail (find->suggest != NULL);
299   g_return_if_fail (probability <= 100);
300   g_return_if_fail (caps != NULL);
301   g_return_if_fail (gst_caps_is_fixed (caps));
302
303   find->suggest (find->data, probability, caps);
304 }
305
306 /**
307  * gst_type_find_get_length:
308  * @find: the find object the function was called with
309  *
310  * Get the length of the data stream.
311  * 
312  * Returns: the length of the data stream or 0 if it is not available.
313  */
314 guint64
315 gst_type_find_get_length (GstTypeFind * find)
316 {
317   if (find->get_length == NULL)
318     return 0;
319
320   return find->get_length (find->data);
321 }