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