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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 * SECTION:gstcontrolbinding
24 * @short_description: attachment for control source sources
26 * A value mapping object that attaches control sources to gobject properties.
28 /* FIXME(ensonic): should we make gst_object_add_control_binding() internal
29 * - we create the control_binding for a certain object anyway
30 * - we could call gst_object_add_control_binding() at the end of
31 * gst_control_binding_constructor()
32 * - the weak-ref on object is not nice, as is the same as gst_object_parent()
33 * once the object is added to the parent
35 * - another option would be do defer what I am doing in _constructor to when
36 * the parent is set (need to listen to the signal then)
37 * then basically I could
38 * a) remove the obj arg and wait the binding to be added or
39 * b) add the binding from constructor, unref object there and make obj
43 #include "gst_private.h"
45 #include <glib-object.h>
48 #include "gstcontrolbinding.h"
52 #define GST_CAT_DEFAULT control_binding_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
56 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
57 "dynamic parameter control source attachment");
59 static GObject *gst_control_binding_constructor (GType type,
60 guint n_construct_params, GObjectConstructParam * construct_params);
61 static void gst_control_binding_set_property (GObject * object, guint prop_id,
62 const GValue * value, GParamSpec * pspec);
63 static void gst_control_binding_get_property (GObject * object, guint prop_id,
64 GValue * value, GParamSpec * pspec);
65 static void gst_control_binding_dispose (GObject * object);
66 static void gst_control_binding_finalize (GObject * object);
68 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
69 GST_TYPE_OBJECT, _do_init);
79 static GParamSpec *properties[PROP_LAST];
82 gst_control_binding_class_init (GstControlBindingClass * klass)
84 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
86 gobject_class->constructor = gst_control_binding_constructor;
87 gobject_class->set_property = gst_control_binding_set_property;
88 gobject_class->get_property = gst_control_binding_get_property;
89 gobject_class->dispose = gst_control_binding_dispose;
90 gobject_class->finalize = gst_control_binding_finalize;
92 properties[PROP_OBJECT] =
93 g_param_spec_object ("object", "Object",
94 "The object of the property", GST_TYPE_OBJECT,
95 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
97 properties[PROP_NAME] =
98 g_param_spec_string ("name", "Name", "The name of the property", NULL,
99 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
102 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
106 gst_control_binding_init (GstControlBinding * binding)
111 gst_control_binding_constructor (GType type, guint n_construct_params,
112 GObjectConstructParam * construct_params)
114 GstControlBinding *binding;
118 GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
119 ->constructor (type, n_construct_params, construct_params));
121 GST_INFO_OBJECT (binding->object, "trying to put property '%s' under control",
124 /* check if the object has a property of that name */
126 g_object_class_find_property (G_OBJECT_GET_CLASS (binding->object),
128 GST_DEBUG_OBJECT (binding->object, " psec->flags : 0x%08x", pspec->flags);
130 /* check if this param is witable && controlable && !construct-only */
131 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
132 G_PARAM_CONSTRUCT_ONLY)) ==
133 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
134 binding->pspec = pspec;
137 GST_WARNING_OBJECT (binding->object, "class '%s' has no property '%s'",
138 G_OBJECT_TYPE_NAME (binding->object), binding->name);
140 return (GObject *) binding;
144 gst_control_binding_dispose (GObject * object)
146 GstControlBinding *self = GST_CONTROL_BINDING (object);
148 /* we did not took a reference */
149 g_object_remove_weak_pointer ((GObject *) self->object,
150 (gpointer *) & self->object);
153 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
157 gst_control_binding_finalize (GObject * object)
159 GstControlBinding *self = GST_CONTROL_BINDING (object);
163 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
167 gst_control_binding_set_property (GObject * object, guint prop_id,
168 const GValue * value, GParamSpec * pspec)
170 GstControlBinding *self = GST_CONTROL_BINDING (object);
174 /* do not ref to avoid a ref cycle */
175 self->object = g_value_get_object (value);
176 g_object_add_weak_pointer ((GObject *) self->object,
177 (gpointer *) & self->object);
180 self->name = g_value_dup_string (value);
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
189 gst_control_binding_get_property (GObject * object, guint prop_id,
190 GValue * value, GParamSpec * pspec)
192 GstControlBinding *self = GST_CONTROL_BINDING (object);
196 g_value_set_object (value, self->object);
199 g_value_set_string (value, self->name);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 * gst_control_binding_sync_values:
211 * @binding: the control binding
212 * @object: the object that has controlled properties
213 * @timestamp: the time that should be processed
214 * @last_sync: the last time this was called
216 * Sets the property of the @object, according to the #GstControlSources that
217 * handle them and for the given timestamp.
219 * If this function fails, it is most likely the application developers fault.
220 * Most probably the control sources are not setup correctly.
222 * Returns: %TRUE if the controller value could be applied to the object
223 * property, %FALSE otherwise
226 gst_control_binding_sync_values (GstControlBinding * binding,
227 GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
229 GstControlBindingClass *klass;
230 gboolean ret = FALSE;
232 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
234 if (binding->disabled)
237 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
239 if (G_LIKELY (klass->sync_values != NULL)) {
240 ret = klass->sync_values (binding, object, timestamp, last_sync);
242 GST_WARNING_OBJECT (binding, "missing sync_values implementation");
248 * gst_control_binding_get_value:
249 * @binding: the control binding
250 * @timestamp: the time the control-change should be read from
252 * Gets the value for the given controlled property at the requested time.
254 * Returns: the GValue of the property at the given time, or %NULL if the
255 * property isn't controlled.
258 gst_control_binding_get_value (GstControlBinding * binding,
259 GstClockTime timestamp)
261 GstControlBindingClass *klass;
264 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
265 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
267 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
269 if (G_LIKELY (klass->get_value != NULL)) {
270 ret = klass->get_value (binding, timestamp);
272 GST_WARNING_OBJECT (binding, "missing get_value implementation");
278 * gst_control_binding_get_value_array:
279 * @binding: the control binding
280 * @timestamp: the time that should be processed
281 * @interval: the time spacing between subsequent values
282 * @n_values: the number of values
283 * @values: array to put control-values in
285 * Gets a number of values for the given controlled property starting at the
286 * requested time. The array @values need to hold enough space for @n_values of
287 * the same type as the objects property's type.
289 * This function is useful if one wants to e.g. draw a graph of the control
290 * curve or apply a control curve sample by sample.
292 * The values are unboxed and ready to be used. The similar function
293 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
294 * better suites for bindings.
296 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
299 gst_control_binding_get_value_array (GstControlBinding * binding,
300 GstClockTime timestamp, GstClockTime interval, guint n_values,
303 GstControlBindingClass *klass;
304 gboolean ret = FALSE;
306 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
307 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
308 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
309 g_return_val_if_fail (values, FALSE);
311 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
313 if (G_LIKELY (klass->get_value_array != NULL)) {
315 klass->get_value_array (binding, timestamp, interval, n_values, values);
317 GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
322 #define CONVERT_ARRAY(type,TYPE) \
324 g##type *v = g_new (g##type,n_values); \
325 ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
328 for (i = 0; i < n_values; i++) { \
329 g_value_init (&values[i], G_TYPE_##TYPE); \
330 g_value_set_##type (&values[i], v[i]); \
337 * gst_control_binding_get_g_value_array:
338 * @binding: the control binding
339 * @timestamp: the time that should be processed
340 * @interval: the time spacing between subsequent values
341 * @n_values: the number of values
342 * @values: array to put control-values in
344 * Gets a number of #GValues for the given controlled property starting at the
345 * requested time. The array @values need to hold enough space for @n_values of
348 * This function is useful if one wants to e.g. draw a graph of the control
349 * curve or apply a control curve sample by sample.
351 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
354 gst_control_binding_get_g_value_array (GstControlBinding * binding,
355 GstClockTime timestamp, GstClockTime interval, guint n_values,
358 GstControlBindingClass *klass;
359 gboolean ret = FALSE;
361 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
362 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
363 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
364 g_return_val_if_fail (values, FALSE);
366 klass = GST_CONTROL_BINDING_GET_CLASS (binding);
368 if (G_LIKELY (klass->get_g_value_array != NULL)) {
370 klass->get_g_value_array (binding, timestamp, interval, n_values,
376 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
377 while ((type = g_type_parent (type)))
380 GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
384 CONVERT_ARRAY (int, INT);
387 CONVERT_ARRAY (uint, UINT);
390 CONVERT_ARRAY (long, LONG);
393 CONVERT_ARRAY (ulong, ULONG);
396 CONVERT_ARRAY (int64, INT64);
399 CONVERT_ARRAY (uint64, UINT64);
402 CONVERT_ARRAY (float, FLOAT);
405 CONVERT_ARRAY (double, DOUBLE);
408 CONVERT_ARRAY (boolean, BOOLEAN);
412 gint *v = g_new (gint, n_values);
413 ret = gst_control_binding_get_value_array (binding, timestamp, interval,
416 for (i = 0; i < n_values; i++) {
417 g_value_init (&values[i], type);
418 g_value_set_enum (&values[i], v[i]);
425 GST_WARNING ("incomplete implementation for paramspec type '%s'",
426 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
427 GST_CONTROL_BINDING_PSPEC (binding) = NULL;
435 * gst_control_binding_set_disabled:
436 * @binding: the control binding
437 * @disabled: boolean that specifies whether to disable the controller
440 * This function is used to disable a control binding for some time, i.e.
441 * gst_object_sync_values() will do nothing.
444 gst_control_binding_set_disabled (GstControlBinding * binding,
447 g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
448 binding->disabled = disabled;
452 * gst_control_binding_is_disabled:
453 * @binding: the control binding
455 * Check if the control binding is disabled.
457 * Returns: %TRUE if the binding is inactive
460 gst_control_binding_is_disabled (GstControlBinding * binding)
462 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
463 return (binding->disabled == TRUE);