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 * self)
111 gst_control_binding_constructor (GType type, guint n_construct_params,
112 GObjectConstructParam * construct_params)
114 GstControlBinding *self;
117 self = GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
118 ->constructor (type, n_construct_params, construct_params));
120 GST_INFO_OBJECT (self->object, "trying to put property '%s' under control",
123 /* check if the object has a property of that name */
125 g_object_class_find_property (G_OBJECT_GET_CLASS (self->object),
127 GST_DEBUG_OBJECT (self->object, " psec->flags : 0x%08x", pspec->flags);
129 /* check if this param is witable && controlable && !construct-only */
130 if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
131 G_PARAM_CONSTRUCT_ONLY)) ==
132 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
136 GST_WARNING_OBJECT (self->object, "class '%s' has no property '%s'",
137 G_OBJECT_TYPE_NAME (self->object), self->name);
139 return (GObject *) self;
143 gst_control_binding_dispose (GObject * object)
145 GstControlBinding *self = GST_CONTROL_BINDING (object);
147 /* we did not took a reference */
148 g_object_remove_weak_pointer ((GObject *) self->object,
149 (gpointer *) & self->object);
152 ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
156 gst_control_binding_finalize (GObject * object)
158 GstControlBinding *self = GST_CONTROL_BINDING (object);
162 ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
166 gst_control_binding_set_property (GObject * object, guint prop_id,
167 const GValue * value, GParamSpec * pspec)
169 GstControlBinding *self = GST_CONTROL_BINDING (object);
173 /* do not ref to avoid a ref cycle */
174 self->object = g_value_get_object (value);
175 g_object_add_weak_pointer ((GObject *) self->object,
176 (gpointer *) & self->object);
179 self->name = g_value_dup_string (value);
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 gst_control_binding_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec)
191 GstControlBinding *self = GST_CONTROL_BINDING (object);
195 g_value_set_object (value, self->object);
198 g_value_set_string (value, self->name);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209 * gst_control_binding_sync_values:
210 * @self: the control binding
211 * @object: the object that has controlled properties
212 * @timestamp: the time that should be processed
213 * @last_sync: the last time this was called
215 * Sets the property of the @object, according to the #GstControlSources that
216 * handle them and for the given timestamp.
218 * If this function fails, it is most likely the application developers fault.
219 * Most probably the control sources are not setup correctly.
221 * Returns: %TRUE if the controller value could be applied to the object
222 * property, %FALSE otherwise
225 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
226 GstClockTime timestamp, GstClockTime last_sync)
228 GstControlBindingClass *klass;
229 gboolean ret = FALSE;
231 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
236 klass = GST_CONTROL_BINDING_GET_CLASS (self);
238 if (G_LIKELY (klass->sync_values != NULL)) {
239 ret = klass->sync_values (self, object, timestamp, last_sync);
241 GST_WARNING_OBJECT (self, "missing sync_values implementation");
247 * gst_control_binding_get_value:
248 * @self: the control binding
249 * @timestamp: the time the control-change should be read from
251 * Gets the value for the given controlled property at the requested time.
253 * Returns: the GValue of the property at the given time, or %NULL if the
254 * property isn't controlled.
257 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
259 GstControlBindingClass *klass;
262 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
263 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
265 klass = GST_CONTROL_BINDING_GET_CLASS (self);
267 if (G_LIKELY (klass->get_value != NULL)) {
268 ret = klass->get_value (self, timestamp);
270 GST_WARNING_OBJECT (self, "missing get_value implementation");
276 * gst_control_binding_get_value_array:
277 * @self: the control binding
278 * @timestamp: the time that should be processed
279 * @interval: the time spacing between subsequent values
280 * @n_values: the number of values
281 * @values: array to put control-values in
283 * Gets a number of values for the given controlled property starting at the
284 * requested time. The array @values need to hold enough space for @n_values of
285 * the same type as the objects property's type.
287 * This function is useful if one wants to e.g. draw a graph of the control
288 * curve or apply a control curve sample by sample.
290 * The values are unboxed and ready to be used. The similar function
291 * gst_control_binding_get_g_value_array() returns the array as #GValues and is
292 * better suites for bindings.
294 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
297 gst_control_binding_get_value_array (GstControlBinding * self,
298 GstClockTime timestamp, GstClockTime interval, guint n_values,
301 GstControlBindingClass *klass;
302 gboolean ret = FALSE;
304 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
305 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
306 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
307 g_return_val_if_fail (values, FALSE);
309 klass = GST_CONTROL_BINDING_GET_CLASS (self);
311 if (G_LIKELY (klass->get_value_array != NULL)) {
312 ret = klass->get_value_array (self, timestamp, interval, n_values, values);
314 GST_WARNING_OBJECT (self, "missing get_value_array implementation");
319 #define CONVERT_ARRAY(type,TYPE) \
321 g##type *v = g_new (g##type,n_values); \
322 ret = gst_control_binding_get_value_array (self, timestamp, interval, \
325 for (i = 0; i < n_values; i++) { \
326 g_value_init (&values[i], G_TYPE_##TYPE); \
327 g_value_set_##type (&values[i], v[i]); \
334 * gst_control_binding_get_g_value_array:
335 * @self: the control binding
336 * @timestamp: the time that should be processed
337 * @interval: the time spacing between subsequent values
338 * @n_values: the number of values
339 * @values: array to put control-values in
341 * Gets a number of #GValues for the given controlled property starting at the
342 * requested time. The array @values need to hold enough space for @n_values of
345 * This function is useful if one wants to e.g. draw a graph of the control
346 * curve or apply a control curve sample by sample.
348 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
351 gst_control_binding_get_g_value_array (GstControlBinding * self,
352 GstClockTime timestamp, GstClockTime interval, guint n_values,
355 GstControlBindingClass *klass;
356 gboolean ret = FALSE;
358 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
359 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
360 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
361 g_return_val_if_fail (values, FALSE);
363 klass = GST_CONTROL_BINDING_GET_CLASS (self);
365 if (G_LIKELY (klass->get_g_value_array != NULL)) {
367 klass->get_g_value_array (self, timestamp, interval, n_values, values);
372 base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (self));
373 while ((type = g_type_parent (type)))
376 GST_INFO_OBJECT (self, "missing get_g_value_array implementation, we're "
380 CONVERT_ARRAY (int, INT);
383 CONVERT_ARRAY (uint, UINT);
386 CONVERT_ARRAY (long, LONG);
389 CONVERT_ARRAY (ulong, ULONG);
392 CONVERT_ARRAY (int64, INT64);
395 CONVERT_ARRAY (uint64, UINT64);
398 CONVERT_ARRAY (float, FLOAT);
401 CONVERT_ARRAY (double, DOUBLE);
404 CONVERT_ARRAY (boolean, BOOLEAN);
408 gint *v = g_new (gint, n_values);
409 ret = gst_control_binding_get_value_array (self, timestamp, interval,
412 for (i = 0; i < n_values; i++) {
413 g_value_init (&values[i], type);
414 g_value_set_enum (&values[i], v[i]);
421 GST_WARNING ("incomplete implementation for paramspec type '%s'",
422 G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (self)));
423 GST_CONTROL_BINDING_PSPEC (self) = NULL;
431 * gst_control_binding_set_disabled:
432 * @self: the control binding
433 * @disabled: boolean that specifies whether to disable the controller
436 * This function is used to disable a control binding for some time, i.e.
437 * gst_object_sync_values() will do nothing.
440 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
442 g_return_if_fail (GST_IS_CONTROL_BINDING (self));
443 self->disabled = disabled;
447 * gst_control_binding_is_disabled:
448 * @self: the control binding
450 * Check if the control binding is disabled.
452 * Returns: %TRUE if the binding is inactive
455 gst_control_binding_is_disabled (GstControlBinding * self)
457 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
458 return (self->disabled == TRUE);