3 * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
5 * gstcontrolbinding.c: 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:gstcontrolbinding
24 * @short_description: attachment for control source sources
26 * A base class for value mapping objects that attaches control sources to gobject
27 * properties. Such an object is taking one or more #GstControlSource instances,
28 * combines them and maps the resulting value to the type and value range of the
31 /* FIXME(ensonic): should we make gst_object_add_control_binding() internal
32 * - we create the control_binding for a certain object anyway
33 * - we could call gst_object_add_control_binding() at the end of
34 * gst_control_binding_constructor()
35 * - the weak-ref on object is not nice, as is the same as gst_object_parent()
36 * once the object is added to the parent
38 * - another option would be do defer what I am doing in _constructor to when
39 * the parent is set (need to listen to the signal then)
40 * then basically I could
41 * a) remove the obj arg and wait the binding to be added or
42 * b) add the binding from constructor, unref object there and make obj
46 #include "gst_private.h"
48 #include <glib-object.h>
51 #include "gstcontrolbinding.h"
55 #define GST_CAT_DEFAULT control_binding_debug
56 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
59 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
60 "dynamic parameter control source attachment");
62 static GObject *gst_control_binding_constructor (GType type,
63 guint n_construct_params, GObjectConstructParam * construct_params);
64 static void gst_control_binding_set_property (GObject * object, guint prop_id,
65 const GValue * value, GParamSpec * pspec);
66 static void gst_control_binding_get_property (GObject * object, guint prop_id,
67 GValue * value, GParamSpec * pspec);
68 static void gst_control_binding_dispose (GObject * object);
69 static void gst_control_binding_finalize (GObject * object);
71 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
72 GST_TYPE_OBJECT, _do_init);
82 static GParamSpec *properties[PROP_LAST];
85 gst_control_binding_class_init (GstControlBindingClass * klass)
87 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
89 gobject_class->constructor = gst_control_binding_constructor;
90 gobject_class->set_property = gst_control_binding_set_property;
91 gobject_class->get_property = gst_control_binding_get_property;
92 gobject_class->dispose = gst_control_binding_dispose;
93 gobject_class->finalize = gst_control_binding_finalize;
95 properties[PROP_OBJECT] =
96 g_param_spec_object ("object", "Object",
97 "The object of the property", GST_TYPE_OBJECT,
98 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
100 properties[PROP_NAME] =
101 g_param_spec_string ("name", "Name", "The name of the property", NULL,
102 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
105 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
109 gst_control_binding_init (GstControlBinding * binding)
114 gst_control_binding_constructor (GType type, guint n_construct_params,
115 GObjectConstructParam * construct_params)
117 GstControlBinding *binding;
121 GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
122 ->constructor (type, n_construct_params, construct_params));
124 GST_INFO_OBJECT (binding->object, "trying to put property '%s' under control",
127 /* check if the object has a property of that name */
129 g_object_class_find_property (G_OBJECT_GET_CLASS (binding->object),
131 GST_DEBUG_OBJECT (binding->object, " psec->flags : 0x%08x", pspec->flags);
133 /* check if this param is witable && controlable && !construct-only */
134 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
135 G_PARAM_CONSTRUCT_ONLY)) ==
136 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
137 binding->pspec = pspec;
140 GST_WARNING_OBJECT (binding->object, "class '%s' has no property '%s'",
141 G_OBJECT_TYPE_NAME (binding->object), binding->name);
143 return (GObject *) binding;
147 gst_control_binding_dispose (GObject * object)
149 GstControlBinding *self = GST_CONTROL_BINDING (object);
151 /* we did not took a reference */
152 g_object_remove_weak_pointer ((GObject *) self->object,
153 (gpointer *) & self->object);
156 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
160 gst_control_binding_finalize (GObject * object)
162 GstControlBinding *self = GST_CONTROL_BINDING (object);
166 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
170 gst_control_binding_set_property (GObject * object, guint prop_id,
171 const GValue * value, GParamSpec * pspec)
173 GstControlBinding *self = GST_CONTROL_BINDING (object);
177 /* do not ref to avoid a ref cycle */
178 self->object = g_value_get_object (value);
179 g_object_add_weak_pointer ((GObject *) self->object,
180 (gpointer *) & self->object);
183 self->name = g_value_dup_string (value);
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 gst_control_binding_get_property (GObject * object, guint prop_id,
193 GValue * value, GParamSpec * pspec)
195 GstControlBinding *self = GST_CONTROL_BINDING (object);
199 g_value_set_object (value, self->object);
202 g_value_set_string (value, self->name);
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 * gst_control_binding_sync_values:
214 * @binding: the control binding
215 * @object: the object that has controlled properties
216 * @timestamp: the time that should be processed
217 * @last_sync: the last time this was called
219 * Sets the property of the @object, according to the #GstControlSources that
220 * handle them and for the given timestamp.
222 * If this function fails, it is most likely the application developers fault.
223 * Most probably the control sources are not setup correctly.
225 * Returns: %TRUE if the controller value could be applied to the object
226 * property, %FALSE otherwise
229 gst_control_binding_sync_values (GstControlBinding * binding,
230 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
232 GstControlBindingClass *klass;
233 gboolean ret = FALSE;
235 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
237 if (binding->disabled)
240 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
242 if (G_LIKELY (klass->sync_values != NULL)) {
243 ret = klass->sync_values (binding, object, timestamp, last_sync);
245 GST_WARNING_OBJECT (binding, "missing sync_values implementation");
251 * gst_control_binding_get_value:
252 * @binding: the control binding
253 * @timestamp: the time the control-change should be read from
255 * Gets the value for the given controlled property at the requested time.
257 * Returns: the GValue of the property at the given time, or %NULL if the
258 * property isn't controlled.
261 gst_control_binding_get_value (GstControlBinding * binding,
262 GstClockTime timestamp)
264 GstControlBindingClass *klass;
267 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
268 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
270 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
272 if (G_LIKELY (klass->get_value != NULL)) {
273 ret = klass->get_value (binding, timestamp);
275 GST_WARNING_OBJECT (binding, "missing get_value implementation");
281 * gst_control_binding_get_value_array:
282 * @binding: the control binding
283 * @timestamp: the time that should be processed
284 * @interval: the time spacing between subsequent values
285 * @n_values: the number of values
286 * @values: array to put control-values in
288 * Gets a number of values for the given controlled property starting at the
289 * requested time. The array @values need to hold enough space for @n_values of
290 * the same type as the objects property's type.
292 * This function is useful if one wants to e.g. draw a graph of the control
293 * curve or apply a control curve sample by sample.
295 * The values are unboxed and ready to be used. The similar function
296 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
297 * better suites for bindings.
299 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
302 gst_control_binding_get_value_array (GstControlBinding * binding,
303 GstClockTime timestamp, GstClockTime interval, guint n_values,
306 GstControlBindingClass *klass;
307 gboolean ret = FALSE;
309 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
310 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
311 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
312 g_return_val_if_fail (values, FALSE);
314 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
316 if (G_LIKELY (klass->get_value_array != NULL)) {
318 klass->get_value_array (binding, timestamp, interval, n_values, values);
320 GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
325 #define CONVERT_ARRAY(type,TYPE) \
327 g##type *v = g_new (g##type,n_values); \
328 ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
331 for (i = 0; i < n_values; i++) { \
332 g_value_init (&values[i], G_TYPE_##TYPE); \
333 g_value_set_##type (&values[i], v[i]); \
340 * gst_control_binding_get_g_value_array:
341 * @binding: the control binding
342 * @timestamp: the time that should be processed
343 * @interval: the time spacing between subsequent values
344 * @n_values: the number of values
345 * @values: array to put control-values in
347 * Gets a number of #GValues for the given controlled property starting at the
348 * requested time. The array @values need to hold enough space for @n_values of
351 * This function is useful if one wants to e.g. draw a graph of the control
352 * curve or apply a control curve sample by sample.
354 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
357 gst_control_binding_get_g_value_array (GstControlBinding * binding,
358 GstClockTime timestamp, GstClockTime interval, guint n_values,
361 GstControlBindingClass *klass;
362 gboolean ret = FALSE;
364 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
365 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
366 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
367 g_return_val_if_fail (values, FALSE);
369 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
371 if (G_LIKELY (klass->get_g_value_array != NULL)) {
373 klass->get_g_value_array (binding, timestamp, interval, n_values,
379 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
380 while ((type = g_type_parent (type)))
383 GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
387 CONVERT_ARRAY (int, INT);
390 CONVERT_ARRAY (uint, UINT);
393 CONVERT_ARRAY (long, LONG);
396 CONVERT_ARRAY (ulong, ULONG);
399 CONVERT_ARRAY (int64, INT64);
402 CONVERT_ARRAY (uint64, UINT64);
405 CONVERT_ARRAY (float, FLOAT);
408 CONVERT_ARRAY (double, DOUBLE);
411 CONVERT_ARRAY (boolean, BOOLEAN);
415 gint *v = g_new (gint, n_values);
416 ret = gst_control_binding_get_value_array (binding, timestamp, interval,
419 for (i = 0; i < n_values; i++) {
420 g_value_init (&values[i], type);
421 g_value_set_enum (&values[i], v[i]);
428 GST_WARNING ("incomplete implementation for paramspec type '%s'",
429 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
430 GST_CONTROL_BINDING_PSPEC (binding) = NULL;
438 * gst_control_binding_set_disabled:
439 * @binding: the control binding
440 * @disabled: boolean that specifies whether to disable the controller
443 * This function is used to disable a control binding for some time, i.e.
444 * gst_object_sync_values() will do nothing.
447 gst_control_binding_set_disabled (GstControlBinding * binding,
450 g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
451 binding->disabled = disabled;
455 * gst_control_binding_is_disabled:
456 * @binding: the control binding
458 * Check if the control binding is disabled.
460 * Returns: %TRUE if the binding is inactive
463 gst_control_binding_is_disabled (GstControlBinding * binding)
465 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
466 return (binding->disabled == TRUE);