2021-06-25 15:43:47 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Nack.h"
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2022-02-27 02:08:17 +08:00
|
|
|
|
static constexpr uint32_t kMaxNackMS = 5 * 1000;
|
2022-02-27 12:24:54 +08:00
|
|
|
|
static constexpr uint32_t kRtpCacheCheckInterval = 100;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
void NackList::pushBack(RtpPacket::Ptr rtp) {
|
2021-06-25 15:43:47 +08:00
|
|
|
|
auto seq = rtp->getSeq();
|
|
|
|
|
_nack_cache_seq.emplace_back(seq);
|
|
|
|
|
_nack_cache_pkt.emplace(seq, std::move(rtp));
|
2022-02-27 12:24:54 +08:00
|
|
|
|
if (++_cache_ms_check < kRtpCacheCheckInterval) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_cache_ms_check = 0;
|
|
|
|
|
while (getCacheMS() >= kMaxNackMS) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 需要清除部分nack缓存
|
2022-02-27 02:07:25 +08:00
|
|
|
|
popFront();
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
void NackList::forEach(const FCI_NACK &nack, const function<void(const RtpPacket::Ptr &rtp)> &func) {
|
2021-06-25 15:43:47 +08:00
|
|
|
|
auto seq = nack.getPid();
|
|
|
|
|
for (auto bit : nack.getBitArray()) {
|
|
|
|
|
if (bit) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 丢包
|
2022-02-27 02:07:25 +08:00
|
|
|
|
RtpPacket::Ptr *ptr = getRtp(seq);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
if (ptr) {
|
|
|
|
|
func(*ptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
++seq;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
void NackList::popFront() {
|
2021-06-25 15:43:47 +08:00
|
|
|
|
if (_nack_cache_seq.empty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_nack_cache_pkt.erase(_nack_cache_seq.front());
|
|
|
|
|
_nack_cache_seq.pop_front();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
RtpPacket::Ptr *NackList::getRtp(uint16_t seq) {
|
2021-06-25 15:43:47 +08:00
|
|
|
|
auto it = _nack_cache_pkt.find(seq);
|
|
|
|
|
if (it == _nack_cache_pkt.end()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return &it->second;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 02:07:25 +08:00
|
|
|
|
uint32_t NackList::getCacheMS() {
|
2022-02-27 12:24:54 +08:00
|
|
|
|
while (_nack_cache_seq.size() > 2) {
|
|
|
|
|
auto back_stamp = getRtpStamp(_nack_cache_seq.back());
|
|
|
|
|
if (back_stamp == -1) {
|
|
|
|
|
_nack_cache_seq.pop_back();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-27 02:07:25 +08:00
|
|
|
|
|
2022-02-27 12:24:54 +08:00
|
|
|
|
auto front_stamp = getRtpStamp(_nack_cache_seq.front());
|
|
|
|
|
if (front_stamp == -1) {
|
|
|
|
|
_nack_cache_seq.pop_front();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-02-27 02:07:25 +08:00
|
|
|
|
|
2022-02-27 12:24:54 +08:00
|
|
|
|
if (back_stamp >= front_stamp) {
|
|
|
|
|
return back_stamp - front_stamp;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 很有可能回环了
|
2022-02-27 12:24:54 +08:00
|
|
|
|
return back_stamp + (UINT32_MAX - front_stamp);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
2022-02-27 12:24:54 +08:00
|
|
|
|
return 0;
|
2022-02-27 02:07:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t NackList::getRtpStamp(uint16_t seq) {
|
|
|
|
|
auto it = _nack_cache_pkt.find(seq);
|
|
|
|
|
if (it == _nack_cache_pkt.end()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return it->second->getStampMS(false);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-02-25 00:19:00 +08:00
|
|
|
|
NackContext::NackContext() {
|
|
|
|
|
setOnNack(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 20:37:43 +08:00
|
|
|
|
void NackContext::received(uint16_t seq, bool is_rtx) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
if (!_started) {
|
|
|
|
|
// 记录第一个seq
|
|
|
|
|
_started = true;
|
|
|
|
|
_nack_seq = seq - 1;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
|
|
|
|
|
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重复, 忽略
|
2021-07-27 20:37:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 15:43:47 +08:00
|
|
|
|
auto max_seq = *_seq.rbegin();
|
|
|
|
|
auto min_seq = *_seq.begin();
|
|
|
|
|
auto diff = max_seq - min_seq;
|
2023-02-25 00:19:00 +08:00
|
|
|
|
if (diff > (UINT16_MAX >> 1)) {
|
|
|
|
|
// 回环后,收到回环前的大值seq, 忽略掉
|
|
|
|
|
_seq.erase(max_seq);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
if (min_seq == (uint16_t)(_nack_seq + 1) && _seq.size() == diff + 1) {
|
|
|
|
|
// 都是连续的seq,未丢包
|
2021-06-25 15:43:47 +08:00
|
|
|
|
_seq.clear();
|
2023-02-25 00:19:00 +08:00
|
|
|
|
_nack_seq = max_seq;
|
|
|
|
|
} else {
|
|
|
|
|
// seq不连续,有丢包
|
|
|
|
|
makeNack(max_seq, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NackContext::makeNack(uint16_t max_seq, bool flush) {
|
|
|
|
|
// 尝试移除前面部分连续的seq
|
|
|
|
|
eraseFrontSeq();
|
|
|
|
|
|
|
|
|
|
if (max_seq == _nack_seq) {
|
|
|
|
|
// 完成所有seq丢包判断
|
2021-06-25 15:43:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 有丢包,丢包从_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);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
} else {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
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();
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
doNack(FCI_NACK(_nack_seq + 1, vec), true);
|
|
|
|
|
_nack_seq += nack_rtp_count + 1;
|
|
|
|
|
if (_nack_seq >= max_seq) {
|
|
|
|
|
_seq.clear();
|
|
|
|
|
} else {
|
|
|
|
|
// 返回第一个比_last_max_seq大的元素
|
|
|
|
|
auto it = _seq.upper_bound(_nack_seq);
|
|
|
|
|
// 移除 <=_last_max_seq 的seq
|
|
|
|
|
_seq.erase(_seq.begin(), it);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NackContext::setOnNack(onNack cb) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
if (cb) {
|
|
|
|
|
_cb = std::move(cb);
|
|
|
|
|
} else {
|
|
|
|
|
_cb = [](const FCI_NACK &nack) {};
|
|
|
|
|
}
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 20:37:43 +08:00
|
|
|
|
void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
|
|
|
|
|
if (record_nack) {
|
|
|
|
|
recordNack(nack);
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
_cb(nack);
|
2021-06-25 15:43:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NackContext::eraseFrontSeq() {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 前面部分seq是连续的,未丢包,移除之
|
2021-06-25 15:43:47 +08:00
|
|
|
|
for (auto it = _seq.begin(); it != _seq.end();) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
if (*it != (uint16_t)(_nack_seq + 1)) {
|
2022-09-18 21:03:05 +08:00
|
|
|
|
// seq不连续,丢包了
|
2021-06-25 15:43:47 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
_nack_seq = *it;
|
2021-06-25 15:43:47 +08:00
|
|
|
|
it = _seq.erase(it);
|
|
|
|
|
}
|
2021-07-27 20:37:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 00:19:00 +08:00
|
|
|
|
void NackContext::clearNackStatus(uint16_t seq) {
|
2021-07-27 20:52:51 +08:00
|
|
|
|
auto it = _nack_send_status.find(seq);
|
|
|
|
|
if (it == _nack_send_status.end()) {
|
2021-07-27 20:37:43 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto rtt = getCurrentMillisecond() - it->second.update_stamp;
|
2021-07-27 20:52:51 +08:00
|
|
|
|
_nack_send_status.erase(it);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
|
|
|
|
|
if (rtt >= 0) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// rtt不能小于0
|
2021-07-27 20:37:43 +08:00
|
|
|
|
_rtt = rtt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NackContext::recordNack(const FCI_NACK &nack) {
|
|
|
|
|
auto now = getCurrentMillisecond();
|
|
|
|
|
auto i = nack.getPid();
|
|
|
|
|
for (auto flag : nack.getBitArray()) {
|
|
|
|
|
if (flag) {
|
2021-07-27 20:52:51 +08:00
|
|
|
|
auto &ref = _nack_send_status[i];
|
2021-07-27 20:37:43 +08:00
|
|
|
|
ref.first_stamp = now;
|
|
|
|
|
ref.update_stamp = now;
|
|
|
|
|
ref.nack_count = 1;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 记录太多了,移除一部分早期的记录
|
2021-07-27 20:52:51 +08:00
|
|
|
|
while (_nack_send_status.size() > kNackMaxSize) {
|
|
|
|
|
_nack_send_status.erase(_nack_send_status.begin());
|
2021-07-27 20:37:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64_t NackContext::reSendNack() {
|
|
|
|
|
set<uint16_t> nack_rtp;
|
|
|
|
|
auto now = getCurrentMillisecond();
|
2021-07-27 20:52:51 +08:00
|
|
|
|
for (auto it = _nack_send_status.begin(); it != _nack_send_status.end();) {
|
2021-07-27 20:37:43 +08:00
|
|
|
|
if (now - it->second.first_stamp > kNackMaxMS) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 该rtp丢失太久了,不再要求重传
|
2021-07-27 20:52:51 +08:00
|
|
|
|
it = _nack_send_status.erase(it);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-07-28 11:18:09 +08:00
|
|
|
|
if (now - it->second.update_stamp < kNackIntervalRatio * _rtt) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 距离上次nack不足2倍的rtt,不用再发送nack
|
2021-07-27 20:37:43 +08:00
|
|
|
|
++it;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 此rtp需要请求重传
|
2021-07-27 20:37:43 +08:00
|
|
|
|
nack_rtp.emplace(it->first);
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 更新nack发送时间戳
|
2021-07-27 20:37:43 +08:00
|
|
|
|
it->second.update_stamp = now;
|
|
|
|
|
if (++(it->second.nack_count) == kNackMaxCount) {
|
2022-09-18 21:03:05 +08:00
|
|
|
|
// nack次数太多,移除之
|
2021-07-27 20:52:51 +08:00
|
|
|
|
it = _nack_send_status.erase(it);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 20:52:51 +08:00
|
|
|
|
if (_nack_send_status.empty()) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 不需要再发送nack
|
2021-07-27 20:37:43 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pid = -1;
|
|
|
|
|
vector<bool> vec;
|
|
|
|
|
for (auto it = nack_rtp.begin(); it != nack_rtp.end();) {
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
pid = *it;
|
2021-07-27 20:52:51 +08:00
|
|
|
|
vec.resize(FCI_NACK::kBitSize, false);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
++it;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
auto inc = *it - pid;
|
2021-12-30 15:28:02 +08:00
|
|
|
|
if (inc > (ssize_t)FCI_NACK::kBitSize) {
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 新的nack包
|
2021-07-27 20:37:43 +08:00
|
|
|
|
doNack(FCI_NACK(pid, vec), false);
|
|
|
|
|
pid = -1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 这个包丢了
|
2021-07-27 20:37:43 +08:00
|
|
|
|
vec[inc - 1] = true;
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
if (pid != -1) {
|
|
|
|
|
doNack(FCI_NACK(pid, vec), false);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-25 00:19:00 +08:00
|
|
|
|
// 重传间隔不得低于5ms
|
2021-08-14 10:42:32 +08:00
|
|
|
|
return max(_rtt, 5);
|
2021-07-27 20:37:43 +08:00
|
|
|
|
}
|
2022-09-18 21:03:05 +08:00
|
|
|
|
|
|
|
|
|
} // namespace mediakit
|