增加 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);

View File

@ -20,11 +20,7 @@ namespace mediakit {
class RtcpContext {
public:
using Ptr = std::shared_ptr<RtcpContext>;
/**
* rtcp上下文
* @param is_receiver rtp接收者
*/
RtcpContext(bool is_receiver);
virtual ~RtcpContext() = default;
/**
* rtp时调用
@ -34,7 +30,7 @@ public:
* @param rtp rtp时间戳采样率90000
* @param bytes rtp数据长度
*/
void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes);
virtual void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes);
/**
* sr rtcp包
@ -45,19 +41,19 @@ public:
/**
*
*/
size_t getLost();
virtual size_t getLost();
/**
* rtp数
*/
size_t getExpectedPackets() const;
virtual size_t getExpectedPackets() const;
/**
* SR rtcp包
* @param rtcp_ssrc rtcp的ssrc
* @return rtcp包
*/
Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
virtual Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
/**
* RR rtcp包
@ -65,7 +61,7 @@ public:
* @param rtp_ssrc rtp的ssrc
* @return rtcp包
*/
Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
virtual Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
/**
* rtt
@ -84,9 +80,7 @@ public:
*/
size_t geLostInterval();
private:
//是否为接收者
bool _is_receiver;
protected:
//时间戳抖动值
double _jitter = 0;
//收到或发送的rtp的字节数
@ -120,5 +114,17 @@ private:
map<uint32_t/*last_sr_lsr*/, uint64_t/*ntp stamp*/> _sender_report_ntp;
};
class RtcpContextForSend : public RtcpContext {
public:
Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc) override;
};
class RtcpContextForRecv : public RtcpContext {
public:
void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) override;
Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
size_t getExpectedPackets() const override;
size_t getLost() override;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_RTCPCONTEXT_H

View File

@ -23,11 +23,11 @@ RtpServer::~RtpServer() {
}
}
class RtcpHelper : public RtcpContext, public std::enable_shared_from_this<RtcpHelper> {
class RtcpHelper : public RtcpContextForRecv, public std::enable_shared_from_this<RtcpHelper> {
public:
using Ptr = std::shared_ptr<RtcpHelper>;
RtcpHelper(Socket::Ptr rtcp_sock, uint32_t sample_rate) : RtcpContext(true){
RtcpHelper(Socket::Ptr rtcp_sock, uint32_t sample_rate) {
_rtcp_sock = std::move(rtcp_sock);
_sample_rate = sample_rate;
}

View File

@ -206,7 +206,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
}
_rtcp_context.clear();
for (auto &track : _sdp_track) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(true));
_rtcp_context.emplace_back(std::make_shared<RtcpContextForRecv>());
}
sendSetup(0);
}

View File

@ -179,7 +179,7 @@ void RtspPusher::sendAnnounce() {
}
_rtcp_context.clear();
for (auto &track : _track_vec) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(false));
_rtcp_context.emplace_back(std::make_shared<RtcpContextForSend>());
}
_on_res_func = std::bind(&RtspPusher::handleResAnnounce, this, placeholders::_1);
sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp());

View File

@ -257,7 +257,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
}
_rtcp_context.clear();
for (auto &track : _sdp_track) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(true));
_rtcp_context.emplace_back(std::make_shared<RtcpContextForRecv>());
}
_push_src = std::make_shared<RtspMediaSourceImp>(_media_info._vhost, _media_info._app, _media_info._streamid);
_push_src->setListener(dynamic_pointer_cast<MediaSourceEvent>(shared_from_this()));
@ -418,7 +418,7 @@ void RtspSession::onAuthSuccess() {
}
strongSelf->_rtcp_context.clear();
for (auto &track : strongSelf->_sdp_track) {
strongSelf->_rtcp_context.emplace_back(std::make_shared<RtcpContext>(false));
strongSelf->_rtcp_context.emplace_back(std::make_shared<RtcpContextForSend>());
}
strongSelf->_sessionid = makeRandStr(12);
strongSelf->_play_src = rtsp_src;

View File

@ -420,7 +420,7 @@ void WebRtcTransportImp::onStartWebRTC() {
track->offer_ssrc_rtx = m_offer->getRtxSSRC();
track->plan_rtp = &m_answer.plan[0];;
track->plan_rtx = m_answer.getRelatedRtxPlan(track->plan_rtp->pt);
track->rtcp_context_send = std::make_shared<RtcpContext>(false);
track->rtcp_context_send = std::make_shared<RtcpContextForSend>();
//send ssrc --> MediaTrack
_ssrc_to_track[track->answer_ssrc_rtp] = track;
@ -656,7 +656,7 @@ private:
private:
NackContext _nack_ctx;
RtcpContext _rtcp_context{true};
RtcpContextForRecv _rtcp_context;
EventPoller::Ptr _poller;
DelayTask::Ptr _delay_task;
function<void(const FCI_NACK &nack)> _on_nack;