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

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

Issue 1416583003: Lock scheme #5: Applied the render queueing to the agc (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@introduce_queue_CL
Patch Set: Merge Created 5 years, 1 month 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
(...skipping 17 matching lines...) Expand all
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;
39 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame2;
40
38 GainControlImpl::GainControlImpl(const AudioProcessing* apm, 41 GainControlImpl::GainControlImpl(const AudioProcessing* apm,
39 CriticalSectionWrapper* crit) 42 CriticalSectionWrapper* crit)
40 : ProcessingComponent(), 43 : ProcessingComponent(),
41 apm_(apm), 44 apm_(apm),
42 crit_(crit), 45 crit_(crit),
43 mode_(kAdaptiveAnalog), 46 mode_(kAdaptiveAnalog),
44 minimum_capture_level_(0), 47 minimum_capture_level_(0),
45 maximum_capture_level_(255), 48 maximum_capture_level_(255),
46 limiter_enabled_(true), 49 limiter_enabled_(true),
47 target_level_dbfs_(3), 50 target_level_dbfs_(3),
48 compression_gain_db_(9), 51 compression_gain_db_(9),
49 analog_capture_level_(0), 52 analog_capture_level_(0),
50 was_analog_level_set_(false), 53 was_analog_level_set_(false),
51 stream_is_saturated_(false) {} 54 stream_is_saturated_(false),
55 render_queue_element_max_size_(0) {
56 AllocateRenderQueue();
57 }
52 58
53 GainControlImpl::~GainControlImpl() {} 59 GainControlImpl::~GainControlImpl() {}
54 60
55 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { 61 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) {
56 if (!is_component_enabled()) { 62 if (!is_component_enabled()) {
57 return apm_->kNoError; 63 return apm_->kNoError;
58 } 64 }
59 65
60 assert(audio->num_frames_per_band() <= 160); 66 assert(audio->num_frames_per_band() <= 160);
61 67
68 int buffer_index = 0;
62 for (int i = 0; i < num_handles(); i++) { 69 for (int i = 0; i < num_handles(); i++) {
63 Handle* my_handle = static_cast<Handle*>(handle(i)); 70 Handle* my_handle = static_cast<Handle*>(handle(i));
64 int err = WebRtcAgc_AddFarend( 71 int err =
65 my_handle, 72 WebRtcAgc_GetAddFarendError(my_handle, audio->num_frames_per_band());
66 audio->mixed_low_pass_data(),
67 audio->num_frames_per_band());
68 73
69 if (err != apm_->kNoError) { 74 if (err != apm_->kNoError)
70 return GetHandleError(my_handle); 75 return GetHandleError(my_handle);
71 } 76
77 // Buffer the samples in the render queue.
78 RTC_DCHECK((buffer_index + audio->num_frames_per_band()) <=
79 render_queue_element_max_size_);
80
81 memcpy(&render_queue_buffer_[buffer_index], audio->mixed_low_pass_data(),
82 (audio->num_frames_per_band() *
83 sizeof(audio->mixed_low_pass_data()[0])));
84
85 buffer_index += audio->num_frames_per_band();
72 } 86 }
73 87
88 render_queue_buffer_.resize(buffer_index);
89 render_signal_queue_->Insert(&render_queue_buffer_);
90
74 return apm_->kNoError; 91 return apm_->kNoError;
75 } 92 }
76 93
94 // Read chunks of data that were received and queued on the render side from
95 // a queue. All the data chunks are buffered into the farend signal of the AGC.
96 void GainControlImpl::ReadQueuedRenderData() {
the sun 2015/10/27 16:42:49 Why do you need this extra function? Why not pop t
peah-webrtc 2015/10/29 14:05:23 Not fully sure what you mean? Do you mean that it
97 if (!is_component_enabled()) {
98 return;
99 }
100
101 bool samples_read = render_signal_queue_->Remove(&capture_queue_buffer_);
102 while (samples_read) {
103 int buffer_index = 0;
104 const int num_frames_per_band =
105 capture_queue_buffer_.size() / num_handles();
106 for (int i = 0; i < num_handles(); i++) {
107 Handle* my_handle = static_cast<Handle*>(handle(i));
108 (void)WebRtcAgc_AddFarend(my_handle, &capture_queue_buffer_[buffer_index],
109 num_frames_per_band);
110
111 buffer_index += num_frames_per_band;
112 }
113 samples_read = render_signal_queue_->Remove(&capture_queue_buffer_);
114 }
115 }
116
77 int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { 117 int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) {
78 if (!is_component_enabled()) { 118 if (!is_component_enabled()) {
79 return apm_->kNoError; 119 return apm_->kNoError;
80 } 120 }
81 121
82 assert(audio->num_frames_per_band() <= 160); 122 assert(audio->num_frames_per_band() <= 160);
83 assert(audio->num_channels() == num_handles()); 123 assert(audio->num_channels() == num_handles());
84 124
85 int err = apm_->kNoError; 125 int err = apm_->kNoError;
86 126
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 212
173 analog_capture_level_ /= num_handles(); 213 analog_capture_level_ /= num_handles();
174 } 214 }
175 215
176 was_analog_level_set_ = false; 216 was_analog_level_set_ = false;
177 return apm_->kNoError; 217 return apm_->kNoError;
178 } 218 }
179 219
180 // TODO(ajm): ensure this is called under kAdaptiveAnalog. 220 // TODO(ajm): ensure this is called under kAdaptiveAnalog.
181 int GainControlImpl::set_stream_analog_level(int level) { 221 int GainControlImpl::set_stream_analog_level(int level) {
222 // TODO(peah): Verify that this is really needed to do the reading.
223 // here as well as in ProcessStream. It works since these functions
224 // are called from the same thread, but it is not nice to do it in two
225 // places if not needed.
226 ReadQueuedRenderData();
227
182 CriticalSectionScoped crit_scoped(crit_); 228 CriticalSectionScoped crit_scoped(crit_);
183 was_analog_level_set_ = true; 229 was_analog_level_set_ = true;
184 if (level < minimum_capture_level_ || level > maximum_capture_level_) { 230 if (level < minimum_capture_level_ || level > maximum_capture_level_) {
185 return apm_->kBadParameterError; 231 return apm_->kBadParameterError;
186 } 232 }
187 analog_capture_level_ = level; 233 analog_capture_level_ = level;
188 234
189 return apm_->kNoError; 235 return apm_->kNoError;
190 } 236 }
191 237
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 bool GainControlImpl::is_limiter_enabled() const { 335 bool GainControlImpl::is_limiter_enabled() const {
290 return limiter_enabled_; 336 return limiter_enabled_;
291 } 337 }
292 338
293 int GainControlImpl::Initialize() { 339 int GainControlImpl::Initialize() {
294 int err = ProcessingComponent::Initialize(); 340 int err = ProcessingComponent::Initialize();
295 if (err != apm_->kNoError || !is_component_enabled()) { 341 if (err != apm_->kNoError || !is_component_enabled()) {
296 return err; 342 return err;
297 } 343 }
298 344
345 AllocateRenderQueue();
346
299 capture_levels_.assign(num_handles(), analog_capture_level_); 347 capture_levels_.assign(num_handles(), analog_capture_level_);
300 return apm_->kNoError; 348 return apm_->kNoError;
301 } 349 }
302 350
351 void GainControlImpl::AllocateRenderQueue() {
352 const size_t max_frame_size = std::max(kAllowedValuesOfSamplesPerFrame1,
353 kAllowedValuesOfSamplesPerFrame2);
354 const size_t min_frame_size = std::min(kAllowedValuesOfSamplesPerFrame1,
355 kAllowedValuesOfSamplesPerFrame2);
356
357 render_queue_element_max_size_ =
358 (max_frame_size * num_handles());
359
360 const size_t render_queue_element_min_size =
361 (min_frame_size * num_handles());
362
363 std::vector<int16_t> template_queue_element(render_queue_element_max_size_);
364
365 render_signal_queue_.reset(
366 new SwapQueue<std::vector<int16_t>, AgcRenderQueueItemVerifier>(
367 kMaxNumFramesToBuffer,
368 AgcRenderQueueItemVerifier(render_queue_element_min_size,
369 render_queue_element_max_size_),
370 template_queue_element));
371
372 render_queue_buffer_.resize(render_queue_element_max_size_);
373 capture_queue_buffer_.resize(render_queue_element_max_size_);
374 }
375
303 void* GainControlImpl::CreateHandle() const { 376 void* GainControlImpl::CreateHandle() const {
304 return WebRtcAgc_Create(); 377 return WebRtcAgc_Create();
305 } 378 }
306 379
307 void GainControlImpl::DestroyHandle(void* handle) const { 380 void GainControlImpl::DestroyHandle(void* handle) const {
308 WebRtcAgc_Free(static_cast<Handle*>(handle)); 381 WebRtcAgc_Free(static_cast<Handle*>(handle));
309 } 382 }
310 383
311 int GainControlImpl::InitializeHandle(void* handle) const { 384 int GainControlImpl::InitializeHandle(void* handle) const {
312 return WebRtcAgc_Init(static_cast<Handle*>(handle), 385 return WebRtcAgc_Init(static_cast<Handle*>(handle),
(...skipping 21 matching lines...) Expand all
334 return apm_->num_output_channels(); 407 return apm_->num_output_channels();
335 } 408 }
336 409
337 int GainControlImpl::GetHandleError(void* handle) const { 410 int GainControlImpl::GetHandleError(void* handle) const {
338 // The AGC has no get_error() function. 411 // The AGC has no get_error() function.
339 // (Despite listing errors in its interface...) 412 // (Despite listing errors in its interface...)
340 assert(handle != NULL); 413 assert(handle != NULL);
341 return apm_->kUnspecifiedError; 414 return apm_->kUnspecifiedError;
342 } 415 }
343 } // namespace webrtc 416 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698