gstobject.c 36.3 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 */
Stefan Kost's avatar
Stefan Kost committed
101 102
/* FIXME-0.11: have a read-only parent property instead of the two signals
 * then we get notify::parent for free */
103 104
enum
{
Erik Walthinsen's avatar
Erik Walthinsen committed
105
  PARENT_SET,
106
  PARENT_UNSET,
107
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
108
  OBJECT_SAVED,
109
#endif
Wim Taymans's avatar
Wim Taymans committed
110
  DEEP_NOTIFY,
Erik Walthinsen's avatar
Erik Walthinsen committed
111 112 113
  LAST_SIGNAL
};

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

121 122
enum
{
123 124 125 126
  SO_OBJECT_LOADED,
  SO_LAST_SIGNAL
};

127 128
/* maps type name quark => count */
static GData *object_name_counts = NULL;
129

130
G_LOCK_DEFINE_STATIC (object_name_mutex);
131

132 133 134
typedef struct _GstSignalObject GstSignalObject;
typedef struct _GstSignalObjectClass GstSignalObjectClass;

135
static GType gst_signal_object_get_type (void);
136

137
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
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 142 143 144 145 146
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
147

148 149
static void gst_object_dispose (GObject * object);
static void gst_object_finalize (GObject * object);
150

151
static gboolean gst_object_set_name_default (GstObject * object);
152

153 154 155 156
#ifdef GST_DISABLE_DEPRECATED
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
#undef GstXmlNodePtr
#define GstXmlNodePtr xmlNodePtr
157
#include <libxml/parser.h>
158 159 160 161 162 163 164 165 166
GstXmlNodePtr gst_object_save_thyself (GstObject * object,
    GstXmlNodePtr parent);
void gst_object_restore_thyself (GstObject * object, GstXmlNodePtr parent);
void gst_class_signal_emit_by_name (GstObject * object, const gchar * name,
    GstXmlNodePtr self);
#endif
#endif

#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
167
static void gst_object_real_restore_thyself (GstObject * object,
168
    GstXmlNodePtr self);
169 170
#endif

171
static GObjectClass *parent_class = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
172 173
static guint gst_object_signals[LAST_SIGNAL] = { 0 };

174
G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_OBJECT);
Erik Walthinsen's avatar
Erik Walthinsen committed
175

176
static void
177
gst_object_class_init (GstObjectClass * klass)
178
{
179
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
180

181
  parent_class = g_type_class_peek_parent (klass);
Erik Walthinsen's avatar
Erik Walthinsen committed
182

183 184 185 186
#ifndef GST_DISABLE_TRACE
  _gst_object_trace = gst_alloc_trace_register (g_type_name (GST_TYPE_OBJECT));
#endif

187 188
  gobject_class->set_property = gst_object_set_property;
  gobject_class->get_property = gst_object_get_property;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
189

190
  g_object_class_install_property (gobject_class, ARG_NAME,
191
      g_param_spec_string ("name", "Name", "The name of the object",
192 193
          NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
194

195 196
  /**
   * GstObject::parent-set:
197
   * @gstobject: a #GstObject
198 199
   * @parent: the new parent
   *
200
   * Emitted when the parent of an object is set.
201
   */
Erik Walthinsen's avatar
Erik Walthinsen committed
202
  gst_object_signals[PARENT_SET] =
203 204
      g_signal_new ("parent-set", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstObjectClass, parent_set), NULL, NULL,
205
      g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_OBJECT);
206 207 208

  /**
   * GstObject::parent-unset:
209
   * @gstobject: a #GstObject
210 211
   * @parent: the old parent
   *
212
   * Emitted when the parent of an object is unset.
213
   */
214
  gst_object_signals[PARENT_UNSET] =
215 216
      g_signal_new ("parent-unset", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstObjectClass, parent_unset), NULL,
217
      NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, GST_TYPE_OBJECT);
218

219
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
220 221
  /**
   * GstObject::object-saved:
222
   * @gstobject: a #GstObject
223 224
   * @xml_node: the xmlNodePtr of the parent node
   *
225
   * Trigered whenever a new object is saved to XML. You can connect to this
226 227 228 229 230
   * 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)
   */
231
  gst_object_signals[OBJECT_SAVED] =
232 233 234 235
      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);

236 237 238
  klass->restore_thyself =
      ((void (*)(GstObject * object,
              gpointer self)) *gst_object_real_restore_thyself);
239
#endif
240 241 242

  /**
   * GstObject::deep-notify:
243
   * @gstobject: a #GstObject
244 245 246 247 248 249 250
   * @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
251
  gst_object_signals[DEEP_NOTIFY] =
252 253 254
      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,
255
      NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
256
      G_TYPE_PARAM);
257 258

  klass->path_string_separator = "/";
259 260
  /* FIXME 0.11: Store this directly in the class struct */
  klass->lock = g_slice_new (GStaticRecMutex);
261
  g_static_rec_mutex_init (klass->lock);
262

263
  klass->signal_object = g_object_newv (gst_signal_object_get_type (), 0, NULL);
Wim Taymans's avatar
Wim Taymans committed
264

265
  /* see the comments at gst_object_dispatch_properties_changed */
Wim Taymans's avatar
Wim Taymans committed
266
  gobject_class->dispatch_properties_changed
267
      = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
268

269
  gobject_class->dispose = gst_object_dispose;
270
  gobject_class->finalize = gst_object_finalize;
Erik Walthinsen's avatar
Erik Walthinsen committed
271 272
}

273
static void
274
gst_object_init (GstObject * object)
275
{
276
  object->lock = g_mutex_new ();
Erik Walthinsen's avatar
Erik Walthinsen committed
277
  object->parent = NULL;
Wim Taymans's avatar
Wim Taymans committed
278
  object->name = NULL;
279
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
280

281 282 283 284
#ifndef GST_DISABLE_TRACE
  gst_alloc_trace_new (_gst_object_trace, object);
#endif

285
  object->flags = 0;
286
  GST_OBJECT_FLAG_SET (object, GST_OBJECT_FLOATING);
Erik Walthinsen's avatar
Erik Walthinsen committed
287 288
}

289 290
/**
 * gst_object_ref:
291
 * @object: a #GstObject to reference
292
 *
293
 * Increments the reference count on @object. This function
294
 * does not take the lock on @object because it relies on
295 296 297 298 299
 * 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
300
 *
301
 * Returns: A pointer to @object
302
 */
303 304
gpointer
gst_object_ref (gpointer object)
305
{
306
  g_return_val_if_fail (object != NULL, NULL);
307

308
#ifdef DEBUG_REFCOUNT
309
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
310 311
      ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
#endif
312
  g_object_ref (object);
313 314 315 316 317 318

  return object;
}

/**
 * gst_object_unref:
319
 * @object: a #GstObject to unreference
320
 *
321
 * Decrements the reference count on @object.  If reference count hits
322 323
 * zero, destroy @object. This function does not take the lock
 * on @object as it relies on atomic refcounting.
324 325 326
 *
 * The unref method should never be called with the LOCK held since
 * this might deadlock the dispose function.
327
 */
328 329
void
gst_object_unref (gpointer object)
330
{
331
  g_return_if_fail (object != NULL);
332
  g_return_if_fail (((GObject *) object)->ref_count);
333 334

#ifdef DEBUG_REFCOUNT
335
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
336 337
      ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
#endif
338
  g_object_unref (object);
339 340
}

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/**
 * gst_object_ref_sink:
 * @object: a #GstObject to sink
 *
 * Increase the reference count of @object, and possibly remove the floating
 * reference, if @object has a floating reference.
 *
 * In other words, if the object is floating, then this call "assumes ownership"
 * of the floating reference, converting it to a normal reference by clearing
 * the floating flag while leaving the reference count unchanged. If the object
 * is not floating, then this call adds a new normal reference increasing the
 * reference count by one.
 *
 * MT safe. This function grabs and releases @object lock.
 *
 * Since: 0.10.24
 */
void
gst_object_ref_sink (gpointer object)
{
  g_return_if_fail (GST_IS_OBJECT (object));

  GST_OBJECT_LOCK (object);
  if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
365 366
    GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object,
        "unsetting floating flag");
367 368 369 370 371 372 373 374
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
    GST_OBJECT_UNLOCK (object);
  } else {
    GST_OBJECT_UNLOCK (object);
    gst_object_ref (object);
  }
}

375 376
/**
 * gst_object_sink:
377 378
 * @object: a #GstObject to sink
 *
379
 * If @object was floating, the #GST_OBJECT_FLOATING flag is removed
380 381
 * and @object is unreffed. When @object was not floating,
 * this function does nothing.
382
 *
383 384
 * Any newly created object has a refcount of 1 and is floating.
 * This function should be used when creating a new object to
385 386 387 388
 * 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.
389
 *
390
 * MT safe. This function grabs and releases @object lock.
391 392
 */
void
393
gst_object_sink (gpointer object)
394 395 396
{
  g_return_if_fail (GST_IS_OBJECT (object));

397
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "sink");
Wim Taymans's avatar
Wim Taymans committed
398

399
  GST_OBJECT_LOCK (object);
400
  if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
401
    GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "clear floating flag");
402
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
403
    GST_OBJECT_UNLOCK (object);
404
    gst_object_unref (object);
405
  } else {
406
    GST_OBJECT_UNLOCK (object);
407 408 409
  }
}

410
/**
411
 * gst_object_replace:
412 413 414
 * @oldobj: (inout) (transfer full): pointer to a place of a #GstObject to
 *     replace
 * @newobj: (transfer none): a new #GstObject
415
 *
416 417
 * Unrefs the #GstObject pointed to by @oldobj, refs @newobj and
 * puts @newobj in *@oldobj. Be carefull when calling this
418
 * function, it does not take any locks. You might want to lock
419
 * the object owning @oldobj pointer before calling this
420
 * function.
421
 *
422
 * Make sure not to LOCK @oldobj because it might be unreffed
423
 * which could cause a deadlock when it is disposed.
424 425
 */
void
426
gst_object_replace (GstObject ** oldobj, GstObject * newobj)
427
{
428 429 430
  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));
431

432
#ifdef DEBUG_REFCOUNT
433
  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
434
      *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
435
      *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
436
      newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
437
      newobj ? G_OBJECT (newobj)->ref_count : 0);
438
#endif
Wim Taymans's avatar
Wim Taymans committed
439

440
  if (G_LIKELY (*oldobj != newobj)) {
441 442 443 444
    if (newobj)
      gst_object_ref (newobj);
    if (*oldobj)
      gst_object_unref (*oldobj);
445 446 447 448 449

    *oldobj = newobj;
  }
}

450 451
/* dispose is called when the object has to release all links
 * to other objects */
452
static void
453
gst_object_dispose (GObject * object)
454
{
455 456
  GstObject *parent;

457
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
458

459
  GST_OBJECT_LOCK (object);
460 461
  if ((parent = GST_OBJECT_PARENT (object)))
    goto have_parent;
462
  GST_OBJECT_PARENT (object) = NULL;
463
  GST_OBJECT_UNLOCK (object);
464

465
  parent_class->dispose (object);
466 467 468 469 470 471 472 473 474 475 476

  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);
477
    /* ref the object again to revive it in this error case */
478
    gst_object_ref (object);
479 480
    return;
  }
481 482
}

483
/* finalize is called when the object has to free its resources */
484
static void
485
gst_object_finalize (GObject * object)
486
{
Wim Taymans's avatar
Wim Taymans committed
487
  GstObject *gstobject = GST_OBJECT_CAST (object);
488

489
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
490

491 492
  g_signal_handlers_destroy (object);

493
  g_free (gstobject->name);
494
  g_mutex_free (gstobject->lock);
495

496
#ifndef GST_DISABLE_TRACE
497
  gst_alloc_trace_free (_gst_object_trace, object);
498 499
#endif

500
  parent_class->finalize (object);
501 502
}

503
/* Changing a GObject property of a GstObject will result in "deep-notify"
504 505
 * 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
506
 * top-level bin to catch property-change notifications for all contained
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
507
 * elements.
508
 *
509 510
 * This function is not MT safe in glib < 2.8 so we need to lock it with a
 * classwide mutex in that case.
511 512 513
 *
 * MT safe.
 */
Wim Taymans's avatar
Wim Taymans committed
514
static void
515 516
gst_object_dispatch_properties_changed (GObject * object,
    guint n_pspecs, GParamSpec ** pspecs)
Wim Taymans's avatar
Wim Taymans committed
517
{
518
  GstObject *gst_object, *parent, *old_parent;
Wim Taymans's avatar
Wim Taymans committed
519
  guint i;
520 521
#ifndef GST_DISABLE_GST_DEBUG
  gchar *name = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
522
  const gchar *debug_name;
523
#endif
524

Wim Taymans's avatar
Wim Taymans committed
525
  /* do the standard dispatching */
Wim Taymans's avatar
Wim Taymans committed
526
  parent_class->dispatch_properties_changed (object, n_pspecs, pspecs);
527 528

  gst_object = GST_OBJECT_CAST (object);
529 530 531 532 533 534 535
#ifndef GST_DISABLE_GST_DEBUG
  if (G_UNLIKELY (__gst_debug_min >= GST_LEVEL_LOG)) {
    name = gst_object_get_name (gst_object);
    debug_name = GST_STR_NULL (name);
  } else
    debug_name = "";
#endif
Wim Taymans's avatar
Wim Taymans committed
536 537

  /* now let the parent dispatch those, too */
538 539
  parent = gst_object_get_parent (gst_object);
  while (parent) {
Wim Taymans's avatar
Wim Taymans committed
540
    for (i = 0; i < n_pspecs; i++) {
541 542
      GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
          "deep notification from %s (%s)", debug_name, pspecs[i]->name);
543 544

      g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
Wim Taymans's avatar
Wim Taymans committed
545
          g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
Wim Taymans's avatar
Wim Taymans committed
546 547
    }

548 549 550
    old_parent = parent;
    parent = gst_object_get_parent (old_parent);
    gst_object_unref (old_parent);
Wim Taymans's avatar
Wim Taymans committed
551
  }
552
#ifndef GST_DISABLE_GST_DEBUG
553
  g_free (name);
554
#endif
Wim Taymans's avatar
Wim Taymans committed
555 556
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
557
/**
Wim Taymans's avatar
Wim Taymans committed
558 559 560 561
 * 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.
562 563 564
 * @excluded_props: (array zero-terminated=1) (element-type gchar*)
 *     (allow-none):a set of user-specified properties to exclude or
 *     NULL to show all changes.
Wim Taymans's avatar
Wim Taymans committed
565
 *
566
 * A default deep_notify signal callback for an object. The user data
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
567 568
 * 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
569
 * using g_print.
570
 *
571 572
 * MT safe. This function grabs and releases @object's LOCK for getting its
 *          path string.
Wim Taymans's avatar
Wim Taymans committed
573 574
 */
void
575 576
gst_object_default_deep_notify (GObject * object, GstObject * orig,
    GParamSpec * pspec, gchar ** excluded_props)
Wim Taymans's avatar
Wim Taymans committed
577
{
578
  GValue value = { 0, };        /* the important thing is that value.type = 0 */
579
  gchar *str = NULL;
580
  gchar *name = NULL;
Wim Taymans's avatar
Wim Taymans committed
581 582 583 584 585

  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)
586
        return;
Wim Taymans's avatar
Wim Taymans committed
587 588
      excluded_props++;
    }
589
    g_value_init (&value, pspec->value_type);
Wim Taymans's avatar
Wim Taymans committed
590 591
    g_object_get_property (G_OBJECT (orig), pspec->name, &value);

592
    /* FIXME: handle flags */
Wim Taymans's avatar
Wim Taymans committed
593 594
    if (G_IS_PARAM_SPEC_ENUM (pspec)) {
      GEnumValue *enum_value;
595
      GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
596

597
      enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
Wim Taymans's avatar
Wim Taymans committed
598 599

      str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
600
          enum_value->value);
601
      g_type_class_unref (klass);
602
    } else {
Wim Taymans's avatar
Wim Taymans committed
603 604
      str = g_strdup_value_contents (&value);
    }
605 606 607
    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
608 609 610
    g_free (str);
    g_value_unset (&value);
  } else {
611
    name = gst_object_get_path_string (orig);
612
    g_warning ("Parameter %s not readable in %s.", pspec->name, name);
613
    g_free (name);
Wim Taymans's avatar
Wim Taymans committed
614 615 616
  }
}

617
static gboolean
618
gst_object_set_name_default (GstObject * object)
619
{
620
  const gchar *type_name;
621
  gint count;
622
  gchar *name;
623
  GQuark q;
624
  guint i, l;
625

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
626 627
  /* to ensure guaranteed uniqueness across threads, only one thread
   * may ever assign a name */
628 629
  G_LOCK (object_name_mutex);

630
  if (!object_name_counts) {
631
    g_datalist_init (&object_name_counts);
632
  }
633

634 635 636
  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));
637

638 639
  G_UNLOCK (object_name_mutex);

640
  /* GstFooSink -> foosink<N> */
641
  type_name = g_quark_to_string (q);
642 643
  if (strncmp (type_name, "Gst", 3) == 0)
    type_name += 3;
644 645 646 647
  l = strlen (type_name);
  name = g_malloc (l + 6 + 1);
  for (i = 0; i < l; i++)
    name[i] = g_ascii_tolower (type_name[i]);
648
  g_snprintf (&name[i], 6, "%d", count);
649

650 651 652
  GST_OBJECT_LOCK (object);
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;
653

654 655 656 657
  g_free (object->name);
  object->name = name;

  GST_OBJECT_UNLOCK (object);
658

659 660 661 662
  return TRUE;

had_parent:
  {
663
    g_free (name);
664 665 666 667
    GST_WARNING ("parented objects can't be renamed");
    GST_OBJECT_UNLOCK (object);
    return FALSE;
  }
668 669
}

670 671
/**
 * gst_object_set_name:
672
 * @object: a #GstObject
673
 * @name:   new name of object
674
 *
675
 * Sets the name of @object, or gives @object a guaranteed unique
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
676
 * name (if @name is NULL).
677 678 679
 * This function makes a copy of the provided name, so the caller
 * retains ownership of the name it sent.
 *
680 681 682
 * Returns: TRUE if the name could be set. Since Objects that have
 * a parent cannot be renamed, this function returns FALSE in those
 * cases.
683
 *
684
 * MT safe.  This function grabs and releases @object's LOCK.
685
 */
686
gboolean
687
gst_object_set_name (GstObject * object, const gchar * name)
688
{
689
  gboolean result;
690

691 692
  g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);

693
  GST_OBJECT_LOCK (object);
694

695 696 697 698 699 700
  /* parented objects cannot be renamed */
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;

  if (name != NULL) {
    g_free (object->name);
701
    object->name = g_strdup (name);
702
    GST_OBJECT_UNLOCK (object);
703 704
    result = TRUE;
  } else {
705
    GST_OBJECT_UNLOCK (object);
706
    result = gst_object_set_name_default (object);
707
  }
708 709 710 711
  /* FIXME-0.11: this misses a g_object_notify (object, "name"); unless called
   * from gst_object_set_property.
   * Ideally remove such custom setters (or make it static).
   */
712 713 714 715 716
  return result;

  /* error */
had_parent:
  {
Stefan Kost's avatar
Stefan Kost committed
717
    GST_WARNING ("parented objects can't be renamed");
718
    GST_OBJECT_UNLOCK (object);
719 720
    return FALSE;
  }
721 722 723 724
}

/**
 * gst_object_get_name:
725
 * @object: a #GstObject
726
 *
727
 * Returns a copy of the name of @object.
728 729 730
 * Caller should g_free() the return value after usage.
 * For a nameless object, this returns NULL, which you can safely g_free()
 * as well.
731
 *
732 733 734
 * Free-function: g_free
 *
 * Returns: (transfer full): the name of @object. g_free() after usage.
735
 *
736
 * MT safe. This function grabs and releases @object's LOCK.
737
 */
738
gchar *
739
gst_object_get_name (GstObject * object)
740
{
741 742
  gchar *result = NULL;

743 744
  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

745
  GST_OBJECT_LOCK (object);
746
  result = g_strdup (object->name);
747
  GST_OBJECT_UNLOCK (object);
748 749 750 751 752 753

  return result;
}

/**
 * gst_object_set_name_prefix:
754
 * @object:      a #GstObject
755
 * @name_prefix: new name prefix of @object
756
 *
757
 * Sets the name prefix of @object to @name_prefix.
758 759 760
 * This function makes a copy of the provided name prefix, so the caller
 * retains ownership of the name prefix it sent.
 *
761
 * MT safe.  This function grabs and releases @object's LOCK.
762 763 764
 *
 * Deprecated: deprecated because the name prefix has never actually been used
 *     for anything.