2 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * gsttypefind.c: 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.
24 * @short_description: Stream type detection
26 * The following functions allow you to detect the media type of an unknown
29 * Last reviewed on 2005-11-09 (0.9.4)
32 #include "gst_private.h"
34 #include "gsttypefind.h"
35 #include "gstregistry.h"
36 #include "gsttypefindfactory.h"
38 GST_DEBUG_CATEGORY_EXTERN (gst_type_find_debug);
39 #define GST_CAT_DEFAULT gst_type_find_debug
42 * gst_type_find_register:
43 * @plugin: A #GstPlugin.
44 * @name: The name for registering
45 * @rank: The rank (or importance) of this typefind function
46 * @func: The #GstTypeFindFunction to use
47 * @extensions: Optional extensions that could belong to this type
48 * @possible_caps: Optionally the caps that could be returned when typefinding succeeds
49 * @data: Optional user data. This user data must be available until the plugin
52 * Registers a new typefind function to be used for typefinding. After
53 * registering this function will be available for typefinding.
54 * This function is typically called during an element's plugin initialization.
56 * Returns: TRUE on success, FALSE otherwise
59 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
60 GstTypeFindFunction func, gchar ** extensions,
61 const GstCaps * possible_caps, gpointer data)
63 GstTypeFindFactory *factory;
65 g_return_val_if_fail (plugin != NULL, FALSE);
66 g_return_val_if_fail (name != NULL, FALSE);
67 g_return_val_if_fail (func != NULL, FALSE);
69 GST_INFO ("registering typefind function for %s", name);
71 factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
72 GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
73 g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
74 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
76 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
77 if (factory->extensions)
78 g_strfreev (factory->extensions);
80 factory->extensions = g_strdupv (extensions);
81 gst_caps_replace (&factory->caps, (GstCaps *) possible_caps);
82 factory->function = func;
83 factory->user_data = data;
84 GST_PLUGIN_FEATURE (factory)->plugin_name = g_strdup (plugin->desc.name);
85 GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
87 gst_registry_add_feature (gst_registry_get_default (),
88 GST_PLUGIN_FEATURE (factory));
93 /*** typefind function interface **********************************************/
97 * @find: The #GstTypeFind object the function was called with
99 * @size: The number of bytes to return
101 * Returns the @size bytes of the stream to identify beginning at offset. If
102 * offset is a positive number, the offset is relative to the beginning of the
103 * stream, if offset is a negative number the offset is relative to the end of
104 * the stream. The returned memory is valid until the typefinding function
105 * returns and must not be freed.
107 * Returns: the requested data, or NULL if that data is not available.
110 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
112 g_return_val_if_fail (find->peek != NULL, NULL);
114 return find->peek (find->data, offset, size);
118 * gst_type_find_suggest:
119 * @find: The #GstTypeFind object the function was called with
120 * @probability: The probability in percent that the suggestion is right
121 * @caps: The fixed #GstCaps to suggest
123 * If a #GstTypeFindFunction calls this function it suggests the caps with the
124 * given probability. A #GstTypeFindFunction may supply different suggestions
126 * It is up to the caller of the #GstTypeFindFunction to interpret these values.
129 gst_type_find_suggest (GstTypeFind * find, guint probability,
130 const GstCaps * caps)
132 g_return_if_fail (find->suggest != NULL);
133 g_return_if_fail (probability <= 100);
134 g_return_if_fail (caps != NULL);
135 g_return_if_fail (gst_caps_is_fixed (caps));
137 find->suggest (find->data, probability, caps);
141 * gst_type_find_get_length:
142 * @find: The #GstTypeFind the function was called with
144 * Get the length of the data stream.
146 * Returns: The length of the data stream, or 0 if it is not available.
149 gst_type_find_get_length (GstTypeFind * find)
151 if (find->get_length == NULL)
154 return find->get_length (find->data);