xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/echo_path_delay_estimator.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 MODULES_AUDIO_PROCESSING_AEC3_ECHO_PATH_DELAY_ESTIMATOR_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_ECHO_PATH_DELAY_ESTIMATOR_H_
13 
14 #include <stddef.h>
15 
16 #include "absl/types/optional.h"
17 #include "api/array_view.h"
18 #include "modules/audio_processing/aec3/alignment_mixer.h"
19 #include "modules/audio_processing/aec3/block.h"
20 #include "modules/audio_processing/aec3/clockdrift_detector.h"
21 #include "modules/audio_processing/aec3/decimator.h"
22 #include "modules/audio_processing/aec3/delay_estimate.h"
23 #include "modules/audio_processing/aec3/matched_filter.h"
24 #include "modules/audio_processing/aec3/matched_filter_lag_aggregator.h"
25 
26 namespace webrtc {
27 
28 class ApmDataDumper;
29 struct DownsampledRenderBuffer;
30 struct EchoCanceller3Config;
31 
32 // Estimates the delay of the echo path.
33 class EchoPathDelayEstimator {
34  public:
35   EchoPathDelayEstimator(ApmDataDumper* data_dumper,
36                          const EchoCanceller3Config& config,
37                          size_t num_capture_channels);
38   ~EchoPathDelayEstimator();
39 
40   EchoPathDelayEstimator(const EchoPathDelayEstimator&) = delete;
41   EchoPathDelayEstimator& operator=(const EchoPathDelayEstimator&) = delete;
42 
43   // Resets the estimation. If the delay confidence is reset, the reset behavior
44   // is as if the call is restarted.
45   void Reset(bool reset_delay_confidence);
46 
47   // Produce a delay estimate if such is avaliable.
48   absl::optional<DelayEstimate> EstimateDelay(
49       const DownsampledRenderBuffer& render_buffer,
50       const Block& capture);
51 
52   // Log delay estimator properties.
LogDelayEstimationProperties(int sample_rate_hz,size_t shift)53   void LogDelayEstimationProperties(int sample_rate_hz, size_t shift) const {
54     matched_filter_.LogFilterProperties(sample_rate_hz, shift,
55                                         down_sampling_factor_);
56   }
57 
58   // Returns the level of detected clockdrift.
Clockdrift()59   ClockdriftDetector::Level Clockdrift() const {
60     return clockdrift_detector_.ClockdriftLevel();
61   }
62 
63  private:
64   ApmDataDumper* const data_dumper_;
65   const size_t down_sampling_factor_;
66   const size_t sub_block_size_;
67   AlignmentMixer capture_mixer_;
68   Decimator capture_decimator_;
69   MatchedFilter matched_filter_;
70   MatchedFilterLagAggregator matched_filter_lag_aggregator_;
71   absl::optional<DelayEstimate> old_aggregated_lag_;
72   size_t consistent_estimate_counter_ = 0;
73   ClockdriftDetector clockdrift_detector_;
74 
75   // Internal reset method with more granularity.
76   void Reset(bool reset_lag_aggregator, bool reset_delay_confidence);
77 };
78 }  // namespace webrtc
79 
80 #endif  // MODULES_AUDIO_PROCESSING_AEC3_ECHO_PATH_DELAY_ESTIMATOR_H_
81