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