niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/timing/jitter_estimator.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <math.h> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 16 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 17 | #include <cstdint> |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 18 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 20 | #include "api/field_trials_view.h" |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 21 | #include "api/units/data_size.h" |
| 22 | #include "api/units/frequency.h" |
| 23 | #include "api/units/time_delta.h" |
| 24 | #include "api/units/timestamp.h" |
Rasmus Brandt | 2377226 | 2022-05-23 09:53:15 +0200 | [diff] [blame] | 25 | #include "modules/video_coding/timing/rtt_filter.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "system_wrappers/include/clock.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 30 | namespace { |
| 31 | static constexpr uint32_t kStartupDelaySamples = 30; |
| 32 | static constexpr int64_t kFsAccuStartupSamples = 5; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 33 | static constexpr Frequency kMaxFramerateEstimate = Frequency::Hertz(200); |
| 34 | static constexpr TimeDelta kNackCountTimeout = TimeDelta::Seconds(60); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 35 | static constexpr double kDefaultMaxTimestampDeviationInSigmas = 3.5; |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 36 | static constexpr double kDefaultAvgAndMaxFrameSize = 500; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 37 | |
| 38 | constexpr double kPhi = 0.97; |
| 39 | constexpr double kPsi = 0.9999; |
| 40 | constexpr uint32_t kAlphaCountMax = 400; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 41 | constexpr uint32_t kNackLimit = 3; |
| 42 | constexpr int32_t kNumStdDevDelayOutlier = 15; |
| 43 | constexpr int32_t kNumStdDevFrameSizeOutlier = 3; |
| 44 | // ~Less than 1% chance (look up in normal distribution table)... |
| 45 | constexpr double kNoiseStdDevs = 2.33; |
| 46 | // ...of getting 30 ms freezes |
| 47 | constexpr double kNoiseStdDevOffset = 30.0; |
| 48 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 49 | } // namespace |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 50 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 51 | JitterEstimator::JitterEstimator(Clock* clock, |
| 52 | const FieldTrialsView& field_trials) |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 53 | : fps_counter_(30), // TODO(sprang): Use an estimator with limit based on |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 54 | // time, rather than number of samples. |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 55 | clock_(clock) { |
| 56 | Reset(); |
| 57 | } |
| 58 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 59 | JitterEstimator::~JitterEstimator() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 60 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 61 | // Resets the JitterEstimate. |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 62 | void JitterEstimator::Reset() { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 63 | var_noise_ = 4.0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 64 | |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 65 | avg_frame_size_bytes_ = kDefaultAvgAndMaxFrameSize; |
| 66 | max_frame_size_bytes_ = kDefaultAvgAndMaxFrameSize; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 67 | var_frame_size_ = 100; |
| 68 | last_update_time_ = absl::nullopt; |
| 69 | prev_estimate_ = absl::nullopt; |
| 70 | prev_frame_size_ = absl::nullopt; |
| 71 | avg_noise_ = 0.0; |
| 72 | alpha_count_ = 1; |
| 73 | filter_jitter_estimate_ = TimeDelta::Zero(); |
| 74 | latest_nack_ = Timestamp::Zero(); |
| 75 | nack_count_ = 0; |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 76 | frame_size_sum_bytes_ = 0; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 77 | frame_size_count_ = 0; |
| 78 | startup_count_ = 0; |
| 79 | rtt_filter_.Reset(); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 80 | fps_counter_.Reset(); |
Rasmus Brandt | 39ae696 | 2022-08-09 14:40:05 +0200 | [diff] [blame] | 81 | |
Rasmus Brandt | ae4a832 | 2022-08-12 13:45:34 +0200 | [diff] [blame] | 82 | kalman_filter_ = FrameDelayDeltaKalmanFilter(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 85 | // Updates the estimates with the new measurements. |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 86 | void JitterEstimator::UpdateEstimate(TimeDelta frame_delay, |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 87 | DataSize frame_size) { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 88 | if (frame_size.IsZero()) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 89 | return; |
| 90 | } |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 91 | // Can't use DataSize since this can be negative. |
| 92 | double delta_frame_bytes = |
| 93 | frame_size.bytes() - prev_frame_size_.value_or(DataSize::Zero()).bytes(); |
| 94 | if (frame_size_count_ < kFsAccuStartupSamples) { |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 95 | frame_size_sum_bytes_ += frame_size.bytes(); |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 96 | frame_size_count_++; |
| 97 | } else if (frame_size_count_ == kFsAccuStartupSamples) { |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 98 | // Give the frame size filter. |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 99 | avg_frame_size_bytes_ = |
| 100 | frame_size_sum_bytes_ / static_cast<double>(frame_size_count_); |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 101 | frame_size_count_++; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 102 | } |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 103 | |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 104 | double avg_frame_size_bytes = |
| 105 | kPhi * avg_frame_size_bytes_ + (1 - kPhi) * frame_size.bytes(); |
| 106 | double deviation_size_bytes = 2 * sqrt(var_frame_size_); |
| 107 | if (frame_size.bytes() < avg_frame_size_bytes_ + deviation_size_bytes) { |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 108 | // Only update the average frame size if this sample wasn't a key frame. |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 109 | avg_frame_size_bytes_ = avg_frame_size_bytes; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 110 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 111 | |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 112 | double delta_bytes = frame_size.bytes() - avg_frame_size_bytes; |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 113 | var_frame_size_ = std::max( |
| 114 | kPhi * var_frame_size_ + (1 - kPhi) * (delta_bytes * delta_bytes), 1.0); |
| 115 | |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 116 | // Update max_frame_size_bytes_ estimate. |
| 117 | max_frame_size_bytes_ = |
| 118 | std::max<double>(kPsi * max_frame_size_bytes_, frame_size.bytes()); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 120 | if (!prev_frame_size_) { |
| 121 | prev_frame_size_ = frame_size; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 122 | return; |
| 123 | } |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 124 | prev_frame_size_ = frame_size; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 126 | // Cap frame_delay based on the current time deviation noise. |
Erik Språng | 93b107d | 2022-07-26 10:42:08 +0200 | [diff] [blame] | 127 | TimeDelta max_time_deviation = TimeDelta::Millis( |
| 128 | kDefaultMaxTimestampDeviationInSigmas * sqrt(var_noise_) + 0.5); |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 129 | frame_delay.Clamp(-max_time_deviation, max_time_deviation); |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 130 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 131 | // Only update the Kalman filter if the sample is not considered an extreme |
| 132 | // outlier. Even if it is an extreme outlier from a delay point of view, if |
| 133 | // the frame size also is large the deviation is probably due to an incorrect |
| 134 | // line slope. |
Rasmus Brandt | 39ae696 | 2022-08-09 14:40:05 +0200 | [diff] [blame] | 135 | double deviation = |
Rasmus Brandt | ae4a832 | 2022-08-12 13:45:34 +0200 | [diff] [blame] | 136 | frame_delay.ms() - |
| 137 | kalman_filter_.GetFrameDelayVariationEstimateTotal(delta_frame_bytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 139 | if (fabs(deviation) < kNumStdDevDelayOutlier * sqrt(var_noise_) || |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 140 | frame_size.bytes() > avg_frame_size_bytes_ + kNumStdDevFrameSizeOutlier * |
| 141 | sqrt(var_frame_size_)) { |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 142 | // Update the variance of the deviation from the line given by the Kalman |
| 143 | // filter. |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 144 | EstimateRandomJitter(deviation); |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 145 | // Prevent updating with frames which have been congested by a large frame, |
| 146 | // and therefore arrives almost at the same time as that frame. |
| 147 | // This can occur when we receive a large frame (key frame) which has been |
| 148 | // delayed. The next frame is of normal size (delta frame), and thus deltaFS |
| 149 | // will be << 0. This removes all frame samples which arrives after a key |
| 150 | // frame. |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 151 | if (delta_frame_bytes > -0.25 * max_frame_size_bytes_) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 152 | // Update the Kalman filter with the new data |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 153 | kalman_filter_.PredictAndUpdate(frame_delay.ms(), delta_frame_bytes, |
| 154 | max_frame_size_bytes_, var_noise_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 156 | } else { |
| 157 | int nStdDev = |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 158 | (deviation >= 0) ? kNumStdDevDelayOutlier : -kNumStdDevDelayOutlier; |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 159 | EstimateRandomJitter(nStdDev * sqrt(var_noise_)); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 160 | } |
| 161 | // Post process the total estimated jitter |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 162 | if (startup_count_ >= kStartupDelaySamples) { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 163 | PostProcessEstimate(); |
| 164 | } else { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 165 | startup_count_++; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 166 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 169 | // Updates the nack/packet ratio. |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 170 | void JitterEstimator::FrameNacked() { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 171 | if (nack_count_ < kNackLimit) { |
| 172 | nack_count_++; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 173 | } |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 174 | latest_nack_ = clock_->CurrentTime(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 177 | // Estimates the random jitter by calculating the variance of the sample |
| 178 | // distance from the line given by theta. |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 179 | void JitterEstimator::EstimateRandomJitter(double d_dT) { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 180 | Timestamp now = clock_->CurrentTime(); |
| 181 | if (last_update_time_.has_value()) { |
| 182 | fps_counter_.AddSample((now - *last_update_time_).us()); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 183 | } |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 184 | last_update_time_ = now; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 185 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 186 | if (alpha_count_ == 0) { |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 187 | RTC_DCHECK_NOTREACHED(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | double alpha = |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 191 | static_cast<double>(alpha_count_ - 1) / static_cast<double>(alpha_count_); |
| 192 | alpha_count_++; |
| 193 | if (alpha_count_ > kAlphaCountMax) |
| 194 | alpha_count_ = kAlphaCountMax; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 195 | |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 196 | // In order to avoid a low frame rate stream to react slower to changes, |
| 197 | // scale the alpha weight relative a 30 fps stream. |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 198 | Frequency fps = GetFrameRate(); |
| 199 | if (fps > Frequency::Zero()) { |
| 200 | constexpr Frequency k30Fps = Frequency::Hertz(30); |
| 201 | double rate_scale = k30Fps / fps; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 202 | // At startup, there can be a lot of noise in the fps estimate. |
| 203 | // Interpolate rate_scale linearly, from 1.0 at sample #1, to 30.0 / fps |
| 204 | // at sample #kStartupDelaySamples. |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 205 | if (alpha_count_ < kStartupDelaySamples) { |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 206 | rate_scale = |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 207 | (alpha_count_ * rate_scale + (kStartupDelaySamples - alpha_count_)) / |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 208 | kStartupDelaySamples; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 209 | } |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 210 | alpha = pow(alpha, rate_scale); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 213 | double avgNoise = alpha * avg_noise_ + (1 - alpha) * d_dT; |
| 214 | double varNoise = alpha * var_noise_ + |
| 215 | (1 - alpha) * (d_dT - avg_noise_) * (d_dT - avg_noise_); |
philipel | e1c707c | 2022-07-05 14:03:25 +0200 | [diff] [blame] | 216 | avg_noise_ = avgNoise; |
| 217 | var_noise_ = varNoise; |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 218 | if (var_noise_ < 1.0) { |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 219 | // The variance should never be zero, since we might get stuck and consider |
| 220 | // all samples as outliers. |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 221 | var_noise_ = 1.0; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 222 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 225 | double JitterEstimator::NoiseThreshold() const { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 226 | double noiseThreshold = kNoiseStdDevs * sqrt(var_noise_) - kNoiseStdDevOffset; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 227 | if (noiseThreshold < 1.0) { |
| 228 | noiseThreshold = 1.0; |
| 229 | } |
| 230 | return noiseThreshold; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 233 | // Calculates the current jitter estimate from the filtered estimates. |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 234 | TimeDelta JitterEstimator::CalculateEstimate() { |
Rasmus Brandt | ae4a832 | 2022-08-12 13:45:34 +0200 | [diff] [blame] | 235 | double retMs = kalman_filter_.GetFrameDelayVariationEstimateSizeBased( |
philipel | 55a9a3d | 2022-08-19 13:23:26 +0200 | [diff] [blame] | 236 | max_frame_size_bytes_ - avg_frame_size_bytes_) + |
Rasmus Brandt | 39ae696 | 2022-08-09 14:40:05 +0200 | [diff] [blame] | 237 | NoiseThreshold(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 238 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 239 | TimeDelta ret = TimeDelta::Millis(retMs); |
| 240 | |
Johannes Kron | ef4a2cf | 2022-08-10 22:39:51 +0000 | [diff] [blame] | 241 | constexpr TimeDelta kMinEstimate = TimeDelta::Millis(1); |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 242 | constexpr TimeDelta kMaxEstimate = TimeDelta::Seconds(10); |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 243 | // A very low estimate (or negative) is neglected. |
Johannes Kron | ef4a2cf | 2022-08-10 22:39:51 +0000 | [diff] [blame] | 244 | if (ret < kMinEstimate) { |
| 245 | ret = prev_estimate_.value_or(kMinEstimate); |
| 246 | // Sanity check to make sure that no other method has set `prev_estimate_` |
| 247 | // to a value lower than `kMinEstimate`. |
| 248 | RTC_DCHECK_GE(ret, kMinEstimate); |
| 249 | } else if (ret > kMaxEstimate) { // Sanity |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 250 | ret = kMaxEstimate; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 251 | } |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 252 | prev_estimate_ = ret; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 253 | return ret; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 256 | void JitterEstimator::PostProcessEstimate() { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 257 | filter_jitter_estimate_ = CalculateEstimate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 260 | void JitterEstimator::UpdateRtt(TimeDelta rtt) { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 261 | rtt_filter_.Update(rtt); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 262 | } |
| 263 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | // Returns the current filtered estimate if available, |
| 265 | // otherwise tries to calculate an estimate. |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 266 | TimeDelta JitterEstimator::GetJitterEstimate( |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 267 | double rtt_multiplier, |
| 268 | absl::optional<TimeDelta> rtt_mult_add_cap) { |
| 269 | TimeDelta jitter = CalculateEstimate() + OPERATING_SYSTEM_JITTER; |
| 270 | Timestamp now = clock_->CurrentTime(); |
Gustavo Garcia | f7a7c8a | 2018-01-08 15:23:38 +0100 | [diff] [blame] | 271 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 272 | if (now - latest_nack_ > kNackCountTimeout) |
| 273 | nack_count_ = 0; |
Gustavo Garcia | f7a7c8a | 2018-01-08 15:23:38 +0100 | [diff] [blame] | 274 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 275 | if (filter_jitter_estimate_ > jitter) |
| 276 | jitter = filter_jitter_estimate_; |
| 277 | if (nack_count_ >= kNackLimit) { |
| 278 | if (rtt_mult_add_cap.has_value()) { |
| 279 | jitter += std::min(rtt_filter_.Rtt() * rtt_multiplier, |
| 280 | rtt_mult_add_cap.value()); |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 281 | } else { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 282 | jitter += rtt_filter_.Rtt() * rtt_multiplier; |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 285 | |
Erik Språng | 23370f2 | 2022-07-27 14:09:02 +0200 | [diff] [blame] | 286 | static const Frequency kJitterScaleLowThreshold = Frequency::Hertz(5); |
| 287 | static const Frequency kJitterScaleHighThreshold = Frequency::Hertz(10); |
| 288 | Frequency fps = GetFrameRate(); |
| 289 | // Ignore jitter for very low fps streams. |
| 290 | if (fps < kJitterScaleLowThreshold) { |
| 291 | if (fps.IsZero()) { |
| 292 | return std::max(TimeDelta::Zero(), jitter); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 293 | } |
Erik Språng | 23370f2 | 2022-07-27 14:09:02 +0200 | [diff] [blame] | 294 | return TimeDelta::Zero(); |
| 295 | } |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 296 | |
Erik Språng | 23370f2 | 2022-07-27 14:09:02 +0200 | [diff] [blame] | 297 | // Semi-low frame rate; scale by factor linearly interpolated from 0.0 at |
| 298 | // kJitterScaleLowThreshold to 1.0 at kJitterScaleHighThreshold. |
| 299 | if (fps < kJitterScaleHighThreshold) { |
| 300 | jitter = (1.0 / (kJitterScaleHighThreshold - kJitterScaleLowThreshold)) * |
| 301 | (fps - kJitterScaleLowThreshold) * jitter; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 302 | } |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 303 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 304 | return std::max(TimeDelta::Zero(), jitter); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Rasmus Brandt | 10944e6 | 2022-05-25 10:12:42 +0200 | [diff] [blame] | 307 | Frequency JitterEstimator::GetFrameRate() const { |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 308 | TimeDelta mean_frame_period = TimeDelta::Micros(fps_counter_.ComputeMean()); |
| 309 | if (mean_frame_period <= TimeDelta::Zero()) |
| 310 | return Frequency::Zero(); |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 311 | |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 312 | Frequency fps = 1 / mean_frame_period; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 313 | // Sanity check. |
Evan Shrubsole | 13e42a8 | 2022-03-07 13:21:51 +0100 | [diff] [blame] | 314 | RTC_DCHECK_GE(fps, Frequency::Zero()); |
| 315 | return std::min(fps, kMaxFramerateEstimate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 316 | } |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 317 | } // namespace webrtc |