3 * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
5 * gstargbcontrolbinding.c: Attachment for multiple control sources to gargb
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 * SECTION:gstargbcontrolbinding
25 * @title: GstARGBControlBinding
26 * @short_description: attachment for control sources to argb properties
28 * A value mapping object that attaches multiple control sources to a guint
29 * gobject properties representing a color. A control value of 0.0 will turn the
30 * color component off and a value of 1.0 will be the color level.
33 #include <glib-object.h>
36 #include "gstargbcontrolbinding.h"
38 #include <gst/math-compat.h>
40 #define GST_CAT_DEFAULT control_binding_debug
41 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
43 static GObject *gst_argb_control_binding_constructor (GType type,
44 guint n_construct_params, GObjectConstructParam * construct_params);
45 static void gst_argb_control_binding_set_property (GObject * object,
46 guint prop_id, const GValue * value, GParamSpec * pspec);
47 static void gst_argb_control_binding_get_property (GObject * object,
48 guint prop_id, GValue * value, GParamSpec * pspec);
49 static void gst_argb_control_binding_dispose (GObject * object);
50 static void gst_argb_control_binding_finalize (GObject * object);
52 static gboolean gst_argb_control_binding_sync_values (GstControlBinding * _self,
53 GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
54 static GValue *gst_argb_control_binding_get_value (GstControlBinding * _self,
55 GstClockTime timestamp);
56 static gboolean gst_argb_control_binding_get_value_array (GstControlBinding *
57 _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
59 static gboolean gst_argb_control_binding_get_g_value_array (GstControlBinding *
60 _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
64 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstargbcontrolbinding", 0, \
65 "dynamic parameter control source attachment");
67 #define gst_argb_control_binding_parent_class parent_class
68 G_DEFINE_TYPE_WITH_CODE (GstARGBControlBinding, gst_argb_control_binding,
69 GST_TYPE_CONTROL_BINDING, _do_init);
81 static GParamSpec *properties[PROP_LAST];
86 gst_argb_control_binding_class_init (GstARGBControlBindingClass * klass)
88 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 GstControlBindingClass *control_binding_class =
90 GST_CONTROL_BINDING_CLASS (klass);
92 gobject_class->constructor = gst_argb_control_binding_constructor;
93 gobject_class->set_property = gst_argb_control_binding_set_property;
94 gobject_class->get_property = gst_argb_control_binding_get_property;
95 gobject_class->dispose = gst_argb_control_binding_dispose;
96 gobject_class->finalize = gst_argb_control_binding_finalize;
98 control_binding_class->sync_values = gst_argb_control_binding_sync_values;
99 control_binding_class->get_value = gst_argb_control_binding_get_value;
100 control_binding_class->get_value_array =
101 gst_argb_control_binding_get_value_array;
102 control_binding_class->get_g_value_array =
103 gst_argb_control_binding_get_g_value_array;
105 properties[PROP_CS_A] =
106 g_param_spec_object ("control-source-a", "ControlSource A",
107 "The control source for the alpha color component",
108 GST_TYPE_CONTROL_SOURCE,
109 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
111 properties[PROP_CS_R] =
112 g_param_spec_object ("control-source-r", "ControlSource R",
113 "The control source for the red color component",
114 GST_TYPE_CONTROL_SOURCE,
115 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
117 properties[PROP_CS_G] =
118 g_param_spec_object ("control-source-g", "ControlSource G",
119 "The control source for the green color component",
120 GST_TYPE_CONTROL_SOURCE,
121 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
123 properties[PROP_CS_B] =
124 g_param_spec_object ("control-source-b", "ControlSource B",
125 "The control source for the blue color component",
126 GST_TYPE_CONTROL_SOURCE,
127 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
129 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
133 gst_argb_control_binding_init (GstARGBControlBinding * self)
138 gst_argb_control_binding_constructor (GType type, guint n_construct_params,
139 GObjectConstructParam * construct_params)
141 GstARGBControlBinding *self;
144 GST_ARGB_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
145 (type, n_construct_params, construct_params));
147 if (GST_CONTROL_BINDING_PSPEC (self)) {
148 if (!(G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self)) ==
150 GST_WARNING ("can't bind to paramspec type '%s'",
151 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
152 GST_CONTROL_BINDING_PSPEC (self) = NULL;
154 g_value_init (&self->cur_value, G_TYPE_UINT);
157 return (GObject *) self;
161 gst_argb_control_binding_set_property (GObject * object, guint prop_id,
162 const GValue * value, GParamSpec * pspec)
164 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
168 gst_object_replace ((GstObject **) & self->cs_a,
169 g_value_get_object (value));
172 gst_object_replace ((GstObject **) & self->cs_r,
173 g_value_get_object (value));
176 gst_object_replace ((GstObject **) & self->cs_g,
177 g_value_get_object (value));
180 gst_object_replace ((GstObject **) & self->cs_b,
181 g_value_get_object (value));
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
190 gst_argb_control_binding_get_property (GObject * object, guint prop_id,
191 GValue * value, GParamSpec * pspec)
193 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
197 g_value_set_object (value, self->cs_a);
200 g_value_set_object (value, self->cs_r);
203 g_value_set_object (value, self->cs_g);
206 g_value_set_object (value, self->cs_b);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 gst_argb_control_binding_dispose (GObject * object)
217 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
219 gst_object_replace ((GstObject **) & self->cs_a, NULL);
220 gst_object_replace ((GstObject **) & self->cs_r, NULL);
221 gst_object_replace ((GstObject **) & self->cs_g, NULL);
222 gst_object_replace ((GstObject **) & self->cs_b, NULL);
224 G_OBJECT_CLASS (parent_class)->dispose (object);
228 gst_argb_control_binding_finalize (GObject * object)
230 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
232 g_value_unset (&self->cur_value);
234 G_OBJECT_CLASS (parent_class)->finalize (object);
238 gst_argb_control_binding_sync_values (GstControlBinding * _self,
239 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
241 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
242 gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
245 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
246 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
248 GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
249 _self->name, GST_TIME_ARGS (timestamp));
252 ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
254 ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
256 ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
258 ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
259 if (G_LIKELY (ret)) {
260 guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
261 (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
262 (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
263 ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
264 GST_LOG_OBJECT (object, " new value 0x%08x", src_val);
265 /* always set the value for first time, but then only if it changed
266 * this should limit g_object_notify invocations.
267 * FIXME: can we detect negative playback rates?
269 if ((timestamp < last_sync) || (src_val != self->last_value)) {
270 GValue *dst_val = &self->cur_value;
272 g_value_set_uint (dst_val, src_val);
273 /* we can make this faster
274 * http://bugzilla.gnome.org/show_bug.cgi?id=536939
276 g_object_set_property ((GObject *) object, _self->name, dst_val);
277 self->last_value = src_val;
280 GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
286 gst_argb_control_binding_get_value (GstControlBinding * _self,
287 GstClockTime timestamp)
289 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
290 GValue *dst_val = NULL;
291 gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
294 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), NULL);
295 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
296 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
298 /* get current value via control source */
300 ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
302 ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
304 ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
306 ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
307 if (G_LIKELY (ret)) {
308 guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
309 (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
310 (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
311 ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
312 dst_val = g_new0 (GValue, 1);
313 g_value_init (dst_val, G_TYPE_UINT);
314 g_value_set_uint (dst_val, src_val);
316 GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
317 _self->name, GST_TIME_ARGS (timestamp));
324 gst_argb_control_binding_get_value_array (GstControlBinding * _self,
325 GstClockTime timestamp, GstClockTime interval, guint n_values,
328 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
330 gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
332 guint *values = (guint *) values_;
335 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
336 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
337 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
338 g_return_val_if_fail (values, FALSE);
339 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
342 src_val_a = g_new0 (gdouble, n_values);
343 ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
344 interval, n_values, src_val_a);
347 src_val_r = g_new0 (gdouble, n_values);
348 ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
349 interval, n_values, src_val_r);
352 src_val_g = g_new0 (gdouble, n_values);
353 ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
354 interval, n_values, src_val_g);
357 src_val_b = g_new0 (gdouble, n_values);
358 ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
359 interval, n_values, src_val_b);
361 if (G_LIKELY (ret)) {
362 for (i = 0; i < n_values; i++) {
363 gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
364 if (src_val_a && !isnan (src_val_a[i]))
366 if (src_val_r && !isnan (src_val_r[i]))
368 if (src_val_g && !isnan (src_val_g[i]))
370 if (src_val_b && !isnan (src_val_b[i]))
372 values[i] = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
373 (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
374 (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
375 ((guint) (CLAMP (b, 0.0, 1.0) * 255));
378 GST_LOG ("failed to get control value for property %s at ts %"
379 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
389 gst_argb_control_binding_get_g_value_array (GstControlBinding * _self,
390 GstClockTime timestamp, GstClockTime interval, guint n_values,
393 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
395 gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
400 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
401 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
402 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
403 g_return_val_if_fail (values, FALSE);
404 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
407 src_val_a = g_new0 (gdouble, n_values);
408 ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
409 interval, n_values, src_val_a);
412 src_val_r = g_new0 (gdouble, n_values);
413 ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
414 interval, n_values, src_val_r);
417 src_val_g = g_new0 (gdouble, n_values);
418 ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
419 interval, n_values, src_val_g);
422 src_val_b = g_new0 (gdouble, n_values);
423 ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
424 interval, n_values, src_val_b);
426 if (G_LIKELY (ret)) {
427 for (i = 0; i < n_values; i++) {
428 gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
429 if (src_val_a && !isnan (src_val_a[i]))
431 if (src_val_r && !isnan (src_val_r[i]))
433 if (src_val_g && !isnan (src_val_g[i]))
435 if (src_val_b && !isnan (src_val_b[i]))
437 src_val = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
438 (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
439 (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
440 ((guint) (CLAMP (b, 0.0, 1.0) * 255));
441 g_value_init (&values[i], G_TYPE_UINT);
442 g_value_set_uint (&values[i], src_val);
445 GST_LOG ("failed to get control value for property %s at ts %"
446 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
458 * gst_argb_control_binding_new:
459 * @object: the object of the property
460 * @property_name: the property-name to attach the control source
461 * @cs_a: the control source for the alpha channel
462 * @cs_r: the control source for the red channel
463 * @cs_g: the control source for the green channel
464 * @cs_b: the control source for the blue channel
466 * Create a new control-binding that attaches the given #GstControlSource to the
469 * Returns: (transfer floating): the new #GstARGBControlBinding
472 gst_argb_control_binding_new (GstObject * object, const gchar * property_name,
473 GstControlSource * cs_a, GstControlSource * cs_r, GstControlSource * cs_g,
474 GstControlSource * cs_b)
476 return (GstControlBinding *) g_object_new (GST_TYPE_ARGB_CONTROL_BINDING,
477 "object", object, "name", property_name,
478 "control-source-a", cs_a,
479 "control-source-r", cs_r,
480 "control-source-g", cs_g, "control-source-b", cs_b, NULL);