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 #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
137 static void
138 gst_control_binding_finalize (GObject * object)
139 {
140   GstControlBinding *self = GST_CONTROL_BINDING (object);
141
142   g_free (self->name);
143 }
144
145 static void
146 gst_control_binding_set_property (GObject * object, guint prop_id,
147     const GValue * value, GParamSpec * pspec)
148 {
149   GstControlBinding *self = GST_CONTROL_BINDING (object);
150
151   switch (prop_id) {
152     case PROP_OBJECT:
153       self->object = g_value_dup_object (value);
154       break;
155     case PROP_NAME:
156       self->name = g_value_dup_string (value);
157       break;
158     default:
159       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160       break;
161   }
162 }
163
164 static void
165 gst_control_binding_get_property (GObject * object, guint prop_id,
166     GValue * value, GParamSpec * pspec)
167 {
168   GstControlBinding *self = GST_CONTROL_BINDING (object);
169
170   switch (prop_id) {
171     case PROP_OBJECT:
172       g_value_set_object (value, self->object);
173       break;
174     case PROP_NAME:
175       g_value_set_string (value, self->name);
176       break;
177     default:
178       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179       break;
180   }
181 }
182
183 /* functions */
184
185 /**
186  * gst_control_binding_sync_values:
187  * @self: the control binding
188  * @object: the object that has controlled properties
189  * @timestamp: the time that should be processed
190  * @last_sync: the last time this was called
191  *
192  * Sets the property of the @object, according to the #GstControlSources that
193  * handle them and for the given timestamp.
194  *
195  * If this function fails, it is most likely the application developers fault.
196  * Most probably the control sources are not setup correctly.
197  *
198  * Returns: %TRUE if the controller value could be applied to the object
199  * property, %FALSE otherwise
200  */
201 gboolean
202 gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
203     GstClockTime timestamp, GstClockTime last_sync)
204 {
205   GstControlBindingClass *klass;
206   gboolean ret = FALSE;
207
208   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
209
210   if (self->disabled)
211     return TRUE;
212
213   klass = GST_CONTROL_BINDING_GET_CLASS (self);
214
215   if (G_LIKELY (klass->sync_values != NULL)) {
216     ret = klass->sync_values (self, object, timestamp, last_sync);
217   } else {
218     GST_WARNING_OBJECT (self, "missing sync_values implementation");
219   }
220   return ret;
221 }
222
223 /**
224  * gst_control_binding_get_value:
225  * @self: the control binding
226  * @timestamp: the time the control-change should be read from
227  *
228  * Gets the value for the given controlled property at the requested time.
229  *
230  * Returns: the GValue of the property at the given time, or %NULL if the
231  * property isn't controlled.
232  */
233 GValue *
234 gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
235 {
236   GstControlBindingClass *klass;
237   GValue *ret = NULL;
238
239   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
240   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
241
242   klass = GST_CONTROL_BINDING_GET_CLASS (self);
243
244   if (G_LIKELY (klass->get_value != NULL)) {
245     ret = klass->get_value (self, timestamp);
246   } else {
247     GST_WARNING_OBJECT (self, "missing get_value implementation");
248   }
249   return ret;
250 }
251
252 /**
253  * gst_control_binding_get_value_array:
254  * @self: the control binding
255  * @timestamp: the time that should be processed
256  * @interval: the time spacing between subsequent values
257  * @n_values: the number of values
258  * @values: array to put control-values in
259  *
260  * Gets a number of values for the given controllered property starting at the
261  * requested time. The array @values need to hold enough space for @n_values of
262  * the same type as the objects property's type.
263  *
264  * This function is useful if one wants to e.g. draw a graph of the control
265  * curve or apply a control curve sample by sample.
266  *
267  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
268  */
269 gboolean
270 gst_control_binding_get_value_array (GstControlBinding * self,
271     GstClockTime timestamp, GstClockTime interval, guint n_values,
272     GValue * values)
273 {
274   GstControlBindingClass *klass;
275   gboolean ret = FALSE;
276
277   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
278   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
279   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
280   g_return_val_if_fail (values, FALSE);
281
282   klass = GST_CONTROL_BINDING_GET_CLASS (self);
283
284   if (G_LIKELY (klass->get_value_array != NULL)) {
285     ret = klass->get_value_array (self, timestamp, interval, n_values, values);
286   } else {
287     GST_WARNING_OBJECT (self, "missing get_value_array implementation");
288   }
289   return ret;
290 }
291
292 /**
293  * gst_control_binding_set_disabled:
294  * @self: the control binding
295  * @disabled: boolean that specifies whether to disable the controller
296  * or not.
297  *
298  * This function is used to disable a control binding for some time, i.e.
299  * gst_object_sync_values() will do nothing.
300  */
301 void
302 gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled)
303 {
304   g_return_if_fail (GST_IS_CONTROL_BINDING (self));
305   self->disabled = disabled;
306 }
307
308 /**
309  * gst_control_binding_is_disabled:
310  * @self: the control binding
311  *
312  * Check if the control binding is disabled.
313  *
314  * Returns: %TRUE if the binding is inactive
315  */
316 gboolean
317 gst_control_binding_is_disabled (GstControlBinding * self)
318 {
319   g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), TRUE);
320   return (self->disabled == TRUE);
321 }