From b314547702953a82ec5cacbd7b716b654a8cd728 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 25 Jul 2019 12:19:41 +0200 Subject: [PATCH] videoconvert: beginnings of video converter --- meson_options.txt | 4 + spa/include/spa/utils/names.h | 11 + spa/plugins/meson.build | 3 + spa/plugins/videoconvert/meson.build | 14 + spa/plugins/videoconvert/plugin.c | 46 ++ spa/plugins/videoconvert/videoadapter.c | 1004 +++++++++++++++++++++++ src/modules/module-adapter/adapter.c | 143 +++- 7 files changed, 1183 insertions(+), 42 deletions(-) create mode 100644 spa/plugins/videoconvert/meson.build create mode 100644 spa/plugins/videoconvert/plugin.c create mode 100644 spa/plugins/videoconvert/videoadapter.c diff --git a/meson_options.txt b/meson_options.txt index 7a05c06db..76a7f8dc5 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -74,6 +74,10 @@ option('v4l2', description: 'Enable v4l2 spa plugin integration', type: 'boolean', value: true) +option('videoconvert', + description: 'Enable videoconvert spa plugin integration', + type: 'boolean', + value: true) option('videotestsrc', description: 'Enable videotestsrc spa plugin integration', type: 'boolean', diff --git a/spa/include/spa/utils/names.h b/spa/include/spa/utils/names.h index 7e3d7868c..8293f72e1 100644 --- a/spa/include/spa/utils/names.h +++ b/spa/include/spa/utils/names.h @@ -65,6 +65,17 @@ extern "C" { #define SPA_NAME_AUDIO_ADAPT "audio.adapt" /**< combination of a node and an * audio.convert. Does clock slaving */ +/** video processing */ +#define SPA_NAME_VIDEO_PROCESS_FORMAT "video.process.format" /**< processes raw video from one format + * to another */ +#define SPA_NAME_VIDEO_PROCESS_SCALE "video.process.scale" /**< scales raw video */ + +/** video convert combines some of the video processing */ +#define SPA_NAME_VIDEO_CONVERT "video.convert" /**< converts raw video from one format + * to another. Must include at least + * format and scaling */ +#define SPA_NAME_VIDEO_ADAPT "video.adapt" /**< combination of a node and a + * video.convert. */ /** keys for alsa factory names */ #define SPA_NAME_API_ALSA_MONITOR "api.alsa.monitor" /**< an alsa Monitor interface */ #define SPA_NAME_API_ALSA_DEVICE "api.alsa.device" /**< an alsa Device interface */ diff --git a/spa/plugins/meson.build b/spa/plugins/meson.build index 090830132..f2948f6a6 100644 --- a/spa/plugins/meson.build +++ b/spa/plugins/meson.build @@ -22,6 +22,9 @@ endif if get_option('test') subdir('test') endif +if get_option('videoconvert') + subdir('videoconvert') +endif if get_option('videotestsrc') subdir('videotestsrc') endif diff --git a/spa/plugins/videoconvert/meson.build b/spa/plugins/videoconvert/meson.build new file mode 100644 index 000000000..2bed99e38 --- /dev/null +++ b/spa/plugins/videoconvert/meson.build @@ -0,0 +1,14 @@ +videoconvert_sources = ['videoadapter.c', + 'plugin.c'] + +simd_cargs = [] +simd_dependencies = [] + +videoconvertlib = shared_library('spa-videoconvert', + videoconvert_sources, + c_args : simd_cargs, + include_directories : [spa_inc], + dependencies : [ mathlib ], + link_with : simd_dependencies, + install : true, + install_dir : '@0@/spa/videoconvert/'.format(get_option('libdir'))) diff --git a/spa/plugins/videoconvert/plugin.c b/spa/plugins/videoconvert/plugin.c new file mode 100644 index 000000000..0f24495a5 --- /dev/null +++ b/spa/plugins/videoconvert/plugin.c @@ -0,0 +1,46 @@ +/* Spa videoconvert plugin + * + * 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. + */ + +#include + +#include + +extern const struct spa_handle_factory spa_videoadapter_factory; + +SPA_EXPORT +int spa_handle_factory_enum(const struct spa_handle_factory **factory, uint32_t *index) +{ + spa_return_val_if_fail(factory != NULL, -EINVAL); + spa_return_val_if_fail(index != NULL, -EINVAL); + + switch (*index) { + case 0: + *factory = &spa_videoadapter_factory; + break; + default: + return 0; + } + (*index)++; + return 1; +} diff --git a/spa/plugins/videoconvert/videoadapter.c b/spa/plugins/videoconvert/videoadapter.c new file mode 100644 index 000000000..74e26fb55 --- /dev/null +++ b/spa/plugins/videoconvert/videoadapter.c @@ -0,0 +1,1004 @@ +/* SPA + * + * Copyright © 2019 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. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NAME "videoadapter" + +/** \cond */ + +struct impl { + struct spa_handle handle; + struct spa_node node; + + struct spa_log *log; + + enum spa_direction direction; + + struct spa_node *target; + struct spa_hook target_listener; + + struct spa_node *slave; + struct spa_hook slave_listener; + uint32_t slave_flags; + + struct spa_handle *hnd_convert; + struct spa_node *convert; + + uint32_t convert_flags; + + uint32_t n_buffers; + struct spa_buffer **buffers; + + struct spa_io_buffers io_buffers; + struct spa_io_rate_match io_rate_match; + + uint64_t info_all; + struct spa_node_info info; + struct spa_param_info params[5]; + + struct spa_hook_list hooks; + struct spa_callbacks callbacks; + + unsigned int use_converter:1; + unsigned int started:1; + unsigned int active:1; + unsigned int driver:1; + unsigned int master:1; + unsigned int monitor:1; +}; + +/** \endcond */ + +static int impl_node_enum_params(void *object, int seq, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct impl *this = object; + struct spa_pod *param; + struct spa_pod_builder b = { 0 }; + uint8_t buffer[1024]; + struct spa_result_node_params result; + uint32_t count = 0; + int res; + + spa_return_val_if_fail(this != NULL, -EINVAL); + spa_return_val_if_fail(num != 0, -EINVAL); + + result.id = id; + result.next = start; +next: + result.index = result.next++; + + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + + switch (id) { + case SPA_PARAM_PropInfo: + case SPA_PARAM_Props: + if ((res = spa_node_enum_params_sync(this->target, + id, &start, filter, ¶m, &b)) != 1) + return res; + break; + + case SPA_PARAM_EnumFormat: + case SPA_PARAM_Format: + if ((res = spa_node_port_enum_params_sync(this->slave, + this->direction, 0, + id, &start, filter, ¶m, &b)) != 1) + return res; + break; + + default: + return -ENOENT; + } + + if (spa_pod_filter(&b, &result.param, param, filter) < 0) + goto next; + + spa_node_emit_result(&this->hooks, seq, 0, SPA_RESULT_TYPE_NODE_PARAMS, &result); + + if (++count != num) + goto next; + + return 0; +} + +#if 0 +static int link_io(struct impl *this) +{ + int res; + + if (!this->use_converter) + return 0; + + spa_log_warn(this->log, NAME " %p: controls", this); + + spa_zero(this->io_rate_match); + this->io_rate_match.rate = 1.0; + + if ((res = spa_node_port_set_io(this->slave, + this->direction, 0, + SPA_IO_RateMatch, + &this->io_rate_match, sizeof(this->io_rate_match))) < 0) { + spa_log_warn(this->log, NAME " %p: set RateMatch on slave failed %d %s", this, + res, spa_strerror(res)); + } + else if ((res = spa_node_port_set_io(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_IO_RateMatch, + &this->io_rate_match, sizeof(this->io_rate_match))) < 0) { + spa_log_warn(this->log, NAME " %p: set RateMatch on convert failed %d %s", this, + res, spa_strerror(res)); + } + + spa_zero(this->io_buffers); + + if ((res = spa_node_port_set_io(this->slave, + this->direction, 0, + SPA_IO_Buffers, + &this->io_buffers, sizeof(this->io_buffers))) < 0) { + spa_log_warn(this->log, NAME " %p: set Buffers on slave failed %d %s", this, + res, spa_strerror(res)); + return res; + } + else if ((res = spa_node_port_set_io(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_IO_Buffers, + &this->io_buffers, sizeof(this->io_buffers))) < 0) { + spa_log_warn(this->log, NAME " %p: set Buffers on convert failed %d %s", this, + res, spa_strerror(res)); + return res; + } + return 0; +} +#endif + +static void emit_node_info(struct impl *this, bool full) +{ + if (full) + this->info.change_mask = this->info_all; + + if (this->info.change_mask) { + struct spa_dict_item items[1]; + + this->info.change_mask |= SPA_NODE_CHANGE_MASK_PROPS; + items[0] = SPA_DICT_ITEM_INIT(SPA_KEY_NODE_DRIVER, this->driver ? "1" : "0"); + this->info.props = &SPA_DICT_INIT(items, 1); + + spa_node_emit_info(&this->hooks, &this->info); + this->info.change_mask = 0; + } +} + +static int impl_node_set_param(void *object, uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + int res = 0; + struct impl *this = object; + + spa_log_debug(this->log, NAME" %p: set param %d", this, id); + + switch (id) { + case SPA_PARAM_Profile: + if (this->started) + return -EIO; + if (this->target != this->slave) { + if ((res = spa_node_set_param(this->target, id, flags, param)) < 0) + return res; + } + break; + case SPA_PARAM_Props: + if (this->target != this->slave) { + if ((res = spa_node_set_param(this->target, id, flags, param)) < 0) + return res; + + this->info.change_mask = SPA_NODE_CHANGE_MASK_PARAMS; + this->params[2].flags ^= SPA_PARAM_INFO_SERIAL; + emit_node_info(this, false); + } + break; + default: + res = -ENOTSUP; + break; + } + return res; +} + +static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size) +{ + struct impl *this = object; + int res = 0; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + if (this->target) + res = spa_node_set_io(this->target, id, data, size); + + if (this->target != this->slave) + res = spa_node_set_io(this->slave, id, data, size); + + return res; +} + +static int impl_node_send_command(void *object, const struct spa_command *command) +{ + struct impl *this = object; + int res; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + switch (SPA_NODE_COMMAND_ID(command)) { + case SPA_NODE_COMMAND_Start: + this->started = true; + break; + case SPA_NODE_COMMAND_Pause: + this->started = false; + break; + default: + break; + } + + if ((res = spa_node_send_command(this->target, command)) < 0) { + spa_log_error(this->log, NAME " %p: can't start convert: %s", + this, spa_strerror(res)); + return res; + } + + if (this->target != this->slave) { + if ((res = spa_node_send_command(this->slave, command)) < 0) { + spa_log_error(this->log, NAME " %p: can't start slave: %s", + this, spa_strerror(res)); + return res; + } + } + return res; +} + +static void target_port_info(void *data, + enum spa_direction direction, uint32_t port_id, + const struct spa_port_info *info) +{ + struct impl *this = data; + + if (direction != this->direction) { + if (port_id == 0) + return; + else + port_id--; + } + + spa_log_trace(this->log, NAME" %p: port info %d:%d", this, + direction, port_id); + + spa_node_emit_port_info(&this->hooks, direction, port_id, info); +} + +static void target_result(void *data, int seq, int res, uint32_t type, const void *result) +{ + struct impl *this = data; + spa_log_trace(this->log, NAME" %p: result %d %d", this, seq, res); + spa_node_emit_result(&this->hooks, seq, res, type, result); +} + +static const struct spa_node_events target_node_events = { + SPA_VERSION_NODE_EVENTS, + .port_info = target_port_info, + .result = target_result, +}; + +static void slave_info(void *data, const struct spa_node_info *info) +{ + struct impl *this = data; + const char *str; + + if (info->max_input_ports > 0) + this->direction = SPA_DIRECTION_INPUT; + else + this->direction = SPA_DIRECTION_OUTPUT; + + spa_log_debug(this->log, NAME" %p: slave info %s", this, + this->direction == SPA_DIRECTION_INPUT ? + "Input" : "Output"); + + if (info->props) { + if ((str = spa_dict_lookup(info->props, SPA_KEY_NODE_DRIVER)) != NULL) + this->driver = strcmp(str, "true") == 0 || atoi(str) == 1; + } +} + +static const struct spa_node_events slave_node_events = { + SPA_VERSION_NODE_EVENTS, + .info = slave_info, +}; + +static int slave_ready(void *data, int status) +{ + struct impl *this = data; + + spa_log_trace(this->log, NAME " %p: ready %d", this, status); + + if (this->direction == SPA_DIRECTION_OUTPUT) + status = spa_node_process(this->convert); + + return spa_node_call_ready(&this->callbacks, status); +} + +static int slave_reuse_buffer(void *data, uint32_t port_id, uint32_t buffer_id) +{ + int res; + struct impl *this = data; + + if (this->use_converter) + res = spa_node_port_reuse_buffer(this->convert, port_id, buffer_id); + else + res = spa_node_call_reuse_buffer(&this->callbacks, port_id, buffer_id); + + return res; +} + +static const struct spa_node_callbacks slave_node_callbacks = { + SPA_VERSION_NODE_CALLBACKS, + .ready = slave_ready, + .reuse_buffer = slave_reuse_buffer, +}; + +static int impl_node_add_listener(void *object, + struct spa_hook *listener, + const struct spa_node_events *events, + void *data) +{ + struct impl *this = object; + struct spa_hook l; + struct spa_hook_list save; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + spa_log_trace(this->log, NAME" %p: add listener %p", this, listener); + spa_hook_list_isolate(&this->hooks, &save, listener, events, data); + + emit_node_info(this, true); + + if (this->use_converter) { + spa_zero(l); + spa_node_add_listener(this->convert, &l, &target_node_events, this); + spa_hook_remove(&l); + } + + spa_hook_list_join(&this->hooks, &save); + + return 0; +} + +static int +impl_node_set_callbacks(void *object, + const struct spa_node_callbacks *callbacks, + void *data) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + this->callbacks = SPA_CALLBACKS_INIT(callbacks, data); + + return 0; +} + +static int +impl_node_sync(void *object, int seq) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + return spa_node_sync(this->slave, seq); +} + +static int +impl_node_add_port(void *object, enum spa_direction direction, uint32_t port_id, + const struct spa_dict *props) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + if (direction != this->direction) + return -EINVAL; + + return spa_node_add_port(this->target, direction, port_id, props); +} + +static int +impl_node_remove_port(void *object, enum spa_direction direction, uint32_t port_id) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + if (direction != this->direction) + return -EINVAL; + + return spa_node_remove_port(this->target, direction, port_id); +} + +static int +impl_node_port_enum_params(void *object, int seq, + enum spa_direction direction, uint32_t port_id, + uint32_t id, uint32_t start, uint32_t num, + const struct spa_pod *filter) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + spa_return_val_if_fail(num != 0, -EINVAL); + + if (direction != this->direction) + port_id++; + + spa_log_debug(this->log, NAME" %p: %d %u", this, seq, id); + + return spa_node_port_enum_params(this->target, seq, direction, port_id, id, + start, num, filter); +} + +static int debug_params(struct impl *this, struct spa_node *node, + enum spa_direction direction, uint32_t port_id, uint32_t id, struct spa_pod *filter, + const char *debug, int err) +{ + struct spa_pod_builder b = { 0 }; + uint8_t buffer[4096]; + uint32_t state; + struct spa_pod *param; + int res; + + spa_log_error(this->log, "params %s: %d:%d (%s) %s", + spa_debug_type_find_name(spa_type_param, id), + direction, port_id, debug, spa_strerror(err)); + + state = 0; + while (true) { + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + res = spa_node_port_enum_params_sync(node, + direction, port_id, + id, &state, + NULL, ¶m, &b); + if (res != 1) { + if (res < 0) + spa_log_error(this->log, " error: %s", spa_strerror(res)); + break; + } + spa_debug_pod(2, NULL, param); + } + + spa_log_error(this->log, "failed filter:"); + if (filter) + spa_debug_pod(2, NULL, filter); + + return 0; +} + + +static int negotiate_format(struct impl *this) +{ + uint32_t state; + struct spa_pod *format; + uint8_t buffer[4096]; + struct spa_pod_builder b = { 0 }; + int res; + + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + + spa_log_debug(this->log, NAME "%p: negiotiate", this); + + state = 0; + format = NULL; + if ((res = spa_node_port_enum_params_sync(this->slave, + this->direction, 0, + SPA_PARAM_EnumFormat, &state, + format, &format, &b)) < 0) { + debug_params(this, this->slave, this->direction, 0, + SPA_PARAM_EnumFormat, format, "slave format", res); + return -ENOTSUP; + } + + state = 0; + if ((res = spa_node_port_enum_params_sync(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_PARAM_EnumFormat, &state, + format, &format, &b)) != 1) { + debug_params(this, this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_PARAM_EnumFormat, format, "convert format", res); + return -ENOTSUP; + } + + spa_pod_fixate(format); + if (spa_log_level_enabled(this->log, SPA_LOG_LEVEL_DEBUG)) + spa_debug_format(0, NULL, format); + + if ((res = spa_node_port_set_param(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_PARAM_Format, 0, + format)) < 0) + return res; + + if ((res = spa_node_port_set_param(this->slave, + this->direction, 0, + SPA_PARAM_Format, 0, + format)) < 0) + return res; + + return res; +} + +static int negotiate_buffers(struct impl *this) +{ + uint8_t buffer[4096]; + struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer)); + uint32_t state; + struct spa_pod *param; + int res, i; + bool slave_alloc, conv_alloc; + int32_t size, buffers, blocks, align, flags; + uint32_t *aligns; + struct spa_data *datas; + uint32_t slave_flags, conv_flags; + + spa_log_debug(this->log, "%p: %d", this, this->n_buffers); + + if (this->n_buffers > 0) + return 0; + + state = 0; + param = NULL; + if ((res = spa_node_port_enum_params_sync(this->slave, + this->direction, 0, + SPA_PARAM_Buffers, &state, + param, ¶m, &b)) < 0) { + debug_params(this, this->slave, this->direction, 0, + SPA_PARAM_Buffers, param, "slave buffers", res); + return -ENOTSUP; + } + + state = 0; + if ((res = spa_node_port_enum_params_sync(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_PARAM_Buffers, &state, + param, ¶m, &b)) != 1) { + debug_params(this, this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + SPA_PARAM_Buffers, param, "convert buffers", res); + return -ENOTSUP; + } + + spa_pod_fixate(param); + + slave_flags = this->slave_flags; + conv_flags = this->convert_flags; + + slave_alloc = SPA_FLAG_CHECK(slave_flags, SPA_PORT_FLAG_CAN_ALLOC_BUFFERS); + conv_alloc = SPA_FLAG_CHECK(conv_flags, SPA_PORT_FLAG_CAN_ALLOC_BUFFERS); + + flags = 0; + if (conv_alloc || slave_alloc) { + flags |= SPA_BUFFER_ALLOC_FLAG_NO_DATA; + if (conv_alloc) + slave_alloc = false; + } + + if ((res = spa_pod_parse_object(param, + SPA_TYPE_OBJECT_ParamBuffers, NULL, + SPA_PARAM_BUFFERS_buffers, SPA_POD_Int(&buffers), + SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(&blocks), + SPA_PARAM_BUFFERS_size, SPA_POD_Int(&size), + SPA_PARAM_BUFFERS_align, SPA_POD_Int(&align))) < 0) + return res; + + spa_log_debug(this->log, "%p: buffers %d, blocks %d, size %d, align %d %d:%d", + this, buffers, blocks, size, align, slave_alloc, conv_alloc); + + datas = alloca(sizeof(struct spa_data) * blocks); + memset(datas, 0, sizeof(struct spa_data) * blocks); + aligns = alloca(sizeof(uint32_t) * blocks); + for (i = 0; i < blocks; i++) { + datas[i].type = SPA_DATA_MemPtr; + datas[i].flags = SPA_DATA_FLAG_DYNAMIC; + datas[i].maxsize = size; + aligns[i] = align; + } + + free(this->buffers); + this->buffers = spa_buffer_alloc_array(buffers, flags, 0, NULL, blocks, datas, aligns); + if (this->buffers == NULL) + return -errno; + this->n_buffers = buffers; + + if (conv_alloc) { + if ((res = spa_node_port_alloc_buffers(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + NULL, 0, + this->buffers, &this->n_buffers)) < 0) + return res; + } + else { + if ((res = spa_node_port_use_buffers(this->convert, + SPA_DIRECTION_REVERSE(this->direction), 0, + this->buffers, this->n_buffers)) < 0) + return res; + } + if (slave_alloc) { + if ((res = spa_node_port_alloc_buffers(this->slave, + this->direction, 0, + NULL, 0, + this->buffers, &this->n_buffers)) < 0) { + return res; + } + } + else { + if ((res = spa_node_port_use_buffers(this->slave, + this->direction, 0, + this->buffers, this->n_buffers)) < 0) { + return res; + } + } + return 0; +} + +static int +impl_node_port_set_param(void *object, + enum spa_direction direction, uint32_t port_id, + uint32_t id, uint32_t flags, + const struct spa_pod *param) +{ + struct impl *this = object; + int res; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + spa_log_debug(this->log, " %d %d %d %d", port_id, id, direction, this->direction); + + if (direction != this->direction) + port_id++; + + if ((res = spa_node_port_set_param(this->target, direction, port_id, id, + flags, param)) < 0) + return res; + + if (id == SPA_PARAM_Format && this->use_converter) { + if (param == NULL) { + if ((res = spa_node_port_set_param(this->target, + SPA_DIRECTION_REVERSE(direction), 0, + id, 0, NULL)) < 0) + return res; + this->n_buffers = 0; + } + else { + if (port_id == 0) + res = negotiate_format(this); + } + } + return res; +} + +static int +impl_node_port_set_io(void *object, + enum spa_direction direction, + uint32_t port_id, + uint32_t id, + void *data, size_t size) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + spa_log_debug(this->log, "set io %d %d %d %d", port_id, id, direction, this->direction); + + if (direction != this->direction) + port_id++; + + return spa_node_port_set_io(this->target, direction, port_id, id, data, size); +} + +static int +impl_node_port_use_buffers(void *object, + enum spa_direction direction, + uint32_t port_id, + struct spa_buffer **buffers, + uint32_t n_buffers) +{ + struct impl *this = object; + int res; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + if (direction != this->direction) + port_id++; + + if ((res = spa_node_port_use_buffers(this->target, + direction, port_id, buffers, n_buffers)) < 0) + return res; + + + spa_log_debug(this->log, NAME" %p: %d %d:%d", this, + n_buffers, direction, port_id); + + if (n_buffers > 0 && this->use_converter) { + if (port_id == 0) + res = negotiate_buffers(this); + } + return res; +} + +static int +impl_node_port_alloc_buffers(void *object, + enum spa_direction direction, + uint32_t port_id, + struct spa_pod **params, + uint32_t n_params, + struct spa_buffer **buffers, + uint32_t *n_buffers) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + if (direction != this->direction) + port_id++; + + return spa_node_port_alloc_buffers(this->target, direction, port_id, + params, n_params, buffers, n_buffers); +} + +static int +impl_node_port_reuse_buffer(void *object, uint32_t port_id, uint32_t buffer_id) +{ + struct impl *this = object; + + spa_return_val_if_fail(this != NULL, -EINVAL); + + return spa_node_port_reuse_buffer(this->target, port_id, buffer_id); +} + +static int impl_node_process(void *object) +{ + struct impl *this = object; + int status; + + spa_log_trace_fp(this->log, "%p: process convert:%u", + this, this->use_converter); + + if (this->direction == SPA_DIRECTION_INPUT) { + if (this->use_converter) + status = spa_node_process(this->convert); + } + + status = spa_node_process(this->slave); + + if (this->monitor) + status |= SPA_STATUS_HAVE_BUFFER; + + if (this->direction == SPA_DIRECTION_OUTPUT && !this->master) { + if (this->use_converter) + status = spa_node_process(this->convert); + } + return status; +} + +static const struct spa_node_methods impl_node = { + SPA_VERSION_NODE_METHODS, + .add_listener = impl_node_add_listener, + .set_callbacks = impl_node_set_callbacks, + .sync = impl_node_sync, + .enum_params = impl_node_enum_params, + .set_param = impl_node_set_param, + .set_io = impl_node_set_io, + .send_command = impl_node_send_command, + .add_port = impl_node_add_port, + .remove_port = impl_node_remove_port, + .port_enum_params = impl_node_port_enum_params, + .port_set_param = impl_node_port_set_param, + .port_use_buffers = impl_node_port_use_buffers, + .port_alloc_buffers = impl_node_port_alloc_buffers, + .port_set_io = impl_node_port_set_io, + .port_reuse_buffer = impl_node_port_reuse_buffer, + .process = impl_node_process, +}; + +static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface) +{ + struct impl *this; + + spa_return_val_if_fail(handle != NULL, -EINVAL); + spa_return_val_if_fail(interface != NULL, -EINVAL); + + this = (struct impl *) handle; + + if (type == SPA_TYPE_INTERFACE_Node) + *interface = &this->node; + else + return -ENOENT; + + return 0; +} + +static int impl_clear(struct spa_handle *handle) +{ + struct impl *this; + + spa_return_val_if_fail(handle != NULL, -EINVAL); + + this = (struct impl *) handle; + + spa_hook_remove(&this->slave_listener); + spa_node_set_callbacks(this->slave, NULL, NULL); + + if (this->buffers) + free(this->buffers); + this->buffers = NULL; + + return 0; +} + +extern const struct spa_handle_factory spa_videoconvert_factory; + +static size_t +impl_get_size(const struct spa_handle_factory *factory, + const struct spa_dict *params) +{ + size_t size = 0; + +#if 0 + size += spa_handle_factory_get_size(&spa_videoconvert_factory, params); +#endif + size += sizeof(struct impl); + + return size; +} + +static int +impl_init(const struct spa_handle_factory *factory, + struct spa_handle *handle, + const struct spa_dict *info, + const struct spa_support *support, + uint32_t n_support) +{ + struct impl *this; +#if 0 + void *iface; +#endif + const char *str; + uint32_t i; + + spa_return_val_if_fail(factory != NULL, -EINVAL); + spa_return_val_if_fail(handle != NULL, -EINVAL); + + handle->get_interface = impl_get_interface; + handle->clear = impl_clear; + + this = (struct impl *) handle; + + for (i = 0; i < n_support; i++) { + switch (support[i].type) { + case SPA_TYPE_INTERFACE_Log: + this->log = support[i].data; + break; + } + } + if (info == NULL || (str = spa_dict_lookup(info, "video.adapt.slave")) == NULL) + return -EINVAL; + + sscanf(str, "pointer:%p", &this->slave); + if (this->slave == NULL) + return -EINVAL; + + if ((str = spa_dict_lookup(info, "merger.monitor")) != NULL) + this->monitor = atoi(str); + + spa_node_add_listener(this->slave, + &this->slave_listener, &slave_node_events, this); + spa_node_set_callbacks(this->slave, &slave_node_callbacks, this); + + this->node.iface = SPA_INTERFACE_INIT( + SPA_TYPE_INTERFACE_Node, + SPA_VERSION_NODE, + &impl_node, this); + spa_hook_list_init(&this->hooks); + +#if 0 + this->hnd_convert = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle); + spa_handle_factory_init(&spa_videooconvert_factory, + this->hnd_convert, + info, support, n_support); + + spa_handle_get_interface(this->hnd_convert, SPA_TYPE_INTERFACE_Node, &iface); + this->convert = iface; + this->target = this->convert; + spa_node_add_listener(this->convert, + &this->convert_listener, &target_node_events, this); + + this->use_converter = true; + link_io(this); + +#else + this->target = this->slave; + spa_node_add_listener(this->target, + &this->target_listener, &target_node_events, this); + +#endif + + this->info_all = SPA_NODE_CHANGE_MASK_PARAMS; + this->info = SPA_NODE_INFO_INIT(); + this->info.max_input_ports = 0; + this->info.max_output_ports = 0; + this->params[0] = SPA_PARAM_INFO(SPA_PARAM_EnumFormat, SPA_PARAM_INFO_READ); + this->params[1] = SPA_PARAM_INFO(SPA_PARAM_PropInfo, SPA_PARAM_INFO_READ); + this->params[2] = SPA_PARAM_INFO(SPA_PARAM_Props, SPA_PARAM_INFO_READWRITE); + this->params[3] = SPA_PARAM_INFO(SPA_PARAM_Format, SPA_PARAM_INFO_READ); + this->params[4] = SPA_PARAM_INFO(SPA_PARAM_Profile, SPA_PARAM_INFO_WRITE); + this->info.params = this->params; + this->info.n_params = 5; + + return 0; +} + +static const struct spa_interface_info impl_interfaces[] = { + { SPA_TYPE_INTERFACE_Node, }, +}; + +static int +impl_enum_interface_info(const struct spa_handle_factory *factory, + const struct spa_interface_info **info, + uint32_t *index) +{ + spa_return_val_if_fail(factory != NULL, -EINVAL); + spa_return_val_if_fail(info != NULL, -EINVAL); + spa_return_val_if_fail(index != NULL, -EINVAL); + + switch (*index) { + case 0: + *info = &impl_interfaces[*index]; + break; + default: + return 0; + } + (*index)++; + return 1; +} + +const struct spa_handle_factory spa_videoadapter_factory = { + .version = SPA_VERSION_HANDLE_FACTORY, + .name = SPA_NAME_VIDEO_ADAPT, + .get_size = impl_get_size, + .init = impl_init, + .enum_interface_info = impl_enum_interface_info, +}; diff --git a/src/modules/module-adapter/adapter.c b/src/modules/module-adapter/adapter.c index f5c5b3121..4483b732e 100644 --- a/src/modules/module-adapter/adapter.c +++ b/src/modules/module-adapter/adapter.c @@ -31,18 +31,20 @@ #include "config.h" #include +#include #include #include -#include #include -#include +#include +#include +#include #include "pipewire/pipewire.h" #include "pipewire/private.h" #include "modules/spa/spa-node.h" -#define NAME "audioadapter" +#define NAME "adapter" #define PORT_BUFFERS 1 #define MAX_BUFFER_SIZE 2048 @@ -85,7 +87,8 @@ struct node { enum pw_direction direction; struct pw_properties *props; - struct spa_audio_info format; + uint32_t media_type; + uint32_t media_subtype; struct spa_list ports; }; @@ -251,27 +254,29 @@ static void node_port_init(void *data, struct pw_port *port) if (direction != n->direction) return; - p = calloc(1, sizeof(struct port) + + if (n->media_type == SPA_MEDIA_TYPE_audio) { + p = calloc(1, sizeof(struct port) + spa_handle_factory_get_size(&spa_floatmix_factory, NULL)); - p->node = n; - p->port = port; - init_port(p, direction); - p->spa_handle = SPA_MEMBER(p, sizeof(struct port), struct spa_handle); + p->node = n; + p->port = port; + init_port(p, direction); + p->spa_handle = SPA_MEMBER(p, sizeof(struct port), struct spa_handle); - support = pw_core_get_support(n->core, &n_support); - spa_handle_factory_init(&spa_floatmix_factory, - p->spa_handle, NULL, - support, n_support); + support = pw_core_get_support(n->core, &n_support); + spa_handle_factory_init(&spa_floatmix_factory, + p->spa_handle, NULL, + support, n_support); - spa_handle_get_interface(p->spa_handle, SPA_TYPE_INTERFACE_Node, &iface); - p->spa_node = iface; + spa_handle_get_interface(p->spa_handle, SPA_TYPE_INTERFACE_Node, &iface); + p->spa_node = iface; - if (direction == PW_DIRECTION_INPUT) { - pw_log_debug("mix node %p", p->spa_node); - pw_port_set_mix(port, p->spa_node, PW_PORT_MIX_FLAG_MULTI); - port->impl = SPA_CALLBACKS_INIT(&port_implementation, p); + if (direction == PW_DIRECTION_INPUT) { + pw_log_debug("mix node %p", p->spa_node); + pw_port_set_mix(port, p->spa_node, PW_PORT_MIX_FLAG_MULTI); + port->impl = SPA_CALLBACKS_INIT(&port_implementation, p); + } + spa_list_append(&n->ports, &p->link); } - spa_list_append(&n->ports, &p->link); } static const struct pw_node_events node_events = { @@ -281,6 +286,37 @@ static const struct pw_node_events node_events = { .port_init = node_port_init, }; + +static int find_format(struct pw_node *node, enum pw_direction direction, + uint32_t *media_type, uint32_t *media_subtype) +{ + uint32_t state = 0; + uint8_t buffer[4096]; + struct spa_pod_builder b; + int res; + struct spa_pod *format; + + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + if ((res = spa_node_port_enum_params_sync(node->node, + direction == PW_DIRECTION_INPUT ? + SPA_DIRECTION_INPUT : + SPA_DIRECTION_OUTPUT, 0, + SPA_PARAM_EnumFormat, &state, + NULL, &format, &b)) != 1) { + pw_log_warn(NAME " %p: no format given", node); + return -ENOENT; + } + + if ((res = spa_format_parse(format, media_type, media_subtype)) < 0) + return res; + + pw_log_debug(NAME " %p: %s/%s", node, + spa_debug_type_find_name(spa_type_media_type, *media_type), + spa_debug_type_find_name(spa_type_media_subtype, *media_subtype)); + return 0; +} + + struct pw_node *pw_adapter_new(struct pw_core *core, struct pw_node *slave, struct pw_properties *props, @@ -288,23 +324,32 @@ struct pw_node *pw_adapter_new(struct pw_core *core, { struct pw_node *node; struct node *n; - const char *name, *str; + const char *name, *str, *factory_name; const struct pw_node_info *info; enum pw_direction direction; - int res = -ENOENT; + int res; + uint32_t media_type, media_subtype; info = pw_node_get_info(slave); if (info == NULL) { - errno = EINVAL; - return NULL; + res = -EINVAL; + goto error; } + pw_log_debug(NAME " %p: in %d/%d out %d/%d", slave, + info->n_input_ports, info->max_input_ports, + info->n_output_ports, info->max_output_ports); + pw_properties_update(props, info->props); - if (info->n_output_ports == 0) - direction = PW_DIRECTION_INPUT; - else + if (info->n_output_ports > 0) { direction = PW_DIRECTION_OUTPUT; + } else if (info->n_input_ports > 0) { + direction = PW_DIRECTION_INPUT; + } else { + res = -EINVAL; + goto error; + } if ((name = pw_properties_get(props, PW_KEY_NODE_NAME)) == NULL) name = NAME; @@ -321,35 +366,49 @@ struct pw_node *pw_adapter_new(struct pw_core *core, pw_properties_set(props, "factory.mode", str); } - pw_properties_setf(props, "audio.adapt.slave", "pointer:%p", slave->node); - pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "audioconvert/libspa-audioconvert"); - if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL) - pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Audio/%s", - direction == PW_DIRECTION_INPUT ? "Sink" : "Source"); + if ((res = find_format(slave, direction, &media_type, &media_subtype)) < 0) + goto error; + + if (media_type == SPA_MEDIA_TYPE_audio) { + pw_properties_setf(props, "audio.adapt.slave", "pointer:%p", slave->node); + pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "audioconvert/libspa-audioconvert"); + if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL) + pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Audio/%s", + direction == PW_DIRECTION_INPUT ? "Sink" : "Source"); + factory_name = SPA_NAME_AUDIO_ADAPT; + } + else if (media_type == SPA_MEDIA_TYPE_video) { + pw_properties_setf(props, "video.adapt.slave", "pointer:%p", slave->node); + pw_properties_set(props, SPA_KEY_LIBRARY_NAME, "videoconvert/libspa-videoconvert"); + if (pw_properties_get(props, PW_KEY_MEDIA_CLASS) == NULL) + pw_properties_setf(props, PW_KEY_MEDIA_CLASS, "Video/%s", + direction == PW_DIRECTION_INPUT ? "Sink" : "Source"); + factory_name = SPA_NAME_VIDEO_ADAPT; + } else { + res = -ENOTSUP; + goto error; + } node = pw_spa_node_load(core, NULL, NULL, - SPA_NAME_AUDIO_ADAPT, - name, - PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER, - pw_properties_copy(props), - sizeof(struct node) + user_data_size); - + factory_name, + name, + PW_SPA_NODE_FLAG_ACTIVATE | PW_SPA_NODE_FLAG_NO_REGISTER, + pw_properties_copy(props), + sizeof(struct node) + user_data_size); if (node == NULL) { res = -errno; pw_log_error("can't load spa node: %m"); goto error; } - pw_log_debug(NAME " %p: in %d/%d out %d/%d", node, - info->n_input_ports, info->max_input_ports, - info->n_output_ports, info->max_output_ports); - n = pw_spa_node_get_user_data(node); n->core = core; n->node = node; n->slave = slave; n->direction = direction; n->props = props; + n->media_type = media_type; + n->media_subtype = media_subtype; spa_list_init(&n->ports); if (user_data_size > 0)