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" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/constructormagic.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 | |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 60 | size_t EchoControlMobileImpl::echo_path_size_bytes() { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 61 | return WebRtcAecm_echo_path_size_bytes(); |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 62 | } |
| 63 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 64 | struct EchoControlMobileImpl::StreamProperties { |
| 65 | StreamProperties() = delete; |
| 66 | StreamProperties(int sample_rate_hz, |
| 67 | size_t num_reverse_channels, |
| 68 | size_t num_output_channels) |
| 69 | : sample_rate_hz(sample_rate_hz), |
| 70 | num_reverse_channels(num_reverse_channels), |
| 71 | num_output_channels(num_output_channels) {} |
| 72 | |
| 73 | int sample_rate_hz; |
| 74 | size_t num_reverse_channels; |
| 75 | size_t num_output_channels; |
| 76 | }; |
| 77 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 78 | class EchoControlMobileImpl::Canceller { |
| 79 | public: |
| 80 | Canceller() { |
| 81 | state_ = WebRtcAecm_Create(); |
| 82 | RTC_CHECK(state_); |
| 83 | } |
| 84 | |
| 85 | ~Canceller() { |
| 86 | RTC_DCHECK(state_); |
| 87 | WebRtcAecm_Free(state_); |
| 88 | } |
| 89 | |
| 90 | void* state() { |
| 91 | RTC_DCHECK(state_); |
| 92 | return state_; |
| 93 | } |
| 94 | |
| 95 | void Initialize(int sample_rate_hz, |
| 96 | unsigned char* external_echo_path, |
| 97 | size_t echo_path_size_bytes) { |
| 98 | RTC_DCHECK(state_); |
| 99 | int error = WebRtcAecm_Init(state_, sample_rate_hz); |
| 100 | RTC_DCHECK_EQ(AudioProcessing::kNoError, error); |
| 101 | if (external_echo_path != NULL) { |
| 102 | error = WebRtcAecm_InitEchoPath(state_, external_echo_path, |
| 103 | echo_path_size_bytes); |
| 104 | RTC_DCHECK_EQ(AudioProcessing::kNoError, error); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private: |
| 109 | void* state_; |
| 110 | RTC_DISALLOW_COPY_AND_ASSIGN(Canceller); |
| 111 | }; |
| 112 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 113 | EchoControlMobileImpl::EchoControlMobileImpl(rtc::CriticalSection* crit_render, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 114 | rtc::CriticalSection* crit_capture) |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 115 | : crit_render_(crit_render), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 116 | crit_capture_(crit_capture), |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 117 | routing_mode_(kSpeakerphone), |
Sam Zackrisson | e507b0c | 2018-07-20 15:22:50 +0200 | [diff] [blame] | 118 | comfort_noise_enabled_(false), |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 119 | external_echo_path_(NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 120 | RTC_DCHECK(crit_render); |
| 121 | RTC_DCHECK(crit_capture); |
| 122 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 124 | EchoControlMobileImpl::~EchoControlMobileImpl() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 125 | if (external_echo_path_ != NULL) { |
| 126 | delete[] external_echo_path_; |
| 127 | external_echo_path_ = NULL; |
| 128 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 129 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 131 | void EchoControlMobileImpl::ProcessRenderAudio( |
| 132 | rtc::ArrayView<const int16_t> packed_render_audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 133 | rtc::CritScope cs_capture(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 134 | if (!enabled_) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 138 | RTC_DCHECK(stream_properties_); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 139 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 140 | size_t buffer_index = 0; |
| 141 | size_t num_frames_per_band = |
| 142 | packed_render_audio.size() / (stream_properties_->num_output_channels * |
| 143 | stream_properties_->num_reverse_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 144 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 145 | for (auto& canceller : cancellers_) { |
| 146 | WebRtcAecm_BufferFarend(canceller->state(), |
| 147 | &packed_render_audio[buffer_index], |
| 148 | num_frames_per_band); |
| 149 | |
| 150 | buffer_index += num_frames_per_band; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void EchoControlMobileImpl::PackRenderAudioBuffer( |
| 155 | const AudioBuffer* audio, |
| 156 | size_t num_output_channels, |
| 157 | size_t num_channels, |
| 158 | std::vector<int16_t>* packed_buffer) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 159 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 160 | RTC_DCHECK_EQ(num_channels, audio->num_channels()); |
| 161 | |
| 162 | // The ordering convention must be followed to pass to the correct AECM. |
| 163 | packed_buffer->clear(); |
| 164 | int render_channel = 0; |
| 165 | for (size_t i = 0; i < num_output_channels; i++) { |
| 166 | for (size_t j = 0; j < audio->num_channels(); j++) { |
| 167 | // Buffer the samples in the render queue. |
| 168 | packed_buffer->insert( |
| 169 | packed_buffer->end(), |
| 170 | audio->split_bands_const(render_channel)[kBand0To8kHz], |
| 171 | (audio->split_bands_const(render_channel)[kBand0To8kHz] + |
| 172 | audio->num_frames_per_band())); |
| 173 | render_channel = (render_channel + 1) % audio->num_channels(); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 178 | size_t EchoControlMobileImpl::NumCancellersRequired( |
| 179 | size_t num_output_channels, |
| 180 | size_t num_reverse_channels) { |
| 181 | return num_output_channels * num_reverse_channels; |
| 182 | } |
| 183 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 184 | int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio, |
| 185 | int stream_delay_ms) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 186 | rtc::CritScope cs_capture(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 187 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 188 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 189 | } |
| 190 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 191 | RTC_DCHECK(stream_properties_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 192 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 193 | RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_output_channels); |
| 194 | RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_reverse_channels * |
| 195 | audio->num_channels()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 197 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | |
| 199 | // The ordering convention must be followed to pass to the correct AECM. |
| 200 | size_t handle_index = 0; |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 201 | for (size_t capture = 0; capture < audio->num_channels(); ++capture) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | // TODO(ajm): improve how this works, possibly inside AECM. |
| 203 | // This is kind of hacked up. |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 204 | const int16_t* noisy = audio->low_pass_reference(capture); |
| 205 | const int16_t* clean = audio->split_bands_const(capture)[kBand0To8kHz]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | if (noisy == NULL) { |
| 207 | noisy = clean; |
| 208 | clean = NULL; |
| 209 | } |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 210 | for (size_t render = 0; render < stream_properties_->num_reverse_channels; |
| 211 | ++render) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 212 | err = WebRtcAecm_Process(cancellers_[handle_index]->state(), noisy, clean, |
| 213 | audio->split_bands(capture)[kBand0To8kHz], |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 214 | audio->num_frames_per_band(), stream_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 215 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 216 | if (err != AudioProcessing::kNoError) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 217 | return MapError(err); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 218 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 220 | ++handle_index; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 221 | } |
aluebs | 776593b | 2016-03-15 14:04:58 -0700 | [diff] [blame] | 222 | for (size_t band = 1u; band < audio->num_bands(); ++band) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 223 | memset(audio->split_bands(capture)[band], 0, |
aluebs | 776593b | 2016-03-15 14:04:58 -0700 | [diff] [blame] | 224 | audio->num_frames_per_band() * |
| 225 | sizeof(audio->split_bands(capture)[band][0])); |
| 226 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 228 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int EchoControlMobileImpl::Enable(bool enable) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 232 | // Ensure AEC and AECM are not both enabled. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 233 | rtc::CritScope cs_render(crit_render_); |
| 234 | rtc::CritScope cs_capture(crit_capture_); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 235 | RTC_DCHECK(stream_properties_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 237 | if (enable && |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 238 | stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 239 | return AudioProcessing::kBadSampleRateError; |
| 240 | } |
| 241 | |
| 242 | if (enable && !enabled_) { |
| 243 | enabled_ = enable; // Must be set before Initialize() is called. |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 244 | |
| 245 | // TODO(peah): Simplify once the Enable function has been removed from |
| 246 | // the public APM API. |
| 247 | Initialize(stream_properties_->sample_rate_hz, |
| 248 | stream_properties_->num_reverse_channels, |
| 249 | stream_properties_->num_output_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 250 | } else { |
| 251 | enabled_ = enable; |
| 252 | } |
| 253 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | bool EchoControlMobileImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 257 | rtc::CritScope cs(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 258 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 263 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | } |
| 265 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 266 | { |
| 267 | rtc::CritScope cs(crit_capture_); |
| 268 | routing_mode_ = mode; |
| 269 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 270 | return Configure(); |
| 271 | } |
| 272 | |
Sam Zackrisson | 8c147b6 | 2018-09-28 12:40:47 +0200 | [diff] [blame] | 273 | EchoControlMobileImpl::RoutingMode EchoControlMobileImpl::routing_mode() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 274 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 275 | return routing_mode_; |
| 276 | } |
| 277 | |
| 278 | int EchoControlMobileImpl::enable_comfort_noise(bool enable) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 279 | { |
| 280 | rtc::CritScope cs(crit_capture_); |
| 281 | comfort_noise_enabled_ = enable; |
| 282 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 283 | return Configure(); |
| 284 | } |
| 285 | |
| 286 | bool EchoControlMobileImpl::is_comfort_noise_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 287 | rtc::CritScope cs(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 288 | return comfort_noise_enabled_; |
| 289 | } |
| 290 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 291 | int EchoControlMobileImpl::SetEchoPath(const void* echo_path, |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 292 | size_t size_bytes) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 293 | { |
| 294 | rtc::CritScope cs_render(crit_render_); |
| 295 | rtc::CritScope cs_capture(crit_capture_); |
| 296 | if (echo_path == NULL) { |
| 297 | return AudioProcessing::kNullPointerError; |
| 298 | } |
| 299 | if (size_bytes != echo_path_size_bytes()) { |
| 300 | // Size mismatch |
| 301 | return AudioProcessing::kBadParameterError; |
| 302 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 303 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 304 | if (external_echo_path_ == NULL) { |
| 305 | external_echo_path_ = new unsigned char[size_bytes]; |
| 306 | } |
| 307 | memcpy(external_echo_path_, echo_path, size_bytes); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 308 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 309 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 310 | // TODO(peah): Simplify once the Enable function has been removed from |
| 311 | // the public APM API. |
| 312 | RTC_DCHECK(stream_properties_); |
| 313 | Initialize(stream_properties_->sample_rate_hz, |
| 314 | stream_properties_->num_reverse_channels, |
| 315 | stream_properties_->num_output_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 316 | return AudioProcessing::kNoError; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | int EchoControlMobileImpl::GetEchoPath(void* echo_path, |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 320 | size_t size_bytes) const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 321 | rtc::CritScope cs(crit_capture_); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 322 | if (echo_path == NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 323 | return AudioProcessing::kNullPointerError; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 324 | } |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 325 | if (size_bytes != echo_path_size_bytes()) { |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 326 | // Size mismatch |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 327 | return AudioProcessing::kBadParameterError; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 328 | } |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 329 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 330 | return AudioProcessing::kNotEnabledError; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Get the echo path from the first channel |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 334 | int32_t err = |
| 335 | WebRtcAecm_GetEchoPath(cancellers_[0]->state(), echo_path, size_bytes); |
| 336 | if (err != 0) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 337 | return MapError(err); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 338 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 339 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 340 | return AudioProcessing::kNoError; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 341 | } |
| 342 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 343 | void EchoControlMobileImpl::Initialize(int sample_rate_hz, |
| 344 | size_t num_reverse_channels, |
| 345 | size_t num_output_channels) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 346 | rtc::CritScope cs_render(crit_render_); |
| 347 | rtc::CritScope cs_capture(crit_capture_); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 348 | |
| 349 | stream_properties_.reset(new StreamProperties( |
| 350 | sample_rate_hz, num_reverse_channels, num_output_channels)); |
| 351 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 352 | if (!enabled_) { |
| 353 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Jonas Olsson | 645b027 | 2018-02-15 15:16:27 +0100 | [diff] [blame] | 356 | // AECM only supports 16 kHz or lower sample rates. |
| 357 | RTC_DCHECK_LE(stream_properties_->sample_rate_hz, |
| 358 | AudioProcessing::kSampleRate16kHz); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 359 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 360 | cancellers_.resize( |
| 361 | NumCancellersRequired(stream_properties_->num_output_channels, |
| 362 | stream_properties_->num_reverse_channels)); |
| 363 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 364 | for (auto& canceller : cancellers_) { |
| 365 | if (!canceller) { |
| 366 | canceller.reset(new Canceller()); |
| 367 | } |
| 368 | canceller->Initialize(sample_rate_hz, external_echo_path_, |
| 369 | echo_path_size_bytes()); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 370 | } |
| 371 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 372 | Configure(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 373 | } |
| 374 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 375 | int EchoControlMobileImpl::Configure() { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 376 | rtc::CritScope cs_render(crit_render_); |
| 377 | rtc::CritScope cs_capture(crit_capture_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 378 | AecmConfig config; |
| 379 | config.cngMode = comfort_noise_enabled_; |
| 380 | config.echoMode = MapSetting(routing_mode_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 381 | int error = AudioProcessing::kNoError; |
| 382 | for (auto& canceller : cancellers_) { |
| 383 | int handle_error = WebRtcAecm_set_config(canceller->state(), config); |
| 384 | if (handle_error != AudioProcessing::kNoError) { |
| 385 | error = handle_error; |
| 386 | } |
| 387 | } |
| 388 | return error; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | } |
| 390 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 391 | } // namespace webrtc |