2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
14 #include "webrtc/base/rollingaccumulator.h"
15 #include "webrtc/modules/video_coding/main/source/rtt_filter.h"
16 #include "webrtc/typedefs.h"
23 class VCMJitterEstimator
26 VCMJitterEstimator(const Clock* clock,
28 int32_t receiverId = 0);
29 virtual ~VCMJitterEstimator();
30 VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs);
32 // Resets the estimate to the initial state
34 void ResetNackCount();
36 // Updates the jitter estimate with the new data.
39 // - frameDelay : Delay-delta calculated by UTILDelayEstimate in milliseconds
40 // - frameSize : Frame size of the current frame.
41 // - incompleteFrame : Flags if the frame is used to update the estimate before it
42 // was complete. Default is false.
43 void UpdateEstimate(int64_t frameDelayMS,
44 uint32_t frameSizeBytes,
45 bool incompleteFrame = false);
47 // Returns the current jitter estimate in milliseconds and adds
48 // also adds an RTT dependent term in cases of retransmission.
50 // - rttMultiplier : RTT param multiplier (when applicable).
52 // Return value : Jitter estimate in milliseconds
53 int GetJitterEstimate(double rttMultiplier);
55 // Updates the nack counter.
58 // Updates the RTT filter.
61 // - rttMs : RTT in ms
62 void UpdateRtt(uint32_t rttMs);
64 void UpdateMaxFrameSize(uint32_t frameSizeBytes);
66 // A constant describing the delay from the jitter buffer
67 // to the delay on the receiving side which is not accounted
68 // for by the jitter buffer nor the decoding delay estimate.
69 static const uint32_t OPERATING_SYSTEM_JITTER = 10;
72 // These are protected for better testing possibilities
73 double _theta[2]; // Estimated line parameters (slope, offset)
74 double _varNoise; // Variance of the time-deviation from the line
76 virtual bool LowRateExperimentEnabled();
79 // Updates the Kalman filter for the line describing
80 // the frame size dependent jitter.
83 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in milliseconds
84 // - deltaFSBytes : Frame size delta, i.e.
85 // : frame size at time T minus frame size at time T-1
86 void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes);
88 // Updates the random jitter estimate, i.e. the variance
89 // of the time deviations from the line given by the Kalman filter.
92 // - d_dT : The deviation from the kalman estimate
93 // - incompleteFrame : True if the frame used to update the estimate
94 // with was incomplete
95 void EstimateRandomJitter(double d_dT, bool incompleteFrame);
97 double NoiseThreshold() const;
99 // Calculates the current jitter estimate.
101 // Return value : The current jitter estimate in milliseconds
102 double CalculateEstimate();
104 // Post process the calculated estimate
105 void PostProcessEstimate();
107 // Calculates the difference in delay between a sample and the
108 // expected delay estimated by the Kalman filter.
111 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in milliseconds
112 // - deltaFS : Frame size delta, i.e. frame size at time
113 // T minus frame size at time T-1
115 // Return value : The difference in milliseconds
116 double DeviationFromExpectedDelay(int64_t frameDelayMS,
117 int32_t deltaFSBytes) const;
119 double GetFrameRate() const;
121 // Constants, filter parameters
126 const uint32_t _alphaCountMax;
127 const double _thetaLow;
128 const uint32_t _nackLimit;
129 const int32_t _numStdDevDelayOutlier;
130 const int32_t _numStdDevFrameSizeOutlier;
131 const double _noiseStdDevs;
132 const double _noiseStdDevOffset;
134 double _thetaCov[2][2]; // Estimate covariance
135 double _Qcov[2][2]; // Process noise covariance
136 double _avgFrameSize; // Average frame size
137 double _varFrameSize; // Frame size variance
138 double _maxFrameSize; // Largest frame size received (descending
139 // with a factor _psi)
143 int64_t _lastUpdateT;
144 double _prevEstimate; // The previously returned jitter estimate
145 uint32_t _prevFrameSize; // Frame size of the previous frame
146 double _avgNoise; // Average of the random jitter
147 uint32_t _alphaCount;
148 double _filterJitterEstimate; // The filtered sum of jitter estimates
150 uint32_t _startupCount;
152 int64_t _latestNackTimestamp; // Timestamp in ms when the latest nack was seen
153 uint32_t _nackCount; // Keeps track of the number of nacks received,
154 // but never goes above _nackLimit
155 VCMRttFilter _rttFilter;
157 rtc::RollingAccumulator<uint64_t> fps_counter_;
158 enum ExperimentFlag { kInit, kEnabled, kDisabled };
159 ExperimentFlag low_rate_experiment_;
163 } // namespace webrtc
165 #endif // WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_