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.
23 #include "gsttypefind.h"
24 #include "gstregistrypool.h"
26 GST_DEBUG_CATEGORY_STATIC (gst_type_find_debug);
27 #define GST_CAT_DEFAULT gst_type_find_debug
29 static void gst_type_find_factory_class_init (gpointer g_class,
31 static void gst_type_find_factory_init (GTypeInstance * instance,
33 static void gst_type_find_factory_dispose (GObject * object);
35 static void gst_type_find_factory_unload_thyself (GstPluginFeature * feature);
37 static void gst_type_find_load_plugin (GstTypeFind * find,
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,
60 typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
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 = GST_PLUGIN_FEATURE_CLASS (g_class);
73 GObjectClass *object_class = G_OBJECT_CLASS (g_class);
75 parent_class = g_type_class_peek_parent (g_class);
77 object_class->dispose = gst_type_find_factory_dispose;
79 gstpluginfeature_class->unload_thyself = GST_DEBUG_FUNCPTR (gst_type_find_factory_unload_thyself);
82 gst_type_find_factory_init (GTypeInstance *instance, gpointer g_class)
84 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
86 factory->user_data = factory;
87 factory->function = gst_type_find_load_plugin;
90 gst_type_find_factory_dispose (GObject *object)
92 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
95 gst_caps_unref (factory->caps);
98 if (factory->extensions) {
99 g_strfreev (factory->extensions);
100 factory->extensions = NULL;
104 gst_type_find_factory_unload_thyself (GstPluginFeature *feature)
106 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
108 factory->function = gst_type_find_load_plugin;
109 factory->user_data = factory;
112 gst_type_find_load_plugin (GstTypeFind *find, gpointer data)
114 GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (data);
116 GST_DEBUG_OBJECT (factory, "need to load typefind function %s", GST_PLUGIN_FEATURE_NAME (factory));
118 if (gst_plugin_feature_ensure_loaded (GST_PLUGIN_FEATURE (factory))) {
119 if (factory->function == gst_type_find_load_plugin) {
120 /* looks like we didn't get a real typefind function */
121 g_warning ("could not load valid typefind function for feature '%s'\n", GST_PLUGIN_FEATURE_NAME (factory));
123 g_assert (factory->function);
124 gst_type_find_factory_call_function (factory, find);
129 * gst_type_find_factory_get_list:
131 * Gets the list of all registered typefind factories. You must free the
132 * list using g_list_free.
134 * Returns: the list of all registered typefind factories
137 gst_type_find_factory_get_list (void)
139 return gst_registry_pool_feature_list (GST_TYPE_TYPE_FIND_FACTORY);
142 * gst_type_find_factory_get_caps:
143 * @factory: a factory
145 * Gets the caps associated with a typefind factory.
147 * Returns: the #GstCaps associated with this factory
150 gst_type_find_factory_get_caps (const GstTypeFindFactory *factory)
152 g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
154 return factory->caps;
157 * gst_type_find_factory_get_extensions:
158 * @factory: a factory
160 * Gets the extensions associated with a typefind factory. The returned
161 * array should not be changed. If you need to change stuff in it, you should
162 * copy it using g_stdupv().
164 * Returns: a NULL-terminated array of extensions associated with this factory
167 gst_type_find_factory_get_extensions (const GstTypeFindFactory *factory)
169 g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
171 return factory->extensions;
174 * gst_type_find_factory_call_function:
175 * @factory: a factory
176 * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
177 * members must be set.
179 * Calls the typefinding function associated with this factory.
182 gst_type_find_factory_call_function (const GstTypeFindFactory *factory, GstTypeFind *find)
184 g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
185 g_return_if_fail (find != NULL);
186 g_return_if_fail (find->peek != NULL);
187 g_return_if_fail (find->suggest != NULL);
189 /* should never happen */
190 g_assert (factory->function != NULL);
192 factory->function (find, factory->user_data);
195 * gst_type_find_factory_register:
196 * @plugin: the GstPlugin to register with
197 * @name: the name for registering
198 * @rank: rank (or importance) of this typefind function
199 * @func: the function to use for typefinding
200 * @extensions: optional extensions that could belong to this type
201 * @possible_caps: optionally the caps that could be returned when typefinding succeeds
202 * @data: optional user data. This user data must be available until the plugin
205 * Registers a new typefind function to be used for typefinding. After
206 * registering this function will be available for typefinding.
207 * This function is typically called during an element's plugin initialization.
209 * Returns: TRUE on success, FALSE otherwise
212 gst_type_find_factory_register (GstPlugin *plugin, const gchar *name, guint rank,
213 GstTypeFindFunction func, gchar **extensions, GstCaps *possible_caps,
216 GstTypeFindFactory *factory;
218 g_return_if_fail (plugin != NULL);
219 g_return_if_fail (name != NULL);
220 g_return_if_fail (func != NULL);
222 GST_INFO ("registering typefind function for %s", name);
223 factory = GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name, GST_TYPE_TYPE_FIND_FACTORY));
225 factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
226 GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
227 g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
228 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
230 GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
233 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
234 if (factory->extensions)
235 g_strfreev (factory->extensions);
237 factory->extensions = g_strdupv (extensions);
238 gst_caps_replace (&factory->caps, possible_caps);
239 factory->function = func;
240 factory->user_data = data;
242 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
245 /*** typefind function interface **********************************************/
248 * gst_type_find_peek:
249 * @find: the find object the function was called with
250 * @offset: the offset
251 * @size: the number of bytes to return
253 * Returns size bytes of the stream to identify beginning at offset. If offset
254 * is a positive number, the offset is relative to the beginning of the stream,
255 * if offset is a negative number the offset is relative to the end of the
256 * stream. The returned memory is valid until the typefinding function returns
257 * and must not be freed.
258 * If NULL is returned, that data is not available.
260 * Returns: the requested data or NULL if that data is not available.
263 gst_type_find_peek (GstTypeFind *find, gint64 offset, guint size)
265 g_return_val_if_fail (find->peek != NULL, NULL);
267 return find->peek (find->data, offset, size);
270 * gst_type_find_suggest:
271 * @find: the find object the function was called with
272 * @probability: the probability in percent that the suggestion is right
273 * @caps: the fixed caps to suggest
275 * If a typefind function calls this function it suggests the caps with the
276 * given probability. A typefind function may supply different suggestions
278 * It is up to the caller of the typefind function to interpret these values.
281 gst_type_find_suggest (GstTypeFind *find, guint probability, GstCaps *caps)
283 g_return_if_fail (find->suggest != NULL);
284 g_return_if_fail (probability <= 100);
285 g_return_if_fail (caps != NULL);
286 g_return_if_fail (GST_CAPS_IS_FIXED (caps));
289 gst_caps_sink (caps);
290 find->suggest (find->data, probability, caps);
291 gst_caps_unref (caps);
294 * gst_type_find_get_length:
295 * @find: the find object the function was called with
297 * Get the length of the data stream.
299 * Returns: the length of the data stream or 0 if it is not available.
302 gst_type_find_get_length (GstTypeFind *find)
304 if (find->get_length == NULL)
307 return find->get_length(find->data);