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...) Expand all 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_.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 // Insert the samples into the queue. |
| 121 if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
| 122 ReadQueuedRenderData(); |
| 123 |
| 124 // Retry the insert (should always work). |
| 125 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
| 126 } |
| 127 |
93 return apm_->kNoError; | 128 return apm_->kNoError; |
94 } | 129 } |
95 | 130 |
| 131 // Read chunks of data that were received and queued on the render side from |
| 132 // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 133 void EchoControlMobileImpl::ReadQueuedRenderData() { |
| 134 if (!is_component_enabled()) { |
| 135 return; |
| 136 } |
| 137 |
| 138 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 139 size_t handle_index = 0; |
| 140 int buffer_index = 0; |
| 141 const int num_frames_per_band = |
| 142 capture_queue_buffer_.size() / |
| 143 (apm_->num_output_channels() * apm_->num_reverse_channels()); |
| 144 for (int i = 0; i < apm_->num_output_channels(); i++) { |
| 145 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
| 146 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
| 147 WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], |
| 148 num_frames_per_band); |
| 149 |
| 150 buffer_index += num_frames_per_band; |
| 151 handle_index++; |
| 152 } |
| 153 } |
| 154 } |
| 155 } |
| 156 |
96 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 157 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
97 if (!is_component_enabled()) { | 158 if (!is_component_enabled()) { |
98 return apm_->kNoError; | 159 return apm_->kNoError; |
99 } | 160 } |
100 | 161 |
101 if (!apm_->was_stream_delay_set()) { | 162 if (!apm_->was_stream_delay_set()) { |
102 return apm_->kStreamParameterNotSetError; | 163 return apm_->kStreamParameterNotSetError; |
103 } | 164 } |
104 | 165 |
105 assert(audio->num_frames_per_band() <= 160); | 166 assert(audio->num_frames_per_band() <= 160); |
(...skipping 15 matching lines...) Expand all Loading... |
121 for (int j = 0; j < apm_->num_reverse_channels(); j++) { | 182 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
122 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 183 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
123 err = WebRtcAecm_Process( | 184 err = WebRtcAecm_Process( |
124 my_handle, | 185 my_handle, |
125 noisy, | 186 noisy, |
126 clean, | 187 clean, |
127 audio->split_bands(i)[kBand0To8kHz], | 188 audio->split_bands(i)[kBand0To8kHz], |
128 audio->num_frames_per_band(), | 189 audio->num_frames_per_band(), |
129 apm_->stream_delay_ms()); | 190 apm_->stream_delay_ms()); |
130 | 191 |
131 if (err != apm_->kNoError) { | 192 if (err != apm_->kNoError) |
132 return GetHandleError(my_handle); // TODO(ajm): warning possible? | 193 return MapError(err); |
133 } | |
134 | 194 |
135 handle_index++; | 195 handle_index++; |
136 } | 196 } |
137 } | 197 } |
138 | 198 |
139 return apm_->kNoError; | 199 return apm_->kNoError; |
140 } | 200 } |
141 | 201 |
142 int EchoControlMobileImpl::Enable(bool enable) { | 202 int EchoControlMobileImpl::Enable(bool enable) { |
143 CriticalSectionScoped crit_scoped(crit_); | 203 CriticalSectionScoped crit_scoped(crit_); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 if (size_bytes != echo_path_size_bytes()) { | 266 if (size_bytes != echo_path_size_bytes()) { |
207 // Size mismatch | 267 // Size mismatch |
208 return apm_->kBadParameterError; | 268 return apm_->kBadParameterError; |
209 } | 269 } |
210 if (!is_component_enabled()) { | 270 if (!is_component_enabled()) { |
211 return apm_->kNotEnabledError; | 271 return apm_->kNotEnabledError; |
212 } | 272 } |
213 | 273 |
214 // Get the echo path from the first channel | 274 // Get the echo path from the first channel |
215 Handle* my_handle = static_cast<Handle*>(handle(0)); | 275 Handle* my_handle = static_cast<Handle*>(handle(0)); |
216 if (WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes) != 0) { | 276 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes); |
217 return GetHandleError(my_handle); | 277 if (err != 0) |
218 } | 278 return MapError(err); |
219 | 279 |
220 return apm_->kNoError; | 280 return apm_->kNoError; |
221 } | 281 } |
222 | 282 |
223 int EchoControlMobileImpl::Initialize() { | 283 int EchoControlMobileImpl::Initialize() { |
224 if (!is_component_enabled()) { | 284 if (!is_component_enabled()) { |
225 return apm_->kNoError; | 285 return apm_->kNoError; |
226 } | 286 } |
227 | 287 |
228 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) { | 288 if (apm_->proc_sample_rate_hz() > apm_->kSampleRate16kHz) { |
229 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; | 289 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; |
230 return apm_->kBadSampleRateError; | 290 return apm_->kBadSampleRateError; |
231 } | 291 } |
232 | 292 |
233 return ProcessingComponent::Initialize(); | 293 int err = ProcessingComponent::Initialize(); |
| 294 if (err != apm_->kNoError) { |
| 295 return err; |
| 296 } |
| 297 |
| 298 AllocateRenderQueue(); |
| 299 |
| 300 return apm_->kNoError; |
| 301 } |
| 302 |
| 303 void EchoControlMobileImpl::AllocateRenderQueue() { |
| 304 const size_t max_frame_size = std::max<size_t>( |
| 305 kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2); |
| 306 const size_t new_render_queue_element_max_size = std::max<size_t>( |
| 307 static_cast<size_t>(1), max_frame_size * num_handles_required()); |
| 308 |
| 309 // Reallocate the queue if the queue item size is too small to fit the |
| 310 // data to put in the queue. |
| 311 if (new_render_queue_element_max_size > render_queue_element_max_size_) { |
| 312 render_queue_element_max_size_ = new_render_queue_element_max_size; |
| 313 |
| 314 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
| 315 |
| 316 render_signal_queue_.reset( |
| 317 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
| 318 kMaxNumFramesToBuffer, template_queue_element, |
| 319 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
| 320 } else { |
| 321 render_signal_queue_->Clear(); |
| 322 } |
| 323 |
| 324 render_queue_buffer_.resize(new_render_queue_element_max_size); |
| 325 capture_queue_buffer_.resize(new_render_queue_element_max_size); |
234 } | 326 } |
235 | 327 |
236 void* EchoControlMobileImpl::CreateHandle() const { | 328 void* EchoControlMobileImpl::CreateHandle() const { |
237 return WebRtcAecm_Create(); | 329 return WebRtcAecm_Create(); |
238 } | 330 } |
239 | 331 |
240 void EchoControlMobileImpl::DestroyHandle(void* handle) const { | 332 void EchoControlMobileImpl::DestroyHandle(void* handle) const { |
241 WebRtcAecm_Free(static_cast<Handle*>(handle)); | 333 WebRtcAecm_Free(static_cast<Handle*>(handle)); |
242 } | 334 } |
243 | 335 |
(...skipping 25 matching lines...) Expand all Loading... |
269 int EchoControlMobileImpl::num_handles_required() const { | 361 int EchoControlMobileImpl::num_handles_required() const { |
270 return apm_->num_output_channels() * | 362 return apm_->num_output_channels() * |
271 apm_->num_reverse_channels(); | 363 apm_->num_reverse_channels(); |
272 } | 364 } |
273 | 365 |
274 int EchoControlMobileImpl::GetHandleError(void* handle) const { | 366 int EchoControlMobileImpl::GetHandleError(void* handle) const { |
275 assert(handle != NULL); | 367 assert(handle != NULL); |
276 return AudioProcessing::kUnspecifiedError; | 368 return AudioProcessing::kUnspecifiedError; |
277 } | 369 } |
278 } // namespace webrtc | 370 } // namespace webrtc |
OLD | NEW |