修复rtt计算逻辑

This commit is contained in:
ziyue 2021-07-27 10:50:34 +08:00
parent 10eb063f63
commit 3840ff2a3f
2 changed files with 22 additions and 25 deletions

View File

@ -14,10 +14,6 @@ using namespace toolkit;
namespace mediakit { namespace mediakit {
void RtcpContext::clear() {
memset(this, 0, sizeof(RtcpContext));
}
RtcpContext::RtcpContext(bool is_receiver) { RtcpContext::RtcpContext(bool is_receiver) {
_is_receiver = is_receiver; _is_receiver = is_receiver;
} }
@ -89,25 +85,21 @@ void RtcpContext::onRtcp(RtcpHeader *rtcp) {
if (!item->last_sr_stamp) { if (!item->last_sr_stamp) {
continue; continue;
} }
auto it = _sender_report_ntp.find(item->last_sr_stamp);
if (it == _sender_report_ntp.end()) {
continue;
}
//发送sr到收到rr之间的时间戳增量
auto ms_inc = getCurrentMillisecond() - it->second;
//rtp接收端收到sr包后回复rr包的延时已转换为毫秒 //rtp接收端收到sr包后回复rr包的延时已转换为毫秒
auto delay_ms = (uint64_t) item->delay_since_last_sr * 1000 / 65536; auto delay_ms = (uint64_t) item->delay_since_last_sr * 1000 / 65536;
//这个rr包对应sr包的ntpmsw和ntplsw
auto ntpmsw = item->last_sr_stamp >> 16;
auto ntplsw = (item->last_sr_stamp & 0xFFFF) << 16;
RtcpSR sr;
//获取当前时间戳
sr.setNtpStamp(getCurrentMillisecond(true));
//当前时间戳与上次发送的sr包直接的ntp时间差
int64_t ntpmsw_inc = (int64_t)(ntohl(sr.ntpmsw) & 0xFFFF) - (int64_t)ntpmsw;
int64_t ntplsw_inc = (int64_t)(ntohl(sr.ntplsw)) - (int64_t)ntplsw;
//转换为毫秒
auto ms_inc = ntpmsw_inc * 1000 + (ntplsw_inc / ((double) (((uint64_t) 1) << 32) * 1.0e-3));
auto rtt = (int) (ms_inc - delay_ms); auto rtt = (int) (ms_inc - delay_ms);
if (rtt >= 0) {
//rtt不可能小于0
_rtt[item->ssrc] = rtt; _rtt[item->ssrc] = rtt;
//InfoL << "ssrc:" << item->ssrc << ",rtt:" << rtt; //InfoL << "ssrc:" << item->ssrc << ",rtt:" << rtt;
} }
}
break; break;
} }
default: break; default: break;
@ -160,6 +152,15 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
rtcp->ssrc = htonl(rtcp_ssrc); rtcp->ssrc = htonl(rtcp_ssrc);
rtcp->packet_count = htonl((uint32_t) _packets); rtcp->packet_count = htonl((uint32_t) _packets);
rtcp->octet_count = htonl((uint32_t) _bytes); rtcp->octet_count = htonl((uint32_t) _bytes);
//记录上次发送的sender report信息用于后续统计rtt
auto last_sr_lsr = ((ntohl(rtcp->ntpmsw) & 0xFFFF) << 16) | ((ntohl(rtcp->ntplsw) >> 16) & 0xFFFF);
_sender_report_ntp[last_sr_lsr] = getCurrentMillisecond();
if (_sender_report_ntp.size() >= 5) {
//删除最早的sr rtcp
_sender_report_ntp.erase(_sender_report_ntp.begin());
}
return RtcpHeader::toBuffer(std::move(rtcp)); return RtcpHeader::toBuffer(std::move(rtcp));
} }

View File

@ -67,11 +67,6 @@ public:
*/ */
Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc); Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
/**
*
*/
void clear();
/** /**
* rtt * rtt
* @param ssrc rtp ssrc * @param ssrc rtp ssrc
@ -122,7 +117,8 @@ private:
uint32_t _last_sr_lsr = 0; uint32_t _last_sr_lsr = 0;
//上次收到sr时的系统时间戳,单位毫秒 //上次收到sr时的系统时间戳,单位毫秒
uint64_t _last_sr_ntp_sys = 0; uint64_t _last_sr_ntp_sys = 0;
unordered_map<uint32_t/*ssrc*/, uint32_t/*rtt*/> _rtt; map<uint32_t/*ssrc*/, uint32_t/*rtt*/> _rtt;
map<uint32_t/*last_sr_lsr*/, uint64_t/*ntp stamp*/> _sender_report_ntp;
}; };
}//namespace mediakit }//namespace mediakit