From d3a1b9fe31ba5978fa50299921fd4481f125dba2 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 1 Mar 2022 20:32:58 +0100 Subject: [PATCH] spa: add dynamic builder helper Resizes the buffer dynamically. Be careful with getting the address of a pod in the buffer, it might not be valid after building more stuff with the builder. --- spa/include/spa/pod/dynamic.h | 81 +++++++++++++++++++++++++++++++ src/modules/module-filter-chain.c | 36 +++++--------- 2 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 spa/include/spa/pod/dynamic.h diff --git a/spa/include/spa/pod/dynamic.h b/spa/include/spa/pod/dynamic.h new file mode 100644 index 000000000..4d265e021 --- /dev/null +++ b/spa/include/spa/pod/dynamic.h @@ -0,0 +1,81 @@ +/* Simple Plugin API + * + * Copyright © 2018 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef SPA_POD_DYNAMIC_H +#define SPA_POD_DYNAMIC_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct spa_pod_dynamic_builder { + struct spa_pod_builder b; + void *data; + uint32_t extend; + uint32_t _padding; +}; + +static int spa_pod_dynamic_builder_overflow(void *data, uint32_t size) +{ + struct spa_pod_dynamic_builder *d = data; + int32_t old_size = d->b.size; + int32_t new_size = SPA_ROUND_UP_N(size, d->extend); + void *old_data = d->b.data; + + if (old_data == d->data) + d->b.data = NULL; + if ((d->b.data = realloc(d->b.data, new_size)) == NULL) + return -errno; + if (old_data == d->data && d->b.data != old_data && old_size > 0) + memcpy(d->b.data, old_data, old_size); + d->b.size = new_size; + return 0; +} + +static inline void spa_pod_dynamic_builder_init(struct spa_pod_dynamic_builder *builder, + void *data, uint32_t size, uint32_t extend) +{ + static const struct spa_pod_builder_callbacks spa_pod_dynamic_builder_callbacks = { + SPA_VERSION_POD_BUILDER_CALLBACKS, + .overflow = spa_pod_dynamic_builder_overflow + }; + builder->b = SPA_POD_BUILDER_INIT(data, size); + spa_pod_builder_set_callbacks(&builder->b, &spa_pod_dynamic_builder_callbacks, builder); + builder->extend = extend; + builder->data = data; +} + +static inline void spa_pod_dynamic_builder_clean(struct spa_pod_dynamic_builder *builder) +{ + if (builder->data != builder->b.data) + free(builder->b.data); +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* SPA_POD_DYNAMIC_H */ diff --git a/src/modules/module-filter-chain.c b/src/modules/module-filter-chain.c index bf2ecdb01..8e7b0f699 100644 --- a/src/modules/module-filter-chain.c +++ b/src/modules/module-filter-chain.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include @@ -613,26 +614,12 @@ static const struct pw_stream_events out_stream_events = { .param_changed = param_changed }; -static int builder_overflow(void *data, uint32_t size) -{ - struct spa_pod_builder *b = data; - b->size = SPA_ROUND_UP_N(size, 4096); - if ((b->data = realloc(b->data, b->size)) == NULL) - return -errno; - return 0; -} - -static const struct spa_pod_builder_callbacks builder_callbacks = { - SPA_VERSION_POD_BUILDER_CALLBACKS, - .overflow = builder_overflow -}; - static int setup_streams(struct impl *impl) { int res; uint32_t i, n_params; const struct spa_pod *params[256]; - struct spa_pod_builder b; + struct spa_pod_dynamic_builder b; struct graph *graph = &impl->graph; impl->capture = pw_stream_new(impl->core, @@ -656,16 +643,15 @@ static int setup_streams(struct impl *impl) &out_stream_events, impl); n_params = 0; - spa_pod_builder_init(&b, NULL, 0); - spa_pod_builder_set_callbacks(&b, &builder_callbacks, &b); + spa_pod_dynamic_builder_init(&b, NULL, 0, 4096); - params[n_params++] = spa_format_audio_raw_build(&b, + params[n_params++] = spa_format_audio_raw_build(&b.b, SPA_PARAM_EnumFormat, &impl->capture_info); for (i = 0; i < graph->n_control; i++) - params[n_params++] = get_prop_info(graph, &b, i); + params[n_params++] = get_prop_info(graph, &b.b, i); - params[n_params++] = get_props_param(graph, &b); + params[n_params++] = get_props_param(graph, &b.b); res = pw_stream_connect(impl->capture, PW_DIRECTION_INPUT, @@ -674,14 +660,14 @@ static int setup_streams(struct impl *impl) PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS, params, n_params); - free(b.data); + + spa_pod_dynamic_builder_clean(&b); if (res < 0) return res; n_params = 0; - spa_pod_builder_init(&b, NULL, 0); - spa_pod_builder_set_callbacks(&b, &builder_callbacks, &b); - params[n_params++] = spa_format_audio_raw_build(&b, + spa_pod_dynamic_builder_init(&b, NULL, 0, 4096); + params[n_params++] = spa_format_audio_raw_build(&b.b, SPA_PARAM_EnumFormat, &impl->playback_info); res = pw_stream_connect(impl->playback, @@ -692,7 +678,7 @@ static int setup_streams(struct impl *impl) PW_STREAM_FLAG_RT_PROCESS | PW_STREAM_FLAG_TRIGGER, params, n_params); - free(b.data); + spa_pod_dynamic_builder_clean(&b); if (res < 0) return res;