blob: 277b80de74650d685c446ddcf3137b3727493567 [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"
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020026#include "rtc_base/experiments/struct_parameters_parser.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000027
28namespace webrtc {
29
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000030class DelayManager {
31 public:
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020032 struct Config {
33 Config();
34 void Log();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020036 // Options that can be configured via field trial.
37 double quantile = 0.97;
38 double forget_factor = 0.9993;
39 absl::optional<double> start_forget_weight = 2;
40 absl::optional<int> resample_interval_ms;
41 int max_history_ms = 2000;
42
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +020043 bool use_reorder_optimizer = true;
44 double reorder_forget_factor = 0.9993;
45 int ms_per_loss_percent = 20;
46
Jakob Ivarsson74158ff2021-09-07 14:24:56 +020047 // Options that are externally populated.
48 int max_packets_in_buffer = 200;
49 int base_minimum_delay_ms = 0;
50
51 private:
52 std::unique_ptr<StructParametersParser> Parser();
53
54 // TODO(jakobi): remove legacy field trial.
55 void MaybeUpdateFromLegacyFieldTrial();
56 };
57
58 DelayManager(const Config& config, const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000060 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
Artem Titovd00ce742021-07-28 20:00:17 +020062 // Updates the delay manager with a new incoming packet, with `timestamp` from
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020063 // the RTP header. This updates the statistics and a new target buffer level
64 // is calculated. Returns the relative delay if it can be calculated. If
Artem Titovd00ce742021-07-28 20:00:17 +020065 // `reset` is true, restarts the relative arrival delay calculation from this
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020066 // packet.
67 virtual absl::optional<int> Update(uint32_t timestamp,
68 int sample_rate_hz,
69 bool reset = false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020071 // Resets all state.
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000072 virtual void Reset();
73
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020074 // Gets the target buffer level in milliseconds.
75 virtual int TargetDelayMs() const;
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000076
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020077 // Notifies the DelayManager of how much audio data is carried in each packet.
78 virtual int SetPacketAudioLength(int length_ms);
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000079
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 // Accessors and mutators.
Artem Titovd00ce742021-07-28 20:00:17 +020081 // Assuming `delay` is in valid range.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000082 virtual bool SetMinimumDelay(int delay_ms);
83 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010084 virtual bool SetBaseMinimumDelay(int delay_ms);
85 virtual int GetBaseMinimumDelay() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020087 // These accessors are only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010088 int effective_minimum_delay_ms_for_test() const {
89 return effective_minimum_delay_ms_;
90 }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010091
Minyue Li002fbb82018-10-04 11:31:03 +020092 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010093 // Provides value which minimum delay can't exceed based on current buffer
Artem Titovd00ce742021-07-28 20:00:17 +020094 // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010095 int MinimumDelayUpperBound() const;
96
Artem Titovd00ce742021-07-28 20:00:17 +020097 // Updates `effective_minimum_delay_ms_` delay based on current
98 // `minimum_delay_ms_`, `base_minimum_delay_ms_` and `maximum_delay_ms_`
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010099 // and buffer size.
100 void UpdateEffectiveMinimumDelay();
101
Artem Titovd00ce742021-07-28 20:00:17 +0200102 // Makes sure that `delay_ms` is less than maximum delay, if any maximum
103 // is set. Also, if possible check `delay_ms` to be less than 75% of
104 // `max_packets_in_buffer_`.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100105 bool IsValidMinimumDelay(int delay_ms) const;
106
107 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100108
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200109 // TODO(jakobi): set maximum buffer delay instead of number of packets.
110 const int max_packets_in_buffer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200111 UnderrunOptimizer underrun_optimizer_;
Jakob Ivarsson58ed02e2021-09-08 16:35:50 +0200112 std::unique_ptr<ReorderOptimizer> reorder_optimizer_;
Jakob Ivarsson74158ff2021-09-07 14:24:56 +0200113 RelativeArrivalDelayTracker relative_arrival_delay_tracker_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100114
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100115 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200116 int effective_minimum_delay_ms_; // Used as lower bound for target delay.
117 int minimum_delay_ms_; // Externally set minimum delay.
118 int maximum_delay_ms_; // Externally set maximum allowed delay.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100119
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200120 int packet_len_ms_ = 0;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200121 int target_level_ms_; // Currently preferred buffer level.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200122
henrikg3c089d72015-09-16 05:37:44 -0700123 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124};
125
126} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200127#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_