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