完善ntp时间戳计算逻辑

This commit is contained in:
ziyue 2021-09-02 21:17:59 +08:00
parent 72c2df057a
commit 415bc95dda
8 changed files with 36 additions and 32 deletions

View File

@ -218,13 +218,12 @@ bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){
return false;
}
void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms) {
assert(sample_rate);
update(uint64_t(rtp_stamp) * 1000 / sample_rate, ntp_stamp_ms);
void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
update(rtp_stamp, ntp_stamp_ms);
}
void NtpStamp::update(uint32_t rtp_stamp_ms, uint64_t ntp_stamp_ms) {
_last_rtp_stamp_ms = rtp_stamp_ms;
void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
_last_rtp_stamp = rtp_stamp;
_last_ntp_stamp_ms = ntp_stamp_ms;
}
@ -238,38 +237,45 @@ uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) {
}
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 (!_last_rtp_stamp_ms && !_last_ntp_stamp_ms) {
if (!_last_ntp_stamp_ms) {
//尚未收到sender report rtcp包那么赋值为本地系统时间戳吧
update(rtp_stamp_ms, getCurrentMillisecond(true));
update(rtp_stamp, getCurrentMillisecond(true));
}
if (rtp_stamp_ms >= _last_rtp_stamp_ms) {
auto diff = rtp_stamp_ms - _last_rtp_stamp_ms;
//rtp时间戳正增长
if (rtp_stamp >= _last_rtp_stamp) {
auto diff = (rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f);
if (diff < MAX_DELTA_STAMP) {
//时间戳正常增长
update(rtp_stamp_ms, _last_ntp_stamp_ms + diff);
update(rtp_stamp, _last_ntp_stamp_ms + diff);
return _last_ntp_stamp_ms;
}
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
//时间戳大幅跳跃
if (_last_rtp_stamp_ms < STAMP_LOOP_DELTA && rtp_stamp_ms > max_rtp_ms - STAMP_LOOP_DELTA) {
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) {
//应该是rtp时间戳溢出+乱序
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
return _last_ntp_stamp_ms + diff - max_rtp_ms;
}
//不明原因的时间戳大幅跳跃,直接返回上次值
WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp;
return _last_ntp_stamp_ms;
}
auto diff = _last_rtp_stamp_ms - rtp_stamp_ms;
//rtp时间戳负增长
auto diff = (_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f);
if (diff < MAX_DELTA_STAMP) {
//正常范围的时间戳回退说明收到rtp乱序了
return _last_ntp_stamp_ms - diff;
}
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) {
//时间戳大幅度回退
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) {
//确定是时间戳溢出
update(rtp_stamp_ms, _last_ntp_stamp_ms + (max_rtp_ms - diff));
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;
}
//不明原因的时间戳回退,直接返回上次值

View File

@ -119,16 +119,15 @@ public:
NtpStamp() = default;
~NtpStamp() = default;
void setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms);
void setNtpStamp(uint32_t rtp_stamp, 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);
void update(uint32_t rtp_stamp, 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 _last_rtp_stamp_ms = 0;
uint64_t _last_ntp_stamp_ms = 0;
};

View File

@ -33,7 +33,7 @@ public:
setOnSorted(std::move(cb));
setBeforeSorted(std::move(cb_before));
//GB28181推流不支持ntp时间戳
setNtpStamp(0, 0, 0);
setNtpStamp(0, 0);
}
~RtpReceiverImp() override = default;

View File

@ -101,10 +101,10 @@ RtpPacket::Ptr RtpTrack::inputRtp(TrackType type, int sample_rate, uint8_t *ptr,
return rtp;
}
void RtpTrack::setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms) {
_disable_ntp = rtp_stamp == 0 && sample_rate == 0 && ntp_stamp_ms == 0;
if (sample_rate) {
_ntp_stamp.setNtpStamp(rtp_stamp, sample_rate, ntp_stamp_ms);
void RtpTrack::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
_disable_ntp = rtp_stamp == 0 && ntp_stamp_ms == 0;
if (!_disable_ntp) {
_ntp_stamp.setNtpStamp(rtp_stamp, ntp_stamp_ms);
}
}

View File

@ -176,7 +176,7 @@ public:
void clear();
uint32_t getSSRC() const;
RtpPacket::Ptr inputRtp(TrackType type, int sample_rate, uint8_t *ptr, size_t len);
void setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms);
void setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms);
protected:
virtual void onRtpSorted(RtpPacket::Ptr rtp) {}
@ -245,11 +245,10 @@ public:
* rtp_stamp/sample_rate/ntp_stamp_ms都为0rtp时间戳为ntp时间戳
* @param index track下标索引
* @param rtp_stamp rtp时间戳
* @param sample_rate
* @param ntp_stamp_ms ntp时间戳
*/
void setNtpStamp(int index, uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms){
_track[index].setNtpStamp(rtp_stamp, sample_rate, ntp_stamp_ms);
void setNtpStamp(int index, uint32_t rtp_stamp, uint64_t ntp_stamp_ms){
_track[index].setNtpStamp(rtp_stamp, ntp_stamp_ms);
}
void clear() {

View File

@ -496,7 +496,7 @@ void RtspPlayer::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, uint8_t *data
if ((RtcpType) rtcp->pt == RtcpType::RTCP_SR) {
auto sr = (RtcpSR *) (rtcp);
//设置rtp时间戳与ntp时间戳的对应关系
setNtpStamp(track_idx, sr->rtpts, track->_samplerate, sr->getNtpUnixStampMS());
setNtpStamp(track_idx, sr->rtpts, sr->getNtpUnixStampMS());
}
}
}

View File

@ -189,7 +189,7 @@ void RtspSession::onRtcpPacket(int track_idx, SdpTrack::Ptr &track, const char *
if ((RtcpType) rtcp->pt == RtcpType::RTCP_SR) {
auto sr = (RtcpSR *) (rtcp);
//设置rtp时间戳与ntp时间戳的对应关系
setNtpStamp(track_idx, sr->rtpts, track->_samplerate, sr->getNtpUnixStampMS());
setNtpStamp(track_idx, sr->rtpts, sr->getNtpUnixStampMS());
}
}
}

View File

@ -674,7 +674,7 @@ void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
} else {
//InfoL << "接收丢包率,ssrc:" << sr->ssrc << ",loss rate(%):" << rtp_chn->getLossRate();
//设置rtp时间戳与ntp时间戳的对应关系
rtp_chn->setNtpStamp(sr->rtpts, track->plan_rtp->sample_rate, sr->getNtpUnixStampMS());
rtp_chn->setNtpStamp(sr->rtpts, sr->getNtpUnixStampMS());
auto rr = rtp_chn->createRtcpRR(sr, track->answer_ssrc_rtp);
sendRtcpPacket(rr->data(), rr->size(), true);
}