blob: cb3b3826287828a10526e7ca72adc200f6d83d73 [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>
15#include <memory>
16#include <vector>
17
18#include "api/array_view.h"
Gustaf Ullberg3646f972018-02-14 15:19:04 +010019#include "api/audio/echo_canceller3_config.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "api/audio/echo_control.h"
Per Åhgren14f252a2018-11-27 18:02:56 +010021#include "modules/audio_processing/aec3/api_call_jitter_metrics.h"
Per Åhgren398689f2018-08-23 11:38:27 +020022#include "modules/audio_processing/aec3/block_delay_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/aec3/block_framer.h"
24#include "modules/audio_processing/aec3/block_processor.h"
25#include "modules/audio_processing/aec3/cascaded_biquad_filter.h"
26#include "modules/audio_processing/aec3/frame_blocker.h"
Gustaf Ullberg74ba9902019-01-17 09:19:35 +010027#include "modules/audio_processing/aec3/message_queue.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_processing/audio_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "modules/audio_processing/logging/apm_data_dumper.h"
Yves Gerey988cc082018-10-23 12:03:01 +020030#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/constructor_magic.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/race_checker.h"
Yves Gerey988cc082018-10-23 12:03:01 +020033#include "rtc_base/thread_annotations.h"
peahe0eae3c2016-12-14 01:16:23 -080034
35namespace webrtc {
36
peahd0263542017-01-03 04:20:34 -080037// Functor for verifying the invariance of the frames being put into the render
38// queue.
39class Aec3RenderQueueItemVerifier {
40 public:
41 explicit Aec3RenderQueueItemVerifier(size_t num_bands, size_t frame_length)
42 : num_bands_(num_bands), frame_length_(frame_length) {}
43
44 bool operator()(const std::vector<std::vector<float>>& v) const {
45 if (v.size() != num_bands_) {
46 return false;
47 }
48 for (const auto& v_k : v) {
49 if (v_k.size() != frame_length_) {
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 Ullbergc5222982017-10-05 10:25:05 +020073class EchoCanceller3 : public EchoControl {
peahe0eae3c2016-12-14 01:16:23 -080074 public:
peahd0263542017-01-03 04:20:34 -080075 // Normal c-tor to use.
Gustaf Ullbergbd83b912017-10-18 12:32:42 +020076 EchoCanceller3(const EchoCanceller3Config& config,
peah697a5902017-06-30 07:06:10 -070077 int sample_rate_hz,
78 bool use_highpass_filter);
peahd0263542017-01-03 04:20:34 -080079 // Testing c-tor that is used only for testing purposes.
Per Åhgren8ba58612017-12-01 23:01:44 +010080 EchoCanceller3(const EchoCanceller3Config& config,
81 int sample_rate_hz,
peahd0263542017-01-03 04:20:34 -080082 bool use_highpass_filter,
83 std::unique_ptr<BlockProcessor> block_processor);
Gustaf Ullbergc5222982017-10-05 10:25:05 +020084 ~EchoCanceller3() override;
peahe0eae3c2016-12-14 01:16:23 -080085 // Analyzes and stores an internal copy of the split-band domain render
86 // signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020087 void AnalyzeRender(AudioBuffer* farend) override;
peahe0eae3c2016-12-14 01:16:23 -080088 // Analyzes the full-band domain capture signal to detect signal saturation.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020089 void AnalyzeCapture(AudioBuffer* capture) override;
peahe0eae3c2016-12-14 01:16:23 -080090 // Processes the split-band domain capture signal in order to remove any echo
91 // present in the signal.
Gustaf Ullbergc5222982017-10-05 10:25:05 +020092 void ProcessCapture(AudioBuffer* capture, bool level_change) override;
Gustaf Ullberg332150d2017-11-22 14:17:39 +010093 // Collect current metrics from the echo canceller.
94 Metrics GetMetrics() const override;
Per Åhgrend0fa8202018-04-18 09:35:13 +020095 // Provides an optional external estimate of the audio buffer delay.
96 void SetAudioBufferDelay(size_t delay_ms) override;
peahe0eae3c2016-12-14 01:16:23 -080097
peahd0263542017-01-03 04:20:34 -080098 // Signals whether an external detector has detected echo leakage from the
99 // echo canceller.
100 // Note that in the case echo leakage has been flagged, it should be unflagged
101 // once it is no longer occurring.
peah69221db2017-01-27 03:28:19 -0800102 void UpdateEchoLeakageStatus(bool leakage_detected) {
peahd0263542017-01-03 04:20:34 -0800103 RTC_DCHECK_RUNS_SERIALIZED(&capture_race_checker_);
peah69221db2017-01-27 03:28:19 -0800104 block_processor_->UpdateEchoLeakageStatus(leakage_detected);
peahd0263542017-01-03 04:20:34 -0800105 }
106
peahe0eae3c2016-12-14 01:16:23 -0800107 private:
peahd0263542017-01-03 04:20:34 -0800108 class RenderWriter;
109
Gustaf Ullberg74ba9902019-01-17 09:19:35 +0100110 // Empties the render MessageQueue.
peahcf02cf12017-04-05 14:18:07 -0700111 void EmptyRenderQueue();
peahd0263542017-01-03 04:20:34 -0800112
113 rtc::RaceChecker capture_race_checker_;
114 rtc::RaceChecker render_race_checker_;
115
116 // State that is accessed by the AnalyzeRender call.
danilchap56359be2017-09-07 07:53:45 -0700117 std::unique_ptr<RenderWriter> render_writer_
118 RTC_GUARDED_BY(render_race_checker_);
peahd0263542017-01-03 04:20:34 -0800119
120 // State that may be accessed by the capture thread.
peahe0eae3c2016-12-14 01:16:23 -0800121 static int instance_count_;
peahd0263542017-01-03 04:20:34 -0800122 std::unique_ptr<ApmDataDumper> data_dumper_;
Per Åhgren398689f2018-08-23 11:38:27 +0200123 const EchoCanceller3Config config_;
peahd0263542017-01-03 04:20:34 -0800124 const int sample_rate_hz_;
125 const int num_bands_;
126 const size_t frame_length_;
danilchap56359be2017-09-07 07:53:45 -0700127 BlockFramer output_framer_ RTC_GUARDED_BY(capture_race_checker_);
128 FrameBlocker capture_blocker_ RTC_GUARDED_BY(capture_race_checker_);
129 FrameBlocker render_blocker_ RTC_GUARDED_BY(capture_race_checker_);
Gustaf Ullberg74ba9902019-01-17 09:19:35 +0100130 MessageQueue<std::vector<std::vector<float>>> render_transfer_queue_;
peahd0263542017-01-03 04:20:34 -0800131 std::unique_ptr<BlockProcessor> block_processor_
danilchap56359be2017-09-07 07:53:45 -0700132 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800133 std::vector<std::vector<float>> render_queue_output_frame_
danilchap56359be2017-09-07 07:53:45 -0700134 RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800135 std::unique_ptr<CascadedBiQuadFilter> capture_highpass_filter_
danilchap56359be2017-09-07 07:53:45 -0700136 RTC_GUARDED_BY(capture_race_checker_);
137 bool saturated_microphone_signal_ RTC_GUARDED_BY(capture_race_checker_) =
138 false;
139 std::vector<std::vector<float>> block_ RTC_GUARDED_BY(capture_race_checker_);
peahd0263542017-01-03 04:20:34 -0800140 std::vector<rtc::ArrayView<float>> sub_frame_view_
danilchap56359be2017-09-07 07:53:45 -0700141 RTC_GUARDED_BY(capture_race_checker_);
Per Åhgren398689f2018-08-23 11:38:27 +0200142 BlockDelayBuffer block_delay_buffer_ RTC_GUARDED_BY(capture_race_checker_);
Per Åhgren14f252a2018-11-27 18:02:56 +0100143 ApiCallJitterMetrics api_call_metrics_ RTC_GUARDED_BY(capture_race_checker_);
peahe0eae3c2016-12-14 01:16:23 -0800144
145 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EchoCanceller3);
146};
147} // namespace webrtc
148
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200149#endif // MODULES_AUDIO_PROCESSING_AEC3_ECHO_CANCELLER3_H_