blob: 4f2cb859a79a567dcac41a6190dac2df4c851bbe [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
henrik.lundin8f8c96d2016-04-28 23:19:20 -070016#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000017#include <vector>
18
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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/neteq/tick_timer.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
26// Forward declaration.
27class DelayPeakDetector;
28
29class DelayManager {
30 public:
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010031 DelayManager(size_t max_packets_in_buffer,
32 int base_minimum_delay_ms,
33 bool enable_rtx_handling,
34 DelayPeakDetector* peak_detector,
35 const TickTimer* tick_timer,
36 std::unique_ptr<Histogram> iat_histogram);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000037
38 // Create a DelayManager object. Notify the delay manager that the packet
39 // buffer can hold no more than |max_packets_in_buffer| packets (i.e., this
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010040 // is the number of packet slots in the buffer) and that the target delay
Ruslan Burakov4a68fb92019-02-13 14:25:39 +010041 // should be greater than or equal to |base_minimum_delay_ms|. Supply a
Jakob Ivarsson10403ae2018-11-27 15:45:20 +010042 // PeakDetector object to the DelayManager.
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +010043 static std::unique_ptr<DelayManager> Create(size_t max_packets_in_buffer,
44 int base_minimum_delay_ms,
45 bool enable_rtx_handling,
46 DelayPeakDetector* peak_detector,
47 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000049 virtual ~DelayManager();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051 // Updates the delay manager with a new incoming packet, with
52 // |sequence_number| and |timestamp| from the RTP header. This updates the
53 // inter-arrival time histogram and other statistics, as well as the
54 // associated DelayPeakDetector. A new target buffer level is calculated.
55 // Returns 0 on success, -1 on failure (invalid sample rate).
56 virtual int Update(uint16_t sequence_number,
57 uint32_t timestamp,
58 int sample_rate_hz);
59
60 // Calculates a new target buffer level. Called from the Update() method.
61 // Sets target_level_ (in Q8) and returns the same value. Also calculates
62 // and updates base_target_level_, which is the target buffer level before
63 // taking delay peaks into account.
Jakob Ivarsson39b934b2019-01-10 10:28:23 +010064 virtual int CalculateTargetLevel(int iat_packets, bool reordered);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065
66 // Notifies the DelayManager of how much audio data is carried in each packet.
67 // The method updates the DelayPeakDetector too, and resets the inter-arrival
68 // time counter. Returns 0 on success, -1 on failure.
69 virtual int SetPacketAudioLength(int length_ms);
70
71 // Resets the DelayManager and the associated DelayPeakDetector.
72 virtual void Reset();
73
74 // Calculates the average inter-arrival time deviation from the histogram.
75 // The result is returned as parts-per-million deviation from the nominal
76 // inter-arrival time. That is, if the average inter-arrival time is equal to
77 // the nominal frame time, the return value is zero. A positive value
78 // corresponds to packet spacing being too large, while a negative value means
79 // that the packets arrive with less spacing than expected.
henrik.lundin0d838572016-10-13 03:35:55 -070080 virtual double EstimatedClockDriftPpm() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000081
82 // Returns true if peak-mode is active. That is, delay peaks were observed
83 // recently. This method simply asks for the same information from the
84 // DelayPeakDetector object.
85 virtual bool PeakFound() const;
86
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087 // Reset the inter-arrival time counter to 0.
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +000088 virtual void ResetPacketIatCount();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089
90 // Writes the lower and higher limits which the buffer level should stay
91 // within to the corresponding pointers. The values are in (fractions of)
92 // packets in Q8.
93 virtual void BufferLimits(int* lower_limit, int* higher_limit) const;
94
95 // Gets the target buffer level, in (fractions of) packets in Q8. This value
96 // includes any extra delay set through the set_extra_delay_ms() method.
97 virtual int TargetLevel() const;
98
ossuf1b08da2016-09-23 02:19:43 -070099 // Informs the delay manager whether or not the last decoded packet contained
100 // speech.
101 virtual void LastDecodedWasCngOrDtmf(bool it_was);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102
henrik.lundinb8c55b12017-05-10 07:38:01 -0700103 // Notify the delay manager that empty packets have been received. These are
104 // packets that are part of the sequence number series, so that an empty
105 // packet will shift the sequence numbers for the following packets.
106 virtual void RegisterEmptyPacket();
107
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 // Accessors and mutators.
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000109 // Assuming |delay| is in valid range.
110 virtual bool SetMinimumDelay(int delay_ms);
111 virtual bool SetMaximumDelay(int delay_ms);
Ruslan Burakovedbea462019-02-04 16:17:31 +0100112 virtual bool SetBaseMinimumDelay(int delay_ms);
113 virtual int GetBaseMinimumDelay() const;
pbos@webrtc.org2d1a55c2013-07-31 15:54:00 +0000114 virtual int base_target_level() const;
115 virtual void set_streaming_mode(bool value);
116 virtual int last_pack_cng_or_dtmf() const;
117 virtual void set_last_pack_cng_or_dtmf(int value);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000118
Minyue Li002fbb82018-10-04 11:31:03 +0200119 // This accessor is only intended for testing purposes.
120 const absl::optional<int>& forced_limit_probability_for_test() const {
121 return forced_limit_probability_;
122 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000123
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100124 // This accessor is only intended for testing purposes.
125 int effective_minimum_delay_ms_for_test() const {
126 return effective_minimum_delay_ms_;
127 }
128
Minyue Li002fbb82018-10-04 11:31:03 +0200129 private:
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100130 // Provides value which minimum delay can't exceed based on current buffer
131 // size and given |maximum_delay_ms_|. Lower bound is a constant 0.
132 int MinimumDelayUpperBound() const;
133
134 // Provides 75% of currently possible maximum buffer size in milliseconds.
135 int MaxBufferTimeQ75() const;
136
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000137 // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are
138 // used by the streaming mode.) This method is called by Update().
139 void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number);
140
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100141 // Updates |effective_minimum_delay_ms_| delay based on current
142 // |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_|
143 // and buffer size.
144 void UpdateEffectiveMinimumDelay();
145
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000146 // Makes sure that |target_level_| is not too large, taking
147 // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is
148 // called by Update().
149 void LimitTargetLevel();
150
Ruslan Burakovedbea462019-02-04 16:17:31 +0100151 // Makes sure that |delay_ms| is less than maximum delay, if any maximum
152 // is set. Also, if possible check |delay_ms| to be less than 75% of
153 // |max_packets_in_buffer_|.
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100154 bool IsValidMinimumDelay(int delay_ms) const;
155
156 bool IsValidBaseMinimumDelay(int delay_ms) const;
Ruslan Burakovedbea462019-02-04 16:17:31 +0100157
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000158 bool first_packet_received_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700159 const size_t max_packets_in_buffer_; // Capacity of the packet buffer.
Jakob Ivarsson1eb3d7e2019-02-21 15:42:31 +0100160 std::unique_ptr<Histogram> iat_histogram_;
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700161 const TickTimer* tick_timer_;
Ruslan Burakov4a68fb92019-02-13 14:25:39 +0100162 int base_minimum_delay_ms_;
163 // Provides delay which is used by LimitTargetLevel as lower bound on target
164 // delay.
165 int effective_minimum_delay_ms_;
166
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700167 // Time elapsed since last packet.
168 std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_;
Yves Gerey665174f2018-06-19 15:03:05 +0200169 int base_target_level_; // Currently preferred buffer level before peak
170 // detection and streaming mode (Q0).
turaj@webrtc.orgf1efc572013-08-16 23:44:24 +0000171 // TODO(turajs) change the comment according to the implementation of
172 // minimum-delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200173 int target_level_; // Currently preferred buffer level in (fractions)
174 // of packets (Q8), before adding any extra delay.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000175 int packet_len_ms_; // Length of audio in each incoming packet [ms].
176 bool streaming_mode_;
Yves Gerey665174f2018-06-19 15:03:05 +0200177 uint16_t last_seq_no_; // Sequence number for last received packet.
178 uint32_t last_timestamp_; // Timestamp for the last received packet.
179 int minimum_delay_ms_; // Externally set minimum delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200180 int maximum_delay_ms_; // Externally set maximum allowed delay.
181 int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times.
182 int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|.
henrik.lundin8f8c96d2016-04-28 23:19:20 -0700183 // Time elapsed since maximum was observed.
184 std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000185 DelayPeakDetector& peak_detector_;
186 int last_pack_cng_or_dtmf_;
Ivo Creusen385b10b2017-10-13 12:37:27 +0200187 const bool frame_length_change_experiment_;
Minyue Li002fbb82018-10-04 11:31:03 +0200188 const absl::optional<int> forced_limit_probability_;
Jakob Ivarssone98954c2019-02-06 15:37:50 +0100189 const bool enable_rtx_handling_;
190 int num_reordered_packets_ = 0; // Number of consecutive reordered packets.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191
henrikg3c089d72015-09-16 05:37:44 -0700192 RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193};
194
195} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200196#endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_