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