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