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.
24 #include "gst_private.h"
26 #include "gstobject.h"
27 #include "gstmarshal.h"
31 #ifndef GST_DISABLE_TRACE
35 #define DEBUG_REFCOUNT
38 /* Refcount hack: since glib is not threadsafe, the glib refcounter can be
39 * screwed up and the object can be freed unexpectedly. We use an evil hack
40 * to work around this problem. We set the glib refcount to a high value so
41 * that glib will never unref the object under realistic circumstances. Then
42 * we use our own atomic refcounting to do proper MT safe refcounting.
44 * A proper fix is of course to make the glib refcounting threadsafe which is
45 * planned. Update: atomic refcounting is now in glib >= 2.7.3
48 #define PATCH_REFCOUNT(obj) ((GObject*)(obj))->ref_count = 100000;
49 #define PATCH_REFCOUNT1(obj) ((GObject*)(obj))->ref_count = 1;
51 #define PATCH_REFCOUNT(obj)
52 #define PATCH_REFCOUNT1(obj)
55 /* Object signals and args */
60 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
80 GType _gst_object_type = 0;
81 static GHashTable *object_name_counts = NULL;
83 G_LOCK_DEFINE_STATIC (object_name_mutex);
85 typedef struct _GstSignalObject GstSignalObject;
86 typedef struct _GstSignalObjectClass GstSignalObjectClass;
88 static GType gst_signal_object_get_type (void);
89 static void gst_signal_object_class_init (GstSignalObjectClass * klass);
90 static void gst_signal_object_init (GstSignalObject * object);
92 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
93 static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
96 static void gst_object_class_init (GstObjectClass * klass);
97 static void gst_object_init (GTypeInstance * instance, gpointer g_class);
99 #ifndef GST_DISABLE_TRACE
100 static GObject *gst_object_constructor (GType type,
101 guint n_construct_properties, GObjectConstructParam * construct_params);
104 static void gst_object_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_object_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
108 static void gst_object_dispatch_properties_changed (GObject * object,
109 guint n_pspecs, GParamSpec ** pspecs);
111 static void gst_object_dispose (GObject * object);
112 static void gst_object_finalize (GObject * object);
114 static gboolean gst_object_set_name_default (GstObject * object,
115 const gchar * type_name);
117 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
118 static void gst_object_real_restore_thyself (GstObject * object,
122 static GObjectClass *parent_class = NULL;
123 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
126 gst_object_get_type (void)
128 if (!_gst_object_type) {
129 static const GTypeInfo object_info = {
130 sizeof (GstObjectClass),
133 (GClassInitFunc) gst_object_class_init,
143 g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info,
144 G_TYPE_FLAG_ABSTRACT);
146 return _gst_object_type;
150 gst_object_class_init (GstObjectClass * klass)
152 GObjectClass *gobject_class;
154 gobject_class = (GObjectClass *) klass;
156 parent_class = g_type_class_ref (G_TYPE_OBJECT);
158 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
159 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
161 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME,
162 g_param_spec_string ("name", "Name", "The name of the object",
163 NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
165 gst_object_signals[PARENT_SET] =
166 g_signal_new ("parent-set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
167 G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
168 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
169 gst_object_signals[PARENT_UNSET] =
170 g_signal_new ("parent-unset", G_TYPE_FROM_CLASS (klass),
171 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL,
172 NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
173 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
174 /* FIXME This should be the GType of xmlNodePtr instead of G_TYPE_POINTER */
175 gst_object_signals[OBJECT_SAVED] =
176 g_signal_new ("object-saved", G_TYPE_FROM_CLASS (klass),
177 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, object_saved), NULL,
178 NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
180 klass->restore_thyself = gst_object_real_restore_thyself;
182 gst_object_signals[DEEP_NOTIFY] =
183 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
184 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
185 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
186 NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, G_TYPE_OBJECT,
189 klass->path_string_separator = "/";
190 klass->lock = g_new0 (GStaticRecMutex, 1);
191 g_static_rec_mutex_init (klass->lock);
193 klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
195 /* see the comments at gst_object_dispatch_properties_changed */
196 gobject_class->dispatch_properties_changed
197 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
199 gobject_class->dispose = gst_object_dispose;
200 gobject_class->finalize = gst_object_finalize;
201 #ifndef GST_DISABLE_TRACE
202 gobject_class->constructor = gst_object_constructor;
207 gst_object_init (GTypeInstance * instance, gpointer g_class)
209 GstObject *object = GST_OBJECT (instance);
211 object->lock = g_mutex_new ();
212 object->parent = NULL;
214 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
215 gst_atomic_int_set (&object->refcount, 1);
216 PATCH_REFCOUNT (object);
219 GST_FLAG_SET (object, GST_OBJECT_FLOATING);
222 #ifndef GST_DISABLE_TRACE
224 gst_object_constructor (GType type, guint n_construct_properties,
225 GObjectConstructParam * construct_params)
228 GstAllocTrace *trace;
230 G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_properties,
233 name = g_type_name (type);
235 trace = gst_alloc_trace_get (name);
237 trace = gst_alloc_trace_register (name);
239 gst_alloc_trace_new (trace, obj);
246 * @object: GstObject to reference
248 * Increments the refence count on the object. This function
249 * does not take the lock on the object because it relies on
250 * atomic refcounting.
252 * This object returns the input parameter to ease writing
254 * result = gst_object_ref (object->parent);
256 * Returns: A pointer to the object
259 gst_object_ref (gpointer object)
261 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
263 #ifdef DEBUG_REFCOUNT
265 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d",
267 GST_OBJECT_REFCOUNT_VALUE (object),
268 GST_OBJECT_REFCOUNT_VALUE (object) + 1);
270 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d",
272 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
277 g_atomic_int_inc (&((GstObject *) object)->refcount);
278 PATCH_REFCOUNT (object);
280 /* FIXME, not MT safe because glib is not MT safe */
281 g_object_ref (object);
289 * @object: GstObject to unreference
291 * Decrements the refence count on the object. If reference count hits
292 * zero, destroy the object. This function does not take the lock
293 * on the object as it relies on atomic refcounting.
295 * The unref method should never be called with the LOCK held since
296 * this might deadlock the dispose function.
299 gst_object_unref (gpointer object)
301 g_return_if_fail (GST_IS_OBJECT (object));
304 g_return_if_fail (GST_OBJECT_REFCOUNT_VALUE (object) > 0);
306 g_return_if_fail (((GObject *) object)->ref_count > 0);
309 #ifdef DEBUG_REFCOUNT
311 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d",
313 GST_OBJECT_REFCOUNT_VALUE (object),
314 GST_OBJECT_REFCOUNT_VALUE (object) - 1);
316 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d",
318 ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
323 if (G_UNLIKELY (g_atomic_int_dec_and_test (&((GstObject *) object)->
325 PATCH_REFCOUNT1 (object);
326 g_object_unref (object);
328 PATCH_REFCOUNT (object);
331 /* FIXME, not MT safe because glib is not MT safe */
332 g_object_unref (object);
338 * @object: GstObject to sink
340 * Removes floating reference on an object. Any newly created object has
341 * a refcount of 1 and is FLOATING. This function should be used when
342 * creating a new object to symbolically 'take ownership' of the object.
343 * Use #gst_object_set_parent to have this done for you.
345 * MT safe. This function grabs and releases the object lock.
348 gst_object_sink (gpointer object)
350 g_return_if_fail (GST_IS_OBJECT (object));
352 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
355 if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
356 GST_FLAG_UNSET (object, GST_OBJECT_FLOATING);
358 gst_object_unref (object);
365 * gst_object_replace:
366 * @oldobj: pointer to place of old GstObject
367 * @newobj: new GstObject
369 * Unrefs the object pointer to by oldobj, refs the newobj and
370 * puts the newobj in *oldobj. Be carefull when calling this
371 * function, it does not take any locks. You might want to lock
372 * the object owning the oldobj pointer before calling this
375 * Make sure not to LOCK the oldobj because it might be unreffed
376 * which could cause a deadlock when it is disposed.
379 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
381 g_return_if_fail (oldobj != NULL);
382 g_return_if_fail (*oldobj == NULL || GST_IS_OBJECT (*oldobj));
383 g_return_if_fail (newobj == NULL || GST_IS_OBJECT (newobj));
385 #ifdef DEBUG_REFCOUNT
387 GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s (%d) with %s (%d)",
388 *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
389 *oldobj ? GST_OBJECT_REFCOUNT_VALUE (*oldobj) : 0,
390 newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
391 newobj ? GST_OBJECT_REFCOUNT_VALUE (newobj) : 0);
393 GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s (%d) with %s (%d)",
394 *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
395 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
396 newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
397 newobj ? G_OBJECT (newobj)->ref_count : 0);
401 if (G_LIKELY (*oldobj != newobj)) {
403 gst_object_ref (newobj);
405 gst_object_unref (*oldobj);
411 /* dispose is called when the object has to release all links
412 * to other objects */
414 gst_object_dispose (GObject * object)
416 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
419 GST_FLAG_SET (GST_OBJECT (object), GST_OBJECT_DESTROYED);
420 GST_OBJECT_PARENT (object) = NULL;
423 /* need to patch refcount so it is finalized */
424 PATCH_REFCOUNT1 (object)
426 parent_class->dispose (object);
429 /* finalize is called when the object has to free its resources */
431 gst_object_finalize (GObject * object)
433 GstObject *gstobject = GST_OBJECT (object);
435 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
437 g_signal_handlers_destroy (object);
439 g_free (gstobject->name);
440 g_mutex_free (gstobject->lock);
442 #ifndef GST_DISABLE_TRACE
445 GstAllocTrace *trace;
447 name = g_type_name (G_OBJECT_TYPE (object));
448 trace = gst_alloc_trace_get (name);
450 gst_alloc_trace_free (trace, object);
454 parent_class->finalize (object);
457 /* Changing a GObject property of a GstObject will result in "deep_notify"
458 * signals being emitted by the object itself, as well as in each parent
459 * object. This is so that an application can connect a listener to the
460 * top-level bin to catch property-change notifications for all contained
463 * This function is not MT safe in glib so we need to lock it with a
469 gst_object_dispatch_properties_changed (GObject * object,
470 guint n_pspecs, GParamSpec ** pspecs)
472 GstObject *gst_object, *parent, *old_parent;
474 gchar *name, *debug_name;
475 GstObjectClass *klass;
477 /* we fail when this is not a GstObject */
478 g_return_if_fail (GST_IS_OBJECT (object));
480 klass = GST_OBJECT_GET_CLASS (object);
482 GST_CLASS_LOCK (klass);
483 /* do the standard dispatching */
484 PATCH_REFCOUNT (object);
485 G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object, n_pspecs,
487 PATCH_REFCOUNT (object);
489 gst_object = GST_OBJECT_CAST (object);
490 name = gst_object_get_name (gst_object);
491 debug_name = GST_STR_NULL (name);
493 /* now let the parent dispatch those, too */
494 parent = gst_object_get_parent (gst_object);
496 /* for debugging ... */
497 gchar *parent_name = gst_object_get_name (parent);
499 #ifndef GST_DISABLE_GST_DEBUG
500 gchar *debug_parent_name = GST_STR_NULL (parent_name);
503 /* need own category? */
504 for (i = 0; i < n_pspecs; i++) {
505 GST_CAT_LOG (GST_CAT_EVENT, "deep notification from %s to %s (%s)",
506 debug_name, debug_parent_name, pspecs[i]->name);
508 /* not MT safe because of glib, fixed by taking class lock higher up */
509 PATCH_REFCOUNT (parent);
510 PATCH_REFCOUNT (object);
511 g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
512 g_quark_from_string (pspecs[i]->name), GST_OBJECT_CAST (object),
514 PATCH_REFCOUNT (parent);
515 PATCH_REFCOUNT (object);
517 g_free (parent_name);
520 parent = gst_object_get_parent (old_parent);
521 gst_object_unref (old_parent);
524 GST_CLASS_UNLOCK (klass);
528 * gst_object_default_deep_notify:
529 * @object: the #GObject that signalled the notify.
530 * @orig: a #GstObject that initiated the notify.
531 * @pspec: a #GParamSpec of the property.
532 * @excluded_props: a set of user-specified properties to exclude or
533 * NULL to show all changes.
535 * A default deep_notify signal callback for an element. The user data
536 * should contain a pointer to an array of strings that should be excluded
537 * from the notify. The default handler will print the new value of the property
540 * MT safe. This function grabs and releases the object's LOCK or getting the
541 * path string of the object.
544 gst_object_default_deep_notify (GObject * object, GstObject * orig,
545 GParamSpec * pspec, gchar ** excluded_props)
547 GValue value = { 0, }; /* the important thing is that value.type = 0 */
551 if (pspec->flags & G_PARAM_READABLE) {
552 /* let's not print these out for excluded properties... */
553 while (excluded_props != NULL && *excluded_props != NULL) {
554 if (strcmp (pspec->name, *excluded_props) == 0)
558 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
559 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
561 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
562 GEnumValue *enum_value;
565 g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
566 g_value_get_enum (&value));
568 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
571 str = g_strdup_value_contents (&value);
573 name = gst_object_get_path_string (orig);
574 g_print ("%s: %s = %s\n", name, pspec->name, str);
577 g_value_unset (&value);
579 name = gst_object_get_path_string (orig);
580 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
586 gst_object_set_name_default (GstObject * object, const gchar * type_name)
592 /* to ensure guaranteed uniqueness across threads, only one thread
593 * may ever assign a name */
594 G_LOCK (object_name_mutex);
596 if (!object_name_counts) {
597 object_name_counts = g_hash_table_new_full (g_str_hash, g_str_equal,
601 count = GPOINTER_TO_INT (g_hash_table_lookup (object_name_counts, type_name));
602 g_hash_table_insert (object_name_counts, g_strdup (type_name),
603 GINT_TO_POINTER (count + 1));
605 G_UNLOCK (object_name_mutex);
607 /* GstFooSink -> foosinkN */
608 if (strncmp (type_name, "Gst", 3) == 0)
610 tmp = g_strdup_printf ("%s%d", type_name, count);
611 name = g_ascii_strdown (tmp, strlen (tmp));
614 result = gst_object_set_name (object, name);
621 * gst_object_set_name:
622 * @object: a #GstObject to set the name of
623 * @name: new name of object
625 * Sets the name of the object, or gives the object a guaranteed unique
626 * name (if @name is NULL).
627 * This function makes a copy of the provided name, so the caller
628 * retains ownership of the name it sent.
630 * Returns: TRUE if the name could be set. Objects that have
631 * a parent cannot be renamed.
633 * MT safe. This function grabs and releases the object's LOCK.
636 gst_object_set_name (GstObject * object, const gchar * name)
640 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
644 /* parented objects cannot be renamed */
645 if (G_UNLIKELY (object->parent != NULL))
649 g_free (object->name);
650 object->name = g_strdup (name);
655 result = gst_object_set_name_default (object, G_OBJECT_TYPE_NAME (object));
668 * gst_object_get_name:
669 * @object: a #GstObject to get the name of
671 * Returns a copy of the name of the object.
672 * Caller should g_free() the return value after usage.
673 * For a nameless object, this returns NULL, which you can safely g_free()
676 * Returns: the name of the object. g_free() after usage.
678 * MT safe. This function grabs and releases the object's LOCK.
681 gst_object_get_name (GstObject * object)
683 gchar *result = NULL;
685 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
688 result = g_strdup (object->name);
695 * gst_object_set_name_prefix:
696 * @object: a #GstObject to set the name prefix of
697 * @name_prefix: new name prefix of object
699 * Sets the name prefix of the object.
700 * This function makes a copy of the provided name prefix, so the caller
701 * retains ownership of the name prefix it sent.
703 * MT safe. This function grabs and releases the object's LOCK.
706 gst_object_set_name_prefix (GstObject * object, const gchar * name_prefix)
708 g_return_if_fail (GST_IS_OBJECT (object));
711 g_free (object->name_prefix);
712 object->name_prefix = g_strdup (name_prefix); /* NULL gives NULL */
717 * gst_object_get_name_prefix:
718 * @object: a #GstObject to get the name prefix of
720 * Returns a copy of the name prefix of the object.
721 * Caller should g_free() the return value after usage.
722 * For a prefixless object, this returns NULL, which you can safely g_free()
725 * Returns: the name prefix of the object. g_free() after usage.
727 * MT safe. This function grabs and releases the object's LOCK.
730 gst_object_get_name_prefix (GstObject * object)
732 gchar *result = NULL;
734 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
737 result = g_strdup (object->name_prefix);
744 * gst_object_set_parent:
745 * @object: GstObject to set parent of
746 * @parent: new parent of object
748 * Sets the parent of @object. The object's reference count will be incremented,
749 * and any floating reference will be removed (see gst_object_sink()).
751 * Causes the parent-set signal to be emitted.
753 * Returns: TRUE if the parent could be set or FALSE when the object
754 * already had a parent, the object and parent are the same or wrong
755 * parameters were provided.
757 * MT safe. Grabs and releases the object's LOCK.
760 gst_object_set_parent (GstObject * object, GstObject * parent)
762 g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
763 g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
764 g_return_val_if_fail (object != parent, FALSE);
766 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "set parent (ref and sink)");
769 if (G_UNLIKELY (object->parent != NULL))
772 /* sink object, we don't call our own function because we don't
773 * need to release/acquire the lock needlessly or touch the refcount
774 * in the floating case. */
775 object->parent = parent;
776 if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
777 GST_FLAG_UNSET (object, GST_OBJECT_FLOATING);
781 gst_object_ref (object);
784 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
797 * gst_object_get_parent:
798 * @object: GstObject to get parent of
800 * Returns the parent of @object. This function increases the refcount
801 * of the parent object so you should gst_object_unref() it after usage.
803 * Returns: parent of the object, this can be NULL if the object has no
804 * parent. unref after usage.
806 * MT safe. Grabs and releases the object's LOCK.
809 gst_object_get_parent (GstObject * object)
811 GstObject *result = NULL;
813 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
816 result = object->parent;
817 if (G_LIKELY (result))
818 gst_object_ref (result);
825 * gst_object_unparent:
826 * @object: GstObject to unparent
828 * Clear the parent of @object, removing the associated reference.
829 * This function decreases the refcount of the object so the object
830 * might not point to valid memory anymore after calling this function.
832 * MT safe. Grabs and releases the object's lock.
835 gst_object_unparent (GstObject * object)
839 g_return_if_fail (GST_IS_OBJECT (object));
842 parent = object->parent;
844 if (G_LIKELY (parent != NULL)) {
845 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
846 object->parent = NULL;
849 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_UNSET], 0,
852 gst_object_unref (object);
859 * gst_object_check_uniqueness:
860 * @list: a list of #GstObject to check through
861 * @name: the name to search for
863 * Checks to see if there is any object named @name in @list. This function
864 * does not do any locking of any kind. You might want to protect the
865 * provided list with the lock of the owner of the list. This function
866 * will lock each GstObject in the list to compare the name, so be
867 * carefull when passing a list with a locked object.
869 * Returns: TRUE if the name does not appear in the list, FALSE if it does.
871 * MT safe. Grabs and releases the LOCK of each object in the list.
874 gst_object_check_uniqueness (GList * list, const gchar * name)
876 gboolean result = TRUE;
878 g_return_val_if_fail (name != NULL, FALSE);
880 for (; list; list = g_list_next (list)) {
884 child = GST_OBJECT (list->data);
887 eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
890 if (G_UNLIKELY (eq)) {
899 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
901 * gst_object_save_thyself:
902 * @object: GstObject to save
903 * @parent: The parent XML node to save the object into
905 * Saves the given object into the parent XML node.
907 * Returns: the new xmlNodePtr with the saved object
910 gst_object_save_thyself (GstObject * object, xmlNodePtr parent)
912 GstObjectClass *oclass;
914 g_return_val_if_fail (GST_IS_OBJECT (object), parent);
915 g_return_val_if_fail (parent != NULL, parent);
917 oclass = GST_OBJECT_GET_CLASS (object);
919 if (oclass->save_thyself)
920 oclass->save_thyself (object, parent);
922 g_signal_emit (G_OBJECT (object), gst_object_signals[OBJECT_SAVED], 0,
929 * gst_object_restore_thyself:
930 * @object: GstObject to load into
931 * @self: The XML node to load the object from
933 * Restores the given object with the data from the parent XML node.
936 gst_object_restore_thyself (GstObject * object, xmlNodePtr self)
938 GstObjectClass *oclass;
940 g_return_if_fail (GST_IS_OBJECT (object));
941 g_return_if_fail (self != NULL);
943 oclass = GST_OBJECT_GET_CLASS (object);
945 if (oclass->restore_thyself)
946 oclass->restore_thyself (object, self);
950 gst_object_real_restore_thyself (GstObject * object, xmlNodePtr self)
952 g_return_if_fail (GST_IS_OBJECT (object));
953 g_return_if_fail (self != NULL);
955 gst_class_signal_emit_by_name (object, "object_loaded", self);
957 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
960 gst_object_set_property (GObject * object, guint prop_id,
961 const GValue * value, GParamSpec * pspec)
963 GstObject *gstobject;
965 gstobject = GST_OBJECT (object);
969 gst_object_set_name (gstobject, g_value_get_string (value));
972 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
978 gst_object_get_property (GObject * object, guint prop_id,
979 GValue * value, GParamSpec * pspec)
981 GstObject *gstobject;
983 gstobject = GST_OBJECT (object);
987 g_value_take_string (value, gst_object_get_name (gstobject));
990 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
996 * gst_object_get_path_string:
997 * @object: GstObject to get the path from
999 * Generates a string describing the path of the object in
1000 * the object hierarchy. Only useful (or used) for debugging.
1002 * Returns: a string describing the path of the object. You must
1003 * g_free() the string after usage.
1005 * MT safe. Grabs and releases the object's LOCK for all objects
1009 gst_object_get_path_string (GstObject * object)
1014 gchar *prevpath, *path;
1018 /* ref object before adding to list */
1019 gst_object_ref (object);
1020 parentage = g_slist_prepend (NULL, object);
1022 path = g_strdup ("");
1024 /* first walk the object hierarchy to build a list of the parents,
1025 * be carefull here with refcounting. */
1027 if (GST_IS_OBJECT (object)) {
1028 parent = gst_object_get_parent (object);
1029 /* add parents to list, refcount remains increased while
1030 * we handle the object */
1032 parentage = g_slist_prepend (parentage, parent);
1037 } while (object != NULL);
1039 /* then walk the parent list and print them out. we need to
1040 * decrease the refcounting on each element after we handled
1042 for (parents = parentage; parents; parents = g_slist_next (parents)) {
1043 if (GST_IS_OBJECT (parents->data)) {
1044 GstObject *item = GST_OBJECT_CAST (parents->data);
1045 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1047 component = gst_object_get_name (item);
1048 separator = oclass->path_string_separator;
1050 gst_object_unref (item);
1052 component = g_strdup_printf ("%p", parents->data);
1057 path = g_strjoin (separator, prevpath, component, NULL);
1062 g_slist_free (parentage);
1067 struct _GstSignalObject
1072 struct _GstSignalObjectClass
1074 GObjectClass parent_class;
1077 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1078 void (*object_loaded) (GstSignalObject * object, GstObject * new,
1080 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
1084 gst_signal_object_get_type (void)
1086 static GType signal_object_type = 0;
1088 if (!signal_object_type) {
1089 static const GTypeInfo signal_object_info = {
1090 sizeof (GstSignalObjectClass),
1093 (GClassInitFunc) gst_signal_object_class_init,
1096 sizeof (GstSignalObject),
1098 (GInstanceInitFunc) gst_signal_object_init,
1102 signal_object_type =
1103 g_type_register_static (G_TYPE_OBJECT, "GstSignalObject",
1104 &signal_object_info, 0);
1106 return signal_object_type;
1110 gst_signal_object_class_init (GstSignalObjectClass * klass)
1112 GObjectClass *gobject_class;
1114 gobject_class = (GObjectClass *) klass;
1116 parent_class = g_type_class_ref (G_TYPE_OBJECT);
1118 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1119 gst_signal_object_signals[SO_OBJECT_LOADED] =
1120 g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
1121 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSignalObjectClass, object_loaded),
1122 NULL, NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2,
1123 G_TYPE_OBJECT, G_TYPE_POINTER);
1128 gst_signal_object_init (GstSignalObject * object)
1133 * gst_class_signal_connect
1134 * @klass: the GstObjectClass to attach the signal to
1135 * @name: the name of the signal to attach to
1136 * @func: the signal function
1137 * @func_data: a pointer to user data
1139 * Connect to a class signal.
1141 * Returns: the signal id.
1144 gst_class_signal_connect (GstObjectClass * klass,
1145 const gchar * name, gpointer func, gpointer func_data)
1147 return g_signal_connect (klass->signal_object, name, func, func_data);
1150 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1152 * gst_class_signal_emit_by_name:
1153 * @object: the object that sends the signal
1154 * @name: the name of the signal to emit
1155 * @self: data for the signal
1157 * emits the named class signal.
1160 gst_class_signal_emit_by_name (GstObject * object,
1161 const gchar * name, xmlNodePtr self)
1163 GstObjectClass *oclass;
1165 oclass = GST_OBJECT_GET_CLASS (object);
1167 g_signal_emit_by_name (oclass->signal_object, name, object, self);
1170 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */