blob: 7ef050f03232683e9c6b42ee9f00722477891288 [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
12#define WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_
13
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000014#include "webrtc/base/rollingaccumulator.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010015#include "webrtc/modules/video_coding/rtt_filter.h"
pbos@webrtc.orga4407322013-07-16 12:32:05 +000016#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
philipel9d3ab612015-12-21 04:12:39 -080018namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000019
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000020class Clock;
21
philipel9d3ab612015-12-21 04:12:39 -080022class VCMJitterEstimator {
23 public:
24 VCMJitterEstimator(const Clock* clock,
25 int32_t vcmId = 0,
26 int32_t receiverId = 0);
27 virtual ~VCMJitterEstimator();
28 VCMJitterEstimator& operator=(const VCMJitterEstimator& rhs);
niklase@google.com470e71d2011-07-07 08:21:25 +000029
philipel9d3ab612015-12-21 04:12:39 -080030 // Resets the estimate to the initial state
31 void Reset();
32 void ResetNackCount();
niklase@google.com470e71d2011-07-07 08:21:25 +000033
philipel9d3ab612015-12-21 04:12:39 -080034 // Updates the jitter estimate with the new data.
35 //
36 // Input:
37 // - frameDelay : Delay-delta calculated by UTILDelayEstimate in
38 // milliseconds
39 // - frameSize : Frame size of the current frame.
40 // - incompleteFrame : Flags if the frame is used to update the
41 // estimate before it
42 // was complete. Default is false.
43 void UpdateEstimate(int64_t frameDelayMS,
44 uint32_t frameSizeBytes,
45 bool incompleteFrame = false);
niklase@google.com470e71d2011-07-07 08:21:25 +000046
philipel9d3ab612015-12-21 04:12:39 -080047 // Returns the current jitter estimate in milliseconds and adds
48 // also adds an RTT dependent term in cases of retransmission.
49 // Input:
50 // - rttMultiplier : RTT param multiplier (when applicable).
51 //
52 // Return value : Jitter estimate in milliseconds
philipel4f6cd6a2016-08-03 10:59:32 +020053 virtual int GetJitterEstimate(double rttMultiplier);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
philipel9d3ab612015-12-21 04:12:39 -080055 // Updates the nack counter.
56 void FrameNacked();
niklase@google.com470e71d2011-07-07 08:21:25 +000057
philipel9d3ab612015-12-21 04:12:39 -080058 // Updates the RTT filter.
59 //
60 // Input:
61 // - rttMs : RTT in ms
62 void UpdateRtt(int64_t rttMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000063
philipel9d3ab612015-12-21 04:12:39 -080064 void UpdateMaxFrameSize(uint32_t frameSizeBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
philipel9d3ab612015-12-21 04:12:39 -080066 // 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
philipel9d3ab612015-12-21 04:12:39 -080071 protected:
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
niklase@google.com470e71d2011-07-07 08:21:25 +000075
philipel9d3ab612015-12-21 04:12:39 -080076 virtual bool LowRateExperimentEnabled();
sprang@webrtc.org70e2d112014-09-24 14:06:56 +000077
philipel9d3ab612015-12-21 04:12:39 -080078 private:
79 // Updates the Kalman filter for the line describing
80 // the frame size dependent jitter.
81 //
82 // Input:
83 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
84 // milliseconds
85 // - deltaFSBytes : Frame size delta, i.e.
86 // : frame size at time T minus frame size at time
87 // T-1
88 void KalmanEstimateChannel(int64_t frameDelayMS, int32_t deltaFSBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
philipel9d3ab612015-12-21 04:12:39 -080090 // Updates the random jitter estimate, i.e. the variance
91 // of the time deviations from the line given by the Kalman filter.
92 //
93 // Input:
94 // - d_dT : The deviation from the kalman estimate
95 // - incompleteFrame : True if the frame used to update the
96 // estimate
97 // with was incomplete
98 void EstimateRandomJitter(double d_dT, bool incompleteFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
philipel9d3ab612015-12-21 04:12:39 -0800100 double NoiseThreshold() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
philipel9d3ab612015-12-21 04:12:39 -0800102 // Calculates the current jitter estimate.
103 //
104 // Return value : The current jitter estimate in milliseconds
105 double CalculateEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
philipel9d3ab612015-12-21 04:12:39 -0800107 // Post process the calculated estimate
108 void PostProcessEstimate();
niklase@google.com470e71d2011-07-07 08:21:25 +0000109
philipel9d3ab612015-12-21 04:12:39 -0800110 // Calculates the difference in delay between a sample and the
111 // expected delay estimated by the Kalman filter.
112 //
113 // Input:
114 // - frameDelayMS : Delay-delta calculated by UTILDelayEstimate in
115 // milliseconds
116 // - deltaFS : Frame size delta, i.e. frame size at time
117 // T minus frame size at time T-1
118 //
119 // Return value : The difference in milliseconds
120 double DeviationFromExpectedDelay(int64_t frameDelayMS,
121 int32_t deltaFSBytes) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
philipel9d3ab612015-12-21 04:12:39 -0800123 double GetFrameRate() const;
sprang@webrtc.org70e2d112014-09-24 14:06:56 +0000124
philipel9d3ab612015-12-21 04:12:39 -0800125 // Constants, filter parameters
126 int32_t _vcmId;
127 int32_t _receiverId;
128 const double _phi;
129 const double _psi;
130 const uint32_t _alphaCountMax;
131 const double _thetaLow;
132 const uint32_t _nackLimit;
133 const int32_t _numStdDevDelayOutlier;
134 const int32_t _numStdDevFrameSizeOutlier;
135 const double _noiseStdDevs;
136 const double _noiseStdDevOffset;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137
philipel9d3ab612015-12-21 04:12:39 -0800138 double _thetaCov[2][2]; // Estimate covariance
139 double _Qcov[2][2]; // Process noise covariance
140 double _avgFrameSize; // Average frame size
141 double _varFrameSize; // Frame size variance
142 double _maxFrameSize; // Largest frame size received (descending
143 // with a factor _psi)
144 uint32_t _fsSum;
145 uint32_t _fsCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
philipel9d3ab612015-12-21 04:12:39 -0800147 int64_t _lastUpdateT;
148 double _prevEstimate; // The previously returned jitter estimate
149 uint32_t _prevFrameSize; // Frame size of the previous frame
150 double _avgNoise; // Average of the random jitter
151 uint32_t _alphaCount;
152 double _filterJitterEstimate; // The filtered sum of jitter estimates
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
philipel9d3ab612015-12-21 04:12:39 -0800154 uint32_t _startupCount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000155
philipel9d3ab612015-12-21 04:12:39 -0800156 int64_t
157 _latestNackTimestamp; // Timestamp in ms when the latest nack was seen
158 uint32_t _nackCount; // Keeps track of the number of nacks received,
159 // but never goes above _nackLimit
160 VCMRttFilter _rttFilter;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161
philipel9d3ab612015-12-21 04:12:39 -0800162 rtc::RollingAccumulator<uint64_t> fps_counter_;
163 enum ExperimentFlag { kInit, kEnabled, kDisabled };
164 ExperimentFlag low_rate_experiment_;
165 const Clock* clock_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000166};
167
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000168} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000169
philipel9d3ab612015-12-21 04:12:39 -0800170#endif // WEBRTC_MODULES_VIDEO_CODING_JITTER_ESTIMATOR_H_