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.
293 gst_object_ref_sink (gpointer object)
295 g_return_val_if_fail (object != NULL, NULL);
297 #ifdef DEBUG_REFCOUNT
298 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
299 object, ((GObject *) object)->ref_count,
300 ((GObject *) object)->ref_count + 1);
302 return g_object_ref_sink (object);
306 * gst_object_replace:
307 * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
308 * a #GstObject to replace
309 * @newobj: (transfer none) (allow-none): a new #GstObject
311 * Atomically modifies a pointer to point to a new object.
312 * The reference count of @oldobj is decreased and the reference count of
313 * @newobj is increased.
315 * Either @newobj and the value pointed to by @oldobj may be %NULL.
317 * Returns: %TRUE if @newobj was different from @oldobj
320 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
324 g_return_val_if_fail (oldobj != NULL, FALSE);
326 #ifdef DEBUG_REFCOUNT
327 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
328 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
329 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
330 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
331 newobj ? G_OBJECT (newobj)->ref_count : 0);
334 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
336 if (G_UNLIKELY (oldptr == newobj))
340 gst_object_ref (newobj);
342 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
343 oldobj, oldptr, newobj))) {
344 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
345 if (G_UNLIKELY (oldptr == newobj))
350 gst_object_unref (oldptr);
352 return oldptr != newobj;
355 /* dispose is called when the object has to release all links
356 * to other objects */
358 gst_object_dispose (GObject * object)
360 GstObject *self = (GstObject *) object;
363 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
365 GST_OBJECT_LOCK (object);
366 if ((parent = GST_OBJECT_PARENT (object)))
368 GST_OBJECT_PARENT (object) = NULL;
369 GST_OBJECT_UNLOCK (object);
371 if (self->control_bindings) {
374 for (node = self->control_bindings; node; node = g_list_next (node)) {
375 gst_object_unparent (node->data);
377 g_list_free (self->control_bindings);
378 self->control_bindings = NULL;
381 ((GObjectClass *) gst_object_parent_class)->dispose (object);
388 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
389 "parent \"%s\".\nYou need to let the parent manage the "
390 "object instead of unreffing the object directly.\n",
391 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
392 GST_OBJECT_UNLOCK (object);
393 /* ref the object again to revive it in this error case */
394 gst_object_ref (object);
399 /* finalize is called when the object has to free its resources */
401 gst_object_finalize (GObject * object)
403 GstObject *gstobject = GST_OBJECT_CAST (object);
405 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
407 g_signal_handlers_destroy (object);
409 g_free (gstobject->name);
410 g_mutex_clear (&gstobject->lock);
412 GST_TRACER_OBJECT_DESTROYED (gstobject);
414 ((GObjectClass *) gst_object_parent_class)->finalize (object);
417 /* Changing a GObject property of a GstObject will result in "deep-notify"
418 * signals being emitted by the object itself, as well as in each parent
419 * object. This is so that an application can connect a listener to the
420 * top-level bin to catch property-change notifications for all contained
426 gst_object_dispatch_properties_changed (GObject * object,
427 guint n_pspecs, GParamSpec ** pspecs)
429 GstObject *gst_object, *parent, *old_parent;
431 #ifndef GST_DISABLE_GST_DEBUG
433 const gchar *debug_name;
436 /* do the standard dispatching */
438 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
441 gst_object = GST_OBJECT_CAST (object);
442 #ifndef GST_DISABLE_GST_DEBUG
443 if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
444 name = gst_object_get_name (gst_object);
445 debug_name = GST_STR_NULL (name);
450 /* now let the parent dispatch those, too */
451 parent = gst_object_get_parent (gst_object);
453 for (i = 0; i < n_pspecs; i++) {
454 GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
455 "deep notification from %s (%s)", debug_name, pspecs[i]->name);
457 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
458 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
462 parent = gst_object_get_parent (old_parent);
463 gst_object_unref (old_parent);
465 #ifndef GST_DISABLE_GST_DEBUG
471 * gst_object_default_deep_notify:
472 * @object: the #GObject that signalled the notify.
473 * @orig: a #GstObject that initiated the notify.
474 * @pspec: a #GParamSpec of the property.
475 * @excluded_props: (array zero-terminated=1) (element-type gchar*) (allow-none):
476 * a set of user-specified properties to exclude or %NULL to show
479 * A default deep_notify signal callback for an object. The user data
480 * should contain a pointer to an array of strings that should be excluded
481 * from the notify. The default handler will print the new value of the property
484 * MT safe. This function grabs and releases @object's LOCK for getting its
488 gst_object_default_deep_notify (GObject * object, GstObject * orig,
489 GParamSpec * pspec, gchar ** excluded_props)
491 GValue value = { 0, }; /* the important thing is that value.type = 0 */
495 if (pspec->flags & G_PARAM_READABLE) {
496 /* let's not print these out for excluded properties... */
497 while (excluded_props != NULL && *excluded_props != NULL) {
498 if (strcmp (pspec->name, *excluded_props) == 0)
502 g_value_init (&value, pspec->value_type);
503 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
505 if (G_VALUE_HOLDS_STRING (&value))
506 str = g_value_dup_string (&value);
508 str = gst_value_serialize (&value);
509 name = gst_object_get_path_string (orig);
510 g_print ("%s: %s = %s\n", name, pspec->name, str);
513 g_value_unset (&value);
515 name = gst_object_get_path_string (orig);
516 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
522 gst_object_set_name_default (GstObject * object)
524 const gchar *type_name;
530 /* to ensure guaranteed uniqueness across threads, only one thread
531 * may ever assign a name */
532 G_LOCK (object_name_mutex);
534 if (!object_name_counts) {
535 g_datalist_init (&object_name_counts);
538 q = g_type_qname (G_OBJECT_TYPE (object));
539 count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
540 g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
542 G_UNLOCK (object_name_mutex);
544 /* GstFooSink -> foosink<N> */
545 type_name = g_quark_to_string (q);
546 if (strncmp (type_name, "Gst", 3) == 0)
548 /* give the 20th "queue" element and the first "queue2" different names */
549 l = strlen (type_name);
550 if (l > 0 && g_ascii_isdigit (type_name[l - 1])) {
551 name = g_strdup_printf ("%s-%d", type_name, count);
553 name = g_strdup_printf ("%s%d", type_name, count);
557 for (i = 0; i < l; i++)
558 name[i] = g_ascii_tolower (name[i]);
560 GST_OBJECT_LOCK (object);
561 if (G_UNLIKELY (object->parent != NULL))
564 g_free (object->name);
567 GST_OBJECT_UNLOCK (object);
574 GST_WARNING ("parented objects can't be renamed");
575 GST_OBJECT_UNLOCK (object);
581 * gst_object_set_name:
582 * @object: a #GstObject
583 * @name: (allow-none): new name of object
585 * Sets the name of @object, or gives @object a guaranteed unique
586 * name (if @name is %NULL).
587 * This function makes a copy of the provided name, so the caller
588 * retains ownership of the name it sent.
590 * Returns: %TRUE if the name could be set. Since Objects that have
591 * a parent cannot be renamed, this function returns %FALSE in those
594 * MT safe. This function grabs and releases @object's LOCK.
597 gst_object_set_name (GstObject * object, const gchar * name)
601 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
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);
619 g_object_notify (G_OBJECT (object), "name");
625 GST_WARNING ("parented objects can't be renamed");
626 GST_OBJECT_UNLOCK (object);
632 * gst_object_get_name:
633 * @object: a #GstObject
635 * Returns a copy of the name of @object.
636 * Caller should g_free() the return value after usage.
637 * For a nameless object, this returns %NULL, which you can safely g_free()
640 * Free-function: g_free
642 * Returns: (transfer full) (nullable): the name of @object. g_free()
645 * MT safe. This function grabs and releases @object's LOCK.
648 gst_object_get_name (GstObject * object)
650 gchar *result = NULL;
652 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
654 GST_OBJECT_LOCK (object);
655 result = g_strdup (object->name);
656 GST_OBJECT_UNLOCK (object);
662 * gst_object_set_parent:
663 * @object: a #GstObject
664 * @parent: new parent of object
666 * Sets the parent of @object to @parent. The object's reference count will
667 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
669 * Returns: %TRUE if @parent could be set or %FALSE when @object
670 * already had a parent or @object and @parent are the same.
672 * MT safe. Grabs and releases @object's LOCK.
675 gst_object_set_parent (GstObject * object, GstObject * parent)
677 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
678 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
679 g_return_val_if_fail (object != parent, FALSE);
681 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
682 "set parent (ref and sink)");
684 GST_OBJECT_LOCK (object);
685 if (G_UNLIKELY (object->parent != NULL))
688 object->parent = parent;
689 gst_object_ref_sink (object);
690 GST_OBJECT_UNLOCK (object);
692 /* FIXME-2.0: this does not work, the deep notify takes the lock from the
693 * parent object and deadlocks when the parent holds its lock when calling
694 * this function (like _element_add_pad()), we need to use a GRecMutex
695 * for locking the parent instead.
697 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
704 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
705 "set parent failed, object already had a parent");
706 GST_OBJECT_UNLOCK (object);
712 * gst_object_get_parent:
713 * @object: a #GstObject
715 * Returns the parent of @object. This function increases the refcount
716 * of the parent object so you should gst_object_unref() it after usage.
718 * Returns: (transfer full) (nullable): parent of @object, this can be
719 * %NULL if @object has no parent. unref after usage.
721 * MT safe. Grabs and releases @object's LOCK.
724 gst_object_get_parent (GstObject * object)
726 GstObject *result = NULL;
728 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
730 GST_OBJECT_LOCK (object);
731 result = object->parent;
732 if (G_LIKELY (result))
733 gst_object_ref (result);
734 GST_OBJECT_UNLOCK (object);
740 * gst_object_unparent:
741 * @object: a #GstObject to unparent
743 * Clear the parent of @object, removing the associated reference.
744 * This function decreases the refcount of @object.
746 * MT safe. Grabs and releases @object's lock.
749 gst_object_unparent (GstObject * object)
753 g_return_if_fail (GST_IS_OBJECT (object));
755 GST_OBJECT_LOCK (object);
756 parent = object->parent;
758 if (G_LIKELY (parent != NULL)) {
759 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
760 object->parent = NULL;
761 GST_OBJECT_UNLOCK (object);
763 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
765 gst_object_unref (object);
767 GST_OBJECT_UNLOCK (object);
772 * gst_object_has_as_parent:
773 * @object: a #GstObject to check
774 * @parent: a #GstObject to check as parent
776 * Check if @parent is the parent of @object.
777 * E.g. a #GstElement can check if it owns a given #GstPad.
779 * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
780 * the parent of @object. Otherwise %FALSE.
782 * MT safe. Grabs and releases @object's locks.
786 gst_object_has_as_parent (GstObject * object, GstObject * parent)
788 gboolean result = FALSE;
790 if (G_LIKELY (GST_IS_OBJECT (object) && GST_IS_OBJECT (parent))) {
791 GST_OBJECT_LOCK (object);
792 result = GST_OBJECT_PARENT (object) == parent;
793 GST_OBJECT_UNLOCK (object);
800 * gst_object_has_as_ancestor:
801 * @object: a #GstObject to check
802 * @ancestor: a #GstObject to check as ancestor
804 * Check if @object has an ancestor @ancestor somewhere up in
805 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
807 * Returns: %TRUE if @ancestor is an ancestor of @object.
809 * MT safe. Grabs and releases @object's locks.
812 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
814 GstObject *parent, *tmp;
816 if (!ancestor || !object)
819 parent = gst_object_ref (object);
821 if (parent == ancestor) {
822 gst_object_unref (parent);
826 tmp = gst_object_get_parent (parent);
827 gst_object_unref (parent);
835 * gst_object_has_ancestor:
836 * @object: a #GstObject to check
837 * @ancestor: a #GstObject to check as ancestor
839 * Check if @object has an ancestor @ancestor somewhere up in
840 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
842 * Returns: %TRUE if @ancestor is an ancestor of @object.
844 * Deprecated: Use gst_object_has_as_ancestor() instead.
846 * MT safe. Grabs and releases @object's locks.
848 /* FIXME 2.0: remove */
849 #ifndef GST_REMOVE_DEPRECATED
850 #ifdef GST_DISABLE_DEPRECATED
851 gboolean gst_object_has_ancestor (GstObject * object, GstObject * ancestor);
854 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
856 return gst_object_has_as_ancestor (object, ancestor);
861 * gst_object_check_uniqueness:
862 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
864 * @name: the name to search for
866 * Checks to see if there is any object named @name in @list. This function
867 * does not do any locking of any kind. You might want to protect the
868 * provided list with the lock of the owner of the list. This function
869 * will lock each #GstObject in the list to compare the name, so be
870 * careful when passing a list with a locked object.
872 * Returns: %TRUE if a #GstObject named @name does not appear in @list,
875 * MT safe. Grabs and releases the LOCK of each object in the list.
878 gst_object_check_uniqueness (GList * list, const gchar * name)
880 gboolean result = TRUE;
882 g_return_val_if_fail (name != NULL, FALSE);
884 for (; list; list = g_list_next (list)) {
888 child = GST_OBJECT_CAST (list->data);
890 GST_OBJECT_LOCK (child);
891 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
892 GST_OBJECT_UNLOCK (child);
894 if (G_UNLIKELY (eq)) {
904 gst_object_set_property (GObject * object, guint prop_id,
905 const GValue * value, GParamSpec * pspec)
907 GstObject *gstobject;
909 gstobject = GST_OBJECT_CAST (object);
913 gst_object_set_name (gstobject, g_value_get_string (value));
916 gst_object_set_parent (gstobject, g_value_get_object (value));
918 #ifdef TIZEN_PROFILE_TV
920 g_atomic_int_set (&gstobject->family_id, g_value_get_int (value));
924 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
930 gst_object_get_property (GObject * object, guint prop_id,
931 GValue * value, GParamSpec * pspec)
933 GstObject *gstobject;
935 gstobject = GST_OBJECT_CAST (object);
939 g_value_take_string (value, gst_object_get_name (gstobject));
942 g_value_take_object (value, gst_object_get_parent (gstobject));
944 #ifdef TIZEN_PROFILE_TV
946 g_value_set_int (value, g_atomic_int_get (&gstobject->family_id));
950 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
956 * gst_object_get_path_string:
957 * @object: a #GstObject
959 * Generates a string describing the path of @object in
960 * the object hierarchy. Only useful (or used) for debugging.
962 * Free-function: g_free
964 * Returns: (transfer full): a string describing the path of @object. You must
965 * g_free() the string after usage.
967 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
971 gst_object_get_path_string (GstObject * object)
976 gchar *prevpath, *path;
977 const gchar *typename;
979 const gchar *separator;
981 /* ref object before adding to list */
982 gst_object_ref (object);
983 parentage = g_slist_prepend (NULL, object);
985 path = g_strdup ("");
987 /* first walk the object hierarchy to build a list of the parents,
988 * be careful here with refcounting. */
990 if (GST_IS_OBJECT (object)) {
991 parent = gst_object_get_parent (object);
992 /* add parents to list, refcount remains increased while
993 * we handle the object */
995 parentage = g_slist_prepend (parentage, parent);
1000 } while (object != NULL);
1002 /* then walk the parent list and print them out. we need to
1003 * decrease the refcounting on each element after we handled
1005 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1006 if (G_IS_OBJECT (parents->data)) {
1007 typename = G_OBJECT_TYPE_NAME (parents->data);
1011 if (GST_IS_OBJECT (parents->data)) {
1012 GstObject *item = GST_OBJECT_CAST (parents->data);
1013 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1014 gchar *objname = gst_object_get_name (item);
1016 component = g_strdup_printf ("%s:%s", typename, objname);
1017 separator = oclass->path_string_separator;
1019 gst_object_unref (item);
1023 component = g_strdup_printf ("%s:%p", typename, parents->data);
1025 component = g_strdup_printf ("%p", parents->data);
1031 path = g_strjoin (separator, prevpath, component, NULL);
1036 g_slist_free (parentage);
1041 /* controller helper functions */
1044 * gst_object_find_control_binding:
1045 * @self: the gobject to search for a property in
1046 * @name: the gobject property name to look for
1048 * Searches the list of properties under control.
1050 * Returns: (nullable): a #GstControlBinding or %NULL if the property
1051 * is not being controlled.
1053 static GstControlBinding *
1054 gst_object_find_control_binding (GstObject * self, const gchar * name)
1056 GstControlBinding *binding;
1059 for (node = self->control_bindings; node; node = g_list_next (node)) {
1060 binding = node->data;
1061 /* FIXME: eventually use GQuark to speed it up */
1062 if (!strcmp (binding->name, name)) {
1063 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1067 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1072 /* controller functions */
1075 * gst_object_suggest_next_sync:
1076 * @object: the object that has controlled properties
1078 * Returns a suggestion for timestamps where buffers should be split
1079 * to get best controller results.
1081 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1082 * if no control-rate was set.
1085 gst_object_suggest_next_sync (GstObject * object)
1089 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1090 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1091 GST_CLOCK_TIME_NONE);
1093 GST_OBJECT_LOCK (object);
1095 /* TODO: Implement more logic, depending on interpolation mode and control
1097 * FIXME: we need playback direction
1099 ret = object->last_sync + object->control_rate;
1101 GST_OBJECT_UNLOCK (object);
1107 * gst_object_sync_values:
1108 * @object: the object that has controlled properties
1109 * @timestamp: the time that should be processed
1111 * Sets the properties of the object, according to the #GstControlSources that
1112 * (maybe) handle them and for the given timestamp.
1114 * If this function fails, it is most likely the application developers fault.
1115 * Most probably the control sources are not setup correctly.
1117 * Returns: %TRUE if the controller values could be applied to the object
1118 * properties, %FALSE otherwise
1121 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1124 gboolean ret = TRUE;
1126 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1127 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1129 GST_LOG_OBJECT (object, "sync_values");
1130 if (!object->control_bindings)
1133 /* FIXME: this deadlocks */
1134 /* GST_OBJECT_LOCK (object); */
1135 g_object_freeze_notify ((GObject *) object);
1136 for (node = object->control_bindings; node; node = g_list_next (node)) {
1137 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1138 object, timestamp, object->last_sync);
1140 object->last_sync = timestamp;
1141 g_object_thaw_notify ((GObject *) object);
1142 /* GST_OBJECT_UNLOCK (object); */
1149 * gst_object_has_active_control_bindings:
1150 * @object: the object that has controlled properties
1152 * Check if the @object has active controlled properties.
1154 * Returns: %TRUE if the object has active controlled properties
1157 gst_object_has_active_control_bindings (GstObject * object)
1159 gboolean res = FALSE;
1162 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1164 GST_OBJECT_LOCK (object);
1165 for (node = object->control_bindings; node; node = g_list_next (node)) {
1166 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1168 GST_OBJECT_UNLOCK (object);
1173 * gst_object_set_control_bindings_disabled:
1174 * @object: the object that has controlled properties
1175 * @disabled: boolean that specifies whether to disable the controller
1178 * This function is used to disable all controlled properties of the @object for
1179 * some time, i.e. gst_object_sync_values() will do nothing.
1182 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1186 g_return_if_fail (GST_IS_OBJECT (object));
1188 GST_OBJECT_LOCK (object);
1189 for (node = object->control_bindings; node; node = g_list_next (node)) {
1190 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1193 GST_OBJECT_UNLOCK (object);
1197 * gst_object_set_control_binding_disabled:
1198 * @object: the object that has controlled properties
1199 * @property_name: property to disable
1200 * @disabled: boolean that specifies whether to disable the controller
1203 * This function is used to disable the control bindings on a property for
1204 * some time, i.e. gst_object_sync_values() will do nothing for the
1208 gst_object_set_control_binding_disabled (GstObject * object,
1209 const gchar * property_name, gboolean disabled)
1211 GstControlBinding *binding;
1213 g_return_if_fail (GST_IS_OBJECT (object));
1214 g_return_if_fail (property_name);
1216 GST_OBJECT_LOCK (object);
1217 if ((binding = gst_object_find_control_binding (object, property_name))) {
1218 gst_control_binding_set_disabled (binding, disabled);
1220 GST_OBJECT_UNLOCK (object);
1225 * gst_object_add_control_binding:
1226 * @object: the controller object
1227 * @binding: (transfer full): the #GstControlBinding that should be used
1229 * Attach the #GstControlBinding to the object. If there already was a
1230 * #GstControlBinding for this property it will be replaced.
1232 * The @object will take ownership of the @binding.
1234 * Returns: %FALSE if the given @binding has not been setup for this object or
1235 * has been setup for a non suitable property, %TRUE otherwise.
1238 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1240 GstControlBinding *old;
1242 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1243 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1244 g_return_val_if_fail (binding->pspec, FALSE);
1246 GST_OBJECT_LOCK (object);
1247 if ((old = gst_object_find_control_binding (object, binding->name))) {
1248 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1249 object->control_bindings = g_list_remove (object->control_bindings, old);
1250 gst_object_unparent (GST_OBJECT_CAST (old));
1252 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1253 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1254 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1255 GST_OBJECT_UNLOCK (object);
1261 * gst_object_get_control_binding:
1262 * @object: the object
1263 * @property_name: name of the property
1265 * Gets the corresponding #GstControlBinding for the property. This should be
1266 * unreferenced again after use.
1268 * Returns: (transfer full) (nullable): the #GstControlBinding for
1269 * @property_name or %NULL if the property is not controlled.
1272 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1274 GstControlBinding *binding;
1276 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1277 g_return_val_if_fail (property_name, NULL);
1279 GST_OBJECT_LOCK (object);
1280 if ((binding = gst_object_find_control_binding (object, property_name))) {
1281 gst_object_ref (binding);
1283 GST_OBJECT_UNLOCK (object);
1289 * gst_object_remove_control_binding:
1290 * @object: the object
1291 * @binding: the binding
1293 * Removes the corresponding #GstControlBinding. If it was the
1294 * last ref of the binding, it will be disposed.
1296 * Returns: %TRUE if the binding could be removed.
1299 gst_object_remove_control_binding (GstObject * object,
1300 GstControlBinding * binding)
1303 gboolean ret = FALSE;
1305 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1306 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1308 GST_OBJECT_LOCK (object);
1309 if ((node = g_list_find (object->control_bindings, binding))) {
1310 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1311 object->control_bindings =
1312 g_list_delete_link (object->control_bindings, node);
1313 gst_object_unparent (GST_OBJECT_CAST (binding));
1316 GST_OBJECT_UNLOCK (object);
1322 * gst_object_get_value:
1323 * @object: the object that has controlled properties
1324 * @property_name: the name of the property to get
1325 * @timestamp: the time the control-change should be read from
1327 * Gets the value for the given controlled property at the requested time.
1329 * Returns: (nullable): the GValue of the property at the given time,
1330 * or %NULL if the property isn't controlled.
1333 gst_object_get_value (GstObject * object, const gchar * property_name,
1334 GstClockTime timestamp)
1336 GstControlBinding *binding;
1339 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1340 g_return_val_if_fail (property_name, NULL);
1341 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1343 GST_OBJECT_LOCK (object);
1344 if ((binding = gst_object_find_control_binding (object, property_name))) {
1345 val = gst_control_binding_get_value (binding, timestamp);
1347 GST_OBJECT_UNLOCK (object);
1353 * gst_object_get_value_array: (skip)
1354 * @object: the object that has controlled properties
1355 * @property_name: the name of the property to get
1356 * @timestamp: the time that should be processed
1357 * @interval: the time spacing between subsequent values
1358 * @n_values: the number of values
1359 * @values: array to put control-values in
1361 * Gets a number of values for the given controlled property starting at the
1362 * requested time. The array @values need to hold enough space for @n_values of
1363 * the same type as the objects property's type.
1365 * This function is useful if one wants to e.g. draw a graph of the control
1366 * curve or apply a control curve sample by sample.
1368 * The values are unboxed and ready to be used. The similar function
1369 * gst_object_get_g_value_array() returns the array as #GValues and is
1370 * better suites for bindings.
1372 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1375 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1376 GstClockTime timestamp, GstClockTime interval, guint n_values,
1379 gboolean res = FALSE;
1380 GstControlBinding *binding;
1382 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1383 g_return_val_if_fail (property_name, FALSE);
1384 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1385 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1386 g_return_val_if_fail (values, FALSE);
1388 GST_OBJECT_LOCK (object);
1389 if ((binding = gst_object_find_control_binding (object, property_name))) {
1390 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1393 GST_OBJECT_UNLOCK (object);
1398 * gst_object_get_g_value_array:
1399 * @object: the object that has controlled properties
1400 * @property_name: the name of the property to get
1401 * @timestamp: the time that should be processed
1402 * @interval: the time spacing between subsequent values
1403 * @n_values: the number of values
1404 * @values: (array length=n_values): array to put control-values in
1406 * Gets a number of #GValues for the given controlled property starting at the
1407 * requested time. The array @values need to hold enough space for @n_values of
1410 * This function is useful if one wants to e.g. draw a graph of the control
1411 * curve or apply a control curve sample by sample.
1413 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1416 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1417 GstClockTime timestamp, GstClockTime interval, guint n_values,
1420 gboolean res = FALSE;
1421 GstControlBinding *binding;
1423 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1424 g_return_val_if_fail (property_name, FALSE);
1425 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1426 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1427 g_return_val_if_fail (values, FALSE);
1429 GST_OBJECT_LOCK (object);
1430 if ((binding = gst_object_find_control_binding (object, property_name))) {
1431 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1434 GST_OBJECT_UNLOCK (object);
1440 * gst_object_get_control_rate:
1441 * @object: the object that has controlled properties
1443 * Obtain the control-rate for this @object. Audio processing #GstElement
1444 * objects will use this rate to sub-divide their processing loop and call
1445 * gst_object_sync_values() inbetween. The length of the processing segment
1446 * should be up to @control-rate nanoseconds.
1448 * If the @object is not under property control, this will return
1449 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1451 * The control-rate is not expected to change if the element is in
1452 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1454 * Returns: the control rate in nanoseconds
1457 gst_object_get_control_rate (GstObject * object)
1459 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1461 return object->control_rate;
1465 * gst_object_set_control_rate:
1466 * @object: the object that has controlled properties
1467 * @control_rate: the new control-rate in nanoseconds.
1469 * Change 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() inbetween. The length of the processing segment
1472 * should be up to @control-rate nanoseconds.
1474 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1475 * %GST_STATE_PLAYING.
1478 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1480 g_return_if_fail (GST_IS_OBJECT (object));
1482 object->control_rate = control_rate;