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 * @short_description: trigger control source
29 * #GstTriggerControlSource is a #GstControlSource, that returns values from user-given
30 * control points. It allows for a tolerance on the time-stamps.
32 * To use #GstTriggerControlSource get a new instance by calling
33 * gst_trigger_control_source_new(), bind it to a #GParamSpec and set some
34 * control points by calling gst_timed_value_control_source_set().
36 * All functions are MT-safe.
39 #include <glib-object.h>
42 #include "gsttriggercontrolsource.h"
43 #include "gst/glib-compat-private.h"
44 #include "gst/math-compat.h"
46 #define GST_CAT_DEFAULT controller_debug
47 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
49 struct _GstTriggerControlSourcePrivate
54 /* control point accessors */
56 /* returns the default value of the property, except for times with specific values */
57 /* needed for one-shot events, such as notes and triggers */
60 _interpolate_trigger (GstTimedValueControlSource * self, GSequenceIter * iter,
61 GstClockTime timestamp)
64 gint64 tolerance = ((GstTriggerControlSource *) self)->priv->tolerance;
65 gboolean found = FALSE;
67 cp = g_sequence_get (iter);
68 if (GST_CLOCK_DIFF (cp->timestamp, timestamp) <= tolerance) {
71 if ((iter = g_sequence_iter_next (iter)) && !g_sequence_iter_is_end (iter)) {
72 cp = g_sequence_get (iter);
73 if (GST_CLOCK_DIFF (timestamp, cp->timestamp) <= tolerance) {
85 interpolate_trigger_get (GstTimedValueControlSource * self,
86 GstClockTime timestamp, gdouble * value)
91 g_mutex_lock (&self->lock);
94 gst_timed_value_control_source_find_control_point_iter (self, timestamp);
96 *value = _interpolate_trigger (self, iter, timestamp);
100 g_mutex_unlock (&self->lock);
105 interpolate_trigger_get_value_array (GstTimedValueControlSource * self,
106 GstClockTime timestamp, GstClockTime interval, guint n_values,
109 gboolean ret = FALSE;
111 GstClockTime ts = timestamp;
112 GstClockTime next_ts = 0;
114 GSequenceIter *iter1 = NULL, *iter2 = NULL;
115 gboolean triggered = FALSE;
117 g_mutex_lock (&self->lock);
118 for (i = 0; i < n_values; i++) {
121 iter1 = gst_timed_value_control_source_find_control_point_iter (self, ts);
123 if (G_LIKELY (self->values))
124 iter2 = g_sequence_get_begin_iter (self->values);
128 iter2 = g_sequence_iter_next (iter1);
131 if (iter2 && !g_sequence_iter_is_end (iter2)) {
134 cp = g_sequence_get (iter2);
135 next_ts = cp->timestamp;
137 next_ts = GST_CLOCK_TIME_NONE;
141 val = _interpolate_trigger (self, iter1, ts);
145 g_mutex_unlock (&self->lock);
149 } else if (triggered) {
151 val = _interpolate_trigger (self, iter1, ts);
155 g_mutex_unlock (&self->lock);
164 g_mutex_unlock (&self->lock);
174 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "trigger control source", 0, \
175 "timeline value trigger control source")
177 G_DEFINE_TYPE_WITH_CODE (GstTriggerControlSource, gst_trigger_control_source,
178 GST_TYPE_TIMED_VALUE_CONTROL_SOURCE, _do_init);
181 * gst_trigger_control_source_new:
183 * This returns a new, unbound #GstTriggerControlSource.
185 * Returns: (transfer full): a new, unbound #GstTriggerControlSource.
188 gst_trigger_control_source_new (void)
190 return g_object_newv (GST_TYPE_TRIGGER_CONTROL_SOURCE, 0, NULL);
194 gst_trigger_control_source_init (GstTriggerControlSource * self)
196 GstControlSource *csource = (GstControlSource *) self;
199 G_TYPE_INSTANCE_GET_PRIVATE (self, GST_TYPE_TRIGGER_CONTROL_SOURCE,
200 GstTriggerControlSourcePrivate);
202 csource->get_value = (GstControlSourceGetValue) interpolate_trigger_get;
203 csource->get_value_array = (GstControlSourceGetValueArray)
204 interpolate_trigger_get_value_array;
208 gst_trigger_control_source_set_property (GObject * object, guint prop_id,
209 const GValue * value, GParamSpec * pspec)
211 GstTriggerControlSource *self = GST_TRIGGER_CONTROL_SOURCE (object);
215 GST_TIMED_VALUE_CONTROL_SOURCE_LOCK (self);
216 self->priv->tolerance = g_value_get_int64 (value);
217 GST_TIMED_VALUE_CONTROL_SOURCE_UNLOCK (self);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gst_trigger_control_source_get_property (GObject * object, guint prop_id,
227 GValue * value, GParamSpec * pspec)
229 GstTriggerControlSource *self = GST_TRIGGER_CONTROL_SOURCE (object);
233 g_value_set_int64 (value, self->priv->tolerance);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gst_trigger_control_source_class_init (GstTriggerControlSourceClass * klass)
244 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
246 g_type_class_add_private (klass, sizeof (GstTriggerControlSourcePrivate));
248 gobject_class->set_property = gst_trigger_control_source_set_property;
249 gobject_class->get_property = gst_trigger_control_source_get_property;
251 g_object_class_install_property (gobject_class, PROP_TOLERANCE,
252 g_param_spec_int64 ("tolerance", "Tolerance",
253 "Amount of ns a control time can be off to still trigger",
254 0, G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));