优化webrtc nack算法:#2249

This commit is contained in:
ziyue 2023-02-25 00:19:00 +08:00
parent a15053ff01
commit a981ce8cb3
3 changed files with 126 additions and 74 deletions

View File

@ -15,20 +15,38 @@ using namespace std;
using namespace toolkit;
using namespace mediakit;
extern void testFCI();
int main() {
Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
srand((unsigned)time(NULL));
NackContext ctx;
for (int i = 1; i < 1000; ++i) {
if (i % (1 + (rand() % 30)) == 0) {
DebugL << "drop:" << i;
ctx.setOnNack([](const FCI_NACK &nack){
InfoL << nack.dumpString();
});
auto drop_start = 0;
auto drop_len = 0;
uint16_t offset = 0xFFFF - 200 - 50;
for (int i = 1; i < 10000; ++i) {
if (i % 100 == 0) {
drop_start = i + rand() % 16;
drop_len = 4 + rand() % 16;
InfoL << "start drop:" << (uint16_t)(drop_start + offset) << " -> "
<< (uint16_t)(drop_start + offset + drop_len);
}
uint16_t seq = i + offset;
if ((i >= drop_start && i <= drop_start + drop_len) || seq == 65535 || seq == 0 || seq == 1) {
TraceL << "drop:" << (uint16_t)(i + offset);
} else {
ctx.received(i);
static auto last_seq = seq;
if (seq - last_seq > 16) {
ctx.received(last_seq);
ctx.received(seq);
DebugL << "seq reduce:" << last_seq;
last_seq = seq;
} else {
ctx.received(seq);
}
}
}
sleep(1);

View File

@ -95,90 +95,122 @@ int64_t NackList::getRtpStamp(uint16_t seq) {
////////////////////////////////////////////////////////////////////////////////////////////////
void NackContext::received(uint16_t seq, bool is_rtx) {
if (!_last_max_seq && _seq.empty()) {
_last_max_seq = seq - 1;
NackContext::NackContext() {
setOnNack(nullptr);
}
if (is_rtx || (seq < _last_max_seq && !(seq < 1024 && _last_max_seq > UINT16_MAX - 1024))) {
//重传包或
// seq回退且非回环那么这个应该是重传包
onRtx(seq);
void NackContext::received(uint16_t seq, bool is_rtx) {
if (!_started) {
// 记录第一个seq
_started = true;
_nack_seq = seq - 1;
}
if (seq < _nack_seq && _nack_seq != UINT16_MAX && seq < 1024 && _nack_seq > UINT16_MAX - 1024) {
// seq回环,清空回环前状态
makeNack(UINT16_MAX, true);
// 等待下一个seq为0的包
_nack_seq = UINT16_MAX;
_seq.emplace(seq);
return;
}
if (is_rtx || (seq < _nack_seq && _nack_seq != UINT16_MAX)) {
// seq非回环回退包猜测其为重传包清空其nack状态
clearNackStatus(seq);
return;
}
auto pr = _seq.emplace(seq);
if (!pr.second) {
// seq重复, 忽略
return;
}
_seq.emplace(seq);
auto max_seq = *_seq.rbegin();
auto min_seq = *_seq.begin();
auto diff = max_seq - min_seq;
if (!diff) {
if (diff > (UINT16_MAX >> 1)) {
// 回环后收到回环前的大值seq, 忽略掉
_seq.erase(max_seq);
return;
}
if (diff > UINT16_MAX / 2) {
//回环
_seq.clear();
_last_max_seq = min_seq;
_nack_send_status.clear();
return;
}
if (_seq.size() == (size_t)diff + 1 && _last_max_seq + 1 == min_seq) {
if (min_seq == (uint16_t)(_nack_seq + 1) && _seq.size() == diff + 1) {
// 都是连续的seq未丢包
_seq.clear();
_last_max_seq = max_seq;
_nack_seq = max_seq;
} else {
// seq不连续有丢包
if (min_seq == _last_max_seq + 1) {
//前面部分seq是连续的未丢包移除之
eraseFrontSeq();
makeNack(max_seq, false);
}
}
//有丢包丢包从_last_max_seq开始
auto nack_rtp_count = FCI_NACK::kBitSize;
if (max_seq > nack_rtp_count + _last_max_seq) {
vector<bool> vec;
vec.resize(FCI_NACK::kBitSize, false);
for (size_t i = 0; i < nack_rtp_count; ++i) {
vec[i] = _seq.find(_last_max_seq + i + 2) == _seq.end();
void NackContext::makeNack(uint16_t max_seq, bool flush) {
// 尝试移除前面部分连续的seq
eraseFrontSeq();
if (max_seq == _nack_seq) {
// 完成所有seq丢包判断
return;
}
doNack(FCI_NACK(_last_max_seq + 1, vec), true);
_last_max_seq += nack_rtp_count + 1;
if (_last_max_seq >= max_seq) {
// 有丢包丢包从_last_max_seq开始统计丢包
uint16_t nack_rtp_count;
if (flush) {
nack_rtp_count = max_seq - _nack_seq - 1;
nack_rtp_count = MIN(FCI_NACK::kBitSize, nack_rtp_count);
} else {
nack_rtp_count = FCI_NACK::kBitSize;
}
auto max_nack = 5u;
// 最多生成5个nack包防止seq大幅跳跃导致一直循环
while (max_nack-- && max_seq > (uint16_t)(nack_rtp_count + _nack_seq)) {
vector<bool> vec;
vec.resize(nack_rtp_count, false);
for (size_t i = 0; i < nack_rtp_count; ++i) {
vec[i] = _seq.find((uint16_t)(_nack_seq + i + 2)) == _seq.end();
}
doNack(FCI_NACK(_nack_seq + 1, vec), true);
_nack_seq += nack_rtp_count + 1;
if (_nack_seq >= max_seq) {
_seq.clear();
} else {
auto it = _seq.emplace_hint(_seq.begin(), _last_max_seq + 1);
// 返回第一个比_last_max_seq大的元素
auto it = _seq.upper_bound(_nack_seq);
// 移除 <=_last_max_seq 的seq
_seq.erase(_seq.begin(), it);
}
}
}
}
void NackContext::setOnNack(onNack cb) {
if (cb) {
_cb = std::move(cb);
} else {
_cb = [](const FCI_NACK &nack) {};
}
}
void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
if (record_nack) {
recordNack(nack);
}
if (_cb) {
_cb(nack);
}
}
void NackContext::eraseFrontSeq() {
// 前面部分seq是连续的未丢包移除之
for (auto it = _seq.begin(); it != _seq.end();) {
if (*it != _last_max_seq + 1) {
if (*it != (uint16_t)(_nack_seq + 1)) {
// seq不连续丢包了
break;
}
_last_max_seq = *it;
_nack_seq = *it;
it = _seq.erase(it);
}
}
void NackContext::onRtx(uint16_t seq) {
void NackContext::clearNackStatus(uint16_t seq) {
auto it = _nack_send_status.find(seq);
if (it == _nack_send_status.end()) {
return;
@ -187,9 +219,8 @@ void NackContext::onRtx(uint16_t seq) {
_nack_send_status.erase(it);
if (rtt >= 0) {
// rtt不小于0
// rtt不小于0
_rtt = rtt;
// InfoL << "rtt:" << rtt;
}
}

View File

@ -53,7 +53,7 @@ public:
// nack重传频率rtt的倍数
static constexpr auto kNackIntervalRatio = 1.0f;
NackContext() = default;
NackContext();
~NackContext() = default;
void received(uint16_t seq, bool is_rtx = false);
@ -64,13 +64,16 @@ private:
void eraseFrontSeq();
void doNack(const FCI_NACK &nack, bool record_nack);
void recordNack(const FCI_NACK &nack);
void onRtx(uint16_t seq);
void clearNackStatus(uint16_t seq);
void makeNack(uint16_t max, bool flush = false);
private:
bool _started = false;
int _rtt = 50;
onNack _cb;
std::set<uint16_t> _seq;
uint16_t _last_max_seq = 0;
// 最新nack包中的rtp seq值
uint16_t _nack_seq = 0;
struct NackStatus {
uint64_t first_stamp;