blob: c751836a6acf1b22505885ceff972caf1f8e060f [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"
23#include "modules/audio_coding/neteq/underrun_optimizer.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "rtc_base/constructor_magic.h"
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020025#include "rtc_base/experiments/struct_parameters_parser.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.
36 double quantile = 0.97;
37 double forget_factor = 0.9993;
38 absl::optional<double> start_forget_weight = 2;
39 absl::optional<int> resample_interval_ms;
40 int max_history_ms = 2000;
41
42 // Options that are externally populated.
43 int max_packets_in_buffer = 200;
44 int base_minimum_delay_ms = 0;
45
46 private:
47 std::unique_ptr<StructParametersParser> Parser();
48
49 // TODO(jakobi): remove legacy field trial.
50 void MaybeUpdateFromLegacyFieldTrial();
51 };
52
53 DelayManager(const Config& config, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000054
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000055 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
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_;
107 RelativeArrivalDelayTracker relative_arrival_delay_tracker_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100108
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100109 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200110 int effective_minimum_delay_ms_; // Used as lower bound for target delay.
111 int minimum_delay_ms_; // Externally set minimum delay.
112 int maximum_delay_ms_; // Externally set maximum allowed delay.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100113
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200114 int packet_len_ms_ = 0;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200115 int target_level_ms_; // Currently preferred buffer level.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200116
henrikg3c089d72015-09-16 05:37:44 -0700117 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118};
119
120} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200121#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_