blob: cb35274424ce0b3b254f3f14b4fb0ed1ec819cba [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"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24namespace webrtc {
25
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000026class DelayManager {
27 public:
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020028 DelayManager(int max_packets_in_buffer,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010029 int base_minimum_delay_ms,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010030 int histogram_quantile,
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +010031 absl::optional<int> resample_interval_ms,
32 int max_history_ms,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010033 const TickTimer* tick_timer,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010034 std::unique_ptr<Histogram> histogram);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
36 // Create a DelayManager object. Notify the delay manager that the packet
Artem Titovd00ce742021-07-28 20:00:17 +020037 // buffer can hold no more than `max_packets_in_buffer` packets (i.e., this
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010038 // is the number of packet slots in the buffer) and that the target delay
Artem Titovd00ce742021-07-28 20:00:17 +020039 // should be greater than or equal to `base_minimum_delay_ms`. Supply a
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010040 // PeakDetector object to the DelayManager.
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020041 static std::unique_ptr<DelayManager> Create(int max_packets_in_buffer,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010042 int base_minimum_delay_ms,
Ivo Creusen53a31f72019-10-24 15:20:39 +020043 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000045 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
Artem Titovd00ce742021-07-28 20:00:17 +020047 // Updates the delay manager with a new incoming packet, with `timestamp` from
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020048 // the RTP header. This updates the statistics and a new target buffer level
49 // is calculated. Returns the relative delay if it can be calculated. If
Artem Titovd00ce742021-07-28 20:00:17 +020050 // `reset` is true, restarts the relative arrival delay calculation from this
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020051 // packet.
52 virtual absl::optional<int> Update(uint32_t timestamp,
53 int sample_rate_hz,
54 bool reset = false);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020056 // Resets all state.
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000057 virtual void Reset();
58
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020059 // Gets the target buffer level in milliseconds.
60 virtual int TargetDelayMs() const;
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000061
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020062 // Notifies the DelayManager of how much audio data is carried in each packet.
63 virtual int SetPacketAudioLength(int length_ms);
Jakob Ivarssonff9f6462020-10-07 12:46:42 +000064
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 // Accessors and mutators.
Artem Titovd00ce742021-07-28 20:00:17 +020066 // Assuming `delay` is in valid range.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000067 virtual bool SetMinimumDelay(int delay_ms);
68 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010069 virtual bool SetBaseMinimumDelay(int delay_ms);
70 virtual int GetBaseMinimumDelay() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
Jakob Ivarsson80fb9782020-10-09 13:41:06 +020072 // These accessors are only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010073 int effective_minimum_delay_ms_for_test() const {
74 return effective_minimum_delay_ms_;
75 }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010076 int histogram_quantile() const { return histogram_quantile_; }
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +020077 Histogram* histogram() const { return histogram_.get(); }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010078
Minyue Li002fbb82018-10-04 11:31:03 +020079 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010080 // Provides value which minimum delay can't exceed based on current buffer
Artem Titovd00ce742021-07-28 20:00:17 +020081 // size and given `maximum_delay_ms_`. Lower bound is a constant 0.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010082 int MinimumDelayUpperBound() const;
83
Artem Titovd00ce742021-07-28 20:00:17 +020084 // Updates `delay_history_`.
Jakob Ivarsson74154e62019-08-22 15:00:16 +020085 void UpdateDelayHistory(int iat_delay_ms,
86 uint32_t timestamp,
87 int sample_rate_hz);
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010088
Artem Titovd00ce742021-07-28 20:00:17 +020089 // Calculate relative packet arrival delay from `delay_history_`.
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010090 int CalculateRelativePacketArrivalDelay() 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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000104 bool first_packet_received_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200105 // TODO(jakobi): set maximum buffer delay instead of number of packets.
106 const int max_packets_in_buffer_;
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100107 std::unique_ptr<Histogram> histogram_;
108 const int histogram_quantile_;
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700109 const TickTimer* tick_timer_;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100110 const absl::optional<int> resample_interval_ms_;
111 const int max_history_ms_;
112
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100113 int base_minimum_delay_ms_;
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200114 int effective_minimum_delay_ms_; // Used as lower bound for target delay.
115 int minimum_delay_ms_; // Externally set minimum delay.
116 int maximum_delay_ms_; // Externally set maximum allowed delay.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100117
Jakob Ivarsson80fb9782020-10-09 13:41:06 +0200118 int packet_len_ms_ = 0;
119 std::unique_ptr<TickTimer::Stopwatch>
120 packet_iat_stopwatch_; // Time elapsed since last packet.
121 int target_level_ms_; // Currently preferred buffer level.
122 uint32_t last_timestamp_; // Timestamp for the last received packet.
123 int num_reordered_packets_ = 0;
Jakob Ivarsson7dff9f32020-11-11 15:26:10 +0100124 int max_delay_in_interval_ms_ = 0;
125 std::unique_ptr<TickTimer::Stopwatch> resample_stopwatch_;
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200126
127 struct PacketDelay {
128 int iat_delay_ms;
129 uint32_t timestamp;
130 };
131 std::deque<PacketDelay> delay_history_;
132
henrikg3c089d72015-09-16 05:37:44 -0700133 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000134};
135
136} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200137#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_