xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/underrun_optimizer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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_CODING_NETEQ_UNDERRUN_OPTIMIZER_H_
12 #define MODULES_AUDIO_CODING_NETEQ_UNDERRUN_OPTIMIZER_H_
13 
14 #include <memory>
15 
16 #include "absl/types/optional.h"
17 #include "api/neteq/tick_timer.h"
18 #include "modules/audio_coding/neteq/histogram.h"
19 
20 namespace webrtc {
21 
22 // Estimates probability of buffer underrun due to late packet arrival.
23 // The optimal delay is decided such that the probability of underrun is lower
24 // than 1 - `histogram_quantile`.
25 class UnderrunOptimizer {
26  public:
27   UnderrunOptimizer(const TickTimer* tick_timer,
28                     int histogram_quantile,
29                     int forget_factor,
30                     absl::optional<int> start_forget_weight,
31                     absl::optional<int> resample_interval_ms);
32 
33   void Update(int relative_delay_ms);
34 
GetOptimalDelayMs()35   absl::optional<int> GetOptimalDelayMs() const { return optimal_delay_ms_; }
36 
37   void Reset();
38 
39  private:
40   const TickTimer* tick_timer_;
41   Histogram histogram_;
42   const int histogram_quantile_;  // In Q30.
43   const absl::optional<int> resample_interval_ms_;
44   std::unique_ptr<TickTimer::Stopwatch> resample_stopwatch_;
45   int max_delay_in_interval_ms_ = 0;
46   absl::optional<int> optimal_delay_ms_;
47 };
48 
49 }  // namespace webrtc
50 #endif  // MODULES_AUDIO_CODING_NETEQ_UNDERRUN_OPTIMIZER_H_
51