修复NTP时间戳计算精度不足导致误差累积问题 (#2576 #2570 )

ntp时间戳计算精度由毫秒调整为微秒,解决误差累积问题。
This commit is contained in:
Luosh 2023-06-21 17:20:39 +08:00 committed by GitHub
parent 9f753b5e5f
commit 24d210f61d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 31 deletions

View File

@ -236,68 +236,68 @@ void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
WarnL << "Invalid sender report rtcp, ntp_stamp_ms = " << ntp_stamp_ms << ", rtp_stamp = " << rtp_stamp; WarnL << "Invalid sender report rtcp, ntp_stamp_ms = " << ntp_stamp_ms << ", rtp_stamp = " << rtp_stamp;
return; return;
} }
update(rtp_stamp, ntp_stamp_ms); update(rtp_stamp, ntp_stamp_ms * 1000);
} }
void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) { void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_us) {
_last_rtp_stamp = rtp_stamp; _last_rtp_stamp = rtp_stamp;
_last_ntp_stamp_ms = ntp_stamp_ms; _last_ntp_stamp_us = ntp_stamp_us;
} }
uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) { uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) {
if (rtp_stamp == _last_rtp_stamp) { if (rtp_stamp == _last_rtp_stamp) {
return _last_ntp_stamp_ms; return _last_ntp_stamp_us / 1000;
} }
return getNtpStamp_l(rtp_stamp, sample_rate); return getNtpStampUS(rtp_stamp, sample_rate) / 1000;
} }
uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) { uint64_t NtpStamp::getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate) {
if (!_last_ntp_stamp_ms) { if (!_last_ntp_stamp_us) {
// 尚未收到sender report rtcp包那么赋值为本地系统时间戳吧 // 尚未收到sender report rtcp包那么赋值为本地系统时间戳吧
update(rtp_stamp, getCurrentMillisecond(true)); update(rtp_stamp, getCurrentMicrosecond(true));
} }
// rtp时间戳正增长 // rtp时间戳正增长
if (rtp_stamp >= _last_rtp_stamp) { if (rtp_stamp >= _last_rtp_stamp) {
auto diff = static_cast<int>((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f)); auto diff_us = static_cast<int64_t>((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000000.0f));
if (diff < MAX_DELTA_STAMP) { if (diff_us < MAX_DELTA_STAMP * 1000) {
// 时间戳正常增长 // 时间戳正常增长
update(rtp_stamp, _last_ntp_stamp_ms + diff); update(rtp_stamp, _last_ntp_stamp_us + diff_us);
return _last_ntp_stamp_ms; return _last_ntp_stamp_us;
} }
// 时间戳大幅跳跃 // 时间戳大幅跳跃
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000;
if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) { if (_last_rtp_stamp < loop_delta_hz && rtp_stamp > UINT32_MAX - loop_delta_hz) {
// 应该是rtp时间戳溢出+乱序 // 应该是rtp时间戳溢出+乱序
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate;
return _last_ntp_stamp_ms + diff - max_rtp_ms; return _last_ntp_stamp_us + diff_us - max_rtp_us;
} }
// 不明原因的时间戳大幅跳跃,直接返回上次值 // 不明原因的时间戳大幅跳跃,直接返回上次值
WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp; WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp;
update(rtp_stamp, _last_ntp_stamp_ms); update(rtp_stamp, _last_ntp_stamp_us);
return _last_ntp_stamp_ms; return _last_ntp_stamp_us;
} }
// rtp时间戳负增长 // rtp时间戳负增长
auto diff = static_cast<int>((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f)); auto diff_us = static_cast<int64_t>((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000000.0f));
if (diff < MAX_DELTA_STAMP) { if (diff_us < MAX_DELTA_STAMP * 1000) {
// 正常范围的时间戳回退说明收到rtp乱序了 // 正常范围的时间戳回退说明收到rtp乱序了
return _last_ntp_stamp_ms - diff; return _last_ntp_stamp_us - diff_us;
} }
// 时间戳大幅度回退 // 时间戳大幅度回退
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000;
if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) { if (rtp_stamp < loop_delta_hz && _last_rtp_stamp > UINT32_MAX - loop_delta_hz) {
// 确定是时间戳溢出 // 确定是时间戳溢出
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate;
update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff)); update(rtp_stamp, _last_ntp_stamp_us + (max_rtp_us - diff_us));
return _last_ntp_stamp_ms; return _last_ntp_stamp_us;
} }
// 不明原因的时间戳回退,直接返回上次值 // 不明原因的时间戳回退,直接返回上次值
WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp; WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp;
update(rtp_stamp, _last_ntp_stamp_ms); update(rtp_stamp, _last_ntp_stamp_us);
return _last_ntp_stamp_ms; return _last_ntp_stamp_us;
} }
} // namespace mediakit } // namespace mediakit

View File

@ -125,12 +125,12 @@ public:
uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate); uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate);
private: private:
void update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms); void update(uint32_t rtp_stamp, uint64_t ntp_stamp_us);
uint64_t getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate); uint64_t getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate);
private: private:
uint32_t _last_rtp_stamp = 0; uint32_t _last_rtp_stamp = 0;
uint64_t _last_ntp_stamp_ms = 0; uint64_t _last_ntp_stamp_us = 0;
}; };
}//namespace mediakit }//namespace mediakit