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 */
123 #ifdef TIZEN_PROFILE_TV
135 /* maps type name quark => count */
136 static GData *object_name_counts = NULL;
138 G_LOCK_DEFINE_STATIC (object_name_mutex);
140 static void gst_object_set_property (GObject * object, guint prop_id,
141 const GValue * value, GParamSpec * pspec);
142 static void gst_object_get_property (GObject * object, guint prop_id,
143 GValue * value, GParamSpec * pspec);
145 static void gst_object_dispatch_properties_changed (GObject * object,
146 guint n_pspecs, GParamSpec ** pspecs);
148 static void gst_object_dispose (GObject * object);
149 static void gst_object_finalize (GObject * object);
151 static gboolean gst_object_set_name_default (GstObject * object);
153 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
155 static GParamSpec *properties[PROP_LAST];
157 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
160 gst_object_class_init (GstObjectClass * klass)
162 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
164 #ifndef GST_DISABLE_TRACE
166 _gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT), -2);
169 gobject_class->set_property = gst_object_set_property;
170 gobject_class->get_property = gst_object_get_property;
172 properties[PROP_NAME] =
173 g_param_spec_string ("name", "Name", "The name of the object", NULL,
174 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
179 * The parent of the object. Please note, that when changing the 'parent'
180 * property, we don't emit #GObject::notify and #GstObject::deep-notify
181 * signals due to locking issues. In some cases one can use
182 * #GstBin::element-added or #GstBin::element-removed signals on the parent to
183 * achieve a similar effect.
185 properties[PROP_PARENT] =
186 g_param_spec_object ("parent", "Parent", "The parent of the object",
187 GST_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
188 #ifdef TIZEN_PROFILE_TV
189 properties[PROP_FAMILY_ID] =
190 g_param_spec_int ("family-id", "Family Id",
191 "The family id of this object group, usually inherit from parent.",
192 -G_MAXINT, G_MAXINT, 0, 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->dispose = gst_object_dispose;
220 gobject_class->finalize = gst_object_finalize;
224 gst_object_init (GstObject * object)
226 g_mutex_init (&object->lock);
227 object->parent = NULL;
229 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
231 #ifndef GST_DISABLE_TRACE
232 _gst_alloc_trace_new (_gst_object_trace, object);
236 #ifdef TIZEN_PROFILE_TV
237 object->family_id = 0;
239 object->control_rate = 100 * GST_MSECOND;
240 object->last_sync = GST_CLOCK_TIME_NONE;
245 * @object: (type Gst.Object): a #GstObject to reference
247 * Increments the reference count on @object. This function
248 * does not take the lock on @object because it relies on
249 * atomic refcounting.
251 * This object returns the input parameter to ease writing
253 * result = gst_object_ref (object->parent);
255 * Returns: (transfer full) (type Gst.Object): A pointer to @object
258 gst_object_ref (gpointer object)
260 g_return_val_if_fail (object != NULL, NULL);
262 #ifdef DEBUG_REFCOUNT
263 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
264 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
266 g_object_ref (object);
273 * @object: (type Gst.Object): a #GstObject to unreference
275 * Decrements the reference count on @object. If reference count hits
276 * zero, destroy @object. This function does not take the lock
277 * on @object as it relies on atomic refcounting.
279 * The unref method should never be called with the LOCK held since
280 * this might deadlock the dispose function.
283 gst_object_unref (gpointer object)
285 g_return_if_fail (object != NULL);
286 g_return_if_fail (((GObject *) object)->ref_count > 0);
288 #ifdef DEBUG_REFCOUNT
289 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
290 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
292 g_object_unref (object);
296 * gst_object_ref_sink: (skip)
297 * @object: a #GstObject to sink
299 * Increase the reference count of @object, and possibly remove the floating
300 * reference, if @object has a floating reference.
302 * In other words, if the object is floating, then this call "assumes ownership"
303 * of the floating reference, converting it to a normal reference by clearing
304 * the floating flag while leaving the reference count unchanged. If the object
305 * is not floating, then this call adds a new normal reference increasing the
306 * reference count by one.
309 gst_object_ref_sink (gpointer object)
311 g_return_val_if_fail (object != NULL, NULL);
313 #ifdef DEBUG_REFCOUNT
314 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
315 object, ((GObject *) object)->ref_count,
316 ((GObject *) object)->ref_count + 1);
318 return g_object_ref_sink (object);
322 * gst_object_replace:
323 * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
324 * a #GstObject to replace
325 * @newobj: (transfer none) (allow-none): a new #GstObject
327 * Atomically modifies a pointer to point to a new object.
328 * The reference count of @oldobj is decreased and the reference count of
329 * @newobj is increased.
331 * Either @newobj and the value pointed to by @oldobj may be %NULL.
333 * Returns: %TRUE if @newobj was different from @oldobj
336 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
340 g_return_val_if_fail (oldobj != NULL, FALSE);
342 #ifdef DEBUG_REFCOUNT
343 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
344 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
345 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
346 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
347 newobj ? G_OBJECT (newobj)->ref_count : 0);
350 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
352 if (G_UNLIKELY (oldptr == newobj))
356 gst_object_ref (newobj);
358 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
359 oldobj, oldptr, newobj))) {
360 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
361 if (G_UNLIKELY (oldptr == newobj))
366 gst_object_unref (oldptr);
368 return oldptr != newobj;
371 /* dispose is called when the object has to release all links
372 * to other objects */
374 gst_object_dispose (GObject * object)
376 GstObject *self = (GstObject *) object;
379 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
381 GST_OBJECT_LOCK (object);
382 if ((parent = GST_OBJECT_PARENT (object)))
384 GST_OBJECT_PARENT (object) = NULL;
385 GST_OBJECT_UNLOCK (object);
387 if (self->control_bindings) {
390 for (node = self->control_bindings; node; node = g_list_next (node)) {
391 gst_object_unparent (node->data);
393 g_list_free (self->control_bindings);
394 self->control_bindings = NULL;
397 ((GObjectClass *) gst_object_parent_class)->dispose (object);
404 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
405 "parent \"%s\".\nYou need to let the parent manage the "
406 "object instead of unreffing the object directly.\n",
407 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
408 GST_OBJECT_UNLOCK (object);
409 /* ref the object again to revive it in this error case */
410 gst_object_ref (object);
415 /* finalize is called when the object has to free its resources */
417 gst_object_finalize (GObject * object)
419 GstObject *gstobject = GST_OBJECT_CAST (object);
421 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
423 g_signal_handlers_destroy (object);
425 g_free (gstobject->name);
426 g_mutex_clear (&gstobject->lock);
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);
636 /* FIXME-0.11: this misses a g_object_notify (object, "name"); unless called
637 * from gst_object_set_property.
638 * Ideally remove such custom setters (or make it static).
645 GST_WARNING ("parented objects can't be renamed");
646 GST_OBJECT_UNLOCK (object);
652 * gst_object_get_name:
653 * @object: a #GstObject
655 * Returns a copy of the name of @object.
656 * Caller should g_free() the return value after usage.
657 * For a nameless object, this returns %NULL, which you can safely g_free()
660 * Free-function: g_free
662 * Returns: (transfer full) (nullable): the name of @object. g_free()
665 * MT safe. This function grabs and releases @object's LOCK.
668 gst_object_get_name (GstObject * object)
670 gchar *result = NULL;
672 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
674 GST_OBJECT_LOCK (object);
675 result = g_strdup (object->name);
676 GST_OBJECT_UNLOCK (object);
682 * gst_object_set_parent:
683 * @object: a #GstObject
684 * @parent: new parent of object
686 * Sets the parent of @object to @parent. The object's reference count will
687 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
689 * Returns: %TRUE if @parent could be set or %FALSE when @object
690 * already had a parent or @object and @parent are the same.
692 * MT safe. Grabs and releases @object's LOCK.
695 gst_object_set_parent (GstObject * object, GstObject * parent)
697 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
698 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
699 g_return_val_if_fail (object != parent, FALSE);
701 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
702 "set parent (ref and sink)");
704 GST_OBJECT_LOCK (object);
705 if (G_UNLIKELY (object->parent != NULL))
708 object->parent = parent;
709 gst_object_ref_sink (object);
710 GST_OBJECT_UNLOCK (object);
712 /* FIXME-2.0: this does not work, the deep notify takes the lock from the
713 * parent object and deadlocks when the parent holds its lock when calling
714 * this function (like _element_add_pad()), we need to use a GRecMutex
715 * for locking the parent instead.
717 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
724 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
725 "set parent failed, object already had a parent");
726 GST_OBJECT_UNLOCK (object);
732 * gst_object_get_parent:
733 * @object: a #GstObject
735 * Returns the parent of @object. This function increases the refcount
736 * of the parent object so you should gst_object_unref() it after usage.
738 * Returns: (transfer full) (nullable): parent of @object, this can be
739 * %NULL if @object has no parent. unref after usage.
741 * MT safe. Grabs and releases @object's LOCK.
744 gst_object_get_parent (GstObject * object)
746 GstObject *result = NULL;
748 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
750 GST_OBJECT_LOCK (object);
751 result = object->parent;
752 if (G_LIKELY (result))
753 gst_object_ref (result);
754 GST_OBJECT_UNLOCK (object);
760 * gst_object_unparent:
761 * @object: a #GstObject to unparent
763 * Clear the parent of @object, removing the associated reference.
764 * This function decreases the refcount of @object.
766 * MT safe. Grabs and releases @object's lock.
769 gst_object_unparent (GstObject * object)
773 g_return_if_fail (GST_IS_OBJECT (object));
775 GST_OBJECT_LOCK (object);
776 parent = object->parent;
778 if (G_LIKELY (parent != NULL)) {
779 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
780 object->parent = NULL;
781 GST_OBJECT_UNLOCK (object);
783 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
785 gst_object_unref (object);
787 GST_OBJECT_UNLOCK (object);
792 * gst_object_has_as_parent:
793 * @object: a #GstObject to check
794 * @parent: a #GstObject to check as parent
796 * Check if @parent is the parent of @object.
797 * E.g. a #GstElement can check if it owns a given #GstPad.
799 * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
800 * the parent of @object. Otherwise %FALSE.
802 * MT safe. Grabs and releases @object's locks.
806 gst_object_has_as_parent (GstObject * object, GstObject * parent)
808 gboolean result = FALSE;
810 if (G_LIKELY (GST_IS_OBJECT (object) && GST_IS_OBJECT (parent))) {
811 GST_OBJECT_LOCK (object);
812 result = GST_OBJECT_PARENT (object) == parent;
813 GST_OBJECT_UNLOCK (object);
820 * gst_object_has_as_ancestor:
821 * @object: a #GstObject to check
822 * @ancestor: a #GstObject to check as ancestor
824 * Check if @object has an ancestor @ancestor somewhere up in
825 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
827 * Returns: %TRUE if @ancestor is an ancestor of @object.
829 * MT safe. Grabs and releases @object's locks.
832 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
834 GstObject *parent, *tmp;
836 if (!ancestor || !object)
839 parent = gst_object_ref (object);
841 if (parent == ancestor) {
842 gst_object_unref (parent);
846 tmp = gst_object_get_parent (parent);
847 gst_object_unref (parent);
855 * gst_object_has_ancestor:
856 * @object: a #GstObject to check
857 * @ancestor: a #GstObject to check as ancestor
859 * Check if @object has an ancestor @ancestor somewhere up in
860 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
862 * Returns: %TRUE if @ancestor is an ancestor of @object.
864 * Deprecated: Use gst_object_has_as_ancestor() instead.
866 * MT safe. Grabs and releases @object's locks.
868 /* FIXME 2.0: remove */
869 #ifndef GST_REMOVE_DEPRECATED
870 #ifdef GST_DISABLE_DEPRECATED
871 gboolean gst_object_has_ancestor (GstObject * object, GstObject * ancestor);
874 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
876 return gst_object_has_as_ancestor (object, ancestor);
881 * gst_object_check_uniqueness:
882 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
884 * @name: the name to search for
886 * Checks to see if there is any object named @name in @list. This function
887 * does not do any locking of any kind. You might want to protect the
888 * provided list with the lock of the owner of the list. This function
889 * will lock each #GstObject in the list to compare the name, so be
890 * careful when passing a list with a locked object.
892 * Returns: %TRUE if a #GstObject named @name does not appear in @list,
895 * MT safe. Grabs and releases the LOCK of each object in the list.
898 gst_object_check_uniqueness (GList * list, const gchar * name)
900 gboolean result = TRUE;
902 g_return_val_if_fail (name != NULL, FALSE);
904 for (; list; list = g_list_next (list)) {
908 child = GST_OBJECT_CAST (list->data);
910 GST_OBJECT_LOCK (child);
911 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
912 GST_OBJECT_UNLOCK (child);
914 if (G_UNLIKELY (eq)) {
924 gst_object_set_property (GObject * object, guint prop_id,
925 const GValue * value, GParamSpec * pspec)
927 GstObject *gstobject;
929 gstobject = GST_OBJECT_CAST (object);
933 gst_object_set_name (gstobject, g_value_get_string (value));
936 gst_object_set_parent (gstobject, g_value_get_object (value));
938 #ifdef TIZEN_PROFILE_TV
940 g_atomic_int_set (&gstobject->family_id, g_value_get_int (value));
944 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
950 gst_object_get_property (GObject * object, guint prop_id,
951 GValue * value, GParamSpec * pspec)
953 GstObject *gstobject;
955 gstobject = GST_OBJECT_CAST (object);
959 g_value_take_string (value, gst_object_get_name (gstobject));
962 g_value_take_object (value, gst_object_get_parent (gstobject));
964 #ifdef TIZEN_PROFILE_TV
966 g_value_set_int (value, g_atomic_int_get (&gstobject->family_id));
970 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
976 * gst_object_get_path_string:
977 * @object: a #GstObject
979 * Generates a string describing the path of @object in
980 * the object hierarchy. Only useful (or used) for debugging.
982 * Free-function: g_free
984 * Returns: (transfer full): a string describing the path of @object. You must
985 * g_free() the string after usage.
987 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
991 gst_object_get_path_string (GstObject * object)
996 gchar *prevpath, *path;
997 const gchar *typename;
999 const gchar *separator;
1001 /* ref object before adding to list */
1002 gst_object_ref (object);
1003 parentage = g_slist_prepend (NULL, object);
1005 path = g_strdup ("");
1007 /* first walk the object hierarchy to build a list of the parents,
1008 * be careful here with refcounting. */
1010 if (GST_IS_OBJECT (object)) {
1011 parent = gst_object_get_parent (object);
1012 /* add parents to list, refcount remains increased while
1013 * we handle the object */
1015 parentage = g_slist_prepend (parentage, parent);
1020 } while (object != NULL);
1022 /* then walk the parent list and print them out. we need to
1023 * decrease the refcounting on each element after we handled
1025 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1026 if (G_IS_OBJECT (parents->data)) {
1027 typename = G_OBJECT_TYPE_NAME (parents->data);
1031 if (GST_IS_OBJECT (parents->data)) {
1032 GstObject *item = GST_OBJECT_CAST (parents->data);
1033 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1034 gchar *objname = gst_object_get_name (item);
1036 component = g_strdup_printf ("%s:%s", typename, objname);
1037 separator = oclass->path_string_separator;
1039 gst_object_unref (item);
1043 component = g_strdup_printf ("%s:%p", typename, parents->data);
1045 component = g_strdup_printf ("%p", parents->data);
1051 path = g_strjoin (separator, prevpath, component, NULL);
1056 g_slist_free (parentage);
1061 /* controller helper functions */
1064 * gst_object_find_control_binding:
1065 * @self: the gobject to search for a property in
1066 * @name: the gobject property name to look for
1068 * Searches the list of properties under control.
1070 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1071 * is not being controlled.
1073 static GstControlBinding *
1074 gst_object_find_control_binding (GstObject * self, const gchar * name)
1076 GstControlBinding *binding;
1079 for (node = self->control_bindings; node; node = g_list_next (node)) {
1080 binding = node->data;
1081 /* FIXME: eventually use GQuark to speed it up */
1082 if (!strcmp (binding->name, name)) {
1083 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1087 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1092 /* controller functions */
1095 * gst_object_suggest_next_sync:
1096 * @object: the object that has controlled properties
1098 * Returns a suggestion for timestamps where buffers should be split
1099 * to get best controller results.
1101 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1102 * if no control-rate was set.
1105 gst_object_suggest_next_sync (GstObject * object)
1109 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1110 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1111 GST_CLOCK_TIME_NONE);
1113 GST_OBJECT_LOCK (object);
1115 /* TODO: Implement more logic, depending on interpolation mode and control
1117 * FIXME: we need playback direction
1119 ret = object->last_sync + object->control_rate;
1121 GST_OBJECT_UNLOCK (object);
1127 * gst_object_sync_values:
1128 * @object: the object that has controlled properties
1129 * @timestamp: the time that should be processed
1131 * Sets the properties of the object, according to the #GstControlSources that
1132 * (maybe) handle them and for the given timestamp.
1134 * If this function fails, it is most likely the application developers fault.
1135 * Most probably the control sources are not setup correctly.
1137 * Returns: %TRUE if the controller values could be applied to the object
1138 * properties, %FALSE otherwise
1141 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1144 gboolean ret = TRUE;
1146 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1147 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1149 GST_LOG_OBJECT (object, "sync_values");
1150 if (!object->control_bindings)
1153 /* FIXME: this deadlocks */
1154 /* GST_OBJECT_LOCK (object); */
1155 g_object_freeze_notify ((GObject *) object);
1156 for (node = object->control_bindings; node; node = g_list_next (node)) {
1157 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1158 object, timestamp, object->last_sync);
1160 object->last_sync = timestamp;
1161 g_object_thaw_notify ((GObject *) object);
1162 /* GST_OBJECT_UNLOCK (object); */
1169 * gst_object_has_active_control_bindings:
1170 * @object: the object that has controlled properties
1172 * Check if the @object has an active controlled properties.
1174 * Returns: %TRUE if the object has active controlled properties
1177 gst_object_has_active_control_bindings (GstObject * object)
1179 gboolean res = FALSE;
1182 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1184 GST_OBJECT_LOCK (object);
1185 for (node = object->control_bindings; node; node = g_list_next (node)) {
1186 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1188 GST_OBJECT_UNLOCK (object);
1193 * gst_object_set_control_bindings_disabled:
1194 * @object: the object that has controlled properties
1195 * @disabled: boolean that specifies whether to disable the controller
1198 * This function is used to disable all controlled properties of the @object for
1199 * some time, i.e. gst_object_sync_values() will do nothing.
1202 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1206 g_return_if_fail (GST_IS_OBJECT (object));
1208 GST_OBJECT_LOCK (object);
1209 for (node = object->control_bindings; node; node = g_list_next (node)) {
1210 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1213 GST_OBJECT_UNLOCK (object);
1217 * gst_object_set_control_binding_disabled:
1218 * @object: the object that has controlled properties
1219 * @property_name: property to disable
1220 * @disabled: boolean that specifies whether to disable the controller
1223 * This function is used to disable the control bindings on a property for
1224 * some time, i.e. gst_object_sync_values() will do nothing for the
1228 gst_object_set_control_binding_disabled (GstObject * object,
1229 const gchar * property_name, gboolean disabled)
1231 GstControlBinding *binding;
1233 g_return_if_fail (GST_IS_OBJECT (object));
1234 g_return_if_fail (property_name);
1236 GST_OBJECT_LOCK (object);
1237 if ((binding = gst_object_find_control_binding (object, property_name))) {
1238 gst_control_binding_set_disabled (binding, disabled);
1240 GST_OBJECT_UNLOCK (object);
1245 * gst_object_add_control_binding:
1246 * @object: the controller object
1247 * @binding: (transfer full): the #GstControlBinding that should be used
1249 * Attach the #GstControlBinding to the object. If there already was a
1250 * #GstControlBinding for this property it will be replaced.
1252 * The @object will take ownership of the @binding.
1254 * Returns: %FALSE if the given @binding has not been setup for this object or
1255 * has been setup for a non suitable property, %TRUE otherwise.
1258 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1260 GstControlBinding *old;
1262 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1263 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1264 g_return_val_if_fail (binding->pspec, FALSE);
1266 GST_OBJECT_LOCK (object);
1267 if ((old = gst_object_find_control_binding (object, binding->name))) {
1268 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1269 object->control_bindings = g_list_remove (object->control_bindings, old);
1270 gst_object_unparent (GST_OBJECT_CAST (old));
1272 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1273 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1274 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1275 GST_OBJECT_UNLOCK (object);
1281 * gst_object_get_control_binding:
1282 * @object: the object
1283 * @property_name: name of the property
1285 * Gets the corresponding #GstControlBinding for the property. This should be
1286 * unreferenced again after use.
1288 * Returns: (transfer full) (nullable): the #GstControlBinding for
1289 * @property_name or %NULL if the property is not controlled.
1292 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1294 GstControlBinding *binding;
1296 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1297 g_return_val_if_fail (property_name, NULL);
1299 GST_OBJECT_LOCK (object);
1300 if ((binding = gst_object_find_control_binding (object, property_name))) {
1301 gst_object_ref (binding);
1303 GST_OBJECT_UNLOCK (object);
1309 * gst_object_remove_control_binding:
1310 * @object: the object
1311 * @binding: the binding
1313 * Removes the corresponding #GstControlBinding. If it was the
1314 * last ref of the binding, it will be disposed.
1316 * Returns: %TRUE if the binding could be removed.
1319 gst_object_remove_control_binding (GstObject * object,
1320 GstControlBinding * binding)
1323 gboolean ret = FALSE;
1325 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1326 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1328 GST_OBJECT_LOCK (object);
1329 if ((node = g_list_find (object->control_bindings, binding))) {
1330 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1331 object->control_bindings =
1332 g_list_delete_link (object->control_bindings, node);
1333 gst_object_unparent (GST_OBJECT_CAST (binding));
1336 GST_OBJECT_UNLOCK (object);
1342 * gst_object_get_value:
1343 * @object: the object that has controlled properties
1344 * @property_name: the name of the property to get
1345 * @timestamp: the time the control-change should be read from
1347 * Gets the value for the given controlled property at the requested time.
1349 * Returns: (nullable): the GValue of the property at the given time,
1350 * or %NULL if the property isn't controlled.
1353 gst_object_get_value (GstObject * object, const gchar * property_name,
1354 GstClockTime timestamp)
1356 GstControlBinding *binding;
1359 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1360 g_return_val_if_fail (property_name, NULL);
1361 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1363 GST_OBJECT_LOCK (object);
1364 if ((binding = gst_object_find_control_binding (object, property_name))) {
1365 val = gst_control_binding_get_value (binding, timestamp);
1367 GST_OBJECT_UNLOCK (object);
1373 * gst_object_get_value_array:
1374 * @object: the object that has controlled properties
1375 * @property_name: the name of the property to get
1376 * @timestamp: the time that should be processed
1377 * @interval: the time spacing between subsequent values
1378 * @n_values: the number of values
1379 * @values: array to put control-values in
1381 * Gets a number of values for the given controlled property starting at the
1382 * requested time. The array @values need to hold enough space for @n_values of
1383 * the same type as the objects property's type.
1385 * This function is useful if one wants to e.g. draw a graph of the control
1386 * curve or apply a control curve sample by sample.
1388 * The values are unboxed and ready to be used. The similar function
1389 * gst_object_get_g_value_array() returns the array as #GValues and is
1390 * better suites for bindings.
1392 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1395 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1396 GstClockTime timestamp, GstClockTime interval, guint n_values,
1399 gboolean res = FALSE;
1400 GstControlBinding *binding;
1402 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1403 g_return_val_if_fail (property_name, FALSE);
1404 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1405 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1406 g_return_val_if_fail (values, FALSE);
1408 GST_OBJECT_LOCK (object);
1409 if ((binding = gst_object_find_control_binding (object, property_name))) {
1410 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1413 GST_OBJECT_UNLOCK (object);
1418 * gst_object_get_g_value_array:
1419 * @object: the object that has controlled properties
1420 * @property_name: the name of the property to get
1421 * @timestamp: the time that should be processed
1422 * @interval: the time spacing between subsequent values
1423 * @n_values: the number of values
1424 * @values: array to put control-values in
1426 * Gets a number of #GValues for the given controlled property starting at the
1427 * requested time. The array @values need to hold enough space for @n_values of
1430 * This function is useful if one wants to e.g. draw a graph of the control
1431 * curve or apply a control curve sample by sample.
1433 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1436 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1437 GstClockTime timestamp, GstClockTime interval, guint n_values,
1440 gboolean res = FALSE;
1441 GstControlBinding *binding;
1443 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1444 g_return_val_if_fail (property_name, FALSE);
1445 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1446 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1447 g_return_val_if_fail (values, FALSE);
1449 GST_OBJECT_LOCK (object);
1450 if ((binding = gst_object_find_control_binding (object, property_name))) {
1451 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1454 GST_OBJECT_UNLOCK (object);
1460 * gst_object_get_control_rate:
1461 * @object: the object that has controlled properties
1463 * Obtain the control-rate for this @object. Audio processing #GstElement
1464 * objects will use this rate to sub-divide their processing loop and call
1465 * gst_object_sync_values() inbetween. The length of the processing segment
1466 * should be up to @control-rate nanoseconds.
1468 * If the @object is not under property control, this will return
1469 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1471 * The control-rate is not expected to change if the element is in
1472 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1474 * Returns: the control rate in nanoseconds
1477 gst_object_get_control_rate (GstObject * object)
1479 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1481 return object->control_rate;
1485 * gst_object_set_control_rate:
1486 * @object: the object that has controlled properties
1487 * @control_rate: the new control-rate in nanoseconds.
1489 * Change the control-rate for this @object. Audio processing #GstElement
1490 * objects will use this rate to sub-divide their processing loop and call
1491 * gst_object_sync_values() inbetween. The length of the processing segment
1492 * should be up to @control-rate nanoseconds.
1494 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1495 * %GST_STATE_PLAYING.
1498 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1500 g_return_if_fail (GST_IS_OBJECT (object));
1502 object->control_rate = control_rate;