Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: webrtc/modules/audio_processing/gain_control_impl.cc

Issue 1424663003: Lock scheme #8: Introduced the new locking scheme (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@add_threadcheckers_CL
Patch Set: Fixed a bad merge error for the beamformer settings and updated with the latest merge from master Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/modules/audio_processing/audio_buffer.h" 15 #include "webrtc/modules/audio_processing/audio_buffer.h"
16 #include "webrtc/modules/audio_processing/agc/legacy/gain_control.h" 16 #include "webrtc/modules/audio_processing/agc/legacy/gain_control.h"
17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
18 17
19 namespace webrtc { 18 namespace webrtc {
20 19
21 typedef void Handle; 20 typedef void Handle;
22 21
23 namespace { 22 namespace {
24 int16_t MapSetting(GainControl::Mode mode) { 23 int16_t MapSetting(GainControl::Mode mode) {
25 switch (mode) { 24 switch (mode) {
26 case GainControl::kAdaptiveAnalog: 25 case GainControl::kAdaptiveAnalog:
27 return kAgcModeAdaptiveAnalog; 26 return kAgcModeAdaptiveAnalog;
(...skipping 10 matching lines...) Expand all
38 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; 37 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160;
39 // Maximum number of frames to buffer in the render queue. 38 // Maximum number of frames to buffer in the render queue.
40 // TODO(peah): Decrease this once we properly handle hugely unbalanced 39 // TODO(peah): Decrease this once we properly handle hugely unbalanced
41 // reverse and forward call numbers. 40 // reverse and forward call numbers.
42 static const size_t kMaxNumFramesToBuffer = 100; 41 static const size_t kMaxNumFramesToBuffer = 100;
43 42
44 } // namespace 43 } // namespace
45 44
46 GainControlImpl::GainControlImpl( 45 GainControlImpl::GainControlImpl(
47 const AudioProcessing* apm, 46 const AudioProcessing* apm,
48 CriticalSectionWrapper* crit, 47 rtc::CriticalSection* crit_render,
48 rtc::CriticalSection* crit_capture,
49 const rtc::ThreadChecker* render_thread_checker, 49 const rtc::ThreadChecker* render_thread_checker,
50 const rtc::ThreadChecker* capture_thread_checker) 50 const rtc::ThreadChecker* capture_thread_checker)
51 : ProcessingComponent(), 51 : ProcessingComponent(),
52 apm_(apm), 52 apm_(apm),
53 crit_(crit), 53 crit_render_(crit_render),
54 crit_capture_(crit_capture),
54 render_thread_checker_(render_thread_checker), 55 render_thread_checker_(render_thread_checker),
55 capture_thread_checker_(capture_thread_checker), 56 capture_thread_checker_(capture_thread_checker),
56 mode_(kAdaptiveAnalog), 57 mode_(kAdaptiveAnalog),
57 minimum_capture_level_(0), 58 minimum_capture_level_(0),
58 maximum_capture_level_(255), 59 maximum_capture_level_(255),
59 limiter_enabled_(true), 60 limiter_enabled_(true),
60 target_level_dbfs_(3), 61 target_level_dbfs_(3),
61 compression_gain_db_(9), 62 compression_gain_db_(9),
62 analog_capture_level_(0), 63 analog_capture_level_(0),
63 was_analog_level_set_(false), 64 was_analog_level_set_(false),
(...skipping 20 matching lines...) Expand all
84 return GetHandleError(my_handle); 85 return GetHandleError(my_handle);
85 86
86 // Buffer the samples in the render queue. 87 // Buffer the samples in the render queue.
87 render_queue_buffer_.insert( 88 render_queue_buffer_.insert(
88 render_queue_buffer_.end(), audio->mixed_low_pass_data(), 89 render_queue_buffer_.end(), audio->mixed_low_pass_data(),
89 (audio->mixed_low_pass_data() + audio->num_frames_per_band())); 90 (audio->mixed_low_pass_data() + audio->num_frames_per_band()));
90 } 91 }
91 92
92 // Insert the samples into the queue. 93 // Insert the samples into the queue.
93 if (!render_signal_queue_->Insert(&render_queue_buffer_)) { 94 if (!render_signal_queue_->Insert(&render_queue_buffer_)) {
94 ReadQueuedRenderData(); 95 // The data queue is full and needs to be emptied.
96 {
97 rtc::CritScope cs_capture(crit_capture_);
98 ReadQueuedRenderData();
99 }
95 100
96 // Retry the insert (should always work). 101 // Retry the insert (should always work).
97 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); 102 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true);
98 } 103 }
99 104
100 return apm_->kNoError; 105 return apm_->kNoError;
101 } 106 }
102 107
103 // Read chunks of data that were received and queued on the render side from 108 // Read chunks of data that were received and queued on the render side from
104 // a queue. All the data chunks are buffered into the farend signal of the AGC. 109 // a queue. All the data chunks are buffered into the farend signal of the AGC.
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 analog_capture_level_ /= num_handles(); 225 analog_capture_level_ /= num_handles();
221 } 226 }
222 227
223 was_analog_level_set_ = false; 228 was_analog_level_set_ = false;
224 return apm_->kNoError; 229 return apm_->kNoError;
225 } 230 }
226 231
227 // TODO(ajm): ensure this is called under kAdaptiveAnalog. 232 // TODO(ajm): ensure this is called under kAdaptiveAnalog.
228 int GainControlImpl::set_stream_analog_level(int level) { 233 int GainControlImpl::set_stream_analog_level(int level) {
229 RTC_DCHECK(capture_thread_checker_->CalledOnValidThread()); 234 RTC_DCHECK(capture_thread_checker_->CalledOnValidThread());
230 CriticalSectionScoped crit_scoped(crit_); 235
236 rtc::CritScope cs(crit_capture_);
231 was_analog_level_set_ = true; 237 was_analog_level_set_ = true;
232 if (level < minimum_capture_level_ || level > maximum_capture_level_) { 238 if (level < minimum_capture_level_ || level > maximum_capture_level_) {
233 return apm_->kBadParameterError; 239 return apm_->kBadParameterError;
234 } 240 }
235 analog_capture_level_ = level; 241 analog_capture_level_ = level;
236 242
237 return apm_->kNoError; 243 return apm_->kNoError;
238 } 244 }
239 245
240 int GainControlImpl::stream_analog_level() { 246 int GainControlImpl::stream_analog_level() {
247 rtc::CritScope cs(crit_capture_);
241 RTC_DCHECK(capture_thread_checker_->CalledOnValidThread()); 248 RTC_DCHECK(capture_thread_checker_->CalledOnValidThread());
242 // TODO(ajm): enable this assertion? 249 // TODO(ajm): enable this assertion?
243 //assert(mode_ == kAdaptiveAnalog); 250 //assert(mode_ == kAdaptiveAnalog);
244 251
245 return analog_capture_level_; 252 return analog_capture_level_;
246 } 253 }
247 254
248 int GainControlImpl::Enable(bool enable) { 255 int GainControlImpl::Enable(bool enable) {
249 CriticalSectionScoped crit_scoped(crit_); 256 rtc::CritScope cs_render(crit_render_);
257 rtc::CritScope cs(crit_capture_);
the sun 2015/11/23 21:36:05 Why do you sometimes use "cs_capture", and sometim
peah-webrtc 2015/11/24 21:42:24 The scheme is to only use cs, when there is only o
250 return EnableComponent(enable); 258 return EnableComponent(enable);
251 } 259 }
252 260
253 bool GainControlImpl::is_enabled() const { 261 bool GainControlImpl::is_enabled() const {
262 rtc::CritScope cs(crit_capture_);
254 return is_component_enabled(); 263 return is_component_enabled();
255 } 264 }
256 265
257 int GainControlImpl::set_mode(Mode mode) { 266 int GainControlImpl::set_mode(Mode mode) {
258 CriticalSectionScoped crit_scoped(crit_); 267 rtc::CritScope cs_render(crit_render_);
268 rtc::CritScope cs_capture(crit_capture_);
259 if (MapSetting(mode) == -1) { 269 if (MapSetting(mode) == -1) {
260 return apm_->kBadParameterError; 270 return apm_->kBadParameterError;
261 } 271 }
262 272
263 mode_ = mode; 273 mode_ = mode;
264 return Initialize(); 274 return Initialize();
265 } 275 }
266 276
267 GainControl::Mode GainControlImpl::mode() const { 277 GainControl::Mode GainControlImpl::mode() const {
278 rtc::CritScope cs(crit_capture_);
268 return mode_; 279 return mode_;
269 } 280 }
270 281
271 int GainControlImpl::set_analog_level_limits(int minimum, 282 int GainControlImpl::set_analog_level_limits(int minimum,
272 int maximum) { 283 int maximum) {
273 CriticalSectionScoped crit_scoped(crit_); 284 rtc::CritScope cs(crit_capture_);
274 if (minimum < 0) { 285 if (minimum < 0) {
275 return apm_->kBadParameterError; 286 return apm_->kBadParameterError;
276 } 287 }
277 288
278 if (maximum > 65535) { 289 if (maximum > 65535) {
279 return apm_->kBadParameterError; 290 return apm_->kBadParameterError;
280 } 291 }
281 292
282 if (maximum < minimum) { 293 if (maximum < minimum) {
283 return apm_->kBadParameterError; 294 return apm_->kBadParameterError;
284 } 295 }
285 296
286 minimum_capture_level_ = minimum; 297 minimum_capture_level_ = minimum;
287 maximum_capture_level_ = maximum; 298 maximum_capture_level_ = maximum;
288 299
289 return Initialize(); 300 return Initialize();
290 } 301 }
291 302
292 int GainControlImpl::analog_level_minimum() const { 303 int GainControlImpl::analog_level_minimum() const {
304 rtc::CritScope cs(crit_capture_);
293 return minimum_capture_level_; 305 return minimum_capture_level_;
294 } 306 }
295 307
296 int GainControlImpl::analog_level_maximum() const { 308 int GainControlImpl::analog_level_maximum() const {
309 rtc::CritScope cs(crit_capture_);
297 return maximum_capture_level_; 310 return maximum_capture_level_;
298 } 311 }
299 312
300 bool GainControlImpl::stream_is_saturated() const { 313 bool GainControlImpl::stream_is_saturated() const {
314 rtc::CritScope cs(crit_capture_);
301 return stream_is_saturated_; 315 return stream_is_saturated_;
302 } 316 }
303 317
304 int GainControlImpl::set_target_level_dbfs(int level) { 318 int GainControlImpl::set_target_level_dbfs(int level) {
305 CriticalSectionScoped crit_scoped(crit_); 319 rtc::CritScope cs(crit_capture_);
306 if (level > 31 || level < 0) { 320 if (level > 31 || level < 0) {
307 return apm_->kBadParameterError; 321 return apm_->kBadParameterError;
308 } 322 }
309 323
310 target_level_dbfs_ = level; 324 target_level_dbfs_ = level;
311 return Configure(); 325 return Configure();
312 } 326 }
313 327
314 int GainControlImpl::target_level_dbfs() const { 328 int GainControlImpl::target_level_dbfs() const {
329 rtc::CritScope cs(crit_capture_);
315 return target_level_dbfs_; 330 return target_level_dbfs_;
316 } 331 }
317 332
318 int GainControlImpl::set_compression_gain_db(int gain) { 333 int GainControlImpl::set_compression_gain_db(int gain) {
319 CriticalSectionScoped crit_scoped(crit_); 334 rtc::CritScope cs(crit_capture_);
320 if (gain < 0 || gain > 90) { 335 if (gain < 0 || gain > 90) {
321 return apm_->kBadParameterError; 336 return apm_->kBadParameterError;
322 } 337 }
323 338
324 compression_gain_db_ = gain; 339 compression_gain_db_ = gain;
325 return Configure(); 340 return Configure();
326 } 341 }
327 342
328 int GainControlImpl::compression_gain_db() const { 343 int GainControlImpl::compression_gain_db() const {
344 rtc::CritScope cs(crit_capture_);
329 return compression_gain_db_; 345 return compression_gain_db_;
330 } 346 }
331 347
332 int GainControlImpl::enable_limiter(bool enable) { 348 int GainControlImpl::enable_limiter(bool enable) {
333 CriticalSectionScoped crit_scoped(crit_); 349 rtc::CritScope cs(crit_capture_);
334 limiter_enabled_ = enable; 350 limiter_enabled_ = enable;
335 return Configure(); 351 return Configure();
336 } 352 }
337 353
338 bool GainControlImpl::is_limiter_enabled() const { 354 bool GainControlImpl::is_limiter_enabled() const {
355 rtc::CritScope cs(crit_capture_);
339 return limiter_enabled_; 356 return limiter_enabled_;
340 } 357 }
341 358
342 int GainControlImpl::Initialize() { 359 int GainControlImpl::Initialize() {
343 int err = ProcessingComponent::Initialize(); 360 int err = ProcessingComponent::Initialize();
344 if (err != apm_->kNoError || !is_component_enabled()) { 361 if (err != apm_->kNoError || !is_component_enabled()) {
345 return err; 362 return err;
346 } 363 }
347 364
348 AllocateRenderQueue(); 365 AllocateRenderQueue();
349 366
350 const int n = num_handles(); 367 const int n = num_handles();
351 RTC_CHECK_GE(n, 0) << "Bad number of handles: " << n; 368 RTC_CHECK_GE(n, 0) << "Bad number of handles: " << n;
352 capture_levels_.assign(n, analog_capture_level_); 369 capture_levels_.assign(n, analog_capture_level_);
353 return apm_->kNoError; 370 return apm_->kNoError;
354 } 371 }
355 372
356 void GainControlImpl::AllocateRenderQueue() { 373 void GainControlImpl::AllocateRenderQueue() {
374 // Only called from within APM, hence no locking is needed.
357 const size_t new_render_queue_element_max_size = 375 const size_t new_render_queue_element_max_size =
358 std::max<size_t>(static_cast<size_t>(1), 376 std::max<size_t>(static_cast<size_t>(1),
359 kMaxAllowedValuesOfSamplesPerFrame * num_handles()); 377 kMaxAllowedValuesOfSamplesPerFrame * num_handles());
360 378
361 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { 379 if (render_queue_element_max_size_ < new_render_queue_element_max_size) {
362 render_queue_element_max_size_ = new_render_queue_element_max_size; 380 render_queue_element_max_size_ = new_render_queue_element_max_size;
363 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); 381 std::vector<int16_t> template_queue_element(render_queue_element_max_size_);
364 382
365 render_signal_queue_.reset( 383 render_signal_queue_.reset(
366 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( 384 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>(
367 kMaxNumFramesToBuffer, template_queue_element, 385 kMaxNumFramesToBuffer, template_queue_element,
368 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); 386 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_)));
369 387
370 render_queue_buffer_.resize(render_queue_element_max_size_); 388 render_queue_buffer_.resize(render_queue_element_max_size_);
371 capture_queue_buffer_.resize(render_queue_element_max_size_); 389 capture_queue_buffer_.resize(render_queue_element_max_size_);
372 } else { 390 } else {
373 render_signal_queue_->Clear(); 391 render_signal_queue_->Clear();
374 } 392 }
375 } 393 }
376 394
377 void* GainControlImpl::CreateHandle() const { 395 void* GainControlImpl::CreateHandle() const {
396 // Only called from within APM, hence no locking is needed.
378 return WebRtcAgc_Create(); 397 return WebRtcAgc_Create();
379 } 398 }
380 399
381 void GainControlImpl::DestroyHandle(void* handle) const { 400 void GainControlImpl::DestroyHandle(void* handle) const {
401 // Only called from within APM, hence no locking is needed.
382 WebRtcAgc_Free(static_cast<Handle*>(handle)); 402 WebRtcAgc_Free(static_cast<Handle*>(handle));
383 } 403 }
384 404
385 int GainControlImpl::InitializeHandle(void* handle) const { 405 int GainControlImpl::InitializeHandle(void* handle) const {
406 // Only called from within APM, hence no locking is needed.
386 return WebRtcAgc_Init(static_cast<Handle*>(handle), 407 return WebRtcAgc_Init(static_cast<Handle*>(handle),
387 minimum_capture_level_, 408 minimum_capture_level_,
388 maximum_capture_level_, 409 maximum_capture_level_,
389 MapSetting(mode_), 410 MapSetting(mode_),
390 apm_->proc_sample_rate_hz()); 411 apm_->proc_sample_rate_hz());
391 } 412 }
392 413
393 int GainControlImpl::ConfigureHandle(void* handle) const { 414 int GainControlImpl::ConfigureHandle(void* handle) const {
415 // Only called from within APM, hence no locking is needed.
394 WebRtcAgcConfig config; 416 WebRtcAgcConfig config;
395 // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we 417 // TODO(ajm): Flip the sign here (since AGC expects a positive value) if we
396 // change the interface. 418 // change the interface.
397 //assert(target_level_dbfs_ <= 0); 419 //assert(target_level_dbfs_ <= 0);
398 //config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_); 420 //config.targetLevelDbfs = static_cast<int16_t>(-target_level_dbfs_);
399 config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_); 421 config.targetLevelDbfs = static_cast<int16_t>(target_level_dbfs_);
400 config.compressionGaindB = 422 config.compressionGaindB =
401 static_cast<int16_t>(compression_gain_db_); 423 static_cast<int16_t>(compression_gain_db_);
402 config.limiterEnable = limiter_enabled_; 424 config.limiterEnable = limiter_enabled_;
403 425
404 return WebRtcAgc_set_config(static_cast<Handle*>(handle), config); 426 return WebRtcAgc_set_config(static_cast<Handle*>(handle), config);
405 } 427 }
406 428
407 int GainControlImpl::num_handles_required() const { 429 int GainControlImpl::num_handles_required() const {
430 // Only called from within APM, hence no locking is needed.
408 return apm_->num_output_channels(); 431 return apm_->num_output_channels();
409 } 432 }
410 433
411 int GainControlImpl::GetHandleError(void* handle) const { 434 int GainControlImpl::GetHandleError(void* handle) const {
435 // Only called from within APM, hence no locking is needed.
412 // The AGC has no get_error() function. 436 // The AGC has no get_error() function.
413 // (Despite listing errors in its interface...) 437 // (Despite listing errors in its interface...)
414 assert(handle != NULL); 438 assert(handle != NULL);
415 return apm_->kUnspecifiedError; 439 return apm_->kUnspecifiedError;
416 } 440 }
417 } // namespace webrtc 441 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698