blob: 7905be051323dd14d880eec645fc2392a20e16a5 [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
peah522d71b2017-02-23 05:16:26 -080079 // Updates the aec state.
80 void Update(const std::vector<std::array<float, kFftLengthBy2Plus1>>&
peah86afe9d2017-04-06 15:45:32 -070081 adaptive_filter_frequency_response,
peah522d71b2017-02-23 05:16:26 -080082 const rtc::Optional<size_t>& external_delay_samples,
peah86afe9d2017-04-06 15:45:32 -070083 const RenderBuffer& render_buffer,
peah522d71b2017-02-23 05:16:26 -080084 const std::array<float, kFftLengthBy2Plus1>& E2_main,
peah522d71b2017-02-23 05:16:26 -080085 const std::array<float, kFftLengthBy2Plus1>& Y2,
86 rtc::ArrayView<const float> x,
peah522d71b2017-02-23 05:16:26 -080087 bool echo_leakage_detected);
88
89 private:
90 static int instance_count_;
91 std::unique_ptr<ApmDataDumper> data_dumper_;
92 ErlEstimator erl_estimator_;
93 ErleEstimator erle_estimator_;
94 int echo_path_change_counter_;
peahebe77782017-02-27 07:29:21 -080095 size_t active_render_blocks_ = 0;
peah522d71b2017-02-23 05:16:26 -080096 bool usable_linear_estimate_ = false;
97 bool echo_leakage_detected_ = false;
peah522d71b2017-02-23 05:16:26 -080098 bool capture_signal_saturation_ = false;
peah86afe9d2017-04-06 15:45:32 -070099 bool echo_saturation_ = false;
peah522d71b2017-02-23 05:16:26 -0800100 bool headset_detected_ = false;
peah86afe9d2017-04-06 15:45:32 -0700101 float previous_max_sample_ = 0.f;
peah522d71b2017-02-23 05:16:26 -0800102 rtc::Optional<size_t> filter_delay_;
103 rtc::Optional<size_t> external_delay_;
peah86afe9d2017-04-06 15:45:32 -0700104 size_t blocks_since_last_saturation_ = 1000;
peah522d71b2017-02-23 05:16:26 -0800105
106 RTC_DISALLOW_COPY_AND_ASSIGN(AecState);
107};
108
109} // namespace webrtc
110
111#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_AEC_STATE_H_