blob: 5192a929c66d26903b14388aeb556001eb62d57c [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"
Edward Lemurc20978e2017-07-06 19:44:34 +020023#include "webrtc/rtc_base/array_view.h"
24#include "webrtc/rtc_base/constructormagic.h"
25#include "webrtc/rtc_base/optional.h"
peah522d71b2017-02-23 05:16:26 -080026
27namespace webrtc {
28
29class ApmDataDumper;
30
31// Handles the state and the conditions for the echo removal functionality.
32class AecState {
33 public:
peah29103572017-07-11 02:54:02 -070034 explicit AecState(float reverb_decay);
peah522d71b2017-02-23 05:16:26 -080035 ~AecState();
36
37 // Returns whether the linear filter estimate is usable.
38 bool UsableLinearEstimate() const { return usable_linear_estimate_; }
39
40 // Returns whether there has been echo leakage detected.
41 bool EchoLeakageDetected() const { return echo_leakage_detected_; }
42
peah522d71b2017-02-23 05:16:26 -080043 // Returns whether the render signal is currently active.
peahe52a2032017-04-19 09:03:40 -070044 // TODO(peah): Deprecate this in an upcoming CL.
45 bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; }
peahebe77782017-02-27 07:29:21 -080046
peah522d71b2017-02-23 05:16:26 -080047 // Returns the ERLE.
48 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
49 return erle_estimator_.Erle();
50 }
51
52 // Returns the ERL.
53 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
54 return erl_estimator_.Erl();
55 }
56
57 // Returns the delay estimate based on the linear filter.
58 rtc::Optional<size_t> FilterDelay() const { return filter_delay_; }
59
60 // Returns the externally provided delay.
61 rtc::Optional<size_t> ExternalDelay() const { return external_delay_; }
62
peah522d71b2017-02-23 05:16:26 -080063 // Returns whether the capture signal is saturated.
64 bool SaturatedCapture() const { return capture_signal_saturation_; }
65
peah86afe9d2017-04-06 15:45:32 -070066 // Returns whether the echo signal is saturated.
67 bool SaturatedEcho() const { return echo_saturation_; }
68
peah522d71b2017-02-23 05:16:26 -080069 // Updates the capture signal saturation.
70 void UpdateCaptureSaturation(bool capture_signal_saturation) {
71 capture_signal_saturation_ = capture_signal_saturation;
72 }
73
74 // Returns whether a probable headset setup has been detected.
75 bool HeadsetDetected() const { return headset_detected_; }
76
peah86afe9d2017-04-06 15:45:32 -070077 // Takes appropriate action at an echo path change.
78 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
79
peah89420452017-04-07 06:13:39 -070080 // Returns the decay factor for the echo reverberation.
peah29103572017-07-11 02:54:02 -070081 float ReverbDecay() const { return reverb_decay_; }
peah89420452017-04-07 06:13:39 -070082
peah6d822ad2017-04-10 13:52:14 -070083 // Returns whether the echo suppression gain should be forced to zero.
84 bool ForcedZeroGain() const { return force_zero_gain_; }
85
peah29103572017-07-11 02:54:02 -070086 // Returns whether the echo in the capture signal is audible.
87 bool InaudibleEcho() const { return echo_audibility_.InaudibleEcho(); }
88
89 // Updates the aec state with the AEC output signal.
90 void UpdateWithOutput(rtc::ArrayView<const float> e) {
91 echo_audibility_.UpdateWithOutput(e);
92 }
93
peah522d71b2017-02-23 05:16:26 -080094 // Updates the aec state.
95 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -070096 adaptive_filter_frequency_response,
peah29103572017-07-11 02:54:02 -070097 const std::array<float, kAdaptiveFilterTimeDomainLength>&
98 adaptive_filter_impulse_response,
peah522d71b2017-02-23 05:16:26 -080099 const rtc::Optional<size_t>& external_delay_samples,
peah86afe9d2017-04-06 15:45:32 -0700100 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -0800101 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -0800102 const std::array<float, kFftLengthBy2Plus1>& Y2,
103 rtc::ArrayView<const float> x,
peah29103572017-07-11 02:54:02 -0700104 const std::array<float, kBlockSize>& s_main,
peah522d71b2017-02-23 05:16:26 -0800105 bool echo_leakage_detected);
106
107 private:
peah29103572017-07-11 02:54:02 -0700108 class EchoAudibility {
109 public:
110 void Update(rtc::ArrayView<const float> x,
111 const std::array<float, kBlockSize>& s);
112 void UpdateWithOutput(rtc::ArrayView<const float> e);
113 bool InaudibleEcho() const { return inaudible_echo_; }
114
115 private:
116 float max_nearend_ = 0.f;
117 size_t max_nearend_counter_ = 0;
118 size_t low_farend_counter_ = 0;
119 bool inaudible_echo_ = false;
120 };
121
122 void UpdateReverb(const std::array<float, kAdaptiveFilterTimeDomainLength>&
123 impulse_response);
124
peah522d71b2017-02-23 05:16:26 -0800125 static int instance_count_;
126 std::unique_ptr<ApmDataDumper> data_dumper_;
127 ErlEstimator erl_estimator_;
128 ErleEstimator erle_estimator_;
129 int echo_path_change_counter_;
peahe52a2032017-04-19 09:03:40 -0700130 size_t blocks_with_filter_adaptation_ = 0;
peah522d71b2017-02-23 05:16:26 -0800131 bool usable_linear_estimate_ = false;
132 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800133 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700134 bool echo_saturation_ = false;
peah522d71b2017-02-23 05:16:26 -0800135 bool headset_detected_ = false;
peah86afe9d2017-04-06 15:45:32 -0700136 float previous_max_sample_ = 0.f;
peah6d822ad2017-04-10 13:52:14 -0700137 bool force_zero_gain_ = false;
peahe52a2032017-04-19 09:03:40 -0700138 bool render_received_ = false;
peah6d822ad2017-04-10 13:52:14 -0700139 size_t force_zero_gain_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -0800140 rtc::Optional<size_t> filter_delay_;
141 rtc::Optional<size_t> external_delay_;
peah86afe9d2017-04-06 15:45:32 -0700142 size_t blocks_since_last_saturation_ = 1000;
peah29103572017-07-11 02:54:02 -0700143 float reverb_decay_;
144 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_;
148
peah697a5902017-06-30 07:06:10 -0700149 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AecState);
peah522d71b2017-02-23 05:16:26 -0800150};
151
152} // namespace webrtc
153
154#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_