Merge remote-tracking branch 'origin/0.10'
[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 #define _do_init \
42   GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
43       "dynamic parameter control source attachment");
44
45 static GObject *gst_control_binding_constructor (GType type,
46     guint n_construct_params, GObjectConstructParam * construct_params);
47 static void gst_control_binding_set_property (GObject * object, guint prop_id,
48     const GValue * value, GParamSpec * pspec);
49 static void gst_control_binding_get_property (GObject * object, guint prop_id,
50     GValue * value, GParamSpec * pspec);
51 static void gst_control_binding_dispose (GObject * object);
52 static void gst_control_binding_finalize (GObject * object);
53
54 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
55     GST_TYPE_OBJECT, _do_init);
56
57 enum
58 {
59   PROP_0,
60   PROP_OBJECT,
61   PROP_NAME,
62   PROP_LAST
63 };
64
65 static GParamSpec *properties[PROP_LAST];
66
67 static void
68 gst_control_binding_class_init (GstControlBindingClass * klass)
69 {
70   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
71
72   gobject_class->constructor = gst_control_binding_constructor;
73   gobject_class->set_property = gst_control_binding_set_property;
74   gobject_class->get_property = gst_control_binding_get_property;
75   gobject_class->dispose = gst_control_binding_dispose;
76   gobject_class->finalize = gst_control_binding_finalize;
77
78   properties[PROP_OBJECT] =
79       g_param_spec_object ("object", "Object",
80       "The object of the property", GST_TYPE_OBJECT,
81       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
82
83   properties[PROP_NAME] =
84       g_param_spec_string ("name", "Name", "The name of the property", NULL,
85       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
86
87
88   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
89 }
90
91 static void
92 gst_control_binding_init (GstControlBinding * self)
93 {
94 }
95
96 static GObject *
97 gst_control_binding_constructor (GType type, guint n_construct_params,
98     GObjectConstructParam * construct_params)
99 {
100   GstControlBinding *self;
101   GParamSpec *pspec;
102
103   self = GST_CONTROL_BINDING (G_OBJECT_CLASS (gst_control_binding_parent_class)
104       ->constructor (type, n_construct_params, construct_params));
105
106   GST_INFO_OBJECT (self->object, "trying to put property '%s' under control",
107       self->name);
108
109   /* check if the object has a property of that name */
110   if ((pspec =
111           g_object_class_find_property (G_OBJECT_GET_CLASS (self->object),
112               self->name))) {
113     GST_DEBUG_OBJECT (self->object, "  psec->flags : 0x%08x", pspec->flags);
114
115     /* check if this param is witable && controlable && !construct-only */
116     if ((pspec->flags & (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE |
117                 G_PARAM_CONSTRUCT_ONLY)) ==
118         (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE)) {
119       self->pspec = pspec;
120     }
121   } else {
122     GST_WARNING_OBJECT (self->object, "class '%s' has no property '%s'",
123         G_OBJECT_TYPE_NAME (self->object), self->name);
124   }
125   return (GObject *) self;
126 }
127
128 static void
129 gst_control_binding_dispose (GObject * object)
130 {
131   GstControlBinding *self = GST_CONTROL_BINDING (object);
132
133   if (self->object)
134     gst_object_replace (&self->object, NULL);
135
136   ((GObjectClass *) gst_control_binding_parent_class)->dispose (object);
137 }
138
139 static void
140 gst_control_binding_finalize (GObject * object)
141 {
142   GstControlBinding *self = GST_CONTROL_BINDING (object);
143
144   g_free (self->name);
145
146   ((GObjectClass *) gst_control_binding_parent_class)->finalize (object);
147 }
148
149 static void
150 gst_control_binding_set_property (GObject * object, guint prop_id,
151     const GValue * value, GParamSpec * pspec)
152 {
153   GstControlBinding *self = GST_CONTROL_BINDING (object);
154
155   switch (prop_id) {
156     case PROP_OBJECT:
157       self->object = g_value_dup_object (value);
158       break;
159     case PROP_NAME:
160       self->name = g_value_dup_string (value);
161       break;
162     default:
163       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164       break;
165   }
166 }
167
168 static void
169 gst_control_binding_get_property (GObject * object, guint prop_id,
170     GValue * value, GParamSpec * pspec)
171 {
172   GstControlBinding *self = GST_CONTROL_BINDING (object);
173
174   switch (prop_id) {
175     case PROP_OBJECT:
176       g_value_set_object (value, self->object);
177       break;
178     case PROP_NAME:
179       g_value_set_string (value, self->name);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184   }
185 }
186
187 /* functions */
188
189 /**
190  * gst_control_binding_sync_values:
191  * @self: the control binding
192  * @object: the object that has controlled properties
193  * @timestamp: the time that should be processed
194  * @last_sync: the last time this was called
195  *
196  * Sets the property of the @object, according to the #GstControlSources that
197  * handle them and for the given timestamp.
198  *
199  * If this function fails, it is most likely the application developers fault.
200  * Most probably the control sources are not setup correctly.
201  *
202  * Returns: %TRUE if the controller value could be applied to the object
203  * property, %FALSE otherwise
204  */
205 gboolean
206 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
207     GstClockTime timestamp, GstClockTime last_sync)
208 {
209   GstControlBindingClass *klass;
210   gboolean ret = FALSE;
211
212   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
213
214   if (self->disabled)
215     return TRUE;
216
217   klass = GST_CONTROL_BINDING_GET_CLASS (self);
218
219   if (G_LIKELY (klass->sync_values != NULL)) {
220     ret = klass->sync_values (self, object, timestamp, last_sync);
221   } else {
222     GST_WARNING_OBJECT (self, "missing sync_values implementation");
223   }
224   return ret;
225 }
226
227 /**
228  * gst_control_binding_get_value:
229  * @self: the control binding
230  * @timestamp: the time the control-change should be read from
231  *
232  * Gets the value for the given controlled property at the requested time.
233  *
234  * Returns: the GValue of the property at the given time, or %NULL if the
235  * property isn't controlled.
236  */
237 GValue *
238 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
239 {
240   GstControlBindingClass *klass;
241   GValue *ret = NULL;
242
243   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
244   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
245
246   klass = GST_CONTROL_BINDING_GET_CLASS (self);
247
248   if (G_LIKELY (klass->get_value != NULL)) {
249     ret = klass->get_value (self, timestamp);
250   } else {
251     GST_WARNING_OBJECT (self, "missing get_value implementation");
252   }
253   return ret;
254 }
255
256 /**
257  * gst_control_binding_get_value_array:
258  * @self: the control binding
259  * @timestamp: the time that should be processed
260  * @interval: the time spacing between subsequent values
261  * @n_values: the number of values
262  * @values: array to put control-values in
263  *
264  * Gets a number of values for the given controllered property starting at the
265  * requested time. The array @values need to hold enough space for @n_values of
266  * the same type as the objects property's type.
267  *
268  * This function is useful if one wants to e.g. draw a graph of the control
269  * curve or apply a control curve sample by sample.
270  *
271  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
272  */
273 gboolean
274 gst_control_binding_get_value_array (GstControlBinding * self,
275     GstClockTime timestamp, GstClockTime interval, guint n_values,
276     GValue * values)
277 {
278   GstControlBindingClass *klass;
279   gboolean ret = FALSE;
280
281   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
282   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
283   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
284   g_return_val_if_fail (values, FALSE);
285
286   klass = GST_CONTROL_BINDING_GET_CLASS (self);
287
288   if (G_LIKELY (klass->get_value_array != NULL)) {
289     ret = klass->get_value_array (self, timestamp, interval, n_values, values);
290   } else {
291     GST_WARNING_OBJECT (self, "missing get_value_array implementation");
292   }
293   return ret;
294 }
295
296 /**
297  * gst_control_binding_set_disabled:
298  * @self: the control binding
299  * @disabled: boolean that specifies whether to disable the controller
300  * or not.
301  *
302  * This function is used to disable a control binding for some time, i.e.
303  * gst_object_sync_values() will do nothing.
304  */
305 void
306 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
307 {
308   g_return_if_fail (GST_IS_CONTROL_BINDING (self));
309   self->disabled = disabled;
310 }
311
312 /**
313  * gst_control_binding_is_disabled:
314  * @self: the control binding
315  *
316  * Check if the control binding is disabled.
317  *
318  * Returns: %TRUE if the binding is inactive
319  */
320 gboolean
321 gst_control_binding_is_disabled (GstControlBinding * self)
322 {
323   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
324   return (self->disabled == TRUE);
325 }