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 struct _GstControlBindingPrivate
61 #define GST_CAT_DEFAULT control_binding_debug
62 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
65 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
66 "dynamic parameter control source attachment");
68 static GObject *gst_control_binding_constructor (GType type,
69 guint n_construct_params, GObjectConstructParam * construct_params);
70 static void gst_control_binding_set_property (GObject * object, guint prop_id,
71 const GValue * value, GParamSpec * pspec);
72 static void gst_control_binding_get_property (GObject * object, guint prop_id,
73 GValue * value, GParamSpec * pspec);
74 static void gst_control_binding_dispose (GObject * object);
75 static void gst_control_binding_finalize (GObject * object);
77 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
78 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstControlBinding) _do_init);
88 static GParamSpec *properties[PROP_LAST];
91 gst_control_binding_class_init (GstControlBindingClass * klass)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
95 gobject_class->constructor = gst_control_binding_constructor;
96 gobject_class->set_property = gst_control_binding_set_property;
97 gobject_class->get_property = gst_control_binding_get_property;
98 gobject_class->dispose = gst_control_binding_dispose;
99 gobject_class->finalize = gst_control_binding_finalize;
101 properties[PROP_OBJECT] =
102 g_param_spec_object ("object", "Object",
103 "The object of the property", GST_TYPE_OBJECT,
104 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
106 properties[PROP_NAME] =
107 g_param_spec_string ("name", "Name", "The name of the property", NULL,
108 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
111 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
115 gst_control_binding_init (GstControlBinding * binding)
117 binding->ABI.abi.priv = gst_control_binding_get_instance_private (binding);
118 g_weak_ref_init (&binding->ABI.abi.priv->object, NULL);
122 gst_control_binding_constructor (GType type, guint n_construct_params,
123 GObjectConstructParam * construct_params)
125 GstControlBinding *binding;
130 GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
131 ->constructor (type, n_construct_params, construct_params));
133 object = g_weak_ref_get (&binding->ABI.abi.priv->object);
135 GST_WARNING_OBJECT (object, "no object set");
136 return (GObject *) binding;
139 GST_INFO_OBJECT (object, "trying to put property '%s' under control",
142 /* check if the object has a property of that name */
144 g_object_class_find_property (G_OBJECT_GET_CLASS (object),
146 GST_DEBUG_OBJECT (object, " psec->flags : 0x%08x", pspec->flags);
148 /* check if this param is writeable && controllable && !construct-only */
149 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
150 G_PARAM_CONSTRUCT_ONLY)) ==
151 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
152 binding->pspec = pspec;
154 GST_WARNING_OBJECT (object,
155 "property '%s' on class '%s' needs to "
156 "be writeable, controllable and not construct_only", binding->name,
157 G_OBJECT_TYPE_NAME (object));
160 GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
161 G_OBJECT_TYPE_NAME (object), binding->name);
164 gst_object_unref (object);
166 return (GObject *) binding;
170 gst_control_binding_dispose (GObject * object)
172 GstControlBinding *self = GST_CONTROL_BINDING (object);
174 /* we did not took a reference */
176 g_object_remove_weak_pointer ((GObject *) self->__object,
177 (gpointer *) & self->__object);
178 self->__object = NULL;
179 g_weak_ref_clear (&self->ABI.abi.priv->object);
181 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
185 gst_control_binding_finalize (GObject * object)
187 GstControlBinding *self = GST_CONTROL_BINDING (object);
191 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
195 gst_control_binding_set_property (GObject * object, guint prop_id,
196 const GValue * value, GParamSpec * pspec)
198 GstControlBinding *self = GST_CONTROL_BINDING (object);
202 /* do not ref to avoid a ref cycle */
203 self->__object = g_value_get_object (value);
204 g_object_add_weak_pointer ((GObject *) self->__object,
205 (gpointer *) & self->__object);
207 g_weak_ref_set (&self->ABI.abi.priv->object, self->__object);
210 self->name = g_value_dup_string (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 gst_control_binding_get_property (GObject * object, guint prop_id,
220 GValue * value, GParamSpec * pspec)
222 GstControlBinding *self = GST_CONTROL_BINDING (object);
226 g_value_take_object (value, g_weak_ref_get (&self->ABI.abi.priv->object));
229 g_value_set_string (value, self->name);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 * gst_control_binding_sync_values:
241 * @binding: the control binding
242 * @object: the object that has controlled properties
243 * @timestamp: the time that should be processed
244 * @last_sync: the last time this was called
246 * Sets the property of the @object, according to the #GstControlSources that
247 * handles it and for the given timestamp.
249 * If this function fails, it is most likely the application developers fault.
250 * Most probably the control sources are not setup correctly.
252 * Returns: %TRUE if the controller value could be applied to the object
253 * property, %FALSE otherwise
256 gst_control_binding_sync_values (GstControlBinding * binding,
257 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
259 GstControlBindingClass *klass;
260 gboolean ret = FALSE;
262 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
263 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
265 if (binding->disabled)
268 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
270 if (G_LIKELY (klass->sync_values != NULL)) {
271 ret = klass->sync_values (binding, object, timestamp, last_sync);
273 GST_WARNING_OBJECT (binding, "missing sync_values implementation");
279 * gst_control_binding_get_value:
280 * @binding: the control binding
281 * @timestamp: the time the control-change should be read from
283 * Gets the value for the given controlled property at the requested time.
285 * Returns: (nullable): the GValue of the property at the given time,
286 * or %NULL if the property isn't controlled.
289 gst_control_binding_get_value (GstControlBinding * binding,
290 GstClockTime timestamp)
292 GstControlBindingClass *klass;
295 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
296 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
298 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
300 if (G_LIKELY (klass->get_value != NULL)) {
301 ret = klass->get_value (binding, timestamp);
303 GST_WARNING_OBJECT (binding, "missing get_value implementation");
309 * gst_control_binding_get_value_array: (skip)
310 * @binding: the control binding
311 * @timestamp: the time that should be processed
312 * @interval: the time spacing between subsequent values
313 * @n_values: the number of values
314 * @values: (array length=n_values): array to put control-values in
316 * Gets a number of values for the given controlled property starting at the
317 * requested time. The array @values needs to hold enough space for @n_values of
318 * the same type as the objects property's type.
320 * This function is useful if one wants to e.g. draw a graph of the control
321 * curve or apply a control curve sample by sample.
323 * The values are unboxed and ready to be used. The similar function
324 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
325 * more suitable for bindings.
327 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
330 gst_control_binding_get_value_array (GstControlBinding * binding,
331 GstClockTime timestamp, GstClockTime interval, guint n_values,
334 GstControlBindingClass *klass;
335 gboolean ret = FALSE;
337 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
338 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
339 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
340 g_return_val_if_fail (values, FALSE);
342 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
344 if (G_LIKELY (klass->get_value_array != NULL)) {
346 klass->get_value_array (binding, timestamp, interval, n_values, values);
348 GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
353 #define CONVERT_ARRAY(type,TYPE) \
355 g##type *v = g_new (g##type,n_values); \
356 ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
359 for (i = 0; i < n_values; i++) { \
360 g_value_init (&values[i], G_TYPE_##TYPE); \
361 g_value_set_##type (&values[i], v[i]); \
368 * gst_control_binding_get_g_value_array:
369 * @binding: the control binding
370 * @timestamp: the time that should be processed
371 * @interval: the time spacing between subsequent values
372 * @n_values: the number of values
373 * @values: (array length=n_values): array to put control-values in
375 * Gets a number of #GValues for the given controlled property starting at the
376 * requested time. The array @values need to hold enough space for @n_values of
379 * This function is useful if one wants to e.g. draw a graph of the control
380 * curve or apply a control curve sample by sample.
382 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
385 gst_control_binding_get_g_value_array (GstControlBinding * binding,
386 GstClockTime timestamp, GstClockTime interval, guint n_values,
389 GstControlBindingClass *klass;
390 gboolean ret = FALSE;
392 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
393 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
394 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
395 g_return_val_if_fail (values, FALSE);
397 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
399 if (G_LIKELY (klass->get_g_value_array != NULL)) {
401 klass->get_g_value_array (binding, timestamp, interval, n_values,
407 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
408 while ((type = g_type_parent (type)))
411 GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
415 CONVERT_ARRAY (int, INT);
418 CONVERT_ARRAY (uint, UINT);
421 CONVERT_ARRAY (long, LONG);
424 CONVERT_ARRAY (ulong, ULONG);
427 CONVERT_ARRAY (int64, INT64);
430 CONVERT_ARRAY (uint64, UINT64);
433 CONVERT_ARRAY (float, FLOAT);
436 CONVERT_ARRAY (double, DOUBLE);
439 CONVERT_ARRAY (boolean, BOOLEAN);
443 gint *v = g_new (gint, n_values);
444 ret = gst_control_binding_get_value_array (binding, timestamp, interval,
447 for (i = 0; i < n_values; i++) {
448 g_value_init (&values[i], type);
449 g_value_set_enum (&values[i], v[i]);
456 GST_WARNING ("incomplete implementation for paramspec type '%s'",
457 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
458 GST_CONTROL_BINDING_PSPEC (binding) = NULL;
466 * gst_control_binding_set_disabled:
467 * @binding: the control binding
468 * @disabled: boolean that specifies whether to disable the controller
471 * This function is used to disable a control binding for some time, i.e.
472 * gst_object_sync_values() will do nothing.
475 gst_control_binding_set_disabled (GstControlBinding * binding,
478 g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
479 binding->disabled = disabled;
483 * gst_control_binding_is_disabled:
484 * @binding: the control binding
486 * Checks if the control binding is disabled.
488 * Returns: %TRUE if the binding is inactive
491 gst_control_binding_is_disabled (GstControlBinding * binding)
493 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
494 return ! !binding->disabled;