添加部分rtcp代码

This commit is contained in:
xiongziliang 2021-04-03 00:04:52 +08:00
parent a176cb83a8
commit e9c963dc82
2 changed files with 55 additions and 2 deletions

View File

@ -237,9 +237,12 @@ void WebRtcTransportImp::onStartWebRTC() {
continue; continue;
} }
auto &ref = _rtp_receiver[plan.pt]; auto &ref = _rtp_receiver[plan.pt];
_ssrc_info[m.rtp_ssrc.ssrc] = &ref;
ref.plan = &plan; ref.plan = &plan;
ref.media = &m; ref.media = &m;
ref.is_common_rtp = getCodecId(plan.codec) != CodecInvalid; ref.is_common_rtp = getCodecId(plan.codec) != CodecInvalid;
ref.rtcp_context_recv = std::make_shared<RtcpContext>(ref.plan->sample_rate, true);
ref.rtcp_context_send = std::make_shared<RtcpContext>(ref.plan->sample_rate, false);
ref.receiver = std::make_shared<RtpReceiverImp>([&ref, this](RtpPacket::Ptr rtp) { ref.receiver = std::make_shared<RtpReceiverImp>([&ref, this](RtpPacket::Ptr rtp) {
onSortedRtp(ref, std::move(rtp)); onSortedRtp(ref, std::move(rtp));
}, [ref, this](const RtpPacket::Ptr &rtp) { }, [ref, this](const RtpPacket::Ptr &rtp) {
@ -274,6 +277,7 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush){
//设置pt //设置pt
rtp->getHeader()->pt = _send_rtp_pt[rtp->type]; rtp->getHeader()->pt = _send_rtp_pt[rtp->type];
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush); sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush);
_rtp_receiver[_send_rtp_pt[rtp->type]].rtcp_context_send->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
//还原pt //还原pt
rtp->getHeader()->pt = tmp; rtp->getHeader()->pt = tmp;
} }
@ -399,23 +403,68 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
} }
void WebRtcTransportImp::onRtcp(const char *buf, size_t len) { void WebRtcTransportImp::onRtcp(const char *buf, size_t len) {
RtcpHeader *rtcp = (RtcpHeader *) buf; auto rtcps = RtcpHeader::loadFromBytes((char *) buf, len);
//todo rtcp相关 for (auto rtcp : rtcps) {
switch ((RtcpType) rtcp->pt) {
case RtcpType::RTCP_SR : {
//对方汇报rtp发送情况
RtcpSR *sr = (RtcpSR *) rtcp;
auto it = _ssrc_info.find(sr->items.ssrc);
if (it != _ssrc_info.end()) {
it->second->rtcp_context_recv->onRtcp(sr);
auto rr = it->second->rtcp_context_recv->createRtcpRR(sr->ssrc, sr->items.ssrc);
sendRtcpPacket(rr->data(), rr->size(), true);
InfoL << "send rtcp rr";
}
break;
}
case RtcpType::RTCP_RR : {
//对方汇报rtp接收情况
RtcpRR *rr = (RtcpRR *) rtcp;
auto it = _ssrc_info.find(rr->items.ssrc);
if (it != _ssrc_info.end()) {
auto sr = it->second->rtcp_context_send->createRtcpSR(rr->ssrc);
sendRtcpPacket(sr->data(), sr->size(), true);
InfoL << "send rtcp sr";
}
break;
}
default: break;
}
}
}
int makeRtcpPli(char *packet, int len) {
if (packet == NULL || len != 12)
return -1;
memset(packet, 0, len);
RtcpHeader *rtcp = (RtcpHeader *) packet;
rtcp->version = 2;
rtcp->pt = (uint8_t) RtcpType::RTCP_PSFB;
rtcp->report_count = 1;
rtcp->length = htons((len / 4) - 1);
return 12;
} }
void WebRtcTransportImp::onSortedRtp(const RtpPayloadInfo &info, RtpPacket::Ptr rtp) { void WebRtcTransportImp::onSortedRtp(const RtpPayloadInfo &info, RtpPacket::Ptr rtp) {
if(!info.is_common_rtp){ if(!info.is_common_rtp){
WarnL; WarnL;
return;
} }
if (_pli_ticker.elapsedTime() > 2000) { if (_pli_ticker.elapsedTime() > 2000) {
//todo 发送pli //todo 发送pli
_pli_ticker.resetTime(); _pli_ticker.resetTime();
char rtcpbuf[12];
makeRtcpPli(rtcpbuf, 12);
sendRtcpPacket(rtcpbuf, 12, true);
InfoL << "send pli";
} }
_push_src->onWrite(std::move(rtp), false); _push_src->onWrite(std::move(rtp), false);
} }
void WebRtcTransportImp::onBeforeSortedRtp(const RtpPayloadInfo &info, const RtpPacket::Ptr &rtp) { void WebRtcTransportImp::onBeforeSortedRtp(const RtpPayloadInfo &info, const RtpPacket::Ptr &rtp) {
//todo rtcp相关 //todo rtcp相关
info.rtcp_context_recv->onRtp(rtp->getSeq(), rtp->getStampMS(), rtp->size() - RtpPacket::kRtpTcpHeaderSize);
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////

View File

@ -10,6 +10,7 @@
#include "Poller/EventPoller.h" #include "Poller/EventPoller.h"
#include "Network/Socket.h" #include "Network/Socket.h"
#include "Rtsp/RtspMediaSourceImp.h" #include "Rtsp/RtspMediaSourceImp.h"
#include "Rtcp/RtcpContext.h"
using namespace toolkit; using namespace toolkit;
using namespace mediakit; using namespace mediakit;
@ -139,6 +140,8 @@ private:
const RtcCodecPlan *plan; const RtcCodecPlan *plan;
const RtcMedia *media; const RtcMedia *media;
std::shared_ptr<RtpReceiverImp> receiver; std::shared_ptr<RtpReceiverImp> receiver;
RtcpContext::Ptr rtcp_context_recv;
RtcpContext::Ptr rtcp_context_send;
}; };
void onSortedRtp(const RtpPayloadInfo &info,RtpPacket::Ptr rtp); void onSortedRtp(const RtpPayloadInfo &info,RtpPacket::Ptr rtp);
@ -153,6 +156,7 @@ private:
mutable uint8_t _send_rtp_pt[2] = {0, 0}; mutable uint8_t _send_rtp_pt[2] = {0, 0};
RtspMediaSourceImp::Ptr _push_src; RtspMediaSourceImp::Ptr _push_src;
unordered_map<uint8_t, RtpPayloadInfo> _rtp_receiver; unordered_map<uint8_t, RtpPayloadInfo> _rtp_receiver;
unordered_map<uint32_t, RtpPayloadInfo*> _ssrc_info;
Ticker _pli_ticker; Ticker _pli_ticker;
}; };