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