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 * @short_description: attachment for control sources to argb properties
27 * A value mapping object that attaches multiple control sources to a guint
28 * gobject properties representing a color. A control value of 0.0 will turn the
29 * color component off and a value of 1.0 will be the color level.
32 #include <glib-object.h>
35 #include "gstargbcontrolbinding.h"
37 #include <gst/math-compat.h>
39 #define GST_CAT_DEFAULT control_binding_debug
40 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
42 static GObject *gst_argb_control_binding_constructor (GType type,
43 guint n_construct_params, GObjectConstructParam * construct_params);
44 static void gst_argb_control_binding_set_property (GObject * object,
45 guint prop_id, const GValue * value, GParamSpec * pspec);
46 static void gst_argb_control_binding_get_property (GObject * object,
47 guint prop_id, GValue * value, GParamSpec * pspec);
48 static void gst_argb_control_binding_dispose (GObject * object);
49 static void gst_argb_control_binding_finalize (GObject * object);
51 static gboolean gst_argb_control_binding_sync_values (GstControlBinding * _self,
52 GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
53 static GValue *gst_argb_control_binding_get_value (GstControlBinding * _self,
54 GstClockTime timestamp);
55 static gboolean gst_argb_control_binding_get_value_array (GstControlBinding *
56 _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
58 static gboolean gst_argb_control_binding_get_g_value_array (GstControlBinding *
59 _self, GstClockTime timestamp, GstClockTime interval, guint n_values,
63 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstargbcontrolbinding", 0, \
64 "dynamic parameter control source attachment");
66 #define gst_argb_control_binding_parent_class parent_class
67 G_DEFINE_TYPE_WITH_CODE (GstARGBControlBinding, gst_argb_control_binding,
68 GST_TYPE_CONTROL_BINDING, _do_init);
80 static GParamSpec *properties[PROP_LAST];
85 gst_argb_control_binding_class_init (GstARGBControlBindingClass * klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88 GstControlBindingClass *control_binding_class =
89 GST_CONTROL_BINDING_CLASS (klass);
91 gobject_class->constructor = gst_argb_control_binding_constructor;
92 gobject_class->set_property = gst_argb_control_binding_set_property;
93 gobject_class->get_property = gst_argb_control_binding_get_property;
94 gobject_class->dispose = gst_argb_control_binding_dispose;
95 gobject_class->finalize = gst_argb_control_binding_finalize;
97 control_binding_class->sync_values = gst_argb_control_binding_sync_values;
98 control_binding_class->get_value = gst_argb_control_binding_get_value;
99 control_binding_class->get_value_array =
100 gst_argb_control_binding_get_value_array;
101 control_binding_class->get_g_value_array =
102 gst_argb_control_binding_get_g_value_array;
104 properties[PROP_CS_A] =
105 g_param_spec_object ("control-source-a", "ControlSource A",
106 "The control source for the alpha color component",
107 GST_TYPE_CONTROL_SOURCE,
108 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
110 properties[PROP_CS_R] =
111 g_param_spec_object ("control-source-r", "ControlSource R",
112 "The control source for the red color component",
113 GST_TYPE_CONTROL_SOURCE,
114 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
116 properties[PROP_CS_G] =
117 g_param_spec_object ("control-source-g", "ControlSource G",
118 "The control source for the green color component",
119 GST_TYPE_CONTROL_SOURCE,
120 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
122 properties[PROP_CS_B] =
123 g_param_spec_object ("control-source-b", "ControlSource B",
124 "The control source for the blue color component",
125 GST_TYPE_CONTROL_SOURCE,
126 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
128 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
132 gst_argb_control_binding_init (GstARGBControlBinding * self)
137 gst_argb_control_binding_constructor (GType type, guint n_construct_params,
138 GObjectConstructParam * construct_params)
140 GstARGBControlBinding *self;
143 GST_ARGB_CONTROL_BINDING (G_OBJECT_CLASS (parent_class)->constructor
144 (type, n_construct_params, construct_params));
146 if (GST_CONTROL_BINDING_PSPEC (self)) {
147 if (!(G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self)) ==
149 GST_WARNING ("can't bind to paramspec type '%s'",
150 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
151 GST_CONTROL_BINDING_PSPEC (self) = NULL;
153 g_value_init (&self->cur_value, G_TYPE_UINT);
156 return (GObject *) self;
160 gst_argb_control_binding_set_property (GObject * object, guint prop_id,
161 const GValue * value, GParamSpec * pspec)
163 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
167 gst_object_replace ((GstObject **) & self->cs_a,
168 g_value_get_object (value));
171 gst_object_replace ((GstObject **) & self->cs_r,
172 g_value_get_object (value));
175 gst_object_replace ((GstObject **) & self->cs_g,
176 g_value_get_object (value));
179 gst_object_replace ((GstObject **) & self->cs_b,
180 g_value_get_object (value));
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 gst_argb_control_binding_get_property (GObject * object, guint prop_id,
190 GValue * value, GParamSpec * pspec)
192 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
196 g_value_set_object (value, self->cs_a);
199 g_value_set_object (value, self->cs_r);
202 g_value_set_object (value, self->cs_g);
205 g_value_set_object (value, self->cs_b);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
214 gst_argb_control_binding_dispose (GObject * object)
216 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
218 gst_object_replace ((GstObject **) & self->cs_a, NULL);
219 gst_object_replace ((GstObject **) & self->cs_r, NULL);
220 gst_object_replace ((GstObject **) & self->cs_g, NULL);
221 gst_object_replace ((GstObject **) & self->cs_b, NULL);
223 G_OBJECT_CLASS (parent_class)->dispose (object);
227 gst_argb_control_binding_finalize (GObject * object)
229 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (object);
231 g_value_unset (&self->cur_value);
233 G_OBJECT_CLASS (parent_class)->finalize (object);
237 gst_argb_control_binding_sync_values (GstControlBinding * _self,
238 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
240 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
241 gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
244 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
245 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
247 GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
248 _self->name, GST_TIME_ARGS (timestamp));
251 ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
253 ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
255 ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
257 ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
258 if (G_LIKELY (ret)) {
259 guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
260 (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
261 (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
262 ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
263 GST_LOG_OBJECT (object, " new value 0x%08x", src_val);
264 /* always set the value for first time, but then only if it changed
265 * this should limit g_object_notify invocations.
266 * FIXME: can we detect negative playback rates?
268 if ((timestamp < last_sync) || (src_val != self->last_value)) {
269 GValue *dst_val = &self->cur_value;
271 g_value_set_uint (dst_val, src_val);
272 /* we can make this faster
273 * http://bugzilla.gnome.org/show_bug.cgi?id=536939
275 g_object_set_property ((GObject *) object, _self->name, dst_val);
276 self->last_value = src_val;
279 GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
285 gst_argb_control_binding_get_value (GstControlBinding * _self,
286 GstClockTime timestamp)
288 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
289 GValue *dst_val = NULL;
290 gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
293 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), NULL);
294 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
295 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
297 /* get current value via control source */
299 ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
301 ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
303 ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
305 ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
306 if (G_LIKELY (ret)) {
307 guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
308 (((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
309 (((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
310 ((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
311 dst_val = g_new0 (GValue, 1);
312 g_value_init (dst_val, G_TYPE_UINT);
313 g_value_set_uint (dst_val, src_val);
315 GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
316 _self->name, GST_TIME_ARGS (timestamp));
323 gst_argb_control_binding_get_value_array (GstControlBinding * _self,
324 GstClockTime timestamp, GstClockTime interval, guint n_values,
327 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
329 gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
331 guint *values = (guint *) values_;
334 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
335 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
336 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
337 g_return_val_if_fail (values, FALSE);
338 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
341 src_val_a = g_new0 (gdouble, n_values);
342 ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
343 interval, n_values, src_val_a);
346 src_val_r = g_new0 (gdouble, n_values);
347 ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
348 interval, n_values, src_val_r);
351 src_val_g = g_new0 (gdouble, n_values);
352 ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
353 interval, n_values, src_val_g);
356 src_val_b = g_new0 (gdouble, n_values);
357 ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
358 interval, n_values, src_val_b);
360 if (G_LIKELY (ret)) {
361 for (i = 0; i < n_values; i++) {
362 gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
363 if (src_val_a && !isnan (src_val_a[i]))
365 if (src_val_r && !isnan (src_val_r[i]))
367 if (src_val_g && !isnan (src_val_g[i]))
369 if (src_val_b && !isnan (src_val_b[i]))
371 values[i] = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
372 (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
373 (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
374 ((guint) (CLAMP (b, 0.0, 1.0) * 255));
377 GST_LOG ("failed to get control value for property %s at ts %"
378 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
388 gst_argb_control_binding_get_g_value_array (GstControlBinding * _self,
389 GstClockTime timestamp, GstClockTime interval, guint n_values,
392 GstARGBControlBinding *self = GST_ARGB_CONTROL_BINDING (_self);
394 gdouble *src_val_a = NULL, *src_val_r = NULL, *src_val_g = NULL, *src_val_b =
399 g_return_val_if_fail (GST_IS_ARGB_CONTROL_BINDING (self), FALSE);
400 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
401 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
402 g_return_val_if_fail (values, FALSE);
403 g_return_val_if_fail (GST_CONTROL_BINDING_PSPEC (self), FALSE);
406 src_val_a = g_new0 (gdouble, n_values);
407 ret &= gst_control_source_get_value_array (self->cs_a, timestamp,
408 interval, n_values, src_val_a);
411 src_val_r = g_new0 (gdouble, n_values);
412 ret &= gst_control_source_get_value_array (self->cs_r, timestamp,
413 interval, n_values, src_val_r);
416 src_val_g = g_new0 (gdouble, n_values);
417 ret &= gst_control_source_get_value_array (self->cs_g, timestamp,
418 interval, n_values, src_val_g);
421 src_val_b = g_new0 (gdouble, n_values);
422 ret &= gst_control_source_get_value_array (self->cs_b, timestamp,
423 interval, n_values, src_val_b);
425 if (G_LIKELY (ret)) {
426 for (i = 0; i < n_values; i++) {
427 gdouble a = 1.0, r = 0.0, g = 0.0, b = 0.0;
428 if (src_val_a && !isnan (src_val_a[i]))
430 if (src_val_r && !isnan (src_val_r[i]))
432 if (src_val_g && !isnan (src_val_g[i]))
434 if (src_val_b && !isnan (src_val_b[i]))
436 src_val = (((guint) (CLAMP (a, 0.0, 1.0) * 255)) << 24) |
437 (((guint) (CLAMP (r, 0.0, 1.0) * 255)) << 16) |
438 (((guint) (CLAMP (g, 0.0, 1.0) * 255)) << 8) |
439 ((guint) (CLAMP (b, 0.0, 1.0) * 255));
440 g_value_init (&values[i], G_TYPE_UINT);
441 g_value_set_uint (&values[i], src_val);
444 GST_LOG ("failed to get control value for property %s at ts %"
445 GST_TIME_FORMAT, _self->name, GST_TIME_ARGS (timestamp));
457 * gst_argb_control_binding_new:
458 * @object: the object of the property
459 * @property_name: the property-name to attach the control source
460 * @cs_a: the control source for the alpha channel
461 * @cs_r: the control source for the red channel
462 * @cs_g: the control source for the green channel
463 * @cs_b: the control source for the blue channel
465 * Create a new control-binding that attaches the given #GstControlSource to the
468 * Returns: (transfer floating): the new #GstARGBControlBinding
471 gst_argb_control_binding_new (GstObject * object, const gchar * property_name,
472 GstControlSource * cs_a, GstControlSource * cs_r, GstControlSource * cs_g,
473 GstControlSource * cs_b)
475 return (GstControlBinding *) g_object_new (GST_TYPE_ARGB_CONTROL_BINDING,
476 "object", object, "name", property_name,
477 "control-source-a", cs_a,
478 "control-source-r", cs_r,
479 "control-source-g", cs_g, "control-source-b", cs_b, NULL);