From 9d44d0b135205041fbcc9c2195e0ed33ff65a153 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 16 Nov 2018 16:52:04 +0100 Subject: [PATCH] parser: add some utils to parse values --- spa/include/spa/pod/parser.h | 39 +++++++++++++++++++++++++++ spa/plugins/audioconvert/channelmix.c | 4 +-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/spa/include/spa/pod/parser.h b/spa/include/spa/pod/parser.h index 29cda7594..169a85644 100644 --- a/spa/include/spa/pod/parser.h +++ b/spa/include/spa/pod/parser.h @@ -320,6 +320,45 @@ static inline int spa_pod_parser_get(struct spa_pod_parser *parser, spa_pod_parser_get(&__p, "{", ##__VA_ARGS__, NULL); \ }) +static inline int spa_pod_is_bool(struct spa_pod *pod) +{ + return (SPA_POD_TYPE(pod) == SPA_TYPE_Bool && SPA_POD_BODY_SIZE(pod) >= sizeof(int32_t)); +} + +static inline int spa_pod_get_bool(struct spa_pod *pod, bool *value) +{ + if (!spa_pod_is_bool(pod)) + return -EINVAL; + *value = SPA_POD_VALUE(struct spa_pod_bool, pod); + return 0; +} + +static inline int spa_pod_is_float(struct spa_pod *pod) +{ + return (SPA_POD_TYPE(pod) == SPA_TYPE_Float && SPA_POD_BODY_SIZE(pod) >= sizeof(float)); +} + +static inline int spa_pod_get_float(struct spa_pod *pod, float *value) +{ + if (!spa_pod_is_float(pod)) + return -EINVAL; + *value = SPA_POD_VALUE(struct spa_pod_float, pod); + return 0; +} + +static inline int spa_pod_is_double(struct spa_pod *pod) +{ + return (SPA_POD_TYPE(pod) == SPA_TYPE_Double && SPA_POD_BODY_SIZE(pod) >= sizeof(double)); +} + +static inline int spa_pod_get_double(struct spa_pod *pod, double *value) +{ + if (!spa_pod_is_double(pod)) + return -EINVAL; + *value = SPA_POD_VALUE(struct spa_pod_double, pod); + return 0; +} + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/spa/plugins/audioconvert/channelmix.c b/spa/plugins/audioconvert/channelmix.c index 0b0de4c1c..666909708 100644 --- a/spa/plugins/audioconvert/channelmix.c +++ b/spa/plugins/audioconvert/channelmix.c @@ -517,10 +517,10 @@ static int apply_props(struct impl *this, const struct spa_pod *param) SPA_POD_OBJECT_FOREACH(obj, prop) { switch (prop->key) { case SPA_PROP_volume: - p->volume = SPA_POD_VALUE(struct spa_pod_float, &prop->value); + spa_pod_get_float(&prop->value, &p->volume); break; case SPA_PROP_mute: - p->mute = SPA_POD_VALUE(struct spa_pod_bool, &prop->value); + spa_pod_get_bool(&prop->value, &p->mute); break; default: break;