• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:gstformat
26  * @title: GstFormat
27  * @short_description: Dynamically register new data formats
28  * @see_also: #GstPad, #GstElement
29  *
30  * GstFormats functions are used to register a new format to the gstreamer
31  * core.  Formats can be used to perform seeking or conversions/query
32  * operations.
33  */
34 
35 
36 #include "gst_private.h"
37 #include <string.h>
38 #include "gstformat.h"
39 #include "gstenumtypes.h"
40 
41 static GMutex mutex;
42 static GList *_gst_formats = NULL;
43 static GHashTable *_nick_to_format = NULL;
44 static GHashTable *_format_to_nick = NULL;
45 static guint32 _n_values = 1;   /* we start from 1 because 0 reserved for UNDEFINED */
46 
47 static GstFormatDefinition standard_definitions[] = {
48   {GST_FORMAT_DEFAULT, "default", "Default format for the media type", 0},
49   {GST_FORMAT_BYTES, "bytes", "Bytes", 0},
50   {GST_FORMAT_TIME, "time", "Time", 0},
51   {GST_FORMAT_BUFFERS, "buffers", "Buffers", 0},
52   {GST_FORMAT_PERCENT, "percent", "Percent", 0},
53   {GST_FORMAT_UNDEFINED, NULL, NULL, 0}
54 };
55 
56 void
_priv_gst_format_initialize(void)57 _priv_gst_format_initialize (void)
58 {
59   GstFormatDefinition *standards = standard_definitions;
60 
61   g_mutex_lock (&mutex);
62   if (_nick_to_format == NULL) {
63     _nick_to_format = g_hash_table_new (g_str_hash, g_str_equal);
64     _format_to_nick = g_hash_table_new (NULL, NULL);
65   }
66 
67   while (standards->nick) {
68     standards->quark = g_quark_from_static_string (standards->nick);
69     g_hash_table_insert (_nick_to_format, (gpointer) standards->nick,
70         standards);
71     g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (standards->value),
72         standards);
73 
74     _gst_formats = g_list_append (_gst_formats, standards);
75     standards++;
76     _n_values++;
77   }
78   /* getting the type registers the enum */
79   g_type_class_ref (gst_format_get_type ());
80   g_mutex_unlock (&mutex);
81 }
82 
83 /**
84  * gst_format_get_name:
85  * @format: a #GstFormat
86  *
87  * Get a printable name for the given format. Do not modify or free.
88  *
89  * Returns: (nullable): a reference to the static name of the format
90  * or %NULL if the format is unknown.
91  */
92 const gchar *
gst_format_get_name(GstFormat format)93 gst_format_get_name (GstFormat format)
94 {
95   const GstFormatDefinition *def;
96   const gchar *result;
97 
98   if ((def = gst_format_get_details (format)) != NULL)
99     result = def->nick;
100   else
101     result = NULL;
102 
103   return result;
104 }
105 
106 /**
107  * gst_format_to_quark:
108  * @format: a #GstFormat
109  *
110  * Get the unique quark for the given format.
111  *
112  * Returns: the quark associated with the format or 0 if the format
113  * is unknown.
114  */
115 GQuark
gst_format_to_quark(GstFormat format)116 gst_format_to_quark (GstFormat format)
117 {
118   const GstFormatDefinition *def;
119   GQuark result;
120 
121   if ((def = gst_format_get_details (format)) != NULL)
122     result = def->quark;
123   else
124     result = 0;
125 
126   return result;
127 }
128 
129 /**
130  * gst_format_register:
131  * @nick: The nick of the new format
132  * @description: The description of the new format
133  *
134  * Create a new GstFormat based on the nick or return an
135  * already registered format with that nick.
136  *
137  * Returns: A new GstFormat or an already registered format
138  * with the same nick.
139  *
140  * MT safe.
141  */
142 GstFormat
gst_format_register(const gchar * nick,const gchar * description)143 gst_format_register (const gchar * nick, const gchar * description)
144 {
145   GstFormatDefinition *format;
146   GstFormat query;
147 
148   g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
149   g_return_val_if_fail (description != NULL, GST_FORMAT_UNDEFINED);
150 
151   query = gst_format_get_by_nick (nick);
152   if (query != GST_FORMAT_UNDEFINED)
153     return query;
154 
155   g_mutex_lock (&mutex);
156   format = g_slice_new (GstFormatDefinition);
157   format->value = (GstFormat) _n_values;
158   format->nick = g_strdup (nick);
159   format->description = g_strdup (description);
160   format->quark = g_quark_from_static_string (format->nick);
161 
162   g_hash_table_insert (_nick_to_format, (gpointer) format->nick, format);
163   g_hash_table_insert (_format_to_nick, GINT_TO_POINTER (format->value),
164       format);
165   _gst_formats = g_list_append (_gst_formats, format);
166   _n_values++;
167   g_mutex_unlock (&mutex);
168 
169   return format->value;
170 }
171 
172 /**
173  * gst_format_get_by_nick:
174  * @nick: The nick of the format
175  *
176  * Return the format registered with the given nick.
177  *
178  * Returns: The format with @nick or GST_FORMAT_UNDEFINED
179  * if the format was not registered.
180  */
181 GstFormat
gst_format_get_by_nick(const gchar * nick)182 gst_format_get_by_nick (const gchar * nick)
183 {
184   GstFormatDefinition *format;
185 
186   g_return_val_if_fail (nick != NULL, GST_FORMAT_UNDEFINED);
187 
188   g_mutex_lock (&mutex);
189   format = g_hash_table_lookup (_nick_to_format, nick);
190   g_mutex_unlock (&mutex);
191 
192   if (format != NULL)
193     return format->value;
194   else
195     return GST_FORMAT_UNDEFINED;
196 }
197 
198 /**
199  * gst_formats_contains:
200  * @formats: (array zero-terminated=1): The format array to search
201  * @format: the format to find
202  *
203  * See if the given format is inside the format array.
204  *
205  * Returns: %TRUE if the format is found inside the array
206  */
207 gboolean
gst_formats_contains(const GstFormat * formats,GstFormat format)208 gst_formats_contains (const GstFormat * formats, GstFormat format)
209 {
210   if (!formats)
211     return FALSE;
212 
213   while (*formats) {
214     if (*formats == format)
215       return TRUE;
216 
217     formats++;
218   }
219   return FALSE;
220 }
221 
222 
223 /**
224  * gst_format_get_details:
225  * @format: The format to get details of
226  *
227  * Get details about the given format.
228  *
229  * Returns: (nullable): The #GstFormatDefinition for @format or %NULL
230  * on failure.
231  *
232  * MT safe.
233  */
234 const GstFormatDefinition *
gst_format_get_details(GstFormat format)235 gst_format_get_details (GstFormat format)
236 {
237   const GstFormatDefinition *result;
238 
239   g_mutex_lock (&mutex);
240   result = g_hash_table_lookup (_format_to_nick, GINT_TO_POINTER (format));
241   g_mutex_unlock (&mutex);
242 
243   return result;
244 }
245 
246 /**
247  * gst_format_iterate_definitions:
248  *
249  * Iterate all the registered formats. The format definition is read
250  * only.
251  *
252  * Returns: (transfer full): a GstIterator of #GstFormatDefinition.
253  */
254 GstIterator *
gst_format_iterate_definitions(void)255 gst_format_iterate_definitions (void)
256 {
257   GstIterator *result;
258 
259   g_mutex_lock (&mutex);
260   /* FIXME: register a boxed type for GstFormatDefinition */
261   result = gst_iterator_new_list (G_TYPE_POINTER,
262       &mutex, &_n_values, &_gst_formats, NULL, NULL);
263   g_mutex_unlock (&mutex);
264 
265   return result;
266 }
267