spa: improve the AEC interface

Place the methods on the interface so that we can call them.
Rename create to init because that is what it does.
Add support for listener and events so that we can signal property
changes later.
This commit is contained in:
Wim Taymans 2022-02-16 16:18:18 +01:00
parent 9386c70b3a
commit c5c9ecdd87
5 changed files with 130 additions and 130 deletions

View file

@ -25,20 +25,71 @@
#include <spa/utils/dict.h>
#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
#ifndef SPA_AUDIO_AEC_H
#define SPA_AUDIO_AEC_H
struct echo_cancel_info {
#ifdef __cplusplus
extern "C" {
#endif
#define SPA_TYPE_INTERFACE_AUDIO_AEC SPA_TYPE_INFO_INTERFACE_BASE "Audio:AEC"
#define SPA_VERSION_AUDIO_AEC 0
struct spa_audio_aec {
struct spa_interface iface;
const char *name;
const struct spa_dict info;
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);
};
struct spa_audio_aec_info {
#define SPA_AUDIO_AEC_CHANGE_MASK_PROPS (1u<<0)
uint64_t change_mask;
const struct spa_dict *props;
};
struct spa_audio_aec_events {
#define SPA_VERSION_AUDIO_AEC_EVENTS 0
uint32_t version; /**< version of this structure */
/** Emitted when info changes */
void (*info) (void *data, const struct spa_audio_aec_info *info);
};
struct spa_audio_aec_methods {
#define SPA_VERSION_AUDIO_AEC_METHODS 0
uint32_t version;
int (*add_listener) (void *object,
struct spa_hook *listener,
const struct spa_audio_aec_events *events,
void *data);
int (*init) (void *data, const struct spa_dict *args, const struct spa_audio_info_raw *info);
int (*run) (void *data, const float *rec[], const float *play[], float *out[], uint32_t n_samples);
int (*set_props) (void *data, const struct spa_dict *args);
};
#define spa_audio_aec_method(o,method,version,...) \
({ \
int _res = -ENOTSUP; \
struct spa_audio_aec *_o = o; \
spa_interface_call_res(&_o->iface, \
struct spa_audio_aec_methods, _res, \
method, version, ##__VA_ARGS__); \
_res; \
})
#define spa_audio_aec_add_listener(o,...) spa_audio_aec_method(o, add_listener, 0, __VA_ARGS__)
#define spa_audio_aec_init(o,...) spa_audio_aec_method(o, init, 0, __VA_ARGS__)
#define spa_audio_aec_run(o,...) spa_audio_aec_method(o, run, 0, __VA_ARGS__)
#define spa_audio_aec_set_props(o,...) spa_audio_aec_method(o, set_props, 0, __VA_ARGS__)
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_AUDIO_AEC_H */

View file

@ -30,7 +30,11 @@
struct impl {
struct spa_handle handle;
struct spa_audio_aec aec;
struct spa_log *log;
struct spa_hook_list hooks_list;
uint32_t channels;
};
@ -38,54 +42,38 @@ 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)
static int null_init(void *data, const struct spa_dict *args, const struct spa_audio_info_raw *info)
{
struct impl *impl;
impl = (struct impl *) handle;
struct impl *impl = data;
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)
static int null_run(void *data, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
{
struct impl *impl = (struct impl *) handle;
struct impl *impl = data;
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,
static struct spa_audio_aec_methods impl_aec = {
.init = null_init,
.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)
{
struct impl *impl;
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;
impl = (struct impl *) handle;
if (spa_streq(type, SPA_TYPE_INTERFACE_AUDIO_AEC))
*interface = &impl->aec;
else
return -ENOENT;
@ -118,23 +106,29 @@ impl_init(const struct spa_handle_factory *factory,
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->aec.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_AUDIO_AEC,
SPA_VERSION_AUDIO_AEC,
&impl_aec, impl);
impl->aec.name = "null";
impl->aec.info = NULL;
impl->aec.latency = NULL;
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
spa_log_topic_init(impl->log, &log_topic);
spa_hook_list_init(&impl->hooks_list);
return 0;
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE_INTERFACE_AEC,},
{SPA_TYPE_INTERFACE_AUDIO_AEC,},
};
static int

View file

@ -38,6 +38,8 @@
struct impl_data {
struct spa_handle handle;
struct spa_audio_aec aec;
struct spa_log *log;
std::unique_ptr<webrtc::AudioProcessing> apm;
spa_audio_info_raw info;
@ -58,9 +60,9 @@ static bool webrtc_get_spa_bool(const struct spa_dict *args, const char *key, bo
return value;
}
static int webrtc_create(struct spa_handle *handle, const struct spa_dict *args, const struct spa_audio_info_raw *info)
static int webrtc_init(void *data, const struct spa_dict *args, const struct spa_audio_info_raw *info)
{
auto impl = reinterpret_cast<struct impl_data*>(handle);
auto impl = reinterpret_cast<struct impl_data*>(data);
bool extended_filter = webrtc_get_spa_bool(args, "webrtc.extended_filter", true);
bool delay_agnostic = webrtc_get_spa_bool(args, "webrtc.delay_agnostic", true);
@ -120,9 +122,9 @@ static int webrtc_create(struct spa_handle *handle, const struct spa_dict *args,
return 0;
}
static int webrtc_run(struct spa_handle *handle, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
static int webrtc_run(void *data, const float *rec[], const float *play[], float *out[], uint32_t n_samples)
{
auto impl = reinterpret_cast<struct impl_data*>(handle);
auto impl = reinterpret_cast<struct impl_data*>(data);
webrtc::StreamConfig config =
webrtc::StreamConfig(impl->info.rate, impl->info.channels, false);
unsigned int num_blocks = n_samples * 1000 / impl->info.rate / 10;
@ -158,36 +160,20 @@ static int webrtc_run(struct spa_handle *handle, const float *rec[], const float
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,
static struct spa_audio_aec_methods impl_aec = {
.init = webrtc_init,
.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)
{
auto impl = reinterpret_cast<struct impl_data*>(handle);
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;
if (spa_streq(type, SPA_TYPE_INTERFACE_AUDIO_AEC))
*interface = &impl->aec;
else
return -ENOENT;
@ -219,15 +205,19 @@ impl_init(const struct spa_handle_factory *factory,
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->aec.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_AUDIO_AEC,
SPA_VERSION_AUDIO_AEC,
&impl_aec, impl);
impl->aec.name = "webrtc",
impl->aec.info = NULL;
impl->aec.latency = "480/48000",
impl->log = (struct spa_log*)spa_support_find(support, n_support, SPA_TYPE_INTERFACE_Log);
spa_log_topic_init(impl->log, &log_topic);
@ -235,7 +225,7 @@ impl_init(const struct spa_handle_factory *factory,
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE_INTERFACE_AEC,},
{SPA_TYPE_INTERFACE_AUDIO_AEC,},
};
static int