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 | #include "modules/audio_coding/neteq/delay_manager.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <algorithm> |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 17 | #include <numeric> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 18 | #include <string> |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_coding/neteq/delay_peak_detector.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 21 | #include "modules/include/module_common_types_public.h" |
| 22 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 24 | #include "rtc_base/numerics/safe_conversions.h" |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 25 | #include "system_wrappers/include/field_trial.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 26 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | constexpr int kLimitProbability = 53687091; // 1/20 in Q30. |
| 30 | constexpr int kLimitProbabilityStreaming = 536871; // 1/2000 in Q30. |
| 31 | constexpr int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms. |
| 32 | constexpr int kCumulativeSumDrift = 2; // Drift term for cumulative sum |
| 33 | // |iat_cumulative_sum_|. |
| 34 | // Steady-state forgetting factor for |iat_vector_|, 0.9993 in Q15. |
| 35 | constexpr int kIatFactor_ = 32745; |
| 36 | constexpr int kMaxIat = 64; // Max inter-arrival time to register. |
| 37 | |
| 38 | absl::optional<int> GetForcedLimitProbability() { |
| 39 | constexpr char kForceTargetDelayPercentileFieldTrial[] = |
| 40 | "WebRTC-Audio-NetEqForceTargetDelayPercentile"; |
| 41 | const bool use_forced_target_delay_percentile = |
| 42 | webrtc::field_trial::IsEnabled(kForceTargetDelayPercentileFieldTrial); |
| 43 | if (use_forced_target_delay_percentile) { |
| 44 | const std::string field_trial_string = webrtc::field_trial::FindFullName( |
| 45 | kForceTargetDelayPercentileFieldTrial); |
| 46 | double percentile = -1.0; |
| 47 | if (sscanf(field_trial_string.c_str(), "Enabled-%lf", &percentile) == 1 && |
| 48 | percentile >= 0.0 && percentile <= 100.0) { |
| 49 | return absl::make_optional<int>(static_cast<int>( |
| 50 | (1 << 30) * (100.0 - percentile) / 100.0 + 0.5)); // in Q30. |
| 51 | } else { |
| 52 | RTC_LOG(LS_WARNING) << "Invalid parameter for " |
| 53 | << kForceTargetDelayPercentileFieldTrial |
| 54 | << ", ignored."; |
| 55 | } |
| 56 | } |
| 57 | return absl::nullopt; |
| 58 | } |
| 59 | |
| 60 | } // namespace |
| 61 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | namespace webrtc { |
| 63 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 64 | DelayManager::DelayManager(size_t max_packets_in_buffer, |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 65 | int base_min_target_delay_ms, |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 66 | DelayPeakDetector* peak_detector, |
| 67 | const TickTimer* tick_timer) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | : first_packet_received_(false), |
| 69 | max_packets_in_buffer_(max_packets_in_buffer), |
| 70 | iat_vector_(kMaxIat + 1, 0), |
| 71 | iat_factor_(0), |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 72 | tick_timer_(tick_timer), |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 73 | base_min_target_delay_ms_(base_min_target_delay_ms), |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 74 | base_target_level_(4), // In Q0 domain. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | target_level_(base_target_level_ << 8), // In Q8 domain. |
| 76 | packet_len_ms_(0), |
| 77 | streaming_mode_(false), |
| 78 | last_seq_no_(0), |
| 79 | last_timestamp_(0), |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 80 | minimum_delay_ms_(base_min_target_delay_ms_), |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 81 | maximum_delay_ms_(target_level_), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | iat_cumulative_sum_(0), |
| 83 | max_iat_cumulative_sum_(0), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 84 | peak_detector_(*peak_detector), |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 85 | last_pack_cng_or_dtmf_(1), |
| 86 | frame_length_change_experiment_( |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 87 | field_trial::IsEnabled("WebRTC-Audio-NetEqFramelengthExperiment")), |
| 88 | forced_limit_probability_(GetForcedLimitProbability()) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 89 | assert(peak_detector); // Should never be NULL. |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 90 | RTC_DCHECK_GE(base_min_target_delay_ms_, 0); |
| 91 | RTC_DCHECK_LE(minimum_delay_ms_, maximum_delay_ms_); |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 92 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | Reset(); |
| 94 | } |
| 95 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 96 | DelayManager::~DelayManager() {} |
| 97 | |
| 98 | const DelayManager::IATVector& DelayManager::iat_vector() const { |
| 99 | return iat_vector_; |
| 100 | } |
| 101 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 102 | // Set the histogram vector to an exponentially decaying distribution |
| 103 | // iat_vector_[i] = 0.5^(i+1), i = 0, 1, 2, ... |
| 104 | // iat_vector_ is in Q30. |
| 105 | void DelayManager::ResetHistogram() { |
| 106 | // Set temp_prob to (slightly more than) 1 in Q14. This ensures that the sum |
| 107 | // of iat_vector_ is 1. |
| 108 | uint16_t temp_prob = 0x4002; // 16384 + 2 = 100000000000010 binary. |
| 109 | IATVector::iterator it = iat_vector_.begin(); |
| 110 | for (; it < iat_vector_.end(); it++) { |
| 111 | temp_prob >>= 1; |
| 112 | (*it) = temp_prob << 16; |
| 113 | } |
| 114 | base_target_level_ = 4; |
| 115 | target_level_ = base_target_level_ << 8; |
| 116 | } |
| 117 | |
| 118 | int DelayManager::Update(uint16_t sequence_number, |
| 119 | uint32_t timestamp, |
| 120 | int sample_rate_hz) { |
| 121 | if (sample_rate_hz <= 0) { |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (!first_packet_received_) { |
| 126 | // Prepare for next packet arrival. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 127 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | last_seq_no_ = sequence_number; |
| 129 | last_timestamp_ = timestamp; |
| 130 | first_packet_received_ = true; |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | // Try calculating packet length from current and previous timestamps. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 135 | int packet_len_ms; |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 136 | if (!IsNewerTimestamp(timestamp, last_timestamp_) || |
| 137 | !IsNewerSequenceNumber(sequence_number, last_seq_no_)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 138 | // Wrong timestamp or sequence order; use stored value. |
| 139 | packet_len_ms = packet_len_ms_; |
| 140 | } else { |
| 141 | // Calculate timestamps per packet and derive packet length in ms. |
henrik.lundin | 07c51e3 | 2016-02-11 03:35:43 -0800 | [diff] [blame] | 142 | int64_t packet_len_samp = |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 143 | static_cast<uint32_t>(timestamp - last_timestamp_) / |
| 144 | static_cast<uint16_t>(sequence_number - last_seq_no_); |
henrik.lundin | 07c51e3 | 2016-02-11 03:35:43 -0800 | [diff] [blame] | 145 | packet_len_ms = |
henrik.lundin | 38d840c | 2016-08-18 03:49:32 -0700 | [diff] [blame] | 146 | rtc::saturated_cast<int>(1000 * packet_len_samp / sample_rate_hz); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (packet_len_ms > 0) { |
| 150 | // Cannot update statistics unless |packet_len_ms| is valid. |
| 151 | // Calculate inter-arrival time (IAT) in integer "packet times" |
| 152 | // (rounding down). This is the value used as index to the histogram |
| 153 | // vector |iat_vector_|. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 154 | int iat_packets = packet_iat_stopwatch_->ElapsedMs() / packet_len_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 155 | |
| 156 | if (streaming_mode_) { |
| 157 | UpdateCumulativeSums(packet_len_ms, sequence_number); |
| 158 | } |
| 159 | |
| 160 | // Check for discontinuous packet sequence and re-ordering. |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 161 | bool reordered = false; |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 162 | if (IsNewerSequenceNumber(sequence_number, last_seq_no_ + 1)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 163 | // Compensate for gap in the sequence numbers. Reduce IAT with the |
| 164 | // expected extra time due to lost packets, but ensure that the IAT is |
| 165 | // not negative. |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 166 | iat_packets -= static_cast<uint16_t>(sequence_number - last_seq_no_ - 1); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 167 | iat_packets = std::max(iat_packets, 0); |
turaj@webrtc.org | 78b41a0 | 2013-11-22 20:27:07 +0000 | [diff] [blame] | 168 | } else if (!IsNewerSequenceNumber(sequence_number, last_seq_no_)) { |
| 169 | iat_packets += static_cast<uint16_t>(last_seq_no_ + 1 - sequence_number); |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 170 | reordered = true; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Saturate IAT at maximum value. |
| 174 | const int max_iat = kMaxIat; |
| 175 | iat_packets = std::min(iat_packets, max_iat); |
| 176 | UpdateHistogram(iat_packets); |
| 177 | // Calculate new |target_level_| based on updated statistics. |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 178 | target_level_ = CalculateTargetLevel(iat_packets, reordered); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 179 | if (streaming_mode_) { |
| 180 | target_level_ = std::max(target_level_, max_iat_cumulative_sum_); |
| 181 | } |
| 182 | |
| 183 | LimitTargetLevel(); |
| 184 | } // End if (packet_len_ms > 0). |
| 185 | |
| 186 | // Prepare for next packet arrival. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 187 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 188 | last_seq_no_ = sequence_number; |
| 189 | last_timestamp_ = timestamp; |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | void DelayManager::UpdateCumulativeSums(int packet_len_ms, |
| 194 | uint16_t sequence_number) { |
| 195 | // Calculate IAT in Q8, including fractions of a packet (i.e., more |
| 196 | // accurate than |iat_packets|. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 197 | int iat_packets_q8 = |
| 198 | (packet_iat_stopwatch_->ElapsedMs() << 8) / packet_len_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 199 | // Calculate cumulative sum IAT with sequence number compensation. The sum |
| 200 | // is zero if there is no clock-drift. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 201 | iat_cumulative_sum_ += |
| 202 | (iat_packets_q8 - |
| 203 | (static_cast<int>(sequence_number - last_seq_no_) << 8)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | // Subtract drift term. |
| 205 | iat_cumulative_sum_ -= kCumulativeSumDrift; |
| 206 | // Ensure not negative. |
| 207 | iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0); |
| 208 | if (iat_cumulative_sum_ > max_iat_cumulative_sum_) { |
| 209 | // Found a new maximum. |
| 210 | max_iat_cumulative_sum_ = iat_cumulative_sum_; |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 211 | max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 212 | } |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 213 | if (max_iat_stopwatch_->ElapsedMs() > kMaxStreamingPeakPeriodMs) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 214 | // Too long since the last maximum was observed; decrease max value. |
| 215 | max_iat_cumulative_sum_ -= kCumulativeSumDrift; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Each element in the vector is first multiplied by the forgetting factor |
| 220 | // |iat_factor_|. Then the vector element indicated by |iat_packets| is then |
| 221 | // increased (additive) by 1 - |iat_factor_|. This way, the probability of |
| 222 | // |iat_packets| is slightly increased, while the sum of the histogram remains |
| 223 | // constant (=1). |
| 224 | // Due to inaccuracies in the fixed-point arithmetic, the histogram may no |
| 225 | // longer sum up to 1 (in Q30) after the update. To correct this, a correction |
| 226 | // term is added or subtracted from the first element (or elements) of the |
| 227 | // vector. |
| 228 | // The forgetting factor |iat_factor_| is also updated. When the DelayManager |
| 229 | // is reset, the factor is set to 0 to facilitate rapid convergence in the |
| 230 | // beginning. With each update of the histogram, the factor is increased towards |
| 231 | // the steady-state value |kIatFactor_|. |
| 232 | void DelayManager::UpdateHistogram(size_t iat_packets) { |
| 233 | assert(iat_packets < iat_vector_.size()); |
| 234 | int vector_sum = 0; // Sum up the vector elements as they are processed. |
| 235 | // Multiply each element in |iat_vector_| with |iat_factor_|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 236 | for (IATVector::iterator it = iat_vector_.begin(); it != iat_vector_.end(); |
| 237 | ++it) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | *it = (static_cast<int64_t>(*it) * iat_factor_) >> 15; |
| 239 | vector_sum += *it; |
| 240 | } |
| 241 | |
| 242 | // Increase the probability for the currently observed inter-arrival time |
| 243 | // by 1 - |iat_factor_|. The factor is in Q15, |iat_vector_| in Q30. |
| 244 | // Thus, left-shift 15 steps to obtain result in Q30. |
| 245 | iat_vector_[iat_packets] += (32768 - iat_factor_) << 15; |
| 246 | vector_sum += (32768 - iat_factor_) << 15; // Add to vector sum. |
| 247 | |
| 248 | // |iat_vector_| should sum up to 1 (in Q30), but it may not due to |
| 249 | // fixed-point rounding errors. |
| 250 | vector_sum -= 1 << 30; // Should be zero. Compensate if not. |
| 251 | if (vector_sum != 0) { |
| 252 | // Modify a few values early in |iat_vector_|. |
| 253 | int flip_sign = vector_sum > 0 ? -1 : 1; |
| 254 | IATVector::iterator it = iat_vector_.begin(); |
| 255 | while (it != iat_vector_.end() && abs(vector_sum) > 0) { |
| 256 | // Add/subtract 1/16 of the element, but not more than |vector_sum|. |
| 257 | int correction = flip_sign * std::min(abs(vector_sum), (*it) >> 4); |
| 258 | *it += correction; |
| 259 | vector_sum += correction; |
| 260 | ++it; |
| 261 | } |
| 262 | } |
| 263 | assert(vector_sum == 0); // Verify that the above is correct. |
| 264 | |
| 265 | // Update |iat_factor_| (changes only during the first seconds after a reset). |
| 266 | // The factor converges to |kIatFactor_|. |
| 267 | iat_factor_ += (kIatFactor_ - iat_factor_ + 3) >> 2; |
| 268 | } |
| 269 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 270 | // Enforces upper and lower limits for |target_level_|. The upper limit is |
| 271 | // chosen to be minimum of i) 75% of |max_packets_in_buffer_|, to leave some |
| 272 | // headroom for natural fluctuations around the target, and ii) equivalent of |
| 273 | // |maximum_delay_ms_| in packets. Note that in practice, if no |
| 274 | // |maximum_delay_ms_| is specified, this does not have any impact, since the |
| 275 | // target level is far below the buffer capacity in all reasonable cases. |
| 276 | // The lower limit is equivalent of |minimum_delay_ms_| in packets. We update |
| 277 | // |least_required_level_| while the above limits are applied. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 278 | // TODO(hlundin): Move this check to the buffer logistics class. |
| 279 | void DelayManager::LimitTargetLevel() { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 280 | if (packet_len_ms_ > 0 && minimum_delay_ms_ > 0) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 281 | int minimum_delay_packet_q8 = (minimum_delay_ms_ << 8) / packet_len_ms_; |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 282 | target_level_ = std::max(target_level_, minimum_delay_packet_q8); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 283 | } |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 284 | |
| 285 | if (maximum_delay_ms_ > 0 && packet_len_ms_ > 0) { |
| 286 | int maximum_delay_packet_q8 = (maximum_delay_ms_ << 8) / packet_len_ms_; |
| 287 | target_level_ = std::min(target_level_, maximum_delay_packet_q8); |
| 288 | } |
| 289 | |
| 290 | // Shift to Q8, then 75%.; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 291 | int max_buffer_packets_q8 = |
| 292 | static_cast<int>((3 * (max_packets_in_buffer_ << 8)) / 4); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 293 | target_level_ = std::min(target_level_, max_buffer_packets_q8); |
| 294 | |
| 295 | // Sanity check, at least 1 packet (in Q8). |
| 296 | target_level_ = std::max(target_level_, 1 << 8); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 299 | int DelayManager::CalculateTargetLevel(int iat_packets, bool reordered) { |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 300 | int limit_probability = forced_limit_probability_.value_or(kLimitProbability); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 301 | if (streaming_mode_) { |
| 302 | limit_probability = kLimitProbabilityStreaming; |
| 303 | } |
| 304 | |
| 305 | // Calculate target buffer level from inter-arrival time histogram. |
| 306 | // Find the |iat_index| for which the probability of observing an |
| 307 | // inter-arrival time larger than or equal to |iat_index| is less than or |
| 308 | // equal to |limit_probability|. The sought probability is estimated using |
| 309 | // the histogram as the reverse cumulant PDF, i.e., the sum of elements from |
| 310 | // the end up until |iat_index|. Now, since the sum of all elements is 1 |
| 311 | // (in Q30) by definition, and since the solution is often a low value for |
| 312 | // |iat_index|, it is more efficient to start with |sum| = 1 and subtract |
| 313 | // elements from the start of the histogram. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 314 | size_t index = 0; // Start from the beginning of |iat_vector_|. |
| 315 | int sum = 1 << 30; // Assign to 1 in Q30. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 316 | sum -= iat_vector_[index]; // Ensure that target level is >= 1. |
| 317 | |
| 318 | do { |
| 319 | // Subtract the probabilities one by one until the sum is no longer greater |
| 320 | // than limit_probability. |
| 321 | ++index; |
| 322 | sum -= iat_vector_[index]; |
| 323 | } while ((sum > limit_probability) && (index < iat_vector_.size() - 1)); |
| 324 | |
| 325 | // This is the base value for the target buffer level. |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 326 | int target_level = static_cast<int>(index); |
| 327 | base_target_level_ = static_cast<int>(index); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 328 | |
| 329 | // Update detector for delay peaks. |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 330 | bool delay_peak_found = |
| 331 | peak_detector_.Update(iat_packets, reordered, target_level); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 332 | if (delay_peak_found) { |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 333 | target_level = std::max(target_level, peak_detector_.MaxPeakHeight()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // Sanity check. |target_level| must be strictly positive. |
| 337 | target_level = std::max(target_level, 1); |
| 338 | // Scale to Q8 and assign to member variable. |
| 339 | target_level_ = target_level << 8; |
| 340 | return target_level_; |
| 341 | } |
| 342 | |
| 343 | int DelayManager::SetPacketAudioLength(int length_ms) { |
| 344 | if (length_ms <= 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 345 | RTC_LOG_F(LS_ERROR) << "length_ms = " << length_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 346 | return -1; |
| 347 | } |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 348 | if (frame_length_change_experiment_ && packet_len_ms_ != length_ms) { |
| 349 | iat_vector_ = ScaleHistogram(iat_vector_, packet_len_ms_, length_ms); |
| 350 | } |
| 351 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 352 | packet_len_ms_ = length_ms; |
| 353 | peak_detector_.SetPacketAudioLength(packet_len_ms_); |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 354 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 355 | last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove? |
| 356 | return 0; |
| 357 | } |
| 358 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 359 | void DelayManager::Reset() { |
| 360 | packet_len_ms_ = 0; // Packet size unknown. |
| 361 | streaming_mode_ = false; |
| 362 | peak_detector_.Reset(); |
| 363 | ResetHistogram(); // Resets target levels too. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 364 | iat_factor_ = 0; // Adapt the histogram faster for the first few packets. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 365 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
| 366 | max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 367 | iat_cumulative_sum_ = 0; |
| 368 | max_iat_cumulative_sum_ = 0; |
| 369 | last_pack_cng_or_dtmf_ = 1; |
| 370 | } |
| 371 | |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 372 | double DelayManager::EstimatedClockDriftPpm() const { |
| 373 | double sum = 0.0; |
| 374 | // Calculate the expected value based on the probabilities in |iat_vector_|. |
| 375 | for (size_t i = 0; i < iat_vector_.size(); ++i) { |
| 376 | sum += static_cast<double>(iat_vector_[i]) * i; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 377 | } |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 378 | // The probabilities in |iat_vector_| are in Q30. Divide by 1 << 30 to convert |
| 379 | // to Q0; subtract the nominal inter-arrival time (1) to make a zero |
| 380 | // clockdrift represent as 0; mulitply by 1000000 to produce parts-per-million |
| 381 | // (ppm). |
| 382 | return (sum / (1 << 30) - 1) * 1e6; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | bool DelayManager::PeakFound() const { |
| 386 | return peak_detector_.peak_found(); |
| 387 | } |
| 388 | |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 389 | void DelayManager::ResetPacketIatCount() { |
| 390 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 391 | } |
| 392 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 393 | // Note that |low_limit| and |higher_limit| are not assigned to |
| 394 | // |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this |
| 395 | // class. They are computed from |target_level_| and used for decision making. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 396 | void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const { |
| 397 | if (!lower_limit || !higher_limit) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 398 | RTC_LOG_F(LS_ERROR) << "NULL pointers supplied as input"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 399 | assert(false); |
| 400 | return; |
| 401 | } |
| 402 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 403 | int window_20ms = 0x7FFF; // Default large value for legacy bit-exactness. |
| 404 | if (packet_len_ms_ > 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 405 | window_20ms = (20 << 8) / packet_len_ms_; |
| 406 | } |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 407 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 408 | // |target_level_| is in Q8 already. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 409 | *lower_limit = (target_level_ * 3) / 4; |
| 410 | // |higher_limit| is equal to |target_level_|, but should at |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 411 | // least be 20 ms higher than |lower_limit_|. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 412 | *higher_limit = std::max(target_level_, *lower_limit + window_20ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | int DelayManager::TargetLevel() const { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 416 | return target_level_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 417 | } |
| 418 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 419 | void DelayManager::LastDecodedWasCngOrDtmf(bool it_was) { |
| 420 | if (it_was) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 421 | last_pack_cng_or_dtmf_ = 1; |
| 422 | } else if (last_pack_cng_or_dtmf_ != 0) { |
| 423 | last_pack_cng_or_dtmf_ = -1; |
| 424 | } |
| 425 | } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 426 | |
henrik.lundin | b8c55b1 | 2017-05-10 07:38:01 -0700 | [diff] [blame] | 427 | void DelayManager::RegisterEmptyPacket() { |
| 428 | ++last_seq_no_; |
| 429 | } |
| 430 | |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 431 | DelayManager::IATVector DelayManager::ScaleHistogram(const IATVector& histogram, |
| 432 | int old_packet_length, |
| 433 | int new_packet_length) { |
Ivo Creusen | 25eb28c | 2017-10-17 17:19:14 +0200 | [diff] [blame] | 434 | if (old_packet_length == 0) { |
| 435 | // If we don't know the previous frame length, don't make any changes to the |
| 436 | // histogram. |
| 437 | return histogram; |
| 438 | } |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 439 | RTC_DCHECK_GT(new_packet_length, 0); |
| 440 | RTC_DCHECK_EQ(old_packet_length % 10, 0); |
| 441 | RTC_DCHECK_EQ(new_packet_length % 10, 0); |
| 442 | IATVector new_histogram(histogram.size(), 0); |
Ivo Creusen | d95a7dd | 2017-12-11 16:47:48 +0100 | [diff] [blame] | 443 | int64_t acc = 0; |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 444 | int time_counter = 0; |
| 445 | size_t new_histogram_idx = 0; |
| 446 | for (size_t i = 0; i < histogram.size(); i++) { |
| 447 | acc += histogram[i]; |
| 448 | time_counter += old_packet_length; |
| 449 | // The bins should be scaled, to ensure the histogram still sums to one. |
Ivo Creusen | d95a7dd | 2017-12-11 16:47:48 +0100 | [diff] [blame] | 450 | const int64_t scaled_acc = acc * new_packet_length / time_counter; |
| 451 | int64_t actually_used_acc = 0; |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 452 | while (time_counter >= new_packet_length) { |
Ivo Creusen | d95a7dd | 2017-12-11 16:47:48 +0100 | [diff] [blame] | 453 | const int64_t old_histogram_val = new_histogram[new_histogram_idx]; |
| 454 | new_histogram[new_histogram_idx] = |
| 455 | rtc::saturated_cast<int>(old_histogram_val + scaled_acc); |
| 456 | actually_used_acc += new_histogram[new_histogram_idx] - old_histogram_val; |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 457 | new_histogram_idx = |
| 458 | std::min(new_histogram_idx + 1, new_histogram.size() - 1); |
| 459 | time_counter -= new_packet_length; |
| 460 | } |
| 461 | // Only subtract the part that was succesfully written to the new histogram. |
| 462 | acc -= actually_used_acc; |
| 463 | } |
| 464 | // If there is anything left in acc (due to rounding errors), add it to the |
Ivo Creusen | d95a7dd | 2017-12-11 16:47:48 +0100 | [diff] [blame] | 465 | // last bin. If we cannot add everything to the last bin we need to add as |
| 466 | // much as possible to the bins after the last bin (this is only possible |
| 467 | // when compressing a histogram). |
| 468 | while (acc > 0 && new_histogram_idx < new_histogram.size()) { |
| 469 | const int64_t old_histogram_val = new_histogram[new_histogram_idx]; |
| 470 | new_histogram[new_histogram_idx] = |
| 471 | rtc::saturated_cast<int>(old_histogram_val + acc); |
| 472 | acc -= new_histogram[new_histogram_idx] - old_histogram_val; |
| 473 | new_histogram_idx++; |
| 474 | } |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 475 | RTC_DCHECK_EQ(histogram.size(), new_histogram.size()); |
Ivo Creusen | d95a7dd | 2017-12-11 16:47:48 +0100 | [diff] [blame] | 476 | if (acc == 0) { |
| 477 | // If acc is non-zero, we were not able to add everything to the new |
| 478 | // histogram, so this check will not hold. |
| 479 | RTC_DCHECK_EQ(accumulate(histogram.begin(), histogram.end(), 0ll), |
| 480 | accumulate(new_histogram.begin(), new_histogram.end(), 0ll)); |
| 481 | } |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 482 | return new_histogram; |
| 483 | } |
| 484 | |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame^] | 485 | bool DelayManager::IsValidMinimumDelay(int delay_ms) { |
| 486 | const int q75 = |
| 487 | rtc::dchecked_cast<int>(3 * max_packets_in_buffer_ * packet_len_ms_ / 4); |
| 488 | return delay_ms >= 0 && |
| 489 | (maximum_delay_ms_ <= 0 || delay_ms <= maximum_delay_ms_) && |
| 490 | (packet_len_ms_ <= 0 || delay_ms <= q75); |
| 491 | } |
| 492 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 493 | bool DelayManager::SetMinimumDelay(int delay_ms) { |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame^] | 494 | if (!IsValidMinimumDelay(delay_ms)) { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 495 | return false; |
| 496 | } |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame^] | 497 | // Ensure that minimum_delay_ms is no bigger than base_min_target_delay_ms_ |
Jakob Ivarsson | 10403ae | 2018-11-27 15:45:20 +0100 | [diff] [blame] | 498 | minimum_delay_ms_ = std::max(delay_ms, base_min_target_delay_ms_); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
| 502 | bool DelayManager::SetMaximumDelay(int delay_ms) { |
| 503 | if (delay_ms == 0) { |
| 504 | // Zero input unsets the maximum delay. |
| 505 | maximum_delay_ms_ = 0; |
| 506 | return true; |
| 507 | } else if (delay_ms < minimum_delay_ms_ || delay_ms < packet_len_ms_) { |
| 508 | // Maximum delay shouldn't be less than minimum delay or less than a packet. |
| 509 | return false; |
| 510 | } |
| 511 | maximum_delay_ms_ = delay_ms; |
| 512 | return true; |
| 513 | } |
| 514 | |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame^] | 515 | bool DelayManager::SetBaseMinimumDelay(int delay_ms) { |
| 516 | if (!IsValidMinimumDelay(delay_ms)) { |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | base_min_target_delay_ms_ = delay_ms; |
| 521 | // Ensure that minimum_delay_ms is no bigger than base_min_target_delay_ms_ |
| 522 | minimum_delay_ms_ = std::max(delay_ms, base_min_target_delay_ms_); |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | int DelayManager::GetBaseMinimumDelay() const { |
| 527 | return base_min_target_delay_ms_; |
| 528 | } |
| 529 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 530 | int DelayManager::base_target_level() const { |
| 531 | return base_target_level_; |
| 532 | } |
| 533 | void DelayManager::set_streaming_mode(bool value) { |
| 534 | streaming_mode_ = value; |
| 535 | } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 536 | int DelayManager::last_pack_cng_or_dtmf() const { |
| 537 | return last_pack_cng_or_dtmf_; |
| 538 | } |
| 539 | |
| 540 | void DelayManager::set_last_pack_cng_or_dtmf(int value) { |
| 541 | last_pack_cng_or_dtmf_ = value; |
| 542 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 543 | } // namespace webrtc |