Commit b23977cb authored by Stefan Sauer's avatar Stefan Sauer

controller: allow different controlbindings

Make controlbinding an abstract baseclass. Move implementation to control-
binding-direct and add a control-binding-argb. Add an example.
parent 2c8c4497
......@@ -596,11 +596,9 @@ GST_USING_PRINTF_EXTENSION
GstControlBinding
GstControlBindingClass
GstControlBindingConvert
gst_control_binding_new
gst_control_binding_sync_values
gst_control_binding_get_value
gst_control_binding_get_value_array
gst_control_binding_get_control_source
gst_control_binding_set_disabled
gst_control_binding_is_disabled
<SUBSECTION Standard>
......
......@@ -38,23 +38,16 @@
#define GST_CAT_DEFAULT control_binding_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
static void gst_control_binding_dispose (GObject * object);
static void gst_control_binding_finalize (GObject * object);
#define _do_init \
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbinding", 0, \
"dynamic parameter control source attachment");
G_DEFINE_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstControlBinding, gst_control_binding,
GST_TYPE_OBJECT, _do_init);
static void
gst_control_binding_class_init (GstControlBindingClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = gst_control_binding_dispose;
gobject_class->finalize = gst_control_binding_finalize;
}
static void
......@@ -62,162 +55,6 @@ gst_control_binding_init (GstControlBinding * self)
{
}
static void
gst_control_binding_dispose (GObject * object)
{
GstControlBinding *self = GST_CONTROL_BINDING (object);
if (self->csource) {
g_object_unref (self->csource);
self->csource = NULL;
}
}
static void
gst_control_binding_finalize (GObject * object)
{
GstControlBinding *self = GST_CONTROL_BINDING (object);
g_value_unset (&self->cur_value);
}
/* mapping functions */
#define DEFINE_CONVERT(type,Type,TYPE) \
static void \
convert_to_##type (GstControlBinding *self, gdouble s, GValue *d) \
{ \
GParamSpec##Type *pspec = G_PARAM_SPEC_##TYPE (self->pspec); \
g##type v; \
\
s = CLAMP (s, 0.0, 1.0); \
v = pspec->minimum + (g##type) ((pspec->maximum - pspec->minimum) * s); \
g_value_set_##type (d, v); \
}
DEFINE_CONVERT (int, Int, INT);
DEFINE_CONVERT (uint, UInt, UINT);
DEFINE_CONVERT (long, Long, LONG);
DEFINE_CONVERT (ulong, ULong, ULONG);
DEFINE_CONVERT (int64, Int64, INT64);
DEFINE_CONVERT (uint64, UInt64, UINT64);
DEFINE_CONVERT (float, Float, FLOAT);
DEFINE_CONVERT (double, Double, DOUBLE);
static void
convert_to_boolean (GstControlBinding * self, gdouble s, GValue * d)
{
s = CLAMP (s, 0.0, 1.0);
g_value_set_boolean (d, (gboolean) (s + 0.5));
}
static void
convert_to_enum (GstControlBinding * self, gdouble s, GValue * d)
{
GParamSpecEnum *pspec = G_PARAM_SPEC_ENUM (self->pspec);
GEnumClass *e = pspec->enum_class;
gint v;
s = CLAMP (s, 0.0, 1.0);
v = s * (e->n_values - 1);
g_value_set_enum (d, e->values[v].value);
}
/**
* gst_control_binding_new:
* @object: the object of the property
* @property_name: the property-name to attach the control source
* @csource: the control source
*
* Create a new control-binding that attaches the #GstControlSource to the
* #GObject property.
*
* Returns: the new #GstControlBinding
*/
GstControlBinding *
gst_control_binding_new (GstObject * object, const gchar * property_name,
GstControlSource * csource)
{
GstControlBinding *self = NULL;
GParamSpec *pspec;
GST_INFO_OBJECT (object, "trying to put property '%s' under control",
property_name);
/* check if the object has a property of that name */
if ((pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
property_name))) {
GST_DEBUG_OBJECT (object, " psec->flags : 0x%08x", pspec->flags);
/* check if this param is witable && controlable && !construct-only */
g_return_val_if_fail ((pspec->flags & (G_PARAM_WRITABLE |
GST_PARAM_CONTROLLABLE | G_PARAM_CONSTRUCT_ONLY)) ==
(G_PARAM_WRITABLE | GST_PARAM_CONTROLLABLE), NULL);
if ((self = (GstControlBinding *) g_object_newv (GST_TYPE_CONTROL_BINDING,
0, NULL))) {
GType type = G_PARAM_SPEC_VALUE_TYPE (pspec), base;
// add pspec as a construction parameter and move below to construct()
self->pspec = pspec;
self->name = pspec->name;
self->csource = g_object_ref (csource);
self->disabled = FALSE;
g_value_init (&self->cur_value, type);
base = type = G_PARAM_SPEC_VALUE_TYPE (pspec);
while ((type = g_type_parent (type)))
base = type;
GST_DEBUG_OBJECT (object, " using type %s", g_type_name (base));
// select mapping function
// FIXME: only select mapping if super class hasn't set any?
switch (base) {
case G_TYPE_INT:
self->convert = convert_to_int;
break;
case G_TYPE_UINT:
self->convert = convert_to_uint;
break;
case G_TYPE_LONG:
self->convert = convert_to_long;
break;
case G_TYPE_ULONG:
self->convert = convert_to_ulong;
break;
case G_TYPE_INT64:
self->convert = convert_to_int64;
break;
case G_TYPE_UINT64:
self->convert = convert_to_uint64;
break;
case G_TYPE_FLOAT:
self->convert = convert_to_float;
break;
case G_TYPE_DOUBLE:
self->convert = convert_to_double;
break;
case G_TYPE_BOOLEAN:
self->convert = convert_to_boolean;
break;
case G_TYPE_ENUM:
self->convert = convert_to_enum;
break;
default:
// FIXME: return NULL?
GST_WARNING ("incomplete implementation for paramspec type '%s'",
G_PARAM_SPEC_TYPE_NAME (pspec));
break;
}
}
} else {
GST_WARNING_OBJECT (object, "class '%s' has no property '%s'",
G_OBJECT_TYPE_NAME (object), property_name);
}
return self;
}
/* functions */
/**
......@@ -240,41 +77,22 @@ gboolean
gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
GstClockTime timestamp, GstClockTime last_sync)
{
GValue *dst_val;
gdouble src_val;
gboolean ret;
GstControlBindingClass *klass;
gboolean ret = FALSE;
g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
if (self->disabled)
return TRUE;
GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
self->name, GST_TIME_ARGS (timestamp));
klass = GST_CONTROL_BINDING_GET_CLASS (self);
dst_val = &self->cur_value;
ret = gst_control_source_get_value (self->csource, timestamp, &src_val);
if (G_LIKELY (ret)) {
GST_LOG_OBJECT (object, " new value %lf", src_val);
/* always set the value for first time, but then only if it changed
* this should limit g_object_notify invocations.
* FIXME: can we detect negative playback rates?
*/
if ((timestamp < last_sync) || (src_val != self->last_value)) {
GST_LOG_OBJECT (object, " mapping %s to value of type %s", self->name,
G_VALUE_TYPE_NAME (dst_val));
/* run mapping function to convert gdouble to GValue */
self->convert (self, src_val, dst_val);
/* we can make this faster
* http://bugzilla.gnome.org/show_bug.cgi?id=536939
*/
g_object_set_property ((GObject *) object, self->name, dst_val);
self->last_value = src_val;
}
if (G_LIKELY (klass->sync_values != NULL)) {
ret = klass->sync_values (self, object, timestamp, last_sync);
} else {
GST_DEBUG_OBJECT (object, "no control value for param %s", self->name);
GST_WARNING_OBJECT (self, "missing sync_values implementation");
}
return (ret);
return ret;
}
/**
......@@ -290,23 +108,20 @@ gst_control_binding_sync_values (GstControlBinding * self, GstObject * object,
GValue *
gst_control_binding_get_value (GstControlBinding * self, GstClockTime timestamp)
{
GValue *dst_val = NULL;
gdouble src_val;
GstControlBindingClass *klass;
GValue *ret = NULL;
g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
/* get current value via control source */
if (gst_control_source_get_value (self->csource, timestamp, &src_val)) {
dst_val = g_new0 (GValue, 1);
g_value_init (dst_val, G_PARAM_SPEC_VALUE_TYPE (self->pspec));
self->convert (self, src_val, dst_val);
klass = GST_CONTROL_BINDING_GET_CLASS (self);
if (G_LIKELY (klass->get_value != NULL)) {
ret = klass->get_value (self, timestamp);
} else {
GST_LOG ("no control value for property %s at ts %" GST_TIME_FORMAT,
self->name, GST_TIME_ARGS (timestamp));
GST_WARNING_OBJECT (self, "missing get_value implementation");
}
return dst_val;
return ret;
}
/**
......@@ -331,52 +146,22 @@ gst_control_binding_get_value_array (GstControlBinding * self,
GstClockTime timestamp, GstClockTime interval, guint n_values,
GValue * values)
{
gint i;
gdouble *src_val;
gboolean res = FALSE;
GType type;
GstControlBindingConvert convert;
GstControlBindingClass *klass;
gboolean ret = FALSE;
g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), FALSE);
g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), FALSE);
g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (interval), FALSE);
g_return_val_if_fail (values, FALSE);
convert = self->convert;
type = G_PARAM_SPEC_VALUE_TYPE (self->pspec);
klass = GST_CONTROL_BINDING_GET_CLASS (self);
src_val = g_new0 (gdouble, n_values);
if ((res = gst_control_source_get_value_array (self->csource, timestamp,
interval, n_values, src_val))) {
for (i = 0; i < n_values; i++) {
if (!isnan (src_val[i])) {
g_value_init (&values[i], type);
convert (self, src_val[i], &values[i]);
} else {
GST_LOG ("no control value for property %s at index %d", self->name, i);
}
}
if (G_LIKELY (klass->get_value_array != NULL)) {
ret = klass->get_value_array (self, timestamp, interval, n_values, values);
} else {
GST_LOG ("failed to get control value for property %s at ts %"
GST_TIME_FORMAT, self->name, GST_TIME_ARGS (timestamp));
GST_WARNING_OBJECT (self, "missing get_value_array implementation");
}
g_free (src_val);
return res;
}
/**
* gst_control_binding_get_control_source:
* @self: the control binding
*
* Get the control source.
*
* Returns: the control source. Unref when done with it.
*/
GstControlSource *
gst_control_binding_get_control_source (GstControlBinding * self)
{
g_return_val_if_fail (GST_IS_CONTROL_BINDING (self), NULL);
return g_object_ref (self->csource);
return ret;
}
/**
......
......@@ -68,15 +68,10 @@ struct _GstControlBinding {
/*< public >*/
const gchar *name; /* name of the property */
GParamSpec *pspec; /* GParamSpec for this property */
/*< private >*/
GParamSpec *pspec; /* GParamSpec for this property */
GstControlSource *csource; /* GstControlSource for this property */
gboolean disabled;
GValue cur_value;
gdouble last_value;
GstControlBindingConvert convert;
gpointer _gst_reserved[GST_PADDING];
};
......@@ -92,12 +87,11 @@ struct _GstControlBinding {
struct _GstControlBindingClass
{
GstObjectClass parent_class;
/* need vfuncs for:
_sync_values
_get_value
_get_value_array
*/
/* virtual methods */
gboolean (* sync_values) (GstControlBinding *self, GstObject *object, GstClockTime timestamp, GstClockTime last_sync);
GValue * (* get_value) (GstControlBinding *self, GstClockTime timestamp);
gboolean (* get_value_array) (GstControlBinding *self, GstClockTime timestamp,GstClockTime interval, guint n_values, GValue *values);
/*< private >*/
gpointer _gst_reserved[GST_PADDING];
......@@ -107,16 +101,13 @@ GType gst_control_binding_get_type (void);
/* Functions */
GstControlBinding * gst_control_binding_new (GstObject * object, const gchar * property_name,
GstControlSource * csource);
gboolean gst_control_binding_sync_values (GstControlBinding * self, GstObject *object,
GstClockTime timestamp, GstClockTime last_sync);
GValue * gst_control_binding_get_value (GstControlBinding *binding,
GstClockTime timestamp);
gboolean gst_control_binding_get_value_array (GstControlBinding *binding, GstClockTime timestamp,
GstClockTime interval, guint n_values, GValue *values);
GstControlSource * gst_control_binding_get_control_source (GstControlBinding * self);
void gst_control_binding_set_disabled (GstControlBinding * self, gboolean disabled);
gboolean gst_control_binding_is_disabled (GstControlBinding * self);
G_END_DECLS
......
......@@ -117,7 +117,7 @@
* </para></listitem>
* <listitem><para>
* Attach the #GstControlSource on the controller to a property.
* gst_object_add_control_binding (object, gst_control_binding_new (objetct, "prop1", csource));
* gst_object_add_control_binding (object, gst_control_binding_direct_new (objetct, "prop1", csource));
* </para></listitem>
* <listitem><para>
* Set the control values
......@@ -1222,8 +1222,8 @@ gst_object_add_control_binding (GstObject * object, GstControlBinding * binding)
g_return_val_if_fail (GST_IS_OBJECT (object), FALSE);
g_return_val_if_fail (GST_IS_CONTROL_BINDING (binding), FALSE);
g_return_val_if_fail (g_type_is_a (binding->pspec->owner_type,
G_OBJECT_TYPE (object)), FALSE);
//g_return_val_if_fail (g_type_is_a (binding->pspec->owner_type,
// G_OBJECT_TYPE (object)), FALSE);
GST_OBJECT_LOCK (object);
if ((old = gst_object_find_control_binding (object, binding->name))) {
......
......@@ -2,12 +2,16 @@ lib_LTLIBRARIES = libgstcontroller-@GST_MAJORMINOR@.la
libgstcontroller_@GST_MAJORMINOR@_includedir = $(includedir)/gstreamer-@GST_MAJORMINOR@/gst/controller
libgstcontroller_@GST_MAJORMINOR@_include_HEADERS = \
gstcontrolbindingargb.h \
gstcontrolbindingdirect.h \
gsttimedvaluecontrolsource.h \
gstinterpolationcontrolsource.h \
gsttriggercontrolsource.h \
gstlfocontrolsource.h
libgstcontroller_@GST_MAJORMINOR@_la_SOURCES = \
gstcontrolbindingargb.c \
gstcontrolbindingdirect.c \
gsttimedvaluecontrolsource.c \
gstinterpolationcontrolsource.c \
gsttriggercontrolsource.c \
......
/* GStreamer
*
* Copyright (C) 2011 Stefan Sauer <ensonic@users.sf.net>
*
* gstcontrolbindingargb.c: Attachment for multiple control sources to gargb
* properties
*
* 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.
*
* 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.
*/
/**
* SECTION:gstcontrolbindingargb
* @short_description: attachment for control source sources to argb properties
*
* A value mapping object that attaches multiple control sources to a guint
* gobject properties representing a color.
*/
#include <glib-object.h>
#include <gst/gst.h>
#include "gstcontrolbindingargb.h"
#include <math.h>
#define GST_CAT_DEFAULT control_binding_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
static void gst_control_binding_argb_dispose (GObject * object);
static void gst_control_binding_argb_finalize (GObject * object);
static gboolean gst_control_binding_argb_sync_values (GstControlBinding * _self,
GstObject * object, GstClockTime timestamp, GstClockTime last_sync);
static GValue *gst_control_binding_argb_get_value (GstControlBinding * _self,
GstClockTime timestamp);
static gboolean gst_control_binding_argb_get_value_array (GstControlBinding *
_self, GstClockTime timestamp, GstClockTime interval, guint n_values,
GValue * values);
#define _do_init \
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gstcontrolbindingargb", 0, \
"dynamic parameter control source attachment");
G_DEFINE_TYPE_WITH_CODE (GstControlBindingARGB, gst_control_binding_argb,
GST_TYPE_CONTROL_BINDING, _do_init);
static void
gst_control_binding_argb_class_init (GstControlBindingARGBClass * klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GstControlBindingClass *control_binding_class =
GST_CONTROL_BINDING_CLASS (klass);
gobject_class->dispose = gst_control_binding_argb_dispose;
gobject_class->finalize = gst_control_binding_argb_finalize;
control_binding_class->sync_values = gst_control_binding_argb_sync_values;
control_binding_class->get_value = gst_control_binding_argb_get_value;
control_binding_class->get_value_array =
gst_control_binding_argb_get_value_array;
}
static void
gst_control_binding_argb_init (GstControlBindingARGB * self)
{
}
static void
gst_control_binding_argb_dispose (GObject * object)
{
GstControlBindingARGB *self = GST_CONTROL_BINDING_ARGB (object);
if (self->cs_a)
gst_object_replace ((GstObject **) & self->cs_a, NULL);
if (self->cs_r)
gst_object_replace ((GstObject **) & self->cs_r, NULL);
if (self->cs_g)
gst_object_replace ((GstObject **) & self->cs_g, NULL);
if (self->cs_b)
gst_object_replace ((GstObject **) & self->cs_b, NULL);
}
static void
gst_control_binding_argb_finalize (GObject * object)
{
GstControlBindingARGB *self = GST_CONTROL_BINDING_ARGB (object);
g_value_unset (&self->cur_value);
}
static gboolean
gst_control_binding_argb_sync_values (GstControlBinding * _self,
GstObject * object, GstClockTime timestamp, GstClockTime last_sync)
{
GstControlBindingARGB *self = GST_CONTROL_BINDING_ARGB (_self);
gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
gboolean ret = TRUE;
g_return_val_if_fail (GST_IS_CONTROL_BINDING_ARGB (self), FALSE);
GST_LOG_OBJECT (object, "property '%s' at ts=%" GST_TIME_FORMAT,
_self->name, GST_TIME_ARGS (timestamp));
if (self->cs_a)
ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
if (self->cs_r)
ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
if (self->cs_g)
ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
if (self->cs_b)
ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
if (G_LIKELY (ret)) {
guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
(((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
(((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
GST_LOG_OBJECT (object, " new value 0x%08x", src_val);
/* always set the value for first time, but then only if it changed
* this should limit g_object_notify invocations.
* FIXME: can we detect negative playback rates?
*/
if ((timestamp < last_sync) || (src_val != self->last_value)) {
GValue *dst_val = &self->cur_value;
g_value_set_uint (dst_val, src_val);
/* we can make this faster
* http://bugzilla.gnome.org/show_bug.cgi?id=536939
*/
g_object_set_property ((GObject *) object, _self->name, dst_val);
self->last_value = src_val;
}
} else {
GST_DEBUG_OBJECT (object, "no control value for param %s", _self->name);
}
return (ret);
}
static GValue *
gst_control_binding_argb_get_value (GstControlBinding * _self,
GstClockTime timestamp)
{
GstControlBindingARGB *self = GST_CONTROL_BINDING_ARGB (_self);
GValue *dst_val = NULL;
gdouble src_val_a = 1.0, src_val_r = 0.0, src_val_g = 0.0, src_val_b = 0.0;
gboolean ret = TRUE;
g_return_val_if_fail (GST_IS_CONTROL_BINDING_ARGB (self), NULL);
g_return_val_if_fail (GST_CLOCK_TIME_IS_VALID (timestamp), NULL);
/* get current value via control source */
if (self->cs_a)
ret &= gst_control_source_get_value (self->cs_a, timestamp, &src_val_a);
if (self->cs_r)
ret &= gst_control_source_get_value (self->cs_r, timestamp, &src_val_r);
if (self->cs_g)
ret &= gst_control_source_get_value (self->cs_g, timestamp, &src_val_g);
if (self->cs_b)
ret &= gst_control_source_get_value (self->cs_b, timestamp, &src_val_b);
if (G_LIKELY (ret)) {
guint src_val = (((guint) (CLAMP (src_val_a, 0.0, 1.0) * 255)) << 24) |
(((guint) (CLAMP (src_val_r, 0.0, 1.0) * 255)) << 16) |
(((guint) (CLAMP (src_val_g, 0.0, 1.0) * 255)) << 8) |
((guint) (CLAMP (src_val_b, 0.0, 1.0) * 255));
dst_val = g_new0 (GValue, 1);
g_value_init (dst_val, G_TYPE_UINT);
g_value_set_uint (dst_val, src_val);
} else {