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