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);
164 * The parent of the object. Please note, that when changing the 'parent'
165 * property, we don't emit #GObject::notify and #GstObject::deep-notify
166 * signals due to locking issues. In some cases one can use
167 * #GstBin::element-added or #GstBin::element-removed signals on the parent to
168 * achieve a similar effect.
170 properties[PROP_PARENT] =
171 g_param_spec_object ("parent", "Parent", "The parent of the object",
172 GST_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
173 #ifdef TIZEN_PROFILE_TV
174 properties[PROP_FAMILY_ID] =
175 g_param_spec_int ("family-id", "Family Id",
176 "The family id of this object group, usually inherit from parent.",
177 -G_MAXINT, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
179 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
182 * GstObject::deep-notify:
183 * @gstobject: a #GstObject
184 * @prop_object: the object that originated the signal
185 * @prop: the property that changed
187 * The deep notify signal is used to be notified of property changes. It is
188 * typically attached to the toplevel bin to receive notifications from all
189 * the elements contained in that bin.
191 gst_object_signals[DEEP_NOTIFY] =
192 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
194 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
195 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
198 klass->path_string_separator = "/";
200 /* see the comments at gst_object_dispatch_properties_changed */
201 gobject_class->dispatch_properties_changed
202 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
204 gobject_class->constructed = gst_object_constructed;
205 gobject_class->dispose = gst_object_dispose;
206 gobject_class->finalize = gst_object_finalize;
210 gst_object_init (GstObject * object)
212 g_mutex_init (&object->lock);
213 object->parent = NULL;
215 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
218 #ifdef TIZEN_PROFILE_TV
219 object->family_id = 0;
221 object->control_rate = 100 * GST_MSECOND;
222 object->last_sync = GST_CLOCK_TIME_NONE;
227 * @object: (type Gst.Object): a #GstObject to reference
229 * Increments the reference count on @object. This function
230 * does not take the lock on @object because it relies on
231 * atomic refcounting.
233 * This object returns the input parameter to ease writing
235 * result = gst_object_ref (object->parent);
237 * Returns: (transfer full) (type Gst.Object): A pointer to @object
240 gst_object_ref (gpointer object)
242 g_return_val_if_fail (object != NULL, NULL);
244 GST_TRACER_OBJECT_REFFED (object, ((GObject *) object)->ref_count + 1);
245 #ifdef DEBUG_REFCOUNT
246 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
247 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
249 g_object_ref (object);
256 * @object: (type Gst.Object): a #GstObject to unreference
258 * Decrements the reference count on @object. If reference count hits
259 * zero, destroy @object. This function does not take the lock
260 * on @object as it relies on atomic refcounting.
262 * The unref method should never be called with the LOCK held since
263 * this might deadlock the dispose function.
266 gst_object_unref (gpointer object)
268 g_return_if_fail (object != NULL);
269 g_return_if_fail (((GObject *) object)->ref_count > 0);
271 GST_TRACER_OBJECT_UNREFFED (object, ((GObject *) object)->ref_count - 1);
272 #ifdef DEBUG_REFCOUNT
273 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
274 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
276 g_object_unref (object);
280 * gst_object_ref_sink: (skip)
281 * @object: a #GstObject to sink
283 * Increase the reference count of @object, and possibly remove the floating
284 * reference, if @object has a floating reference.
286 * In other words, if the object is floating, then this call "assumes ownership"
287 * of the floating reference, converting it to a normal reference by clearing
288 * the floating flag while leaving the reference count unchanged. If the object
289 * is not floating, then this call adds a new normal reference increasing the
290 * reference count by one.
292 * For more background on "floating references" please see the #GObject
296 gst_object_ref_sink (gpointer object)
298 g_return_val_if_fail (object != NULL, NULL);
300 #ifdef DEBUG_REFCOUNT
301 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
302 object, ((GObject *) object)->ref_count,
303 ((GObject *) object)->ref_count + 1);
305 return g_object_ref_sink (object);
309 * gst_clear_object: (skip)
310 * @object_ptr: a pointer to a #GstObject reference
312 * Clears a reference to a #GstObject.
314 * @object_ptr must not be %NULL.
316 * If the reference is %NULL then this function does nothing.
317 * Otherwise, the reference count of the object is decreased using
318 * gst_object_unref() and the pointer is set to %NULL.
320 * A macro is also included that allows this function to be used without
325 #undef gst_clear_object
327 gst_clear_object (GstObject ** object_ptr)
329 g_clear_pointer (object_ptr, gst_object_unref);
333 * gst_object_replace:
334 * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
335 * a #GstObject to replace
336 * @newobj: (transfer none) (allow-none): a new #GstObject
338 * Atomically modifies a pointer to point to a new object.
339 * The reference count of @oldobj is decreased and the reference count of
340 * @newobj is increased.
342 * Either @newobj and the value pointed to by @oldobj may be %NULL.
344 * Returns: %TRUE if @newobj was different from @oldobj
347 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
351 g_return_val_if_fail (oldobj != NULL, FALSE);
353 #ifdef DEBUG_REFCOUNT
354 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
355 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
356 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
357 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
358 newobj ? G_OBJECT (newobj)->ref_count : 0);
361 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
363 if (G_UNLIKELY (oldptr == newobj))
367 gst_object_ref (newobj);
369 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
370 oldobj, oldptr, newobj))) {
371 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
372 if (G_UNLIKELY (oldptr == newobj))
377 gst_object_unref (oldptr);
379 return oldptr != newobj;
382 /* dispose is called when the object has to release all links
383 * to other objects */
385 gst_object_dispose (GObject * object)
387 GstObject *self = (GstObject *) object;
390 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
392 GST_OBJECT_LOCK (object);
393 if ((parent = GST_OBJECT_PARENT (object)))
395 GST_OBJECT_PARENT (object) = NULL;
396 GST_OBJECT_UNLOCK (object);
398 if (self->control_bindings) {
401 for (node = self->control_bindings; node; node = g_list_next (node)) {
402 gst_object_unparent (node->data);
404 g_list_free (self->control_bindings);
405 self->control_bindings = NULL;
408 ((GObjectClass *) gst_object_parent_class)->dispose (object);
415 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
416 "parent \"%s\".\nYou need to let the parent manage the "
417 "object instead of unreffing the object directly.\n",
418 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
419 GST_OBJECT_UNLOCK (object);
420 /* ref the object again to revive it in this error case */
421 gst_object_ref (object);
426 /* finalize is called when the object has to free its resources */
428 gst_object_finalize (GObject * object)
430 GstObject *gstobject = GST_OBJECT_CAST (object);
432 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
434 g_signal_handlers_destroy (object);
436 g_free (gstobject->name);
437 g_mutex_clear (&gstobject->lock);
439 GST_TRACER_OBJECT_DESTROYED (gstobject);
441 ((GObjectClass *) gst_object_parent_class)->finalize (object);
444 /* Changing a GObject property of a GstObject will result in "deep-notify"
445 * signals being emitted by the object itself, as well as in each parent
446 * object. This is so that an application can connect a listener to the
447 * top-level bin to catch property-change notifications for all contained
453 gst_object_dispatch_properties_changed (GObject * object,
454 guint n_pspecs, GParamSpec ** pspecs)
456 GstObject *gst_object, *parent, *old_parent;
458 #ifndef GST_DISABLE_GST_DEBUG
460 const gchar *debug_name;
463 /* do the standard dispatching */
465 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
468 gst_object = GST_OBJECT_CAST (object);
469 #ifndef GST_DISABLE_GST_DEBUG
470 if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
471 name = gst_object_get_name (gst_object);
472 debug_name = GST_STR_NULL (name);
477 /* now let the parent dispatch those, too */
478 parent = gst_object_get_parent (gst_object);
480 for (i = 0; i < n_pspecs; i++) {
481 GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
482 "deep notification from %s (%s)", debug_name, pspecs[i]->name);
484 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
485 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
489 parent = gst_object_get_parent (old_parent);
490 gst_object_unref (old_parent);
492 #ifndef GST_DISABLE_GST_DEBUG
498 * gst_object_default_deep_notify:
499 * @object: the #GObject that signalled the notify.
500 * @orig: a #GstObject that initiated the notify.
501 * @pspec: a #GParamSpec of the property.
502 * @excluded_props: (array zero-terminated=1) (element-type gchar*) (allow-none):
503 * a set of user-specified properties to exclude or %NULL to show
506 * A default deep_notify signal callback for an object. The user data
507 * should contain a pointer to an array of strings that should be excluded
508 * from the notify. The default handler will print the new value of the property
511 * MT safe. This function grabs and releases @object's LOCK for getting its
515 gst_object_default_deep_notify (GObject * object, GstObject * orig,
516 GParamSpec * pspec, gchar ** excluded_props)
518 GValue value = { 0, }; /* the important thing is that value.type = 0 */
522 if (pspec->flags & G_PARAM_READABLE) {
523 /* let's not print these out for excluded properties... */
524 while (excluded_props != NULL && *excluded_props != NULL) {
525 if (strcmp (pspec->name, *excluded_props) == 0)
529 g_value_init (&value, pspec->value_type);
530 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
532 if (G_VALUE_HOLDS_STRING (&value))
533 str = g_value_dup_string (&value);
535 str = gst_value_serialize (&value);
536 name = gst_object_get_path_string (orig);
537 g_print ("%s: %s = %s\n", name, pspec->name, str);
540 g_value_unset (&value);
542 name = gst_object_get_path_string (orig);
543 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
549 gst_object_set_name_default (GstObject * object)
551 const gchar *type_name;
557 /* to ensure guaranteed uniqueness across threads, only one thread
558 * may ever assign a name */
559 G_LOCK (object_name_mutex);
561 if (!object_name_counts) {
562 g_datalist_init (&object_name_counts);
565 q = g_type_qname (G_OBJECT_TYPE (object));
566 count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
567 g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
569 G_UNLOCK (object_name_mutex);
571 /* GstFooSink -> foosink<N> */
572 type_name = g_quark_to_string (q);
573 if (strncmp (type_name, "Gst", 3) == 0)
575 /* give the 20th "queue" element and the first "queue2" different names */
576 l = strlen (type_name);
577 if (l > 0 && g_ascii_isdigit (type_name[l - 1])) {
578 name = g_strdup_printf ("%s-%d", type_name, count);
580 name = g_strdup_printf ("%s%d", type_name, count);
584 for (i = 0; i < l; i++)
585 name[i] = g_ascii_tolower (name[i]);
587 GST_OBJECT_LOCK (object);
588 if (G_UNLIKELY (object->parent != NULL))
591 g_free (object->name);
594 GST_OBJECT_UNLOCK (object);
601 GST_WARNING ("parented objects can't be renamed");
602 GST_OBJECT_UNLOCK (object);
608 * gst_object_set_name:
609 * @object: a #GstObject
610 * @name: (allow-none): new name of object
612 * Sets the name of @object, or gives @object a guaranteed unique
613 * name (if @name is %NULL).
614 * This function makes a copy of the provided name, so the caller
615 * retains ownership of the name it sent.
617 * Returns: %TRUE if the name could be set. Since Objects that have
618 * a parent cannot be renamed, this function returns %FALSE in those
621 * MT safe. This function grabs and releases @object's LOCK.
624 gst_object_set_name (GstObject * object, const gchar * name)
628 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
630 GST_OBJECT_LOCK (object);
632 /* parented objects cannot be renamed */
633 if (G_UNLIKELY (object->parent != NULL))
637 g_free (object->name);
638 object->name = g_strdup (name);
639 GST_OBJECT_UNLOCK (object);
642 GST_OBJECT_UNLOCK (object);
643 result = gst_object_set_name_default (object);
646 g_object_notify (G_OBJECT (object), "name");
652 GST_WARNING ("parented objects can't be renamed");
653 GST_OBJECT_UNLOCK (object);
659 * gst_object_get_name:
660 * @object: a #GstObject
662 * Returns a copy of the name of @object.
663 * Caller should g_free() the return value after usage.
664 * For a nameless object, this returns %NULL, which you can safely g_free()
667 * Free-function: g_free
669 * Returns: (transfer full) (nullable): the name of @object. g_free()
672 * MT safe. This function grabs and releases @object's LOCK.
675 gst_object_get_name (GstObject * object)
677 gchar *result = NULL;
679 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
681 GST_OBJECT_LOCK (object);
682 result = g_strdup (object->name);
683 GST_OBJECT_UNLOCK (object);
689 * gst_object_set_parent:
690 * @object: (transfer floating): a #GstObject
691 * @parent: new parent of object
693 * Sets the parent of @object to @parent. The object's reference count will
694 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
696 * Returns: %TRUE if @parent could be set or %FALSE when @object
697 * already had a parent or @object and @parent are the same.
699 * MT safe. Grabs and releases @object's LOCK.
702 gst_object_set_parent (GstObject * object, GstObject * parent)
704 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
705 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
706 g_return_val_if_fail (object != parent, FALSE);
708 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
709 "set parent (ref and sink)");
711 GST_OBJECT_LOCK (object);
712 if (G_UNLIKELY (object->parent != NULL))
715 object->parent = parent;
716 gst_object_ref_sink (object);
717 GST_OBJECT_UNLOCK (object);
719 /* FIXME-2.0: this does not work, the deep notify takes the lock from the
720 * parent object and deadlocks when the parent holds its lock when calling
721 * this function (like _element_add_pad()), we need to use a GRecMutex
722 * for locking the parent instead.
724 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
731 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
732 "set parent failed, object already had a parent");
733 gst_object_ref_sink (object);
734 gst_object_unref (object);
735 GST_OBJECT_UNLOCK (object);
741 * gst_object_get_parent:
742 * @object: a #GstObject
744 * Returns the parent of @object. This function increases the refcount
745 * of the parent object so you should gst_object_unref() it after usage.
747 * Returns: (transfer full) (nullable): parent of @object, this can be
748 * %NULL if @object has no parent. unref after usage.
750 * MT safe. Grabs and releases @object's LOCK.
753 gst_object_get_parent (GstObject * object)
755 GstObject *result = NULL;
757 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
759 GST_OBJECT_LOCK (object);
760 result = object->parent;
761 if (G_LIKELY (result))
762 gst_object_ref (result);
763 GST_OBJECT_UNLOCK (object);
769 * gst_object_unparent:
770 * @object: a #GstObject to unparent
772 * Clear the parent of @object, removing the associated reference.
773 * This function decreases the refcount of @object.
775 * MT safe. Grabs and releases @object's lock.
778 gst_object_unparent (GstObject * object)
782 g_return_if_fail (GST_IS_OBJECT (object));
784 GST_OBJECT_LOCK (object);
785 parent = object->parent;
787 if (G_LIKELY (parent != NULL)) {
788 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
789 object->parent = NULL;
790 GST_OBJECT_UNLOCK (object);
792 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
794 gst_object_unref (object);
796 GST_OBJECT_UNLOCK (object);
801 * gst_object_has_as_parent:
802 * @object: a #GstObject to check
803 * @parent: a #GstObject to check as parent
805 * Check if @parent is the parent of @object.
806 * E.g. a #GstElement can check if it owns a given #GstPad.
808 * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
809 * the parent of @object. Otherwise %FALSE.
811 * MT safe. Grabs and releases @object's locks.
815 gst_object_has_as_parent (GstObject * object, GstObject * parent)
817 gboolean result = FALSE;
819 if (G_LIKELY (GST_IS_OBJECT (object) && GST_IS_OBJECT (parent))) {
820 GST_OBJECT_LOCK (object);
821 result = GST_OBJECT_PARENT (object) == parent;
822 GST_OBJECT_UNLOCK (object);
829 * gst_object_has_as_ancestor:
830 * @object: a #GstObject to check
831 * @ancestor: a #GstObject to check as ancestor
833 * Check if @object has an ancestor @ancestor somewhere up in
834 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
836 * Returns: %TRUE if @ancestor is an ancestor of @object.
838 * MT safe. Grabs and releases @object's locks.
841 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
843 GstObject *parent, *tmp;
845 if (!ancestor || !object)
848 parent = gst_object_ref (object);
850 if (parent == ancestor) {
851 gst_object_unref (parent);
855 tmp = gst_object_get_parent (parent);
856 gst_object_unref (parent);
864 * gst_object_has_ancestor:
865 * @object: a #GstObject to check
866 * @ancestor: a #GstObject to check as ancestor
868 * Check if @object has an ancestor @ancestor somewhere up in
869 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
871 * Returns: %TRUE if @ancestor is an ancestor of @object.
873 * Deprecated: Use gst_object_has_as_ancestor() instead.
875 * MT safe. Grabs and releases @object's locks.
877 #ifndef GST_REMOVE_DEPRECATED
879 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
881 return gst_object_has_as_ancestor (object, ancestor);
886 * gst_object_check_uniqueness:
887 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
889 * @name: the name to search for
891 * Checks to see if there is any object named @name in @list. This function
892 * does not do any locking of any kind. You might want to protect the
893 * provided list with the lock of the owner of the list. This function
894 * will lock each #GstObject in the list to compare the name, so be
895 * careful when passing a list with a locked object.
897 * Returns: %TRUE if a #GstObject named @name does not appear in @list,
900 * MT safe. Grabs and releases the LOCK of each object in the list.
903 gst_object_check_uniqueness (GList * list, const gchar * name)
905 gboolean result = TRUE;
907 g_return_val_if_fail (name != NULL, FALSE);
909 for (; list; list = g_list_next (list)) {
913 child = GST_OBJECT_CAST (list->data);
915 GST_OBJECT_LOCK (child);
916 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
917 GST_OBJECT_UNLOCK (child);
919 if (G_UNLIKELY (eq)) {
929 gst_object_set_property (GObject * object, guint prop_id,
930 const GValue * value, GParamSpec * pspec)
932 GstObject *gstobject;
934 gstobject = GST_OBJECT_CAST (object);
938 gst_object_set_name (gstobject, g_value_get_string (value));
941 gst_object_set_parent (gstobject, g_value_get_object (value));
943 #ifdef TIZEN_PROFILE_TV
945 g_atomic_int_set (&gstobject->family_id, g_value_get_int (value));
949 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
955 gst_object_get_property (GObject * object, guint prop_id,
956 GValue * value, GParamSpec * pspec)
958 GstObject *gstobject;
960 gstobject = GST_OBJECT_CAST (object);
964 g_value_take_string (value, gst_object_get_name (gstobject));
967 g_value_take_object (value, gst_object_get_parent (gstobject));
969 #ifdef TIZEN_PROFILE_TV
971 g_value_set_int (value, g_atomic_int_get (&gstobject->family_id));
975 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
981 * gst_object_get_path_string:
982 * @object: a #GstObject
984 * Generates a string describing the path of @object in
985 * the object hierarchy. Only useful (or used) for debugging.
987 * Free-function: g_free
989 * Returns: (transfer full): a string describing the path of @object. You must
990 * g_free() the string after usage.
992 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
996 gst_object_get_path_string (GstObject * object)
1001 gchar *prevpath, *path;
1002 const gchar *typename;
1004 const gchar *separator;
1006 /* ref object before adding to list */
1007 gst_object_ref (object);
1008 parentage = g_slist_prepend (NULL, object);
1010 path = g_strdup ("");
1012 /* first walk the object hierarchy to build a list of the parents,
1013 * be careful here with refcounting. */
1015 if (GST_IS_OBJECT (object)) {
1016 parent = gst_object_get_parent (object);
1017 /* add parents to list, refcount remains increased while
1018 * we handle the object */
1020 parentage = g_slist_prepend (parentage, parent);
1025 } while (object != NULL);
1027 /* then walk the parent list and print them out. we need to
1028 * decrease the refcounting on each element after we handled
1030 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1031 if (G_IS_OBJECT (parents->data)) {
1032 typename = G_OBJECT_TYPE_NAME (parents->data);
1036 if (GST_IS_OBJECT (parents->data)) {
1037 GstObject *item = GST_OBJECT_CAST (parents->data);
1038 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1039 gchar *objname = gst_object_get_name (item);
1041 component = g_strdup_printf ("%s:%s", typename, objname);
1042 separator = oclass->path_string_separator;
1044 gst_object_unref (item);
1048 component = g_strdup_printf ("%s:%p", typename, parents->data);
1050 component = g_strdup_printf ("%p", parents->data);
1056 path = g_strjoin (separator, prevpath, component, NULL);
1061 g_slist_free (parentage);
1066 /* controller helper functions */
1069 * gst_object_find_control_binding:
1070 * @self: the gobject to search for a property in
1071 * @name: the gobject property name to look for
1073 * Searches the list of properties under control.
1075 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1076 * is not being controlled.
1078 static GstControlBinding *
1079 gst_object_find_control_binding (GstObject * self, const gchar * name)
1081 GstControlBinding *binding;
1084 for (node = self->control_bindings; node; node = g_list_next (node)) {
1085 binding = node->data;
1086 /* FIXME: eventually use GQuark to speed it up */
1087 if (!strcmp (binding->name, name)) {
1088 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1092 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1097 /* controller functions */
1100 * gst_object_suggest_next_sync:
1101 * @object: the object that has controlled properties
1103 * Returns a suggestion for timestamps where buffers should be split
1104 * to get best controller results.
1106 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1107 * if no control-rate was set.
1110 gst_object_suggest_next_sync (GstObject * object)
1114 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1115 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1116 GST_CLOCK_TIME_NONE);
1118 GST_OBJECT_LOCK (object);
1120 /* TODO: Implement more logic, depending on interpolation mode and control
1122 * FIXME: we need playback direction
1124 ret = object->last_sync + object->control_rate;
1126 GST_OBJECT_UNLOCK (object);
1132 * gst_object_sync_values:
1133 * @object: the object that has controlled properties
1134 * @timestamp: the time that should be processed
1136 * Sets the properties of the object, according to the #GstControlSources that
1137 * (maybe) handle them and for the given timestamp.
1139 * If this function fails, it is most likely the application developers fault.
1140 * Most probably the control sources are not setup correctly.
1142 * Returns: %TRUE if the controller values could be applied to the object
1143 * properties, %FALSE otherwise
1146 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1149 gboolean ret = TRUE;
1151 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1152 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1154 GST_LOG_OBJECT (object, "sync_values");
1155 if (!object->control_bindings)
1158 /* FIXME: this deadlocks */
1159 /* GST_OBJECT_LOCK (object); */
1160 g_object_freeze_notify ((GObject *) object);
1161 for (node = object->control_bindings; node; node = g_list_next (node)) {
1162 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1163 object, timestamp, object->last_sync);
1165 object->last_sync = timestamp;
1166 g_object_thaw_notify ((GObject *) object);
1167 /* GST_OBJECT_UNLOCK (object); */
1174 * gst_object_has_active_control_bindings:
1175 * @object: the object that has controlled properties
1177 * Check if the @object has active controlled properties.
1179 * Returns: %TRUE if the object has active controlled properties
1182 gst_object_has_active_control_bindings (GstObject * object)
1184 gboolean res = FALSE;
1187 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1189 GST_OBJECT_LOCK (object);
1190 for (node = object->control_bindings; node; node = g_list_next (node)) {
1191 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1193 GST_OBJECT_UNLOCK (object);
1198 * gst_object_set_control_bindings_disabled:
1199 * @object: the object that has controlled properties
1200 * @disabled: boolean that specifies whether to disable the controller
1203 * This function is used to disable all controlled properties of the @object for
1204 * some time, i.e. gst_object_sync_values() will do nothing.
1207 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1211 g_return_if_fail (GST_IS_OBJECT (object));
1213 GST_OBJECT_LOCK (object);
1214 for (node = object->control_bindings; node; node = g_list_next (node)) {
1215 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1218 GST_OBJECT_UNLOCK (object);
1222 * gst_object_set_control_binding_disabled:
1223 * @object: the object that has controlled properties
1224 * @property_name: property to disable
1225 * @disabled: boolean that specifies whether to disable the controller
1228 * This function is used to disable the control bindings on a property for
1229 * some time, i.e. gst_object_sync_values() will do nothing for the
1233 gst_object_set_control_binding_disabled (GstObject * object,
1234 const gchar * property_name, gboolean disabled)
1236 GstControlBinding *binding;
1238 g_return_if_fail (GST_IS_OBJECT (object));
1239 g_return_if_fail (property_name);
1241 GST_OBJECT_LOCK (object);
1242 if ((binding = gst_object_find_control_binding (object, property_name))) {
1243 gst_control_binding_set_disabled (binding, disabled);
1245 GST_OBJECT_UNLOCK (object);
1250 * gst_object_add_control_binding:
1251 * @object: the controller object
1252 * @binding: (transfer floating): the #GstControlBinding that should be used
1254 * Attach the #GstControlBinding to the object. If there already was a
1255 * #GstControlBinding for this property it will be replaced.
1257 * The object's reference count will be incremented, and any floating
1258 * reference will be removed (see gst_object_ref_sink())
1260 * Returns: %FALSE if the given @binding has not been setup for this object or
1261 * has been setup for a non suitable property, %TRUE otherwise.
1264 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1266 GstControlBinding *old;
1268 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1269 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1270 g_return_val_if_fail (binding->pspec, FALSE);
1272 GST_OBJECT_LOCK (object);
1273 if ((old = gst_object_find_control_binding (object, binding->name))) {
1274 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1275 object->control_bindings = g_list_remove (object->control_bindings, old);
1276 gst_object_unparent (GST_OBJECT_CAST (old));
1278 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1279 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1280 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1281 GST_OBJECT_UNLOCK (object);
1287 * gst_object_get_control_binding:
1288 * @object: the object
1289 * @property_name: name of the property
1291 * Gets the corresponding #GstControlBinding for the property. This should be
1292 * unreferenced again after use.
1294 * Returns: (transfer full) (nullable): the #GstControlBinding for
1295 * @property_name or %NULL if the property is not controlled.
1298 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1300 GstControlBinding *binding;
1302 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1303 g_return_val_if_fail (property_name, NULL);
1305 GST_OBJECT_LOCK (object);
1306 if ((binding = gst_object_find_control_binding (object, property_name))) {
1307 gst_object_ref (binding);
1309 GST_OBJECT_UNLOCK (object);
1315 * gst_object_remove_control_binding:
1316 * @object: the object
1317 * @binding: the binding
1319 * Removes the corresponding #GstControlBinding. If it was the
1320 * last ref of the binding, it will be disposed.
1322 * Returns: %TRUE if the binding could be removed.
1325 gst_object_remove_control_binding (GstObject * object,
1326 GstControlBinding * binding)
1329 gboolean ret = FALSE;
1331 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1332 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1334 GST_OBJECT_LOCK (object);
1335 if ((node = g_list_find (object->control_bindings, binding))) {
1336 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1337 object->control_bindings =
1338 g_list_delete_link (object->control_bindings, node);
1339 gst_object_unparent (GST_OBJECT_CAST (binding));
1342 GST_OBJECT_UNLOCK (object);
1348 * gst_object_get_value:
1349 * @object: the object that has controlled properties
1350 * @property_name: the name of the property to get
1351 * @timestamp: the time the control-change should be read from
1353 * Gets the value for the given controlled property at the requested time.
1355 * Returns: (nullable): the GValue of the property at the given time,
1356 * or %NULL if the property isn't controlled.
1359 gst_object_get_value (GstObject * object, const gchar * property_name,
1360 GstClockTime timestamp)
1362 GstControlBinding *binding;
1365 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1366 g_return_val_if_fail (property_name, NULL);
1367 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1369 GST_OBJECT_LOCK (object);
1370 if ((binding = gst_object_find_control_binding (object, property_name))) {
1371 val = gst_control_binding_get_value (binding, timestamp);
1373 GST_OBJECT_UNLOCK (object);
1379 * gst_object_get_value_array: (skip)
1380 * @object: the object that has controlled properties
1381 * @property_name: the name of the property to get
1382 * @timestamp: the time that should be processed
1383 * @interval: the time spacing between subsequent values
1384 * @n_values: the number of values
1385 * @values: array to put control-values in
1387 * Gets a number of values for the given controlled property starting at the
1388 * requested time. The array @values need to hold enough space for @n_values of
1389 * the same type as the objects property's type.
1391 * This function is useful if one wants to e.g. draw a graph of the control
1392 * curve or apply a control curve sample by sample.
1394 * The values are unboxed and ready to be used. The similar function
1395 * gst_object_get_g_value_array() returns the array as #GValues and is
1396 * better suites for bindings.
1398 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1401 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1402 GstClockTime timestamp, GstClockTime interval, guint n_values,
1405 gboolean res = FALSE;
1406 GstControlBinding *binding;
1408 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1409 g_return_val_if_fail (property_name, FALSE);
1410 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1411 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1412 g_return_val_if_fail (values, FALSE);
1414 GST_OBJECT_LOCK (object);
1415 if ((binding = gst_object_find_control_binding (object, property_name))) {
1416 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1419 GST_OBJECT_UNLOCK (object);
1424 * gst_object_get_g_value_array:
1425 * @object: the object that has controlled properties
1426 * @property_name: the name of the property to get
1427 * @timestamp: the time that should be processed
1428 * @interval: the time spacing between subsequent values
1429 * @n_values: the number of values
1430 * @values: (array length=n_values): array to put control-values in
1432 * Gets a number of #GValues for the given controlled property starting at the
1433 * requested time. The array @values need to hold enough space for @n_values of
1436 * This function is useful if one wants to e.g. draw a graph of the control
1437 * curve or apply a control curve sample by sample.
1439 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1442 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1443 GstClockTime timestamp, GstClockTime interval, guint n_values,
1446 gboolean res = FALSE;
1447 GstControlBinding *binding;
1449 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1450 g_return_val_if_fail (property_name, FALSE);
1451 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1452 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1453 g_return_val_if_fail (values, FALSE);
1455 GST_OBJECT_LOCK (object);
1456 if ((binding = gst_object_find_control_binding (object, property_name))) {
1457 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1460 GST_OBJECT_UNLOCK (object);
1466 * gst_object_get_control_rate:
1467 * @object: the object that has controlled properties
1469 * Obtain the control-rate for this @object. Audio processing #GstElement
1470 * objects will use this rate to sub-divide their processing loop and call
1471 * gst_object_sync_values() in between. The length of the processing segment
1472 * should be up to @control-rate nanoseconds.
1474 * If the @object is not under property control, this will return
1475 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1477 * The control-rate is not expected to change if the element is in
1478 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1480 * Returns: the control rate in nanoseconds
1483 gst_object_get_control_rate (GstObject * object)
1485 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1487 return object->control_rate;
1491 * gst_object_set_control_rate:
1492 * @object: the object that has controlled properties
1493 * @control_rate: the new control-rate in nanoseconds.
1495 * Change the control-rate for this @object. Audio processing #GstElement
1496 * objects will use this rate to sub-divide their processing loop and call
1497 * gst_object_sync_values() in between. The length of the processing segment
1498 * should be up to @control-rate nanoseconds.
1500 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1501 * %GST_STATE_PLAYING.
1504 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1506 g_return_if_fail (GST_IS_OBJECT (object));
1508 object->control_rate = control_rate;