增加 RtcpContextForSend/RtcpContextForRecv作为RtcpContext子类

This commit is contained in:
Johnny
2021-10-13 15:06:13 +08:00
parent 290b3f37a5
commit c98e85caec
7 changed files with 52 additions and 42 deletions

View File

@@ -14,12 +14,16 @@ using namespace toolkit;
namespace mediakit {
RtcpContext::RtcpContext(bool is_receiver) {
_is_receiver = is_receiver;
}
void RtcpContext::onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) {
if (_is_receiver) {
++_packets;
_bytes += bytes;
_last_rtp_stamp = stamp;
_last_ntp_stamp_ms = ntp_stamp_ms;
}
void RtcpContextForRecv::onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) {
{
//接收者才做复杂的统计运算
auto sys_stamp = getCurrentMillisecond();
if (_last_rtp_sys_stamp) {
@@ -57,11 +61,7 @@ void RtcpContext::onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uin
_last_rtp_seq = seq;
_last_rtp_sys_stamp = sys_stamp;
}
++_packets;
_bytes += bytes;
_last_rtp_stamp = stamp;
_last_ntp_stamp_ms = ntp_stamp_ms;
RtcpContext::onRtp(seq, stamp, ntp_stamp_ms, sample_rate, bytes);
}
void RtcpContext::onRtcp(RtcpHeader *rtcp) {
@@ -115,9 +115,9 @@ uint32_t RtcpContext::getRtt(uint32_t ssrc) const {
}
size_t RtcpContext::getExpectedPackets() const {
if (!_is_receiver) {
throw std::runtime_error("rtp发送者无法统计应收包数");
}
throw std::runtime_error("没有实现, rtp发送者无法统计应收包数");
}
size_t RtcpContextForRecv::getExpectedPackets() const {
return (_seq_cycles << 16) + _seq_max - _seq_base + 1;
}
@@ -129,9 +129,10 @@ size_t RtcpContext::getExpectedPacketsInterval() {
}
size_t RtcpContext::getLost() {
if (!_is_receiver) {
throw std::runtime_error("rtp发送者无法统计丢包率");
}
throw std::runtime_error("没有实现, rtp发送者无法统计丢包率");
}
size_t RtcpContextForRecv::getLost() {
return getExpectedPackets() - _packets;
}
@@ -143,9 +144,10 @@ size_t RtcpContext::geLostInterval() {
}
Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
if (_is_receiver) {
throw std::runtime_error("rtp接收者尝试发送sr包");
}
throw std::runtime_error("没有实现, rtp接收者尝试发送sr包");
}
Buffer::Ptr RtcpContextForSend::createRtcpSR(uint32_t rtcp_ssrc) {
auto rtcp = RtcpSR::create(0);
rtcp->setNtpStamp(_last_ntp_stamp_ms);
rtcp->rtpts = htonl(_last_rtp_stamp);
@@ -165,9 +167,11 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
}
Buffer::Ptr RtcpContext::createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) {
if (!_is_receiver) {
throw std::runtime_error("rtp发送者尝试发送rr包");
}
throw std::runtime_error("没有实现, rtp发送者尝试发送rr包");
}
Buffer::Ptr RtcpContextForRecv::createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) {
auto rtcp = RtcpRR::create(1);
rtcp->ssrc = htonl(rtcp_ssrc);