gstcontroller.c 41.9 KB
Newer Older
1
2
3
/* GStreamer
 *
 * Copyright (C) <2005> Stefan Kost <ensonic at users dot sf dot net>
4
 * Copyright (C) 2007 Sebastian Dröge <slomo@circular-chaos.org>
5
 *
6
 * gstcontroller.c: dynamic parameter control subsystem
7
8
9
10
11
12
13
14
15
16
 *
 * 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.
17
 *
18
19
20
21
 * 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.
22
23
 */

24
25
26
27
/**
 * SECTION:gstcontroller
 * @short_description: dynamic parameter control subsystem
 *
28
 * The controller subsystem offers a lightweight way to adjust gobject
29
30
 * properties over stream-time. It works by using time-stamped value pairs that
 * are queued for element-properties. At run-time the elements continously pull
31
 * values changes for the current stream-time.
32
33
34
35
36
37
38
39
40
41
 *
 * What needs to be changed in a #GstElement?
 * Very little - it is just two steps to make a plugin controllable!
 * <orderedlist>
 *   <listitem><para>
 *     mark gobject-properties paramspecs that make sense to be controlled,
 *     by GST_PARAM_CONTROLLABLE.
 *   </para></listitem>
 *   <listitem><para>
 *     when processing data (get, chain, loop function) at the beginning call
42
 *     gst_object_sync_values(element,timestamp).
43
44
45
46
47
48
49
50
51
 *     This will made the controller to update all gobject properties that are under
 *     control with the current values based on timestamp.
 *   </para></listitem>
 * </orderedlist>
 *
 * What needs to be done in applications?
 * Again its not a lot to change.
 * <orderedlist>
 *   <listitem><para>
52
53
 *     first put some properties under control, by calling
 *     controller = g_object_control_properties(object, "prop1", "prop2",...);
54
55
56
57
58
59
60
 *   </para></listitem>
 *   <listitem><para>
 *     set how the controller will smooth inbetween values.
 *     gst_controller_set_interpolation_mode(controller,"prop1",mode);
 *   </para></listitem>
 *   <listitem><para>
 *     set key values
61
62
 *     gst_controller_set (controller, "prop1" ,0 * GST_SECOND, value1);
 *     gst_controller_set (controller, "prop1" ,1 * GST_SECOND, value2);
63
64
65
66
67
68
 *   </para></listitem>
 *   <listitem><para>
 *     start your pipeline
 *   </para></listitem>
 * </orderedlist>
 */
69

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
70
71
72
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
73
#include "gstcontrollerprivate.h"
74
#include "gstcontroller.h"
75
76
77
78
79

#define GST_CAT_DEFAULT gst_controller_debug
GST_DEBUG_CATEGORY_EXTERN (GST_CAT_DEFAULT);

static GObjectClass *parent_class = NULL;
80
GQuark __gst_controller_key;
81

82
83
84
85
86
87
88
89
/* property ids */
enum
{
  PROP_CONTROL_RATE = 1
};

struct _GstControllerPrivate
{
90
91
  GstClockTime control_rate;
  GstClockTime last_sync;
92
};
93
94
95
96

/* imports from gst-interpolation.c */

extern GList
97
    * gst_controlled_property_find_control_point_node (GstControlledProperty *
98
99
    prop, GstClockTime timestamp);
extern GstInterpolateMethod *interpolation_methods[];
100
extern guint num_interpolation_methods;
101
102
103
104
105
106
107
108
109
110

/* callbacks */

void
on_object_controlled_property_changed (const GObject * object, GParamSpec * arg,
    gpointer user_data)
{
  GstControlledProperty *prop = GST_CONTROLLED_PROPERTY (user_data);
  GstController *ctrl;

111
  GST_LOG ("notify for '%s'", prop->name);
112

113
  ctrl = g_object_get_qdata (G_OBJECT (object), __gst_controller_key);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  g_return_if_fail (ctrl);

  if (g_mutex_trylock (ctrl->lock)) {
    if (!G_IS_VALUE (&prop->live_value.value)) {
      g_value_init (&prop->live_value.value, prop->type);
    }
    g_object_get_property (G_OBJECT (object), prop->name,
        &prop->live_value.value);
    prop->live_value.timestamp = prop->last_value.timestamp;
    g_mutex_unlock (ctrl->lock);
    GST_DEBUG ("-> is live update : ts=%" G_GUINT64_FORMAT,
        prop->live_value.timestamp);
  }
}

/* helper */

/*
132
133
134
 * gst_control_point_compare:
 * @p1: a pointer to a #GstControlPoint
 * @p2: a pointer to a #GstControlPoint
135
 *
136
 * Compare function for g_list operations that operates on two #GstControlPoint
137
138
139
 * parameters.
 */
static gint
140
gst_control_point_compare (gconstpointer p1, gconstpointer p2)
141
{
142
143
  GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
  GstClockTime ct2 = ((GstControlPoint *) p2)->timestamp;
144
145
146
147
148
149
150
151

  return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
/* this does not produce an gint :(
  return ((ct1 - ct2));
*/
}

/*
152
153
 * gst_control_point_find:
 * @p1: a pointer to a #GstControlPoint
154
155
 * @p2: a pointer to a #GstClockTime
 *
156
 * Compare function for g_list operations that operates on a #GstControlPoint and
157
158
159
 * a #GstClockTime.
 */
static gint
160
gst_control_point_find (gconstpointer p1, gconstpointer p2)
161
{
162
  GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  GstClockTime ct2 = *(GstClockTime *) p2;

  return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
/* this does not produce an gint :(
  return ((ct1 - ct2));
*/
}

/*
 * gst_controlled_property_set_interpolation_mode:
 * @self: the controlled property object to change
 * @mode: the new interpolation mode
 *
 * Sets the given Interpolation mode for the controlled property and activates
 * the respective interpolation hooks.
178
179
 *
 * Returns: %TRUE for success
180
181
182
183
184
 */
static gboolean
gst_controlled_property_set_interpolation_mode (GstControlledProperty * self,
    GstInterpolateMode mode)
{
185
186
  gboolean res = TRUE;

187
188
189
190
191
  if (mode >= num_interpolation_methods || interpolation_methods[mode] == NULL) {
    GST_WARNING ("interpolation mode %d invalid or not implemented yet", mode);
    return FALSE;
  }

192
193
  self->interpolation = mode;
  if (mode != GST_INTERPOLATE_USER) {
194
    switch (self->base) {
195
196
197
198
199
      case G_TYPE_INT:
        self->get = interpolation_methods[mode]->get_int;
        self->get_value_array =
            interpolation_methods[mode]->get_int_value_array;
        break;
200
201
202
203
204
      case G_TYPE_UINT:
        self->get = interpolation_methods[mode]->get_uint;
        self->get_value_array =
            interpolation_methods[mode]->get_uint_value_array;
        break;
205
206
207
208
209
      case G_TYPE_LONG:
        self->get = interpolation_methods[mode]->get_long;
        self->get_value_array =
            interpolation_methods[mode]->get_long_value_array;
        break;
210
211
212
213
214
      case G_TYPE_ULONG:
        self->get = interpolation_methods[mode]->get_ulong;
        self->get_value_array =
            interpolation_methods[mode]->get_ulong_value_array;
        break;
215
216
217
218
219
220
221
222
223
224
      case G_TYPE_FLOAT:
        self->get = interpolation_methods[mode]->get_float;
        self->get_value_array =
            interpolation_methods[mode]->get_float_value_array;
        break;
      case G_TYPE_DOUBLE:
        self->get = interpolation_methods[mode]->get_double;
        self->get_value_array =
            interpolation_methods[mode]->get_double_value_array;
        break;
225
226
227
228
229
      case G_TYPE_BOOLEAN:
        self->get = interpolation_methods[mode]->get_boolean;
        self->get_value_array =
            interpolation_methods[mode]->get_boolean_value_array;
        break;
230
231
232
233
      case G_TYPE_ENUM:
        self->get = interpolation_methods[mode]->get_uint;
        self->get_value_array =
            interpolation_methods[mode]->get_enum_value_array;
234
        break;
235
236
237
238
239
      case G_TYPE_STRING:
        self->get = interpolation_methods[mode]->get_string;
        self->get_value_array =
            interpolation_methods[mode]->get_string_value_array;
        break;
240
      default:
241
242
        self->get = NULL;
        self->get_value_array = NULL;
243
    }
244
    if (!self->get || !self->get_value_array) {
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
245
      GST_WARNING ("incomplete implementation for type %lu/%lu:'%s'/'%s'",
246
247
          self->type, self->base,
          g_type_name (self->type), g_type_name (self->base));
248
      res = FALSE;
249
    }
250
251
252
253
    if (mode == GST_INTERPOLATE_QUADRATIC) {
      GST_WARNING ("Quadratic interpolation mode is deprecated, using cubic"
          "interpolation mode");
    }
254
255
256
257
  } else {
    /* TODO shouldn't this also get a GstInterpolateMethod *user_method
       for the case mode==GST_INTERPOLATE_USER
     */
258
    res = FALSE;
259
  }
260
261
262

  self->valid_cache = FALSE;

263
  return (res);
264
265
}

266
267
268
269
270
271
272
273
274
275
276
277
static void
gst_controlled_property_prepend_default (GstControlledProperty * prop)
{
  GstControlPoint *cp = g_new0 (GstControlPoint, 1);

  cp->timestamp = 0;
  g_value_init (&cp->value, prop->type);
  g_value_copy (&prop->default_value, &cp->value);
  prop->values = g_list_prepend (prop->values, cp);
  prop->nvalues++;
}

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * gst_controlled_property_new:
 * @object: for which object the controlled property should be set up
 * @name: the name of the property to be controlled
 *
 * Private method which initializes the fields of a new controlled property
 * structure.
 *
 * Returns: a freshly allocated structure or %NULL
 */
static GstControlledProperty *
gst_controlled_property_new (GObject * object, const gchar * name)
{
  GstControlledProperty *prop = NULL;
  GParamSpec *pspec;

  GST_INFO ("trying to put property '%s' under control", name);

296
  /* check if the object has a property of that name */
297
298
299
300
  if ((pspec =
          g_object_class_find_property (G_OBJECT_GET_CLASS (object), name))) {
    GST_DEBUG ("  psec->flags : 0x%08x", pspec->flags);

301
    /* check if this param is witable */
302
    g_return_val_if_fail ((pspec->flags & G_PARAM_WRITABLE), NULL);
303
    /* check if property is controlable */
304
    g_return_val_if_fail ((pspec->flags & GST_PARAM_CONTROLLABLE), NULL);
305
    /* check if this param is not construct-only */
306
307
    g_return_val_if_fail (!(pspec->flags & G_PARAM_CONSTRUCT_ONLY), NULL);

308
309
310
311
312
313
314
315
316
317
318
319
320
    /* TODO do sanity checks
       we don't control some pspec->value_type:
       G_TYPE_PARAM_BOXED
       G_TYPE_PARAM_ENUM
       G_TYPE_PARAM_FLAGS
       G_TYPE_PARAM_OBJECT
       G_TYPE_PARAM_PARAM
       G_TYPE_PARAM_POINTER
       G_TYPE_PARAM_STRING
     */

    if ((prop = g_new0 (GstControlledProperty, 1))) {
      gchar *signal_name;
321
      GType base;
322

323
      prop->name = pspec->name; /* so we don't use the same mem twice */
324
      prop->type = G_PARAM_SPEC_VALUE_TYPE (pspec);
325
326
327
328
329
330
331
332
333
      /* get the fundamental base type */
      prop->base = prop->type;
      while ((base = g_type_parent (prop->base))) {
        prop->base = base;
      }
      /* initialize mode specific accessor callbacks */
      if (!gst_controlled_property_set_interpolation_mode (prop,
              GST_INTERPOLATE_NONE))
        goto Error;
334
335
336
337
      /* prepare our gvalues */
      g_value_init (&prop->default_value, prop->type);
      g_value_init (&prop->result_value, prop->type);
      g_value_init (&prop->last_value.value, prop->type);
338
      switch (prop->base) {
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        case G_TYPE_INT:{
          GParamSpecInt *tpspec = G_PARAM_SPEC_INT (pspec);

          g_value_set_int (&prop->default_value, tpspec->default_value);
        }
          break;
        case G_TYPE_UINT:{
          GParamSpecUInt *tpspec = G_PARAM_SPEC_UINT (pspec);

          g_value_set_uint (&prop->default_value, tpspec->default_value);
        }
          break;
        case G_TYPE_LONG:{
          GParamSpecLong *tpspec = G_PARAM_SPEC_LONG (pspec);

          g_value_set_long (&prop->default_value, tpspec->default_value);
        }
          break;
        case G_TYPE_ULONG:{
          GParamSpecULong *tpspec = G_PARAM_SPEC_ULONG (pspec);

          g_value_set_ulong (&prop->default_value, tpspec->default_value);
        }
          break;
        case G_TYPE_FLOAT:{
          GParamSpecFloat *tpspec = G_PARAM_SPEC_FLOAT (pspec);

          g_value_set_float (&prop->default_value, tpspec->default_value);
        }
368
          break;
369
370
371
372
373
374
        case G_TYPE_DOUBLE:{
          GParamSpecDouble *tpspec = G_PARAM_SPEC_DOUBLE (pspec);

          g_value_set_double (&prop->default_value, tpspec->default_value);
        }
          break;
375
376
377
378
379
380
        case G_TYPE_BOOLEAN:{
          GParamSpecBoolean *tpspec = G_PARAM_SPEC_BOOLEAN (pspec);

          g_value_set_boolean (&prop->default_value, tpspec->default_value);
        }
          break;
381
382
383
384
385
386
        case G_TYPE_ENUM:{
          GParamSpecEnum *tpspec = G_PARAM_SPEC_ENUM (pspec);

          g_value_set_enum (&prop->default_value, tpspec->default_value);
        }
          break;
387
388
389
390
391
392
        case G_TYPE_STRING:{
          GParamSpecString *tpspec = G_PARAM_SPEC_STRING (pspec);

          g_value_set_string (&prop->default_value, tpspec->default_value);
        }
          break;
393
394
395
396
        default:
          GST_WARNING ("incomplete implementation for paramspec type '%s'",
              G_PARAM_SPEC_TYPE_NAME (pspec));
      }
397
398
399
400
401
402
403
404

      prop->valid_cache = FALSE;
      prop->nvalues = 0;

      /* Add a control point at timestamp 0 with the default value
       * to make the life of interpolators easier. */
      gst_controlled_property_prepend_default (prop);

405
406
407
408
409
410
411
412
413
414
415
      signal_name = g_alloca (8 + 1 + strlen (name));
      g_sprintf (signal_name, "notify::%s", name);
      prop->notify_handler_id =
          g_signal_connect (object, signal_name,
          G_CALLBACK (on_object_controlled_property_changed), (gpointer) prop);
    }
  } else {
    GST_WARNING ("class '%s' has no property '%s'", G_OBJECT_TYPE_NAME (object),
        name);
  }
  return (prop);
416
417
418
419
Error:
  if (prop)
    g_free (prop);
  return (NULL);
420
421
422
}

/*
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
 * gst_control_point_free:
 * @prop: the object to free
 *
 * Private method which frees all data allocated by a #GstControlPoint
 * instance.
 */
static void
gst_control_point_free (GstControlPoint * cp)
{
  g_return_if_fail (cp);

  g_value_unset (&cp->value);
  g_free (cp);
}

/*

440
441
442
443
444
445
446
447
448
 * gst_controlled_property_free:
 * @prop: the object to free
 *
 * Private method which frees all data allocated by a #GstControlledProperty
 * instance.
 */
static void
gst_controlled_property_free (GstControlledProperty * prop)
{
449
  g_list_foreach (prop->values, (GFunc) gst_control_point_free, NULL);
450
  g_list_free (prop->values);
451
452
453
454
455
456
457

  g_value_unset (&prop->default_value);
  g_value_unset (&prop->result_value);
  g_value_unset (&prop->last_value.value);
  if (G_IS_VALUE (&prop->live_value.value))
    g_value_unset (&prop->live_value.value);

458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  g_free (prop);
}

/*
 * gst_controller_find_controlled_property:
 * @self: the controller object to search for a property in
 * @name: the gobject property name to look for
 *
 * Searches the list of properties under control.
 *
 * Returns: a #GstControlledProperty object of %NULL if the property is not
 * being controlled.
 */
static GstControlledProperty *
gst_controller_find_controlled_property (GstController * self,
    const gchar * name)
{
  GstControlledProperty *prop;
  GList *node;

  for (node = self->properties; node; node = g_list_next (node)) {
    prop = node->data;
    if (!strcmp (prop->name, name)) {
      return (prop);
    }
  }
484
  GST_DEBUG ("controller does not (yet) manage property '%s'", name);
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504

  return (NULL);
}

/* methods */

/**
 * gst_controller_new_valist:
 * @object: the object of which some properties should be controlled
 * @var_args: %NULL terminated list of property names that should be controlled
 *
 * Creates a new GstController for the given object's properties
 *
 * Returns: the new controller.
 */
GstController *
gst_controller_new_valist (GObject * object, va_list var_args)
{
  GstController *self;
  GstControlledProperty *prop;
505
  gboolean ref_existing = TRUE;
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  gchar *name;

  g_return_val_if_fail (G_IS_OBJECT (object), NULL);

  GST_INFO ("setting up a new controller");

  /* TODO should this method check if the given object implements GstParent and
     if so instantiate a GstParentController ?

     BilboEd: This is too specific to be put here, don't we want
     GstController to be as generic as possible ?

     Ensonic: So we will have gst_parent_controller_new as well and maybe a
     convinience function that automatically chooses the right one (how to name it)?
     GstParent will be in core after all.
   */

523
  self = g_object_get_qdata (object, __gst_controller_key);
524
  /* create GstControlledProperty for each property */
525
  while ((name = va_arg (var_args, gchar *))) {
526
    /* test if this property isn't yet controlled */
527
    if (!self || !(prop = gst_controller_find_controlled_property (self, name))) {
528
      /* create GstControlledProperty and add to self->propeties List */
529
      if ((prop = gst_controlled_property_new (object, name))) {
530
        /* if we don't have a controller object yet, now is the time to create one */
531
532
        if (!self) {
          self = g_object_new (GST_TYPE_CONTROLLER, NULL);
533
          self->object = g_object_ref (object);
534
          /* store the controller */
535
          g_object_set_qdata (object, __gst_controller_key, self);
536
          ref_existing = FALSE;
537
        } else {
538
539
540
541
542
543
          /* only want one single _ref(), even for multiple properties */
          if (ref_existing) {
            g_object_ref (self);
            ref_existing = FALSE;
            GST_INFO ("returning existing controller");
          }
544
545
        }
        self->properties = g_list_prepend (self->properties, prop);
546
      }
547
548
    } else {
      GST_WARNING ("trying to control property again");
549
550
551
552
      if (ref_existing) {
        g_object_ref (self);
        ref_existing = FALSE;
      }
553
    }
554
555
556
  }
  va_end (var_args);

557
558
  if (self)
    GST_INFO ("controller->ref_count=%d", G_OBJECT (self)->ref_count);
559
560
561
  return (self);
}

562
563
564
565
566
567
568
569
570
571
572
573
574
575
/**
 * gst_controller_new_list:
 * @object: the object of which some properties should be controlled
 * @list: list of property names that should be controlled
 *
 * Creates a new GstController for the given object's properties
 *
 * Returns: the new controller.
 */
GstController *
gst_controller_new_list (GObject * object, GList * list)
{
  GstController *self;
  GstControlledProperty *prop;
576
  gboolean ref_existing = TRUE;
577
578
579
580
581
582
583
584
  gchar *name;
  GList *node;

  g_return_val_if_fail (G_IS_OBJECT (object), NULL);

  GST_INFO ("setting up a new controller");

  self = g_object_get_qdata (object, __gst_controller_key);
585
  /* create GstControlledProperty for each property */
586
  for (node = list; node; node = g_list_next (node)) {
587
    name = (gchar *) node->data;
588
    /* test if this property isn't yet controlled */
589
    if (!self || !(prop = gst_controller_find_controlled_property (self, name))) {
590
      /* create GstControlledProperty and add to self->propeties List */
591
      if ((prop = gst_controlled_property_new (object, name))) {
592
        /* if we don't have a controller object yet, now is the time to create one */
593
594
        if (!self) {
          self = g_object_new (GST_TYPE_CONTROLLER, NULL);
595
          self->object = g_object_ref (object);
596
          /* store the controller */
597
          g_object_set_qdata (object, __gst_controller_key, self);
598
          ref_existing = FALSE;
599
        } else {
600
601
602
603
604
605
          /* only want one single _ref(), even for multiple properties */
          if (ref_existing) {
            g_object_ref (self);
            ref_existing = FALSE;
            GST_INFO ("returning existing controller");
          }
606
607
608
609
610
        }
        self->properties = g_list_prepend (self->properties, prop);
      }
    } else {
      GST_WARNING ("trying to control property again");
611
612
613
614
      if (ref_existing) {
        g_object_ref (self);
        ref_existing = FALSE;
      }
615
616
617
618
619
620
621
622
    }
  }

  if (self)
    GST_INFO ("controller->ref_count=%d", G_OBJECT (self)->ref_count);
  return (self);
}

623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/**
 * gst_controller_new:
 * @object: the object of which some properties should be controlled
 * @...: %NULL terminated list of property names that should be controlled
 *
 * Creates a new GstController for the given object's properties
 *
 * Returns: the new controller.
 */
GstController *
gst_controller_new (GObject * object, ...)
{
  GstController *self;
  va_list var_args;

  g_return_val_if_fail (G_IS_OBJECT (object), NULL);

  va_start (var_args, object);
  self = gst_controller_new_valist (object, var_args);
  va_end (var_args);

  return (self);
}

/**
648
 * gst_controller_remove_properties_valist:
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
 * @self: the controller object from which some properties should be removed
 * @var_args: %NULL terminated list of property names that should be removed
 *
 * Removes the given object properties from the controller
 *
 * Returns: %FALSE if one of the given property isn't handled by the controller, %TRUE otherwise
 */
gboolean
gst_controller_remove_properties_valist (GstController * self, va_list var_args)
{
  gboolean res = TRUE;
  GstControlledProperty *prop;
  gchar *name;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);

  while ((name = va_arg (var_args, gchar *))) {
666
    /* find the property in the properties list of the controller, remove and free it */
667
668
669
    g_mutex_lock (self->lock);
    if ((prop = gst_controller_find_controlled_property (self, name))) {
      self->properties = g_list_remove (self->properties, prop);
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
      g_signal_handler_disconnect (self->object, prop->notify_handler_id);
      gst_controlled_property_free (prop);
    } else {
      res = FALSE;
    }
    g_mutex_unlock (self->lock);
  }

  return (res);
}

/**
 * gst_controller_remove_properties_list:
 * @self: the controller object from which some properties should be removed
 * @list: #GList of property names that should be removed
 *
 * Removes the given object properties from the controller
 *
 * Returns: %FALSE if one of the given property isn't handled by the controller, %TRUE otherwise
 */
gboolean
gst_controller_remove_properties_list (GstController * self, GList * list)
{
  gboolean res = TRUE;
  GstControlledProperty *prop;
  gchar *name;
  GList *tmp;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);

  for (tmp = list; tmp; tmp = g_list_next (tmp)) {
    name = (gchar *) tmp->data;

    /* find the property in the properties list of the controller, remove and free it */
    g_mutex_lock (self->lock);
    if ((prop = gst_controller_find_controlled_property (self, name))) {
      self->properties = g_list_remove (self->properties, prop);
707
      g_signal_handler_disconnect (self->object, prop->notify_handler_id);
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
      gst_controlled_property_free (prop);
    } else {
      res = FALSE;
    }
    g_mutex_unlock (self->lock);
  }

  return (res);
}

/**
 * gst_controller_remove_properties:
 * @self: the controller object from which some properties should be removed
 * @...: %NULL terminated list of property names that should be removed
 *
 * Removes the given object properties from the controller
 *
 * Returns: %FALSE if one of the given property isn't handled by the controller, %TRUE otherwise
 */
gboolean
gst_controller_remove_properties (GstController * self, ...)
{
  gboolean res;
  va_list var_args;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);

  va_start (var_args, self);
  res = gst_controller_remove_properties_valist (self, var_args);
  va_end (var_args);

  return (res);
}

742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
static gboolean
gst_controller_set_unlocked (GstController * self, GstControlledProperty * prop,
    GstClockTime timestamp, GValue * value)
{
  gboolean res = FALSE;

  if (G_VALUE_TYPE (value) == prop->type) {
    GstControlPoint *cp;
    GList *node;

    /* check if a control point for the timestamp already exists */
    if ((node = g_list_find_custom (prop->values, &timestamp,
                gst_control_point_find))) {
      cp = node->data;
      g_value_reset (&cp->value);
      g_value_copy (value, &cp->value);
    } else {
      /* create a new GstControlPoint */
      cp = g_new0 (GstControlPoint, 1);
      cp->timestamp = timestamp;
      g_value_init (&cp->value, prop->type);
      g_value_copy (value, &cp->value);
      /* and sort it into the prop->values list */
      prop->values =
          g_list_insert_sorted (prop->values, cp, gst_control_point_compare);
      prop->nvalues++;
    }
    prop->valid_cache = FALSE;
    res = TRUE;
  } else {
    GST_WARNING ("incompatible value type for property '%s'", prop->name);
  }

  return res;
}

778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/**
 * gst_controller_set:
 * @self: the controller object which handles the properties
 * @property_name: the name of the property to set
 * @timestamp: the time the control-change is schedules for
 * @value: the control-value
 *
 * Set the value of given controller-handled property at a certain time.
 *
 * Returns: FALSE if the values couldn't be set (ex : properties not handled by controller), TRUE otherwise
 */
gboolean
gst_controller_set (GstController * self, gchar * property_name,
    GstClockTime timestamp, GValue * value)
{
  gboolean res = FALSE;
  GstControlledProperty *prop;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (property_name, FALSE);
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
  g_return_val_if_fail (G_IS_VALUE (value), FALSE);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
803
    res = gst_controller_set_unlocked (self, prop, timestamp, value);
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
  }
  g_mutex_unlock (self->lock);

  return (res);
}

/**
 * gst_controller_set_from_list:
 * @self: the controller object which handles the properties
 * @property_name: the name of the property to set
 * @timedvalues: a list with #GstTimedValue items
 *
 * Sets multiple timed values at once.
 *
 * Returns: %FALSE if the values couldn't be set (ex : properties not handled by controller), %TRUE otherwise
 */

gboolean
gst_controller_set_from_list (GstController * self, gchar * property_name,
    GSList * timedvalues)
{
  gboolean res = FALSE;
  GstControlledProperty *prop;
  GSList *node;
  GstTimedValue *tv;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (property_name, FALSE);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
    for (node = timedvalues; node; node = g_slist_next (node)) {
      tv = node->data;
837
838
839
840
841
842
      if (!GST_CLOCK_TIME_IS_VALID (tv->timestamp)) {
        GST_WARNING ("GstTimedValued with invalid timestamp passed to %s "
            "for property '%s'", GST_FUNCTION, property_name);
      } else if (!G_IS_VALUE (&tv->value)) {
        GST_WARNING ("GstTimedValued with invalid value passed to %s "
            "for property '%s'", GST_FUNCTION, property_name);
843
      } else {
844
845
        res =
            gst_controller_set_unlocked (self, prop, tv->timestamp, &tv->value);
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
      }
    }
  }
  g_mutex_unlock (self->lock);

  return (res);
}

/**
 * gst_controller_unset:
 * @self: the controller object which handles the properties
 * @property_name: the name of the property to unset
 * @timestamp: the time the control-change should be removed from
 *
 * Used to remove the value of given controller-handled property at a certain
 * time.
 *
 * Returns: %FALSE if the values couldn't be unset (ex : properties not handled by controller), %TRUE otherwise
 */
gboolean
gst_controller_unset (GstController * self, gchar * property_name,
    GstClockTime timestamp)
{
  gboolean res = FALSE;
  GstControlledProperty *prop;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (property_name, FALSE);
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
878
879
    GList *node;

880
    /* check if a control point for the timestamp exists */
881
    if ((node = g_list_find_custom (prop->values, &timestamp,
882
                gst_control_point_find))) {
883
884
885
886
887
888
889
890
891
892
893
894
895
896
      GstControlPoint *cp = node->data;

      if (cp->timestamp == 0) {
        /* Restore the default node */
        g_value_reset (&cp->value);
        g_value_copy (&prop->default_value, &cp->value);
      } else {
        if (node == prop->last_requested_value)
          prop->last_requested_value = NULL;
        gst_control_point_free (node->data);    /* free GstControlPoint */
        prop->values = g_list_delete_link (prop->values, node);
        prop->nvalues--;
      }
      prop->valid_cache = FALSE;
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
      res = TRUE;
    }
  }
  g_mutex_unlock (self->lock);

  return (res);
}

/**
 * gst_controller_unset_all:
 * @self: the controller object which handles the properties
 * @property_name: the name of the property to unset
 *
 * Used to remove all time-stamped values of given controller-handled property
 *
 * Returns: %FALSE if the values couldn't be unset (ex : properties not handled
 * by controller), %TRUE otherwise
 * Since: 0.10.5
 */
gboolean
gst_controller_unset_all (GstController * self, gchar * property_name)
{
  gboolean res = FALSE;
  GstControlledProperty *prop;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), FALSE);
  g_return_val_if_fail (property_name, FALSE);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
927
928
    /* free GstControlPoint structures */
    g_list_foreach (prop->values, (GFunc) gst_control_point_free, NULL);
929
    g_list_free (prop->values);
930
    prop->last_requested_value = NULL;
931
    prop->values = NULL;
932
933
934
935
936
937
    prop->nvalues = 0;
    prop->valid_cache = FALSE;

    /* Insert the default control point again */
    gst_controlled_property_prepend_default (prop);

938
939
940
941
942
943
944
945
946
947
948
    res = TRUE;
  }
  g_mutex_unlock (self->lock);

  return (res);
}

/**
 * gst_controller_get:
 * @self: the controller object which handles the properties
 * @property_name: the name of the property to get
949
 * @timestamp: the time the control-change should be read from
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
 *
 * Gets the value for the given controller-handled property at the requested
 * time.
 *
 * Returns: the GValue of the property at the given time, or %NULL if the property isn't handled by the controller
 */
GValue *
gst_controller_get (GstController * self, gchar * property_name,
    GstClockTime timestamp)
{
  GstControlledProperty *prop;
  GValue *val = NULL;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), NULL);
  g_return_val_if_fail (property_name, NULL);
  g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
969
    /* get current value via interpolator */
970
971
972
973
974
975
976
977
978
979
980
    val = prop->get (prop, timestamp);
  }
  g_mutex_unlock (self->lock);

  return (val);
}

/**
 * gst_controller_get_all:
 * @self: the controller to get the list from
 * @property_name: the name of the property to get the list for
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
981
 *
982
983
984
 * Returns a read-only copy of the list of GstTimedValue for the given property.
 * Free the list after done with it.
 *
985
986
 * <note><para>This doesn't modify the controlled GObject property!</para></note>
 *
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
 * Returns: a copy of the list, or %NULL if the property isn't handled by the controller
 */
const GList *
gst_controller_get_all (GstController * self, gchar * property_name)
{
  GList *res = NULL;
  GstControlledProperty *prop;

  g_return_val_if_fail (GST_IS_CONTROLLER (self), NULL);
  g_return_val_if_fail (property_name, NULL);

  g_mutex_lock (self->lock);
  if ((prop = gst_controller_find_controlled_property (self, property_name))) {
    res = g_list_copy (prop->values);
For faster browsing, not all history is shown. View entire blame