niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/echo_control_mobile_impl.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <string.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <cstdint> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/audio_processing/aecm/echo_control_mobile.h" |
| 17 | #include "modules/audio_processing/audio_buffer.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/include/audio_processing.h" |
| 19 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/constructor_magic.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | namespace { |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 25 | int16_t MapSetting(EchoControlMobileImpl::RoutingMode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | switch (mode) { |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 27 | case EchoControlMobileImpl::kQuietEarpieceOrHeadset: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | return 0; |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 29 | case EchoControlMobileImpl::kEarpiece: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | return 1; |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 31 | case EchoControlMobileImpl::kLoudEarpiece: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | return 2; |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 33 | case EchoControlMobileImpl::kSpeakerphone: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | return 3; |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 35 | case EchoControlMobileImpl::kLoudSpeakerphone: |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | return 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | } |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 38 | RTC_NOTREACHED(); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 39 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 40 | } |
| 41 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 42 | AudioProcessing::Error MapError(int err) { |
| 43 | switch (err) { |
| 44 | case AECM_UNSUPPORTED_FUNCTION_ERROR: |
| 45 | return AudioProcessing::kUnsupportedFunctionError; |
| 46 | case AECM_NULL_POINTER_ERROR: |
| 47 | return AudioProcessing::kNullPointerError; |
| 48 | case AECM_BAD_PARAMETER_ERROR: |
| 49 | return AudioProcessing::kBadParameterError; |
| 50 | case AECM_BAD_PARAMETER_WARNING: |
| 51 | return AudioProcessing::kBadStreamParameterWarning; |
| 52 | default: |
| 53 | // AECM_UNSPECIFIED_ERROR |
| 54 | // AECM_UNINITIALIZED_ERROR |
| 55 | return AudioProcessing::kUnspecifiedError; |
| 56 | } |
| 57 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } // namespace |
| 59 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 60 | struct EchoControlMobileImpl::StreamProperties { |
| 61 | StreamProperties() = delete; |
| 62 | StreamProperties(int sample_rate_hz, |
| 63 | size_t num_reverse_channels, |
| 64 | size_t num_output_channels) |
| 65 | : sample_rate_hz(sample_rate_hz), |
| 66 | num_reverse_channels(num_reverse_channels), |
| 67 | num_output_channels(num_output_channels) {} |
| 68 | |
| 69 | int sample_rate_hz; |
| 70 | size_t num_reverse_channels; |
| 71 | size_t num_output_channels; |
| 72 | }; |
| 73 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 74 | class EchoControlMobileImpl::Canceller { |
| 75 | public: |
| 76 | Canceller() { |
| 77 | state_ = WebRtcAecm_Create(); |
| 78 | RTC_CHECK(state_); |
| 79 | } |
| 80 | |
| 81 | ~Canceller() { |
| 82 | RTC_DCHECK(state_); |
| 83 | WebRtcAecm_Free(state_); |
| 84 | } |
| 85 | |
| 86 | void* state() { |
| 87 | RTC_DCHECK(state_); |
| 88 | return state_; |
| 89 | } |
| 90 | |
Sam Zackrisson | c22f551 | 2018-11-05 16:10:00 +0100 | [diff] [blame] | 91 | void Initialize(int sample_rate_hz) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 92 | RTC_DCHECK(state_); |
| 93 | int error = WebRtcAecm_Init(state_, sample_rate_hz); |
| 94 | RTC_DCHECK_EQ(AudioProcessing::kNoError, error); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
| 98 | void* state_; |
| 99 | RTC_DISALLOW_COPY_AND_ASSIGN(Canceller); |
| 100 | }; |
| 101 | |
Sam Zackrisson | c22f551 | 2018-11-05 16:10:00 +0100 | [diff] [blame] | 102 | EchoControlMobileImpl::EchoControlMobileImpl() |
| 103 | : routing_mode_(kSpeakerphone), comfort_noise_enabled_(false) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 104 | |
Sam Zackrisson | c22f551 | 2018-11-05 16:10:00 +0100 | [diff] [blame] | 105 | EchoControlMobileImpl::~EchoControlMobileImpl() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 107 | void EchoControlMobileImpl::ProcessRenderAudio( |
| 108 | rtc::ArrayView<const int16_t> packed_render_audio) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 109 | if (!enabled_) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 110 | return; |
| 111 | } |
| 112 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 113 | RTC_DCHECK(stream_properties_); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 114 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 115 | size_t buffer_index = 0; |
| 116 | size_t num_frames_per_band = |
| 117 | packed_render_audio.size() / (stream_properties_->num_output_channels * |
| 118 | stream_properties_->num_reverse_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 119 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 120 | for (auto& canceller : cancellers_) { |
| 121 | WebRtcAecm_BufferFarend(canceller->state(), |
| 122 | &packed_render_audio[buffer_index], |
| 123 | num_frames_per_band); |
| 124 | |
| 125 | buffer_index += num_frames_per_band; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void EchoControlMobileImpl::PackRenderAudioBuffer( |
| 130 | const AudioBuffer* audio, |
| 131 | size_t num_output_channels, |
| 132 | size_t num_channels, |
| 133 | std::vector<int16_t>* packed_buffer) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 134 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 135 | RTC_DCHECK_EQ(num_channels, audio->num_channels()); |
| 136 | |
| 137 | // The ordering convention must be followed to pass to the correct AECM. |
| 138 | packed_buffer->clear(); |
| 139 | int render_channel = 0; |
| 140 | for (size_t i = 0; i < num_output_channels; i++) { |
| 141 | for (size_t j = 0; j < audio->num_channels(); j++) { |
| 142 | // Buffer the samples in the render queue. |
| 143 | packed_buffer->insert( |
| 144 | packed_buffer->end(), |
| 145 | audio->split_bands_const(render_channel)[kBand0To8kHz], |
| 146 | (audio->split_bands_const(render_channel)[kBand0To8kHz] + |
| 147 | audio->num_frames_per_band())); |
| 148 | render_channel = (render_channel + 1) % audio->num_channels(); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 153 | size_t EchoControlMobileImpl::NumCancellersRequired( |
| 154 | size_t num_output_channels, |
| 155 | size_t num_reverse_channels) { |
| 156 | return num_output_channels * num_reverse_channels; |
| 157 | } |
| 158 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 159 | int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio, |
| 160 | int stream_delay_ms) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 161 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 162 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | } |
| 164 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 165 | RTC_DCHECK(stream_properties_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 166 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 167 | RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_output_channels); |
| 168 | RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_reverse_channels * |
| 169 | audio->num_channels()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 171 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
| 173 | // The ordering convention must be followed to pass to the correct AECM. |
| 174 | size_t handle_index = 0; |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 175 | for (size_t capture = 0; capture < audio->num_channels(); ++capture) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | // TODO(ajm): improve how this works, possibly inside AECM. |
| 177 | // This is kind of hacked up. |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 178 | const int16_t* noisy = audio->low_pass_reference(capture); |
| 179 | const int16_t* clean = audio->split_bands_const(capture)[kBand0To8kHz]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 180 | if (noisy == NULL) { |
| 181 | noisy = clean; |
| 182 | clean = NULL; |
| 183 | } |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 184 | for (size_t render = 0; render < stream_properties_->num_reverse_channels; |
| 185 | ++render) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 186 | err = WebRtcAecm_Process(cancellers_[handle_index]->state(), noisy, clean, |
| 187 | audio->split_bands(capture)[kBand0To8kHz], |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 188 | audio->num_frames_per_band(), stream_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 190 | if (err != AudioProcessing::kNoError) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 191 | return MapError(err); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 192 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 194 | ++handle_index; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | } |
aluebs | 776593b | 2016-03-15 14:04:58 -0700 | [diff] [blame] | 196 | for (size_t band = 1u; band < audio->num_bands(); ++band) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 197 | memset(audio->split_bands(capture)[band], 0, |
aluebs | 776593b | 2016-03-15 14:04:58 -0700 | [diff] [blame] | 198 | audio->num_frames_per_band() * |
| 199 | sizeof(audio->split_bands(capture)[band][0])); |
| 200 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 201 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 202 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | int EchoControlMobileImpl::Enable(bool enable) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | // Ensure AEC and AECM are not both enabled. |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 207 | RTC_DCHECK(stream_properties_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 209 | if (enable && |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 210 | stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 211 | return AudioProcessing::kBadSampleRateError; |
| 212 | } |
| 213 | |
| 214 | if (enable && !enabled_) { |
| 215 | enabled_ = enable; // Must be set before Initialize() is called. |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 216 | |
| 217 | // TODO(peah): Simplify once the Enable function has been removed from |
| 218 | // the public APM API. |
| 219 | Initialize(stream_properties_->sample_rate_hz, |
| 220 | stream_properties_->num_reverse_channels, |
| 221 | stream_properties_->num_output_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 222 | } else { |
| 223 | enabled_ = enable; |
| 224 | } |
| 225 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool EchoControlMobileImpl::is_enabled() const { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 229 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 233 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 234 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 236 | routing_mode_ = mode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 237 | return Configure(); |
| 238 | } |
| 239 | |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 240 | EchoControlMobileImpl::RoutingMode EchoControlMobileImpl::routing_mode() const { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | return routing_mode_; |
| 242 | } |
| 243 | |
| 244 | int EchoControlMobileImpl::enable_comfort_noise(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 245 | comfort_noise_enabled_ = enable; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 246 | return Configure(); |
| 247 | } |
| 248 | |
| 249 | bool EchoControlMobileImpl::is_comfort_noise_enabled() const { |
| 250 | return comfort_noise_enabled_; |
| 251 | } |
| 252 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 253 | void EchoControlMobileImpl::Initialize(int sample_rate_hz, |
| 254 | size_t num_reverse_channels, |
| 255 | size_t num_output_channels) { |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 256 | stream_properties_.reset(new StreamProperties( |
| 257 | sample_rate_hz, num_reverse_channels, num_output_channels)); |
| 258 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 259 | if (!enabled_) { |
| 260 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 263 | // AECM only supports 16 kHz or lower sample rates. |
| 264 | RTC_DCHECK_LE(stream_properties_->sample_rate_hz, |
| 265 | AudioProcessing::kSampleRate16kHz); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 267 | cancellers_.resize( |
| 268 | NumCancellersRequired(stream_properties_->num_output_channels, |
| 269 | stream_properties_->num_reverse_channels)); |
| 270 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 271 | for (auto& canceller : cancellers_) { |
| 272 | if (!canceller) { |
| 273 | canceller.reset(new Canceller()); |
| 274 | } |
Sam Zackrisson | c22f551 | 2018-11-05 16:10:00 +0100 | [diff] [blame] | 275 | canceller->Initialize(sample_rate_hz); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 276 | } |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 277 | Configure(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 278 | } |
| 279 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 280 | int EchoControlMobileImpl::Configure() { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 281 | AecmConfig config; |
| 282 | config.cngMode = comfort_noise_enabled_; |
| 283 | config.echoMode = MapSetting(routing_mode_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 284 | int error = AudioProcessing::kNoError; |
| 285 | for (auto& canceller : cancellers_) { |
| 286 | int handle_error = WebRtcAecm_set_config(canceller->state(), config); |
| 287 | if (handle_error != AudioProcessing::kNoError) { |
| 288 | error = handle_error; |
| 289 | } |
| 290 | } |
| 291 | return error; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 292 | } |
| 293 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 294 | } // namespace webrtc |