controller: expand the api to offer functions for plain and GValue arrays
[platform/upstream/gstreamer.git] / gst / gstcontrolbinding.c
1 /* GStreamer
2  *
3  * Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
4  *
5  * gstcontrolbinding.c: Attachment for control sources
6  *
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.
11  *
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.
16  *
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.
21  */
22 /**
23  * SECTION:gstcontrolbinding
24  * @short_description: attachment for control source sources
25  *
26  * A value mapping object that attaches control sources to gobject properties.
27  */
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
34  *
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
40  *      writeonly
41  */
42
43 #include "gst_private.h"
44
45 #include <glib-object.h>
46 #include <gst/gst.h>
47
48 #include "gstcontrolbinding.h"
49
50 #include <math.h>
51
52 #define GST_CAT_DEFAULT control_binding_debug
53 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
54
55 #define _do_init \
56   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
57       "dynamic parameter control source attachment");
58
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);
67
68 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
69     GST_TYPE_OBJECT, _do_init);
70
71 enum
72 {
73   PROP_0,
74   PROP_OBJECT,
75   PROP_NAME,
76   PROP_LAST
77 };
78
79 static GParamSpec *properties[PROP_LAST];
80
81 static void
82 gst_control_binding_class_init (GstControlBindingClass * klass)
83 {
84   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
85
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;
91
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);
96
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);
100
101
102   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
103 }
104
105 static void
106 gst_control_binding_init (GstControlBinding * self)
107 {
108 }
109
110 static GObject *
111 gst_control_binding_constructor (GType type, guint n_construct_params,
112     GObjectConstructParam * construct_params)
113 {
114   GstControlBinding *self;
115   GParamSpec *pspec;
116
117   self = GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
118       ->constructor (type, n_construct_params, construct_params));
119
120   GST_INFO_OBJECT (self->object, "trying to put property '%s' under control",
121       self->name);
122
123   /* check if the object has a property of that name */
124   if ((pspec =
125           g_object_class_find_property (G_OBJECT_GET_CLASS (self->object),
126               self->name))) {
127     GST_DEBUG_OBJECT (self->object, "  psec->flags : 0x%08x", pspec->flags);
128
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)) {
133       self->pspec = pspec;
134     }
135   } else {
136     GST_WARNING_OBJECT (self->object, "class '%s' has no property '%s'",
137         G_OBJECT_TYPE_NAME (self->object), self->name);
138   }
139   return (GObject *) self;
140 }
141
142 static void
143 gst_control_binding_dispose (GObject * object)
144 {
145   GstControlBinding *self = GST_CONTROL_BINDING (object);
146
147   /* we did not took a reference */
148   g_object_remove_weak_pointer ((GObject *) self->object,
149       (gpointer *) & self->object);
150   self->object = NULL;
151
152   ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
153 }
154
155 static void
156 gst_control_binding_finalize (GObject * object)
157 {
158   GstControlBinding *self = GST_CONTROL_BINDING (object);
159
160   g_free (self->name);
161
162   ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
163 }
164
165 static void
166 gst_control_binding_set_property (GObject * object, guint prop_id,
167     const GValue * value, GParamSpec * pspec)
168 {
169   GstControlBinding *self = GST_CONTROL_BINDING (object);
170
171   switch (prop_id) {
172     case PROP_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);
177       break;
178     case PROP_NAME:
179       self->name = g_value_dup_string (value);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186
187 static void
188 gst_control_binding_get_property (GObject * object, guint prop_id,
189     GValue * value, GParamSpec * pspec)
190 {
191   GstControlBinding *self = GST_CONTROL_BINDING (object);
192
193   switch (prop_id) {
194     case PROP_OBJECT:
195       g_value_set_object (value, self->object);
196       break;
197     case PROP_NAME:
198       g_value_set_string (value, self->name);
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204 }
205
206 /* functions */
207
208 /**
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
214  *
215  * Sets the property of the @object, according to the #GstControlSources that
216  * handle them and for the given timestamp.
217  *
218  * If this function fails, it is most likely the application developers fault.
219  * Most probably the control sources are not setup correctly.
220  *
221  * Returns: %TRUE if the controller value could be applied to the object
222  * property, %FALSE otherwise
223  */
224 gboolean
225 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
226     GstClockTime timestamp, GstClockTime last_sync)
227 {
228   GstControlBindingClass *klass;
229   gboolean ret = FALSE;
230
231   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
232
233   if (self->disabled)
234     return TRUE;
235
236   klass = GST_CONTROL_BINDING_GET_CLASS (self);
237
238   if (G_LIKELY (klass->sync_values != NULL)) {
239     ret = klass->sync_values (self, object, timestamp, last_sync);
240   } else {
241     GST_WARNING_OBJECT (self, "missing sync_values implementation");
242   }
243   return ret;
244 }
245
246 /**
247  * gst_control_binding_get_value:
248  * @self: the control binding
249  * @timestamp: the time the control-change should be read from
250  *
251  * Gets the value for the given controlled property at the requested time.
252  *
253  * Returns: the GValue of the property at the given time, or %NULL if the
254  * property isn't controlled.
255  */
256 GValue *
257 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
258 {
259   GstControlBindingClass *klass;
260   GValue *ret = NULL;
261
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);
264
265   klass = GST_CONTROL_BINDING_GET_CLASS (self);
266
267   if (G_LIKELY (klass->get_value != NULL)) {
268     ret = klass->get_value (self, timestamp);
269   } else {
270     GST_WARNING_OBJECT (self, "missing get_value implementation");
271   }
272   return ret;
273 }
274
275 /**
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
282  *
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.
286  *
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.
289  *
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.
293  *
294  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
295  */
296 gboolean
297 gst_control_binding_get_value_array (GstControlBinding * self,
298     GstClockTime timestamp, GstClockTime interval, guint n_values,
299     gpointer values)
300 {
301   GstControlBindingClass *klass;
302   gboolean ret = FALSE;
303
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);
308
309   klass = GST_CONTROL_BINDING_GET_CLASS (self);
310
311   if (G_LIKELY (klass->get_value_array != NULL)) {
312     ret = klass->get_value_array (self, timestamp, interval, n_values, values);
313   } else {
314     GST_WARNING_OBJECT (self, "missing get_value_array implementation");
315   }
316   return ret;
317 }
318
319 /**
320  * gst_control_binding_get_g_value_array:
321  * @self: the control binding
322  * @timestamp: the time that should be processed
323  * @interval: the time spacing between subsequent values
324  * @n_values: the number of values
325  * @values: array to put control-values in
326  *
327  * Gets a number of #GValues for the given controlled property starting at the
328  * requested time. The array @values need to hold enough space for @n_values of
329  * #GValue.
330  *
331  * This function is useful if one wants to e.g. draw a graph of the control
332  * curve or apply a control curve sample by sample.
333  *
334  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
335  */
336 gboolean
337 gst_control_binding_get_g_value_array (GstControlBinding * self,
338     GstClockTime timestamp, GstClockTime interval, guint n_values,
339     GValue * values)
340 {
341   GstControlBindingClass *klass;
342   gboolean ret = FALSE;
343
344   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
345   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
346   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
347   g_return_val_if_fail (values, FALSE);
348
349   klass = GST_CONTROL_BINDING_GET_CLASS (self);
350
351   if (G_LIKELY (klass->get_g_value_array != NULL)) {
352     ret =
353         klass->get_g_value_array (self, timestamp, interval, n_values, values);
354   } else {
355     GST_WARNING_OBJECT (self, "missing get_g_value_array implementation");
356     // FIXME(ensonic): emulate
357   }
358   return ret;
359 }
360
361 /**
362  * gst_control_binding_set_disabled:
363  * @self: the control binding
364  * @disabled: boolean that specifies whether to disable the controller
365  * or not.
366  *
367  * This function is used to disable a control binding for some time, i.e.
368  * gst_object_sync_values() will do nothing.
369  */
370 void
371 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
372 {
373   g_return_if_fail (GST_IS_CONTROL_BINDING (self));
374   self->disabled = disabled;
375 }
376
377 /**
378  * gst_control_binding_is_disabled:
379  * @self: the control binding
380  *
381  * Check if the control binding is disabled.
382  *
383  * Returns: %TRUE if the binding is inactive
384  */
385 gboolean
386 gst_control_binding_is_disabled (GstControlBinding * self)
387 {
388   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
389   return (self->disabled == TRUE);
390 }