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