blob: 410aa94b61a8436cda172fa1f3a7652a24c528ad [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"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026
27namespace webrtc {
28
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029class DelayManager {
30 public:
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020031 struct Config {
32 Config();
33 void Log();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020035 // Options that can be configured via field trial.
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010036 double quantile = 0.95;
37 double forget_factor = 0.983;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020038 absl::optional<double> start_forget_weight = 2;
Jakob Ivarssonfa68ac02021-11-09 12:58:45 +010039 absl::optional<int> resample_interval_ms = 500;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020040 int max_history_ms = 2000;
41
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020042 bool use_reorder_optimizer = true;
43 double reorder_forget_factor = 0.9993;
44 int ms_per_loss_percent = 20;
45
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020046 // Options that are externally populated.
47 int max_packets_in_buffer = 200;
48 int base_minimum_delay_ms = 0;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020049 };
50
51 DelayManager(const Config& config, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000052
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000053 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
Artem Titovd00ce742021-07-28 20:00:17 +020055 // Updates the delay manager with a new incoming packet, with `timestamp` from
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020056 // the RTP header. This updates the statistics and a new target buffer level
57 // is calculated. Returns the relative delay if it can be calculated. If
Artem Titovd00ce742021-07-28 20:00:17 +020058 // `reset` is true, restarts the relative arrival delay calculation from this
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020059 // packet.
60 virtual absl::optional<int> Update(uint32_t timestamp,
61 int sample_rate_hz,
62 bool reset = false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020064 // Resets all state.
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000065 virtual void Reset();
66
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020067 // Gets the target buffer level in milliseconds.
68 virtual int TargetDelayMs() const;
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000069
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020070 // Notifies the DelayManager of how much audio data is carried in each packet.
71 virtual int SetPacketAudioLength(int length_ms);
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000072
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073 // Accessors and mutators.
Artem Titovd00ce742021-07-28 20:00:17 +020074 // Assuming `delay` is in valid range.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000075 virtual bool SetMinimumDelay(int delay_ms);
76 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010077 virtual bool SetBaseMinimumDelay(int delay_ms);
78 virtual int GetBaseMinimumDelay() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020080 // These accessors are only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010081 int effective_minimum_delay_ms_for_test() const {
82 return effective_minimum_delay_ms_;
83 }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010084
Minyue Li002fbb82018-10-04 11:31:03 +020085 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010086 // Provides value which minimum delay can't exceed based on current buffer
Artem Titovd00ce742021-07-28 20:00:17 +020087 // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010088 int MinimumDelayUpperBound() const;
89
Artem Titovd00ce742021-07-28 20:00:17 +020090 // Updates `effective_minimum_delay_ms_` delay based on current
91 // `minimum_delay_ms_`, `base_minimum_delay_ms_` and `maximum_delay_ms_`
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010092 // and buffer size.
93 void UpdateEffectiveMinimumDelay();
94
Artem Titovd00ce742021-07-28 20:00:17 +020095 // Makes sure that `delay_ms` is less than maximum delay, if any maximum
96 // is set. Also, if possible check `delay_ms` to be less than 75% of
97 // `max_packets_in_buffer_`.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010098 bool IsValidMinimumDelay(int delay_ms) const;
99
100 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100101
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200102 // TODO(jakobi): set maximum buffer delay instead of number of packets.
103 const int max_packets_in_buffer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200104 UnderrunOptimizer underrun_optimizer_;
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +0200105 std::unique_ptr<ReorderOptimizer> reorder_optimizer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200106 RelativeArrivalDelayTracker relative_arrival_delay_tracker_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100107
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100108 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200109 int effective_minimum_delay_ms_; // Used as lower bound for target delay.
110 int minimum_delay_ms_; // Externally set minimum delay.
111 int maximum_delay_ms_; // Externally set maximum allowed delay.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100112
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200113 int packet_len_ms_ = 0;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200114 int target_level_ms_; // Currently preferred buffer level.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200115
henrikg3c089d72015-09-16 05:37:44 -0700116 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
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_