2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstobject.c: Fundamental class used for all of GStreamer
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include "gst_private.h"
25 #include "gstobject.h"
26 #include "gstmarshal.h"
29 #ifndef GST_DISABLE_TRACE
33 /* Object signals and args */
38 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
58 GType _gst_object_type = 0;
59 static GHashTable *object_name_counts = NULL;
61 G_LOCK_DEFINE_STATIC (object_name_mutex);
63 typedef struct _GstSignalObject GstSignalObject;
64 typedef struct _GstSignalObjectClass GstSignalObjectClass;
66 static GType gst_signal_object_get_type (void);
67 static void gst_signal_object_class_init (GstSignalObjectClass * klass);
68 static void gst_signal_object_init (GstSignalObject * object);
70 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
71 static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
74 static void gst_object_class_init (GstObjectClass * klass);
75 static void gst_object_init (GstObject * object);
77 #ifndef GST_DISABLE_TRACE
78 static GObject *gst_object_constructor (GType type,
79 guint n_construct_properties, GObjectConstructParam * construct_params);
82 static void gst_object_set_property (GObject * object, guint prop_id,
83 const GValue * value, GParamSpec * pspec);
84 static void gst_object_get_property (GObject * object, guint prop_id,
85 GValue * value, GParamSpec * pspec);
86 static void gst_object_dispatch_properties_changed (GObject * object,
87 guint n_pspecs, GParamSpec ** pspecs);
89 static void gst_object_dispose (GObject * object);
90 static void gst_object_finalize (GObject * object);
92 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
93 static void gst_object_real_restore_thyself (GstObject * object,
97 static GObjectClass *parent_class = NULL;
98 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
101 gst_object_get_type (void)
103 if (!_gst_object_type) {
104 static const GTypeInfo object_info = {
105 sizeof (GstObjectClass),
108 (GClassInitFunc) gst_object_class_init,
113 (GInstanceInitFunc) gst_object_init,
117 g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info,
118 G_TYPE_FLAG_ABSTRACT);
120 return _gst_object_type;
124 gst_object_class_init (GstObjectClass * klass)
126 GObjectClass *gobject_class;
128 gobject_class = (GObjectClass *) klass;
130 parent_class = g_type_class_ref (G_TYPE_OBJECT);
132 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
133 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
135 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME,
136 g_param_spec_string ("name", "Name", "The name of the object",
137 NULL, G_PARAM_READWRITE));
139 gst_object_signals[PARENT_SET] =
140 g_signal_new ("parent-set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
141 G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
142 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
143 gst_object_signals[PARENT_UNSET] =
144 g_signal_new ("parent-unset", G_TYPE_FROM_CLASS (klass),
145 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL,
146 NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
147 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
148 /* FIXME This should be the GType of xmlNodePtr instead of G_TYPE_POINTER */
149 gst_object_signals[OBJECT_SAVED] =
150 g_signal_new ("object-saved", G_TYPE_FROM_CLASS (klass),
151 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, object_saved), NULL,
152 NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
154 klass->restore_thyself = gst_object_real_restore_thyself;
156 gst_object_signals[DEEP_NOTIFY] =
157 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
158 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
159 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
160 NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, G_TYPE_OBJECT,
163 klass->path_string_separator = "/";
165 klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
167 /* see the comments at gst_object_dispatch_properties_changed */
168 gobject_class->dispatch_properties_changed
169 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
171 gobject_class->dispose = gst_object_dispose;
172 gobject_class->finalize = gst_object_finalize;
173 #ifndef GST_DISABLE_TRACE
174 gobject_class->constructor = gst_object_constructor;
179 gst_object_init (GstObject * object)
181 object->lock = g_mutex_new ();
182 object->parent = NULL;
186 GST_FLAG_SET (object, GST_FLOATING);
189 #ifndef GST_DISABLE_TRACE
191 gst_object_constructor (GType type, guint n_construct_properties,
192 GObjectConstructParam * construct_params)
195 GstAllocTrace *trace;
197 G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_properties,
200 name = g_type_name (type);
202 trace = gst_alloc_trace_get (name);
204 trace = gst_alloc_trace_register (name);
206 gst_alloc_trace_new (trace, obj);
213 * @object: GstObject to reference
215 * Increments the refence count on the object.
217 * Returns: A pointer to the object
220 gst_object_ref (GstObject * object)
222 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
224 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "ref %d->%d",
225 G_OBJECT (object)->ref_count, G_OBJECT (object)->ref_count + 1);
227 g_object_ref (G_OBJECT (object));
233 * @object: GstObject to unreference
235 * Decrements the refence count on the object. If reference count hits
236 * zero, destroy the object.
239 gst_object_unref (GstObject * object)
241 g_return_if_fail (GST_IS_OBJECT (object));
242 g_return_if_fail (G_OBJECT (object)->ref_count > 0);
244 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unref %d->%d",
245 G_OBJECT (object)->ref_count, G_OBJECT (object)->ref_count - 1);
247 g_object_unref (G_OBJECT (object));
252 * @object: GstObject to sink
254 * Removes floating reference on an object. Any newly created object has
255 * a refcount of 1 and is FLOATING. This function should be used when
256 * creating a new object to symbolically 'take ownership' of the object.
257 * Use #gst_object_set_parent to have this done for you.
260 gst_object_sink (GstObject * object)
262 g_return_if_fail (object != NULL);
263 g_return_if_fail (GST_IS_OBJECT (object));
265 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
267 if (GST_OBJECT_FLOATING (object)) {
268 GST_FLAG_UNSET (object, GST_FLOATING);
269 gst_object_unref (object);
274 * gst_object_replace:
275 * @oldobj: pointer to place of old GstObject
276 * @newobj: new GstObject
278 * Unrefs the object pointer to by oldobj, refs the newobj and
279 * puts the newobj in *oldobj.
282 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
284 g_return_if_fail (oldobj != NULL);
285 g_return_if_fail (*oldobj == NULL || GST_IS_OBJECT (*oldobj));
286 g_return_if_fail (newobj == NULL || GST_IS_OBJECT (newobj));
288 GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s %s",
289 *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
290 newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)");
292 if (*oldobj != newobj) {
294 gst_object_ref (newobj);
296 gst_object_unref (*oldobj);
303 gst_object_dispose (GObject * object)
305 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
307 GST_FLAG_SET (GST_OBJECT (object), GST_DESTROYED);
308 GST_OBJECT_PARENT (object) = NULL;
310 parent_class->dispose (object);
313 /* finalize is called when the object has to free its resources */
315 gst_object_finalize (GObject * object)
317 GstObject *gstobject = GST_OBJECT (object);
319 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
321 g_signal_handlers_destroy (object);
323 g_free (gstobject->name);
325 g_mutex_free (gstobject->lock);
327 #ifndef GST_DISABLE_TRACE
330 GstAllocTrace *trace;
332 name = g_type_name (G_OBJECT_TYPE (object));
333 trace = gst_alloc_trace_get (name);
335 gst_alloc_trace_free (trace, object);
339 parent_class->finalize (object);
342 /* Changing a GObject property of a GstObject will result in "deep_notify"
343 * signals being emitted by the object itself, as well as in each parent
344 * object. This is so that an application can connect a listener to the
345 * top-level bin to catch property-change notifications for all contained
348 gst_object_dispatch_properties_changed (GObject * object,
349 guint n_pspecs, GParamSpec ** pspecs)
351 GstObject *gst_object;
354 /* do the standard dispatching */
355 G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object, n_pspecs,
358 /* now let the parent dispatch those, too */
359 gst_object = GST_OBJECT_PARENT (object);
361 /* need own category? */
362 for (i = 0; i < n_pspecs; i++) {
363 GST_CAT_LOG (GST_CAT_EVENT, "deep notification from %s to %s (%s)",
364 GST_OBJECT_NAME (object), GST_OBJECT_NAME (gst_object),
366 g_signal_emit (gst_object, gst_object_signals[DEEP_NOTIFY],
367 g_quark_from_string (pspecs[i]->name), (GstObject *) object,
371 gst_object = GST_OBJECT_PARENT (gst_object);
376 * gst_object_default_deep_notify:
377 * @object: the #GObject that signalled the notify.
378 * @orig: a #GstObject that initiated the notify.
379 * @pspec: a #GParamSpec of the property.
380 * @excluded_props: a set of user-specified properties to exclude or
381 * NULL to show all changes.
383 * Adds a default deep_notify signal callback to an
384 * element. The user data should contain a pointer to an array of
385 * strings that should be excluded from the notify.
386 * The default handler will print the new value of the property
390 gst_object_default_deep_notify (GObject * object, GstObject * orig,
391 GParamSpec * pspec, gchar ** excluded_props)
393 GValue value = { 0, }; /* the important thing is that value.type = 0 */
397 if (pspec->flags & G_PARAM_READABLE) {
398 /* let's not print these out for excluded properties... */
399 while (excluded_props != NULL && *excluded_props != NULL) {
400 if (strcmp (pspec->name, *excluded_props) == 0)
404 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
405 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
407 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
408 GEnumValue *enum_value;
411 g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
412 g_value_get_enum (&value));
414 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
417 str = g_strdup_value_contents (&value);
419 name = gst_object_get_path_string (orig);
420 g_print ("%s: %s = %s\n", name, pspec->name, str);
423 g_value_unset (&value);
425 name = gst_object_get_path_string (orig);
426 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
432 gst_object_set_name_default (GstObject * object)
436 const gchar *type_name;
438 type_name = G_OBJECT_TYPE_NAME (object);
440 /* to ensure guaranteed uniqueness across threads, only one thread
441 * may ever assign a name */
442 G_LOCK (object_name_mutex);
444 if (!object_name_counts) {
445 object_name_counts = g_hash_table_new_full (g_str_hash, g_str_equal,
449 count = GPOINTER_TO_INT (g_hash_table_lookup (object_name_counts, type_name));
450 g_hash_table_insert (object_name_counts, g_strdup (type_name),
451 GINT_TO_POINTER (count + 1));
453 G_UNLOCK (object_name_mutex);
455 /* GstFooSink -> foosinkN */
456 if (strncmp (type_name, "Gst", 3) == 0)
458 tmp = g_strdup_printf ("%s%d", type_name, count);
459 name = g_ascii_strdown (tmp, strlen (tmp));
462 gst_object_set_name (object, name);
467 * gst_object_set_name:
468 * @object: GstObject to set the name of
469 * @name: new name of object
471 * Sets the name of the object, or gives the element a guaranteed unique
472 * name (if @name is NULL).
475 gst_object_set_name (GstObject * object, const gchar * name)
477 g_return_if_fail (object != NULL);
478 g_return_if_fail (GST_IS_OBJECT (object));
480 if (object->name != NULL)
481 g_free (object->name);
484 object->name = g_strdup (name);
486 gst_object_set_name_default (object);
490 * gst_object_get_name:
491 * @object: GstObject to get the name of
493 * Get the name of the object.
495 * Returns: name of the object
498 gst_object_get_name (GstObject * object)
500 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
506 * gst_object_set_parent:
507 * @object: GstObject to set parent of
508 * @parent: new parent of object
510 * Sets the parent of @object. The object's reference count will be incremented,
511 * and any floating reference will be removed (see gst_object_sink()).
513 * Causes the parent-set signal to be emitted.
516 gst_object_set_parent (GstObject * object, GstObject * parent)
518 g_return_if_fail (object != NULL);
519 g_return_if_fail (GST_IS_OBJECT (object));
520 g_return_if_fail (parent != NULL);
521 g_return_if_fail (GST_IS_OBJECT (parent));
522 g_return_if_fail (object != parent);
523 g_return_if_fail (object->parent == NULL);
525 gst_object_ref (object);
526 gst_object_sink (object);
527 object->parent = parent;
529 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
533 * gst_object_get_parent:
534 * @object: GstObject to get parent of
536 * Returns the parent of @object.
538 * Returns: parent of the object
541 gst_object_get_parent (GstObject * object)
543 g_return_val_if_fail (object != NULL, NULL);
544 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
546 return object->parent;
550 * gst_object_unparent:
551 * @object: GstObject to unparent
553 * Clear the parent of @object, removing the associated reference.
556 gst_object_unparent (GstObject * object)
558 g_return_if_fail (object != NULL);
559 g_return_if_fail (GST_IS_OBJECT (object));
560 if (object->parent == NULL)
563 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
565 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_UNSET], 0,
568 object->parent = NULL;
569 gst_object_unref (object);
573 * gst_object_check_uniqueness:
574 * @list: a list of #GstObject to check through
575 * @name: the name to search for
577 * Checks to see if there is any object named @name in @list.
579 * Returns: TRUE if the name does not appear in the list, FALSE if it does.
582 gst_object_check_uniqueness (GList * list, const gchar * name)
584 g_return_val_if_fail (name != NULL, FALSE);
587 GstObject *child = GST_OBJECT (list->data);
589 list = g_list_next (list);
591 if (strcmp (GST_OBJECT_NAME (child), name) == 0)
599 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
601 * gst_object_save_thyself:
602 * @object: GstObject to save
603 * @parent: The parent XML node to save the object into
605 * Saves the given object into the parent XML node.
607 * Returns: the new xmlNodePtr with the saved object
610 gst_object_save_thyself (GstObject * object, xmlNodePtr parent)
612 GstObjectClass *oclass;
614 g_return_val_if_fail (object != NULL, parent);
615 g_return_val_if_fail (GST_IS_OBJECT (object), parent);
616 g_return_val_if_fail (parent != NULL, parent);
618 oclass = GST_OBJECT_GET_CLASS (object);
620 if (oclass->save_thyself)
621 oclass->save_thyself (object, parent);
623 g_signal_emit (G_OBJECT (object), gst_object_signals[OBJECT_SAVED], 0,
630 * gst_object_restore_thyself:
631 * @object: GstObject to load into
632 * @self: The XML node to load the object from
634 * Restores the given object with the data from the parent XML node.
637 gst_object_restore_thyself (GstObject * object, xmlNodePtr self)
639 GstObjectClass *oclass;
641 g_return_if_fail (object != NULL);
642 g_return_if_fail (GST_IS_OBJECT (object));
643 g_return_if_fail (self != NULL);
645 oclass = GST_OBJECT_GET_CLASS (object);
647 if (oclass->restore_thyself)
648 oclass->restore_thyself (object, self);
652 gst_object_real_restore_thyself (GstObject * object, xmlNodePtr self)
654 g_return_if_fail (object != NULL);
655 g_return_if_fail (GST_IS_OBJECT (object));
656 g_return_if_fail (self != NULL);
658 gst_class_signal_emit_by_name (object, "object_loaded", self);
660 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
663 gst_object_set_property (GObject * object, guint prop_id,
664 const GValue * value, GParamSpec * pspec)
666 GstObject *gstobject;
668 /* it's not null if we got it, but it might not be ours */
669 g_return_if_fail (GST_IS_OBJECT (object));
671 gstobject = GST_OBJECT (object);
675 gst_object_set_name (gstobject, g_value_get_string (value));
678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
684 gst_object_get_property (GObject * object, guint prop_id,
685 GValue * value, GParamSpec * pspec)
687 GstObject *gstobject;
689 /* it's not null if we got it, but it might not be ours */
690 g_return_if_fail (GST_IS_OBJECT (object));
692 gstobject = GST_OBJECT (object);
696 g_value_set_string (value, (gchar *) GST_OBJECT_NAME (gstobject));
699 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
705 * gst_object_get_path_string:
706 * @object: GstObject to get the path from
708 * Generates a string describing the path of the object in
709 * the object hierarchy. Only useful (or used) for debugging.
711 * Returns: a string describing the path of the object
714 gst_object_get_path_string (GstObject * object)
716 GSList *parentage = NULL;
719 gchar *prevpath, *path;
720 const char *component;
721 gchar *separator = "";
722 gboolean free_component;
724 parentage = g_slist_prepend (NULL, object);
726 path = g_strdup ("");
728 /* first walk the object hierarchy to build a list of the parents */
730 if (GST_IS_OBJECT (object)) {
731 parent = gst_object_get_parent (object);
733 parentage = g_slist_prepend (parentage, NULL);
737 if (parent != NULL) {
738 parentage = g_slist_prepend (parentage, parent);
742 } while (object != NULL);
744 /* then walk the parent list and print them out */
747 if (GST_IS_OBJECT (parents->data)) {
748 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (parents->data);
750 component = gst_object_get_name (parents->data);
751 separator = oclass->path_string_separator;
752 free_component = FALSE;
754 component = g_strdup_printf ("%p", parents->data);
756 free_component = TRUE;
760 path = g_strjoin (separator, prevpath, component, NULL);
763 g_free ((gchar *) component);
765 parents = g_slist_next (parents);
768 g_slist_free (parentage);
775 struct _GstSignalObject
780 struct _GstSignalObjectClass
782 GObjectClass parent_class;
785 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
786 void (*object_loaded) (GstSignalObject * object, GstObject * new,
788 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
792 gst_signal_object_get_type (void)
794 static GType signal_object_type = 0;
796 if (!signal_object_type) {
797 static const GTypeInfo signal_object_info = {
798 sizeof (GstSignalObjectClass),
801 (GClassInitFunc) gst_signal_object_class_init,
804 sizeof (GstSignalObject),
806 (GInstanceInitFunc) gst_signal_object_init,
810 g_type_register_static (G_TYPE_OBJECT, "GstSignalObject",
811 &signal_object_info, 0);
813 return signal_object_type;
817 gst_signal_object_class_init (GstSignalObjectClass * klass)
819 GObjectClass *gobject_class;
821 gobject_class = (GObjectClass *) klass;
823 parent_class = g_type_class_ref (G_TYPE_OBJECT);
825 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
826 gst_signal_object_signals[SO_OBJECT_LOADED] =
827 g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
828 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSignalObjectClass, object_loaded),
829 NULL, NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2,
830 G_TYPE_OBJECT, G_TYPE_POINTER);
835 gst_signal_object_init (GstSignalObject * object)
840 * gst_class_signal_connect
841 * @klass: the GstObjectClass to attach the signal to
842 * @name: the name of the signal to attach to
843 * @func: the signal function
844 * @func_data: a pointer to user data
846 * Connect to a class signal.
848 * Returns: the signal id.
851 gst_class_signal_connect (GstObjectClass * klass,
852 const gchar * name, gpointer func, gpointer func_data)
854 return g_signal_connect (klass->signal_object, name, func, func_data);
857 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
859 * gst_class_signal_emit_by_name:
860 * @object: the object that sends the signal
861 * @name: the name of the signal to emit
862 * @self: data for the signal
864 * emits the named class signal.
867 gst_class_signal_emit_by_name (GstObject * object,
868 const gchar * name, xmlNodePtr self)
870 GstObjectClass *oclass;
872 oclass = GST_OBJECT_GET_CLASS (object);
874 g_signal_emit_by_name (oclass->signal_object, name, object, self);
877 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */