1 /* GStreamer - GParamSpecs for some of our types
2 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * SECTION:gstparamspec
21 * @title: GstParamSpec
22 * @short_description: GParamSpec implementations specific
25 * GParamSpec implementations specific to GStreamer.
32 #include "gst_private.h"
33 #include "glib-compat-private.h"
34 #include "gstparamspecs.h"
36 /* --- GstParamSpecFraction --- */
39 _gst_param_fraction_init (GParamSpec * pspec)
41 GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
45 fspec->max_num = G_MAXINT;
52 _gst_param_fraction_set_default (GParamSpec * pspec, GValue * value)
54 value->data[0].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_num;
55 value->data[1].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_den;
59 _gst_param_fraction_validate (GParamSpec * pspec, GValue * value)
61 GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
62 gboolean within_range = FALSE;
63 GValue f_this = { 0, };
64 GValue f_min = { 0, };
65 GValue f_max = { 0, };
68 g_value_init (&f_this, GST_TYPE_FRACTION);
69 gst_value_set_fraction (&f_this, value->data[0].v_int, value->data[1].v_int);
71 g_value_init (&f_min, GST_TYPE_FRACTION);
72 gst_value_set_fraction (&f_min, fspec->min_num, fspec->min_den);
74 g_value_init (&f_max, GST_TYPE_FRACTION);
75 gst_value_set_fraction (&f_max, fspec->max_num, fspec->max_den);
77 res = gst_value_compare (&f_min, &f_this);
78 #ifndef GST_DISABLE_GST_DEBUG
79 GST_LOG ("comparing %d/%d to %d/%d, result = %d", fspec->min_num,
80 fspec->min_den, value->data[0].v_int, value->data[1].v_int, res);
82 if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
85 #ifndef GST_DISABLE_GST_DEBUG
86 GST_LOG ("comparing %d/%d to %d/%d, result = %d", value->data[0].v_int,
87 value->data[1].v_int, fspec->max_num, fspec->max_den, res);
89 res = gst_value_compare (&f_this, &f_max);
90 if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
97 g_value_unset (&f_min);
98 g_value_unset (&f_max);
99 g_value_unset (&f_this);
101 #ifndef GST_DISABLE_GST_DEBUG
102 GST_LOG ("%swithin range", (within_range) ? "" : "not ");
105 /* return FALSE if everything ok, otherwise TRUE */
106 return !within_range;
110 _gst_param_fraction_values_cmp (GParamSpec * pspec, const GValue * value1,
111 const GValue * value2)
115 res = gst_value_compare (value1, value2);
117 g_assert (res != GST_VALUE_UNORDERED);
119 /* GST_VALUE_LESS_THAN is -1, EQUAL is 0, and GREATER_THAN is 1 */
124 gst_param_spec_fraction_get_type (void)
126 static GType type; /* 0 */
128 /* register GST_TYPE_PARAM_FRACTION */
130 static GParamSpecTypeInfo pspec_info = {
131 sizeof (GstParamSpecFraction), /* instance_size */
133 _gst_param_fraction_init, /* instance_init */
134 G_TYPE_INVALID, /* value_type */
136 _gst_param_fraction_set_default, /* value_set_default */
137 _gst_param_fraction_validate, /* value_validate */
138 _gst_param_fraction_values_cmp, /* values_cmp */
140 pspec_info.value_type = GST_TYPE_FRACTION;
141 type = g_param_type_register_static ("GstParamFraction", &pspec_info);
147 * gst_param_spec_fraction:
148 * @name: canonical name of the property specified
149 * @nick: nick name for the property specified
150 * @blurb: description of the property specified
151 * @min_num: minimum value (fraction numerator)
152 * @min_denom: minimum value (fraction denominator)
153 * @max_num: maximum value (fraction numerator)
154 * @max_denom: maximum value (fraction denominator)
155 * @default_num: default value (fraction numerator)
156 * @default_denom: default value (fraction denominator)
157 * @flags: flags for the property specified
159 * This function creates a fraction GParamSpec for use by objects/elements
160 * that want to expose properties of fraction type. This function is typically
161 * used in connection with g_object_class_install_property() in a GObjects's
162 * instance_init function.
164 * Returns: (transfer full): a newly created parameter specification
167 gst_param_spec_fraction (const gchar * name, const gchar * nick,
168 const gchar * blurb, gint min_num, gint min_denom, gint max_num,
169 gint max_denom, gint default_num, gint default_denom, GParamFlags flags)
171 GstParamSpecFraction *fspec;
173 GValue default_val = { 0, };
176 g_param_spec_internal (GST_TYPE_PARAM_FRACTION, name, nick, blurb, flags);
178 fspec->min_num = min_num;
179 fspec->min_den = min_denom;
180 fspec->max_num = max_num;
181 fspec->max_den = max_denom;
182 fspec->def_num = default_num;
183 fspec->def_den = default_denom;
185 pspec = G_PARAM_SPEC (fspec);
187 /* check that min <= default <= max */
188 g_value_init (&default_val, GST_TYPE_FRACTION);
189 gst_value_set_fraction (&default_val, default_num, default_denom);
190 /* validate returns TRUE if the validation fails */
191 if (_gst_param_fraction_validate (pspec, &default_val)) {
192 g_critical ("GstParamSpec of type 'fraction' for property '%s' has a "
193 "default value of %d/%d, which is not within the allowed range of "
194 "%d/%d to %d/%d", name, default_num, default_denom, min_num,
195 min_denom, max_num, max_denom);
196 g_param_spec_ref (pspec);
197 g_param_spec_sink (pspec);
198 g_param_spec_unref (pspec);
201 g_value_unset (&default_val);
207 _gst_param_array_init (GParamSpec * pspec)
209 GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
211 aspec->element_spec = NULL;
215 _gst_param_array_finalize (GParamSpec * pspec)
217 GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
218 GParamSpecClass *parent_class =
219 g_type_class_peek (g_type_parent (GST_TYPE_PARAM_ARRAY_LIST));
221 if (aspec->element_spec) {
222 g_param_spec_unref (aspec->element_spec);
223 aspec->element_spec = NULL;
226 parent_class->finalize (pspec);
230 _gst_param_array_validate (GParamSpec * pspec, GValue * value)
232 GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
234 /* ensure array values validity against a present element spec */
235 if (aspec->element_spec) {
236 GParamSpec *element_spec = aspec->element_spec;
239 for (i = 0; i < gst_value_array_get_size (value); i++) {
240 GValue *element = (GValue *) gst_value_array_get_value (value, i);
242 /* need to fixup value type, or ensure that the array value is initialized at all */
243 if (!g_value_type_compatible (G_VALUE_TYPE (element),
244 G_PARAM_SPEC_VALUE_TYPE (element_spec))) {
245 if (G_VALUE_TYPE (element) != 0)
246 g_value_unset (element);
247 g_value_init (element, G_PARAM_SPEC_VALUE_TYPE (element_spec));
248 g_param_value_set_default (element_spec, element);
251 /* validate array value against element_spec */
252 if (!g_param_value_validate (element_spec, element))
261 _gst_param_array_values_cmp (GParamSpec * pspec, const GValue * value1,
262 const GValue * value2)
264 GstParamSpecArray *aspec = GST_PARAM_SPEC_ARRAY_LIST (pspec);
267 if (!value1 || !value2)
268 return value2 ? -1 : value1 != value2;
270 size1 = gst_value_array_get_size (value1);
271 size2 = gst_value_array_get_size (value2);
274 return size1 < size2 ? -1 : 1;
275 else if (!aspec->element_spec) {
276 /* we need an element specification for comparisons, so there's not much
277 * to compare here, try to at least provide stable lesser/greater result
279 return size1 < size2 ? -1 : size1 > size2;
280 } else { /* size1 == size2 */
283 for (i = 0; i < size1; i++) {
284 const GValue *element1 = gst_value_array_get_value (value1, i);
285 const GValue *element2 = gst_value_array_get_value (value2, i);
288 /* need corresponding element types, provide stable result otherwise */
289 if (G_VALUE_TYPE (element1) != G_VALUE_TYPE (element2))
290 return G_VALUE_TYPE (element1) < G_VALUE_TYPE (element2) ? -1 : 1;
291 cmp = g_param_values_cmp (aspec->element_spec, element1, element2);
300 gst_param_spec_array_get_type (void)
302 static GType type; /* 0 */
304 /* register GST_TYPE_PARAM_FRACTION */
306 static GParamSpecTypeInfo pspec_info = {
307 sizeof (GstParamSpecArray), /* instance_size */
309 _gst_param_array_init, /* instance_init */
310 G_TYPE_INVALID, /* value_type */
311 _gst_param_array_finalize, /* finalize */
312 NULL, /* value_set_default */
313 _gst_param_array_validate, /* value_validate */
314 _gst_param_array_values_cmp, /* values_cmp */
316 pspec_info.value_type = GST_TYPE_ARRAY;
317 type = g_param_type_register_static ("GstParamArray", &pspec_info);
323 * gst_param_spec_array:
324 * @name: canonical name of the property specified
325 * @nick: nick name for the property specified
326 * @blurb: description of the property specified
327 * @element_spec: GParamSpec of the array
328 * @flags: flags for the property specified
330 * This function creates a GstArray GParamSpec for use by objects/elements
331 * that want to expose properties of GstArray type. This function is
332 * typically * used in connection with g_object_class_install_property() in a
333 * GObjects's instance_init function.
335 * Returns: (transfer full): a newly created parameter specification
339 gst_param_spec_array (const gchar * name,
341 const gchar * blurb, GParamSpec * element_spec, GParamFlags flags)
343 GstParamSpecArray *aspec;
346 g_return_val_if_fail (G_IS_PARAM_SPEC (element_spec), NULL);
348 aspec = g_param_spec_internal (GST_TYPE_PARAM_ARRAY_LIST,
349 name, nick, blurb, flags);
354 aspec->element_spec = g_param_spec_ref (element_spec);
355 g_param_spec_sink (element_spec);
358 return G_PARAM_SPEC (aspec);