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