various style fixes
[platform/upstream/gstreamer.git] / gst / gsttypefindfactory.c
1 /* GStreamer
2  * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
3  *
4  * gsttypefindfactory.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:gsttypefindfactory
24  * @short_description: Information about registered typefind functions
25  *
26  * These functions allow querying informations about registered typefind
27  * functions. How to create and register these functions is described in
28  * the section <link linkend="gstreamer-Writing-typefind-functions">
29  * "Writing typefind functions"</link>.
30  *
31  * <example>
32  *   <title>how to write a simple typefinder</title>
33  *   <programlisting>
34  *   typedef struct {
35  *     guint8 *data;
36  *     guint size;
37  *     guint probability;
38  *     GstCaps *data;
39  *   } MyTypeFind;
40  *   static void
41  *   my_peek (gpointer data, gint64 offset, guint size)
42  *   {
43  *     MyTypeFind *find = (MyTypeFind *) data;
44  *     if (offset &gt;= 0 &amp;&amp; offset + size &lt;= find->size) {
45  *       return find->data + offset;
46  *     }
47  *     return NULL;
48  *   }
49  *   static void
50  *   my_suggest (gpointer data, guint probability, GstCaps *caps)
51  *   {
52  *     MyTypeFind *find = (MyTypeFind *) data;
53  *     if (probability &gt; find->probability) {
54  *       find->probability = probability;
55  *       gst_caps_replace (&amp;find->caps, caps);
56  *     }
57  *   }
58  *   static GstCaps *
59  *   find_type (guint8 *data, guint size)
60  *   {
61  *     GList *walk, *type_list;
62  *     MyTypeFind find = {data, size, 0, NULL};
63  *     GstTypeFind gst_find = {my_peek, my_suggest, &amp;find, };
64  *
65  *     walk = type_list = gst_type_find_factory_get_list ();
66  *     while (walk) {
67  *       GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);
68  *       walk = g_list_next (walk)
69  *       gst_type_find_factory_call_function (factory, &amp;gst_find);
70  *     }
71  *     g_list_free (type_list);
72  *     return find.caps;
73  *   };
74  *   </programlisting>
75  * </example>
76  *
77  * The above example shows how to write a very simple typefinder that
78  * identifies the given data. You can get quite a bit more complicated than
79  * that though.
80  */
81
82 #include "gst_private.h"
83 #include "gstinfo.h"
84 #include "gsttypefind.h"
85 #include "gsttypefindfactory.h"
86 #include "gstregistry.h"
87
88 GST_DEBUG_CATEGORY (gst_type_find_debug);
89 #define GST_CAT_DEFAULT gst_type_find_debug
90
91 static void gst_type_find_factory_class_init (gpointer g_class,
92     gpointer class_data);
93 static void gst_type_find_factory_init (GTypeInstance * instance,
94     gpointer g_class);
95 static void gst_type_find_factory_dispose (GObject * object);
96
97 static GstPluginFeatureClass *parent_class = NULL;
98
99 GType
100 gst_type_find_factory_get_type (void)
101 {
102   static GType typefind_type = 0;
103
104   if (!typefind_type) {
105     static const GTypeInfo typefind_info = {
106       sizeof (GstTypeFindFactoryClass),
107       NULL,
108       NULL,
109       gst_type_find_factory_class_init,
110       NULL,
111       NULL,
112       sizeof (GstTypeFindFactory),
113       0,
114       gst_type_find_factory_init,
115       NULL
116     };
117
118     typefind_type = g_type_register_static (GST_TYPE_PLUGIN_FEATURE,
119         "GstTypeFindFactory", &typefind_info, 0);
120     GST_DEBUG_CATEGORY_INIT (gst_type_find_debug, "GST_TYPEFIND",
121         GST_DEBUG_FG_GREEN, "typefinding subsystem");
122   }
123
124   return typefind_type;
125 }
126
127 static void
128 gst_type_find_factory_class_init (gpointer g_class, gpointer class_data)
129 {
130   GObjectClass *object_class = G_OBJECT_CLASS (g_class);
131
132   parent_class = g_type_class_peek_parent (g_class);
133
134   object_class->dispose = gst_type_find_factory_dispose;
135 }
136
137 static void
138 gst_type_find_factory_init (GTypeInstance * instance, gpointer g_class)
139 {
140   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (instance);
141
142   factory->user_data = factory;
143 }
144
145 static void
146 gst_type_find_factory_dispose (GObject * object)
147 {
148   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
149
150   if (factory->caps) {
151     gst_caps_unref (factory->caps);
152     factory->caps = NULL;
153   }
154   if (factory->extensions) {
155     g_strfreev (factory->extensions);
156     factory->extensions = NULL;
157   }
158 }
159
160 /**
161  * gst_type_find_factory_get_list:
162  *
163  * Gets the list of all registered typefind factories. You must free the
164  * list using g_list_free.
165  *
166  * Returns: the list of all registered typefind factories
167  */
168 GList *
169 gst_type_find_factory_get_list (void)
170 {
171   return gst_registry_get_feature_list (gst_registry_get_default (),
172       GST_TYPE_TYPE_FIND_FACTORY);
173 }
174
175 /**
176  * gst_type_find_factory_get_caps:
177  * @factory: a factory
178  *
179  * Gets the caps associated with a typefind factory.
180  *
181  * Returns: the #GstCaps associated with this factory
182  */
183 GstCaps *
184 gst_type_find_factory_get_caps (GstTypeFindFactory * factory)
185 {
186   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
187
188   return factory->caps;
189 }
190
191 /**
192  * gst_type_find_factory_get_extensions:
193  * @factory: a factory
194  *
195  * Gets the extensions associated with a typefind factory. The returned
196  * array should not be changed. If you need to change stuff in it, you should
197  * copy it using g_stdupv().  This function may return NULL to indicate
198  * a 0-length list.
199  *
200  * Returns: a NULL-terminated array of extensions associated with this factory
201  */
202 gchar **
203 gst_type_find_factory_get_extensions (GstTypeFindFactory * factory)
204 {
205   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
206
207   return factory->extensions;
208 }
209
210 /**
211  * gst_type_find_factory_call_function:
212  * @factory: a factory
213  * @find: a properly setup #GstTypeFind entry. The get_data and suggest_type
214  *        members must be set.
215  *
216  * Calls the typefinding function associated with this factory.
217  */
218 void
219 gst_type_find_factory_call_function (GstTypeFindFactory * factory,
220     GstTypeFind * find)
221 {
222   GstTypeFindFactory *new_factory;
223
224   g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
225   g_return_if_fail (find != NULL);
226   g_return_if_fail (find->peek != NULL);
227   g_return_if_fail (find->suggest != NULL);
228
229   new_factory =
230       GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
231           (factory)));
232   if (new_factory) {
233     g_assert (new_factory->function != NULL);
234
235     new_factory->function (find, new_factory->user_data);
236     gst_object_unref (new_factory);
237   }
238 }