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,
118 g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info,
119 G_TYPE_FLAG_ABSTRACT);
121 return _gst_object_type;
125 gst_object_class_init (GstObjectClass * klass)
127 GObjectClass *gobject_class;
129 gobject_class = (GObjectClass *) klass;
131 parent_class = g_type_class_ref (G_TYPE_OBJECT);
133 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
134 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
136 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME,
137 g_param_spec_string ("name", "Name", "The name of the object",
138 NULL, G_PARAM_READWRITE));
140 gst_object_signals[PARENT_SET] =
141 g_signal_new ("parent-set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
142 G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
143 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
144 gst_object_signals[PARENT_UNSET] =
145 g_signal_new ("parent-unset", G_TYPE_FROM_CLASS (klass),
146 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL,
147 NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
148 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
149 /* FIXME This should be the GType of xmlNodePtr instead of G_TYPE_POINTER */
150 gst_object_signals[OBJECT_SAVED] =
151 g_signal_new ("object-saved", G_TYPE_FROM_CLASS (klass),
152 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, object_saved), NULL,
153 NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
155 klass->restore_thyself = gst_object_real_restore_thyself;
157 gst_object_signals[DEEP_NOTIFY] =
158 g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
159 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
160 G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
161 NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, G_TYPE_OBJECT,
164 klass->path_string_separator = "/";
166 klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
168 /* see the comments at gst_object_dispatch_properties_changed */
169 gobject_class->dispatch_properties_changed
170 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
172 gobject_class->dispose = gst_object_dispose;
173 gobject_class->finalize = gst_object_finalize;
174 #ifndef GST_DISABLE_TRACE
175 gobject_class->constructor = gst_object_constructor;
180 gst_object_init (GstObject * object)
182 object->lock = g_mutex_new ();
183 object->parent = NULL;
187 GST_FLAG_SET (object, GST_FLOATING);
190 #ifndef GST_DISABLE_TRACE
192 gst_object_constructor (GType type, guint n_construct_properties,
193 GObjectConstructParam * construct_params)
196 GstAllocTrace *trace;
198 G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_properties,
201 name = g_type_name (type);
203 trace = gst_alloc_trace_get (name);
205 trace = gst_alloc_trace_register (name);
207 gst_alloc_trace_new (trace, obj);
214 * @object: GstObject to reference
216 * Increments the refence count on the object.
218 * Returns: A pointer to the object
221 gst_object_ref (GstObject * object)
223 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
225 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "ref %d->%d",
226 G_OBJECT (object)->ref_count, G_OBJECT (object)->ref_count + 1);
228 g_object_ref (G_OBJECT (object));
234 * @object: GstObject to unreference
236 * Decrements the refence count on the object. If reference count hits
237 * zero, destroy the object.
240 gst_object_unref (GstObject * object)
242 g_return_if_fail (GST_IS_OBJECT (object));
243 g_return_if_fail (G_OBJECT (object)->ref_count > 0);
245 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unref %d->%d",
246 G_OBJECT (object)->ref_count, G_OBJECT (object)->ref_count - 1);
248 g_object_unref (G_OBJECT (object));
253 * @object: GstObject to sink
255 * Removes floating reference on an object. Any newly created object has
256 * a refcount of 1 and is FLOATING. This function should be used when
257 * creating a new object to symbolically 'take ownership' of the object.
258 * Use #gst_object_set_parent to have this done for you.
261 gst_object_sink (GstObject * object)
263 g_return_if_fail (object != NULL);
264 g_return_if_fail (GST_IS_OBJECT (object));
266 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
268 if (GST_OBJECT_FLOATING (object)) {
269 GST_FLAG_UNSET (object, GST_FLOATING);
270 gst_object_unref (object);
275 * gst_object_replace:
276 * @oldobj: pointer to place of old GstObject
277 * @newobj: new GstObject
279 * Unrefs the object pointer to by oldobj, refs the newobj and
280 * puts the newobj in *oldobj.
283 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
285 g_return_if_fail (oldobj != NULL);
286 g_return_if_fail (*oldobj == NULL || GST_IS_OBJECT (*oldobj));
287 g_return_if_fail (newobj == NULL || GST_IS_OBJECT (newobj));
289 GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s (%d) with %s (%d)",
290 *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
291 *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
292 newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
293 newobj ? G_OBJECT (newobj)->ref_count : 0);
295 if (*oldobj != newobj) {
297 gst_object_ref (newobj);
299 gst_object_unref (*oldobj);
306 gst_object_dispose (GObject * object)
308 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
310 GST_FLAG_SET (GST_OBJECT (object), GST_DESTROYED);
311 GST_OBJECT_PARENT (object) = NULL;
313 parent_class->dispose (object);
316 /* finalize is called when the object has to free its resources */
318 gst_object_finalize (GObject * object)
320 GstObject *gstobject = GST_OBJECT (object);
322 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
324 g_signal_handlers_destroy (object);
326 g_free (gstobject->name);
328 g_mutex_free (gstobject->lock);
330 #ifndef GST_DISABLE_TRACE
333 GstAllocTrace *trace;
335 name = g_type_name (G_OBJECT_TYPE (object));
336 trace = gst_alloc_trace_get (name);
338 gst_alloc_trace_free (trace, object);
342 parent_class->finalize (object);
345 /* Changing a GObject property of a GstObject will result in "deep_notify"
346 * signals being emitted by the object itself, as well as in each parent
347 * object. This is so that an application can connect a listener to the
348 * top-level bin to catch property-change notifications for all contained
351 gst_object_dispatch_properties_changed (GObject * object,
352 guint n_pspecs, GParamSpec ** pspecs)
354 GstObject *gst_object;
357 /* do the standard dispatching */
358 G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object, n_pspecs,
361 /* now let the parent dispatch those, too */
362 gst_object = GST_OBJECT_PARENT (object);
364 /* need own category? */
365 for (i = 0; i < n_pspecs; i++) {
366 GST_CAT_LOG (GST_CAT_EVENT, "deep notification from %s to %s (%s)",
367 GST_OBJECT_NAME (object) ? GST_OBJECT_NAME (object) : "(null)",
368 GST_OBJECT_NAME (gst_object) ? GST_OBJECT_NAME (gst_object) :
369 "(null)", pspecs[i]->name);
370 g_signal_emit (gst_object, gst_object_signals[DEEP_NOTIFY],
371 g_quark_from_string (pspecs[i]->name), (GstObject *) object,
375 gst_object = GST_OBJECT_PARENT (gst_object);
380 * gst_object_default_deep_notify:
381 * @object: the #GObject that signalled the notify.
382 * @orig: a #GstObject that initiated the notify.
383 * @pspec: a #GParamSpec of the property.
384 * @excluded_props: a set of user-specified properties to exclude or
385 * NULL to show all changes.
387 * Adds a default deep_notify signal callback to an
388 * element. The user data should contain a pointer to an array of
389 * strings that should be excluded from the notify.
390 * The default handler will print the new value of the property
394 gst_object_default_deep_notify (GObject * object, GstObject * orig,
395 GParamSpec * pspec, gchar ** excluded_props)
397 GValue value = { 0, }; /* the important thing is that value.type = 0 */
401 if (pspec->flags & G_PARAM_READABLE) {
402 /* let's not print these out for excluded properties... */
403 while (excluded_props != NULL && *excluded_props != NULL) {
404 if (strcmp (pspec->name, *excluded_props) == 0)
408 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
409 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
411 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
412 GEnumValue *enum_value;
415 g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
416 g_value_get_enum (&value));
418 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
421 str = g_strdup_value_contents (&value);
423 name = gst_object_get_path_string (orig);
424 g_print ("%s: %s = %s\n", name, pspec->name, str);
427 g_value_unset (&value);
429 name = gst_object_get_path_string (orig);
430 g_warning ("Parameter %s not readable in %s.", pspec->name, name);
436 gst_object_set_name_default (GstObject * object)
440 const gchar *type_name;
442 type_name = G_OBJECT_TYPE_NAME (object);
444 /* to ensure guaranteed uniqueness across threads, only one thread
445 * may ever assign a name */
446 G_LOCK (object_name_mutex);
448 if (!object_name_counts) {
449 object_name_counts = g_hash_table_new_full (g_str_hash, g_str_equal,
453 count = GPOINTER_TO_INT (g_hash_table_lookup (object_name_counts, type_name));
454 g_hash_table_insert (object_name_counts, g_strdup (type_name),
455 GINT_TO_POINTER (count + 1));
457 G_UNLOCK (object_name_mutex);
459 /* GstFooSink -> foosinkN */
460 if (strncmp (type_name, "Gst", 3) == 0)
462 tmp = g_strdup_printf ("%s%d", type_name, count);
463 name = g_ascii_strdown (tmp, strlen (tmp));
466 gst_object_set_name (object, name);
471 * gst_object_set_name:
472 * @object: GstObject to set the name of
473 * @name: new name of object
475 * Sets the name of the object, or gives the element a guaranteed unique
476 * name (if @name is NULL).
479 gst_object_set_name (GstObject * object, const gchar * name)
481 g_return_if_fail (object != NULL);
482 g_return_if_fail (GST_IS_OBJECT (object));
484 if (object->name != NULL)
485 g_free (object->name);
488 object->name = g_strdup (name);
490 gst_object_set_name_default (object);
494 * gst_object_get_name:
495 * @object: GstObject to get the name of
497 * Get the name of the object.
499 * Returns: name of the object
502 gst_object_get_name (GstObject * object)
504 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
510 * gst_object_set_parent:
511 * @object: GstObject to set parent of
512 * @parent: new parent of object
514 * Sets the parent of @object. The object's reference count will be incremented,
515 * and any floating reference will be removed (see gst_object_sink()).
517 * Causes the parent-set signal to be emitted.
520 gst_object_set_parent (GstObject * object, GstObject * parent)
522 g_return_if_fail (object != NULL);
523 g_return_if_fail (GST_IS_OBJECT (object));
524 g_return_if_fail (parent != NULL);
525 g_return_if_fail (GST_IS_OBJECT (parent));
526 g_return_if_fail (object != parent);
527 g_return_if_fail (object->parent == NULL);
529 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "set parent (ref and sink)");
530 gst_object_ref (object);
531 gst_object_sink (object);
532 object->parent = parent;
534 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
538 * gst_object_get_parent:
539 * @object: GstObject to get parent of
541 * Returns the parent of @object.
543 * Returns: parent of the object
546 gst_object_get_parent (GstObject * object)
548 g_return_val_if_fail (object != NULL, NULL);
549 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
551 return object->parent;
555 * gst_object_unparent:
556 * @object: GstObject to unparent
558 * Clear the parent of @object, removing the associated reference.
561 gst_object_unparent (GstObject * object)
563 g_return_if_fail (object != NULL);
564 g_return_if_fail (GST_IS_OBJECT (object));
565 if (object->parent == NULL)
568 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
570 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_UNSET], 0,
573 object->parent = NULL;
574 gst_object_unref (object);
578 * gst_object_check_uniqueness:
579 * @list: a list of #GstObject to check through
580 * @name: the name to search for
582 * Checks to see if there is any object named @name in @list.
584 * Returns: TRUE if the name does not appear in the list, FALSE if it does.
587 gst_object_check_uniqueness (GList * list, const gchar * name)
589 g_return_val_if_fail (name != NULL, FALSE);
592 GstObject *child = GST_OBJECT (list->data);
594 list = g_list_next (list);
596 if (strcmp (GST_OBJECT_NAME (child), name) == 0)
604 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
606 * gst_object_save_thyself:
607 * @object: GstObject to save
608 * @parent: The parent XML node to save the object into
610 * Saves the given object into the parent XML node.
612 * Returns: the new xmlNodePtr with the saved object
615 gst_object_save_thyself (GstObject * object, xmlNodePtr parent)
617 GstObjectClass *oclass;
619 g_return_val_if_fail (object != NULL, parent);
620 g_return_val_if_fail (GST_IS_OBJECT (object), parent);
621 g_return_val_if_fail (parent != NULL, parent);
623 oclass = GST_OBJECT_GET_CLASS (object);
625 if (oclass->save_thyself)
626 oclass->save_thyself (object, parent);
628 g_signal_emit (G_OBJECT (object), gst_object_signals[OBJECT_SAVED], 0,
635 * gst_object_restore_thyself:
636 * @object: GstObject to load into
637 * @self: The XML node to load the object from
639 * Restores the given object with the data from the parent XML node.
642 gst_object_restore_thyself (GstObject * object, xmlNodePtr self)
644 GstObjectClass *oclass;
646 g_return_if_fail (object != NULL);
647 g_return_if_fail (GST_IS_OBJECT (object));
648 g_return_if_fail (self != NULL);
650 oclass = GST_OBJECT_GET_CLASS (object);
652 if (oclass->restore_thyself)
653 oclass->restore_thyself (object, self);
657 gst_object_real_restore_thyself (GstObject * object, xmlNodePtr self)
659 g_return_if_fail (object != NULL);
660 g_return_if_fail (GST_IS_OBJECT (object));
661 g_return_if_fail (self != NULL);
663 gst_class_signal_emit_by_name (object, "object_loaded", self);
665 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
668 gst_object_set_property (GObject * object, guint prop_id,
669 const GValue * value, GParamSpec * pspec)
671 GstObject *gstobject;
673 /* it's not null if we got it, but it might not be ours */
674 g_return_if_fail (GST_IS_OBJECT (object));
676 gstobject = GST_OBJECT (object);
680 gst_object_set_name (gstobject, g_value_get_string (value));
683 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
689 gst_object_get_property (GObject * object, guint prop_id,
690 GValue * value, GParamSpec * pspec)
692 GstObject *gstobject;
694 /* it's not null if we got it, but it might not be ours */
695 g_return_if_fail (GST_IS_OBJECT (object));
697 gstobject = GST_OBJECT (object);
701 g_value_set_string (value, (gchar *) GST_OBJECT_NAME (gstobject));
704 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
710 * gst_object_get_path_string:
711 * @object: GstObject to get the path from
713 * Generates a string describing the path of the object in
714 * the object hierarchy. Only useful (or used) for debugging.
716 * Returns: a string describing the path of the object
719 gst_object_get_path_string (GstObject * object)
721 GSList *parentage = NULL;
724 gchar *prevpath, *path;
725 const char *component;
726 gchar *separator = "";
727 gboolean free_component;
729 parentage = g_slist_prepend (NULL, object);
731 path = g_strdup ("");
733 /* first walk the object hierarchy to build a list of the parents */
735 if (GST_IS_OBJECT (object)) {
736 parent = gst_object_get_parent (object);
738 parentage = g_slist_prepend (parentage, NULL);
742 if (parent != NULL) {
743 parentage = g_slist_prepend (parentage, parent);
747 } while (object != NULL);
749 /* then walk the parent list and print them out */
752 if (GST_IS_OBJECT (parents->data)) {
753 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (parents->data);
755 component = gst_object_get_name (parents->data);
756 separator = oclass->path_string_separator;
757 free_component = FALSE;
759 component = g_strdup_printf ("%p", parents->data);
761 free_component = TRUE;
765 path = g_strjoin (separator, prevpath, component, NULL);
768 g_free ((gchar *) component);
770 parents = g_slist_next (parents);
773 g_slist_free (parentage);
780 struct _GstSignalObject
785 struct _GstSignalObjectClass
787 GObjectClass parent_class;
790 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
791 void (*object_loaded) (GstSignalObject * object, GstObject * new,
793 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
797 gst_signal_object_get_type (void)
799 static GType signal_object_type = 0;
801 if (!signal_object_type) {
802 static const GTypeInfo signal_object_info = {
803 sizeof (GstSignalObjectClass),
806 (GClassInitFunc) gst_signal_object_class_init,
809 sizeof (GstSignalObject),
811 (GInstanceInitFunc) gst_signal_object_init,
816 g_type_register_static (G_TYPE_OBJECT, "GstSignalObject",
817 &signal_object_info, 0);
819 return signal_object_type;
823 gst_signal_object_class_init (GstSignalObjectClass * klass)
825 GObjectClass *gobject_class;
827 gobject_class = (GObjectClass *) klass;
829 parent_class = g_type_class_ref (G_TYPE_OBJECT);
831 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
832 gst_signal_object_signals[SO_OBJECT_LOADED] =
833 g_signal_new ("object-loaded", G_TYPE_FROM_CLASS (klass),
834 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstSignalObjectClass, object_loaded),
835 NULL, NULL, gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2,
836 G_TYPE_OBJECT, G_TYPE_POINTER);
841 gst_signal_object_init (GstSignalObject * object)
846 * gst_class_signal_connect
847 * @klass: the GstObjectClass to attach the signal to
848 * @name: the name of the signal to attach to
849 * @func: the signal function
850 * @func_data: a pointer to user data
852 * Connect to a class signal.
854 * Returns: the signal id.
857 gst_class_signal_connect (GstObjectClass * klass,
858 const gchar * name, gpointer func, gpointer func_data)
860 return g_signal_connect (klass->signal_object, name, func, func_data);
863 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
865 * gst_class_signal_emit_by_name:
866 * @object: the object that sends the signal
867 * @name: the name of the signal to emit
868 * @self: data for the signal
870 * emits the named class signal.
873 gst_class_signal_emit_by_name (GstObject * object,
874 const gchar * name, xmlNodePtr self)
876 GstObjectClass *oclass;
878 oclass = GST_OBJECT_GET_CLASS (object);
880 g_signal_emit_by_name (oclass->signal_object, name, object, self);
883 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */