2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
17 TimestampExtrapolator::TimestampExtrapolator(int64_t start_ms)
18 : _rwLock(RWLockWrapper::CreateRWLock()),
22 _prevUnwrappedTimestamp(-1),
23 _prevWrapTimestamp(-1),
25 _firstAfterReset(true),
27 _startUpFilterDelayInPackets(2),
28 _detectorAccumulatorPos(0),
29 _detectorAccumulatorNeg(0),
30 _alarmThreshold(60e3),
31 _accDrift(6600), // in timestamp ticks, i.e. 15 ms
37 TimestampExtrapolator::~TimestampExtrapolator()
42 void TimestampExtrapolator::Reset(int64_t start_ms)
44 WriteLockScoped wl(*_rwLock);
52 _pP[0][1] = _pP[1][0] = 0;
53 _firstAfterReset = true;
54 _prevUnwrappedTimestamp = -1;
55 _prevWrapTimestamp = -1;
58 _detectorAccumulatorPos = 0;
59 _detectorAccumulatorNeg = 0;
63 TimestampExtrapolator::Update(int64_t tMs, uint32_t ts90khz)
66 _rwLock->AcquireLockExclusive();
67 if (tMs - _prevMs > 10e3)
69 // Ten seconds without a complete frame.
70 // Reset the extrapolator
71 _rwLock->ReleaseLockExclusive();
73 _rwLock->AcquireLockExclusive();
80 // Remove offset to prevent badly scaled matrices
83 CheckForWrapArounds(ts90khz);
85 int64_t unwrapped_ts90khz = static_cast<int64_t>(ts90khz) +
86 _wrapArounds * ((static_cast<int64_t>(1) << 32) - 1);
88 if (_prevUnwrappedTimestamp >= 0 &&
89 unwrapped_ts90khz < _prevUnwrappedTimestamp)
91 // Drop reordered frames.
92 _rwLock->ReleaseLockExclusive();
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;
102 _firstTimestamp = unwrapped_ts90khz;
103 _firstAfterReset = false;
107 (static_cast<double>(unwrapped_ts90khz) - _firstTimestamp) -
108 static_cast<double>(tMs) * _w[0] - _w[1];
109 if (DelayChangeDetection(residual) &&
110 _packetCount >= _startUpFilterDelayInPackets)
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.
119 //K = P*T/(lambda + T'*P*T);
121 K[0] = _pP[0][0] * tMs + _pP[0][1];
122 K[1] = _pP[1][0] * tMs + _pP[1][1];
123 double TPT = _lambda + tMs * K[0] + K[1];
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);
130 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]));
140 _prevUnwrappedTimestamp = unwrapped_ts90khz;
141 if (_packetCount < _startUpFilterDelayInPackets)
145 _rwLock->ReleaseLockExclusive();
149 TimestampExtrapolator::ExtrapolateLocalTime(uint32_t timestamp90khz)
151 ReadLockScoped rl(*_rwLock);
152 int64_t localTimeMs = 0;
153 CheckForWrapArounds(timestamp90khz);
154 double unwrapped_ts90khz = static_cast<double>(timestamp90khz) +
155 _wrapArounds * ((static_cast<int64_t>(1) << 32) - 1);
156 if (_packetCount == 0)
160 else if (_packetCount < _startUpFilterDelayInPackets)
162 localTimeMs = _prevMs + static_cast<int64_t>(
163 static_cast<double>(unwrapped_ts90khz - _prevUnwrappedTimestamp) /
170 localTimeMs = _startMs;
174 double timestampDiff = unwrapped_ts90khz -
175 static_cast<double>(_firstTimestamp);
176 localTimeMs = static_cast<int64_t>(
177 static_cast<double>(_startMs) + (timestampDiff - _w[1]) /
184 // Investigates if the timestamp clock has overflowed since the last timestamp and
185 // keeps track of the number of wrap arounds since reset.
187 TimestampExtrapolator::CheckForWrapArounds(uint32_t ts90khz)
189 if (_prevWrapTimestamp == -1)
191 _prevWrapTimestamp = ts90khz;
194 if (ts90khz < _prevWrapTimestamp)
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.
199 if (static_cast<int32_t>(ts90khz - _prevWrapTimestamp) > 0)
201 // Forward wrap around
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.
207 else if (static_cast<int32_t>(_prevWrapTimestamp - ts90khz) > 0)
209 // Backward wrap around
212 _prevWrapTimestamp = ts90khz;
216 TimestampExtrapolator::DelayChangeDetection(double error)
218 // CUSUM detection of sudden delay changes
219 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);
225 if (_detectorAccumulatorPos > _alarmThreshold || _detectorAccumulatorNeg < -_alarmThreshold)
228 _detectorAccumulatorPos = _detectorAccumulatorNeg = 0;