blob: f2b70926863bd3e3e2956f4ad9bd1148ae54d501 [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
wu@webrtc.org66773a02014-05-07 17:09:44 +000011#include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
wu@webrtc.orged4cb562014-05-06 04:50:49 +000012
13#include <algorithm>
niklase@google.com470e71d2011-07-07 08:21:25 +000014
15namespace webrtc {
16
wu@webrtc.org66773a02014-05-07 17:09:44 +000017TimestampExtrapolator::TimestampExtrapolator(int64_t start_ms)
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000018 : _rwLock(RWLockWrapper::CreateRWLock()),
stefan@webrtc.org34c5da62014-04-11 14:08:35 +000019 _startMs(0),
20 _firstTimestamp(0),
21 _wrapArounds(0),
22 _prevUnwrappedTimestamp(-1),
23 _prevWrapTimestamp(-1),
24 _lambda(1),
25 _firstAfterReset(true),
26 _packetCount(0),
27 _startUpFilterDelayInPackets(2),
28 _detectorAccumulatorPos(0),
29 _detectorAccumulatorNeg(0),
30 _alarmThreshold(60e3),
31 _accDrift(6600), // in timestamp ticks, i.e. 15 ms
32 _accMaxError(7000),
andrew@webrtc.orgcc476aa2014-10-31 16:01:25 +000033 _pP11(1e10) {
wu@webrtc.orged4cb562014-05-06 04:50:49 +000034 Reset(start_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
wu@webrtc.org66773a02014-05-07 17:09:44 +000037TimestampExtrapolator::~TimestampExtrapolator()
niklase@google.com470e71d2011-07-07 08:21:25 +000038{
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000039 delete _rwLock;
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
wu@webrtc.org66773a02014-05-07 17:09:44 +000042void TimestampExtrapolator::Reset(int64_t start_ms)
niklase@google.com470e71d2011-07-07 08:21:25 +000043{
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000044 WriteLockScoped wl(*_rwLock);
wu@webrtc.orged4cb562014-05-06 04:50:49 +000045 _startMs = start_ms;
niklase@google.com470e71d2011-07-07 08:21:25 +000046 _prevMs = _startMs;
47 _firstTimestamp = 0;
48 _w[0] = 90.0;
49 _w[1] = 0;
andrew@webrtc.orgcc476aa2014-10-31 16:01:25 +000050 _pP[0][0] = 1;
51 _pP[1][1] = _pP11;
52 _pP[0][1] = _pP[1][0] = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000053 _firstAfterReset = true;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000054 _prevUnwrappedTimestamp = -1;
55 _prevWrapTimestamp = -1;
niklase@google.com470e71d2011-07-07 08:21:25 +000056 _wrapArounds = 0;
57 _packetCount = 0;
58 _detectorAccumulatorPos = 0;
59 _detectorAccumulatorNeg = 0;
60}
61
62void
wu@webrtc.org66773a02014-05-07 17:09:44 +000063TimestampExtrapolator::Update(int64_t tMs, uint32_t ts90khz)
niklase@google.com470e71d2011-07-07 08:21:25 +000064{
65
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000066 _rwLock->AcquireLockExclusive();
niklase@google.com470e71d2011-07-07 08:21:25 +000067 if (tMs - _prevMs > 10e3)
68 {
69 // Ten seconds without a complete frame.
70 // Reset the extrapolator
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000071 _rwLock->ReleaseLockExclusive();
wu@webrtc.orged4cb562014-05-06 04:50:49 +000072 Reset(tMs);
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000073 _rwLock->AcquireLockExclusive();
niklase@google.com470e71d2011-07-07 08:21:25 +000074 }
75 else
76 {
77 _prevMs = tMs;
78 }
79
80 // Remove offset to prevent badly scaled matrices
81 tMs -= _startMs;
82
niklase@google.com470e71d2011-07-07 08:21:25 +000083 CheckForWrapArounds(ts90khz);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000085 int64_t unwrapped_ts90khz = static_cast<int64_t>(ts90khz) +
86 _wrapArounds * ((static_cast<int64_t>(1) << 32) - 1);
87
88 if (_prevUnwrappedTimestamp >= 0 &&
89 unwrapped_ts90khz < _prevUnwrappedTimestamp)
niklase@google.com470e71d2011-07-07 08:21:25 +000090 {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +000091 // Drop reordered frames.
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +000092 _rwLock->ReleaseLockExclusive();
niklase@google.com470e71d2011-07-07 08:21:25 +000093 return;
94 }
95
96 if (_firstAfterReset)
97 {
98 // Make an initial guess of the offset,
99 // should be almost correct since tMs - _startMs
100 // should about zero at this time.
101 _w[1] = -_w[0] * tMs;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000102 _firstTimestamp = unwrapped_ts90khz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 _firstAfterReset = false;
104 }
105
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000106 double residual =
107 (static_cast<double>(unwrapped_ts90khz) - _firstTimestamp) -
108 static_cast<double>(tMs) * _w[0] - _w[1];
stefan@webrtc.org34c5da62014-04-11 14:08:35 +0000109 if (DelayChangeDetection(residual) &&
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 _packetCount >= _startUpFilterDelayInPackets)
111 {
112 // A sudden change of average network delay has been detected.
113 // Force the filter to adjust its offset parameter by changing
114 // the offset uncertainty. Don't do this during startup.
andrew@webrtc.orgcc476aa2014-10-31 16:01:25 +0000115 _pP[1][1] = _pP11;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 }
117 //T = [t(k) 1]';
118 //that = T'*w;
119 //K = P*T/(lambda + T'*P*T);
120 double K[2];
andrew@webrtc.orgcc476aa2014-10-31 16:01:25 +0000121 K[0] = _pP[0][0] * tMs + _pP[0][1];
122 K[1] = _pP[1][0] * tMs + _pP[1][1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 double TPT = _lambda + tMs * K[0] + K[1];
124 K[0] /= TPT;
125 K[1] /= TPT;
126 //w = w + K*(ts(k) - that);
127 _w[0] = _w[0] + K[0] * residual;
128 _w[1] = _w[1] + K[1] * residual;
129 //P = 1/lambda*(P - K*T'*P);
andrew@webrtc.orgcc476aa2014-10-31 16:01:25 +0000130 double p00 = 1 / _lambda *
131 (_pP[0][0] - (K[0] * tMs * _pP[0][0] + K[0] * _pP[1][0]));
132 double p01 = 1 / _lambda *
133 (_pP[0][1] - (K[0] * tMs * _pP[0][1] + K[0] * _pP[1][1]));
134 _pP[1][0] = 1 / _lambda *
135 (_pP[1][0] - (K[1] * tMs * _pP[0][0] + K[1] * _pP[1][0]));
136 _pP[1][1] = 1 / _lambda *
137 (_pP[1][1] - (K[1] * tMs * _pP[0][1] + K[1] * _pP[1][1]));
138 _pP[0][0] = p00;
139 _pP[0][1] = p01;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000140 _prevUnwrappedTimestamp = unwrapped_ts90khz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 if (_packetCount < _startUpFilterDelayInPackets)
142 {
143 _packetCount++;
144 }
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +0000145 _rwLock->ReleaseLockExclusive();
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000148int64_t
wu@webrtc.org66773a02014-05-07 17:09:44 +0000149TimestampExtrapolator::ExtrapolateLocalTime(uint32_t timestamp90khz)
niklase@google.com470e71d2011-07-07 08:21:25 +0000150{
stefan@webrtc.org7889a9b2011-12-12 08:18:24 +0000151 ReadLockScoped rl(*_rwLock);
pbos@webrtc.org7b859cc2013-04-02 15:54:38 +0000152 int64_t localTimeMs = 0;
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000153 CheckForWrapArounds(timestamp90khz);
154 double unwrapped_ts90khz = static_cast<double>(timestamp90khz) +
155 _wrapArounds * ((static_cast<int64_t>(1) << 32) - 1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 if (_packetCount == 0)
157 {
158 localTimeMs = -1;
159 }
160 else if (_packetCount < _startUpFilterDelayInPackets)
161 {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000162 localTimeMs = _prevMs + static_cast<int64_t>(
163 static_cast<double>(unwrapped_ts90khz - _prevUnwrappedTimestamp) /
164 90.0 + 0.5);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165 }
166 else
167 {
168 if (_w[0] < 1e-3)
169 {
170 localTimeMs = _startMs;
171 }
172 else
173 {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000174 double timestampDiff = unwrapped_ts90khz -
175 static_cast<double>(_firstTimestamp);
176 localTimeMs = static_cast<int64_t>(
177 static_cast<double>(_startMs) + (timestampDiff - _w[1]) /
178 _w[0] + 0.5);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179 }
180 }
181 return localTimeMs;
182}
183
184// Investigates if the timestamp clock has overflowed since the last timestamp and
185// keeps track of the number of wrap arounds since reset.
186void
wu@webrtc.org66773a02014-05-07 17:09:44 +0000187TimestampExtrapolator::CheckForWrapArounds(uint32_t ts90khz)
niklase@google.com470e71d2011-07-07 08:21:25 +0000188{
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000189 if (_prevWrapTimestamp == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 {
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000191 _prevWrapTimestamp = ts90khz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000192 return;
193 }
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000194 if (ts90khz < _prevWrapTimestamp)
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 {
196 // This difference will probably be less than -2^31 if we have had a wrap around
197 // (e.g. timestamp = 1, _previousTimestamp = 2^32 - 1). Since it is casted to a Word32,
198 // it should be positive.
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000199 if (static_cast<int32_t>(ts90khz - _prevWrapTimestamp) > 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 {
201 // Forward wrap around
202 _wrapArounds++;
203 }
204 }
205 // This difference will probably be less than -2^31 if we have had a backward wrap around.
206 // Since it is casted to a Word32, it should be positive.
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000207 else if (static_cast<int32_t>(_prevWrapTimestamp - ts90khz) > 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 {
209 // Backward wrap around
210 _wrapArounds--;
211 }
stefan@webrtc.org9f557c12013-05-17 12:55:07 +0000212 _prevWrapTimestamp = ts90khz;
niklase@google.com470e71d2011-07-07 08:21:25 +0000213}
214
215bool
wu@webrtc.org66773a02014-05-07 17:09:44 +0000216TimestampExtrapolator::DelayChangeDetection(double error)
niklase@google.com470e71d2011-07-07 08:21:25 +0000217{
218 // CUSUM detection of sudden delay changes
wu@webrtc.orged4cb562014-05-06 04:50:49 +0000219 error = (error > 0) ? std::min(error, _accMaxError) :
220 std::max(error, -_accMaxError);
221 _detectorAccumulatorPos =
222 std::max(_detectorAccumulatorPos + error - _accDrift, (double)0);
223 _detectorAccumulatorNeg =
224 std::min(_detectorAccumulatorNeg + error + _accDrift, (double)0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 if (_detectorAccumulatorPos > _alarmThreshold || _detectorAccumulatorNeg < -_alarmThreshold)
226 {
227 // Alarm
niklase@google.com470e71d2011-07-07 08:21:25 +0000228 _detectorAccumulatorPos = _detectorAccumulatorNeg = 0;
229 return true;
230 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 return false;
232}
233
234}