blob: d9798b40a19dbd46f41b8be1b05bf8b8fcbbd446 [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"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/rolling_accumulator.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:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020023 explicit VCMJitterEstimator(Clock* clock);
philipel9d3ab612015-12-21 04:12:39 -080024 virtual ~VCMJitterEstimator();
25 VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs);
niklase@google.com470e71d2011-07-07 08:21:25 +000026
Åsa Persson3fcc5be2019-04-04 09:40:27 +020027 // Resets the estimate to the initial state.
philipel9d3ab612015-12-21 04:12:39 -080028 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000029
philipel9d3ab612015-12-21 04:12:39 -080030 // Updates the jitter estimate with the new data.
31 //
32 // Input:
33 // - frameDelay : Delay-delta calculated by UTILDelayEstimate in
Åsa Persson3fcc5be2019-04-04 09:40:27 +020034 // milliseconds.
philipel9d3ab612015-12-21 04:12:39 -080035 // - frameSize : Frame size of the current frame.
36 // - incompleteFrame : Flags if the frame is used to update the
Åsa Persson3fcc5be2019-04-04 09:40:27 +020037 // estimate before it was complete.
38 // Default is false.
philipel9d3ab612015-12-21 04:12:39 -080039 void UpdateEstimate(int64_t frameDelayMS,
40 uint32_t frameSizeBytes,
41 bool incompleteFrame = false);
niklase@google.com470e71d2011-07-07 08:21:25 +000042
Åsa Persson3fcc5be2019-04-04 09:40:27 +020043 // Returns the current jitter estimate in milliseconds and adds an RTT
44 // dependent term in cases of retransmission.
philipel9d3ab612015-12-21 04:12:39 -080045 // Input:
46 // - rttMultiplier : RTT param multiplier (when applicable).
47 //
Åsa Persson3fcc5be2019-04-04 09:40:27 +020048 // Return value : Jitter estimate in milliseconds.
“Michaele0f37042019-06-04 10:04:12 -050049 virtual int GetJitterEstimate(double rttMultiplier,
50 absl::optional<double> rttMultAddCapMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
philipel9d3ab612015-12-21 04:12:39 -080052 // Updates the nack counter.
53 void FrameNacked();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
philipel9d3ab612015-12-21 04:12:39 -080055 // Updates the RTT filter.
56 //
57 // Input:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020058 // - rttMs : RTT in ms.
philipel9d3ab612015-12-21 04:12:39 -080059 void UpdateRtt(int64_t rttMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000060
Åsa Persson3fcc5be2019-04-04 09:40:27 +020061 // A constant describing the delay from the jitter buffer to the delay on the
62 // receiving side which is not accounted for by the jitter buffer nor the
63 // decoding delay estimate.
philipel9d3ab612015-12-21 04:12:39 -080064 static const uint32_t OPERATING_SYSTEM_JITTER = 10;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
philipel9d3ab612015-12-21 04:12:39 -080066 protected:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020067 // These are protected for better testing possibilities.
philipel9d3ab612015-12-21 04:12:39 -080068 double _theta[2]; // Estimated line parameters (slope, offset)
69 double _varNoise; // Variance of the time-deviation from the line
niklase@google.com470e71d2011-07-07 08:21:25 +000070
philipel9d3ab612015-12-21 04:12:39 -080071 private:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020072 // Updates the Kalman filter for the line describing the frame size dependent
73 // jitter.
philipel9d3ab612015-12-21 04:12:39 -080074 //
75 // Input:
76 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
Åsa Persson3fcc5be2019-04-04 09:40:27 +020077 // milliseconds.
78 // - deltaFSBytes : Frame size delta, i.e. frame size at time T
79 // : minus frame size at time T-1.
philipel9d3ab612015-12-21 04:12:39 -080080 void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
Åsa Persson3fcc5be2019-04-04 09:40:27 +020082 // Updates the random jitter estimate, i.e. the variance of the time
83 // deviations from the line given by the Kalman filter.
philipel9d3ab612015-12-21 04:12:39 -080084 //
85 // Input:
Åsa Persson3fcc5be2019-04-04 09:40:27 +020086 // - d_dT : The deviation from the kalman estimate.
philipel9d3ab612015-12-21 04:12:39 -080087 // - incompleteFrame : True if the frame used to update the
Åsa Persson3fcc5be2019-04-04 09:40:27 +020088 // estimate with was incomplete.
philipel9d3ab612015-12-21 04:12:39 -080089 void EstimateRandomJitter(double d_dT, bool incompleteFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
philipel9d3ab612015-12-21 04:12:39 -080091 double NoiseThreshold() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
philipel9d3ab612015-12-21 04:12:39 -080093 // Calculates the current jitter estimate.
94 //
Åsa Persson3fcc5be2019-04-04 09:40:27 +020095 // Return value : The current jitter estimate in milliseconds.
philipel9d3ab612015-12-21 04:12:39 -080096 double CalculateEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +000097
Åsa Persson3fcc5be2019-04-04 09:40:27 +020098 // Post process the calculated estimate.
philipel9d3ab612015-12-21 04:12:39 -080099 void PostProcessEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
Åsa Persson3fcc5be2019-04-04 09:40:27 +0200101 // Calculates the difference in delay between a sample and the expected delay
102 // estimated by the Kalman filter.
philipel9d3ab612015-12-21 04:12:39 -0800103 //
104 // Input:
105 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
Åsa Persson3fcc5be2019-04-04 09:40:27 +0200106 // milliseconds.
philipel9d3ab612015-12-21 04:12:39 -0800107 // - deltaFS : Frame size delta, i.e. frame size at time
Åsa Persson3fcc5be2019-04-04 09:40:27 +0200108 // T minus frame size at time T-1.
philipel9d3ab612015-12-21 04:12:39 -0800109 //
Åsa Persson3fcc5be2019-04-04 09:40:27 +0200110 // Return value : The difference in milliseconds.
philipel9d3ab612015-12-21 04:12:39 -0800111 double DeviationFromExpectedDelay(int64_t frameDelayMS,
112 int32_t deltaFSBytes) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
philipel9d3ab612015-12-21 04:12:39 -0800114 double GetFrameRate() const;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000115
Åsa Persson3fcc5be2019-04-04 09:40:27 +0200116 // Constants, filter parameters.
philipel9d3ab612015-12-21 04:12:39 -0800117 const double _phi;
118 const double _psi;
119 const uint32_t _alphaCountMax;
120 const double _thetaLow;
121 const uint32_t _nackLimit;
122 const int32_t _numStdDevDelayOutlier;
123 const int32_t _numStdDevFrameSizeOutlier;
124 const double _noiseStdDevs;
125 const double _noiseStdDevOffset;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
philipel9d3ab612015-12-21 04:12:39 -0800127 double _thetaCov[2][2]; // Estimate covariance
128 double _Qcov[2][2]; // Process noise covariance
129 double _avgFrameSize; // Average frame size
130 double _varFrameSize; // Frame size variance
131 double _maxFrameSize; // Largest frame size received (descending
132 // with a factor _psi)
133 uint32_t _fsSum;
134 uint32_t _fsCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
philipel9d3ab612015-12-21 04:12:39 -0800136 int64_t _lastUpdateT;
137 double _prevEstimate; // The previously returned jitter estimate
138 uint32_t _prevFrameSize; // Frame size of the previous frame
139 double _avgNoise; // Average of the random jitter
140 uint32_t _alphaCount;
141 double _filterJitterEstimate; // The filtered sum of jitter estimates
niklase@google.com470e71d2011-07-07 08:21:25 +0000142
philipel9d3ab612015-12-21 04:12:39 -0800143 uint32_t _startupCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
philipel9d3ab612015-12-21 04:12:39 -0800145 int64_t
146 _latestNackTimestamp; // Timestamp in ms when the latest nack was seen
147 uint32_t _nackCount; // Keeps track of the number of nacks received,
148 // but never goes above _nackLimit
149 VCMRttFilter _rttFilter;
niklase@google.com470e71d2011-07-07 08:21:25 +0000150
philipel9d3ab612015-12-21 04:12:39 -0800151 rtc::RollingAccumulator<uint64_t> fps_counter_;
Erik Språngb1e031a2018-11-01 11:20:49 +0100152 const double time_deviation_upper_bound_;
Sebastian Janssonaa01f272019-01-30 11:28:59 +0100153 Clock* clock_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000154};
155
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000156} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200158#endif // MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_