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"
28 #ifndef GST_DISABLE_TRACE
32 /* Object signals and args */
36 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
54 GType _gst_object_type = 0;
55 static GHashTable *object_name_counts = NULL;
56 G_LOCK_DEFINE_STATIC (object_name_mutex);
58 typedef struct _GstSignalObject GstSignalObject;
59 typedef struct _GstSignalObjectClass GstSignalObjectClass;
61 static GType gst_signal_object_get_type (void);
62 static void gst_signal_object_class_init (GstSignalObjectClass *klass);
63 static void gst_signal_object_init (GstSignalObject *object);
65 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
66 static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
69 static void gst_object_class_init (GstObjectClass *klass);
70 static void gst_object_init (GstObject *object);
71 #ifndef GST_DISABLE_TRACE
72 static GObject * gst_object_constructor (GType type, guint n_construct_properties,
73 GObjectConstructParam *construct_params);
76 static void gst_object_set_property (GObject * object, guint prop_id, const GValue * value,
78 static void gst_object_get_property (GObject * object, guint prop_id, GValue * value,
80 static void gst_object_dispatch_properties_changed (GObject *object,
84 static void gst_object_dispose (GObject *object);
85 static void gst_object_finalize (GObject *object);
87 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
88 static void gst_object_real_restore_thyself (GstObject *object, xmlNodePtr self);
91 static GObjectClass *parent_class = NULL;
92 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
95 gst_object_get_type (void)
97 if (!_gst_object_type) {
98 static const GTypeInfo object_info = {
99 sizeof (GstObjectClass),
102 (GClassInitFunc) gst_object_class_init,
107 (GInstanceInitFunc) gst_object_init,
110 _gst_object_type = g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info, G_TYPE_FLAG_ABSTRACT);
112 return _gst_object_type;
116 gst_object_class_init (GstObjectClass *klass)
118 GObjectClass *gobject_class;
120 gobject_class = (GObjectClass*) klass;
122 parent_class = g_type_class_ref (G_TYPE_OBJECT);
124 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
125 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
127 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME,
128 g_param_spec_string ("name", "Name", "The name of the object",
129 NULL, G_PARAM_READWRITE));
131 gst_object_signals[PARENT_SET] =
132 g_signal_new ("parent_set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
133 G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
134 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
136 gst_object_signals[PARENT_UNSET] =
137 g_signal_new ("parent_unset", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
138 G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL, NULL,
139 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1,
141 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
142 /* FIXME This should be the GType of xmlNodePtr instead of G_TYPE_POINTER */
143 gst_object_signals[OBJECT_SAVED] =
144 g_signal_new ("object_saved", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
145 G_STRUCT_OFFSET (GstObjectClass, object_saved), NULL, NULL,
146 g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1,
149 klass->restore_thyself = gst_object_real_restore_thyself;
151 gst_object_signals[DEEP_NOTIFY] =
152 g_signal_new ("deep_notify", G_TYPE_FROM_CLASS (klass),
153 G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
154 G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL, NULL,
155 gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE,
156 2, G_TYPE_OBJECT, G_TYPE_PARAM);
158 klass->path_string_separator = "/";
160 klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
162 /* see the comments at gst_object_dispatch_properties_changed */
163 gobject_class->dispatch_properties_changed
164 = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
166 gobject_class->dispose = gst_object_dispose;
167 gobject_class->finalize = gst_object_finalize;
168 #ifndef GST_DISABLE_TRACE
169 gobject_class->constructor = gst_object_constructor;
174 gst_object_init (GstObject *object)
176 object->lock = g_mutex_new();
177 object->parent = NULL;
181 GST_FLAG_SET (object, GST_FLOATING);
184 #ifndef GST_DISABLE_TRACE
186 gst_object_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_params)
189 GstAllocTrace *trace;
190 GObject *obj = G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_properties, construct_params);
192 name = g_type_name (type);
194 trace = gst_alloc_trace_get (name);
196 trace = gst_alloc_trace_register (name);
198 gst_alloc_trace_new (trace, obj);
205 * @object: GstObject to reference
207 * Increments the refence count on the object.
209 * Returns: A pointer to the object
212 gst_object_ref (GstObject *object)
214 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
216 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "ref %d->%d",
217 G_OBJECT (object)->ref_count,
218 G_OBJECT (object)->ref_count + 1);
220 g_object_ref (G_OBJECT (object));
226 * @object: GstObject to unreference
228 * Decrements the refence count on the object. If reference count hits
229 * zero, destroy the object.
232 gst_object_unref (GstObject *object)
234 g_return_if_fail (GST_IS_OBJECT (object));
235 g_return_if_fail (G_OBJECT (object)->ref_count > 0);
237 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unref %d->%d",
238 G_OBJECT (object)->ref_count,
239 G_OBJECT (object)->ref_count - 1);
241 g_object_unref (G_OBJECT (object));
246 * @object: GstObject to sink
248 * Removes floating reference on an object. Any newly created object has
249 * a refcount of 1 and is FLOATING. This function should be used when
250 * creating a new object to symbolically 'take ownership of' the object.
251 * Use #gst_object_set_parent to have this done for you.
254 gst_object_sink (GstObject *object)
256 g_return_if_fail (object != NULL);
257 g_return_if_fail (GST_IS_OBJECT (object));
259 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
261 if (GST_OBJECT_FLOATING (object)) {
262 GST_FLAG_UNSET (object, GST_FLOATING);
263 gst_object_unref (object);
268 * gst_object_replace:
269 * @oldobj: pointer to place of old GstObject
270 * @newobj: new GstObject
272 * Unrefs the object pointer to by oldobj, refs the newobj and
273 * puts the newobj in *oldobj.
276 gst_object_replace (GstObject **oldobj, GstObject *newobj)
278 g_return_if_fail (oldobj != NULL);
279 g_return_if_fail (*oldobj == NULL || GST_IS_OBJECT (*oldobj));
280 g_return_if_fail (newobj == NULL || GST_IS_OBJECT (newobj));
282 GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s %s",
283 *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
284 newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)");
286 if (*oldobj != newobj) {
287 if (newobj) gst_object_ref (newobj);
288 if (*oldobj) gst_object_unref (*oldobj);
295 gst_object_dispose (GObject *object)
297 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
299 GST_FLAG_SET (GST_OBJECT (object), GST_DESTROYED);
300 GST_OBJECT_PARENT (object) = NULL;
302 parent_class->dispose (object);
305 /* finilize is called when the object has to free its resources */
307 gst_object_finalize (GObject *object)
309 GstObject *gstobject = GST_OBJECT (object);
311 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
313 g_signal_handlers_destroy (object);
315 g_free (gstobject->name);
317 g_mutex_free (gstobject->lock);
319 #ifndef GST_DISABLE_TRACE
322 GstAllocTrace *trace;
324 name = g_type_name (G_OBJECT_TYPE (object));
325 trace = gst_alloc_trace_get (name);
327 gst_alloc_trace_free (trace, object);
331 parent_class->finalize (object);
334 /* Changing a GObject property of a GstObject will result in "deep_notify"
335 * signals being emitted by the object itself, as well as in each parent
336 * object. This is so that an application can connect a listener to the
337 * top-level bin to catch property-change notifications for all contained
340 gst_object_dispatch_properties_changed (GObject *object,
344 GstObject *gst_object;
347 /* do the standard dispatching */
348 G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object, n_pspecs, pspecs);
350 /* now let the parent dispatch those, too */
351 gst_object = GST_OBJECT_PARENT (object);
353 /* need own category? */
354 for (i = 0; i < n_pspecs; i++) {
355 GST_CAT_LOG (GST_CAT_EVENT, "deep notification from %s to %s (%s)", GST_OBJECT_NAME (object),
356 GST_OBJECT_NAME (gst_object), pspecs[i]->name);
357 g_signal_emit (gst_object, gst_object_signals[DEEP_NOTIFY], g_quark_from_string (pspecs[i]->name),
358 (GstObject *) object, pspecs[i]);
361 gst_object = GST_OBJECT_PARENT (gst_object);
366 * gst_object_default_deep_notify:
367 * @object: the #GObject that signalled the notify.
368 * @orig: a #GstObject that initiated the notify.
369 * @pspec: a #GParamSpec of the property.
370 * @excluded_props: a set of user-specified properties to exclude or
371 * NULL to show all changes.
373 * Adds a default deep_notify signal callback to an
374 * element. The user data should contain a pointer to an array of
375 * strings that should be excluded from the notify.
376 * The default handler will print the new value of the property
380 gst_object_default_deep_notify (GObject *object, GstObject *orig,
381 GParamSpec *pspec, gchar **excluded_props)
383 GValue value = { 0, }; /* the important thing is that value.type = 0 */
387 if (pspec->flags & G_PARAM_READABLE) {
388 /* let's not print these out for excluded properties... */
389 while (excluded_props != NULL && *excluded_props != NULL) {
390 if (strcmp (pspec->name, *excluded_props) == 0)
394 g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
395 g_object_get_property (G_OBJECT (orig), pspec->name, &value);
397 if (G_IS_PARAM_SPEC_ENUM (pspec)) {
398 GEnumValue *enum_value;
399 enum_value = g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
400 g_value_get_enum (&value));
402 str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
406 str = g_strdup_value_contents (&value);
408 name = gst_object_get_path_string (orig);
409 g_print ("%s: %s = %s\n", name, pspec->name, str);
412 g_value_unset (&value);
414 name = gst_object_get_path_string (orig);
415 g_warning ("Parameter %s not readable in %s.",
422 gst_object_set_name_default (GstObject *object)
426 const gchar *type_name;
428 type_name = G_OBJECT_TYPE_NAME (object);
430 /* to ensure guaranteed uniqueness across threads, only one thread
431 * may ever assign a name */
432 G_LOCK (object_name_mutex);
434 if (!object_name_counts)
435 object_name_counts = g_hash_table_new (g_str_hash, g_str_equal);
437 count = GPOINTER_TO_INT (g_hash_table_lookup (object_name_counts, type_name));
438 g_hash_table_insert (object_name_counts, g_strdup (type_name),
439 GINT_TO_POINTER (count + 1));
441 G_UNLOCK (object_name_mutex);
443 /* GstFooSink -> foosinkN */
444 if (strncmp (type_name, "Gst", 3) == 0)
446 tmp = g_strdup_printf ("%s%d", type_name, count);
447 name = g_ascii_strdown (tmp, strlen (tmp));
450 gst_object_set_name (object, name);
455 * gst_object_set_name:
456 * @object: GstObject to set the name of
457 * @name: new name of object
459 * Sets the name of the object, or gives the element a guaranteed unique
460 * name (if @name is NULL).
463 gst_object_set_name (GstObject *object, const gchar *name)
465 g_return_if_fail (object != NULL);
466 g_return_if_fail (GST_IS_OBJECT (object));
468 if (object->name != NULL)
469 g_free (object->name);
472 object->name = g_strdup (name);
474 gst_object_set_name_default (object);
478 * gst_object_get_name:
479 * @object: GstObject to get the name of
481 * Get the name of the object.
483 * Returns: name of the object
486 gst_object_get_name (GstObject *object)
488 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
494 * gst_object_set_parent:
495 * @object: GstObject to set parent of
496 * @parent: new parent of object
498 * Set the parent of the object. The object's reference count is
500 * signals the parent-set signal
503 gst_object_set_parent (GstObject *object, GstObject *parent)
505 g_return_if_fail (object != NULL);
506 g_return_if_fail (GST_IS_OBJECT (object));
507 g_return_if_fail (parent != NULL);
508 g_return_if_fail (GST_IS_OBJECT (parent));
509 g_return_if_fail (object != parent);
510 g_return_if_fail (object->parent == NULL);
512 gst_object_ref (object);
513 gst_object_sink (object);
514 object->parent = parent;
516 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
520 * gst_object_get_parent:
521 * @object: GstObject to get parent of
523 * Return the parent of the object.
525 * Returns: parent of the object
528 gst_object_get_parent (GstObject *object)
530 g_return_val_if_fail (object != NULL, NULL);
531 g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
533 return object->parent;
537 * gst_object_unparent:
538 * @object: GstObject to unparent
540 * Clear the parent of the object, removing the associated reference.
543 gst_object_unparent (GstObject *object)
545 g_return_if_fail (object != NULL);
546 g_return_if_fail (GST_IS_OBJECT(object));
547 if (object->parent == NULL)
550 GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
552 g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_UNSET], 0, object->parent);
554 object->parent = NULL;
555 gst_object_unref (object);
559 * gst_object_check_uniqueness:
560 * @list: a list of #GstObject to check through
561 * @name: the name to search for
563 * This function checks through the list of objects to see if the name
564 * given appears in the list as the name of an object. It returns TRUE if
565 * the name does not exist in the list.
567 * Returns: TRUE if the name doesn't appear in the list, FALSE if it does.
570 gst_object_check_uniqueness (GList *list, const gchar *name)
572 g_return_val_if_fail (name != NULL, FALSE);
575 GstObject *child = GST_OBJECT (list->data);
577 list = g_list_next (list);
579 if (strcmp (GST_OBJECT_NAME (child), name) == 0)
587 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
589 * gst_object_save_thyself:
590 * @object: GstObject to save
591 * @parent: The parent XML node to save the object into
593 * Saves the given object into the parent XML node.
595 * Returns: the new xmlNodePtr with the saved object
598 gst_object_save_thyself (GstObject *object, xmlNodePtr parent)
600 GstObjectClass *oclass;
602 g_return_val_if_fail (object != NULL, parent);
603 g_return_val_if_fail (GST_IS_OBJECT (object), parent);
604 g_return_val_if_fail (parent != NULL, parent);
606 oclass = GST_OBJECT_GET_CLASS (object);
608 if (oclass->save_thyself)
609 oclass->save_thyself (object, parent);
611 g_signal_emit (G_OBJECT (object), gst_object_signals[OBJECT_SAVED], 0, parent);
617 * gst_object_restore_thyself:
618 * @object: GstObject to load into
619 * @self: The XML node to load the object from
621 * Restores the given object with the data from the parent XML node.
624 gst_object_restore_thyself (GstObject *object, xmlNodePtr self)
626 GstObjectClass *oclass;
628 g_return_if_fail (object != NULL);
629 g_return_if_fail (GST_IS_OBJECT (object));
630 g_return_if_fail (self != NULL);
632 oclass = GST_OBJECT_GET_CLASS (object);
634 if (oclass->restore_thyself)
635 oclass->restore_thyself (object, self);
639 gst_object_real_restore_thyself (GstObject *object, xmlNodePtr self)
641 g_return_if_fail (object != NULL);
642 g_return_if_fail (GST_IS_OBJECT (object));
643 g_return_if_fail (self != NULL);
645 gst_class_signal_emit_by_name (object, "object_loaded", self);
647 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
650 gst_object_set_property (GObject* object, guint prop_id,
651 const GValue* value, GParamSpec* pspec)
653 GstObject *gstobject;
655 /* it's not null if we got it, but it might not be ours */
656 g_return_if_fail (GST_IS_OBJECT (object));
658 gstobject = GST_OBJECT (object);
662 gst_object_set_name (gstobject, g_value_get_string (value));
665 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
671 gst_object_get_property (GObject* object, guint prop_id,
672 GValue* value, GParamSpec* pspec)
674 GstObject *gstobject;
676 /* it's not null if we got it, but it might not be ours */
677 g_return_if_fail (GST_IS_OBJECT (object));
679 gstobject = GST_OBJECT (object);
683 g_value_set_string (value, (gchar*)GST_OBJECT_NAME (gstobject));
686 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 * gst_object_get_path_string:
693 * @object: GstObject to get the path from
695 * Generates a string describing the path of the object in
696 * the object hierarchy. Only useful (or used) for debugging
698 * Returns: a string describing the path of the object
701 gst_object_get_path_string (GstObject *object)
703 GSList *parentage = NULL;
706 gchar *prevpath, *path;
707 const char *component;
708 gchar *separator = "";
709 gboolean free_component;
711 parentage = g_slist_prepend (NULL, object);
713 path = g_strdup ("");
715 /* first walk the object hierarchy to build a list of the parents */
717 if (GST_IS_OBJECT (object)) {
718 parent = gst_object_get_parent (object);
720 parentage = g_slist_prepend (parentage, NULL);
724 if (parent != NULL) {
725 parentage = g_slist_prepend (parentage, parent);
729 } while (object != NULL);
731 /* then walk the parent list and print them out */
734 if (GST_IS_OBJECT (parents->data)) {
735 GstObjectClass *oclass = GST_OBJECT_GET_CLASS (parents->data);
737 component = gst_object_get_name (parents->data);
738 separator = oclass->path_string_separator;
739 free_component = FALSE;
741 component = g_strdup_printf("%p",parents->data);
743 free_component = TRUE;
747 path = g_strjoin (separator, prevpath, component, NULL);
750 g_free((gchar *)component);
752 parents = g_slist_next(parents);
755 g_slist_free (parentage);
762 struct _GstSignalObject {
766 struct _GstSignalObjectClass {
767 GObjectClass parent_class;
770 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
771 void (*object_loaded) (GstSignalObject *object, GstObject *new, xmlNodePtr self);
772 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
776 gst_signal_object_get_type (void)
778 static GType signal_object_type = 0;
780 if (!signal_object_type) {
781 static const GTypeInfo signal_object_info = {
782 sizeof(GstSignalObjectClass),
785 (GClassInitFunc)gst_signal_object_class_init,
788 sizeof(GstSignalObject),
790 (GInstanceInitFunc)gst_signal_object_init,
793 signal_object_type = g_type_register_static(G_TYPE_OBJECT, "GstSignalObject", &signal_object_info, 0);
795 return signal_object_type;
799 gst_signal_object_class_init (GstSignalObjectClass *klass)
801 GObjectClass *gobject_class;
803 gobject_class = (GObjectClass*) klass;
805 parent_class = g_type_class_ref (G_TYPE_OBJECT);
807 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
808 gst_signal_object_signals[SO_OBJECT_LOADED] =
809 g_signal_new ("object_loaded", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
810 G_STRUCT_OFFSET (GstSignalObjectClass, object_loaded), NULL, NULL,
811 gst_marshal_VOID__OBJECT_POINTER, G_TYPE_NONE, 2,
812 G_TYPE_OBJECT, G_TYPE_POINTER);
817 gst_signal_object_init (GstSignalObject *object)
822 * gst_class_signal_connect
823 * @klass: the GstObjectClass to attach the signal to
824 * @name: the name of the signal to attach to
825 * @func: the signal function
826 * @func_data: a pointer to user data
828 * Connect to a class signal.
830 * Returns: the signal id.
833 gst_class_signal_connect (GstObjectClass *klass,
838 return g_signal_connect (klass->signal_object, name, func, func_data);
841 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
843 * gst_class_signal_emit_by_name:
844 * @object: the object that sends the signal
845 * @name: the name of the signal to emit
846 * @self: data for the signal
848 * emits the named class signal.
851 gst_class_signal_emit_by_name (GstObject *object,
855 GstObjectClass *oclass;
857 oclass = GST_OBJECT_GET_CLASS (object);
859 g_signal_emit_by_name (oclass->signal_object, name, object, self);
862 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */