Convert %lld and %llu in printf formats to G_G[U]INT64_FORMAT. Fix pointer<->int...
[platform/upstream/gstreamer.git] / libs / gst / control / dparam.c
1 /* GStreamer
2  * Copyright (C) 2001 Steve Baker <stevebaker_org@yahoo.co.uk>
3  *
4  * gstdparam.c: Dynamic Parameter functionality
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <math.h>
23 #include <string.h>
24 #include <gst/gstinfo.h>
25
26 #include "dparam.h"
27 #include "dparammanager.h"
28
29 static void gst_dparam_class_init (GstDParamClass *klass);
30 static void gst_dparam_init (GstDParam *dparam);
31 static void gst_dparam_dispose (GObject *object);
32 static void gst_dparam_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
33 static void gst_dparam_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
34
35 enum {
36         ARG_0,
37         ARG_VALUE_FLOAT,
38         ARG_VALUE_INT,
39         ARG_VALUE_INT64,
40 };
41
42 enum {
43   VALUE_CHANGED,
44   LAST_SIGNAL
45 };
46
47 static guint gst_dparam_signals[LAST_SIGNAL] = { 0 };
48
49 GType 
50 gst_dparam_get_type(void) {
51         static GType dparam_type = 0;
52
53         if (!dparam_type) {
54                 static const GTypeInfo dparam_info = {
55                         sizeof(GstDParamClass),
56                         NULL,
57                         NULL,
58                         (GClassInitFunc)gst_dparam_class_init,
59                         NULL,
60                         NULL,
61                         sizeof(GstDParam),
62                         0,
63                         (GInstanceInitFunc)gst_dparam_init,
64                 };
65                 dparam_type = g_type_register_static(GST_TYPE_OBJECT, "GstDParam", &dparam_info, 0);
66         }
67         return dparam_type;
68 }
69
70 static void
71 gst_dparam_class_init (GstDParamClass *klass)
72 {
73         GObjectClass *gobject_class;
74         GstDParamClass *dparam_class;
75         GstObjectClass *gstobject_class;
76
77         gobject_class = (GObjectClass*)klass;
78         dparam_class = (GstDParamClass*)klass;
79         gstobject_class = (GstObjectClass*) klass;
80
81         gobject_class->get_property = gst_dparam_get_property;
82         gobject_class->set_property = gst_dparam_set_property;
83
84         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_VALUE_FLOAT,
85                 g_param_spec_float("value_float","Float Value",
86                              "The value that should be changed if gfloat is the type",
87                      (gfloat)G_MININT64, (gfloat)G_MAXINT64, 0.0F, G_PARAM_READWRITE));
88         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_VALUE_INT,
89                 g_param_spec_int("value_int","Integer Value",
90                              "The value that should be changed if gint is the type",
91                      G_MININT, G_MAXINT, 0, G_PARAM_READWRITE));
92         g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_VALUE_INT64,
93                 g_param_spec_int64("value_int64","64 bit Integer Value",
94                              "The value that should be changed if gint64 is the type",
95                      G_MININT64, G_MAXINT64, 0, G_PARAM_READWRITE));
96                      
97         gobject_class->dispose = gst_dparam_dispose;
98         
99         gst_dparam_signals[VALUE_CHANGED] =
100                 g_signal_new ("value_changed", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST,
101                               G_STRUCT_OFFSET (GstDParamClass, value_changed), NULL, NULL,
102                               gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
103         /*gstobject_class->save_thyself = gst_dparam_save_thyself; */
104
105 }
106
107 static void
108 gst_dparam_init (GstDParam *dparam)
109 {
110         g_return_if_fail (dparam != NULL);
111         GST_DPARAM_TYPE(dparam) = 0;
112         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam)=0LL;
113         GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam)=0LL;
114         GST_DPARAM_READY_FOR_UPDATE(dparam)=FALSE;
115         dparam->lock = g_mutex_new ();
116 }
117
118 /**
119  * gst_dparam_new:
120  * @type: the type that this dparam will store
121  *
122  * Returns: a new instance of GstDParam
123  */
124 GstDParam* 
125 gst_dparam_new (GType type)
126 {
127         GstDParam *dparam;
128
129         dparam = g_object_new (gst_dparam_get_type (), NULL);
130         dparam->do_update_func = gst_dparam_do_update_default;
131
132         GST_DPARAM_TYPE(dparam) = type;
133         
134         return dparam;
135 }
136
137
138 static void 
139 gst_dparam_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
140 {
141         GstDParam *dparam;
142         g_return_if_fail(GST_IS_DPARAM(object));
143         dparam = GST_DPARAM(object);
144   
145         switch (prop_id) {   
146                 case ARG_VALUE_FLOAT:
147                         g_value_set_float (value, dparam->value_float);
148                         break;
149                         
150                 case ARG_VALUE_INT:
151                         g_value_set_float (value, dparam->value_int);
152                         break;
153                         
154                 case ARG_VALUE_INT64:
155                         g_value_set_float (value, dparam->value_int64);
156                         break;
157                 default:
158                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159                         break;
160         }
161 }
162
163 static void 
164 gst_dparam_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) 
165 {
166         GstDParam *dparam;
167         g_return_if_fail(GST_IS_DPARAM(object));
168         dparam = GST_DPARAM(object);
169         GST_DPARAM_LOCK(dparam);
170
171         switch (prop_id) {
172                 case ARG_VALUE_FLOAT:
173                         GST_DEBUG(GST_CAT_PARAMS, "setting value from %f to %f", dparam->value_float, g_value_get_float (value));
174                         dparam->value_float = g_value_get_float (value);
175                         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam);
176                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
177                         break;
178                         
179                 case ARG_VALUE_INT:
180                         GST_DEBUG(GST_CAT_PARAMS, "setting value from %d to %d", dparam->value_int, g_value_get_int (value));
181                         dparam->value_int = g_value_get_int (value);
182                         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam);
183                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
184                         break;
185                         
186                 case ARG_VALUE_INT64:
187                         GST_DEBUG(GST_CAT_PARAMS, "setting value from %"
188                                                   G_GINT64_FORMAT " to %"
189                                                   G_GINT64_FORMAT,
190                                   dparam->value_int64, g_value_get_int64 (value));
191                         dparam->value_int64 = g_value_get_int (value);
192                         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam);
193                         GST_DPARAM_READY_FOR_UPDATE(dparam) = TRUE;
194                         break;
195                         
196                 default:
197                         break;
198         }
199
200         /* note that this signal is sent while we still have the lock. */
201         g_signal_emit (G_OBJECT (dparam), gst_dparam_signals[VALUE_CHANGED], 0);
202         GST_DPARAM_UNLOCK(dparam);
203 }
204
205 void
206 gst_dparam_do_update_default (GstDParam *dparam, gint64 timestamp, GValue *value, GstDParamUpdateInfo update_info)
207 {
208         GST_DPARAM_LOCK(dparam);
209         
210         g_return_if_fail (G_VALUE_TYPE(value) == GST_DPARAM_TYPE(dparam));
211         GST_DEBUG(GST_CAT_PARAMS, "updating value for %s(%p)",GST_DPARAM_NAME (dparam),dparam);
212         
213         switch (G_VALUE_TYPE(value)) {
214                 case G_TYPE_FLOAT:
215                         g_value_set_float(value, dparam->value_float);
216                         break;
217                         
218                 case G_TYPE_INT:
219                         g_value_set_int(value, dparam->value_int);
220                         break;
221                         
222                 case G_TYPE_INT64:
223                         g_value_set_int64(value, dparam->value_int64);
224                         break;
225                         
226                 default:
227                         break;
228         }
229
230         GST_DPARAM_LAST_UPDATE_TIMESTAMP(dparam) = timestamp;
231         GST_DPARAM_NEXT_UPDATE_TIMESTAMP(dparam) = timestamp;
232         GST_DPARAM_READY_FOR_UPDATE(dparam) = FALSE;
233
234         GST_DPARAM_UNLOCK(dparam);
235 }
236
237 static void
238 gst_dparam_dispose (GObject *object)
239 {
240         GstDParam *dparam = GST_DPARAM(object);
241         gchar *dparam_name = g_strdup(GST_DPARAM_NAME(dparam));
242         
243         GST_DEBUG (GST_CAT_PLUGIN_INFO, "disposing of %s", dparam_name);
244         if (GST_DPARAM_MANAGER(dparam)){
245                 gst_dpman_detach_dparam(GST_DPARAM_MANAGER(dparam), dparam_name);
246         }
247         g_free(dparam_name);
248 }
249
250 /**
251  * gst_dparam_attach
252  * @dparam: GstDParam instance
253  * @manager: the GstDParamManager that this dparam belongs to
254  *
255  */
256 void
257 gst_dparam_attach (GstDParam *dparam, GstDParamManager *manager, GParamSpec *param_spec, gchar *unit_name)
258 {
259         
260         g_return_if_fail (dparam != NULL);
261         g_return_if_fail (GST_IS_DPARAM (dparam));
262         g_return_if_fail (manager != NULL);
263         g_return_if_fail (GST_IS_DPMAN (manager));
264         g_return_if_fail (param_spec != NULL);
265         g_return_if_fail (unit_name != NULL);
266         g_return_if_fail (G_IS_PARAM_SPEC (param_spec));
267
268         GST_DPARAM_NAME(dparam) = g_param_spec_get_name(param_spec);
269         GST_DPARAM_PARAM_SPEC(dparam) = param_spec;
270         GST_DPARAM_MANAGER(dparam) = manager;
271         GST_DPARAM_UNIT_NAME(dparam) = unit_name;
272         GST_DPARAM_IS_LOG(dparam) = gst_unitconv_unit_is_logarithmic(unit_name);
273         GST_DEBUG(GST_CAT_PARAMS, "attaching %s to dparam %p",GST_DPARAM_NAME (dparam),dparam);
274
275 }
276
277 /**
278  * gst_dparam_detach
279  * @dparam: GstDParam instance
280  * @manager: the GstDParamManager that this dparam belongs to
281  *
282  */
283 void
284 gst_dparam_detach (GstDParam *dparam)
285 {
286         
287         g_return_if_fail (dparam != NULL);
288         g_return_if_fail (GST_IS_DPARAM (dparam));
289
290         GST_DEBUG(GST_CAT_PARAMS, "detaching %s from dparam %p",GST_DPARAM_NAME (dparam),dparam);
291         
292         GST_DPARAM_NAME(dparam) = NULL;
293         GST_DPARAM_PARAM_SPEC(dparam) = NULL;
294         GST_DPARAM_MANAGER(dparam) = NULL;
295 }
296
297
298