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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 return AudioProcessing::kBadStreamParameterWarning; | 48 return AudioProcessing::kBadStreamParameterWarning; |
49 default: | 49 default: |
50 // AEC_UNSPECIFIED_ERROR | 50 // AEC_UNSPECIFIED_ERROR |
51 // AEC_UNINITIALIZED_ERROR | 51 // AEC_UNINITIALIZED_ERROR |
52 // AEC_NULL_POINTER_ERROR | 52 // AEC_NULL_POINTER_ERROR |
53 return AudioProcessing::kUnspecifiedError; | 53 return AudioProcessing::kUnspecifiedError; |
54 } | 54 } |
55 } | 55 } |
56 } // namespace | 56 } // namespace |
57 | 57 |
| 58 const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame1; |
| 59 const size_t EchoCancellationImpl::kAllowedValuesOfSamplesPerFrame2; |
| 60 |
58 EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm, | 61 EchoCancellationImpl::EchoCancellationImpl(const AudioProcessing* apm, |
59 CriticalSectionWrapper* crit) | 62 CriticalSectionWrapper* crit) |
60 : ProcessingComponent(), | 63 : ProcessingComponent(), |
61 apm_(apm), | 64 apm_(apm), |
62 crit_(crit), | 65 crit_(crit), |
63 drift_compensation_enabled_(false), | 66 drift_compensation_enabled_(false), |
64 metrics_enabled_(false), | 67 metrics_enabled_(false), |
65 suppression_level_(kModerateSuppression), | 68 suppression_level_(kModerateSuppression), |
66 stream_drift_samples_(0), | 69 stream_drift_samples_(0), |
67 was_stream_drift_set_(false), | 70 was_stream_drift_set_(false), |
68 stream_has_echo_(false), | 71 stream_has_echo_(false), |
69 delay_logging_enabled_(false), | 72 delay_logging_enabled_(false), |
70 extended_filter_enabled_(false), | 73 extended_filter_enabled_(false), |
71 delay_agnostic_enabled_(false) { | 74 delay_agnostic_enabled_(false), |
| 75 render_queue_element_max_size_(0) { |
| 76 AllocateRenderQueue(); |
72 } | 77 } |
73 | 78 |
74 EchoCancellationImpl::~EchoCancellationImpl() {} | 79 EchoCancellationImpl::~EchoCancellationImpl() {} |
75 | 80 |
76 int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) { | 81 int EchoCancellationImpl::ProcessRenderAudio(const AudioBuffer* audio) { |
77 if (!is_component_enabled()) { | 82 if (!is_component_enabled()) { |
78 return apm_->kNoError; | 83 return apm_->kNoError; |
79 } | 84 } |
80 | 85 |
81 assert(audio->num_frames_per_band() <= 160); | 86 assert(audio->num_frames_per_band() <= 160); |
82 assert(audio->num_channels() == apm_->num_reverse_channels()); | 87 assert(audio->num_channels() == apm_->num_reverse_channels()); |
83 | 88 |
84 int err = apm_->kNoError; | 89 int err = apm_->kNoError; |
85 | 90 |
86 // The ordering convention must be followed to pass to the correct AEC. | 91 // The ordering convention must be followed to pass to the correct AEC. |
87 size_t handle_index = 0; | 92 size_t handle_index = 0; |
| 93 render_queue_buffer_.clear(); |
88 for (int i = 0; i < apm_->num_output_channels(); i++) { | 94 for (int i = 0; i < apm_->num_output_channels(); i++) { |
89 for (int j = 0; j < audio->num_channels(); j++) { | 95 for (int j = 0; j < audio->num_channels(); j++) { |
90 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); | 96 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
91 err = WebRtcAec_BufferFarend( | 97 // Retrieve any error code produced by the buffering of the farend |
92 my_handle, | 98 // signal |
93 audio->split_bands_const_f(j)[kBand0To8kHz], | 99 err = WebRtcAec_GetBufferFarendError( |
| 100 my_handle, audio->split_bands_const_f(j)[kBand0To8kHz], |
94 audio->num_frames_per_band()); | 101 audio->num_frames_per_band()); |
95 | 102 |
96 if (err != apm_->kNoError) { | 103 if (err != apm_->kNoError) { |
97 return MapError(err); // TODO(ajm): warning possible? | 104 return MapError(err); // TODO(ajm): warning possible? |
98 } | 105 } |
99 | 106 |
100 handle_index++; | 107 // Buffer the samples in the render queue. |
| 108 render_queue_buffer_.insert(render_queue_buffer_.end(), |
| 109 audio->split_bands_const_f(j)[kBand0To8kHz], |
| 110 (audio->split_bands_const_f(j)[kBand0To8kHz] + |
| 111 audio->num_frames_per_band())); |
101 } | 112 } |
102 } | 113 } |
103 | 114 |
| 115 // Insert the samples into the queue. |
| 116 if (!render_signal_queue_->Insert(&render_queue_buffer_)) { |
| 117 ReadQueuedRenderData(); |
| 118 |
| 119 // Retry the insert (should always work). |
| 120 RTC_DCHECK_EQ(render_signal_queue_->Insert(&render_queue_buffer_), true); |
| 121 } |
| 122 |
104 return apm_->kNoError; | 123 return apm_->kNoError; |
105 } | 124 } |
106 | 125 |
| 126 // Read chunks of data that were received and queued on the render side from |
| 127 // a queue. All the data chunks are buffered into the farend signal of the AEC. |
| 128 void EchoCancellationImpl::ReadQueuedRenderData() { |
| 129 if (!is_component_enabled()) { |
| 130 return; |
| 131 } |
| 132 |
| 133 while (render_signal_queue_->Remove(&capture_queue_buffer_)) { |
| 134 size_t handle_index = 0; |
| 135 int buffer_index = 0; |
| 136 const int num_frames_per_band = |
| 137 capture_queue_buffer_.size() / |
| 138 (apm_->num_output_channels() * apm_->num_reverse_channels()); |
| 139 for (int i = 0; i < apm_->num_output_channels(); i++) { |
| 140 for (int j = 0; j < apm_->num_reverse_channels(); j++) { |
| 141 Handle* my_handle = static_cast<Handle*>(handle(handle_index)); |
| 142 WebRtcAec_BufferFarend(my_handle, &capture_queue_buffer_[buffer_index], |
| 143 num_frames_per_band); |
| 144 |
| 145 buffer_index += num_frames_per_band; |
| 146 handle_index++; |
| 147 } |
| 148 } |
| 149 } |
| 150 } |
| 151 |
107 int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) { | 152 int EchoCancellationImpl::ProcessCaptureAudio(AudioBuffer* audio) { |
108 if (!is_component_enabled()) { | 153 if (!is_component_enabled()) { |
109 return apm_->kNoError; | 154 return apm_->kNoError; |
110 } | 155 } |
111 | 156 |
112 if (!apm_->was_stream_delay_set()) { | 157 if (!apm_->was_stream_delay_set()) { |
113 return apm_->kStreamParameterNotSetError; | 158 return apm_->kStreamParameterNotSetError; |
114 } | 159 } |
115 | 160 |
116 if (drift_compensation_enabled_ && !was_stream_drift_set_) { | 161 if (drift_compensation_enabled_ && !was_stream_drift_set_) { |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 Handle* my_handle = static_cast<Handle*>(handle(0)); | 371 Handle* my_handle = static_cast<Handle*>(handle(0)); |
327 return WebRtcAec_aec_core(my_handle); | 372 return WebRtcAec_aec_core(my_handle); |
328 } | 373 } |
329 | 374 |
330 int EchoCancellationImpl::Initialize() { | 375 int EchoCancellationImpl::Initialize() { |
331 int err = ProcessingComponent::Initialize(); | 376 int err = ProcessingComponent::Initialize(); |
332 if (err != apm_->kNoError || !is_component_enabled()) { | 377 if (err != apm_->kNoError || !is_component_enabled()) { |
333 return err; | 378 return err; |
334 } | 379 } |
335 | 380 |
| 381 AllocateRenderQueue(); |
| 382 |
336 return apm_->kNoError; | 383 return apm_->kNoError; |
337 } | 384 } |
338 | 385 |
| 386 void EchoCancellationImpl::AllocateRenderQueue() { |
| 387 const size_t max_frame_size = std::max<size_t>( |
| 388 kAllowedValuesOfSamplesPerFrame1, kAllowedValuesOfSamplesPerFrame2); |
| 389 |
| 390 const size_t new_render_queue_element_max_size = std::max<size_t>( |
| 391 static_cast<size_t>(1), max_frame_size * num_handles_required()); |
| 392 |
| 393 // Reallocate the queue if the queue item size is too small to fit the |
| 394 // data to put in the queue. |
| 395 if (new_render_queue_element_max_size > render_queue_element_max_size_) { |
| 396 render_queue_element_max_size_ = new_render_queue_element_max_size; |
| 397 |
| 398 std::vector<float> template_queue_element(render_queue_element_max_size_); |
| 399 |
| 400 render_signal_queue_.reset( |
| 401 new SwapQueue<std::vector<float>, RenderQueueItemVerifier<float>>( |
| 402 kMaxNumFramesToBuffer, template_queue_element, |
| 403 RenderQueueItemVerifier<float>(render_queue_element_max_size_))); |
| 404 } else { |
| 405 render_signal_queue_->Clear(); |
| 406 } |
| 407 |
| 408 render_queue_buffer_.resize(new_render_queue_element_max_size); |
| 409 capture_queue_buffer_.resize(new_render_queue_element_max_size); |
| 410 } |
| 411 |
339 void EchoCancellationImpl::SetExtraOptions(const Config& config) { | 412 void EchoCancellationImpl::SetExtraOptions(const Config& config) { |
340 extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled; | 413 extended_filter_enabled_ = config.Get<ExtendedFilter>().enabled; |
341 delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled; | 414 delay_agnostic_enabled_ = config.Get<DelayAgnostic>().enabled; |
342 Configure(); | 415 Configure(); |
343 } | 416 } |
344 | 417 |
345 void* EchoCancellationImpl::CreateHandle() const { | 418 void* EchoCancellationImpl::CreateHandle() const { |
346 return WebRtcAec_Create(); | 419 return WebRtcAec_Create(); |
347 } | 420 } |
348 | 421 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 int EchoCancellationImpl::num_handles_required() const { | 454 int EchoCancellationImpl::num_handles_required() const { |
382 return apm_->num_output_channels() * | 455 return apm_->num_output_channels() * |
383 apm_->num_reverse_channels(); | 456 apm_->num_reverse_channels(); |
384 } | 457 } |
385 | 458 |
386 int EchoCancellationImpl::GetHandleError(void* handle) const { | 459 int EchoCancellationImpl::GetHandleError(void* handle) const { |
387 assert(handle != NULL); | 460 assert(handle != NULL); |
388 return AudioProcessing::kUnspecifiedError; | 461 return AudioProcessing::kUnspecifiedError; |
389 } | 462 } |
390 } // namespace webrtc | 463 } // namespace webrtc |
OLD | NEW |