blob: 1c4fe423b0509205885762fa4893a62b53d758fa [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
12#define MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <string.h> // Provide access to size_t.
15
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010016#include <deque>
henrik.lundin8f8c96d2016-04-28 23:19:20 -070017#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
Minyue Li002fbb82018-10-04 11:31:03 +020019#include "absl/types/optional.h"
Ivo Creusen3ce44a32019-10-31 14:38:11 +010020#include "api/neteq/tick_timer.h"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010021#include "modules/audio_coding/neteq/histogram.h"
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020022#include "modules/audio_coding/neteq/reorder_optimizer.h"
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020023#include "modules/audio_coding/neteq/underrun_optimizer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25namespace webrtc {
26
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027class DelayManager {
28 public:
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020029 struct Config {
30 Config();
31 void Log();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020033 // Options that can be configured via field trial.
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010034 double quantile = 0.95;
35 double forget_factor = 0.983;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020036 absl::optional<double> start_forget_weight = 2;
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010037 absl::optional<int> resample_interval_ms = 500;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020038
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020039 bool use_reorder_optimizer = true;
40 double reorder_forget_factor = 0.9993;
41 int ms_per_loss_percent = 20;
42
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020043 // Options that are externally populated.
44 int max_packets_in_buffer = 200;
45 int base_minimum_delay_ms = 0;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020046 };
47
48 DelayManager(const Config& config, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000050 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090052 DelayManager(const DelayManager&) = delete;
53 DelayManager& operator=(const DelayManager&) = delete;
54
Jakob Ivarsson01ab7d52022-05-25 21:06:14 +020055 // 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);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020061 // Resets all state.
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000062 virtual void Reset();
63
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020064 // Gets the target buffer level in milliseconds.
65 virtual int TargetDelayMs() const;
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000066
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020067 // Notifies the DelayManager of how much audio data is carried in each packet.
68 virtual int SetPacketAudioLength(int length_ms);
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000069
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 // Accessors and mutators.
Artem Titovd00ce742021-07-28 20:00:17 +020071 // Assuming `delay` is in valid range.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000072 virtual bool SetMinimumDelay(int delay_ms);
73 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010074 virtual bool SetBaseMinimumDelay(int delay_ms);
75 virtual int GetBaseMinimumDelay() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020077 // These accessors are only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010078 int effective_minimum_delay_ms_for_test() const {
79 return effective_minimum_delay_ms_;
80 }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010081
Minyue Li002fbb82018-10-04 11:31:03 +020082 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010083 // Provides value which minimum delay can't exceed based on current buffer
Artem Titovd00ce742021-07-28 20:00:17 +020084 // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010085 int MinimumDelayUpperBound() const;
86
Artem Titovd00ce742021-07-28 20:00:17 +020087 // Updates `effective_minimum_delay_ms_` delay based on current
88 // `minimum_delay_ms_`, `base_minimum_delay_ms_` and `maximum_delay_ms_`
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010089 // and buffer size.
90 void UpdateEffectiveMinimumDelay();
91
Artem Titovd00ce742021-07-28 20:00:17 +020092 // Makes sure that `delay_ms` is less than maximum delay, if any maximum
93 // is set. Also, if possible check `delay_ms` to be less than 75% of
94 // `max_packets_in_buffer_`.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010095 bool IsValidMinimumDelay(int delay_ms) const;
96
97 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +010098
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020099 // TODO(jakobi): set maximum buffer delay instead of number of packets.
100 const int max_packets_in_buffer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200101 UnderrunOptimizer underrun_optimizer_;
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +0200102 std::unique_ptr<ReorderOptimizer> reorder_optimizer_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100103
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100104 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200105 int effective_minimum_delay_ms_; // Used as lower bound for target delay.
106 int minimum_delay_ms_; // Externally set minimum delay.
107 int maximum_delay_ms_; // Externally set maximum allowed delay.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100108
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200109 int packet_len_ms_ = 0;
Byoungchan Lee604fd2f2022-01-21 09:49:39 +0900110 int target_level_ms_; // Currently preferred buffer level.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111};
112
113} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200114#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_