From 42ef41cb5813ab4c009b634b89a768d970470822 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 22 Jul 2021 20:31:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtp=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E6=98=A0=E5=B0=84=E6=9C=BA=E5=88=B6=E7=9B=B8=E5=85=B3?= =?UTF-8?q?bug:=20#998?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/Stamp.cpp | 52 +++++++++++++++++++++++++++----------------- src/Common/Stamp.h | 9 +++++--- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/Common/Stamp.cpp b/src/Common/Stamp.cpp index 476f3cd0..5d3230aa 100644 --- a/src/Common/Stamp.cpp +++ b/src/Common/Stamp.cpp @@ -219,49 +219,61 @@ bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){ } void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms) { - _rtp_stamp_ms = uint64_t(rtp_stamp) * 1000 / sample_rate; - _ntp_stamp_ms = ntp_stamp_ms; + update(uint64_t(rtp_stamp) * 1000 / sample_rate, ntp_stamp_ms); +} + +void NtpStamp::update(uint32_t rtp_stamp_ms, uint64_t ntp_stamp_ms) { + _last_rtp_stamp_ms = rtp_stamp_ms; + _last_ntp_stamp_ms = ntp_stamp_ms; } uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) { if (rtp_stamp == _last_rtp_stamp) { - return _last_ret; + return _last_ntp_stamp_ms; } + auto ret = getNtpStamp_l(rtp_stamp, sample_rate); + _last_rtp_stamp = rtp_stamp; + return ret; +} + +uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) { uint64_t rtp_stamp_ms = uint64_t(rtp_stamp) * 1000 / sample_rate; - if (!_rtp_stamp_ms && !_ntp_stamp_ms) { + if (!_last_rtp_stamp_ms && !_last_ntp_stamp_ms) { //尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧 - _rtp_stamp_ms = rtp_stamp_ms; - _ntp_stamp_ms = getCurrentMillisecond(true); + update(rtp_stamp_ms, getCurrentMillisecond(true)); } - uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; - if (rtp_stamp_ms > _rtp_stamp_ms) { - auto diff = rtp_stamp_ms - _rtp_stamp_ms; + if (rtp_stamp_ms >= _last_rtp_stamp_ms) { + auto diff = rtp_stamp_ms - _last_rtp_stamp_ms; if (diff < MAX_DELTA_STAMP) { //时间戳正常增长 - _last_ret = _ntp_stamp_ms + diff; - _last_rtp_stamp = rtp_stamp; - return _last_ret; + update(rtp_stamp_ms, _last_ntp_stamp_ms + diff); + return _last_ntp_stamp_ms; } + uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; //时间戳大幅跳跃 - if (_rtp_stamp_ms < STAMP_LOOP_DELTA && rtp_stamp_ms > max_rtp_ms - STAMP_LOOP_DELTA) { + if (_last_rtp_stamp_ms < STAMP_LOOP_DELTA && rtp_stamp_ms > max_rtp_ms - STAMP_LOOP_DELTA) { //应该是rtp时间戳溢出+乱序 - return _ntp_stamp_ms + diff - max_rtp_ms; + return _last_ntp_stamp_ms + diff - max_rtp_ms; } //不明原因的时间戳大幅跳跃,直接返回上次值 - return _last_ret; + WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp; + return _last_ntp_stamp_ms; } - auto diff = _rtp_stamp_ms - rtp_stamp_ms; + auto diff = _last_rtp_stamp_ms - rtp_stamp_ms; if (diff < MAX_DELTA_STAMP) { //正常范围的时间戳回退,说明收到rtp乱序了 - return _ntp_stamp_ms - diff; + return _last_ntp_stamp_ms - diff; } - if (rtp_stamp_ms < STAMP_LOOP_DELTA && _rtp_stamp_ms > max_rtp_ms - STAMP_LOOP_DELTA) { + uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate; + if (rtp_stamp_ms < STAMP_LOOP_DELTA && _last_rtp_stamp_ms > max_rtp_ms - STAMP_LOOP_DELTA) { //确定是时间戳溢出 - return _ntp_stamp_ms + (max_rtp_ms - diff); + update(rtp_stamp_ms, _last_ntp_stamp_ms + (max_rtp_ms - diff)); + return _last_ntp_stamp_ms; } //不明原因的时间戳回退,直接返回上次值 - return _last_ret; + WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp; + return _last_ntp_stamp_ms; } }//namespace mediakit \ No newline at end of file diff --git a/src/Common/Stamp.h b/src/Common/Stamp.h index 079eb48b..0bca9f1b 100644 --- a/src/Common/Stamp.h +++ b/src/Common/Stamp.h @@ -122,11 +122,14 @@ public: void setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms); uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate); +private: + void update(uint32_t rtp_stamp_ms, uint64_t ntp_stamp_ms); + uint64_t getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate); + private: uint32_t _last_rtp_stamp = 0; - uint64_t _rtp_stamp_ms = 0; - uint64_t _ntp_stamp_ms = 0; - uint64_t _last_ret = 0; + uint64_t _last_rtp_stamp_ms = 0; + uint64_t _last_ntp_stamp_ms = 0; }; }//namespace mediakit