Merge remote-tracking branch 'origin/master' into 0.11
[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  *     walk = type_list = gst_type_find_factory_get_list ();
65  *     while (walk) {
66  *       GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (walk->data);
67  *       walk = g_list_next (walk)
68  *       gst_type_find_factory_call_function (factory, &amp;gst_find);
69  *     }
70  *     g_list_free (type_list);
71  *     return find.caps;
72  *   };
73  *   </programlisting>
74  * </example>
75  *
76  * The above example shows how to write a very simple typefinder that
77  * identifies the given data. You can get quite a bit more complicated than
78  * that though.
79  *
80  * Last reviewed on 2005-11-09 (0.9.4)
81  */
82
83 #include "gst_private.h"
84 #include "gstinfo.h"
85 #include "gsttypefind.h"
86 #include "gsttypefindfactory.h"
87 #include "gstregistry.h"
88
89 GST_DEBUG_CATEGORY (type_find_debug);
90 #define GST_CAT_DEFAULT type_find_debug
91
92 static void gst_type_find_factory_dispose (GObject * object);
93
94 #define _do_init \
95 { \
96   GST_DEBUG_CATEGORY_INIT (type_find_debug, "GST_TYPEFIND", \
97       GST_DEBUG_FG_GREEN, "typefinding subsystem"); \
98 }
99
100 #define gst_type_find_factory_parent_class parent_class
101 G_DEFINE_TYPE_WITH_CODE (GstTypeFindFactory, gst_type_find_factory,
102     GST_TYPE_PLUGIN_FEATURE, _do_init);
103
104 static void
105 gst_type_find_factory_class_init (GstTypeFindFactoryClass * klass)
106 {
107   GObjectClass *object_class = G_OBJECT_CLASS (klass);
108
109   object_class->dispose = gst_type_find_factory_dispose;
110 }
111
112 static void
113 gst_type_find_factory_init (GstTypeFindFactory * factory)
114 {
115   factory->user_data = factory;
116   factory->user_data_notify = NULL;
117 }
118
119 static void
120 gst_type_find_factory_dispose (GObject * object)
121 {
122   GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (object);
123
124   if (factory->caps) {
125     gst_caps_unref (factory->caps);
126     factory->caps = NULL;
127   }
128   if (factory->extensions) {
129     g_strfreev (factory->extensions);
130     factory->extensions = NULL;
131   }
132   if (factory->user_data_notify && factory->user_data) {
133     factory->user_data_notify (factory->user_data);
134     factory->user_data = NULL;
135   }
136
137   G_OBJECT_CLASS (parent_class)->dispose (object);
138 }
139
140 /**
141  * gst_type_find_factory_get_list:
142  *
143  * Gets the list of all registered typefind factories. You must free the
144  * list using gst_plugin_feature_list_free().
145  *
146  * The returned factories are sorted by highest rank first, and then by
147  * factory name. (behaviour change since 0.10.26)
148  *
149  * Free-function: gst_plugin_feature_list_free
150  *
151  * Returns: (transfer full) (element-type Gst.TypeFindFactory): the list of all
152  *     registered #GstTypeFindFactory.
153  */
154 GList *
155 gst_type_find_factory_get_list (void)
156 {
157   return gst_registry_get_feature_list (gst_registry_get_default (),
158       GST_TYPE_TYPE_FIND_FACTORY);
159 }
160
161 /**
162  * gst_type_find_factory_get_caps:
163  * @factory: A #GstTypeFindFactory
164  *
165  * Gets the #GstCaps associated with a typefind factory.
166  *
167  * Returns: (transfer none): the #GstCaps associated with this factory
168  */
169 GstCaps *
170 gst_type_find_factory_get_caps (GstTypeFindFactory * factory)
171 {
172   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
173
174   return factory->caps;
175 }
176
177 /**
178  * gst_type_find_factory_get_extensions:
179  * @factory: A #GstTypeFindFactory
180  *
181  * Gets the extensions associated with a #GstTypeFindFactory. The returned
182  * array should not be changed. If you need to change stuff in it, you should
183  * copy it using g_strdupv().  This function may return NULL to indicate
184  * a 0-length list.
185  *
186  * Returns: (transfer none) (array zero-terminated=1) (element-type utf8): a
187  *     NULL-terminated array of extensions associated with this factory
188  */
189 gchar **
190 gst_type_find_factory_get_extensions (GstTypeFindFactory * factory)
191 {
192   g_return_val_if_fail (GST_IS_TYPE_FIND_FACTORY (factory), NULL);
193
194   return factory->extensions;
195 }
196
197 /**
198  * gst_type_find_factory_call_function:
199  * @factory: A #GstTypeFindFactory
200  * @find: (transfer none): a properly setup #GstTypeFind entry. The get_data
201  *     and suggest_type members must be set.
202  *
203  * Calls the #GstTypeFindFunction associated with this factory.
204  */
205 void
206 gst_type_find_factory_call_function (GstTypeFindFactory * factory,
207     GstTypeFind * find)
208 {
209   GstTypeFindFactory *new_factory;
210
211   g_return_if_fail (GST_IS_TYPE_FIND_FACTORY (factory));
212   g_return_if_fail (find != NULL);
213   g_return_if_fail (find->peek != NULL);
214   g_return_if_fail (find->suggest != NULL);
215
216   new_factory =
217       GST_TYPE_FIND_FACTORY (gst_plugin_feature_load (GST_PLUGIN_FEATURE
218           (factory)));
219   if (new_factory) {
220     if (new_factory->function)
221       new_factory->function (find, new_factory->user_data);
222     gst_object_unref (new_factory);
223   }
224 }