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 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 20 | #include "absl/memory/memory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "modules/audio_coding/neteq/delay_peak_detector.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 22 | #include "modules/include/module_common_types_public.h" |
| 23 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 25 | #include "rtc_base/numerics/safe_conversions.h" |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 26 | #include "rtc_base/numerics/safe_minmax.h" |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 27 | #include "system_wrappers/include/field_trial.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 28 | |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 29 | namespace { |
| 30 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 31 | constexpr int kLimitProbability = 1020054733; // 19/20 in Q30. |
| 32 | constexpr int kLimitProbabilityStreaming = 1073204953; // 1999/2000 in Q30. |
| 33 | constexpr int kMaxStreamingPeakPeriodMs = 600000; // 10 minutes in ms. |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 34 | constexpr int kCumulativeSumDrift = 2; // Drift term for cumulative sum |
| 35 | // |iat_cumulative_sum_|. |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 36 | constexpr int kMinBaseMinimumDelayMs = 0; |
| 37 | constexpr int kMaxBaseMinimumDelayMs = 10000; |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 38 | constexpr int kIatFactor = 32745; // 0.9993 in Q15. |
| 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) { |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 54 | return absl::make_optional<int>( |
| 55 | static_cast<int>((1 << 30) * percentile / 100.0 + 0.5)); // in Q30. |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 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, |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 73 | const TickTimer* tick_timer, |
| 74 | std::unique_ptr<Histogram> iat_histogram) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 75 | : first_packet_received_(false), |
| 76 | max_packets_in_buffer_(max_packets_in_buffer), |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 77 | iat_histogram_(std::move(iat_histogram)), |
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 | 76a74e6 | 2019-02-22 19:28:02 +0100 | [diff] [blame] | 87 | minimum_delay_ms_(0), |
Ruslan Burakov | b35bacc | 2019-02-20 13:41:59 +0100 | [diff] [blame] | 88 | maximum_delay_ms_(0), |
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. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 98 | RTC_CHECK(iat_histogram_); |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 99 | RTC_DCHECK_GE(base_minimum_delay_ms_, 0); |
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 | |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 104 | std::unique_ptr<DelayManager> DelayManager::Create( |
| 105 | size_t max_packets_in_buffer, |
| 106 | int base_minimum_delay_ms, |
| 107 | bool enable_rtx_handling, |
| 108 | DelayPeakDetector* peak_detector, |
| 109 | const TickTimer* tick_timer) { |
| 110 | return absl::make_unique<DelayManager>( |
| 111 | max_packets_in_buffer, base_minimum_delay_ms, enable_rtx_handling, |
| 112 | peak_detector, tick_timer, |
| 113 | absl::make_unique<Histogram>(kMaxIat + 1, kIatFactor)); |
| 114 | } |
| 115 | |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 116 | DelayManager::~DelayManager() {} |
| 117 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
Jakob Ivarsson | e98954c | 2019-02-06 15:37:50 +0100 | [diff] [blame] | 149 | bool reordered = false; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 150 | if (packet_len_ms > 0) { |
| 151 | // Cannot update statistics unless |packet_len_ms| is valid. |
| 152 | // Calculate inter-arrival time (IAT) in integer "packet times" |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 153 | // (rounding down). This is the value added to the inter-arrival time |
| 154 | // histogram. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 155 | int iat_packets = packet_iat_stopwatch_->ElapsedMs() / packet_len_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 156 | |
| 157 | if (streaming_mode_) { |
| 158 | UpdateCumulativeSums(packet_len_ms, sequence_number); |
| 159 | } |
| 160 | |
| 161 | // Check for discontinuous packet sequence and re-ordering. |
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. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 174 | iat_packets = std::min(iat_packets, iat_histogram_->NumBuckets() - 1); |
| 175 | iat_histogram_->Add(iat_packets); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 176 | // Calculate new |target_level_| based on updated statistics. |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 177 | target_level_ = CalculateTargetLevel(iat_packets, reordered); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 178 | if (streaming_mode_) { |
| 179 | target_level_ = std::max(target_level_, max_iat_cumulative_sum_); |
| 180 | } |
| 181 | |
| 182 | LimitTargetLevel(); |
| 183 | } // End if (packet_len_ms > 0). |
| 184 | |
Jakob Ivarsson | e98954c | 2019-02-06 15:37:50 +0100 | [diff] [blame] | 185 | if (enable_rtx_handling_ && reordered && |
| 186 | num_reordered_packets_ < kMaxReorderedPackets) { |
| 187 | ++num_reordered_packets_; |
| 188 | return 0; |
| 189 | } |
| 190 | num_reordered_packets_ = 0; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 191 | // Prepare for next packet arrival. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 192 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 193 | last_seq_no_ = sequence_number; |
| 194 | last_timestamp_ = timestamp; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | void DelayManager::UpdateCumulativeSums(int packet_len_ms, |
| 199 | uint16_t sequence_number) { |
| 200 | // Calculate IAT in Q8, including fractions of a packet (i.e., more |
| 201 | // accurate than |iat_packets|. |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 202 | int iat_packets_q8 = |
| 203 | (packet_iat_stopwatch_->ElapsedMs() << 8) / packet_len_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | // Calculate cumulative sum IAT with sequence number compensation. The sum |
| 205 | // is zero if there is no clock-drift. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 206 | iat_cumulative_sum_ += |
| 207 | (iat_packets_q8 - |
| 208 | (static_cast<int>(sequence_number - last_seq_no_) << 8)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | // Subtract drift term. |
| 210 | iat_cumulative_sum_ -= kCumulativeSumDrift; |
| 211 | // Ensure not negative. |
| 212 | iat_cumulative_sum_ = std::max(iat_cumulative_sum_, 0); |
| 213 | if (iat_cumulative_sum_ > max_iat_cumulative_sum_) { |
| 214 | // Found a new maximum. |
| 215 | max_iat_cumulative_sum_ = iat_cumulative_sum_; |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 216 | max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 217 | } |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 218 | if (max_iat_stopwatch_->ElapsedMs() > kMaxStreamingPeakPeriodMs) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 219 | // Too long since the last maximum was observed; decrease max value. |
| 220 | max_iat_cumulative_sum_ -= kCumulativeSumDrift; |
| 221 | } |
| 222 | } |
| 223 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 224 | // Enforces upper and lower limits for |target_level_|. The upper limit is |
| 225 | // chosen to be minimum of i) 75% of |max_packets_in_buffer_|, to leave some |
| 226 | // headroom for natural fluctuations around the target, and ii) equivalent of |
| 227 | // |maximum_delay_ms_| in packets. Note that in practice, if no |
| 228 | // |maximum_delay_ms_| is specified, this does not have any impact, since the |
| 229 | // target level is far below the buffer capacity in all reasonable cases. |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 230 | // The lower limit is equivalent of |effective_minimum_delay_ms_| in packets. |
| 231 | // 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] | 232 | // TODO(hlundin): Move this check to the buffer logistics class. |
| 233 | void DelayManager::LimitTargetLevel() { |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 234 | if (packet_len_ms_ > 0 && effective_minimum_delay_ms_ > 0) { |
| 235 | int minimum_delay_packet_q8 = |
| 236 | (effective_minimum_delay_ms_ << 8) / packet_len_ms_; |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 237 | target_level_ = std::max(target_level_, minimum_delay_packet_q8); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 238 | } |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 239 | |
| 240 | if (maximum_delay_ms_ > 0 && packet_len_ms_ > 0) { |
| 241 | int maximum_delay_packet_q8 = (maximum_delay_ms_ << 8) / packet_len_ms_; |
| 242 | target_level_ = std::min(target_level_, maximum_delay_packet_q8); |
| 243 | } |
| 244 | |
| 245 | // Shift to Q8, then 75%.; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 246 | int max_buffer_packets_q8 = |
| 247 | static_cast<int>((3 * (max_packets_in_buffer_ << 8)) / 4); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 248 | target_level_ = std::min(target_level_, max_buffer_packets_q8); |
| 249 | |
| 250 | // Sanity check, at least 1 packet (in Q8). |
| 251 | target_level_ = std::max(target_level_, 1 << 8); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 254 | int DelayManager::CalculateTargetLevel(int iat_packets, bool reordered) { |
Minyue Li | 002fbb8 | 2018-10-04 11:31:03 +0200 | [diff] [blame] | 255 | int limit_probability = forced_limit_probability_.value_or(kLimitProbability); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 256 | if (streaming_mode_) { |
| 257 | limit_probability = kLimitProbabilityStreaming; |
| 258 | } |
| 259 | |
| 260 | // Calculate target buffer level from inter-arrival time histogram. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 261 | // This is the base value for the target buffer level. |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 262 | int target_level = iat_histogram_->Quantile(limit_probability); |
| 263 | base_target_level_ = target_level; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 264 | |
| 265 | // Update detector for delay peaks. |
Jakob Ivarsson | 39b934b | 2019-01-10 10:28:23 +0100 | [diff] [blame] | 266 | bool delay_peak_found = |
| 267 | peak_detector_.Update(iat_packets, reordered, target_level); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 268 | if (delay_peak_found) { |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 269 | target_level = std::max(target_level, peak_detector_.MaxPeakHeight()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | // Sanity check. |target_level| must be strictly positive. |
| 273 | target_level = std::max(target_level, 1); |
| 274 | // Scale to Q8 and assign to member variable. |
| 275 | target_level_ = target_level << 8; |
| 276 | return target_level_; |
| 277 | } |
| 278 | |
| 279 | int DelayManager::SetPacketAudioLength(int length_ms) { |
| 280 | if (length_ms <= 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 281 | RTC_LOG_F(LS_ERROR) << "length_ms = " << length_ms; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 282 | return -1; |
| 283 | } |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 284 | if (frame_length_change_experiment_ && packet_len_ms_ != length_ms && |
| 285 | packet_len_ms_ > 0) { |
| 286 | iat_histogram_->Scale(packet_len_ms_, length_ms); |
Ivo Creusen | 385b10b | 2017-10-13 12:37:27 +0200 | [diff] [blame] | 287 | } |
| 288 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 289 | packet_len_ms_ = length_ms; |
| 290 | peak_detector_.SetPacketAudioLength(packet_len_ms_); |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 291 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 292 | last_pack_cng_or_dtmf_ = 1; // TODO(hlundin): Legacy. Remove? |
| 293 | return 0; |
| 294 | } |
| 295 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 296 | void DelayManager::Reset() { |
| 297 | packet_len_ms_ = 0; // Packet size unknown. |
| 298 | streaming_mode_ = false; |
| 299 | peak_detector_.Reset(); |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 300 | iat_histogram_->Reset(); |
| 301 | base_target_level_ = 4; |
| 302 | target_level_ = base_target_level_ << 8; |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 303 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
| 304 | max_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 305 | iat_cumulative_sum_ = 0; |
| 306 | max_iat_cumulative_sum_ = 0; |
| 307 | last_pack_cng_or_dtmf_ = 1; |
| 308 | } |
| 309 | |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 310 | double DelayManager::EstimatedClockDriftPpm() const { |
| 311 | double sum = 0.0; |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 312 | // Calculate the expected value based on the probabilities in |
| 313 | // |iat_histogram_|. |
| 314 | auto buckets = iat_histogram_->buckets(); |
| 315 | for (size_t i = 0; i < buckets.size(); ++i) { |
| 316 | sum += static_cast<double>(buckets[i]) * i; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 317 | } |
Jakob Ivarsson | 1eb3d7e | 2019-02-21 15:42:31 +0100 | [diff] [blame] | 318 | // The probabilities in |iat_histogram_| are in Q30. Divide by 1 << 30 to |
| 319 | // convert to Q0; subtract the nominal inter-arrival time (1) to make a zero |
henrik.lundin | 0d83857 | 2016-10-13 03:35:55 -0700 | [diff] [blame] | 320 | // clockdrift represent as 0; mulitply by 1000000 to produce parts-per-million |
| 321 | // (ppm). |
| 322 | return (sum / (1 << 30) - 1) * 1e6; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | bool DelayManager::PeakFound() const { |
| 326 | return peak_detector_.peak_found(); |
| 327 | } |
| 328 | |
henrik.lundin | 8f8c96d | 2016-04-28 23:19:20 -0700 | [diff] [blame] | 329 | void DelayManager::ResetPacketIatCount() { |
| 330 | packet_iat_stopwatch_ = tick_timer_->GetNewStopwatch(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 333 | // Note that |low_limit| and |higher_limit| are not assigned to |
| 334 | // |minimum_delay_ms_| and |maximum_delay_ms_| defined by the client of this |
| 335 | // 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] | 336 | void DelayManager::BufferLimits(int* lower_limit, int* higher_limit) const { |
| 337 | if (!lower_limit || !higher_limit) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 338 | RTC_LOG_F(LS_ERROR) << "NULL pointers supplied as input"; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 339 | assert(false); |
| 340 | return; |
| 341 | } |
| 342 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 343 | int window_20ms = 0x7FFF; // Default large value for legacy bit-exactness. |
| 344 | if (packet_len_ms_ > 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 345 | window_20ms = (20 << 8) / packet_len_ms_; |
| 346 | } |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 347 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 348 | // |target_level_| is in Q8 already. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 349 | *lower_limit = (target_level_ * 3) / 4; |
| 350 | // |higher_limit| is equal to |target_level_|, but should at |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 351 | // least be 20 ms higher than |lower_limit_|. |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 352 | *higher_limit = std::max(target_level_, *lower_limit + window_20ms); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | int DelayManager::TargetLevel() const { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 356 | return target_level_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 357 | } |
| 358 | |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 359 | void DelayManager::LastDecodedWasCngOrDtmf(bool it_was) { |
| 360 | if (it_was) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 361 | last_pack_cng_or_dtmf_ = 1; |
| 362 | } else if (last_pack_cng_or_dtmf_ != 0) { |
| 363 | last_pack_cng_or_dtmf_ = -1; |
| 364 | } |
| 365 | } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 366 | |
henrik.lundin | b8c55b1 | 2017-05-10 07:38:01 -0700 | [diff] [blame] | 367 | void DelayManager::RegisterEmptyPacket() { |
| 368 | ++last_seq_no_; |
| 369 | } |
| 370 | |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 371 | bool DelayManager::IsValidMinimumDelay(int delay_ms) const { |
| 372 | return 0 <= delay_ms && delay_ms <= MinimumDelayUpperBound(); |
| 373 | } |
| 374 | |
| 375 | bool DelayManager::IsValidBaseMinimumDelay(int delay_ms) const { |
| 376 | return kMinBaseMinimumDelayMs <= delay_ms && |
| 377 | delay_ms <= kMaxBaseMinimumDelayMs; |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 378 | } |
| 379 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 380 | bool DelayManager::SetMinimumDelay(int delay_ms) { |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 381 | if (!IsValidMinimumDelay(delay_ms)) { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 382 | return false; |
| 383 | } |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 384 | |
| 385 | minimum_delay_ms_ = delay_ms; |
| 386 | UpdateEffectiveMinimumDelay(); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
| 390 | bool DelayManager::SetMaximumDelay(int delay_ms) { |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 391 | // If |delay_ms| is zero then it unsets the maximum delay and target level is |
| 392 | // unconstrained by maximum delay. |
| 393 | if (delay_ms != 0 && |
| 394 | (delay_ms < minimum_delay_ms_ || delay_ms < packet_len_ms_)) { |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 395 | // Maximum delay shouldn't be less than minimum delay or less than a packet. |
| 396 | return false; |
| 397 | } |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 398 | |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 399 | maximum_delay_ms_ = delay_ms; |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 400 | UpdateEffectiveMinimumDelay(); |
turaj@webrtc.org | f1efc57 | 2013-08-16 23:44:24 +0000 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 404 | bool DelayManager::SetBaseMinimumDelay(int delay_ms) { |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 405 | if (!IsValidBaseMinimumDelay(delay_ms)) { |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 409 | base_minimum_delay_ms_ = delay_ms; |
| 410 | UpdateEffectiveMinimumDelay(); |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
| 414 | int DelayManager::GetBaseMinimumDelay() const { |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 415 | return base_minimum_delay_ms_; |
Ruslan Burakov | edbea46 | 2019-02-04 16:17:31 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 418 | int DelayManager::base_target_level() const { |
| 419 | return base_target_level_; |
| 420 | } |
| 421 | void DelayManager::set_streaming_mode(bool value) { |
| 422 | streaming_mode_ = value; |
| 423 | } |
pbos@webrtc.org | 2d1a55c | 2013-07-31 15:54:00 +0000 | [diff] [blame] | 424 | int DelayManager::last_pack_cng_or_dtmf() const { |
| 425 | return last_pack_cng_or_dtmf_; |
| 426 | } |
| 427 | |
| 428 | void DelayManager::set_last_pack_cng_or_dtmf(int value) { |
| 429 | last_pack_cng_or_dtmf_ = value; |
| 430 | } |
Ruslan Burakov | 4a68fb9 | 2019-02-13 14:25:39 +0100 | [diff] [blame] | 431 | |
| 432 | void DelayManager::UpdateEffectiveMinimumDelay() { |
| 433 | // Clamp |base_minimum_delay_ms_| into the range which can be effectively |
| 434 | // used. |
| 435 | const int base_minimum_delay_ms = |
| 436 | rtc::SafeClamp(base_minimum_delay_ms_, 0, MinimumDelayUpperBound()); |
| 437 | effective_minimum_delay_ms_ = |
| 438 | std::max(minimum_delay_ms_, base_minimum_delay_ms); |
| 439 | } |
| 440 | |
| 441 | int DelayManager::MinimumDelayUpperBound() const { |
| 442 | // Choose the lowest possible bound discarding 0 cases which mean the value |
| 443 | // is not set and unconstrained. |
| 444 | int q75 = MaxBufferTimeQ75(); |
| 445 | q75 = q75 > 0 ? q75 : kMaxBaseMinimumDelayMs; |
| 446 | const int maximum_delay_ms = |
| 447 | maximum_delay_ms_ > 0 ? maximum_delay_ms_ : kMaxBaseMinimumDelayMs; |
| 448 | return std::min(maximum_delay_ms, q75); |
| 449 | } |
| 450 | |
| 451 | int DelayManager::MaxBufferTimeQ75() const { |
| 452 | const int max_buffer_time = max_packets_in_buffer_ * packet_len_ms_; |
| 453 | return rtc::dchecked_cast<int>(3 * max_buffer_time / 4); |
| 454 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 455 | } // namespace webrtc |