blob: 387c6ea42ded33aec95cf7930a047dc31b9d14fc [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
18#include "webrtc/base/array_view.h"
19#include "webrtc/base/constructormagic.h"
20#include "webrtc/base/optional.h"
21#include "webrtc/modules/audio_processing/aec3/aec3_common.h"
22#include "webrtc/modules/audio_processing/aec3/echo_path_variability.h"
peah522d71b2017-02-23 05:16:26 -080023#include "webrtc/modules/audio_processing/aec3/erl_estimator.h"
peahcf02cf12017-04-05 14:18:07 -070024#include "webrtc/modules/audio_processing/aec3/erle_estimator.h"
25#include "webrtc/modules/audio_processing/aec3/render_buffer.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:
34 AecState();
35 ~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.
peah86afe9d2017-04-06 15:45:32 -070044 bool ActiveRender() const { return active_render_blocks_ > 200; }
peahebe77782017-02-27 07:29:21 -080045
peah522d71b2017-02-23 05:16:26 -080046 // Returns the ERLE.
47 const std::array<float, kFftLengthBy2Plus1>& Erle() const {
48 return erle_estimator_.Erle();
49 }
50
51 // Returns the ERL.
52 const std::array<float, kFftLengthBy2Plus1>& Erl() const {
53 return erl_estimator_.Erl();
54 }
55
56 // Returns the delay estimate based on the linear filter.
57 rtc::Optional<size_t> FilterDelay() const { return filter_delay_; }
58
59 // Returns the externally provided delay.
60 rtc::Optional<size_t> ExternalDelay() const { return external_delay_; }
61
peah522d71b2017-02-23 05:16:26 -080062 // Returns whether the capture signal is saturated.
63 bool SaturatedCapture() const { return capture_signal_saturation_; }
64
peah86afe9d2017-04-06 15:45:32 -070065 // Returns whether the echo signal is saturated.
66 bool SaturatedEcho() const { return echo_saturation_; }
67
peah522d71b2017-02-23 05:16:26 -080068 // Updates the capture signal saturation.
69 void UpdateCaptureSaturation(bool capture_signal_saturation) {
70 capture_signal_saturation_ = capture_signal_saturation;
71 }
72
73 // Returns whether a probable headset setup has been detected.
74 bool HeadsetDetected() const { return headset_detected_; }
75
peah86afe9d2017-04-06 15:45:32 -070076 // Takes appropriate action at an echo path change.
77 void HandleEchoPathChange(const EchoPathVariability& echo_path_variability);
78
peah89420452017-04-07 06:13:39 -070079 // Returns the decay factor for the echo reverberation.
80 // TODO(peah): Make this adaptive.
81 float ReverbDecayFactor() const { return 0.f; }
82
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
peah522d71b2017-02-23 05:16:26 -080086 // Updates the aec state.
87 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -070088 adaptive_filter_frequency_response,
peah522d71b2017-02-23 05:16:26 -080089 const rtc::Optional<size_t>& external_delay_samples,
peah86afe9d2017-04-06 15:45:32 -070090 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080091 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -080092 const std::array<float, kFftLengthBy2Plus1>& Y2,
93 rtc::ArrayView<const float> x,
peah522d71b2017-02-23 05:16:26 -080094 bool echo_leakage_detected);
95
96 private:
97 static int instance_count_;
98 std::unique_ptr<ApmDataDumper> data_dumper_;
99 ErlEstimator erl_estimator_;
100 ErleEstimator erle_estimator_;
101 int echo_path_change_counter_;
peahebe77782017-02-27 07:29:21 -0800102 size_t active_render_blocks_ = 0;
peah522d71b2017-02-23 05:16:26 -0800103 bool usable_linear_estimate_ = false;
104 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -0800105 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -0700106 bool echo_saturation_ = false;
peah522d71b2017-02-23 05:16:26 -0800107 bool headset_detected_ = false;
peah86afe9d2017-04-06 15:45:32 -0700108 float previous_max_sample_ = 0.f;
peah6d822ad2017-04-10 13:52:14 -0700109 bool force_zero_gain_ = false;
110 size_t force_zero_gain_counter_ = 0;
peah522d71b2017-02-23 05:16:26 -0800111 rtc::Optional<size_t> filter_delay_;
112 rtc::Optional<size_t> external_delay_;
peah86afe9d2017-04-06 15:45:32 -0700113 size_t blocks_since_last_saturation_ = 1000;
peah522d71b2017-02-23 05:16:26 -0800114
115 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
116};
117
118} // namespace webrtc
119
120#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_