blob: 44a1767125544074ddf9735cfac67dd99c681b70 [file] [log] [blame]
peahc18cc3a2017-02-23 13:16:261/*
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
peahc18cc3a2017-02-23 13:16:2618#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
19#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
peahc18cc3a2017-02-23 13:16:2620#include "webrtc/modules/audio_processing/aec3/erl_estimator.h"
peah22c5fa52017-04-05 21:18:0721#include "webrtc/modules/audio_processing/aec3/erle_estimator.h"
22#include "webrtc/modules/audio_processing/aec3/render_buffer.h"
peah47c96e52017-08-25 05:36:5323#include "webrtc/modules/audio_processing/include/audio_processing.h"
Edward Lemur76de83e2017-07-06 17:44:3424#include "webrtc/rtc_base/array_view.h"
25#include "webrtc/rtc_base/constructormagic.h"
26#include "webrtc/rtc_base/optional.h"
peahc18cc3a2017-02-23 13:16:2627
28namespace webrtc {
29
30class ApmDataDumper;
31
32// Handles the state and the conditions for the echo removal functionality.
33class AecState {
34 public:
peah47c96e52017-08-25 05:36:5335 explicit AecState(const AudioProcessing::Config::EchoCanceller3& config);
peahc18cc3a2017-02-23 13:16:2636 ~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
peahc18cc3a2017-02-23 13:16:2644 // Returns whether the render signal is currently active.
peah2a9d40c2017-04-19 16:03:4045 // TODO(peah): Deprecate this in an upcoming CL.
46 bool ActiveRender() const { return blocks_with_filter_adaptation_ > 200; }
peah50fb9932017-02-27 15:29:2147
peahc18cc3a2017-02-23 13:16:2648 // 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
peahc18cc3a2017-02-23 13:16:2664 // Returns whether the capture signal is saturated.
65 bool SaturatedCapture() const { return capture_signal_saturation_; }
66
peahd149fca2017-04-06 22:45:3267 // Returns whether the echo signal is saturated.
68 bool SaturatedEcho() const { return echo_saturation_; }
69
peahc18cc3a2017-02-23 13:16:2670 // 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
peahd149fca2017-04-06 22:45:3278 // Takes appropriate action at an echo path change.
79 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
80
peah05fb3192017-04-07 13:13:3981 // Returns the decay factor for the echo reverberation.
peah6552e112017-07-11 09:54:0282 float ReverbDecay() const { return reverb_decay_; }
peah05fb3192017-04-07 13:13:3983
peah4f6d5702017-04-10 20:52:1484 // Returns whether the echo suppression gain should be forced to zero.
85 bool ForcedZeroGain() const { return force_zero_gain_; }
86
peah6552e112017-07-11 09:54:0287 // 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
peahc18cc3a2017-02-23 13:16:2695 // Updates the aec state.
96 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peahd149fca2017-04-06 22:45:3297 adaptive_filter_frequency_response,
peah6552e112017-07-11 09:54:0298 const std::array<float, kAdaptiveFilterTimeDomainLength>&
99 adaptive_filter_impulse_response,
peahc18cc3a2017-02-23 13:16:26100 const rtc::Optional<size_t>& external_delay_samples,
peahd149fca2017-04-06 22:45:32101 const RenderBuffer& render_buffer,
peahc18cc3a2017-02-23 13:16:26102 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peahc18cc3a2017-02-23 13:16:26103 const std::array<float, kFftLengthBy2Plus1>& Y2,
104 rtc::ArrayView<const float> x,
peah6552e112017-07-11 09:54:02105 const std::array<float, kBlockSize>& s_main,
peahc18cc3a2017-02-23 13:16:26106 bool echo_leakage_detected);
107
108 private:
peah6552e112017-07-11 09:54:02109 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
peahc18cc3a2017-02-23 13:16:26126 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_;
peah2a9d40c2017-04-19 16:03:40131 size_t blocks_with_filter_adaptation_ = 0;
peahc18cc3a2017-02-23 13:16:26132 bool usable_linear_estimate_ = false;
133 bool echo_leakage_detected_ = false;
peahc18cc3a2017-02-23 13:16:26134 bool capture_signal_saturation_ = false;
peahd149fca2017-04-06 22:45:32135 bool echo_saturation_ = false;
peahc18cc3a2017-02-23 13:16:26136 bool headset_detected_ = false;
peahd149fca2017-04-06 22:45:32137 float previous_max_sample_ = 0.f;
peah4f6d5702017-04-10 20:52:14138 bool force_zero_gain_ = false;
peah2a9d40c2017-04-19 16:03:40139 bool render_received_ = false;
peah4f6d5702017-04-10 20:52:14140 size_t force_zero_gain_counter_ = 0;
peahc18cc3a2017-02-23 13:16:26141 rtc::Optional<size_t> filter_delay_;
142 rtc::Optional<size_t> external_delay_;
peahd149fca2017-04-06 22:45:32143 size_t blocks_since_last_saturation_ = 1000;
peah6552e112017-07-11 09:54:02144 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_;
peah47c96e52017-08-25 05:36:53148 const AudioProcessing::Config::EchoCanceller3 config_;
149 float reverb_decay_;
peah6552e112017-07-11 09:54:02150
peah47c96e52017-08-25 05:36:53151 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
peahc18cc3a2017-02-23 13:16:26152};
153
154} // namespace webrtc
155
156#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_