mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-16 13:05:59 +08:00
nack/rtx支持多次重传,提高抗丢包率
This commit is contained in:
122
webrtc/Nack.cpp
122
webrtc/Nack.cpp
@@ -67,10 +67,17 @@ uint32_t NackList::get_cache_ms() {
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void NackContext::received(uint16_t seq) {
|
||||
void NackContext::received(uint16_t seq, bool is_rtx) {
|
||||
if (!_last_max_seq && _seq.empty()) {
|
||||
_last_max_seq = seq - 1;
|
||||
}
|
||||
if (is_rtx || (seq < _last_max_seq && !(seq < 1024 && _last_max_seq > UINT16_MAX - 1024))) {
|
||||
//重传包或
|
||||
//seq回退,且非回环,那么这个应该是重传包
|
||||
onRtx(seq);
|
||||
return;
|
||||
}
|
||||
|
||||
_seq.emplace(seq);
|
||||
auto max_seq = *_seq.rbegin();
|
||||
auto min_seq = *_seq.begin();
|
||||
@@ -83,6 +90,7 @@ void NackContext::received(uint16_t seq) {
|
||||
//回环
|
||||
_seq.clear();
|
||||
_last_max_seq = min_seq;
|
||||
_nack_send_ntp.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -98,18 +106,19 @@ void NackContext::received(uint16_t seq) {
|
||||
}
|
||||
|
||||
//有丢包,丢包从_last_max_seq开始
|
||||
if (max_seq - _last_max_seq > FCI_NACK::kBitSize) {
|
||||
auto nack_rtp_count = FCI_NACK::kBitSize;
|
||||
if (max_seq - _last_max_seq > nack_rtp_count) {
|
||||
vector<bool> vec;
|
||||
vec.resize(FCI_NACK::kBitSize);
|
||||
for (auto i = 0; i < FCI_NACK::kBitSize; ++i) {
|
||||
vec.resize(FCI_NACK::kBitSize, false);
|
||||
for (auto i = 0; i < nack_rtp_count; ++i) {
|
||||
vec[i] = _seq.find(_last_max_seq + i + 2) == _seq.end();
|
||||
}
|
||||
doNack(FCI_NACK(_last_max_seq + 1, vec));
|
||||
_last_max_seq += FCI_NACK::kBitSize + 1;
|
||||
doNack(FCI_NACK(_last_max_seq + 1, vec), true);
|
||||
_last_max_seq += nack_rtp_count + 1;
|
||||
if (_last_max_seq >= max_seq) {
|
||||
_seq.clear();
|
||||
} else {
|
||||
auto it = _seq.emplace_hint(_seq.begin(), _last_max_seq);
|
||||
auto it = _seq.emplace_hint(_seq.begin(), _last_max_seq + 1);
|
||||
_seq.erase(_seq.begin(), it);
|
||||
}
|
||||
}
|
||||
@@ -120,7 +129,10 @@ void NackContext::setOnNack(onNack cb) {
|
||||
_cb = std::move(cb);
|
||||
}
|
||||
|
||||
void NackContext::doNack(const FCI_NACK &nack) {
|
||||
void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
|
||||
if (record_nack) {
|
||||
recordNack(nack);
|
||||
}
|
||||
if (_cb) {
|
||||
_cb(nack);
|
||||
}
|
||||
@@ -136,4 +148,96 @@ void NackContext::eraseFrontSeq() {
|
||||
_last_max_seq = *it;
|
||||
it = _seq.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NackContext::onRtx(uint16_t seq) {
|
||||
auto it = _nack_send_ntp.find(seq);
|
||||
if (it == _nack_send_ntp.end()) {
|
||||
return;
|
||||
}
|
||||
auto rtt = getCurrentMillisecond() - it->second.update_stamp;
|
||||
_nack_send_ntp.erase(it);
|
||||
|
||||
if (rtt >= 0) {
|
||||
//rtt不肯小于0
|
||||
_rtt = rtt;
|
||||
//InfoL << "rtt:" << rtt;
|
||||
}
|
||||
}
|
||||
|
||||
void NackContext::recordNack(const FCI_NACK &nack) {
|
||||
auto now = getCurrentMillisecond();
|
||||
auto i = nack.getPid();
|
||||
for (auto flag : nack.getBitArray()) {
|
||||
if (flag) {
|
||||
auto &ref = _nack_send_ntp[i];
|
||||
ref.first_stamp = now;
|
||||
ref.update_stamp = now;
|
||||
ref.nack_count = 1;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
//记录太多了,移除一部分早期的记录
|
||||
while (_nack_send_ntp.size() > kNackMaxSize) {
|
||||
_nack_send_ntp.erase(_nack_send_ntp.begin());
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t NackContext::reSendNack() {
|
||||
set<uint16_t> nack_rtp;
|
||||
auto now = getCurrentMillisecond();
|
||||
for (auto it = _nack_send_ntp.begin(); it != _nack_send_ntp.end();) {
|
||||
if (now - it->second.first_stamp > kNackMaxMS) {
|
||||
//该rtp丢失太久了,不再要求重传
|
||||
it = _nack_send_ntp.erase(it);
|
||||
continue;
|
||||
}
|
||||
if (now - it->second.update_stamp < 2 * _rtt) {
|
||||
//距离上次nack不足2倍的rtt,不用再发送nack
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
//此rtp需要请求重传
|
||||
nack_rtp.emplace(it->first);
|
||||
//更新nack发送时间戳
|
||||
it->second.update_stamp = now;
|
||||
if (++(it->second.nack_count) == kNackMaxCount) {
|
||||
//nack次数太多,移除之
|
||||
it = _nack_send_ntp.erase(it);
|
||||
continue;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
|
||||
if (_nack_send_ntp.empty()) {
|
||||
//不需要再发送nack
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pid = -1;
|
||||
vector<bool> vec;
|
||||
for (auto it = nack_rtp.begin(); it != nack_rtp.end();) {
|
||||
if (pid == -1) {
|
||||
pid = *it;
|
||||
vec.resize(16, false);
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
auto inc = *it - pid;
|
||||
if (inc > FCI_NACK::kBitSize) {
|
||||
//新的nack包
|
||||
doNack(FCI_NACK(pid, vec), false);
|
||||
pid = -1;
|
||||
continue;
|
||||
}
|
||||
//这个包丢了
|
||||
vec[inc - 1] = true;
|
||||
++it;
|
||||
}
|
||||
if (pid != -1) {
|
||||
doNack(FCI_NACK(pid, vec), false);
|
||||
}
|
||||
|
||||
//重传间隔不得低于5ms
|
||||
return std::max(_rtt, 5);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user