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., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 * @short_description: Base class for the GStreamer object hierarchy
28 * #GstObject provides a root for the object hierarchy tree filed in by the
29 * GStreamer library. It is currently a thin wrapper on top of
30 * #GInitiallyUnowned. It is an abstract class that is not very usable on its own.
32 * #GstObject gives us basic refcounting, parenting functionality and locking.
33 * Most of the function are just extended for special GStreamer needs and can be
34 * found under the same name in the base class of #GstObject which is #GObject
35 * (e.g. g_object_ref() becomes gst_object_ref()).
37 * Since #GstObject dereives from #GInitiallyUnowned, it also inherits the
38 * floating reference. Be aware that functions such as gst_bin_add() and
39 * gst_element_add_pad() take ownership of the floating reference.
41 * In contrast to #GObject instances, #GstObject adds a name property. The functions
42 * gst_object_set_name() and gst_object_get_name() are used to set/get the name
46 * <title>controlled properties</title>
48 * Controlled properties offers a lightweight way to adjust gobject
49 * properties over stream-time. It works by using time-stamped value pairs that
50 * are queued for element-properties. At run-time the elements continously pull
51 * values changes for the current stream-time.
53 * What needs to be changed in a #GstElement?
54 * Very little - it is just two steps to make a plugin controllable!
57 * mark gobject-properties paramspecs that make sense to be controlled,
58 * by GST_PARAM_CONTROLLABLE.
61 * when processing data (get, chain, loop function) at the beginning call
62 * gst_object_sync_values(element,timestamp).
63 * This will made the controller to update all gobject properties that are under
64 * control with the current values based on timestamp.
68 * What needs to be done in applications?
69 * Again its not a lot to change.
72 * first put some properties under control, by calling
73 * gst_object_control_properties (object, "prop1", "prop2",...);
76 * create a #GstControlSource.
77 * csource = gst_interpolation_control_source_new ();
78 * g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
81 * Attach the #GstControlSource on the controller to a property.
82 * gst_object_add_control_binding (object, gst_direct_control_binding_new (objetct, "prop1", csource));
85 * Set the control values
86 * gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,0 * GST_SECOND, value1);
87 * gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,1 * GST_SECOND, value2);
96 * Last reviewed on 2012-03-29 (0.11.3)
99 #include "gst_private.h"
100 #include "glib-compat-private.h"
102 #include "gstobject.h"
103 #include "gstclock.h"
104 #include "gstcontrolbinding.h"
105 #include "gstcontrolsource.h"
107 #include "gstparamspecs.h"
108 #include "gstutils.h"
110 #ifndef GST_DISABLE_TRACE
111 #include "gsttrace.h"
112 static GstAllocTrace *_gst_object_trace;
115 #define DEBUG_REFCOUNT
117 /* Object signals and args */
138 /* maps type name quark => count */
139 static GData *object_name_counts = NULL;
141 G_LOCK_DEFINE_STATIC (object_name_mutex);
143 static void gst_object_set_property (GObject * object, guint prop_id,
144 const GValue * value, GParamSpec * pspec);
145 static void gst_object_get_property (GObject * object, guint prop_id,
146 GValue * value, GParamSpec * pspec);
148 static void gst_object_dispatch_properties_changed (GObject * object,
149 guint n_pspecs, GParamSpec ** pspecs);
151 static void gst_object_dispose (GObject * object);
152 static void gst_object_finalize (GObject * object);
154 static gboolean gst_object_set_name_default (GstObject * object);
156 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
158 static GParamSpec *properties[PROP_LAST];
160 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
163 gst_object_class_init (GstObjectClass * klass)
165 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167 #ifndef GST_DISABLE_TRACE
169 _gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT), -2);
172 gobject_class->set_property = gst_object_set_property;
173 gobject_class->get_property = gst_object_get_property;
175 properties[PROP_NAME] =
176 g_param_spec_string ("name", "Name", "The name of the object", NULL,
177 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
179 properties[PROP_PARENT] =
180 g_param_spec_object ("parent", "Parent", "The parent of the object",
181 GST_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
183 g_object_class_install_properties (gobject_class, PROP_LAST, properties);
186 * GstObject::deep-notify:
187 * @gstobject: a #GstObject
188 * @prop_object: the object that originated the signal
189 * @prop: the property that changed
191 * The deep notify signal is used to be notified of property changes. It is
192 * typically attached to the toplevel bin to receive notifications from all
193 * the elements contained in that bin.
195 gst_object_signals[DEEP_NOTIFY] =
196 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
197 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
198 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
199 NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
202 klass->path_string_separator = "/";
204 /* see the comments at gst_object_dispatch_properties_changed */
205 gobject_class->dispatch_properties_changed
206 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
208 gobject_class->dispose = gst_object_dispose;
209 gobject_class->finalize = gst_object_finalize;
213 gst_object_init (GstObject * object)
215 g_mutex_init (&object->lock);
216 object->parent = NULL;
218 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
220 #ifndef GST_DISABLE_TRACE
221 _gst_alloc_trace_new (_gst_object_trace, object);
226 object->control_rate = 100 * GST_MSECOND;
227 object->last_sync = GST_CLOCK_TIME_NONE;
232 * @object: a #GstObject to reference
234 * Increments the reference count on @object. This function
235 * does not take the lock on @object because it relies on
236 * atomic refcounting.
238 * This object returns the input parameter to ease writing
240 * result = gst_object_ref (object->parent);
242 * Returns: (transfer full): A pointer to @object
245 gst_object_ref (gpointer object)
247 g_return_val_if_fail (object != NULL, NULL);
249 #ifdef DEBUG_REFCOUNT
250 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
251 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
253 g_object_ref (object);
260 * @object: a #GstObject to unreference
262 * Decrements the reference count on @object. If reference count hits
263 * zero, destroy @object. This function does not take the lock
264 * on @object as it relies on atomic refcounting.
266 * The unref method should never be called with the LOCK held since
267 * this might deadlock the dispose function.
270 gst_object_unref (gpointer object)
272 g_return_if_fail (object != NULL);
273 g_return_if_fail (((GObject *) object)->ref_count > 0);
275 #ifdef DEBUG_REFCOUNT
276 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
277 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
279 g_object_unref (object);
283 * gst_object_ref_sink: (skip)
284 * @object: a #GstObject to sink
286 * Increase the reference count of @object, and possibly remove the floating
287 * reference, if @object has a floating reference.
289 * In other words, if the object is floating, then this call "assumes ownership"
290 * of the floating reference, converting it to a normal reference by clearing
291 * the floating flag while leaving the reference count unchanged. If the object
292 * is not floating, then this call adds a new normal reference increasing the
293 * reference count by one.
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_object_replace:
310 * @oldobj: (inout) (transfer full): pointer to a place of a #GstObject to
312 * @newobj: (transfer none): a new #GstObject
314 * Atomically modifies a pointer to point to a new object.
315 * The reference count of @oldobj is decreased and the reference count of
316 * @newobj is increased.
318 * Either @newobj and the value pointed to by @oldobj may be NULL.
320 * Returns: TRUE if @newobj was different from @oldobj
323 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
327 g_return_val_if_fail (oldobj != NULL, FALSE);
329 #ifdef DEBUG_REFCOUNT
330 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
331 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
332 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
333 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
334 newobj ? G_OBJECT (newobj)->ref_count : 0);
337 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
339 if (G_UNLIKELY (oldptr == newobj))
343 gst_object_ref (newobj);
345 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
346 oldobj, oldptr, newobj))) {
347 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
348 if (G_UNLIKELY (oldptr == newobj))
353 gst_object_unref (oldptr);
355 return oldptr != newobj;
358 /* dispose is called when the object has to release all links
359 * to other objects */
361 gst_object_dispose (GObject * object)
363 GstObject *self = (GstObject *) object;
366 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
368 GST_OBJECT_LOCK (object);
369 if ((parent = GST_OBJECT_PARENT (object)))
371 GST_OBJECT_PARENT (object) = NULL;
372 GST_OBJECT_UNLOCK (object);
374 if (self->control_bindings) {
377 for (node = self->control_bindings; node; node = g_list_next (node)) {
378 gst_object_unparent (node->data);
380 g_list_free (self->control_bindings);
381 self->control_bindings = NULL;
384 ((GObjectClass *) gst_object_parent_class)->dispose (object);
391 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
392 "parent \"%s\".\nYou need to let the parent manage the "
393 "object instead of unreffing the object directly.\n",
394 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
395 GST_OBJECT_UNLOCK (object);
396 /* ref the object again to revive it in this error case */
397 gst_object_ref (object);
402 /* finalize is called when the object has to free its resources */
404 gst_object_finalize (GObject * object)
406 GstObject *gstobject = GST_OBJECT_CAST (object);
408 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
410 g_signal_handlers_destroy (object);
412 g_free (gstobject->name);
413 g_mutex_clear (&gstobject->lock);
415 #ifndef GST_DISABLE_TRACE
416 _gst_alloc_trace_free (_gst_object_trace, object);
419 ((GObjectClass *) gst_object_parent_class)->finalize (object);
422 /* Changing a GObject property of a GstObject will result in "deep-notify"
423 * signals being emitted by the object itself, as well as in each parent
424 * object. This is so that an application can connect a listener to the
425 * top-level bin to catch property-change notifications for all contained
431 gst_object_dispatch_properties_changed (GObject * object,
432 guint n_pspecs, GParamSpec ** pspecs)
434 GstObject *gst_object, *parent, *old_parent;
436 #ifndef GST_DISABLE_GST_DEBUG
438 const gchar *debug_name;
441 /* do the standard dispatching */
443 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
446 gst_object = GST_OBJECT_CAST (object);
447 #ifndef GST_DISABLE_GST_DEBUG
448 if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
449 name = gst_object_get_name (gst_object);
450 debug_name = GST_STR_NULL (name);
455 /* now let the parent dispatch those, too */
456 parent = gst_object_get_parent (gst_object);
458 for (i = 0; i < n_pspecs; i++) {
459 GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
460 "deep notification from %s (%s)", debug_name, pspecs[i]->name);
462 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
463 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
467 parent = gst_object_get_parent (old_parent);
468 gst_object_unref (old_parent);
470 #ifndef GST_DISABLE_GST_DEBUG
476 * gst_object_default_deep_notify:
477 * @object: the #GObject that signalled the notify.
478 * @orig: a #GstObject that initiated the notify.
479 * @pspec: a #GParamSpec of the property.
480 * @excluded_props: (array zero-terminated=1) (element-type gchar*)
481 * (allow-none):a set of user-specified properties to exclude or
482 * NULL to show all changes.
484 * A default deep_notify signal callback for an object. The user data
485 * should contain a pointer to an array of strings that should be excluded
486 * from the notify. The default handler will print the new value of the property
489 * MT safe. This function grabs and releases @object's LOCK for getting its
493 gst_object_default_deep_notify (GObject * object, GstObject * orig,
494 GParamSpec * pspec, gchar ** excluded_props)
496 GValue value = { 0, }; /* the important thing is that value.type = 0 */
500 if (pspec->flags & G_PARAM_READABLE) {
501 /* let's not print these out for excluded properties... */
502 while (excluded_props != NULL && *excluded_props != NULL) {
503 if (strcmp (pspec->name, *excluded_props) == 0)
507 g_value_init (&value, pspec->value_type);
508 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
510 /* FIXME: handle flags */
511 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
512 GEnumValue *enum_value;
513 GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
515 enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
517 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
519 g_type_class_unref (klass);
521 str = g_strdup_value_contents (&value);
523 name = gst_object_get_path_string (orig);
524 g_print ("%s: %s = %s\n", name, pspec->name, str);
527 g_value_unset (&value);
529 name = gst_object_get_path_string (orig);
530 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
536 gst_object_set_name_default (GstObject * object)
538 const gchar *type_name;
544 /* to ensure guaranteed uniqueness across threads, only one thread
545 * may ever assign a name */
546 G_LOCK (object_name_mutex);
548 if (!object_name_counts) {
549 g_datalist_init (&object_name_counts);
552 q = g_type_qname (G_OBJECT_TYPE (object));
553 count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
554 g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
556 G_UNLOCK (object_name_mutex);
558 /* GstFooSink -> foosink<N> */
559 type_name = g_quark_to_string (q);
560 if (strncmp (type_name, "Gst", 3) == 0)
562 name = g_strdup_printf ("%s%d", type_name, count);
564 for (i = 0; i < l; i++)
565 name[i] = g_ascii_tolower (name[i]);
567 GST_OBJECT_LOCK (object);
568 if (G_UNLIKELY (object->parent != NULL))
571 g_free (object->name);
574 GST_OBJECT_UNLOCK (object);
581 GST_WARNING ("parented objects can't be renamed");
582 GST_OBJECT_UNLOCK (object);
588 * gst_object_set_name:
589 * @object: a #GstObject
590 * @name: new name of object
592 * Sets the name of @object, or gives @object a guaranteed unique
593 * name (if @name is NULL).
594 * This function makes a copy of the provided name, so the caller
595 * retains ownership of the name it sent.
597 * Returns: TRUE if the name could be set. Since Objects that have
598 * a parent cannot be renamed, this function returns FALSE in those
601 * MT safe. This function grabs and releases @object's LOCK.
604 gst_object_set_name (GstObject * object, const gchar * name)
608 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
610 GST_OBJECT_LOCK (object);
612 /* parented objects cannot be renamed */
613 if (G_UNLIKELY (object->parent != NULL))
617 g_free (object->name);
618 object->name = g_strdup (name);
619 GST_OBJECT_UNLOCK (object);
622 GST_OBJECT_UNLOCK (object);
623 result = gst_object_set_name_default (object);
625 /* FIXME-0.11: this misses a g_object_notify (object, "name"); unless called
626 * from gst_object_set_property.
627 * Ideally remove such custom setters (or make it static).
634 GST_WARNING ("parented objects can't be renamed");
635 GST_OBJECT_UNLOCK (object);
641 * gst_object_get_name:
642 * @object: a #GstObject
644 * Returns a copy of the name of @object.
645 * Caller should g_free() the return value after usage.
646 * For a nameless object, this returns NULL, which you can safely g_free()
649 * Free-function: g_free
651 * Returns: (transfer full): the name of @object. g_free() after usage.
653 * MT safe. This function grabs and releases @object's LOCK.
656 gst_object_get_name (GstObject * object)
658 gchar *result = NULL;
660 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
662 GST_OBJECT_LOCK (object);
663 result = g_strdup (object->name);
664 GST_OBJECT_UNLOCK (object);
670 * gst_object_set_parent:
671 * @object: a #GstObject
672 * @parent: new parent of object
674 * Sets the parent of @object to @parent. The object's reference count will
675 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
677 * Returns: TRUE if @parent could be set or FALSE when @object
678 * already had a parent or @object and @parent are the same.
680 * MT safe. Grabs and releases @object's LOCK.
683 gst_object_set_parent (GstObject * object, GstObject * parent)
685 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
686 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
687 g_return_val_if_fail (object != parent, FALSE);
689 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
690 "set parent (ref and sink)");
692 GST_OBJECT_LOCK (object);
693 if (G_UNLIKELY (object->parent != NULL))
696 object->parent = parent;
697 gst_object_ref_sink (object);
698 GST_OBJECT_UNLOCK (object);
700 /* FIXME, this does not work, the deep notify takes the lock from the parent
701 * object and deadlocks when the parent holds its lock when calling this
702 * function (like _element_add_pad()) */
703 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
710 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
711 "set parent failed, object already had a parent");
712 GST_OBJECT_UNLOCK (object);
718 * gst_object_get_parent:
719 * @object: a #GstObject
721 * Returns the parent of @object. This function increases the refcount
722 * of the parent object so you should gst_object_unref() it after usage.
724 * Returns: (transfer full): parent of @object, this can be NULL if @object
725 * has no parent. unref after usage.
727 * MT safe. Grabs and releases @object's LOCK.
730 gst_object_get_parent (GstObject * object)
732 GstObject *result = NULL;
734 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
736 GST_OBJECT_LOCK (object);
737 result = object->parent;
738 if (G_LIKELY (result))
739 gst_object_ref (result);
740 GST_OBJECT_UNLOCK (object);
746 * gst_object_unparent:
747 * @object: a #GstObject to unparent
749 * Clear the parent of @object, removing the associated reference.
750 * This function decreases the refcount of @object.
752 * MT safe. Grabs and releases @object's lock.
755 gst_object_unparent (GstObject * object)
759 g_return_if_fail (GST_IS_OBJECT (object));
761 GST_OBJECT_LOCK (object);
762 parent = object->parent;
764 if (G_LIKELY (parent != NULL)) {
765 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
766 object->parent = NULL;
767 GST_OBJECT_UNLOCK (object);
769 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
771 gst_object_unref (object);
773 GST_OBJECT_UNLOCK (object);
778 * gst_object_has_ancestor:
779 * @object: a #GstObject to check
780 * @ancestor: a #GstObject to check as ancestor
782 * Check if @object has an ancestor @ancestor somewhere up in
783 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
785 * Returns: TRUE if @ancestor is an ancestor of @object.
787 * MT safe. Grabs and releases @object's locks.
790 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
792 GstObject *parent, *tmp;
794 if (!ancestor || !object)
797 parent = gst_object_ref (object);
799 if (parent == ancestor) {
800 gst_object_unref (parent);
804 tmp = gst_object_get_parent (parent);
805 gst_object_unref (parent);
813 * gst_object_check_uniqueness:
814 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
816 * @name: the name to search for
818 * Checks to see if there is any object named @name in @list. This function
819 * does not do any locking of any kind. You might want to protect the
820 * provided list with the lock of the owner of the list. This function
821 * will lock each #GstObject in the list to compare the name, so be
822 * carefull when passing a list with a locked object.
824 * Returns: TRUE if a #GstObject named @name does not appear in @list,
827 * MT safe. Grabs and releases the LOCK of each object in the list.
830 gst_object_check_uniqueness (GList * list, const gchar * name)
832 gboolean result = TRUE;
834 g_return_val_if_fail (name != NULL, FALSE);
836 for (; list; list = g_list_next (list)) {
840 child = GST_OBJECT_CAST (list->data);
842 GST_OBJECT_LOCK (child);
843 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
844 GST_OBJECT_UNLOCK (child);
846 if (G_UNLIKELY (eq)) {
856 gst_object_set_property (GObject * object, guint prop_id,
857 const GValue * value, GParamSpec * pspec)
859 GstObject *gstobject;
861 gstobject = GST_OBJECT_CAST (object);
865 gst_object_set_name (gstobject, g_value_get_string (value));
868 gst_object_set_parent (gstobject, g_value_get_object (value));
871 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
877 gst_object_get_property (GObject * object, guint prop_id,
878 GValue * value, GParamSpec * pspec)
880 GstObject *gstobject;
882 gstobject = GST_OBJECT_CAST (object);
886 g_value_take_string (value, gst_object_get_name (gstobject));
889 g_value_take_object (value, gst_object_get_parent (gstobject));
892 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
898 * gst_object_get_path_string:
899 * @object: a #GstObject
901 * Generates a string describing the path of @object in
902 * the object hierarchy. Only useful (or used) for debugging.
904 * Free-function: g_free
906 * Returns: (transfer full): a string describing the path of @object. You must
907 * g_free() the string after usage.
909 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
913 gst_object_get_path_string (GstObject * object)
918 gchar *prevpath, *path;
919 const gchar *typename;
921 const gchar *separator;
923 /* ref object before adding to list */
924 gst_object_ref (object);
925 parentage = g_slist_prepend (NULL, object);
927 path = g_strdup ("");
929 /* first walk the object hierarchy to build a list of the parents,
930 * be carefull here with refcounting. */
932 if (GST_IS_OBJECT (object)) {
933 parent = gst_object_get_parent (object);
934 /* add parents to list, refcount remains increased while
935 * we handle the object */
937 parentage = g_slist_prepend (parentage, parent);
942 } while (object != NULL);
944 /* then walk the parent list and print them out. we need to
945 * decrease the refcounting on each element after we handled
947 for (parents = parentage; parents; parents = g_slist_next (parents)) {
948 if (G_IS_OBJECT (parents->data)) {
949 typename = G_OBJECT_TYPE_NAME (parents->data);
953 if (GST_IS_OBJECT (parents->data)) {
954 GstObject *item = GST_OBJECT_CAST (parents->data);
955 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
956 gchar *objname = gst_object_get_name (item);
958 component = g_strdup_printf ("%s:%s", typename, objname);
959 separator = oclass->path_string_separator;
961 gst_object_unref (item);
965 component = g_strdup_printf ("%s:%p", typename, parents->data);
967 component = g_strdup_printf ("%p", parents->data);
973 path = g_strjoin (separator, prevpath, component, NULL);
978 g_slist_free (parentage);
983 /* controller helper functions */
986 * gst_object_find_control_binding:
987 * @self: the gobject to search for a property in
988 * @name: the gobject property name to look for
990 * Searches the list of properties under control.
992 * Returns: a #GstControlBinding or %NULL if the property is not being
995 static GstControlBinding *
996 gst_object_find_control_binding (GstObject * self, const gchar * name)
998 GstControlBinding *binding;
1001 for (node = self->control_bindings; node; node = g_list_next (node)) {
1002 binding = node->data;
1003 /* FIXME: eventually use GQuark to speed it up */
1004 if (!strcmp (binding->name, name)) {
1005 GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1009 GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1014 /* controller functions */
1017 * gst_object_suggest_next_sync:
1018 * @object: the object that has controlled properties
1020 * Returns a suggestion for timestamps where buffers should be split
1021 * to get best controller results.
1023 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1024 * if no control-rate was set.
1027 gst_object_suggest_next_sync (GstObject * object)
1031 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1032 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1033 GST_CLOCK_TIME_NONE);
1035 GST_OBJECT_LOCK (object);
1037 /* TODO: Implement more logic, depending on interpolation mode and control
1039 * FIXME: we need playback direction
1041 ret = object->last_sync + object->control_rate;
1043 GST_OBJECT_UNLOCK (object);
1049 * gst_object_sync_values:
1050 * @object: the object that has controlled properties
1051 * @timestamp: the time that should be processed
1053 * Sets the properties of the object, according to the #GstControlSources that
1054 * (maybe) handle them and for the given timestamp.
1056 * If this function fails, it is most likely the application developers fault.
1057 * Most probably the control sources are not setup correctly.
1059 * Returns: %TRUE if the controller values could be applied to the object
1060 * properties, %FALSE otherwise
1063 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1066 gboolean ret = TRUE;
1068 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1069 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1071 GST_LOG_OBJECT (object, "sync_values");
1072 if (!object->control_bindings)
1075 /* FIXME: this deadlocks */
1076 /* GST_OBJECT_LOCK (object); */
1077 g_object_freeze_notify ((GObject *) object);
1078 for (node = object->control_bindings; node; node = g_list_next (node)) {
1079 ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1080 object, timestamp, object->last_sync);
1082 object->last_sync = timestamp;
1083 g_object_thaw_notify ((GObject *) object);
1084 /* GST_OBJECT_UNLOCK (object); */
1091 * gst_object_has_active_control_bindings:
1092 * @object: the object that has controlled properties
1094 * Check if the @object has an active controlled properties.
1096 * Returns: %TRUE if the object has active controlled properties
1099 gst_object_has_active_control_bindings (GstObject * object)
1101 gboolean res = FALSE;
1104 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1106 GST_OBJECT_LOCK (object);
1107 for (node = object->control_bindings; node; node = g_list_next (node)) {
1108 res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1110 GST_OBJECT_UNLOCK (object);
1115 * gst_object_set_control_bindings_disabled:
1116 * @object: the object that has controlled properties
1117 * @disabled: boolean that specifies whether to disable the controller
1120 * This function is used to disable all controlled properties of the @object for
1121 * some time, i.e. gst_object_sync_values() will do nothing.
1124 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1128 g_return_if_fail (GST_IS_OBJECT (object));
1130 GST_OBJECT_LOCK (object);
1131 for (node = object->control_bindings; node; node = g_list_next (node)) {
1132 gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1135 GST_OBJECT_UNLOCK (object);
1139 * gst_object_set_control_binding_disabled:
1140 * @object: the object that has controlled properties
1141 * @property_name: property to disable
1142 * @disabled: boolean that specifies whether to disable the controller
1145 * This function is used to disable the #GstController on a property for
1146 * some time, i.e. gst_controller_sync_values() will do nothing for the
1150 gst_object_set_control_binding_disabled (GstObject * object,
1151 const gchar * property_name, gboolean disabled)
1153 GstControlBinding *binding;
1155 g_return_if_fail (GST_IS_OBJECT (object));
1156 g_return_if_fail (property_name);
1158 GST_OBJECT_LOCK (object);
1159 if ((binding = gst_object_find_control_binding (object, property_name))) {
1160 gst_control_binding_set_disabled (binding, disabled);
1162 GST_OBJECT_UNLOCK (object);
1167 * gst_object_add_control_binding:
1168 * @object: the controller object
1169 * @binding: (transfer full): the #GstControlBinding that should be used
1171 * Sets the #GstControlBinding. If there already was a #GstControlBinding
1172 * for this property it will be replaced.
1173 * The @object will take ownership of the @binding.
1175 * Returns: %FALSE if the given @binding has not been setup for this object or
1179 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1181 GstControlBinding *old;
1183 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1184 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1185 //g_return_val_if_fail (g_type_is_a (binding->pspec->owner_type,
1186 // G_OBJECT_TYPE (object)), FALSE);
1188 GST_OBJECT_LOCK (object);
1189 if ((old = gst_object_find_control_binding (object, binding->name))) {
1190 GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1191 object->control_bindings = g_list_remove (object->control_bindings, old);
1192 gst_object_unparent (GST_OBJECT_CAST (old));
1194 object->control_bindings = g_list_prepend (object->control_bindings, binding);
1195 gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1196 GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1197 GST_OBJECT_UNLOCK (object);
1203 * gst_object_get_control_binding:
1204 * @object: the object
1205 * @property_name: name of the property
1207 * Gets the corresponding #GstControlBinding for the property. This should be
1208 * unreferenced again after use.
1210 * Returns: (transfer full): the #GstControlBinding for @property_name or %NULL if
1211 * the property is not controlled.
1214 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1216 GstControlBinding *binding;
1218 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1219 g_return_val_if_fail (property_name, NULL);
1221 GST_OBJECT_LOCK (object);
1222 if ((binding = gst_object_find_control_binding (object, property_name))) {
1223 gst_object_ref (binding);
1225 GST_OBJECT_UNLOCK (object);
1231 * gst_object_remove_control_binding:
1232 * @object: the object
1233 * @binding: the binding
1235 * Removes the corresponding #GstControlBinding. If it was the
1236 * last ref of the binding, it will be disposed.
1238 * Returns: %TRUE if the binding could be removed.
1241 gst_object_remove_control_binding (GstObject * object,
1242 GstControlBinding * binding)
1245 gboolean ret = FALSE;
1247 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1248 g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1250 GST_OBJECT_LOCK (object);
1251 if ((node = g_list_find (object->control_bindings, binding))) {
1252 GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1253 object->control_bindings =
1254 g_list_delete_link (object->control_bindings, node);
1255 gst_object_unparent (GST_OBJECT_CAST (binding));
1258 GST_OBJECT_UNLOCK (object);
1264 * gst_object_get_value:
1265 * @object: the object that has controlled properties
1266 * @property_name: the name of the property to get
1267 * @timestamp: the time the control-change should be read from
1269 * Gets the value for the given controlled property at the requested time.
1271 * Returns: the GValue of the property at the given time, or %NULL if the
1272 * property isn't controlled.
1275 gst_object_get_value (GstObject * object, const gchar * property_name,
1276 GstClockTime timestamp)
1278 GstControlBinding *binding;
1281 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1282 g_return_val_if_fail (property_name, NULL);
1283 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1285 GST_OBJECT_LOCK (object);
1286 if ((binding = gst_object_find_control_binding (object, property_name))) {
1287 val = gst_control_binding_get_value (binding, timestamp);
1289 GST_OBJECT_UNLOCK (object);
1295 * gst_object_get_value_array:
1296 * @object: the object that has controlled properties
1297 * @property_name: the name of the property to get
1298 * @timestamp: the time that should be processed
1299 * @interval: the time spacing between subsequent values
1300 * @n_values: the number of values
1301 * @values: array to put control-values in
1303 * Gets a number of values for the given controlled property starting at the
1304 * requested time. The array @values need to hold enough space for @n_values of
1305 * the same type as the objects property's type.
1307 * This function is useful if one wants to e.g. draw a graph of the control
1308 * curve or apply a control curve sample by sample.
1310 * The values are unboxed and ready to be used. The similar function
1311 * gst_object_get_g_value_array() returns the array as #GValues and is
1312 * better suites for bindings.
1314 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1317 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1318 GstClockTime timestamp, GstClockTime interval, guint n_values,
1321 gboolean res = FALSE;
1322 GstControlBinding *binding;
1324 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1325 g_return_val_if_fail (property_name, FALSE);
1326 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1327 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1328 g_return_val_if_fail (values, FALSE);
1330 GST_OBJECT_LOCK (object);
1331 if ((binding = gst_object_find_control_binding (object, property_name))) {
1332 res = gst_control_binding_get_value_array (binding, timestamp, interval,
1335 GST_OBJECT_UNLOCK (object);
1340 * gst_object_get_g_value_array:
1341 * @object: the object that has controlled properties
1342 * @property_name: the name of the property to get
1343 * @timestamp: the time that should be processed
1344 * @interval: the time spacing between subsequent values
1345 * @n_values: the number of values
1346 * @values: array to put control-values in
1348 * Gets a number of #GValues for the given controlled property starting at the
1349 * requested time. The array @values need to hold enough space for @n_values of
1352 * This function is useful if one wants to e.g. draw a graph of the control
1353 * curve or apply a control curve sample by sample.
1355 * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1358 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1359 GstClockTime timestamp, GstClockTime interval, guint n_values,
1362 gboolean res = FALSE;
1363 GstControlBinding *binding;
1365 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1366 g_return_val_if_fail (property_name, FALSE);
1367 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1368 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1369 g_return_val_if_fail (values, FALSE);
1371 GST_OBJECT_LOCK (object);
1372 if ((binding = gst_object_find_control_binding (object, property_name))) {
1373 res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1376 GST_OBJECT_UNLOCK (object);
1382 * gst_object_get_control_rate:
1383 * @object: the object that has controlled properties
1385 * Obtain the control-rate for this @object. Audio processing #GstElement
1386 * objects will use this rate to sub-divide their processing loop and call
1387 * gst_object_sync_values() inbetween. The length of the processing segment
1388 * should be up to @control-rate nanoseconds.
1390 * If the @object is not under property control, this will return
1391 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1393 * The control-rate is not expected to change if the element is in
1394 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1396 * Returns: the control rate in nanoseconds
1399 gst_object_get_control_rate (GstObject * object)
1401 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1403 return object->control_rate;
1407 * gst_object_set_control_rate:
1408 * @object: the object that has controlled properties
1409 * @control_rate: the new control-rate in nanoseconds.
1411 * Change the control-rate for this @object. Audio processing #GstElement
1412 * objects will use this rate to sub-divide their processing loop and call
1413 * gst_object_sync_values() inbetween. The length of the processing segment
1414 * should be up to @control-rate nanoseconds.
1416 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1417 * %GST_STATE_PLAYING.
1420 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1422 g_return_if_fail (GST_IS_OBJECT (object));
1424 object->control_rate = control_rate;