优化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 toolkit;
using namespace mediakit; using namespace mediakit;
extern void testFCI();
int main() { int main() {
Logger::Instance().add(std::make_shared<ConsoleChannel>()); Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
srand((unsigned)time(NULL)); srand((unsigned)time(NULL));
NackContext ctx; NackContext ctx;
for (int i = 1; i < 1000; ++i) { ctx.setOnNack([](const FCI_NACK &nack){
if (i % (1 + (rand() % 30)) == 0) { InfoL << nack.dumpString();
DebugL << "drop:" << i; });
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 { } 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); sleep(1);

View File

@ -95,90 +95,122 @@ int64_t NackList::getRtpStamp(uint16_t seq) {
//////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////
void NackContext::received(uint16_t seq, bool is_rtx) { NackContext::NackContext() {
if (!_last_max_seq && _seq.empty()) { setOnNack(nullptr);
_last_max_seq = seq - 1;
} }
if (is_rtx || (seq < _last_max_seq && !(seq < 1024 && _last_max_seq > UINT16_MAX - 1024))) {
//重传包或 void NackContext::received(uint16_t seq, bool is_rtx) {
// seq回退且非回环那么这个应该是重传包 if (!_started) {
onRtx(seq); // 记录第一个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; return;
} }
_seq.emplace(seq);
auto max_seq = *_seq.rbegin(); auto max_seq = *_seq.rbegin();
auto min_seq = *_seq.begin(); auto min_seq = *_seq.begin();
auto diff = max_seq - min_seq; auto diff = max_seq - min_seq;
if (!diff) { if (diff > (UINT16_MAX >> 1)) {
// 回环后收到回环前的大值seq, 忽略掉
_seq.erase(max_seq);
return; return;
} }
if (min_seq == (uint16_t)(_nack_seq + 1) && _seq.size() == diff + 1) {
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) {
// 都是连续的seq未丢包 // 都是连续的seq未丢包
_seq.clear(); _seq.clear();
_last_max_seq = max_seq; _nack_seq = max_seq;
} else { } else {
// seq不连续有丢包 // seq不连续有丢包
if (min_seq == _last_max_seq + 1) { makeNack(max_seq, false);
//前面部分seq是连续的未丢包移除之 }
eraseFrontSeq();
} }
//有丢包丢包从_last_max_seq开始 void NackContext::makeNack(uint16_t max_seq, bool flush) {
auto nack_rtp_count = FCI_NACK::kBitSize; // 尝试移除前面部分连续的seq
if (max_seq > nack_rtp_count + _last_max_seq) { eraseFrontSeq();
vector<bool> vec;
vec.resize(FCI_NACK::kBitSize, false); if (max_seq == _nack_seq) {
for (size_t i = 0; i < nack_rtp_count; ++i) { // 完成所有seq丢包判断
vec[i] = _seq.find(_last_max_seq + i + 2) == _seq.end(); return;
} }
doNack(FCI_NACK(_last_max_seq + 1, vec), true);
_last_max_seq += nack_rtp_count + 1; // 有丢包丢包从_last_max_seq开始统计丢包
if (_last_max_seq >= 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(); _seq.clear();
} else { } 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); _seq.erase(_seq.begin(), it);
} }
} }
} }
}
void NackContext::setOnNack(onNack cb) { void NackContext::setOnNack(onNack cb) {
if (cb) {
_cb = std::move(cb); _cb = std::move(cb);
} else {
_cb = [](const FCI_NACK &nack) {};
}
} }
void NackContext::doNack(const FCI_NACK &nack, bool record_nack) { void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
if (record_nack) { if (record_nack) {
recordNack(nack); recordNack(nack);
} }
if (_cb) {
_cb(nack); _cb(nack);
} }
}
void NackContext::eraseFrontSeq() { void NackContext::eraseFrontSeq() {
// 前面部分seq是连续的未丢包移除之 // 前面部分seq是连续的未丢包移除之
for (auto it = _seq.begin(); it != _seq.end();) { for (auto it = _seq.begin(); it != _seq.end();) {
if (*it != _last_max_seq + 1) { if (*it != (uint16_t)(_nack_seq + 1)) {
// seq不连续丢包了 // seq不连续丢包了
break; break;
} }
_last_max_seq = *it; _nack_seq = *it;
it = _seq.erase(it); it = _seq.erase(it);
} }
} }
void NackContext::onRtx(uint16_t seq) { void NackContext::clearNackStatus(uint16_t seq) {
auto it = _nack_send_status.find(seq); auto it = _nack_send_status.find(seq);
if (it == _nack_send_status.end()) { if (it == _nack_send_status.end()) {
return; return;
@ -187,9 +219,8 @@ void NackContext::onRtx(uint16_t seq) {
_nack_send_status.erase(it); _nack_send_status.erase(it);
if (rtt >= 0) { if (rtt >= 0) {
// rtt不小于0 // rtt不小于0
_rtt = rtt; _rtt = rtt;
// InfoL << "rtt:" << rtt;
} }
} }

View File

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