peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_ |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 16 | #include <memory> |
| 17 | #include <vector> |
| 18 | |
| 19 | #include "api/array_view.h" |
Gustaf Ullberg | 3646f97 | 2018-02-14 15:19:04 +0100 | [diff] [blame] | 20 | #include "api/audio/echo_canceller3_config.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | #include "api/audio/echo_control.h" |
Per Åhgren | 14f252a | 2018-11-27 18:02:56 +0100 | [diff] [blame] | 22 | #include "modules/audio_processing/aec3/api_call_jitter_metrics.h" |
Per Åhgren | 398689f | 2018-08-23 11:38:27 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/aec3/block_delay_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "modules/audio_processing/aec3/block_framer.h" |
| 25 | #include "modules/audio_processing/aec3/block_processor.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/audio_processing/aec3/frame_blocker.h" |
| 27 | #include "modules/audio_processing/audio_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 29 | #include "rtc_base/checks.h" |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 30 | #include "rtc_base/constructor_magic.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/race_checker.h" |
Mirko Bonadei | f0d9cda | 2019-01-17 20:43:58 +0000 | [diff] [blame] | 32 | #include "rtc_base/swap_queue.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 33 | #include "rtc_base/thread_annotations.h" |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 37 | // Functor for verifying the invariance of the frames being put into the render |
| 38 | // queue. |
| 39 | class Aec3RenderQueueItemVerifier { |
| 40 | public: |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 41 | explicit Aec3RenderQueueItemVerifier(size_t num_bands, size_t frame_length) |
| 42 | : num_bands_(num_bands), frame_length_(frame_length) {} |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 43 | |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 44 | bool operator()(const std::vector<std::vector<float>>& v) const { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 45 | if (v.size() != num_bands_) { |
| 46 | return false; |
| 47 | } |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 48 | for (const auto& v_k : v) { |
| 49 | if (v_k.size() != frame_length_) { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | const size_t num_bands_; |
| 58 | const size_t frame_length_; |
| 59 | }; |
| 60 | |
| 61 | // Main class for the echo canceller3. |
| 62 | // It does 4 things: |
| 63 | // -Receives 10 ms frames of band-split audio. |
| 64 | // -Optionally applies an anti-hum (high-pass) filter on the |
| 65 | // received signals. |
| 66 | // -Provides the lower level echo canceller functionality with |
| 67 | // blocks of 64 samples of audio data. |
| 68 | // -Partially handles the jitter in the render and capture API |
| 69 | // call sequence. |
| 70 | // |
| 71 | // The class is supposed to be used in a non-concurrent manner apart from the |
| 72 | // AnalyzeRender call which can be called concurrently with the other methods. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 73 | class EchoCanceller3 : public EchoControl { |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 74 | public: |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 75 | // Normal c-tor to use. |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 76 | EchoCanceller3(const EchoCanceller3Config& config, int sample_rate_hz); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 77 | // Testing c-tor that is used only for testing purposes. |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 78 | EchoCanceller3(const EchoCanceller3Config& config, |
| 79 | int sample_rate_hz, |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 80 | std::unique_ptr<BlockProcessor> block_processor); |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 81 | ~EchoCanceller3() override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 82 | // Analyzes and stores an internal copy of the split-band domain render |
| 83 | // signal. |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 84 | void AnalyzeRender(AudioBuffer* render) override { AnalyzeRender(*render); } |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 85 | // Analyzes the full-band domain capture signal to detect signal saturation. |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 86 | void AnalyzeCapture(AudioBuffer* capture) override { |
| 87 | AnalyzeCapture(*capture); |
| 88 | } |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 89 | // Processes the split-band domain capture signal in order to remove any echo |
| 90 | // present in the signal. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 91 | void ProcessCapture(AudioBuffer* capture, bool level_change) override; |
Gustaf Ullberg | 332150d | 2017-11-22 14:17:39 +0100 | [diff] [blame] | 92 | // Collect current metrics from the echo canceller. |
| 93 | Metrics GetMetrics() const override; |
Per Åhgren | d0fa820 | 2018-04-18 09:35:13 +0200 | [diff] [blame] | 94 | // Provides an optional external estimate of the audio buffer delay. |
| 95 | void SetAudioBufferDelay(size_t delay_ms) override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 96 | |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 97 | // Signals whether an external detector has detected echo leakage from the |
| 98 | // echo canceller. |
| 99 | // Note that in the case echo leakage has been flagged, it should be unflagged |
| 100 | // once it is no longer occurring. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 101 | void UpdateEchoLeakageStatus(bool leakage_detected) { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 102 | RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 103 | block_processor_->UpdateEchoLeakageStatus(leakage_detected); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 104 | } |
| 105 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 106 | private: |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 107 | class RenderWriter; |
| 108 | |
Mirko Bonadei | f0d9cda | 2019-01-17 20:43:58 +0000 | [diff] [blame] | 109 | // Empties the render SwapQueue. |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 110 | void EmptyRenderQueue(); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 111 | |
Per Åhgren | 0aefbf0 | 2019-08-23 21:29:17 +0200 | [diff] [blame] | 112 | // Analyzes and stores an internal copy of the split-band domain render |
| 113 | // signal. |
| 114 | void AnalyzeRender(const AudioBuffer& render); |
| 115 | // Analyzes the full-band domain capture signal to detect signal saturation. |
| 116 | void AnalyzeCapture(const AudioBuffer& capture); |
| 117 | |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 118 | rtc::RaceChecker capture_race_checker_; |
| 119 | rtc::RaceChecker render_race_checker_; |
| 120 | |
| 121 | // State that is accessed by the AnalyzeRender call. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 122 | std::unique_ptr<RenderWriter> render_writer_ |
| 123 | RTC_GUARDED_BY(render_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 124 | |
| 125 | // State that may be accessed by the capture thread. |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 126 | static int instance_count_; |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 127 | std::unique_ptr<ApmDataDumper> data_dumper_; |
Per Åhgren | 398689f | 2018-08-23 11:38:27 +0200 | [diff] [blame] | 128 | const EchoCanceller3Config config_; |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 129 | const int sample_rate_hz_; |
| 130 | const int num_bands_; |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 131 | const size_t frame_length_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 132 | BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_); |
| 133 | FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_); |
| 134 | FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_); |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 135 | SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier> |
Mirko Bonadei | f0d9cda | 2019-01-17 20:43:58 +0000 | [diff] [blame] | 136 | render_transfer_queue_; |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 137 | std::unique_ptr<BlockProcessor> block_processor_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 138 | RTC_GUARDED_BY(capture_race_checker_); |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 139 | std::vector<std::vector<float>> render_queue_output_frame_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 140 | RTC_GUARDED_BY(capture_race_checker_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 141 | bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) = |
| 142 | false; |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 143 | std::vector<std::vector<float>> block_ RTC_GUARDED_BY(capture_race_checker_); |
| 144 | std::vector<rtc::ArrayView<float>> sub_frame_view_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 145 | RTC_GUARDED_BY(capture_race_checker_); |
Per Åhgren | 398689f | 2018-08-23 11:38:27 +0200 | [diff] [blame] | 146 | BlockDelayBuffer block_delay_buffer_ RTC_GUARDED_BY(capture_race_checker_); |
Per Åhgren | 14f252a | 2018-11-27 18:02:56 +0100 | [diff] [blame] | 147 | ApiCallJitterMetrics api_call_metrics_ RTC_GUARDED_BY(capture_race_checker_); |
Per Åhgren | d112c75 | 2019-09-02 13:56:56 +0000 | [diff] [blame] | 148 | |
| 149 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCanceller3); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 150 | }; |
| 151 | } // namespace webrtc |
| 152 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 153 | #endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_ |