blob: 44a1767125544074ddf9735cfac67dd99c681b70 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
2 * Copyright (c) 2017 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
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_
13
14#include <algorithm>
15#include <memory>
16#include <vector>
17
peah522d71b2017-02-23 05:16:26 -080018#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
19#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
peah522d71b2017-02-23 05:16:26 -080020#include "webrtc/modules/audio_processing/aec3/erl_estimator.h"
peahcf02cf12017-04-05 14:18:07 -070021#include "webrtc/modules/audio_processing/aec3/erle_estimator.h"
22#include "webrtc/modules/audio_processing/aec3/render_buffer.h"
peah8cee56f2017-08-24 22:36:53 -070023#include "webrtc/modules/audio_processing/include/audio_processing.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020024#include "webrtc/rtc_base/array_view.h"
25#include "webrtc/rtc_base/constructormagic.h"
26#include "webrtc/rtc_base/optional.h"
peah522d71b2017-02-23 05:16:26 -080027
28namespace webrtc {
29
30class ApmDataDumper;
31
32// Handles the state and the conditions for the echo removal functionality.
33class AecState {
34 public:
peah8cee56f2017-08-24 22:36:53 -070035 explicit AecState(const AudioProcessing::Config::EchoCanceller3& config);
peah522d71b2017-02-23 05:16:26 -080036 ~AecState();
37
38 // Returns whether the linear filter estimate is usable.
39 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
40
41 // Returns whether there has been echo leakage detected.
42 bool EchoLeakageDetected() const { return echo_leakage_detected_; }
43
peah522d71b2017-02-23 05:16:26 -080044 // Returns whether the render signal is currently active.
peahe52a2032017-04-19 09:03:40 -070045 // TODO(peah): Deprecate this in an upcoming CL.
46 bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; }
peahebe77782017-02-27 07:29:21 -080047
peah522d71b2017-02-23 05:16:26 -080048 // Returns the ERLE.
49 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
50 return erle_estimator_.Erle();
51 }
52
53 // Returns the ERL.
54 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
55 return erl_estimator_.Erl();
56 }
57
58 // Returns the delay estimate based on the linear filter.
59 rtc::Optional<size_t> FilterDelay() const { return filter_delay_; }
60
61 // Returns the externally provided delay.
62 rtc::Optional<size_t> ExternalDelay() const { return external_delay_; }
63
peah522d71b2017-02-23 05:16:26 -080064 // Returns whether the capture signal is saturated.
65 bool SaturatedCapture() const { return capture_signal_saturation_; }
66
peah86afe9d2017-04-06 15:45:32 -070067 // Returns whether the echo signal is saturated.
68 bool SaturatedEcho() const { return echo_saturation_; }
69
peah522d71b2017-02-23 05:16:26 -080070 // Updates the capture signal saturation.
71 void UpdateCaptureSaturation(bool capture_signal_saturation) {
72 capture_signal_saturation_ = capture_signal_saturation;
73 }
74
75 // Returns whether a probable headset setup has been detected.
76 bool HeadsetDetected() const { return headset_detected_; }
77
peah86afe9d2017-04-06 15:45:32 -070078 // Takes appropriate action at an echo path change.
79 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
80
peah89420452017-04-07 06:13:39 -070081 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -070082 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -070083
peah6d822ad2017-04-10 13:52:14 -070084 // Returns whether the echo suppression gain should be forced to zero.
85 bool ForcedZeroGain() const { return force_zero_gain_; }
86
peah29103572017-07-11 02:54:02 -070087 // Returns whether the echo in the capture signal is audible.
88 bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); }
89
90 // Updates the aec state with the AEC output signal.
91 void UpdateWithOutput(rtc::ArrayView<const float> e) {
92 echo_audibility_.UpdateWithOutput(e);
93 }
94
peah522d71b2017-02-23 05:16:26 -080095 // Updates the aec state.
96 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -070097 adaptive_filter_frequency_response,
peah29103572017-07-11 02:54:02 -070098 const std::array<float, kAdaptiveFilterTimeDomainLength>&
99 adaptive_filter_impulse_response,
peah522d71b2017-02-23 05:16:26 -0800100 const rtc::Optional<size_t>& external_delay_samples,
peah86afe9d2017-04-06 15:45:32 -0700101 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800102 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800103 const std::array<float, kFftLengthBy2Plus1>& Y2,
104 rtc::ArrayView<const float> x,
peah29103572017-07-11 02:54:02 -0700105 const std::array<float, kBlockSize>& s_main,
peah522d71b2017-02-23 05:16:26 -0800106 bool echo_leakage_detected);
107
108 private:
peah29103572017-07-11 02:54:02 -0700109 class EchoAudibility {
110 public:
111 void Update(rtc::ArrayView<const float> x,
112 const std::array<float, kBlockSize>& s);
113 void UpdateWithOutput(rtc::ArrayView<const float> e);
114 bool InaudibleEcho() const { return inaudible_echo_; }
115
116 private:
117 float max_nearend_ = 0.f;
118 size_t max_nearend_counter_ = 0;
119 size_t low_farend_counter_ = 0;
120 bool inaudible_echo_ = false;
121 };
122
123 void UpdateReverb(const std::array<float, kAdaptiveFilterTimeDomainLength>&
124 impulse_response);
125
peah522d71b2017-02-23 05:16:26 -0800126 static int instance_count_;
127 std::unique_ptr<ApmDataDumper> data_dumper_;
128 ErlEstimator erl_estimator_;
129 ErleEstimator erle_estimator_;
130 int echo_path_change_counter_;
peahe52a2032017-04-19 09:03:40 -0700131 size_t blocks_with_filter_adaptation_ = 0;
peah522d71b2017-02-23 05:16:26 -0800132 bool usable_linear_estimate_ = false;
133 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800134 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700135 bool echo_saturation_ = false;
peah522d71b2017-02-23 05:16:26 -0800136 bool headset_detected_ = false;
peah86afe9d2017-04-06 15:45:32 -0700137 float previous_max_sample_ = 0.f;
peah6d822ad2017-04-10 13:52:14 -0700138 bool force_zero_gain_ = false;
peahe52a2032017-04-19 09:03:40 -0700139 bool render_received_ = false;
peah6d822ad2017-04-10 13:52:14 -0700140 size_t force_zero_gain_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -0800141 rtc::Optional<size_t> filter_delay_;
142 rtc::Optional<size_t> external_delay_;
peah86afe9d2017-04-06 15:45:32 -0700143 size_t blocks_since_last_saturation_ = 1000;
peah29103572017-07-11 02:54:02 -0700144 float reverb_decay_to_test_ = 0.9f;
145 float reverb_decay_candidate_ = 0.f;
146 float reverb_decay_candidate_residual_ = -1.f;
147 EchoAudibility echo_audibility_;
peah8cee56f2017-08-24 22:36:53 -0700148 const AudioProcessing::Config::EchoCanceller3 config_;
149 float reverb_decay_;
peah29103572017-07-11 02:54:02 -0700150
peah8cee56f2017-08-24 22:36:53 -0700151 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peah522d71b2017-02-23 05:16:26 -0800152};
153
154} // namespace webrtc
155
156#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_