Commit c98e85ca by Johnny

增加 RtcpContextForSend/RtcpContextForRecv作为RtcpContext子类

parent 290b3f37
...@@ -14,12 +14,16 @@ using namespace toolkit; ...@@ -14,12 +14,16 @@ using namespace toolkit;
namespace mediakit { 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) { 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(); auto sys_stamp = getCurrentMillisecond();
if (_last_rtp_sys_stamp) { if (_last_rtp_sys_stamp) {
...@@ -57,11 +61,7 @@ void RtcpContext::onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uin ...@@ -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_seq = seq;
_last_rtp_sys_stamp = sys_stamp; _last_rtp_sys_stamp = sys_stamp;
} }
RtcpContext::onRtp(seq, stamp, ntp_stamp_ms, sample_rate, bytes);
++_packets;
_bytes += bytes;
_last_rtp_stamp = stamp;
_last_ntp_stamp_ms = ntp_stamp_ms;
} }
void RtcpContext::onRtcp(RtcpHeader *rtcp) { void RtcpContext::onRtcp(RtcpHeader *rtcp) {
...@@ -115,9 +115,9 @@ uint32_t RtcpContext::getRtt(uint32_t ssrc) const { ...@@ -115,9 +115,9 @@ uint32_t RtcpContext::getRtt(uint32_t ssrc) const {
} }
size_t RtcpContext::getExpectedPackets() 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; return (_seq_cycles << 16) + _seq_max - _seq_base + 1;
} }
...@@ -129,9 +129,10 @@ size_t RtcpContext::getExpectedPacketsInterval() { ...@@ -129,9 +129,10 @@ size_t RtcpContext::getExpectedPacketsInterval() {
} }
size_t RtcpContext::getLost() { size_t RtcpContext::getLost() {
if (!_is_receiver) { throw std::runtime_error("没有实现, rtp发送者无法统计丢包率");
throw std::runtime_error("rtp发送者无法统计丢包率"); }
}
size_t RtcpContextForRecv::getLost() {
return getExpectedPackets() - _packets; return getExpectedPackets() - _packets;
} }
...@@ -143,9 +144,10 @@ size_t RtcpContext::geLostInterval() { ...@@ -143,9 +144,10 @@ size_t RtcpContext::geLostInterval() {
} }
Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) { 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); auto rtcp = RtcpSR::create(0);
rtcp->setNtpStamp(_last_ntp_stamp_ms); rtcp->setNtpStamp(_last_ntp_stamp_ms);
rtcp->rtpts = htonl(_last_rtp_stamp); rtcp->rtpts = htonl(_last_rtp_stamp);
...@@ -165,9 +167,11 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) { ...@@ -165,9 +167,11 @@ Buffer::Ptr RtcpContext::createRtcpSR(uint32_t rtcp_ssrc) {
} }
Buffer::Ptr RtcpContext::createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_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); auto rtcp = RtcpRR::create(1);
rtcp->ssrc = htonl(rtcp_ssrc); rtcp->ssrc = htonl(rtcp_ssrc);
......
...@@ -20,11 +20,7 @@ namespace mediakit { ...@@ -20,11 +20,7 @@ namespace mediakit {
class RtcpContext { class RtcpContext {
public: public:
using Ptr = std::shared_ptr<RtcpContext>; using Ptr = std::shared_ptr<RtcpContext>;
/** virtual ~RtcpContext() = default;
* 创建rtcp上下文
* @param is_receiver 是否为rtp接收者,接收者更消耗性能
*/
RtcpContext(bool is_receiver);
/** /**
* 输出或输入rtp时调用 * 输出或输入rtp时调用
...@@ -34,7 +30,7 @@ public: ...@@ -34,7 +30,7 @@ public:
* @param rtp rtp时间戳采样率,视频一般为90000,音频一般为采样率 * @param rtp rtp时间戳采样率,视频一般为90000,音频一般为采样率
* @param bytes rtp数据长度 * @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包 * 输入sr rtcp包
...@@ -45,19 +41,19 @@ public: ...@@ -45,19 +41,19 @@ public:
/** /**
* 计算总丢包数 * 计算总丢包数
*/ */
size_t getLost(); virtual size_t getLost();
/** /**
* 返回理应收到的rtp数 * 返回理应收到的rtp数
*/ */
size_t getExpectedPackets() const; virtual size_t getExpectedPackets() const;
/** /**
* 创建SR rtcp包 * 创建SR rtcp包
* @param rtcp_ssrc rtcp的ssrc * @param rtcp_ssrc rtcp的ssrc
* @return rtcp包 * @return rtcp包
*/ */
Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc); virtual Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
/** /**
* 创建RR rtcp包 * 创建RR rtcp包
...@@ -65,7 +61,7 @@ public: ...@@ -65,7 +61,7 @@ public:
* @param rtp_ssrc rtp的ssrc * @param rtp_ssrc rtp的ssrc
* @return rtcp包 * @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 * 获取rtt
...@@ -84,9 +80,7 @@ public: ...@@ -84,9 +80,7 @@ public:
*/ */
size_t geLostInterval(); size_t geLostInterval();
private: protected:
//是否为接收者
bool _is_receiver;
//时间戳抖动值 //时间戳抖动值
double _jitter = 0; double _jitter = 0;
//收到或发送的rtp的字节数 //收到或发送的rtp的字节数
...@@ -120,5 +114,17 @@ private: ...@@ -120,5 +114,17 @@ private:
map<uint32_t/*last_sr_lsr*/, uint64_t/*ntp stamp*/> _sender_report_ntp; 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 }//namespace mediakit
#endif //ZLMEDIAKIT_RTCPCONTEXT_H #endif //ZLMEDIAKIT_RTCPCONTEXT_H
...@@ -23,11 +23,11 @@ RtpServer::~RtpServer() { ...@@ -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: public:
using Ptr = std::shared_ptr<RtcpHelper>; 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); _rtcp_sock = std::move(rtcp_sock);
_sample_rate = sample_rate; _sample_rate = sample_rate;
} }
......
...@@ -206,7 +206,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) { ...@@ -206,7 +206,7 @@ void RtspPlayer::handleResDESCRIBE(const Parser& parser) {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _sdp_track) { for (auto &track : _sdp_track) {
_rtcp_context.emplace_back(std::make_shared<RtcpContext>(true)); _rtcp_context.emplace_back(std::make_shared<RtcpContextForRecv>());
} }
sendSetup(0); sendSetup(0);
} }
......
...@@ -179,7 +179,7 @@ void RtspPusher::sendAnnounce() { ...@@ -179,7 +179,7 @@ void RtspPusher::sendAnnounce() {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _track_vec) { 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); _on_res_func = std::bind(&RtspPusher::handleResAnnounce, this, placeholders::_1);
sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp()); sendRtspRequest("ANNOUNCE", _url, {}, src->getSdp());
......
...@@ -257,7 +257,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) { ...@@ -257,7 +257,7 @@ void RtspSession::handleReq_ANNOUNCE(const Parser &parser) {
} }
_rtcp_context.clear(); _rtcp_context.clear();
for (auto &track : _sdp_track) { 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 = std::make_shared<RtspMediaSourceImp>(_media_info._vhost, _media_info._app, _media_info._streamid);
_push_src->setListener(dynamic_pointer_cast<MediaSourceEvent>(shared_from_this())); _push_src->setListener(dynamic_pointer_cast<MediaSourceEvent>(shared_from_this()));
...@@ -418,7 +418,7 @@ void RtspSession::onAuthSuccess() { ...@@ -418,7 +418,7 @@ void RtspSession::onAuthSuccess() {
} }
strongSelf->_rtcp_context.clear(); strongSelf->_rtcp_context.clear();
for (auto &track : strongSelf->_sdp_track) { 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->_sessionid = makeRandStr(12);
strongSelf->_play_src = rtsp_src; strongSelf->_play_src = rtsp_src;
......
...@@ -420,7 +420,7 @@ void WebRtcTransportImp::onStartWebRTC() { ...@@ -420,7 +420,7 @@ void WebRtcTransportImp::onStartWebRTC() {
track->offer_ssrc_rtx = m_offer->getRtxSSRC(); track->offer_ssrc_rtx = m_offer->getRtxSSRC();
track->plan_rtp = &m_answer.plan[0];; track->plan_rtp = &m_answer.plan[0];;
track->plan_rtx = m_answer.getRelatedRtxPlan(track->plan_rtp->pt); 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 //send ssrc --> MediaTrack
_ssrc_to_track[track->answer_ssrc_rtp] = track; _ssrc_to_track[track->answer_ssrc_rtp] = track;
...@@ -656,7 +656,7 @@ private: ...@@ -656,7 +656,7 @@ private:
private: private:
NackContext _nack_ctx; NackContext _nack_ctx;
RtcpContext _rtcp_context{true}; RtcpContextForRecv _rtcp_context;
EventPoller::Ptr _poller; EventPoller::Ptr _poller;
DelayTask::Ptr _delay_task; DelayTask::Ptr _delay_task;
function<void(const FCI_NACK &nack)> _on_nack; function<void(const FCI_NACK &nack)> _on_nack;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论