完善nack与丢包重传2

This commit is contained in:
xia-chu 2021-05-11 12:12:28 +08:00
parent 5c90a1e137
commit 0d61de758e
2 changed files with 38 additions and 9 deletions

View File

@ -429,6 +429,9 @@ void WebRtcTransportImp::onStartWebRTC() {
}, [&ref, this](const RtpPacket::Ptr &rtp) mutable { }, [&ref, this](const RtpPacket::Ptr &rtp) mutable {
onBeforeSortedRtp(ref, rtp); onBeforeSortedRtp(ref, rtp);
}); });
ref.nack_ctx.setOnNack([&ref, this](const FCI_NACK &nack) mutable{
onNack(ref, nack);
});
} }
if (m.type != TrackApplication) { if (m.type != TrackApplication) {
//记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id //记录rtp ext类型与id的关系方便接收或发送rtp时修改rtp ext id
@ -676,14 +679,21 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
auto &info = it->second; auto &info = it->second;
#if 1 #if 1
//此处模拟接受丢包
auto header = (RtpHeader *) buf; auto header = (RtpHeader *) buf;
auto seq = ntohs(header->seq); auto seq = ntohs(header->seq);
if (seq % 10 == 0) { if (info.is_common_rtp) {
//丢包 //此处模拟接受丢包
return; if (info.media->type == TrackVideo && seq % 10 == 0) {
//丢包
DebugL << "模拟接受丢包:" << seq;
return;
} else {
info.nack_ctx.received(seq);
}
} else { } else {
info.nack_ctx.received(seq); //收到重传包
header->ssrc = info.media->rtp_rtx_ssrc[0].ssrc;
InfoL << "收到重传包:" << seq;
} }
#endif #endif
@ -691,6 +701,14 @@ void WebRtcTransportImp::onRtp(const char *buf, size_t len) {
info.receiver->inputRtp(info.media->type, info.plan->sample_rate, (uint8_t *) buf, len); info.receiver->inputRtp(info.media->type, info.plan->sample_rate, (uint8_t *) buf, len);
} }
void WebRtcTransportImp::onNack(RtpPayloadInfo &info, const FCI_NACK &nack) {
auto rtcp = RtcpFB::create(RTPFBType::RTCP_RTPFB_NACK, &nack, FCI_NACK::kSize);
rtcp->ssrc = htons(0);
rtcp->ssrc_media = htonl(info.media->rtp_rtx_ssrc[0].ssrc);
InfoL << rtcp->RtcpHeader::dumpString();
sendRtcpPacket((char *) rtcp.get(), rtcp->getSize(), true);
}
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
void WebRtcTransportImp::onSortedRtp(RtpPayloadInfo &info, RtpPacket::Ptr rtp) { void WebRtcTransportImp::onSortedRtp(RtpPayloadInfo &info, RtpPacket::Ptr rtp) {
@ -756,11 +774,12 @@ void WebRtcTransportImp::onSendRtp(const RtpPacket::Ptr &rtp, bool flush, bool r
#if 0 #if 0
//此处模拟发送丢包 //此处模拟发送丢包
if(rtp->getSeq() % 10 == 0){ if(rtp->getSeq() % 10 == 0){
DebugL << "模拟发送丢包:" << rtp->getSeq();
return; return;
} }
#endif #endif
} else { } else {
WarnL << "重传rtp:" << rtp->getSeq(); WarnL << "rtp发送重传:" << rtp->getSeq();
} }
sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush, info); sendRtpPacket(rtp->data() + RtpPacket::kRtpTcpHeaderSize, rtp->size() - RtpPacket::kRtpTcpHeaderSize, flush, info);
_bytes_usage += rtp->size() - RtpPacket::kRtpTcpHeaderSize; _bytes_usage += rtp->size() - RtpPacket::kRtpTcpHeaderSize;

View File

@ -192,6 +192,8 @@ private:
class NackContext { class NackContext {
public: public:
using onNack = function<void(const FCI_NACK &nack)>;
void received(uint16_t seq) { void received(uint16_t seq) {
if (!_last_max_seq && _seq.empty()) { if (!_last_max_seq && _seq.empty()) {
_last_max_seq = seq - 1; _last_max_seq = seq - 1;
@ -229,7 +231,7 @@ public:
for (auto i = 0; i < FCI_NACK::kBitSize; ++i) { for (auto i = 0; i < FCI_NACK::kBitSize; ++i) {
vec[i] = _seq.find(_last_max_seq + i + 2) == _seq.end(); vec[i] = _seq.find(_last_max_seq + i + 2) == _seq.end();
} }
onNack(FCI_NACK(_last_max_seq + 1, vec)); doNack(FCI_NACK(_last_max_seq + 1, vec));
_last_max_seq += FCI_NACK::kBitSize + 1; _last_max_seq += FCI_NACK::kBitSize + 1;
if (_last_max_seq >= max_seq) { if (_last_max_seq >= max_seq) {
_seq.clear(); _seq.clear();
@ -241,11 +243,17 @@ public:
} }
} }
void onNack(const FCI_NACK &nack) { void setOnNack(onNack cb) {
InfoL << nack.dumpString() << " " << _seq.size(); _cb = std::move(cb);
} }
private: private:
void doNack(const FCI_NACK &nack) {
if (_cb) {
_cb(nack);
}
}
void eraseFrontSeq(){ void eraseFrontSeq(){
//前面部分seq是连续的未丢包移除之 //前面部分seq是连续的未丢包移除之
for (auto it = _seq.begin(); it != _seq.end();) { for (auto it = _seq.begin(); it != _seq.end();) {
@ -259,6 +267,7 @@ private:
} }
private: private:
onNack _cb;
set<uint16_t> _seq; set<uint16_t> _seq;
uint16_t _last_max_seq = 0; uint16_t _last_max_seq = 0;
}; };
@ -343,6 +352,7 @@ private:
void onSortedRtp(RtpPayloadInfo &info, RtpPacket::Ptr rtp); void onSortedRtp(RtpPayloadInfo &info, RtpPacket::Ptr rtp);
void onBeforeSortedRtp(RtpPayloadInfo &info, const RtpPacket::Ptr &rtp); void onBeforeSortedRtp(RtpPayloadInfo &info, const RtpPacket::Ptr &rtp);
void onNack(RtpPayloadInfo &info, const FCI_NACK &nack);
private: private:
//用掉的总流量 //用掉的总流量