blob: f5520ba3ace5c3492a6b2a26896c883255f8855a [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
Gustaf Ullberg3646f972018-02-14 15:19:04 +010014#include "api/audio/echo_canceller3_config.h"
Per Åhgren398689f2018-08-23 11:38:27 +020015#include "modules/audio_processing/aec3/block_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/aec3/block_framer.h"
17#include "modules/audio_processing/aec3/block_processor.h"
18#include "modules/audio_processing/aec3/cascaded_biquad_filter.h"
19#include "modules/audio_processing/aec3/frame_blocker.h"
20#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_processing/logging/apm_data_dumper.h"
22#include "rtc_base/constructormagic.h"
23#include "rtc_base/race_checker.h"
24#include "rtc_base/swap_queue.h"
peahe0eae3c2016-12-14 01:16:23 -080025
26namespace webrtc {
27
peahd0263542017-01-03 04:20:34 -080028// Functor for verifying the invariance of the frames being put into the render
29// queue.
30class Aec3RenderQueueItemVerifier {
31 public:
32 explicit Aec3RenderQueueItemVerifier(size_t num_bands, size_t frame_length)
33 : num_bands_(num_bands), frame_length_(frame_length) {}
34
35 bool operator()(const std::vector<std::vector<float>>& v) const {
36 if (v.size() != num_bands_) {
37 return false;
38 }
39 for (const auto& v_k : v) {
40 if (v_k.size() != frame_length_) {
41 return false;
42 }
43 }
44 return true;
45 }
46
47 private:
48 const size_t num_bands_;
49 const size_t frame_length_;
50};
51
52// Main class for the echo canceller3.
53// It does 4 things:
54// -Receives 10 ms frames of band-split audio.
55// -Optionally applies an anti-hum (high-pass) filter on the
56// received signals.
57// -Provides the lower level echo canceller functionality with
58// blocks of 64 samples of audio data.
59// -Partially handles the jitter in the render and capture API
60// call sequence.
61//
62// The class is supposed to be used in a non-concurrent manner apart from the
63// AnalyzeRender call which can be called concurrently with the other methods.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020064class EchoCanceller3 : public EchoControl {
peahe0eae3c2016-12-14 01:16:23 -080065 public:
peahd0263542017-01-03 04:20:34 -080066 // Normal c-tor to use.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020067 EchoCanceller3(const EchoCanceller3Config& config,
peah697a5902017-06-30 07:06:10 -070068 int sample_rate_hz,
69 bool use_highpass_filter);
peahd0263542017-01-03 04:20:34 -080070 // Testing c-tor that is used only for testing purposes.
Per Åhgren8ba58612017-12-01 23:01:44 +010071 EchoCanceller3(const EchoCanceller3Config& config,
72 int sample_rate_hz,
peahd0263542017-01-03 04:20:34 -080073 bool use_highpass_filter,
74 std::unique_ptr<BlockProcessor> block_processor);
Gustaf Ullbergc5222982017-10-05 10:25:05 +020075 ~EchoCanceller3() override;
peahe0eae3c2016-12-14 01:16:23 -080076 // Analyzes and stores an internal copy of the split-band domain render
77 // signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020078 void AnalyzeRender(AudioBuffer* farend) override;
peahe0eae3c2016-12-14 01:16:23 -080079 // Analyzes the full-band domain capture signal to detect signal saturation.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020080 void AnalyzeCapture(AudioBuffer* capture) override;
peahe0eae3c2016-12-14 01:16:23 -080081 // Processes the split-band domain capture signal in order to remove any echo
82 // present in the signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020083 void ProcessCapture(AudioBuffer* capture, bool level_change) override;
Gustaf Ullberg332150d2017-11-22 14:17:39 +010084 // Collect current metrics from the echo canceller.
85 Metrics GetMetrics() const override;
Per Åhgrend0fa8202018-04-18 09:35:13 +020086 // Provides an optional external estimate of the audio buffer delay.
87 void SetAudioBufferDelay(size_t delay_ms) override;
peahe0eae3c2016-12-14 01:16:23 -080088
peahd0263542017-01-03 04:20:34 -080089 // Signals whether an external detector has detected echo leakage from the
90 // echo canceller.
91 // Note that in the case echo leakage has been flagged, it should be unflagged
92 // once it is no longer occurring.
peah69221db2017-01-27 03:28:19 -080093 void UpdateEchoLeakageStatus(bool leakage_detected) {
peahd0263542017-01-03 04:20:34 -080094 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peah69221db2017-01-27 03:28:19 -080095 block_processor_->UpdateEchoLeakageStatus(leakage_detected);
peahd0263542017-01-03 04:20:34 -080096 }
97
peahe0eae3c2016-12-14 01:16:23 -080098 private:
peahd0263542017-01-03 04:20:34 -080099 class RenderWriter;
100
peahcf02cf12017-04-05 14:18:07 -0700101 // Empties the render SwapQueue.
102 void EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800103
104 rtc::RaceChecker capture_race_checker_;
105 rtc::RaceChecker render_race_checker_;
106
107 // State that is accessed by the AnalyzeRender call.
danilchap56359be2017-09-07 07:53:45 -0700108 std::unique_ptr<RenderWriter> render_writer_
109 RTC_GUARDED_BY(render_race_checker_);
peahd0263542017-01-03 04:20:34 -0800110
111 // State that may be accessed by the capture thread.
peahe0eae3c2016-12-14 01:16:23 -0800112 static int instance_count_;
peahd0263542017-01-03 04:20:34 -0800113 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren398689f2018-08-23 11:38:27 +0200114 const EchoCanceller3Config config_;
peahd0263542017-01-03 04:20:34 -0800115 const int sample_rate_hz_;
116 const int num_bands_;
117 const size_t frame_length_;
danilchap56359be2017-09-07 07:53:45 -0700118 BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_);
119 FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_);
120 FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800121 SwapQueue<std::vector<std::vector<float>>, Aec3RenderQueueItemVerifier>
122 render_transfer_queue_;
123 std::unique_ptr<BlockProcessor> block_processor_
danilchap56359be2017-09-07 07:53:45 -0700124 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800125 std::vector<std::vector<float>> render_queue_output_frame_
danilchap56359be2017-09-07 07:53:45 -0700126 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800127 std::unique_ptr<CascadedBiQuadFilter> capture_highpass_filter_
danilchap56359be2017-09-07 07:53:45 -0700128 RTC_GUARDED_BY(capture_race_checker_);
129 bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) =
130 false;
131 std::vector<std::vector<float>> block_ RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800132 std::vector<rtc::ArrayView<float>> sub_frame_view_
danilchap56359be2017-09-07 07:53:45 -0700133 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgren398689f2018-08-23 11:38:27 +0200134 BlockDelayBuffer block_delay_buffer_ RTC_GUARDED_BY(capture_race_checker_);
peahe0eae3c2016-12-14 01:16:23 -0800135
136 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCanceller3);
137};
138} // namespace webrtc
139
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200140#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_