move check stuff to its own library to be used by other modules
[platform/upstream/gstreamer.git] / gst / gstobject.c
1 /* GStreamer
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>
5  *
6  * gstobject.c: Fundamental class used for all of GStreamer
7  *
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.
12  *
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.
17  *
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.
22  */
23
24 #include "gst_private.h"
25
26 #include "gstobject.h"
27 #include "gstmarshal.h"
28 #include "gstinfo.h"
29 #include "gstutils.h"
30
31 #ifndef GST_DISABLE_TRACE
32 #include "gsttrace.h"
33 #endif
34
35 #define DEBUG_REFCOUNT
36 #define REFCOUNT_HACK
37
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.
43  *
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
46  */
47 #ifdef REFCOUNT_HACK
48 #define PATCH_REFCOUNT(obj)    ((GObject*)(obj))->ref_count = 100000;
49 #define PATCH_REFCOUNT1(obj)    ((GObject*)(obj))->ref_count = 1;
50 #else
51 #define PATCH_REFCOUNT(obj)
52 #define PATCH_REFCOUNT1(obj)
53 #endif
54
55 /* Object signals and args */
56 enum
57 {
58   PARENT_SET,
59   PARENT_UNSET,
60 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
61   OBJECT_SAVED,
62 #endif
63   DEEP_NOTIFY,
64   LAST_SIGNAL
65 };
66
67 enum
68 {
69   ARG_0,
70   ARG_NAME
71       /* FILL ME */
72 };
73
74 enum
75 {
76   SO_OBJECT_LOADED,
77   SO_LAST_SIGNAL
78 };
79
80 GType _gst_object_type = 0;
81 static GHashTable *object_name_counts = NULL;
82
83 G_LOCK_DEFINE_STATIC (object_name_mutex);
84
85 typedef struct _GstSignalObject GstSignalObject;
86 typedef struct _GstSignalObjectClass GstSignalObjectClass;
87
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);
91
92 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
93 static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
94 #endif
95
96 static void gst_object_class_init (GstObjectClass * klass);
97 static void gst_object_init (GTypeInstance * instance, gpointer g_class);
98
99 #ifndef GST_DISABLE_TRACE
100 static GObject *gst_object_constructor (GType type,
101     guint n_construct_properties, GObjectConstructParam * construct_params);
102 #endif
103
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);
110
111 static void gst_object_dispose (GObject * object);
112 static void gst_object_finalize (GObject * object);
113
114 static gboolean gst_object_set_name_default (GstObject * object,
115     const gchar * type_name);
116
117 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
118 static void gst_object_real_restore_thyself (GstObject * object,
119     xmlNodePtr self);
120 #endif
121
122 static GObjectClass *parent_class = NULL;
123 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
124
125 GType
126 gst_object_get_type (void)
127 {
128   if (!_gst_object_type) {
129     static const GTypeInfo object_info = {
130       sizeof (GstObjectClass),
131       NULL,
132       NULL,
133       (GClassInitFunc) gst_object_class_init,
134       NULL,
135       NULL,
136       sizeof (GstObject),
137       0,
138       gst_object_init,
139       NULL
140     };
141
142     _gst_object_type =
143         g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info,
144         G_TYPE_FLAG_ABSTRACT);
145   }
146   return _gst_object_type;
147 }
148
149 static void
150 gst_object_class_init (GstObjectClass * klass)
151 {
152   GObjectClass *gobject_class;
153
154   gobject_class = (GObjectClass *) klass;
155
156   parent_class = g_type_class_ref (G_TYPE_OBJECT);
157
158   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
159   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
160
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));
164
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);
179
180   klass->restore_thyself = gst_object_real_restore_thyself;
181 #endif
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,
187       G_TYPE_PARAM);
188
189   klass->path_string_separator = "/";
190   klass->lock = g_new0 (GStaticRecMutex, 1);
191   g_static_rec_mutex_init (klass->lock);
192
193   klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
194
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);
198
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;
203 #endif
204 }
205
206 static void
207 gst_object_init (GTypeInstance * instance, gpointer g_class)
208 {
209   GstObject *object = GST_OBJECT (instance);
210
211   object->lock = g_mutex_new ();
212   object->parent = NULL;
213   object->name = 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);
217
218   object->flags = 0;
219   GST_FLAG_SET (object, GST_OBJECT_FLOATING);
220 }
221
222 #ifndef GST_DISABLE_TRACE
223 static GObject *
224 gst_object_constructor (GType type, guint n_construct_properties,
225     GObjectConstructParam * construct_params)
226 {
227   const gchar *name;
228   GstAllocTrace *trace;
229   GObject *obj =
230       G_OBJECT_CLASS (parent_class)->constructor (type, n_construct_properties,
231       construct_params);
232
233   name = g_type_name (type);
234
235   trace = gst_alloc_trace_get (name);
236   if (!trace) {
237     trace = gst_alloc_trace_register (name);
238   }
239   gst_alloc_trace_new (trace, obj);
240
241   return obj;
242 }
243 #endif
244 /**
245  * gst_object_ref:
246  * @object: GstObject to reference
247  *
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.
251  *
252  * This object returns the input parameter to ease writing
253  * constructs like :
254  *  result = gst_object_ref (object->parent);
255  *
256  * Returns: A pointer to the object
257  */
258 gpointer
259 gst_object_ref (gpointer object)
260 {
261   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
262
263 #ifdef DEBUG_REFCOUNT
264 #ifdef REFCOUNT_HACK
265   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d",
266       object,
267       GST_OBJECT_REFCOUNT_VALUE (object),
268       GST_OBJECT_REFCOUNT_VALUE (object) + 1);
269 #else
270   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d",
271       object,
272       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
273 #endif
274 #endif
275
276 #ifdef REFCOUNT_HACK
277   g_atomic_int_inc (&((GstObject *) object)->refcount);
278   PATCH_REFCOUNT (object);
279 #else
280   /* FIXME, not MT safe because glib is not MT safe */
281   g_object_ref (object);
282 #endif
283
284   return object;
285 }
286
287 /**
288  * gst_object_unref:
289  * @object: GstObject to unreference
290  *
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.
294  *
295  * The unref method should never be called with the LOCK held since
296  * this might deadlock the dispose function.
297  */
298 void
299 gst_object_unref (gpointer object)
300 {
301   g_return_if_fail (GST_IS_OBJECT (object));
302
303 #ifdef REFCOUNT_HACK
304   g_return_if_fail (GST_OBJECT_REFCOUNT_VALUE (object) > 0);
305 #else
306   g_return_if_fail (((GObject *) object)->ref_count > 0);
307 #endif
308
309 #ifdef DEBUG_REFCOUNT
310 #ifdef REFCOUNT_HACK
311   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d",
312       object,
313       GST_OBJECT_REFCOUNT_VALUE (object),
314       GST_OBJECT_REFCOUNT_VALUE (object) - 1);
315 #else
316   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d",
317       object,
318       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
319 #endif
320 #endif
321
322 #ifdef REFCOUNT_HACK
323   if (G_UNLIKELY (g_atomic_int_dec_and_test (&((GstObject *) object)->
324               refcount))) {
325     PATCH_REFCOUNT1 (object);
326     g_object_unref (object);
327   } else {
328     PATCH_REFCOUNT (object);
329   }
330 #else
331   /* FIXME, not MT safe because glib is not MT safe */
332   g_object_unref (object);
333 #endif
334 }
335
336 /**
337  * gst_object_sink:
338  * @object: GstObject to sink
339  *
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.
344  *
345  * MT safe. This function grabs and releases the object lock.
346  */
347 void
348 gst_object_sink (gpointer object)
349 {
350   g_return_if_fail (GST_IS_OBJECT (object));
351
352   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
353
354   GST_LOCK (object);
355   if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
356     GST_FLAG_UNSET (object, GST_OBJECT_FLOATING);
357     GST_UNLOCK (object);
358     gst_object_unref (object);
359   } else {
360     GST_UNLOCK (object);
361   }
362 }
363
364 /**
365  * gst_object_replace:
366  * @oldobj: pointer to place of old GstObject
367  * @newobj: new GstObject
368  *
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
373  * function.
374  *
375  * Make sure not to LOCK the oldobj because it might be unreffed
376  * which could cause a deadlock when it is disposed.
377  */
378 void
379 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
380 {
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));
384
385 #ifdef DEBUG_REFCOUNT
386 #ifdef REFCOUNT_HACK
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);
392 #else
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);
398 #endif
399 #endif
400
401   if (G_LIKELY (*oldobj != newobj)) {
402     if (newobj)
403       gst_object_ref (newobj);
404     if (*oldobj)
405       gst_object_unref (*oldobj);
406
407     *oldobj = newobj;
408   }
409 }
410
411 /* dispose is called when the object has to release all links
412  * to other objects */
413 static void
414 gst_object_dispose (GObject * object)
415 {
416   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
417
418   GST_LOCK (object);
419   GST_FLAG_SET (GST_OBJECT (object), GST_OBJECT_DESTROYED);
420   GST_OBJECT_PARENT (object) = NULL;
421   GST_UNLOCK (object);
422
423   /* need to patch refcount so it is finalized */
424   PATCH_REFCOUNT1 (object)
425
426       parent_class->dispose (object);
427 }
428
429 /* finalize is called when the object has to free its resources */
430 static void
431 gst_object_finalize (GObject * object)
432 {
433   GstObject *gstobject = GST_OBJECT (object);
434
435   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
436
437   g_signal_handlers_destroy (object);
438
439   g_free (gstobject->name);
440   g_mutex_free (gstobject->lock);
441
442 #ifndef GST_DISABLE_TRACE
443   {
444     const gchar *name;
445     GstAllocTrace *trace;
446
447     name = g_type_name (G_OBJECT_TYPE (object));
448     trace = gst_alloc_trace_get (name);
449     g_assert (trace);
450     gst_alloc_trace_free (trace, object);
451   }
452 #endif
453
454   parent_class->finalize (object);
455 }
456
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
461  * elements. 
462  *
463  * This function is not MT safe in glib so we need to lock it with a 
464  * classwide mutex.
465  *
466  * MT safe.
467  */
468 static void
469 gst_object_dispatch_properties_changed (GObject * object,
470     guint n_pspecs, GParamSpec ** pspecs)
471 {
472   GstObject *gst_object, *parent, *old_parent;
473   guint i;
474   gchar *name, *debug_name;
475   GstObjectClass *klass;
476
477   /* we fail when this is not a GstObject */
478   g_return_if_fail (GST_IS_OBJECT (object));
479
480   klass = GST_OBJECT_GET_CLASS (object);
481
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,
486       pspecs);
487   PATCH_REFCOUNT (object);
488
489   gst_object = GST_OBJECT_CAST (object);
490   name = gst_object_get_name (gst_object);
491   debug_name = GST_STR_NULL (name);
492
493   /* now let the parent dispatch those, too */
494   parent = gst_object_get_parent (gst_object);
495   while (parent) {
496     /* for debugging ... */
497     gchar *parent_name = gst_object_get_name (parent);
498
499 #ifndef GST_DISABLE_GST_DEBUG
500     gchar *debug_parent_name = GST_STR_NULL (parent_name);
501 #endif
502
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);
507
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),
513           pspecs[i]);
514       PATCH_REFCOUNT (parent);
515       PATCH_REFCOUNT (object);
516     }
517     g_free (parent_name);
518
519     old_parent = parent;
520     parent = gst_object_get_parent (old_parent);
521     gst_object_unref (old_parent);
522   }
523   g_free (name);
524   GST_CLASS_UNLOCK (klass);
525 }
526
527 /** 
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.
534  *
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 
538  * using g_print.
539  *
540  * MT safe. This function grabs and releases the object's LOCK or getting the
541  *          path string of the object.
542  */
543 void
544 gst_object_default_deep_notify (GObject * object, GstObject * orig,
545     GParamSpec * pspec, gchar ** excluded_props)
546 {
547   GValue value = { 0, };        /* the important thing is that value.type = 0 */
548   gchar *str = NULL;
549   gchar *name = NULL;
550
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)
555         return;
556       excluded_props++;
557     }
558     g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
559     g_object_get_property (G_OBJECT (orig), pspec->name, &value);
560
561     if (G_IS_PARAM_SPEC_ENUM (pspec)) {
562       GEnumValue *enum_value;
563
564       enum_value =
565           g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
566           g_value_get_enum (&value));
567
568       str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
569           enum_value->value);
570     } else {
571       str = g_strdup_value_contents (&value);
572     }
573     name = gst_object_get_path_string (orig);
574     g_print ("%s: %s = %s\n", name, pspec->name, str);
575     g_free (name);
576     g_free (str);
577     g_value_unset (&value);
578   } else {
579     name = gst_object_get_path_string (orig);
580     g_warning ("Parameter %s not readable in %s.", pspec->name, name);
581     g_free (name);
582   }
583 }
584
585 static gboolean
586 gst_object_set_name_default (GstObject * object, const gchar * type_name)
587 {
588   gint count;
589   gchar *name, *tmp;
590   gboolean result;
591
592   /* to ensure guaranteed uniqueness across threads, only one thread
593    * may ever assign a name */
594   G_LOCK (object_name_mutex);
595
596   if (!object_name_counts) {
597     object_name_counts = g_hash_table_new_full (g_str_hash, g_str_equal,
598         g_free, NULL);
599   }
600
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));
604
605   G_UNLOCK (object_name_mutex);
606
607   /* GstFooSink -> foosinkN */
608   if (strncmp (type_name, "Gst", 3) == 0)
609     type_name += 3;
610   tmp = g_strdup_printf ("%s%d", type_name, count);
611   name = g_ascii_strdown (tmp, strlen (tmp));
612   g_free (tmp);
613
614   result = gst_object_set_name (object, name);
615   g_free (name);
616
617   return result;
618 }
619
620 /**
621  * gst_object_set_name:
622  * @object: a #GstObject to set the name of
623  * @name:   new name of object
624  *
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.
629  *
630  * Returns: TRUE if the name could be set. Objects that have
631  * a parent cannot be renamed.
632  *
633  * MT safe.  This function grabs and releases the object's LOCK.
634  */
635 gboolean
636 gst_object_set_name (GstObject * object, const gchar * name)
637 {
638   gboolean result;
639
640   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
641
642   GST_LOCK (object);
643
644   /* parented objects cannot be renamed */
645   if (G_UNLIKELY (object->parent != NULL))
646     goto had_parent;
647
648   if (name != NULL) {
649     g_free (object->name);
650     object->name = g_strdup (name);
651     GST_UNLOCK (object);
652     result = TRUE;
653   } else {
654     GST_UNLOCK (object);
655     result = gst_object_set_name_default (object, G_OBJECT_TYPE_NAME (object));
656   }
657   return result;
658
659   /* error */
660 had_parent:
661   {
662     GST_UNLOCK (object);
663     return FALSE;
664   }
665 }
666
667 /**
668  * gst_object_get_name:
669  * @object: a #GstObject to get the name of
670  *
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()
674  * as well.
675  *
676  * Returns: the name of the object. g_free() after usage.
677  *
678  * MT safe. This function grabs and releases the object's LOCK.
679  */
680 gchar *
681 gst_object_get_name (GstObject * object)
682 {
683   gchar *result = NULL;
684
685   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
686
687   GST_LOCK (object);
688   result = g_strdup (object->name);
689   GST_UNLOCK (object);
690
691   return result;
692 }
693
694 /**
695  * gst_object_set_name_prefix:
696  * @object:      a #GstObject to set the name prefix of
697  * @name_prefix: new name prefix of object
698  *
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.
702  *
703  * MT safe.  This function grabs and releases the object's LOCK.
704  */
705 void
706 gst_object_set_name_prefix (GstObject * object, const gchar * name_prefix)
707 {
708   g_return_if_fail (GST_IS_OBJECT (object));
709
710   GST_LOCK (object);
711   g_free (object->name_prefix);
712   object->name_prefix = g_strdup (name_prefix); /* NULL gives NULL */
713   GST_UNLOCK (object);
714 }
715
716 /**
717  * gst_object_get_name_prefix:
718  * @object: a #GstObject to get the name prefix of
719  *
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()
723  * as well.
724  *
725  * Returns: the name prefix of the object. g_free() after usage.
726  *
727  * MT safe. This function grabs and releases the object's LOCK.
728  */
729 gchar *
730 gst_object_get_name_prefix (GstObject * object)
731 {
732   gchar *result = NULL;
733
734   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
735
736   GST_LOCK (object);
737   result = g_strdup (object->name_prefix);
738   GST_UNLOCK (object);
739
740   return result;
741 }
742
743 /**
744  * gst_object_set_parent:
745  * @object: GstObject to set parent of
746  * @parent: new parent of object
747  *
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()).
750  *
751  * Causes the parent-set signal to be emitted.
752  *
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.
756  *
757  * MT safe. Grabs and releases the object's LOCK.
758  */
759 gboolean
760 gst_object_set_parent (GstObject * object, GstObject * parent)
761 {
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);
765
766   GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "set parent (ref and sink)");
767
768   GST_LOCK (object);
769   if (G_UNLIKELY (object->parent != NULL))
770     goto had_parent;
771
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);
778     GST_UNLOCK (object);
779   } else {
780     GST_UNLOCK (object);
781     gst_object_ref (object);
782   }
783
784   g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
785
786   return TRUE;
787
788   /* ERROR handling */
789 had_parent:
790   {
791     GST_UNLOCK (object);
792     return FALSE;
793   }
794 }
795
796 /**
797  * gst_object_get_parent:
798  * @object: GstObject to get parent of
799  *
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.
802  *
803  * Returns: parent of the object, this can be NULL if the object has no
804  *   parent. unref after usage.
805  *
806  * MT safe. Grabs and releases the object's LOCK.
807  */
808 GstObject *
809 gst_object_get_parent (GstObject * object)
810 {
811   GstObject *result = NULL;
812
813   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
814
815   GST_LOCK (object);
816   result = object->parent;
817   if (G_LIKELY (result))
818     gst_object_ref (result);
819   GST_UNLOCK (object);
820
821   return result;
822 }
823
824 /**
825  * gst_object_unparent:
826  * @object: GstObject to unparent
827  *
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.
831  *
832  * MT safe. Grabs and releases the object's lock.
833  */
834 void
835 gst_object_unparent (GstObject * object)
836 {
837   GstObject *parent;
838
839   g_return_if_fail (GST_IS_OBJECT (object));
840
841   GST_LOCK (object);
842   parent = object->parent;
843
844   if (G_LIKELY (parent != NULL)) {
845     GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
846     object->parent = NULL;
847     GST_UNLOCK (object);
848
849     g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_UNSET], 0,
850         parent);
851
852     gst_object_unref (object);
853   } else {
854     GST_UNLOCK (object);
855   }
856 }
857
858 /**
859  * gst_object_check_uniqueness:
860  * @list: a list of #GstObject to check through
861  * @name: the name to search for
862  *
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.
868  *
869  * Returns: TRUE if the name does not appear in the list, FALSE if it does.
870  *
871  * MT safe. Grabs and releases the LOCK of each object in the list.
872  */
873 gboolean
874 gst_object_check_uniqueness (GList * list, const gchar * name)
875 {
876   gboolean result = TRUE;
877
878   g_return_val_if_fail (name != NULL, FALSE);
879
880   for (; list; list = g_list_next (list)) {
881     GstObject *child;
882     gboolean eq;
883
884     child = GST_OBJECT (list->data);
885
886     GST_LOCK (child);
887     eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
888     GST_UNLOCK (child);
889
890     if (G_UNLIKELY (eq)) {
891       result = FALSE;
892       break;
893     }
894   }
895   return result;
896 }
897
898
899 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
900 /**
901  * gst_object_save_thyself:
902  * @object: GstObject to save
903  * @parent: The parent XML node to save the object into
904  *
905  * Saves the given object into the parent XML node.
906  *
907  * Returns: the new xmlNodePtr with the saved object
908  */
909 xmlNodePtr
910 gst_object_save_thyself (GstObject * object, xmlNodePtr parent)
911 {
912   GstObjectClass *oclass;
913
914   g_return_val_if_fail (GST_IS_OBJECT (object), parent);
915   g_return_val_if_fail (parent != NULL, parent);
916
917   oclass = GST_OBJECT_GET_CLASS (object);
918
919   if (oclass->save_thyself)
920     oclass->save_thyself (object, parent);
921
922   g_signal_emit (G_OBJECT (object), gst_object_signals[OBJECT_SAVED], 0,
923       parent);
924
925   return parent;
926 }
927
928 /**
929  * gst_object_restore_thyself:
930  * @object: GstObject to load into
931  * @self: The XML node to load the object from
932  *
933  * Restores the given object with the data from the parent XML node.
934  */
935 void
936 gst_object_restore_thyself (GstObject * object, xmlNodePtr self)
937 {
938   GstObjectClass *oclass;
939
940   g_return_if_fail (GST_IS_OBJECT (object));
941   g_return_if_fail (self != NULL);
942
943   oclass = GST_OBJECT_GET_CLASS (object);
944
945   if (oclass->restore_thyself)
946     oclass->restore_thyself (object, self);
947 }
948
949 static void
950 gst_object_real_restore_thyself (GstObject * object, xmlNodePtr self)
951 {
952   g_return_if_fail (GST_IS_OBJECT (object));
953   g_return_if_fail (self != NULL);
954
955   gst_class_signal_emit_by_name (object, "object_loaded", self);
956 }
957 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */
958
959 static void
960 gst_object_set_property (GObject * object, guint prop_id,
961     const GValue * value, GParamSpec * pspec)
962 {
963   GstObject *gstobject;
964
965   gstobject = GST_OBJECT (object);
966
967   switch (prop_id) {
968     case ARG_NAME:
969       gst_object_set_name (gstobject, g_value_get_string (value));
970       break;
971     default:
972       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
973       break;
974   }
975 }
976
977 static void
978 gst_object_get_property (GObject * object, guint prop_id,
979     GValue * value, GParamSpec * pspec)
980 {
981   GstObject *gstobject;
982
983   gstobject = GST_OBJECT (object);
984
985   switch (prop_id) {
986     case ARG_NAME:
987       g_value_take_string (value, gst_object_get_name (gstobject));
988       break;
989     default:
990       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991       break;
992   }
993 }
994
995 /**
996  * gst_object_get_path_string:
997  * @object: GstObject to get the path from
998  *
999  * Generates a string describing the path of the object in
1000  * the object hierarchy. Only useful (or used) for debugging.
1001  *
1002  * Returns: a string describing the path of the object. You must
1003  *          g_free() the string after usage. 
1004  *
1005  * MT safe. Grabs and releases the object's LOCK for all objects
1006  *          in the hierarchy.
1007  */
1008 gchar *
1009 gst_object_get_path_string (GstObject * object)
1010 {
1011   GSList *parentage;
1012   GSList *parents;
1013   void *parent;
1014   gchar *prevpath, *path;
1015   gchar *component;
1016   gchar *separator;
1017
1018   /* ref object before adding to list */
1019   gst_object_ref (object);
1020   parentage = g_slist_prepend (NULL, object);
1021
1022   path = g_strdup ("");
1023
1024   /* first walk the object hierarchy to build a list of the parents,
1025    * be carefull here with refcounting. */
1026   do {
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 */
1031       if (parent)
1032         parentage = g_slist_prepend (parentage, parent);
1033     } else {
1034       break;
1035     }
1036     object = parent;
1037   } while (object != NULL);
1038
1039   /* then walk the parent list and print them out. we need to
1040    * decrease the refcounting on each element after we handled
1041    * it. */
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);
1046
1047       component = gst_object_get_name (item);
1048       separator = oclass->path_string_separator;
1049       /* and unref now */
1050       gst_object_unref (item);
1051     } else {
1052       component = g_strdup_printf ("%p", parents->data);
1053       separator = "/";
1054     }
1055
1056     prevpath = path;
1057     path = g_strjoin (separator, prevpath, component, NULL);
1058     g_free (prevpath);
1059     g_free (component);
1060   }
1061
1062   g_slist_free (parentage);
1063
1064   return path;
1065 }
1066
1067 struct _GstSignalObject
1068 {
1069   GObject object;
1070 };
1071
1072 struct _GstSignalObjectClass
1073 {
1074   GObjectClass parent_class;
1075
1076   /* signals */
1077 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1078   void (*object_loaded) (GstSignalObject * object, GstObject * new,
1079       xmlNodePtr self);
1080 #endif                          /* GST_DISABLE_LOADSAVE_REGISTRY */
1081 };
1082
1083 static GType
1084 gst_signal_object_get_type (void)
1085 {
1086   static GType signal_object_type = 0;
1087
1088   if (!signal_object_type) {
1089     static const GTypeInfo signal_object_info = {
1090       sizeof (GstSignalObjectClass),
1091       NULL,
1092       NULL,
1093       (GClassInitFunc) gst_signal_object_class_init,
1094       NULL,
1095       NULL,
1096       sizeof (GstSignalObject),
1097       0,
1098       (GInstanceInitFunc) gst_signal_object_init,
1099       NULL
1100     };
1101
1102     signal_object_type =
1103         g_type_register_static (G_TYPE_OBJECT, "GstSignalObject",
1104         &signal_object_info, 0);
1105   }
1106   return signal_object_type;
1107 }
1108
1109 static void
1110 gst_signal_object_class_init (GstSignalObjectClass * klass)
1111 {
1112   GObjectClass *gobject_class;
1113
1114   gobject_class = (GObjectClass *) klass;
1115
1116   parent_class = g_type_class_ref (G_TYPE_OBJECT);
1117
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);
1124 #endif
1125 }
1126
1127 static void
1128 gst_signal_object_init (GstSignalObject * object)
1129 {
1130 }
1131
1132 /**
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
1138  *
1139  * Connect to a class signal.
1140  *
1141  * Returns: the signal id.
1142  */
1143 guint
1144 gst_class_signal_connect (GstObjectClass * klass,
1145     const gchar * name, gpointer func, gpointer func_data)
1146 {
1147   return g_signal_connect (klass->signal_object, name, func, func_data);
1148 }
1149
1150 #ifndef GST_DISABLE_LOADSAVE_REGISTRY
1151 /**
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
1156  *
1157  * emits the named class signal.
1158  */
1159 void
1160 gst_class_signal_emit_by_name (GstObject * object,
1161     const gchar * name, xmlNodePtr self)
1162 {
1163   GstObjectClass *oclass;
1164
1165   oclass = GST_OBJECT_GET_CLASS (object);
1166
1167   g_signal_emit_by_name (oclass->signal_object, name, object, self);
1168 }
1169
1170 #endif /* GST_DISABLE_LOADSAVE_REGISTRY */