henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 14 | #include <string.h> // Provide access to size_t. |
| 15 | |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 16 | #include <deque> |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 17 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/histogram.h" |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 21 | #include "modules/audio_coding/neteq/statistics_calculator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_coding/neteq/tick_timer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | // Forward declaration. |
| 28 | class DelayPeakDetector; |
| 29 | |
| 30 | class DelayManager { |
| 31 | public: |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 32 | enum HistogramMode { |
| 33 | INTER_ARRIVAL_TIME, |
| 34 | RELATIVE_ARRIVAL_DELAY, |
| 35 | }; |
| 36 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 37 | DelayManager(size_t max_packets_in_buffer, |
| 38 | int base_minimum_delay_ms, |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 39 | int histogram_quantile, |
| 40 | HistogramMode histogram_mode, |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 41 | bool enable_rtx_handling, |
| 42 | DelayPeakDetector* peak_detector, |
| 43 | const TickTimer* tick_timer, |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 44 | StatisticsCalculator* statistics, |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 45 | std::unique_ptr<Histogram> histogram); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 46 | |
| 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 Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 49 | // is the number of packet slots in the buffer) and that the target delay |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 50 | // should be greater than or equal to |base_minimum_delay_ms|. Supply a |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 51 | // PeakDetector object to the DelayManager. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 52 | 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 Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 56 | const TickTimer* tick_timer, |
| 57 | StatisticsCalculator* statistics); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 58 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 59 | virtual ~DelayManager(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 60 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 61 | // 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 Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 74 | virtual int CalculateTargetLevel(int iat_packets, bool reordered); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | |
| 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 | |
| 84 | // Calculates the average inter-arrival time deviation from the histogram. |
| 85 | // The result is returned as parts-per-million deviation from the nominal |
| 86 | // inter-arrival time. That is, if the average inter-arrival time is equal to |
| 87 | // the nominal frame time, the return value is zero. A positive value |
| 88 | // corresponds to packet spacing being too large, while a negative value means |
| 89 | // that the packets arrive with less spacing than expected. |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 90 | virtual double EstimatedClockDriftPpm() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 91 | |
| 92 | // Returns true if peak-mode is active. That is, delay peaks were observed |
| 93 | // recently. This method simply asks for the same information from the |
| 94 | // DelayPeakDetector object. |
| 95 | virtual bool PeakFound() const; |
| 96 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 97 | // Reset the inter-arrival time counter to 0. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 98 | virtual void ResetPacketIatCount(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 99 | |
| 100 | // Writes the lower and higher limits which the buffer level should stay |
| 101 | // within to the corresponding pointers. The values are in (fractions of) |
| 102 | // packets in Q8. |
| 103 | virtual void BufferLimits(int* lower_limit, int* higher_limit) const; |
| 104 | |
| 105 | // Gets the target buffer level, in (fractions of) packets in Q8. This value |
| 106 | // includes any extra delay set through the set_extra_delay_ms() method. |
| 107 | virtual int TargetLevel() const; |
| 108 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 109 | // Informs the delay manager whether or not the last decoded packet contained |
| 110 | // speech. |
| 111 | virtual void LastDecodedWasCngOrDtmf(bool it_was); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 112 | |
henrik.lundin | b8c55b1 | 2017-05-10 07:38:01 -0700 | [diff] [blame] | 113 | // Notify the delay manager that empty packets have been received. These are |
| 114 | // packets that are part of the sequence number series, so that an empty |
| 115 | // packet will shift the sequence numbers for the following packets. |
| 116 | virtual void RegisterEmptyPacket(); |
| 117 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 118 | // Accessors and mutators. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 119 | // Assuming |delay| is in valid range. |
| 120 | virtual bool SetMinimumDelay(int delay_ms); |
| 121 | virtual bool SetMaximumDelay(int delay_ms); |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 122 | virtual bool SetBaseMinimumDelay(int delay_ms); |
| 123 | virtual int GetBaseMinimumDelay() const; |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 124 | virtual int base_target_level() const; |
| 125 | virtual void set_streaming_mode(bool value); |
| 126 | virtual int last_pack_cng_or_dtmf() const; |
| 127 | virtual void set_last_pack_cng_or_dtmf(int value); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 129 | // This accessor is only intended for testing purposes. |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 130 | int effective_minimum_delay_ms_for_test() const { |
| 131 | return effective_minimum_delay_ms_; |
| 132 | } |
| 133 | |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 134 | // This accessor is only intended for testing purposes. |
| 135 | HistogramMode histogram_mode() const { return histogram_mode_; } |
| 136 | int histogram_quantile() const { return histogram_quantile_; } |
| 137 | int histogram_forget_factor() const { return histogram_->forget_factor(); } |
| 138 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 139 | private: |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 140 | // Provides value which minimum delay can't exceed based on current buffer |
| 141 | // size and given |maximum_delay_ms_|. Lower bound is a constant 0. |
| 142 | int MinimumDelayUpperBound() const; |
| 143 | |
| 144 | // Provides 75% of currently possible maximum buffer size in milliseconds. |
| 145 | int MaxBufferTimeQ75() const; |
| 146 | |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 147 | // Updates |delay_history_|. |
| 148 | void UpdateDelayHistory(int iat_delay); |
| 149 | |
| 150 | // Calculate relative packet arrival delay from |delay_history_|. |
| 151 | int CalculateRelativePacketArrivalDelay() const; |
| 152 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 153 | // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are |
| 154 | // used by the streaming mode.) This method is called by Update(). |
| 155 | void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number); |
| 156 | |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 157 | // Updates |effective_minimum_delay_ms_| delay based on current |
| 158 | // |minimum_delay_ms_|, |base_minimum_delay_ms_| and |maximum_delay_ms_| |
| 159 | // and buffer size. |
| 160 | void UpdateEffectiveMinimumDelay(); |
| 161 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 162 | // Makes sure that |target_level_| is not too large, taking |
| 163 | // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is |
| 164 | // called by Update(). |
| 165 | void LimitTargetLevel(); |
| 166 | |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 167 | // Makes sure that |delay_ms| is less than maximum delay, if any maximum |
| 168 | // is set. Also, if possible check |delay_ms| to be less than 75% of |
| 169 | // |max_packets_in_buffer_|. |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 170 | bool IsValidMinimumDelay(int delay_ms) const; |
| 171 | |
| 172 | bool IsValidBaseMinimumDelay(int delay_ms) const; |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 173 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 174 | bool first_packet_received_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 175 | const size_t max_packets_in_buffer_; // Capacity of the packet buffer. |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 176 | std::unique_ptr<Histogram> histogram_; |
| 177 | const int histogram_quantile_; |
| 178 | const HistogramMode histogram_mode_; |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 179 | const TickTimer* tick_timer_; |
Jakob Ivarsson | 4450708 | 2019-03-05 16:59:03 +0100 | [diff] [blame] | 180 | StatisticsCalculator* statistics_; |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 181 | int base_minimum_delay_ms_; |
| 182 | // Provides delay which is used by LimitTargetLevel as lower bound on target |
| 183 | // delay. |
| 184 | int effective_minimum_delay_ms_; |
| 185 | |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 186 | // Time elapsed since last packet. |
| 187 | std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 188 | int base_target_level_; // Currently preferred buffer level before peak |
| 189 | // detection and streaming mode (Q0). |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 190 | // TODO(turajs) change the comment according to the implementation of |
| 191 | // minimum-delay. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 192 | int target_level_; // Currently preferred buffer level in (fractions) |
| 193 | // of packets (Q8), before adding any extra delay. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 194 | int packet_len_ms_; // Length of audio in each incoming packet [ms]. |
| 195 | bool streaming_mode_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 196 | uint16_t last_seq_no_; // Sequence number for last received packet. |
| 197 | uint32_t last_timestamp_; // Timestamp for the last received packet. |
| 198 | int minimum_delay_ms_; // Externally set minimum delay. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 199 | int maximum_delay_ms_; // Externally set maximum allowed delay. |
| 200 | int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times. |
| 201 | int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 202 | // Time elapsed since maximum was observed. |
| 203 | std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | DelayPeakDetector& peak_detector_; |
| 205 | int last_pack_cng_or_dtmf_; |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 206 | const bool frame_length_change_experiment_; |
Jakob Ivarsson | e98954c | 2019-02-06 15:37:50 +0100 | [diff] [blame] | 207 | const bool enable_rtx_handling_; |
| 208 | int num_reordered_packets_ = 0; // Number of consecutive reordered packets. |
Jakob Ivarsson | db42ed2 | 2019-02-27 10:08:09 +0100 | [diff] [blame] | 209 | std::deque<int> delay_history_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 210 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 211 | RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 215 | #endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_ |