blob: f62016f9f6e7e5126ecad95e9897051d24641cee [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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"
peahe0eae3c2016-12-14 01:16:23 -080024
25namespace webrtc {
26
peahd0263542017-01-03 04:20:34 -080027// Functor for verifying the invariance of the frames being put into the render
28// queue.
29class 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 Ullbergc5222982017-10-05 10:25:05 +020063class EchoCanceller3 : public EchoControl {
peahe0eae3c2016-12-14 01:16:23 -080064 public:
peahd0263542017-01-03 04:20:34 -080065 // Normal c-tor to use.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020066 EchoCanceller3(const EchoCanceller3Config& config,
peah697a5902017-06-30 07:06:10 -070067 int sample_rate_hz,
68 bool use_highpass_filter);
peahd0263542017-01-03 04:20:34 -080069 // 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 Ullbergc5222982017-10-05 10:25:05 +020073 ~EchoCanceller3() override;
peahe0eae3c2016-12-14 01:16:23 -080074 // Analyzes and stores an internal copy of the split-band domain render
75 // signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020076 void AnalyzeRender(AudioBuffer* farend) override;
peahe0eae3c2016-12-14 01:16:23 -080077 // Analyzes the full-band domain capture signal to detect signal saturation.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020078 void AnalyzeCapture(AudioBuffer* capture) override;
peahe0eae3c2016-12-14 01:16:23 -080079 // Processes the split-band domain capture signal in order to remove any echo
80 // present in the signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020081 void ProcessCapture(AudioBuffer* capture, bool level_change) override;
peahe0eae3c2016-12-14 01:16:23 -080082
peahd0263542017-01-03 04:20:34 -080083 // 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.
peah69221db2017-01-27 03:28:19 -080087 void UpdateEchoLeakageStatus(bool leakage_detected) {
peahd0263542017-01-03 04:20:34 -080088 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peah69221db2017-01-27 03:28:19 -080089 block_processor_->UpdateEchoLeakageStatus(leakage_detected);
peahd0263542017-01-03 04:20:34 -080090 }
91
peahe0eae3c2016-12-14 01:16:23 -080092 // Validates a config.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020093 static bool Validate(const EchoCanceller3Config& config);
peahe0eae3c2016-12-14 01:16:23 -080094
95 private:
peahd0263542017-01-03 04:20:34 -080096 class RenderWriter;
97
peahcf02cf12017-04-05 14:18:07 -070098 // Empties the render SwapQueue.
99 void EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800100
101 rtc::RaceChecker capture_race_checker_;
102 rtc::RaceChecker render_race_checker_;
103
104 // State that is accessed by the AnalyzeRender call.
danilchap56359be2017-09-07 07:53:45 -0700105 std::unique_ptr<RenderWriter> render_writer_
106 RTC_GUARDED_BY(render_race_checker_);
peahd0263542017-01-03 04:20:34 -0800107
108 // State that may be accessed by the capture thread.
peahe0eae3c2016-12-14 01:16:23 -0800109 static int instance_count_;
peahd0263542017-01-03 04:20:34 -0800110 std::unique_ptr<ApmDataDumper> data_dumper_;
111 const int sample_rate_hz_;
112 const int num_bands_;
113 const size_t frame_length_;
danilchap56359be2017-09-07 07:53:45 -0700114 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_);
peahd0263542017-01-03 04:20:34 -0800117 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>
118 render_transfer_queue_;
119 std::unique_ptr<BlockProcessor> block_processor_
danilchap56359be2017-09-07 07:53:45 -0700120 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800121 std::vector<std::vector<float>> render_queue_output_frame_
danilchap56359be2017-09-07 07:53:45 -0700122 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800123 std::unique_ptr<CascadedBiQuadFilter> capture_highpass_filter_
danilchap56359be2017-09-07 07:53:45 -0700124 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_);
peahd0263542017-01-03 04:20:34 -0800128 std::vector<rtc::ArrayView<float>> sub_frame_view_
danilchap56359be2017-09-07 07:53:45 -0700129 RTC_GUARDED_BY(capture_race_checker_);
peahe0eae3c2016-12-14 01:16:23 -0800130
131 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCanceller3);
132};
133} // namespace webrtc
134
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200135#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_