introspection: assorted introspection and documentation fixes
[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 * binding)
107 {
108 }
109
110 static GObject *
111 gst_control_binding_constructor (GType type, guint n_construct_params,
112     GObjectConstructParam * construct_params)
113 {
114   GstControlBinding *binding;
115   GParamSpec *pspec;
116
117   binding =
118       GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
119       ->constructor (type, n_construct_params, construct_params));
120
121   GST_INFO_OBJECT (binding->object, "trying to put property '%s' under control",
122       binding->name);
123
124   /* check if the object has a property of that name */
125   if ((pspec =
126           g_object_class_find_property (G_OBJECT_GET_CLASS (binding->object),
127               binding->name))) {
128     GST_DEBUG_OBJECT (binding->object, "  psec->flags : 0x%08x", pspec->flags);
129
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;
135     }
136   } else {
137     GST_WARNING_OBJECT (binding->object, "class '%s' has no property '%s'",
138         G_OBJECT_TYPE_NAME (binding->object), binding->name);
139   }
140   return (GObject *) binding;
141 }
142
143 static void
144 gst_control_binding_dispose (GObject * object)
145 {
146   GstControlBinding *self = GST_CONTROL_BINDING (object);
147
148   /* we did not took a reference */
149   g_object_remove_weak_pointer ((GObject *) self->object,
150       (gpointer *) & self->object);
151   self->object = NULL;
152
153   ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
154 }
155
156 static void
157 gst_control_binding_finalize (GObject * object)
158 {
159   GstControlBinding *self = GST_CONTROL_BINDING (object);
160
161   g_free (self->name);
162
163   ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
164 }
165
166 static void
167 gst_control_binding_set_property (GObject * object, guint prop_id,
168     const GValue * value, GParamSpec * pspec)
169 {
170   GstControlBinding *self = GST_CONTROL_BINDING (object);
171
172   switch (prop_id) {
173     case PROP_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);
178       break;
179     case PROP_NAME:
180       self->name = g_value_dup_string (value);
181       break;
182     default:
183       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
184       break;
185   }
186 }
187
188 static void
189 gst_control_binding_get_property (GObject * object, guint prop_id,
190     GValue * value, GParamSpec * pspec)
191 {
192   GstControlBinding *self = GST_CONTROL_BINDING (object);
193
194   switch (prop_id) {
195     case PROP_OBJECT:
196       g_value_set_object (value, self->object);
197       break;
198     case PROP_NAME:
199       g_value_set_string (value, self->name);
200       break;
201     default:
202       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203       break;
204   }
205 }
206
207 /* functions */
208
209 /**
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
215  *
216  * Sets the property of the @object, according to the #GstControlSources that
217  * handle them and for the given timestamp.
218  *
219  * If this function fails, it is most likely the application developers fault.
220  * Most probably the control sources are not setup correctly.
221  *
222  * Returns: %TRUE if the controller value could be applied to the object
223  * property, %FALSE otherwise
224  */
225 gboolean
226 gst_control_binding_sync_values (GstControlBinding * binding,
227     GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
228 {
229   GstControlBindingClass *klass;
230   gboolean ret = FALSE;
231
232   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
233
234   if (binding->disabled)
235     return TRUE;
236
237   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
238
239   if (G_LIKELY (klass->sync_values != NULL)) {
240     ret = klass->sync_values (binding, object, timestamp, last_sync);
241   } else {
242     GST_WARNING_OBJECT (binding, "missing sync_values implementation");
243   }
244   return ret;
245 }
246
247 /**
248  * gst_control_binding_get_value:
249  * @binding: the control binding
250  * @timestamp: the time the control-change should be read from
251  *
252  * Gets the value for the given controlled property at the requested time.
253  *
254  * Returns: the GValue of the property at the given time, or %NULL if the
255  * property isn't controlled.
256  */
257 GValue *
258 gst_control_binding_get_value (GstControlBinding * binding,
259     GstClockTime timestamp)
260 {
261   GstControlBindingClass *klass;
262   GValue *ret = NULL;
263
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);
266
267   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
268
269   if (G_LIKELY (klass->get_value != NULL)) {
270     ret = klass->get_value (binding, timestamp);
271   } else {
272     GST_WARNING_OBJECT (binding, "missing get_value implementation");
273   }
274   return ret;
275 }
276
277 /**
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
284  *
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.
288  *
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.
291  *
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.
295  *
296  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
297  */
298 gboolean
299 gst_control_binding_get_value_array (GstControlBinding * binding,
300     GstClockTime timestamp, GstClockTime interval, guint n_values,
301     gpointer values)
302 {
303   GstControlBindingClass *klass;
304   gboolean ret = FALSE;
305
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);
310
311   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
312
313   if (G_LIKELY (klass->get_value_array != NULL)) {
314     ret =
315         klass->get_value_array (binding, timestamp, interval, n_values, values);
316   } else {
317     GST_WARNING_OBJECT (binding, "missing get_value_array implementation");
318   }
319   return ret;
320 }
321
322 #define CONVERT_ARRAY(type,TYPE) \
323 { \
324   g##type *v = g_new (g##type,n_values); \
325   ret = gst_control_binding_get_value_array (binding, timestamp, interval, \
326       n_values, v); \
327   if (ret) { \
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]); \
331     } \
332   } \
333   g_free (v); \
334 }
335
336 /**
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
343  *
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
346  * #GValue.
347  *
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.
350  *
351  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
352  */
353 gboolean
354 gst_control_binding_get_g_value_array (GstControlBinding * binding,
355     GstClockTime timestamp, GstClockTime interval, guint n_values,
356     GValue * values)
357 {
358   GstControlBindingClass *klass;
359   gboolean ret = FALSE;
360
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);
365
366   klass = GST_CONTROL_BINDING_GET_CLASS (binding);
367
368   if (G_LIKELY (klass->get_g_value_array != NULL)) {
369     ret =
370         klass->get_g_value_array (binding, timestamp, interval, n_values,
371         values);
372   } else {
373     guint i;
374     GType type, base;
375
376     base = type = G_PARAM_SPEC_VALUE_TYPE (GST_CONTROL_BINDING_PSPEC (binding));
377     while ((type = g_type_parent (type)))
378       base = type;
379
380     GST_INFO_OBJECT (binding, "missing get_g_value_array implementation, we're "
381         "emulating it");
382     switch (base) {
383       case G_TYPE_INT:
384         CONVERT_ARRAY (int, INT);
385         break;
386       case G_TYPE_UINT:
387         CONVERT_ARRAY (uint, UINT);
388         break;
389       case G_TYPE_LONG:
390         CONVERT_ARRAY (long, LONG);
391         break;
392       case G_TYPE_ULONG:
393         CONVERT_ARRAY (ulong, ULONG);
394         break;
395       case G_TYPE_INT64:
396         CONVERT_ARRAY (int64, INT64);
397         break;
398       case G_TYPE_UINT64:
399         CONVERT_ARRAY (uint64, UINT64);
400         break;
401       case G_TYPE_FLOAT:
402         CONVERT_ARRAY (float, FLOAT);
403         break;
404       case G_TYPE_DOUBLE:
405         CONVERT_ARRAY (double, DOUBLE);
406         break;
407       case G_TYPE_BOOLEAN:
408         CONVERT_ARRAY (boolean, BOOLEAN);
409         break;
410       case G_TYPE_ENUM:
411       {
412         gint *v = g_new (gint, n_values);
413         ret = gst_control_binding_get_value_array (binding, timestamp, interval,
414             n_values, v);
415         if (ret) {
416           for (i = 0; i < n_values; i++) {
417             g_value_init (&values[i], type);
418             g_value_set_enum (&values[i], v[i]);
419           }
420         }
421         g_free (v);
422       }
423         break;
424       default:
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;
428         break;
429     }
430   }
431   return ret;
432 }
433
434 /**
435  * gst_control_binding_set_disabled:
436  * @binding: the control binding
437  * @disabled: boolean that specifies whether to disable the controller
438  * or not.
439  *
440  * This function is used to disable a control binding for some time, i.e.
441  * gst_object_sync_values() will do nothing.
442  */
443 void
444 gst_control_binding_set_disabled (GstControlBinding * binding,
445     gboolean disabled)
446 {
447   g_return_if_fail (GST_IS_CONTROL_BINDING (binding));
448   binding->disabled = disabled;
449 }
450
451 /**
452  * gst_control_binding_is_disabled:
453  * @binding: the control binding
454  *
455  * Check if the control binding is disabled.
456  *
457  * Returns: %TRUE if the binding is inactive
458  */
459 gboolean
460 gst_control_binding_is_disabled (GstControlBinding * binding)
461 {
462   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), TRUE);
463   return (binding->disabled == TRUE);
464 }