Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / mobile / gst / gsttypefind.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefind.c: 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 /**
23  * SECTION:gsttypefind
24  * @short_description: Stream type detection
25  *
26  * The following functions allow you to detect the media type of an unknown
27  * stream.
28  *
29  * Last reviewed on 2005-11-09 (0.9.4)
30  */
31
32 #include "gst_private.h"
33 #include "gstinfo.h"
34 #include "gsttypefind.h"
35 #include "gstregistry.h"
36 #include "gsttypefindfactory.h"
37
38 GST_DEBUG_CATEGORY_EXTERN (type_find_debug);
39 #define GST_CAT_DEFAULT type_find_debug
40
41 GType
42 gst_type_find_get_type (void)
43 {
44   static GType typefind_type = 0;
45
46   if (G_UNLIKELY (typefind_type == 0)) {
47     typefind_type = g_pointer_type_register_static ("GstTypeFind");
48   }
49   return typefind_type;
50 }
51
52 /**
53  * gst_type_find_register:
54  * @plugin: A #GstPlugin, or NULL for a static typefind function (note that
55  *    passing NULL only works in GStreamer 0.10.16 and later)
56  * @name: The name for registering
57  * @rank: The rank (or importance) of this typefind function
58  * @func: The #GstTypeFindFunction to use
59  * @extensions: (transfer none) (array zero-terminated=1) (element-type utf8):
60  *     Optional extensions that could belong to this type
61  * @possible_caps: Optionally the caps that could be returned when typefinding
62  *                 succeeds
63  * @data: Optional user data. This user data must be available until the plugin
64  *        is unloaded.
65  * @data_notify: a #GDestroyNotify that will be called on @data when the plugin
66  *        is unloaded.
67  *
68  * Registers a new typefind function to be used for typefinding. After
69  * registering this function will be available for typefinding.
70  * This function is typically called during an element's plugin initialization.
71  *
72  * Returns: TRUE on success, FALSE otherwise
73  */
74 gboolean
75 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
76     GstTypeFindFunction func, gchar ** extensions,
77     const GstCaps * possible_caps, gpointer data, GDestroyNotify data_notify)
78 {
79   GstTypeFindFactory *factory;
80
81   g_return_val_if_fail (name != NULL, FALSE);
82
83   GST_INFO ("registering typefind function for %s", name);
84
85   factory = g_object_newv (GST_TYPE_TYPE_FIND_FACTORY, 0, NULL);
86   GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
87   g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
88
89   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
90   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory), rank);
91
92   if (factory->extensions)
93     g_strfreev (factory->extensions);
94   factory->extensions = g_strdupv (extensions);
95
96   gst_caps_replace (&factory->caps, (GstCaps *) possible_caps);
97   factory->function = func;
98   factory->user_data = data;
99   factory->user_data_notify = data_notify;
100   if (plugin && plugin->desc.name) {
101     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
102     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
103     g_object_add_weak_pointer ((GObject *) plugin,
104         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
105   } else {
106     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
107     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
108   }
109   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
110
111   gst_registry_add_feature (gst_registry_get_default (),
112       GST_PLUGIN_FEATURE_CAST (factory));
113
114   return TRUE;
115 }
116
117 /*** typefind function interface **********************************************/
118
119 /**
120  * gst_type_find_peek:
121  * @find: The #GstTypeFind object the function was called with
122  * @offset: The offset
123  * @size: The number of bytes to return
124  *
125  * Returns the @size bytes of the stream to identify beginning at offset. If
126  * offset is a positive number, the offset is relative to the beginning of the
127  * stream, if offset is a negative number the offset is relative to the end of
128  * the stream. The returned memory is valid until the typefinding function
129  * returns and must not be freed.
130  *
131  * Returns: (transfer none) (array length=size): the requested data, or NULL
132  *     if that data is not available.
133  */
134 guint8 *
135 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
136 {
137   g_return_val_if_fail (find->peek != NULL, NULL);
138
139   return find->peek (find->data, offset, size);
140 }
141
142 /**
143  * gst_type_find_suggest:
144  * @find: The #GstTypeFind object the function was called with
145  * @probability: The probability in percent that the suggestion is right
146  * @caps: The fixed #GstCaps to suggest
147  *
148  * If a #GstTypeFindFunction calls this function it suggests the caps with the
149  * given probability. A #GstTypeFindFunction may supply different suggestions
150  * in one call.
151  * It is up to the caller of the #GstTypeFindFunction to interpret these values.
152  */
153 void
154 gst_type_find_suggest (GstTypeFind * find, guint probability,
155     const GstCaps * caps)
156 {
157   g_return_if_fail (find->suggest != NULL);
158   g_return_if_fail (probability <= 100);
159   g_return_if_fail (caps != NULL);
160   g_return_if_fail (gst_caps_is_fixed (caps));
161
162   find->suggest (find->data, probability, caps);
163 }
164
165 /**
166  * gst_type_find_suggest_simple:
167  * @find: The #GstTypeFind object the function was called with
168  * @probability: The probability in percent that the suggestion is right
169  * @media_type: the media type of the suggested caps
170  * @fieldname: first field of the suggested caps, or NULL
171  * @...: additional arguments to the suggested caps in the same format as the
172  *     arguments passed to gst_structure_new() (ie. triplets of field name,
173  *     field GType and field value)
174  *
175  * If a #GstTypeFindFunction calls this function it suggests the caps with the
176  * given probability. A #GstTypeFindFunction may supply different suggestions
177  * in one call. It is up to the caller of the #GstTypeFindFunction to interpret
178  * these values.
179  *
180  * This function is similar to gst_type_find_suggest(), only that instead of
181  * passing a #GstCaps argument you can create the caps on the fly in the same
182  * way as you can with gst_caps_new_simple().
183  *
184  * Make sure you terminate the list of arguments with a NULL argument and that
185  * the values passed have the correct type (in terms of width in bytes when
186  * passed to the vararg function - this applies particularly to gdouble and
187  * guint64 arguments).
188  *
189  * Since: 0.10.20
190  */
191 void
192 gst_type_find_suggest_simple (GstTypeFind * find, guint probability,
193     const char *media_type, const char *fieldname, ...)
194 {
195   GstStructure *structure;
196   va_list var_args;
197   GstCaps *caps;
198
199   g_return_if_fail (find->suggest != NULL);
200   g_return_if_fail (probability <= 100);
201   g_return_if_fail (media_type != NULL);
202
203   caps = gst_caps_new_empty ();
204
205   va_start (var_args, fieldname);
206   structure = gst_structure_new_valist (media_type, fieldname, var_args);
207   va_end (var_args);
208
209   gst_caps_append_structure (caps, structure);
210   g_return_if_fail (gst_caps_is_fixed (caps));
211
212   find->suggest (find->data, probability, caps);
213   gst_caps_unref (caps);
214 }
215
216 /**
217  * gst_type_find_get_length:
218  * @find: The #GstTypeFind the function was called with
219  *
220  * Get the length of the data stream.
221  *
222  * Returns: The length of the data stream, or 0 if it is not available.
223  */
224 guint64
225 gst_type_find_get_length (GstTypeFind * find)
226 {
227   if (find->get_length == NULL)
228     return 0;
229
230   return find->get_length (find->data);
231 }