xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/delay_manager.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2012 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_DELAY_MANAGER_H_
12 #define MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
13 
14 #include <string.h>  // Provide access to size_t.
15 
16 #include <deque>
17 #include <memory>
18 
19 #include "absl/types/optional.h"
20 #include "api/neteq/tick_timer.h"
21 #include "modules/audio_coding/neteq/histogram.h"
22 #include "modules/audio_coding/neteq/reorder_optimizer.h"
23 #include "modules/audio_coding/neteq/underrun_optimizer.h"
24 
25 namespace webrtc {
26 
27 class DelayManager {
28  public:
29   struct Config {
30     Config();
31     void Log();
32 
33     // Options that can be configured via field trial.
34     double quantile = 0.95;
35     double forget_factor = 0.983;
36     absl::optional<double> start_forget_weight = 2;
37     absl::optional<int> resample_interval_ms = 500;
38 
39     bool use_reorder_optimizer = true;
40     double reorder_forget_factor = 0.9993;
41     int ms_per_loss_percent = 20;
42 
43     // Options that are externally populated.
44     int max_packets_in_buffer = 200;
45     int base_minimum_delay_ms = 0;
46   };
47 
48   DelayManager(const Config& config, const TickTimer* tick_timer);
49 
50   virtual ~DelayManager();
51 
52   DelayManager(const DelayManager&) = delete;
53   DelayManager& operator=(const DelayManager&) = delete;
54 
55   // Updates the delay manager that a new packet arrived with delay
56   // `arrival_delay_ms`. This updates the statistics and a new target buffer
57   // level is calculated. The `reordered` flag indicates if the packet was
58   // reordered.
59   virtual void Update(int arrival_delay_ms, bool reordered);
60 
61   // Resets all state.
62   virtual void Reset();
63 
64   // Gets the target buffer level in milliseconds. If a minimum or maximum delay
65   // has been set, the target delay reported here also respects the configured
66   // min/max delay.
67   virtual int TargetDelayMs() const;
68 
69   // Reports the target delay that would be used if no minimum/maximum delay
70   // would be set.
71   virtual int UnlimitedTargetLevelMs() const;
72 
73   // Notifies the DelayManager of how much audio data is carried in each packet.
74   virtual int SetPacketAudioLength(int length_ms);
75 
76   // Accessors and mutators.
77   // Assuming `delay` is in valid range.
78   virtual bool SetMinimumDelay(int delay_ms);
79   virtual bool SetMaximumDelay(int delay_ms);
80   virtual bool SetBaseMinimumDelay(int delay_ms);
81   virtual int GetBaseMinimumDelay() const;
82 
83   // These accessors are only intended for testing purposes.
effective_minimum_delay_ms_for_test()84   int effective_minimum_delay_ms_for_test() const {
85     return effective_minimum_delay_ms_;
86   }
87 
88  private:
89   // Provides value which minimum delay can't exceed based on current buffer
90   // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
91   int MinimumDelayUpperBound() const;
92 
93   // Updates `effective_minimum_delay_ms_` delay based on current
94   // `minimum_delay_ms_`, `base_minimum_delay_ms_` and `maximum_delay_ms_`
95   // and buffer size.
96   void UpdateEffectiveMinimumDelay();
97 
98   // Makes sure that `delay_ms` is less than maximum delay, if any maximum
99   // is set. Also, if possible check `delay_ms` to be less than 75% of
100   // `max_packets_in_buffer_`.
101   bool IsValidMinimumDelay(int delay_ms) const;
102 
103   bool IsValidBaseMinimumDelay(int delay_ms) const;
104 
105   // TODO(jakobi): set maximum buffer delay instead of number of packets.
106   const int max_packets_in_buffer_;
107   UnderrunOptimizer underrun_optimizer_;
108   std::unique_ptr<ReorderOptimizer> reorder_optimizer_;
109 
110   int base_minimum_delay_ms_;
111   int effective_minimum_delay_ms_;  // Used as lower bound for target delay.
112   int minimum_delay_ms_;            // Externally set minimum delay.
113   int maximum_delay_ms_;            // Externally set maximum allowed delay.
114 
115   int packet_len_ms_ = 0;
116   int target_level_ms_ = 0;  // Currently preferred buffer level.
117   int unlimited_target_level_ms_ = 0;
118 };
119 
120 }  // namespace webrtc
121 #endif  // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
122