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 */
114 /* maps type name quark => count */
115 static GData *object_name_counts = NULL;
117 G_LOCK_DEFINE_STATIC (object_name_mutex);
119 static void gst_object_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_object_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static void gst_object_dispatch_properties_changed (GObject * object,
125 guint n_pspecs, GParamSpec ** pspecs);
127 static void gst_object_dispose (GObject * object);
128 static void gst_object_finalize (GObject * object);
130 static gboolean gst_object_set_name_default (GstObject * object);
132 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
134 static GParamSpec *properties[PROP_LAST];
136 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
139 gst_object_constructed (GObject * object)
141 GST_TRACER_OBJECT_CREATED (GST_OBJECT_CAST (object));
143 ((GObjectClass *) gst_object_parent_class)->constructed (object);
147 gst_object_class_init (GstObjectClass * klass)
149 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
151 gobject_class->set_property = gst_object_set_property;
152 gobject_class->get_property = gst_object_get_property;
154 properties[PROP_NAME] =
155 g_param_spec_string ("name", "Name", "The name of the object", NULL,
156 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS |
157 GST_PARAM_DOC_SHOW_DEFAULT);
162 * The parent of the object. Please note, that when changing the 'parent'
163 * property, we don't emit #GObject::notify and #GstObject::deep-notify
164 * signals due to locking issues. In some cases one can use
165 * #GstBin::element-added or #GstBin::element-removed signals on the parent to
166 * achieve a similar effect.
168 properties[PROP_PARENT] =
169 g_param_spec_object ("parent", "Parent", "The parent of the object",
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_DOC_SHOW_DEFAULT);
173 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
176 * GstObject::deep-notify:
177 * @gstobject: a #GstObject
178 * @prop_object: the object that originated the signal
179 * @prop: the property that changed
181 * The deep notify signal is used to be notified of property changes. It is
182 * typically attached to the toplevel bin to receive notifications from all
183 * the elements contained in that bin.
185 gst_object_signals[DEEP_NOTIFY] =
186 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
187 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
188 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
189 NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_OBJECT, G_TYPE_PARAM);
191 klass->path_string_separator = "/";
193 /* see the comments at gst_object_dispatch_properties_changed */
194 gobject_class->dispatch_properties_changed
195 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
197 gobject_class->constructed = gst_object_constructed;
198 gobject_class->dispose = gst_object_dispose;
199 gobject_class->finalize = gst_object_finalize;
203 gst_object_init (GstObject * object)
205 g_mutex_init (&object->lock);
206 object->parent = NULL;
208 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
212 object->control_rate = 100 * GST_MSECOND;
213 object->last_sync = GST_CLOCK_TIME_NONE;
218 * @object: (type Gst.Object): a #GstObject to reference
220 * Increments the reference count on @object. This function
221 * does not take the lock on @object because it relies on
222 * atomic refcounting.
224 * This object returns the input parameter to ease writing
226 * result = gst_object_ref (object->parent);
228 * Returns: (transfer full) (type Gst.Object): A pointer to @object
231 gst_object_ref (gpointer object)
233 g_return_val_if_fail (object != NULL, NULL);
235 GST_TRACER_OBJECT_REFFED (object, ((GObject *) object)->ref_count + 1);
236 #ifdef DEBUG_REFCOUNT
237 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
238 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
240 g_object_ref (object);
247 * @object: (type Gst.Object): a #GstObject to unreference
249 * Decrements the reference count on @object. If reference count hits
250 * zero, destroy @object. This function does not take the lock
251 * on @object as it relies on atomic refcounting.
253 * The unref method should never be called with the LOCK held since
254 * this might deadlock the dispose function.
257 gst_object_unref (gpointer object)
259 g_return_if_fail (object != NULL);
260 g_return_if_fail (((GObject *) object)->ref_count > 0);
262 GST_TRACER_OBJECT_UNREFFED (object, ((GObject *) object)->ref_count - 1);
263 #ifdef DEBUG_REFCOUNT
264 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
265 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
267 g_object_unref (object);
271 * gst_object_ref_sink: (skip)
272 * @object: a #GstObject to sink
274 * Increase the reference count of @object, and possibly remove the floating
275 * reference, if @object has a floating reference.
277 * In other words, if the object is floating, then this call "assumes ownership"
278 * of the floating reference, converting it to a normal reference by clearing
279 * the floating flag while leaving the reference count unchanged. If the object
280 * is not floating, then this call adds a new normal reference increasing the
281 * reference count by one.
283 * For more background on "floating references" please see the #GObject
287 gst_object_ref_sink (gpointer object)
289 g_return_val_if_fail (object != NULL, NULL);
291 #ifdef DEBUG_REFCOUNT
292 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
293 object, ((GObject *) object)->ref_count,
294 ((GObject *) object)->ref_count + 1);
296 return g_object_ref_sink (object);
300 * gst_clear_object: (skip)
301 * @object_ptr: a pointer to a #GstObject reference
303 * Clears a reference to a #GstObject.
305 * @object_ptr must not be %NULL.
307 * If the reference is %NULL then this function does nothing.
308 * Otherwise, the reference count of the object is decreased using
309 * gst_object_unref() and the pointer is set to %NULL.
311 * A macro is also included that allows this function to be used without
316 #undef gst_clear_object
318 gst_clear_object (GstObject ** object_ptr)
320 g_clear_pointer (object_ptr, gst_object_unref);
324 * gst_object_replace:
325 * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
326 * a #GstObject to replace
327 * @newobj: (transfer none) (nullable): a new #GstObject
329 * Atomically modifies a pointer to point to a new object.
330 * The reference count of @oldobj is decreased and the reference count of
331 * @newobj is increased.
333 * Either @newobj and the value pointed to by @oldobj may be %NULL.
335 * Returns: %TRUE if @newobj was different from @oldobj
338 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
342 g_return_val_if_fail (oldobj != NULL, FALSE);
344 #ifdef DEBUG_REFCOUNT
345 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
346 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
347 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
348 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
349 newobj ? G_OBJECT (newobj)->ref_count : 0);
352 oldptr = (GstObject *) g_atomic_pointer_get ((gpointer *) oldobj);
354 if (G_UNLIKELY (oldptr == newobj))
358 gst_object_ref (newobj);
360 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
361 oldobj, (gpointer) oldptr, newobj))) {
362 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
363 if (G_UNLIKELY (oldptr == newobj))
368 gst_object_unref (oldptr);
370 return oldptr != newobj;
373 /* dispose is called when the object has to release all links
374 * to other objects */
376 gst_object_dispose (GObject * object)
378 GstObject *self = (GstObject *) object;
381 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
383 GST_OBJECT_LOCK (object);
384 if ((parent = GST_OBJECT_PARENT (object)))
386 GST_OBJECT_PARENT (object) = NULL;
387 GST_OBJECT_UNLOCK (object);
389 if (self->control_bindings) {
392 for (node = self->control_bindings; node; node = g_list_next (node)) {
393 gst_object_unparent (node->data);
395 g_list_free (self->control_bindings);
396 self->control_bindings = NULL;
399 ((GObjectClass *) gst_object_parent_class)->dispose (object);
406 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
407 "parent \"%s\".\nYou need to let the parent manage the "
408 "object instead of unreffing the object directly.\n",
409 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
410 GST_OBJECT_UNLOCK (object);
411 /* ref the object again to revive it in this error case */
412 gst_object_ref (object);
417 /* finalize is called when the object has to free its resources */
419 gst_object_finalize (GObject * object)
421 GstObject *gstobject = GST_OBJECT_CAST (object);
423 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
425 g_signal_handlers_destroy (object);
427 g_free (gstobject->name);
428 g_mutex_clear (&gstobject->lock);
430 GST_TRACER_OBJECT_DESTROYED (gstobject);
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_intern (GstObject * object, const gchar * name)
603 GST_OBJECT_LOCK (object);
605 /* parented objects cannot be renamed */
606 if (G_UNLIKELY (object->parent != NULL))
610 g_free (object->name);
611 object->name = g_strdup (name);
612 GST_OBJECT_UNLOCK (object);
615 GST_OBJECT_UNLOCK (object);
616 result = gst_object_set_name_default (object);
624 GST_WARNING ("parented objects can't be renamed");
625 GST_OBJECT_UNLOCK (object);
631 * gst_object_set_name:
632 * @object: a #GstObject
633 * @name: (nullable): new name of object
635 * Sets the name of @object, or gives @object a guaranteed unique
636 * name (if @name is %NULL).
637 * This function makes a copy of the provided name, so the caller
638 * retains ownership of the name it sent.
640 * Returns: %TRUE if the name could be set. Since Objects that have
641 * a parent cannot be renamed, this function returns %FALSE in those
644 * MT safe. This function grabs and releases @object's LOCK.
647 gst_object_set_name (GstObject * object, const gchar * name)
651 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
653 if ((result = gst_object_set_name_intern (object, name)))
654 g_object_notify_by_pspec (G_OBJECT (object), properties[PROP_NAME]);
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_intern (gstobject, g_value_get_string (value));
941 gst_object_set_parent (gstobject, g_value_get_object (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));
965 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
971 * gst_object_get_path_string:
972 * @object: a #GstObject
974 * Generates a string describing the path of @object in
975 * the object hierarchy. Only useful (or used) for debugging.
977 * Free-function: g_free
979 * Returns: (transfer full): a string describing the path of @object. You must
980 * g_free() the string after usage.
982 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
986 gst_object_get_path_string (GstObject * object)
991 gchar *prevpath, *path;
992 const gchar *typename;
994 const gchar *separator;
996 /* ref object before adding to list */
997 gst_object_ref (object);
998 parentage = g_slist_prepend (NULL, object);
1000 path = g_strdup ("");
1002 /* first walk the object hierarchy to build a list of the parents,
1003 * be careful here with refcounting. */
1005 if (GST_IS_OBJECT (object)) {
1006 parent = gst_object_get_parent (object);
1007 /* add parents to list, refcount remains increased while
1008 * we handle the object */
1010 parentage = g_slist_prepend (parentage, parent);
1015 } while (object != NULL);
1017 /* then walk the parent list and print them out. we need to
1018 * decrease the refcounting on each element after we handled
1020 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1021 if (G_IS_OBJECT (parents->data)) {
1022 typename = G_OBJECT_TYPE_NAME (parents->data);
1026 if (GST_IS_OBJECT (parents->data)) {
1027 GstObject *item = GST_OBJECT_CAST (parents->data);
1028 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1029 gchar *objname = gst_object_get_name (item);
1031 component = g_strdup_printf ("%s:%s", typename, objname);
1032 separator = oclass->path_string_separator;
1034 gst_object_unref (item);
1038 component = g_strdup_printf ("%s:%p", typename, parents->data);
1040 component = g_strdup_printf ("%p", parents->data);
1046 path = g_strjoin (separator, prevpath, component, NULL);
1051 g_slist_free (parentage);
1056 /* controller helper functions */
1059 * gst_object_find_control_binding:
1060 * @self: the gobject to search for a property in
1061 * @name: the gobject property name to look for
1063 * Searches the list of properties under control.
1065 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1066 * is not being controlled.
1068 static GstControlBinding *
1069 gst_object_find_control_binding (GstObject * self, const gchar * name)
1071 GstControlBinding *binding;
1074 for (node = self->control_bindings; node; node = g_list_next (node)) {
1075 binding = node->data;
1076 /* FIXME: eventually use GQuark to speed it up */
1077 if (!strcmp (binding->name, name)) {
1078 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1082 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1087 /* controller functions */
1090 * gst_object_suggest_next_sync:
1091 * @object: the object that has controlled properties
1093 * Returns a suggestion for timestamps where buffers should be split
1094 * to get best controller results.
1096 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1097 * if no control-rate was set.
1100 gst_object_suggest_next_sync (GstObject * object)
1104 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1105 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1106 GST_CLOCK_TIME_NONE);
1108 GST_OBJECT_LOCK (object);
1110 /* TODO: Implement more logic, depending on interpolation mode and control
1112 * FIXME: we need playback direction
1114 ret = object->last_sync + object->control_rate;
1116 GST_OBJECT_UNLOCK (object);
1122 * gst_object_sync_values:
1123 * @object: the object that has controlled properties
1124 * @timestamp: the time that should be processed
1126 * Sets the properties of the object, according to the #GstControlSources that
1127 * (maybe) handle them and for the given timestamp.
1129 * If this function fails, it is most likely the application developers fault.
1130 * Most probably the control sources are not setup correctly.
1132 * Returns: %TRUE if the controller values could be applied to the object
1133 * properties, %FALSE otherwise
1136 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1139 gboolean ret = TRUE;
1141 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1142 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1144 GST_LOG_OBJECT (object, "sync_values");
1145 if (!object->control_bindings)
1148 /* FIXME: this deadlocks */
1149 /* GST_OBJECT_LOCK (object); */
1150 g_object_freeze_notify ((GObject *) object);
1151 for (node = object->control_bindings; node; node = g_list_next (node)) {
1152 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1153 object, timestamp, object->last_sync);
1155 object->last_sync = timestamp;
1156 g_object_thaw_notify ((GObject *) object);
1157 /* GST_OBJECT_UNLOCK (object); */
1164 * gst_object_has_active_control_bindings:
1165 * @object: the object that has controlled properties
1167 * Check if the @object has active controlled properties.
1169 * Returns: %TRUE if the object has active controlled properties
1172 gst_object_has_active_control_bindings (GstObject * object)
1174 gboolean res = FALSE;
1177 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1179 GST_OBJECT_LOCK (object);
1180 for (node = object->control_bindings; node; node = g_list_next (node)) {
1181 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1183 GST_OBJECT_UNLOCK (object);
1188 * gst_object_set_control_bindings_disabled:
1189 * @object: the object that has controlled properties
1190 * @disabled: boolean that specifies whether to disable the controller
1193 * This function is used to disable all controlled properties of the @object for
1194 * some time, i.e. gst_object_sync_values() will do nothing.
1197 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1201 g_return_if_fail (GST_IS_OBJECT (object));
1203 GST_OBJECT_LOCK (object);
1204 for (node = object->control_bindings; node; node = g_list_next (node)) {
1205 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1208 GST_OBJECT_UNLOCK (object);
1212 * gst_object_set_control_binding_disabled:
1213 * @object: the object that has controlled properties
1214 * @property_name: property to disable
1215 * @disabled: boolean that specifies whether to disable the controller
1218 * This function is used to disable the control bindings on a property for
1219 * some time, i.e. gst_object_sync_values() will do nothing for the
1223 gst_object_set_control_binding_disabled (GstObject * object,
1224 const gchar * property_name, gboolean disabled)
1226 GstControlBinding *binding;
1228 g_return_if_fail (GST_IS_OBJECT (object));
1229 g_return_if_fail (property_name);
1231 GST_OBJECT_LOCK (object);
1232 if ((binding = gst_object_find_control_binding (object, property_name))) {
1233 gst_control_binding_set_disabled (binding, disabled);
1235 GST_OBJECT_UNLOCK (object);
1240 * gst_object_add_control_binding:
1241 * @object: the controller object
1242 * @binding: (transfer floating): the #GstControlBinding that should be used
1244 * Attach the #GstControlBinding to the object. If there already was a
1245 * #GstControlBinding for this property it will be replaced.
1247 * The object's reference count will be incremented, and any floating
1248 * reference will be removed (see gst_object_ref_sink())
1250 * Returns: %FALSE if the given @binding has not been setup for this object or
1251 * has been setup for a non suitable property, %TRUE otherwise.
1254 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1256 GstControlBinding *old;
1258 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1259 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1260 g_return_val_if_fail (binding->pspec, FALSE);
1262 GST_OBJECT_LOCK (object);
1263 if ((old = gst_object_find_control_binding (object, binding->name))) {
1264 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1265 object->control_bindings = g_list_remove (object->control_bindings, old);
1266 gst_object_unparent (GST_OBJECT_CAST (old));
1268 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1269 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1270 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1271 GST_OBJECT_UNLOCK (object);
1277 * gst_object_get_control_binding:
1278 * @object: the object
1279 * @property_name: name of the property
1281 * Gets the corresponding #GstControlBinding for the property. This should be
1282 * unreferenced again after use.
1284 * Returns: (transfer full) (nullable): the #GstControlBinding for
1285 * @property_name or %NULL if the property is not controlled.
1288 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1290 GstControlBinding *binding;
1292 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1293 g_return_val_if_fail (property_name, NULL);
1295 GST_OBJECT_LOCK (object);
1296 if ((binding = gst_object_find_control_binding (object, property_name))) {
1297 gst_object_ref (binding);
1299 GST_OBJECT_UNLOCK (object);
1305 * gst_object_remove_control_binding:
1306 * @object: the object
1307 * @binding: the binding
1309 * Removes the corresponding #GstControlBinding. If it was the
1310 * last ref of the binding, it will be disposed.
1312 * Returns: %TRUE if the binding could be removed.
1315 gst_object_remove_control_binding (GstObject * object,
1316 GstControlBinding * binding)
1319 gboolean ret = FALSE;
1321 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1322 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1324 GST_OBJECT_LOCK (object);
1325 if ((node = g_list_find (object->control_bindings, binding))) {
1326 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1327 object->control_bindings =
1328 g_list_delete_link (object->control_bindings, node);
1329 gst_object_unparent (GST_OBJECT_CAST (binding));
1332 GST_OBJECT_UNLOCK (object);
1338 * gst_object_get_value:
1339 * @object: the object that has controlled properties
1340 * @property_name: the name of the property to get
1341 * @timestamp: the time the control-change should be read from
1343 * Gets the value for the given controlled property at the requested time.
1345 * Returns: (transfer full) (nullable): the GValue of the property at the given time,
1346 * or %NULL if the property isn't controlled.
1349 gst_object_get_value (GstObject * object, const gchar * property_name,
1350 GstClockTime timestamp)
1352 GstControlBinding *binding;
1355 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1356 g_return_val_if_fail (property_name, NULL);
1357 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1359 GST_OBJECT_LOCK (object);
1360 if ((binding = gst_object_find_control_binding (object, property_name))) {
1361 val = gst_control_binding_get_value (binding, timestamp);
1363 GST_OBJECT_UNLOCK (object);
1369 * gst_object_get_value_array: (skip)
1370 * @object: the object that has controlled properties
1371 * @property_name: the name of the property to get
1372 * @timestamp: the time that should be processed
1373 * @interval: the time spacing between subsequent values
1374 * @n_values: the number of values
1375 * @values: (array length=n_values): array to put control-values in
1377 * Gets a number of values for the given controlled property starting at the
1378 * requested time. The array @values need to hold enough space for @n_values of
1379 * the same type as the objects property's type.
1381 * This function is useful if one wants to e.g. draw a graph of the control
1382 * curve or apply a control curve sample by sample.
1384 * The values are unboxed and ready to be used. The similar function
1385 * gst_object_get_g_value_array() returns the array as #GValues and is
1386 * better suites for bindings.
1388 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1391 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1392 GstClockTime timestamp, GstClockTime interval, guint n_values,
1395 gboolean res = FALSE;
1396 GstControlBinding *binding;
1398 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1399 g_return_val_if_fail (property_name, FALSE);
1400 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1401 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1402 g_return_val_if_fail (values, FALSE);
1404 GST_OBJECT_LOCK (object);
1405 if ((binding = gst_object_find_control_binding (object, property_name))) {
1406 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1409 GST_OBJECT_UNLOCK (object);
1414 * gst_object_get_g_value_array:
1415 * @object: the object that has controlled properties
1416 * @property_name: the name of the property to get
1417 * @timestamp: the time that should be processed
1418 * @interval: the time spacing between subsequent values
1419 * @n_values: the number of values
1420 * @values: (array length=n_values): array to put control-values in
1422 * Gets a number of #GValues for the given controlled property starting at the
1423 * requested time. The array @values need to hold enough space for @n_values of
1426 * This function is useful if one wants to e.g. draw a graph of the control
1427 * curve or apply a control curve sample by sample.
1429 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1432 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1433 GstClockTime timestamp, GstClockTime interval, guint n_values,
1436 gboolean res = FALSE;
1437 GstControlBinding *binding;
1439 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1440 g_return_val_if_fail (property_name, FALSE);
1441 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1442 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1443 g_return_val_if_fail (values, FALSE);
1445 GST_OBJECT_LOCK (object);
1446 if ((binding = gst_object_find_control_binding (object, property_name))) {
1447 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1450 GST_OBJECT_UNLOCK (object);
1456 * gst_object_get_control_rate:
1457 * @object: the object that has controlled properties
1459 * Obtain the control-rate for this @object. Audio processing #GstElement
1460 * objects will use this rate to sub-divide their processing loop and call
1461 * gst_object_sync_values() in between. The length of the processing segment
1462 * should be up to @control-rate nanoseconds.
1464 * If the @object is not under property control, this will return
1465 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1467 * The control-rate is not expected to change if the element is in
1468 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1470 * Returns: the control rate in nanoseconds
1473 gst_object_get_control_rate (GstObject * object)
1475 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1477 return object->control_rate;
1481 * gst_object_set_control_rate:
1482 * @object: the object that has controlled properties
1483 * @control_rate: the new control-rate in nanoseconds.
1485 * Change the control-rate for this @object. Audio processing #GstElement
1486 * objects will use this rate to sub-divide their processing loop and call
1487 * gst_object_sync_values() in between. The length of the processing segment
1488 * should be up to @control-rate nanoseconds.
1490 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1491 * %GST_STATE_PLAYING.
1494 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1496 g_return_if_fail (GST_IS_OBJECT (object));
1498 object->control_rate = control_rate;