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