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> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 15 | #include "modules/audio_processing/aecm/echo_control_mobile.h" |
| 16 | #include "modules/audio_processing/audio_buffer.h" |
| 17 | #include "rtc_base/constructormagic.h" |
| 18 | #include "rtc_base/logging.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | namespace { |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 23 | int16_t MapSetting(EchoControlMobile::RoutingMode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | switch (mode) { |
| 25 | case EchoControlMobile::kQuietEarpieceOrHeadset: |
| 26 | return 0; |
| 27 | case EchoControlMobile::kEarpiece: |
| 28 | return 1; |
| 29 | case EchoControlMobile::kLoudEarpiece: |
| 30 | return 2; |
| 31 | case EchoControlMobile::kSpeakerphone: |
| 32 | return 3; |
| 33 | case EchoControlMobile::kLoudSpeakerphone: |
| 34 | return 4; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | } |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 36 | RTC_NOTREACHED(); |
mflodman@webrtc.org | 657b2a4 | 2012-02-06 11:06:01 +0000 | [diff] [blame] | 37 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | } |
| 39 | |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 40 | AudioProcessing::Error MapError(int err) { |
| 41 | switch (err) { |
| 42 | case AECM_UNSUPPORTED_FUNCTION_ERROR: |
| 43 | return AudioProcessing::kUnsupportedFunctionError; |
| 44 | case AECM_NULL_POINTER_ERROR: |
| 45 | return AudioProcessing::kNullPointerError; |
| 46 | case AECM_BAD_PARAMETER_ERROR: |
| 47 | return AudioProcessing::kBadParameterError; |
| 48 | case AECM_BAD_PARAMETER_WARNING: |
| 49 | return AudioProcessing::kBadStreamParameterWarning; |
| 50 | default: |
| 51 | // AECM_UNSPECIFIED_ERROR |
| 52 | // AECM_UNINITIALIZED_ERROR |
| 53 | return AudioProcessing::kUnspecifiedError; |
| 54 | } |
| 55 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } // namespace |
| 57 | |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 58 | size_t EchoControlMobile::echo_path_size_bytes() { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 59 | return WebRtcAecm_echo_path_size_bytes(); |
ajm@google.com | 22e6515 | 2011-07-18 18:03:01 +0000 | [diff] [blame] | 60 | } |
| 61 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 62 | struct EchoControlMobileImpl::StreamProperties { |
| 63 | StreamProperties() = delete; |
| 64 | StreamProperties(int sample_rate_hz, |
| 65 | size_t num_reverse_channels, |
| 66 | size_t num_output_channels) |
| 67 | : sample_rate_hz(sample_rate_hz), |
| 68 | num_reverse_channels(num_reverse_channels), |
| 69 | num_output_channels(num_output_channels) {} |
| 70 | |
| 71 | int sample_rate_hz; |
| 72 | size_t num_reverse_channels; |
| 73 | size_t num_output_channels; |
| 74 | }; |
| 75 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 76 | class EchoControlMobileImpl::Canceller { |
| 77 | public: |
| 78 | Canceller() { |
| 79 | state_ = WebRtcAecm_Create(); |
| 80 | RTC_CHECK(state_); |
| 81 | } |
| 82 | |
| 83 | ~Canceller() { |
| 84 | RTC_DCHECK(state_); |
| 85 | WebRtcAecm_Free(state_); |
| 86 | } |
| 87 | |
| 88 | void* state() { |
| 89 | RTC_DCHECK(state_); |
| 90 | return state_; |
| 91 | } |
| 92 | |
| 93 | void Initialize(int sample_rate_hz, |
| 94 | unsigned char* external_echo_path, |
| 95 | size_t echo_path_size_bytes) { |
| 96 | RTC_DCHECK(state_); |
| 97 | int error = WebRtcAecm_Init(state_, sample_rate_hz); |
| 98 | RTC_DCHECK_EQ(AudioProcessing::kNoError, error); |
| 99 | if (external_echo_path != NULL) { |
| 100 | error = WebRtcAecm_InitEchoPath(state_, external_echo_path, |
| 101 | echo_path_size_bytes); |
| 102 | RTC_DCHECK_EQ(AudioProcessing::kNoError, error); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | void* state_; |
| 108 | RTC_DISALLOW_COPY_AND_ASSIGN(Canceller); |
| 109 | }; |
| 110 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 111 | EchoControlMobileImpl::EchoControlMobileImpl(rtc::CriticalSection* crit_render, |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 112 | rtc::CriticalSection* crit_capture) |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 113 | : crit_render_(crit_render), |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 114 | crit_capture_(crit_capture), |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 115 | routing_mode_(kSpeakerphone), |
| 116 | comfort_noise_enabled_(true), |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 117 | external_echo_path_(NULL) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 118 | RTC_DCHECK(crit_render); |
| 119 | RTC_DCHECK(crit_capture); |
| 120 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 122 | EchoControlMobileImpl::~EchoControlMobileImpl() { |
| 123 | if (external_echo_path_ != NULL) { |
| 124 | delete [] external_echo_path_; |
| 125 | external_echo_path_ = NULL; |
| 126 | } |
| 127 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 129 | void EchoControlMobileImpl::ProcessRenderAudio( |
| 130 | rtc::ArrayView<const int16_t> packed_render_audio) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 131 | rtc::CritScope cs_capture(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 132 | if (!enabled_) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 136 | RTC_DCHECK(stream_properties_); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 137 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 138 | size_t buffer_index = 0; |
| 139 | size_t num_frames_per_band = |
| 140 | packed_render_audio.size() / (stream_properties_->num_output_channels * |
| 141 | stream_properties_->num_reverse_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 142 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 143 | for (auto& canceller : cancellers_) { |
| 144 | WebRtcAecm_BufferFarend(canceller->state(), |
| 145 | &packed_render_audio[buffer_index], |
| 146 | num_frames_per_band); |
| 147 | |
| 148 | buffer_index += num_frames_per_band; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void EchoControlMobileImpl::PackRenderAudioBuffer( |
| 153 | const AudioBuffer* audio, |
| 154 | size_t num_output_channels, |
| 155 | size_t num_channels, |
| 156 | std::vector<int16_t>* packed_buffer) { |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 157 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 158 | RTC_DCHECK_EQ(num_channels, audio->num_channels()); |
| 159 | |
| 160 | // The ordering convention must be followed to pass to the correct AECM. |
| 161 | packed_buffer->clear(); |
| 162 | int render_channel = 0; |
| 163 | for (size_t i = 0; i < num_output_channels; i++) { |
| 164 | for (size_t j = 0; j < audio->num_channels(); j++) { |
| 165 | // Buffer the samples in the render queue. |
| 166 | packed_buffer->insert( |
| 167 | packed_buffer->end(), |
| 168 | audio->split_bands_const(render_channel)[kBand0To8kHz], |
| 169 | (audio->split_bands_const(render_channel)[kBand0To8kHz] + |
| 170 | audio->num_frames_per_band())); |
| 171 | render_channel = (render_channel + 1) % audio->num_channels(); |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
peah | a062460 | 2016-10-25 04:45:24 -0700 | [diff] [blame] | 176 | size_t EchoControlMobileImpl::NumCancellersRequired( |
| 177 | size_t num_output_channels, |
| 178 | size_t num_reverse_channels) { |
| 179 | return num_output_channels * num_reverse_channels; |
| 180 | } |
| 181 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 182 | int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio, |
| 183 | int stream_delay_ms) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 184 | rtc::CritScope cs_capture(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 185 | if (!enabled_) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 186 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 187 | } |
| 188 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 189 | RTC_DCHECK(stream_properties_); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 190 | RTC_DCHECK_GE(160, audio->num_frames_per_band()); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 191 | RTC_DCHECK_EQ(audio->num_channels(), stream_properties_->num_output_channels); |
| 192 | RTC_DCHECK_GE(cancellers_.size(), stream_properties_->num_reverse_channels * |
| 193 | audio->num_channels()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 195 | int err = AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | |
| 197 | // The ordering convention must be followed to pass to the correct AECM. |
| 198 | size_t handle_index = 0; |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 199 | for (size_t capture = 0; capture < audio->num_channels(); ++capture) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 200 | // TODO(ajm): improve how this works, possibly inside AECM. |
| 201 | // This is kind of hacked up. |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 202 | const int16_t* noisy = audio->low_pass_reference(capture); |
| 203 | const int16_t* clean = audio->split_bands_const(capture)[kBand0To8kHz]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | if (noisy == NULL) { |
| 205 | noisy = clean; |
| 206 | clean = NULL; |
| 207 | } |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 208 | for (size_t render = 0; render < stream_properties_->num_reverse_channels; |
| 209 | ++render) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 210 | err = WebRtcAecm_Process(cancellers_[handle_index]->state(), noisy, clean, |
| 211 | audio->split_bands(capture)[kBand0To8kHz], |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 212 | audio->num_frames_per_band(), stream_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 214 | if (err != AudioProcessing::kNoError) { |
peah | fa6228e | 2015-11-16 16:27:42 -0800 | [diff] [blame] | 215 | return MapError(err); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 216 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 217 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 218 | ++handle_index; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 219 | } |
aluebs | 776593b | 2016-03-15 14:04:58 -0700 | [diff] [blame] | 220 | for (size_t band = 1u; band < audio->num_bands(); ++band) { |
| 221 | memset(audio->split_bands(capture)[band], |
| 222 | 0, |
| 223 | audio->num_frames_per_band() * |
| 224 | sizeof(audio->split_bands(capture)[band][0])); |
| 225 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | } |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 227 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int EchoControlMobileImpl::Enable(bool enable) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | // Ensure AEC and AECM are not both enabled. |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 232 | rtc::CritScope cs_render(crit_render_); |
| 233 | rtc::CritScope cs_capture(crit_capture_); |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 234 | RTC_DCHECK(stream_properties_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 236 | if (enable && |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 237 | stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 238 | return AudioProcessing::kBadSampleRateError; |
| 239 | } |
| 240 | |
| 241 | if (enable && !enabled_) { |
| 242 | enabled_ = enable; // Must be set before Initialize() is called. |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 243 | |
| 244 | // TODO(peah): Simplify once the Enable function has been removed from |
| 245 | // the public APM API. |
| 246 | Initialize(stream_properties_->sample_rate_hz, |
| 247 | stream_properties_->num_reverse_channels, |
| 248 | stream_properties_->num_output_channels); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 249 | } else { |
| 250 | enabled_ = enable; |
| 251 | } |
| 252 | return AudioProcessing::kNoError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | bool EchoControlMobileImpl::is_enabled() const { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 256 | rtc::CritScope cs(crit_capture_); |
peah | bb9edbd | 2016-03-10 12:54:25 -0800 | [diff] [blame] | 257 | return enabled_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | if (MapSetting(mode) == -1) { |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 262 | return AudioProcessing::kBadParameterError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 263 | } |
| 264 | |
peah | df3efa8 | 2015-11-28 12:35:15 -0800 | [diff] [blame] | 265 | { |
| 266 | rtc::CritScope cs(crit_capture_); |
| 267 | routing_mode_ = mode; |
| 268 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | return Configure(); |
| 270 | } |
| 271 | |
| 272 | EchoControlMobile::RoutingMode EchoControlMobileImpl::routing_mode() |
| 273 | 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 | |
peah | 253534d | 2016-03-15 04:32:28 -0700 | [diff] [blame] | 356 | if (stream_properties_->sample_rate_hz > AudioProcessing::kSampleRate16kHz) { |
perkj | dfc2870 | 2016-03-09 16:23:23 -0800 | [diff] [blame] | 357 | LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 358 | } |
| 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 |