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 | |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 16 | #include <memory> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/tick_timer.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 21 | #include "rtc_base/constructor_magic.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
| 25 | // Forward declaration. |
| 26 | class DelayPeakDetector; |
| 27 | |
| 28 | class DelayManager { |
| 29 | public: |
| 30 | typedef std::vector<int> IATVector; |
| 31 | |
| 32 | // Create a DelayManager object. Notify the delay manager that the packet |
| 33 | // 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] | 34 | // is the number of packet slots in the buffer) and that the target delay |
| 35 | // should be greater than or equal to |base_min_target_delay_ms|. Supply a |
| 36 | // PeakDetector object to the DelayManager. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 37 | DelayManager(size_t max_packets_in_buffer, |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 38 | int base_min_target_delay_ms, |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 39 | DelayPeakDetector* peak_detector, |
| 40 | const TickTimer* tick_timer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 41 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 42 | virtual ~DelayManager(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 43 | |
| 44 | // Read the inter-arrival time histogram. Mainly for testing purposes. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 45 | virtual const IATVector& iat_vector() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 46 | |
| 47 | // 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. |
| 51 | // Returns 0 on success, -1 on failure (invalid sample rate). |
| 52 | virtual int Update(uint16_t sequence_number, |
| 53 | uint32_t timestamp, |
| 54 | int sample_rate_hz); |
| 55 | |
| 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 Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 60 | virtual int CalculateTargetLevel(int iat_packets, bool reordered); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 61 | |
| 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 | |
| 70 | // Calculates the average inter-arrival time deviation from the histogram. |
| 71 | // The result is returned as parts-per-million deviation from the nominal |
| 72 | // inter-arrival time. That is, if the average inter-arrival time is equal to |
| 73 | // the nominal frame time, the return value is zero. A positive value |
| 74 | // corresponds to packet spacing being too large, while a negative value means |
| 75 | // that the packets arrive with less spacing than expected. |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 76 | virtual double EstimatedClockDriftPpm() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 77 | |
| 78 | // Returns true if peak-mode is active. That is, delay peaks were observed |
| 79 | // recently. This method simply asks for the same information from the |
| 80 | // DelayPeakDetector object. |
| 81 | virtual bool PeakFound() const; |
| 82 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 83 | // Reset the inter-arrival time counter to 0. |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 84 | virtual void ResetPacketIatCount(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 85 | |
| 86 | // Writes the lower and higher limits which the buffer level should stay |
| 87 | // within to the corresponding pointers. The values are in (fractions of) |
| 88 | // packets in Q8. |
| 89 | virtual void BufferLimits(int* lower_limit, int* higher_limit) const; |
| 90 | |
| 91 | // Gets the target buffer level, in (fractions of) packets in Q8. This value |
| 92 | // includes any extra delay set through the set_extra_delay_ms() method. |
| 93 | virtual int TargetLevel() const; |
| 94 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 95 | // Informs the delay manager whether or not the last decoded packet contained |
| 96 | // speech. |
| 97 | virtual void LastDecodedWasCngOrDtmf(bool it_was); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 98 | |
henrik.lundin | b8c55b1 | 2017-05-10 07:38:01 -0700 | [diff] [blame] | 99 | // Notify the delay manager that empty packets have been received. These are |
| 100 | // packets that are part of the sequence number series, so that an empty |
| 101 | // packet will shift the sequence numbers for the following packets. |
| 102 | virtual void RegisterEmptyPacket(); |
| 103 | |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 104 | // Apply compression or stretching to the IAT histogram, for a change in frame |
| 105 | // size. This returns an updated histogram. This function is public for |
| 106 | // testability. |
| 107 | static IATVector ScaleHistogram(const IATVector& histogram, |
| 108 | int old_packet_length, |
| 109 | int new_packet_length); |
| 110 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 111 | // Accessors and mutators. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 112 | // Assuming |delay| is in valid range. |
| 113 | virtual bool SetMinimumDelay(int delay_ms); |
| 114 | virtual bool SetMaximumDelay(int delay_ms); |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 115 | virtual int base_target_level() const; |
| 116 | virtual void set_streaming_mode(bool value); |
| 117 | virtual int last_pack_cng_or_dtmf() const; |
| 118 | virtual void set_last_pack_cng_or_dtmf(int value); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 119 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 120 | // This accessor is only intended for testing purposes. |
| 121 | const absl::optional<int>& forced_limit_probability_for_test() const { |
| 122 | return forced_limit_probability_; |
| 123 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 124 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 125 | private: |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 126 | // Sets |iat_vector_| to the default start distribution and sets the |
| 127 | // |base_target_level_| and |target_level_| to the corresponding values. |
| 128 | void ResetHistogram(); |
| 129 | |
| 130 | // Updates |iat_cumulative_sum_| and |max_iat_cumulative_sum_|. (These are |
| 131 | // used by the streaming mode.) This method is called by Update(). |
| 132 | void UpdateCumulativeSums(int packet_len_ms, uint16_t sequence_number); |
| 133 | |
| 134 | // Updates the histogram |iat_vector_|. The probability for inter-arrival time |
| 135 | // equal to |iat_packets| (in integer packets) is increased slightly, while |
| 136 | // all other entries are decreased. This method is called by Update(). |
| 137 | void UpdateHistogram(size_t iat_packets); |
| 138 | |
| 139 | // Makes sure that |target_level_| is not too large, taking |
| 140 | // |max_packets_in_buffer_| and |extra_delay_ms_| into account. This method is |
| 141 | // called by Update(). |
| 142 | void LimitTargetLevel(); |
| 143 | |
| 144 | bool first_packet_received_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 145 | const size_t max_packets_in_buffer_; // Capacity of the packet buffer. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 146 | IATVector iat_vector_; // Histogram of inter-arrival times. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | int iat_factor_; // Forgetting factor for updating the IAT histogram (Q15). |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 148 | const TickTimer* tick_timer_; |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 149 | const int base_min_target_delay_ms_; // Lower bound for target_level_ and |
| 150 | // minimum_delay_ms_. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 151 | // Time elapsed since last packet. |
| 152 | std::unique_ptr<TickTimer::Stopwatch> packet_iat_stopwatch_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 153 | int base_target_level_; // Currently preferred buffer level before peak |
| 154 | // detection and streaming mode (Q0). |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 155 | // TODO(turajs) change the comment according to the implementation of |
| 156 | // minimum-delay. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | int target_level_; // Currently preferred buffer level in (fractions) |
| 158 | // of packets (Q8), before adding any extra delay. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 159 | int packet_len_ms_; // Length of audio in each incoming packet [ms]. |
| 160 | bool streaming_mode_; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 161 | uint16_t last_seq_no_; // Sequence number for last received packet. |
| 162 | uint32_t last_timestamp_; // Timestamp for the last received packet. |
| 163 | int minimum_delay_ms_; // Externally set minimum delay. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 164 | int maximum_delay_ms_; // Externally set maximum allowed delay. |
| 165 | int iat_cumulative_sum_; // Cumulative sum of delta inter-arrival times. |
| 166 | int max_iat_cumulative_sum_; // Max of |iat_cumulative_sum_|. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 167 | // Time elapsed since maximum was observed. |
| 168 | std::unique_ptr<TickTimer::Stopwatch> max_iat_stopwatch_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 169 | DelayPeakDetector& peak_detector_; |
| 170 | int last_pack_cng_or_dtmf_; |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 171 | const bool frame_length_change_experiment_; |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 172 | const absl::optional<int> forced_limit_probability_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 173 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 174 | RTC_DISALLOW_COPY_AND_ASSIGN(DelayManager); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 178 | #endif // MODULES_AUDIO_CODING_NETEQ_DELAY_MANAGER_H_ |