Merge branch '0.10'
[platform/upstream/gstreamer.git] / libs / gst / controller / gstdirectcontrolbinding.c
1 /* GStreamer
2  *
3  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
4  *
5  * gstdirectcontrolbinding.c: Direct attachment for control sources
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:gstdirectcontrolbinding
24  * @short_description: direct attachment for control source sources
25  *
26  * A value mapping object that attaches control sources to gobject properties.
27  */
28
29
30 #include <glib-object.h>
31 #include <gst/gst.h>
32
33 #include "gstdirectcontrolbinding.h"
34
35 #include <math.h>
36
37 #define GST_CAT_DEFAULT control_binding_debug
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
39
40 static GObject *gst_direct_control_binding_constructor (GType type,
41     guint n_construct_params, GObjectConstructParam * construct_params);
42 static void gst_direct_control_binding_set_property (GObject * object,
43     guint prop_id, const GValue * value, GParamSpec * pspec);
44 static void gst_direct_control_binding_get_property (GObject * object,
45     guint prop_id, GValue * value, GParamSpec * pspec);
46 static void gst_direct_control_binding_dispose (GObject * object);
47 static void gst_direct_control_binding_finalize (GObject * object);
48
49 static gboolean gst_direct_control_binding_sync_values (GstControlBinding *
50     _self, GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
51 static GValue *gst_direct_control_binding_get_value (GstControlBinding * _self,
52     GstClockTime timestamp);
53 static gboolean gst_direct_control_binding_get_value_array (GstControlBinding *
54     _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
55     GValue * values);
56
57 #define _do_init \
58   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstdirectcontrolbinding", 0, \
59       "dynamic parameter control source attachment");
60
61 G_DEFINE_TYPE_WITH_CODE (GstDirectControlBinding, gst_direct_control_binding,
62     GST_TYPE_CONTROL_BINDING, _do_init);
63
64 enum
65 {
66   PROP_0,
67   PROP_CS,
68   PROP_LAST
69 };
70
71 static GParamSpec *properties[PROP_LAST];
72
73 /* mapping functions */
74
75 #define DEFINE_CONVERT(type,Type,TYPE) \
76 static void \
77 convert_to_##type (GstDirectControlBinding *self, gdouble s, GValue *d) \
78 { \
79   GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
80   g##type v; \
81   \
82   s = CLAMP (s, 0.0, 1.0); \
83   v = pspec->minimum + (g##type) ((pspec->maximum - pspec->minimum) * s); \
84   g_value_set_##type (d, v); \
85 }
86
87 DEFINE_CONVERT (int, Int, INT);
88 DEFINE_CONVERT (uint, UInt, UINT);
89 DEFINE_CONVERT (long, Long, LONG);
90 DEFINE_CONVERT (ulong, ULong, ULONG);
91 DEFINE_CONVERT (int64, Int64, INT64);
92 DEFINE_CONVERT (uint64, UInt64, UINT64);
93 DEFINE_CONVERT (float, Float, FLOAT);
94 DEFINE_CONVERT (double, Double, DOUBLE);
95
96 static void
97 convert_to_boolean (GstDirectControlBinding * self, gdouble s, GValue * d)
98 {
99   s = CLAMP (s, 0.0, 1.0);
100   g_value_set_boolean (d, (gboolean) (s + 0.5));
101 }
102
103 static void
104 convert_to_enum (GstDirectControlBinding * self, gdouble s, GValue * d)
105 {
106   GParamSpecEnum *pspec =
107       G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
108   GEnumClass *e = pspec->enum_class;
109   gint v;
110
111   s = CLAMP (s, 0.0, 1.0);
112   v = s * (e->n_values - 1);
113   g_value_set_enum (d, e->values[v].value);
114 }
115
116 /* vmethods */
117
118 static void
119 gst_direct_control_binding_class_init (GstDirectControlBindingClass * klass)
120 {
121   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122   GstControlBindingClass *control_binding_class =
123       GST_CONTROL_BINDING_CLASS (klass);
124
125   gobject_class->constructor = gst_direct_control_binding_constructor;
126   gobject_class->set_property = gst_direct_control_binding_set_property;
127   gobject_class->get_property = gst_direct_control_binding_get_property;
128   gobject_class->dispose = gst_direct_control_binding_dispose;
129   gobject_class->finalize = gst_direct_control_binding_finalize;
130
131   control_binding_class->sync_values = gst_direct_control_binding_sync_values;
132   control_binding_class->get_value = gst_direct_control_binding_get_value;
133   control_binding_class->get_value_array =
134       gst_direct_control_binding_get_value_array;
135
136   properties[PROP_CS] =
137       g_param_spec_object ("control-source", "ControlSource",
138       "The control source",
139       GST_TYPE_CONTROL_SOURCE,
140       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
141
142   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
143 }
144
145 static void
146 gst_direct_control_binding_init (GstDirectControlBinding * self)
147 {
148 }
149
150 static GObject *
151 gst_direct_control_binding_constructor (GType type, guint n_construct_params,
152     GObjectConstructParam * construct_params)
153 {
154   GstDirectControlBinding *self;
155
156   self =
157       GST_DIRECT_CONTROL_BINDING (G_OBJECT_CLASS
158       (gst_direct_control_binding_parent_class)
159       ->constructor (type, n_construct_params, construct_params));
160
161   if (GST_CONTROL_BINDING_PSPEC (self)) {
162     GType type, base;
163
164     base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self));
165     g_value_init (&self->cur_value, type);
166     while ((type = g_type_parent (type)))
167       base = type;
168
169     GST_DEBUG ("  using type %s", g_type_name (base));
170
171     // select mapping function
172     switch (base) {
173       case G_TYPE_INT:
174         self->convert = convert_to_int;
175         break;
176       case G_TYPE_UINT:
177         self->convert = convert_to_uint;
178         break;
179       case G_TYPE_LONG:
180         self->convert = convert_to_long;
181         break;
182       case G_TYPE_ULONG:
183         self->convert = convert_to_ulong;
184         break;
185       case G_TYPE_INT64:
186         self->convert = convert_to_int64;
187         break;
188       case G_TYPE_UINT64:
189         self->convert = convert_to_uint64;
190         break;
191       case G_TYPE_FLOAT:
192         self->convert = convert_to_float;
193         break;
194       case G_TYPE_DOUBLE:
195         self->convert = convert_to_double;
196         break;
197       case G_TYPE_BOOLEAN:
198         self->convert = convert_to_boolean;
199         break;
200       case G_TYPE_ENUM:
201         self->convert = convert_to_enum;
202         break;
203       default:
204         GST_WARNING ("incomplete implementation for paramspec type '%s'",
205             G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
206         GST_CONTROL_BINDING_PSPEC (self) = NULL;
207         break;
208     }
209   }
210   return (GObject *) self;
211 }
212
213 static void
214 gst_direct_control_binding_set_property (GObject * object, guint prop_id,
215     const GValue * value, GParamSpec * pspec)
216 {
217   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
218
219   switch (prop_id) {
220     case PROP_CS:
221       self->cs = g_value_get_object (value);
222       break;
223     default:
224       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225       break;
226   }
227 }
228
229 static void
230 gst_direct_control_binding_get_property (GObject * object, guint prop_id,
231     GValue * value, GParamSpec * pspec)
232 {
233   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
234
235   switch (prop_id) {
236     case PROP_CS:
237       g_value_set_object (value, self->cs);
238       break;
239     default:
240       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
241       break;
242   }
243 }
244
245 static void
246 gst_direct_control_binding_dispose (GObject * object)
247 {
248   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
249
250   if (self->cs)
251     gst_object_replace ((GstObject **) & self->cs, NULL);
252 }
253
254 static void
255 gst_direct_control_binding_finalize (GObject * object)
256 {
257   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
258
259   g_value_unset (&self->cur_value);
260 }
261
262 static gboolean
263 gst_direct_control_binding_sync_values (GstControlBinding * _self,
264     GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
265 {
266   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
267   gdouble src_val;
268   gboolean ret;
269
270   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
271   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
272
273   GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
274       _self->name, GST_TIME_ARGS (timestamp));
275
276   ret = gst_control_source_get_value (self->cs, timestamp, &src_val);
277   if (G_LIKELY (ret)) {
278     GST_LOG_OBJECT (object, "  new value %lf", src_val);
279     /* always set the value for first time, but then only if it changed
280      * this should limit g_object_notify invocations.
281      * FIXME: can we detect negative playback rates?
282      */
283     if ((timestamp < last_sync) || (src_val != self->last_value)) {
284       GValue *dst_val = &self->cur_value;
285
286       GST_LOG_OBJECT (object, "  mapping %s to value of type %s", _self->name,
287           G_VALUE_TYPE_NAME (dst_val));
288       /* run mapping function to convert gdouble to GValue */
289       self->convert (self, src_val, dst_val);
290       /* we can make this faster
291        * http://bugzilla.gnome.org/show_bug.cgi?id=536939
292        */
293       g_object_set_property ((GObject *) object, _self->name, dst_val);
294       self->last_value = src_val;
295     }
296   } else {
297     GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
298   }
299   return (ret);
300 }
301
302 static GValue *
303 gst_direct_control_binding_get_value (GstControlBinding * _self,
304     GstClockTime timestamp)
305 {
306   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
307   GValue *dst_val = NULL;
308   gdouble src_val;
309
310   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), NULL);
311   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
312   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
313
314   /* get current value via control source */
315   if (gst_control_source_get_value (self->cs, timestamp, &src_val)) {
316     dst_val = g_new0 (GValue, 1);
317     g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (_self->pspec));
318     self->convert (self, src_val, dst_val);
319   } else {
320     GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
321         _self->name, GST_TIME_ARGS (timestamp));
322   }
323
324   return dst_val;
325 }
326
327 static gboolean
328 gst_direct_control_binding_get_value_array (GstControlBinding * _self,
329     GstClockTime timestamp, GstClockTime interval, guint n_values,
330     GValue * values)
331 {
332   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
333   gint i;
334   gdouble *src_val;
335   gboolean res = FALSE;
336   GType type;
337   GstDirectControlBindingConvert convert;
338
339   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
340   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
341   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
342   g_return_val_if_fail (values, FALSE);
343   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
344
345   convert = self->convert;
346   type = G_PARAM_SPEC_VALUE_TYPE (_self->pspec);
347
348   src_val = g_new0 (gdouble, n_values);
349   if ((res = gst_control_source_get_value_array (self->cs, timestamp,
350               interval, n_values, src_val))) {
351     for (i = 0; i < n_values; i++) {
352       if (!isnan (src_val[i])) {
353         g_value_init (&values[i], type);
354         convert (self, src_val[i], &values[i]);
355       } else {
356         GST_LOG ("no control value for property %s at index %d", _self->name,
357             i);
358       }
359     }
360   } else {
361     GST_LOG ("failed to get control value for property %s at ts %"
362         GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
363   }
364   g_free (src_val);
365   return res;
366 }
367
368 /* functions */
369
370 /**
371  * gst_direct_control_binding_new:
372  * @object: the object of the property
373  * @property_name: the property-name to attach the control source
374  * @csource: the control source
375  *
376  * Create a new control-binding that attaches the #GstControlSource to the
377  * #GObject property.
378  *
379  * Returns: (transfer floating): the new #GstDirectControlBinding
380  */
381 GstControlBinding *
382 gst_direct_control_binding_new (GstObject * object, const gchar * property_name,
383     GstControlSource * cs)
384 {
385   return (GstControlBinding *) g_object_new (GST_TYPE_DIRECT_CONTROL_BINDING,
386       "object", object, "name", property_name, "control-source", cs, NULL);
387 }