From 3840ff2a3f7102d4a62414525a0bd3ff9abd2e40 Mon Sep 17 00:00:00 2001
From: ziyue <1213642868@qq.com>
Date: Tue, 27 Jul 2021 10:50:34 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Drtt=E8=AE=A1=E7=AE=97?=
=?UTF-8?q?=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/Rtcp/RtcpContext.cpp | 39 ++++++++++++++++++++-------------------
src/Rtcp/RtcpContext.h | 8 ++------
2 files changed, 22 insertions(+), 25 deletions(-)
diff --git a/src/Rtcp/RtcpContext.cpp b/src/Rtcp/RtcpContext.cpp
index bf8e03df..7ced4f4c 100644
--- a/src/Rtcp/RtcpContext.cpp
+++ b/src/Rtcp/RtcpContext.cpp
@@ -14,10 +14,6 @@ using namespace toolkit;
namespace mediakit {
-void RtcpContext::clear() {
- memset(this, 0, sizeof(RtcpContext));
-}
-
RtcpContext::RtcpContext(bool is_receiver) {
_is_receiver = is_receiver;
}
@@ -89,24 +85,20 @@ void RtcpContext::onRtcp(RtcpHeader *rtcp) {
if (!item->last_sr_stamp) {
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包的延时,已转换为毫秒
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);
- _rtt[item->ssrc] = rtt;
- //InfoL << "ssrc:" << item->ssrc << ",rtt:" << rtt;
+ if (rtt >= 0) {
+ //rtt不可能小于0
+ _rtt[item->ssrc] = rtt;
+ //InfoL << "ssrc:" << item->ssrc << ",rtt:" << rtt;
+ }
}
break;
}
@@ -160,6 +152,15 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
rtcp->ssrc = htonl(rtcp_ssrc);
rtcp->packet_count = htonl((uint32_t) _packets);
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));
}
diff --git a/src/Rtcp/RtcpContext.h b/src/Rtcp/RtcpContext.h
index 21ad9c76..48453a09 100644
--- a/src/Rtcp/RtcpContext.h
+++ b/src/Rtcp/RtcpContext.h
@@ -67,11 +67,6 @@ public:
*/
Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
- /**
- * 清空状态
- */
- void clear();
-
/**
* 获取rtt
* @param ssrc rtp ssrc
@@ -122,7 +117,8 @@ private:
uint32_t _last_sr_lsr = 0;
//上次收到sr时的系统时间戳,单位毫秒
uint64_t _last_sr_ntp_sys = 0;
- unordered_map _rtt;
+ map _rtt;
+ map _sender_report_ntp;
};
}//namespace mediakit