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 |
(...skipping 22 matching lines...) Loading... | |
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_.resize(0); | |
kwiberg-webrtc
2015/11/05 12:20:03
.clear()
peah-webrtc
2015/11/06 06:55:04
Done.
| |
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_); | |
hlundin-webrtc
2015/11/05 12:45:26
Drop (void).
peah-webrtc
2015/11/06 06:55:04
I think it is needed, as the Insert is annotated w
hlundin-webrtc
2015/11/06 07:05:13
Acknowledged.
| |
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 (void)WebRtcAecm_BufferFarend(my_handle, | |
144 &capture_queue_buffer_[buffer_index], | |
145 num_frames_per_band); | |
146 | |
147 buffer_index += num_frames_per_band; | |
148 handle_index++; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 | |
96 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 154 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
97 if (!is_component_enabled()) { | 155 if (!is_component_enabled()) { |
98 return apm_->kNoError; | 156 return apm_->kNoError; |
99 } | 157 } |
100 | 158 |
101 if (!apm_->was_stream_delay_set()) { | 159 if (!apm_->was_stream_delay_set()) { |
102 return apm_->kStreamParameterNotSetError; | 160 return apm_->kStreamParameterNotSetError; |
103 } | 161 } |
104 | 162 |
105 assert(audio->num_frames_per_band() <= 160); | 163 assert(audio->num_frames_per_band() <= 160); |
(...skipping 15 matching lines...) Loading... | |
121 for (int j = 0; j < apm_->num_reverse_channels(); j++) { | 179 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
122 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 180 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
123 err = WebRtcAecm_Process( | 181 err = WebRtcAecm_Process( |
124 my_handle, | 182 my_handle, |
125 noisy, | 183 noisy, |
126 clean, | 184 clean, |
127 audio->split_bands(i)[kBand0To8kHz], | 185 audio->split_bands(i)[kBand0To8kHz], |
128 audio->num_frames_per_band(), | 186 audio->num_frames_per_band(), |
129 apm_->stream_delay_ms()); | 187 apm_->stream_delay_ms()); |
130 | 188 |
131 if (err != apm_->kNoError) { | 189 if (err != apm_->kNoError) |
132 return GetHandleError(my_handle); // TODO(ajm): warning possible? | 190 return MapError(err); |
133 } | |
134 | 191 |
135 handle_index++; | 192 handle_index++; |
136 } | 193 } |
137 } | 194 } |
138 | 195 |
139 return apm_->kNoError; | 196 return apm_->kNoError; |
140 } | 197 } |
141 | 198 |
142 int EchoControlMobileImpl::Enable(bool enable) { | 199 int EchoControlMobileImpl::Enable(bool enable) { |
143 CriticalSectionScoped crit_scoped(crit_); | 200 CriticalSectionScoped crit_scoped(crit_); |
(...skipping 62 matching lines...) Loading... | |
206 if (size_bytes != echo_path_size_bytes()) { | 263 if (size_bytes != echo_path_size_bytes()) { |
207 // Size mismatch | 264 // Size mismatch |
208 return apm_->kBadParameterError; | 265 return apm_->kBadParameterError; |
209 } | 266 } |
210 if (!is_component_enabled()) { | 267 if (!is_component_enabled()) { |
211 return apm_->kNotEnabledError; | 268 return apm_->kNotEnabledError; |
212 } | 269 } |
213 | 270 |
214 // Get the echo path from the first channel | 271 // Get the echo path from the first channel |
215 Handle* my_handle = static_cast<Handle*>(handle(0)); | 272 Handle* my_handle = static_cast<Handle*>(handle(0)); |
216 if (WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes) != 0) { | 273 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes); |
217 return GetHandleError(my_handle); | 274 if (err != 0) |
218 } | 275 return MapError(err); |
219 | 276 |
220 return apm_->kNoError; | 277 return apm_->kNoError; |
221 } | 278 } |
222 | 279 |
223 int EchoControlMobileImpl::Initialize() { | 280 int EchoControlMobileImpl::Initialize() { |
224 if (!is_component_enabled()) { | 281 if (!is_component_enabled()) { |
225 return apm_->kNoError; | 282 return apm_->kNoError; |
226 } | 283 } |
227 | 284 |
228 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) { | 285 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) { |
229 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; | 286 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; |
230 return apm_->kBadSampleRateError; | 287 return apm_->kBadSampleRateError; |
231 } | 288 } |
232 | 289 |
233 return ProcessingComponent::Initialize(); | 290 int err = ProcessingComponent::Initialize(); |
291 if (err != apm_->kNoError) { | |
292 return err; | |
293 } | |
294 | |
295 AllocateRenderQueue(); | |
296 | |
297 return apm_->kNoError; | |
298 } | |
299 | |
300 void EchoControlMobileImpl::AllocateRenderQueue() { | |
301 const size_t max_frame_size = std::max(kAllowedValuesOfSamplesPerFrame1, | |
302 kAllowedValuesOfSamplesPerFrame2); | |
303 const size_t new_render_queue_element_max_size = | |
304 std::max(1UL, max_frame_size * num_handles_required()); | |
hlundin-webrtc
2015/11/05 12:45:26
Same question again about 1UL.
peah-webrtc
2015/11/06 06:55:04
Done.
| |
305 | |
306 // Reallocate the queue if the queue item size is too small to fit the | |
307 // data to put in the queue. | |
308 if (new_render_queue_element_max_size > render_queue_element_max_size_) { | |
309 render_queue_element_max_size_ = new_render_queue_element_max_size; | |
310 | |
311 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); | |
312 | |
313 render_signal_queue_.reset( | |
314 new SwapQueue<std::vector<int16_t>, AecmRenderQueueItemVerifier>( | |
315 kMaxNumFramesToBuffer, | |
316 AecmRenderQueueItemVerifier(render_queue_element_max_size_), | |
317 template_queue_element)); | |
318 } else { | |
319 render_signal_queue_->Clear(); | |
320 } | |
321 | |
322 render_queue_buffer_.resize(new_render_queue_element_max_size); | |
323 capture_queue_buffer_.resize(new_render_queue_element_max_size); | |
234 } | 324 } |
235 | 325 |
236 void* EchoControlMobileImpl::CreateHandle() const { | 326 void* EchoControlMobileImpl::CreateHandle() const { |
237 return WebRtcAecm_Create(); | 327 return WebRtcAecm_Create(); |
238 } | 328 } |
239 | 329 |
240 void EchoControlMobileImpl::DestroyHandle(void* handle) const { | 330 void EchoControlMobileImpl::DestroyHandle(void* handle) const { |
241 WebRtcAecm_Free(static_cast<Handle*>(handle)); | 331 WebRtcAecm_Free(static_cast<Handle*>(handle)); |
242 } | 332 } |
243 | 333 |
(...skipping 24 matching lines...) Loading... | |
268 | 358 |
269 int EchoControlMobileImpl::num_handles_required() const { | 359 int EchoControlMobileImpl::num_handles_required() const { |
270 return apm_->num_output_channels() * | 360 return apm_->num_output_channels() * |
271 apm_->num_reverse_channels(); | 361 apm_->num_reverse_channels(); |
272 } | 362 } |
273 | 363 |
274 int EchoControlMobileImpl::GetHandleError(void* handle) const { | 364 int EchoControlMobileImpl::GetHandleError(void* handle) const { |
275 assert(handle != NULL); | 365 assert(handle != NULL); |
276 return AudioProcessing::kUnspecifiedError; | 366 return AudioProcessing::kUnspecifiedError; |
277 } | 367 } |
368 | |
hlundin-webrtc
2015/11/05 12:45:26
Why?
peah-webrtc
2015/11/06 06:55:04
Done.
| |
278 } // namespace webrtc | 369 } // namespace webrtc |
OLD | NEW |