|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2026 Intel Corporation. |
| 4 | +// |
| 5 | +// Real AECm backend for the webrtc_aec module. |
| 6 | +// |
| 7 | +// Wraps WebRtcAecm_Create/Init/BufferFarend/Process from the WebRTC AECm |
| 8 | +// (Mobile) fixed-point library extracted from webrtc-audio-processing 0.3.x. |
| 9 | +// |
| 10 | +// API summary: |
| 11 | +// WebRtcAecm_Create() — allocate AecmCore handle |
| 12 | +// WebRtcAecm_Init() — configure sample rate (8000 or 16000 Hz) |
| 13 | +// WebRtcAecm_BufferFarend() — queue 10 ms of echo reference (far-end) |
| 14 | +// WebRtcAecm_Process() — cancel echo from near-end mic frame |
| 15 | +// WebRtcAecm_Enable() — configure filter length and CNG suppression |
| 16 | +// WebRtcAecm_Free() — release handle |
| 17 | +// |
| 18 | +// All operations are Q15 fixed-point on int16_t. One handle per channel. |
| 19 | + |
| 20 | +#include <sof/audio/module_adapter/module/generic.h> |
| 21 | +#include <errno.h> |
| 22 | +#include "webrtc_aec.h" |
| 23 | + |
| 24 | +#include <echo_control_mobile.h> /* from cross-built webrtc-audio-processing 0.3.x */ |
| 25 | + |
| 26 | +LOG_MODULE_DECLARE(webrtc_aec, CONFIG_SOF_LOG_LEVEL); |
| 27 | + |
| 28 | +struct webrtc_aec_real_data { |
| 29 | + void *aecm[WEBRTC_AEC_CHANNELS_MAX]; |
| 30 | + int num_channels; |
| 31 | + int filter_len_ms; |
| 32 | + int suppression; |
| 33 | +}; |
| 34 | + |
| 35 | +static int webrtc_aec_real_init(struct processing_module *mod) |
| 36 | +{ |
| 37 | + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); |
| 38 | + struct webrtc_aec_real_data *rd; |
| 39 | + |
| 40 | + rd = mod_zalloc(mod, sizeof(*rd)); |
| 41 | + if (!rd) |
| 42 | + return -ENOMEM; |
| 43 | + |
| 44 | + cd->backend_data = rd; |
| 45 | + comp_info(mod->dev, "webrtc_aec: AECm real backend initialised"); |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +static int webrtc_aec_real_configure(struct processing_module *mod, int sample_rate_hz, |
| 50 | + int filter_len_ms, int suppression, int num_channels) |
| 51 | +{ |
| 52 | + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); |
| 53 | + struct webrtc_aec_real_data *rd = cd->backend_data; |
| 54 | + AecmConfig config; |
| 55 | + int c, ret; |
| 56 | + |
| 57 | + /* Free previously allocated handles. */ |
| 58 | + for (c = 0; c < rd->num_channels; c++) { |
| 59 | + if (rd->aecm[c]) { |
| 60 | + WebRtcAecm_Free(rd->aecm[c]); |
| 61 | + rd->aecm[c] = NULL; |
| 62 | + } |
| 63 | + } |
| 64 | + rd->num_channels = 0; |
| 65 | + rd->filter_len_ms = filter_len_ms; |
| 66 | + rd->suppression = suppression; |
| 67 | + |
| 68 | + for (c = 0; c < num_channels; c++) { |
| 69 | + rd->aecm[c] = WebRtcAecm_Create(); |
| 70 | + if (!rd->aecm[c]) { |
| 71 | + comp_err(mod->dev, "webrtc_aec: WebRtcAecm_Create() failed ch%d", c); |
| 72 | + goto err; |
| 73 | + } |
| 74 | + |
| 75 | + ret = WebRtcAecm_Init(rd->aecm[c], sample_rate_hz); |
| 76 | + if (ret) { |
| 77 | + comp_err(mod->dev, |
| 78 | + "webrtc_aec: WebRtcAecm_Init(ch%d, %d) failed %d", |
| 79 | + c, sample_rate_hz, ret); |
| 80 | + goto err; |
| 81 | + } |
| 82 | + |
| 83 | + config.cngMode = suppression; |
| 84 | + config.echoMode = (filter_len_ms == 128) ? 4 : |
| 85 | + (filter_len_ms == 64) ? 3 : |
| 86 | + (filter_len_ms == 32) ? 2 : 3; |
| 87 | + ret = WebRtcAecm_set_config(rd->aecm[c], config); |
| 88 | + if (ret) { |
| 89 | + comp_err(mod->dev, "webrtc_aec: set_config ch%d failed %d", c, ret); |
| 90 | + goto err; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + rd->num_channels = num_channels; |
| 95 | + comp_info(mod->dev, "webrtc_aec: AECm rate=%d filter=%dms cng=%d ch=%d", |
| 96 | + sample_rate_hz, filter_len_ms, suppression, num_channels); |
| 97 | + return 0; |
| 98 | + |
| 99 | +err: |
| 100 | + for (c = 0; c < num_channels; c++) { |
| 101 | + if (rd->aecm[c]) { |
| 102 | + WebRtcAecm_Free(rd->aecm[c]); |
| 103 | + rd->aecm[c] = NULL; |
| 104 | + } |
| 105 | + } |
| 106 | + return -ENOMEM; |
| 107 | +} |
| 108 | + |
| 109 | +static int webrtc_aec_real_process_ch(struct processing_module *mod, |
| 110 | + const int16_t *mic, const int16_t *ref, int16_t *out, |
| 111 | + int frame_samples, int ch) |
| 112 | +{ |
| 113 | + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); |
| 114 | + struct webrtc_aec_real_data *rd = cd->backend_data; |
| 115 | + int ret; |
| 116 | + |
| 117 | + /* Queue the far-end (reference/playback) frame first. */ |
| 118 | + ret = WebRtcAecm_BufferFarend(rd->aecm[ch], ref, frame_samples); |
| 119 | + if (ret) { |
| 120 | + comp_err(mod->dev, "webrtc_aec: BufferFarend ch%d failed %d", ch, ret); |
| 121 | + return ret; |
| 122 | + } |
| 123 | + |
| 124 | + /* Process near-end (mic) and produce echo-cancelled output. |
| 125 | + * The third parameter (near_end_noiseless) can be NULL. */ |
| 126 | + ret = WebRtcAecm_Process(rd->aecm[ch], mic, NULL, out, frame_samples, 0); |
| 127 | + if (ret) { |
| 128 | + comp_err(mod->dev, "webrtc_aec: Process ch%d failed %d", ch, ret); |
| 129 | + return ret; |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | +} |
| 134 | + |
| 135 | +static int webrtc_aec_real_reset(struct processing_module *mod) |
| 136 | +{ |
| 137 | + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); |
| 138 | + struct webrtc_aec_real_data *rd = cd->backend_data; |
| 139 | + int c, ret; |
| 140 | + |
| 141 | + for (c = 0; c < rd->num_channels; c++) { |
| 142 | + if (!rd->aecm[c]) |
| 143 | + continue; |
| 144 | + ret = WebRtcAecm_Init(rd->aecm[c], cd->proc_rate); |
| 145 | + if (ret) |
| 146 | + comp_warn(mod->dev, "webrtc_aec: reset ch%d failed %d", c, ret); |
| 147 | + } |
| 148 | + return 0; |
| 149 | +} |
| 150 | + |
| 151 | +static int webrtc_aec_real_free(struct processing_module *mod) |
| 152 | +{ |
| 153 | + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); |
| 154 | + struct webrtc_aec_real_data *rd = cd->backend_data; |
| 155 | + int c; |
| 156 | + |
| 157 | + if (!rd) |
| 158 | + return 0; |
| 159 | + |
| 160 | + for (c = 0; c < rd->num_channels; c++) { |
| 161 | + if (rd->aecm[c]) { |
| 162 | + WebRtcAecm_Free(rd->aecm[c]); |
| 163 | + rd->aecm[c] = NULL; |
| 164 | + } |
| 165 | + } |
| 166 | + mod_free(mod, rd); |
| 167 | + cd->backend_data = NULL; |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +const struct webrtc_aec_backend webrtc_aec_backend = { |
| 172 | + .name = "aecm", |
| 173 | + .init = webrtc_aec_real_init, |
| 174 | + .configure = webrtc_aec_real_configure, |
| 175 | + .process_ch = webrtc_aec_real_process_ch, |
| 176 | + .reset = webrtc_aec_real_reset, |
| 177 | + .free = webrtc_aec_real_free, |
| 178 | +}; |
0 commit comments