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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_processing/aec3/block_framer.h" |
| 15 | #include "modules/audio_processing/aec3/block_processor.h" |
| 16 | #include "modules/audio_processing/aec3/cascaded_biquad_filter.h" |
| 17 | #include "modules/audio_processing/aec3/frame_blocker.h" |
| 18 | #include "modules/audio_processing/audio_buffer.h" |
| 19 | #include "modules/audio_processing/include/audio_processing.h" |
| 20 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
| 21 | #include "rtc_base/constructormagic.h" |
| 22 | #include "rtc_base/race_checker.h" |
| 23 | #include "rtc_base/swap_queue.h" |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 27 | // Functor for verifying the invariance of the frames being put into the render |
| 28 | // queue. |
| 29 | class Aec3RenderQueueItemVerifier { |
| 30 | public: |
| 31 | explicit Aec3RenderQueueItemVerifier(size_t num_bands, size_t frame_length) |
| 32 | : num_bands_(num_bands), frame_length_(frame_length) {} |
| 33 | |
| 34 | bool operator()(const std::vector<std::vector<float>>& v) const { |
| 35 | if (v.size() != num_bands_) { |
| 36 | return false; |
| 37 | } |
| 38 | for (const auto& v_k : v) { |
| 39 | if (v_k.size() != frame_length_) { |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | private: |
| 47 | const size_t num_bands_; |
| 48 | const size_t frame_length_; |
| 49 | }; |
| 50 | |
| 51 | // Main class for the echo canceller3. |
| 52 | // It does 4 things: |
| 53 | // -Receives 10 ms frames of band-split audio. |
| 54 | // -Optionally applies an anti-hum (high-pass) filter on the |
| 55 | // received signals. |
| 56 | // -Provides the lower level echo canceller functionality with |
| 57 | // blocks of 64 samples of audio data. |
| 58 | // -Partially handles the jitter in the render and capture API |
| 59 | // call sequence. |
| 60 | // |
| 61 | // The class is supposed to be used in a non-concurrent manner apart from the |
| 62 | // AnalyzeRender call which can be called concurrently with the other methods. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 63 | class EchoCanceller3 : public EchoControl { |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 64 | public: |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 65 | // Normal c-tor to use. |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame^] | 66 | EchoCanceller3(const EchoCanceller3Config& config, |
peah | 697a590 | 2017-06-30 07:06:10 -0700 | [diff] [blame] | 67 | int sample_rate_hz, |
| 68 | bool use_highpass_filter); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 69 | // Testing c-tor that is used only for testing purposes. |
| 70 | EchoCanceller3(int sample_rate_hz, |
| 71 | bool use_highpass_filter, |
| 72 | std::unique_ptr<BlockProcessor> block_processor); |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 73 | ~EchoCanceller3() override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 74 | // Analyzes and stores an internal copy of the split-band domain render |
| 75 | // signal. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 76 | void AnalyzeRender(AudioBuffer* farend) override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 77 | // Analyzes the full-band domain capture signal to detect signal saturation. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 78 | void AnalyzeCapture(AudioBuffer* capture) override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 79 | // Processes the split-band domain capture signal in order to remove any echo |
| 80 | // present in the signal. |
Gustaf Ullberg | c522298 | 2017-10-05 10:25:05 +0200 | [diff] [blame] | 81 | void ProcessCapture(AudioBuffer* capture, bool level_change) override; |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 82 | |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 83 | // Signals whether an external detector has detected echo leakage from the |
| 84 | // echo canceller. |
| 85 | // Note that in the case echo leakage has been flagged, it should be unflagged |
| 86 | // once it is no longer occurring. |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 87 | void UpdateEchoLeakageStatus(bool leakage_detected) { |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 88 | RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_); |
peah | 69221db | 2017-01-27 03:28:19 -0800 | [diff] [blame] | 89 | block_processor_->UpdateEchoLeakageStatus(leakage_detected); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 90 | } |
| 91 | |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 92 | // Validates a config. |
Gustaf Ullberg | bd83b91 | 2017-10-18 12:32:42 +0200 | [diff] [blame^] | 93 | static bool Validate(const EchoCanceller3Config& config); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 94 | |
| 95 | private: |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 96 | class RenderWriter; |
| 97 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 98 | // Empties the render SwapQueue. |
| 99 | void EmptyRenderQueue(); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 100 | |
| 101 | rtc::RaceChecker capture_race_checker_; |
| 102 | rtc::RaceChecker render_race_checker_; |
| 103 | |
| 104 | // State that is accessed by the AnalyzeRender call. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 105 | std::unique_ptr<RenderWriter> render_writer_ |
| 106 | RTC_GUARDED_BY(render_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 107 | |
| 108 | // State that may be accessed by the capture thread. |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 109 | static int instance_count_; |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 110 | std::unique_ptr<ApmDataDumper> data_dumper_; |
| 111 | const int sample_rate_hz_; |
| 112 | const int num_bands_; |
| 113 | const size_t frame_length_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 114 | BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_); |
| 115 | FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_); |
| 116 | FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 117 | SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier> |
| 118 | render_transfer_queue_; |
| 119 | std::unique_ptr<BlockProcessor> block_processor_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 120 | RTC_GUARDED_BY(capture_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 121 | std::vector<std::vector<float>> render_queue_output_frame_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 122 | RTC_GUARDED_BY(capture_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 123 | std::unique_ptr<CascadedBiQuadFilter> capture_highpass_filter_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 124 | RTC_GUARDED_BY(capture_race_checker_); |
| 125 | bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) = |
| 126 | false; |
| 127 | std::vector<std::vector<float>> block_ RTC_GUARDED_BY(capture_race_checker_); |
peah | d026354 | 2017-01-03 04:20:34 -0800 | [diff] [blame] | 128 | std::vector<rtc::ArrayView<float>> sub_frame_view_ |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 129 | RTC_GUARDED_BY(capture_race_checker_); |
peah | e0eae3c | 2016-12-14 01:16:23 -0800 | [diff] [blame] | 130 | |
| 131 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCanceller3); |
| 132 | }; |
| 133 | } // namespace webrtc |
| 134 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 135 | #endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_ |