Chromium Code Reviews| 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 17 matching lines...) Expand all Loading... | |
| 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 render_queue_buffer_.resize(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); |
| 76 | |
| 77 // Buffer the samples in the render queue. | |
| 78 render_queue_buffer_.insert( | |
| 79 render_queue_buffer_.end(), audio->mixed_low_pass_data(), | |
| 80 (audio->mixed_low_pass_data() + audio->num_frames_per_band())); | |
| 81 } | |
| 82 | |
| 83 // Check of success is temporarily disabled as it breaks a unit test. | |
| 84 // TODO(peah): Will be fixed in the next CL. | |
| 85 (void)render_signal_queue_->Insert(&render_queue_buffer_); | |
|
hlundin-webrtc
2015/11/05 13:01:09
Drop (void).
peah-webrtc
2015/11/06 07:10:58
I think is needed due to the WARN_UNUSED_RESULT an
hlundin-webrtc
2015/11/06 07:52:33
Acknowledged.
peah-webrtc
2015/11/06 08:10:03
Acknowledged.
| |
| 86 | |
| 87 return apm_->kNoError; | |
| 88 } | |
| 89 | |
| 90 // Read chunks of data that were received and queued on the render side from | |
| 91 // a queue. All the data chunks are buffered into the farend signal of the AGC. | |
| 92 void GainControlImpl::ReadQueuedRenderData() { | |
| 93 if (!is_component_enabled()) { | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { | |
| 98 int buffer_index = 0; | |
| 99 const int num_frames_per_band = | |
| 100 capture_queue_buffer_.size() / num_handles(); | |
| 101 for (int i = 0; i < num_handles(); i++) { | |
| 102 Handle* my_handle = static_cast<Handle*>(handle(i)); | |
| 103 (void)WebRtcAgc_AddFarend(my_handle, &capture_queue_buffer_[buffer_index], | |
|
hlundin-webrtc
2015/11/05 13:01:09
Drop (void).
peah-webrtc
2015/11/06 07:10:58
Done.
| |
| 104 num_frames_per_band); | |
| 105 | |
| 106 buffer_index += num_frames_per_band; | |
| 71 } | 107 } |
| 72 } | 108 } |
| 73 | |
| 74 return apm_->kNoError; | |
| 75 } | 109 } |
| 76 | 110 |
| 77 int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { | 111 int GainControlImpl::AnalyzeCaptureAudio(AudioBuffer* audio) { |
| 78 if (!is_component_enabled()) { | 112 if (!is_component_enabled()) { |
| 79 return apm_->kNoError; | 113 return apm_->kNoError; |
| 80 } | 114 } |
| 81 | 115 |
| 82 assert(audio->num_frames_per_band() <= 160); | 116 assert(audio->num_frames_per_band() <= 160); |
| 83 assert(audio->num_channels() == num_handles()); | 117 assert(audio->num_channels() == num_handles()); |
| 84 | 118 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 | 206 |
| 173 analog_capture_level_ /= num_handles(); | 207 analog_capture_level_ /= num_handles(); |
| 174 } | 208 } |
| 175 | 209 |
| 176 was_analog_level_set_ = false; | 210 was_analog_level_set_ = false; |
| 177 return apm_->kNoError; | 211 return apm_->kNoError; |
| 178 } | 212 } |
| 179 | 213 |
| 180 // TODO(ajm): ensure this is called under kAdaptiveAnalog. | 214 // TODO(ajm): ensure this is called under kAdaptiveAnalog. |
| 181 int GainControlImpl::set_stream_analog_level(int level) { | 215 int GainControlImpl::set_stream_analog_level(int level) { |
| 216 // TODO(peah): Verify that this is really needed to do the reading. | |
|
hlundin-webrtc
2015/11/05 13:01:09
Extra . at end of line.
peah-webrtc
2015/11/06 07:10:58
Done.
| |
| 217 // here as well as in ProcessStream. It works since these functions | |
| 218 // are called from the same thread, but it is not nice to do it in two | |
| 219 // places if not needed. | |
| 220 ReadQueuedRenderData(); | |
| 221 | |
| 182 CriticalSectionScoped crit_scoped(crit_); | 222 CriticalSectionScoped crit_scoped(crit_); |
| 183 was_analog_level_set_ = true; | 223 was_analog_level_set_ = true; |
| 184 if (level < minimum_capture_level_ || level > maximum_capture_level_) { | 224 if (level < minimum_capture_level_ || level > maximum_capture_level_) { |
| 185 return apm_->kBadParameterError; | 225 return apm_->kBadParameterError; |
| 186 } | 226 } |
| 187 analog_capture_level_ = level; | 227 analog_capture_level_ = level; |
| 188 | 228 |
| 189 return apm_->kNoError; | 229 return apm_->kNoError; |
| 190 } | 230 } |
| 191 | 231 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 bool GainControlImpl::is_limiter_enabled() const { | 329 bool GainControlImpl::is_limiter_enabled() const { |
| 290 return limiter_enabled_; | 330 return limiter_enabled_; |
| 291 } | 331 } |
| 292 | 332 |
| 293 int GainControlImpl::Initialize() { | 333 int GainControlImpl::Initialize() { |
| 294 int err = ProcessingComponent::Initialize(); | 334 int err = ProcessingComponent::Initialize(); |
| 295 if (err != apm_->kNoError || !is_component_enabled()) { | 335 if (err != apm_->kNoError || !is_component_enabled()) { |
| 296 return err; | 336 return err; |
| 297 } | 337 } |
| 298 | 338 |
| 339 AllocateRenderQueue(); | |
| 340 | |
| 299 capture_levels_.assign(num_handles(), analog_capture_level_); | 341 capture_levels_.assign(num_handles(), analog_capture_level_); |
| 300 return apm_->kNoError; | 342 return apm_->kNoError; |
| 301 } | 343 } |
| 302 | 344 |
| 345 void GainControlImpl::AllocateRenderQueue() { | |
| 346 const size_t max_frame_size = std::max(kAllowedValuesOfSamplesPerFrame1, | |
| 347 kAllowedValuesOfSamplesPerFrame2); | |
| 348 | |
| 349 const size_t new_render_queue_element_max_size = | |
| 350 std::max(1UL, (max_frame_size * num_handles())); | |
|
hlundin-webrtc
2015/11/05 13:01:09
1UL...
peah-webrtc
2015/11/06 07:10:58
Done.
| |
| 351 | |
| 352 if (new_render_queue_element_max_size > render_queue_element_max_size_) { | |
| 353 std::vector<int16_t> template_queue_element(render_queue_element_max_size_); | |
| 354 | |
| 355 render_signal_queue_.reset( | |
| 356 new SwapQueue<std::vector<int16_t>, AgcRenderQueueItemVerifier>( | |
| 357 kMaxNumFramesToBuffer, | |
| 358 AgcRenderQueueItemVerifier(render_queue_element_max_size_), | |
| 359 template_queue_element)); | |
| 360 } else { | |
| 361 render_signal_queue_->Clear(); | |
| 362 } | |
| 363 | |
| 364 render_queue_buffer_.resize(new_render_queue_element_max_size); | |
| 365 capture_queue_buffer_.resize(new_render_queue_element_max_size); | |
| 366 } | |
| 367 | |
| 303 void* GainControlImpl::CreateHandle() const { | 368 void* GainControlImpl::CreateHandle() const { |
| 304 return WebRtcAgc_Create(); | 369 return WebRtcAgc_Create(); |
| 305 } | 370 } |
| 306 | 371 |
| 307 void GainControlImpl::DestroyHandle(void* handle) const { | 372 void GainControlImpl::DestroyHandle(void* handle) const { |
| 308 WebRtcAgc_Free(static_cast<Handle*>(handle)); | 373 WebRtcAgc_Free(static_cast<Handle*>(handle)); |
| 309 } | 374 } |
| 310 | 375 |
| 311 int GainControlImpl::InitializeHandle(void* handle) const { | 376 int GainControlImpl::InitializeHandle(void* handle) const { |
| 312 return WebRtcAgc_Init(static_cast<Handle*>(handle), | 377 return WebRtcAgc_Init(static_cast<Handle*>(handle), |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 334 return apm_->num_output_channels(); | 399 return apm_->num_output_channels(); |
| 335 } | 400 } |
| 336 | 401 |
| 337 int GainControlImpl::GetHandleError(void* handle) const { | 402 int GainControlImpl::GetHandleError(void* handle) const { |
| 338 // The AGC has no get_error() function. | 403 // The AGC has no get_error() function. |
| 339 // (Despite listing errors in its interface...) | 404 // (Despite listing errors in its interface...) |
| 340 assert(handle != NULL); | 405 assert(handle != NULL); |
| 341 return apm_->kUnspecifiedError; | 406 return apm_->kUnspecifiedError; |
| 342 } | 407 } |
| 343 } // namespace webrtc | 408 } // namespace webrtc |
| OLD | NEW |