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>
6 * gstobject.c: Fundamental class used for all of GStreamer
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.
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.
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.
26 * @short_description: Base class for the GStreamer object hierarchy
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.
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()).
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.
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
46 * <title>controlled properties</title>
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.
53 * What needs to be changed in a #GstElement?
54 * Very little - it is just two steps to make a plugin controllable!
57 * mark gobject-properties paramspecs that make sense to be controlled,
58 * by GST_PARAM_CONTROLLABLE.
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.
68 * What needs to be done in applications?
69 * Again it's not a lot to change.
72 * create a #GstControlSource.
73 * csource = gst_interpolation_control_source_new ();
74 * g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
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));
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);
93 #include "gst_private.h"
94 #include "glib-compat-private.h"
96 #include "gstobject.h"
98 #include "gstcontrolbinding.h"
99 #include "gstcontrolsource.h"
101 #include "gstparamspecs.h"
102 #include "gstutils.h"
104 #ifndef GST_DISABLE_TRACE
105 #include "gsttrace.h"
106 static GstAllocTrace *_gst_object_trace;
109 #define DEBUG_REFCOUNT
111 /* Object signals and args */
132 /* maps type name quark => count */
133 static GData *object_name_counts = NULL;
135 G_LOCK_DEFINE_STATIC (object_name_mutex);
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);
142 static void gst_object_dispatch_properties_changed (GObject * object,
143 guint n_pspecs, GParamSpec ** pspecs);
145 static void gst_object_dispose (GObject * object);
146 static void gst_object_finalize (GObject * object);
148 static gboolean gst_object_set_name_default (GstObject * object);
150 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
152 static GParamSpec *properties[PROP_LAST];
154 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
157 gst_object_constructed (GObject * object)
159 GST_TRACER_OBJECT_CREATED (GST_OBJECT_CAST (object));
161 ((GObjectClass *) gst_object_parent_class)->constructed (object);
165 gst_object_class_init (GstObjectClass * klass)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 #ifndef GST_DISABLE_TRACE
171 _gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT), -2);
174 gobject_class->set_property = gst_object_set_property;
175 gobject_class->get_property = gst_object_get_property;
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);
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.
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);
194 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
197 * GstObject::deep-notify:
198 * @gstobject: a #GstObject
199 * @prop_object: the object that originated the signal
200 * @prop: the property that changed
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.
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,
213 klass->path_string_separator = "/";
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);
219 gobject_class->constructed = gst_object_constructed;
220 gobject_class->dispose = gst_object_dispose;
221 gobject_class->finalize = gst_object_finalize;
225 gst_object_init (GstObject * object)
227 g_mutex_init (&object->lock);
228 object->parent = NULL;
230 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
232 #ifndef GST_DISABLE_TRACE
233 _gst_alloc_trace_new (_gst_object_trace, object);
238 object->control_rate = 100 * GST_MSECOND;
239 object->last_sync = GST_CLOCK_TIME_NONE;
244 * @object: (type Gst.Object): a #GstObject to reference
246 * Increments the reference count on @object. This function
247 * does not take the lock on @object because it relies on
248 * atomic refcounting.
250 * This object returns the input parameter to ease writing
252 * result = gst_object_ref (object->parent);
254 * Returns: (transfer full) (type Gst.Object): A pointer to @object
257 gst_object_ref (gpointer object)
259 g_return_val_if_fail (object != NULL, NULL);
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);
265 g_object_ref (object);
272 * @object: (type Gst.Object): a #GstObject to unreference
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.
278 * The unref method should never be called with the LOCK held since
279 * this might deadlock the dispose function.
282 gst_object_unref (gpointer object)
284 g_return_if_fail (object != NULL);
285 g_return_if_fail (((GObject *) object)->ref_count > 0);
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);
291 g_object_unref (object);
295 * gst_object_ref_sink: (skip)
296 * @object: a #GstObject to sink
298 * Increase the reference count of @object, and possibly remove the floating
299 * reference, if @object has a floating reference.
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.
308 gst_object_ref_sink (gpointer object)
310 g_return_val_if_fail (object != NULL, NULL);
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);
317 return g_object_ref_sink (object);
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
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.
330 * Either @newobj and the value pointed to by @oldobj may be %NULL.
332 * Returns: %TRUE if @newobj was different from @oldobj
335 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
339 g_return_val_if_fail (oldobj != NULL, FALSE);
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);
349 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
351 if (G_UNLIKELY (oldptr == newobj))
355 gst_object_ref (newobj);
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))
365 gst_object_unref (oldptr);
367 return oldptr != newobj;
370 /* dispose is called when the object has to release all links
371 * to other objects */
373 gst_object_dispose (GObject * object)
375 GstObject *self = (GstObject *) object;
378 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
380 GST_OBJECT_LOCK (object);
381 if ((parent = GST_OBJECT_PARENT (object)))
383 GST_OBJECT_PARENT (object) = NULL;
384 GST_OBJECT_UNLOCK (object);
386 if (self->control_bindings) {
389 for (node = self->control_bindings; node; node = g_list_next (node)) {
390 gst_object_unparent (node->data);
392 g_list_free (self->control_bindings);
393 self->control_bindings = NULL;
396 ((GObjectClass *) gst_object_parent_class)->dispose (object);
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);
414 /* finalize is called when the object has to free its resources */
416 gst_object_finalize (GObject * object)
418 GstObject *gstobject = GST_OBJECT_CAST (object);
420 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
422 g_signal_handlers_destroy (object);
424 g_free (gstobject->name);
425 g_mutex_clear (&gstobject->lock);
427 GST_TRACER_OBJECT_DESTROYED (gstobject);
428 #ifndef GST_DISABLE_TRACE
429 _gst_alloc_trace_free (_gst_object_trace, object);
432 ((GObjectClass *) gst_object_parent_class)->finalize (object);
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
444 gst_object_dispatch_properties_changed (GObject * object,
445 guint n_pspecs, GParamSpec ** pspecs)
447 GstObject *gst_object, *parent, *old_parent;
449 #ifndef GST_DISABLE_GST_DEBUG
451 const gchar *debug_name;
454 /* do the standard dispatching */
456 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
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);
468 /* now let the parent dispatch those, too */
469 parent = gst_object_get_parent (gst_object);
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);
475 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
476 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
480 parent = gst_object_get_parent (old_parent);
481 gst_object_unref (old_parent);
483 #ifndef GST_DISABLE_GST_DEBUG
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
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
502 * MT safe. This function grabs and releases @object's LOCK for getting its
506 gst_object_default_deep_notify (GObject * object, GstObject * orig,
507 GParamSpec * pspec, gchar ** excluded_props)
509 GValue value = { 0, }; /* the important thing is that value.type = 0 */
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)
520 g_value_init (&value, pspec->value_type);
521 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
523 if (G_VALUE_HOLDS_STRING (&value))
524 str = g_value_dup_string (&value);
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);
531 g_value_unset (&value);
533 name = gst_object_get_path_string (orig);
534 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
540 gst_object_set_name_default (GstObject * object)
542 const gchar *type_name;
548 /* to ensure guaranteed uniqueness across threads, only one thread
549 * may ever assign a name */
550 G_LOCK (object_name_mutex);
552 if (!object_name_counts) {
553 g_datalist_init (&object_name_counts);
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));
560 G_UNLOCK (object_name_mutex);
562 /* GstFooSink -> foosink<N> */
563 type_name = g_quark_to_string (q);
564 if (strncmp (type_name, "Gst", 3) == 0)
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);
571 name = g_strdup_printf ("%s%d", type_name, count);
575 for (i = 0; i < l; i++)
576 name[i] = g_ascii_tolower (name[i]);
578 GST_OBJECT_LOCK (object);
579 if (G_UNLIKELY (object->parent != NULL))
582 g_free (object->name);
585 GST_OBJECT_UNLOCK (object);
592 GST_WARNING ("parented objects can't be renamed");
593 GST_OBJECT_UNLOCK (object);
599 * gst_object_set_name:
600 * @object: a #GstObject
601 * @name: (allow-none): new name of object
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.
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
612 * MT safe. This function grabs and releases @object's LOCK.
615 gst_object_set_name (GstObject * object, const gchar * name)
619 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
621 GST_OBJECT_LOCK (object);
623 /* parented objects cannot be renamed */
624 if (G_UNLIKELY (object->parent != NULL))
628 g_free (object->name);
629 object->name = g_strdup (name);
630 GST_OBJECT_UNLOCK (object);
633 GST_OBJECT_UNLOCK (object);
634 result = gst_object_set_name_default (object);
637 g_object_notify (G_OBJECT (object), "name");
643 GST_WARNING ("parented objects can't be renamed");
644 GST_OBJECT_UNLOCK (object);
650 * gst_object_get_name:
651 * @object: a #GstObject
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()
658 * Free-function: g_free
660 * Returns: (transfer full) (nullable): the name of @object. g_free()
663 * MT safe. This function grabs and releases @object's LOCK.
666 gst_object_get_name (GstObject * object)
668 gchar *result = NULL;
670 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
672 GST_OBJECT_LOCK (object);
673 result = g_strdup (object->name);
674 GST_OBJECT_UNLOCK (object);
680 * gst_object_set_parent:
681 * @object: a #GstObject
682 * @parent: new parent of object
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()).
687 * Returns: %TRUE if @parent could be set or %FALSE when @object
688 * already had a parent or @object and @parent are the same.
690 * MT safe. Grabs and releases @object's LOCK.
693 gst_object_set_parent (GstObject * object, GstObject * parent)
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);
699 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
700 "set parent (ref and sink)");
702 GST_OBJECT_LOCK (object);
703 if (G_UNLIKELY (object->parent != NULL))
706 object->parent = parent;
707 gst_object_ref_sink (object);
708 GST_OBJECT_UNLOCK (object);
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.
715 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
722 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
723 "set parent failed, object already had a parent");
724 GST_OBJECT_UNLOCK (object);
730 * gst_object_get_parent:
731 * @object: a #GstObject
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.
736 * Returns: (transfer full) (nullable): parent of @object, this can be
737 * %NULL if @object has no parent. unref after usage.
739 * MT safe. Grabs and releases @object's LOCK.
742 gst_object_get_parent (GstObject * object)
744 GstObject *result = NULL;
746 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
748 GST_OBJECT_LOCK (object);
749 result = object->parent;
750 if (G_LIKELY (result))
751 gst_object_ref (result);
752 GST_OBJECT_UNLOCK (object);
758 * gst_object_unparent:
759 * @object: a #GstObject to unparent
761 * Clear the parent of @object, removing the associated reference.
762 * This function decreases the refcount of @object.
764 * MT safe. Grabs and releases @object's lock.
767 gst_object_unparent (GstObject * object)
771 g_return_if_fail (GST_IS_OBJECT (object));
773 GST_OBJECT_LOCK (object);
774 parent = object->parent;
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);
781 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
783 gst_object_unref (object);
785 GST_OBJECT_UNLOCK (object);
790 * gst_object_has_as_parent:
791 * @object: a #GstObject to check
792 * @parent: a #GstObject to check as parent
794 * Check if @parent is the parent of @object.
795 * E.g. a #GstElement can check if it owns a given #GstPad.
797 * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
798 * the parent of @object. Otherwise %FALSE.
800 * MT safe. Grabs and releases @object's locks.
804 gst_object_has_as_parent (GstObject * object, GstObject * parent)
806 gboolean result = FALSE;
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);
818 * gst_object_has_as_ancestor:
819 * @object: a #GstObject to check
820 * @ancestor: a #GstObject to check as ancestor
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.
825 * Returns: %TRUE if @ancestor is an ancestor of @object.
827 * MT safe. Grabs and releases @object's locks.
830 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
832 GstObject *parent, *tmp;
834 if (!ancestor || !object)
837 parent = gst_object_ref (object);
839 if (parent == ancestor) {
840 gst_object_unref (parent);
844 tmp = gst_object_get_parent (parent);
845 gst_object_unref (parent);
853 * gst_object_has_ancestor:
854 * @object: a #GstObject to check
855 * @ancestor: a #GstObject to check as ancestor
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.
860 * Returns: %TRUE if @ancestor is an ancestor of @object.
862 * Deprecated: Use gst_object_has_as_ancestor() instead.
864 * MT safe. Grabs and releases @object's locks.
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);
872 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
874 return gst_object_has_as_ancestor (object, ancestor);
879 * gst_object_check_uniqueness:
880 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
882 * @name: the name to search for
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.
890 * Returns: %TRUE if a #GstObject named @name does not appear in @list,
893 * MT safe. Grabs and releases the LOCK of each object in the list.
896 gst_object_check_uniqueness (GList * list, const gchar * name)
898 gboolean result = TRUE;
900 g_return_val_if_fail (name != NULL, FALSE);
902 for (; list; list = g_list_next (list)) {
906 child = GST_OBJECT_CAST (list->data);
908 GST_OBJECT_LOCK (child);
909 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
910 GST_OBJECT_UNLOCK (child);
912 if (G_UNLIKELY (eq)) {
922 gst_object_set_property (GObject * object, guint prop_id,
923 const GValue * value, GParamSpec * pspec)
925 GstObject *gstobject;
927 gstobject = GST_OBJECT_CAST (object);
931 gst_object_set_name (gstobject, g_value_get_string (value));
934 gst_object_set_parent (gstobject, g_value_get_object (value));
937 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
943 gst_object_get_property (GObject * object, guint prop_id,
944 GValue * value, GParamSpec * pspec)
946 GstObject *gstobject;
948 gstobject = GST_OBJECT_CAST (object);
952 g_value_take_string (value, gst_object_get_name (gstobject));
955 g_value_take_object (value, gst_object_get_parent (gstobject));
958 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
964 * gst_object_get_path_string:
965 * @object: a #GstObject
967 * Generates a string describing the path of @object in
968 * the object hierarchy. Only useful (or used) for debugging.
970 * Free-function: g_free
972 * Returns: (transfer full): a string describing the path of @object. You must
973 * g_free() the string after usage.
975 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
979 gst_object_get_path_string (GstObject * object)
984 gchar *prevpath, *path;
985 const gchar *typename;
987 const gchar *separator;
989 /* ref object before adding to list */
990 gst_object_ref (object);
991 parentage = g_slist_prepend (NULL, object);
993 path = g_strdup ("");
995 /* first walk the object hierarchy to build a list of the parents,
996 * be careful here with refcounting. */
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 */
1003 parentage = g_slist_prepend (parentage, parent);
1008 } while (object != NULL);
1010 /* then walk the parent list and print them out. we need to
1011 * decrease the refcounting on each element after we handled
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);
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);
1024 component = g_strdup_printf ("%s:%s", typename, objname);
1025 separator = oclass->path_string_separator;
1027 gst_object_unref (item);
1031 component = g_strdup_printf ("%s:%p", typename, parents->data);
1033 component = g_strdup_printf ("%p", parents->data);
1039 path = g_strjoin (separator, prevpath, component, NULL);
1044 g_slist_free (parentage);
1049 /* controller helper functions */
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
1056 * Searches the list of properties under control.
1058 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1059 * is not being controlled.
1061 static GstControlBinding *
1062 gst_object_find_control_binding (GstObject * self, const gchar * name)
1064 GstControlBinding *binding;
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);
1075 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1080 /* controller functions */
1083 * gst_object_suggest_next_sync:
1084 * @object: the object that has controlled properties
1086 * Returns a suggestion for timestamps where buffers should be split
1087 * to get best controller results.
1089 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1090 * if no control-rate was set.
1093 gst_object_suggest_next_sync (GstObject * object)
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);
1101 GST_OBJECT_LOCK (object);
1103 /* TODO: Implement more logic, depending on interpolation mode and control
1105 * FIXME: we need playback direction
1107 ret = object->last_sync + object->control_rate;
1109 GST_OBJECT_UNLOCK (object);
1115 * gst_object_sync_values:
1116 * @object: the object that has controlled properties
1117 * @timestamp: the time that should be processed
1119 * Sets the properties of the object, according to the #GstControlSources that
1120 * (maybe) handle them and for the given timestamp.
1122 * If this function fails, it is most likely the application developers fault.
1123 * Most probably the control sources are not setup correctly.
1125 * Returns: %TRUE if the controller values could be applied to the object
1126 * properties, %FALSE otherwise
1129 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1132 gboolean ret = TRUE;
1134 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1135 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1137 GST_LOG_OBJECT (object, "sync_values");
1138 if (!object->control_bindings)
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);
1148 object->last_sync = timestamp;
1149 g_object_thaw_notify ((GObject *) object);
1150 /* GST_OBJECT_UNLOCK (object); */
1157 * gst_object_has_active_control_bindings:
1158 * @object: the object that has controlled properties
1160 * Check if the @object has an active controlled properties.
1162 * Returns: %TRUE if the object has active controlled properties
1165 gst_object_has_active_control_bindings (GstObject * object)
1167 gboolean res = FALSE;
1170 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
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);
1176 GST_OBJECT_UNLOCK (object);
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
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.
1190 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1194 g_return_if_fail (GST_IS_OBJECT (object));
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,
1201 GST_OBJECT_UNLOCK (object);
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
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
1216 gst_object_set_control_binding_disabled (GstObject * object,
1217 const gchar * property_name, gboolean disabled)
1219 GstControlBinding *binding;
1221 g_return_if_fail (GST_IS_OBJECT (object));
1222 g_return_if_fail (property_name);
1224 GST_OBJECT_LOCK (object);
1225 if ((binding = gst_object_find_control_binding (object, property_name))) {
1226 gst_control_binding_set_disabled (binding, disabled);
1228 GST_OBJECT_UNLOCK (object);
1233 * gst_object_add_control_binding:
1234 * @object: the controller object
1235 * @binding: (transfer full): the #GstControlBinding that should be used
1237 * Attach the #GstControlBinding to the object. If there already was a
1238 * #GstControlBinding for this property it will be replaced.
1240 * The @object will take ownership of the @binding.
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.
1246 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1248 GstControlBinding *old;
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);
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));
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);
1269 * gst_object_get_control_binding:
1270 * @object: the object
1271 * @property_name: name of the property
1273 * Gets the corresponding #GstControlBinding for the property. This should be
1274 * unreferenced again after use.
1276 * Returns: (transfer full) (nullable): the #GstControlBinding for
1277 * @property_name or %NULL if the property is not controlled.
1280 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1282 GstControlBinding *binding;
1284 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1285 g_return_val_if_fail (property_name, NULL);
1287 GST_OBJECT_LOCK (object);
1288 if ((binding = gst_object_find_control_binding (object, property_name))) {
1289 gst_object_ref (binding);
1291 GST_OBJECT_UNLOCK (object);
1297 * gst_object_remove_control_binding:
1298 * @object: the object
1299 * @binding: the binding
1301 * Removes the corresponding #GstControlBinding. If it was the
1302 * last ref of the binding, it will be disposed.
1304 * Returns: %TRUE if the binding could be removed.
1307 gst_object_remove_control_binding (GstObject * object,
1308 GstControlBinding * binding)
1311 gboolean ret = FALSE;
1313 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1314 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
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));
1324 GST_OBJECT_UNLOCK (object);
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
1335 * Gets the value for the given controlled property at the requested time.
1337 * Returns: (nullable): the GValue of the property at the given time,
1338 * or %NULL if the property isn't controlled.
1341 gst_object_get_value (GstObject * object, const gchar * property_name,
1342 GstClockTime timestamp)
1344 GstControlBinding *binding;
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);
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);
1355 GST_OBJECT_UNLOCK (object);
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
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.
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.
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.
1380 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1383 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1384 GstClockTime timestamp, GstClockTime interval, guint n_values,
1387 gboolean res = FALSE;
1388 GstControlBinding *binding;
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);
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,
1401 GST_OBJECT_UNLOCK (object);
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
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
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.
1421 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1424 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1425 GstClockTime timestamp, GstClockTime interval, guint n_values,
1428 gboolean res = FALSE;
1429 GstControlBinding *binding;
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);
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,
1442 GST_OBJECT_UNLOCK (object);
1448 * gst_object_get_control_rate:
1449 * @object: the object that has controlled properties
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.
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.
1459 * The control-rate is not expected to change if the element is in
1460 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1462 * Returns: the control rate in nanoseconds
1465 gst_object_get_control_rate (GstObject * object)
1467 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1469 return object->control_rate;
1473 * gst_object_set_control_rate:
1474 * @object: the object that has controlled properties
1475 * @control_rate: the new control-rate in nanoseconds.
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.
1482 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1483 * %GST_STATE_PLAYING.
1486 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1488 g_return_if_fail (GST_IS_OBJECT (object));
1490 object->control_rate = control_rate;