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> | 13 #include <assert.h> |
14 #include <string.h> | 14 #include <string.h> |
15 | 15 |
16 #include "webrtc/base/constructormagic.h" | |
the sun
2016/03/09 09:23:14
Not needed.
peah-webrtc
2016/03/09 12:34:53
Done.
| |
16 #include "webrtc/modules/audio_processing/aecm/echo_control_mobile.h" | 17 #include "webrtc/modules/audio_processing/aecm/echo_control_mobile.h" |
17 #include "webrtc/modules/audio_processing/audio_buffer.h" | 18 #include "webrtc/modules/audio_processing/audio_buffer.h" |
18 #include "webrtc/system_wrappers/include/logging.h" | 19 #include "webrtc/system_wrappers/include/logging.h" |
19 | 20 |
20 namespace webrtc { | 21 namespace webrtc { |
21 | 22 |
22 typedef void Handle; | 23 typedef void Handle; |
23 | 24 |
24 namespace { | 25 namespace { |
25 int16_t MapSetting(EchoControlMobile::RoutingMode mode) { | 26 int16_t MapSetting(EchoControlMobile::RoutingMode mode) { |
26 switch (mode) { | 27 switch (mode) { |
27 case EchoControlMobile::kQuietEarpieceOrHeadset: | 28 case EchoControlMobile::kQuietEarpieceOrHeadset: |
28 return 0; | 29 return 0; |
29 case EchoControlMobile::kEarpiece: | 30 case EchoControlMobile::kEarpiece: |
30 return 1; | 31 return 1; |
31 case EchoControlMobile::kLoudEarpiece: | 32 case EchoControlMobile::kLoudEarpiece: |
32 return 2; | 33 return 2; |
33 case EchoControlMobile::kSpeakerphone: | 34 case EchoControlMobile::kSpeakerphone: |
34 return 3; | 35 return 3; |
35 case EchoControlMobile::kLoudSpeakerphone: | 36 case EchoControlMobile::kLoudSpeakerphone: |
36 return 4; | 37 return 4; |
37 } | 38 } |
38 assert(false); | 39 assert(false); |
the sun
2016/03/09 09:23:14
If you're replacing assert() with RTC_DCHECK(), he
| |
39 return -1; | 40 return -1; |
40 } | 41 } |
41 | 42 |
42 AudioProcessing::Error MapError(int err) { | 43 AudioProcessing::Error MapError(int err) { |
43 switch (err) { | 44 switch (err) { |
44 case AECM_UNSUPPORTED_FUNCTION_ERROR: | 45 case AECM_UNSUPPORTED_FUNCTION_ERROR: |
45 return AudioProcessing::kUnsupportedFunctionError; | 46 return AudioProcessing::kUnsupportedFunctionError; |
46 case AECM_NULL_POINTER_ERROR: | 47 case AECM_NULL_POINTER_ERROR: |
47 return AudioProcessing::kNullPointerError; | 48 return AudioProcessing::kNullPointerError; |
48 case AECM_BAD_PARAMETER_ERROR: | 49 case AECM_BAD_PARAMETER_ERROR: |
49 return AudioProcessing::kBadParameterError; | 50 return AudioProcessing::kBadParameterError; |
50 case AECM_BAD_PARAMETER_WARNING: | 51 case AECM_BAD_PARAMETER_WARNING: |
51 return AudioProcessing::kBadStreamParameterWarning; | 52 return AudioProcessing::kBadStreamParameterWarning; |
52 default: | 53 default: |
53 // AECM_UNSPECIFIED_ERROR | 54 // AECM_UNSPECIFIED_ERROR |
54 // AECM_UNINITIALIZED_ERROR | 55 // AECM_UNINITIALIZED_ERROR |
55 return AudioProcessing::kUnspecifiedError; | 56 return AudioProcessing::kUnspecifiedError; |
56 } | 57 } |
57 } | 58 } |
58 // Maximum length that a frame of samples can have. | 59 // Maximum length that a frame of samples can have. |
59 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; | 60 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; |
60 // Maximum number of frames to buffer in the render queue. | 61 // Maximum number of frames to buffer in the render queue. |
61 // TODO(peah): Decrease this once we properly handle hugely unbalanced | 62 // TODO(peah): Decrease this once we properly handle hugely unbalanced |
62 // reverse and forward call numbers. | 63 // reverse and forward call numbers. |
63 static const size_t kMaxNumFramesToBuffer = 100; | 64 static const size_t kMaxNumFramesToBuffer = 100; |
64 } // namespace | 65 } // namespace |
65 | 66 |
66 size_t EchoControlMobile::echo_path_size_bytes() { | 67 size_t EchoControlMobile::echo_path_size_bytes() { |
67 return WebRtcAecm_echo_path_size_bytes(); | 68 return WebRtcAecm_echo_path_size_bytes(); |
68 } | 69 } |
69 | 70 |
71 class EchoControlMobileImpl::Canceller { | |
72 public: | |
73 Canceller() { | |
74 state_ = WebRtcAecm_Create(); | |
75 RTC_CHECK(state_); | |
76 } | |
77 | |
78 ~Canceller() { | |
79 RTC_DCHECK(state_); | |
80 WebRtcAecm_Free(state_); | |
81 } | |
82 | |
83 Handle* state() { return state_; } | |
84 | |
85 void Initialize(int sample_rate_hz, | |
86 unsigned char* external_echo_path, | |
87 size_t echo_path_size_bytes) { | |
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, | |
the sun
2016/03/09 09:23:14
nit: since you're DCHECKing in the dtor, you shoul
peah-webrtc
2016/03/09 12:34:53
This makes a lot of sense. Since the class is so s
the sun
2016/03/09 12:46:11
I don't understand. Why did you remove the DCHECK
peah-webrtc
2016/03/09 14:29:52
Sorry, I misunderstood you. Now I get it :-)
Done.
| |
92 echo_path_size_bytes); | |
93 RTC_DCHECK_EQ(AudioProcessing::kNoError, error); | |
94 } | |
95 } | |
96 | |
97 private: | |
98 Handle* 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; | 137 size_t handle_index = 0; |
106 render_queue_buffer_.clear(); | 138 render_queue_buffer_.clear(); |
107 for (size_t i = 0; i < apm_->num_output_channels(); i++) { | 139 for (size_t i = 0; i < apm_->num_output_channels(); i++) { |
108 for (size_t j = 0; j < audio->num_channels(); j++) { | 140 for (size_t j = 0; j < audio->num_channels(); j++) { |
109 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 141 Handle* my_handle = cancellers_[handle_index++]->state(); |
110 err = WebRtcAecm_GetBufferFarendError( | 142 err = WebRtcAecm_GetBufferFarendError( |
111 my_handle, audio->split_bands_const(j)[kBand0To8kHz], | 143 my_handle, audio->split_bands_const(j)[kBand0To8kHz], |
112 audio->num_frames_per_band()); | 144 audio->num_frames_per_band()); |
113 | 145 |
114 if (err != AudioProcessing::kNoError) | 146 if (err != AudioProcessing::kNoError) |
115 return MapError(err); // TODO(ajm): warning possible?); | 147 return MapError(err); // TODO(ajm): warning possible?); |
116 | 148 |
117 // Buffer the samples in the render queue. | 149 // Buffer the samples in the render queue. |
118 render_queue_buffer_.insert(render_queue_buffer_.end(), | 150 render_queue_buffer_.insert(render_queue_buffer_.end(), |
119 audio->split_bands_const(j)[kBand0To8kHz], | 151 audio->split_bands_const(j)[kBand0To8kHz], |
120 (audio->split_bands_const(j)[kBand0To8kHz] + | 152 (audio->split_bands_const(j)[kBand0To8kHz] + |
121 audio->num_frames_per_band())); | 153 audio->num_frames_per_band())); |
122 | |
123 handle_index++; | |
124 } | 154 } |
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; | 179 size_t handle_index = 0; |
150 size_t buffer_index = 0; | 180 size_t buffer_index = 0; |
151 const size_t num_frames_per_band = | 181 const size_t num_frames_per_band = |
152 capture_queue_buffer_.size() / | 182 capture_queue_buffer_.size() / |
153 (apm_->num_output_channels() * apm_->num_reverse_channels()); | 183 (apm_->num_output_channels() * apm_->num_reverse_channels()); |
154 for (size_t i = 0; i < apm_->num_output_channels(); i++) { | 184 for (size_t i = 0; i < apm_->num_output_channels(); i++) { |
155 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { | 185 for (size_t j = 0; j < apm_->num_reverse_channels(); j++) { |
156 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 186 Handle* my_handle = cancellers_[handle_index++]->state(); |
157 WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], | 187 WebRtcAecm_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], |
158 num_frames_per_band); | 188 num_frames_per_band); |
159 | 189 |
160 buffer_index += num_frames_per_band; | 190 buffer_index += num_frames_per_band; |
161 handle_index++; | |
162 } | 191 } |
163 } | 192 } |
164 } | 193 } |
165 } | 194 } |
166 | 195 |
167 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 196 int EchoControlMobileImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
168 rtc::CritScope cs_capture(crit_capture_); | 197 rtc::CritScope cs_capture(crit_capture_); |
169 | 198 if (!enabled_) { |
170 if (!is_component_enabled()) { | |
171 return AudioProcessing::kNoError; | 199 return AudioProcessing::kNoError; |
172 } | 200 } |
173 | 201 |
174 if (!apm_->was_stream_delay_set()) { | 202 if (!apm_->was_stream_delay_set()) { |
175 return AudioProcessing::kStreamParameterNotSetError; | 203 return AudioProcessing::kStreamParameterNotSetError; |
176 } | 204 } |
177 | 205 |
178 assert(audio->num_frames_per_band() <= 160); | 206 RTC_DCHECK_GE(160u, audio->num_frames_per_band()); |
179 assert(audio->num_channels() == apm_->num_output_channels()); | 207 RTC_DCHECK_EQ(audio->num_channels(), apm_->num_output_channels()); |
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 i = 0; i < audio->num_channels(); i++) { |
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(i); |
189 const int16_t* clean = audio->split_bands_const(i)[kBand0To8kHz]; | 217 const int16_t* clean = audio->split_bands_const(i)[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 j = 0; j < apm_->num_reverse_channels(); j++) { |
195 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 223 Handle* my_handle = cancellers_[handle_index++]->state(); |
196 err = WebRtcAecm_Process( | 224 err = WebRtcAecm_Process( |
197 my_handle, | 225 my_handle, |
198 noisy, | 226 noisy, |
199 clean, | 227 clean, |
200 audio->split_bands(i)[kBand0To8kHz], | 228 audio->split_bands(i)[kBand0To8kHz], |
201 audio->num_frames_per_band(), | 229 audio->num_frames_per_band(), |
202 apm_->stream_delay_ms()); | 230 apm_->stream_delay_ms()); |
203 | 231 |
204 if (err != AudioProcessing::kNoError) | 232 if (err != AudioProcessing::kNoError) |
205 return MapError(err); | 233 return MapError(err); |
206 | |
207 handle_index++; | |
208 } | 234 } |
209 } | 235 } |
210 | 236 |
211 return AudioProcessing::kNoError; | 237 return AudioProcessing::kNoError; |
212 } | 238 } |
213 | 239 |
214 int EchoControlMobileImpl::Enable(bool enable) { | 240 int EchoControlMobileImpl::Enable(bool enable) { |
215 // Ensure AEC and AECM are not both enabled. | 241 // Ensure AEC and AECM are not both enabled. |
216 rtc::CritScope cs_render(crit_render_); | 242 rtc::CritScope cs_render(crit_render_); |
217 rtc::CritScope cs_capture(crit_capture_); | 243 rtc::CritScope cs_capture(crit_capture_); |
218 // The is_enabled call is safe from a deadlock perspective | 244 // The is_enabled call is safe from a deadlock perspective |
219 // as both locks are allready held in the correct order. | 245 // as both locks are allready held in the correct order. |
220 if (enable && apm_->echo_cancellation()->is_enabled()) { | 246 if (enable && apm_->echo_cancellation()->is_enabled()) { |
221 return AudioProcessing::kBadParameterError; | 247 return AudioProcessing::kBadParameterError; |
222 } | 248 } |
223 | 249 |
224 return EnableComponent(enable); | 250 if (enable && !enabled_) { |
251 enabled_ = enable; // Must be set before Initialize() is called. | |
252 Initialize(); | |
253 } else { | |
254 enabled_ = enable; | |
255 } | |
256 return AudioProcessing::kNoError; | |
225 } | 257 } |
226 | 258 |
227 bool EchoControlMobileImpl::is_enabled() const { | 259 bool EchoControlMobileImpl::is_enabled() const { |
228 rtc::CritScope cs(crit_capture_); | 260 rtc::CritScope cs(crit_capture_); |
229 return is_component_enabled(); | 261 return enabled_; |
230 } | 262 } |
231 | 263 |
232 int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { | 264 int EchoControlMobileImpl::set_routing_mode(RoutingMode mode) { |
233 if (MapSetting(mode) == -1) { | 265 if (MapSetting(mode) == -1) { |
234 return AudioProcessing::kBadParameterError; | 266 return AudioProcessing::kBadParameterError; |
235 } | 267 } |
236 | 268 |
237 { | 269 { |
238 rtc::CritScope cs(crit_capture_); | 270 rtc::CritScope cs(crit_capture_); |
239 routing_mode_ = mode; | 271 routing_mode_ = mode; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 // Size mismatch | 304 // Size mismatch |
273 return AudioProcessing::kBadParameterError; | 305 return AudioProcessing::kBadParameterError; |
274 } | 306 } |
275 | 307 |
276 if (external_echo_path_ == NULL) { | 308 if (external_echo_path_ == NULL) { |
277 external_echo_path_ = new unsigned char[size_bytes]; | 309 external_echo_path_ = new unsigned char[size_bytes]; |
278 } | 310 } |
279 memcpy(external_echo_path_, echo_path, size_bytes); | 311 memcpy(external_echo_path_, echo_path, size_bytes); |
280 } | 312 } |
281 | 313 |
282 return Initialize(); | 314 Initialize(); |
315 return AudioProcessing::kNoError; | |
283 } | 316 } |
284 | 317 |
285 int EchoControlMobileImpl::GetEchoPath(void* echo_path, | 318 int EchoControlMobileImpl::GetEchoPath(void* echo_path, |
286 size_t size_bytes) const { | 319 size_t size_bytes) const { |
287 rtc::CritScope cs(crit_capture_); | 320 rtc::CritScope cs(crit_capture_); |
288 if (echo_path == NULL) { | 321 if (echo_path == NULL) { |
289 return AudioProcessing::kNullPointerError; | 322 return AudioProcessing::kNullPointerError; |
290 } | 323 } |
291 if (size_bytes != echo_path_size_bytes()) { | 324 if (size_bytes != echo_path_size_bytes()) { |
292 // Size mismatch | 325 // Size mismatch |
293 return AudioProcessing::kBadParameterError; | 326 return AudioProcessing::kBadParameterError; |
294 } | 327 } |
295 if (!is_component_enabled()) { | 328 if (!enabled_) { |
296 return AudioProcessing::kNotEnabledError; | 329 return AudioProcessing::kNotEnabledError; |
297 } | 330 } |
298 | 331 |
299 // Get the echo path from the first channel | 332 // Get the echo path from the first channel |
300 Handle* my_handle = static_cast<Handle*>(handle(0)); | 333 const int32_t err = |
301 int32_t err = WebRtcAecm_GetEchoPath(my_handle, echo_path, size_bytes); | 334 WebRtcAecm_GetEchoPath(cancellers_[0]->state(), echo_path, size_bytes); |
302 if (err != 0) | 335 if (err != 0) { |
303 return MapError(err); | 336 return MapError(err); |
337 } | |
304 | 338 |
305 return AudioProcessing::kNoError; | 339 return AudioProcessing::kNoError; |
306 } | 340 } |
307 | 341 |
308 int EchoControlMobileImpl::Initialize() { | 342 void EchoControlMobileImpl::Initialize() { |
309 { | 343 rtc::CritScope cs_render(crit_render_); |
310 rtc::CritScope cs_capture(crit_capture_); | 344 rtc::CritScope cs_capture(crit_capture_); |
311 if (!is_component_enabled()) { | 345 if (!enabled_) { |
312 return AudioProcessing::kNoError; | 346 return; |
313 } | |
314 } | 347 } |
315 | 348 |
316 if (apm_->proc_sample_rate_hz() > AudioProcessing::kSampleRate16kHz) { | 349 if (apm_->proc_sample_rate_hz() > AudioProcessing::kSampleRate16kHz) { |
317 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; | 350 LOG(LS_ERROR) << "AECM only supports 16 kHz or lower sample rates"; |
318 return AudioProcessing::kBadSampleRateError; | |
319 } | 351 } |
320 | 352 |
321 int err = ProcessingComponent::Initialize(); | 353 const int sample_rate_hz = apm_->proc_sample_rate_hz(); |
322 if (err != AudioProcessing::kNoError) { | 354 |
323 return err; | 355 if (num_handles_required() > cancellers_.size()) { |
356 const size_t cancellers_old_size = cancellers_.size(); | |
357 cancellers_.resize(num_handles_required()); | |
358 | |
359 for (size_t i = cancellers_old_size; i < cancellers_.size(); ++i) { | |
360 cancellers_[i].reset(new Canceller()); | |
361 } | |
324 } | 362 } |
325 | 363 |
364 for (auto& canceller : cancellers_) { | |
365 canceller->Initialize(sample_rate_hz, external_echo_path_, | |
366 echo_path_size_bytes()); | |
367 } | |
368 | |
369 Configure(); | |
370 | |
326 AllocateRenderQueue(); | 371 AllocateRenderQueue(); |
327 | |
328 return AudioProcessing::kNoError; | |
329 } | 372 } |
330 | 373 |
331 void EchoControlMobileImpl::AllocateRenderQueue() { | 374 void EchoControlMobileImpl::AllocateRenderQueue() { |
332 const size_t new_render_queue_element_max_size = std::max<size_t>( | 375 const size_t new_render_queue_element_max_size = std::max<size_t>( |
333 static_cast<size_t>(1), | 376 static_cast<size_t>(1), |
334 kMaxAllowedValuesOfSamplesPerFrame * num_handles_required()); | 377 kMaxAllowedValuesOfSamplesPerFrame * num_handles_required()); |
335 | 378 |
336 rtc::CritScope cs_render(crit_render_); | 379 rtc::CritScope cs_render(crit_render_); |
337 rtc::CritScope cs_capture(crit_capture_); | 380 rtc::CritScope cs_capture(crit_capture_); |
338 | 381 |
339 // Reallocate the queue if the queue item size is too small to fit the | 382 // Reallocate the queue if the queue item size is too small to fit the |
340 // data to put in the queue. | 383 // data to put in the queue. |
341 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { | 384 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
342 render_queue_element_max_size_ = new_render_queue_element_max_size; | 385 render_queue_element_max_size_ = new_render_queue_element_max_size; |
343 | 386 |
344 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); | 387 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
345 | 388 |
346 render_signal_queue_.reset( | 389 render_signal_queue_.reset( |
347 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( | 390 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
348 kMaxNumFramesToBuffer, template_queue_element, | 391 kMaxNumFramesToBuffer, template_queue_element, |
349 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); | 392 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
350 | 393 |
351 render_queue_buffer_.resize(render_queue_element_max_size_); | 394 render_queue_buffer_.resize(render_queue_element_max_size_); |
352 capture_queue_buffer_.resize(render_queue_element_max_size_); | 395 capture_queue_buffer_.resize(render_queue_element_max_size_); |
353 } else { | 396 } else { |
354 render_signal_queue_->Clear(); | 397 render_signal_queue_->Clear(); |
355 } | 398 } |
356 } | 399 } |
357 | 400 |
358 void* EchoControlMobileImpl::CreateHandle() const { | 401 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_); | 402 rtc::CritScope cs_render(crit_render_); |
389 rtc::CritScope cs_capture(crit_capture_); | 403 rtc::CritScope cs_capture(crit_capture_); |
390 AecmConfig config; | 404 AecmConfig config; |
391 config.cngMode = comfort_noise_enabled_; | 405 config.cngMode = comfort_noise_enabled_; |
392 config.echoMode = MapSetting(routing_mode_); | 406 config.echoMode = MapSetting(routing_mode_); |
393 | 407 int error = AudioProcessing::kNoError; |
394 return WebRtcAecm_set_config(static_cast<Handle*>(handle), config); | 408 for (auto& canceller : cancellers_) { |
409 Handle* my_handle = canceller->state(); | |
410 const int handle_error = WebRtcAecm_set_config(my_handle, config); | |
the sun
2016/03/09 09:23:14
There's that const again
peah-webrtc
2016/03/09 12:34:53
Done.
| |
411 if (handle_error != AudioProcessing::kNoError) { | |
412 error = AudioProcessing::kNoError; | |
the sun
2016/03/09 09:23:14
Not nit:
= handle_error ?
peah-webrtc
2016/03/09 12:34:53
Excellent find!
Done.
| |
413 } | |
414 } | |
415 return error; | |
395 } | 416 } |
396 | 417 |
397 size_t EchoControlMobileImpl::num_handles_required() const { | 418 size_t EchoControlMobileImpl::num_handles_required() const { |
398 // Not locked as it only relies on APM public API which is threadsafe. | 419 // Not locked as it only relies on APM public API which is threadsafe. |
399 return apm_->num_output_channels() * apm_->num_reverse_channels(); | 420 return apm_->num_output_channels() * apm_->num_reverse_channels(); |
400 } | 421 } |
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 | 422 } // namespace webrtc |
OLD | NEW |