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 [0.0 ... 1.0] to the target property range. If a
28 * control value is outside of the range, it will be clipped.
31 #include <glib-object.h>
34 #include "gstdirectcontrolbinding.h"
36 #include <gst/math-compat.h>
38 #define GST_CAT_DEFAULT control_binding_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
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);
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,
57 static gboolean gst_direct_control_binding_get_g_value_array (GstControlBinding
58 * _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
62 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstdirectcontrolbinding", 0, \
63 "dynamic parameter control source attachment");
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);
76 static GParamSpec *properties[PROP_LAST];
78 /* mapping functions */
80 #define DEFINE_CONVERT(type,Type,TYPE,ROUNDING_OP) \
82 convert_g_value_to_##type (GstDirectControlBinding *self, gdouble s, GValue *d) \
84 GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
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); \
93 convert_value_to_##type (GstDirectControlBinding *self, gdouble s, gpointer d_) \
95 GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (((GstControlBinding *)self)->pspec); \
96 g##type *d = (g##type *)d_; \
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); \
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*/);
113 convert_g_value_to_boolean (GstDirectControlBinding * self, gdouble s,
116 s = CLAMP (s, 0.0, 1.0);
117 g_value_set_boolean (d, (gboolean) (s + 0.5));
121 convert_value_to_boolean (GstDirectControlBinding * self, gdouble s,
124 gboolean *d = (gboolean *) d_;
126 s = CLAMP (s, 0.0, 1.0);
127 *d = (gboolean) (s + 0.5);
131 convert_g_value_to_enum (GstDirectControlBinding * self, gdouble s, GValue * d)
133 GParamSpecEnum *pspec =
134 G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
135 GEnumClass *e = pspec->enum_class;
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);
144 convert_value_to_enum (GstDirectControlBinding * self, gdouble s, gpointer d_)
146 GParamSpecEnum *pspec =
147 G_PARAM_SPEC_ENUM (((GstControlBinding *) self)->pspec);
148 GEnumClass *e = pspec->enum_class;
149 gint *d = (gint *) d_;
151 s = CLAMP (s, 0.0, 1.0);
152 *d = e->values[(gint) (s * (e->n_values - 1))].value;
158 gst_direct_control_binding_class_init (GstDirectControlBindingClass * klass)
160 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
161 GstControlBindingClass *control_binding_class =
162 GST_CONTROL_BINDING_CLASS (klass);
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;
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;
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);
183 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
187 gst_direct_control_binding_init (GstDirectControlBinding * self)
192 gst_direct_control_binding_constructor (GType type, guint n_construct_params,
193 GObjectConstructParam * construct_params)
195 GstDirectControlBinding *self;
198 GST_DIRECT_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
199 (type, n_construct_params, construct_params));
201 if (GST_CONTROL_BINDING_PSPEC (self)) {
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)))
209 GST_DEBUG (" using type %s", g_type_name (base));
211 /* select mapping function */
214 self->convert_g_value = convert_g_value_to_int;
215 self->convert_value = convert_value_to_int;
216 self->byte_size = sizeof (gint);
219 self->convert_g_value = convert_g_value_to_uint;
220 self->convert_value = convert_value_to_uint;
221 self->byte_size = sizeof (guint);
224 self->convert_g_value = convert_g_value_to_long;
225 self->convert_value = convert_value_to_long;
226 self->byte_size = sizeof (glong);
229 self->convert_g_value = convert_g_value_to_ulong;
230 self->convert_value = convert_value_to_ulong;
231 self->byte_size = sizeof (gulong);
234 self->convert_g_value = convert_g_value_to_int64;
235 self->convert_value = convert_value_to_int64;
236 self->byte_size = sizeof (gint64);
239 self->convert_g_value = convert_g_value_to_uint64;
240 self->convert_value = convert_value_to_uint64;
241 self->byte_size = sizeof (guint64);
244 self->convert_g_value = convert_g_value_to_float;
245 self->convert_value = convert_value_to_float;
246 self->byte_size = sizeof (gfloat);
249 self->convert_g_value = convert_g_value_to_double;
250 self->convert_value = convert_value_to_double;
251 self->byte_size = sizeof (gdouble);
254 self->convert_g_value = convert_g_value_to_boolean;
255 self->convert_value = convert_value_to_boolean;
256 self->byte_size = sizeof (gboolean);
259 self->convert_g_value = convert_g_value_to_enum;
260 self->convert_value = convert_value_to_enum;
261 self->byte_size = sizeof (gint);
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;
270 return (GObject *) self;
274 gst_direct_control_binding_set_property (GObject * object, guint prop_id,
275 const GValue * value, GParamSpec * pspec)
277 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
281 self->cs = g_value_dup_object (value);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gst_direct_control_binding_get_property (GObject * object, guint prop_id,
291 GValue * value, GParamSpec * pspec)
293 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
297 g_value_set_object (value, self->cs);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gst_direct_control_binding_dispose (GObject * object)
308 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
311 gst_object_replace ((GstObject **) & self->cs, NULL);
313 G_OBJECT_CLASS (parent_class)->dispose (object);
317 gst_direct_control_binding_finalize (GObject * object)
319 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (object);
321 g_value_unset (&self->cur_value);
323 G_OBJECT_CLASS (parent_class)->finalize (object);
327 gst_direct_control_binding_sync_values (GstControlBinding * _self,
328 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
330 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
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);
337 GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
338 _self->name, GST_TIME_ARGS (timestamp));
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?
347 if ((timestamp < last_sync) || (src_val != self->last_value)) {
348 GValue *dst_val = &self->cur_value;
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
357 g_object_set_property ((GObject *) object, _self->name, dst_val);
358 self->last_value = src_val;
361 GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
367 gst_direct_control_binding_get_value (GstControlBinding * _self,
368 GstClockTime timestamp)
370 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
371 GValue *dst_val = NULL;
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);
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);
384 GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
385 _self->name, GST_TIME_ARGS (timestamp));
392 gst_direct_control_binding_get_value_array (GstControlBinding * _self,
393 GstClockTime timestamp, GstClockTime interval, guint n_values,
396 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
399 gboolean res = FALSE;
400 GstDirectControlBindingConvertValue convert;
402 guint8 *values = (guint8 *) values_;
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);
410 convert = self->convert_value;
411 byte_size = self->byte_size;
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);
421 GST_LOG ("no control value for property %s at index %d", _self->name,
427 GST_LOG ("failed to get control value for property %s at ts %"
428 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
435 gst_direct_control_binding_get_g_value_array (GstControlBinding * _self,
436 GstClockTime timestamp, GstClockTime interval, guint n_values,
439 GstDirectControlBinding *self = GST_DIRECT_CONTROL_BINDING (_self);
442 gboolean res = FALSE;
444 GstDirectControlBindingConvertGValue convert;
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);
452 convert = self->convert_g_value;
453 type = G_PARAM_SPEC_VALUE_TYPE (_self->pspec);
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]);
464 GST_LOG ("no control value for property %s at index %d", _self->name,
469 GST_LOG ("failed to get control value for property %s at ts %"
470 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
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
484 * Create a new control-binding that attaches the #GstControlSource to the
487 * Returns: (transfer floating): the new #GstDirectControlBinding
490 gst_direct_control_binding_new (GstObject * object, const gchar * property_name,
491 GstControlSource * cs)
493 return (GstControlBinding *) g_object_new (GST_TYPE_DIRECT_CONTROL_BINDING,
494 "object", object, "name", property_name, "control-source", cs, NULL);