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/gain_control_impl.h" | 11 #include "webrtc/modules/audio_processing/gain_control_impl.h" |
12 | 12 |
13 #include <assert.h> | 13 #include <assert.h> |
14 | 14 |
| 15 #include "webrtc/base/criticalsection.h" |
15 #include "webrtc/modules/audio_processing/audio_buffer.h" | 16 #include "webrtc/modules/audio_processing/audio_buffer.h" |
16 #include "webrtc/modules/audio_processing/agc/legacy/gain_control.h" | 17 #include "webrtc/modules/audio_processing/agc/legacy/gain_control.h" |
17 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" | |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
20 | 20 |
21 typedef void Handle; | 21 typedef void Handle; |
22 | 22 |
23 namespace { | 23 namespace { |
24 int16_t MapSetting(GainControl::Mode mode) { | 24 int16_t MapSetting(GainControl::Mode mode) { |
25 switch (mode) { | 25 switch (mode) { |
26 case GainControl::kAdaptiveAnalog: | 26 case GainControl::kAdaptiveAnalog: |
27 return kAgcModeAdaptiveAnalog; | 27 return kAgcModeAdaptiveAnalog; |
28 case GainControl::kAdaptiveDigital: | 28 case GainControl::kAdaptiveDigital: |
29 return kAgcModeAdaptiveDigital; | 29 return kAgcModeAdaptiveDigital; |
30 case GainControl::kFixedDigital: | 30 case GainControl::kFixedDigital: |
31 return kAgcModeFixedDigital; | 31 return kAgcModeFixedDigital; |
32 } | 32 } |
33 assert(false); | 33 assert(false); |
34 return -1; | 34 return -1; |
35 } | 35 } |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame1; | 38 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame1; |
39 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame2; | 39 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame2; |
40 | 40 |
41 GainControlImpl::GainControlImpl(const AudioProcessing* apm, | 41 GainControlImpl::GainControlImpl(const AudioProcessing* apm, |
42 CriticalSectionWrapper* crit, | 42 rtc::CriticalSection* crit_render, |
| 43 rtc::CriticalSection* crit_capture, |
43 rtc::ThreadChecker* render_thread, | 44 rtc::ThreadChecker* render_thread, |
44 rtc::ThreadChecker* capture_thread) | 45 rtc::ThreadChecker* capture_thread) |
45 : ProcessingComponent(), | 46 : ProcessingComponent(), |
46 apm_(apm), | 47 apm_(apm), |
47 crit_(crit), | 48 crit_render_(crit_render), |
| 49 crit_capture_(crit_capture), |
48 render_thread_(render_thread), | 50 render_thread_(render_thread), |
49 capture_thread_(capture_thread), | 51 capture_thread_(capture_thread), |
50 mode_(kAdaptiveAnalog), | 52 mode_(kAdaptiveAnalog), |
51 minimum_capture_level_(0), | 53 minimum_capture_level_(0), |
52 maximum_capture_level_(255), | 54 maximum_capture_level_(255), |
53 limiter_enabled_(true), | 55 limiter_enabled_(true), |
54 target_level_dbfs_(3), | 56 target_level_dbfs_(3), |
55 compression_gain_db_(9), | 57 compression_gain_db_(9), |
56 analog_capture_level_(0), | 58 analog_capture_level_(0), |
57 was_analog_level_set_(false), | 59 was_analog_level_set_(false), |
58 stream_is_saturated_(false), | 60 stream_is_saturated_(false), |
59 render_queue_element_max_size_(0) { | 61 render_queue_element_max_size_(0) { |
60 AllocateRenderQueue(); | 62 AllocateRenderQueue(); |
61 } | 63 } |
62 | 64 |
63 GainControlImpl::~GainControlImpl() {} | 65 GainControlImpl::~GainControlImpl() {} |
64 | 66 |
65 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { | 67 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { |
66 RTC_DCHECK(render_thread_->CalledOnValidThread()); | 68 RTC_DCHECK(render_thread_->CalledOnValidThread()); |
| 69 rtc::CritScope cs(crit_render_); |
67 if (!is_component_enabled()) { | 70 if (!is_component_enabled()) { |
68 return apm_->kNoError; | 71 return apm_->kNoError; |
69 } | 72 } |
70 | 73 |
71 assert(audio->num_frames_per_band() <= 160); | 74 assert(audio->num_frames_per_band() <= 160); |
72 | 75 |
73 int buffer_index = 0; | 76 int buffer_index = 0; |
74 for (int i = 0; i < num_handles(); i++) { | 77 for (int i = 0; i < num_handles(); i++) { |
75 Handle* my_handle = static_cast<Handle*>(handle(i)); | 78 Handle* my_handle = static_cast<Handle*>(handle(i)); |
76 int err = | 79 int err = |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 | 230 |
228 // TODO(ajm): ensure this is called under kAdaptiveAnalog. | 231 // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
229 int GainControlImpl::set_stream_analog_level(int level) { | 232 int GainControlImpl::set_stream_analog_level(int level) { |
230 RTC_DCHECK(capture_thread_->CalledOnValidThread()); | 233 RTC_DCHECK(capture_thread_->CalledOnValidThread()); |
231 // TODO(peah): Verify that this is really needed to do the reading. | 234 // TODO(peah): Verify that this is really needed to do the reading. |
232 // here as well as in ProcessStream. It works since these functions | 235 // here as well as in ProcessStream. It works since these functions |
233 // are called from the same thread, but it is not nice to do it in two | 236 // are called from the same thread, but it is not nice to do it in two |
234 // places if not needed. | 237 // places if not needed. |
235 ReadQueuedRenderData(); | 238 ReadQueuedRenderData(); |
236 | 239 |
237 CriticalSectionScoped crit_scoped(crit_); | 240 rtc::CritScope cs(crit_capture_); |
238 was_analog_level_set_ = true; | 241 was_analog_level_set_ = true; |
239 if (level < minimum_capture_level_ || level > maximum_capture_level_) { | 242 if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
240 return apm_->kBadParameterError; | 243 return apm_->kBadParameterError; |
241 } | 244 } |
242 analog_capture_level_ = level; | 245 analog_capture_level_ = level; |
243 | 246 |
244 return apm_->kNoError; | 247 return apm_->kNoError; |
245 } | 248 } |
246 | 249 |
247 int GainControlImpl::stream_analog_level() { | 250 int GainControlImpl::stream_analog_level() { |
| 251 rtc::CritScope cs(crit_capture_); |
248 RTC_DCHECK(capture_thread_->CalledOnValidThread()); | 252 RTC_DCHECK(capture_thread_->CalledOnValidThread()); |
249 // TODO(ajm): enable this assertion? | 253 // TODO(ajm): enable this assertion? |
250 //assert(mode_ == kAdaptiveAnalog); | 254 //assert(mode_ == kAdaptiveAnalog); |
251 | 255 |
252 return analog_capture_level_; | 256 return analog_capture_level_; |
253 } | 257 } |
254 | 258 |
255 int GainControlImpl::Enable(bool enable) { | 259 int GainControlImpl::Enable(bool enable) { |
256 CriticalSectionScoped crit_scoped(crit_); | 260 rtc::CritScope cs(crit_capture_); |
257 return EnableComponent(enable); | 261 return EnableComponent(enable); |
258 } | 262 } |
259 | 263 |
260 bool GainControlImpl::is_enabled() const { | 264 bool GainControlImpl::is_enabled() const { |
| 265 rtc::CritScope cs(crit_capture_); |
261 return is_component_enabled(); | 266 return is_component_enabled(); |
262 } | 267 } |
263 | 268 |
264 int GainControlImpl::set_mode(Mode mode) { | 269 int GainControlImpl::set_mode(Mode mode) { |
265 CriticalSectionScoped crit_scoped(crit_); | 270 rtc::CritScope cs(crit_capture_); |
266 if (MapSetting(mode) == -1) { | 271 if (MapSetting(mode) == -1) { |
267 return apm_->kBadParameterError; | 272 return apm_->kBadParameterError; |
268 } | 273 } |
269 | 274 |
270 mode_ = mode; | 275 mode_ = mode; |
271 return Initialize(); | 276 return Initialize(); |
272 } | 277 } |
273 | 278 |
274 GainControl::Mode GainControlImpl::mode() const { | 279 GainControl::Mode GainControlImpl::mode() const { |
| 280 rtc::CritScope cs(crit_capture_); |
275 return mode_; | 281 return mode_; |
276 } | 282 } |
277 | 283 |
278 int GainControlImpl::set_analog_level_limits(int minimum, | 284 int GainControlImpl::set_analog_level_limits(int minimum, |
279 int maximum) { | 285 int maximum) { |
280 CriticalSectionScoped crit_scoped(crit_); | 286 rtc::CritScope cs(crit_capture_); |
281 if (minimum < 0) { | 287 if (minimum < 0) { |
282 return apm_->kBadParameterError; | 288 return apm_->kBadParameterError; |
283 } | 289 } |
284 | 290 |
285 if (maximum > 65535) { | 291 if (maximum > 65535) { |
286 return apm_->kBadParameterError; | 292 return apm_->kBadParameterError; |
287 } | 293 } |
288 | 294 |
289 if (maximum < minimum) { | 295 if (maximum < minimum) { |
290 return apm_->kBadParameterError; | 296 return apm_->kBadParameterError; |
291 } | 297 } |
292 | 298 |
293 minimum_capture_level_ = minimum; | 299 minimum_capture_level_ = minimum; |
294 maximum_capture_level_ = maximum; | 300 maximum_capture_level_ = maximum; |
295 | 301 |
296 return Initialize(); | 302 return Initialize(); |
297 } | 303 } |
298 | 304 |
299 int GainControlImpl::analog_level_minimum() const { | 305 int GainControlImpl::analog_level_minimum() const { |
| 306 rtc::CritScope cs(crit_capture_); |
300 return minimum_capture_level_; | 307 return minimum_capture_level_; |
301 } | 308 } |
302 | 309 |
303 int GainControlImpl::analog_level_maximum() const { | 310 int GainControlImpl::analog_level_maximum() const { |
| 311 rtc::CritScope cs(crit_capture_); |
304 return maximum_capture_level_; | 312 return maximum_capture_level_; |
305 } | 313 } |
306 | 314 |
307 bool GainControlImpl::stream_is_saturated() const { | 315 bool GainControlImpl::stream_is_saturated() const { |
| 316 rtc::CritScope cs(crit_capture_); |
308 return stream_is_saturated_; | 317 return stream_is_saturated_; |
309 } | 318 } |
310 | 319 |
311 int GainControlImpl::set_target_level_dbfs(int level) { | 320 int GainControlImpl::set_target_level_dbfs(int level) { |
312 CriticalSectionScoped crit_scoped(crit_); | 321 rtc::CritScope cs(crit_capture_); |
313 if (level > 31 || level < 0) { | 322 if (level > 31 || level < 0) { |
314 return apm_->kBadParameterError; | 323 return apm_->kBadParameterError; |
315 } | 324 } |
316 | 325 |
317 target_level_dbfs_ = level; | 326 target_level_dbfs_ = level; |
318 return Configure(); | 327 return Configure(); |
319 } | 328 } |
320 | 329 |
321 int GainControlImpl::target_level_dbfs() const { | 330 int GainControlImpl::target_level_dbfs() const { |
| 331 rtc::CritScope cs(crit_capture_); |
322 return target_level_dbfs_; | 332 return target_level_dbfs_; |
323 } | 333 } |
324 | 334 |
325 int GainControlImpl::set_compression_gain_db(int gain) { | 335 int GainControlImpl::set_compression_gain_db(int gain) { |
326 CriticalSectionScoped crit_scoped(crit_); | 336 rtc::CritScope cs(crit_capture_); |
327 if (gain < 0 || gain > 90) { | 337 if (gain < 0 || gain > 90) { |
328 return apm_->kBadParameterError; | 338 return apm_->kBadParameterError; |
329 } | 339 } |
330 | 340 |
331 compression_gain_db_ = gain; | 341 compression_gain_db_ = gain; |
332 return Configure(); | 342 return Configure(); |
333 } | 343 } |
334 | 344 |
335 int GainControlImpl::compression_gain_db() const { | 345 int GainControlImpl::compression_gain_db() const { |
| 346 rtc::CritScope cs(crit_capture_); |
336 return compression_gain_db_; | 347 return compression_gain_db_; |
337 } | 348 } |
338 | 349 |
339 int GainControlImpl::enable_limiter(bool enable) { | 350 int GainControlImpl::enable_limiter(bool enable) { |
340 CriticalSectionScoped crit_scoped(crit_); | 351 rtc::CritScope cs(crit_capture_); |
341 limiter_enabled_ = enable; | 352 limiter_enabled_ = enable; |
342 return Configure(); | 353 return Configure(); |
343 } | 354 } |
344 | 355 |
345 bool GainControlImpl::is_limiter_enabled() const { | 356 bool GainControlImpl::is_limiter_enabled() const { |
| 357 rtc::CritScope cs(crit_capture_); |
346 return limiter_enabled_; | 358 return limiter_enabled_; |
347 } | 359 } |
348 | 360 |
349 int GainControlImpl::Initialize() { | 361 int GainControlImpl::Initialize() { |
350 int err = ProcessingComponent::Initialize(); | 362 int err = ProcessingComponent::Initialize(); |
351 if (err != apm_->kNoError || !is_component_enabled()) { | 363 if (err != apm_->kNoError || !is_component_enabled()) { |
352 return err; | 364 return err; |
353 } | 365 } |
354 | 366 |
355 AllocateRenderQueue(); | 367 AllocateRenderQueue(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 return apm_->num_output_channels(); | 429 return apm_->num_output_channels(); |
418 } | 430 } |
419 | 431 |
420 int GainControlImpl::GetHandleError(void* handle) const { | 432 int GainControlImpl::GetHandleError(void* handle) const { |
421 // The AGC has no get_error() function. | 433 // The AGC has no get_error() function. |
422 // (Despite listing errors in its interface...) | 434 // (Despite listing errors in its interface...) |
423 assert(handle != NULL); | 435 assert(handle != NULL); |
424 return apm_->kUnspecifiedError; | 436 return apm_->kUnspecifiedError; |
425 } | 437 } |
426 } // namespace webrtc | 438 } // namespace webrtc |
OLD | NEW |