6425e183fd53c698de2d2729c49d97842149a4b9
[platform/upstream/gstreamer.git] / gst / gstparamspecs.c
1 /* GStreamer - GParamSpecs for some of our types
2  * Copyright (C) 2007 Tim-Philipp Müller  <tim centricular net>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:gstparamspec
21  * @short_description: GParamSpec implementations specific
22  * to GStreamer
23  *
24  * GParamSpec implementations specific to GStreamer.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "gst_private.h"
32 #include "glib-compat-private.h"
33 #include "gstparamspecs.h"
34
35 /* --- GstParamSpecFraction --- */
36
37 static void
38 _gst_param_fraction_init (GParamSpec * pspec)
39 {
40   GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
41
42   fspec->min_num = 0;
43   fspec->min_den = 1;
44   fspec->max_num = G_MAXINT;
45   fspec->max_den = 1;
46   fspec->def_num = 1;
47   fspec->def_den = 1;
48 }
49
50 static void
51 _gst_param_fraction_set_default (GParamSpec * pspec, GValue * value)
52 {
53   value->data[0].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_num;
54   value->data[1].v_int = GST_PARAM_SPEC_FRACTION (pspec)->def_den;
55 }
56
57 static gboolean
58 _gst_param_fraction_validate (GParamSpec * pspec, GValue * value)
59 {
60   GstParamSpecFraction *fspec = GST_PARAM_SPEC_FRACTION (pspec);
61   gboolean within_range = FALSE;
62   GValue f_this = { 0, };
63   GValue f_min = { 0, };
64   GValue f_max = { 0, };
65   gint res;
66
67   g_value_init (&f_this, GST_TYPE_FRACTION);
68   gst_value_set_fraction (&f_this, value->data[0].v_int, value->data[1].v_int);
69
70   g_value_init (&f_min, GST_TYPE_FRACTION);
71   gst_value_set_fraction (&f_min, fspec->min_num, fspec->min_den);
72
73   g_value_init (&f_max, GST_TYPE_FRACTION);
74   gst_value_set_fraction (&f_max, fspec->max_num, fspec->max_den);
75
76   res = gst_value_compare (&f_min, &f_this);
77 #ifndef GST_DISABLE_GST_DEBUG
78   GST_LOG ("comparing %d/%d to %d/%d, result = %d", fspec->min_num,
79       fspec->min_den, value->data[0].v_int, value->data[1].v_int, res);
80 #endif
81   if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
82     goto out;
83
84 #ifndef GST_DISABLE_GST_DEBUG
85   GST_LOG ("comparing %d/%d to %d/%d, result = %d", value->data[0].v_int,
86       value->data[1].v_int, fspec->max_num, fspec->max_den, res);
87 #endif
88   res = gst_value_compare (&f_this, &f_max);
89   if (res != GST_VALUE_LESS_THAN && res != GST_VALUE_EQUAL)
90     goto out;
91
92   within_range = TRUE;
93
94 out:
95
96   g_value_unset (&f_min);
97   g_value_unset (&f_max);
98   g_value_unset (&f_this);
99
100 #ifndef GST_DISABLE_GST_DEBUG
101   GST_LOG ("%swithin range", (within_range) ? "" : "not ");
102 #endif
103
104   /* return FALSE if everything ok, otherwise TRUE */
105   return !within_range;
106 }
107
108 static gint
109 _gst_param_fraction_values_cmp (GParamSpec * pspec, const GValue * value1,
110     const GValue * value2)
111 {
112   gint res;
113
114   res = gst_value_compare (value1, value2);
115
116   g_assert (res != GST_VALUE_UNORDERED);
117
118   /* GST_VALUE_LESS_THAN is -1, EQUAL is 0, and GREATER_THAN is 1 */
119   return res;
120 }
121
122 GType
123 gst_param_spec_fraction_get_type (void)
124 {
125   static GType type;            /* 0 */
126
127   /* register GST_TYPE_PARAM_FRACTION */
128   if (type == 0) {
129     static GParamSpecTypeInfo pspec_info = {
130       sizeof (GstParamSpecFraction),    /* instance_size     */
131       0,                        /* n_preallocs       */
132       _gst_param_fraction_init, /* instance_init     */
133       G_TYPE_INVALID,           /* value_type        */
134       NULL,                     /* finalize          */
135       _gst_param_fraction_set_default,  /* value_set_default */
136       _gst_param_fraction_validate,     /* value_validate    */
137       _gst_param_fraction_values_cmp,   /* values_cmp        */
138     };
139     pspec_info.value_type = GST_TYPE_FRACTION;
140     type = g_param_type_register_static ("GstParamFraction", &pspec_info);
141   }
142   return type;
143 }
144
145 /**
146  * gst_param_spec_fraction:
147  * @name: canonical name of the property specified
148  * @nick: nick name for the property specified
149  * @blurb: description of the property specified
150  * @min_num: minimum value (fraction numerator)
151  * @min_denom: minimum value (fraction denominator)
152  * @max_num: maximum value (fraction numerator)
153  * @max_denom: maximum value (fraction denominator)
154  * @default_num: default value (fraction numerator)
155  * @default_denom: default value (fraction denominator)
156  * @flags: flags for the property specified
157  *
158  * This function creates a fraction GParamSpec for use by objects/elements
159  * that want to expose properties of fraction type. This function is typically
160  * used in connection with g_object_class_install_property() in a GObjects's
161  * instance_init function.
162  *
163  * Returns: (transfer full): a newly created parameter specification
164  */
165 GParamSpec *
166 gst_param_spec_fraction (const gchar * name, const gchar * nick,
167     const gchar * blurb, gint min_num, gint min_denom, gint max_num,
168     gint max_denom, gint default_num, gint default_denom, GParamFlags flags)
169 {
170   GstParamSpecFraction *fspec;
171   GParamSpec *pspec;
172   GValue default_val = { 0, };
173
174   fspec =
175       g_param_spec_internal (GST_TYPE_PARAM_FRACTION, name, nick, blurb, flags);
176
177   fspec->min_num = min_num;
178   fspec->min_den = min_denom;
179   fspec->max_num = max_num;
180   fspec->max_den = max_denom;
181   fspec->def_num = default_num;
182   fspec->def_den = default_denom;
183
184   pspec = G_PARAM_SPEC (fspec);
185
186   /* check that min <= default <= max */
187   g_value_init (&default_val, GST_TYPE_FRACTION);
188   gst_value_set_fraction (&default_val, default_num, default_denom);
189   /* validate returns TRUE if the validation fails */
190   if (_gst_param_fraction_validate (pspec, &default_val)) {
191     g_critical ("GstParamSpec of type 'fraction' for property '%s' has a "
192         "default value of %d/%d, which is not within the allowed range of "
193         "%d/%d to %d/%d", name, default_num, default_denom, min_num,
194         min_denom, max_num, max_denom);
195     g_param_spec_ref (pspec);
196     g_param_spec_sink (pspec);
197     g_param_spec_unref (pspec);
198     pspec = NULL;
199   }
200   g_value_unset (&default_val);
201
202   return pspec;
203 }