| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 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 #include "webrtc/modules/audio_processing/aec3/block_processor.h" | 10 #include "webrtc/modules/audio_processing/aec3/block_processor.h" |
| 11 | 11 |
| 12 #include "webrtc/base/atomicops.h" | 12 #include "webrtc/base/atomicops.h" |
| 13 #include "webrtc/base/constructormagic.h" |
| 13 #include "webrtc/base/optional.h" | 14 #include "webrtc/base/optional.h" |
| 14 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" | 15 #include "webrtc/modules/audio_processing/aec3/aec3_constants.h" |
| 16 #include "webrtc/modules/audio_processing/aec3/echo_path_variability.h" |
| 17 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 18 #include "webrtc/system_wrappers/include/logging.h" |
| 15 | 19 |
| 16 namespace webrtc { | 20 namespace webrtc { |
| 17 namespace { | 21 namespace { |
| 18 | 22 |
| 19 class BlockProcessorImpl final : public BlockProcessor { | 23 class BlockProcessorImpl final : public BlockProcessor { |
| 20 public: | 24 public: |
| 21 explicit BlockProcessorImpl(int sample_rate_hz); | 25 BlockProcessorImpl(int sample_rate_hz, |
| 26 std::unique_ptr<RenderDelayBuffer> render_buffer, |
| 27 std::unique_ptr<RenderDelayController> delay_controller, |
| 28 std::unique_ptr<EchoRemover> echo_remover); |
| 29 |
| 22 ~BlockProcessorImpl() override; | 30 ~BlockProcessorImpl() override; |
| 23 | 31 |
| 24 void ProcessCapture(bool known_echo_path_change, | 32 void ProcessCapture(bool echo_path_gain_change, |
| 25 bool saturated_microphone_signal, | 33 bool capture_signal_saturation, |
| 26 std::vector<std::vector<float>>* capture_block) override; | 34 std::vector<std::vector<float>>* capture_block) override; |
| 27 | 35 |
| 28 bool BufferRender(std::vector<std::vector<float>>* block) override; | 36 bool BufferRender(std::vector<std::vector<float>>* block) override; |
| 29 | 37 |
| 30 void ReportEchoLeakage(bool leakage_detected) override; | 38 void UpdateEchoLeakageStatus(bool leakage_detected) override; |
| 31 | 39 |
| 32 private: | 40 private: |
| 33 const size_t sample_rate_hz_; | |
| 34 static int instance_count_; | 41 static int instance_count_; |
| 35 std::unique_ptr<ApmDataDumper> data_dumper_; | 42 std::unique_ptr<ApmDataDumper> data_dumper_; |
| 43 const size_t sample_rate_hz_; |
| 44 std::unique_ptr<RenderDelayBuffer> render_buffer_; |
| 45 std::unique_ptr<RenderDelayController> delay_controller_; |
| 46 std::unique_ptr<EchoRemover> echo_remover_; |
| 36 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(BlockProcessorImpl); | 47 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(BlockProcessorImpl); |
| 37 }; | 48 }; |
| 38 | 49 |
| 50 constexpr size_t kRenderBufferSize = 250; |
| 39 int BlockProcessorImpl::instance_count_ = 0; | 51 int BlockProcessorImpl::instance_count_ = 0; |
| 52 constexpr size_t kMaxApiJitter = 30; |
| 40 | 53 |
| 41 BlockProcessorImpl::BlockProcessorImpl(int sample_rate_hz) | 54 BlockProcessorImpl::BlockProcessorImpl( |
| 42 : sample_rate_hz_(sample_rate_hz), | 55 int sample_rate_hz, |
| 43 data_dumper_( | 56 std::unique_ptr<RenderDelayBuffer> render_buffer, |
| 44 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))) {} | 57 std::unique_ptr<RenderDelayController> delay_controller, |
| 58 std::unique_ptr<EchoRemover> echo_remover) |
| 59 : data_dumper_( |
| 60 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
| 61 sample_rate_hz_(sample_rate_hz), |
| 62 render_buffer_(std::move(render_buffer)), |
| 63 delay_controller_(std::move(delay_controller)), |
| 64 echo_remover_(std::move(echo_remover)) {} |
| 45 | 65 |
| 46 BlockProcessorImpl::~BlockProcessorImpl() = default; | 66 BlockProcessorImpl::~BlockProcessorImpl() = default; |
| 47 | 67 |
| 48 void BlockProcessorImpl::ProcessCapture( | 68 void BlockProcessorImpl::ProcessCapture( |
| 49 bool known_echo_path_change, | 69 bool echo_path_gain_change, |
| 50 bool saturated_microphone_signal, | 70 bool capture_signal_saturation, |
| 51 std::vector<std::vector<float>>* capture_block) { | 71 std::vector<std::vector<float>>* capture_block) { |
| 52 RTC_DCHECK(capture_block); | 72 RTC_DCHECK(capture_block); |
| 53 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size()); | 73 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), capture_block->size()); |
| 54 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size()); | 74 RTC_DCHECK_EQ(kBlockSize, (*capture_block)[0].size()); |
| 75 |
| 76 const size_t delay = delay_controller_->GetDelay((*capture_block)[0]); |
| 77 const bool render_delay_change = delay != render_buffer_->Delay(); |
| 78 |
| 79 if (render_delay_change) { |
| 80 render_buffer_->SetDelay(delay); |
| 81 } |
| 82 |
| 83 if (render_buffer_->IsBlockAvailable()) { |
| 84 auto& render_block = render_buffer_->GetNext(); |
| 85 echo_remover_->ProcessBlock( |
| 86 delay_controller_->AlignmentHeadroomSamples(), |
| 87 EchoPathVariability(echo_path_gain_change, render_delay_change), |
| 88 capture_signal_saturation, render_block, capture_block); |
| 89 } else { |
| 90 LOG(LS_INFO) << "AEC3 empty render buffer"; |
| 91 } |
| 55 } | 92 } |
| 56 | 93 |
| 57 bool BlockProcessorImpl::BufferRender( | 94 bool BlockProcessorImpl::BufferRender(std::vector<std::vector<float>>* block) { |
| 58 std::vector<std::vector<float>>* render_block) { | 95 RTC_DCHECK(block); |
| 59 RTC_DCHECK(render_block); | 96 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), block->size()); |
| 60 RTC_DCHECK_EQ(NumBandsForRate(sample_rate_hz_), render_block->size()); | 97 RTC_DCHECK_EQ(kBlockSize, (*block)[0].size()); |
| 61 RTC_DCHECK_EQ(kBlockSize, (*render_block)[0].size()); | 98 |
| 62 return false; | 99 const bool delay_controller_overrun = |
| 100 !delay_controller_->AnalyzeRender((*block)[0]); |
| 101 const bool render_buffer_overrun = !render_buffer_->Insert(block); |
| 102 if (delay_controller_overrun || render_buffer_overrun) { |
| 103 LOG(LS_INFO) << "AEC3 buffer overrrun"; |
| 104 return false; |
| 105 } |
| 106 |
| 107 return true; |
| 63 } | 108 } |
| 64 | 109 |
| 65 void BlockProcessorImpl::ReportEchoLeakage(bool leakage_detected) {} | 110 void BlockProcessorImpl::UpdateEchoLeakageStatus(bool leakage_detected) { |
| 111 echo_remover_->UpdateEchoLeakageStatus(leakage_detected); |
| 112 } |
| 66 | 113 |
| 67 } // namespace | 114 } // namespace |
| 68 | 115 |
| 69 BlockProcessor* BlockProcessor::Create(int sample_rate_hz) { | 116 BlockProcessor* BlockProcessor::Create(int sample_rate_hz) { |
| 70 return new BlockProcessorImpl(sample_rate_hz); | 117 std::unique_ptr<RenderDelayBuffer> render_buffer(RenderDelayBuffer::Create( |
| 118 kRenderBufferSize, NumBandsForRate(sample_rate_hz), kMaxApiJitter)); |
| 119 std::unique_ptr<RenderDelayController> delay_controller( |
| 120 RenderDelayController::Create(sample_rate_hz, *render_buffer)); |
| 121 std::unique_ptr<EchoRemover> echo_remover( |
| 122 EchoRemover::Create(sample_rate_hz)); |
| 123 return Create(sample_rate_hz, std::move(render_buffer), |
| 124 std::move(delay_controller), std::move(echo_remover)); |
| 125 } |
| 126 |
| 127 BlockProcessor* BlockProcessor::Create( |
| 128 int sample_rate_hz, |
| 129 std::unique_ptr<RenderDelayBuffer> render_buffer) { |
| 130 std::unique_ptr<RenderDelayController> delay_controller( |
| 131 RenderDelayController::Create(sample_rate_hz, *render_buffer)); |
| 132 std::unique_ptr<EchoRemover> echo_remover( |
| 133 EchoRemover::Create(sample_rate_hz)); |
| 134 return Create(sample_rate_hz, std::move(render_buffer), |
| 135 std::move(delay_controller), std::move(echo_remover)); |
| 136 } |
| 137 |
| 138 BlockProcessor* BlockProcessor::Create( |
| 139 int sample_rate_hz, |
| 140 std::unique_ptr<RenderDelayBuffer> render_buffer, |
| 141 std::unique_ptr<RenderDelayController> delay_controller, |
| 142 std::unique_ptr<EchoRemover> echo_remover) { |
| 143 return new BlockProcessorImpl(sample_rate_hz, std::move(render_buffer), |
| 144 std::move(delay_controller), |
| 145 std::move(echo_remover)); |
| 71 } | 146 } |
| 72 | 147 |
| 73 } // namespace webrtc | 148 } // namespace webrtc |
| OLD | NEW |