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 * #GObject. 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 * The most interesting difference between #GstObject and #GObject is the
38 * "floating" reference count. A #GObject is created with a reference count of
39 * 1, owned by the creator of the #GObject. (The owner of a reference is the
40 * code section that has the right to call gst_object_unref() in order to
41 * remove that reference.) A #GstObject is created with a reference count of 1
42 * also, but it isn't owned by anyone; Instead, the initial reference count
43 * of a #GstObject is "floating". The floating reference can be removed by
44 * anyone at any time, by calling gst_object_sink(). gst_object_sink() does
45 * nothing if an object is already sunk (has no floating reference).
47 * When you add a #GstElement to its parent container, the parent container will
51 * gst_object_ref (GST_OBJECT (child_element));
52 * gst_object_sink (GST_OBJECT (child_element));
55 * This means that the container now owns a reference to the child element
56 * (since it called gst_object_ref()), and the child element has no floating
59 * The purpose of the floating reference is to keep the child element alive
60 * until you add it to a parent container, which then manages the lifetime of
64 * element = gst_element_factory_make (factoryname, name);
65 * // element has one floating reference to keep it alive
66 * gst_bin_add (GST_BIN (bin), element);
67 * // element has one non-floating reference owned by the container
71 * Another effect of this is, that calling gst_object_unref() on a bin object,
72 * will also destoy all the #GstElement objects in it. The same is true for
73 * calling gst_bin_remove().
75 * Special care has to be taken for all methods that gst_object_sink() an object
76 * since if the caller of those functions had a floating reference to the object,
77 * the object reference is now invalid.
79 * In contrast to #GObject instances, #GstObject adds a name property. The functions
80 * gst_object_set_name() and gst_object_get_name() are used to set/get the name
84 * <title>controlled properties</title>
86 * Controlled properties offers a lightweight way to adjust gobject
87 * properties over stream-time. It works by using time-stamped value pairs that
88 * are queued for element-properties. At run-time the elements continously pull
89 * values changes for the current stream-time.
91 * What needs to be changed in a #GstElement?
92 * Very little - it is just two steps to make a plugin controllable!
95 * mark gobject-properties paramspecs that make sense to be controlled,
96 * by GST_PARAM_CONTROLLABLE.
99 * when processing data (get, chain, loop function) at the beginning call
100 * gst_object_sync_values(element,timestamp).
101 * This will made the controller to update all gobject properties that are under
102 * control with the current values based on timestamp.
106 * What needs to be done in applications?
107 * Again its not a lot to change.
110 * first put some properties under control, by calling
111 * gst_object_control_properties (object, "prop1", "prop2",...);
114 * create a #GstControlSource.
115 * csource = gst_interpolation_control_source_new ();
116 * gst_interpolation_control_source_set_interpolation_mode(csource, mode);
119 * Attach the #GstControlSource on the controller to a property.
120 * gst_object_set_control_source (object, "prop1", csource);
123 * Set the control values
124 * gst_interpolation_control_source_set (csource,0 * GST_SECOND, value1);
125 * gst_interpolation_control_source_set (csource,1 * GST_SECOND, value2);
128 * start your pipeline
134 * Last reviewed on 2005-11-09 (0.9.4)
137 #include "gst_private.h"
138 #include "glib-compat-private.h"
140 #include "gstobject.h"
141 #include "gstmarshal.h"
142 #include "gstclock.h"
143 #include "gstcontrolsource.h"
145 #include "gstparamspecs.h"
146 #include "gstutils.h"
148 #ifndef GST_DISABLE_TRACE
149 #include "gsttrace.h"
150 static GstAllocTrace *_gst_object_trace;
153 #define DEBUG_REFCOUNT
155 /* Object signals and args */
176 /* maps type name quark => count */
177 static GData *object_name_counts = NULL;
179 G_LOCK_DEFINE_STATIC (object_name_mutex);
181 static void gst_object_set_property (GObject * object, guint prop_id,
182 const GValue * value, GParamSpec * pspec);
183 static void gst_object_get_property (GObject * object, guint prop_id,
184 GValue * value, GParamSpec * pspec);
186 static void gst_object_dispatch_properties_changed (GObject * object,
187 guint n_pspecs, GParamSpec ** pspecs);
189 static void gst_object_dispose (GObject * object);
190 static void gst_object_finalize (GObject * object);
192 static gboolean gst_object_set_name_default (GstObject * object);
194 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
196 static GParamSpec *properties[PROP_LAST];
199 * GstControlledProperty:
201 typedef struct _GstControlledProperty
203 GParamSpec *pspec; /* GParamSpec for this property */
204 const gchar *name; /* name of the property */
205 GstControlSource *csource; /* GstControlSource for this property */
208 } GstControlledProperty;
210 static void gst_controlled_property_free (GstControlledProperty * prop);
213 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
216 gst_object_class_init (GstObjectClass * klass)
218 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
220 #ifndef GST_DISABLE_TRACE
221 _gst_object_trace = gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT));
224 gobject_class->set_property = gst_object_set_property;
225 gobject_class->get_property = gst_object_get_property;
227 properties[PROP_NAME] =
228 g_param_spec_string ("name", "Name", "The name of the object", NULL,
229 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
230 g_object_class_install_property (gobject_class, PROP_NAME,
231 properties[PROP_NAME]);
233 properties[PROP_PARENT] =
234 g_param_spec_object ("parent", "Parent", "The parent of the object",
235 GST_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
236 g_object_class_install_property (gobject_class, PROP_PARENT,
237 properties[PROP_PARENT]);
240 * GstObject::deep-notify:
241 * @gstobject: a #GstObject
242 * @prop_object: the object that originated the signal
243 * @prop: the property that changed
245 * The deep notify signal is used to be notified of property changes. It is
246 * typically attached to the toplevel bin to receive notifications from all
247 * the elements contained in that bin.
249 gst_object_signals[DEEP_NOTIFY] =
250 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
251 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
252 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
253 NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
256 klass->path_string_separator = "/";
258 /* see the comments at gst_object_dispatch_properties_changed */
259 gobject_class->dispatch_properties_changed
260 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
262 gobject_class->dispose = gst_object_dispose;
263 gobject_class->finalize = gst_object_finalize;
267 gst_object_init (GstObject * object)
269 object->lock = g_mutex_new ();
270 object->parent = NULL;
272 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
274 #ifndef GST_DISABLE_TRACE
275 gst_alloc_trace_new (_gst_object_trace, object);
280 object->control_rate = 100 * GST_MSECOND;
281 object->last_sync = GST_CLOCK_TIME_NONE;
286 * @object: a #GstObject to reference
288 * Increments the reference count on @object. This function
289 * does not take the lock on @object because it relies on
290 * atomic refcounting.
292 * This object returns the input parameter to ease writing
294 * result = gst_object_ref (object->parent);
296 * Returns: (transfer full): A pointer to @object
299 gst_object_ref (gpointer object)
301 g_return_val_if_fail (object != NULL, NULL);
303 #ifdef DEBUG_REFCOUNT
304 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
305 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
307 g_object_ref (object);
314 * @object: a #GstObject to unreference
316 * Decrements the reference count on @object. If reference count hits
317 * zero, destroy @object. This function does not take the lock
318 * on @object as it relies on atomic refcounting.
320 * The unref method should never be called with the LOCK held since
321 * this might deadlock the dispose function.
324 gst_object_unref (gpointer object)
326 g_return_if_fail (object != NULL);
327 g_return_if_fail (((GObject *) object)->ref_count > 0);
329 #ifdef DEBUG_REFCOUNT
330 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
331 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
333 g_object_unref (object);
337 * gst_object_ref_sink: (skip)
338 * @object: a #GstObject to sink
340 * Increase the reference count of @object, and possibly remove the floating
341 * reference, if @object has a floating reference.
343 * In other words, if the object is floating, then this call "assumes ownership"
344 * of the floating reference, converting it to a normal reference by clearing
345 * the floating flag while leaving the reference count unchanged. If the object
346 * is not floating, then this call adds a new normal reference increasing the
347 * reference count by one.
350 gst_object_ref_sink (gpointer object)
352 g_return_val_if_fail (object != NULL, NULL);
354 #ifdef DEBUG_REFCOUNT
355 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
356 object, ((GObject *) object)->ref_count,
357 ((GObject *) object)->ref_count + 1);
359 return g_object_ref_sink (object);
363 * gst_object_replace:
364 * @oldobj: (inout) (transfer full): pointer to a place of a #GstObject to
366 * @newobj: (transfer none): a new #GstObject
368 * Atomically modifies a pointer to point to a new object.
369 * The reference count of @oldobj is decreased and the reference count of
370 * @newobj is increased.
372 * Either @newobj and the value pointed to by @oldobj may be NULL.
374 * Returns: TRUE if @newobj was different from @oldobj
377 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
381 g_return_val_if_fail (oldobj != NULL, FALSE);
383 #ifdef DEBUG_REFCOUNT
384 GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
385 *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
386 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
387 newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
388 newobj ? G_OBJECT (newobj)->ref_count : 0);
391 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
393 if (G_UNLIKELY (oldptr == newobj))
397 g_object_ref (newobj);
399 while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
400 oldobj, oldptr, newobj))) {
401 oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
402 if (G_UNLIKELY (oldptr == newobj))
407 g_object_unref (oldptr);
409 return oldptr != newobj;
412 /* dispose is called when the object has to release all links
413 * to other objects */
415 gst_object_dispose (GObject * object)
417 GstObject *self = (GstObject *) object;
420 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
422 GST_OBJECT_LOCK (object);
423 if ((parent = GST_OBJECT_PARENT (object)))
425 GST_OBJECT_PARENT (object) = NULL;
426 GST_OBJECT_UNLOCK (object);
428 if (self->properties) {
431 for (node = self->properties; node; node = g_list_next (node)) {
432 gst_controlled_property_free ((GstControlledProperty *) node->data);
434 g_list_free (self->properties);
435 self->properties = NULL;
438 ((GObjectClass *) gst_object_parent_class)->dispose (object);
445 g_critical ("\nTrying to dispose object \"%s\", but it still has a "
446 "parent \"%s\".\nYou need to let the parent manage the "
447 "object instead of unreffing the object directly.\n",
448 GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
449 GST_OBJECT_UNLOCK (object);
450 /* ref the object again to revive it in this error case */
451 gst_object_ref (object);
456 /* finalize is called when the object has to free its resources */
458 gst_object_finalize (GObject * object)
460 GstObject *gstobject = GST_OBJECT_CAST (object);
462 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
464 g_signal_handlers_destroy (object);
466 g_free (gstobject->name);
467 g_mutex_free (gstobject->lock);
469 #ifndef GST_DISABLE_TRACE
470 gst_alloc_trace_free (_gst_object_trace, object);
473 ((GObjectClass *) gst_object_parent_class)->finalize (object);
476 /* Changing a GObject property of a GstObject will result in "deep-notify"
477 * signals being emitted by the object itself, as well as in each parent
478 * object. This is so that an application can connect a listener to the
479 * top-level bin to catch property-change notifications for all contained
485 gst_object_dispatch_properties_changed (GObject * object,
486 guint n_pspecs, GParamSpec ** pspecs)
488 GstObject *gst_object, *parent, *old_parent;
490 #ifndef GST_DISABLE_GST_DEBUG
492 const gchar *debug_name;
495 /* do the standard dispatching */
497 gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
500 gst_object = GST_OBJECT_CAST (object);
501 #ifndef GST_DISABLE_GST_DEBUG
502 if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
503 name = gst_object_get_name (gst_object);
504 debug_name = GST_STR_NULL (name);
509 /* now let the parent dispatch those, too */
510 parent = gst_object_get_parent (gst_object);
512 for (i = 0; i < n_pspecs; i++) {
513 GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
514 "deep notification from %s (%s)", debug_name, pspecs[i]->name);
516 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
517 g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
521 parent = gst_object_get_parent (old_parent);
522 gst_object_unref (old_parent);
524 #ifndef GST_DISABLE_GST_DEBUG
530 * gst_object_default_deep_notify:
531 * @object: the #GObject that signalled the notify.
532 * @orig: a #GstObject that initiated the notify.
533 * @pspec: a #GParamSpec of the property.
534 * @excluded_props: (array zero-terminated=1) (element-type gchar*)
535 * (allow-none):a set of user-specified properties to exclude or
536 * NULL to show all changes.
538 * A default deep_notify signal callback for an object. The user data
539 * should contain a pointer to an array of strings that should be excluded
540 * from the notify. The default handler will print the new value of the property
543 * MT safe. This function grabs and releases @object's LOCK for getting its
547 gst_object_default_deep_notify (GObject * object, GstObject * orig,
548 GParamSpec * pspec, gchar ** excluded_props)
550 GValue value = { 0, }; /* the important thing is that value.type = 0 */
554 if (pspec->flags & G_PARAM_READABLE) {
555 /* let's not print these out for excluded properties... */
556 while (excluded_props != NULL && *excluded_props != NULL) {
557 if (strcmp (pspec->name, *excluded_props) == 0)
561 g_value_init (&value, pspec->value_type);
562 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
564 /* FIXME: handle flags */
565 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
566 GEnumValue *enum_value;
567 GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
569 enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
571 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
573 g_type_class_unref (klass);
575 str = g_strdup_value_contents (&value);
577 name = gst_object_get_path_string (orig);
578 g_print ("%s: %s = %s\n", name, pspec->name, str);
581 g_value_unset (&value);
583 name = gst_object_get_path_string (orig);
584 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
590 gst_object_set_name_default (GstObject * object)
592 const gchar *type_name;
598 /* to ensure guaranteed uniqueness across threads, only one thread
599 * may ever assign a name */
600 G_LOCK (object_name_mutex);
602 if (!object_name_counts) {
603 g_datalist_init (&object_name_counts);
606 q = g_type_qname (G_OBJECT_TYPE (object));
607 count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
608 g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
610 G_UNLOCK (object_name_mutex);
612 /* GstFooSink -> foosink<N> */
613 type_name = g_quark_to_string (q);
614 if (strncmp (type_name, "Gst", 3) == 0)
616 name = g_strdup_printf ("%s%d", type_name, count);
618 for (i = 0; i < l; i++)
619 name[i] = g_ascii_tolower (name[i]);
621 GST_OBJECT_LOCK (object);
622 if (G_UNLIKELY (object->parent != NULL))
625 g_free (object->name);
628 GST_OBJECT_UNLOCK (object);
635 GST_WARNING ("parented objects can't be renamed");
636 GST_OBJECT_UNLOCK (object);
642 * gst_object_set_name:
643 * @object: a #GstObject
644 * @name: new name of object
646 * Sets the name of @object, or gives @object a guaranteed unique
647 * name (if @name is NULL).
648 * This function makes a copy of the provided name, so the caller
649 * retains ownership of the name it sent.
651 * Returns: TRUE if the name could be set. Since Objects that have
652 * a parent cannot be renamed, this function returns FALSE in those
655 * MT safe. This function grabs and releases @object's LOCK.
658 gst_object_set_name (GstObject * object, const gchar * name)
662 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
664 GST_OBJECT_LOCK (object);
666 /* parented objects cannot be renamed */
667 if (G_UNLIKELY (object->parent != NULL))
671 g_free (object->name);
672 object->name = g_strdup (name);
673 GST_OBJECT_UNLOCK (object);
676 GST_OBJECT_UNLOCK (object);
677 result = gst_object_set_name_default (object);
679 /* FIXME-0.11: this misses a g_object_notify (object, "name"); unless called
680 * from gst_object_set_property.
681 * Ideally remove such custom setters (or make it static).
688 GST_WARNING ("parented objects can't be renamed");
689 GST_OBJECT_UNLOCK (object);
695 * gst_object_get_name:
696 * @object: a #GstObject
698 * Returns a copy of the name of @object.
699 * Caller should g_free() the return value after usage.
700 * For a nameless object, this returns NULL, which you can safely g_free()
703 * Free-function: g_free
705 * Returns: (transfer full): the name of @object. g_free() after usage.
707 * MT safe. This function grabs and releases @object's LOCK.
710 gst_object_get_name (GstObject * object)
712 gchar *result = NULL;
714 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
716 GST_OBJECT_LOCK (object);
717 result = g_strdup (object->name);
718 GST_OBJECT_UNLOCK (object);
724 * gst_object_set_parent:
725 * @object: a #GstObject
726 * @parent: new parent of object
728 * Sets the parent of @object to @parent. The object's reference count will
729 * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
731 * Returns: TRUE if @parent could be set or FALSE when @object
732 * already had a parent or @object and @parent are the same.
734 * MT safe. Grabs and releases @object's LOCK.
737 gst_object_set_parent (GstObject * object, GstObject * parent)
739 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
740 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
741 g_return_val_if_fail (object != parent, FALSE);
743 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
744 "set parent (ref and sink)");
746 GST_OBJECT_LOCK (object);
747 if (G_UNLIKELY (object->parent != NULL))
750 object->parent = parent;
751 gst_object_ref_sink (object);
752 GST_OBJECT_UNLOCK (object);
754 /* FIXME, this does not work, the deep notify takes the lock from the parent
755 * object and deadlocks when the parent holds its lock when calling this
756 * function (like _element_add_pad()) */
757 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
764 GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
765 "set parent failed, object already had a parent");
766 GST_OBJECT_UNLOCK (object);
772 * gst_object_get_parent:
773 * @object: a #GstObject
775 * Returns the parent of @object. This function increases the refcount
776 * of the parent object so you should gst_object_unref() it after usage.
778 * Returns: (transfer full): parent of @object, this can be NULL if @object
779 * has no parent. unref after usage.
781 * MT safe. Grabs and releases @object's LOCK.
784 gst_object_get_parent (GstObject * object)
786 GstObject *result = NULL;
788 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
790 GST_OBJECT_LOCK (object);
791 result = object->parent;
792 if (G_LIKELY (result))
793 gst_object_ref (result);
794 GST_OBJECT_UNLOCK (object);
800 * gst_object_unparent:
801 * @object: a #GstObject to unparent
803 * Clear the parent of @object, removing the associated reference.
804 * This function decreases the refcount of @object.
806 * MT safe. Grabs and releases @object's lock.
809 gst_object_unparent (GstObject * object)
813 g_return_if_fail (GST_IS_OBJECT (object));
815 GST_OBJECT_LOCK (object);
816 parent = object->parent;
818 if (G_LIKELY (parent != NULL)) {
819 GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
820 object->parent = NULL;
821 GST_OBJECT_UNLOCK (object);
823 /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
825 gst_object_unref (object);
827 GST_OBJECT_UNLOCK (object);
832 * gst_object_has_ancestor:
833 * @object: a #GstObject to check
834 * @ancestor: a #GstObject to check as ancestor
836 * Check if @object has an ancestor @ancestor somewhere up in
837 * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
839 * Returns: TRUE if @ancestor is an ancestor of @object.
841 * MT safe. Grabs and releases @object's locks.
844 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
846 GstObject *parent, *tmp;
848 if (!ancestor || !object)
851 parent = gst_object_ref (object);
853 if (parent == ancestor) {
854 gst_object_unref (parent);
858 tmp = gst_object_get_parent (parent);
859 gst_object_unref (parent);
867 * gst_object_check_uniqueness:
868 * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
870 * @name: the name to search for
872 * Checks to see if there is any object named @name in @list. This function
873 * does not do any locking of any kind. You might want to protect the
874 * provided list with the lock of the owner of the list. This function
875 * will lock each #GstObject in the list to compare the name, so be
876 * carefull when passing a list with a locked object.
878 * Returns: TRUE if a #GstObject named @name does not appear in @list,
881 * MT safe. Grabs and releases the LOCK of each object in the list.
884 gst_object_check_uniqueness (GList * list, const gchar * name)
886 gboolean result = TRUE;
888 g_return_val_if_fail (name != NULL, FALSE);
890 for (; list; list = g_list_next (list)) {
894 child = GST_OBJECT_CAST (list->data);
896 GST_OBJECT_LOCK (child);
897 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
898 GST_OBJECT_UNLOCK (child);
900 if (G_UNLIKELY (eq)) {
910 gst_object_set_property (GObject * object, guint prop_id,
911 const GValue * value, GParamSpec * pspec)
913 GstObject *gstobject;
915 gstobject = GST_OBJECT_CAST (object);
919 gst_object_set_name (gstobject, g_value_get_string (value));
922 gst_object_set_parent (gstobject, g_value_get_object (value));
925 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
931 gst_object_get_property (GObject * object, guint prop_id,
932 GValue * value, GParamSpec * pspec)
934 GstObject *gstobject;
936 gstobject = GST_OBJECT_CAST (object);
940 g_value_take_string (value, gst_object_get_name (gstobject));
943 g_value_take_object (value, gst_object_get_parent (gstobject));
946 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
952 * gst_object_get_path_string:
953 * @object: a #GstObject
955 * Generates a string describing the path of @object in
956 * the object hierarchy. Only useful (or used) for debugging.
958 * Free-function: g_free
960 * Returns: (transfer full): a string describing the path of @object. You must
961 * g_free() the string after usage.
963 * MT safe. Grabs and releases the #GstObject's LOCK for all objects
967 gst_object_get_path_string (GstObject * object)
972 gchar *prevpath, *path;
973 const gchar *typename;
975 const gchar *separator;
977 /* ref object before adding to list */
978 gst_object_ref (object);
979 parentage = g_slist_prepend (NULL, object);
981 path = g_strdup ("");
983 /* first walk the object hierarchy to build a list of the parents,
984 * be carefull here with refcounting. */
986 if (GST_IS_OBJECT (object)) {
987 parent = gst_object_get_parent (object);
988 /* add parents to list, refcount remains increased while
989 * we handle the object */
991 parentage = g_slist_prepend (parentage, parent);
996 } while (object != NULL);
998 /* then walk the parent list and print them out. we need to
999 * decrease the refcounting on each element after we handled
1001 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1002 if (G_IS_OBJECT (parents->data)) {
1003 typename = G_OBJECT_TYPE_NAME (parents->data);
1007 if (GST_IS_OBJECT (parents->data)) {
1008 GstObject *item = GST_OBJECT_CAST (parents->data);
1009 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1010 gchar *objname = gst_object_get_name (item);
1012 component = g_strdup_printf ("%s:%s", typename, objname);
1013 separator = oclass->path_string_separator;
1015 gst_object_unref (item);
1019 component = g_strdup_printf ("%s:%p", typename, parents->data);
1021 component = g_strdup_printf ("%p", parents->data);
1027 path = g_strjoin (separator, prevpath, component, NULL);
1032 g_slist_free (parentage);
1037 /* controller helper functions */
1040 * gst_controlled_property_new:
1041 * @object: for which object the controlled property should be set up
1042 * @name: the name of the property to be controlled
1044 * Private method which initializes the fields of a new controlled property
1047 * Returns: a freshly allocated structure or %NULL
1049 static GstControlledProperty *
1050 gst_controlled_property_new (GstObject * object, const gchar * name)
1052 GstControlledProperty *prop = NULL;
1055 GST_INFO ("trying to put property '%s' under control", name);
1057 /* check if the object has a property of that name */
1059 g_object_class_find_property (G_OBJECT_GET_CLASS (object), name))) {
1060 GST_DEBUG (" psec->flags : 0x%08x", pspec->flags);
1062 /* check if this param is witable && controlable && !construct-only */
1063 g_return_val_if_fail ((pspec->flags & (G_PARAM_WRITABLE |
1064 GST_PARAM_CONTROLLABLE | G_PARAM_CONSTRUCT_ONLY)) ==
1065 (G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE), NULL);
1067 if ((prop = g_slice_new (GstControlledProperty))) {
1068 prop->pspec = pspec;
1069 prop->name = pspec->name;
1070 prop->csource = NULL;
1071 prop->disabled = FALSE;
1072 memset (&prop->last_value, 0, sizeof (GValue));
1073 g_value_init (&prop->last_value, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1076 GST_WARNING ("class '%s' has no property '%s'", G_OBJECT_TYPE_NAME (object),
1083 * gst_controlled_property_free:
1084 * @prop: the object to free
1086 * Private method which frees all data allocated by a #GstControlledProperty
1090 gst_controlled_property_free (GstControlledProperty * prop)
1093 g_object_unref (prop->csource);
1094 g_value_unset (&prop->last_value);
1095 g_slice_free (GstControlledProperty, prop);
1099 * gst_object_find_controlled_property:
1100 * @self: the gobject to search for a property in
1101 * @name: the gobject property name to look for
1103 * Searches the list of properties under control.
1105 * Returns: a #GstControlledProperty or %NULL if the property is not being
1108 static GstControlledProperty *
1109 gst_object_find_controlled_property (GstObject * self, const gchar * name)
1111 GstControlledProperty *prop;
1114 for (node = self->properties; node; node = g_list_next (node)) {
1116 /* FIXME: eventually use GQuark to speed it up */
1117 if (!strcmp (prop->name, name)) {
1121 GST_DEBUG ("controller does not (yet) manage property '%s'", name);
1126 /* controller functions */
1129 * gst_object_suggest_next_sync:
1130 * @object: the object that has controlled properties
1132 * Returns a suggestion for timestamps where buffers should be split
1133 * to get best controller results.
1135 * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1136 * if no control-rate was set.
1139 gst_object_suggest_next_sync (GstObject * object)
1143 g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1144 g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1145 GST_CLOCK_TIME_NONE);
1147 GST_OBJECT_LOCK (object);
1149 /* TODO: Implement more logic, depending on interpolation mode and control
1151 * FIXME: we need playback direction
1153 ret = object->last_sync + object->control_rate;
1155 GST_OBJECT_UNLOCK (object);
1161 * gst_object_sync_values:
1162 * @object: the object that has controlled properties
1163 * @timestamp: the time that should be processed
1165 * Sets the properties of the object, according to the #GstControlSources that
1166 * (maybe) handle them and for the given timestamp.
1168 * If this function fails, it is most likely the application developers fault.
1169 * Most probably the control sources are not setup correctly.
1171 * Returns: %TRUE if the controller values could be applied to the object
1172 * properties, %FALSE otherwise
1175 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1177 GstControlledProperty *prop;
1179 gboolean ret = TRUE, val_ret;
1180 GValue value = { 0, };
1182 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1183 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1185 GST_LOG ("sync_values");
1187 /* FIXME: this deadlocks */
1188 /* GST_OBJECT_LOCK (object); */
1189 g_object_freeze_notify ((GObject *) object);
1190 /* go over the controlled properties of the controller */
1191 for (node = object->properties; node; node = g_list_next (node)) {
1197 GST_LOG ("property '%s' at ts=%" G_GUINT64_FORMAT, prop->name, timestamp);
1199 /* we can make this faster
1200 * http://bugzilla.gnome.org/show_bug.cgi?id=536939
1202 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1203 val_ret = gst_control_source_get_value (prop->csource, timestamp, &value);
1204 if (G_LIKELY (val_ret)) {
1205 /* always set the value for first time, but then only if it changed
1206 * this should limit g_object_notify invocations.
1207 * FIXME: can we detect negative playback rates?
1209 if ((timestamp < object->last_sync) ||
1210 gst_value_compare (&value, &prop->last_value) != GST_VALUE_EQUAL) {
1211 g_object_set_property ((GObject *) object, prop->name, &value);
1212 g_value_copy (&value, &prop->last_value);
1215 GST_DEBUG ("no control value for param %s", prop->name);
1217 g_value_unset (&value);
1220 object->last_sync = timestamp;
1221 g_object_thaw_notify ((GObject *) object);
1222 /* GST_OBJECT_UNLOCK (object); */
1228 * gst_object_has_active_controlled_properties:
1229 * @object: the object that has controlled properties
1231 * Check if the @object has an active controlled properties.
1233 * Returns: %TRUE if the object has active controlled properties
1236 gst_object_has_active_controlled_properties (GstObject * object)
1238 gboolean res = FALSE;
1240 GstControlledProperty *prop;
1242 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1244 GST_OBJECT_LOCK (object);
1245 for (node = object->properties; node; node = node->next) {
1247 res |= !prop->disabled;
1249 GST_OBJECT_UNLOCK (object);
1254 * gst_object_set_controlled_properties_disabled:
1255 * @object: the object that has controlled properties
1256 * @disabled: boolean that specifies whether to disable the controller
1259 * This function is used to disable all controlled properties of the @object for
1260 * some time, i.e. gst_object_sync_values() will do nothing..
1263 gst_object_set_controlled_properties_disabled (GstObject * object,
1267 GstControlledProperty *prop;
1269 g_return_if_fail (GST_IS_OBJECT (object));
1271 GST_OBJECT_LOCK (object);
1272 for (node = object->properties; node; node = node->next) {
1274 prop->disabled = disabled;
1276 GST_OBJECT_UNLOCK (object);
1280 * gst_object_set_controlled_property_disabled:
1281 * @object: the object that has controlled properties
1282 * @property_name: property to disable
1283 * @disabled: boolean that specifies whether to disable the controller
1286 * This function is used to disable the #GstController on a property for
1287 * some time, i.e. gst_controller_sync_values() will do nothing for the
1291 gst_object_set_controlled_property_disabled (GstObject * object,
1292 const gchar * property_name, gboolean disabled)
1294 GstControlledProperty *prop;
1296 g_return_if_fail (GST_IS_OBJECT (object));
1297 g_return_if_fail (property_name);
1299 GST_OBJECT_LOCK (object);
1300 if ((prop = gst_object_find_controlled_property (object, property_name))) {
1301 prop->disabled = disabled;
1303 GST_OBJECT_UNLOCK (object);
1307 * gst_object_set_control_source:
1308 * @object: the controller object
1309 * @property_name: name of the property for which the #GstControlSource should be set
1310 * @csource: the #GstControlSource that should be used for the property
1312 * Sets the #GstControlSource for @property_name. If there already was a #GstControlSource
1313 * for this property it will be unreferenced.
1315 * Returns: %FALSE if the given property isn't handled by the controller or the new #GstControlSource
1316 * couldn't be bound to the property, %TRUE if everything worked as expected.
1319 gst_object_set_control_source (GstObject * object, const gchar * property_name,
1320 GstControlSource * csource)
1322 GstControlledProperty *prop;
1323 gboolean ret = FALSE;
1325 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1326 g_return_val_if_fail (property_name, FALSE);
1327 g_return_val_if_fail ((!csource || GST_IS_CONTROL_SOURCE (csource)), FALSE);
1329 GST_OBJECT_LOCK (object);
1330 prop = gst_object_find_controlled_property (object, property_name);
1332 if ((prop = gst_controlled_property_new (object, property_name))) {
1333 object->properties = g_list_prepend (object->properties, prop);
1334 GST_DEBUG_OBJECT (object, "controlled property %s added", property_name);
1338 GstControlSource *old = prop->csource;
1340 if (csource != old) {
1341 if (csource && (ret = gst_control_source_bind (csource, prop->pspec))) {
1342 prop->csource = g_object_ref (csource);
1343 } else if (!csource) {
1345 prop->csource = NULL;
1346 object->properties = g_list_remove (object->properties, prop);
1347 //g_signal_handler_disconnect (self->object, prop->notify_handler_id);
1348 gst_controlled_property_free (prop);
1349 GST_DEBUG_OBJECT (object, "controlled property %s removed",
1353 g_object_unref (old);
1356 GST_OBJECT_UNLOCK (object);
1362 * gst_object_get_control_source:
1363 * @object: the object
1364 * @property_name: name of the property for which the #GstControlSource should be get
1366 * Gets the corresponding #GstControlSource for the property. This should be unreferenced
1369 * Returns: (transfer full): the #GstControlSource for @property_name or NULL if
1370 * the property is not controlled by this controller or no #GstControlSource was
1374 gst_object_get_control_source (GstObject * object, const gchar * property_name)
1376 GstControlledProperty *prop;
1377 GstControlSource *ret = NULL;
1379 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1380 g_return_val_if_fail (property_name, NULL);
1382 GST_OBJECT_LOCK (object);
1383 if ((prop = gst_object_find_controlled_property (object, property_name))) {
1384 ret = g_object_ref (prop->csource);
1386 GST_OBJECT_UNLOCK (object);
1392 * gst_object_get_value:
1393 * @object: the object that has controlled properties
1394 * @property_name: the name of the property to get
1395 * @timestamp: the time the control-change should be read from
1397 * Gets the value for the given controllered property at the requested time.
1399 * Returns: the GValue of the property at the given time, or %NULL if the
1400 * property isn't controlled.
1403 gst_object_get_value (GstObject * object, const gchar * property_name,
1404 GstClockTime timestamp)
1406 GstControlledProperty *prop;
1409 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1410 g_return_val_if_fail (property_name, NULL);
1411 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1413 GST_OBJECT_LOCK (object);
1414 if ((prop = gst_object_find_controlled_property (object, property_name))) {
1415 val = g_new0 (GValue, 1);
1416 g_value_init (val, G_PARAM_SPEC_VALUE_TYPE (prop->pspec));
1418 /* get current value via control source */
1419 if (!gst_control_source_get_value (prop->csource, timestamp, val)) {
1424 GST_OBJECT_UNLOCK (object);
1430 * gst_object_get_value_arrays:
1431 * @object: the object that has controlled properties
1432 * @timestamp: the time that should be processed
1433 * @value_arrays: list to return the control-values in
1435 * Function to be able to get an array of values for one or more given element
1438 * If the GstValueArray->values array in list nodes is NULL, it will be created
1440 * The type of the values in the array are the same as the property's type.
1442 * The g_object_* functions are just convenience functions for GObject
1444 * Returns: %TRUE if the given array(s) could be filled, %FALSE otherwise
1447 gst_object_get_value_arrays (GstObject * object, GstClockTime timestamp,
1448 GSList * value_arrays)
1450 gboolean res = TRUE;
1453 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1454 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1455 g_return_val_if_fail (value_arrays, FALSE);
1457 for (node = value_arrays; (res && node); node = g_slist_next (node)) {
1458 res = gst_object_get_value_array (object, timestamp, node->data);
1464 * gst_object_get_value_array:
1465 * @object: the object that has controlled properties
1466 * @timestamp: the time that should be processed
1467 * @value_array: array to put control-values in
1469 * Function to be able to get an array of values for one element properties
1471 * If the GstValueArray->values array is NULL, it will be created by the function.
1472 * The type of the values in the array are the same as the property's type.
1474 * The g_object_* functions are just convenience functions for GObject
1476 * Returns: %TRUE if the given array(s) could be filled, %FALSE otherwise
1479 gst_object_get_value_array (GstObject * object, GstClockTime timestamp,
1480 GstValueArray * value_array)
1482 gboolean res = FALSE;
1483 GstControlledProperty *prop;
1485 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1486 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1487 g_return_val_if_fail (value_array, FALSE);
1488 g_return_val_if_fail (value_array->property_name, FALSE);
1489 g_return_val_if_fail (value_array->values, FALSE);
1491 GST_OBJECT_LOCK (object);
1492 if ((prop = gst_object_find_controlled_property (object,
1493 value_array->property_name))) {
1494 res = gst_control_source_get_value_array (prop->csource, timestamp,
1497 GST_OBJECT_UNLOCK (object);
1502 * gst_object_get_control_rate:
1503 * @object: the object that has controlled properties
1505 * Obtain the control-rate for this @object. Audio processing #GstElement
1506 * objects will use this rate to sub-divide their processing loop and call
1507 * gst_object_sync_values() inbetween. The length of the processing segment
1508 * should be up to @control-rate nanoseconds.
1510 * If the @object is not under property control, this will return
1511 * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1513 * The control-rate is not expected to change if the element is in
1514 * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1516 * Returns: the control rate in nanoseconds
1519 gst_object_get_control_rate (GstObject * object)
1521 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1523 return object->control_rate;
1527 * gst_object_set_control_rate:
1528 * @object: the object that has controlled properties
1529 * @control_rate: the new control-rate in nanoseconds.
1531 * Change the control-rate for this @object. Audio processing #GstElement
1532 * objects will use this rate to sub-divide their processing loop and call
1533 * gst_object_sync_values() inbetween. The length of the processing segment
1534 * should be up to @control-rate nanoseconds.
1536 * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1537 * %GST_STATE_PLAYING.
1540 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1542 g_return_if_fail (GST_IS_OBJECT (object));
1544 object->control_rate = control_rate;