Merge branch 'master' into 0.11
[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
29 #include "gst_private.h"
30
31 #include <glib-object.h>
32 #include <gst/gst.h>
33
34 #include "gstcontrolbinding.h"
35
36 #include <math.h>
37
38 #define GST_CAT_DEFAULT control_binding_debug
39 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
40
41 static void gst_control_binding_dispose (GObject * object);
42 static void gst_control_binding_finalize (GObject * object);
43
44 #define _do_init \
45   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
46       "dynamic parameter control source attachment");
47
48 G_DEFINE_TYPE_WITH_CODE (GstControlBinding, gst_control_binding, G_TYPE_OBJECT,
49     _do_init);
50
51 static void
52 gst_control_binding_class_init (GstControlBindingClass * klass)
53 {
54   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
55
56   gobject_class->dispose = gst_control_binding_dispose;
57   gobject_class->finalize = gst_control_binding_finalize;
58 }
59
60 static void
61 gst_control_binding_init (GstControlBinding * self)
62 {
63 }
64
65 static void
66 gst_control_binding_dispose (GObject * object)
67 {
68   GstControlBinding *self = GST_CONTROL_BINDING (object);
69
70   if (self->csource) {
71     g_object_unref (self->csource);
72     self->csource = NULL;
73   }
74 }
75
76 static void
77 gst_control_binding_finalize (GObject * object)
78 {
79   GstControlBinding *self = GST_CONTROL_BINDING (object);
80
81   g_value_unset (&self->cur_value);
82 }
83
84 /* mapping functions */
85 #define DEFINE_CONVERT(type,Type,TYPE) \
86 static void \
87 convert_to_##type (GstControlBinding *self, gdouble s, GValue *d) \
88 { \
89   GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (self->pspec); \
90   g##type v; \
91   \
92   s = CLAMP (s, 0.0, 1.0); \
93   v = pspec->minimum + (g##type) ((pspec->maximum - pspec->minimum) * s); \
94   g_value_set_##type (d, v); \
95 }
96
97 DEFINE_CONVERT (int, Int, INT);
98 DEFINE_CONVERT (uint, UInt, UINT);
99 DEFINE_CONVERT (long, Long, LONG);
100 DEFINE_CONVERT (ulong, ULong, ULONG);
101 DEFINE_CONVERT (int64, Int64, INT64);
102 DEFINE_CONVERT (uint64, UInt64, UINT64);
103 DEFINE_CONVERT (float, Float, FLOAT);
104 DEFINE_CONVERT (double, Double, DOUBLE);
105
106 static void
107 convert_to_boolean (GstControlBinding * self, gdouble s, GValue * d)
108 {
109   s = CLAMP (s, 0.0, 1.0);
110   g_value_set_boolean (d, (gboolean) (s + 0.5));
111 }
112
113 static void
114 convert_to_enum (GstControlBinding * self, gdouble s, GValue * d)
115 {
116   GParamSpecEnum *pspec = G_PARAM_SPEC_ENUM (self->pspec);
117   GEnumClass *e = pspec->enum_class;
118   gint v;
119
120   s = CLAMP (s, 0.0, 1.0);
121   v = s * (e->n_values - 1);
122   g_value_set_enum (d, e->values[v].value);
123 }
124
125 /**
126  * gst_control_binding_new:
127  * @object: the object of the property
128  * @property_name: the property-name to attach the control source
129  * @csource: the control source
130  *
131  * Create a new control-binding that attaches the #GstControlSource to the
132  * #GObject property.
133  *
134  * Returns: the new #GstControlBinding
135  */
136 GstControlBinding *
137 gst_control_binding_new (GstObject * object, const gchar * property_name,
138     GstControlSource * csource)
139 {
140   GstControlBinding *self = NULL;
141   GParamSpec *pspec;
142
143   GST_INFO_OBJECT (object, "trying to put property '%s' under control",
144       property_name);
145
146   /* check if the object has a property of that name */
147   if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
148               property_name))) {
149     GST_DEBUG_OBJECT (object, "  psec->flags : 0x%08x", pspec->flags);
150
151     /* check if this param is witable && controlable && !construct-only */
152     g_return_val_if_fail ((pspec->flags & (G_PARAM_WRITABLE |
153                 GST_PARAM_CONTROLLABLE | G_PARAM_CONSTRUCT_ONLY)) ==
154         (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE), NULL);
155
156     if ((self = (GstControlBinding *) g_object_newv (GST_TYPE_CONTROL_BINDING,
157                 0, NULL))) {
158       GType type = G_PARAM_SPEC_VALUE_TYPE (pspec), base;
159
160       // add pspec as a construction parameter and move below to construct()
161       self->pspec = pspec;
162       self->name = pspec->name;
163       self->csource = g_object_ref (csource);
164       self->disabled = FALSE;
165
166       g_value_init (&self->cur_value, type);
167
168       base = type = G_PARAM_SPEC_VALUE_TYPE (pspec);
169       while ((type = g_type_parent (type)))
170         base = type;
171
172       GST_DEBUG_OBJECT (object, "  using type %s", g_type_name (base));
173
174       // select mapping function
175       // FIXME: only select mapping if super class hasn't set any?
176       switch (base) {
177         case G_TYPE_INT:
178           self->convert = convert_to_int;
179           break;
180         case G_TYPE_UINT:
181           self->convert = convert_to_uint;
182           break;
183         case G_TYPE_LONG:
184           self->convert = convert_to_long;
185           break;
186         case G_TYPE_ULONG:
187           self->convert = convert_to_ulong;
188           break;
189         case G_TYPE_INT64:
190           self->convert = convert_to_int64;
191           break;
192         case G_TYPE_UINT64:
193           self->convert = convert_to_uint64;
194           break;
195         case G_TYPE_FLOAT:
196           self->convert = convert_to_float;
197           break;
198         case G_TYPE_DOUBLE:
199           self->convert = convert_to_double;
200           break;
201         case G_TYPE_BOOLEAN:
202           self->convert = convert_to_boolean;
203           break;
204         case G_TYPE_ENUM:
205           self->convert = convert_to_enum;
206           break;
207         default:
208           // FIXME: return NULL?
209           GST_WARNING ("incomplete implementation for paramspec type '%s'",
210               G_PARAM_SPEC_TYPE_NAME (pspec));
211           break;
212       }
213     }
214   } else {
215     GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
216         G_OBJECT_TYPE_NAME (object), property_name);
217   }
218   return self;
219 }
220
221 /* functions */
222
223 /**
224  * gst_control_binding_sync_values:
225  * @self: the control binding
226  * @object: the object that has controlled properties
227  * @timestamp: the time that should be processed
228  * @last_sync: the last time this was called
229  *
230  * Sets the property of the @object, according to the #GstControlSources that
231  * handle them and for the given timestamp.
232  *
233  * If this function fails, it is most likely the application developers fault.
234  * Most probably the control sources are not setup correctly.
235  *
236  * Returns: %TRUE if the controller value could be applied to the object
237  * property, %FALSE otherwise
238  */
239 gboolean
240 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
241     GstClockTime timestamp, GstClockTime last_sync)
242 {
243   GValue *dst_val;
244   gdouble src_val;
245   gboolean ret;
246
247   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
248
249   if (self->disabled)
250     return TRUE;
251
252   GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
253       self->name, GST_TIME_ARGS (timestamp));
254
255   dst_val = &self->cur_value;
256   ret = gst_control_source_get_value (self->csource, timestamp, &src_val);
257   if (G_LIKELY (ret)) {
258     GST_LOG_OBJECT (object, "  new value %lf", src_val);
259     /* always set the value for first time, but then only if it changed
260      * this should limit g_object_notify invocations.
261      * FIXME: can we detect negative playback rates?
262      */
263     if ((timestamp < last_sync) || (src_val != self->last_value)) {
264       GST_LOG_OBJECT (object, "  mapping %s to value of type %s", self->name,
265           G_VALUE_TYPE_NAME (dst_val));
266       /* run mapping function to convert gdouble to GValue */
267       self->convert (self, src_val, dst_val);
268       /* we can make this faster
269        * http://bugzilla.gnome.org/show_bug.cgi?id=536939
270        */
271       g_object_set_property ((GObject *) object, self->name, dst_val);
272       self->last_value = src_val;
273     }
274   } else {
275     GST_DEBUG_OBJECT (object, "no control value for param %s", self->name);
276   }
277   return (ret);
278 }
279
280 /**
281  * gst_control_binding_get_value:
282  * @self: 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: the GValue of the property at the given time, or %NULL if the
288  * property isn't controlled.
289  */
290 GValue *
291 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
292 {
293   GValue *dst_val = NULL;
294   gdouble src_val;
295
296   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
297   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
298
299   /* get current value via control source */
300   if (gst_control_source_get_value (self->csource, timestamp, &src_val)) {
301     dst_val = g_new0 (GValue, 1);
302     g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (self->pspec));
303     self->convert (self, src_val, dst_val);
304   } else {
305     GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
306         self->name, GST_TIME_ARGS (timestamp));
307   }
308
309   return dst_val;
310 }
311
312 /**
313  * gst_control_binding_get_value_array:
314  * @self: the control binding
315  * @timestamp: the time that should be processed
316  * @interval: the time spacing between subsequent values
317  * @n_values: the number of values
318  * @values: array to put control-values in
319  *
320  * Gets a number of values for the given controllered property starting at the
321  * requested time. The array @values need to hold enough space for @n_values of
322  * the same type as the objects property's type.
323  *
324  * This function is useful if one wants to e.g. draw a graph of the control
325  * curve or apply a control curve sample by sample.
326  *
327  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
328  */
329 gboolean
330 gst_control_binding_get_value_array (GstControlBinding * self,
331     GstClockTime timestamp, GstClockTime interval, guint n_values,
332     GValue * values)
333 {
334   gint i;
335   gdouble *src_val;
336   gboolean res = FALSE;
337   GType type;
338   GstControlBindingConvert convert;
339
340   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
341   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
342   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
343   g_return_val_if_fail (values, FALSE);
344
345   convert = self->convert;
346   type = G_PARAM_SPEC_VALUE_TYPE (self->pspec);
347
348   src_val = g_new0 (gdouble, n_values);
349   if ((res = gst_control_source_get_value_array (self->csource, timestamp,
350               interval, n_values, src_val))) {
351     for (i = 0; i < n_values; i++) {
352       if (!isnan (src_val[i])) {
353         g_value_init (&values[i], type);
354         convert (self, src_val[i], &values[i]);
355       } else {
356         GST_LOG ("no control value for property %s at index %d", self->name, i);
357       }
358     }
359   } else {
360     GST_LOG ("failed to get control value for property %s at ts %"
361         GST_TIME_FORMAT, self->name, GST_TIME_ARGS (timestamp));
362   }
363   g_free (src_val);
364   return res;
365 }
366
367
368
369 /**
370  * gst_control_binding_get_control_source:
371  * @self: the control binding
372  *
373  * Get the control source.
374  *
375  * Returns: the control source. Unref when done with it.
376  */
377 GstControlSource *
378 gst_control_binding_get_control_source (GstControlBinding * self)
379 {
380   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
381   return g_object_ref (self->csource);
382 }
383
384 /**
385  * gst_control_binding_set_disabled:
386  * @self: the control binding
387  * @disabled: boolean that specifies whether to disable the controller
388  * or not.
389  *
390  * This function is used to disable a control binding for some time, i.e.
391  * gst_object_sync_values() will do nothing.
392  */
393 void
394 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
395 {
396   g_return_if_fail (GST_IS_CONTROL_BINDING (self));
397   self->disabled = disabled;
398 }
399
400 /**
401  * gst_control_binding_is_disabled:
402  * @self: the control binding
403  *
404  * Check if the control binding is disabled.
405  *
406  * Returns: %TRUE if the binding is inactive
407  */
408 gboolean
409 gst_control_binding_is_disabled (GstControlBinding * self)
410 {
411   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
412   return (self->disabled == TRUE);
413 }