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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_ |
| 12 | #define MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/video_coding/rtt_filter.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 15 | #include "rtc_base/rolling_accumulator.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 17 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 19 | class Clock; |
| 20 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 21 | class VCMJitterEstimator { |
| 22 | public: |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 23 | explicit VCMJitterEstimator(Clock* clock); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 24 | virtual ~VCMJitterEstimator(); |
| 25 | VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 27 | // Resets the estimate to the initial state. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 28 | void Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 30 | // Updates the jitter estimate with the new data. |
| 31 | // |
| 32 | // Input: |
| 33 | // - frameDelay : Delay-delta calculated by UTILDelayEstimate in |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 34 | // milliseconds. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 35 | // - frameSize : Frame size of the current frame. |
| 36 | // - incompleteFrame : Flags if the frame is used to update the |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 37 | // estimate before it was complete. |
| 38 | // Default is false. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 39 | void UpdateEstimate(int64_t frameDelayMS, |
| 40 | uint32_t frameSizeBytes, |
| 41 | bool incompleteFrame = false); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 43 | // Returns the current jitter estimate in milliseconds and adds an RTT |
| 44 | // dependent term in cases of retransmission. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 45 | // Input: |
| 46 | // - rttMultiplier : RTT param multiplier (when applicable). |
| 47 | // |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 48 | // Return value : Jitter estimate in milliseconds. |
“Michael | e0f3704 | 2019-06-04 10:04:12 -0500 | [diff] [blame] | 49 | virtual int GetJitterEstimate(double rttMultiplier, |
| 50 | absl::optional<double> rttMultAddCapMs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 52 | // Updates the nack counter. |
| 53 | void FrameNacked(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 55 | // Updates the RTT filter. |
| 56 | // |
| 57 | // Input: |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 58 | // - rttMs : RTT in ms. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 59 | void UpdateRtt(int64_t rttMs); |
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 | // A constant describing the delay from the jitter buffer to the delay on the |
| 62 | // receiving side which is not accounted for by the jitter buffer nor the |
| 63 | // decoding delay estimate. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 64 | static const uint32_t OPERATING_SYSTEM_JITTER = 10; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 66 | protected: |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 67 | // These are protected for better testing possibilities. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 68 | double _theta[2]; // Estimated line parameters (slope, offset) |
| 69 | double _varNoise; // Variance of the time-deviation from the line |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 71 | private: |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 72 | // Updates the Kalman filter for the line describing the frame size dependent |
| 73 | // jitter. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 74 | // |
| 75 | // Input: |
| 76 | // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 77 | // milliseconds. |
| 78 | // - deltaFSBytes : Frame size delta, i.e. frame size at time T |
| 79 | // : minus frame size at time T-1. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 80 | void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 82 | // Updates the random jitter estimate, i.e. the variance of the time |
| 83 | // deviations from the line given by the Kalman filter. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 84 | // |
| 85 | // Input: |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 86 | // - d_dT : The deviation from the kalman estimate. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 87 | // - incompleteFrame : True if the frame used to update the |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 88 | // estimate with was incomplete. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 89 | void EstimateRandomJitter(double d_dT, bool incompleteFrame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 91 | double NoiseThreshold() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 93 | // Calculates the current jitter estimate. |
| 94 | // |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 95 | // Return value : The current jitter estimate in milliseconds. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 96 | double CalculateEstimate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 98 | // Post process the calculated estimate. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 99 | void PostProcessEstimate(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 100 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 101 | // Calculates the difference in delay between a sample and the expected delay |
| 102 | // estimated by the Kalman filter. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 103 | // |
| 104 | // Input: |
| 105 | // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 106 | // milliseconds. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 107 | // - deltaFS : Frame size delta, i.e. frame size at time |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 108 | // T minus frame size at time T-1. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | // |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 110 | // Return value : The difference in milliseconds. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 111 | double DeviationFromExpectedDelay(int64_t frameDelayMS, |
| 112 | int32_t deltaFSBytes) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 114 | double GetFrameRate() const; |
sprang@webrtc.org | 70e2d11 | 2014-09-24 14:06:56 +0000 | [diff] [blame] | 115 | |
Åsa Persson | 3fcc5be | 2019-04-04 09:40:27 +0200 | [diff] [blame] | 116 | // Constants, filter parameters. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 117 | const double _phi; |
| 118 | const double _psi; |
| 119 | const uint32_t _alphaCountMax; |
| 120 | const double _thetaLow; |
| 121 | const uint32_t _nackLimit; |
| 122 | const int32_t _numStdDevDelayOutlier; |
| 123 | const int32_t _numStdDevFrameSizeOutlier; |
| 124 | const double _noiseStdDevs; |
| 125 | const double _noiseStdDevOffset; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 127 | double _thetaCov[2][2]; // Estimate covariance |
| 128 | double _Qcov[2][2]; // Process noise covariance |
| 129 | double _avgFrameSize; // Average frame size |
| 130 | double _varFrameSize; // Frame size variance |
| 131 | double _maxFrameSize; // Largest frame size received (descending |
| 132 | // with a factor _psi) |
| 133 | uint32_t _fsSum; |
| 134 | uint32_t _fsCount; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 136 | int64_t _lastUpdateT; |
| 137 | double _prevEstimate; // The previously returned jitter estimate |
| 138 | uint32_t _prevFrameSize; // Frame size of the previous frame |
| 139 | double _avgNoise; // Average of the random jitter |
| 140 | uint32_t _alphaCount; |
| 141 | double _filterJitterEstimate; // The filtered sum of jitter estimates |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 143 | uint32_t _startupCount; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 145 | int64_t |
| 146 | _latestNackTimestamp; // Timestamp in ms when the latest nack was seen |
| 147 | uint32_t _nackCount; // Keeps track of the number of nacks received, |
| 148 | // but never goes above _nackLimit |
| 149 | VCMRttFilter _rttFilter; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 150 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 151 | rtc::RollingAccumulator<uint64_t> fps_counter_; |
Erik Språng | b1e031a | 2018-11-01 11:20:49 +0100 | [diff] [blame] | 152 | const double time_deviation_upper_bound_; |
Sebastian Jansson | aa01f27 | 2019-01-30 11:28:59 +0100 | [diff] [blame] | 153 | Clock* clock_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 156 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 158 | #endif // MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_ |