controller: fix gpointer vs. gpointer* mess up
[platform/upstream/gstreamer.git] / gst / gstobject.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstobject.c: Fundamental class used for all of GStreamer
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstobject
26  * @short_description: Base class for the GStreamer object hierarchy
27  *
28  * #GstObject provides a root for the object hierarchy tree filed in by the
29  * GStreamer library.  It is currently a thin wrapper on top of
30  * #GObject. It is an abstract class that is not very usable on its own.
31  *
32  * #GstObject gives us basic refcounting, parenting functionality and locking.
33  * Most of the function are just extended for special GStreamer needs and can be
34  * found under the same name in the base class of #GstObject which is #GObject
35  * (e.g. g_object_ref() becomes gst_object_ref()).
36  *
37  * The most interesting difference between #GstObject and #GObject is the
38  * "floating" reference count. A #GObject is created with a reference count of
39  * 1, owned by the creator of the #GObject. (The owner of a reference is the
40  * code section that has the right to call gst_object_unref() in order to
41  * remove that reference.) A #GstObject is created with a reference count of 1
42  * also, but it isn't owned by anyone; Instead, the initial reference count
43  * of a #GstObject is "floating". The floating reference can be removed by
44  * anyone at any time, by calling gst_object_sink().  gst_object_sink() does
45  * nothing if an object is already sunk (has no floating reference).
46  *
47  * When you add a #GstElement to its parent container, the parent container will
48  * do this:
49  * <informalexample>
50  * <programlisting>
51  *   gst_object_ref (GST_OBJECT (child_element));
52  *   gst_object_sink (GST_OBJECT (child_element));
53  * </programlisting>
54  * </informalexample>
55  * This means that the container now owns a reference to the child element
56  * (since it called gst_object_ref()), and the child element has no floating
57  * reference.
58  *
59  * The purpose of the floating reference is to keep the child element alive
60  * until you add it to a parent container, which then manages the lifetime of
61  * the object itself:
62  * <informalexample>
63  * <programlisting>
64  *    element = gst_element_factory_make (factoryname, name);
65  *    // element has one floating reference to keep it alive
66  *    gst_bin_add (GST_BIN (bin), element);
67  *    // element has one non-floating reference owned by the container
68  * </programlisting>
69  * </informalexample>
70  *
71  * Another effect of this is, that calling gst_object_unref() on a bin object,
72  * will also destoy all the #GstElement objects in it. The same is true for
73  * calling gst_bin_remove().
74  *
75  * Special care has to be taken for all methods that gst_object_sink() an object
76  * since if the caller of those functions had a floating reference to the object,
77  * the object reference is now invalid.
78  *
79  * In contrast to #GObject instances, #GstObject adds a name property. The functions
80  * gst_object_set_name() and gst_object_get_name() are used to set/get the name
81  * of the object.
82  *
83  * <refsect2>
84  * <title>controlled properties</title>
85  * <para>
86  * Controlled properties offers a lightweight way to adjust gobject
87  * properties over stream-time. It works by using time-stamped value pairs that
88  * are queued for element-properties. At run-time the elements continously pull
89  * values changes for the current stream-time.
90  *
91  * What needs to be changed in a #GstElement?
92  * Very little - it is just two steps to make a plugin controllable!
93  * <orderedlist>
94  *   <listitem><para>
95  *     mark gobject-properties paramspecs that make sense to be controlled,
96  *     by GST_PARAM_CONTROLLABLE.
97  *   </para></listitem>
98  *   <listitem><para>
99  *     when processing data (get, chain, loop function) at the beginning call
100  *     gst_object_sync_values(element,timestamp).
101  *     This will made the controller to update all gobject properties that are under
102  *     control with the current values based on timestamp.
103  *   </para></listitem>
104  * </orderedlist>
105  *
106  * What needs to be done in applications?
107  * Again its not a lot to change.
108  * <orderedlist>
109  *   <listitem><para>
110  *     first put some properties under control, by calling
111  *     gst_object_control_properties (object, "prop1", "prop2",...);
112  *   </para></listitem>
113  *   <listitem><para>
114  *     create a #GstControlSource.
115  *     csource = gst_interpolation_control_source_new ();
116  *     gst_interpolation_control_source_set_interpolation_mode(csource, mode);
117  *   </para></listitem>
118  *   <listitem><para>
119  *     Attach the #GstControlSource on the controller to a property.
120  *     gst_object_set_control_source (object, "prop1", csource);
121  *   </para></listitem>
122  *   <listitem><para>
123  *     Set the control values
124  *     gst_interpolation_control_source_set (csource,0 * GST_SECOND, value1);
125  *     gst_interpolation_control_source_set (csource,1 * GST_SECOND, value2);
126  *   </para></listitem>
127  *   <listitem><para>
128  *     start your pipeline
129  *   </para></listitem>
130  * </orderedlist>
131  * </para>
132  * </refsect2>
133  *
134  * Last reviewed on 2005-11-09 (0.9.4)
135  */
136
137 #include "gst_private.h"
138 #include "glib-compat-private.h"
139
140 #include "gstobject.h"
141 #include "gstmarshal.h"
142 #include "gstclock.h"
143 #include "gstcontrolsource.h"
144 #include "gstinfo.h"
145 #include "gstparamspecs.h"
146 #include "gstutils.h"
147
148 #ifndef GST_DISABLE_TRACE
149 #include "gsttrace.h"
150 static GstAllocTrace *_gst_object_trace;
151 #endif
152
153 #define DEBUG_REFCOUNT
154
155 /* Object signals and args */
156 enum
157 {
158   DEEP_NOTIFY,
159   LAST_SIGNAL
160 };
161
162 enum
163 {
164   PROP_0,
165   PROP_NAME,
166   PROP_PARENT,
167   PROP_LAST
168 };
169
170 enum
171 {
172   SO_OBJECT_LOADED,
173   SO_LAST_SIGNAL
174 };
175
176 /* maps type name quark => count */
177 static GData *object_name_counts = NULL;
178
179 G_LOCK_DEFINE_STATIC (object_name_mutex);
180
181 static void gst_object_set_property (GObject * object, guint prop_id,
182     const GValue * value, GParamSpec * pspec);
183 static void gst_object_get_property (GObject * object, guint prop_id,
184     GValue * value, GParamSpec * pspec);
185
186 static void gst_object_dispatch_properties_changed (GObject * object,
187     guint n_pspecs, GParamSpec ** pspecs);
188
189 static void gst_object_dispose (GObject * object);
190 static void gst_object_finalize (GObject * object);
191
192 static gboolean gst_object_set_name_default (GstObject * object);
193
194 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
195
196 static GParamSpec *properties[PROP_LAST];
197
198 /*
199  * GstControlledProperty:
200  */
201 typedef struct _GstControlledProperty
202 {
203   GParamSpec *pspec;            /* GParamSpec for this property */
204   const gchar *name;            /* name of the property */
205   GstControlSource *csource;    /* GstControlSource for this property */
206   gboolean disabled;
207   GValue last_value;
208 } GstControlledProperty;
209
210 static void gst_controlled_property_free (GstControlledProperty * prop);
211
212
213 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
214
215 static void
216 gst_object_class_init (GstObjectClass * klass)
217 {
218   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
219
220 #ifndef GST_DISABLE_TRACE
221   _gst_object_trace = gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT));
222 #endif
223
224   gobject_class->set_property = gst_object_set_property;
225   gobject_class->get_property = gst_object_get_property;
226
227   properties[PROP_NAME] =
228       g_param_spec_string ("name", "Name", "The name of the object", NULL,
229       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
230   g_object_class_install_property (gobject_class, PROP_NAME,
231       properties[PROP_NAME]);
232
233   properties[PROP_PARENT] =
234       g_param_spec_object ("parent", "Parent", "The parent of the object",
235       GST_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
236   g_object_class_install_property (gobject_class, PROP_PARENT,
237       properties[PROP_PARENT]);
238
239   /**
240    * GstObject::deep-notify:
241    * @gstobject: a #GstObject
242    * @prop_object: the object that originated the signal
243    * @prop: the property that changed
244    *
245    * The deep notify signal is used to be notified of property changes. It is
246    * typically attached to the toplevel bin to receive notifications from all
247    * the elements contained in that bin.
248    */
249   gst_object_signals[DEEP_NOTIFY] =
250       g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
251       G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
252       G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
253       NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
254       G_TYPE_PARAM);
255
256   klass->path_string_separator = "/";
257
258   /* see the comments at gst_object_dispatch_properties_changed */
259   gobject_class->dispatch_properties_changed
260       = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
261
262   gobject_class->dispose = gst_object_dispose;
263   gobject_class->finalize = gst_object_finalize;
264 }
265
266 static void
267 gst_object_init (GstObject * object)
268 {
269   object->lock = g_mutex_new ();
270   object->parent = NULL;
271   object->name = NULL;
272   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
273
274 #ifndef GST_DISABLE_TRACE
275   gst_alloc_trace_new (_gst_object_trace, object);
276 #endif
277
278   object->flags = 0;
279
280   object->control_rate = 100 * GST_MSECOND;
281   object->last_sync = GST_CLOCK_TIME_NONE;
282 }
283
284 /**
285  * gst_object_ref:
286  * @object: a #GstObject to reference
287  *
288  * Increments the reference count on @object. This function
289  * does not take the lock on @object because it relies on
290  * atomic refcounting.
291  *
292  * This object returns the input parameter to ease writing
293  * constructs like :
294  *  result = gst_object_ref (object->parent);
295  *
296  * Returns: (transfer full): A pointer to @object
297  */
298 gpointer
299 gst_object_ref (gpointer object)
300 {
301   g_return_val_if_fail (object != NULL, NULL);
302
303 #ifdef DEBUG_REFCOUNT
304   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
305       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
306 #endif
307   g_object_ref (object);
308
309   return object;
310 }
311
312 /**
313  * gst_object_unref:
314  * @object: a #GstObject to unreference
315  *
316  * Decrements the reference count on @object.  If reference count hits
317  * zero, destroy @object. This function does not take the lock
318  * on @object as it relies on atomic refcounting.
319  *
320  * The unref method should never be called with the LOCK held since
321  * this might deadlock the dispose function.
322  */
323 void
324 gst_object_unref (gpointer object)
325 {
326   g_return_if_fail (object != NULL);
327   g_return_if_fail (((GObject *) object)->ref_count > 0);
328
329 #ifdef DEBUG_REFCOUNT
330   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
331       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
332 #endif
333   g_object_unref (object);
334 }
335
336 /**
337  * gst_object_ref_sink: (skip)
338  * @object: a #GstObject to sink
339  *
340  * Increase the reference count of @object, and possibly remove the floating
341  * reference, if @object has a floating reference.
342  *
343  * In other words, if the object is floating, then this call "assumes ownership"
344  * of the floating reference, converting it to a normal reference by clearing
345  * the floating flag while leaving the reference count unchanged. If the object
346  * is not floating, then this call adds a new normal reference increasing the
347  * reference count by one.
348  */
349 gpointer
350 gst_object_ref_sink (gpointer object)
351 {
352   g_return_val_if_fail (object != NULL, NULL);
353
354 #ifdef DEBUG_REFCOUNT
355   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
356       object, ((GObject *) object)->ref_count,
357       ((GObject *) object)->ref_count + 1);
358 #endif
359   return g_object_ref_sink (object);
360 }
361
362 /**
363  * gst_object_replace:
364  * @oldobj: (inout) (transfer full): pointer to a place of a #GstObject to
365  *     replace
366  * @newobj: (transfer none): a new #GstObject
367  *
368  * Atomically modifies a pointer to point to a new object.
369  * The reference count of @oldobj is decreased and the reference count of
370  * @newobj is increased.
371  *
372  * Either @newobj and the value pointed to by @oldobj may be NULL.
373  *
374  * Returns: TRUE if @newobj was different from @oldobj
375  */
376 gboolean
377 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
378 {
379   GstObject *oldptr;
380
381   g_return_val_if_fail (oldobj != NULL, FALSE);
382
383 #ifdef DEBUG_REFCOUNT
384   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
385       *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
386       *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
387       newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
388       newobj ? G_OBJECT (newobj)->ref_count : 0);
389 #endif
390
391   oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
392
393   if (G_UNLIKELY (oldptr == newobj))
394     return FALSE;
395
396   if (newobj)
397     g_object_ref (newobj);
398
399   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
400               oldobj, oldptr, newobj))) {
401     oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
402     if (G_UNLIKELY (oldptr == newobj))
403       break;
404   }
405
406   if (oldptr)
407     g_object_unref (oldptr);
408
409   return oldptr != newobj;
410 }
411
412 /* dispose is called when the object has to release all links
413  * to other objects */
414 static void
415 gst_object_dispose (GObject * object)
416 {
417   GstObject *self = (GstObject *) object;
418   GstObject *parent;
419
420   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
421
422   GST_OBJECT_LOCK (object);
423   if ((parent = GST_OBJECT_PARENT (object)))
424     goto have_parent;
425   GST_OBJECT_PARENT (object) = NULL;
426   GST_OBJECT_UNLOCK (object);
427
428   if (self->properties) {
429     GList *node;
430
431     for (node = self->properties; node; node = g_list_next (node)) {
432       gst_controlled_property_free ((GstControlledProperty *) node->data);
433     }
434     g_list_free (self->properties);
435     self->properties = NULL;
436   }
437
438   ((GObjectClass *) gst_object_parent_class)->dispose (object);
439
440   return;
441
442   /* ERRORS */
443 have_parent:
444   {
445     g_critical ("\nTrying to dispose object \"%s\", but it still has a "
446         "parent \"%s\".\nYou need to let the parent manage the "
447         "object instead of unreffing the object directly.\n",
448         GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
449     GST_OBJECT_UNLOCK (object);
450     /* ref the object again to revive it in this error case */
451     gst_object_ref (object);
452     return;
453   }
454 }
455
456 /* finalize is called when the object has to free its resources */
457 static void
458 gst_object_finalize (GObject * object)
459 {
460   GstObject *gstobject = GST_OBJECT_CAST (object);
461
462   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
463
464   g_signal_handlers_destroy (object);
465
466   g_free (gstobject->name);
467   g_mutex_free (gstobject->lock);
468
469 #ifndef GST_DISABLE_TRACE
470   gst_alloc_trace_free (_gst_object_trace, object);
471 #endif
472
473   ((GObjectClass *) gst_object_parent_class)->finalize (object);
474 }
475
476 /* Changing a GObject property of a GstObject will result in "deep-notify"
477  * signals being emitted by the object itself, as well as in each parent
478  * object. This is so that an application can connect a listener to the
479  * top-level bin to catch property-change notifications for all contained
480  * elements.
481  *
482  * MT safe.
483  */
484 static void
485 gst_object_dispatch_properties_changed (GObject * object,
486     guint n_pspecs, GParamSpec ** pspecs)
487 {
488   GstObject *gst_object, *parent, *old_parent;
489   guint i;
490 #ifndef GST_DISABLE_GST_DEBUG
491   gchar *name = NULL;
492   const gchar *debug_name;
493 #endif
494
495   /* do the standard dispatching */
496   ((GObjectClass *)
497       gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
498       pspecs);
499
500   gst_object = GST_OBJECT_CAST (object);
501 #ifndef GST_DISABLE_GST_DEBUG
502   if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
503     name = gst_object_get_name (gst_object);
504     debug_name = GST_STR_NULL (name);
505   } else
506     debug_name = "";
507 #endif
508
509   /* now let the parent dispatch those, too */
510   parent = gst_object_get_parent (gst_object);
511   while (parent) {
512     for (i = 0; i < n_pspecs; i++) {
513       GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
514           "deep notification from %s (%s)", debug_name, pspecs[i]->name);
515
516       g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
517           g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
518     }
519
520     old_parent = parent;
521     parent = gst_object_get_parent (old_parent);
522     gst_object_unref (old_parent);
523   }
524 #ifndef GST_DISABLE_GST_DEBUG
525   g_free (name);
526 #endif
527 }
528
529 /**
530  * gst_object_default_deep_notify:
531  * @object: the #GObject that signalled the notify.
532  * @orig: a #GstObject that initiated the notify.
533  * @pspec: a #GParamSpec of the property.
534  * @excluded_props: (array zero-terminated=1) (element-type gchar*)
535  *     (allow-none):a set of user-specified properties to exclude or
536  *     NULL to show all changes.
537  *
538  * A default deep_notify signal callback for an object. The user data
539  * should contain a pointer to an array of strings that should be excluded
540  * from the notify. The default handler will print the new value of the property
541  * using g_print.
542  *
543  * MT safe. This function grabs and releases @object's LOCK for getting its
544  *          path string.
545  */
546 void
547 gst_object_default_deep_notify (GObject * object, GstObject * orig,
548     GParamSpec * pspec, gchar ** excluded_props)
549 {
550   GValue value = { 0, };        /* the important thing is that value.type = 0 */
551   gchar *str = NULL;
552   gchar *name = NULL;
553
554   if (pspec->flags & G_PARAM_READABLE) {
555     /* let's not print these out for excluded properties... */
556     while (excluded_props != NULL && *excluded_props != NULL) {
557       if (strcmp (pspec->name, *excluded_props) == 0)
558         return;
559       excluded_props++;
560     }
561     g_value_init (&value, pspec->value_type);
562     g_object_get_property (G_OBJECT (orig), pspec->name, &value);
563
564     /* FIXME: handle flags */
565     if (G_IS_PARAM_SPEC_ENUM (pspec)) {
566       GEnumValue *enum_value;
567       GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
568
569       enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
570
571       str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
572           enum_value->value);
573       g_type_class_unref (klass);
574     } else {
575       str = g_strdup_value_contents (&value);
576     }
577     name = gst_object_get_path_string (orig);
578     g_print ("%s: %s = %s\n", name, pspec->name, str);
579     g_free (name);
580     g_free (str);
581     g_value_unset (&value);
582   } else {
583     name = gst_object_get_path_string (orig);
584     g_warning ("Parameter %s not readable in %s.", pspec->name, name);
585     g_free (name);
586   }
587 }
588
589 static gboolean
590 gst_object_set_name_default (GstObject * object)
591 {
592   const gchar *type_name;
593   gint count;
594   gchar *name;
595   GQuark q;
596   guint i, l;
597
598   /* to ensure guaranteed uniqueness across threads, only one thread
599    * may ever assign a name */
600   G_LOCK (object_name_mutex);
601
602   if (!object_name_counts) {
603     g_datalist_init (&object_name_counts);
604   }
605
606   q = g_type_qname (G_OBJECT_TYPE (object));
607   count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
608   g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
609
610   G_UNLOCK (object_name_mutex);
611
612   /* GstFooSink -> foosink<N> */
613   type_name = g_quark_to_string (q);
614   if (strncmp (type_name, "Gst", 3) == 0)
615     type_name += 3;
616   name = g_strdup_printf ("%s%d", type_name, count);
617   l = strlen (name);
618   for (i = 0; i < l; i++)
619     name[i] = g_ascii_tolower (name[i]);
620
621   GST_OBJECT_LOCK (object);
622   if (G_UNLIKELY (object->parent != NULL))
623     goto had_parent;
624
625   g_free (object->name);
626   object->name = name;
627
628   GST_OBJECT_UNLOCK (object);
629
630   return TRUE;
631
632 had_parent:
633   {
634     g_free (name);
635     GST_WARNING ("parented objects can't be renamed");
636     GST_OBJECT_UNLOCK (object);
637     return FALSE;
638   }
639 }
640
641 /**
642  * gst_object_set_name:
643  * @object: a #GstObject
644  * @name:   new name of object
645  *
646  * Sets the name of @object, or gives @object a guaranteed unique
647  * name (if @name is NULL).
648  * This function makes a copy of the provided name, so the caller
649  * retains ownership of the name it sent.
650  *
651  * Returns: TRUE if the name could be set. Since Objects that have
652  * a parent cannot be renamed, this function returns FALSE in those
653  * cases.
654  *
655  * MT safe.  This function grabs and releases @object's LOCK.
656  */
657 gboolean
658 gst_object_set_name (GstObject * object, const gchar * name)
659 {
660   gboolean result;
661
662   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
663
664   GST_OBJECT_LOCK (object);
665
666   /* parented objects cannot be renamed */
667   if (G_UNLIKELY (object->parent != NULL))
668     goto had_parent;
669
670   if (name != NULL) {
671     g_free (object->name);
672     object->name = g_strdup (name);
673     GST_OBJECT_UNLOCK (object);
674     result = TRUE;
675   } else {
676     GST_OBJECT_UNLOCK (object);
677     result = gst_object_set_name_default (object);
678   }
679   /* FIXME-0.11: this misses a g_object_notify (object, "name"); unless called
680    * from gst_object_set_property.
681    * Ideally remove such custom setters (or make it static).
682    */
683   return result;
684
685   /* error */
686 had_parent:
687   {
688     GST_WARNING ("parented objects can't be renamed");
689     GST_OBJECT_UNLOCK (object);
690     return FALSE;
691   }
692 }
693
694 /**
695  * gst_object_get_name:
696  * @object: a #GstObject
697  *
698  * Returns a copy of the name of @object.
699  * Caller should g_free() the return value after usage.
700  * For a nameless object, this returns NULL, which you can safely g_free()
701  * as well.
702  *
703  * Free-function: g_free
704  *
705  * Returns: (transfer full): the name of @object. g_free() after usage.
706  *
707  * MT safe. This function grabs and releases @object's LOCK.
708  */
709 gchar *
710 gst_object_get_name (GstObject * object)
711 {
712   gchar *result = NULL;
713
714   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
715
716   GST_OBJECT_LOCK (object);
717   result = g_strdup (object->name);
718   GST_OBJECT_UNLOCK (object);
719
720   return result;
721 }
722
723 /**
724  * gst_object_set_parent:
725  * @object: a #GstObject
726  * @parent: new parent of object
727  *
728  * Sets the parent of @object to @parent. The object's reference count will
729  * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
730  *
731  * Returns: TRUE if @parent could be set or FALSE when @object
732  * already had a parent or @object and @parent are the same.
733  *
734  * MT safe. Grabs and releases @object's LOCK.
735  */
736 gboolean
737 gst_object_set_parent (GstObject * object, GstObject * parent)
738 {
739   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
740   g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
741   g_return_val_if_fail (object != parent, FALSE);
742
743   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
744       "set parent (ref and sink)");
745
746   GST_OBJECT_LOCK (object);
747   if (G_UNLIKELY (object->parent != NULL))
748     goto had_parent;
749
750   object->parent = parent;
751   gst_object_ref_sink (object);
752   GST_OBJECT_UNLOCK (object);
753
754   /* FIXME, this does not work, the deep notify takes the lock from the parent
755    * object and deadlocks when the parent holds its lock when calling this
756    * function (like _element_add_pad()) */
757   /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
758
759   return TRUE;
760
761   /* ERROR handling */
762 had_parent:
763   {
764     GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
765         "set parent failed, object already had a parent");
766     GST_OBJECT_UNLOCK (object);
767     return FALSE;
768   }
769 }
770
771 /**
772  * gst_object_get_parent:
773  * @object: a #GstObject
774  *
775  * Returns the parent of @object. This function increases the refcount
776  * of the parent object so you should gst_object_unref() it after usage.
777  *
778  * Returns: (transfer full): parent of @object, this can be NULL if @object
779  *   has no parent. unref after usage.
780  *
781  * MT safe. Grabs and releases @object's LOCK.
782  */
783 GstObject *
784 gst_object_get_parent (GstObject * object)
785 {
786   GstObject *result = NULL;
787
788   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
789
790   GST_OBJECT_LOCK (object);
791   result = object->parent;
792   if (G_LIKELY (result))
793     gst_object_ref (result);
794   GST_OBJECT_UNLOCK (object);
795
796   return result;
797 }
798
799 /**
800  * gst_object_unparent:
801  * @object: a #GstObject to unparent
802  *
803  * Clear the parent of @object, removing the associated reference.
804  * This function decreases the refcount of @object.
805  *
806  * MT safe. Grabs and releases @object's lock.
807  */
808 void
809 gst_object_unparent (GstObject * object)
810 {
811   GstObject *parent;
812
813   g_return_if_fail (GST_IS_OBJECT (object));
814
815   GST_OBJECT_LOCK (object);
816   parent = object->parent;
817
818   if (G_LIKELY (parent != NULL)) {
819     GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
820     object->parent = NULL;
821     GST_OBJECT_UNLOCK (object);
822
823     /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
824
825     gst_object_unref (object);
826   } else {
827     GST_OBJECT_UNLOCK (object);
828   }
829 }
830
831 /**
832  * gst_object_has_ancestor:
833  * @object: a #GstObject to check
834  * @ancestor: a #GstObject to check as ancestor
835  *
836  * Check if @object has an ancestor @ancestor somewhere up in
837  * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
838  *
839  * Returns: TRUE if @ancestor is an ancestor of @object.
840  *
841  * MT safe. Grabs and releases @object's locks.
842  */
843 gboolean
844 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
845 {
846   GstObject *parent, *tmp;
847
848   if (!ancestor || !object)
849     return FALSE;
850
851   parent = gst_object_ref (object);
852   do {
853     if (parent == ancestor) {
854       gst_object_unref (parent);
855       return TRUE;
856     }
857
858     tmp = gst_object_get_parent (parent);
859     gst_object_unref (parent);
860     parent = tmp;
861   } while (parent);
862
863   return FALSE;
864 }
865
866 /**
867  * gst_object_check_uniqueness:
868  * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
869  *      check through
870  * @name: the name to search for
871  *
872  * Checks to see if there is any object named @name in @list. This function
873  * does not do any locking of any kind. You might want to protect the
874  * provided list with the lock of the owner of the list. This function
875  * will lock each #GstObject in the list to compare the name, so be
876  * carefull when passing a list with a locked object.
877  *
878  * Returns: TRUE if a #GstObject named @name does not appear in @list,
879  * FALSE if it does.
880  *
881  * MT safe. Grabs and releases the LOCK of each object in the list.
882  */
883 gboolean
884 gst_object_check_uniqueness (GList * list, const gchar * name)
885 {
886   gboolean result = TRUE;
887
888   g_return_val_if_fail (name != NULL, FALSE);
889
890   for (; list; list = g_list_next (list)) {
891     GstObject *child;
892     gboolean eq;
893
894     child = GST_OBJECT_CAST (list->data);
895
896     GST_OBJECT_LOCK (child);
897     eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
898     GST_OBJECT_UNLOCK (child);
899
900     if (G_UNLIKELY (eq)) {
901       result = FALSE;
902       break;
903     }
904   }
905   return result;
906 }
907
908
909 static void
910 gst_object_set_property (GObject * object, guint prop_id,
911     const GValue * value, GParamSpec * pspec)
912 {
913   GstObject *gstobject;
914
915   gstobject = GST_OBJECT_CAST (object);
916
917   switch (prop_id) {
918     case PROP_NAME:
919       gst_object_set_name (gstobject, g_value_get_string (value));
920       break;
921     case PROP_PARENT:
922       gst_object_set_parent (gstobject, g_value_get_object (value));
923       break;
924     default:
925       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
926       break;
927   }
928 }
929
930 static void
931 gst_object_get_property (GObject * object, guint prop_id,
932     GValue * value, GParamSpec * pspec)
933 {
934   GstObject *gstobject;
935
936   gstobject = GST_OBJECT_CAST (object);
937
938   switch (prop_id) {
939     case PROP_NAME:
940       g_value_take_string (value, gst_object_get_name (gstobject));
941       break;
942     case PROP_PARENT:
943       g_value_take_object (value, gst_object_get_parent (gstobject));
944       break;
945     default:
946       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
947       break;
948   }
949 }
950
951 /**
952  * gst_object_get_path_string:
953  * @object: a #GstObject
954  *
955  * Generates a string describing the path of @object in
956  * the object hierarchy. Only useful (or used) for debugging.
957  *
958  * Free-function: g_free
959  *
960  * Returns: (transfer full): a string describing the path of @object. You must
961  *          g_free() the string after usage.
962  *
963  * MT safe. Grabs and releases the #GstObject's LOCK for all objects
964  *          in the hierarchy.
965  */
966 gchar *
967 gst_object_get_path_string (GstObject * object)
968 {
969   GSList *parentage;
970   GSList *parents;
971   void *parent;
972   gchar *prevpath, *path;
973   const gchar *typename;
974   gchar *component;
975   const gchar *separator;
976
977   /* ref object before adding to list */
978   gst_object_ref (object);
979   parentage = g_slist_prepend (NULL, object);
980
981   path = g_strdup ("");
982
983   /* first walk the object hierarchy to build a list of the parents,
984    * be carefull here with refcounting. */
985   do {
986     if (GST_IS_OBJECT (object)) {
987       parent = gst_object_get_parent (object);
988       /* add parents to list, refcount remains increased while
989        * we handle the object */
990       if (parent)
991         parentage = g_slist_prepend (parentage, parent);
992     } else {
993       break;
994     }
995     object = parent;
996   } while (object != NULL);
997
998   /* then walk the parent list and print them out. we need to
999    * decrease the refcounting on each element after we handled
1000    * it. */
1001   for (parents = parentage; parents; parents = g_slist_next (parents)) {
1002     if (G_IS_OBJECT (parents->data)) {
1003       typename = G_OBJECT_TYPE_NAME (parents->data);
1004     } else {
1005       typename = NULL;
1006     }
1007     if (GST_IS_OBJECT (parents->data)) {
1008       GstObject *item = GST_OBJECT_CAST (parents->data);
1009       GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1010       gchar *objname = gst_object_get_name (item);
1011
1012       component = g_strdup_printf ("%s:%s", typename, objname);
1013       separator = oclass->path_string_separator;
1014       /* and unref now */
1015       gst_object_unref (item);
1016       g_free (objname);
1017     } else {
1018       if (typename) {
1019         component = g_strdup_printf ("%s:%p", typename, parents->data);
1020       } else {
1021         component = g_strdup_printf ("%p", parents->data);
1022       }
1023       separator = "/";
1024     }
1025
1026     prevpath = path;
1027     path = g_strjoin (separator, prevpath, component, NULL);
1028     g_free (prevpath);
1029     g_free (component);
1030   }
1031
1032   g_slist_free (parentage);
1033
1034   return path;
1035 }
1036
1037 /* controller helper functions */
1038
1039 /*
1040  * gst_controlled_property_new:
1041  * @object: for which object the controlled property should be set up
1042  * @name: the name of the property to be controlled
1043  *
1044  * Private method which initializes the fields of a new controlled property
1045  * structure.
1046  *
1047  * Returns: a freshly allocated structure or %NULL
1048  */
1049 static GstControlledProperty *
1050 gst_controlled_property_new (GstObject * object, const gchar * name)
1051 {
1052   GstControlledProperty *prop = NULL;
1053   GParamSpec *pspec;
1054
1055   GST_INFO ("trying to put property '%s' under control", name);
1056
1057   /* check if the object has a property of that name */
1058   if ((pspec =
1059           g_object_class_find_property (G_OBJECT_GET_CLASS (object), name))) {
1060     GST_DEBUG ("  psec->flags : 0x%08x", pspec->flags);
1061
1062     /* check if this param is witable && controlable && !construct-only */
1063     g_return_val_if_fail ((pspec->flags & (G_PARAM_WRITABLE |
1064                 GST_PARAM_CONTROLLABLE | G_PARAM_CONSTRUCT_ONLY)) ==
1065         (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE), NULL);
1066
1067     if ((prop = g_slice_new (GstControlledProperty))) {
1068       prop->pspec = pspec;
1069       prop->name = pspec->name;
1070       prop->csource = NULL;
1071       prop->disabled = FALSE;
1072       memset (&prop->last_value, 0, sizeof (GValue));
1073       g_value_init (&prop->last_value, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1074     }
1075   } else {
1076     GST_WARNING ("class '%s' has no property '%s'", G_OBJECT_TYPE_NAME (object),
1077         name);
1078   }
1079   return prop;
1080 }
1081
1082 /*
1083  * gst_controlled_property_free:
1084  * @prop: the object to free
1085  *
1086  * Private method which frees all data allocated by a #GstControlledProperty
1087  * instance.
1088  */
1089 static void
1090 gst_controlled_property_free (GstControlledProperty * prop)
1091 {
1092   if (prop->csource)
1093     g_object_unref (prop->csource);
1094   g_value_unset (&prop->last_value);
1095   g_slice_free (GstControlledProperty, prop);
1096 }
1097
1098 /*
1099  * gst_object_find_controlled_property:
1100  * @self: the gobject to search for a property in
1101  * @name: the gobject property name to look for
1102  *
1103  * Searches the list of properties under control.
1104  *
1105  * Returns: a #GstControlledProperty or %NULL if the property is not being
1106  * controlled.
1107  */
1108 static GstControlledProperty *
1109 gst_object_find_controlled_property (GstObject * self, const gchar * name)
1110 {
1111   GstControlledProperty *prop;
1112   GList *node;
1113
1114   for (node = self->properties; node; node = g_list_next (node)) {
1115     prop = node->data;
1116     /* FIXME: eventually use GQuark to speed it up */
1117     if (!strcmp (prop->name, name)) {
1118       return prop;
1119     }
1120   }
1121   GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1122
1123   return NULL;
1124 }
1125
1126 /* controller functions */
1127
1128 /**
1129  * gst_object_suggest_next_sync:
1130  * @object: the object that has controlled properties
1131  *
1132  * Returns a suggestion for timestamps where buffers should be split
1133  * to get best controller results.
1134  *
1135  * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1136  * if no control-rate was set.
1137  */
1138 GstClockTime
1139 gst_object_suggest_next_sync (GstObject * object)
1140 {
1141   GstClockTime ret;
1142
1143   g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1144   g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1145       GST_CLOCK_TIME_NONE);
1146
1147   GST_OBJECT_LOCK (object);
1148
1149   /* TODO: Implement more logic, depending on interpolation mode and control
1150    * points
1151    * FIXME: we need playback direction
1152    */
1153   ret = object->last_sync + object->control_rate;
1154
1155   GST_OBJECT_UNLOCK (object);
1156
1157   return ret;
1158 }
1159
1160 /**
1161  * gst_object_sync_values:
1162  * @object: the object that has controlled properties
1163  * @timestamp: the time that should be processed
1164  *
1165  * Sets the properties of the object, according to the #GstControlSources that
1166  * (maybe) handle them and for the given timestamp.
1167  *
1168  * If this function fails, it is most likely the application developers fault.
1169  * Most probably the control sources are not setup correctly.
1170  *
1171  * Returns: %TRUE if the controller values could be applied to the object
1172  * properties, %FALSE otherwise
1173  */
1174 gboolean
1175 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1176 {
1177   GstControlledProperty *prop;
1178   GList *node;
1179   gboolean ret = TRUE, val_ret;
1180   GValue value = { 0, };
1181
1182   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1183   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1184
1185   GST_LOG_OBJECT (object, "sync_values");
1186
1187   /* FIXME: this deadlocks */
1188   /* GST_OBJECT_LOCK (object); */
1189   g_object_freeze_notify ((GObject *) object);
1190   /* go over the controlled properties of the controller */
1191   for (node = object->properties; node; node = g_list_next (node)) {
1192     prop = node->data;
1193
1194     if (prop->disabled)
1195       continue;
1196
1197     GST_LOG_OBJECT (object, "property '%s' at ts=%" G_GUINT64_FORMAT,
1198         prop->name, timestamp);
1199
1200     /* we can make this faster
1201      * http://bugzilla.gnome.org/show_bug.cgi?id=536939
1202      */
1203     g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1204     val_ret = gst_control_source_get_value (prop->csource, timestamp, &value);
1205     if (G_LIKELY (val_ret)) {
1206       /* always set the value for first time, but then only if it changed
1207        * this should limit g_object_notify invocations.
1208        * FIXME: can we detect negative playback rates?
1209        */
1210       if ((timestamp < object->last_sync) ||
1211           gst_value_compare (&value, &prop->last_value) != GST_VALUE_EQUAL) {
1212         g_object_set_property ((GObject *) object, prop->name, &value);
1213         g_value_copy (&value, &prop->last_value);
1214       }
1215     } else {
1216       GST_DEBUG_OBJECT (object, "no control value for param %s", prop->name);
1217     }
1218     g_value_unset (&value);
1219     ret &= val_ret;
1220   }
1221   object->last_sync = timestamp;
1222   g_object_thaw_notify ((GObject *) object);
1223   /* GST_OBJECT_UNLOCK (object); */
1224
1225   return ret;
1226 }
1227
1228 /**
1229  * gst_object_has_active_controlled_properties:
1230  * @object: the object that has controlled properties
1231  *
1232  * Check if the @object has an active controlled properties.
1233  *
1234  * Returns: %TRUE if the object has active controlled properties
1235  */
1236 gboolean
1237 gst_object_has_active_controlled_properties (GstObject * object)
1238 {
1239   gboolean res = FALSE;
1240   GList *node;
1241   GstControlledProperty *prop;
1242
1243   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1244
1245   GST_OBJECT_LOCK (object);
1246   for (node = object->properties; node; node = node->next) {
1247     prop = node->data;
1248     res |= !prop->disabled;
1249   }
1250   GST_OBJECT_UNLOCK (object);
1251   return res;
1252 }
1253
1254 /**
1255  * gst_object_set_controlled_properties_disabled:
1256  * @object: the object that has controlled properties
1257  * @disabled: boolean that specifies whether to disable the controller
1258  * or not.
1259  *
1260  * This function is used to disable all controlled properties of the @object for
1261  * some time, i.e. gst_object_sync_values() will do nothing..
1262  */
1263 void
1264 gst_object_set_controlled_properties_disabled (GstObject * object,
1265     gboolean disabled)
1266 {
1267   GList *node;
1268   GstControlledProperty *prop;
1269
1270   g_return_if_fail (GST_IS_OBJECT (object));
1271
1272   GST_OBJECT_LOCK (object);
1273   for (node = object->properties; node; node = node->next) {
1274     prop = node->data;
1275     prop->disabled = disabled;
1276   }
1277   GST_OBJECT_UNLOCK (object);
1278 }
1279
1280 /**
1281  * gst_object_set_controlled_property_disabled:
1282  * @object: the object that has controlled properties
1283  * @property_name: property to disable
1284  * @disabled: boolean that specifies whether to disable the controller
1285  * or not.
1286  *
1287  * This function is used to disable the #GstController on a property for
1288  * some time, i.e. gst_controller_sync_values() will do nothing for the
1289  * property.
1290  */
1291 void
1292 gst_object_set_controlled_property_disabled (GstObject * object,
1293     const gchar * property_name, gboolean disabled)
1294 {
1295   GstControlledProperty *prop;
1296
1297   g_return_if_fail (GST_IS_OBJECT (object));
1298   g_return_if_fail (property_name);
1299
1300   GST_OBJECT_LOCK (object);
1301   if ((prop = gst_object_find_controlled_property (object, property_name))) {
1302     prop->disabled = disabled;
1303   }
1304   GST_OBJECT_UNLOCK (object);
1305 }
1306
1307 /**
1308  * gst_object_set_control_source:
1309  * @object: the controller object
1310  * @property_name: name of the property for which the #GstControlSource should be set
1311  * @csource: the #GstControlSource that should be used for the property
1312  *
1313  * Sets the #GstControlSource for @property_name. If there already was a #GstControlSource
1314  * for this property it will be unreferenced.
1315  *
1316  * Returns: %FALSE if the given property isn't handled by the controller or the new #GstControlSource
1317  * couldn't be bound to the property, %TRUE if everything worked as expected.
1318  */
1319 gboolean
1320 gst_object_set_control_source (GstObject * object, const gchar * property_name,
1321     GstControlSource * csource)
1322 {
1323   GstControlledProperty *prop;
1324   gboolean ret = FALSE;
1325
1326   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1327   g_return_val_if_fail (property_name, FALSE);
1328   g_return_val_if_fail ((!csource || GST_IS_CONTROL_SOURCE (csource)), FALSE);
1329
1330   GST_OBJECT_LOCK (object);
1331   prop = gst_object_find_controlled_property (object, property_name);
1332   if (!prop) {
1333     if ((prop = gst_controlled_property_new (object, property_name))) {
1334       object->properties = g_list_prepend (object->properties, prop);
1335       GST_DEBUG_OBJECT (object, "controlled property %s added", property_name);
1336     }
1337   }
1338   if (prop) {
1339     GstControlSource *old = prop->csource;
1340
1341     if (csource != old) {
1342       if (csource && (ret = gst_control_source_bind (csource, prop->pspec))) {
1343         prop->csource = g_object_ref (csource);
1344       } else if (!csource) {
1345         ret = TRUE;
1346         prop->csource = NULL;
1347         object->properties = g_list_remove (object->properties, prop);
1348         //g_signal_handler_disconnect (self->object, prop->notify_handler_id);
1349         gst_controlled_property_free (prop);
1350         GST_DEBUG_OBJECT (object, "controlled property %s removed",
1351             property_name);
1352       }
1353       if (ret && old)
1354         g_object_unref (old);
1355     }
1356   }
1357   GST_OBJECT_UNLOCK (object);
1358
1359   return ret;
1360 }
1361
1362 /**
1363  * gst_object_get_control_source:
1364  * @object: the object
1365  * @property_name: name of the property for which the #GstControlSource should be get
1366  *
1367  * Gets the corresponding #GstControlSource for the property. This should be unreferenced
1368  * again after use.
1369  *
1370  * Returns: (transfer full): the #GstControlSource for @property_name or NULL if
1371  * the property is not controlled by this controller or no #GstControlSource was
1372  * assigned yet.
1373  */
1374 GstControlSource *
1375 gst_object_get_control_source (GstObject * object, const gchar * property_name)
1376 {
1377   GstControlledProperty *prop;
1378   GstControlSource *ret = NULL;
1379
1380   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1381   g_return_val_if_fail (property_name, NULL);
1382
1383   GST_OBJECT_LOCK (object);
1384   if ((prop = gst_object_find_controlled_property (object, property_name))) {
1385     ret = g_object_ref (prop->csource);
1386   }
1387   GST_OBJECT_UNLOCK (object);
1388
1389   return ret;
1390 }
1391
1392 /**
1393  * gst_object_get_value:
1394  * @object: the object that has controlled properties
1395  * @property_name: the name of the property to get
1396  * @timestamp: the time the control-change should be read from
1397  *
1398  * Gets the value for the given controllered property at the requested time.
1399  *
1400  * Returns: the GValue of the property at the given time, or %NULL if the
1401  * property isn't controlled.
1402  */
1403 GValue *
1404 gst_object_get_value (GstObject * object, const gchar * property_name,
1405     GstClockTime timestamp)
1406 {
1407   GstControlledProperty *prop;
1408   GValue *val = NULL;
1409
1410   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1411   g_return_val_if_fail (property_name, NULL);
1412   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1413
1414   GST_OBJECT_LOCK (object);
1415   if ((prop = gst_object_find_controlled_property (object, property_name))) {
1416     val = g_new0 (GValue, 1);
1417     g_value_init (val, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1418
1419     /* get current value via control source */
1420     if (!gst_control_source_get_value (prop->csource, timestamp, val)) {
1421       g_free (val);
1422       val = NULL;
1423     }
1424   }
1425   GST_OBJECT_UNLOCK (object);
1426
1427   return val;
1428 }
1429
1430 /**
1431  * gst_object_get_value_array:
1432  * @object: the object that has controlled properties
1433  * @property_name: the name of the property to get
1434  * @timestamp: the time that should be processed
1435  * @interval: the time spacing between subsequent values
1436  * @n_values: the number of values
1437  * @values: array to put control-values in
1438  *
1439  * Gets a number of values for the given controllered property starting at the
1440  * requested time. The array @values need to hold enough space for @n_values of
1441  * the same type as the objects property's type.
1442  *
1443  * This function is useful if one wants to e.g. draw a graph of the control
1444  * curve or apply a control curve sample by sample.
1445  *
1446  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1447  */
1448 gboolean
1449 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1450     GstClockTime timestamp, GstClockTime interval, guint n_values,
1451     gpointer values)
1452 {
1453   gboolean res = FALSE;
1454   GstControlledProperty *prop;
1455
1456   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1457   g_return_val_if_fail (property_name, FALSE);
1458   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1459   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1460   g_return_val_if_fail (values, FALSE);
1461
1462   GST_OBJECT_LOCK (object);
1463   if ((prop = gst_object_find_controlled_property (object, property_name))) {
1464     res = gst_control_source_get_value_array (prop->csource, timestamp,
1465         interval, n_values, values);
1466   }
1467   GST_OBJECT_UNLOCK (object);
1468   return res;
1469 }
1470
1471 /**
1472  * gst_object_get_control_rate:
1473  * @object: the object that has controlled properties
1474  *
1475  * Obtain the control-rate for this @object. Audio processing #GstElement
1476  * objects will use this rate to sub-divide their processing loop and call
1477  * gst_object_sync_values() inbetween. The length of the processing segment
1478  * should be up to @control-rate nanoseconds.
1479  *
1480  * If the @object is not under property control, this will return
1481  * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1482  *
1483  * The control-rate is not expected to change if the element is in
1484  * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1485  *
1486  * Returns: the control rate in nanoseconds
1487  */
1488 GstClockTime
1489 gst_object_get_control_rate (GstObject * object)
1490 {
1491   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1492
1493   return object->control_rate;
1494 }
1495
1496 /**
1497  * gst_object_set_control_rate:
1498  * @object: the object that has controlled properties
1499  * @control_rate: the new control-rate in nanoseconds.
1500  *
1501  * Change the control-rate for this @object. Audio processing #GstElement
1502  * objects will use this rate to sub-divide their processing loop and call
1503  * gst_object_sync_values() inbetween. The length of the processing segment
1504  * should be up to @control-rate nanoseconds.
1505  *
1506  * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1507  * %GST_STATE_PLAYING.
1508  */
1509 void
1510 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1511 {
1512   g_return_if_fail (GST_IS_OBJECT (object));
1513
1514   object->control_rate = control_rate;
1515 }