gstobject.c 34.2 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
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

137
#ifndef GST_DISABLE_LOADSAVE
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
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 209
          NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Wim Taymans's avatar
Wim Taymans committed
210

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

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

235
#ifndef GST_DISABLE_LOADSAVE
236 237
  /**
   * GstObject::object-saved:
238
   * @gstobject: a #GstObject
239 240
   * @xml_node: the xmlNodePtr of the parent node
   *
241
   * Trigered whenever a new object is saved to XML. You can connect to this
242 243 244 245 246
   * 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)
   */
247
  gst_object_signals[OBJECT_SAVED] =
248 249 250 251
      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);

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

  /**
   * GstObject::deep-notify:
257
   * @gstobject: a #GstObject
258 259 260 261 262 263 264
   * @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
265
  gst_object_signals[DEEP_NOTIFY] =
266 267 268
      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,
269
      NULL, gst_marshal_VOID__OBJECT_PARAM, G_TYPE_NONE, 2, GST_TYPE_OBJECT,
270
      G_TYPE_PARAM);
271 272

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

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

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

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

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

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

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

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

304 305
/**
 * gst_object_ref:
306
 * @object: a #GstObject to reference
307
 *
308
 * Increments the reference count on @object. This function
309
 * does not take the lock on @object because it relies on
310 311 312 313 314
 * 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
315
 *
316
 * Returns: A pointer to @object
317
 */
318 319
gpointer
gst_object_ref (gpointer object)
320
{
321
  g_return_val_if_fail (object != NULL, NULL);
322

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

  return object;
}

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

/**
 * gst_object_sink:
360 361
 * @object: a #GstObject to sink
 *
362
 * If @object was floating, the #GST_OBJECT_FLOATING flag is removed
363 364
 * and @object is unreffed. When @object was not floating,
 * this function does nothing.
365
 *
366 367
 * Any newly created object has a refcount of 1 and is floating.
 * This function should be used when creating a new object to
368 369 370 371
 * 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.
372
 *
373
 * MT safe. This function grabs and releases @object lock.
374 375
 */
void
376
gst_object_sink (gpointer object)
377 378 379
{
  g_return_if_fail (GST_IS_OBJECT (object));

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

382
  GST_OBJECT_LOCK (object);
383
  if (G_LIKELY (GST_OBJECT_IS_FLOATING (object))) {
384
    GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "clear floating flag");
385
    GST_OBJECT_FLAG_UNSET (object, GST_OBJECT_FLOATING);
386
    GST_OBJECT_UNLOCK (object);
387
    gst_object_unref (object);
388
  } else {
389
    GST_OBJECT_UNLOCK (object);
390 391 392
  }
}

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

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

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

    *oldobj = newobj;
  }
}

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

439
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "dispose");
440

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

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

  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);
459
    /* ref the object again to revive it in this error case */
460 461 462
    object = gst_object_ref (object);
    return;
  }
463 464
}

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

471
  GST_CAT_LOG_OBJECT (GST_CAT_REFCOUNTING, object, "finalize");
472

473 474
  g_signal_handlers_destroy (object);

475
  g_free (gstobject->name);
476
  g_mutex_free (gstobject->lock);
477

478
#ifndef GST_DISABLE_TRACE
479
  gst_alloc_trace_free (_gst_object_trace, object);
480 481
#endif

482
  parent_class->finalize (object);
483 484
}

485 486 487
/* 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
488
 * top-level bin to catch property-change notifications for all contained
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
489
 * elements.
490
 *
491 492
 * This function is not MT safe in glib < 2.8 so we need to lock it with a
 * classwide mutex in that case.
493 494 495
 *
 * MT safe.
 */
Wim Taymans's avatar
Wim Taymans committed
496
static void
497 498
gst_object_dispatch_properties_changed (GObject * object,
    guint n_pspecs, GParamSpec ** pspecs)
Wim Taymans's avatar
Wim Taymans committed
499
{
500
  GstObject *gst_object, *parent, *old_parent;
Wim Taymans's avatar
Wim Taymans committed
501
  guint i;
502 503 504 505 506 507 508
  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
509 510

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

  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
517 518

  /* now let the parent dispatch those, too */
519 520
  parent = gst_object_get_parent (gst_object);
  while (parent) {
Wim Taymans's avatar
Wim Taymans committed
521
    for (i = 0; i < n_pspecs; i++) {
522 523
      GST_LOG_OBJECT (parent, "deep notification from %s (%s)",
          debug_name, pspecs[i]->name);
524 525 526

      g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
          g_quark_from_string (pspecs[i]->name), GST_OBJECT_CAST (object),
527
          pspecs[i]);
Wim Taymans's avatar
Wim Taymans committed
528 529
    }

530 531 532
    old_parent = parent;
    parent = gst_object_get_parent (old_parent);
    gst_object_unref (old_parent);
Wim Taymans's avatar
Wim Taymans committed
533
  }
534
  g_free (name);
Wim Taymans's avatar
Wim Taymans committed
535 536
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
537
/**
Wim Taymans's avatar
Wim Taymans committed
538 539 540 541
 * 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
542 543
 * @excluded_props: a set of user-specified properties to exclude or
 *  NULL to show all changes.
Wim Taymans's avatar
Wim Taymans committed
544
 *
545
 * A default deep_notify signal callback for an object. The user data
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
546 547
 * 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
548
 * using g_print.
549
 *
550 551
 * MT safe. This function grabs and releases @object's LOCK for getting its
 *          path string.
Wim Taymans's avatar
Wim Taymans committed
552 553
 */
void
554 555
gst_object_default_deep_notify (GObject * object, GstObject * orig,
    GParamSpec * pspec, gchar ** excluded_props)
Wim Taymans's avatar
Wim Taymans committed
556
{
557
  GValue value = { 0, };        /* the important thing is that value.type = 0 */
558
  gchar *str = NULL;
559
  gchar *name = NULL;
Wim Taymans's avatar
Wim Taymans committed
560 561 562 563 564

  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)
565
        return;
Wim Taymans's avatar
Wim Taymans committed
566 567 568 569 570
      excluded_props++;
    }
    g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec));
    g_object_get_property (G_OBJECT (orig), pspec->name, &value);

571
    /* FIXME: handle flags */
Wim Taymans's avatar
Wim Taymans committed
572 573
    if (G_IS_PARAM_SPEC_ENUM (pspec)) {
      GEnumValue *enum_value;
574
      GEnumClass *klass = G_ENUM_CLASS (g_type_class_ref (pspec->value_type));
575

576
      enum_value = g_enum_get_value (klass, g_value_get_enum (&value));
Wim Taymans's avatar
Wim Taymans committed
577 578

      str = g_strdup_printf ("%s (%d)", enum_value->value_nick,
579
          enum_value->value);
580
      g_type_class_unref (klass);
581
    } else {
Wim Taymans's avatar
Wim Taymans committed
582 583
      str = g_strdup_value_contents (&value);
    }
584 585 586
    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
587 588 589
    g_free (str);
    g_value_unset (&value);
  } else {
590
    name = gst_object_get_path_string (orig);
591
    g_warning ("Parameter %s not readable in %s.", pspec->name, name);
592
    g_free (name);
Wim Taymans's avatar
Wim Taymans committed
593 594 595
  }
}

596
static gboolean
597
gst_object_set_name_default (GstObject * object)
598
{
599
  const gchar *type_name;
600
  gint count;
601
  gchar *name, *tmp;
602
  gboolean result;
603
  GQuark q;
604

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
605 606
  /* to ensure guaranteed uniqueness across threads, only one thread
   * may ever assign a name */
607 608
  G_LOCK (object_name_mutex);

609
  if (!object_name_counts) {
610
    g_datalist_init (&object_name_counts);
611
  }
612

613 614 615
  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));
616

617 618
  G_UNLOCK (object_name_mutex);

619
  /* GstFooSink -> foosinkN */
620
  type_name = g_quark_to_string (q);
621 622 623
  if (strncmp (type_name, "Gst", 3) == 0)
    type_name += 3;
  tmp = g_strdup_printf ("%s%d", type_name, count);
624
  name = g_ascii_strdown (tmp, -1);
625
  g_free (tmp);
626

627
  result = gst_object_set_name (object, name);
628
  g_free (name);
629 630

  return result;
631 632
}

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

654 655
  g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);

656
  GST_OBJECT_LOCK (object);
657

658 659 660 661 662 663
  /* parented objects cannot be renamed */
  if (G_UNLIKELY (object->parent != NULL))
    goto had_parent;

  if (name != NULL) {
    g_free (object->name);
664
    object->name = g_strdup (name);
665
    GST_OBJECT_UNLOCK (object);
666 667
    result = TRUE;
  } else {
668
    GST_OBJECT_UNLOCK (object);
669
    result = gst_object_set_name_default (object);
670 671 672 673 674 675
  }
  return result;

  /* error */
had_parent:
  {
Stefan Kost's avatar
Stefan Kost committed
676
    GST_WARNING ("parented objects can't be renamed");
677
    GST_OBJECT_UNLOCK (object);
678 679
    return FALSE;
  }
680 681 682 683
}

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

700 701
  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

702
  GST_OBJECT_LOCK (object);
703
  result = g_strdup (object->name);
704
  GST_OBJECT_UNLOCK (object);
705 706 707 708 709 710

  return result;
}

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

725
  GST_OBJECT_LOCK (object);
726 727
  g_free (object->name_prefix);
  object->name_prefix = g_strdup (name_prefix); /* NULL gives NULL */
728
  GST_OBJECT_UNLOCK (object);
729 730 731 732
}

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

  g_return_val_if_fail (GST_IS_OBJECT (object), NULL);

751
  GST_OBJECT_LOCK (object);
752
  result = g_strdup (object->name_prefix);
753
  GST_OBJECT_UNLOCK (object);
754 755

  return result;
Erik Walthinsen's avatar
Erik Walthinsen committed
756 757