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 * @title: GstControlBinding
25 * @short_description: attachment for control source sources
27 * A base class for value mapping objects that attaches control sources to gobject
28 * properties. Such an object is taking one or more #GstControlSource instances,
29 * combines them and maps the resulting value to the type and value range of the
32 /* FIXME(ensonic): should we make gst_object_add_control_binding() internal
33 * - we create the control_binding for a certain object anyway
34 * - we could call gst_object_add_control_binding() at the end of
35 * gst_control_binding_constructor()
36 * - the weak-ref on object is not nice, as is the same as gst_object_parent()
37 * once the object is added to the parent
39 * - another option would be to defer what is done in _constructor to when
40 * the parent is set (need to listen to the signal then)
41 * then basically I could
42 * a) remove the obj arg and wait the binding to be added or
43 * b) add the binding from constructor, unref object there and make obj
47 #include "gst_private.h"
49 #include <glib-object.h>
52 #include "gstcontrolbinding.h"
56 #define GST_CAT_DEFAULT control_binding_debug
57 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
60 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
61 "dynamic parameter control source attachment");
63 static GObject *gst_control_binding_constructor (GType type,
64 guint n_construct_params, GObjectConstructParam * construct_params);
65 static void gst_control_binding_set_property (GObject * object, guint prop_id,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_control_binding_get_property (GObject * object, guint prop_id,
68 GValue * value, GParamSpec * pspec);
69 static void gst_control_binding_dispose (GObject * object);
70 static void gst_control_binding_finalize (GObject * object);
72 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
73 GST_TYPE_OBJECT, _do_init);
75 struct _GstControlBindingPrivate
88 static GParamSpec *properties[PROP_LAST];
91 gst_control_binding_class_init (GstControlBindingClass * klass)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95 g_type_class_add_private (klass, sizeof (GstControlBindingPrivate));
97 gobject_class->constructor = gst_control_binding_constructor;
98 gobject_class->set_property = gst_control_binding_set_property;
99 gobject_class->get_property = gst_control_binding_get_property;
100 gobject_class->dispose = gst_control_binding_dispose;
101 gobject_class->finalize = gst_control_binding_finalize;
103 properties[PROP_OBJECT] =
104 g_param_spec_object ("object", "Object",
105 "The object of the property", GST_TYPE_OBJECT,
106 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
108 properties[PROP_NAME] =
109 g_param_spec_string ("name", "Name", "The name of the property", NULL,
110 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
113 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
117 gst_control_binding_init (GstControlBinding * binding)
119 binding->ABI.abi.priv =
120 G_TYPE_INSTANCE_GET_PRIVATE (binding, GST_TYPE_CONTROL_BINDING,
121 GstControlBindingPrivate);
122 g_weak_ref_init (&binding->ABI.abi.priv->object, NULL);
126 gst_control_binding_constructor (GType type, guint n_construct_params,
127 GObjectConstructParam * construct_params)
129 GstControlBinding *binding;
134 GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
135 ->constructor (type, n_construct_params, construct_params));
137 object = g_weak_ref_get (&binding->ABI.abi.priv->object);
139 GST_WARNING_OBJECT (object, "no object set");
140 return (GObject *) binding;
143 GST_INFO_OBJECT (object, "trying to put property '%s' under control",
146 /* check if the object has a property of that name */
148 g_object_class_find_property (G_OBJECT_GET_CLASS (object),
150 GST_DEBUG_OBJECT (object, " psec->flags : 0x%08x", pspec->flags);
152 /* check if this param is witable && controlable && !construct-only */
153 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
154 G_PARAM_CONSTRUCT_ONLY)) ==
155 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
156 binding->pspec = pspec;
158 GST_WARNING_OBJECT (object,
159 "property '%s' on class '%s' needs to "
160 "be writeable, controlable and not construct_only", binding->name,
161 G_OBJECT_TYPE_NAME (object));
164 GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
165 G_OBJECT_TYPE_NAME (object), binding->name);
168 gst_object_unref (object);
170 return (GObject *) binding;
174 gst_control_binding_dispose (GObject * object)
176 GstControlBinding *self = GST_CONTROL_BINDING (object);
178 /* we did not took a reference */
179 g_object_remove_weak_pointer ((GObject *) self->__object,
180 (gpointer *) & self->__object);
181 self->__object = NULL;
182 g_weak_ref_clear (&self->ABI.abi.priv->object);
184 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
188 gst_control_binding_finalize (GObject * object)
190 GstControlBinding *self = GST_CONTROL_BINDING (object);
194 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
198 gst_control_binding_set_property (GObject * object, guint prop_id,
199 const GValue * value, GParamSpec * pspec)
201 GstControlBinding *self = GST_CONTROL_BINDING (object);
205 /* do not ref to avoid a ref cycle */
206 self->__object = g_value_get_object (value);
207 g_object_add_weak_pointer ((GObject *) self->__object,
208 (gpointer *) & self->__object);
210 g_weak_ref_set (&self->ABI.abi.priv->object, self->__object);
213 self->name = g_value_dup_string (value);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gst_control_binding_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec)
225 GstControlBinding *self = GST_CONTROL_BINDING (object);
229 g_value_take_object (value, g_weak_ref_get (&self->ABI.abi.priv->object));
232 g_value_set_string (value, self->name);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 * gst_control_binding_sync_values:
244 * @binding: the control binding
245 * @object: the object that has controlled properties
246 * @timestamp: the time that should be processed
247 * @last_sync: the last time this was called
249 * Sets the property of the @object, according to the #GstControlSources that
250 * handle them and for the given timestamp.
252 * If this function fails, it is most likely the application developers fault.
253 * Most probably the control sources are not setup correctly.
255 * Returns: %TRUE if the controller value could be applied to the object
256 * property, %FALSE otherwise
259 gst_control_binding_sync_values (GstControlBinding * binding,
260 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
262 GstControlBindingClass *klass;
263 gboolean ret = FALSE;
265 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
267 if (binding->disabled)
270 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
272 if (G_LIKELY (klass->sync_values != NULL)) {
273 ret = klass->sync_values (binding, object, timestamp, last_sync);
275 GST_WARNING_OBJECT (binding, "missing sync_values implementation");
281 * gst_control_binding_get_value:
282 * @binding: the control binding
283 * @timestamp: the time the control-change should be read from
285 * Gets the value for the given controlled property at the requested time.
287 * Returns: (nullable): the GValue of the property at the given time,
288 * or %NULL if the property isn't controlled.
291 gst_control_binding_get_value (GstControlBinding * binding,
292 GstClockTime timestamp)
294 GstControlBindingClass *klass;
297 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
298 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
300 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
302 if (G_LIKELY (klass->get_value != NULL)) {
303 ret = klass->get_value (binding, timestamp);
305 GST_WARNING_OBJECT (binding, "missing get_value implementation");
311 * gst_control_binding_get_value_array: (skip)
312 * @binding: the control binding
313 * @timestamp: the time that should be processed
314 * @interval: the time spacing between subsequent values
315 * @n_values: the number of values
316 * @values: (array length=n_values): array to put control-values in
318 * Gets a number of values for the given controlled property starting at the
319 * requested time. The array @values need to hold enough space for @n_values of
320 * the same type as the objects property's type.
322 * This function is useful if one wants to e.g. draw a graph of the control
323 * curve or apply a control curve sample by sample.
325 * The values are unboxed and ready to be used. The similar function
326 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
327 * more suitable for bindings.
329 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
332 gst_control_binding_get_value_array (GstControlBinding * binding,
333 GstClockTime timestamp, GstClockTime interval, guint n_values,
336 GstControlBindingClass *klass;
337 gboolean ret = FALSE;
339 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
340 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
341 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
342 g_return_val_if_fail (values, FALSE);
344 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
346 if (G_LIKELY (klass->get_value_array != NULL)) {
348 klass->get_value_array (binding, timestamp, interval, n_values, values);
350 GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
355 #define CONVERT_ARRAY(type,TYPE) \
357 g##type *v = g_new (g##type,n_values); \
358 ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
361 for (i = 0; i < n_values; i++) { \
362 g_value_init (&values[i], G_TYPE_##TYPE); \
363 g_value_set_##type (&values[i], v[i]); \
370 * gst_control_binding_get_g_value_array:
371 * @binding: the control binding
372 * @timestamp: the time that should be processed
373 * @interval: the time spacing between subsequent values
374 * @n_values: the number of values
375 * @values: (array length=n_values): array to put control-values in
377 * Gets a number of #GValues for the given controlled property starting at the
378 * requested time. The array @values need to hold enough space for @n_values of
381 * This function is useful if one wants to e.g. draw a graph of the control
382 * curve or apply a control curve sample by sample.
384 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
387 gst_control_binding_get_g_value_array (GstControlBinding * binding,
388 GstClockTime timestamp, GstClockTime interval, guint n_values,
391 GstControlBindingClass *klass;
392 gboolean ret = FALSE;
394 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
395 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
396 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
397 g_return_val_if_fail (values, FALSE);
399 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
401 if (G_LIKELY (klass->get_g_value_array != NULL)) {
403 klass->get_g_value_array (binding, timestamp, interval, n_values,
409 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
410 while ((type = g_type_parent (type)))
413 GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
417 CONVERT_ARRAY (int, INT);
420 CONVERT_ARRAY (uint, UINT);
423 CONVERT_ARRAY (long, LONG);
426 CONVERT_ARRAY (ulong, ULONG);
429 CONVERT_ARRAY (int64, INT64);
432 CONVERT_ARRAY (uint64, UINT64);
435 CONVERT_ARRAY (float, FLOAT);
438 CONVERT_ARRAY (double, DOUBLE);
441 CONVERT_ARRAY (boolean, BOOLEAN);
445 gint *v = g_new (gint, n_values);
446 ret = gst_control_binding_get_value_array (binding, timestamp, interval,
449 for (i = 0; i < n_values; i++) {
450 g_value_init (&values[i], type);
451 g_value_set_enum (&values[i], v[i]);
458 GST_WARNING ("incomplete implementation for paramspec type '%s'",
459 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
460 GST_CONTROL_BINDING_PSPEC (binding) = NULL;
468 * gst_control_binding_set_disabled:
469 * @binding: the control binding
470 * @disabled: boolean that specifies whether to disable the controller
473 * This function is used to disable a control binding for some time, i.e.
474 * gst_object_sync_values() will do nothing.
477 gst_control_binding_set_disabled (GstControlBinding * binding,
480 g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
481 binding->disabled = disabled;
485 * gst_control_binding_is_disabled:
486 * @binding: the control binding
488 * Check if the control binding is disabled.
490 * Returns: %TRUE if the binding is inactive
493 gst_control_binding_is_disabled (GstControlBinding * binding)
495 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
496 return ! !binding->disabled;