gstobject.c 33.9 KB
Newer Older
1
2
3
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
4
 *                    2005 Wim Taymans <wim@fluendo.com>
5
6
 *
 * gstobject.c: Fundamental class used for all of GStreamer
Erik Walthinsen's avatar
Erik Walthinsen committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
23

24
25
26
27
/**
 * SECTION:gstobject
 * @short_description: Base class for the GStreamer object hierarchy
 *
28
29
 * #GstObject provides a root for the object hierarchy tree filed in by the
 * GStreamer library.  It is currently a thin wrapper on top of
30
31
 * #GObject. It is an abstract class that is not very usable on its own.
 *
32
33
34
 * #GstObject gives us basic refcounting, parenting functionality and locking.
 * Most of the function are just extended for special GStreamer needs and can be
 * found under the same name in the base class of #GstObject which is #GObject
35
36
 * (e.g. g_object_ref() becomes gst_object_ref()).
 *
37
38
39
 * The most interesting difference between #GstObject and #GObject is the
 * "floating" reference count. A #GObject is created with a reference count of
 * 1, owned by the creator of the #GObject. (The owner of a reference is the
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
40
 * code section that has the right to call gst_object_unref() in order to
41
42
43
 * remove that reference.) A #GstObject is created with a reference count of 1
 * also, but it isn't owned by anyone; Instead, the initial reference count
 * of a #GstObject is "floating". The floating reference can be removed by
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
44
45
 * anyone at any time, by calling gst_object_sink().  gst_object_sink() does
 * nothing if an object is already sunk (has no floating reference).
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
46
 *
47
 * When you add a #GstElement to its parent container, the parent container will
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48
 * do this:
49
50
51
52
53
54
 * <informalexample>
 * <programlisting>
 *   gst_object_ref (GST_OBJECT (child_element));
 *   gst_object_sink (GST_OBJECT (child_element));
 * </programlisting>
 * </informalexample>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
55
56
57
 * This means that the container now owns a reference to the child element
 * (since it called gst_object_ref()), and the child element has no floating
 * reference.
58
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
59
 * The purpose of the floating reference is to keep the child element alive
60
61
 * until you add it to a parent container, which then manages the lifetime of
 * the object itself:
62
63
64
65
66
67
68
69
70
 * <informalexample>
 * <programlisting>
 *    element = gst_element_factory_make (factoryname, name);
 *    // element has one floating reference to keep it alive
 *    gst_bin_add (GST_BIN (bin), element);
 *    // element has one non-floating reference owned by the container
 * </programlisting>
 * </informalexample>
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71
 * Another effect of this is, that calling gst_object_unref() on a bin object,
72
 * will also destoy all the #GstElement objects in it. The same is true for
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
73
 * calling gst_bin_remove().
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74
 *
75
76
77
78
79
 * Special care has to be taken for all methods that gst_object_sink() an object
 * since if the caller of those functions had a floating reference to the object,
 * the object reference is now invalid.
 *
 * In contrast to #GObject instances, #GstObject adds a name property. The functions
80
81
 * gst_object_set_name() and gst_object_get_name() are used to set/get the name
 * of the object.
82
83
 *
 * Last reviewed on 2005-11-09 (0.9.4)
84
 */
Erik Walthinsen's avatar
Erik Walthinsen committed
85

86
87
#include "gst_private.h"

88
#include "gstobject.h"
89
#include "gstmarshal.h"
90
#include "gstinfo.h"
91
#include "gstutils.h"
92

93
94
#ifndef GST_DISABLE_TRACE
#include "gsttrace.h"
95
static GstAllocTrace *_gst_object_trace;
96
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
97

98
99
#define DEBUG_REFCOUNT

Erik Walthinsen's avatar
Erik Walthinsen committed
100
/* Object signals and args */
101
102
enum
{
Erik Walthinsen's avatar
Erik Walthinsen committed
103
  PARENT_SET,
104
  PARENT_UNSET,
105
#ifndef GST_DISABLE_LOADSAVE_REGISTRY
106
  OBJECT_SAVED,
107
#endif
Wim Taymans's avatar
Wim Taymans committed
108
  DEEP_NOTIFY,
Erik Walthinsen's avatar
Erik Walthinsen committed
109
110
111
  LAST_SIGNAL
};

112
113
enum
{
Erik Walthinsen's avatar
Erik Walthinsen committed
114
  ARG_0,
115
  ARG_NAME
116
      /* FILL ME */
Erik Walthinsen's avatar
Erik Walthinsen committed
117
118
};

119
120
enum
{
121
122
123
124
  SO_OBJECT_LOADED,
  SO_LAST_SIGNAL
};

125
126
/* maps type name quark => count */
static GData *object_name_counts = NULL;
127

128
G_LOCK_DEFINE_STATIC (object_name_mutex);
129

130
131
132
typedef struct _GstSignalObject GstSignalObject;
typedef struct _GstSignalObjectClass GstSignalObjectClass;

133
134
135
static GType gst_signal_object_get_type (void);
static void gst_signal_object_class_init (GstSignalObjectClass * klass);
static void gst_signal_object_init (GstSignalObject * object);
136

Wim Taymans's avatar
Wim Taymans committed
137
#ifndef GST_DISABLE_LOADSAVE_REGISTRY
138
static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
Wim Taymans's avatar
Wim Taymans committed
139
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
140

141
static void gst_object_class_init (GstObjectClass * klass);
142
static void gst_object_init (GTypeInstance * instance, gpointer g_class);
143
144
145
146
147
148
149

static void gst_object_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_object_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
static void gst_object_dispatch_properties_changed (GObject * object,
    guint n_pspecs, GParamSpec ** pspecs);
Wim Taymans's avatar
Wim Taymans committed
150

151
152
static void gst_object_dispose (GObject * object);
static void gst_object_finalize (GObject * object);
153

154
static gboolean gst_object_set_name_default (GstObject * object);
155

156
#ifndef GST_DISABLE_LOADSAVE_REGISTRY
157
158
static void gst_object_real_restore_thyself (GstObject * object,
    xmlNodePtr self);
159
160
#endif

161
static GObjectClass *parent_class = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
162
163
static guint gst_object_signals[LAST_SIGNAL] = { 0 };

164
GType
165
166
gst_object_get_type (void)
{
167
168
  static GType gst_object_type = 0;

169
  if (G_UNLIKELY (gst_object_type == 0)) {
170
171
172
173
174
175
176
177
    static const GTypeInfo object_info = {
      sizeof (GstObjectClass),
      NULL,
      NULL,
      (GClassInitFunc) gst_object_class_init,
      NULL,
      NULL,
      sizeof (GstObject),
178
      0,
179
      gst_object_init,
180
      NULL
Erik Walthinsen's avatar
Erik Walthinsen committed
181
    };
182

183
    gst_object_type =
184
185
        g_type_register_static (G_TYPE_OBJECT, "GstObject", &object_info,
        G_TYPE_FLAG_ABSTRACT);
Erik Walthinsen's avatar
Erik Walthinsen committed
186
  }
187
  return gst_object_type;
Erik Walthinsen's avatar
Erik Walthinsen committed
188
189
}

190
static void
191
gst_object_class_init (GstObjectClass * klass)
192
{
193
  GObjectClass *gobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
194

195
  gobject_class = G_OBJECT_CLASS (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
196

197
  parent_class = g_type_class_peek_parent (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
198

199
200
201
202
#ifndef GST_DISABLE_TRACE
  _gst_object_trace = gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT));
#endif

Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
203
204
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_object_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_object_get_property);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
205

206
  g_object_class_install_property (gobject_class, ARG_NAME,
207
      g_param_spec_string ("name", "Name", "The name of the object",
208
          NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
Wim Taymans's avatar
Wim Taymans committed
209

210
211
  /**
   * GstObject::parent-set:
212
   * @gstobject: a #GstObject
213
214
   * @parent: the new parent
   *
215
   * Emitted when the parent of an object is set.
216
   */
Erik Walthinsen's avatar
Erik Walthinsen committed
217
  gst_object_signals[PARENT_SET] =
218
219
220
      g_signal_new ("parent-set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
      g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
221
222
223

  /**
   * GstObject::parent-unset:
224
   * @gstobject: a #GstObject
225
226
   * @parent: the old parent
   *
227
   * Emitted when the parent of an object is unset.
228
   */
229
  gst_object_signals[PARENT_UNSET] =
230
231
232
      g_signal_new ("parent-unset", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL,
      NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_OBJECT);
233

234
#ifndef GST_DISABLE_LOADSAVE_REGISTRY
235
236
  /**
   * GstObject::object-saved:
237
   * @gstobject: a #GstObject
238
239
   * @xml_node: the xmlNodePtr of the parent node
   *
240
   * Trigered whenever a new object is saved to XML. You can connect to this
241
242
243
244
245
   * signal to insert custom XML tags into the core XML.
   */
  /* FIXME This should be the GType of xmlNodePtr instead of G_TYPE_POINTER
   *       (if libxml would use GObject)
   */
246
  gst_object_signals[OBJECT_SAVED] =
247
248
249
250
      g_signal_new ("object-saved", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, object_saved), NULL,
      NULL, g_cclosure_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);

251
  klass->restore_thyself = gst_object_real_restore_thyself;
252
#endif
253
254
255

  /**
   * GstObject::deep-notify:
256
   * @gstobject: a #GstObject
257
258
259
260
261
262
263
   * @prop_object: the object that originated the signal
   * @prop: the property that changed
   *
   * The deep notify signal is used to be notified of property changes. It is
   * typically attached to the toplevel bin to receive notifications from all
   * the elements contained in that bin.
   */
Wim Taymans's avatar
Wim Taymans committed
264
  gst_object_signals[DEEP_NOTIFY] =
265
266
267
268
269
      g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
      G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
      NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, G_TYPE_OBJECT,
      G_TYPE_PARAM);
270
271

  klass->path_string_separator = "/";
272
273
  klass->lock = g_new0 (GStaticRecMutex, 1);
  g_static_rec_mutex_init (klass->lock);
274
275

  klass->signal_object = g_object_new (gst_signal_object_get_type (), NULL);
Wim Taymans's avatar
Wim Taymans committed
276

277
  /* see the comments at gst_object_dispatch_properties_changed */
Wim Taymans's avatar
Wim Taymans committed
278
  gobject_class->dispatch_properties_changed
279
      = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
280

281
  gobject_class->dispose = gst_object_dispose;
282
  gobject_class->finalize = gst_object_finalize;
Erik Walthinsen's avatar
Erik Walthinsen committed
283
284
}

285
static void
286
gst_object_init (GTypeInstance * instance, gpointer g_class)
287
{
288
289
  GstObject *object = GST_OBJECT (instance);

290
  object->lock = g_mutex_new ();
Erik Walthinsen's avatar
Erik Walthinsen committed
291
  object->parent = NULL;
Wim Taymans's avatar
Wim Taymans committed
292
  object->name = NULL;
Wim Taymans's avatar
Wim Taymans committed
293
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
294

295
296
297
298
#ifndef GST_DISABLE_TRACE
  gst_alloc_trace_new (_gst_object_trace, object);
#endif

299
  object->flags = 0;
300
  GST_OBJECT_FLAG_SET (object, GST_OBJECT_FLOATING);
Erik Walthinsen's avatar
Erik Walthinsen committed
301
302
}

303
304
/**
 * gst_object_ref:
305
 * @object: a #GstObject to reference
306
 *
307
308
 * Increments the refence count on @object. This function
 * does not take the lock on @object because it relies on
309
310
311
312
313
 * atomic refcounting.
 *
 * This object returns the input parameter to ease writing
 * constructs like :
 *  result = gst_object_ref (object->parent);
Wim Taymans's avatar
Wim Taymans committed
314
 *
315
 * Returns: A pointer to @object
316
 */
317
318
gpointer
gst_object_ref (gpointer object)
319
{
320
  g_return_val_if_fail (object != NULL, NULL);
321

322
#ifdef DEBUG_REFCOUNT
Wim Taymans's avatar
Wim Taymans committed
323
324
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d",
      object,
325
326
      ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
#endif
327
  g_object_ref (object);
328
329
330
331
332
333

  return object;
}

/**
 * gst_object_unref:
334
 * @object: a #GstObject to unreference
335
 *
336
337
338
 * Decrements the refence count on @object.  If reference count hits
 * zero, destroy @object. This function does not take the lock
 * on @object as it relies on atomic refcounting.
339
340
341
 *
 * The unref method should never be called with the LOCK held since
 * this might deadlock the dispose function.
342
 */
343
344
void
gst_object_unref (gpointer object)
345
{
346
  g_return_if_fail (object != NULL);
347
348
349
350
351
352
353
  g_return_if_fail (((GObject *) object)->ref_count > 0);

#ifdef DEBUG_REFCOUNT
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d",
      object,
      ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
#endif
354
  g_object_unref (object);
355
356
357
358
}

/**
 * gst_object_sink:
359
360
361
362
363
 * @object: a #GstObject to sink
 *
 * If @object was floating, the #GST_OBJECT_FLOATING flag is removed 
 * and @object is unreffed. When @object was not floating,
 * this function does nothing.
364
 *
365
366
367
368
369
370
 * Any newly created object has a refcount of 1 and is floating. 
 * This function should be used when creating a new object to 
 * symbolically 'take ownership' of @object. This done by first doing a
 * gst_object_ref() to keep a reference to @object and then gst_object_sink()
 * to remove and unref any floating references to @object.
 * Use gst_object_set_parent() to have this done for you.
371
 *
372
 * MT safe. This function grabs and releases @object lock.
373
374
 */
void
375
gst_object_sink (gpointer object)
376
377
378
{
  g_return_if_fail (GST_IS_OBJECT (object));

379
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
Wim Taymans's avatar
Wim Taymans committed
380

381
  GST_OBJECT_LOCK (object);
382
  if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
383
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
384
    GST_OBJECT_UNLOCK (object);
385
    gst_object_unref (object);
386
  } else {
387
    GST_OBJECT_UNLOCK (object);
388
389
390
  }
}

391
/**
392
 * gst_object_replace:
393
394
 * @oldobj: pointer to a place of a #GstObject to replace
 * @newobj: a new #GstObject
395
 *
396
397
 * Unrefs the #GstObject pointed to by @oldobj, refs @newobj and
 * puts @newobj in *@oldobj. Be carefull when calling this
398
 * function, it does not take any locks. You might want to lock
399
 * the object owning @oldobj pointer before calling this
400
 * function.
401
 *
402
 * Make sure not to LOCK @oldobj because it might be unreffed
403
 * which could cause a deadlock when it is disposed.
404
405
 */
void
406
gst_object_replace (GstObject ** oldobj, GstObject * newobj)
407
{
408
409
410
  g_return_if_fail (oldobj != NULL);
  g_return_if_fail (*oldobj == NULL || GST_IS_OBJECT (*oldobj));
  g_return_if_fail (newobj == NULL || GST_IS_OBJECT (newobj));
411

412
#ifdef DEBUG_REFCOUNT
413
  GST_CAT_LOG (GST_CAT_REFCOUNTING, "replace %s (%d) with %s (%d)",
414
      *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
415
416
417
      *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
      newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
      newobj ? G_OBJECT (newobj)->ref_count : 0);
418
#endif
Wim Taymans's avatar
Wim Taymans committed
419

420
  if (G_LIKELY (*oldobj != newobj)) {
421
422
423
424
    if (newobj)
      gst_object_ref (newobj);
    if (*oldobj)
      gst_object_unref (*oldobj);
425
426
427
428
429

    *oldobj = newobj;
  }
}

430
431
/* dispose is called when the object has to release all links
 * to other objects */
432
static void
433
gst_object_dispose (GObject * object)
434
{
435
436
  GstObject *parent;

437
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
438

439
  GST_OBJECT_LOCK (object);
440
441
  if ((parent = GST_OBJECT_PARENT (object)))
    goto have_parent;
442
  GST_OBJECT_PARENT (object) = NULL;
443
  GST_OBJECT_UNLOCK (object);
444

445
  parent_class->dispose (object);
446
447
448
449
450
451
452
453
454
455
456

  return;

  /* ERRORS */
have_parent:
  {
    g_critical ("\nTrying to dispose object \"%s\", but it still has a "
        "parent \"%s\".\nYou need to let the parent manage the "
        "object instead of unreffing the object directly.\n",
        GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
    GST_OBJECT_UNLOCK (object);
457
    /* ref the object again to revive it in this error case */
458
459
460
    object = gst_object_ref (object);
    return;
  }
461
462
}

463
/* finalize is called when the object has to free its resources */
464
static void
465
gst_object_finalize (GObject * object)
466
{
467
  GstObject *gstobject = GST_OBJECT (object);
468

469
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
470

471
472
  g_signal_handlers_destroy (object);

473
  g_free (gstobject->name);
474
  g_mutex_free (gstobject->lock);
475

476
#ifndef GST_DISABLE_TRACE
477
  gst_alloc_trace_free (_gst_object_trace, object);
478
479
#endif

480
  parent_class->finalize (object);
481
482
}

483
484
485
/* Changing a GObject property of a GstObject will result in "deep_notify"
 * signals being emitted by the object itself, as well as in each parent
 * object. This is so that an application can connect a listener to the
Wim Taymans's avatar
Wim Taymans committed
486
 * top-level bin to catch property-change notifications for all contained
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
487
 * elements.
488
 *
489
490
 * This function is not MT safe in glib < 2.8 so we need to lock it with a
 * classwide mutex in that case.
491
492
493
 *
 * MT safe.
 */
Wim Taymans's avatar
Wim Taymans committed
494
static void
495
496
gst_object_dispatch_properties_changed (GObject * object,
    guint n_pspecs, GParamSpec ** pspecs)
Wim Taymans's avatar
Wim Taymans committed
497
{
498
  GstObject *gst_object, *parent, *old_parent;
Wim Taymans's avatar
Wim Taymans committed
499
  guint i;
500
501
502
503
504
505
506
  gchar *name, *debug_name;
  GstObjectClass *klass;

  /* we fail when this is not a GstObject */
  g_return_if_fail (GST_IS_OBJECT (object));

  klass = GST_OBJECT_GET_CLASS (object);
Wim Taymans's avatar
Wim Taymans committed
507
508

  /* do the standard dispatching */
509
510
  G_OBJECT_CLASS (parent_class)->dispatch_properties_changed (object, n_pspecs,
      pspecs);
511
512
513
514

  gst_object = GST_OBJECT_CAST (object);
  name = gst_object_get_name (gst_object);
  debug_name = GST_STR_NULL (name);
Wim Taymans's avatar
Wim Taymans committed
515
516

  /* now let the parent dispatch those, too */
517
518
519
520
  parent = gst_object_get_parent (gst_object);
  while (parent) {
    /* for debugging ... */
    gchar *parent_name = gst_object_get_name (parent);
521
522

#ifndef GST_DISABLE_GST_DEBUG
523
    gchar *debug_parent_name = GST_STR_NULL (parent_name);
524
#endif
525

Wim Taymans's avatar
Wim Taymans committed
526
527
    /* need own category? */
    for (i = 0; i < n_pspecs; i++) {
528
529
530
531
532
      GST_CAT_LOG (GST_CAT_EVENT, "deep notification from %s to %s (%s)",
          debug_name, debug_parent_name, pspecs[i]->name);

      g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
          g_quark_from_string (pspecs[i]->name), GST_OBJECT_CAST (object),
533
          pspecs[i]);
Wim Taymans's avatar
Wim Taymans committed
534
    }
535
    g_free (parent_name);
Wim Taymans's avatar
Wim Taymans committed
536

537
538
539
    old_parent = parent;
    parent = gst_object_get_parent (old_parent);
    gst_object_unref (old_parent);
Wim Taymans's avatar
Wim Taymans committed
540
  }
541
  g_free (name);
Wim Taymans's avatar
Wim Taymans committed
542
543
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
544
/**
Wim Taymans's avatar
Wim Taymans committed
545
546
547
548
 * gst_object_default_deep_notify:
 * @object: the #GObject that signalled the notify.
 * @orig: a #GstObject that initiated the notify.
 * @pspec: a #GParamSpec of the property.
Wim Taymans's avatar
Wim Taymans committed
549
550
 * @excluded_props: a set of user-specified properties to exclude or
 *  NULL to show all changes.
Wim Taymans's avatar
Wim Taymans committed
551
 *
552
 * A default deep_notify signal callback for an object. The user data
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
553
554
 * should contain a pointer to an array of strings that should be excluded
 * from the notify. The default handler will print the new value of the property
Wim Taymans's avatar
Wim Taymans committed
555
 * using g_print.
556
 *
557
558
 * MT safe. This function grabs and releases @object's LOCK for getting its
 *          path string.
Wim Taymans's avatar
Wim Taymans committed
559
560
 */
void
561
562
gst_object_default_deep_notify (GObject * object, GstObject * orig,
    GParamSpec * pspec, gchar ** excluded_props)
Wim Taymans's avatar
Wim Taymans committed
563
{
564
  GValue value = { 0, };        /* the important thing is that value.type = 0 */
565
  gchar *str = NULL;
566
  gchar *name = NULL;
Wim Taymans's avatar
Wim Taymans committed
567
568
569
570
571

  if (pspec->flags & G_PARAM_READABLE) {
    /* let's not print these out for excluded properties... */
    while (excluded_props != NULL && *excluded_props != NULL) {
      if (strcmp (pspec->name, *excluded_props) == 0)
572
        return;
Wim Taymans's avatar
Wim Taymans committed
573
574
575
576
577
578
579
      excluded_props++;
    }
    g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
    g_object_get_property (G_OBJECT (orig), pspec->name, &value);

    if (G_IS_PARAM_SPEC_ENUM (pspec)) {
      GEnumValue *enum_value;
580
581

      enum_value =
582
583
          g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (pspec->value_type)),
          g_value_get_enum (&value));
Wim Taymans's avatar
Wim Taymans committed
584
585

      str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
586
          enum_value->value);
587
    } else {
Wim Taymans's avatar
Wim Taymans committed
588
589
      str = g_strdup_value_contents (&value);
    }
590
591
592
    name = gst_object_get_path_string (orig);
    g_print ("%s: %s = %s\n", name, pspec->name, str);
    g_free (name);
Wim Taymans's avatar
Wim Taymans committed
593
594
595
    g_free (str);
    g_value_unset (&value);
  } else {
596
    name = gst_object_get_path_string (orig);
597
    g_warning ("Parameter %s not readable in %s.", pspec->name, name);
598
    g_free (name);
Wim Taymans's avatar
Wim Taymans committed
599
600
601
  }
}

602
static gboolean
603
gst_object_set_name_default (GstObject * object)
604
{
605
  const gchar *type_name;
606
  gint count;
607
  gchar *name, *tmp;
608
  gboolean result;
609
  GQuark q;
610

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
611
612
  /* to ensure guaranteed uniqueness across threads, only one thread
   * may ever assign a name */
613
614
  G_LOCK (object_name_mutex);

615
  if (!object_name_counts) {
616
    g_datalist_init (&object_name_counts);
617
  }
618

619
620
621
  q = g_type_qname (G_OBJECT_TYPE (object));
  count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
  g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
622

623
624
  G_UNLOCK (object_name_mutex);

625
  /* GstFooSink -> foosinkN */
626
  type_name = g_quark_to_string (q);
627
628
629
630
631
  if (strncmp (type_name, "Gst", 3) == 0)
    type_name += 3;
  tmp = g_strdup_printf ("%s%d", type_name, count);
  name = g_ascii_strdown (tmp, strlen (tmp));
  g_free (tmp);
632

633
  result = gst_object_set_name (object, name);
634
  g_free (name);
635
636

  return result;
637
638
}

639
640
/**
 * gst_object_set_name:
641
 * @object: a #GstObject 
642
 * @name:   new name of object
643
 *
644
 * Sets the name of @object, or gives @object a guaranteed unique
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
645
 * name (if @name is NULL).
646
647
648
 * This function makes a copy of the provided name, so the caller
 * retains ownership of the name it sent.
 *
649
650
651
 * Returns: TRUE if the name could be set. Since Objects that have
 * a parent cannot be renamed, this function returns FALSE in those
 * cases.
652
 *
653
 * MT safe.  This function grabs and releases @object's LOCK.
654
 */
655
gboolean
656
gst_object_set_name (GstObject * object, const gchar * name)
657
{
658
  gboolean result;
659

660
661
  g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);

662
  GST_OBJECT_LOCK (object);
663

664
665
666
667
668
669
  /* parented objects cannot be renamed */
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;

  if (name != NULL) {
    g_free (object->name);
670
    object->name = g_strdup (name);
671
    GST_OBJECT_UNLOCK (object);
672
673
    result = TRUE;
  } else {
674
    GST_OBJECT_UNLOCK (object);
675
    result = gst_object_set_name_default (object);
676
677
678
679
680
681
  }
  return result;

  /* error */
had_parent:
  {
682
    GST_OBJECT_UNLOCK (object);
683
684
    return FALSE;
  }
685
686
687
688
}

/**
 * gst_object_get_name:
689
 * @object: a #GstObject
690
 *
691
 * Returns a copy of the name of @object.
692
693
694
 * Caller should g_free() the return value after usage.
 * For a nameless object, this returns NULL, which you can safely g_free()
 * as well.
695
 *
696
 * Returns: the name of @object. g_free() after usage.
697
 *
698
 * MT safe. This function grabs and releases @object's LOCK.
699
 */
700
gchar *
701
gst_object_get_name (GstObject * object)
702
{
703
704
  gchar *result = NULL;

705
706
  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

707
  GST_OBJECT_LOCK (object);
708
  result = g_strdup (object->name);
709
  GST_OBJECT_UNLOCK (object);
710
711
712
713
714
715

  return result;
}

/**
 * gst_object_set_name_prefix:
716
717
 * @object:      a #GstObject 
 * @name_prefix: new name prefix of @object
718
 *
719
 * Sets the name prefix of @object to @name_prefix.
720
721
722
 * This function makes a copy of the provided name prefix, so the caller
 * retains ownership of the name prefix it sent.
 *
723
 * MT safe.  This function grabs and releases @object's LOCK.
724
725
726
727
728
729
 */
void
gst_object_set_name_prefix (GstObject * object, const gchar * name_prefix)
{
  g_return_if_fail (GST_IS_OBJECT (object));

730
  GST_OBJECT_LOCK (object);
731
732
  g_free (object->name_prefix);
  object->name_prefix = g_strdup (name_prefix); /* NULL gives NULL */
733
  GST_OBJECT_UNLOCK (object);
734
735
736
737
}

/**
 * gst_object_get_name_prefix:
738
 * @object: a #GstObject 
739
 *
740
 * Returns a copy of the name prefix of @object.
741
742
743
744
 * Caller should g_free() the return value after usage.
 * For a prefixless object, this returns NULL, which you can safely g_free()
 * as well.
 *
745
 * Returns: the name prefix of @object. g_free() after usage.
746
 *
747
 * MT safe. This function grabs and releases @object's LOCK.
748
749
750
751
752
753
754
755
 */
gchar *
gst_object_get_name_prefix (GstObject * object)
{
  gchar *result = NULL;

  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

756
  GST_OBJECT_LOCK (object);
757
  result = g_strdup (object->name_prefix);
758
  GST_OBJECT_UNLOCK (object);
759
760

  return result;
Erik Walthinsen's avatar
Erik Walthinsen committed
761
762
763
764
}

/**
 * gst_object_set_parent:
765
 * @object: a #GstObject 
Erik Walthinsen's avatar
Erik Walthinsen committed
766
767
 * @parent: new parent of object
 *
768
769
 * Sets the parent of @object to @parent. The object's reference count will 
 * be incremented, and any floating reference will be removed (see gst_object_sink()).
770
 *
771
772
 * This function causes the parent-set signal to be emitted when the parent
 * was successfully set.
773
 *
774
775
 * Returns: TRUE if @parent could be set or FALSE when @object
 * already had a parent or @object and @parent are the same.
776
 *
777
 * MT safe. Grabs and releases @object's LOCK.
Erik Walthinsen's avatar
Erik Walthinsen committed
778
 */
779
gboolean
780
gst_object_set_parent (GstObject * object, GstObject * parent)
781
{
782
783
784
  g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
  g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
  g_return_val_if_fail (object != parent, FALSE);
785

786
787
  GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
      "set parent (ref and sink)");
788

789
  GST_OBJECT_LOCK (object);
790
791
792
793
794
795
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;

  /* sink object, we don't call our own function because we don't
   * need to release/acquire the lock needlessly or touch the refcount
   * in the floating case. */
Erik Walthinsen's avatar
Erik Walthinsen committed
796
  object->parent = parent;
797
  if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
798
    GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "unsetting floating flag");
799
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
800
    GST_OBJECT_UNLOCK (object);
801
  } else {
802
    GST_OBJECT_UNLOCK (object);
803
804
    gst_object_ref (object);
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
805

806
  g_signal_emit (G_OBJECT (object), gst_object_signals[PARENT_SET], 0, parent);
807

808
809
  return TRUE;

810
  /* ERROR handling */
811
812
had_parent:
  {
813
814
    GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
        "set parent failed, object already had a parent");
815
    GST_OBJECT_UNLOCK (object);
816
817
    return FALSE;
  }
Erik Walthinsen's avatar
Erik Walthinsen committed
818
819
820
821
}

/**
 * gst_object_get_parent:
822
 * @object: a #GstObject 
Erik Walthinsen's avatar
Erik Walthinsen committed
823
 *
824
825
 * Returns the parent of @object. This function increases the refcount
 * of the parent object so you should gst_object_unref() it after usage.
Erik Walthinsen's avatar
Erik Walthinsen committed
826
 *
827
 * Returns: parent of @object, this can be NULL if @object has no
828
829
 *   parent. unref after usage.
 *
830
 * MT safe. Grabs and releases @object's LOCK.
Erik Walthinsen's avatar
Erik Walthinsen committed
831
 */
832
833
GstObject *
gst_object_get_parent (GstObject * object)
834
{
835
836
  GstObject *result = NULL;

837
  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
Erik Walthinsen's avatar
Erik Walthinsen committed
838

839
  GST_OBJECT_LOCK (object);
840
841
842
  result = object->parent;
  if (G_LIKELY (result))
    gst_object_ref (result);
843
  GST_OBJECT_UNLOCK (object);
844