gst/gstformat.c: Fix locking order (must take lock before using n_values).
[platform/upstream/gstreamer.git] / gst / gstformat.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstformat.c: GstFormat registration
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstformat
26  * @short_description: Dynamically register new data formats
27  * @see_also: #GstPad, #GstElement
28  *
29  * GstFormats functions are used to register a new format to the gstreamer
30  * core.  Formats can be used to perform seeking or conversions/query
31  * operations.
32  */
33
34
35 #include "gst_private.h"
36 #include <string.h>
37 #include "gstformat.h"
38 #include "gstenumtypes.h"
39
40 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
41 static GList *_gst_formats = NULL;
42 static GHashTable *_nick_to_format = NULL;
43 static GHashTable *_format_to_nick = NULL;
44 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
45
46 static GstFormatDefinition standard_definitions[] = {
47   {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
48   {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
49   {GST_FORMAT_TIME, "time", "Time", 0},
50   {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
51   {GST_FORMAT_PERCENT, "percent", "Percent", 0},
52   {0, NULL, NULL, 0}
53 };
54
55 void
56 _gst_format_initialize (void)
57 {
58   GstFormatDefinition *standards = standard_definitions;
59
60   g_static_mutex_lock (&mutex);
61   if (_nick_to_format == NULL) {
62     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
63     _format_to_nick = g_hash_table_new (NULL, NULL);
64   }
65
66   while (standards->nick) {
67     standards->quark = g_quark_from_static_string (standards->nick);
68     g_hash_table_insert (_nick_to_format, standards->nick, standards);
69     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
70         standards);
71
72     _gst_formats = g_list_append (_gst_formats, standards);
73     standards++;
74     _n_values++;
75   }
76   /* getting the type registers the enum */
77   gst_format_get_type ();
78   g_static_mutex_unlock (&mutex);
79 }
80
81 /**
82  * gst_format_get_name:
83  * @format: a #GstFormat
84  *
85  * Get a printable name for the given format. Do not modify or free.
86  *
87  * Returns: a reference to the static name of the format or NULL if
88  * the format is unknown.
89  */
90 const gchar *
91 gst_format_get_name (GstFormat format)
92 {
93   const GstFormatDefinition *def;
94   const gchar *result;
95
96   if ((def = gst_format_get_details (format)) != NULL)
97     result = def->nick;
98   else
99     result = NULL;
100
101   return result;
102 }
103
104 /**
105  * gst_format_to_quark:
106  * @format: a #GstFormat
107  *
108  * Get the unique quark for the given format.
109  *
110  * Returns: the quark associated with the format or 0 if the format
111  * is unknown.
112  */
113 GQuark
114 gst_format_to_quark (GstFormat format)
115 {
116   const GstFormatDefinition *def;
117   GQuark result;
118
119   if ((def = gst_format_get_details (format)) != NULL)
120     result = def->quark;
121   else
122     result = 0;
123
124   return result;
125 }
126
127 /**
128  * gst_format_register:
129  * @nick: The nick of the new format
130  * @description: The description of the new format
131  *
132  * Create a new GstFormat based on the nick or return an
133  * already registered format with that nick.
134  *
135  * Returns: A new GstFormat or an already registered format
136  * with the same nick.
137  *
138  * MT safe.
139  */
140 GstFormat
141 gst_format_register (const gchar * nick, const gchar * description)
142 {
143   GstFormatDefinition *format;
144   GstFormat query;
145
146   g_return_val_if_fail (nick != NULL, 0);
147   g_return_val_if_fail (description != NULL, 0);
148
149   query = gst_format_get_by_nick (nick);
150   if (query != GST_FORMAT_UNDEFINED)
151     return query;
152
153   g_static_mutex_lock (&mutex);
154   format = g_new0 (GstFormatDefinition, 1);
155   format->value = _n_values;
156   format->nick = g_strdup (nick);
157   format->description = g_strdup (description);
158   format->quark = g_quark_from_static_string (format->nick);
159
160   g_hash_table_insert (_nick_to_format, format->nick, format);
161   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
162       format);
163   _gst_formats = g_list_append (_gst_formats, format);
164   _n_values++;
165   g_static_mutex_unlock (&mutex);
166
167   return format->value;
168 }
169
170 /**
171  * gst_format_get_by_nick:
172  * @nick: The nick of the format
173  *
174  * Return the format registered with the given nick.
175  *
176  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
177  * if the format was not registered.
178  */
179 GstFormat
180 gst_format_get_by_nick (const gchar * nick)
181 {
182   GstFormatDefinition *format;
183
184   g_return_val_if_fail (nick != NULL, 0);
185
186   g_static_mutex_lock (&mutex);
187   format = g_hash_table_lookup (_nick_to_format, nick);
188   g_static_mutex_unlock (&mutex);
189
190   if (format != NULL)
191     return format->value;
192   else
193     return GST_FORMAT_UNDEFINED;
194 }
195
196 /**
197  * gst_formats_contains:
198  * @formats: The format array to search
199  * @format: the format to find
200  *
201  * See if the given format is inside the format array.
202  *
203  * Returns: TRUE if the format is found inside the array
204  */
205 gboolean
206 gst_formats_contains (const GstFormat * formats, GstFormat format)
207 {
208   if (!formats)
209     return FALSE;
210
211   while (*formats) {
212     if (*formats == format)
213       return TRUE;
214
215     formats++;
216   }
217   return FALSE;
218 }
219
220
221 /**
222  * gst_format_get_details:
223  * @format: The format to get details of
224  *
225  * Get details about the given format.
226  *
227  * Returns: The #GstFormatDefinition for @format or NULL on failure.
228  *
229  * MT safe.
230  */
231 const GstFormatDefinition *
232 gst_format_get_details (GstFormat format)
233 {
234   const GstFormatDefinition *result;
235
236   g_static_mutex_lock (&mutex);
237   result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
238   g_static_mutex_unlock (&mutex);
239
240   return result;
241 }
242
243 /**
244  * gst_format_iterate_definitions:
245  *
246  * Iterate all the registered formats. The format definition is read
247  * only.
248  *
249  * Returns: A GstIterator of #GstFormatDefinition.
250  */
251 GstIterator *
252 gst_format_iterate_definitions (void)
253 {
254   GstIterator *result;
255
256   g_static_mutex_lock (&mutex);
257   /* FIXME: register a boxed type for GstFormatDefinition */
258   result = gst_iterator_new_list (G_TYPE_POINTER,
259       g_static_mutex_get_mutex (&mutex), &_n_values, &_gst_formats,
260       NULL, NULL, NULL);
261   g_static_mutex_unlock (&mutex);
262
263   return result;
264 }