blob: 56b532851d939ce8936606dc62cfccce4b71871e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
12#define MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/rtt_filter.h"
15#include "rtc_base/rollingaccumulator.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
philipel9d3ab612015-12-21 04:12:39 -080017namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000018
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000019class Clock;
20
philipel9d3ab612015-12-21 04:12:39 -080021class VCMJitterEstimator {
22 public:
23 VCMJitterEstimator(const Clock* clock,
24 int32_t vcmId = 0,
25 int32_t receiverId = 0);
26 virtual ~VCMJitterEstimator();
27 VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs);
niklase@google.com470e71d2011-07-07 08:21:25 +000028
philipel9d3ab612015-12-21 04:12:39 -080029 // Resets the estimate to the initial state
30 void Reset();
31 void ResetNackCount();
niklase@google.com470e71d2011-07-07 08:21:25 +000032
philipel9d3ab612015-12-21 04:12:39 -080033 // 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.com470e71d2011-07-07 08:21:25 +000045
philipel9d3ab612015-12-21 04:12:39 -080046 // 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
philipel4f6cd6a2016-08-03 10:59:32 +020052 virtual int GetJitterEstimate(double rttMultiplier);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
philipel9d3ab612015-12-21 04:12:39 -080054 // Updates the nack counter.
55 void FrameNacked();
niklase@google.com470e71d2011-07-07 08:21:25 +000056
philipel9d3ab612015-12-21 04:12:39 -080057 // Updates the RTT filter.
58 //
59 // Input:
60 // - rttMs : RTT in ms
61 void UpdateRtt(int64_t rttMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
philipel9d3ab612015-12-21 04:12:39 -080063 void UpdateMaxFrameSize(uint32_t frameSizeBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000064
philipel9d3ab612015-12-21 04:12:39 -080065 // 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.com470e71d2011-07-07 08:21:25 +000069
philipel9d3ab612015-12-21 04:12:39 -080070 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.com470e71d2011-07-07 08:21:25 +000074
philipel9d3ab612015-12-21 04:12:39 -080075 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.com470e71d2011-07-07 08:21:25 +000086
philipel9d3ab612015-12-21 04:12:39 -080087 // 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.com470e71d2011-07-07 08:21:25 +000096
philipel9d3ab612015-12-21 04:12:39 -080097 double NoiseThreshold() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000098
philipel9d3ab612015-12-21 04:12:39 -080099 // Calculates the current jitter estimate.
100 //
101 // Return value : The current jitter estimate in milliseconds
102 double CalculateEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
philipel9d3ab612015-12-21 04:12:39 -0800104 // Post process the calculated estimate
105 void PostProcessEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
philipel9d3ab612015-12-21 04:12:39 -0800107 // 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.com470e71d2011-07-07 08:21:25 +0000119
philipel9d3ab612015-12-21 04:12:39 -0800120 double GetFrameRate() const;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000121
philipel9d3ab612015-12-21 04:12:39 -0800122 // 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.com470e71d2011-07-07 08:21:25 +0000134
philipel9d3ab612015-12-21 04:12:39 -0800135 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.com470e71d2011-07-07 08:21:25 +0000143
philipel9d3ab612015-12-21 04:12:39 -0800144 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.com470e71d2011-07-07 08:21:25 +0000150
philipel9d3ab612015-12-21 04:12:39 -0800151 uint32_t _startupCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
philipel9d3ab612015-12-21 04:12:39 -0800153 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.com470e71d2011-07-07 08:21:25 +0000158
philipel9d3ab612015-12-21 04:12:39 -0800159 rtc::RollingAccumulator<uint64_t> fps_counter_;
Erik Språngb1e031a2018-11-01 11:20:49 +0100160 const double time_deviation_upper_bound_;
philipel9d3ab612015-12-21 04:12:39 -0800161 const Clock* clock_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000162};
163
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000164} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200166#endif // MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_