blob: a701242d4f4bfa33e55af0c8d5d8a8f98fea9199 [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"
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010020#include "modules/audio_coding/neteq/histogram.h"
Jakob Ivarsson44507082019-03-05 16:59:03 +010021#include "modules/audio_coding/neteq/statistics_calculator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/audio_coding/neteq/tick_timer.h"
Steve Anton10542f22019-01-11 09:11:00 -080023#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25namespace webrtc {
26
27// Forward declaration.
28class DelayPeakDetector;
29
30class DelayManager {
31 public:
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010032 enum HistogramMode {
33 INTER_ARRIVAL_TIME,
34 RELATIVE_ARRIVAL_DELAY,
35 };
36
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010037 DelayManager(size_t max_packets_in_buffer,
38 int base_minimum_delay_ms,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010039 int histogram_quantile,
40 HistogramMode histogram_mode,
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010041 bool enable_rtx_handling,
42 DelayPeakDetector* peak_detector,
43 const TickTimer* tick_timer,
Jakob Ivarsson44507082019-03-05 16:59:03 +010044 StatisticsCalculator* statistics,
Jakob Ivarssondb42ed22019-02-27 10:08:09 +010045 std::unique_ptr<Histogram> histogram);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
47 // Create a DelayManager object. Notify the delay manager that the packet
48 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010049 // is the number of packet slots in the buffer) and that the target delay
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010050 // should be greater than or equal to |base_minimum_delay_ms|. Supply a
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010051 // PeakDetector object to the DelayManager.
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010052 static std::unique_ptr<DelayManager> Create(size_t max_packets_in_buffer,
53 int base_minimum_delay_ms,
54 bool enable_rtx_handling,
55 DelayPeakDetector* peak_detector,
Jakob Ivarsson44507082019-03-05 16:59:03 +010056 const TickTimer* tick_timer,
57 StatisticsCalculator* statistics);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000059 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000060
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061 // Updates the delay manager with a new incoming packet, with
62 // |sequence_number| and |timestamp| from the RTP header. This updates the
63 // inter-arrival time histogram and other statistics, as well as the
64 // associated DelayPeakDetector. A new target buffer level is calculated.
65 // Returns 0 on success, -1 on failure (invalid sample rate).
66 virtual int Update(uint16_t sequence_number,
67 uint32_t timestamp,
68 int sample_rate_hz);
69
70 // Calculates a new target buffer level. Called from the Update() method.
71 // Sets target_level_ (in Q8) and returns the same value. Also calculates
72 // and updates base_target_level_, which is the target buffer level before
73 // taking delay peaks into account.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010074 virtual int CalculateTargetLevel(int iat_packets, bool reordered);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000075
76 // Notifies the DelayManager of how much audio data is carried in each packet.
77 // The method updates the DelayPeakDetector too, and resets the inter-arrival
78 // time counter. Returns 0 on success, -1 on failure.
79 virtual int SetPacketAudioLength(int length_ms);
80
81 // Resets the DelayManager and the associated DelayPeakDetector.
82 virtual void Reset();
83
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000084 // Returns true if peak-mode is active. That is, delay peaks were observed
85 // recently. This method simply asks for the same information from the
86 // DelayPeakDetector object.
87 virtual bool PeakFound() const;
88
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000090 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000091
92 // Writes the lower and higher limits which the buffer level should stay
93 // within to the corresponding pointers. The values are in (fractions of)
94 // packets in Q8.
95 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
Ruslan Burakov1e193fa2019-05-15 14:31:22 +020096 virtual void BufferLimits(int target_level,
97 int* lower_limit,
98 int* higher_limit) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
Ruslan Burakov0ac1d992019-05-14 18:06:52 +0200100 // Gets the target buffer level, in (fractions of) packets in Q8.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000101 virtual int TargetLevel() const;
102
ossuf1b08da2016-09-23 02:19:43 -0700103 // Informs the delay manager whether or not the last decoded packet contained
104 // speech.
105 virtual void LastDecodedWasCngOrDtmf(bool it_was);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000106
henrik.lundinb8c55b12017-05-10 07:38:01 -0700107 // Notify the delay manager that empty packets have been received. These are
108 // packets that are part of the sequence number series, so that an empty
109 // packet will shift the sequence numbers for the following packets.
110 virtual void RegisterEmptyPacket();
111
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000112 // Accessors and mutators.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000113 // Assuming |delay| is in valid range.
114 virtual bool SetMinimumDelay(int delay_ms);
115 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +0100116 virtual bool SetBaseMinimumDelay(int delay_ms);
117 virtual int GetBaseMinimumDelay() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000118 virtual int base_target_level() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000119 virtual int last_pack_cng_or_dtmf() const;
120 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
Minyue Li002fbb82018-10-04 11:31:03 +0200122 // This accessor is only intended for testing purposes.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100123 int effective_minimum_delay_ms_for_test() const {
124 return effective_minimum_delay_ms_;
125 }
126
Ruslan Burakov1e193fa2019-05-15 14:31:22 +0200127 // These accessors are only intended for testing purposes.
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100128 HistogramMode histogram_mode() const { return histogram_mode_; }
129 int histogram_quantile() const { return histogram_quantile_; }
Jakob Ivarssone9a2ee22019-05-22 16:54:09 +0200130 Histogram* histogram() const { return histogram_.get(); }
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100131
Minyue Li002fbb82018-10-04 11:31:03 +0200132 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100133 // Provides value which minimum delay can't exceed based on current buffer
134 // size and given |maximum_delay_ms_|. Lower bound is a constant 0.
135 int MinimumDelayUpperBound() const;
136
137 // Provides 75% of currently possible maximum buffer size in milliseconds.
138 int MaxBufferTimeQ75() const;
139
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100140 // Updates |delay_history_|.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200141 void UpdateDelayHistory(int iat_delay_ms,
142 uint32_t timestamp,
143 int sample_rate_hz);
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100144
145 // Calculate relative packet arrival delay from |delay_history_|.
146 int CalculateRelativePacketArrivalDelay() const;
147
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100148 // Updates |effective_minimum_delay_ms_| delay based on current
149 // |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_|
150 // and buffer size.
151 void UpdateEffectiveMinimumDelay();
152
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000153 // Makes sure that |target_level_| is not too large, taking
154 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
155 // called by Update().
156 void LimitTargetLevel();
157
Ruslan Burakovedbea462019-02-04 16:17:31 +0100158 // Makes sure that |delay_ms| is less than maximum delay, if any maximum
159 // is set. Also, if possible check |delay_ms| to be less than 75% of
160 // |max_packets_in_buffer_|.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100161 bool IsValidMinimumDelay(int delay_ms) const;
162
163 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100164
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000165 bool first_packet_received_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700166 const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
Jakob Ivarssondb42ed22019-02-27 10:08:09 +0100167 std::unique_ptr<Histogram> histogram_;
168 const int histogram_quantile_;
169 const HistogramMode histogram_mode_;
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700170 const TickTimer* tick_timer_;
Jakob Ivarsson44507082019-03-05 16:59:03 +0100171 StatisticsCalculator* statistics_;
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100172 int base_minimum_delay_ms_;
173 // Provides delay which is used by LimitTargetLevel as lower bound on target
174 // delay.
175 int effective_minimum_delay_ms_;
176
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700177 // Time elapsed since last packet.
178 std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
Yves Gerey665174f2018-06-19 15:03:05 +0200179 int base_target_level_; // Currently preferred buffer level before peak
180 // detection and streaming mode (Q0).
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000181 // TODO(turajs) change the comment according to the implementation of
182 // minimum-delay.
Jonas Olssona4d87372019-07-05 19:08:33 +0200183 int target_level_; // Currently preferred buffer level in (fractions)
184 // of packets (Q8), before adding any extra delay.
185 int packet_len_ms_; // Length of audio in each incoming packet [ms].
186 uint16_t last_seq_no_; // Sequence number for last received packet.
187 uint32_t last_timestamp_; // Timestamp for the last received packet.
188 int minimum_delay_ms_; // Externally set minimum delay.
189 int maximum_delay_ms_; // Externally set maximum allowed delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000190 DelayPeakDetector& peak_detector_;
191 int last_pack_cng_or_dtmf_;
Ivo Creusen385b10b2017-10-13 12:37:27 +0200192 const bool frame_length_change_experiment_;
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100193 const bool enable_rtx_handling_;
194 int num_reordered_packets_ = 0; // Number of consecutive reordered packets.
Jakob Ivarsson74154e62019-08-22 15:00:16 +0200195
196 struct PacketDelay {
197 int iat_delay_ms;
198 uint32_t timestamp;
199 };
200 std::deque<PacketDelay> delay_history_;
201
Jakob Ivarsson81df62b2019-07-17 14:09:04 +0200202 const absl::optional<int> extra_delay_ms_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000203
henrikg3c089d72015-09-16 05:37:44 -0700204 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000205};
206
207} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200208#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_