From 24d210f61db3fc508cc6344a97ef84a38bbff3a2 Mon Sep 17 00:00:00 2001 From: Luosh Date: Wed, 21 Jun 2023 17:20:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DNTP=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E8=AE=A1=E7=AE=97=E7=B2=BE=E5=BA=A6=E4=B8=8D=E8=B6=B3?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E8=AF=AF=E5=B7=AE=E7=B4=AF=E7=A7=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#2576=20#2570=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ntp时间戳计算精度由毫秒调整为微秒,解决误差累积问题。 --- src/Common/Stamp.cpp | 56 ++++++++++++++++++++++---------------------- src/Common/Stamp.h | 6 ++--- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index a19b96d6..87282c39 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -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; 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_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) { 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) { - if (!_last_ntp_stamp_ms) { +uint64_t NtpStamp::getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate) { + if (!_last_ntp_stamp_us) { // 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧 - update(rtp_stamp, getCurrentMillisecond(true)); + update(rtp_stamp, getCurrentMicrosecond(true)); } // rtp时间戳正增长 if (rtp_stamp >= _last_rtp_stamp) { - auto diff = static_cast((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f)); - if (diff < MAX_DELTA_STAMP) { + auto diff_us = static_cast((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000000.0f)); + if (diff_us < MAX_DELTA_STAMP * 1000) { // 时间戳正常增长 - update(rtp_stamp, _last_ntp_stamp_ms + diff); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us + diff_us); + return _last_ntp_stamp_us; } // 时间戳大幅跳跃 - uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; - if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) { + uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000; + if (_last_rtp_stamp < loop_delta_hz && rtp_stamp > UINT32_MAX - loop_delta_hz) { // 应该是rtp时间戳溢出+乱序 - uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; - return _last_ntp_stamp_ms + diff - max_rtp_ms; + uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate; + return _last_ntp_stamp_us + diff_us - max_rtp_us; } // 不明原因的时间戳大幅跳跃,直接返回上次值 WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp; - update(rtp_stamp, _last_ntp_stamp_ms); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us); + return _last_ntp_stamp_us; } // rtp时间戳负增长 - auto diff = static_cast((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f)); - if (diff < MAX_DELTA_STAMP) { + auto diff_us = static_cast((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000000.0f)); + if (diff_us < MAX_DELTA_STAMP * 1000) { // 正常范围的时间戳回退,说明收到rtp乱序了 - return _last_ntp_stamp_ms - diff; + return _last_ntp_stamp_us - diff_us; } // 时间戳大幅度回退 - uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000; - if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) { + uint64_t loop_delta_hz = STAMP_LOOP_DELTA * sample_rate / 1000; + 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; - update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff)); - return _last_ntp_stamp_ms; + uint64_t max_rtp_us = uint64_t(UINT32_MAX) * 1000000 / sample_rate; + update(rtp_stamp, _last_ntp_stamp_us + (max_rtp_us - diff_us)); + return _last_ntp_stamp_us; } // 不明原因的时间戳回退,直接返回上次值 WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp; - update(rtp_stamp, _last_ntp_stamp_ms); - return _last_ntp_stamp_ms; + update(rtp_stamp, _last_ntp_stamp_us); + return _last_ntp_stamp_us; } } // namespace mediakit diff --git a/src/Common/Stamp.h b/src/Common/Stamp.h index cc2dcae0..03cb7bb7 100644 --- a/src/Common/Stamp.h +++ b/src/Common/Stamp.h @@ -125,12 +125,12 @@ public: uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate); private: - void update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms); - uint64_t getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate); + void update(uint32_t rtp_stamp, uint64_t ntp_stamp_us); + uint64_t getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate); private: uint32_t _last_rtp_stamp = 0; - uint64_t _last_ntp_stamp_ms = 0; + uint64_t _last_ntp_stamp_us = 0; }; }//namespace mediakit