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 |
45 size_t EchoControlMobile::echo_path_size_bytes() { | 61 size_t EchoControlMobile::echo_path_size_bytes() { |
46 return WebRtcAecm_echo_path_size_bytes(); | 62 return WebRtcAecm_echo_path_size_bytes(); |
47 } | 63 } |
48 | 64 |
49 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm, | 65 EchoControlMobileImpl::EchoControlMobileImpl(const AudioProcessing* apm, |
50 CriticalSectionWrapper* crit) | 66 CriticalSectionWrapper* crit) |
51 : ProcessingComponent(), | 67 : ProcessingComponent(), |
52 apm_(apm), | 68 apm_(apm), |
53 crit_(crit), | 69 crit_(crit), |
54 routing_mode_(kSpeakerphone), | 70 routing_mode_(kSpeakerphone), |
55 comfort_noise_enabled_(true), | 71 comfort_noise_enabled_(true), |
56 external_echo_path_(NULL) {} | 72 external_echo_path_(NULL), |
| 73 render_queue_buffer_(kMaxNumSamplesPerFrameToBuffer), |
| 74 capture_queue_buffer_(kMaxNumSamplesPerFrameToBuffer) { |
| 75 std::vector<int16_t> template_queue_element(kMaxNumSamplesPerFrameToBuffer); |
| 76 |
| 77 render_signal_queue_.reset( |
| 78 new SwapQueue<std::vector<int16_t>, &RenderQueueItemVerifier>( |
| 79 (kMaxNumFramesToBuffer * kMaxNumChannelsPerFrameToBuffer), |
| 80 template_queue_element)); |
| 81 } |
57 | 82 |
58 EchoControlMobileImpl::~EchoControlMobileImpl() { | 83 EchoControlMobileImpl::~EchoControlMobileImpl() { |
59 if (external_echo_path_ != NULL) { | 84 if (external_echo_path_ != NULL) { |
60 delete [] external_echo_path_; | 85 delete [] external_echo_path_; |
61 external_echo_path_ = NULL; | 86 external_echo_path_ = NULL; |
62 } | 87 } |
63 } | 88 } |
64 | 89 |
65 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) { | 90 int EchoControlMobileImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
66 if (!is_component_enabled()) { | 91 if (!is_component_enabled()) { |
67 return apm_->kNoError; | 92 return apm_->kNoError; |
68 } | 93 } |
69 | 94 |
70 assert(audio->num_frames_per_band() <= 160); | 95 assert(audio->num_frames_per_band() <= 160); |
71 assert(audio->num_channels() == apm_->num_reverse_channels()); | 96 assert(audio->num_channels() == apm_->num_reverse_channels()); |
72 | 97 |
73 int err = apm_->kNoError; | 98 int err = apm_->kNoError; |
74 | 99 |
75 // The ordering convention must be followed to pass to the correct AECM. | 100 // The ordering convention must be followed to pass to the correct AECM. |
76 size_t handle_index = 0; | 101 size_t handle_index = 0; |
77 for (int i = 0; i < apm_->num_output_channels(); i++) { | 102 for (int i = 0; i < apm_->num_output_channels(); i++) { |
78 for (int j = 0; j < audio->num_channels(); j++) { | 103 for (int j = 0; j < audio->num_channels(); j++) { |
79 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 104 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
80 err = WebRtcAecm_BufferFarend( | 105 err = WebRtcAecm_GetBufferFarendError( |
81 my_handle, | 106 my_handle, audio->split_bands_const(j)[kBand0To8kHz], |
82 audio->split_bands_const(j)[kBand0To8kHz], | |
83 audio->num_frames_per_band()); | 107 audio->num_frames_per_band()); |
84 | 108 |
85 if (err != apm_->kNoError) { | 109 if (err != apm_->kNoError) |
86 return GetHandleError(my_handle); // TODO(ajm): warning possible? | 110 return MapError(err); // TODO(ajm): warning possible?); |
87 } | 111 |
| 112 // Buffer the samples in the render queue. |
| 113 // TODO(peah): Do a proper size check agains the actual sample size |
| 114 // once the APM properly checks the sample type passed to AECM. |
| 115 memcpy(&render_queue_buffer_[0], |
| 116 audio->split_bands_const_f(j)[kBand0To8kHz], |
| 117 (audio->num_frames_per_band() * sizeof(int16_t))); |
| 118 render_queue_buffer_.resize(audio->num_frames_per_band()); |
| 119 // TODO(peah): Refactor so that it is possible to check the |
| 120 // return value of Insert. Currently, that is not possible |
| 121 // due to the code design when the capture thread is never |
| 122 // started. |
| 123 render_signal_queue_->Insert(&render_queue_buffer_); |
88 | 124 |
89 handle_index++; | 125 handle_index++; |
90 } | 126 } |
91 } | 127 } |
92 | 128 |
93 return apm_->kNoError; | 129 return apm_->kNoError; |
94 } | 130 } |
95 | 131 |
| 132 // Read chunks of data that were received and queued on the render side from |
| 133 // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 134 void EchoControlMobileImpl::ReadQueuedRenderData() { |
| 135 if (!is_component_enabled()) { |
| 136 return; |
| 137 } |
| 138 |
| 139 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 140 size_t handle_index = 0; |
| 141 for (int i = 0; i < apm_->num_output_channels(); i++) { |
| 142 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
| 143 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
| 144 (void)WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[0], |
| 145 capture_queue_buffer_.size()); |
| 146 } |
| 147 } |
| 148 } |
| 149 } |
| 150 |
96 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 151 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
97 if (!is_component_enabled()) { | 152 if (!is_component_enabled()) { |
98 return apm_->kNoError; | 153 return apm_->kNoError; |
99 } | 154 } |
100 | 155 |
101 if (!apm_->was_stream_delay_set()) { | 156 if (!apm_->was_stream_delay_set()) { |
102 return apm_->kStreamParameterNotSetError; | 157 return apm_->kStreamParameterNotSetError; |
103 } | 158 } |
104 | 159 |
105 assert(audio->num_frames_per_band() <= 160); | 160 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++) { | 176 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
122 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 177 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
123 err = WebRtcAecm_Process( | 178 err = WebRtcAecm_Process( |
124 my_handle, | 179 my_handle, |
125 noisy, | 180 noisy, |
126 clean, | 181 clean, |
127 audio->split_bands(i)[kBand0To8kHz], | 182 audio->split_bands(i)[kBand0To8kHz], |
128 audio->num_frames_per_band(), | 183 audio->num_frames_per_band(), |
129 apm_->stream_delay_ms()); | 184 apm_->stream_delay_ms()); |
130 | 185 |
131 if (err != apm_->kNoError) { | 186 if (err != apm_->kNoError) |
132 return GetHandleError(my_handle); // TODO(ajm): warning possible? | 187 return MapError(err); |
133 } | |
134 | 188 |
135 handle_index++; | 189 handle_index++; |
136 } | 190 } |
137 } | 191 } |
138 | 192 |
139 return apm_->kNoError; | 193 return apm_->kNoError; |
140 } | 194 } |
141 | 195 |
142 int EchoControlMobileImpl::Enable(bool enable) { | 196 int EchoControlMobileImpl::Enable(bool enable) { |
143 CriticalSectionScoped crit_scoped(crit_); | 197 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()) { | 260 if (size_bytes != echo_path_size_bytes()) { |
207 // Size mismatch | 261 // Size mismatch |
208 return apm_->kBadParameterError; | 262 return apm_->kBadParameterError; |
209 } | 263 } |
210 if (!is_component_enabled()) { | 264 if (!is_component_enabled()) { |
211 return apm_->kNotEnabledError; | 265 return apm_->kNotEnabledError; |
212 } | 266 } |
213 | 267 |
214 // Get the echo path from the first channel | 268 // Get the echo path from the first channel |
215 Handle* my_handle = static_cast<Handle*>(handle(0)); | 269 Handle* my_handle = static_cast<Handle*>(handle(0)); |
216 if (WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes) != 0) { | 270 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes); |
217 return GetHandleError(my_handle); | 271 if (err != 0) |
218 } | 272 return MapError(err); |
219 | 273 |
220 return apm_->kNoError; | 274 return apm_->kNoError; |
221 } | 275 } |
222 | 276 |
223 int EchoControlMobileImpl::Initialize() { | 277 int EchoControlMobileImpl::Initialize() { |
224 if (!is_component_enabled()) { | 278 if (!is_component_enabled()) { |
225 return apm_->kNoError; | 279 return apm_->kNoError; |
226 } | 280 } |
227 | 281 |
| 282 render_signal_queue_->Clear(); |
| 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 return ProcessingComponent::Initialize(); |
234 } | 290 } |
235 | 291 |
236 void* EchoControlMobileImpl::CreateHandle() const { | 292 void* EchoControlMobileImpl::CreateHandle() const { |
237 return WebRtcAecm_Create(); | 293 return WebRtcAecm_Create(); |
(...skipping 30 matching lines...) Expand all Loading... |
268 | 324 |
269 int EchoControlMobileImpl::num_handles_required() const { | 325 int EchoControlMobileImpl::num_handles_required() const { |
270 return apm_->num_output_channels() * | 326 return apm_->num_output_channels() * |
271 apm_->num_reverse_channels(); | 327 apm_->num_reverse_channels(); |
272 } | 328 } |
273 | 329 |
274 int EchoControlMobileImpl::GetHandleError(void* handle) const { | 330 int EchoControlMobileImpl::GetHandleError(void* handle) const { |
275 assert(handle != NULL); | 331 assert(handle != NULL); |
276 return AudioProcessing::kUnspecifiedError; | 332 return AudioProcessing::kUnspecifiedError; |
277 } | 333 } |
| 334 |
| 335 bool EchoControlMobileImpl::RenderQueueItemVerifier( |
| 336 const std::vector<int16_t>& v) { |
| 337 return ((v.size() == kMinNumSamplesPerFrameToBuffer) || |
| 338 (v.size() == kMaxNumSamplesPerFrameToBuffer)); |
| 339 } |
| 340 |
278 } // namespace webrtc | 341 } // namespace webrtc |
OLD | NEW |