blob: 56d108ad11f05ac25de5c5142e2d0930ff14cd62 [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 Ivarsson74158ff2021-09-07 14:24:56 +020022#include "modules/audio_coding/neteq/relative_arrival_delay_tracker.h"
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020023#include "modules/audio_coding/neteq/reorder_optimizer.h"
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020024#include "modules/audio_coding/neteq/underrun_optimizer.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000025
26namespace webrtc {
27
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000028class DelayManager {
29 public:
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020030 struct Config {
31 Config();
32 void Log();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000033
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020034 // Options that can be configured via field trial.
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010035 double quantile = 0.95;
36 double forget_factor = 0.983;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020037 absl::optional<double> start_forget_weight = 2;
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010038 absl::optional<int> resample_interval_ms = 500;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020039 int max_history_ms = 2000;
40
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020041 bool use_reorder_optimizer = true;
42 double reorder_forget_factor = 0.9993;
43 int ms_per_loss_percent = 20;
44
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020045 // Options that are externally populated.
46 int max_packets_in_buffer = 200;
47 int base_minimum_delay_ms = 0;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020048 };
49
50 DelayManager(const Config& config, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000052 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090054 DelayManager(const DelayManager&) = delete;
55 DelayManager& operator=(const DelayManager&) = delete;
56
Artem Titovd00ce742021-07-28 20:00:17 +020057 // Updates the delay manager with a new incoming packet, with `timestamp` from
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020058 // the RTP header. This updates the statistics and a new target buffer level
59 // is calculated. Returns the relative delay if it can be calculated. If
Artem Titovd00ce742021-07-28 20:00:17 +020060 // `reset` is true, restarts the relative arrival delay calculation from this
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020061 // packet.
62 virtual absl::optional<int> Update(uint32_t timestamp,
63 int sample_rate_hz,
64 bool reset = false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020066 // Resets all state.
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000067 virtual void Reset();
68
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020069 // Gets the target buffer level in milliseconds.
70 virtual int TargetDelayMs() const;
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000071
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020072 // Notifies the DelayManager of how much audio data is carried in each packet.
73 virtual int SetPacketAudioLength(int length_ms);
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000074
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075 // Accessors and mutators.
Artem Titovd00ce742021-07-28 20:00:17 +020076 // Assuming `delay` is in valid range.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000077 virtual bool SetMinimumDelay(int delay_ms);
78 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010079 virtual bool SetBaseMinimumDelay(int delay_ms);
80 virtual int GetBaseMinimumDelay() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020082 // These accessors are only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010083 int effective_minimum_delay_ms_for_test() const {
84 return effective_minimum_delay_ms_;
85 }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010086
Minyue Li002fbb82018-10-04 11:31:03 +020087 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010088 // Provides value which minimum delay can't exceed based on current buffer
Artem Titovd00ce742021-07-28 20:00:17 +020089 // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010090 int MinimumDelayUpperBound() const;
91
Artem Titovd00ce742021-07-28 20:00:17 +020092 // Updates `effective_minimum_delay_ms_` delay based on current
93 // `minimum_delay_ms_`, `base_minimum_delay_ms_` and `maximum_delay_ms_`
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010094 // and buffer size.
95 void UpdateEffectiveMinimumDelay();
96
Artem Titovd00ce742021-07-28 20:00:17 +020097 // Makes sure that `delay_ms` is less than maximum delay, if any maximum
98 // is set. Also, if possible check `delay_ms` to be less than 75% of
99 // `max_packets_in_buffer_`.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100100 bool IsValidMinimumDelay(int delay_ms) const;
101
102 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100103
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200104 // TODO(jakobi): set maximum buffer delay instead of number of packets.
105 const int max_packets_in_buffer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200106 UnderrunOptimizer underrun_optimizer_;
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +0200107 std::unique_ptr<ReorderOptimizer> reorder_optimizer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200108 RelativeArrivalDelayTracker relative_arrival_delay_tracker_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100109
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100110 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200111 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.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100114
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200115 int packet_len_ms_ = 0;
Byoungchan Lee604fd2f2022-01-21 09:49:39 +0900116 int target_level_ms_; // Currently preferred buffer level.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117};
118
119} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200120#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_