Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <assert.h> | |
| 12 #include <math.h> | |
| 13 #include <stdlib.h> | |
| 14 #include <string.h> | |
| 15 #include <string> | |
| 16 | |
| 11 #include "webrtc/modules/video_coding/internal_defines.h" | 17 #include "webrtc/modules/video_coding/internal_defines.h" |
| 12 #include "webrtc/modules/video_coding/jitter_estimator.h" | 18 #include "webrtc/modules/video_coding/jitter_estimator.h" |
|
mflodman
2015/12/17 13:05:39
Should be on top.
philipel
2015/12/18 12:23:16
Done.
| |
| 13 #include "webrtc/modules/video_coding/rtt_filter.h" | 19 #include "webrtc/modules/video_coding/rtt_filter.h" |
| 14 #include "webrtc/system_wrappers/include/clock.h" | 20 #include "webrtc/system_wrappers/include/clock.h" |
| 15 #include "webrtc/system_wrappers/include/field_trial.h" | 21 #include "webrtc/system_wrappers/include/field_trial.h" |
| 16 | 22 |
| 17 #include <assert.h> | |
| 18 #include <math.h> | |
| 19 #include <stdlib.h> | |
| 20 #include <string.h> | |
| 21 | |
| 22 namespace webrtc { | 23 namespace webrtc { |
| 23 | 24 |
| 24 enum { kStartupDelaySamples = 30 }; | 25 enum { kStartupDelaySamples = 30 }; |
| 25 enum { kFsAccuStartupSamples = 5 }; | 26 enum { kFsAccuStartupSamples = 5 }; |
| 26 enum { kMaxFramerateEstimate = 200 }; | 27 enum { kMaxFramerateEstimate = 200 }; |
| 27 | 28 |
| 28 VCMJitterEstimator::VCMJitterEstimator(const Clock* clock, | 29 VCMJitterEstimator::VCMJitterEstimator(const Clock* clock, |
| 29 int32_t vcmId, | 30 int32_t vcmId, |
| 30 int32_t receiverId) | 31 int32_t receiverId) |
| 31 : _vcmId(vcmId), | 32 : _vcmId(vcmId), |
| 32 _receiverId(receiverId), | 33 _receiverId(receiverId), |
| 33 _phi(0.97), | 34 _phi(0.97), |
| 34 _psi(0.9999), | 35 _psi(0.9999), |
| 35 _alphaCountMax(400), | 36 _alphaCountMax(400), |
| 36 _thetaLow(0.000001), | 37 _thetaLow(0.000001), |
| 37 _nackLimit(3), | 38 _nackLimit(3), |
| 38 _numStdDevDelayOutlier(15), | 39 _numStdDevDelayOutlier(15), |
| 39 _numStdDevFrameSizeOutlier(3), | 40 _numStdDevFrameSizeOutlier(3), |
| 40 _noiseStdDevs(2.33), // ~Less than 1% chance | 41 _noiseStdDevs(2.33), // ~Less than 1% chance |
| 41 // (look up in normal distribution table)... | 42 // (look up in normal distribution table)... |
| 42 _noiseStdDevOffset(30.0), // ...of getting 30 ms freezes | 43 _noiseStdDevOffset(30.0), // ...of getting 30 ms freezes |
| 43 _rttFilter(), | 44 _rttFilter(), |
| 44 fps_counter_(30), // TODO(sprang): Use an estimator with limit based on | 45 fps_counter_(30), // TODO(sprang): Use an estimator with limit based on |
| 45 // time, rather than number of samples. | 46 // time, rather than number of samples. |
| 46 low_rate_experiment_(kInit), | 47 low_rate_experiment_(kInit), |
| 47 clock_(clock) { | 48 clock_(clock) { |
| 48 Reset(); | 49 Reset(); |
| 49 } | 50 } |
| 50 | 51 |
| 51 VCMJitterEstimator::~VCMJitterEstimator() { | 52 VCMJitterEstimator::~VCMJitterEstimator() {} |
| 52 } | 53 |
| 53 | 54 VCMJitterEstimator& VCMJitterEstimator::operator=( |
| 54 VCMJitterEstimator& | 55 const VCMJitterEstimator& rhs) { |
| 55 VCMJitterEstimator::operator=(const VCMJitterEstimator& rhs) | 56 if (this != &rhs) { |
| 56 { | 57 memcpy(_thetaCov, rhs._thetaCov, sizeof(_thetaCov)); |
| 57 if (this != &rhs) | 58 memcpy(_Qcov, rhs._Qcov, sizeof(_Qcov)); |
| 58 { | 59 |
| 59 memcpy(_thetaCov, rhs._thetaCov, sizeof(_thetaCov)); | 60 _vcmId = rhs._vcmId; |
| 60 memcpy(_Qcov, rhs._Qcov, sizeof(_Qcov)); | 61 _receiverId = rhs._receiverId; |
| 61 | 62 _avgFrameSize = rhs._avgFrameSize; |
| 62 _vcmId = rhs._vcmId; | 63 _varFrameSize = rhs._varFrameSize; |
| 63 _receiverId = rhs._receiverId; | 64 _maxFrameSize = rhs._maxFrameSize; |
| 64 _avgFrameSize = rhs._avgFrameSize; | 65 _fsSum = rhs._fsSum; |
| 65 _varFrameSize = rhs._varFrameSize; | 66 _fsCount = rhs._fsCount; |
| 66 _maxFrameSize = rhs._maxFrameSize; | 67 _lastUpdateT = rhs._lastUpdateT; |
| 67 _fsSum = rhs._fsSum; | 68 _prevEstimate = rhs._prevEstimate; |
| 68 _fsCount = rhs._fsCount; | 69 _prevFrameSize = rhs._prevFrameSize; |
| 69 _lastUpdateT = rhs._lastUpdateT; | 70 _avgNoise = rhs._avgNoise; |
| 70 _prevEstimate = rhs._prevEstimate; | 71 _alphaCount = rhs._alphaCount; |
| 71 _prevFrameSize = rhs._prevFrameSize; | 72 _filterJitterEstimate = rhs._filterJitterEstimate; |
| 72 _avgNoise = rhs._avgNoise; | 73 _startupCount = rhs._startupCount; |
| 73 _alphaCount = rhs._alphaCount; | 74 _latestNackTimestamp = rhs._latestNackTimestamp; |
| 74 _filterJitterEstimate = rhs._filterJitterEstimate; | 75 _nackCount = rhs._nackCount; |
| 75 _startupCount = rhs._startupCount; | 76 _rttFilter = rhs._rttFilter; |
| 76 _latestNackTimestamp = rhs._latestNackTimestamp; | 77 } |
| 77 _nackCount = rhs._nackCount; | 78 return *this; |
| 78 _rttFilter = rhs._rttFilter; | 79 } |
| 80 | |
| 81 // Resets the JitterEstimate | |
| 82 void VCMJitterEstimator::Reset() { | |
| 83 _theta[0] = 1 / (512e3 / 8); | |
| 84 _theta[1] = 0; | |
| 85 _varNoise = 4.0; | |
| 86 | |
| 87 _thetaCov[0][0] = 1e-4; | |
| 88 _thetaCov[1][1] = 1e2; | |
| 89 _thetaCov[0][1] = _thetaCov[1][0] = 0; | |
| 90 _Qcov[0][0] = 2.5e-10; | |
| 91 _Qcov[1][1] = 1e-10; | |
| 92 _Qcov[0][1] = _Qcov[1][0] = 0; | |
| 93 _avgFrameSize = 500; | |
| 94 _maxFrameSize = 500; | |
| 95 _varFrameSize = 100; | |
| 96 _lastUpdateT = -1; | |
| 97 _prevEstimate = -1.0; | |
| 98 _prevFrameSize = 0; | |
| 99 _avgNoise = 0.0; | |
| 100 _alphaCount = 1; | |
| 101 _filterJitterEstimate = 0.0; | |
| 102 _latestNackTimestamp = 0; | |
| 103 _nackCount = 0; | |
| 104 _fsSum = 0; | |
| 105 _fsCount = 0; | |
| 106 _startupCount = 0; | |
| 107 _rttFilter.Reset(); | |
| 108 fps_counter_.Reset(); | |
| 109 } | |
| 110 | |
| 111 void VCMJitterEstimator::ResetNackCount() { | |
| 112 _nackCount = 0; | |
| 113 } | |
| 114 | |
| 115 // Updates the estimates with the new measurements | |
| 116 void VCMJitterEstimator::UpdateEstimate(int64_t frameDelayMS, | |
| 117 uint32_t frameSizeBytes, | |
| 118 bool incompleteFrame /* = false */) { | |
| 119 if (frameSizeBytes == 0) { | |
| 120 return; | |
| 121 } | |
| 122 int deltaFS = frameSizeBytes - _prevFrameSize; | |
| 123 if (_fsCount < kFsAccuStartupSamples) { | |
| 124 _fsSum += frameSizeBytes; | |
| 125 _fsCount++; | |
| 126 } else if (_fsCount == kFsAccuStartupSamples) { | |
| 127 // Give the frame size filter | |
| 128 _avgFrameSize = static_cast<double>(_fsSum) / static_cast<double>(_fsCount); | |
| 129 _fsCount++; | |
| 130 } | |
| 131 if (!incompleteFrame || frameSizeBytes > _avgFrameSize) { | |
| 132 double avgFrameSize = _phi * _avgFrameSize + (1 - _phi) * frameSizeBytes; | |
| 133 if (frameSizeBytes < _avgFrameSize + 2 * sqrt(_varFrameSize)) { | |
| 134 // Only update the average frame size if this sample wasn't a | |
| 135 // key frame | |
| 136 _avgFrameSize = avgFrameSize; | |
| 79 } | 137 } |
| 80 return *this; | 138 // Update the variance anyway since we want to capture cases where we only |
| 81 } | 139 // get |
| 82 | 140 // key frames. |
| 83 // Resets the JitterEstimate | 141 _varFrameSize = VCM_MAX(_phi * _varFrameSize + |
| 84 void | 142 (1 - _phi) * (frameSizeBytes - avgFrameSize) * |
| 85 VCMJitterEstimator::Reset() | 143 (frameSizeBytes - avgFrameSize), |
| 86 { | 144 1.0); |
| 87 _theta[0] = 1/(512e3/8); | 145 } |
| 88 _theta[1] = 0; | 146 |
| 89 _varNoise = 4.0; | 147 // Update max frameSize estimate |
| 90 | 148 _maxFrameSize = |
| 91 _thetaCov[0][0] = 1e-4; | 149 VCM_MAX(_psi * _maxFrameSize, static_cast<double>(frameSizeBytes)); |
| 92 _thetaCov[1][1] = 1e2; | 150 |
| 93 _thetaCov[0][1] = _thetaCov[1][0] = 0; | 151 if (_prevFrameSize == 0) { |
| 94 _Qcov[0][0] = 2.5e-10; | 152 _prevFrameSize = frameSizeBytes; |
| 95 _Qcov[1][1] = 1e-10; | 153 return; |
| 96 _Qcov[0][1] = _Qcov[1][0] = 0; | 154 } |
| 97 _avgFrameSize = 500; | 155 _prevFrameSize = frameSizeBytes; |
| 98 _maxFrameSize = 500; | 156 |
| 99 _varFrameSize = 100; | 157 // Only update the Kalman filter if the sample is not considered |
| 100 _lastUpdateT = -1; | 158 // an extreme outlier. Even if it is an extreme outlier from a |
| 101 _prevEstimate = -1.0; | 159 // delay point of view, if the frame size also is large the |
| 102 _prevFrameSize = 0; | 160 // deviation is probably due to an incorrect line slope. |
| 103 _avgNoise = 0.0; | 161 double deviation = DeviationFromExpectedDelay(frameDelayMS, deltaFS); |
| 104 _alphaCount = 1; | 162 |
| 105 _filterJitterEstimate = 0.0; | 163 if (fabs(deviation) < _numStdDevDelayOutlier * sqrt(_varNoise) || |
| 106 _latestNackTimestamp = 0; | 164 frameSizeBytes > |
| 107 _nackCount = 0; | 165 _avgFrameSize + _numStdDevFrameSizeOutlier * sqrt(_varFrameSize)) { |
| 108 _fsSum = 0; | 166 // Update the variance of the deviation from the |
| 109 _fsCount = 0; | 167 // line given by the Kalman filter |
| 110 _startupCount = 0; | 168 EstimateRandomJitter(deviation, incompleteFrame); |
| 111 _rttFilter.Reset(); | 169 // Prevent updating with frames which have been congested by a large |
| 112 fps_counter_.Reset(); | 170 // frame, and therefore arrives almost at the same time as that frame. |
| 113 } | 171 // This can occur when we receive a large frame (key frame) which |
| 114 | 172 // has been delayed. The next frame is of normal size (delta frame), |
| 115 void | 173 // and thus deltaFS will be << 0. This removes all frame samples |
| 116 VCMJitterEstimator::ResetNackCount() | 174 // which arrives after a key frame. |
| 117 { | 175 if ((!incompleteFrame || deviation >= 0.0) && |
| 118 _nackCount = 0; | 176 static_cast<double>(deltaFS) > -0.25 * _maxFrameSize) { |
| 119 } | 177 // Update the Kalman filter with the new data |
| 120 | 178 KalmanEstimateChannel(frameDelayMS, deltaFS); |
| 121 // Updates the estimates with the new measurements | |
| 122 void | |
| 123 VCMJitterEstimator::UpdateEstimate(int64_t frameDelayMS, uint32_t frameSizeBytes , | |
| 124 bool incompleteFrame /* = false */) | |
| 125 { | |
| 126 if (frameSizeBytes == 0) | |
| 127 { | |
| 128 return; | |
| 129 } | 179 } |
| 130 int deltaFS = frameSizeBytes - _prevFrameSize; | 180 } else { |
| 131 if (_fsCount < kFsAccuStartupSamples) | 181 int nStdDev = |
| 132 { | 182 (deviation >= 0) ? _numStdDevDelayOutlier : -_numStdDevDelayOutlier; |
| 133 _fsSum += frameSizeBytes; | 183 EstimateRandomJitter(nStdDev * sqrt(_varNoise), incompleteFrame); |
| 134 _fsCount++; | 184 } |
| 135 } | 185 // Post process the total estimated jitter |
| 136 else if (_fsCount == kFsAccuStartupSamples) | 186 if (_startupCount >= kStartupDelaySamples) { |
| 137 { | 187 PostProcessEstimate(); |
| 138 // Give the frame size filter | 188 } else { |
| 139 _avgFrameSize = static_cast<double>(_fsSum) / | 189 _startupCount++; |
| 140 static_cast<double>(_fsCount); | 190 } |
| 141 _fsCount++; | |
| 142 } | |
| 143 if (!incompleteFrame || frameSizeBytes > _avgFrameSize) | |
| 144 { | |
| 145 double avgFrameSize = _phi * _avgFrameSize + | |
| 146 (1 - _phi) * frameSizeBytes; | |
| 147 if (frameSizeBytes < _avgFrameSize + 2 * sqrt(_varFrameSize)) | |
| 148 { | |
| 149 // Only update the average frame size if this sample wasn't a | |
| 150 // key frame | |
| 151 _avgFrameSize = avgFrameSize; | |
| 152 } | |
| 153 // Update the variance anyway since we want to capture cases where we on ly get | |
| 154 // key frames. | |
| 155 _varFrameSize = VCM_MAX(_phi * _varFrameSize + (1 - _phi) * | |
| 156 (frameSizeBytes - avgFrameSize) * | |
| 157 (frameSizeBytes - avgFrameSize), 1.0); | |
| 158 } | |
| 159 | |
| 160 // Update max frameSize estimate | |
| 161 _maxFrameSize = VCM_MAX(_psi * _maxFrameSize, static_cast<double>(frameSizeB ytes)); | |
| 162 | |
| 163 if (_prevFrameSize == 0) | |
| 164 { | |
| 165 _prevFrameSize = frameSizeBytes; | |
| 166 return; | |
| 167 } | |
| 168 _prevFrameSize = frameSizeBytes; | |
| 169 | |
| 170 // Only update the Kalman filter if the sample is not considered | |
| 171 // an extreme outlier. Even if it is an extreme outlier from a | |
| 172 // delay point of view, if the frame size also is large the | |
| 173 // deviation is probably due to an incorrect line slope. | |
| 174 double deviation = DeviationFromExpectedDelay(frameDelayMS, deltaFS); | |
| 175 | |
| 176 if (fabs(deviation) < _numStdDevDelayOutlier * sqrt(_varNoise) || | |
| 177 frameSizeBytes > _avgFrameSize + _numStdDevFrameSizeOutlier * sqrt(_varF rameSize)) | |
| 178 { | |
| 179 // Update the variance of the deviation from the | |
| 180 // line given by the Kalman filter | |
| 181 EstimateRandomJitter(deviation, incompleteFrame); | |
| 182 // Prevent updating with frames which have been congested by a large | |
| 183 // frame, and therefore arrives almost at the same time as that frame. | |
| 184 // This can occur when we receive a large frame (key frame) which | |
| 185 // has been delayed. The next frame is of normal size (delta frame), | |
| 186 // and thus deltaFS will be << 0. This removes all frame samples | |
| 187 // which arrives after a key frame. | |
| 188 if ((!incompleteFrame || deviation >= 0.0) && | |
| 189 static_cast<double>(deltaFS) > - 0.25 * _maxFrameSize) | |
| 190 { | |
| 191 // Update the Kalman filter with the new data | |
| 192 KalmanEstimateChannel(frameDelayMS, deltaFS); | |
| 193 } | |
| 194 } | |
| 195 else | |
| 196 { | |
| 197 int nStdDev = (deviation >= 0) ? _numStdDevDelayOutlier : -_numStdDevDel ayOutlier; | |
| 198 EstimateRandomJitter(nStdDev * sqrt(_varNoise), incompleteFrame); | |
| 199 } | |
| 200 // Post process the total estimated jitter | |
| 201 if (_startupCount >= kStartupDelaySamples) | |
| 202 { | |
| 203 PostProcessEstimate(); | |
| 204 } | |
| 205 else | |
| 206 { | |
| 207 _startupCount++; | |
| 208 } | |
| 209 } | 191 } |
| 210 | 192 |
| 211 // Updates the nack/packet ratio | 193 // Updates the nack/packet ratio |
| 212 void | 194 void VCMJitterEstimator::FrameNacked() { |
| 213 VCMJitterEstimator::FrameNacked() | 195 // Wait until _nackLimit retransmissions has been received, |
| 214 { | 196 // then always add ~1 RTT delay. |
| 215 // Wait until _nackLimit retransmissions has been received, | 197 // TODO(holmer): Should we ever remove the additional delay if the |
| 216 // then always add ~1 RTT delay. | 198 // the packet losses seem to have stopped? We could for instance scale |
| 217 // TODO(holmer): Should we ever remove the additional delay if the | 199 // the number of RTTs to add with the amount of retransmissions in a given |
| 218 // the packet losses seem to have stopped? We could for instance scale | 200 // time interval, or similar. |
| 219 // the number of RTTs to add with the amount of retransmissions in a given | 201 if (_nackCount < _nackLimit) { |
| 220 // time interval, or similar. | 202 _nackCount++; |
| 221 if (_nackCount < _nackLimit) | 203 } |
| 222 { | |
| 223 _nackCount++; | |
| 224 } | |
| 225 } | 204 } |
| 226 | 205 |
| 227 // Updates Kalman estimate of the channel | 206 // Updates Kalman estimate of the channel |
| 228 // The caller is expected to sanity check the inputs. | 207 // The caller is expected to sanity check the inputs. |
| 229 void | 208 void VCMJitterEstimator::KalmanEstimateChannel(int64_t frameDelayMS, |
| 230 VCMJitterEstimator::KalmanEstimateChannel(int64_t frameDelayMS, | 209 int32_t deltaFSBytes) { |
| 231 int32_t deltaFSBytes) | 210 double Mh[2]; |
| 232 { | 211 double hMh_sigma; |
| 233 double Mh[2]; | 212 double kalmanGain[2]; |
| 234 double hMh_sigma; | 213 double measureRes; |
| 235 double kalmanGain[2]; | 214 double t00, t01; |
| 236 double measureRes; | 215 |
| 237 double t00, t01; | 216 // Kalman filtering |
| 238 | 217 |
| 239 // Kalman filtering | 218 // Prediction |
| 240 | 219 // M = M + Q |
| 241 // Prediction | 220 _thetaCov[0][0] += _Qcov[0][0]; |
| 242 // M = M + Q | 221 _thetaCov[0][1] += _Qcov[0][1]; |
| 243 _thetaCov[0][0] += _Qcov[0][0]; | 222 _thetaCov[1][0] += _Qcov[1][0]; |
| 244 _thetaCov[0][1] += _Qcov[0][1]; | 223 _thetaCov[1][1] += _Qcov[1][1]; |
| 245 _thetaCov[1][0] += _Qcov[1][0]; | 224 |
| 246 _thetaCov[1][1] += _Qcov[1][1]; | 225 // Kalman gain |
| 247 | 226 // K = M*h'/(sigma2n + h*M*h') = M*h'/(1 + h*M*h') |
| 248 // Kalman gain | 227 // h = [dFS 1] |
| 249 // K = M*h'/(sigma2n + h*M*h') = M*h'/(1 + h*M*h') | 228 // Mh = M*h' |
| 250 // h = [dFS 1] | 229 // hMh_sigma = h*M*h' + R |
| 251 // Mh = M*h' | 230 Mh[0] = _thetaCov[0][0] * deltaFSBytes + _thetaCov[0][1]; |
| 252 // hMh_sigma = h*M*h' + R | 231 Mh[1] = _thetaCov[1][0] * deltaFSBytes + _thetaCov[1][1]; |
| 253 Mh[0] = _thetaCov[0][0] * deltaFSBytes + _thetaCov[0][1]; | 232 // sigma weights measurements with a small deltaFS as noisy and |
| 254 Mh[1] = _thetaCov[1][0] * deltaFSBytes + _thetaCov[1][1]; | 233 // measurements with large deltaFS as good |
| 255 // sigma weights measurements with a small deltaFS as noisy and | 234 if (_maxFrameSize < 1.0) { |
| 256 // measurements with large deltaFS as good | 235 return; |
| 257 if (_maxFrameSize < 1.0) | 236 } |
| 258 { | 237 double sigma = (300.0 * exp(-fabs(static_cast<double>(deltaFSBytes)) / |
| 259 return; | 238 (1e0 * _maxFrameSize)) + |
| 260 } | 239 1) * |
| 261 double sigma = (300.0 * exp(-fabs(static_cast<double>(deltaFSBytes)) / | 240 sqrt(_varNoise); |
| 262 (1e0 * _maxFrameSize)) + 1) * sqrt(_varNoise); | 241 if (sigma < 1.0) { |
| 263 if (sigma < 1.0) | 242 sigma = 1.0; |
| 264 { | 243 } |
| 265 sigma = 1.0; | 244 hMh_sigma = deltaFSBytes * Mh[0] + Mh[1] + sigma; |
| 266 } | 245 if ((hMh_sigma < 1e-9 && hMh_sigma >= 0) || |
| 267 hMh_sigma = deltaFSBytes * Mh[0] + Mh[1] + sigma; | 246 (hMh_sigma > -1e-9 && hMh_sigma <= 0)) { |
| 268 if ((hMh_sigma < 1e-9 && hMh_sigma >= 0) || (hMh_sigma > -1e-9 && hMh_sigma <= 0)) | 247 assert(false); |
| 269 { | 248 return; |
| 270 assert(false); | 249 } |
| 271 return; | 250 kalmanGain[0] = Mh[0] / hMh_sigma; |
| 272 } | 251 kalmanGain[1] = Mh[1] / hMh_sigma; |
| 273 kalmanGain[0] = Mh[0] / hMh_sigma; | 252 |
| 274 kalmanGain[1] = Mh[1] / hMh_sigma; | 253 // Correction |
| 275 | 254 // theta = theta + K*(dT - h*theta) |
| 276 // Correction | 255 measureRes = frameDelayMS - (deltaFSBytes * _theta[0] + _theta[1]); |
| 277 // theta = theta + K*(dT - h*theta) | 256 _theta[0] += kalmanGain[0] * measureRes; |
| 278 measureRes = frameDelayMS - (deltaFSBytes * _theta[0] + _theta[1]); | 257 _theta[1] += kalmanGain[1] * measureRes; |
| 279 _theta[0] += kalmanGain[0] * measureRes; | 258 |
| 280 _theta[1] += kalmanGain[1] * measureRes; | 259 if (_theta[0] < _thetaLow) { |
| 281 | 260 _theta[0] = _thetaLow; |
| 282 if (_theta[0] < _thetaLow) | 261 } |
| 283 { | 262 |
| 284 _theta[0] = _thetaLow; | 263 // M = (I - K*h)*M |
| 285 } | 264 t00 = _thetaCov[0][0]; |
| 286 | 265 t01 = _thetaCov[0][1]; |
| 287 // M = (I - K*h)*M | 266 _thetaCov[0][0] = (1 - kalmanGain[0] * deltaFSBytes) * t00 - |
| 288 t00 = _thetaCov[0][0]; | 267 kalmanGain[0] * _thetaCov[1][0]; |
| 289 t01 = _thetaCov[0][1]; | 268 _thetaCov[0][1] = (1 - kalmanGain[0] * deltaFSBytes) * t01 - |
| 290 _thetaCov[0][0] = (1 - kalmanGain[0] * deltaFSBytes) * t00 - | 269 kalmanGain[0] * _thetaCov[1][1]; |
| 291 kalmanGain[0] * _thetaCov[1][0]; | 270 _thetaCov[1][0] = _thetaCov[1][0] * (1 - kalmanGain[1]) - |
| 292 _thetaCov[0][1] = (1 - kalmanGain[0] * deltaFSBytes) * t01 - | 271 kalmanGain[1] * deltaFSBytes * t00; |
| 293 kalmanGain[0] * _thetaCov[1][1]; | 272 _thetaCov[1][1] = _thetaCov[1][1] * (1 - kalmanGain[1]) - |
| 294 _thetaCov[1][0] = _thetaCov[1][0] * (1 - kalmanGain[1]) - | 273 kalmanGain[1] * deltaFSBytes * t01; |
| 295 kalmanGain[1] * deltaFSBytes * t00; | 274 |
| 296 _thetaCov[1][1] = _thetaCov[1][1] * (1 - kalmanGain[1]) - | 275 // Covariance matrix, must be positive semi-definite |
| 297 kalmanGain[1] * deltaFSBytes * t01; | 276 assert(_thetaCov[0][0] + _thetaCov[1][1] >= 0 && |
| 298 | 277 _thetaCov[0][0] * _thetaCov[1][1] - |
| 299 // Covariance matrix, must be positive semi-definite | 278 _thetaCov[0][1] * _thetaCov[1][0] >= |
| 300 assert(_thetaCov[0][0] + _thetaCov[1][1] >= 0 && | 279 0 && |
| 301 _thetaCov[0][0] * _thetaCov[1][1] - _thetaCov[0][1] * _thetaCov[1][0] >= 0 && | 280 _thetaCov[0][0] >= 0); |
| 302 _thetaCov[0][0] >= 0); | |
| 303 } | 281 } |
| 304 | 282 |
| 305 // Calculate difference in delay between a sample and the | 283 // Calculate difference in delay between a sample and the |
| 306 // expected delay estimated by the Kalman filter | 284 // expected delay estimated by the Kalman filter |
| 307 double | 285 double VCMJitterEstimator::DeviationFromExpectedDelay( |
| 308 VCMJitterEstimator::DeviationFromExpectedDelay(int64_t frameDelayMS, | 286 int64_t frameDelayMS, |
| 309 int32_t deltaFSBytes) const | 287 int32_t deltaFSBytes) const { |
| 310 { | 288 return frameDelayMS - (_theta[0] * deltaFSBytes + _theta[1]); |
| 311 return frameDelayMS - (_theta[0] * deltaFSBytes + _theta[1]); | |
| 312 } | 289 } |
| 313 | 290 |
| 314 // Estimates the random jitter by calculating the variance of the | 291 // Estimates the random jitter by calculating the variance of the |
| 315 // sample distance from the line given by theta. | 292 // sample distance from the line given by theta. |
| 316 void VCMJitterEstimator::EstimateRandomJitter(double d_dT, | 293 void VCMJitterEstimator::EstimateRandomJitter(double d_dT, |
| 317 bool incompleteFrame) { | 294 bool incompleteFrame) { |
| 318 uint64_t now = clock_->TimeInMicroseconds(); | 295 uint64_t now = clock_->TimeInMicroseconds(); |
| 319 if (_lastUpdateT != -1) { | 296 if (_lastUpdateT != -1) { |
| 320 fps_counter_.AddSample(now - _lastUpdateT); | 297 fps_counter_.AddSample(now - _lastUpdateT); |
| 321 } | 298 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 _avgNoise = avgNoise; | 333 _avgNoise = avgNoise; |
| 357 _varNoise = varNoise; | 334 _varNoise = varNoise; |
| 358 } | 335 } |
| 359 if (_varNoise < 1.0) { | 336 if (_varNoise < 1.0) { |
| 360 // The variance should never be zero, since we might get | 337 // The variance should never be zero, since we might get |
| 361 // stuck and consider all samples as outliers. | 338 // stuck and consider all samples as outliers. |
| 362 _varNoise = 1.0; | 339 _varNoise = 1.0; |
| 363 } | 340 } |
| 364 } | 341 } |
| 365 | 342 |
| 366 double | 343 double VCMJitterEstimator::NoiseThreshold() const { |
| 367 VCMJitterEstimator::NoiseThreshold() const | 344 double noiseThreshold = _noiseStdDevs * sqrt(_varNoise) - _noiseStdDevOffset; |
| 368 { | 345 if (noiseThreshold < 1.0) { |
| 369 double noiseThreshold = _noiseStdDevs * sqrt(_varNoise) - _noiseStdDevOffset ; | 346 noiseThreshold = 1.0; |
| 370 if (noiseThreshold < 1.0) | 347 } |
| 371 { | 348 return noiseThreshold; |
| 372 noiseThreshold = 1.0; | |
| 373 } | |
| 374 return noiseThreshold; | |
| 375 } | 349 } |
| 376 | 350 |
| 377 // Calculates the current jitter estimate from the filtered estimates | 351 // Calculates the current jitter estimate from the filtered estimates |
| 378 double | 352 double VCMJitterEstimator::CalculateEstimate() { |
| 379 VCMJitterEstimator::CalculateEstimate() | 353 double ret = _theta[0] * (_maxFrameSize - _avgFrameSize) + NoiseThreshold(); |
| 380 { | |
| 381 double ret = _theta[0] * (_maxFrameSize - _avgFrameSize) + NoiseThreshold(); | |
| 382 | 354 |
| 383 // A very low estimate (or negative) is neglected | 355 // A very low estimate (or negative) is neglected |
| 384 if (ret < 1.0) { | 356 if (ret < 1.0) { |
| 385 if (_prevEstimate <= 0.01) | 357 if (_prevEstimate <= 0.01) { |
| 386 { | 358 ret = 1.0; |
| 387 ret = 1.0; | 359 } else { |
| 388 } | 360 ret = _prevEstimate; |
| 389 else | |
| 390 { | |
| 391 ret = _prevEstimate; | |
| 392 } | |
| 393 } | 361 } |
| 394 if (ret > 10000.0) // Sanity | 362 } |
| 395 { | 363 if (ret > 10000.0) { // Sanity |
| 396 ret = 10000.0; | 364 ret = 10000.0; |
| 397 } | 365 } |
| 398 _prevEstimate = ret; | 366 _prevEstimate = ret; |
| 399 return ret; | 367 return ret; |
| 400 } | 368 } |
| 401 | 369 |
| 402 void | 370 void VCMJitterEstimator::PostProcessEstimate() { |
| 403 VCMJitterEstimator::PostProcessEstimate() | 371 _filterJitterEstimate = CalculateEstimate(); |
| 404 { | |
| 405 _filterJitterEstimate = CalculateEstimate(); | |
| 406 } | 372 } |
| 407 | 373 |
| 408 void | 374 void VCMJitterEstimator::UpdateRtt(int64_t rttMs) { |
| 409 VCMJitterEstimator::UpdateRtt(int64_t rttMs) | 375 _rttFilter.Update(rttMs); |
| 410 { | |
| 411 _rttFilter.Update(rttMs); | |
| 412 } | 376 } |
| 413 | 377 |
| 414 void | 378 void VCMJitterEstimator::UpdateMaxFrameSize(uint32_t frameSizeBytes) { |
| 415 VCMJitterEstimator::UpdateMaxFrameSize(uint32_t frameSizeBytes) | 379 if (_maxFrameSize < frameSizeBytes) { |
| 416 { | 380 _maxFrameSize = frameSizeBytes; |
| 417 if (_maxFrameSize < frameSizeBytes) | 381 } |
| 418 { | |
| 419 _maxFrameSize = frameSizeBytes; | |
| 420 } | |
| 421 } | 382 } |
| 422 | 383 |
| 423 // Returns the current filtered estimate if available, | 384 // Returns the current filtered estimate if available, |
| 424 // otherwise tries to calculate an estimate. | 385 // otherwise tries to calculate an estimate. |
| 425 int VCMJitterEstimator::GetJitterEstimate(double rttMultiplier) { | 386 int VCMJitterEstimator::GetJitterEstimate(double rttMultiplier) { |
| 426 double jitterMS = CalculateEstimate() + OPERATING_SYSTEM_JITTER; | 387 double jitterMS = CalculateEstimate() + OPERATING_SYSTEM_JITTER; |
| 427 if (_filterJitterEstimate > jitterMS) | 388 if (_filterJitterEstimate > jitterMS) |
| 428 jitterMS = _filterJitterEstimate; | 389 jitterMS = _filterJitterEstimate; |
| 429 if (_nackCount >= _nackLimit) | 390 if (_nackCount >= _nackLimit) |
| 430 jitterMS += _rttFilter.RttMs() * rttMultiplier; | 391 jitterMS += _rttFilter.RttMs() * rttMultiplier; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 return 0; | 432 return 0; |
| 472 | 433 |
| 473 double fps = 1000000.0 / fps_counter_.ComputeMean(); | 434 double fps = 1000000.0 / fps_counter_.ComputeMean(); |
| 474 // Sanity check. | 435 // Sanity check. |
| 475 assert(fps >= 0.0); | 436 assert(fps >= 0.0); |
| 476 if (fps > kMaxFramerateEstimate) { | 437 if (fps > kMaxFramerateEstimate) { |
| 477 fps = kMaxFramerateEstimate; | 438 fps = kMaxFramerateEstimate; |
| 478 } | 439 } |
| 479 return fps; | 440 return fps; |
| 480 } | 441 } |
| 481 | 442 } // namespace webrtc |
| 482 } | |
| OLD | NEW |