3 * Copyright (C) 2007,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4 * 2011 Stefan Sauer <ensonic@users.sf.net>
6 * gsttriggercontrolsource.c: Control source that provides some values at time-
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:gsttriggercontrolsource
27 * @title: GstTriggerControlSource
28 * @short_description: trigger control source
30 * #GstTriggerControlSource is a #GstControlSource, that returns values from user-given
31 * control points. It allows for a tolerance on the time-stamps.
33 * To use #GstTriggerControlSource get a new instance by calling
34 * gst_trigger_control_source_new(), bind it to a #GParamSpec and set some
35 * control points by calling gst_timed_value_control_source_set().
37 * All functions are MT-safe.
40 #include <glib-object.h>
43 #include "gsttriggercontrolsource.h"
44 #include "gst/glib-compat-private.h"
45 #include "gst/math-compat.h"
47 #define GST_CAT_DEFAULT controller_debug
48 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
50 struct _GstTriggerControlSourcePrivate
55 /* control point accessors */
57 /* returns the default value of the property, except for times with specific values */
58 /* needed for one-shot events, such as notes and triggers */
61 _interpolate_trigger (GstTimedValueControlSource * self, GSequenceIter * iter,
62 GstClockTime timestamp)
65 gint64 tolerance = ((GstTriggerControlSource *) self)->priv->tolerance;
66 gboolean found = FALSE;
68 cp = g_sequence_get (iter);
69 if (GST_CLOCK_DIFF (cp->timestamp, timestamp) <= tolerance) {
72 if ((iter = g_sequence_iter_next (iter)) && !g_sequence_iter_is_end (iter)) {
73 cp = g_sequence_get (iter);
74 if (GST_CLOCK_DIFF (timestamp, cp->timestamp) <= tolerance) {
86 interpolate_trigger_get (GstTimedValueControlSource * self,
87 GstClockTime timestamp, gdouble * value)
92 g_mutex_lock (&self->lock);
95 gst_timed_value_control_source_find_control_point_iter (self, timestamp);
97 *value = _interpolate_trigger (self, iter, timestamp);
101 g_mutex_unlock (&self->lock);
106 interpolate_trigger_get_value_array (GstTimedValueControlSource * self,
107 GstClockTime timestamp, GstClockTime interval, guint n_values,
110 gboolean ret = FALSE;
112 GstClockTime ts = timestamp;
113 GstClockTime next_ts = 0;
115 GSequenceIter *iter1 = NULL, *iter2 = NULL;
116 gboolean triggered = FALSE;
118 g_mutex_lock (&self->lock);
119 for (i = 0; i < n_values; i++) {
122 iter1 = gst_timed_value_control_source_find_control_point_iter (self, ts);
124 if (G_LIKELY (self->values))
125 iter2 = g_sequence_get_begin_iter (self->values);
129 iter2 = g_sequence_iter_next (iter1);
132 if (iter2 && !g_sequence_iter_is_end (iter2)) {
135 cp = g_sequence_get (iter2);
136 next_ts = cp->timestamp;
138 next_ts = GST_CLOCK_TIME_NONE;
142 val = _interpolate_trigger (self, iter1, ts);
146 g_mutex_unlock (&self->lock);
150 } else if (triggered) {
152 val = _interpolate_trigger (self, iter1, ts);
156 g_mutex_unlock (&self->lock);
165 g_mutex_unlock (&self->lock);
175 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "trigger control source", 0, \
176 "timeline value trigger control source")
178 G_DEFINE_TYPE_WITH_CODE (GstTriggerControlSource, gst_trigger_control_source,
179 GST_TYPE_TIMED_VALUE_CONTROL_SOURCE, _do_init);
182 * gst_trigger_control_source_new:
184 * This returns a new, unbound #GstTriggerControlSource.
186 * Returns: (transfer full): a new, unbound #GstTriggerControlSource.
189 gst_trigger_control_source_new (void)
191 return g_object_newv (GST_TYPE_TRIGGER_CONTROL_SOURCE, 0, NULL);
195 gst_trigger_control_source_init (GstTriggerControlSource * self)
197 GstControlSource *csource = (GstControlSource *) self;
200 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_TRIGGER_CONTROL_SOURCE,
201 GstTriggerControlSourcePrivate);
203 csource->get_value = (GstControlSourceGetValue) interpolate_trigger_get;
204 csource->get_value_array = (GstControlSourceGetValueArray)
205 interpolate_trigger_get_value_array;
209 gst_trigger_control_source_set_property (GObject * object, guint prop_id,
210 const GValue * value, GParamSpec * pspec)
212 GstTriggerControlSource *self = GST_TRIGGER_CONTROL_SOURCE (object);
216 GST_TIMED_VALUE_CONTROL_SOURCE_LOCK (self);
217 self->priv->tolerance = g_value_get_int64 (value);
218 GST_TIMED_VALUE_CONTROL_SOURCE_UNLOCK (self);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 gst_trigger_control_source_get_property (GObject * object, guint prop_id,
228 GValue * value, GParamSpec * pspec)
230 GstTriggerControlSource *self = GST_TRIGGER_CONTROL_SOURCE (object);
234 g_value_set_int64 (value, self->priv->tolerance);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 gst_trigger_control_source_class_init (GstTriggerControlSourceClass * klass)
245 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
247 g_type_class_add_private (klass, sizeof (GstTriggerControlSourcePrivate));
249 gobject_class->set_property = gst_trigger_control_source_set_property;
250 gobject_class->get_property = gst_trigger_control_source_get_property;
252 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
253 g_param_spec_int64 ("tolerance", "Tolerance",
254 "Amount of ns a control time can be off to still trigger",
255 0, G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));