docs: convert the examples to use gtk-doc markup, instead of docbook
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, 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  * The following example shows how to write a very simple typefinder that
32  * identifies the given data. You can get quite a bit more complicated than
33  * that though.
34  * |[
35  *   typedef struct {
36  *     guint8 *data;
37  *     guint size;
38  *     guint probability;
39  *     GstCaps *data;
40  *   } MyTypeFind;
41  *   static void
42  *   my_peek (gpointer data, gint64 offset, guint size)
43  *   {
44  *     MyTypeFind *find = (MyTypeFind *) data;
45  *     if (offset &gt;= 0 &amp;&amp; offset + size &lt;= find->size) {
46  *       return find->data + offset;
47  *     }
48  *     return NULL;
49  *   }
50  *   static void
51  *   my_suggest (gpointer data, guint probability, GstCaps *caps)
52  *   {
53  *     MyTypeFind *find = (MyTypeFind *) data;
54  *     if (probability &gt; find->probability) {
55  *       find->probability = probability;
56  *       gst_caps_replace (&amp;find->caps, caps);
57  *     }
58  *   }
59  *   static GstCaps *
60  *   find_type (guint8 *data, guint size)
61  *   {
62  *     GList *walk, *type_list;
63  *     MyTypeFind find = {data, size, 0, NULL};
64  *     GstTypeFind gst_find = {my_peek, my_suggest, &amp;find, };
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  * ]|
75  *
76  * Last reviewed on 2005-11-09 (0.9.4)
77  */
78
79 #include "gst_private.h"
80 #include "gstinfo.h"
81 #include "gsttypefind.h"
82 #include "gsttypefindfactory.h"
83 #include "gstregistry.h"
84
85 GST_DEBUG_CATEGORY (type_find_debug);
86 #define GST_CAT_DEFAULT type_find_debug
87
88 static void gst_type_find_factory_dispose (GObject * object);
89
90 #define _do_init \
91 { \
92   GST_DEBUG_CATEGORY_INIT (type_find_debug, "GST_TYPEFIND", \
93       GST_DEBUG_FG_GREEN, "typefinding subsystem"); \
94 }
95
96 #define gst_type_find_factory_parent_class parent_class
97 G_DEFINE_TYPE_WITH_CODE (GstTypeFindFactory, gst_type_find_factory,
98     GST_TYPE_PLUGIN_FEATURE, _do_init);
99
100 static void
101 gst_type_find_factory_class_init (GstTypeFindFactoryClass * klass)
102 {
103   GObjectClass *object_class = G_OBJECT_CLASS (klass);
104
105   object_class->dispose = gst_type_find_factory_dispose;
106 }
107
108 static void
109 gst_type_find_factory_init (GstTypeFindFactory * factory)
110 {
111   factory->user_data = factory;
112   factory->user_data_notify = NULL;
113 }
114
115 static void
116 gst_type_find_factory_dispose (GObject * object)
117 {
118   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
119
120   if (factory->caps) {
121     gst_caps_unref (factory->caps);
122     factory->caps = NULL;
123   }
124   if (factory->extensions) {
125     g_strfreev (factory->extensions);
126     factory->extensions = NULL;
127   }
128   if (factory->user_data_notify && factory->user_data) {
129     factory->user_data_notify (factory->user_data);
130     factory->user_data = NULL;
131   }
132
133   G_OBJECT_CLASS (parent_class)->dispose (object);
134 }
135
136 /**
137  * gst_type_find_factory_get_list:
138  *
139  * Gets the list of all registered typefind factories. You must free the
140  * list using gst_plugin_feature_list_free().
141  *
142  * The returned factories are sorted by highest rank first, and then by
143  * factory name.
144  *
145  * Free-function: gst_plugin_feature_list_free
146  *
147  * Returns: (transfer full) (element-type Gst.TypeFindFactory): the list of all
148  *     registered #GstTypeFindFactory.
149  */
150 GList *
151 gst_type_find_factory_get_list (void)
152 {
153   return gst_registry_get_feature_list (gst_registry_get (),
154       GST_TYPE_TYPE_FIND_FACTORY);
155 }
156
157 /**
158  * gst_type_find_factory_get_caps:
159  * @factory: A #GstTypeFindFactory
160  *
161  * Gets the #GstCaps associated with a typefind factory.
162  *
163  * Returns: (transfer none): the #GstCaps associated with this factory
164  */
165 GstCaps *
166 gst_type_find_factory_get_caps (GstTypeFindFactory * factory)
167 {
168   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
169
170   return factory->caps;
171 }
172
173 /**
174  * gst_type_find_factory_get_extensions:
175  * @factory: A #GstTypeFindFactory
176  *
177  * Gets the extensions associated with a #GstTypeFindFactory. The returned
178  * array should not be changed. If you need to change stuff in it, you should
179  * copy it using g_strdupv().  This function may return NULL to indicate
180  * a 0-length list.
181  *
182  * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): a
183  *     NULL-terminated array of extensions associated with this factory
184  */
185 const gchar *const *
186 gst_type_find_factory_get_extensions (GstTypeFindFactory * factory)
187 {
188   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
189
190   return (const gchar * const *) factory->extensions;
191 }
192
193 /**
194  * gst_type_find_factory_call_function:
195  * @factory: A #GstTypeFindFactory
196  * @find: (transfer none): a properly setup #GstTypeFind entry. The get_data
197  *     and suggest_type members must be set.
198  *
199  * Calls the #GstTypeFindFunction associated with this factory.
200  */
201 void
202 gst_type_find_factory_call_function (GstTypeFindFactory * factory,
203     GstTypeFind * find)
204 {
205   GstTypeFindFactory *new_factory;
206
207   g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
208   g_return_if_fail (find != NULL);
209   g_return_if_fail (find->peek != NULL);
210   g_return_if_fail (find->suggest != NULL);
211
212   new_factory =
213       GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
214           (factory)));
215   if (new_factory) {
216     if (new_factory->function)
217       new_factory->function (find, new_factory->user_data);
218     gst_object_unref (new_factory);
219   }
220 }
221
222 /**
223  * gst_type_find_factory_has_function:
224  * @factory: A #GstTypeFindFactory
225  *
226  * Check whether the factory has a typefind function. Typefind factories
227  * without typefind functions are a last-effort fallback mechanism to
228  * e.g. assume a certain media type based on the file extension.
229  *
230  * Returns: TRUE if the factory has a typefind functions set, otherwise FALSE
231  */
232 gboolean
233 gst_type_find_factory_has_function (GstTypeFindFactory * factory)
234 {
235   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), FALSE);
236
237   return (factory->function != NULL);
238 }