3 * Copyright (C) 2007,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * 2011 Stefan Sauer <ensonic@users.sf.net>
6 * gsttimedvaluecontrolsource.c: Base class for timeed value based control
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
26 * SECTION:gsttimedvaluecontrolsource
27 * @title: GstTimedValueControlSource
28 * @short_description: timed value control source base class
30 * Base class for #GstControlSource that use time-stamped values.
32 * When overriding bind, chain up first to give this bind implementation a
33 * chance to setup things.
35 * All functions are MT-safe.
42 #include <glib-object.h>
45 #include "gstinterpolationcontrolsource.h"
46 #include "gst/glib-compat-private.h"
48 #define GST_CAT_DEFAULT controller_debug
49 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
52 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "timed value control source", 0, \
53 "timed value control source base class")
55 #define gst_timed_value_control_source_parent_class parent_class
56 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstTimedValueControlSource,
57 gst_timed_value_control_source, GST_TYPE_CONTROL_SOURCE, _do_init);
68 static guint gst_timed_value_control_source_signals[LAST_SIGNAL] = { 0 };
71 * gst_control_point_free:
72 * @cp: the object to free
74 * Frees all data allocated by a #GstControlPoint instance.
77 gst_control_point_free (GstControlPoint * cp)
79 g_return_if_fail (cp);
81 g_slice_free (GstControlPoint, cp);
85 * gst_control_point_copy:
86 * @cp: The control point to copy
88 * Copies a #GstControlPoint
90 * Returns: A copy of @cp
93 gst_control_point_copy (GstControlPoint * cp)
95 return g_slice_dup (GstControlPoint, cp);
99 gst_control_point_get_type (void)
101 static gsize type_id = 0;
103 if (g_once_init_enter (&type_id)) {
105 g_boxed_type_register_static (g_intern_static_string
107 (GBoxedCopyFunc) gst_control_point_copy,
108 (GBoxedFreeFunc) gst_control_point_free);
109 g_once_init_leave (&type_id, tmp);
116 gst_timed_value_control_source_reset (GstTimedValueControlSource * self)
118 GstControlSource *csource = (GstControlSource *) self;
120 csource->get_value = NULL;
121 csource->get_value_array = NULL;
124 g_sequence_free (self->values);
129 self->valid_cache = FALSE;
133 * gst_control_point_compare:
134 * @p1: a pointer to a #GstControlPoint
135 * @p2: a pointer to a #GstControlPoint
137 * Compare function for g_list operations that operates on two #GstControlPoint
141 gst_control_point_compare (gconstpointer p1, gconstpointer p2)
143 GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
144 GstClockTime ct2 = ((GstControlPoint *) p2)->timestamp;
146 return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
150 * gst_control_point_find:
151 * @p1: a pointer to a #GstControlPoint
152 * @p2: a pointer to a #GstClockTime
153 * @user_data: supplied user data
155 * Compare function for g_sequence operations that operates on a #GstControlPoint and
159 gst_control_point_find (gconstpointer p1, gconstpointer p2, gpointer user_data)
161 GstClockTime ct1 = ((GstControlPoint *) p1)->timestamp;
162 GstClockTime ct2 = *(GstClockTime *) p2;
164 return ((ct1 < ct2) ? -1 : ((ct1 == ct2) ? 0 : 1));
167 static GstControlPoint *
168 _make_new_cp (GstTimedValueControlSource * self, GstClockTime timestamp,
173 /* create a new GstControlPoint */
174 cp = g_slice_new0 (GstControlPoint);
175 cp->timestamp = timestamp;
182 gst_timed_value_control_source_set_internal (GstTimedValueControlSource *
183 self, GstClockTime timestamp, const gdouble value)
187 g_mutex_lock (&self->lock);
189 /* check if a control point for the timestamp already exists */
190 if (G_LIKELY (self->values)) {
191 GSequenceIter *iter = g_sequence_lookup (self->values, ×tamp,
192 (GCompareDataFunc) gst_control_point_find, NULL);
195 GstControlPoint *cp = g_sequence_get (iter);
197 /* update control point */
199 g_mutex_unlock (&self->lock);
202 gst_timed_value_control_source_signals[VALUE_CHANGED_SIGNAL], 0, cp);
206 self->values = g_sequence_new ((GDestroyNotify) gst_control_point_free);
207 GST_INFO ("create new timed value sequence");
210 /* sort new cp into the prop->values list */
211 cp = _make_new_cp (self, timestamp, value);
212 g_sequence_insert_sorted (self->values, cp,
213 (GCompareDataFunc) gst_control_point_compare, NULL);
215 g_mutex_unlock (&self->lock);
218 gst_timed_value_control_source_signals[VALUE_ADDED_SIGNAL], 0, cp);
221 self->valid_cache = FALSE;
225 * gst_timed_value_control_source_find_control_point_iter:
226 * @self: the control source to search in
227 * @timestamp: the search key
229 * Find last value before given timestamp in control point list.
230 * If all values in the control point list come after the given
231 * timestamp or no values exist, %NULL is returned.
233 * For use in control source implementations.
235 * Returns: (transfer none): the found #GSequenceIter or %NULL
237 GSequenceIter *gst_timed_value_control_source_find_control_point_iter
238 (GstTimedValueControlSource * self, GstClockTime timestamp)
246 g_sequence_search (self->values, ×tamp,
247 (GCompareDataFunc) gst_control_point_find, NULL);
249 /* g_sequence_search() returns the iter where timestamp
250 * would be inserted, i.e. the iter > timestamp, so
251 * we need to get the previous one. And of course, if
252 * there is no previous one, we return NULL. */
253 if (g_sequence_iter_is_begin (iter))
256 return g_sequence_iter_prev (iter);
261 * gst_timed_value_control_source_set:
262 * @self: the #GstTimedValueControlSource object
263 * @timestamp: the time the control-change is scheduled for
264 * @value: the control-value
266 * Set the value of given controller-handled property at a certain time.
268 * Returns: FALSE if the values couldn't be set, TRUE otherwise.
271 gst_timed_value_control_source_set (GstTimedValueControlSource * self,
272 GstClockTime timestamp, const gdouble value)
274 g_return_val_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self), FALSE);
275 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
277 gst_timed_value_control_source_set_internal (self, timestamp, value);
283 * gst_timed_value_control_source_set_from_list:
284 * @self: the #GstTimedValueControlSource object
285 * @timedvalues: (transfer none) (element-type GstTimedValue): a list
286 * with #GstTimedValue items
288 * Sets multiple timed values at once.
290 * Returns: FALSE if the values couldn't be set, TRUE otherwise.
293 gst_timed_value_control_source_set_from_list (GstTimedValueControlSource *
294 self, const GSList * timedvalues)
298 gboolean res = FALSE;
300 g_return_val_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self), FALSE);
302 for (node = timedvalues; node; node = g_slist_next (node)) {
304 if (!GST_CLOCK_TIME_IS_VALID (tv->timestamp)) {
305 GST_WARNING ("GstTimedValued with invalid timestamp passed to %s",
308 gst_timed_value_control_source_set_internal (self, tv->timestamp,
317 * gst_timed_value_control_source_unset:
318 * @self: the #GstTimedValueControlSource object
319 * @timestamp: the time the control-change should be removed from
321 * Used to remove the value of given controller-handled property at a certain
324 * Returns: FALSE if the value couldn't be unset (i.e. not found, TRUE otherwise.
327 gst_timed_value_control_source_unset (GstTimedValueControlSource * self,
328 GstClockTime timestamp)
331 gboolean res = FALSE;
332 GstControlPoint *cp = NULL;
334 g_return_val_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self), FALSE);
335 g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
337 g_mutex_lock (&self->lock);
338 /* check if a control point for the timestamp exists */
339 if (G_LIKELY (self->values) && (iter =
340 g_sequence_lookup (self->values, ×tamp,
341 (GCompareDataFunc) gst_control_point_find, NULL))) {
343 /* Iter contains the iter right after timestamp, i.e.
344 * we need to get the previous one and check the timestamp
346 cp = g_slice_dup (GstControlPoint, g_sequence_get (iter));
347 g_sequence_remove (iter);
349 self->valid_cache = FALSE;
352 g_mutex_unlock (&self->lock);
356 gst_timed_value_control_source_signals[VALUE_REMOVED_SIGNAL], 0, cp);
357 g_slice_free (GstControlPoint, cp);
364 * gst_timed_value_control_source_unset_all:
365 * @self: the #GstTimedValueControlSource object
367 * Used to remove all time-stamped values of given controller-handled property
371 gst_timed_value_control_source_unset_all (GstTimedValueControlSource * self)
373 g_return_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self));
375 g_mutex_lock (&self->lock);
376 /* free GstControlPoint structures */
378 g_sequence_free (self->values);
382 self->valid_cache = FALSE;
384 g_mutex_unlock (&self->lock);
388 _append_control_point (GstControlPoint * cp, GQueue * res)
390 g_queue_push_tail (res, cp);
394 * gst_timed_value_control_source_get_all:
395 * @self: the #GstTimedValueControlSource to get the list from
397 * Returns a read-only copy of the list of #GstTimedValue for the given property.
398 * Free the list after done with it.
400 * Returns: (transfer container) (element-type GstTimedValue): a copy
401 * of the list, or %NULL if the property isn't handled by the controller
404 gst_timed_value_control_source_get_all (GstTimedValueControlSource * self)
406 GQueue res = G_QUEUE_INIT;
408 g_return_val_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self), NULL);
410 g_mutex_lock (&self->lock);
411 if (G_LIKELY (self->values))
412 g_sequence_foreach (self->values, (GFunc) _append_control_point, &res);
413 g_mutex_unlock (&self->lock);
419 * gst_timed_value_control_source_get_count:
420 * @self: the #GstTimedValueControlSource to get the number of values from
422 * Get the number of control points that are set.
424 * Returns: the number of control points that are set.
427 gst_timed_value_control_source_get_count (GstTimedValueControlSource * self)
429 g_return_val_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self), 0);
430 return self->nvalues;
434 * gst_timed_value_control_invalidate_cache:
435 * @self: the #GstTimedValueControlSource
437 * Reset the controlled value cache.
440 gst_timed_value_control_invalidate_cache (GstTimedValueControlSource * self)
442 g_return_if_fail (GST_IS_TIMED_VALUE_CONTROL_SOURCE (self));
443 self->valid_cache = FALSE;
447 gst_timed_value_control_source_init (GstTimedValueControlSource * self)
449 g_mutex_init (&self->lock);
453 gst_timed_value_control_source_finalize (GObject * obj)
455 GstTimedValueControlSource *self = GST_TIMED_VALUE_CONTROL_SOURCE (obj);
457 g_mutex_lock (&self->lock);
458 gst_timed_value_control_source_reset (self);
459 g_mutex_unlock (&self->lock);
460 g_mutex_clear (&self->lock);
462 G_OBJECT_CLASS (parent_class)->finalize (obj);
466 gst_timed_value_control_source_class_init (GstTimedValueControlSourceClass
469 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
470 //GstControlSourceClass *csource_class = GST_CONTROL_SOURCE_CLASS (klass);
473 * GstTimedValueControlSource::value-changed
474 * @self: The #GstTimedValueControlSource on which a #GstTimedValue has changed
475 * @timed_value: The #GstTimedValue where the value changed
477 * Emitted right after the new value has been set on @timed_signals
481 gst_timed_value_control_source_signals[VALUE_CHANGED_SIGNAL] =
482 g_signal_new ("value-changed", G_TYPE_FROM_CLASS (klass),
483 G_SIGNAL_RUN_FIRST, 0, NULL,
484 NULL, NULL, G_TYPE_NONE, 1, gst_control_point_get_type ());
487 * GstTimedValueControlSource::value-added
488 * @self: The #GstTimedValueControlSource into which a #GstTimedValue has been
490 * @timed_value: The newly added #GstTimedValue
492 * Emitted right after the new value has been added to @self
496 gst_timed_value_control_source_signals[VALUE_ADDED_SIGNAL] =
497 g_signal_new ("value-added", G_TYPE_FROM_CLASS (klass),
498 G_SIGNAL_RUN_FIRST, 0, NULL,
499 NULL, NULL, G_TYPE_NONE, 1, gst_control_point_get_type ());
502 * GstTimedValueControlSource::value-removed
503 * @self: The #GstTimedValueControlSource from which a #GstTimedValue has been
505 * @timed_value: The removed #GstTimedValue
507 * Emitted when @timed_value is removed from @self
511 gst_timed_value_control_source_signals[VALUE_REMOVED_SIGNAL] =
512 g_signal_new ("value-removed", G_TYPE_FROM_CLASS (klass),
513 G_SIGNAL_RUN_FIRST, 0, NULL,
514 NULL, NULL, G_TYPE_NONE, 1, gst_control_point_get_type ());
517 gobject_class->finalize = gst_timed_value_control_source_finalize;