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