• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 Wim Taymans <wim@fluendo.com>
5  *
6  * gstobject.c: Fundamental class used for all of GStreamer
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 /**
25  * SECTION:gstobject
26  * @title: GstObject
27  * @short_description: Base class for the GStreamer object hierarchy
28  *
29  * #GstObject provides a root for the object hierarchy tree filed in by the
30  * GStreamer library.  It is currently a thin wrapper on top of
31  * #GInitiallyUnowned. It is an abstract class that is not very usable on its own.
32  *
33  * #GstObject gives us basic refcounting, parenting functionality and locking.
34  * Most of the functions are just extended for special GStreamer needs and can be
35  * found under the same name in the base class of #GstObject which is #GObject
36  * (e.g. g_object_ref() becomes gst_object_ref()).
37  *
38  * Since #GstObject derives from #GInitiallyUnowned, it also inherits the
39  * floating reference. Be aware that functions such as gst_bin_add() and
40  * gst_element_add_pad() take ownership of the floating reference.
41  *
42  * In contrast to #GObject instances, #GstObject adds a name property. The functions
43  * gst_object_set_name() and gst_object_get_name() are used to set/get the name
44  * of the object.
45  *
46  * ## controlled properties
47  *
48  * Controlled properties offers a lightweight way to adjust gobject properties
49  * over stream-time. It works by using time-stamped value pairs that are queued
50  * for element-properties. At run-time the elements continuously pull value
51  * changes for the current stream-time.
52  *
53  * What needs to be changed in a #GstElement?
54  * Very little - it is just two steps to make a plugin controllable!
55  *
56  *   * mark gobject-properties paramspecs that make sense to be controlled,
57  *     by GST_PARAM_CONTROLLABLE.
58  *
59  *   * when processing data (get, chain, loop function) at the beginning call
60  *     gst_object_sync_values(element,timestamp).
61  *     This will make the controller update all GObject properties that are
62  *     under its control with the current values based on the timestamp.
63  *
64  * What needs to be done in applications? Again it's not a lot to change.
65  *
66  *   * create a #GstControlSource.
67  *     csource = gst_interpolation_control_source_new ();
68  *     g_object_set (csource, "mode", GST_INTERPOLATION_MODE_LINEAR, NULL);
69  *
70  *   * Attach the #GstControlSource on the controller to a property.
71  *     gst_object_add_control_binding (object, gst_direct_control_binding_new (object, "prop1", csource));
72  *
73  *   * Set the control values
74  *     gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,0 * GST_SECOND, value1);
75  *     gst_timed_value_control_source_set ((GstTimedValueControlSource *)csource,1 * GST_SECOND, value2);
76  *
77  *   * start your pipeline
78  */
79 
80 #include "gst_private.h"
81 #include "glib-compat-private.h"
82 
83 #include "gstobject.h"
84 #include "gstclock.h"
85 #include "gstcontrolbinding.h"
86 #include "gstcontrolsource.h"
87 #include "gstinfo.h"
88 #include "gstparamspecs.h"
89 #include "gstutils.h"
90 
91 #define DEBUG_REFCOUNT
92 
93 /* Object signals and args */
94 enum
95 {
96   DEEP_NOTIFY,
97   LAST_SIGNAL
98 };
99 
100 enum
101 {
102   PROP_0,
103   PROP_NAME,
104   PROP_PARENT,
105   PROP_LAST
106 };
107 
108 enum
109 {
110   SO_OBJECT_LOADED,
111   SO_LAST_SIGNAL
112 };
113 
114 /* maps type name quark => count */
115 static GData *object_name_counts = NULL;
116 
117 G_LOCK_DEFINE_STATIC (object_name_mutex);
118 
119 static void gst_object_set_property (GObject * object, guint prop_id,
120     const GValue * value, GParamSpec * pspec);
121 static void gst_object_get_property (GObject * object, guint prop_id,
122     GValue * value, GParamSpec * pspec);
123 
124 static void gst_object_dispatch_properties_changed (GObject * object,
125     guint n_pspecs, GParamSpec ** pspecs);
126 
127 static void gst_object_dispose (GObject * object);
128 static void gst_object_finalize (GObject * object);
129 
130 static gboolean gst_object_set_name_default (GstObject * object);
131 
132 static guint gst_object_signals[LAST_SIGNAL] = { 0 };
133 
134 static GParamSpec *properties[PROP_LAST];
135 
136 G_DEFINE_ABSTRACT_TYPE (GstObject, gst_object, G_TYPE_INITIALLY_UNOWNED);
137 
138 static void
gst_object_constructed(GObject * object)139 gst_object_constructed (GObject * object)
140 {
141   GST_TRACER_OBJECT_CREATED (GST_OBJECT_CAST (object));
142 
143   ((GObjectClass *) gst_object_parent_class)->constructed (object);
144 }
145 
146 static void
gst_object_class_init(GstObjectClass * klass)147 gst_object_class_init (GstObjectClass * klass)
148 {
149   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
150 
151   gobject_class->set_property = gst_object_set_property;
152   gobject_class->get_property = gst_object_get_property;
153 
154   properties[PROP_NAME] =
155       g_param_spec_string ("name", "Name", "The name of the object", NULL,
156       G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS |
157       GST_PARAM_DOC_SHOW_DEFAULT);
158 
159   /**
160    * GstObject:parent:
161    *
162    * The parent of the object. Please note, that when changing the 'parent'
163    * property, we don't emit #GObject::notify and #GstObject::deep-notify
164    * signals due to locking issues. In some cases one can use
165    * #GstBin::element-added or #GstBin::element-removed signals on the parent to
166    * achieve a similar effect.
167    */
168   properties[PROP_PARENT] =
169       g_param_spec_object ("parent", "Parent", "The parent of the object",
170       GST_TYPE_OBJECT,
171       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_DOC_SHOW_DEFAULT);
172 
173   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
174 
175   /**
176    * GstObject::deep-notify:
177    * @gstobject: a #GstObject
178    * @prop_object: the object that originated the signal
179    * @prop: the property that changed
180    *
181    * The deep notify signal is used to be notified of property changes. It is
182    * typically attached to the toplevel bin to receive notifications from all
183    * the elements contained in that bin.
184    */
185   gst_object_signals[DEEP_NOTIFY] =
186       g_signal_new ("deep-notify", G_TYPE_FROM_CLASS (klass),
187       G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED |
188       G_SIGNAL_NO_HOOKS, G_STRUCT_OFFSET (GstObjectClass, deep_notify), NULL,
189       NULL, NULL, G_TYPE_NONE, 2, GST_TYPE_OBJECT, G_TYPE_PARAM);
190 
191   klass->path_string_separator = "/";
192 
193   /* see the comments at gst_object_dispatch_properties_changed */
194   gobject_class->dispatch_properties_changed
195       = GST_DEBUG_FUNCPTR (gst_object_dispatch_properties_changed);
196 
197   gobject_class->constructed = gst_object_constructed;
198   gobject_class->dispose = gst_object_dispose;
199   gobject_class->finalize = gst_object_finalize;
200 }
201 
202 static void
gst_object_init(GstObject * object)203 gst_object_init (GstObject * object)
204 {
205   g_mutex_init (&object->lock);
206   object->parent = NULL;
207   object->name = NULL;
208   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p new", object);
209 
210   object->flags = 0;
211 
212   object->control_rate = 100 * GST_MSECOND;
213   object->last_sync = GST_CLOCK_TIME_NONE;
214 }
215 
216 /**
217  * gst_object_ref:
218  * @object: (type Gst.Object): a #GstObject to reference
219  *
220  * Increments the reference count on @object. This function
221  * does not take the lock on @object because it relies on
222  * atomic refcounting.
223  *
224  * This object returns the input parameter to ease writing
225  * constructs like :
226  *  result = gst_object_ref (object->parent);
227  *
228  * Returns: (transfer full) (type Gst.Object): A pointer to @object
229  */
230 gpointer
gst_object_ref(gpointer object)231 gst_object_ref (gpointer object)
232 {
233   g_return_val_if_fail (object != NULL, NULL);
234 
235   GST_TRACER_OBJECT_REFFED (object, ((GObject *) object)->ref_count + 1);
236 #ifdef DEBUG_REFCOUNT
237   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref %d->%d", object,
238       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count + 1);
239 #endif
240   g_object_ref (object);
241 
242   return object;
243 }
244 
245 /**
246  * gst_object_unref:
247  * @object: (type Gst.Object): a #GstObject to unreference
248  *
249  * Decrements the reference count on @object.  If reference count hits
250  * zero, destroy @object. This function does not take the lock
251  * on @object as it relies on atomic refcounting.
252  *
253  * The unref method should never be called with the LOCK held since
254  * this might deadlock the dispose function.
255  */
256 void
gst_object_unref(gpointer object)257 gst_object_unref (gpointer object)
258 {
259   g_return_if_fail (object != NULL);
260   g_return_if_fail (((GObject *) object)->ref_count > 0);
261 
262   GST_TRACER_OBJECT_UNREFFED (object, ((GObject *) object)->ref_count - 1);
263 #ifdef DEBUG_REFCOUNT
264   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p unref %d->%d", object,
265       ((GObject *) object)->ref_count, ((GObject *) object)->ref_count - 1);
266 #endif
267   g_object_unref (object);
268 }
269 
270 /**
271  * gst_object_ref_sink: (skip)
272  * @object: a #GstObject to sink
273  *
274  * Increase the reference count of @object, and possibly remove the floating
275  * reference, if @object has a floating reference.
276  *
277  * In other words, if the object is floating, then this call "assumes ownership"
278  * of the floating reference, converting it to a normal reference by clearing
279  * the floating flag while leaving the reference count unchanged. If the object
280  * is not floating, then this call adds a new normal reference increasing the
281  * reference count by one.
282  *
283  * For more background on "floating references" please see the #GObject
284  * documentation.
285  */
286 gpointer
gst_object_ref_sink(gpointer object)287 gst_object_ref_sink (gpointer object)
288 {
289   g_return_val_if_fail (object != NULL, NULL);
290 
291 #ifdef DEBUG_REFCOUNT
292   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p ref_sink %d->%d",
293       object, ((GObject *) object)->ref_count,
294       ((GObject *) object)->ref_count + 1);
295 #endif
296   return g_object_ref_sink (object);
297 }
298 
299 /**
300  * gst_clear_object: (skip)
301  * @object_ptr: a pointer to a #GstObject reference
302  *
303  * Clears a reference to a #GstObject.
304  *
305  * @object_ptr must not be %NULL.
306  *
307  * If the reference is %NULL then this function does nothing.
308  * Otherwise, the reference count of the object is decreased using
309  * gst_object_unref() and the pointer is set to %NULL.
310  *
311  * A macro is also included that allows this function to be used without
312  * pointer casts.
313  *
314  * Since: 1.16
315  **/
316 #undef gst_clear_object
317 void
gst_clear_object(GstObject ** object_ptr)318 gst_clear_object (GstObject ** object_ptr)
319 {
320   g_clear_pointer (object_ptr, gst_object_unref);
321 }
322 
323 /**
324  * gst_object_replace:
325  * @oldobj: (inout) (transfer full) (nullable): pointer to a place of
326  *     a #GstObject to replace
327  * @newobj: (transfer none) (allow-none): a new #GstObject
328  *
329  * Atomically modifies a pointer to point to a new object.
330  * The reference count of @oldobj is decreased and the reference count of
331  * @newobj is increased.
332  *
333  * Either @newobj and the value pointed to by @oldobj may be %NULL.
334  *
335  * Returns: %TRUE if @newobj was different from @oldobj
336  */
337 gboolean
gst_object_replace(GstObject ** oldobj,GstObject * newobj)338 gst_object_replace (GstObject ** oldobj, GstObject * newobj)
339 {
340   GstObject *oldptr;
341 
342   g_return_val_if_fail (oldobj != NULL, FALSE);
343 
344 #ifdef DEBUG_REFCOUNT
345   GST_CAT_TRACE (GST_CAT_REFCOUNTING, "replace %p %s (%d) with %p %s (%d)",
346       *oldobj, *oldobj ? GST_STR_NULL (GST_OBJECT_NAME (*oldobj)) : "(NONE)",
347       *oldobj ? G_OBJECT (*oldobj)->ref_count : 0,
348       newobj, newobj ? GST_STR_NULL (GST_OBJECT_NAME (newobj)) : "(NONE)",
349       newobj ? G_OBJECT (newobj)->ref_count : 0);
350 #endif
351 
352   oldptr = (GstObject *) g_atomic_pointer_get ((gpointer *) oldobj);
353 
354   if (G_UNLIKELY (oldptr == newobj))
355     return FALSE;
356 
357   if (newobj)
358     gst_object_ref (newobj);
359 
360   while (G_UNLIKELY (!g_atomic_pointer_compare_and_exchange ((gpointer *)
361               oldobj, (gpointer) oldptr, newobj))) {
362     oldptr = g_atomic_pointer_get ((gpointer *) oldobj);
363     if (G_UNLIKELY (oldptr == newobj))
364       break;
365   }
366 
367   if (oldptr)
368     gst_object_unref (oldptr);
369 
370   return oldptr != newobj;
371 }
372 
373 /* dispose is called when the object has to release all links
374  * to other objects */
375 static void
gst_object_dispose(GObject * object)376 gst_object_dispose (GObject * object)
377 {
378   GstObject *self = (GstObject *) object;
379   GstObject *parent;
380 
381   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p dispose", object);
382 
383   GST_OBJECT_LOCK (object);
384   if ((parent = GST_OBJECT_PARENT (object)))
385     goto have_parent;
386   GST_OBJECT_PARENT (object) = NULL;
387   GST_OBJECT_UNLOCK (object);
388 
389   if (self->control_bindings) {
390     GList *node;
391 
392     for (node = self->control_bindings; node; node = g_list_next (node)) {
393       gst_object_unparent (node->data);
394     }
395     g_list_free (self->control_bindings);
396     self->control_bindings = NULL;
397   }
398 
399   ((GObjectClass *) gst_object_parent_class)->dispose (object);
400 
401   return;
402 
403   /* ERRORS */
404 have_parent:
405   {
406     g_critical ("\nTrying to dispose object \"%s\", but it still has a "
407         "parent \"%s\".\nYou need to let the parent manage the "
408         "object instead of unreffing the object directly.\n",
409         GST_OBJECT_NAME (object), GST_OBJECT_NAME (parent));
410     GST_OBJECT_UNLOCK (object);
411     /* ref the object again to revive it in this error case */
412     gst_object_ref (object);
413     return;
414   }
415 }
416 
417 /* finalize is called when the object has to free its resources */
418 static void
gst_object_finalize(GObject * object)419 gst_object_finalize (GObject * object)
420 {
421   GstObject *gstobject = GST_OBJECT_CAST (object);
422 
423   GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "%p finalize", object);
424 
425   g_signal_handlers_destroy (object);
426 
427   g_free (gstobject->name);
428   g_mutex_clear (&gstobject->lock);
429 
430   GST_TRACER_OBJECT_DESTROYED (gstobject);
431 
432   ((GObjectClass *) gst_object_parent_class)->finalize (object);
433 }
434 
435 /* Changing a GObject property of a GstObject will result in "deep-notify"
436  * signals being emitted by the object itself, as well as in each parent
437  * object. This is so that an application can connect a listener to the
438  * top-level bin to catch property-change notifications for all contained
439  * elements.
440  *
441  * MT safe.
442  */
443 static void
gst_object_dispatch_properties_changed(GObject * object,guint n_pspecs,GParamSpec ** pspecs)444 gst_object_dispatch_properties_changed (GObject * object,
445     guint n_pspecs, GParamSpec ** pspecs)
446 {
447   GstObject *gst_object, *parent, *old_parent;
448   guint i;
449 #ifndef GST_DISABLE_GST_DEBUG
450   gchar *name = NULL;
451   const gchar *debug_name;
452 #endif
453 
454   /* do the standard dispatching */
455   ((GObjectClass *)
456       gst_object_parent_class)->dispatch_properties_changed (object, n_pspecs,
457       pspecs);
458 
459   gst_object = GST_OBJECT_CAST (object);
460 #ifndef GST_DISABLE_GST_DEBUG
461   if (G_UNLIKELY (_gst_debug_min >= GST_LEVEL_LOG)) {
462     name = gst_object_get_name (gst_object);
463     debug_name = GST_STR_NULL (name);
464   } else
465     debug_name = "";
466 #endif
467 
468   /* now let the parent dispatch those, too */
469   parent = gst_object_get_parent (gst_object);
470   while (parent) {
471     for (i = 0; i < n_pspecs; i++) {
472       GST_CAT_LOG_OBJECT (GST_CAT_PROPERTIES, parent,
473           "deep notification from %s (%s)", debug_name, pspecs[i]->name);
474 
475       g_signal_emit (parent, gst_object_signals[DEEP_NOTIFY],
476           g_quark_from_string (pspecs[i]->name), gst_object, pspecs[i]);
477     }
478 
479     old_parent = parent;
480     parent = gst_object_get_parent (old_parent);
481     gst_object_unref (old_parent);
482   }
483 #ifndef GST_DISABLE_GST_DEBUG
484   g_free (name);
485 #endif
486 }
487 
488 /**
489  * gst_object_default_deep_notify:
490  * @object: the #GObject that signalled the notify.
491  * @orig: a #GstObject that initiated the notify.
492  * @pspec: a #GParamSpec of the property.
493  * @excluded_props: (array zero-terminated=1) (element-type gchar*) (allow-none):
494  *     a set of user-specified properties to exclude or %NULL to show
495  *     all changes.
496  *
497  * A default deep_notify signal callback for an object. The user data
498  * should contain a pointer to an array of strings that should be excluded
499  * from the notify. The default handler will print the new value of the property
500  * using g_print.
501  *
502  * MT safe. This function grabs and releases @object's LOCK for getting its
503  *          path string.
504  */
505 void
gst_object_default_deep_notify(GObject * object,GstObject * orig,GParamSpec * pspec,gchar ** excluded_props)506 gst_object_default_deep_notify (GObject * object, GstObject * orig,
507     GParamSpec * pspec, gchar ** excluded_props)
508 {
509   GValue value = { 0, };        /* the important thing is that value.type = 0 */
510   gchar *str = NULL;
511   gchar *name = NULL;
512 
513   if (pspec->flags & G_PARAM_READABLE) {
514     /* let's not print these out for excluded properties... */
515     while (excluded_props != NULL && *excluded_props != NULL) {
516       if (strcmp (pspec->name, *excluded_props) == 0)
517         return;
518       excluded_props++;
519     }
520     g_value_init (&value, pspec->value_type);
521     g_object_get_property (G_OBJECT (orig), pspec->name, &value);
522 
523     if (G_VALUE_HOLDS_STRING (&value))
524       str = g_value_dup_string (&value);
525     else
526       str = gst_value_serialize (&value);
527     name = gst_object_get_path_string (orig);
528     g_print ("%s: %s = %s\n", name, pspec->name, str);
529     g_free (name);
530     g_free (str);
531     g_value_unset (&value);
532   } else {
533     name = gst_object_get_path_string (orig);
534     g_warning ("Parameter %s not readable in %s.", pspec->name, name);
535     g_free (name);
536   }
537 }
538 
539 static gboolean
gst_object_set_name_default(GstObject * object)540 gst_object_set_name_default (GstObject * object)
541 {
542   const gchar *type_name;
543   gint count;
544   gchar *name;
545   GQuark q;
546   guint i, l;
547 
548   /* to ensure guaranteed uniqueness across threads, only one thread
549    * may ever assign a name */
550   G_LOCK (object_name_mutex);
551 
552   if (!object_name_counts) {
553     g_datalist_init (&object_name_counts);
554   }
555 
556   q = g_type_qname (G_OBJECT_TYPE (object));
557   count = GPOINTER_TO_INT (g_datalist_id_get_data (&object_name_counts, q));
558   g_datalist_id_set_data (&object_name_counts, q, GINT_TO_POINTER (count + 1));
559 
560   G_UNLOCK (object_name_mutex);
561 
562   /* GstFooSink -> foosink<N> */
563   type_name = g_quark_to_string (q);
564   if (strncmp (type_name, "Gst", 3) == 0)
565     type_name += 3;
566   /* give the 20th "queue" element and the first "queue2" different names */
567   l = strlen (type_name);
568   if (l > 0 && g_ascii_isdigit (type_name[l - 1])) {
569     name = g_strdup_printf ("%s-%d", type_name, count);
570   } else {
571     name = g_strdup_printf ("%s%d", type_name, count);
572   }
573 
574   l = strlen (name);
575   for (i = 0; i < l; i++)
576     name[i] = g_ascii_tolower (name[i]);
577 
578   GST_OBJECT_LOCK (object);
579   if (G_UNLIKELY (object->parent != NULL))
580     goto had_parent;
581 
582   g_free (object->name);
583   object->name = name;
584 
585   GST_OBJECT_UNLOCK (object);
586 
587   return TRUE;
588 
589 had_parent:
590   {
591     g_free (name);
592     GST_WARNING ("parented objects can't be renamed");
593     GST_OBJECT_UNLOCK (object);
594     return FALSE;
595   }
596 }
597 
598 static gboolean
gst_object_set_name_intern(GstObject * object,const gchar * name)599 gst_object_set_name_intern (GstObject * object, const gchar * name)
600 {
601   gboolean result;
602 
603   GST_OBJECT_LOCK (object);
604 
605   /* parented objects cannot be renamed */
606   if (G_UNLIKELY (object->parent != NULL))
607     goto had_parent;
608 
609   if (name != NULL) {
610     g_free (object->name);
611     object->name = g_strdup (name);
612     GST_OBJECT_UNLOCK (object);
613     result = TRUE;
614   } else {
615     GST_OBJECT_UNLOCK (object);
616     result = gst_object_set_name_default (object);
617   }
618 
619   return result;
620 
621   /* error */
622 had_parent:
623   {
624     GST_WARNING ("parented objects can't be renamed");
625     GST_OBJECT_UNLOCK (object);
626     return FALSE;
627   }
628 }
629 
630 /**
631  * gst_object_set_name:
632  * @object: a #GstObject
633  * @name: (allow-none): new name of object
634  *
635  * Sets the name of @object, or gives @object a guaranteed unique
636  * name (if @name is %NULL).
637  * This function makes a copy of the provided name, so the caller
638  * retains ownership of the name it sent.
639  *
640  * Returns: %TRUE if the name could be set. Since Objects that have
641  * a parent cannot be renamed, this function returns %FALSE in those
642  * cases.
643  *
644  * MT safe.  This function grabs and releases @object's LOCK.
645  */
646 gboolean
gst_object_set_name(GstObject * object,const gchar * name)647 gst_object_set_name (GstObject * object, const gchar * name)
648 {
649   gboolean result;
650 
651   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
652 
653   if ((result = gst_object_set_name_intern (object, name)))
654     g_object_notify_by_pspec (G_OBJECT (object), properties[PROP_NAME]);
655   return result;
656 }
657 
658 /**
659  * gst_object_get_name:
660  * @object: a #GstObject
661  *
662  * Returns a copy of the name of @object.
663  * Caller should g_free() the return value after usage.
664  * For a nameless object, this returns %NULL, which you can safely g_free()
665  * as well.
666  *
667  * Free-function: g_free
668  *
669  * Returns: (transfer full) (nullable): the name of @object. g_free()
670  * after usage.
671  *
672  * MT safe. This function grabs and releases @object's LOCK.
673  */
674 gchar *
gst_object_get_name(GstObject * object)675 gst_object_get_name (GstObject * object)
676 {
677   gchar *result = NULL;
678 
679   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
680 
681   GST_OBJECT_LOCK (object);
682   result = g_strdup (object->name);
683   GST_OBJECT_UNLOCK (object);
684 
685   return result;
686 }
687 
688 /**
689  * gst_object_set_parent:
690  * @object: (transfer floating): a #GstObject
691  * @parent: new parent of object
692  *
693  * Sets the parent of @object to @parent. The object's reference count will
694  * be incremented, and any floating reference will be removed (see gst_object_ref_sink()).
695  *
696  * Returns: %TRUE if @parent could be set or %FALSE when @object
697  * already had a parent or @object and @parent are the same.
698  *
699  * MT safe. Grabs and releases @object's LOCK.
700  */
701 gboolean
gst_object_set_parent(GstObject * object,GstObject * parent)702 gst_object_set_parent (GstObject * object, GstObject * parent)
703 {
704   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
705   g_return_val_if_fail (GST_IS_OBJECT (parent), FALSE);
706   g_return_val_if_fail (object != parent, FALSE);
707 
708   GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
709       "set parent (ref and sink)");
710 
711   GST_OBJECT_LOCK (object);
712   if (G_UNLIKELY (object->parent != NULL))
713     goto had_parent;
714 
715   object->parent = parent;
716   gst_object_ref_sink (object);
717   GST_OBJECT_UNLOCK (object);
718 
719   /* FIXME-2.0: this does not work, the deep notify takes the lock from the
720    * parent object and deadlocks when the parent holds its lock when calling
721    * this function (like _element_add_pad()), we need to use a GRecMutex
722    * for locking the parent instead.
723    */
724   /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
725 
726   return TRUE;
727 
728   /* ERROR handling */
729 had_parent:
730   {
731     GST_CAT_DEBUG_OBJECT (GST_CAT_REFCOUNTING, object,
732         "set parent failed, object already had a parent");
733     gst_object_ref_sink (object);
734     gst_object_unref (object);
735     GST_OBJECT_UNLOCK (object);
736     return FALSE;
737   }
738 }
739 
740 /**
741  * gst_object_get_parent:
742  * @object: a #GstObject
743  *
744  * Returns the parent of @object. This function increases the refcount
745  * of the parent object so you should gst_object_unref() it after usage.
746  *
747  * Returns: (transfer full) (nullable): parent of @object, this can be
748  *   %NULL if @object has no parent. unref after usage.
749  *
750  * MT safe. Grabs and releases @object's LOCK.
751  */
752 GstObject *
gst_object_get_parent(GstObject * object)753 gst_object_get_parent (GstObject * object)
754 {
755   GstObject *result = NULL;
756 
757   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
758 
759   GST_OBJECT_LOCK (object);
760   result = object->parent;
761   if (G_LIKELY (result))
762     gst_object_ref (result);
763   GST_OBJECT_UNLOCK (object);
764 
765   return result;
766 }
767 
768 /**
769  * gst_object_unparent:
770  * @object: a #GstObject to unparent
771  *
772  * Clear the parent of @object, removing the associated reference.
773  * This function decreases the refcount of @object.
774  *
775  * MT safe. Grabs and releases @object's lock.
776  */
777 void
gst_object_unparent(GstObject * object)778 gst_object_unparent (GstObject * object)
779 {
780   GstObject *parent;
781 
782   g_return_if_fail (GST_IS_OBJECT (object));
783 
784   GST_OBJECT_LOCK (object);
785   parent = object->parent;
786 
787   if (G_LIKELY (parent != NULL)) {
788     GST_CAT_TRACE_OBJECT (GST_CAT_REFCOUNTING, object, "unparent");
789     object->parent = NULL;
790     GST_OBJECT_UNLOCK (object);
791 
792     /* g_object_notify_by_pspec ((GObject *)object, properties[PROP_PARENT]); */
793 
794     gst_object_unref (object);
795   } else {
796     GST_OBJECT_UNLOCK (object);
797   }
798 }
799 
800 /**
801  * gst_object_has_as_parent:
802  * @object: a #GstObject to check
803  * @parent: a #GstObject to check as parent
804  *
805  * Check if @parent is the parent of @object.
806  * E.g. a #GstElement can check if it owns a given #GstPad.
807  *
808  * Returns: %FALSE if either @object or @parent is %NULL. %TRUE if @parent is
809  *          the parent of @object. Otherwise %FALSE.
810  *
811  * MT safe. Grabs and releases @object's locks.
812  * Since: 1.6
813  */
814 gboolean
gst_object_has_as_parent(GstObject * object,GstObject * parent)815 gst_object_has_as_parent (GstObject * object, GstObject * parent)
816 {
817   gboolean result = FALSE;
818 
819   if (G_LIKELY (GST_IS_OBJECT (object) && GST_IS_OBJECT (parent))) {
820     GST_OBJECT_LOCK (object);
821     result = GST_OBJECT_PARENT (object) == parent;
822     GST_OBJECT_UNLOCK (object);
823   }
824 
825   return result;
826 }
827 
828 /**
829  * gst_object_has_as_ancestor:
830  * @object: a #GstObject to check
831  * @ancestor: a #GstObject to check as ancestor
832  *
833  * Check if @object has an ancestor @ancestor somewhere up in
834  * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
835  *
836  * Returns: %TRUE if @ancestor is an ancestor of @object.
837  *
838  * MT safe. Grabs and releases @object's locks.
839  */
840 gboolean
gst_object_has_as_ancestor(GstObject * object,GstObject * ancestor)841 gst_object_has_as_ancestor (GstObject * object, GstObject * ancestor)
842 {
843   GstObject *parent, *tmp;
844 
845   if (!ancestor || !object)
846     return FALSE;
847 
848   parent = gst_object_ref (object);
849   do {
850     if (parent == ancestor) {
851       gst_object_unref (parent);
852       return TRUE;
853     }
854 
855     tmp = gst_object_get_parent (parent);
856     gst_object_unref (parent);
857     parent = tmp;
858   } while (parent);
859 
860   return FALSE;
861 }
862 
863 /**
864  * gst_object_has_ancestor:
865  * @object: a #GstObject to check
866  * @ancestor: a #GstObject to check as ancestor
867  *
868  * Check if @object has an ancestor @ancestor somewhere up in
869  * the hierarchy. One can e.g. check if a #GstElement is inside a #GstPipeline.
870  *
871  * Returns: %TRUE if @ancestor is an ancestor of @object.
872  *
873  * Deprecated: Use gst_object_has_as_ancestor() instead.
874  *
875  * MT safe. Grabs and releases @object's locks.
876  */
877 #ifndef GST_REMOVE_DEPRECATED
878 gboolean
gst_object_has_ancestor(GstObject * object,GstObject * ancestor)879 gst_object_has_ancestor (GstObject * object, GstObject * ancestor)
880 {
881   return gst_object_has_as_ancestor (object, ancestor);
882 }
883 #endif
884 
885 /**
886  * gst_object_check_uniqueness:
887  * @list: (transfer none) (element-type Gst.Object): a list of #GstObject to
888  *      check through
889  * @name: the name to search for
890  *
891  * Checks to see if there is any object named @name in @list. This function
892  * does not do any locking of any kind. You might want to protect the
893  * provided list with the lock of the owner of the list. This function
894  * will lock each #GstObject in the list to compare the name, so be
895  * careful when passing a list with a locked object.
896  *
897  * Returns: %TRUE if a #GstObject named @name does not appear in @list,
898  * %FALSE if it does.
899  *
900  * MT safe. Grabs and releases the LOCK of each object in the list.
901  */
902 gboolean
gst_object_check_uniqueness(GList * list,const gchar * name)903 gst_object_check_uniqueness (GList * list, const gchar * name)
904 {
905   gboolean result = TRUE;
906 
907   g_return_val_if_fail (name != NULL, FALSE);
908 
909   for (; list; list = g_list_next (list)) {
910     GstObject *child;
911     gboolean eq;
912 
913     child = GST_OBJECT_CAST (list->data);
914 
915     GST_OBJECT_LOCK (child);
916     eq = strcmp (GST_OBJECT_NAME (child), name) == 0;
917     GST_OBJECT_UNLOCK (child);
918 
919     if (G_UNLIKELY (eq)) {
920       result = FALSE;
921       break;
922     }
923   }
924   return result;
925 }
926 
927 
928 static void
gst_object_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)929 gst_object_set_property (GObject * object, guint prop_id,
930     const GValue * value, GParamSpec * pspec)
931 {
932   GstObject *gstobject;
933 
934   gstobject = GST_OBJECT_CAST (object);
935 
936   switch (prop_id) {
937     case PROP_NAME:
938       gst_object_set_name_intern (gstobject, g_value_get_string (value));
939       break;
940     case PROP_PARENT:
941       gst_object_set_parent (gstobject, g_value_get_object (value));
942       break;
943     default:
944       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
945       break;
946   }
947 }
948 
949 static void
gst_object_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)950 gst_object_get_property (GObject * object, guint prop_id,
951     GValue * value, GParamSpec * pspec)
952 {
953   GstObject *gstobject;
954 
955   gstobject = GST_OBJECT_CAST (object);
956 
957   switch (prop_id) {
958     case PROP_NAME:
959       g_value_take_string (value, gst_object_get_name (gstobject));
960       break;
961     case PROP_PARENT:
962       g_value_take_object (value, gst_object_get_parent (gstobject));
963       break;
964     default:
965       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
966       break;
967   }
968 }
969 
970 /**
971  * gst_object_get_path_string:
972  * @object: a #GstObject
973  *
974  * Generates a string describing the path of @object in
975  * the object hierarchy. Only useful (or used) for debugging.
976  *
977  * Free-function: g_free
978  *
979  * Returns: (transfer full): a string describing the path of @object. You must
980  *          g_free() the string after usage.
981  *
982  * MT safe. Grabs and releases the #GstObject's LOCK for all objects
983  *          in the hierarchy.
984  */
985 gchar *
gst_object_get_path_string(GstObject * object)986 gst_object_get_path_string (GstObject * object)
987 {
988   GSList *parentage;
989   GSList *parents;
990   void *parent;
991   gchar *prevpath, *path;
992   const gchar *typename;
993   gchar *component;
994   const gchar *separator;
995 
996   /* ref object before adding to list */
997   gst_object_ref (object);
998   parentage = g_slist_prepend (NULL, object);
999 
1000   path = g_strdup ("");
1001 
1002   /* first walk the object hierarchy to build a list of the parents,
1003    * be careful here with refcounting. */
1004   do {
1005     if (GST_IS_OBJECT (object)) {
1006       parent = gst_object_get_parent (object);
1007       /* add parents to list, refcount remains increased while
1008        * we handle the object */
1009       if (parent)
1010         parentage = g_slist_prepend (parentage, parent);
1011     } else {
1012       break;
1013     }
1014     object = parent;
1015   } while (object != NULL);
1016 
1017   /* then walk the parent list and print them out. we need to
1018    * decrease the refcounting on each element after we handled
1019    * it. */
1020   for (parents = parentage; parents; parents = g_slist_next (parents)) {
1021     if (G_IS_OBJECT (parents->data)) {
1022       typename = G_OBJECT_TYPE_NAME (parents->data);
1023     } else {
1024       typename = NULL;
1025     }
1026     if (GST_IS_OBJECT (parents->data)) {
1027       GstObject *item = GST_OBJECT_CAST (parents->data);
1028       GstObjectClass *oclass = GST_OBJECT_GET_CLASS (item);
1029       gchar *objname = gst_object_get_name (item);
1030 
1031       component = g_strdup_printf ("%s:%s", typename, objname);
1032       separator = oclass->path_string_separator;
1033       /* and unref now */
1034       gst_object_unref (item);
1035       g_free (objname);
1036     } else {
1037       if (typename) {
1038         component = g_strdup_printf ("%s:%p", typename, parents->data);
1039       } else {
1040         component = g_strdup_printf ("%p", parents->data);
1041       }
1042       separator = "/";
1043     }
1044 
1045     prevpath = path;
1046     path = g_strjoin (separator, prevpath, component, NULL);
1047     g_free (prevpath);
1048     g_free (component);
1049   }
1050 
1051   g_slist_free (parentage);
1052 
1053   return path;
1054 }
1055 
1056 /* controller helper functions */
1057 
1058 /*
1059  * gst_object_find_control_binding:
1060  * @self: the gobject to search for a property in
1061  * @name: the gobject property name to look for
1062  *
1063  * Searches the list of properties under control.
1064  *
1065  * Returns: (nullable): a #GstControlBinding or %NULL if the property
1066  * is not being controlled.
1067  */
1068 static GstControlBinding *
gst_object_find_control_binding(GstObject * self,const gchar * name)1069 gst_object_find_control_binding (GstObject * self, const gchar * name)
1070 {
1071   GstControlBinding *binding;
1072   GList *node;
1073 
1074   for (node = self->control_bindings; node; node = g_list_next (node)) {
1075     binding = node->data;
1076     /* FIXME: eventually use GQuark to speed it up */
1077     if (!strcmp (binding->name, name)) {
1078       GST_DEBUG_OBJECT (self, "found control binding for property '%s'", name);
1079       return binding;
1080     }
1081   }
1082   GST_DEBUG_OBJECT (self, "controller does not manage property '%s'", name);
1083 
1084   return NULL;
1085 }
1086 
1087 /* controller functions */
1088 
1089 /**
1090  * gst_object_suggest_next_sync:
1091  * @object: the object that has controlled properties
1092  *
1093  * Returns a suggestion for timestamps where buffers should be split
1094  * to get best controller results.
1095  *
1096  * Returns: Returns the suggested timestamp or %GST_CLOCK_TIME_NONE
1097  * if no control-rate was set.
1098  */
1099 GstClockTime
gst_object_suggest_next_sync(GstObject * object)1100 gst_object_suggest_next_sync (GstObject * object)
1101 {
1102   GstClockTime ret;
1103 
1104   g_return_val_if_fail (GST_IS_OBJECT (object), GST_CLOCK_TIME_NONE);
1105   g_return_val_if_fail (object->control_rate != GST_CLOCK_TIME_NONE,
1106       GST_CLOCK_TIME_NONE);
1107 
1108   GST_OBJECT_LOCK (object);
1109 
1110   /* TODO: Implement more logic, depending on interpolation mode and control
1111    * points
1112    * FIXME: we need playback direction
1113    */
1114   ret = object->last_sync + object->control_rate;
1115 
1116   GST_OBJECT_UNLOCK (object);
1117 
1118   return ret;
1119 }
1120 
1121 /**
1122  * gst_object_sync_values:
1123  * @object: the object that has controlled properties
1124  * @timestamp: the time that should be processed
1125  *
1126  * Sets the properties of the object, according to the #GstControlSources that
1127  * (maybe) handle them and for the given timestamp.
1128  *
1129  * If this function fails, it is most likely the application developers fault.
1130  * Most probably the control sources are not setup correctly.
1131  *
1132  * Returns: %TRUE if the controller values could be applied to the object
1133  * properties, %FALSE otherwise
1134  */
1135 gboolean
gst_object_sync_values(GstObject * object,GstClockTime timestamp)1136 gst_object_sync_values (GstObject * object, GstClockTime timestamp)
1137 {
1138   GList *node;
1139   gboolean ret = TRUE;
1140 
1141   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1142   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1143 
1144   GST_LOG_OBJECT (object, "sync_values");
1145   if (!object->control_bindings)
1146     return TRUE;
1147 
1148   /* FIXME: this deadlocks */
1149   /* GST_OBJECT_LOCK (object); */
1150   g_object_freeze_notify ((GObject *) object);
1151   for (node = object->control_bindings; node; node = g_list_next (node)) {
1152     ret &= gst_control_binding_sync_values ((GstControlBinding *) node->data,
1153         object, timestamp, object->last_sync);
1154   }
1155   object->last_sync = timestamp;
1156   g_object_thaw_notify ((GObject *) object);
1157   /* GST_OBJECT_UNLOCK (object); */
1158 
1159   return ret;
1160 }
1161 
1162 
1163 /**
1164  * gst_object_has_active_control_bindings:
1165  * @object: the object that has controlled properties
1166  *
1167  * Check if the @object has active controlled properties.
1168  *
1169  * Returns: %TRUE if the object has active controlled properties
1170  */
1171 gboolean
gst_object_has_active_control_bindings(GstObject * object)1172 gst_object_has_active_control_bindings (GstObject * object)
1173 {
1174   gboolean res = FALSE;
1175   GList *node;
1176 
1177   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1178 
1179   GST_OBJECT_LOCK (object);
1180   for (node = object->control_bindings; node; node = g_list_next (node)) {
1181     res |= !gst_control_binding_is_disabled ((GstControlBinding *) node->data);
1182   }
1183   GST_OBJECT_UNLOCK (object);
1184   return res;
1185 }
1186 
1187 /**
1188  * gst_object_set_control_bindings_disabled:
1189  * @object: the object that has controlled properties
1190  * @disabled: boolean that specifies whether to disable the controller
1191  * or not.
1192  *
1193  * This function is used to disable all controlled properties of the @object for
1194  * some time, i.e. gst_object_sync_values() will do nothing.
1195  */
1196 void
gst_object_set_control_bindings_disabled(GstObject * object,gboolean disabled)1197 gst_object_set_control_bindings_disabled (GstObject * object, gboolean disabled)
1198 {
1199   GList *node;
1200 
1201   g_return_if_fail (GST_IS_OBJECT (object));
1202 
1203   GST_OBJECT_LOCK (object);
1204   for (node = object->control_bindings; node; node = g_list_next (node)) {
1205     gst_control_binding_set_disabled ((GstControlBinding *) node->data,
1206         disabled);
1207   }
1208   GST_OBJECT_UNLOCK (object);
1209 }
1210 
1211 /**
1212  * gst_object_set_control_binding_disabled:
1213  * @object: the object that has controlled properties
1214  * @property_name: property to disable
1215  * @disabled: boolean that specifies whether to disable the controller
1216  * or not.
1217  *
1218  * This function is used to disable the control bindings on a property for
1219  * some time, i.e. gst_object_sync_values() will do nothing for the
1220  * property.
1221  */
1222 void
gst_object_set_control_binding_disabled(GstObject * object,const gchar * property_name,gboolean disabled)1223 gst_object_set_control_binding_disabled (GstObject * object,
1224     const gchar * property_name, gboolean disabled)
1225 {
1226   GstControlBinding *binding;
1227 
1228   g_return_if_fail (GST_IS_OBJECT (object));
1229   g_return_if_fail (property_name);
1230 
1231   GST_OBJECT_LOCK (object);
1232   if ((binding = gst_object_find_control_binding (object, property_name))) {
1233     gst_control_binding_set_disabled (binding, disabled);
1234   }
1235   GST_OBJECT_UNLOCK (object);
1236 }
1237 
1238 
1239 /**
1240  * gst_object_add_control_binding:
1241  * @object: the controller object
1242  * @binding: (transfer floating): the #GstControlBinding that should be used
1243  *
1244  * Attach the #GstControlBinding to the object. If there already was a
1245  * #GstControlBinding for this property it will be replaced.
1246  *
1247  * The object's reference count will be incremented, and any floating
1248  * reference will be removed (see gst_object_ref_sink())
1249  *
1250  * Returns: %FALSE if the given @binding has not been setup for this object or
1251  * has been setup for a non suitable property, %TRUE otherwise.
1252  */
1253 gboolean
gst_object_add_control_binding(GstObject * object,GstControlBinding * binding)1254 gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
1255 {
1256   GstControlBinding *old;
1257 
1258   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1259   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1260   g_return_val_if_fail (binding->pspec, FALSE);
1261 
1262   GST_OBJECT_LOCK (object);
1263   if ((old = gst_object_find_control_binding (object, binding->name))) {
1264     GST_DEBUG_OBJECT (object, "controlled property %s removed", old->name);
1265     object->control_bindings = g_list_remove (object->control_bindings, old);
1266     gst_object_unparent (GST_OBJECT_CAST (old));
1267   }
1268   object->control_bindings = g_list_prepend (object->control_bindings, binding);
1269   gst_object_set_parent (GST_OBJECT_CAST (binding), object);
1270   GST_DEBUG_OBJECT (object, "controlled property %s added", binding->name);
1271   GST_OBJECT_UNLOCK (object);
1272 
1273   return TRUE;
1274 }
1275 
1276 /**
1277  * gst_object_get_control_binding:
1278  * @object: the object
1279  * @property_name: name of the property
1280  *
1281  * Gets the corresponding #GstControlBinding for the property. This should be
1282  * unreferenced again after use.
1283  *
1284  * Returns: (transfer full) (nullable): the #GstControlBinding for
1285  * @property_name or %NULL if the property is not controlled.
1286  */
1287 GstControlBinding *
gst_object_get_control_binding(GstObject * object,const gchar * property_name)1288 gst_object_get_control_binding (GstObject * object, const gchar * property_name)
1289 {
1290   GstControlBinding *binding;
1291 
1292   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1293   g_return_val_if_fail (property_name, NULL);
1294 
1295   GST_OBJECT_LOCK (object);
1296   if ((binding = gst_object_find_control_binding (object, property_name))) {
1297     gst_object_ref (binding);
1298   }
1299   GST_OBJECT_UNLOCK (object);
1300 
1301   return binding;
1302 }
1303 
1304 /**
1305  * gst_object_remove_control_binding:
1306  * @object: the object
1307  * @binding: the binding
1308  *
1309  * Removes the corresponding #GstControlBinding. If it was the
1310  * last ref of the binding, it will be disposed.
1311  *
1312  * Returns: %TRUE if the binding could be removed.
1313  */
1314 gboolean
gst_object_remove_control_binding(GstObject * object,GstControlBinding * binding)1315 gst_object_remove_control_binding (GstObject * object,
1316     GstControlBinding * binding)
1317 {
1318   GList *node;
1319   gboolean ret = FALSE;
1320 
1321   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1322   g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
1323 
1324   GST_OBJECT_LOCK (object);
1325   if ((node = g_list_find (object->control_bindings, binding))) {
1326     GST_DEBUG_OBJECT (object, "controlled property %s removed", binding->name);
1327     object->control_bindings =
1328         g_list_delete_link (object->control_bindings, node);
1329     gst_object_unparent (GST_OBJECT_CAST (binding));
1330     ret = TRUE;
1331   }
1332   GST_OBJECT_UNLOCK (object);
1333 
1334   return ret;
1335 }
1336 
1337 /**
1338  * gst_object_get_value:
1339  * @object: the object that has controlled properties
1340  * @property_name: the name of the property to get
1341  * @timestamp: the time the control-change should be read from
1342  *
1343  * Gets the value for the given controlled property at the requested time.
1344  *
1345  * Returns: (nullable): the GValue of the property at the given time,
1346  * or %NULL if the property isn't controlled.
1347  */
1348 GValue *
gst_object_get_value(GstObject * object,const gchar * property_name,GstClockTime timestamp)1349 gst_object_get_value (GstObject * object, const gchar * property_name,
1350     GstClockTime timestamp)
1351 {
1352   GstControlBinding *binding;
1353   GValue *val = NULL;
1354 
1355   g_return_val_if_fail (GST_IS_OBJECT (object), NULL);
1356   g_return_val_if_fail (property_name, NULL);
1357   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
1358 
1359   GST_OBJECT_LOCK (object);
1360   if ((binding = gst_object_find_control_binding (object, property_name))) {
1361     val = gst_control_binding_get_value (binding, timestamp);
1362   }
1363   GST_OBJECT_UNLOCK (object);
1364 
1365   return val;
1366 }
1367 
1368 /**
1369  * gst_object_get_value_array: (skip)
1370  * @object: the object that has controlled properties
1371  * @property_name: the name of the property to get
1372  * @timestamp: the time that should be processed
1373  * @interval: the time spacing between subsequent values
1374  * @n_values: the number of values
1375  * @values: array to put control-values in
1376  *
1377  * Gets a number of values for the given controlled property starting at the
1378  * requested time. The array @values need to hold enough space for @n_values of
1379  * the same type as the objects property's type.
1380  *
1381  * This function is useful if one wants to e.g. draw a graph of the control
1382  * curve or apply a control curve sample by sample.
1383  *
1384  * The values are unboxed and ready to be used. The similar function
1385  * gst_object_get_g_value_array() returns the array as #GValues and is
1386  * better suites for bindings.
1387  *
1388  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1389  */
1390 gboolean
gst_object_get_value_array(GstObject * object,const gchar * property_name,GstClockTime timestamp,GstClockTime interval,guint n_values,gpointer values)1391 gst_object_get_value_array (GstObject * object, const gchar * property_name,
1392     GstClockTime timestamp, GstClockTime interval, guint n_values,
1393     gpointer values)
1394 {
1395   gboolean res = FALSE;
1396   GstControlBinding *binding;
1397 
1398   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1399   g_return_val_if_fail (property_name, FALSE);
1400   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1401   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1402   g_return_val_if_fail (values, FALSE);
1403 
1404   GST_OBJECT_LOCK (object);
1405   if ((binding = gst_object_find_control_binding (object, property_name))) {
1406     res = gst_control_binding_get_value_array (binding, timestamp, interval,
1407         n_values, values);
1408   }
1409   GST_OBJECT_UNLOCK (object);
1410   return res;
1411 }
1412 
1413 /**
1414  * gst_object_get_g_value_array:
1415  * @object: the object that has controlled properties
1416  * @property_name: the name of the property to get
1417  * @timestamp: the time that should be processed
1418  * @interval: the time spacing between subsequent values
1419  * @n_values: the number of values
1420  * @values: (array length=n_values): array to put control-values in
1421  *
1422  * Gets a number of #GValues for the given controlled property starting at the
1423  * requested time. The array @values need to hold enough space for @n_values of
1424  * #GValue.
1425  *
1426  * This function is useful if one wants to e.g. draw a graph of the control
1427  * curve or apply a control curve sample by sample.
1428  *
1429  * Returns: %TRUE if the given array could be filled, %FALSE otherwise
1430  */
1431 gboolean
gst_object_get_g_value_array(GstObject * object,const gchar * property_name,GstClockTime timestamp,GstClockTime interval,guint n_values,GValue * values)1432 gst_object_get_g_value_array (GstObject * object, const gchar * property_name,
1433     GstClockTime timestamp, GstClockTime interval, guint n_values,
1434     GValue * values)
1435 {
1436   gboolean res = FALSE;
1437   GstControlBinding *binding;
1438 
1439   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1440   g_return_val_if_fail (property_name, FALSE);
1441   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
1442   g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
1443   g_return_val_if_fail (values, FALSE);
1444 
1445   GST_OBJECT_LOCK (object);
1446   if ((binding = gst_object_find_control_binding (object, property_name))) {
1447     res = gst_control_binding_get_g_value_array (binding, timestamp, interval,
1448         n_values, values);
1449   }
1450   GST_OBJECT_UNLOCK (object);
1451   return res;
1452 }
1453 
1454 
1455 /**
1456  * gst_object_get_control_rate:
1457  * @object: the object that has controlled properties
1458  *
1459  * Obtain the control-rate for this @object. Audio processing #GstElement
1460  * objects will use this rate to sub-divide their processing loop and call
1461  * gst_object_sync_values() in between. The length of the processing segment
1462  * should be up to @control-rate nanoseconds.
1463  *
1464  * If the @object is not under property control, this will return
1465  * %GST_CLOCK_TIME_NONE. This allows the element to avoid the sub-dividing.
1466  *
1467  * The control-rate is not expected to change if the element is in
1468  * %GST_STATE_PAUSED or %GST_STATE_PLAYING.
1469  *
1470  * Returns: the control rate in nanoseconds
1471  */
1472 GstClockTime
gst_object_get_control_rate(GstObject * object)1473 gst_object_get_control_rate (GstObject * object)
1474 {
1475   g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
1476 
1477   return object->control_rate;
1478 }
1479 
1480 /**
1481  * gst_object_set_control_rate:
1482  * @object: the object that has controlled properties
1483  * @control_rate: the new control-rate in nanoseconds.
1484  *
1485  * Change the control-rate for this @object. Audio processing #GstElement
1486  * objects will use this rate to sub-divide their processing loop and call
1487  * gst_object_sync_values() in between. The length of the processing segment
1488  * should be up to @control-rate nanoseconds.
1489  *
1490  * The control-rate should not change if the element is in %GST_STATE_PAUSED or
1491  * %GST_STATE_PLAYING.
1492  */
1493 void
gst_object_set_control_rate(GstObject * object,GstClockTime control_rate)1494 gst_object_set_control_rate (GstObject * object, GstClockTime control_rate)
1495 {
1496   g_return_if_fail (GST_IS_OBJECT (object));
1497 
1498   object->control_rate = control_rate;
1499 }
1500