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