docs: improve api docs for controlsource and -binding
[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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 /**
23  * SECTION:gstcontrolbinding
24  * @short_description: attachment for control source sources
25  *
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
29  * bound property.
30  */
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
37  *
38  * - another option would be do defer what I am doing 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
43  *      writeonly
44  */
45
46 #include "gst_private.h"
47
48 #include <glib-object.h>
49 #include <gst/gst.h>
50
51 #include "gstcontrolbinding.h"
52
53 #include <math.h>
54
55 #define GST_CAT_DEFAULT control_binding_debug
56 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
57
58 #define _do_init \
59   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
60       "dynamic parameter control source attachment");
61
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);
70
71 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
72     GST_TYPE_OBJECT, _do_init);
73
74 enum
75 {
76   PROP_0,
77   PROP_OBJECT,
78   PROP_NAME,
79   PROP_LAST
80 };
81
82 static GParamSpec *properties[PROP_LAST];
83
84 static void
85 gst_control_binding_class_init (GstControlBindingClass * klass)
86 {
87   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
88
89   gobject_class->constructor = gst_control_binding_constructor;
90   gobject_class->set_property = gst_control_binding_set_property;
91   gobject_class->get_property = gst_control_binding_get_property;
92   gobject_class->dispose = gst_control_binding_dispose;
93   gobject_class->finalize = gst_control_binding_finalize;
94
95   properties[PROP_OBJECT] =
96       g_param_spec_object ("object", "Object",
97       "The object of the property", GST_TYPE_OBJECT,
98       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
99
100   properties[PROP_NAME] =
101       g_param_spec_string ("name", "Name", "The name of the property", NULL,
102       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
103
104
105   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
106 }
107
108 static void
109 gst_control_binding_init (GstControlBinding * binding)
110 {
111 }
112
113 static GObject *
114 gst_control_binding_constructor (GType type, guint n_construct_params,
115     GObjectConstructParam * construct_params)
116 {
117   GstControlBinding *binding;
118   GParamSpec *pspec;
119
120   binding =
121       GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
122       ->constructor (type, n_construct_params, construct_params));
123
124   GST_INFO_OBJECT (binding->object, "trying to put property '%s' under control",
125       binding->name);
126
127   /* check if the object has a property of that name */
128   if ((pspec =
129           g_object_class_find_property (G_OBJECT_GET_CLASS (binding->object),
130               binding->name))) {
131     GST_DEBUG_OBJECT (binding->object, "  psec->flags : 0x%08x", pspec->flags);
132
133     /* check if this param is witable && controlable && !construct-only */
134     if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
135                 G_PARAM_CONSTRUCT_ONLY)) ==
136         (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
137       binding->pspec = pspec;
138     }
139   } else {
140     GST_WARNING_OBJECT (binding->object, "class '%s' has no property '%s'",
141         G_OBJECT_TYPE_NAME (binding->object), binding->name);
142   }
143   return (GObject *) binding;
144 }
145
146 static void
147 gst_control_binding_dispose (GObject * object)
148 {
149   GstControlBinding *self = GST_CONTROL_BINDING (object);
150
151   /* we did not took a reference */
152   g_object_remove_weak_pointer ((GObject *) self->object,
153       (gpointer *) & self->object);
154   self->object = NULL;
155
156   ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
157 }
158
159 static void
160 gst_control_binding_finalize (GObject * object)
161 {
162   GstControlBinding *self = GST_CONTROL_BINDING (object);
163
164   g_free (self->name);
165
166   ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
167 }
168
169 static void
170 gst_control_binding_set_property (GObject * object, guint prop_id,
171     const GValue * value, GParamSpec * pspec)
172 {
173   GstControlBinding *self = GST_CONTROL_BINDING (object);
174
175   switch (prop_id) {
176     case PROP_OBJECT:
177       /* do not ref to avoid a ref cycle */
178       self->object = g_value_get_object (value);
179       g_object_add_weak_pointer ((GObject *) self->object,
180           (gpointer *) & self->object);
181       break;
182     case PROP_NAME:
183       self->name = g_value_dup_string (value);
184       break;
185     default:
186       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187       break;
188   }
189 }
190
191 static void
192 gst_control_binding_get_property (GObject * object, guint prop_id,
193     GValue * value, GParamSpec * pspec)
194 {
195   GstControlBinding *self = GST_CONTROL_BINDING (object);
196
197   switch (prop_id) {
198     case PROP_OBJECT:
199       g_value_set_object (value, self->object);
200       break;
201     case PROP_NAME:
202       g_value_set_string (value, self->name);
203       break;
204     default:
205       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206       break;
207   }
208 }
209
210 /* functions */
211
212 /**
213  * gst_control_binding_sync_values:
214  * @binding: the control binding
215  * @object: the object that has controlled properties
216  * @timestamp: the time that should be processed
217  * @last_sync: the last time this was called
218  *
219  * Sets the property of the @object, according to the #GstControlSources that
220  * handle them and for the given timestamp.
221  *
222  * If this function fails, it is most likely the application developers fault.
223  * Most probably the control sources are not setup correctly.
224  *
225  * Returns: %TRUE if the controller value could be applied to the object
226  * property, %FALSE otherwise
227  */
228 gboolean
229 gst_control_binding_sync_values (GstControlBinding * binding,
230     GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
231 {
232   GstControlBindingClass *klass;
233   gboolean ret = FALSE;
234
235   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
236
237   if (binding->disabled)
238     return TRUE;
239
240   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
241
242   if (G_LIKELY (klass->sync_values != NULL)) {
243     ret = klass->sync_values (binding, object, timestamp, last_sync);
244   } else {
245     GST_WARNING_OBJECT (binding, "missing sync_values implementation");
246   }
247   return ret;
248 }
249
250 /**
251  * gst_control_binding_get_value:
252  * @binding: the control binding
253  * @timestamp: the time the control-change should be read from
254  *
255  * Gets the value for the given controlled property at the requested time.
256  *
257  * Returns: the GValue of the property at the given time, or %NULL if the
258  * property isn't controlled.
259  */
260 GValue *
261 gst_control_binding_get_value (GstControlBinding * binding,
262     GstClockTime timestamp)
263 {
264   GstControlBindingClass *klass;
265   GValue *ret = NULL;
266
267   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), NULL);
268   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
269
270   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
271
272   if (G_LIKELY (klass->get_value != NULL)) {
273     ret = klass->get_value (binding, timestamp);
274   } else {
275     GST_WARNING_OBJECT (binding, "missing get_value implementation");
276   }
277   return ret;
278 }
279
280 /**
281  * gst_control_binding_get_value_array:
282  * @binding: the control binding
283  * @timestamp: the time that should be processed
284  * @interval: the time spacing between subsequent values
285  * @n_values: the number of values
286  * @values: array to put control-values in
287  *
288  * Gets a number of values for the given controlled property starting at the
289  * requested time. The array @values need to hold enough space for @n_values of
290  * the same type as the objects property's type.
291  *
292  * This function is useful if one wants to e.g. draw a graph of the control
293  * curve or apply a control curve sample by sample.
294  *
295  * The values are unboxed and ready to be used. The similar function 
296  * gst_control_binding_get_g_value_array() returns the array as #GValues and is
297  * better suites for bindings.
298  *
299  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
300  */
301 gboolean
302 gst_control_binding_get_value_array (GstControlBinding * binding,
303     GstClockTime timestamp, GstClockTime interval, guint n_values,
304     gpointer values)
305 {
306   GstControlBindingClass *klass;
307   gboolean ret = FALSE;
308
309   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
310   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
311   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
312   g_return_val_if_fail (values, FALSE);
313
314   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
315
316   if (G_LIKELY (klass->get_value_array != NULL)) {
317     ret =
318         klass->get_value_array (binding, timestamp, interval, n_values, values);
319   } else {
320     GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
321   }
322   return ret;
323 }
324
325 #define CONVERT_ARRAY(type,TYPE) \
326 { \
327   g##type *v = g_new (g##type,n_values); \
328   ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
329       n_values, v); \
330   if (ret) { \
331     for (i = 0; i < n_values; i++) { \
332       g_value_init (&values[i], G_TYPE_##TYPE); \
333       g_value_set_##type (&values[i], v[i]); \
334     } \
335   } \
336   g_free (v); \
337 }
338
339 /**
340  * gst_control_binding_get_g_value_array:
341  * @binding: the control binding
342  * @timestamp: the time that should be processed
343  * @interval: the time spacing between subsequent values
344  * @n_values: the number of values
345  * @values: array to put control-values in
346  *
347  * Gets a number of #GValues for the given controlled property starting at the
348  * requested time. The array @values need to hold enough space for @n_values of
349  * #GValue.
350  *
351  * This function is useful if one wants to e.g. draw a graph of the control
352  * curve or apply a control curve sample by sample.
353  *
354  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
355  */
356 gboolean
357 gst_control_binding_get_g_value_array (GstControlBinding * binding,
358     GstClockTime timestamp, GstClockTime interval, guint n_values,
359     GValue * values)
360 {
361   GstControlBindingClass *klass;
362   gboolean ret = FALSE;
363
364   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
365   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
366   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
367   g_return_val_if_fail (values, FALSE);
368
369   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
370
371   if (G_LIKELY (klass->get_g_value_array != NULL)) {
372     ret =
373         klass->get_g_value_array (binding, timestamp, interval, n_values,
374         values);
375   } else {
376     guint i;
377     GType type, base;
378
379     base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
380     while ((type = g_type_parent (type)))
381       base = type;
382
383     GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
384         "emulating it");
385     switch (base) {
386       case G_TYPE_INT:
387         CONVERT_ARRAY (int, INT);
388         break;
389       case G_TYPE_UINT:
390         CONVERT_ARRAY (uint, UINT);
391         break;
392       case G_TYPE_LONG:
393         CONVERT_ARRAY (long, LONG);
394         break;
395       case G_TYPE_ULONG:
396         CONVERT_ARRAY (ulong, ULONG);
397         break;
398       case G_TYPE_INT64:
399         CONVERT_ARRAY (int64, INT64);
400         break;
401       case G_TYPE_UINT64:
402         CONVERT_ARRAY (uint64, UINT64);
403         break;
404       case G_TYPE_FLOAT:
405         CONVERT_ARRAY (float, FLOAT);
406         break;
407       case G_TYPE_DOUBLE:
408         CONVERT_ARRAY (double, DOUBLE);
409         break;
410       case G_TYPE_BOOLEAN:
411         CONVERT_ARRAY (boolean, BOOLEAN);
412         break;
413       case G_TYPE_ENUM:
414       {
415         gint *v = g_new (gint, n_values);
416         ret = gst_control_binding_get_value_array (binding, timestamp, interval,
417             n_values, v);
418         if (ret) {
419           for (i = 0; i < n_values; i++) {
420             g_value_init (&values[i], type);
421             g_value_set_enum (&values[i], v[i]);
422           }
423         }
424         g_free (v);
425       }
426         break;
427       default:
428         GST_WARNING ("incomplete implementation for paramspec type '%s'",
429             G_PARAM_SPEC_TYPE_NAME (GST_CONTROL_BINDING_PSPEC (binding)));
430         GST_CONTROL_BINDING_PSPEC (binding) = NULL;
431         break;
432     }
433   }
434   return ret;
435 }
436
437 /**
438  * gst_control_binding_set_disabled:
439  * @binding: the control binding
440  * @disabled: boolean that specifies whether to disable the controller
441  * or not.
442  *
443  * This function is used to disable a control binding for some time, i.e.
444  * gst_object_sync_values() will do nothing.
445  */
446 void
447 gst_control_binding_set_disabled (GstControlBinding * binding,
448     gboolean disabled)
449 {
450   g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
451   binding->disabled = disabled;
452 }
453
454 /**
455  * gst_control_binding_is_disabled:
456  * @binding: the control binding
457  *
458  * Check if the control binding is disabled.
459  *
460  * Returns: %TRUE if the binding is inactive
461  */
462 gboolean
463 gst_control_binding_is_disabled (GstControlBinding * binding)
464 {
465   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
466   return (binding->disabled == TRUE);
467 }