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