controlbindings: fix pspec relaxation for control source properties
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION:gstdirectcontrolbinding
24  * @short_description: direct attachment for control sources
25  *
26  * A value mapping object that attaches control sources to gobject properties. It
27  * will map the control values [0.0 ... 1.0] to the target property range. If a
28  * control value is outside of the range, it will be clipped.
29  */
30
31 #include <glib-object.h>
32 #include <gst/gst.h>
33
34 #include "gstdirectcontrolbinding.h"
35
36 #include <gst/math-compat.h>
37
38 #define GST_CAT_DEFAULT control_binding_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40
41 static GObject *gst_direct_control_binding_constructor (GType type,
42     guint n_construct_params, GObjectConstructParam * construct_params);
43 static void gst_direct_control_binding_set_property (GObject * object,
44     guint prop_id, const GValue * value, GParamSpec * pspec);
45 static void gst_direct_control_binding_get_property (GObject * object,
46     guint prop_id, GValue * value, GParamSpec * pspec);
47 static void gst_direct_control_binding_dispose (GObject * object);
48 static void gst_direct_control_binding_finalize (GObject * object);
49
50 static gboolean gst_direct_control_binding_sync_values (GstControlBinding *
51     _self, GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
52 static GValue *gst_direct_control_binding_get_value (GstControlBinding * _self,
53     GstClockTime timestamp);
54 static gboolean gst_direct_control_binding_get_value_array (GstControlBinding *
55     _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
56     gpointer values);
57 static gboolean gst_direct_control_binding_get_g_value_array (GstControlBinding
58     * _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
59     GValue * values);
60
61 #define _do_init \
62   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstdirectcontrolbinding", 0, \
63       "dynamic parameter control source attachment");
64
65 #define gst_direct_control_binding_parent_class parent_class
66 G_DEFINE_TYPE_WITH_CODE (GstDirectControlBinding, gst_direct_control_binding,
67     GST_TYPE_CONTROL_BINDING, _do_init);
68
69 enum
70 {
71   PROP_0,
72   PROP_CS,
73   PROP_LAST
74 };
75
76 static GParamSpec *properties[PROP_LAST];
77
78 /* mapping functions */
79
80 #define DEFINE_CONVERT(type,Type,TYPE,ROUNDING_OP) \
81 static void \
82 convert_g_value_to_##type (GstDirectControlBinding *self, gdouble s, GValue *d) \
83 { \
84   GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
85   g##type v; \
86   \
87   s = CLAMP (s, 0.0, 1.0); \
88   v = (g##type) ROUNDING_OP (pspec->minimum * (1-s)) + (g##type) ROUNDING_OP (pspec->maximum * s); \
89   g_value_set_##type (d, v); \
90 } \
91 \
92 static void \
93 convert_value_to_##type (GstDirectControlBinding *self, gdouble s, gpointer d_) \
94 { \
95   GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
96   g##type *d = (g##type *)d_; \
97   \
98   s = CLAMP (s, 0.0, 1.0); \
99   *d = (g##type) ROUNDING_OP (pspec->minimum * (1-s)) + (g##type) ROUNDING_OP (pspec->maximum * s); \
100 }
101
102
103 DEFINE_CONVERT (int, Int, INT, rint);
104 DEFINE_CONVERT (uint, UInt, UINT, rint);
105 DEFINE_CONVERT (long, Long, LONG, rint);
106 DEFINE_CONVERT (ulong, ULong, ULONG, rint);
107 DEFINE_CONVERT (int64, Int64, INT64, rint);
108 DEFINE_CONVERT (uint64, UInt64, UINT64, rint);
109 DEFINE_CONVERT (float, Float, FLOAT, /*NOOP*/);
110 DEFINE_CONVERT (double, Double, DOUBLE, /*NOOP*/);
111
112 static void
113 convert_g_value_to_boolean (GstDirectControlBinding * self, gdouble s,
114     GValue * d)
115 {
116   s = CLAMP (s, 0.0, 1.0);
117   g_value_set_boolean (d, (gboolean) (s + 0.5));
118 }
119
120 static void
121 convert_value_to_boolean (GstDirectControlBinding * self, gdouble s,
122     gpointer d_)
123 {
124   gboolean *d = (gboolean *) d_;
125
126   s = CLAMP (s, 0.0, 1.0);
127   *d = (gboolean) (s + 0.5);
128 }
129
130 static void
131 convert_g_value_to_enum (GstDirectControlBinding * self, gdouble s, GValue * d)
132 {
133   GParamSpecEnum *pspec =
134       G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
135   GEnumClass *e = pspec->enum_class;
136   gint v;
137
138   s = CLAMP (s, 0.0, 1.0);
139   v = s * (e->n_values - 1);
140   g_value_set_enum (d, e->values[v].value);
141 }
142
143 static void
144 convert_value_to_enum (GstDirectControlBinding * self, gdouble s, gpointer d_)
145 {
146   GParamSpecEnum *pspec =
147       G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
148   GEnumClass *e = pspec->enum_class;
149   gint *d = (gint *) d_;
150
151   s = CLAMP (s, 0.0, 1.0);
152   *d = e->values[(gint) (s * (e->n_values - 1))].value;
153 }
154
155 /* vmethods */
156
157 static void
158 gst_direct_control_binding_class_init (GstDirectControlBindingClass * klass)
159 {
160   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
161   GstControlBindingClass *control_binding_class =
162       GST_CONTROL_BINDING_CLASS (klass);
163
164   gobject_class->constructor = gst_direct_control_binding_constructor;
165   gobject_class->set_property = gst_direct_control_binding_set_property;
166   gobject_class->get_property = gst_direct_control_binding_get_property;
167   gobject_class->dispose = gst_direct_control_binding_dispose;
168   gobject_class->finalize = gst_direct_control_binding_finalize;
169
170   control_binding_class->sync_values = gst_direct_control_binding_sync_values;
171   control_binding_class->get_value = gst_direct_control_binding_get_value;
172   control_binding_class->get_value_array =
173       gst_direct_control_binding_get_value_array;
174   control_binding_class->get_g_value_array =
175       gst_direct_control_binding_get_g_value_array;
176
177   properties[PROP_CS] =
178       g_param_spec_object ("control-source", "ControlSource",
179       "The control source",
180       GST_TYPE_CONTROL_SOURCE,
181       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
182
183   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
184 }
185
186 static void
187 gst_direct_control_binding_init (GstDirectControlBinding * self)
188 {
189 }
190
191 static GObject *
192 gst_direct_control_binding_constructor (GType type, guint n_construct_params,
193     GObjectConstructParam * construct_params)
194 {
195   GstDirectControlBinding *self;
196
197   self =
198       GST_DIRECT_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
199       (type, n_construct_params, construct_params));
200
201   if (GST_CONTROL_BINDING_PSPEC (self)) {
202     GType type, base;
203
204     base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self));
205     g_value_init (&self->cur_value, type);
206     while ((type = g_type_parent (type)))
207       base = type;
208
209     GST_DEBUG ("  using type %s", g_type_name (base));
210
211     /* select mapping function */
212     switch (base) {
213       case G_TYPE_INT:
214         self->convert_g_value = convert_g_value_to_int;
215         self->convert_value = convert_value_to_int;
216         self->byte_size = sizeof (gint);
217         break;
218       case G_TYPE_UINT:
219         self->convert_g_value = convert_g_value_to_uint;
220         self->convert_value = convert_value_to_uint;
221         self->byte_size = sizeof (guint);
222         break;
223       case G_TYPE_LONG:
224         self->convert_g_value = convert_g_value_to_long;
225         self->convert_value = convert_value_to_long;
226         self->byte_size = sizeof (glong);
227         break;
228       case G_TYPE_ULONG:
229         self->convert_g_value = convert_g_value_to_ulong;
230         self->convert_value = convert_value_to_ulong;
231         self->byte_size = sizeof (gulong);
232         break;
233       case G_TYPE_INT64:
234         self->convert_g_value = convert_g_value_to_int64;
235         self->convert_value = convert_value_to_int64;
236         self->byte_size = sizeof (gint64);
237         break;
238       case G_TYPE_UINT64:
239         self->convert_g_value = convert_g_value_to_uint64;
240         self->convert_value = convert_value_to_uint64;
241         self->byte_size = sizeof (guint64);
242         break;
243       case G_TYPE_FLOAT:
244         self->convert_g_value = convert_g_value_to_float;
245         self->convert_value = convert_value_to_float;
246         self->byte_size = sizeof (gfloat);
247         break;
248       case G_TYPE_DOUBLE:
249         self->convert_g_value = convert_g_value_to_double;
250         self->convert_value = convert_value_to_double;
251         self->byte_size = sizeof (gdouble);
252         break;
253       case G_TYPE_BOOLEAN:
254         self->convert_g_value = convert_g_value_to_boolean;
255         self->convert_value = convert_value_to_boolean;
256         self->byte_size = sizeof (gboolean);
257         break;
258       case G_TYPE_ENUM:
259         self->convert_g_value = convert_g_value_to_enum;
260         self->convert_value = convert_value_to_enum;
261         self->byte_size = sizeof (gint);
262         break;
263       default:
264         GST_WARNING ("incomplete implementation for paramspec type '%s'",
265             G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
266         GST_CONTROL_BINDING_PSPEC (self) = NULL;
267         break;
268     }
269   }
270   return (GObject *) self;
271 }
272
273 static void
274 gst_direct_control_binding_set_property (GObject * object, guint prop_id,
275     const GValue * value, GParamSpec * pspec)
276 {
277   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
278
279   switch (prop_id) {
280     case PROP_CS:
281       self->cs = g_value_dup_object (value);
282       break;
283     default:
284       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
285       break;
286   }
287 }
288
289 static void
290 gst_direct_control_binding_get_property (GObject * object, guint prop_id,
291     GValue * value, GParamSpec * pspec)
292 {
293   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
294
295   switch (prop_id) {
296     case PROP_CS:
297       g_value_set_object (value, self->cs);
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302   }
303 }
304
305 static void
306 gst_direct_control_binding_dispose (GObject * object)
307 {
308   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
309
310   if (self->cs)
311     gst_object_replace ((GstObject **) & self->cs, NULL);
312
313   G_OBJECT_CLASS (parent_class)->dispose (object);
314 }
315
316 static void
317 gst_direct_control_binding_finalize (GObject * object)
318 {
319   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
320
321   g_value_unset (&self->cur_value);
322
323   G_OBJECT_CLASS (parent_class)->finalize (object);
324 }
325
326 static gboolean
327 gst_direct_control_binding_sync_values (GstControlBinding * _self,
328     GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
329 {
330   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
331   gdouble src_val;
332   gboolean ret;
333
334   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
335   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
336
337   GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
338       _self->name, GST_TIME_ARGS (timestamp));
339
340   ret = gst_control_source_get_value (self->cs, timestamp, &src_val);
341   if (G_LIKELY (ret)) {
342     GST_LOG_OBJECT (object, "  new value %lf", src_val);
343     /* always set the value for first time, but then only if it changed
344      * this should limit g_object_notify invocations.
345      * FIXME: can we detect negative playback rates?
346      */
347     if ((timestamp < last_sync) || (src_val != self->last_value)) {
348       GValue *dst_val = &self->cur_value;
349
350       GST_LOG_OBJECT (object, "  mapping %s to value of type %s", _self->name,
351           G_VALUE_TYPE_NAME (dst_val));
352       /* run mapping function to convert gdouble to GValue */
353       self->convert_g_value (self, src_val, dst_val);
354       /* we can make this faster
355        * http://bugzilla.gnome.org/show_bug.cgi?id=536939
356        */
357       g_object_set_property ((GObject *) object, _self->name, dst_val);
358       self->last_value = src_val;
359     }
360   } else {
361     GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
362   }
363   return (ret);
364 }
365
366 static GValue *
367 gst_direct_control_binding_get_value (GstControlBinding * _self,
368     GstClockTime timestamp)
369 {
370   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
371   GValue *dst_val = NULL;
372   gdouble src_val;
373
374   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), NULL);
375   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
376   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
377
378   /* get current value via control source */
379   if (gst_control_source_get_value (self->cs, timestamp, &src_val)) {
380     dst_val = g_new0 (GValue, 1);
381     g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (_self->pspec));
382     self->convert_g_value (self, src_val, dst_val);
383   } else {
384     GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
385         _self->name, GST_TIME_ARGS (timestamp));
386   }
387
388   return dst_val;
389 }
390
391 static gboolean
392 gst_direct_control_binding_get_value_array (GstControlBinding * _self,
393     GstClockTime timestamp, GstClockTime interval, guint n_values,
394     gpointer values_)
395 {
396   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
397   gint i;
398   gdouble *src_val;
399   gboolean res = FALSE;
400   GstDirectControlBindingConvertValue convert;
401   gint byte_size;
402   guint8 *values = (guint8 *) values_;
403
404   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
405   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
406   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
407   g_return_val_if_fail (values, FALSE);
408   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
409
410   convert = self->convert_value;
411   byte_size = self->byte_size;
412
413   src_val = g_new0 (gdouble, n_values);
414   if ((res = gst_control_source_get_value_array (self->cs, timestamp,
415               interval, n_values, src_val))) {
416     for (i = 0; i < n_values; i++) {
417       /* we will only get NAN for sparse control sources, such as triggers */
418       if (!isnan (src_val[i])) {
419         convert (self, src_val[i], (gpointer) values);
420       } else {
421         GST_LOG ("no control value for property %s at index %d", _self->name,
422             i);
423       }
424       values += byte_size;
425     }
426   } else {
427     GST_LOG ("failed to get control value for property %s at ts %"
428         GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
429   }
430   g_free (src_val);
431   return res;
432 }
433
434 static gboolean
435 gst_direct_control_binding_get_g_value_array (GstControlBinding * _self,
436     GstClockTime timestamp, GstClockTime interval, guint n_values,
437     GValue * values)
438 {
439   GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
440   gint i;
441   gdouble *src_val;
442   gboolean res = FALSE;
443   GType type;
444   GstDirectControlBindingConvertGValue convert;
445
446   g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
447   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
448   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
449   g_return_val_if_fail (values, FALSE);
450   g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
451
452   convert = self->convert_g_value;
453   type = G_PARAM_SPEC_VALUE_TYPE (_self->pspec);
454
455   src_val = g_new0 (gdouble, n_values);
456   if ((res = gst_control_source_get_value_array (self->cs, timestamp,
457               interval, n_values, src_val))) {
458     for (i = 0; i < n_values; i++) {
459       /* we will only get NAN for sparse control sources, such as triggers */
460       if (!isnan (src_val[i])) {
461         g_value_init (&values[i], type);
462         convert (self, src_val[i], &values[i]);
463       } else {
464         GST_LOG ("no control value for property %s at index %d", _self->name,
465             i);
466       }
467     }
468   } else {
469     GST_LOG ("failed to get control value for property %s at ts %"
470         GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
471   }
472   g_free (src_val);
473   return res;
474 }
475
476 /* functions */
477
478 /**
479  * gst_direct_control_binding_new:
480  * @object: the object of the property
481  * @property_name: the property-name to attach the control source
482  * @cs: the control source
483  *
484  * Create a new control-binding that attaches the #GstControlSource to the
485  * #GObject property.
486  *
487  * Returns: (transfer floating): the new #GstDirectControlBinding
488  */
489 GstControlBinding *
490 gst_direct_control_binding_new (GstObject * object, const gchar * property_name,
491     GstControlSource * cs)
492 {
493   return (GstControlBinding *) g_object_new (GST_TYPE_DIRECT_CONTROL_BINDING,
494       "object", object, "name", property_name, "control-source", cs, NULL);
495 }