gst/: splitted gsttypefind into gsttypefind, gsttypefindfactory
[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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 /**
22  * SECTION:gsttypefind
23  * @short_description: Stream type detection
24  *
25  */
26
27 #include "gst_private.h"
28 #include "gstinfo.h"
29 #include "gsttypefind.h"
30 #include "gsttypefindfactory.h"
31 #include "gstregistrypool.h"
32
33 GST_DEBUG_CATEGORY_EXTERN (gst_type_find_debug);
34 #define GST_CAT_DEFAULT gst_type_find_debug
35
36 /**
37  * gst_type_find_register:
38  * @plugin: the GstPlugin to register with
39  * @name: the name for registering
40  * @rank: rank (or importance) of this typefind function
41  * @func: the function to use for typefinding
42  * @extensions: optional extensions that could belong to this type
43  * @possible_caps: optionally the caps that could be returned when typefinding succeeds
44  * @data: optional user data. This user data must be available until the plugin 
45  *        is unloaded.
46  *
47  * Registers a new typefind function to be used for typefinding. After 
48  * registering this function will be available for typefinding.
49  * This function is typically called during an element's plugin initialization.
50  *
51  * Returns: TRUE on success, FALSE otherwise
52  */
53 gboolean
54 gst_type_find_register (GstPlugin * plugin, const gchar * name, guint rank,
55     GstTypeFindFunction func, gchar ** extensions,
56     const GstCaps * possible_caps, gpointer data)
57 {
58   GstTypeFindFactory *factory;
59
60   g_return_val_if_fail (plugin != NULL, FALSE);
61   g_return_val_if_fail (name != NULL, FALSE);
62   g_return_val_if_fail (func != NULL, FALSE);
63
64   GST_INFO ("registering typefind function for %s", name);
65   factory =
66       GST_TYPE_FIND_FACTORY (gst_registry_pool_find_feature (name,
67           GST_TYPE_TYPE_FIND_FACTORY));
68   if (!factory) {
69     factory = g_object_new (GST_TYPE_TYPE_FIND_FACTORY, NULL);
70     GST_DEBUG_OBJECT (factory, "using new typefind factory for %s", name);
71     g_assert (GST_IS_TYPE_FIND_FACTORY (factory));
72     gst_plugin_feature_set_name (GST_PLUGIN_FEATURE (factory), name);
73     gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
74   } else {
75     GST_DEBUG_OBJECT (factory, "using old typefind factory for %s", name);
76   }
77
78   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE (factory), rank);
79   if (factory->extensions)
80     g_strfreev (factory->extensions);
81
82   factory->extensions = g_strdupv (extensions);
83   gst_caps_replace (&factory->caps, (GstCaps *) possible_caps);
84   factory->function = func;
85   factory->user_data = data;
86
87   return TRUE;
88 }
89
90 /*** typefind function interface **********************************************/
91
92 /**
93  * gst_type_find_peek:
94  * @find: the find object the function was called with
95  * @offset: the offset
96  * @size: the number of bytes to return
97  *
98  * Returns size bytes of the stream to identify beginning at offset. If offset 
99  * is a positive number, the offset is relative to the beginning of the stream,
100  * if offset is a negative number the offset is relative to the end of the 
101  * stream. The returned memory is valid until the typefinding function returns
102  * and must not be freed.
103  * If NULL is returned, that data is not available.
104  *
105  * Returns: the requested data or NULL if that data is not available.
106  */
107 guint8 *
108 gst_type_find_peek (GstTypeFind * find, gint64 offset, guint size)
109 {
110   g_return_val_if_fail (find->peek != NULL, NULL);
111
112   return find->peek (find->data, offset, size);
113 }
114
115 /**
116  * gst_type_find_suggest:
117  * @find: the find object the function was called with
118  * @probability: the probability in percent that the suggestion is right
119  * @caps: the fixed caps to suggest
120  *
121  * If a typefind function calls this function it suggests the caps with the
122  * given probability. A typefind function may supply different suggestions
123  * in one call.
124  * It is up to the caller of the typefind function to interpret these values.
125  */
126 void
127 gst_type_find_suggest (GstTypeFind * find, guint probability,
128     const GstCaps * caps)
129 {
130   g_return_if_fail (find->suggest != NULL);
131   g_return_if_fail (probability <= 100);
132   g_return_if_fail (caps != NULL);
133   g_return_if_fail (gst_caps_is_fixed (caps));
134
135   find->suggest (find->data, probability, caps);
136 }
137
138 /**
139  * gst_type_find_get_length:
140  * @find: the find object the function was called with
141  *
142  * Get the length of the data stream.
143  * 
144  * Returns: the length of the data stream or 0 if it is not available.
145  */
146 guint64
147 gst_type_find_get_length (GstTypeFind * find)
148 {
149   if (find->get_length == NULL)
150     return 0;
151
152   return find->get_length (find->data);
153 }