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.
29 #include "gst_private.h"
31 #include <glib-object.h>
34 #include "gstcontrolbinding.h"
38 #define GST_CAT_DEFAULT control_binding_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41 static void gst_control_binding_dispose (GObject * object);
42 static void gst_control_binding_finalize (GObject * object);
45 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
46 "dynamic parameter control source attachment");
48 G_DEFINE_TYPE_WITH_CODE (GstControlBinding, gst_control_binding, G_TYPE_OBJECT,
52 gst_control_binding_class_init (GstControlBindingClass * klass)
54 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
56 gobject_class->dispose = gst_control_binding_dispose;
57 gobject_class->finalize = gst_control_binding_finalize;
61 gst_control_binding_init (GstControlBinding * self)
66 gst_control_binding_dispose (GObject * object)
68 GstControlBinding *self = GST_CONTROL_BINDING (object);
71 g_object_unref (self->csource);
77 gst_control_binding_finalize (GObject * object)
79 GstControlBinding *self = GST_CONTROL_BINDING (object);
81 g_value_unset (&self->cur_value);
84 /* mapping functions */
85 #define DEFINE_CONVERT(type,Type,TYPE) \
87 convert_to_##type (GstControlBinding *self, gdouble s, GValue *d) \
89 GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (self->pspec); \
92 s = CLAMP (s, 0.0, 1.0); \
93 v = pspec->minimum + (g##type) ((pspec->maximum - pspec->minimum) * s); \
94 g_value_set_##type (d, v); \
97 DEFINE_CONVERT (int, Int, INT);
98 DEFINE_CONVERT (uint, UInt, UINT);
99 DEFINE_CONVERT (long, Long, LONG);
100 DEFINE_CONVERT (ulong, ULong, ULONG);
101 DEFINE_CONVERT (int64, Int64, INT64);
102 DEFINE_CONVERT (uint64, UInt64, UINT64);
103 DEFINE_CONVERT (float, Float, FLOAT);
104 DEFINE_CONVERT (double, Double, DOUBLE);
107 convert_to_boolean (GstControlBinding * self, gdouble s, GValue * d)
109 s = CLAMP (s, 0.0, 1.0);
110 g_value_set_boolean (d, (gboolean) (s + 0.5));
114 convert_to_enum (GstControlBinding * self, gdouble s, GValue * d)
116 GParamSpecEnum *pspec = G_PARAM_SPEC_ENUM (self->pspec);
117 GEnumClass *e = pspec->enum_class;
120 s = CLAMP (s, 0.0, 1.0);
121 v = s * (e->n_values - 1);
122 g_value_set_enum (d, e->values[v].value);
126 * gst_control_binding_new:
127 * @object: the object of the property
128 * @property_name: the property-name to attach the control source
129 * @csource: the control source
131 * Create a new control-binding that attaches the #GstControlSource to the
134 * Returns: the new #GstControlBinding
137 gst_control_binding_new (GstObject * object, const gchar * property_name,
138 GstControlSource * csource)
140 GstControlBinding *self = NULL;
143 GST_INFO_OBJECT (object, "trying to put property '%s' under control",
146 /* check if the object has a property of that name */
147 if ((pspec = 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 g_return_val_if_fail ((pspec->flags & (G_PARAM_WRITABLE |
153 GST_PARAM_CONTROLLABLE | G_PARAM_CONSTRUCT_ONLY)) ==
154 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE), NULL);
156 if ((self = (GstControlBinding *) g_object_newv (GST_TYPE_CONTROL_BINDING,
158 GType type = G_PARAM_SPEC_VALUE_TYPE (pspec), base;
160 // add pspec as a construction parameter and move below to construct()
162 self->name = pspec->name;
163 self->csource = g_object_ref (csource);
164 self->disabled = FALSE;
166 g_value_init (&self->cur_value, type);
168 base = type = G_PARAM_SPEC_VALUE_TYPE (pspec);
169 while ((type = g_type_parent (type)))
172 GST_DEBUG_OBJECT (object, " using type %s", g_type_name (base));
174 // select mapping function
175 // FIXME: only select mapping if super class hasn't set any?
178 self->convert = convert_to_int;
181 self->convert = convert_to_uint;
184 self->convert = convert_to_long;
187 self->convert = convert_to_ulong;
190 self->convert = convert_to_int64;
193 self->convert = convert_to_uint64;
196 self->convert = convert_to_float;
199 self->convert = convert_to_double;
202 self->convert = convert_to_boolean;
205 self->convert = convert_to_enum;
208 // FIXME: return NULL?
209 GST_WARNING ("incomplete implementation for paramspec type '%s'",
210 G_PARAM_SPEC_TYPE_NAME (pspec));
215 GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
216 G_OBJECT_TYPE_NAME (object), property_name);
224 * gst_control_binding_sync_values:
225 * @self: the control binding
226 * @object: the object that has controlled properties
227 * @timestamp: the time that should be processed
228 * @last_sync: the last time this was called
230 * Sets the property of the @object, according to the #GstControlSources that
231 * handle them and for the given timestamp.
233 * If this function fails, it is most likely the application developers fault.
234 * Most probably the control sources are not setup correctly.
236 * Returns: %TRUE if the controller value could be applied to the object
237 * property, %FALSE otherwise
240 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
241 GstClockTime timestamp, GstClockTime last_sync)
247 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
252 GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
253 self->name, GST_TIME_ARGS (timestamp));
255 dst_val = &self->cur_value;
256 ret = gst_control_source_get_value (self->csource, timestamp, &src_val);
257 if (G_LIKELY (ret)) {
258 GST_LOG_OBJECT (object, " new value %lf", src_val);
259 /* always set the value for first time, but then only if it changed
260 * this should limit g_object_notify invocations.
261 * FIXME: can we detect negative playback rates?
263 if ((timestamp < last_sync) || (src_val != self->last_value)) {
264 GST_LOG_OBJECT (object, " mapping %s to value of type %s", self->name,
265 G_VALUE_TYPE_NAME (dst_val));
266 /* run mapping function to convert gdouble to GValue */
267 self->convert (self, src_val, dst_val);
268 /* we can make this faster
269 * http://bugzilla.gnome.org/show_bug.cgi?id=536939
271 g_object_set_property ((GObject *) object, self->name, dst_val);
272 self->last_value = src_val;
275 GST_DEBUG_OBJECT (object, "no control value for param %s", self->name);
281 * gst_control_binding_get_value:
282 * @self: 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: the GValue of the property at the given time, or %NULL if the
288 * property isn't controlled.
291 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
293 GValue *dst_val = NULL;
296 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
297 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
299 /* get current value via control source */
300 if (gst_control_source_get_value (self->csource, timestamp, &src_val)) {
301 dst_val = g_new0 (GValue, 1);
302 g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (self->pspec));
303 self->convert (self, src_val, dst_val);
305 GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
306 self->name, GST_TIME_ARGS (timestamp));
313 * gst_control_binding_get_value_array:
314 * @self: the control binding
315 * @timestamp: the time that should be processed
316 * @interval: the time spacing between subsequent values
317 * @n_values: the number of values
318 * @values: array to put control-values in
320 * Gets a number of values for the given controllered property starting at the
321 * requested time. The array @values need to hold enough space for @n_values of
322 * the same type as the objects property's type.
324 * This function is useful if one wants to e.g. draw a graph of the control
325 * curve or apply a control curve sample by sample.
327 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
330 gst_control_binding_get_value_array (GstControlBinding * self,
331 GstClockTime timestamp, GstClockTime interval, guint n_values,
336 gboolean res = FALSE;
338 GstControlBindingConvert convert;
340 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
341 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
342 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
343 g_return_val_if_fail (values, FALSE);
345 convert = self->convert;
346 type = G_PARAM_SPEC_VALUE_TYPE (self->pspec);
348 src_val = g_new0 (gdouble, n_values);
349 if ((res = gst_control_source_get_value_array (self->csource, timestamp,
350 interval, n_values, src_val))) {
351 for (i = 0; i < n_values; i++) {
352 if (!isnan (src_val[i])) {
353 g_value_init (&values[i], type);
354 convert (self, src_val[i], &values[i]);
356 GST_LOG ("no control value for property %s at index %d", self->name, i);
360 GST_LOG ("failed to get control value for property %s at ts %"
361 GST_TIME_FORMAT, self->name, GST_TIME_ARGS (timestamp));
370 * gst_control_binding_get_control_source:
371 * @self: the control binding
373 * Get the control source.
375 * Returns: the control source. Unref when done with it.
378 gst_control_binding_get_control_source (GstControlBinding * self)
380 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
381 return g_object_ref (self->csource);
385 * gst_control_binding_set_disabled:
386 * @self: the control binding
387 * @disabled: boolean that specifies whether to disable the controller
390 * This function is used to disable a control binding for some time, i.e.
391 * gst_object_sync_values() will do nothing.
394 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
396 g_return_if_fail (GST_IS_CONTROL_BINDING (self));
397 self->disabled = disabled;
401 * gst_control_binding_is_disabled:
402 * @self: the control binding
404 * Check if the control binding is disabled.
406 * Returns: %TRUE if the binding is inactive
409 gst_control_binding_is_disabled (GstControlBinding * self)
411 g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
412 return (self->disabled == TRUE);