blob: ab9ba341674074f8a0798e74385c37bcafd582df [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 Ivarsson1eb3d7e2019-02-21 15:42:31 +010028 DelayManager(size_t max_packets_in_buffer,
29 int base_minimum_delay_ms,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010030 int histogram_quantile,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010031 bool enable_rtx_handling,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010032 const TickTimer* tick_timer,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010033 std::unique_ptr<Histogram> histogram);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000034
35 // Create a DelayManager object. Notify the delay manager that the packet
36 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010037 // is the number of packet slots in the buffer) and that the target delay
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010038 // should be greater than or equal to |base_minimum_delay_ms|. Supply a
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010039 // PeakDetector object to the DelayManager.
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010040 static std::unique_ptr<DelayManager> Create(size_t max_packets_in_buffer,
41 int base_minimum_delay_ms,
42 bool enable_rtx_handling,
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
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 // Updates the delay manager with a new incoming packet, with
48 // |sequence_number| and |timestamp| from the RTP header. This updates the
49 // inter-arrival time histogram and other statistics, as well as the
50 // associated DelayPeakDetector. A new target buffer level is calculated.
Ivo Creusen53a31f72019-10-24 15:20:39 +020051 // Returns the relative delay if it can be calculated.
52 virtual absl::optional<int> Update(uint16_t sequence_number,
53 uint32_t timestamp,
54 int sample_rate_hz);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
56 // Calculates a new target buffer level. Called from the Update() method.
57 // Sets target_level_ (in Q8) and returns the same value. Also calculates
58 // and updates base_target_level_, which is the target buffer level before
59 // taking delay peaks into account.
Jakob Ivarssonbd5874a2020-01-07 17:07:40 +010060 virtual int CalculateTargetLevel();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
62 // Notifies the DelayManager of how much audio data is carried in each packet.
63 // The method updates the DelayPeakDetector too, and resets the inter-arrival
64 // time counter. Returns 0 on success, -1 on failure.
65 virtual int SetPacketAudioLength(int length_ms);
66
67 // Resets the DelayManager and the associated DelayPeakDetector.
68 virtual void Reset();
69
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000071 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
73 // Writes the lower and higher limits which the buffer level should stay
74 // within to the corresponding pointers. The values are in (fractions of)
75 // packets in Q8.
76 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
Ruslan Burakov1e193fa2019-05-15 14:31:22 +020077 virtual void BufferLimits(int target_level,
78 int* lower_limit,
79 int* higher_limit) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080
Ruslan Burakov0ac1d992019-05-14 18:06:52 +020081 // Gets the target buffer level, in (fractions of) packets in Q8.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 virtual int TargetLevel() const;
83
ossuf1b08da2016-09-23 02:19:43 -070084 // Informs the delay manager whether or not the last decoded packet contained
85 // speech.
86 virtual void LastDecodedWasCngOrDtmf(bool it_was);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087
henrik.lundinb8c55b12017-05-10 07:38:01 -070088 // Notify the delay manager that empty packets have been received. These are
89 // packets that are part of the sequence number series, so that an empty
90 // packet will shift the sequence numbers for the following packets.
91 virtual void RegisterEmptyPacket();
92
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 // Accessors and mutators.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +000094 // Assuming |delay| is in valid range.
95 virtual bool SetMinimumDelay(int delay_ms);
96 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +010097 virtual bool SetBaseMinimumDelay(int delay_ms);
98 virtual int GetBaseMinimumDelay() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000099 virtual int base_target_level() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000100 virtual int last_pack_cng_or_dtmf() const;
101 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102
Minyue Li002fbb82018-10-04 11:31:03 +0200103 // This accessor is only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100104 int effective_minimum_delay_ms_for_test() const {
105 return effective_minimum_delay_ms_;
106 }
107
Ruslan Burakov1e193fa2019-05-15 14:31:22 +0200108 // These accessors are only intended for testing purposes.
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100109 int histogram_quantile() const { return histogram_quantile_; }
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +0200110 Histogram* histogram() const { return histogram_.get(); }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100111
Minyue Li002fbb82018-10-04 11:31:03 +0200112 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100113 // Provides value which minimum delay can't exceed based on current buffer
114 // size and given |maximum_delay_ms_|. Lower bound is a constant 0.
115 int MinimumDelayUpperBound() const;
116
117 // Provides 75% of currently possible maximum buffer size in milliseconds.
118 int MaxBufferTimeQ75() const;
119
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100120 // Updates |delay_history_|.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200121 void UpdateDelayHistory(int iat_delay_ms,
122 uint32_t timestamp,
123 int sample_rate_hz);
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100124
125 // Calculate relative packet arrival delay from |delay_history_|.
126 int CalculateRelativePacketArrivalDelay() const;
127
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100128 // Updates |effective_minimum_delay_ms_| delay based on current
129 // |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_|
130 // and buffer size.
131 void UpdateEffectiveMinimumDelay();
132
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000133 // Makes sure that |target_level_| is not too large, taking
Jakob Ivarsson2ee15eb2020-01-08 14:29:04 +0100134 // |max_packets_in_buffer_| into account. This method is called by Update().
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 void LimitTargetLevel();
136
Ruslan Burakovedbea462019-02-04 16:17:31 +0100137 // Makes sure that |delay_ms| is less than maximum delay, if any maximum
138 // is set. Also, if possible check |delay_ms| to be less than 75% of
139 // |max_packets_in_buffer_|.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100140 bool IsValidMinimumDelay(int delay_ms) const;
141
142 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100143
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000144 bool first_packet_received_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700145 const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100146 std::unique_ptr<Histogram> histogram_;
147 const int histogram_quantile_;
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700148 const TickTimer* tick_timer_;
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100149 int base_minimum_delay_ms_;
150 // Provides delay which is used by LimitTargetLevel as lower bound on target
151 // delay.
152 int effective_minimum_delay_ms_;
153
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700154 // Time elapsed since last packet.
155 std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
Yves Gerey665174f2018-06-19 15:03:05 +0200156 int base_target_level_; // Currently preferred buffer level before peak
157 // detection and streaming mode (Q0).
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000158 // TODO(turajs) change the comment according to the implementation of
159 // minimum-delay.
Jonas Olssona4d87372019-07-05 19:08:33 +0200160 int target_level_; // Currently preferred buffer level in (fractions)
161 // of packets (Q8), before adding any extra delay.
162 int packet_len_ms_; // Length of audio in each incoming packet [ms].
163 uint16_t last_seq_no_; // Sequence number for last received packet.
164 uint32_t last_timestamp_; // Timestamp for the last received packet.
165 int minimum_delay_ms_; // Externally set minimum delay.
166 int maximum_delay_ms_; // Externally set maximum allowed delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000167 int last_pack_cng_or_dtmf_;
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100168 const bool enable_rtx_handling_;
169 int num_reordered_packets_ = 0; // Number of consecutive reordered packets.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200170
171 struct PacketDelay {
172 int iat_delay_ms;
173 uint32_t timestamp;
174 };
175 std::deque<PacketDelay> delay_history_;
176
henrikg3c089d72015-09-16 05:37:44 -0700177 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000178};
179
180} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200181#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_