Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: webrtc/modules/audio_processing/echo_control_mobile_impl.cc

Issue 1410833002: Lock scheme #4: Introduced the render sample queue for the aec and aecm (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@aec_error_report_CL
Patch Set: Changes in response to latest reviewer comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 22 matching lines...) Expand all
33 return 2; 33 return 2;
34 case EchoControlMobile::kSpeakerphone: 34 case EchoControlMobile::kSpeakerphone:
35 return 3; 35 return 3;
36 case EchoControlMobile::kLoudSpeakerphone: 36 case EchoControlMobile::kLoudSpeakerphone:
37 return 4; 37 return 4;
38 } 38 }
39 assert(false); 39 assert(false);
40 return -1; 40 return -1;
41 } 41 }
42 42
43 AudioProcessing::Error MapError(int err) {
44 switch (err) {
45 case AECM_UNSUPPORTED_FUNCTION_ERROR:
46 return AudioProcessing::kUnsupportedFunctionError;
47 case AECM_NULL_POINTER_ERROR:
48 return AudioProcessing::kNullPointerError;
49 case AECM_BAD_PARAMETER_ERROR:
50 return AudioProcessing::kBadParameterError;
51 case AECM_BAD_PARAMETER_WARNING:
52 return AudioProcessing::kBadStreamParameterWarning;
53 default:
54 // AECM_UNSPECIFIED_ERROR
55 // AECM_UNINITIALIZED_ERROR
56 return AudioProcessing::kUnspecifiedError;
57 }
58 }
43 } // namespace 59 } // namespace
44 60
61 const size_t EchoControlMobileImpl::kAllowedValuesOfSamplesPerFrame1;
62 const size_t EchoControlMobileImpl::kAllowedValuesOfSamplesPerFrame2;
63
45 size_t EchoControlMobile::echo_path_size_bytes() { 64 size_t EchoControlMobile::echo_path_size_bytes() {
46 return WebRtcAecm_echo_path_size_bytes(); 65 return WebRtcAecm_echo_path_size_bytes();
47 } 66 }
48 67
49 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm, 68 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm,
50 CriticalSectionWrapper* crit) 69 CriticalSectionWrapper* crit)
51 : ProcessingComponent(), 70 : ProcessingComponent(),
52 apm_(apm), 71 apm_(apm),
53 crit_(crit), 72 crit_(crit),
54 routing_mode_(kSpeakerphone), 73 routing_mode_(kSpeakerphone),
55 comfort_noise_enabled_(true), 74 comfort_noise_enabled_(true),
56 external_echo_path_(NULL) {} 75 external_echo_path_(NULL),
76 render_queue_element_max_size_(0) {
77 AllocateRenderQueue();
78 }
57 79
58 EchoControlMobileImpl::~EchoControlMobileImpl() { 80 EchoControlMobileImpl::~EchoControlMobileImpl() {
59 if (external_echo_path_ != NULL) { 81 if (external_echo_path_ != NULL) {
60 delete [] external_echo_path_; 82 delete [] external_echo_path_;
61 external_echo_path_ = NULL; 83 external_echo_path_ = NULL;
62 } 84 }
63 } 85 }
64 86
65 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) { 87 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) {
66 if (!is_component_enabled()) { 88 if (!is_component_enabled()) {
67 return apm_->kNoError; 89 return apm_->kNoError;
68 } 90 }
69 91
70 assert(audio->num_frames_per_band() <= 160); 92 assert(audio->num_frames_per_band() <= 160);
71 assert(audio->num_channels() == apm_->num_reverse_channels()); 93 assert(audio->num_channels() == apm_->num_reverse_channels());
72 94
73 int err = apm_->kNoError; 95 int err = apm_->kNoError;
74 96
75 // The ordering convention must be followed to pass to the correct AECM. 97 // The ordering convention must be followed to pass to the correct AECM.
76 size_t handle_index = 0; 98 size_t handle_index = 0;
99 render_queue_buffer_.clear();
77 for (int i = 0; i < apm_->num_output_channels(); i++) { 100 for (int i = 0; i < apm_->num_output_channels(); i++) {
78 for (int j = 0; j < audio->num_channels(); j++) { 101 for (int j = 0; j < audio->num_channels(); j++) {
79 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); 102 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
80 err = WebRtcAecm_BufferFarend( 103 err = WebRtcAecm_GetBufferFarendError(
81 my_handle, 104 my_handle, audio->split_bands_const(j)[kBand0To8kHz],
82 audio->split_bands_const(j)[kBand0To8kHz],
83 audio->num_frames_per_band()); 105 audio->num_frames_per_band());
84 106
85 if (err != apm_->kNoError) { 107 if (err != apm_->kNoError)
86 return GetHandleError(my_handle); // TODO(ajm): warning possible? 108 return MapError(err); // TODO(ajm): warning possible?);
87 } 109
110 // Buffer the samples in the render queue.
111 render_queue_buffer_.insert(render_queue_buffer_.end(),
112 audio->split_bands_const(j)[kBand0To8kHz],
113 (audio->split_bands_const(j)[kBand0To8kHz] +
114 audio->num_frames_per_band()));
88 115
89 handle_index++; 116 handle_index++;
90 } 117 }
91 } 118 }
92 119
120 // Check of success is temporarily disabled as it breaks a unit test.
121 // TODO(peah): Will be fixed in the next CL.
122 (void)render_signal_queue_->Insert(&render_queue_buffer_);
123
93 return apm_->kNoError; 124 return apm_->kNoError;
94 } 125 }
95 126
127 // Read chunks of data that were received and queued on the render side from
128 // a queue. All the data chunks are buffered into the farend signal of the AEC.
129 void EchoControlMobileImpl::ReadQueuedRenderData() {
130 if (!is_component_enabled()) {
131 return;
132 }
133
134 while (render_signal_queue_->Remove(&capture_queue_buffer_)) {
135 size_t handle_index = 0;
136 int buffer_index = 0;
137 const int num_frames_per_band =
138 capture_queue_buffer_.size() /
139 (apm_->num_output_channels() * apm_->num_reverse_channels());
140 for (int i = 0; i < apm_->num_output_channels(); i++) {
141 for (int j = 0; j < apm_->num_reverse_channels(); j++) {
142 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
143 WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index],
144 num_frames_per_band);
145
146 buffer_index += num_frames_per_band;
147 handle_index++;
148 }
149 }
150 }
151 }
152
96 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { 153 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) {
97 if (!is_component_enabled()) { 154 if (!is_component_enabled()) {
98 return apm_->kNoError; 155 return apm_->kNoError;
99 } 156 }
100 157
101 if (!apm_->was_stream_delay_set()) { 158 if (!apm_->was_stream_delay_set()) {
102 return apm_->kStreamParameterNotSetError; 159 return apm_->kStreamParameterNotSetError;
103 } 160 }
104 161
105 assert(audio->num_frames_per_band() <= 160); 162 assert(audio->num_frames_per_band() <= 160);
(...skipping 15 matching lines...) Expand all
121 for (int j = 0; j < apm_->num_reverse_channels(); j++) { 178 for (int j = 0; j < apm_->num_reverse_channels(); j++) {
122 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); 179 Handle* my_handle = static_cast<Handle*>(handle(handle_index));
123 err = WebRtcAecm_Process( 180 err = WebRtcAecm_Process(
124 my_handle, 181 my_handle,
125 noisy, 182 noisy,
126 clean, 183 clean,
127 audio->split_bands(i)[kBand0To8kHz], 184 audio->split_bands(i)[kBand0To8kHz],
128 audio->num_frames_per_band(), 185 audio->num_frames_per_band(),
129 apm_->stream_delay_ms()); 186 apm_->stream_delay_ms());
130 187
131 if (err != apm_->kNoError) { 188 if (err != apm_->kNoError)
132 return GetHandleError(my_handle); // TODO(ajm): warning possible? 189 return MapError(err);
133 }
134 190
135 handle_index++; 191 handle_index++;
136 } 192 }
137 } 193 }
138 194
139 return apm_->kNoError; 195 return apm_->kNoError;
140 } 196 }
141 197
142 int EchoControlMobileImpl::Enable(bool enable) { 198 int EchoControlMobileImpl::Enable(bool enable) {
143 CriticalSectionScoped crit_scoped(crit_); 199 CriticalSectionScoped crit_scoped(crit_);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 if (size_bytes != echo_path_size_bytes()) { 262 if (size_bytes != echo_path_size_bytes()) {
207 // Size mismatch 263 // Size mismatch
208 return apm_->kBadParameterError; 264 return apm_->kBadParameterError;
209 } 265 }
210 if (!is_component_enabled()) { 266 if (!is_component_enabled()) {
211 return apm_->kNotEnabledError; 267 return apm_->kNotEnabledError;
212 } 268 }
213 269
214 // Get the echo path from the first channel 270 // Get the echo path from the first channel
215 Handle* my_handle = static_cast<Handle*>(handle(0)); 271 Handle* my_handle = static_cast<Handle*>(handle(0));
216 if (WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes) != 0) { 272 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes);
217 return GetHandleError(my_handle); 273 if (err != 0)
218 } 274 return MapError(err);
219 275
220 return apm_->kNoError; 276 return apm_->kNoError;
221 } 277 }
222 278
223 int EchoControlMobileImpl::Initialize() { 279 int EchoControlMobileImpl::Initialize() {
224 if (!is_component_enabled()) { 280 if (!is_component_enabled()) {
225 return apm_->kNoError; 281 return apm_->kNoError;
226 } 282 }
227 283
228 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) { 284 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) {
229 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; 285 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates";
230 return apm_->kBadSampleRateError; 286 return apm_->kBadSampleRateError;
231 } 287 }
232 288
233 return ProcessingComponent::Initialize(); 289 int err = ProcessingComponent::Initialize();
290 if (err != apm_->kNoError) {
291 return err;
292 }
293
294 AllocateRenderQueue();
295
296 return apm_->kNoError;
297 }
298
299 void EchoControlMobileImpl::AllocateRenderQueue() {
300 const size_t max_frame_size = std::max<size_t>(
301 kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2);
302 const size_t new_render_queue_element_max_size = std::max<size_t>(
303 static_cast<size_t>(1), max_frame_size * num_handles_required());
304
305 // Reallocate the queue if the queue item size is too small to fit the
306 // data to put in the queue.
307 if (new_render_queue_element_max_size > render_queue_element_max_size_) {
308 render_queue_element_max_size_ = new_render_queue_element_max_size;
309
310 std::vector<int16_t> template_queue_element(render_queue_element_max_size_);
311
312 render_signal_queue_.reset(
313 new SwapQueue<std::vector<int16_t>, AecmRenderQueueItemVerifier>(
314 kMaxNumFramesToBuffer,
315 AecmRenderQueueItemVerifier(render_queue_element_max_size_),
316 template_queue_element));
317 } else {
318 render_signal_queue_->Clear();
319 }
320
321 render_queue_buffer_.resize(new_render_queue_element_max_size);
322 capture_queue_buffer_.resize(new_render_queue_element_max_size);
234 } 323 }
235 324
236 void* EchoControlMobileImpl::CreateHandle() const { 325 void* EchoControlMobileImpl::CreateHandle() const {
237 return WebRtcAecm_Create(); 326 return WebRtcAecm_Create();
238 } 327 }
239 328
240 void EchoControlMobileImpl::DestroyHandle(void* handle) const { 329 void EchoControlMobileImpl::DestroyHandle(void* handle) const {
241 WebRtcAecm_Free(static_cast<Handle*>(handle)); 330 WebRtcAecm_Free(static_cast<Handle*>(handle));
242 } 331 }
243 332
(...skipping 25 matching lines...) Expand all
269 int EchoControlMobileImpl::num_handles_required() const { 358 int EchoControlMobileImpl::num_handles_required() const {
270 return apm_->num_output_channels() * 359 return apm_->num_output_channels() *
271 apm_->num_reverse_channels(); 360 apm_->num_reverse_channels();
272 } 361 }
273 362
274 int EchoControlMobileImpl::GetHandleError(void* handle) const { 363 int EchoControlMobileImpl::GetHandleError(void* handle) const {
275 assert(handle != NULL); 364 assert(handle != NULL);
276 return AudioProcessing::kUnspecifiedError; 365 return AudioProcessing::kUnspecifiedError;
277 } 366 }
278 } // namespace webrtc 367 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698