First THREADED backport attempt, focusing on adding locks and making sure the API...
[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 #include <string.h>
25
26 #include "gst_private.h"
27 #include "gstformat.h"
28
29 static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
30 static GList *_gst_formats = NULL;
31 static GHashTable *_nick_to_format = NULL;
32 static GHashTable *_format_to_nick = NULL;
33 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
34
35 static GstFormatDefinition standard_definitions[] = {
36   {GST_FORMAT_DEFAULT, "default", "Default format for the media type"},
37   {GST_FORMAT_BYTES, "bytes", "Bytes"},
38   {GST_FORMAT_TIME, "time", "Time"},
39   {GST_FORMAT_BUFFERS, "buffers", "Buffers"},
40   {GST_FORMAT_PERCENT, "percent", "Percent"},
41   {0, NULL, NULL}
42 };
43
44 void
45 _gst_format_initialize (void)
46 {
47   GstFormatDefinition *standards = standard_definitions;
48
49   g_static_mutex_lock (&mutex);
50   if (_nick_to_format == NULL) {
51     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
52     _format_to_nick = g_hash_table_new (NULL, NULL);
53   }
54
55   while (standards->nick) {
56     g_hash_table_insert (_nick_to_format, standards->nick, standards);
57     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
58         standards);
59
60     _gst_formats = g_list_append (_gst_formats, standards);
61     standards++;
62     _n_values++;
63   }
64   g_static_mutex_unlock (&mutex);
65 }
66
67 /**
68  * gst_format_register:
69  * @nick: The nick of the new format
70  * @description: The description of the new format
71  *
72  * Create a new GstFormat based on the nick or return an
73  * already registered format with that nick.
74  *
75  * Returns: A new GstFormat or an already registered format
76  * with the same nick.
77  *
78  * MT safe.
79  */
80 GstFormat
81 gst_format_register (const gchar * nick, const gchar * description)
82 {
83   GstFormatDefinition *format;
84   GstFormat query;
85
86   g_return_val_if_fail (nick != NULL, 0);
87   g_return_val_if_fail (description != NULL, 0);
88
89   query = gst_format_get_by_nick (nick);
90   if (query != GST_FORMAT_UNDEFINED)
91     return query;
92
93   format = g_new0 (GstFormatDefinition, 1);
94   format->value = _n_values;
95   format->nick = g_strdup (nick);
96   format->description = g_strdup (description);
97
98   g_static_mutex_lock (&mutex);
99   g_hash_table_insert (_nick_to_format, format->nick, format);
100   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
101       format);
102   _gst_formats = g_list_append (_gst_formats, format);
103   _n_values++;
104   g_static_mutex_unlock (&mutex);
105
106   return format->value;
107 }
108
109 /**
110  * gst_format_get_by_nick:
111  * @nick: The nick of the format
112  *
113  * Return the format registered with the given nick. 
114  *
115  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
116  * if the format was not registered.
117  */
118 GstFormat
119 gst_format_get_by_nick (const gchar * nick)
120 {
121   GstFormatDefinition *format;
122
123   g_return_val_if_fail (nick != NULL, 0);
124
125   g_static_mutex_lock (&mutex);
126   format = g_hash_table_lookup (_nick_to_format, nick);
127   g_static_mutex_unlock (&mutex);
128
129   if (format != NULL)
130     return format->value;
131   else
132     return GST_FORMAT_UNDEFINED;
133 }
134
135 /**
136  * gst_formats_contains:
137  * @formats: The format array to search
138  * @format: the format to find
139  *
140  * See if the given format is inside the format array.
141  *
142  * Returns: TRUE if the format is found inside the array
143  */
144 gboolean
145 gst_formats_contains (const GstFormat * formats, GstFormat format)
146 {
147   if (!formats)
148     return FALSE;
149
150   while (*formats) {
151     if (*formats == format)
152       return TRUE;
153
154     formats++;
155   }
156   return FALSE;
157 }
158
159
160 /**
161  * gst_format_get_details:
162  * @format: The format to get details of
163  *
164  * Get details about the given format.
165  *
166  * Returns: The #GstFormatDefinition for @format or NULL on failure.
167  *
168  * MT safe.
169  */
170 const GstFormatDefinition *
171 gst_format_get_details (GstFormat format)
172 {
173   const GstFormatDefinition *result;
174
175   g_static_mutex_lock (&mutex);
176   result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
177   g_static_mutex_unlock (&mutex);
178
179   return result;
180 }
181
182 /**
183  * gst_format_iterate_definitions:
184  *
185  * Iterate all the registered formats. The format definition is read 
186  * only.
187  *
188  * Returns: A GstIterator of #GstFormatDefinition.
189  */
190 GstIterator *
191 gst_format_iterate_definitions (void)
192 {
193   GstIterator *result;
194
195   g_static_mutex_lock (&mutex);
196   result = gst_iterator_new_list (g_static_mutex_get_mutex (&mutex),
197       &_n_values, &_gst_formats, NULL, NULL, NULL);
198   g_static_mutex_unlock (&mutex);
199
200   return result;
201 }