| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2021 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
#include "tsbpd_time.h" |
| 11 |
|
| 12 |
#include "logging.h" |
| 13 |
#include "logger_defs.h" |
| 14 |
#include "packet.h" |
| 15 |
|
| 16 |
using namespace srt_logging; |
| 17 |
using namespace srt::sync; |
| 18 |
|
| 19 |
namespace srt |
| 20 |
{ |
| 21 |
|
| 22 |
#if SRT_DEBUG_TRACE_DRIFT |
| 23 |
class drift_logger |
| 24 |
{ |
| 25 |
typedef srt::sync::steady_clock steady_clock; |
| 26 |
|
| 27 |
public: |
| 28 |
drift_logger() {} |
| 29 |
|
| 30 |
~drift_logger() |
| 31 |
{ |
| 32 |
ScopedLock lck(m_mtx); |
| 33 |
m_fout.close(); |
| 34 |
} |
| 35 |
|
| 36 |
void trace(unsigned ackack_timestamp, |
| 37 |
int rtt_us, |
| 38 |
int64_t drift_sample, |
| 39 |
int64_t drift, |
| 40 |
int64_t overdrift, |
| 41 |
const srt::sync::steady_clock::time_point& pkt_base, |
| 42 |
const srt::sync::steady_clock::time_point& tsbpd_base) |
| 43 |
{ |
| 44 |
using namespace srt::sync; |
| 45 |
ScopedLock lck(m_mtx); |
| 46 |
create_file(); |
| 47 |
|
| 48 |
// std::string str_tnow = srt::sync::FormatTime(steady_clock::now()); |
| 49 |
// str_tnow.resize(str_tnow.size() - 7); // remove trailing ' [STDY]' part |
| 50 |
|
| 51 |
std::string str_tbase = srt::sync::FormatTime(tsbpd_base); |
| 52 |
str_tbase.resize(str_tbase.size() - 7); // remove trailing ' [STDY]' part |
| 53 |
|
| 54 |
std::string str_pkt_base = srt::sync::FormatTime(pkt_base); |
| 55 |
str_pkt_base.resize(str_pkt_base.size() - 7); // remove trailing ' [STDY]' part |
| 56 |
|
| 57 |
// m_fout << str_tnow << ","; |
| 58 |
m_fout << count_microseconds(steady_clock::now() - m_start_time) << ","; |
| 59 |
m_fout << ackack_timestamp << ","; |
| 60 |
m_fout << rtt_us << ","; |
| 61 |
m_fout << drift_sample << ","; |
| 62 |
m_fout << drift << ","; |
| 63 |
m_fout << overdrift << ","; |
| 64 |
m_fout << str_pkt_base << ","; |
| 65 |
m_fout << str_tbase << "\n"; |
| 66 |
m_fout.flush(); |
| 67 |
} |
| 68 |
|
| 69 |
private: |
| 70 |
void print_header() |
| 71 |
{ |
| 72 |
m_fout << "usElapsedStd,usAckAckTimestampStd,"; |
| 73 |
m_fout << "usRTTStd,usDriftSampleStd,usDriftStd,usOverdriftStd,tsPktBase,TSBPDBase\n"; |
| 74 |
} |
| 75 |
|
| 76 |
void create_file() |
| 77 |
{ |
| 78 |
if (m_fout.is_open()) |
| 79 |
return; |
| 80 |
|
| 81 |
m_start_time = srt::sync::steady_clock::now(); |
| 82 |
std::string str_tnow = srt::sync::FormatTimeSys(m_start_time); |
| 83 |
str_tnow.resize(str_tnow.size() - 7); // remove trailing ' [SYST]' part |
| 84 |
while (str_tnow.find(':') != std::string::npos) |
| 85 |
{ |
| 86 |
str_tnow.replace(str_tnow.find(':'), 1, 1, '_'); |
| 87 |
} |
| 88 |
const std::string fname = "drift_trace_" + str_tnow + ".csv"; |
| 89 |
m_fout.open(fname, std::ofstream::out); |
| 90 |
if (!m_fout) |
| 91 |
std::cerr << "IPE: Failed to open " << fname << "!!!\n"; |
| 92 |
|
| 93 |
print_header(); |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
srt::sync::Mutex m_mtx; |
| 98 |
std::ofstream m_fout; |
| 99 |
srt::sync::steady_clock::time_point m_start_time; |
| 100 |
}; |
| 101 |
|
| 102 |
drift_logger g_drift_logger; |
| 103 |
|
| 104 |
#endif // SRT_DEBUG_TRACE_DRIFT |
| 105 |
|
| 106 |
bool CTsbpdTime::addDriftSample(uint32_t usPktTimestamp, const time_point& tsPktArrival, int usRTTSample) |
| 107 |
{ |
| 108 |
if (!m_bTsbPdMode) |
| 109 |
return false; |
| 110 |
|
| 111 |
ExclusiveLock lck(m_mtxRW); |
| 112 |
|
| 113 |
// Remember the first RTT sample measured. Ideally we need RTT0 - the one from the handshaking phase, |
| 114 |
// because TSBPD base is initialized there. But HS-based RTT is not yet implemented. |
| 115 |
// Take the first one assuming it is close to RTT0. |
| 116 |
if (m_iFirstRTT == -1) |
| 117 |
{ |
| 118 |
m_iFirstRTT = usRTTSample; |
| 119 |
} |
| 120 |
|
| 121 |
// A change in network delay has to be taken into account. The only way to get some estimation of it |
| 122 |
// is to estimate RTT change and assume that the change of the one way network delay is |
| 123 |
// approximated by the half of the RTT change. |
| 124 |
const duration tdRTTDelta = usRTTSample >= 0 ? microseconds_from((usRTTSample - m_iFirstRTT) / 2) : duration(0); |
| 125 |
const time_point tsPktBaseTime = getPktBaseTimeNoLock(usPktTimestamp); |
| 126 |
const steady_clock::duration tdDrift = tsPktArrival - tsPktBaseTime - tdRTTDelta; |
| 127 |
|
| 128 |
const bool updated = m_DriftTracer.update(count_microseconds(tdDrift)); |
| 129 |
|
| 130 |
if (updated) |
| 131 |
{ |
| 132 |
IF_HEAVY_LOGGING(const steady_clock::time_point oldbase = m_tsTsbPdTimeBase); |
| 133 |
steady_clock::duration overdrift = microseconds_from(m_DriftTracer.overdrift()); |
| 134 |
m_tsTsbPdTimeBase += overdrift; |
| 135 |
|
| 136 |
HLOGC(brlog.Debug, |
| 137 |
log << "DRIFT=" << FormatDuration(tdDrift) << " AVG=" << (m_DriftTracer.drift() / 1000.0) |
| 138 |
<< "ms, TB: " << FormatTime(oldbase) << " EXCESS: " << FormatDuration(overdrift) |
| 139 |
<< " UPDATED TO: " << FormatTime(m_tsTsbPdTimeBase)); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
HLOGC(brlog.Debug, |
| 144 |
log << "DRIFT=" << FormatDuration(tdDrift) << " TB REMAINS: " << FormatTime(m_tsTsbPdTimeBase)); |
| 145 |
} |
| 146 |
|
| 147 |
#if SRT_DEBUG_TRACE_DRIFT |
| 148 |
g_drift_logger.trace(usPktTimestamp, |
| 149 |
usRTTSample, |
| 150 |
count_microseconds(tdDrift), |
| 151 |
m_DriftTracer.drift(), |
| 152 |
m_DriftTracer.overdrift(), |
| 153 |
tsPktBaseTime, |
| 154 |
m_tsTsbPdTimeBase); |
| 155 |
#endif |
| 156 |
return updated; |
| 157 |
} |
| 158 |
|
| 159 |
void CTsbpdTime::setTsbPdMode(const steady_clock::time_point& timebase, bool wrap, duration delay) |
| 160 |
{ |
| 161 |
ExclusiveLock lck(m_mtxRW); |
| 162 |
m_bTsbPdMode = true; |
| 163 |
m_bTsbPdWrapCheck = wrap; |
| 164 |
|
| 165 |
// Timebase passed here comes is calculated as: |
| 166 |
// Tnow - hspkt.m_iTimeStamp |
| 167 |
// where hspkt is the packet with SRT_CMD_HSREQ message. |
| 168 |
// |
| 169 |
// This function is called in the HSREQ reception handler only. |
| 170 |
m_tsTsbPdTimeBase = timebase; |
| 171 |
m_tdTsbPdDelay = delay; |
| 172 |
} |
| 173 |
|
| 174 |
void CTsbpdTime::applyGroupTime(const steady_clock::time_point& timebase, |
| 175 |
bool wrp, |
| 176 |
uint32_t delay, |
| 177 |
const steady_clock::duration& udrift) |
| 178 |
{ |
| 179 |
// Same as setTsbPdMode, but predicted to be used for group members. |
| 180 |
// This synchronizes the time from the INTERNAL TIMEBASE of an existing |
| 181 |
// socket's internal timebase. This is required because the initial time |
| 182 |
// base stays always the same, whereas the internal timebase undergoes |
| 183 |
// adjustment as the 32-bit timestamps in the sockets wrap. The socket |
| 184 |
// newly added to the group must get EXACTLY the same internal timebase |
| 185 |
// or otherwise the TsbPd time calculation will ship different results |
| 186 |
// on different member sockets. |
| 187 |
ExclusiveLock lck(m_mtxRW); |
| 188 |
|
| 189 |
m_bTsbPdMode = true; |
| 190 |
|
| 191 |
m_tsTsbPdTimeBase = timebase; |
| 192 |
m_bTsbPdWrapCheck = wrp; |
| 193 |
m_tdTsbPdDelay = microseconds_from(delay); |
| 194 |
m_DriftTracer.forceDrift(count_microseconds(udrift)); |
| 195 |
} |
| 196 |
|
| 197 |
void CTsbpdTime::applyGroupDrift(const steady_clock::time_point& timebase, |
| 198 |
bool wrp, |
| 199 |
const steady_clock::duration& udrift) |
| 200 |
{ |
| 201 |
ExclusiveLock lck(m_mtxRW); |
| 202 |
// This is only when a drift was updated on one of the group members. |
| 203 |
HLOGC(brlog.Debug, |
| 204 |
log << "rcv-buffer: group synch uDRIFT: " << m_DriftTracer.drift() << " -> " << FormatDuration(udrift) |
| 205 |
<< " TB: " << FormatTime(m_tsTsbPdTimeBase) << " -> " << FormatTime(timebase)); |
| 206 |
|
| 207 |
m_tsTsbPdTimeBase = timebase; |
| 208 |
m_bTsbPdWrapCheck = wrp; |
| 209 |
|
| 210 |
m_DriftTracer.forceDrift(count_microseconds(udrift)); |
| 211 |
} |
| 212 |
|
| 213 |
CTsbpdTime::time_point CTsbpdTime::getBaseTimeNoLock(uint32_t timestamp_us) const |
| 214 |
{ |
| 215 |
// A data packet within [TSBPD_WRAP_PERIOD; 2 * TSBPD_WRAP_PERIOD] would end TSBPD wrap-aware state. |
| 216 |
// Some incoming control packets may not update the TSBPD base (calling updateBaseTime(..)), |
| 217 |
// but may come before a data packet with a timestamp in this range. Therefore the whole range should be tracked. |
| 218 |
const int64_t carryover_us = |
| 219 |
(m_bTsbPdWrapCheck && timestamp_us <= 2 * TSBPD_WRAP_PERIOD) ? int64_t(CPacket::MAX_TIMESTAMP) + 1 : 0; |
| 220 |
|
| 221 |
return (m_tsTsbPdTimeBase + microseconds_from(carryover_us)); |
| 222 |
} |
| 223 |
|
| 224 |
CTsbpdTime::time_point CTsbpdTime::getBaseTime(uint32_t timestamp_us) const |
| 225 |
{ |
| 226 |
SharedLock lck(m_mtxRW); |
| 227 |
return getBaseTimeNoLock(timestamp_us); |
| 228 |
} |
| 229 |
|
| 230 |
CTsbpdTime::time_point CTsbpdTime::getPktTime(uint32_t usPktTimestamp) const |
| 231 |
{ |
| 232 |
SharedLock lck(m_mtxRW); |
| 233 |
time_point value = getPktBaseTimeNoLock(usPktTimestamp) + m_tdTsbPdDelay + microseconds_from(m_DriftTracer.drift()); |
| 234 |
|
| 235 |
return value; |
| 236 |
} |
| 237 |
|
| 238 |
CTsbpdTime::time_point CTsbpdTime::getPktBaseTimeNoLock(uint32_t usPktTimestamp) const |
| 239 |
{ |
| 240 |
return getBaseTimeNoLock(usPktTimestamp) + microseconds_from(usPktTimestamp); |
| 241 |
} |
| 242 |
|
| 243 |
CTsbpdTime::time_point CTsbpdTime::getPktBaseTime(uint32_t usPktTimestamp) const |
| 244 |
{ |
| 245 |
return getBaseTime(usPktTimestamp) + microseconds_from(usPktTimestamp); |
| 246 |
} |
| 247 |
|
| 248 |
void CTsbpdTime::updateBaseTime(uint32_t usPktTimestamp) |
| 249 |
{ |
| 250 |
ExclusiveLock lck(m_mtxRW); |
| 251 |
if (m_bTsbPdWrapCheck) |
| 252 |
{ |
| 253 |
// Wrap check period. |
| 254 |
if ((usPktTimestamp >= TSBPD_WRAP_PERIOD) && (usPktTimestamp <= (TSBPD_WRAP_PERIOD * 2))) |
| 255 |
{ |
| 256 |
/* Exiting wrap check period (if for packet delivery head) */ |
| 257 |
m_bTsbPdWrapCheck = false; |
| 258 |
m_tsTsbPdTimeBase += microseconds_from(int64_t(CPacket::MAX_TIMESTAMP) + 1); |
| 259 |
LOGC(tslog.Debug, |
| 260 |
log << "tsbpd wrap period ends with ts=" << usPktTimestamp << " - NEW TIME BASE: " |
| 261 |
<< FormatTime(m_tsTsbPdTimeBase) << " drift: " << m_DriftTracer.drift() << "us"); |
| 262 |
} |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
// Check if timestamp is within the TSBPD_WRAP_PERIOD before reaching the MAX_TIMESTAMP. |
| 267 |
if (usPktTimestamp > (CPacket::MAX_TIMESTAMP - TSBPD_WRAP_PERIOD)) |
| 268 |
{ |
| 269 |
// Approaching wrap around point, start wrap check period (if for packet delivery head) |
| 270 |
m_bTsbPdWrapCheck = true; |
| 271 |
LOGC(tslog.Debug, |
| 272 |
log << "tsbpd wrap period begins with ts=" << usPktTimestamp |
| 273 |
<< " TIME BASE: " << FormatTime(m_tsTsbPdTimeBase) << " drift: " << m_DriftTracer.drift() << "us."); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
void CTsbpdTime::getInternalTimeBase(time_point& w_tb, bool& w_wrp, duration& w_udrift) const |
| 278 |
{ |
| 279 |
ExclusiveLock lck(m_mtxRW); |
| 280 |
w_tb = m_tsTsbPdTimeBase; |
| 281 |
w_udrift = microseconds_from(m_DriftTracer.drift()); |
| 282 |
w_wrp = m_bTsbPdWrapCheck; |
| 283 |
} |
| 284 |
|
| 285 |
} // namespace srt |