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 to defer what is done 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);
74 struct _GstControlBindingPrivate
87 static GParamSpec *properties[PROP_LAST];
90 gst_control_binding_class_init (GstControlBindingClass * klass)
92 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 g_type_class_add_private (klass, sizeof (GstControlBindingPrivate));
96 gobject_class->constructor = gst_control_binding_constructor;
97 gobject_class->set_property = gst_control_binding_set_property;
98 gobject_class->get_property = gst_control_binding_get_property;
99 gobject_class->dispose = gst_control_binding_dispose;
100 gobject_class->finalize = gst_control_binding_finalize;
102 properties[PROP_OBJECT] =
103 g_param_spec_object ("object", "Object",
104 "The object of the property", GST_TYPE_OBJECT,
105 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
107 properties[PROP_NAME] =
108 g_param_spec_string ("name", "Name", "The name of the property", NULL,
109 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
112 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
116 gst_control_binding_init (GstControlBinding * binding)
118 binding->ABI.abi.priv =
119 G_TYPE_INSTANCE_GET_PRIVATE (binding, GST_TYPE_CONTROL_BINDING,
120 GstControlBindingPrivate);
121 g_weak_ref_init (&binding->ABI.abi.priv->object, NULL);
125 gst_control_binding_constructor (GType type, guint n_construct_params,
126 GObjectConstructParam * construct_params)
128 GstControlBinding *binding;
133 GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
134 ->constructor (type, n_construct_params, construct_params));
136 object = g_weak_ref_get (&binding->ABI.abi.priv->object);
138 GST_WARNING_OBJECT (object, "no object set");
139 return (GObject *) binding;
142 GST_INFO_OBJECT (object, "trying to put property '%s' under control",
145 /* check if the object has a property of that name */
147 g_object_class_find_property (G_OBJECT_GET_CLASS (object),
149 GST_DEBUG_OBJECT (object, " psec->flags : 0x%08x", pspec->flags);
151 /* check if this param is witable && controlable && !construct-only */
152 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
153 G_PARAM_CONSTRUCT_ONLY)) ==
154 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
155 binding->pspec = pspec;
157 GST_WARNING_OBJECT (object,
158 "property '%s' on class '%s' needs to "
159 "be writeable, controlable and not construct_only", binding->name,
160 G_OBJECT_TYPE_NAME (object));
163 GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
164 G_OBJECT_TYPE_NAME (object), binding->name);
167 gst_object_unref (object);
169 return (GObject *) binding;
173 gst_control_binding_dispose (GObject * object)
175 GstControlBinding *self = GST_CONTROL_BINDING (object);
177 /* we did not took a reference */
178 g_object_remove_weak_pointer ((GObject *) self->__object,
179 (gpointer *) & self->__object);
180 self->__object = NULL;
181 g_weak_ref_clear (&self->ABI.abi.priv->object);
183 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
187 gst_control_binding_finalize (GObject * object)
189 GstControlBinding *self = GST_CONTROL_BINDING (object);
193 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
197 gst_control_binding_set_property (GObject * object, guint prop_id,
198 const GValue * value, GParamSpec * pspec)
200 GstControlBinding *self = GST_CONTROL_BINDING (object);
204 /* do not ref to avoid a ref cycle */
205 self->__object = g_value_get_object (value);
206 g_object_add_weak_pointer ((GObject *) self->__object,
207 (gpointer *) & self->__object);
209 g_weak_ref_set (&self->ABI.abi.priv->object, self->__object);
212 self->name = g_value_dup_string (value);
215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
221 gst_control_binding_get_property (GObject * object, guint prop_id,
222 GValue * value, GParamSpec * pspec)
224 GstControlBinding *self = GST_CONTROL_BINDING (object);
228 g_value_take_object (value, g_weak_ref_get (&self->ABI.abi.priv->object));
231 g_value_set_string (value, self->name);
234 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 * gst_control_binding_sync_values:
243 * @binding: the control binding
244 * @object: the object that has controlled properties
245 * @timestamp: the time that should be processed
246 * @last_sync: the last time this was called
248 * Sets the property of the @object, according to the #GstControlSources that
249 * handle them and for the given timestamp.
251 * If this function fails, it is most likely the application developers fault.
252 * Most probably the control sources are not setup correctly.
254 * Returns: %TRUE if the controller value could be applied to the object
255 * property, %FALSE otherwise
258 gst_control_binding_sync_values (GstControlBinding * binding,
259 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
261 GstControlBindingClass *klass;
262 gboolean ret = FALSE;
264 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
266 if (binding->disabled)
269 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
271 if (G_LIKELY (klass->sync_values != NULL)) {
272 ret = klass->sync_values (binding, object, timestamp, last_sync);
274 GST_WARNING_OBJECT (binding, "missing sync_values implementation");
280 * gst_control_binding_get_value:
281 * @binding: the control binding
282 * @timestamp: the time the control-change should be read from
284 * Gets the value for the given controlled property at the requested time.
286 * Returns: (nullable): the GValue of the property at the given time,
287 * or %NULL if the property isn't controlled.
290 gst_control_binding_get_value (GstControlBinding * binding,
291 GstClockTime timestamp)
293 GstControlBindingClass *klass;
296 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
297 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
299 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
301 if (G_LIKELY (klass->get_value != NULL)) {
302 ret = klass->get_value (binding, timestamp);
304 GST_WARNING_OBJECT (binding, "missing get_value implementation");
310 * gst_control_binding_get_value_array: (skip)
311 * @binding: the control binding
312 * @timestamp: the time that should be processed
313 * @interval: the time spacing between subsequent values
314 * @n_values: the number of values
315 * @values: (array length=n_values): array to put control-values in
317 * Gets a number of values for the given controlled property starting at the
318 * requested time. The array @values need to hold enough space for @n_values of
319 * the same type as the objects property's type.
321 * This function is useful if one wants to e.g. draw a graph of the control
322 * curve or apply a control curve sample by sample.
324 * The values are unboxed and ready to be used. The similar function
325 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
326 * more suitable for bindings.
328 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
331 gst_control_binding_get_value_array (GstControlBinding * binding,
332 GstClockTime timestamp, GstClockTime interval, guint n_values,
335 GstControlBindingClass *klass;
336 gboolean ret = FALSE;
338 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
339 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
340 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
341 g_return_val_if_fail (values, FALSE);
343 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
345 if (G_LIKELY (klass->get_value_array != NULL)) {
347 klass->get_value_array (binding, timestamp, interval, n_values, values);
349 GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
354 #define CONVERT_ARRAY(type,TYPE) \
356 g##type *v = g_new (g##type,n_values); \
357 ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
360 for (i = 0; i < n_values; i++) { \
361 g_value_init (&values[i], G_TYPE_##TYPE); \
362 g_value_set_##type (&values[i], v[i]); \
369 * gst_control_binding_get_g_value_array:
370 * @binding: the control binding
371 * @timestamp: the time that should be processed
372 * @interval: the time spacing between subsequent values
373 * @n_values: the number of values
374 * @values: (array length=n_values): array to put control-values in
376 * Gets a number of #GValues for the given controlled property starting at the
377 * requested time. The array @values need to hold enough space for @n_values of
380 * This function is useful if one wants to e.g. draw a graph of the control
381 * curve or apply a control curve sample by sample.
383 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
386 gst_control_binding_get_g_value_array (GstControlBinding * binding,
387 GstClockTime timestamp, GstClockTime interval, guint n_values,
390 GstControlBindingClass *klass;
391 gboolean ret = FALSE;
393 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
394 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
395 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
396 g_return_val_if_fail (values, FALSE);
398 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
400 if (G_LIKELY (klass->get_g_value_array != NULL)) {
402 klass->get_g_value_array (binding, timestamp, interval, n_values,
408 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
409 while ((type = g_type_parent (type)))
412 GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
416 CONVERT_ARRAY (int, INT);
419 CONVERT_ARRAY (uint, UINT);
422 CONVERT_ARRAY (long, LONG);
425 CONVERT_ARRAY (ulong, ULONG);
428 CONVERT_ARRAY (int64, INT64);
431 CONVERT_ARRAY (uint64, UINT64);
434 CONVERT_ARRAY (float, FLOAT);
437 CONVERT_ARRAY (double, DOUBLE);
440 CONVERT_ARRAY (boolean, BOOLEAN);
444 gint *v = g_new (gint, n_values);
445 ret = gst_control_binding_get_value_array (binding, timestamp, interval,
448 for (i = 0; i < n_values; i++) {
449 g_value_init (&values[i], type);
450 g_value_set_enum (&values[i], v[i]);
457 GST_WARNING ("incomplete implementation for paramspec type '%s'",
458 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
459 GST_CONTROL_BINDING_PSPEC (binding) = NULL;
467 * gst_control_binding_set_disabled:
468 * @binding: the control binding
469 * @disabled: boolean that specifies whether to disable the controller
472 * This function is used to disable a control binding for some time, i.e.
473 * gst_object_sync_values() will do nothing.
476 gst_control_binding_set_disabled (GstControlBinding * binding,
479 g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
480 binding->disabled = disabled;
484 * gst_control_binding_is_disabled:
485 * @binding: the control binding
487 * Check if the control binding is disabled.
489 * Returns: %TRUE if the binding is inactive
492 gst_control_binding_is_disabled (GstControlBinding * binding)
494 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
495 return ! !binding->disabled;