blob: a828d5712b161019d0851cba86c77a529f4dc718 [file] [log] [blame]
peahe0eae3c2016-12-14 01:16:23 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_
12#define MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_
peahe0eae3c2016-12-14 01:16:23 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Yves Gerey988cc082018-10-23 12:03:01 +020016#include <memory>
17#include <vector>
18
19#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010020#include "api/audio/echo_canceller3_config.h"
Yves Gerey988cc082018-10-23 12:03:01 +020021#include "api/audio/echo_control.h"
Per Åhgren14f252a2018-11-27 18:02:56 +010022#include "modules/audio_processing/aec3/api_call_jitter_metrics.h"
Per Åhgren398689f2018-08-23 11:38:27 +020023#include "modules/audio_processing/aec3/block_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/aec3/block_framer.h"
25#include "modules/audio_processing/aec3/block_processor.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "modules/audio_processing/aec3/frame_blocker.h"
27#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/logging/apm_data_dumper.h"
Yves Gerey988cc082018-10-23 12:03:01 +020029#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/race_checker.h"
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +000031#include "rtc_base/swap_queue.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "rtc_base/thread_annotations.h"
peahe0eae3c2016-12-14 01:16:23 -080033
34namespace webrtc {
35
peahd0263542017-01-03 04:20:34 -080036// Functor for verifying the invariance of the frames being put into the render
37// queue.
38class Aec3RenderQueueItemVerifier {
39 public:
Per Åhgrence202a02019-09-02 17:01:19 +020040 Aec3RenderQueueItemVerifier(size_t num_bands,
41 size_t num_channels,
42 size_t frame_length)
43 : num_bands_(num_bands),
44 num_channels_(num_channels),
45 frame_length_(frame_length) {}
peahd0263542017-01-03 04:20:34 -080046
Per Åhgrence202a02019-09-02 17:01:19 +020047 bool operator()(const std::vector<std::vector<std::vector<float>>>& v) const {
peahd0263542017-01-03 04:20:34 -080048 if (v.size() != num_bands_) {
49 return false;
50 }
Per Åhgrence202a02019-09-02 17:01:19 +020051 for (const auto& band : v) {
52 if (band.size() != num_channels_) {
peahd0263542017-01-03 04:20:34 -080053 return false;
54 }
Per Åhgrence202a02019-09-02 17:01:19 +020055 for (const auto& channel : band) {
56 if (channel.size() != frame_length_) {
57 return false;
58 }
59 }
peahd0263542017-01-03 04:20:34 -080060 }
61 return true;
62 }
63
64 private:
65 const size_t num_bands_;
Per Åhgrence202a02019-09-02 17:01:19 +020066 const size_t num_channels_;
peahd0263542017-01-03 04:20:34 -080067 const size_t frame_length_;
68};
69
70// Main class for the echo canceller3.
71// It does 4 things:
72// -Receives 10 ms frames of band-split audio.
peahd0263542017-01-03 04:20:34 -080073// -Provides the lower level echo canceller functionality with
74// blocks of 64 samples of audio data.
75// -Partially handles the jitter in the render and capture API
76// call sequence.
77//
78// The class is supposed to be used in a non-concurrent manner apart from the
79// AnalyzeRender call which can be called concurrently with the other methods.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020080class EchoCanceller3 : public EchoControl {
peahe0eae3c2016-12-14 01:16:23 -080081 public:
peahd0263542017-01-03 04:20:34 -080082 // Normal c-tor to use.
Per Åhgrence202a02019-09-02 17:01:19 +020083 EchoCanceller3(const EchoCanceller3Config& config,
84 int sample_rate_hz,
85 size_t num_render_channels,
86 size_t num_capture_channels);
peahd0263542017-01-03 04:20:34 -080087 // Testing c-tor that is used only for testing purposes.
Per Åhgren8ba58612017-12-01 23:01:44 +010088 EchoCanceller3(const EchoCanceller3Config& config,
89 int sample_rate_hz,
Per Åhgrence202a02019-09-02 17:01:19 +020090 size_t num_render_channels,
91 size_t num_capture_channels,
peahd0263542017-01-03 04:20:34 -080092 std::unique_ptr<BlockProcessor> block_processor);
Gustaf Ullbergc5222982017-10-05 10:25:05 +020093 ~EchoCanceller3() override;
Per Åhgrence202a02019-09-02 17:01:19 +020094 EchoCanceller3(const EchoCanceller3&) = delete;
95 EchoCanceller3& operator=(const EchoCanceller3&) = delete;
96
peahe0eae3c2016-12-14 01:16:23 -080097 // Analyzes and stores an internal copy of the split-band domain render
98 // signal.
Per Åhgren0aefbf02019-08-23 21:29:17 +020099 void AnalyzeRender(AudioBuffer* render) override { AnalyzeRender(*render); }
peahe0eae3c2016-12-14 01:16:23 -0800100 // Analyzes the full-band domain capture signal to detect signal saturation.
Per Åhgren0aefbf02019-08-23 21:29:17 +0200101 void AnalyzeCapture(AudioBuffer* capture) override {
102 AnalyzeCapture(*capture);
103 }
peahe0eae3c2016-12-14 01:16:23 -0800104 // Processes the split-band domain capture signal in order to remove any echo
105 // present in the signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +0200106 void ProcessCapture(AudioBuffer* capture, bool level_change) override;
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100107 // As above, but also returns the linear filter output.
108 void ProcessCapture(AudioBuffer* capture,
109 AudioBuffer* linear_output,
110 bool level_change) override;
Gustaf Ullberg332150d2017-11-22 14:17:39 +0100111 // Collect current metrics from the echo canceller.
112 Metrics GetMetrics() const override;
Per Åhgrend0fa8202018-04-18 09:35:13 +0200113 // Provides an optional external estimate of the audio buffer delay.
Gustaf Ullberg3cb61042019-10-24 15:52:10 +0200114 void SetAudioBufferDelay(int delay_ms) override;
peahe0eae3c2016-12-14 01:16:23 -0800115
Gustaf Ullberg8675eee2019-10-09 13:34:36 +0200116 bool ActiveProcessing() const override;
117
peahd0263542017-01-03 04:20:34 -0800118 // Signals whether an external detector has detected echo leakage from the
119 // echo canceller.
120 // Note that in the case echo leakage has been flagged, it should be unflagged
121 // once it is no longer occurring.
peah69221db2017-01-27 03:28:19 -0800122 void UpdateEchoLeakageStatus(bool leakage_detected) {
peahd0263542017-01-03 04:20:34 -0800123 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peah69221db2017-01-27 03:28:19 -0800124 block_processor_->UpdateEchoLeakageStatus(leakage_detected);
peahd0263542017-01-03 04:20:34 -0800125 }
126
peahe0eae3c2016-12-14 01:16:23 -0800127 private:
peahd0263542017-01-03 04:20:34 -0800128 class RenderWriter;
129
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000130 // Empties the render SwapQueue.
peahcf02cf12017-04-05 14:18:07 -0700131 void EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800132
Per Åhgren0aefbf02019-08-23 21:29:17 +0200133 // Analyzes and stores an internal copy of the split-band domain render
134 // signal.
135 void AnalyzeRender(const AudioBuffer& render);
136 // Analyzes the full-band domain capture signal to detect signal saturation.
137 void AnalyzeCapture(const AudioBuffer& capture);
138
peahd0263542017-01-03 04:20:34 -0800139 rtc::RaceChecker capture_race_checker_;
140 rtc::RaceChecker render_race_checker_;
141
142 // State that is accessed by the AnalyzeRender call.
danilchap56359be2017-09-07 07:53:45 -0700143 std::unique_ptr<RenderWriter> render_writer_
144 RTC_GUARDED_BY(render_race_checker_);
peahd0263542017-01-03 04:20:34 -0800145
146 // State that may be accessed by the capture thread.
peahe0eae3c2016-12-14 01:16:23 -0800147 static int instance_count_;
peahd0263542017-01-03 04:20:34 -0800148 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren398689f2018-08-23 11:38:27 +0200149 const EchoCanceller3Config config_;
peahd0263542017-01-03 04:20:34 -0800150 const int sample_rate_hz_;
151 const int num_bands_;
Per Åhgrence202a02019-09-02 17:01:19 +0200152 const size_t num_render_channels_;
153 const size_t num_capture_channels_;
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100154 std::unique_ptr<BlockFramer> linear_output_framer_
155 RTC_GUARDED_BY(capture_race_checker_);
danilchap56359be2017-09-07 07:53:45 -0700156 BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_);
157 FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_);
158 FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrence202a02019-09-02 17:01:19 +0200159 SwapQueue<std::vector<std::vector<std::vector<float>>>,
160 Aec3RenderQueueItemVerifier>
Mirko Bonadeif0d9cda2019-01-17 20:43:58 +0000161 render_transfer_queue_;
peahd0263542017-01-03 04:20:34 -0800162 std::unique_ptr<BlockProcessor> block_processor_
danilchap56359be2017-09-07 07:53:45 -0700163 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrence202a02019-09-02 17:01:19 +0200164 std::vector<std::vector<std::vector<float>>> render_queue_output_frame_
danilchap56359be2017-09-07 07:53:45 -0700165 RTC_GUARDED_BY(capture_race_checker_);
danilchap56359be2017-09-07 07:53:45 -0700166 bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) =
167 false;
Per Åhgrence202a02019-09-02 17:01:19 +0200168 std::vector<std::vector<std::vector<float>>> render_block_
169 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100170 std::unique_ptr<std::vector<std::vector<std::vector<float>>>>
171 linear_output_block_ RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrence202a02019-09-02 17:01:19 +0200172 std::vector<std::vector<std::vector<float>>> capture_block_
173 RTC_GUARDED_BY(capture_race_checker_);
174 std::vector<std::vector<rtc::ArrayView<float>>> render_sub_frame_view_
175 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrenc20a19c2019-11-13 11:12:29 +0100176 std::vector<std::vector<rtc::ArrayView<float>>> linear_output_sub_frame_view_
177 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgrence202a02019-09-02 17:01:19 +0200178 std::vector<std::vector<rtc::ArrayView<float>>> capture_sub_frame_view_
danilchap56359be2017-09-07 07:53:45 -0700179 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgren398689f2018-08-23 11:38:27 +0200180 BlockDelayBuffer block_delay_buffer_ RTC_GUARDED_BY(capture_race_checker_);
Per Åhgren14f252a2018-11-27 18:02:56 +0100181 ApiCallJitterMetrics api_call_metrics_ RTC_GUARDED_BY(capture_race_checker_);
peahe0eae3c2016-12-14 01:16:23 -0800182};
183} // namespace webrtc
184
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200185#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_