Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" | 11 #include "webrtc/modules/audio_processing/echo_control_mobile_impl.h" |
| 12 | 12 |
| 13 #include <assert.h> | |
| 14 #include <string.h> | 13 #include <string.h> |
| 15 | 14 |
| 16 #include "webrtc/modules/audio_processing/aecm/echo_control_mobile.h" | 15 #include "webrtc/modules/audio_processing/aecm/echo_control_mobile.h" |
| 17 #include "webrtc/modules/audio_processing/audio_buffer.h" | 16 #include "webrtc/modules/audio_processing/audio_buffer.h" |
| 18 #include "webrtc/system_wrappers/include/logging.h" | 17 #include "webrtc/system_wrappers/include/logging.h" |
| 19 | 18 |
| 20 namespace webrtc { | 19 namespace webrtc { |
| 21 | 20 |
| 22 typedef void Handle; | |
| 23 | |
| 24 namespace { | 21 namespace { |
| 25 int16_t MapSetting(EchoControlMobile::RoutingMode mode) { | 22 int16_t MapSetting(EchoControlMobile::RoutingMode mode) { |
| 26 switch (mode) { | 23 switch (mode) { |
| 27 case EchoControlMobile::kQuietEarpieceOrHeadset: | 24 case EchoControlMobile::kQuietEarpieceOrHeadset: |
| 28 return 0; | 25 return 0; |
| 29 case EchoControlMobile::kEarpiece: | 26 case EchoControlMobile::kEarpiece: |
| 30 return 1; | 27 return 1; |
| 31 case EchoControlMobile::kLoudEarpiece: | 28 case EchoControlMobile::kLoudEarpiece: |
| 32 return 2; | 29 return 2; |
| 33 case EchoControlMobile::kSpeakerphone: | 30 case EchoControlMobile::kSpeakerphone: |
| 34 return 3; | 31 return 3; |
| 35 case EchoControlMobile::kLoudSpeakerphone: | 32 case EchoControlMobile::kLoudSpeakerphone: |
| 36 return 4; | 33 return 4; |
| 37 } | 34 } |
| 38 assert(false); | 35 RTC_DCHECK(false); |
| 39 return -1; | 36 return -1; |
| 40 } | 37 } |
| 41 | 38 |
| 42 AudioProcessing::Error MapError(int err) { | 39 AudioProcessing::Error MapError(int err) { |
| 43 switch (err) { | 40 switch (err) { |
| 44 case AECM_UNSUPPORTED_FUNCTION_ERROR: | 41 case AECM_UNSUPPORTED_FUNCTION_ERROR: |
| 45 return AudioProcessing::kUnsupportedFunctionError; | 42 return AudioProcessing::kUnsupportedFunctionError; |
| 46 case AECM_NULL_POINTER_ERROR: | 43 case AECM_NULL_POINTER_ERROR: |
| 47 return AudioProcessing::kNullPointerError; | 44 return AudioProcessing::kNullPointerError; |
| 48 case AECM_BAD_PARAMETER_ERROR: | 45 case AECM_BAD_PARAMETER_ERROR: |
| 49 return AudioProcessing::kBadParameterError; | 46 return AudioProcessing::kBadParameterError; |
| 50 case AECM_BAD_PARAMETER_WARNING: | 47 case AECM_BAD_PARAMETER_WARNING: |
| 51 return AudioProcessing::kBadStreamParameterWarning; | 48 return AudioProcessing::kBadStreamParameterWarning; |
| 52 default: | 49 default: |
| 53 // AECM_UNSPECIFIED_ERROR | 50 // AECM_UNSPECIFIED_ERROR |
| 54 // AECM_UNINITIALIZED_ERROR | 51 // AECM_UNINITIALIZED_ERROR |
| 55 return AudioProcessing::kUnspecifiedError; | 52 return AudioProcessing::kUnspecifiedError; |
| 56 } | 53 } |
| 57 } | 54 } |
| 58 // Maximum length that a frame of samples can have. | 55 // Maximum length that a frame of samples can have. |
| 59 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; | 56 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; |
| 60 // Maximum number of frames to buffer in the render queue. | 57 // Maximum number of frames to buffer in the render queue. |
| 61 // TODO(peah): Decrease this once we properly handle hugely unbalanced | 58 // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 62 // reverse and forward call numbers. | 59 // reverse and forward call numbers. |
| 63 static const size_t kMaxNumFramesToBuffer = 100; | 60 static const size_t kMaxNumFramesToBuffer = 100; |
| 64 } // namespace | 61 } // namespace |
| 65 | 62 |
| 66 size_t EchoControlMobile::echo_path_size_bytes() { | 63 size_t EchoControlMobile::echo_path_size_bytes() { |
| 67 return WebRtcAecm_echo_path_size_bytes(); | 64 return WebRtcAecm_echo_path_size_bytes(); |
| 68 } | 65 } |
| 69 | 66 |
| 67 class EchoControlMobileImpl::Canceller { | |
| 68 public: | |
| 69 Canceller() { | |
| 70 state_ = WebRtcAecm_Create(); | |
| 71 RTC_CHECK(state_); | |
| 72 } | |
| 73 | |
| 74 ~Canceller() { | |
| 75 RTC_DCHECK(state_); | |
| 76 WebRtcAecm_Free(state_); | |
| 77 } | |
| 78 | |
| 79 void* state() { | |
| 80 RTC_DCHECK(state_); | |
| 81 return state_; | |
| 82 } | |
| 83 | |
| 84 void Initialize(int sample_rate_hz, | |
| 85 unsigned char* external_echo_path, | |
| 86 size_t echo_path_size_bytes) { | |
| 87 RTC_DCHECK(state_); | |
| 88 int error = WebRtcAecm_Init(state_, sample_rate_hz); | |
| 89 RTC_DCHECK_EQ(AudioProcessing::kNoError, error); | |
| 90 if (external_echo_path != NULL) { | |
| 91 error = WebRtcAecm_InitEchoPath(state_, external_echo_path, | |
| 92 echo_path_size_bytes); | |
| 93 RTC_DCHECK_EQ(AudioProcessing::kNoError, error); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 void* state_; | |
| 99 RTC_DISALLOW_COPY_AND_ASSIGN(Canceller); | |
| 100 }; | |
| 101 | |
| 70 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm, | 102 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm, |
| 71 rtc::CriticalSection* crit_render, | 103 rtc::CriticalSection* crit_render, |
| 72 rtc::CriticalSection* crit_capture) | 104 rtc::CriticalSection* crit_capture) |
| 73 : ProcessingComponent(), | 105 : apm_(apm), |
| 74 apm_(apm), | |
| 75 crit_render_(crit_render), | 106 crit_render_(crit_render), |
| 76 crit_capture_(crit_capture), | 107 crit_capture_(crit_capture), |
| 77 routing_mode_(kSpeakerphone), | 108 routing_mode_(kSpeakerphone), |
| 78 comfort_noise_enabled_(true), | 109 comfort_noise_enabled_(true), |
| 79 external_echo_path_(NULL), | 110 external_echo_path_(NULL), |
| 80 render_queue_element_max_size_(0) { | 111 render_queue_element_max_size_(0) { |
| 81 RTC_DCHECK(apm); | 112 RTC_DCHECK(apm); |
| 82 RTC_DCHECK(crit_render); | 113 RTC_DCHECK(crit_render); |
| 83 RTC_DCHECK(crit_capture); | 114 RTC_DCHECK(crit_capture); |
| 84 } | 115 } |
| 85 | 116 |
| 86 EchoControlMobileImpl::~EchoControlMobileImpl() { | 117 EchoControlMobileImpl::~EchoControlMobileImpl() { |
| 87 if (external_echo_path_ != NULL) { | 118 if (external_echo_path_ != NULL) { |
| 88 delete [] external_echo_path_; | 119 delete [] external_echo_path_; |
| 89 external_echo_path_ = NULL; | 120 external_echo_path_ = NULL; |
| 90 } | 121 } |
| 91 } | 122 } |
| 92 | 123 |
| 93 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) { | 124 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
| 94 rtc::CritScope cs_render(crit_render_); | 125 rtc::CritScope cs_render(crit_render_); |
| 95 | 126 if (!enabled_) { |
| 96 if (!is_component_enabled()) { | |
| 97 return AudioProcessing::kNoError; | 127 return AudioProcessing::kNoError; |
| 98 } | 128 } |
| 99 | 129 |
| 100 assert(audio->num_frames_per_band() <= 160); | 130 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
| 101 assert(audio->num_channels() == apm_->num_reverse_channels()); | 131 RTC_DCHECK_EQ(audio->num_channels(), apm_->num_reverse_channels()); |
| 132 RTC_DCHECK_GE(cancellers_.size(), | |
| 133 apm_->num_output_channels() * audio->num_channels()); | |
| 102 | 134 |
| 103 int err = AudioProcessing::kNoError; | 135 int err = AudioProcessing::kNoError; |
| 104 // The ordering convention must be followed to pass to the correct AECM. | 136 // The ordering convention must be followed to pass to the correct AECM. |
| 105 size_t handle_index = 0; | |
| 106 render_queue_buffer_.clear(); | 137 render_queue_buffer_.clear(); |
| 107 for (size_t i = 0; i < apm_->num_output_channels(); i++) { | 138 int render_channel = 0; |
| 108 for (size_t j = 0; j < audio->num_channels(); j++) { | 139 for (auto& canceller : cancellers_) { |
| 109 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 140 err = WebRtcAecm_GetBufferFarendError( |
| 110 err = WebRtcAecm_GetBufferFarendError( | 141 canceller->state(), |
| 111 my_handle, audio->split_bands_const(j)[kBand0To8kHz], | 142 audio->split_bands_const(render_channel)[kBand0To8kHz], |
| 112 audio->num_frames_per_band()); | 143 audio->num_frames_per_band()); |
| 113 | 144 |
| 114 if (err != AudioProcessing::kNoError) | 145 if (err != AudioProcessing::kNoError) |
| 115 return MapError(err); // TODO(ajm): warning possible?); | 146 return MapError(err); // TODO(ajm): warning possible?); |
| 116 | 147 |
| 117 // Buffer the samples in the render queue. | 148 // Buffer the samples in the render queue. |
| 118 render_queue_buffer_.insert(render_queue_buffer_.end(), | 149 render_queue_buffer_.insert( |
| 119 audio->split_bands_const(j)[kBand0To8kHz], | 150 render_queue_buffer_.end(), |
| 120 (audio->split_bands_const(j)[kBand0To8kHz] + | 151 audio->split_bands_const(render_channel)[kBand0To8kHz], |
| 121 audio->num_frames_per_band())); | 152 (audio->split_bands_const(render_channel)[kBand0To8kHz] + |
| 122 | 153 audio->num_frames_per_band())); |
| 123 handle_index++; | 154 render_channel = (render_channel + 1) % audio->num_channels(); |
| 124 } | |
| 125 } | 155 } |
| 126 | 156 |
| 127 // Insert the samples into the queue. | 157 // Insert the samples into the queue. |
| 128 if (!render_signal_queue_->Insert(&render_queue_buffer_)) { | 158 if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
| 129 // The data queue is full and needs to be emptied. | 159 // The data queue is full and needs to be emptied. |
| 130 ReadQueuedRenderData(); | 160 ReadQueuedRenderData(); |
| 131 | 161 |
| 132 // Retry the insert (should always work). | 162 // Retry the insert (should always work). |
| 133 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); | 163 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
| 134 } | 164 } |
| 135 | 165 |
| 136 return AudioProcessing::kNoError; | 166 return AudioProcessing::kNoError; |
| 137 } | 167 } |
| 138 | 168 |
| 139 // Read chunks of data that were received and queued on the render side from | 169 // Read chunks of data that were received and queued on the render side from |
| 140 // a queue. All the data chunks are buffered into the farend signal of the AEC. | 170 // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 141 void EchoControlMobileImpl::ReadQueuedRenderData() { | 171 void EchoControlMobileImpl::ReadQueuedRenderData() { |
| 142 rtc::CritScope cs_capture(crit_capture_); | 172 rtc::CritScope cs_capture(crit_capture_); |
| 143 | 173 |
| 144 if (!is_component_enabled()) { | 174 if (!enabled_) { |
| 145 return; | 175 return; |
| 146 } | 176 } |
| 147 | 177 |
| 148 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { | 178 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 149 size_t handle_index = 0; | |
| 150 size_t buffer_index = 0; | 179 size_t buffer_index = 0; |
| 151 const size_t num_frames_per_band = | 180 size_t num_frames_per_band = |
| 152 capture_queue_buffer_.size() / | 181 capture_queue_buffer_.size() / |
| 153 (apm_->num_output_channels() * apm_->num_reverse_channels()); | 182 (apm_->num_output_channels() * apm_->num_reverse_channels()); |
| 154 for (size_t i = 0; i < apm_->num_output_channels(); i++) { | |
| 155 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { | |
| 156 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | |
| 157 WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], | |
| 158 num_frames_per_band); | |
| 159 | 183 |
| 160 buffer_index += num_frames_per_band; | 184 for (auto& canceller : cancellers_) { |
| 161 handle_index++; | 185 WebRtcAecm_BufferFarend(canceller->state(), |
| 162 } | 186 &capture_queue_buffer_[buffer_index], |
| 187 num_frames_per_band); | |
| 188 | |
| 189 buffer_index += num_frames_per_band; | |
| 163 } | 190 } |
| 164 } | 191 } |
| 165 } | 192 } |
| 166 | 193 |
| 167 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 194 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
| 168 rtc::CritScope cs_capture(crit_capture_); | 195 rtc::CritScope cs_capture(crit_capture_); |
| 169 | 196 if (!enabled_) { |
| 170 if (!is_component_enabled()) { | |
| 171 return AudioProcessing::kNoError; | 197 return AudioProcessing::kNoError; |
| 172 } | 198 } |
| 173 | 199 |
| 174 if (!apm_->was_stream_delay_set()) { | 200 if (!apm_->was_stream_delay_set()) { |
| 175 return AudioProcessing::kStreamParameterNotSetError; | 201 return AudioProcessing::kStreamParameterNotSetError; |
| 176 } | 202 } |
| 177 | 203 |
| 178 assert(audio->num_frames_per_band() <= 160); | 204 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
| 179 assert(audio->num_channels() == apm_->num_output_channels()); | 205 RTC_DCHECK_EQ(audio->num_channels(), apm_->num_output_channels()); |
| 206 RTC_DCHECK_LE(audio->num_channels() * apm_->num_reverse_channels(), | |
|
the sun
2016/03/09 21:46:52
nit: same but opposite condition on line 132
peah-webrtc
2016/03/10 07:23:23
Good catch, I'll make them similar.
Done.
| |
| 207 cancellers_.size()); | |
| 180 | 208 |
| 181 int err = AudioProcessing::kNoError; | 209 int err = AudioProcessing::kNoError; |
| 182 | 210 |
| 183 // The ordering convention must be followed to pass to the correct AECM. | 211 // The ordering convention must be followed to pass to the correct AECM. |
| 184 size_t handle_index = 0; | 212 size_t handle_index = 0; |
| 185 for (size_t i = 0; i < audio->num_channels(); i++) { | 213 for (size_t capture = 0; capture < audio->num_channels(); ++capture) { |
| 186 // TODO(ajm): improve how this works, possibly inside AECM. | 214 // TODO(ajm): improve how this works, possibly inside AECM. |
| 187 // This is kind of hacked up. | 215 // This is kind of hacked up. |
| 188 const int16_t* noisy = audio->low_pass_reference(i); | 216 const int16_t* noisy = audio->low_pass_reference(capture); |
| 189 const int16_t* clean = audio->split_bands_const(i)[kBand0To8kHz]; | 217 const int16_t* clean = audio->split_bands_const(capture)[kBand0To8kHz]; |
| 190 if (noisy == NULL) { | 218 if (noisy == NULL) { |
| 191 noisy = clean; | 219 noisy = clean; |
| 192 clean = NULL; | 220 clean = NULL; |
| 193 } | 221 } |
| 194 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { | 222 for (size_t render = 0; render < apm_->num_reverse_channels(); ++render) { |
| 195 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 223 err = WebRtcAecm_Process(cancellers_[handle_index++]->state(), noisy, |
| 196 err = WebRtcAecm_Process( | 224 clean, audio->split_bands(capture)[kBand0To8kHz], |
| 197 my_handle, | 225 audio->num_frames_per_band(), |
| 198 noisy, | 226 apm_->stream_delay_ms()); |
| 199 clean, | |
| 200 audio->split_bands(i)[kBand0To8kHz], | |
| 201 audio->num_frames_per_band(), | |
| 202 apm_->stream_delay_ms()); | |
| 203 | 227 |
| 204 if (err != AudioProcessing::kNoError) | 228 if (err != AudioProcessing::kNoError) { |
| 205 return MapError(err); | 229 return MapError(err); |
| 230 } | |
| 206 | 231 |
| 207 handle_index++; | 232 ++handle_index; |
| 208 } | 233 } |
| 209 } | 234 } |
| 210 | |
| 211 return AudioProcessing::kNoError; | 235 return AudioProcessing::kNoError; |
| 212 } | 236 } |
| 213 | 237 |
| 214 int EchoControlMobileImpl::Enable(bool enable) { | 238 int EchoControlMobileImpl::Enable(bool enable) { |
| 215 // Ensure AEC and AECM are not both enabled. | 239 // Ensure AEC and AECM are not both enabled. |
| 216 rtc::CritScope cs_render(crit_render_); | 240 rtc::CritScope cs_render(crit_render_); |
| 217 rtc::CritScope cs_capture(crit_capture_); | 241 rtc::CritScope cs_capture(crit_capture_); |
| 218 // The is_enabled call is safe from a deadlock perspective | 242 // The is_enabled call is safe from a deadlock perspective |
| 219 // as both locks are allready held in the correct order. | 243 // as both locks are allready held in the correct order. |
| 220 if (enable && apm_->echo_cancellation()->is_enabled()) { | 244 if (enable && apm_->echo_cancellation()->is_enabled()) { |
| 221 return AudioProcessing::kBadParameterError; | 245 return AudioProcessing::kBadParameterError; |
| 222 } | 246 } |
| 223 | 247 |
| 224 return EnableComponent(enable); | 248 if (enable && !enabled_) { |
| 249 enabled_ = enable; // Must be set before Initialize() is called. | |
| 250 Initialize(); | |
| 251 } else { | |
| 252 enabled_ = enable; | |
| 253 } | |
| 254 return AudioProcessing::kNoError; | |
| 225 } | 255 } |
| 226 | 256 |
| 227 bool EchoControlMobileImpl::is_enabled() const { | 257 bool EchoControlMobileImpl::is_enabled() const { |
| 228 rtc::CritScope cs(crit_capture_); | 258 rtc::CritScope cs(crit_capture_); |
| 229 return is_component_enabled(); | 259 return enabled_; |
| 230 } | 260 } |
| 231 | 261 |
| 232 int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { | 262 int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { |
| 233 if (MapSetting(mode) == -1) { | 263 if (MapSetting(mode) == -1) { |
| 234 return AudioProcessing::kBadParameterError; | 264 return AudioProcessing::kBadParameterError; |
| 235 } | 265 } |
| 236 | 266 |
| 237 { | 267 { |
| 238 rtc::CritScope cs(crit_capture_); | 268 rtc::CritScope cs(crit_capture_); |
| 239 routing_mode_ = mode; | 269 routing_mode_ = mode; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 // Size mismatch | 302 // Size mismatch |
| 273 return AudioProcessing::kBadParameterError; | 303 return AudioProcessing::kBadParameterError; |
| 274 } | 304 } |
| 275 | 305 |
| 276 if (external_echo_path_ == NULL) { | 306 if (external_echo_path_ == NULL) { |
| 277 external_echo_path_ = new unsigned char[size_bytes]; | 307 external_echo_path_ = new unsigned char[size_bytes]; |
| 278 } | 308 } |
| 279 memcpy(external_echo_path_, echo_path, size_bytes); | 309 memcpy(external_echo_path_, echo_path, size_bytes); |
| 280 } | 310 } |
| 281 | 311 |
| 282 return Initialize(); | 312 Initialize(); |
| 313 return AudioProcessing::kNoError; | |
| 283 } | 314 } |
| 284 | 315 |
| 285 int EchoControlMobileImpl::GetEchoPath(void* echo_path, | 316 int EchoControlMobileImpl::GetEchoPath(void* echo_path, |
| 286 size_t size_bytes) const { | 317 size_t size_bytes) const { |
| 287 rtc::CritScope cs(crit_capture_); | 318 rtc::CritScope cs(crit_capture_); |
| 288 if (echo_path == NULL) { | 319 if (echo_path == NULL) { |
| 289 return AudioProcessing::kNullPointerError; | 320 return AudioProcessing::kNullPointerError; |
| 290 } | 321 } |
| 291 if (size_bytes != echo_path_size_bytes()) { | 322 if (size_bytes != echo_path_size_bytes()) { |
| 292 // Size mismatch | 323 // Size mismatch |
| 293 return AudioProcessing::kBadParameterError; | 324 return AudioProcessing::kBadParameterError; |
| 294 } | 325 } |
| 295 if (!is_component_enabled()) { | 326 if (!enabled_) { |
| 296 return AudioProcessing::kNotEnabledError; | 327 return AudioProcessing::kNotEnabledError; |
| 297 } | 328 } |
| 298 | 329 |
| 299 // Get the echo path from the first channel | 330 // Get the echo path from the first channel |
| 300 Handle* my_handle = static_cast<Handle*>(handle(0)); | 331 int32_t err = |
| 301 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes); | 332 WebRtcAecm_GetEchoPath(cancellers_[0]->state(), echo_path, size_bytes); |
| 302 if (err != 0) | 333 if (err != 0) { |
| 303 return MapError(err); | 334 return MapError(err); |
| 335 } | |
| 304 | 336 |
| 305 return AudioProcessing::kNoError; | 337 return AudioProcessing::kNoError; |
| 306 } | 338 } |
| 307 | 339 |
| 308 int EchoControlMobileImpl::Initialize() { | 340 void EchoControlMobileImpl::Initialize() { |
| 309 { | 341 rtc::CritScope cs_render(crit_render_); |
| 310 rtc::CritScope cs_capture(crit_capture_); | 342 rtc::CritScope cs_capture(crit_capture_); |
| 311 if (!is_component_enabled()) { | 343 if (!enabled_) { |
| 312 return AudioProcessing::kNoError; | 344 return; |
| 313 } | |
| 314 } | 345 } |
| 315 | 346 |
| 316 if (apm_->proc_sample_rate_hz() > AudioProcessing::kSampleRate16kHz) { | 347 if (apm_->proc_sample_rate_hz() > AudioProcessing::kSampleRate16kHz) { |
| 317 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; | 348 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; |
| 318 return AudioProcessing::kBadSampleRateError; | |
| 319 } | 349 } |
| 320 | 350 |
| 321 int err = ProcessingComponent::Initialize(); | 351 int sample_rate_hz = apm_->proc_sample_rate_hz(); |
| 322 if (err != AudioProcessing::kNoError) { | 352 cancellers_.resize(num_handles_required()); |
| 323 return err; | 353 for (auto& canceller : cancellers_) { |
| 354 if (!canceller) { | |
| 355 canceller.reset(new Canceller()); | |
| 356 } | |
| 357 canceller->Initialize(sample_rate_hz, external_echo_path_, | |
| 358 echo_path_size_bytes()); | |
| 324 } | 359 } |
| 325 | 360 |
| 361 Configure(); | |
| 362 | |
| 326 AllocateRenderQueue(); | 363 AllocateRenderQueue(); |
| 327 | |
| 328 return AudioProcessing::kNoError; | |
| 329 } | 364 } |
| 330 | 365 |
| 331 void EchoControlMobileImpl::AllocateRenderQueue() { | 366 void EchoControlMobileImpl::AllocateRenderQueue() { |
| 332 const size_t new_render_queue_element_max_size = std::max<size_t>( | 367 const size_t new_render_queue_element_max_size = std::max<size_t>( |
| 333 static_cast<size_t>(1), | 368 static_cast<size_t>(1), |
| 334 kMaxAllowedValuesOfSamplesPerFrame * num_handles_required()); | 369 kMaxAllowedValuesOfSamplesPerFrame * num_handles_required()); |
| 335 | 370 |
| 336 rtc::CritScope cs_render(crit_render_); | 371 rtc::CritScope cs_render(crit_render_); |
| 337 rtc::CritScope cs_capture(crit_capture_); | 372 rtc::CritScope cs_capture(crit_capture_); |
| 338 | 373 |
| 339 // Reallocate the queue if the queue item size is too small to fit the | 374 // Reallocate the queue if the queue item size is too small to fit the |
| 340 // data to put in the queue. | 375 // data to put in the queue. |
| 341 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { | 376 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
| 342 render_queue_element_max_size_ = new_render_queue_element_max_size; | 377 render_queue_element_max_size_ = new_render_queue_element_max_size; |
| 343 | 378 |
| 344 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); | 379 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
| 345 | 380 |
| 346 render_signal_queue_.reset( | 381 render_signal_queue_.reset( |
| 347 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( | 382 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 348 kMaxNumFramesToBuffer, template_queue_element, | 383 kMaxNumFramesToBuffer, template_queue_element, |
| 349 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); | 384 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
| 350 | 385 |
| 351 render_queue_buffer_.resize(render_queue_element_max_size_); | 386 render_queue_buffer_.resize(render_queue_element_max_size_); |
| 352 capture_queue_buffer_.resize(render_queue_element_max_size_); | 387 capture_queue_buffer_.resize(render_queue_element_max_size_); |
| 353 } else { | 388 } else { |
| 354 render_signal_queue_->Clear(); | 389 render_signal_queue_->Clear(); |
| 355 } | 390 } |
| 356 } | 391 } |
| 357 | 392 |
| 358 void* EchoControlMobileImpl::CreateHandle() const { | 393 int EchoControlMobileImpl::Configure() { |
| 359 return WebRtcAecm_Create(); | |
| 360 } | |
| 361 | |
| 362 void EchoControlMobileImpl::DestroyHandle(void* handle) const { | |
| 363 // This method is only called in a non-concurrent manner during APM | |
| 364 // destruction. | |
| 365 WebRtcAecm_Free(static_cast<Handle*>(handle)); | |
| 366 } | |
| 367 | |
| 368 int EchoControlMobileImpl::InitializeHandle(void* handle) const { | |
| 369 rtc::CritScope cs_render(crit_render_); | |
| 370 rtc::CritScope cs_capture(crit_capture_); | |
| 371 assert(handle != NULL); | |
| 372 Handle* my_handle = static_cast<Handle*>(handle); | |
| 373 if (WebRtcAecm_Init(my_handle, apm_->proc_sample_rate_hz()) != 0) { | |
| 374 return GetHandleError(my_handle); | |
| 375 } | |
| 376 if (external_echo_path_ != NULL) { | |
| 377 if (WebRtcAecm_InitEchoPath(my_handle, | |
| 378 external_echo_path_, | |
| 379 echo_path_size_bytes()) != 0) { | |
| 380 return GetHandleError(my_handle); | |
| 381 } | |
| 382 } | |
| 383 | |
| 384 return AudioProcessing::kNoError; | |
| 385 } | |
| 386 | |
| 387 int EchoControlMobileImpl::ConfigureHandle(void* handle) const { | |
| 388 rtc::CritScope cs_render(crit_render_); | 394 rtc::CritScope cs_render(crit_render_); |
| 389 rtc::CritScope cs_capture(crit_capture_); | 395 rtc::CritScope cs_capture(crit_capture_); |
| 390 AecmConfig config; | 396 AecmConfig config; |
| 391 config.cngMode = comfort_noise_enabled_; | 397 config.cngMode = comfort_noise_enabled_; |
| 392 config.echoMode = MapSetting(routing_mode_); | 398 config.echoMode = MapSetting(routing_mode_); |
| 393 | 399 int error = AudioProcessing::kNoError; |
| 394 return WebRtcAecm_set_config(static_cast<Handle*>(handle), config); | 400 for (auto& canceller : cancellers_) { |
| 401 int handle_error = WebRtcAecm_set_config(canceller->state(), config); | |
| 402 if (handle_error != AudioProcessing::kNoError) { | |
| 403 error = handle_error; | |
| 404 } | |
| 405 } | |
| 406 return error; | |
| 395 } | 407 } |
| 396 | 408 |
| 397 size_t EchoControlMobileImpl::num_handles_required() const { | 409 size_t EchoControlMobileImpl::num_handles_required() const { |
| 398 // Not locked as it only relies on APM public API which is threadsafe. | 410 // Not locked as it only relies on APM public API which is threadsafe. |
| 399 return apm_->num_output_channels() * apm_->num_reverse_channels(); | 411 return apm_->num_output_channels() * apm_->num_reverse_channels(); |
| 400 } | 412 } |
| 401 | |
| 402 int EchoControlMobileImpl::GetHandleError(void* handle) const { | |
| 403 // Not locked as it does not rely on anything in the state. | |
| 404 assert(handle != NULL); | |
| 405 return AudioProcessing::kUnspecifiedError; | |
| 406 } | |
| 407 } // namespace webrtc | 413 } // namespace webrtc |
| OLD | NEW |