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 15 matching lines...) Expand all Loading... |
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 |
| 37 // Maximum length that a frame of samples can have. |
| 38 static const size_t kMaxAllowedValuesOfSamplesPerFrame = 160; |
| 39 // Maximum number of frames to buffer in the render queue. |
| 40 // TODO(peah): Decrease this once we properly handle hugely unbalanced |
| 41 // reverse and forward call numbers. |
| 42 static const size_t kMaxNumFramesToBuffer = 100; |
| 43 |
36 } // namespace | 44 } // namespace |
37 | 45 |
38 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame1; | |
39 const size_t GainControlImpl::kAllowedValuesOfSamplesPerFrame2; | |
40 | |
41 GainControlImpl::GainControlImpl(const AudioProcessing* apm, | 46 GainControlImpl::GainControlImpl(const AudioProcessing* apm, |
42 CriticalSectionWrapper* crit) | 47 CriticalSectionWrapper* crit) |
43 : ProcessingComponent(), | 48 : ProcessingComponent(), |
44 apm_(apm), | 49 apm_(apm), |
45 crit_(crit), | 50 crit_(crit), |
46 mode_(kAdaptiveAnalog), | 51 mode_(kAdaptiveAnalog), |
47 minimum_capture_level_(0), | 52 minimum_capture_level_(0), |
48 maximum_capture_level_(255), | 53 maximum_capture_level_(255), |
49 limiter_enabled_(true), | 54 limiter_enabled_(true), |
50 target_level_dbfs_(3), | 55 target_level_dbfs_(3), |
51 compression_gain_db_(9), | 56 compression_gain_db_(9), |
52 analog_capture_level_(0), | 57 analog_capture_level_(0), |
53 was_analog_level_set_(false), | 58 was_analog_level_set_(false), |
54 stream_is_saturated_(false), | 59 stream_is_saturated_(false), |
55 render_queue_element_max_size_(0) { | 60 render_queue_element_max_size_(0) {} |
56 AllocateRenderQueue(); | |
57 } | |
58 | 61 |
59 GainControlImpl::~GainControlImpl() {} | 62 GainControlImpl::~GainControlImpl() {} |
60 | 63 |
61 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { | 64 int GainControlImpl::ProcessRenderAudio(AudioBuffer* audio) { |
62 if (!is_component_enabled()) { | 65 if (!is_component_enabled()) { |
63 return apm_->kNoError; | 66 return apm_->kNoError; |
64 } | 67 } |
65 | 68 |
66 assert(audio->num_frames_per_band() <= 160); | 69 assert(audio->num_frames_per_band() <= 160); |
67 | 70 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 | 213 |
211 analog_capture_level_ /= num_handles(); | 214 analog_capture_level_ /= num_handles(); |
212 } | 215 } |
213 | 216 |
214 was_analog_level_set_ = false; | 217 was_analog_level_set_ = false; |
215 return apm_->kNoError; | 218 return apm_->kNoError; |
216 } | 219 } |
217 | 220 |
218 // TODO(ajm): ensure this is called under kAdaptiveAnalog. | 221 // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
219 int GainControlImpl::set_stream_analog_level(int level) { | 222 int GainControlImpl::set_stream_analog_level(int level) { |
220 // TODO(peah): Verify that this is really needed to do the reading | |
221 // here as well as in ProcessStream. It works since these functions | |
222 // are called from the same thread, but it is not nice to do it in two | |
223 // places if not needed. | |
224 ReadQueuedRenderData(); | |
225 | |
226 CriticalSectionScoped crit_scoped(crit_); | 223 CriticalSectionScoped crit_scoped(crit_); |
227 was_analog_level_set_ = true; | 224 was_analog_level_set_ = true; |
228 if (level < minimum_capture_level_ || level > maximum_capture_level_) { | 225 if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
229 return apm_->kBadParameterError; | 226 return apm_->kBadParameterError; |
230 } | 227 } |
231 analog_capture_level_ = level; | 228 analog_capture_level_ = level; |
232 | 229 |
233 return apm_->kNoError; | 230 return apm_->kNoError; |
234 } | 231 } |
235 | 232 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 339 |
343 AllocateRenderQueue(); | 340 AllocateRenderQueue(); |
344 | 341 |
345 const int n = num_handles(); | 342 const int n = num_handles(); |
346 RTC_CHECK_GE(n, 0) << "Bad number of handles: " << n; | 343 RTC_CHECK_GE(n, 0) << "Bad number of handles: " << n; |
347 capture_levels_.assign(n, analog_capture_level_); | 344 capture_levels_.assign(n, analog_capture_level_); |
348 return apm_->kNoError; | 345 return apm_->kNoError; |
349 } | 346 } |
350 | 347 |
351 void GainControlImpl::AllocateRenderQueue() { | 348 void GainControlImpl::AllocateRenderQueue() { |
352 const size_t max_frame_size = std::max<size_t>( | 349 const size_t new_render_queue_element_max_size = |
353 kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2); | 350 std::max<size_t>(static_cast<size_t>(1), |
| 351 kMaxAllowedValuesOfSamplesPerFrame * num_handles()); |
354 | 352 |
355 const size_t new_render_queue_element_max_size = std::max<size_t>( | 353 if (render_queue_element_max_size_ < new_render_queue_element_max_size) { |
356 static_cast<size_t>(1), (max_frame_size * num_handles())); | 354 render_queue_element_max_size_ = new_render_queue_element_max_size; |
357 | |
358 if (new_render_queue_element_max_size > render_queue_element_max_size_) { | |
359 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); | 355 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); |
360 | 356 |
361 render_signal_queue_.reset( | 357 render_signal_queue_.reset( |
362 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( | 358 new SwapQueue<std::vector<int16_t>, RenderQueueItemVerifier<int16_t>>( |
363 kMaxNumFramesToBuffer, template_queue_element, | 359 kMaxNumFramesToBuffer, template_queue_element, |
364 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); | 360 RenderQueueItemVerifier<int16_t>(render_queue_element_max_size_))); |
| 361 |
| 362 render_queue_buffer_.resize(render_queue_element_max_size_); |
| 363 capture_queue_buffer_.resize(render_queue_element_max_size_); |
365 } else { | 364 } else { |
366 render_signal_queue_->Clear(); | 365 render_signal_queue_->Clear(); |
367 } | 366 } |
368 | |
369 render_queue_buffer_.resize(new_render_queue_element_max_size); | |
370 capture_queue_buffer_.resize(new_render_queue_element_max_size); | |
371 } | 367 } |
372 | 368 |
373 void* GainControlImpl::CreateHandle() const { | 369 void* GainControlImpl::CreateHandle() const { |
374 return WebRtcAgc_Create(); | 370 return WebRtcAgc_Create(); |
375 } | 371 } |
376 | 372 |
377 void GainControlImpl::DestroyHandle(void* handle) const { | 373 void GainControlImpl::DestroyHandle(void* handle) const { |
378 WebRtcAgc_Free(static_cast<Handle*>(handle)); | 374 WebRtcAgc_Free(static_cast<Handle*>(handle)); |
379 } | 375 } |
380 | 376 |
(...skipping 23 matching lines...) Expand all Loading... |
404 return apm_->num_output_channels(); | 400 return apm_->num_output_channels(); |
405 } | 401 } |
406 | 402 |
407 int GainControlImpl::GetHandleError(void* handle) const { | 403 int GainControlImpl::GetHandleError(void* handle) const { |
408 // The AGC has no get_error() function. | 404 // The AGC has no get_error() function. |
409 // (Despite listing errors in its interface...) | 405 // (Despite listing errors in its interface...) |
410 assert(handle != NULL); | 406 assert(handle != NULL); |
411 return apm_->kUnspecifiedError; | 407 return apm_->kUnspecifiedError; |
412 } | 408 } |
413 } // namespace webrtc | 409 } // namespace webrtc |
OLD | NEW |