blob: aeba3eded33cd157c54ce75ca31d1fcf43386c0d [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 virtual bool LowRateExperimentEnabled();
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000076
philipel9d3ab612015-12-21 04:12:39 -080077 private:
78 // Updates the Kalman filter for the line describing
79 // the frame size dependent jitter.
80 //
81 // Input:
82 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
83 // milliseconds
84 // - deltaFSBytes : Frame size delta, i.e.
85 // : frame size at time T minus frame size at time
86 // T-1
87 void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000088
philipel9d3ab612015-12-21 04:12:39 -080089 // Updates the random jitter estimate, i.e. the variance
90 // of the time deviations from the line given by the Kalman filter.
91 //
92 // Input:
93 // - d_dT : The deviation from the kalman estimate
94 // - incompleteFrame : True if the frame used to update the
95 // estimate
96 // with was incomplete
97 void EstimateRandomJitter(double d_dT, bool incompleteFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
philipel9d3ab612015-12-21 04:12:39 -080099 double NoiseThreshold() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
philipel9d3ab612015-12-21 04:12:39 -0800101 // Calculates the current jitter estimate.
102 //
103 // Return value : The current jitter estimate in milliseconds
104 double CalculateEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
philipel9d3ab612015-12-21 04:12:39 -0800106 // Post process the calculated estimate
107 void PostProcessEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
philipel9d3ab612015-12-21 04:12:39 -0800109 // Calculates the difference in delay between a sample and the
110 // expected delay estimated by the Kalman filter.
111 //
112 // Input:
113 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
114 // milliseconds
115 // - deltaFS : Frame size delta, i.e. frame size at time
116 // T minus frame size at time T-1
117 //
118 // Return value : The difference in milliseconds
119 double DeviationFromExpectedDelay(int64_t frameDelayMS,
120 int32_t deltaFSBytes) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
philipel9d3ab612015-12-21 04:12:39 -0800122 double GetFrameRate() const;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000123
philipel9d3ab612015-12-21 04:12:39 -0800124 // Constants, filter parameters
125 int32_t _vcmId;
126 int32_t _receiverId;
127 const double _phi;
128 const double _psi;
129 const uint32_t _alphaCountMax;
130 const double _thetaLow;
131 const uint32_t _nackLimit;
132 const int32_t _numStdDevDelayOutlier;
133 const int32_t _numStdDevFrameSizeOutlier;
134 const double _noiseStdDevs;
135 const double _noiseStdDevOffset;
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
philipel9d3ab612015-12-21 04:12:39 -0800137 double _thetaCov[2][2]; // Estimate covariance
138 double _Qcov[2][2]; // Process noise covariance
139 double _avgFrameSize; // Average frame size
140 double _varFrameSize; // Frame size variance
141 double _maxFrameSize; // Largest frame size received (descending
142 // with a factor _psi)
143 uint32_t _fsSum;
144 uint32_t _fsCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
philipel9d3ab612015-12-21 04:12:39 -0800146 int64_t _lastUpdateT;
147 double _prevEstimate; // The previously returned jitter estimate
148 uint32_t _prevFrameSize; // Frame size of the previous frame
149 double _avgNoise; // Average of the random jitter
150 uint32_t _alphaCount;
151 double _filterJitterEstimate; // The filtered sum of jitter estimates
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
philipel9d3ab612015-12-21 04:12:39 -0800153 uint32_t _startupCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
philipel9d3ab612015-12-21 04:12:39 -0800155 int64_t
156 _latestNackTimestamp; // Timestamp in ms when the latest nack was seen
157 uint32_t _nackCount; // Keeps track of the number of nacks received,
158 // but never goes above _nackLimit
159 VCMRttFilter _rttFilter;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
philipel9d3ab612015-12-21 04:12:39 -0800161 rtc::RollingAccumulator<uint64_t> fps_counter_;
162 enum ExperimentFlag { kInit, kEnabled, kDisabled };
163 ExperimentFlag low_rate_experiment_;
164 const Clock* clock_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165};
166
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000167} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000168
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200169#endif // MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_