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 (type_find_debug);
39 #define GST_CAT_DEFAULT type_find_debug
41 G_DEFINE_POINTER_TYPE (GstTypeFind, gst_type_find);
44 * gst_type_find_register:
45 * @plugin: A #GstPlugin, or NULL for a static typefind function (note that
46 * passing NULL only works in GStreamer 0.10.16 and later)
47 * @name: The name for registering
48 * @rank: The rank (or importance) of this typefind function
49 * @func: The #GstTypeFindFunction to use
50 * @extensions: (allow-none): Optional comma-separated list of extensions
51 * that could belong to this type
52 * @possible_caps: Optionally the caps that could be returned when typefinding
54 * @data: Optional user data. This user data must be available until the plugin
56 * @data_notify: a #GDestroyNotify that will be called on @data when the plugin
59 * Registers a new typefind function to be used for typefinding. After
60 * registering this function will be available for typefinding.
61 * This function is typically called during an element's plugin initialization.
63 * Returns: TRUE on success, FALSE otherwise
66 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
67 GstTypeFindFunction func, const gchar * extensions,
68 GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
70 GstTypeFindFactory *factory;
72 g_return_val_if_fail (name != NULL, FALSE);
74 GST_INFO ("registering typefind function for %s", name);
76 factory = g_object_newv (GST_TYPE_TYPE_FIND_FACTORY, 0, NULL);
77 GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
78 g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
80 gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
81 gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
83 if (factory->extensions) {
84 g_strfreev (factory->extensions);
85 factory->extensions = NULL;
88 factory->extensions = g_strsplit (extensions, ",", -1);
90 gst_caps_replace (&factory->caps, possible_caps);
91 factory->function = func;
92 factory->user_data = data;
93 factory->user_data_notify = data_notify;
94 if (plugin && plugin->desc.name) {
95 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
96 GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
97 g_object_add_weak_pointer ((GObject *) plugin,
98 (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
100 GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
101 GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
103 GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
105 gst_registry_add_feature (gst_registry_get (),
106 GST_PLUGIN_FEATURE_CAST (factory));
111 /*** typefind function interface **********************************************/
114 * gst_type_find_peek:
115 * @find: The #GstTypeFind object the function was called with
116 * @offset: The offset
117 * @size: The number of bytes to return
119 * Returns the @size bytes of the stream to identify beginning at offset. If
120 * offset is a positive number, the offset is relative to the beginning of the
121 * stream, if offset is a negative number the offset is relative to the end of
122 * the stream. The returned memory is valid until the typefinding function
123 * returns and must not be freed.
125 * Returns: (transfer none) (array length=size): the requested data, or NULL
126 * if that data is not available.
129 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
131 g_return_val_if_fail (find->peek != NULL, NULL);
133 return find->peek (find->data, offset, size);
137 * gst_type_find_suggest:
138 * @find: The #GstTypeFind object the function was called with
139 * @probability: The probability in percent that the suggestion is right
140 * @caps: The fixed #GstCaps to suggest
142 * If a #GstTypeFindFunction calls this function it suggests the caps with the
143 * given probability. A #GstTypeFindFunction may supply different suggestions
145 * It is up to the caller of the #GstTypeFindFunction to interpret these values.
148 gst_type_find_suggest (GstTypeFind * find, guint probability, GstCaps * caps)
150 g_return_if_fail (find->suggest != NULL);
151 g_return_if_fail (probability <= 100);
152 g_return_if_fail (caps != NULL);
153 g_return_if_fail (gst_caps_is_fixed (caps));
155 find->suggest (find->data, probability, caps);
159 * gst_type_find_suggest_simple:
160 * @find: The #GstTypeFind object the function was called with
161 * @probability: The probability in percent that the suggestion is right
162 * @media_type: the media type of the suggested caps
163 * @fieldname: first field of the suggested caps, or NULL
164 * @...: additional arguments to the suggested caps in the same format as the
165 * arguments passed to gst_structure_new() (ie. triplets of field name,
166 * field GType and field value)
168 * If a #GstTypeFindFunction calls this function it suggests the caps with the
169 * given probability. A #GstTypeFindFunction may supply different suggestions
170 * in one call. It is up to the caller of the #GstTypeFindFunction to interpret
173 * This function is similar to gst_type_find_suggest(), only that instead of
174 * passing a #GstCaps argument you can create the caps on the fly in the same
175 * way as you can with gst_caps_new_simple().
177 * Make sure you terminate the list of arguments with a NULL argument and that
178 * the values passed have the correct type (in terms of width in bytes when
179 * passed to the vararg function - this applies particularly to gdouble and
180 * guint64 arguments).
185 gst_type_find_suggest_simple (GstTypeFind * find, guint probability,
186 const char *media_type, const char *fieldname, ...)
188 GstStructure *structure;
192 g_return_if_fail (find->suggest != NULL);
193 g_return_if_fail (probability <= 100);
194 g_return_if_fail (media_type != NULL);
196 caps = gst_caps_new_empty ();
198 va_start (var_args, fieldname);
199 structure = gst_structure_new_valist (media_type, fieldname, var_args);
202 gst_caps_append_structure (caps, structure);
203 g_return_if_fail (gst_caps_is_fixed (caps));
205 find->suggest (find->data, probability, caps);
206 gst_caps_unref (caps);
210 * gst_type_find_get_length:
211 * @find: The #GstTypeFind the function was called with
213 * Get the length of the data stream.
215 * Returns: The length of the data stream, or 0 if it is not available.
218 gst_type_find_get_length (GstTypeFind * find)
220 if (find->get_length == NULL)
223 return find->get_length (find->data);