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