gstobject.c 36.4 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
#include "gst_private.h"
87
#include "glib-compat-private.h"
88

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

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

99 100
#define DEBUG_REFCOUNT

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

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

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

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

131
G_LOCK_DEFINE_STATIC (object_name_mutex);
132

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

136
static GType gst_signal_object_get_type (void);
137

138
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
139
static guint gst_signal_object_signals[SO_LAST_SIGNAL] = { 0 };
Wim Taymans's avatar
Wim Taymans committed
140
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
141

142 143 144 145 146 147
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
148

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

152
static gboolean gst_object_set_name_default (GstObject * object);
153

154 155 156 157
#ifdef GST_DISABLE_DEPRECATED
#if !defined(GST_DISABLE_LOADSAVE) && !defined(GST_REMOVE_DEPRECATED)
#undef GstXmlNodePtr
#define GstXmlNodePtr xmlNodePtr
158
#include <libxml/parser.h>
159 160 161 162 163 164 165 166 167
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)
168
static void gst_object_real_restore_thyself (GstObject * object,
169
    GstXmlNodePtr self);
170 171
#endif

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

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

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

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

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

188 189
  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
190

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  return object;
}

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

#ifdef DEBUG_REFCOUNT
336
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
337 338
      ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
#endif
339
  g_object_unref (object);
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 365
/**
 * 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))) {
366 367
    GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object,
        "unsetting floating flag");
368 369 370 371 372 373 374 375
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
    GST_OBJECT_UNLOCK (object);
  } else {
    GST_OBJECT_UNLOCK (object);
    gst_object_ref (object);
  }
}

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

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

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

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

433 434 435
  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));
436

437
#ifdef DEBUG_REFCOUNT
438
  GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
439
      *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
440
      *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
441
      newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
442
      newobj ? G_OBJECT (newobj)->ref_count : 0);
443
#endif
Wim Taymans's avatar
Wim Taymans committed
444

445 446 447 448
  if (newobj)
    g_object_ref (newobj);
  do {
    oldptr = *oldobj;
449
  } while (!G_ATOMIC_POINTER_COMPARE_AND_EXCHANGE (oldobj, oldptr, newobj));
450 451
  if (oldptr)
    g_object_unref (oldptr);
452 453
}

454 455
/* dispose is called when the object has to release all links
 * to other objects */
456
static void
457
gst_object_dispose (GObject * object)
458
{
459 460
  GstObject *parent;

461
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
462

463
  GST_OBJECT_LOCK (object);
464 465
  if ((parent = GST_OBJECT_PARENT (object)))
    goto have_parent;
466
  GST_OBJECT_PARENT (object) = NULL;
467
  GST_OBJECT_UNLOCK (object);
468

469
  parent_class->dispose (object);
470 471 472 473 474 475 476 477 478 479 480

  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);
481
    /* ref the object again to revive it in this error case */
482
    gst_object_ref (object);
483 484
    return;
  }
485 486
}

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

493
  GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
494

495 496
  g_signal_handlers_destroy (object);

497
  g_free (gstobject->name);
498
  g_mutex_free (gstobject->lock);
499

500
#ifndef GST_DISABLE_TRACE
501
  gst_alloc_trace_free (_gst_object_trace, object);
502 503
#endif

504
  parent_class->finalize (object);
505 506
}

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

Wim Taymans's avatar
Wim Taymans committed
529
  /* do the standard dispatching */
Wim Taymans's avatar
Wim Taymans committed
530
  parent_class->dispatch_properties_changed (object, n_pspecs, pspecs);
531 532

  gst_object = GST_OBJECT_CAST (object);
533 534 535 536 537 538 539
#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
540 541

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

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

552 553 554
    old_parent = parent;
    parent = gst_object_get_parent (old_parent);
    gst_object_unref (old_parent);
Wim Taymans's avatar
Wim Taymans committed
555
  }
556
#ifndef GST_DISABLE_GST_DEBUG
557
  g_free (name);
558
#endif
Wim Taymans's avatar
Wim Taymans committed
559 560
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
561
/**
Wim Taymans's avatar
Wim Taymans committed
562 563 564 565
 * 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.
566 567 568
 * @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
569
 *
570
 * A default deep_notify signal callback for an object. The user data
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
571 572
 * 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
573
 * using g_print.
574
 *
575 576
 * MT safe. This function grabs and releases @object's LOCK for getting its
 *          path string.
Wim Taymans's avatar
Wim Taymans committed
577 578
 */
void
579 580
gst_object_default_deep_notify (GObject * object, GstObject * orig,
    GParamSpec * pspec, gchar ** excluded_props)
Wim Taymans's avatar
Wim Taymans committed
581
{
582
  GValue value = { 0, };        /* the important thing is that value.type = 0 */
583
  gchar *str = NULL;
584
  gchar *name = NULL;
Wim Taymans's avatar
Wim Taymans committed
585 586 587 588 589

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

596
    /* FIXME: handle flags */
Wim Taymans's avatar
Wim Taymans committed
597 598
    if (G_IS_PARAM_SPEC_ENUM (pspec)) {
      GEnumValue *enum_value;
599
      GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
600

601
      enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
Wim Taymans's avatar
Wim Taymans committed
602 603

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

621
static gboolean
622
gst_object_set_name_default (GstObject * object)
623
{
624
  const gchar *type_name;
625
  gint count;
626
  gchar *name;
627
  GQuark q;
628
  guint i, l;
629

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
630 631
  /* to ensure guaranteed uniqueness across threads, only one thread
   * may ever assign a name */
632 633
  G_LOCK (object_name_mutex);

634
  if (!object_name_counts) {
635
    g_datalist_init (&object_name_counts);
636
  }
637

638 639 640
  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));
641

642 643
  G_UNLOCK (object_name_mutex);

644
  /* GstFooSink -> foosink<N> */
645
  type_name = g_quark_to_string (q);
646 647
  if (strncmp (type_name, "Gst", 3) == 0)
    type_name += 3;
648 649
  name = g_strdup_printf ("%s%d", type_name, count);
  l = strlen (name);
650
  for (i = 0; i < l; i++)
651
    name[i] = g_ascii_tolower (name[i]);
652

653 654 655
  GST_OBJECT_LOCK (object);
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;
656

657 658 659 660
  g_free (object->name);
  object->name = name;

  GST_OBJECT_UNLOCK (object);
661

662 663 664 665
  return TRUE;

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

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

694 695
  g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);

696
  GST_OBJECT_LOCK (object);
697

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

  if (name != NULL) {
    g_free (object->name);
704
    object->name = g_strdup (name);
705
    GST_OBJECT_UNLOCK (object);
706 707
    result = TRUE;
  } else {
708
    GST_OBJECT_UNLOCK (object);
709
    result = gst_object_set_name_default (object);
710
  }
711 712 713 714
  /* 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).
   */
715 716 717 718 719
  return result;

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

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

746 747
  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

748
  GST_OBJECT_LOCK (object);
749
  result = g_strdup (object->name);
750
  GST_OBJECT_UNLOCK (object);
751 752 753 754 755 756

  return result;
}

/**
 * gst_object_set_name_prefix:
757
 * @object:      a #GstObject
758
 * @name_prefix: new name prefix of @object
759
 *
760
 * Sets the name prefix of @object to @name_prefix.
761 762 763
 * This function makes a copy of the provided name prefix, so the caller
 * retains ownership of the name prefix it sent.
 *
764
 * MT safe.  This function grabs and releases @object's LOCK.
765 766 767
 *
 * Deprecated: deprecated because the name prefix has never actually been used
 *     for anything.
768
 */
769 770 771 772
#ifndef GST_REMOVE_DEPRECATED
#ifdef GST_DISABLE_DEPRECATED
void gst_object_set_name_prefix (GstObject * object, const gchar * name_prefix);
#endif
773 774 775 776 777
void
gst_object_set_name_prefix (GstObject * object, const gchar * name_prefix)
{
  g_return_if_fail (GST_IS_OBJECT (object));

778
  GST_OBJECT_LOCK (object);
779 780
  g_free (object->name_prefix);
  object->name_prefix = g_strdup (name_prefix); /* NULL gives NULL */
781
  GST_OBJECT_UNLOCK (object);
782
}
783
#endif /* GST_REMOVE_DEPRECATED */
784 785 786

/**