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.
27 * @short_description: Base class for the GStreamer object hierarchy
29 * #GstObject provides a root for the object hierarchy tree filed in by the
30 * GStreamer library. It is currently a thin wrapper on top of
31 * #GInitiallyUnowned. It is an abstract class that is not very usable on its own.
33 * #GstObject gives us basic refcounting, parenting functionality and locking.
34 * Most of the functions are just extended for special GStreamer needs and can be
35 * found under the same name in the base class of #GstObject which is #GObject
36 * (e.g. g_object_ref() becomes gst_object_ref()).
38 * Since #GstObject derives from #GInitiallyUnowned, it also inherits the
39 * floating reference. Be aware that functions such as gst_bin_add() and
40 * gst_element_add_pad() take ownership of the floating reference.
42 * In contrast to #GObject instances, #GstObject adds a name property. The functions
43 * gst_object_set_name() and gst_object_get_name() are used to set/get the name
46 * ## controlled properties
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!
56 * * mark gobject-properties paramspecs that make sense to be controlled,
57 * by GST_PARAM_CONTROLLABLE.
59 * * when processing data (get, chain, loop function) at the beginning call
60 * gst_object_sync_values(element,timestamp).
61 * This will make the controller update all GObject properties that are
62 * under its control with the current values based on the timestamp.
64 * What needs to be done in applications? Again it's not a lot to change.
66 * * create a #GstControlSource.
67 * csource = gst_interpolation_control_source_new ();
68 * g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
70 * * Attach the #GstControlSource on the controller to a property.
71 * gst_object_add_control_binding (object, gst_direct_control_binding_new (object, "prop1", csource));
73 * * Set the control values
74 * gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,0 * GST_SECOND, value1);
75 * gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,1 * GST_SECOND, value2);
77 * * start your pipeline
80 #include "gst_private.h"
81 #include "glib-compat-private.h"
83 #include "gstobject.h"
85 #include "gstcontrolbinding.h"
86 #include "gstcontrolsource.h"
88 #include "gstparamspecs.h"
91 #define DEBUG_REFCOUNT
93 /* Object signals and args */
105 #ifdef TIZEN_PROFILE_TV
117 /* maps type name quark => count */
118 static GData *object_name_counts = NULL;
120 G_LOCK_DEFINE_STATIC (object_name_mutex);
122 static void gst_object_set_property (GObject * object, guint prop_id,
123 const GValue * value, GParamSpec * pspec);
124 static void gst_object_get_property (GObject * object, guint prop_id,
125 GValue * value, GParamSpec * pspec);
127 static void gst_object_dispatch_properties_changed (GObject * object,
128 guint n_pspecs, GParamSpec ** pspecs);
130 static void gst_object_dispose (GObject * object);
131 static void gst_object_finalize (GObject * object);
133 static gboolean gst_object_set_name_default (GstObject * object);
135 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
137 static GParamSpec *properties[PROP_LAST];
139 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
142 gst_object_constructed (GObject * object)
144 GST_TRACER_OBJECT_CREATED (GST_OBJECT_CAST (object));
146 ((GObjectClass *) gst_object_parent_class)->constructed (object);
150 gst_object_class_init (GstObjectClass * klass)
152 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154 gobject_class->set_property = gst_object_set_property;
155 gobject_class->get_property = gst_object_get_property;
157 properties[PROP_NAME] =
158 g_param_spec_string ("name", "Name", "The name of the object", NULL,
159 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS |
160 GST_PARAM_DOC_SHOW_DEFAULT);
165 * The parent of the object. Please note, that when changing the 'parent'
166 * property, we don't emit #GObject::notify and #GstObject::deep-notify
167 * signals due to locking issues. In some cases one can use
168 * #GstBin::element-added or #GstBin::element-removed signals on the parent to
169 * achieve a similar effect.
171 properties[PROP_PARENT] =
172 g_param_spec_object ("parent", "Parent", "The parent of the object",
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_DOC_SHOW_DEFAULT);
176 #ifdef TIZEN_PROFILE_TV
177 properties[PROP_FAMILY_ID] =
178 g_param_spec_int ("family-id", "Family Id",
179 "The family id of this object group, usually inherit from parent.",
180 -G_MAXINT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
183 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
186 * GstObject::deep-notify:
187 * @gstobject: a #GstObject
188 * @prop_object: the object that originated the signal
189 * @prop: the property that changed
191 * The deep notify signal is used to be notified of property changes. It is
192 * typically attached to the toplevel bin to receive notifications from all
193 * the elements contained in that bin.
195 gst_object_signals[DEEP_NOTIFY] =
196 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
198 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
199 NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_OBJECT, G_TYPE_PARAM);
201 klass->path_string_separator = "/";
203 /* see the comments at gst_object_dispatch_properties_changed */
204 gobject_class->dispatch_properties_changed
205 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
207 gobject_class->constructed = gst_object_constructed;
208 gobject_class->dispose = gst_object_dispose;
209 gobject_class->finalize = gst_object_finalize;
213 gst_object_init (GstObject * object)
215 g_mutex_init (&object->lock);
216 object->parent = NULL;
218 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
221 #ifdef TIZEN_PROFILE_TV
222 object->family_id = 0;
224 object->control_rate = 100 * GST_MSECOND;
225 object->last_sync = GST_CLOCK_TIME_NONE;
230 * @object: (type Gst.Object): a #GstObject to reference
232 * Increments the reference count on @object. This function
233 * does not take the lock on @object because it relies on
234 * atomic refcounting.
236 * This object returns the input parameter to ease writing
238 * result = gst_object_ref (object->parent);
240 * Returns: (transfer full) (type Gst.Object): A pointer to @object
243 gst_object_ref (gpointer object)
245 g_return_val_if_fail (object != NULL, NULL);
247 GST_TRACER_OBJECT_REFFED (object, ((GObject *) object)->ref_count + 1);
248 #ifdef DEBUG_REFCOUNT
249 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
250 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
252 g_object_ref (object);
259 * @object: (type Gst.Object): a #GstObject to unreference
261 * Decrements the reference count on @object. If reference count hits
262 * zero, destroy @object. This function does not take the lock
263 * on @object as it relies on atomic refcounting.
265 * The unref method should never be called with the LOCK held since
266 * this might deadlock the dispose function.
269 gst_object_unref (gpointer object)
271 g_return_if_fail (object != NULL);
272 g_return_if_fail (((GObject *) object)->ref_count > 0);
274 GST_TRACER_OBJECT_UNREFFED (object, ((GObject *) object)->ref_count - 1);
275 #ifdef DEBUG_REFCOUNT
276 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
277 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
279 g_object_unref (object);
283 * gst_object_ref_sink: (skip)
284 * @object: a #GstObject to sink
286 * Increase the reference count of @object, and possibly remove the floating
287 * reference, if @object has a floating reference.
289 * In other words, if the object is floating, then this call "assumes ownership"
290 * of the floating reference, converting it to a normal reference by clearing
291 * the floating flag while leaving the reference count unchanged. If the object
292 * is not floating, then this call adds a new normal reference increasing the
293 * reference count by one.
295 * For more background on "floating references" please see the #GObject
299 gst_object_ref_sink (gpointer object)
301 g_return_val_if_fail (object != NULL, NULL);
303 #ifdef DEBUG_REFCOUNT
304 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
305 object, ((GObject *) object)->ref_count,
306 ((GObject *) object)->ref_count + 1);
308 return g_object_ref_sink (object);
312 * gst_clear_object: (skip)
313 * @object_ptr: a pointer to a #GstObject reference
315 * Clears a reference to a #GstObject.
317 * @object_ptr must not be %NULL.
319 * If the reference is %NULL then this function does nothing.
320 * Otherwise, the reference count of the object is decreased using
321 * gst_object_unref() and the pointer is set to %NULL.
323 * A macro is also included that allows this function to be used without
328 #undef gst_clear_object
330 gst_clear_object (GstObject ** object_ptr)
332 g_clear_pointer (object_ptr, gst_object_unref);
336 * gst_object_replace:
337 * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
338 * a #GstObject to replace
339 * @newobj: (transfer none) (allow-none): a new #GstObject
341 * Atomically modifies a pointer to point to a new object.
342 * The reference count of @oldobj is decreased and the reference count of
343 * @newobj is increased.
345 * Either @newobj and the value pointed to by @oldobj may be %NULL.
347 * Returns: %TRUE if @newobj was different from @oldobj
350 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
354 g_return_val_if_fail (oldobj != NULL, FALSE);
356 #ifdef DEBUG_REFCOUNT
357 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
358 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
359 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
360 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
361 newobj ? G_OBJECT (newobj)->ref_count : 0);
364 oldptr = (GstObject *) g_atomic_pointer_get ((gpointer *) oldobj);
366 if (G_UNLIKELY (oldptr == newobj))
370 gst_object_ref (newobj);
372 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
373 oldobj, (gpointer) oldptr, newobj))) {
374 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
375 if (G_UNLIKELY (oldptr == newobj))
380 gst_object_unref (oldptr);
382 return oldptr != newobj;
385 /* dispose is called when the object has to release all links
386 * to other objects */
388 gst_object_dispose (GObject * object)
390 GstObject *self = (GstObject *) object;
393 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
395 GST_OBJECT_LOCK (object);
396 if ((parent = GST_OBJECT_PARENT (object)))
398 GST_OBJECT_PARENT (object) = NULL;
399 GST_OBJECT_UNLOCK (object);
401 if (self->control_bindings) {
404 for (node = self->control_bindings; node; node = g_list_next (node)) {
405 gst_object_unparent (node->data);
407 g_list_free (self->control_bindings);
408 self->control_bindings = NULL;
411 ((GObjectClass *) gst_object_parent_class)->dispose (object);
418 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
419 "parent \"%s\".\nYou need to let the parent manage the "
420 "object instead of unreffing the object directly.\n",
421 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
422 GST_OBJECT_UNLOCK (object);
423 /* ref the object again to revive it in this error case */
424 gst_object_ref (object);
429 /* finalize is called when the object has to free its resources */
431 gst_object_finalize (GObject * object)
433 GstObject *gstobject = GST_OBJECT_CAST (object);
435 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
437 g_signal_handlers_destroy (object);
439 g_free (gstobject->name);
440 g_mutex_clear (&gstobject->lock);
442 GST_TRACER_OBJECT_DESTROYED (gstobject);
444 ((GObjectClass *) gst_object_parent_class)->finalize (object);
447 /* Changing a GObject property of a GstObject will result in "deep-notify"
448 * signals being emitted by the object itself, as well as in each parent
449 * object. This is so that an application can connect a listener to the
450 * top-level bin to catch property-change notifications for all contained
456 gst_object_dispatch_properties_changed (GObject * object,
457 guint n_pspecs, GParamSpec ** pspecs)
459 GstObject *gst_object, *parent, *old_parent;
461 #ifndef GST_DISABLE_GST_DEBUG
463 const gchar *debug_name;
466 /* do the standard dispatching */
468 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
471 gst_object = GST_OBJECT_CAST (object);
472 #ifndef GST_DISABLE_GST_DEBUG
473 if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
474 name = gst_object_get_name (gst_object);
475 debug_name = GST_STR_NULL (name);
480 /* now let the parent dispatch those, too */
481 parent = gst_object_get_parent (gst_object);
483 for (i = 0; i < n_pspecs; i++) {
484 GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
485 "deep notification from %s (%s)", debug_name, pspecs[i]->name);
487 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
488 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
492 parent = gst_object_get_parent (old_parent);
493 gst_object_unref (old_parent);
495 #ifndef GST_DISABLE_GST_DEBUG
501 * gst_object_default_deep_notify:
502 * @object: the #GObject that signalled the notify.
503 * @orig: a #GstObject that initiated the notify.
504 * @pspec: a #GParamSpec of the property.
505 * @excluded_props: (array zero-terminated=1) (element-type gchar*) (allow-none):
506 * a set of user-specified properties to exclude or %NULL to show
509 * A default deep_notify signal callback for an object. The user data
510 * should contain a pointer to an array of strings that should be excluded
511 * from the notify. The default handler will print the new value of the property
514 * MT safe. This function grabs and releases @object's LOCK for getting its
518 gst_object_default_deep_notify (GObject * object, GstObject * orig,
519 GParamSpec * pspec, gchar ** excluded_props)
521 GValue value = { 0, }; /* the important thing is that value.type = 0 */
525 if (pspec->flags & G_PARAM_READABLE) {
526 /* let's not print these out for excluded properties... */
527 while (excluded_props != NULL && *excluded_props != NULL) {
528 if (strcmp (pspec->name, *excluded_props) == 0)
532 g_value_init (&value, pspec->value_type);
533 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
535 if (G_VALUE_HOLDS_STRING (&value))
536 str = g_value_dup_string (&value);
538 str = gst_value_serialize (&value);
539 name = gst_object_get_path_string (orig);
540 g_print ("%s: %s = %s\n", name, pspec->name, str);
543 g_value_unset (&value);
545 name = gst_object_get_path_string (orig);
546 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
552 gst_object_set_name_default (GstObject * object)
554 const gchar *type_name;
560 /* to ensure guaranteed uniqueness across threads, only one thread
561 * may ever assign a name */
562 G_LOCK (object_name_mutex);
564 if (!object_name_counts) {
565 g_datalist_init (&object_name_counts);
568 q = g_type_qname (G_OBJECT_TYPE (object));
569 count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
570 g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
572 G_UNLOCK (object_name_mutex);
574 /* GstFooSink -> foosink<N> */
575 type_name = g_quark_to_string (q);
576 if (strncmp (type_name, "Gst", 3) == 0)
578 /* give the 20th "queue" element and the first "queue2" different names */
579 l = strlen (type_name);
580 if (l > 0 && g_ascii_isdigit (type_name[l - 1])) {
581 name = g_strdup_printf ("%s-%d", type_name, count);
583 name = g_strdup_printf ("%s%d", type_name, count);
587 for (i = 0; i < l; i++)
588 name[i] = g_ascii_tolower (name[i]);
590 GST_OBJECT_LOCK (object);
591 if (G_UNLIKELY (object->parent != NULL))
594 g_free (object->name);
597 GST_OBJECT_UNLOCK (object);
604 GST_WARNING ("parented objects can't be renamed");
605 GST_OBJECT_UNLOCK (object);
611 gst_object_set_name_intern (GstObject * object, const gchar * name)
615 GST_OBJECT_LOCK (object);
617 /* parented objects cannot be renamed */
618 if (G_UNLIKELY (object->parent != NULL))
622 g_free (object->name);
623 object->name = g_strdup (name);
624 GST_OBJECT_UNLOCK (object);
627 GST_OBJECT_UNLOCK (object);
628 result = gst_object_set_name_default (object);
636 GST_WARNING ("parented objects can't be renamed");
637 GST_OBJECT_UNLOCK (object);
643 * gst_object_set_name:
644 * @object: a #GstObject
645 * @name: (allow-none): new name of object
647 * Sets the name of @object, or gives @object a guaranteed unique
648 * name (if @name is %NULL).
649 * This function makes a copy of the provided name, so the caller
650 * retains ownership of the name it sent.
652 * Returns: %TRUE if the name could be set. Since Objects that have
653 * a parent cannot be renamed, this function returns %FALSE in those
656 * MT safe. This function grabs and releases @object's LOCK.
659 gst_object_set_name (GstObject * object, const gchar * name)
663 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
665 if ((result = gst_object_set_name_intern (object, name)))
666 g_object_notify_by_pspec (G_OBJECT (object), properties[PROP_NAME]);
671 * gst_object_get_name:
672 * @object: a #GstObject
674 * Returns a copy of the name of @object.
675 * Caller should g_free() the return value after usage.
676 * For a nameless object, this returns %NULL, which you can safely g_free()
679 * Free-function: g_free
681 * Returns: (transfer full) (nullable): the name of @object. g_free()
684 * MT safe. This function grabs and releases @object's LOCK.
687 gst_object_get_name (GstObject * object)
689 gchar *result = NULL;
691 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
693 GST_OBJECT_LOCK (object);
694 result = g_strdup (object->name);
695 GST_OBJECT_UNLOCK (object);
701 * gst_object_set_parent:
702 * @object: (transfer floating): a #GstObject
703 * @parent: new parent of object
705 * Sets the parent of @object to @parent. The object's reference count will
706 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
708 * Returns: %TRUE if @parent could be set or %FALSE when @object
709 * already had a parent or @object and @parent are the same.
711 * MT safe. Grabs and releases @object's LOCK.
714 gst_object_set_parent (GstObject * object, GstObject * parent)
716 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
717 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
718 g_return_val_if_fail (object != parent, FALSE);
720 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
721 "set parent (ref and sink)");
723 GST_OBJECT_LOCK (object);
724 if (G_UNLIKELY (object->parent != NULL))
727 object->parent = parent;
728 gst_object_ref_sink (object);
729 GST_OBJECT_UNLOCK (object);
731 /* FIXME-2.0: this does not work, the deep notify takes the lock from the
732 * parent object and deadlocks when the parent holds its lock when calling
733 * this function (like _element_add_pad()), we need to use a GRecMutex
734 * for locking the parent instead.
736 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
743 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
744 "set parent failed, object already had a parent");
745 gst_object_ref_sink (object);
746 gst_object_unref (object);
747 GST_OBJECT_UNLOCK (object);
753 * gst_object_get_parent:
754 * @object: a #GstObject
756 * Returns the parent of @object. This function increases the refcount
757 * of the parent object so you should gst_object_unref() it after usage.
759 * Returns: (transfer full) (nullable): parent of @object, this can be
760 * %NULL if @object has no parent. unref after usage.
762 * MT safe. Grabs and releases @object's LOCK.
765 gst_object_get_parent (GstObject * object)
767 GstObject *result = NULL;
769 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
771 GST_OBJECT_LOCK (object);
772 result = object->parent;
773 if (G_LIKELY (result))
774 gst_object_ref (result);
775 GST_OBJECT_UNLOCK (object);
781 * gst_object_unparent:
782 * @object: a #GstObject to unparent
784 * Clear the parent of @object, removing the associated reference.
785 * This function decreases the refcount of @object.
787 * MT safe. Grabs and releases @object's lock.
790 gst_object_unparent (GstObject * object)
794 g_return_if_fail (GST_IS_OBJECT (object));
796 GST_OBJECT_LOCK (object);
797 parent = object->parent;
799 if (G_LIKELY (parent != NULL)) {
800 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
801 object->parent = NULL;
802 GST_OBJECT_UNLOCK (object);
804 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
806 gst_object_unref (object);
808 GST_OBJECT_UNLOCK (object);
813 * gst_object_has_as_parent:
814 * @object: a #GstObject to check
815 * @parent: a #GstObject to check as parent
817 * Check if @parent is the parent of @object.
818 * E.g. a #GstElement can check if it owns a given #GstPad.
820 * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
821 * the parent of @object. Otherwise %FALSE.
823 * MT safe. Grabs and releases @object's locks.
827 gst_object_has_as_parent (GstObject * object, GstObject * parent)
829 gboolean result = FALSE;
831 if (G_LIKELY (GST_IS_OBJECT (object) && GST_IS_OBJECT (parent))) {
832 GST_OBJECT_LOCK (object);
833 result = GST_OBJECT_PARENT (object) == parent;
834 GST_OBJECT_UNLOCK (object);
841 * gst_object_has_as_ancestor:
842 * @object: a #GstObject to check
843 * @ancestor: a #GstObject to check as ancestor
845 * Check if @object has an ancestor @ancestor somewhere up in
846 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
848 * Returns: %TRUE if @ancestor is an ancestor of @object.
850 * MT safe. Grabs and releases @object's locks.
853 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
855 GstObject *parent, *tmp;
857 if (!ancestor || !object)
860 parent = gst_object_ref (object);
862 if (parent == ancestor) {
863 gst_object_unref (parent);
867 tmp = gst_object_get_parent (parent);
868 gst_object_unref (parent);
876 * gst_object_has_ancestor:
877 * @object: a #GstObject to check
878 * @ancestor: a #GstObject to check as ancestor
880 * Check if @object has an ancestor @ancestor somewhere up in
881 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
883 * Returns: %TRUE if @ancestor is an ancestor of @object.
885 * Deprecated: Use gst_object_has_as_ancestor() instead.
887 * MT safe. Grabs and releases @object's locks.
889 #ifndef GST_REMOVE_DEPRECATED
891 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
893 return gst_object_has_as_ancestor (object, ancestor);
898 * gst_object_check_uniqueness:
899 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
901 * @name: the name to search for
903 * Checks to see if there is any object named @name in @list. This function
904 * does not do any locking of any kind. You might want to protect the
905 * provided list with the lock of the owner of the list. This function
906 * will lock each #GstObject in the list to compare the name, so be
907 * careful when passing a list with a locked object.
909 * Returns: %TRUE if a #GstObject named @name does not appear in @list,
912 * MT safe. Grabs and releases the LOCK of each object in the list.
915 gst_object_check_uniqueness (GList * list, const gchar * name)
917 gboolean result = TRUE;
919 g_return_val_if_fail (name != NULL, FALSE);
921 for (; list; list = g_list_next (list)) {
925 child = GST_OBJECT_CAST (list->data);
927 GST_OBJECT_LOCK (child);
928 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
929 GST_OBJECT_UNLOCK (child);
931 if (G_UNLIKELY (eq)) {
941 gst_object_set_property (GObject * object, guint prop_id,
942 const GValue * value, GParamSpec * pspec)
944 GstObject *gstobject;
946 gstobject = GST_OBJECT_CAST (object);
950 gst_object_set_name_intern (gstobject, g_value_get_string (value));
953 gst_object_set_parent (gstobject, g_value_get_object (value));
955 #ifdef TIZEN_PROFILE_TV
957 g_atomic_int_set (&gstobject->family_id, g_value_get_int (value));
961 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
967 gst_object_get_property (GObject * object, guint prop_id,
968 GValue * value, GParamSpec * pspec)
970 GstObject *gstobject;
972 gstobject = GST_OBJECT_CAST (object);
976 g_value_take_string (value, gst_object_get_name (gstobject));
979 g_value_take_object (value, gst_object_get_parent (gstobject));
981 #ifdef TIZEN_PROFILE_TV
983 g_value_set_int (value, g_atomic_int_get (&gstobject->family_id));
987 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
993 * gst_object_get_path_string:
994 * @object: a #GstObject
996 * Generates a string describing the path of @object in
997 * the object hierarchy. Only useful (or used) for debugging.
999 * Free-function: g_free
1001 * Returns: (transfer full): a string describing the path of @object. You must
1002 * g_free() the string after usage.
1004 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
1008 gst_object_get_path_string (GstObject * object)
1013 gchar *prevpath, *path;
1014 const gchar *typename;
1016 const gchar *separator;
1018 /* ref object before adding to list */
1019 gst_object_ref (object);
1020 parentage = g_slist_prepend (NULL, object);
1022 path = g_strdup ("");
1024 /* first walk the object hierarchy to build a list of the parents,
1025 * be careful here with refcounting. */
1027 if (GST_IS_OBJECT (object)) {
1028 parent = gst_object_get_parent (object);
1029 /* add parents to list, refcount remains increased while
1030 * we handle the object */
1032 parentage = g_slist_prepend (parentage, parent);
1037 } while (object != NULL);
1039 /* then walk the parent list and print them out. we need to
1040 * decrease the refcounting on each element after we handled
1042 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1043 if (G_IS_OBJECT (parents->data)) {
1044 typename = G_OBJECT_TYPE_NAME (parents->data);
1048 if (GST_IS_OBJECT (parents->data)) {
1049 GstObject *item = GST_OBJECT_CAST (parents->data);
1050 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1051 gchar *objname = gst_object_get_name (item);
1053 component = g_strdup_printf ("%s:%s", typename, objname);
1054 separator = oclass->path_string_separator;
1056 gst_object_unref (item);
1060 component = g_strdup_printf ("%s:%p", typename, parents->data);
1062 component = g_strdup_printf ("%p", parents->data);
1068 path = g_strjoin (separator, prevpath, component, NULL);
1073 g_slist_free (parentage);
1078 /* controller helper functions */
1081 * gst_object_find_control_binding:
1082 * @self: the gobject to search for a property in
1083 * @name: the gobject property name to look for
1085 * Searches the list of properties under control.
1087 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1088 * is not being controlled.
1090 static GstControlBinding *
1091 gst_object_find_control_binding (GstObject * self, const gchar * name)
1093 GstControlBinding *binding;
1096 for (node = self->control_bindings; node; node = g_list_next (node)) {
1097 binding = node->data;
1098 /* FIXME: eventually use GQuark to speed it up */
1099 if (!strcmp (binding->name, name)) {
1100 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1104 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1109 /* controller functions */
1112 * gst_object_suggest_next_sync:
1113 * @object: the object that has controlled properties
1115 * Returns a suggestion for timestamps where buffers should be split
1116 * to get best controller results.
1118 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1119 * if no control-rate was set.
1122 gst_object_suggest_next_sync (GstObject * object)
1126 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1127 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1128 GST_CLOCK_TIME_NONE);
1130 GST_OBJECT_LOCK (object);
1132 /* TODO: Implement more logic, depending on interpolation mode and control
1134 * FIXME: we need playback direction
1136 ret = object->last_sync + object->control_rate;
1138 GST_OBJECT_UNLOCK (object);
1144 * gst_object_sync_values:
1145 * @object: the object that has controlled properties
1146 * @timestamp: the time that should be processed
1148 * Sets the properties of the object, according to the #GstControlSources that
1149 * (maybe) handle them and for the given timestamp.
1151 * If this function fails, it is most likely the application developers fault.
1152 * Most probably the control sources are not setup correctly.
1154 * Returns: %TRUE if the controller values could be applied to the object
1155 * properties, %FALSE otherwise
1158 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1161 gboolean ret = TRUE;
1163 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1164 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1166 GST_LOG_OBJECT (object, "sync_values");
1167 if (!object->control_bindings)
1170 /* FIXME: this deadlocks */
1171 /* GST_OBJECT_LOCK (object); */
1172 g_object_freeze_notify ((GObject *) object);
1173 for (node = object->control_bindings; node; node = g_list_next (node)) {
1174 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1175 object, timestamp, object->last_sync);
1177 object->last_sync = timestamp;
1178 g_object_thaw_notify ((GObject *) object);
1179 /* GST_OBJECT_UNLOCK (object); */
1186 * gst_object_has_active_control_bindings:
1187 * @object: the object that has controlled properties
1189 * Check if the @object has active controlled properties.
1191 * Returns: %TRUE if the object has active controlled properties
1194 gst_object_has_active_control_bindings (GstObject * object)
1196 gboolean res = FALSE;
1199 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1201 GST_OBJECT_LOCK (object);
1202 for (node = object->control_bindings; node; node = g_list_next (node)) {
1203 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1205 GST_OBJECT_UNLOCK (object);
1210 * gst_object_set_control_bindings_disabled:
1211 * @object: the object that has controlled properties
1212 * @disabled: boolean that specifies whether to disable the controller
1215 * This function is used to disable all controlled properties of the @object for
1216 * some time, i.e. gst_object_sync_values() will do nothing.
1219 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1223 g_return_if_fail (GST_IS_OBJECT (object));
1225 GST_OBJECT_LOCK (object);
1226 for (node = object->control_bindings; node; node = g_list_next (node)) {
1227 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1230 GST_OBJECT_UNLOCK (object);
1234 * gst_object_set_control_binding_disabled:
1235 * @object: the object that has controlled properties
1236 * @property_name: property to disable
1237 * @disabled: boolean that specifies whether to disable the controller
1240 * This function is used to disable the control bindings on a property for
1241 * some time, i.e. gst_object_sync_values() will do nothing for the
1245 gst_object_set_control_binding_disabled (GstObject * object,
1246 const gchar * property_name, gboolean disabled)
1248 GstControlBinding *binding;
1250 g_return_if_fail (GST_IS_OBJECT (object));
1251 g_return_if_fail (property_name);
1253 GST_OBJECT_LOCK (object);
1254 if ((binding = gst_object_find_control_binding (object, property_name))) {
1255 gst_control_binding_set_disabled (binding, disabled);
1257 GST_OBJECT_UNLOCK (object);
1262 * gst_object_add_control_binding:
1263 * @object: the controller object
1264 * @binding: (transfer floating): the #GstControlBinding that should be used
1266 * Attach the #GstControlBinding to the object. If there already was a
1267 * #GstControlBinding for this property it will be replaced.
1269 * The object's reference count will be incremented, and any floating
1270 * reference will be removed (see gst_object_ref_sink())
1272 * Returns: %FALSE if the given @binding has not been setup for this object or
1273 * has been setup for a non suitable property, %TRUE otherwise.
1276 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1278 GstControlBinding *old;
1280 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1281 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1282 g_return_val_if_fail (binding->pspec, FALSE);
1284 GST_OBJECT_LOCK (object);
1285 if ((old = gst_object_find_control_binding (object, binding->name))) {
1286 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1287 object->control_bindings = g_list_remove (object->control_bindings, old);
1288 gst_object_unparent (GST_OBJECT_CAST (old));
1290 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1291 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1292 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1293 GST_OBJECT_UNLOCK (object);
1299 * gst_object_get_control_binding:
1300 * @object: the object
1301 * @property_name: name of the property
1303 * Gets the corresponding #GstControlBinding for the property. This should be
1304 * unreferenced again after use.
1306 * Returns: (transfer full) (nullable): the #GstControlBinding for
1307 * @property_name or %NULL if the property is not controlled.
1310 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1312 GstControlBinding *binding;
1314 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1315 g_return_val_if_fail (property_name, NULL);
1317 GST_OBJECT_LOCK (object);
1318 if ((binding = gst_object_find_control_binding (object, property_name))) {
1319 gst_object_ref (binding);
1321 GST_OBJECT_UNLOCK (object);
1327 * gst_object_remove_control_binding:
1328 * @object: the object
1329 * @binding: the binding
1331 * Removes the corresponding #GstControlBinding. If it was the
1332 * last ref of the binding, it will be disposed.
1334 * Returns: %TRUE if the binding could be removed.
1337 gst_object_remove_control_binding (GstObject * object,
1338 GstControlBinding * binding)
1341 gboolean ret = FALSE;
1343 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1344 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1346 GST_OBJECT_LOCK (object);
1347 if ((node = g_list_find (object->control_bindings, binding))) {
1348 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1349 object->control_bindings =
1350 g_list_delete_link (object->control_bindings, node);
1351 gst_object_unparent (GST_OBJECT_CAST (binding));
1354 GST_OBJECT_UNLOCK (object);
1360 * gst_object_get_value:
1361 * @object: the object that has controlled properties
1362 * @property_name: the name of the property to get
1363 * @timestamp: the time the control-change should be read from
1365 * Gets the value for the given controlled property at the requested time.
1367 * Returns: (nullable): the GValue of the property at the given time,
1368 * or %NULL if the property isn't controlled.
1371 gst_object_get_value (GstObject * object, const gchar * property_name,
1372 GstClockTime timestamp)
1374 GstControlBinding *binding;
1377 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1378 g_return_val_if_fail (property_name, NULL);
1379 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1381 GST_OBJECT_LOCK (object);
1382 if ((binding = gst_object_find_control_binding (object, property_name))) {
1383 val = gst_control_binding_get_value (binding, timestamp);
1385 GST_OBJECT_UNLOCK (object);
1391 * gst_object_get_value_array: (skip)
1392 * @object: the object that has controlled properties
1393 * @property_name: the name of the property to get
1394 * @timestamp: the time that should be processed
1395 * @interval: the time spacing between subsequent values
1396 * @n_values: the number of values
1397 * @values: array to put control-values in
1399 * Gets a number of values for the given controlled property starting at the
1400 * requested time. The array @values need to hold enough space for @n_values of
1401 * the same type as the objects property's type.
1403 * This function is useful if one wants to e.g. draw a graph of the control
1404 * curve or apply a control curve sample by sample.
1406 * The values are unboxed and ready to be used. The similar function
1407 * gst_object_get_g_value_array() returns the array as #GValues and is
1408 * better suites for bindings.
1410 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1413 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1414 GstClockTime timestamp, GstClockTime interval, guint n_values,
1417 gboolean res = FALSE;
1418 GstControlBinding *binding;
1420 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1421 g_return_val_if_fail (property_name, FALSE);
1422 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1423 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1424 g_return_val_if_fail (values, FALSE);
1426 GST_OBJECT_LOCK (object);
1427 if ((binding = gst_object_find_control_binding (object, property_name))) {
1428 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1431 GST_OBJECT_UNLOCK (object);
1436 * gst_object_get_g_value_array:
1437 * @object: the object that has controlled properties
1438 * @property_name: the name of the property to get
1439 * @timestamp: the time that should be processed
1440 * @interval: the time spacing between subsequent values
1441 * @n_values: the number of values
1442 * @values: (array length=n_values): array to put control-values in
1444 * Gets a number of #GValues for the given controlled property starting at the
1445 * requested time. The array @values need to hold enough space for @n_values of
1448 * This function is useful if one wants to e.g. draw a graph of the control
1449 * curve or apply a control curve sample by sample.
1451 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1454 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1455 GstClockTime timestamp, GstClockTime interval, guint n_values,
1458 gboolean res = FALSE;
1459 GstControlBinding *binding;
1461 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1462 g_return_val_if_fail (property_name, FALSE);
1463 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1464 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1465 g_return_val_if_fail (values, FALSE);
1467 GST_OBJECT_LOCK (object);
1468 if ((binding = gst_object_find_control_binding (object, property_name))) {
1469 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1472 GST_OBJECT_UNLOCK (object);
1478 * gst_object_get_control_rate:
1479 * @object: the object that has controlled properties
1481 * Obtain the control-rate for this @object. Audio processing #GstElement
1482 * objects will use this rate to sub-divide their processing loop and call
1483 * gst_object_sync_values() in between. The length of the processing segment
1484 * should be up to @control-rate nanoseconds.
1486 * If the @object is not under property control, this will return
1487 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1489 * The control-rate is not expected to change if the element is in
1490 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1492 * Returns: the control rate in nanoseconds
1495 gst_object_get_control_rate (GstObject * object)
1497 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1499 return object->control_rate;
1503 * gst_object_set_control_rate:
1504 * @object: the object that has controlled properties
1505 * @control_rate: the new control-rate in nanoseconds.
1507 * Change the control-rate for this @object. Audio processing #GstElement
1508 * objects will use this rate to sub-divide their processing loop and call
1509 * gst_object_sync_values() in between. The length of the processing segment
1510 * should be up to @control-rate nanoseconds.
1512 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1513 * %GST_STATE_PLAYING.
1516 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1518 g_return_if_fail (GST_IS_OBJECT (object));
1520 object->control_rate = control_rate;