2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gsttypefind.h: typefinding subsystem
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.
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.
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.
22 #include "gst_private.h"
24 #include "gsttypefind.h"
25 #include "gstregistrypool.h"
27 GST_DEBUG_CATEGORY_STATIC (gst_type_find_debug);
28 #define GST_CAT_DEFAULT gst_type_find_debug
30 static void gst_type_find_factory_class_init (gpointer g_class,
32 static void gst_type_find_factory_init (GTypeInstance * instance,
34 static void gst_type_find_factory_dispose (GObject * object);
36 static void gst_type_find_factory_unload_thyself (GstPluginFeature * feature);
38 static void gst_type_find_load_plugin (GstTypeFind * find, gpointer data);
40 static GstPluginFeatureClass *parent_class = NULL;
43 gst_type_find_factory_get_type (void)
45 static GType typefind_type = 0;
48 static const GTypeInfo typefind_info = {
49 sizeof (GstTypeFindFactoryClass),
52 gst_type_find_factory_class_init,
55 sizeof (GstTypeFindFactory),
57 gst_type_find_factory_init,
61 typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
62 "GstTypeFindFactory", &typefind_info, 0);
63 GST_DEBUG_CATEGORY_INIT (gst_type_find_debug, "GST_TYPEFIND",
64 GST_DEBUG_FG_GREEN, "typefinding subsystem");
70 gst_type_find_factory_class_init (gpointer g_class, gpointer class_data)
72 GstPluginFeatureClass *gstpluginfeature_class =
73 GST_PLUGIN_FEATURE_CLASS (g_class);
74 GObjectClass *object_class = G_OBJECT_CLASS (g_class);
76 parent_class = g_type_class_peek_parent (g_class);
78 object_class->dispose = gst_type_find_factory_dispose;
80 gstpluginfeature_class->unload_thyself =
81 GST_DEBUG_FUNCPTR (gst_type_find_factory_unload_thyself);
84 gst_type_find_factory_init (GTypeInstance * instance, gpointer g_class)
86 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
88 factory->user_data = factory;
89 factory->function = gst_type_find_load_plugin;
92 gst_type_find_factory_dispose (GObject * object)
94 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
97 gst_caps_unref (factory->caps);
100 if (factory->extensions) {
101 g_strfreev (factory->extensions);
102 factory->extensions = NULL;
106 gst_type_find_factory_unload_thyself (GstPluginFeature * feature)
108 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
110 factory->function = gst_type_find_load_plugin;
111 factory->user_data = factory;
114 gst_type_find_load_plugin (GstTypeFind * find, gpointer data)
116 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (data);
118 GST_DEBUG_OBJECT (factory, "need to load typefind function %s",
119 GST_PLUGIN_FEATURE_NAME (factory));
121 if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
122 if (factory->function == gst_type_find_load_plugin) {
123 /* looks like we didn't get a real typefind function */
124 g_warning ("could not load valid typefind function for feature '%s'\n",
125 GST_PLUGIN_FEATURE_NAME (factory));
127 g_assert (factory->function);
128 gst_type_find_factory_call_function (factory, find);
134 * gst_type_find_factory_get_list:
136 * Gets the list of all registered typefind factories. You must free the
137 * list using g_list_free.
139 * Returns: the list of all registered typefind factories
142 gst_type_find_factory_get_list (void)
144 return gst_registry_pool_feature_list (GST_TYPE_TYPE_FIND_FACTORY);
148 * gst_type_find_factory_get_caps:
149 * @factory: a factory
151 * Gets the caps associated with a typefind factory.
153 * Returns: the #GstCaps associated with this factory
156 gst_type_find_factory_get_caps (const GstTypeFindFactory * factory)
158 g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
160 return factory->caps;
164 * gst_type_find_factory_get_extensions:
165 * @factory: a factory
167 * Gets the extensions associated with a typefind factory. The returned
168 * array should not be changed. If you need to change stuff in it, you should
169 * copy it using g_stdupv(). This function may return NULL to indicate
172 * Returns: a NULL-terminated array of extensions associated with this factory
175 gst_type_find_factory_get_extensions (const GstTypeFindFactory * factory)
177 g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
179 return factory->extensions;
183 * gst_type_find_factory_call_function:
184 * @factory: a factory
185 * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
186 * members must be set.
188 * Calls the typefinding function associated with this factory.
191 gst_type_find_factory_call_function (const GstTypeFindFactory * factory,
194 g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
195 g_return_if_fail (find != NULL);
196 g_return_if_fail (find->peek != NULL);
197 g_return_if_fail (find->suggest != NULL);
199 /* should never happen */
200 g_assert (factory->function != NULL);
202 factory->function (find, factory->user_data);
206 * gst_type_find_register:
207 * @plugin: the GstPlugin to register with
208 * @name: the name for registering
209 * @rank: rank (or importance) of this typefind function
210 * @func: the function to use for typefinding
211 * @extensions: optional extensions that could belong to this type
212 * @possible_caps: optionally the caps that could be returned when typefinding succeeds
213 * @data: optional user data. This user data must be available until the plugin
216 * Registers a new typefind function to be used for typefinding. After
217 * registering this function will be available for typefinding.
218 * This function is typically called during an element's plugin initialization.
220 * Returns: TRUE on success, FALSE otherwise
223 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
224 GstTypeFindFunction func, gchar ** extensions,
225 const GstCaps * possible_caps, gpointer data)
227 GstTypeFindFactory *factory;
229 g_return_val_if_fail (plugin != NULL, FALSE);
230 g_return_val_if_fail (name != NULL, FALSE);
231 g_return_val_if_fail (func != NULL, FALSE);
233 GST_INFO ("registering typefind function for %s", name);
235 GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name,
236 GST_TYPE_TYPE_FIND_FACTORY));
238 factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
239 GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
240 g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
241 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
242 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
244 GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
247 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
248 if (factory->extensions)
249 g_strfreev (factory->extensions);
251 factory->extensions = g_strdupv (extensions);
252 gst_caps_replace (&factory->caps, (GstCaps *) possible_caps);
253 factory->function = func;
254 factory->user_data = data;
259 /*** typefind function interface **********************************************/
262 * gst_type_find_peek:
263 * @find: the find object the function was called with
264 * @offset: the offset
265 * @size: the number of bytes to return
267 * Returns size bytes of the stream to identify beginning at offset. If offset
268 * is a positive number, the offset is relative to the beginning of the stream,
269 * if offset is a negative number the offset is relative to the end of the
270 * stream. The returned memory is valid until the typefinding function returns
271 * and must not be freed.
272 * If NULL is returned, that data is not available.
274 * Returns: the requested data or NULL if that data is not available.
277 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
279 g_return_val_if_fail (find->peek != NULL, NULL);
281 return find->peek (find->data, offset, size);
285 * gst_type_find_suggest:
286 * @find: the find object the function was called with
287 * @probability: the probability in percent that the suggestion is right
288 * @caps: the fixed caps to suggest
290 * If a typefind function calls this function it suggests the caps with the
291 * given probability. A typefind function may supply different suggestions
293 * It is up to the caller of the typefind function to interpret these values.
296 gst_type_find_suggest (GstTypeFind * find, guint probability,
297 const GstCaps * caps)
299 g_return_if_fail (find->suggest != NULL);
300 g_return_if_fail (probability <= 100);
301 g_return_if_fail (caps != NULL);
302 g_return_if_fail (gst_caps_is_fixed (caps));
304 find->suggest (find->data, probability, caps);
308 * gst_type_find_get_length:
309 * @find: the find object the function was called with
311 * Get the length of the data stream.
313 * Returns: the length of the data stream or 0 if it is not available.
316 gst_type_find_get_length (GstTypeFind * find)
318 if (find->get_length == NULL)
321 return find->get_length (find->data);