3 * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
5 * gstdirectcontrolbinding.c: Direct attachment for control sources
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.
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.
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.
23 * SECTION:gstdirectcontrolbinding
24 * @short_description: direct attachment for control sources
26 * A value mapping object that attaches control sources to gobject properties. It
27 * will map the control values directly to the target property range. If a
28 * non-absolute direct control binding is used, the value range [0.0 ... 1.0]
29 * is mapped to full target property range, and all values outside the range
30 * will be clipped. An absolute control binding will not do any value
34 #include <glib-object.h>
37 #include "gstdirectcontrolbinding.h"
39 #include <gst/math-compat.h>
41 #define GST_CAT_DEFAULT control_binding_debug
42 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
45 static GObject *gst_direct_control_binding_constructor (GType type,
46 guint n_construct_params, GObjectConstructParam * construct_params);
47 static void gst_direct_control_binding_set_property (GObject * object,
48 guint prop_id, const GValue * value, GParamSpec * pspec);
49 static void gst_direct_control_binding_get_property (GObject * object,
50 guint prop_id, GValue * value, GParamSpec * pspec);
51 static void gst_direct_control_binding_dispose (GObject * object);
52 static void gst_direct_control_binding_finalize (GObject * object);
54 static gboolean gst_direct_control_binding_sync_values (GstControlBinding *
55 _self, GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
56 static GValue *gst_direct_control_binding_get_value (GstControlBinding * _self,
57 GstClockTime timestamp);
58 static gboolean gst_direct_control_binding_get_value_array (GstControlBinding *
59 _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
61 static gboolean gst_direct_control_binding_get_g_value_array (GstControlBinding
62 * _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
66 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstdirectcontrolbinding", 0, \
67 "dynamic parameter control source attachment");
69 #define gst_direct_control_binding_parent_class parent_class
70 G_DEFINE_TYPE_WITH_CODE (GstDirectControlBinding, gst_direct_control_binding,
71 GST_TYPE_CONTROL_BINDING, _do_init);
81 static GParamSpec *properties[PROP_LAST];
83 /* mapping functions */
85 #define DEFINE_CONVERT(type,Type,TYPE,ROUNDING_OP) \
87 convert_g_value_to_##type (GstDirectControlBinding *self, gdouble s, GValue *d) \
89 GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
92 s = CLAMP (s, 0.0, 1.0); \
93 v = (g##type) ROUNDING_OP (pspec->minimum * (1-s)) + (g##type) ROUNDING_OP (pspec->maximum * s); \
94 g_value_set_##type (d, v); \
98 convert_value_to_##type (GstDirectControlBinding *self, gdouble s, gpointer d_) \
100 GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
101 g##type *d = (g##type *)d_; \
103 s = CLAMP (s, 0.0, 1.0); \
104 *d = (g##type) ROUNDING_OP (pspec->minimum * (1-s)) + (g##type) ROUNDING_OP (pspec->maximum * s); \
108 abs_convert_g_value_to_##type (GstDirectControlBinding *self, gdouble s, GValue *d) \
111 v = (g##type) ROUNDING_OP (s); \
112 g_value_set_##type (d, v); \
116 abs_convert_value_to_##type (GstDirectControlBinding *self, gdouble s, gpointer d_) \
118 g##type *d = (g##type *)d_; \
119 *d = (g##type) ROUNDING_OP (s); \
122 DEFINE_CONVERT (int, Int, INT, rint);
123 DEFINE_CONVERT (uint, UInt, UINT, rint);
124 DEFINE_CONVERT (long, Long, LONG, rint);
125 DEFINE_CONVERT (ulong, ULong, ULONG, rint);
126 DEFINE_CONVERT (int64, Int64, INT64, rint);
127 DEFINE_CONVERT (uint64, UInt64, UINT64, rint);
128 DEFINE_CONVERT (float, Float, FLOAT, /*NOOP*/);
129 DEFINE_CONVERT (double, Double, DOUBLE, /*NOOP*/);
132 convert_g_value_to_boolean (GstDirectControlBinding * self, gdouble s,
135 s = CLAMP (s, 0.0, 1.0);
136 g_value_set_boolean (d, (gboolean) (s + 0.5));
140 convert_value_to_boolean (GstDirectControlBinding * self, gdouble s,
143 gboolean *d = (gboolean *) d_;
145 s = CLAMP (s, 0.0, 1.0);
146 *d = (gboolean) (s + 0.5);
150 convert_g_value_to_enum (GstDirectControlBinding * self, gdouble s, GValue * d)
152 GParamSpecEnum *pspec =
153 G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
154 GEnumClass *e = pspec->enum_class;
157 s = CLAMP (s, 0.0, 1.0);
158 v = s * (e->n_values - 1);
159 g_value_set_enum (d, e->values[v].value);
163 convert_value_to_enum (GstDirectControlBinding * self, gdouble s, gpointer d_)
165 GParamSpecEnum *pspec =
166 G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
167 GEnumClass *e = pspec->enum_class;
168 gint *d = (gint *) d_;
170 s = CLAMP (s, 0.0, 1.0);
171 *d = e->values[(gint) (s * (e->n_values - 1))].value;
177 gst_direct_control_binding_class_init (GstDirectControlBindingClass * klass)
179 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
180 GstControlBindingClass *control_binding_class =
181 GST_CONTROL_BINDING_CLASS (klass);
183 gobject_class->constructor = gst_direct_control_binding_constructor;
184 gobject_class->set_property = gst_direct_control_binding_set_property;
185 gobject_class->get_property = gst_direct_control_binding_get_property;
186 gobject_class->dispose = gst_direct_control_binding_dispose;
187 gobject_class->finalize = gst_direct_control_binding_finalize;
189 control_binding_class->sync_values = gst_direct_control_binding_sync_values;
190 control_binding_class->get_value = gst_direct_control_binding_get_value;
191 control_binding_class->get_value_array =
192 gst_direct_control_binding_get_value_array;
193 control_binding_class->get_g_value_array =
194 gst_direct_control_binding_get_g_value_array;
196 properties[PROP_CS] =
197 g_param_spec_object ("control-source", "ControlSource",
198 "The control source",
199 GST_TYPE_CONTROL_SOURCE,
200 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
202 properties[PROP_ABSOLUTE] =
203 g_param_spec_boolean ("absolute", "Absolute",
204 "Whether the control values are absolute",
206 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
208 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
212 gst_direct_control_binding_init (GstDirectControlBinding * self)
217 gst_direct_control_binding_constructor (GType type, guint n_construct_params,
218 GObjectConstructParam * construct_params)
220 GstDirectControlBinding *self;
223 GST_DIRECT_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
224 (type, n_construct_params, construct_params));
226 if (GST_CONTROL_BINDING_PSPEC (self)) {
229 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self));
230 g_value_init (&self->cur_value, type);
231 while ((type = g_type_parent (type)))
234 GST_DEBUG (" using type %s", g_type_name (base));
236 /* select mapping function */
238 #define SET_CONVERT_FUNCTION(type) \
239 if (self->ABI.abi.want_absolute) { \
240 self->convert_g_value = abs_convert_g_value_to_##type; \
241 self->convert_value = abs_convert_value_to_##type; \
244 self->convert_g_value = convert_g_value_to_##type; \
245 self->convert_value = convert_value_to_##type; \
247 self->byte_size = sizeof (g##type);
252 SET_CONVERT_FUNCTION (int);
255 SET_CONVERT_FUNCTION (uint);
258 SET_CONVERT_FUNCTION (long);
261 SET_CONVERT_FUNCTION (ulong);
264 SET_CONVERT_FUNCTION (int64);
267 SET_CONVERT_FUNCTION (uint64);
270 SET_CONVERT_FUNCTION (float);
273 SET_CONVERT_FUNCTION (double);
276 self->convert_g_value = convert_g_value_to_boolean;
277 self->convert_value = convert_value_to_boolean;
278 self->byte_size = sizeof (gboolean);
281 self->convert_g_value = convert_g_value_to_enum;
282 self->convert_value = convert_value_to_enum;
283 self->byte_size = sizeof (gint);
286 GST_WARNING ("incomplete implementation for paramspec type '%s'",
287 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
288 GST_CONTROL_BINDING_PSPEC (self) = NULL;
292 return (GObject *) self;
296 gst_direct_control_binding_set_property (GObject * object, guint prop_id,
297 const GValue * value, GParamSpec * pspec)
299 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
303 self->cs = g_value_dup_object (value);
306 self->ABI.abi.want_absolute = g_value_get_boolean (value);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 gst_direct_control_binding_get_property (GObject * object, guint prop_id,
316 GValue * value, GParamSpec * pspec)
318 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
322 g_value_set_object (value, self->cs);
325 g_value_set_boolean (value, self->ABI.abi.want_absolute);
328 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334 gst_direct_control_binding_dispose (GObject * object)
336 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
339 gst_object_replace ((GstObject **) & self->cs, NULL);
341 G_OBJECT_CLASS (parent_class)->dispose (object);
345 gst_direct_control_binding_finalize (GObject * object)
347 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
349 if (G_IS_VALUE (&self->cur_value))
350 g_value_unset (&self->cur_value);
352 G_OBJECT_CLASS (parent_class)->finalize (object);
356 gst_direct_control_binding_sync_values (GstControlBinding * _self,
357 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
359 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
363 g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
364 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
366 GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
367 _self->name, GST_TIME_ARGS (timestamp));
369 ret = gst_control_source_get_value (self->cs, timestamp, &src_val);
370 if (G_LIKELY (ret)) {
371 GST_LOG_OBJECT (object, " new value %lf", src_val);
372 /* always set the value for first time, but then only if it changed
373 * this should limit g_object_notify invocations.
374 * FIXME: can we detect negative playback rates?
376 if ((timestamp < last_sync) || (src_val != self->last_value)) {
377 GValue *dst_val = &self->cur_value;
379 GST_LOG_OBJECT (object, " mapping %s to value of type %s", _self->name,
380 G_VALUE_TYPE_NAME (dst_val));
381 /* run mapping function to convert gdouble to GValue */
382 self->convert_g_value (self, src_val, dst_val);
383 /* we can make this faster
384 * http://bugzilla.gnome.org/show_bug.cgi?id=536939
386 g_object_set_property ((GObject *) object, _self->name, dst_val);
387 self->last_value = src_val;
390 GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
396 gst_direct_control_binding_get_value (GstControlBinding * _self,
397 GstClockTime timestamp)
399 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
400 GValue *dst_val = NULL;
403 g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), NULL);
404 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
405 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
407 /* get current value via control source */
408 if (gst_control_source_get_value (self->cs, timestamp, &src_val)) {
409 dst_val = g_new0 (GValue, 1);
410 g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (_self->pspec));
411 self->convert_g_value (self, src_val, dst_val);
413 GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
414 _self->name, GST_TIME_ARGS (timestamp));
421 gst_direct_control_binding_get_value_array (GstControlBinding * _self,
422 GstClockTime timestamp, GstClockTime interval, guint n_values,
425 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
428 gboolean res = FALSE;
429 GstDirectControlBindingConvertValue convert;
431 guint8 *values = (guint8 *) values_;
433 g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
434 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
435 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
436 g_return_val_if_fail (values, FALSE);
437 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
439 convert = self->convert_value;
440 byte_size = self->byte_size;
442 src_val = g_new0 (gdouble, n_values);
443 if ((res = gst_control_source_get_value_array (self->cs, timestamp,
444 interval, n_values, src_val))) {
445 for (i = 0; i < n_values; i++) {
446 /* we will only get NAN for sparse control sources, such as triggers */
447 if (!isnan (src_val[i])) {
448 convert (self, src_val[i], (gpointer) values);
450 GST_LOG ("no control value for property %s at index %d", _self->name,
456 GST_LOG ("failed to get control value for property %s at ts %"
457 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
464 gst_direct_control_binding_get_g_value_array (GstControlBinding * _self,
465 GstClockTime timestamp, GstClockTime interval, guint n_values,
468 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
471 gboolean res = FALSE;
473 GstDirectControlBindingConvertGValue convert;
475 g_return_val_if_fail (GST_IS_DIRECT_CONTROL_BINDING (self), FALSE);
476 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
477 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
478 g_return_val_if_fail (values, FALSE);
479 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
481 convert = self->convert_g_value;
482 type = G_PARAM_SPEC_VALUE_TYPE (_self->pspec);
484 src_val = g_new0 (gdouble, n_values);
485 if ((res = gst_control_source_get_value_array (self->cs, timestamp,
486 interval, n_values, src_val))) {
487 for (i = 0; i < n_values; i++) {
488 /* we will only get NAN for sparse control sources, such as triggers */
489 if (!isnan (src_val[i])) {
490 g_value_init (&values[i], type);
491 convert (self, src_val[i], &values[i]);
493 GST_LOG ("no control value for property %s at index %d", _self->name,
498 GST_LOG ("failed to get control value for property %s at ts %"
499 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
508 * gst_direct_control_binding_new:
509 * @object: the object of the property
510 * @property_name: the property-name to attach the control source
511 * @cs: the control source
513 * Create a new control-binding that attaches the #GstControlSource to the
514 * #GObject property. It will map the control source range [0.0 ... 1.0] to
515 * the full target property range, and clip all values outside this range.
517 * Returns: (transfer floating): the new #GstDirectControlBinding
520 gst_direct_control_binding_new (GstObject * object, const gchar * property_name,
521 GstControlSource * cs)
523 return (GstControlBinding *) g_object_new (GST_TYPE_DIRECT_CONTROL_BINDING,
524 "object", object, "name", property_name, "control-source", cs, NULL);
528 * gst_direct_control_binding_new_absolute:
529 * @object: the object of the property
530 * @property_name: the property-name to attach the control source
531 * @cs: the control source
533 * Create a new control-binding that attaches the #GstControlSource to the
534 * #GObject property. It will directly map the control source values to the
535 * target property range without any transformations.
537 * Returns: (transfer floating): the new #GstDirectControlBinding
542 gst_direct_control_binding_new_absolute (GstObject * object,
543 const gchar * property_name, GstControlSource * cs)
545 return (GstControlBinding *) g_object_new (GST_TYPE_DIRECT_CONTROL_BINDING,
546 "object", object, "name", property_name, "control-source", cs, "absolute",