mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-12-17 08:56:49 -05:00
module-echo-cancel: Move backends to dynamic libaries
Move all backends to dynamic libaries loaded with spa_plugin_loader so new backends not needs changes in pipewire or pipewire dependency to external code Change-Id: I702ce047598d0c318d6dc6ac8248062a5c12f643
This commit is contained in:
parent
761199be70
commit
9386c70b3a
11 changed files with 576 additions and 247 deletions
|
|
@ -3,6 +3,7 @@ spa_sections = [
|
||||||
'control',
|
'control',
|
||||||
'debug',
|
'debug',
|
||||||
'graph',
|
'graph',
|
||||||
|
'interfaces',
|
||||||
'monitor',
|
'monitor',
|
||||||
'node',
|
'node',
|
||||||
'param',
|
'param',
|
||||||
|
|
|
||||||
|
|
@ -22,43 +22,23 @@
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "echo-cancel.h"
|
|
||||||
|
|
||||||
struct impl {
|
#include <spa/utils/dict.h>
|
||||||
uint32_t channels;
|
#include <spa/utils/hook.h>
|
||||||
|
#include <spa/pod/pod.h>
|
||||||
|
#include <spa/param/audio/raw.h>
|
||||||
|
#include <spa/support/plugin.h>
|
||||||
|
|
||||||
|
#define SPA_TYPE_INTERFACE_AEC SPA_TYPE_INFO_INTERFACE_BASE "AEC"
|
||||||
|
#define SPA_VERSION_AUDIO_AEC 1
|
||||||
|
|
||||||
|
struct echo_cancel_info {
|
||||||
|
struct spa_interface iface;
|
||||||
|
const char *name;
|
||||||
|
const struct spa_dict info;
|
||||||
|
const char *latency;
|
||||||
|
int (*create) (struct spa_handle *handle, const struct spa_dict *args, const struct spa_audio_info_raw *info);
|
||||||
|
int (*run) (struct spa_handle *handle, const float *rec[], const float *play[], float *out[], uint32_t n_samples);
|
||||||
|
struct spa_dict *(*get_properties) (struct spa_handle *handle);
|
||||||
|
int (*set_properties) (struct spa_handle *handle, const struct spa_dict *args);
|
||||||
};
|
};
|
||||||
|
|
||||||
static void *null_create(const struct pw_properties *args, const struct spa_audio_info_raw *info)
|
|
||||||
{
|
|
||||||
struct impl *impl;
|
|
||||||
impl = calloc(1, sizeof(struct impl));
|
|
||||||
impl->channels = info->channels;
|
|
||||||
return impl;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void null_destroy(void *ec)
|
|
||||||
{
|
|
||||||
free(ec);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int null_run(void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
|
|
||||||
{
|
|
||||||
struct impl *impl = ec;
|
|
||||||
uint32_t i;
|
|
||||||
for (i = 0; i < impl->channels; i++)
|
|
||||||
memcpy(out[i], rec[i], n_samples * sizeof(float));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct echo_cancel_info echo_cancel_null_impl = {
|
|
||||||
.name = "null",
|
|
||||||
.info = SPA_DICT_INIT(NULL, 0),
|
|
||||||
.latency = NULL,
|
|
||||||
|
|
||||||
.create = null_create,
|
|
||||||
.destroy = null_destroy,
|
|
||||||
|
|
||||||
.run = null_run,
|
|
||||||
};
|
|
||||||
|
|
||||||
const struct echo_cancel_info *echo_cancel_null = &echo_cancel_null_impl;
|
|
||||||
|
|
@ -82,6 +82,8 @@ extern "C" {
|
||||||
#define SPA_NAME_AUDIO_ADAPT "audio.adapt" /**< combination of a node and an
|
#define SPA_NAME_AUDIO_ADAPT "audio.adapt" /**< combination of a node and an
|
||||||
* audio.convert. Does clock slaving */
|
* audio.convert. Does clock slaving */
|
||||||
|
|
||||||
|
#define SPA_NAME_AEC "audio.aec" /**< Echo canceling */
|
||||||
|
|
||||||
/** video processing */
|
/** video processing */
|
||||||
#define SPA_NAME_VIDEO_PROCESS_FORMAT "video.process.format" /**< processes raw video from one format
|
#define SPA_NAME_VIDEO_PROCESS_FORMAT "video.process.format" /**< processes raw video from one format
|
||||||
* to another */
|
* to another */
|
||||||
|
|
|
||||||
185
spa/plugins/aec/aec-null.c
Normal file
185
spa/plugins/aec/aec-null.c
Normal file
|
|
@ -0,0 +1,185 @@
|
||||||
|
/* PipeWire
|
||||||
|
*
|
||||||
|
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <spa/interfaces/audio/aec.h>
|
||||||
|
#include <spa/support/log.h>
|
||||||
|
#include <spa/utils/string.h>
|
||||||
|
#include <spa/utils/names.h>
|
||||||
|
#include <spa/support/plugin.h>
|
||||||
|
|
||||||
|
struct impl {
|
||||||
|
struct spa_handle handle;
|
||||||
|
struct spa_log *log;
|
||||||
|
uint32_t channels;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.aec.null");
|
||||||
|
#undef SPA_LOG_TOPIC_DEFAULT
|
||||||
|
#define SPA_LOG_TOPIC_DEFAULT &log_topic
|
||||||
|
|
||||||
|
static int null_create(struct spa_handle *handle, const struct spa_dict *args, const struct spa_audio_info_raw *info)
|
||||||
|
{
|
||||||
|
struct impl *impl;
|
||||||
|
impl = (struct impl *) handle;
|
||||||
|
impl->channels = info->channels;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int null_run(struct spa_handle *handle, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
|
||||||
|
{
|
||||||
|
struct impl *impl = (struct impl *) handle;
|
||||||
|
uint32_t i;
|
||||||
|
for (i = 0; i < impl->channels; i++)
|
||||||
|
memcpy(out[i], rec[i], n_samples * sizeof(float));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct spa_dict *null_get_properties(SPA_UNUSED struct spa_handle *handle)
|
||||||
|
{
|
||||||
|
/* Not supported */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int null_set_properties(SPA_UNUSED struct spa_handle *handle, SPA_UNUSED const struct spa_dict *args)
|
||||||
|
{
|
||||||
|
/* Not supported */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct echo_cancel_info echo_cancel_null_impl = {
|
||||||
|
.name = "null",
|
||||||
|
.info = SPA_DICT_INIT(NULL, 0),
|
||||||
|
.latency = NULL,
|
||||||
|
.create = null_create,
|
||||||
|
.run = null_run,
|
||||||
|
.get_properties = null_get_properties,
|
||||||
|
.set_properties = null_set_properties,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||||
|
{
|
||||||
|
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||||
|
|
||||||
|
if (spa_streq(type, SPA_TYPE_INTERFACE_AEC))
|
||||||
|
*interface = &echo_cancel_null_impl;
|
||||||
|
else
|
||||||
|
return -ENOENT;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int impl_clear(struct spa_handle *handle)
|
||||||
|
{
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
impl_get_size(const struct spa_handle_factory *factory,
|
||||||
|
const struct spa_dict *params)
|
||||||
|
{
|
||||||
|
return sizeof(struct impl);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 *impl;
|
||||||
|
|
||||||
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
|
||||||
|
echo_cancel_null_impl.iface = SPA_INTERFACE_INIT(
|
||||||
|
SPA_TYPE_INTERFACE_AEC,
|
||||||
|
SPA_VERSION_AUDIO_AEC,
|
||||||
|
NULL,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
handle->get_interface = impl_get_interface;
|
||||||
|
handle->clear = impl_clear;
|
||||||
|
impl = (struct impl *) handle;
|
||||||
|
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||||
|
spa_log_topic_init(impl->log, &log_topic);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct spa_interface_info impl_interfaces[] = {
|
||||||
|
{SPA_TYPE_INTERFACE_AEC,},
|
||||||
|
};
|
||||||
|
|
||||||
|
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_aec_exaudio_factory = {
|
||||||
|
SPA_VERSION_HANDLE_FACTORY,
|
||||||
|
SPA_NAME_AEC,
|
||||||
|
NULL,
|
||||||
|
impl_get_size,
|
||||||
|
impl_init,
|
||||||
|
impl_enum_interface_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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_aec_exaudio_factory;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
(*index)++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
286
spa/plugins/aec/aec-webrtc.cpp
Normal file
286
spa/plugins/aec/aec-webrtc.cpp
Normal file
|
|
@ -0,0 +1,286 @@
|
||||||
|
/* PipeWire
|
||||||
|
*
|
||||||
|
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
|
||||||
|
* © 2021 Arun Raghavan <arun@asymptotic.io>
|
||||||
|
*
|
||||||
|
* 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 <memory>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <spa/interfaces/audio/aec.h>
|
||||||
|
#include <spa/support/log.h>
|
||||||
|
#include <spa/utils/string.h>
|
||||||
|
#include <spa/utils/names.h>
|
||||||
|
#include <spa/support/plugin.h>
|
||||||
|
|
||||||
|
#include <webrtc/modules/audio_processing/include/audio_processing.h>
|
||||||
|
#include <webrtc/modules/interface/module_common_types.h>
|
||||||
|
#include <webrtc/system_wrappers/include/trace.h>
|
||||||
|
|
||||||
|
struct impl_data {
|
||||||
|
struct spa_handle handle;
|
||||||
|
struct spa_log *log;
|
||||||
|
std::unique_ptr<webrtc::AudioProcessing> apm;
|
||||||
|
spa_audio_info_raw info;
|
||||||
|
std::unique_ptr<float *[]> play_buffer, rec_buffer, out_buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct spa_log_topic log_topic = SPA_LOG_TOPIC(0, "spa.eac.webrtc");
|
||||||
|
#undef SPA_LOG_TOPIC_DEFAULT
|
||||||
|
#define SPA_LOG_TOPIC_DEFAULT &log_topic
|
||||||
|
|
||||||
|
static bool webrtc_get_spa_bool(const struct spa_dict *args, const char *key, bool default_value) {
|
||||||
|
const char *str_val;
|
||||||
|
bool value = default_value;
|
||||||
|
str_val = spa_dict_lookup(args, key);
|
||||||
|
if (str_val != NULL)
|
||||||
|
value =spa_atob(str_val);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int webrtc_create(struct spa_handle *handle, const struct spa_dict *args, const struct spa_audio_info_raw *info)
|
||||||
|
{
|
||||||
|
auto impl = reinterpret_cast<struct impl_data*>(handle);
|
||||||
|
|
||||||
|
bool extended_filter = webrtc_get_spa_bool(args, "webrtc.extended_filter", true);
|
||||||
|
bool delay_agnostic = webrtc_get_spa_bool(args, "webrtc.delay_agnostic", true);
|
||||||
|
bool high_pass_filter = webrtc_get_spa_bool(args, "webrtc.high_pass_filter", true);
|
||||||
|
bool noise_suppression = webrtc_get_spa_bool(args, "webrtc.noise_suppression", true);
|
||||||
|
bool voice_detection = webrtc_get_spa_bool(args, "webrtc.voice_detection", true);
|
||||||
|
|
||||||
|
// Note: AGC seems to mess up with Agnostic Delay Detection, especially with speech,
|
||||||
|
// result in very poor performance, disable by default
|
||||||
|
bool gain_control = webrtc_get_spa_bool(args, "webrtc.gain_control", false);
|
||||||
|
|
||||||
|
// Disable experimental flags by default
|
||||||
|
bool experimental_agc = webrtc_get_spa_bool(args, "webrtc.experimental_agc", false);
|
||||||
|
bool experimental_ns = webrtc_get_spa_bool(args, "webrtc.experimental_ns", false);
|
||||||
|
|
||||||
|
// FIXME: Intelligibility enhancer is not currently supported
|
||||||
|
// This filter will modify playback buffer (when calling ProcessReverseStream), but now
|
||||||
|
// playback buffer modifications are discarded.
|
||||||
|
|
||||||
|
webrtc::Config config;
|
||||||
|
config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(extended_filter));
|
||||||
|
config.Set<webrtc::DelayAgnostic>(new webrtc::DelayAgnostic(delay_agnostic));
|
||||||
|
config.Set<webrtc::ExperimentalAgc>(new webrtc::ExperimentalAgc(experimental_agc));
|
||||||
|
config.Set<webrtc::ExperimentalNs>(new webrtc::ExperimentalNs(experimental_ns));
|
||||||
|
|
||||||
|
webrtc::ProcessingConfig pconfig = {{
|
||||||
|
webrtc::StreamConfig(info->rate, info->channels, false), /* input stream */
|
||||||
|
webrtc::StreamConfig(info->rate, info->channels, false), /* output stream */
|
||||||
|
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse input stream */
|
||||||
|
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse output stream */
|
||||||
|
}};
|
||||||
|
|
||||||
|
auto apm = std::unique_ptr<webrtc::AudioProcessing>(webrtc::AudioProcessing::Create(config));
|
||||||
|
if (apm->Initialize(pconfig) != webrtc::AudioProcessing::kNoError) {
|
||||||
|
spa_log_error(impl->log, "Error initialising webrtc audio processing module");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
apm->high_pass_filter()->Enable(high_pass_filter);
|
||||||
|
// Always disable drift compensation since it requires drift sampling
|
||||||
|
apm->echo_cancellation()->enable_drift_compensation(false);
|
||||||
|
apm->echo_cancellation()->Enable(true);
|
||||||
|
// TODO: wire up supression levels to args
|
||||||
|
apm->echo_cancellation()->set_suppression_level(webrtc::EchoCancellation::kHighSuppression);
|
||||||
|
apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kHigh);
|
||||||
|
apm->noise_suppression()->Enable(noise_suppression);
|
||||||
|
apm->voice_detection()->Enable(voice_detection);
|
||||||
|
// TODO: wire up AGC parameters to args
|
||||||
|
apm->gain_control()->set_analog_level_limits(0, 255);
|
||||||
|
apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveDigital);
|
||||||
|
apm->gain_control()->Enable(gain_control);
|
||||||
|
impl->apm = std::move(apm);
|
||||||
|
impl->info = *info;
|
||||||
|
impl->play_buffer = std::make_unique<float *[]>(info->channels);
|
||||||
|
impl->rec_buffer = std::make_unique<float *[]>(info->channels);
|
||||||
|
impl->out_buffer = std::make_unique<float *[]>(info->channels);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int webrtc_run(struct spa_handle *handle, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
|
||||||
|
{
|
||||||
|
auto impl = reinterpret_cast<struct impl_data*>(handle);
|
||||||
|
webrtc::StreamConfig config =
|
||||||
|
webrtc::StreamConfig(impl->info.rate, impl->info.channels, false);
|
||||||
|
unsigned int num_blocks = n_samples * 1000 / impl->info.rate / 10;
|
||||||
|
|
||||||
|
if (n_samples * 1000 / impl->info.rate % 10 != 0) {
|
||||||
|
spa_log_error(impl->log, "Buffers must be multiples of 10ms in length (currently %u samples)", n_samples);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < num_blocks; i ++) {
|
||||||
|
for (size_t j = 0; j < impl->info.channels; j++) {
|
||||||
|
impl->play_buffer[j] = const_cast<float *>(play[j]) + config.num_frames() * i;
|
||||||
|
impl->rec_buffer[j] = const_cast<float *>(rec[j]) + config.num_frames() * i;
|
||||||
|
impl->out_buffer[j] = out[j] + config.num_frames() * i;
|
||||||
|
}
|
||||||
|
/* FIXME: ProcessReverseStream may change the playback buffer, in which
|
||||||
|
* case we should use that, if we ever expose the intelligibility
|
||||||
|
* enhancer */
|
||||||
|
if (impl->apm->ProcessReverseStream(impl->play_buffer.get(), config, config, impl->play_buffer.get()) !=
|
||||||
|
webrtc::AudioProcessing::kNoError) {
|
||||||
|
spa_log_error(impl->log, "Processing reverse stream failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extra delay introduced by multiple frames
|
||||||
|
impl->apm->set_stream_delay_ms((num_blocks - 1) * 10);
|
||||||
|
|
||||||
|
if (impl->apm->ProcessStream(impl->rec_buffer.get(), config, config, impl->out_buffer.get()) !=
|
||||||
|
webrtc::AudioProcessing::kNoError) {
|
||||||
|
spa_log_error(impl->log, "Processing stream failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct spa_dict *webrtc_get_properties(SPA_UNUSED struct spa_handle *handle)
|
||||||
|
{
|
||||||
|
/* Not supported */
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int webrtc_set_properties(SPA_UNUSED struct spa_handle *handle, SPA_UNUSED const struct spa_dict *args)
|
||||||
|
{
|
||||||
|
/* Not supported */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct echo_cancel_info echo_cancel_webrtc_impl = {
|
||||||
|
.name = "webrtc",
|
||||||
|
.info = SPA_DICT_INIT(NULL, 0),
|
||||||
|
.latency = "480/48000",
|
||||||
|
.create = webrtc_create,
|
||||||
|
.run = webrtc_run,
|
||||||
|
.get_properties = webrtc_get_properties,
|
||||||
|
.set_properties = webrtc_set_properties,
|
||||||
|
};
|
||||||
|
|
||||||
|
static int impl_get_interface(struct spa_handle *handle, const char *type, void **interface)
|
||||||
|
{
|
||||||
|
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
spa_return_val_if_fail(interface != NULL, -EINVAL);
|
||||||
|
|
||||||
|
if (spa_streq(type, SPA_TYPE_INTERFACE_AEC))
|
||||||
|
*interface = &echo_cancel_webrtc_impl;
|
||||||
|
else
|
||||||
|
return -ENOENT;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int impl_clear(struct spa_handle *handle)
|
||||||
|
{
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
auto impl = reinterpret_cast<struct impl_data*>(handle);
|
||||||
|
impl->~impl_data();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
impl_get_size(const struct spa_handle_factory *factory,
|
||||||
|
const struct spa_dict *params)
|
||||||
|
{
|
||||||
|
return sizeof(struct impl_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
spa_return_val_if_fail(factory != NULL, -EINVAL);
|
||||||
|
spa_return_val_if_fail(handle != NULL, -EINVAL);
|
||||||
|
|
||||||
|
echo_cancel_webrtc_impl.iface = SPA_INTERFACE_INIT(
|
||||||
|
SPA_TYPE_INTERFACE_AEC,
|
||||||
|
SPA_VERSION_AUDIO_AEC,
|
||||||
|
NULL,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
auto impl = new (handle) impl_data();
|
||||||
|
impl->handle.get_interface = impl_get_interface;
|
||||||
|
impl->handle.clear = impl_clear;
|
||||||
|
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
|
||||||
|
spa_log_topic_init(impl->log, &log_topic);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct spa_interface_info impl_interfaces[] = {
|
||||||
|
{SPA_TYPE_INTERFACE_AEC,},
|
||||||
|
};
|
||||||
|
|
||||||
|
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_aec_exaudio_factory = {
|
||||||
|
SPA_VERSION_HANDLE_FACTORY,
|
||||||
|
SPA_NAME_AEC,
|
||||||
|
NULL,
|
||||||
|
impl_get_size,
|
||||||
|
impl_init,
|
||||||
|
impl_enum_interface_info,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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_aec_exaudio_factory;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
(*index)++;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
16
spa/plugins/aec/meson.build
Normal file
16
spa/plugins/aec/meson.build
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
aec_null = shared_library('spa-aec-null',
|
||||||
|
[ 'aec-null.c' ],
|
||||||
|
include_directories : [ configinc ],
|
||||||
|
dependencies : [ spa_dep ],
|
||||||
|
install : true,
|
||||||
|
install_dir : spa_plugindir / 'aec')
|
||||||
|
|
||||||
|
if webrtc_dep.found()
|
||||||
|
aec_webrtc = shared_library('spa-aec-webrtc',
|
||||||
|
[ 'aec-webrtc.cpp' ],
|
||||||
|
include_directories : [ configinc ],
|
||||||
|
dependencies : [ spa_dep, webrtc_dep ],
|
||||||
|
install : true,
|
||||||
|
install_dir : spa_plugindir / 'aec')
|
||||||
|
endif
|
||||||
|
|
||||||
|
|
@ -51,3 +51,5 @@ endif
|
||||||
if libcamera_dep.found()
|
if libcamera_dep.found()
|
||||||
subdir('libcamera')
|
subdir('libcamera')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
subdir('aec')
|
||||||
|
|
@ -110,22 +110,15 @@ pipewire_module_filter_chain = shared_library('pipewire-module-filter-chain',
|
||||||
|
|
||||||
pipewire_module_echo_cancel_sources = [
|
pipewire_module_echo_cancel_sources = [
|
||||||
'module-echo-cancel.c',
|
'module-echo-cancel.c',
|
||||||
'module-echo-cancel/aec-null.c',
|
|
||||||
]
|
]
|
||||||
|
|
||||||
if webrtc_dep.found()
|
|
||||||
pipewire_module_echo_cancel_sources += [
|
|
||||||
'module-echo-cancel/aec-webrtc.cpp'
|
|
||||||
]
|
|
||||||
endif
|
|
||||||
|
|
||||||
pipewire_module_echo_cancel = shared_library('pipewire-module-echo-cancel',
|
pipewire_module_echo_cancel = shared_library('pipewire-module-echo-cancel',
|
||||||
pipewire_module_echo_cancel_sources,
|
pipewire_module_echo_cancel_sources,
|
||||||
include_directories : [configinc],
|
include_directories : [configinc],
|
||||||
install : true,
|
install : true,
|
||||||
install_dir : modules_install_dir,
|
install_dir : modules_install_dir,
|
||||||
install_rpath: modules_install_dir,
|
install_rpath: modules_install_dir,
|
||||||
dependencies : [mathlib, dl_lib, pipewire_dep, webrtc_dep],
|
dependencies : [mathlib, dl_lib, pipewire_dep],
|
||||||
)
|
)
|
||||||
|
|
||||||
pipewire_module_profiler = shared_library('pipewire-module-profiler',
|
pipewire_module_profiler = shared_library('pipewire-module-profiler',
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "module-echo-cancel/echo-cancel.h"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
@ -43,10 +44,14 @@
|
||||||
#include <spa/param/audio/raw.h>
|
#include <spa/param/audio/raw.h>
|
||||||
#include <spa/param/profiler.h>
|
#include <spa/param/profiler.h>
|
||||||
#include <spa/pod/builder.h>
|
#include <spa/pod/builder.h>
|
||||||
|
#include <spa/support/plugin.h>
|
||||||
#include <spa/utils/json.h>
|
#include <spa/utils/json.h>
|
||||||
|
#include <spa/utils/names.h>
|
||||||
#include <spa/utils/result.h>
|
#include <spa/utils/result.h>
|
||||||
#include <spa/utils/ringbuffer.h>
|
#include <spa/utils/ringbuffer.h>
|
||||||
#include <spa/utils/string.h>
|
#include <spa/utils/string.h>
|
||||||
|
#include <spa/support/plugin-loader.h>
|
||||||
|
#include <spa/interfaces/audio/aec.h>
|
||||||
|
|
||||||
#include <pipewire/private.h>
|
#include <pipewire/private.h>
|
||||||
#include <pipewire/impl.h>
|
#include <pipewire/impl.h>
|
||||||
|
|
@ -54,8 +59,6 @@
|
||||||
|
|
||||||
#include <pipewire/extensions/profiler.h>
|
#include <pipewire/extensions/profiler.h>
|
||||||
|
|
||||||
#include "module-echo-cancel/echo-cancel.h"
|
|
||||||
|
|
||||||
/** \page page_module_echo_cancel PipeWire Module: Echo Cancel
|
/** \page page_module_echo_cancel PipeWire Module: Echo Cancel
|
||||||
*
|
*
|
||||||
* The `echo-cancel` module performs echo cancellation. The module creates
|
* The `echo-cancel` module performs echo cancellation. The module creates
|
||||||
|
|
@ -68,8 +71,8 @@
|
||||||
*
|
*
|
||||||
* - `source.props = {}`: properties to be passed to the source stream
|
* - `source.props = {}`: properties to be passed to the source stream
|
||||||
* - `sink.props = {}`: properties to be passed to the sink stream
|
* - `sink.props = {}`: properties to be passed to the sink stream
|
||||||
* - `aec.method = <str>`: the echo cancellation method. Currently supported:
|
* - `library.name = <str>`: the echo cancellation library Currently supported:
|
||||||
* `webrtc`. Leave unset to use the default method (`webrtc`).
|
* `aec/libspa-aec-exaudio`. Leave unset to use the default method (`aec/libspa-aec-exaudio`).
|
||||||
* - `aec.args = <str>`: arguments to pass to the echo cancellation method
|
* - `aec.args = <str>`: arguments to pass to the echo cancellation method
|
||||||
*
|
*
|
||||||
* ## General options
|
* ## General options
|
||||||
|
|
@ -94,7 +97,7 @@
|
||||||
* context.modules = [
|
* context.modules = [
|
||||||
* { name = libpipewire-module-echo-cancel
|
* { name = libpipewire-module-echo-cancel
|
||||||
* args = {
|
* args = {
|
||||||
* # aec.method = webrtc
|
* # library.name = aec/libspa-aec-exaudio
|
||||||
* # node.latency = 1024/48000
|
* # node.latency = 1024/48000
|
||||||
* source.props = {
|
* source.props = {
|
||||||
* node.name = "Echo Cancellation Source"
|
* node.name = "Echo Cancellation Source"
|
||||||
|
|
@ -138,7 +141,7 @@ static const struct spa_dict_item module_props[] = {
|
||||||
"[ audio.position=<channel map> ] "
|
"[ audio.position=<channel map> ] "
|
||||||
"[ buffer.max_size=<max buffer size in ms> ] "
|
"[ buffer.max_size=<max buffer size in ms> ] "
|
||||||
"[ buffer.play_delay=<play delay in ms> ] "
|
"[ buffer.play_delay=<play delay in ms> ] "
|
||||||
"[ aec.method=<aec method> ] "
|
"[ library.name =<library name> ] "
|
||||||
"[ aec.args=<aec arguments> ] "
|
"[ aec.args=<aec arguments> ] "
|
||||||
"[ source.props=<properties> ] "
|
"[ source.props=<properties> ] "
|
||||||
"[ sink.props=<properties> ] " },
|
"[ sink.props=<properties> ] " },
|
||||||
|
|
@ -186,7 +189,6 @@ struct impl {
|
||||||
struct spa_ringbuffer out_ring;
|
struct spa_ringbuffer out_ring;
|
||||||
|
|
||||||
const struct echo_cancel_info *aec_info;
|
const struct echo_cancel_info *aec_info;
|
||||||
void *aec;
|
|
||||||
uint32_t aec_blocksize;
|
uint32_t aec_blocksize;
|
||||||
|
|
||||||
unsigned int capture_ready:1;
|
unsigned int capture_ready:1;
|
||||||
|
|
@ -197,6 +199,9 @@ struct impl {
|
||||||
|
|
||||||
uint32_t max_buffer_size;
|
uint32_t max_buffer_size;
|
||||||
uint32_t buffer_delay;
|
uint32_t buffer_delay;
|
||||||
|
|
||||||
|
struct spa_handle *spa_handle;
|
||||||
|
struct spa_plugin_loader *loader;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void do_unload_module(void *obj, void *data, int res, uint32_t id)
|
static void do_unload_module(void *obj, void *data, int res, uint32_t id)
|
||||||
|
|
@ -283,7 +288,7 @@ static void process(struct impl *impl)
|
||||||
pw_stream_queue_buffer(impl->playback, pout);
|
pw_stream_queue_buffer(impl->playback, pout);
|
||||||
|
|
||||||
/* Now run the canceller */
|
/* Now run the canceller */
|
||||||
echo_cancel_run(impl->aec_info, impl->aec, rec, play_delayed, out, size / sizeof(float));
|
echo_cancel_run(impl->aec_info, impl->spa_handle, rec, play_delayed, out, size / sizeof(float));
|
||||||
|
|
||||||
/* Next, copy over the output to the output ringbuffer */
|
/* Next, copy over the output to the output ringbuffer */
|
||||||
avail = spa_ringbuffer_get_write_index(&impl->out_ring, &oindex);
|
avail = spa_ringbuffer_get_write_index(&impl->out_ring, &oindex);
|
||||||
|
|
@ -803,8 +808,8 @@ static void impl_destroy(struct impl *impl)
|
||||||
pw_stream_destroy(impl->sink);
|
pw_stream_destroy(impl->sink);
|
||||||
if (impl->core && impl->do_disconnect)
|
if (impl->core && impl->do_disconnect)
|
||||||
pw_core_disconnect(impl->core);
|
pw_core_disconnect(impl->core);
|
||||||
if (impl->aec)
|
if (impl->spa_handle)
|
||||||
echo_cancel_destroy(impl->aec_info, impl->aec);
|
spa_plugin_loader_unload(impl->loader, impl->spa_handle);
|
||||||
pw_properties_free(impl->source_props);
|
pw_properties_free(impl->source_props);
|
||||||
pw_properties_free(impl->sink_props);
|
pw_properties_free(impl->sink_props);
|
||||||
|
|
||||||
|
|
@ -893,7 +898,10 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
||||||
struct impl *impl;
|
struct impl *impl;
|
||||||
uint32_t id = pw_global_get_id(pw_impl_module_get_global(module));
|
uint32_t id = pw_global_get_id(pw_impl_module_get_global(module));
|
||||||
const char *str;
|
const char *str;
|
||||||
int res;
|
const char *path;
|
||||||
|
int res = 0;
|
||||||
|
struct spa_handle *handle = NULL;
|
||||||
|
void *iface;
|
||||||
|
|
||||||
PW_LOG_TOPIC_INIT(mod_topic);
|
PW_LOG_TOPIC_INIT(mod_topic);
|
||||||
|
|
||||||
|
|
@ -967,22 +975,57 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
||||||
if (pw_properties_get(impl->sink_props, PW_KEY_MEDIA_CLASS) == NULL)
|
if (pw_properties_get(impl->sink_props, PW_KEY_MEDIA_CLASS) == NULL)
|
||||||
pw_properties_set(impl->sink_props, PW_KEY_MEDIA_CLASS, "Audio/Sink");
|
pw_properties_set(impl->sink_props, PW_KEY_MEDIA_CLASS, "Audio/Sink");
|
||||||
|
|
||||||
if ((str = pw_properties_get(props, "aec.method")) == NULL)
|
if ((str = pw_properties_get(props, "aec.method")) != NULL)
|
||||||
str = "webrtc";
|
pw_log_warn("aec.method is not supported anymore use library.name");
|
||||||
|
|
||||||
#ifdef HAVE_WEBRTC
|
/* Use webrtc as default */
|
||||||
if (spa_streq(str, "webrtc"))
|
if ((path = pw_properties_get(props, "library.name")) == NULL)
|
||||||
impl->aec_info = echo_cancel_webrtc;
|
path = "aec/libspa-aec-webrtc";
|
||||||
else
|
|
||||||
#endif
|
struct spa_dict_item info_items[] = {
|
||||||
impl->aec_info = echo_cancel_null;
|
{ SPA_KEY_LIBRARY_NAME, path },
|
||||||
|
};
|
||||||
|
struct spa_dict info = SPA_DICT_INIT_ARRAY(info_items);
|
||||||
|
|
||||||
|
impl->loader = spa_support_find(context->support, context->n_support, SPA_TYPE_INTERFACE_PluginLoader);
|
||||||
|
if (impl->loader == NULL) {
|
||||||
|
pw_log_error("a plugin loader is needed");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
handle = spa_plugin_loader_load(impl->loader, SPA_NAME_AEC, &info);
|
||||||
|
if (handle == NULL) {
|
||||||
|
pw_log_error("AEC codec plugin %s not available library.name %s", SPA_NAME_AEC, path);
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_AEC, &iface)) < 0) {
|
||||||
|
pw_log_error("can't get %s interface %d", SPA_TYPE_INTERFACE_AEC, res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
impl->aec_info = iface;
|
||||||
|
impl->spa_handle = handle;
|
||||||
|
if (impl->aec_info->iface.version != SPA_VERSION_AUDIO_AEC) {
|
||||||
|
pw_log_error("codec plugin %s has incompatible ABI version (%d != %d)",
|
||||||
|
SPA_NAME_AEC, impl->aec_info->iface.version, SPA_VERSION_AUDIO_AEC);
|
||||||
|
res = -ENOENT;
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void)SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_AEC, (struct echo_cancel_info *)impl->aec_info);
|
||||||
|
|
||||||
|
pw_log_info("Using plugin AEC %s", impl->aec_info->name);
|
||||||
|
|
||||||
if ((str = pw_properties_get(props, "aec.args")) != NULL)
|
if ((str = pw_properties_get(props, "aec.args")) != NULL)
|
||||||
aec_props = pw_properties_new_string(str);
|
aec_props = pw_properties_new_string(str);
|
||||||
else
|
else
|
||||||
aec_props = pw_properties_new(NULL, NULL);
|
aec_props = pw_properties_new(NULL, NULL);
|
||||||
|
|
||||||
impl->aec = echo_cancel_create(impl->aec_info, aec_props, &impl->info);
|
if (echo_cancel_create(impl->aec_info, impl->spa_handle, &aec_props->dict, &impl->info)) {
|
||||||
|
pw_log_error("codec plugin %s create failed", impl->aec_info->name);
|
||||||
|
res = -ENOENT;
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
pw_properties_free(aec_props);
|
pw_properties_free(aec_props);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,163 +0,0 @@
|
||||||
/* PipeWire
|
|
||||||
*
|
|
||||||
* Copyright © 2021 Wim Taymans <wim.taymans@gmail.com>
|
|
||||||
* © 2021 Arun Raghavan <arun@asymptotic.io>
|
|
||||||
*
|
|
||||||
* 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 <memory>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "echo-cancel.h"
|
|
||||||
|
|
||||||
#include <pipewire/pipewire.h>
|
|
||||||
|
|
||||||
#include <webrtc/modules/audio_processing/include/audio_processing.h>
|
|
||||||
#include <webrtc/modules/interface/module_common_types.h>
|
|
||||||
#include <webrtc/system_wrappers/include/trace.h>
|
|
||||||
|
|
||||||
struct impl {
|
|
||||||
std::unique_ptr<webrtc::AudioProcessing> apm;
|
|
||||||
spa_audio_info_raw info;
|
|
||||||
std::unique_ptr<float *[]> play_buffer, rec_buffer, out_buffer;
|
|
||||||
|
|
||||||
impl(std::unique_ptr<webrtc::AudioProcessing> apm, const spa_audio_info_raw& info)
|
|
||||||
: apm(std::move(apm)),
|
|
||||||
info(info),
|
|
||||||
play_buffer(std::make_unique<float *[]>(info.channels)),
|
|
||||||
rec_buffer(std::make_unique<float *[]>(info.channels)),
|
|
||||||
out_buffer(std::make_unique<float *[]>(info.channels))
|
|
||||||
{ }
|
|
||||||
};
|
|
||||||
|
|
||||||
static void *webrtc_create(const struct pw_properties *args, const spa_audio_info_raw *info)
|
|
||||||
{
|
|
||||||
bool extended_filter = pw_properties_get_bool(args, "webrtc.extended_filter", true);
|
|
||||||
bool delay_agnostic = pw_properties_get_bool(args, "webrtc.delay_agnostic", true);
|
|
||||||
bool high_pass_filter = pw_properties_get_bool(args, "webrtc.high_pass_filter", true);
|
|
||||||
bool noise_suppression = pw_properties_get_bool(args, "webrtc.noise_suppression", true);
|
|
||||||
bool voice_detection = pw_properties_get_bool(args, "webrtc.voice_detection", true);
|
|
||||||
|
|
||||||
// Note: AGC seems to mess up with Agnostic Delay Detection, especially with speech,
|
|
||||||
// result in very poor performance, disable by default
|
|
||||||
bool gain_control = pw_properties_get_bool(args, "webrtc.gain_control", false);
|
|
||||||
|
|
||||||
// Disable experimental flags by default
|
|
||||||
bool experimental_agc = pw_properties_get_bool(args, "webrtc.experimental_agc", false);
|
|
||||||
bool experimental_ns = pw_properties_get_bool(args, "webrtc.experimental_ns", false);
|
|
||||||
|
|
||||||
// FIXME: Intelligibility enhancer is not currently supported
|
|
||||||
// This filter will modify playback buffer (when calling ProcessReverseStream), but now
|
|
||||||
// playback buffer modifications are discarded.
|
|
||||||
|
|
||||||
webrtc::Config config;
|
|
||||||
config.Set<webrtc::ExtendedFilter>(new webrtc::ExtendedFilter(extended_filter));
|
|
||||||
config.Set<webrtc::DelayAgnostic>(new webrtc::DelayAgnostic(delay_agnostic));
|
|
||||||
config.Set<webrtc::ExperimentalAgc>(new webrtc::ExperimentalAgc(experimental_agc));
|
|
||||||
config.Set<webrtc::ExperimentalNs>(new webrtc::ExperimentalNs(experimental_ns));
|
|
||||||
|
|
||||||
webrtc::ProcessingConfig pconfig = {{
|
|
||||||
webrtc::StreamConfig(info->rate, info->channels, false), /* input stream */
|
|
||||||
webrtc::StreamConfig(info->rate, info->channels, false), /* output stream */
|
|
||||||
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse input stream */
|
|
||||||
webrtc::StreamConfig(info->rate, info->channels, false), /* reverse output stream */
|
|
||||||
}};
|
|
||||||
|
|
||||||
auto apm = std::unique_ptr<webrtc::AudioProcessing>(webrtc::AudioProcessing::Create(config));
|
|
||||||
if (apm->Initialize(pconfig) != webrtc::AudioProcessing::kNoError) {
|
|
||||||
pw_log_error("Error initialising webrtc audio processing module");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
apm->high_pass_filter()->Enable(high_pass_filter);
|
|
||||||
// Always disable drift compensation since it requires drift sampling
|
|
||||||
apm->echo_cancellation()->enable_drift_compensation(false);
|
|
||||||
apm->echo_cancellation()->Enable(true);
|
|
||||||
// TODO: wire up supression levels to args
|
|
||||||
apm->echo_cancellation()->set_suppression_level(webrtc::EchoCancellation::kHighSuppression);
|
|
||||||
apm->noise_suppression()->set_level(webrtc::NoiseSuppression::kHigh);
|
|
||||||
apm->noise_suppression()->Enable(noise_suppression);
|
|
||||||
apm->voice_detection()->Enable(voice_detection);
|
|
||||||
// TODO: wire up AGC parameters to args
|
|
||||||
apm->gain_control()->set_analog_level_limits(0, 255);
|
|
||||||
apm->gain_control()->set_mode(webrtc::GainControl::kAdaptiveDigital);
|
|
||||||
apm->gain_control()->Enable(gain_control);
|
|
||||||
|
|
||||||
return new impl(std::move(apm), *info);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void webrtc_destroy(void *ec)
|
|
||||||
{
|
|
||||||
auto impl = static_cast<struct impl *>(ec);
|
|
||||||
|
|
||||||
delete impl;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int webrtc_run(void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
|
|
||||||
{
|
|
||||||
auto impl = static_cast<struct impl *>(ec);
|
|
||||||
webrtc::StreamConfig config =
|
|
||||||
webrtc::StreamConfig(impl->info.rate, impl->info.channels, false);
|
|
||||||
unsigned int num_blocks = n_samples * 1000 / impl->info.rate / 10;
|
|
||||||
|
|
||||||
if (n_samples * 1000 / impl->info.rate % 10 != 0) {
|
|
||||||
pw_log_error("Buffers must be multiples of 10ms in length (currently %u samples)", n_samples);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = 0; i < num_blocks; i ++) {
|
|
||||||
for (size_t j = 0; j < impl->info.channels; j++) {
|
|
||||||
impl->play_buffer[j] = const_cast<float *>(play[j]) + config.num_frames() * i;
|
|
||||||
impl->rec_buffer[j] = const_cast<float *>(rec[j]) + config.num_frames() * i;
|
|
||||||
impl->out_buffer[j] = out[j] + config.num_frames() * i;
|
|
||||||
}
|
|
||||||
/* FIXME: ProcessReverseStream may change the playback buffer, in which
|
|
||||||
* case we should use that, if we ever expose the intelligibility
|
|
||||||
* enhancer */
|
|
||||||
if (impl->apm->ProcessReverseStream(impl->play_buffer.get(), config, config, impl->play_buffer.get()) !=
|
|
||||||
webrtc::AudioProcessing::kNoError) {
|
|
||||||
pw_log_error("Processing reverse stream failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extra delay introduced by multiple frames
|
|
||||||
impl->apm->set_stream_delay_ms((num_blocks - 1) * 10);
|
|
||||||
|
|
||||||
if (impl->apm->ProcessStream(impl->rec_buffer.get(), config, config, impl->out_buffer.get()) !=
|
|
||||||
webrtc::AudioProcessing::kNoError) {
|
|
||||||
pw_log_error("Processing stream failed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const struct echo_cancel_info echo_cancel_webrtc_impl = {
|
|
||||||
.name = "webrtc",
|
|
||||||
.info = SPA_DICT_INIT(NULL, 0),
|
|
||||||
.latency = "480/48000",
|
|
||||||
|
|
||||||
.create = webrtc_create,
|
|
||||||
.destroy = webrtc_destroy,
|
|
||||||
|
|
||||||
.run = webrtc_run,
|
|
||||||
};
|
|
||||||
|
|
||||||
const struct echo_cancel_info *echo_cancel_webrtc = &echo_cancel_webrtc_impl;
|
|
||||||
|
|
@ -22,29 +22,13 @@
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
|
||||||
|
|
||||||
#include <spa/utils/dict.h>
|
#include <spa/utils/dict.h>
|
||||||
|
#include <spa/utils/hook.h>
|
||||||
#include <spa/param/audio/raw.h>
|
#include <spa/param/audio/raw.h>
|
||||||
|
#include <spa/support/plugin.h>
|
||||||
|
|
||||||
#include <pipewire/properties.h>
|
#include <pipewire/properties.h>
|
||||||
|
|
||||||
struct echo_cancel_info {
|
|
||||||
const char *name;
|
|
||||||
const struct spa_dict info;
|
|
||||||
const char *latency;
|
|
||||||
|
|
||||||
void *(*create) (const struct pw_properties *args, const struct spa_audio_info_raw *info);
|
|
||||||
void (*destroy) (void *ec);
|
|
||||||
|
|
||||||
int (*run) (void *ec, const float *rec[], const float *play[], float *out[], uint32_t n_samples);
|
|
||||||
};
|
|
||||||
|
|
||||||
#define echo_cancel_create(i,...) (i)->create(__VA_ARGS__)
|
#define echo_cancel_create(i,...) (i)->create(__VA_ARGS__)
|
||||||
#define echo_cancel_destroy(i,...) (i)->destroy(__VA_ARGS__)
|
|
||||||
#define echo_cancel_run(i,...) (i)->run(__VA_ARGS__)
|
#define echo_cancel_run(i,...) (i)->run(__VA_ARGS__)
|
||||||
|
|
||||||
#ifdef HAVE_WEBRTC
|
|
||||||
extern const struct echo_cancel_info *echo_cancel_webrtc;
|
|
||||||
#endif
|
|
||||||
extern const struct echo_cancel_info *echo_cancel_null;
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue