Nack.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT-like license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "Nack.h"
  11. #include "Common/config.h"
  12. using namespace std;
  13. using namespace toolkit;
  14. namespace mediakit {
  15. // RTC配置项目 [AUTO-TRANSLATED:19940011]
  16. // RTC configuration project
  17. namespace Rtc {
  18. #define RTC_FIELD "rtc."
  19. // ~ nack接收端, rtp发送端 [AUTO-TRANSLATED:e1065811]
  20. // ~ nack receiver, rtp sender
  21. // rtp重发缓存列队最大长度,单位毫秒 [AUTO-TRANSLATED:80bccd26]
  22. // rtp retransmission cache queue maximum length, in milliseconds
  23. const string kMaxRtpCacheMS = RTC_FIELD "maxRtpCacheMS";
  24. // rtp重发缓存列队最大长度,单位个数 [AUTO-TRANSLATED:7d710bf5]
  25. // rtp retransmission cache queue maximum length, in number
  26. const string kMaxRtpCacheSize = RTC_FIELD "maxRtpCacheSize";
  27. // ~ nack发送端,rtp接收端 [AUTO-TRANSLATED:bb169205]
  28. // ~ nack sender, rtp receiver
  29. // 最大保留的rtp丢包状态个数 [AUTO-TRANSLATED:3aeb53f8]
  30. // Maximum number of rtp packet loss states to keep
  31. const string kNackMaxSize = RTC_FIELD "nackMaxSize";
  32. // rtp丢包状态最长保留时间 [AUTO-TRANSLATED:f9306375]
  33. // Maximum retention time for rtp packet loss state
  34. const string kNackMaxMS = RTC_FIELD "nackMaxMS";
  35. // nack最多请求重传次数 [AUTO-TRANSLATED:300be0d0]
  36. // Maximum number of nack retransmission requests
  37. const string kNackMaxCount = RTC_FIELD "nackMaxCount";
  38. // nack重传频率,rtt的倍数 [AUTO-TRANSLATED:924d53d2]
  39. // Nack retransmission frequency, multiple of rtt
  40. const string kNackIntervalRatio = RTC_FIELD "nackIntervalRatio";
  41. // nack包中rtp个数,减小此值可以让nack包响应更灵敏 [AUTO-TRANSLATED:12393868]
  42. // Number of rtp in nack packet, reducing this value can make nack packet response more sensitive
  43. const string kNackRtpSize = RTC_FIELD "nackRtpSize";
  44. static onceToken token([]() {
  45. mINI::Instance()[kMaxRtpCacheMS] = 5 * 1000;
  46. mINI::Instance()[kMaxRtpCacheSize] = 2048;
  47. mINI::Instance()[kNackMaxSize] = 2048;
  48. mINI::Instance()[kNackMaxMS] = 3 * 1000;
  49. mINI::Instance()[kNackMaxCount] = 15;
  50. mINI::Instance()[kNackIntervalRatio] = 1.0f;
  51. mINI::Instance()[kNackRtpSize] = 8;
  52. });
  53. } // namespace Rtc
  54. void NackList::pushBack(RtpPacket::Ptr rtp) {
  55. GET_CONFIG(uint32_t, max_rtp_cache_ms, Rtc::kMaxRtpCacheMS);
  56. GET_CONFIG(uint32_t, max_rtp_cache_size, Rtc::kMaxRtpCacheSize);
  57. // 记录rtp [AUTO-TRANSLATED:f08e12e2]
  58. // Record rtp
  59. auto seq = rtp->getSeq();
  60. _nack_cache_seq.emplace_back(seq);
  61. _nack_cache_pkt.emplace(seq, std::move(rtp));
  62. // 限制rtp缓存最大个数 [AUTO-TRANSLATED:a6bb50f5]
  63. // Limit the maximum number of rtp cache
  64. if (_nack_cache_seq.size() > max_rtp_cache_size) {
  65. popFront();
  66. }
  67. if (++_cache_ms_check < 100) {
  68. // 每100个rtp包检测下缓存长度,节省cpu资源 [AUTO-TRANSLATED:6399c705]
  69. // Check the cache length every 100 rtp packets to save cpu resources
  70. return;
  71. }
  72. _cache_ms_check = 0;
  73. // 限制rtp缓存最大时长 [AUTO-TRANSLATED:83e5be93]
  74. // Limit the maximum duration of rtp cache
  75. while (getCacheMS() >= max_rtp_cache_ms) {
  76. popFront();
  77. }
  78. }
  79. void NackList::forEach(const FCI_NACK &nack, const function<void(const RtpPacket::Ptr &rtp)> &func) {
  80. auto seq = nack.getPid();
  81. for (auto bit : nack.getBitArray()) {
  82. if (bit) {
  83. // 丢包 [AUTO-TRANSLATED:ac2c9d55]
  84. // Packet loss
  85. RtpPacket::Ptr *ptr = getRtp(seq);
  86. if (ptr) {
  87. func(*ptr);
  88. }
  89. }
  90. ++seq;
  91. }
  92. }
  93. void NackList::popFront() {
  94. if (_nack_cache_seq.empty()) {
  95. return;
  96. }
  97. _nack_cache_pkt.erase(_nack_cache_seq.front());
  98. _nack_cache_seq.pop_front();
  99. }
  100. RtpPacket::Ptr *NackList::getRtp(uint16_t seq) {
  101. auto it = _nack_cache_pkt.find(seq);
  102. if (it == _nack_cache_pkt.end()) {
  103. return nullptr;
  104. }
  105. return &it->second;
  106. }
  107. uint32_t NackList::getCacheMS() {
  108. while (_nack_cache_seq.size() > 2) {
  109. auto back_stamp = getNtpStamp(_nack_cache_seq.back());
  110. if (back_stamp == -1) {
  111. _nack_cache_seq.pop_back();
  112. continue;
  113. }
  114. auto front_stamp = getNtpStamp(_nack_cache_seq.front());
  115. if (front_stamp == -1) {
  116. _nack_cache_seq.pop_front();
  117. continue;
  118. }
  119. if (back_stamp >= front_stamp) {
  120. return back_stamp - front_stamp;
  121. }
  122. // ntp时间戳回退了,非法数据,丢掉 [AUTO-TRANSLATED:79ddf252]
  123. // Ntp timestamp has been rolled back, illegal data, discard
  124. _nack_cache_seq.pop_front();
  125. }
  126. return 0;
  127. }
  128. int64_t NackList::getNtpStamp(uint16_t seq) {
  129. auto it = _nack_cache_pkt.find(seq);
  130. if (it == _nack_cache_pkt.end()) {
  131. return -1;
  132. }
  133. // 使用ntp时间戳,不会回退 [AUTO-TRANSLATED:2d509f8f]
  134. // Use ntp timestamp, will not roll back
  135. return it->second->getStampMS(true);
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////////////////////
  138. NackContext::NackContext() {
  139. setOnNack(nullptr);
  140. }
  141. void NackContext::received(uint16_t seq, bool is_rtx) {
  142. if (!_started) {
  143. // 记录第一个seq [AUTO-TRANSLATED:410c831f]
  144. // Record the first seq
  145. _started = true;
  146. _nack_seq = seq - 1;
  147. }
  148. if (seq < _nack_seq && _nack_seq != UINT16_MAX && seq < 1024 && _nack_seq > UINT16_MAX - 1024) {
  149. // seq回环,清空回环前状态 [AUTO-TRANSLATED:4cb8027e]
  150. // Seq loop, clear the state before the loop
  151. makeNack(UINT16_MAX, true);
  152. _seq.emplace(seq);
  153. return;
  154. }
  155. if (is_rtx || (seq < _nack_seq && _nack_seq != UINT16_MAX)) {
  156. // seq非回环回退包,猜测其为重传包,清空其nack状态 [AUTO-TRANSLATED:74c7b706]
  157. // Seq non-loop rollback packet, guess it is a retransmission packet, clear its nack state
  158. clearNackStatus(seq);
  159. return;
  160. }
  161. auto pr = _seq.emplace(seq);
  162. if (!pr.second) {
  163. // seq重复, 忽略 [AUTO-TRANSLATED:95ec10db]
  164. // Seq duplicate, ignore
  165. return;
  166. }
  167. auto max_seq = *_seq.rbegin();
  168. auto min_seq = *_seq.begin();
  169. auto diff = max_seq - min_seq;
  170. if (diff > (UINT16_MAX >> 1)) {
  171. // 回环后,收到回环前的大值seq, 忽略掉 [AUTO-TRANSLATED:6a30b91f]
  172. // After the loop, receive a large seq value before the loop, ignore it
  173. _seq.erase(max_seq);
  174. return;
  175. }
  176. if (min_seq == (uint16_t)(_nack_seq + 1) && _seq.size() == (size_t)diff + 1) {
  177. // 都是连续的seq,未丢包 [AUTO-TRANSLATED:62d3ffbd]
  178. // All are continuous seq, no packet loss
  179. _seq.clear();
  180. _nack_seq = max_seq;
  181. } else {
  182. // seq不连续,有丢包 [AUTO-TRANSLATED:ba1bfbc2]
  183. // Seq is not continuous, there is packet loss
  184. makeNack(max_seq, false);
  185. }
  186. }
  187. void NackContext::makeNack(uint16_t max_seq, bool flush) {
  188. // 尝试移除前面部分连续的seq [AUTO-TRANSLATED:04593c1b]
  189. // Try to remove the continuous seq in front
  190. eraseFrontSeq();
  191. // 最多生成5个nack包,防止seq大幅跳跃导致一直循环 [AUTO-TRANSLATED:9cc5da25]
  192. // Generate at most 5 nack packets to prevent seq from jumping significantly and causing continuous loops
  193. auto max_nack = 5u;
  194. GET_CONFIG(uint32_t, nack_rtpsize, Rtc::kNackRtpSize);
  195. // kNackRtpSize must between 0 and 16
  196. nack_rtpsize = std::min<uint32_t>(nack_rtpsize, FCI_NACK::kBitSize);
  197. while (_nack_seq != max_seq && max_nack--) {
  198. // 一次不能发送超过16+1个rtp的状态 [AUTO-TRANSLATED:1954831a]
  199. // Cannot send more than 16+1 rtp states at a time
  200. uint16_t nack_rtp_count = std::min<uint16_t>(FCI_NACK::kBitSize, max_seq - (uint16_t)(_nack_seq + 1));
  201. if (!flush && nack_rtp_count < nack_rtpsize) {
  202. // 非flush状态下,seq个数不足以发送一次nack [AUTO-TRANSLATED:94f561c1]
  203. // In non-flush state, the number of seq is not enough to send a nack
  204. break;
  205. }
  206. vector<bool> vec;
  207. vec.resize(nack_rtp_count, false);
  208. for (size_t i = 0; i < nack_rtp_count; ++i) {
  209. vec[i] = _seq.find((uint16_t)(_nack_seq + i + 2)) == _seq.end();
  210. }
  211. doNack(FCI_NACK(_nack_seq + 1, vec), true);
  212. _nack_seq += nack_rtp_count + 1;
  213. // 返回第一个比_last_max_seq大的元素 [AUTO-TRANSLATED:425c4e63]
  214. // Return the first element greater than _last_max_seq
  215. auto it = _seq.upper_bound(_nack_seq);
  216. // 移除 <=_last_max_seq 的seq [AUTO-TRANSLATED:a64ff3fd]
  217. // Remove seq <= _last_max_seq
  218. _seq.erase(_seq.begin(), it);
  219. }
  220. }
  221. void NackContext::setOnNack(onNack cb) {
  222. if (cb) {
  223. _cb = std::move(cb);
  224. } else {
  225. _cb = [](const FCI_NACK &nack) {};
  226. }
  227. }
  228. void NackContext::doNack(const FCI_NACK &nack, bool record_nack) {
  229. if (record_nack) {
  230. recordNack(nack);
  231. }
  232. _cb(nack);
  233. }
  234. void NackContext::eraseFrontSeq() {
  235. // 前面部分seq是连续的,未丢包,移除之 [AUTO-TRANSLATED:ef3eed87]
  236. // The previous part of the sequence is continuous and has no packet loss, remove it.
  237. for (auto it = _seq.begin(); it != _seq.end();) {
  238. if (*it != (uint16_t)(_nack_seq + 1)) {
  239. // seq不连续,丢包了 [AUTO-TRANSLATED:dcee49fe]
  240. // The sequence is not continuous, there is packet loss.
  241. break;
  242. }
  243. _nack_seq = *it;
  244. it = _seq.erase(it);
  245. }
  246. }
  247. void NackContext::clearNackStatus(uint16_t seq) {
  248. auto it = _nack_send_status.find(seq);
  249. if (it == _nack_send_status.end()) {
  250. return;
  251. }
  252. // 收到重传包与第一个nack包间的时间约等于rtt时间 [AUTO-TRANSLATED:f702811e]
  253. // The time between receiving the retransmitted packet and the first nack packet is approximately equal to the rtt time.
  254. auto rtt = getCurrentMillisecond() - it->second.first_stamp;
  255. _nack_send_status.erase(it);
  256. // 限定rtt在合理有效范围内 [AUTO-TRANSLATED:42fbed04]
  257. // Limit the rtt within a reasonable and valid range.
  258. GET_CONFIG(uint32_t, nack_maxms, Rtc::kNackMaxMS);
  259. GET_CONFIG(uint32_t, nack_maxcount, Rtc::kNackMaxCount);
  260. _rtt = max<int>(10, min<int>(rtt, nack_maxms / nack_maxcount));
  261. }
  262. void NackContext::recordNack(const FCI_NACK &nack) {
  263. auto now = getCurrentMillisecond();
  264. auto i = nack.getPid();
  265. for (auto flag : nack.getBitArray()) {
  266. if (flag) {
  267. auto &ref = _nack_send_status[i];
  268. ref.first_stamp = now;
  269. ref.update_stamp = now;
  270. ref.nack_count = 1;
  271. }
  272. ++i;
  273. }
  274. // 记录太多了,移除一部分早期的记录 [AUTO-TRANSLATED:6f4ea62d]
  275. // There are too many records, remove some of the earlier records.
  276. GET_CONFIG(uint32_t, nack_maxsize, Rtc::kNackMaxSize);
  277. while (_nack_send_status.size() > nack_maxsize) {
  278. _nack_send_status.erase(_nack_send_status.begin());
  279. }
  280. }
  281. uint64_t NackContext::reSendNack() {
  282. set<uint16_t> nack_rtp;
  283. auto now = getCurrentMillisecond();
  284. GET_CONFIG(uint32_t, nack_maxms, Rtc::kNackMaxMS);
  285. GET_CONFIG(uint32_t, nack_maxcount, Rtc::kNackMaxCount);
  286. GET_CONFIG(float, nack_intervalratio, Rtc::kNackIntervalRatio);
  287. for (auto it = _nack_send_status.begin(); it != _nack_send_status.end();) {
  288. if (now - it->second.first_stamp > nack_maxms) {
  289. // 该rtp丢失太久了,不再要求重传 [AUTO-TRANSLATED:a0a1e471]
  290. // This rtp has been lost for too long, no longer require retransmission.
  291. it = _nack_send_status.erase(it);
  292. continue;
  293. }
  294. if (now - it->second.update_stamp < nack_intervalratio * _rtt) {
  295. // 距离上次nack不足2倍的rtt,不用再发送nack [AUTO-TRANSLATED:0e7edf4d]
  296. // The distance from the last nack is less than 2 times the rtt, no need to send nack again.
  297. ++it;
  298. continue;
  299. }
  300. // 此rtp需要请求重传 [AUTO-TRANSLATED:c29d8eb5]
  301. // This rtp needs to request retransmission.
  302. nack_rtp.emplace(it->first);
  303. // 更新nack发送时间戳 [AUTO-TRANSLATED:16ef9fac]
  304. // Update the nack sending timestamp.
  305. it->second.update_stamp = now;
  306. if (++(it->second.nack_count) == nack_maxcount) {
  307. // nack次数太多,移除之 [AUTO-TRANSLATED:1b684a9c]
  308. // Too many nack times, remove it.
  309. it = _nack_send_status.erase(it);
  310. continue;
  311. }
  312. ++it;
  313. }
  314. int pid = -1;
  315. vector<bool> vec;
  316. for (auto it = nack_rtp.begin(); it != nack_rtp.end();) {
  317. if (pid == -1) {
  318. pid = *it;
  319. vec.assign(FCI_NACK::kBitSize, false);
  320. ++it;
  321. continue;
  322. }
  323. auto inc = *it - pid;
  324. if (inc > (ssize_t)FCI_NACK::kBitSize) {
  325. // 新的nack包 [AUTO-TRANSLATED:aec9b818]
  326. // New nack packet.
  327. doNack(FCI_NACK(pid, vec), false);
  328. pid = -1;
  329. continue;
  330. }
  331. // 这个包丢了 [AUTO-TRANSLATED:60f91f2f]
  332. // This packet is lost.
  333. vec[inc - 1] = true;
  334. ++it;
  335. }
  336. if (pid != -1) {
  337. doNack(FCI_NACK(pid, vec), false);
  338. }
  339. // 没有任何包需要重传时返回0,否则返回下次重传间隔(不得低于5ms) [AUTO-TRANSLATED:c326264d]
  340. // Return 0 when there are no packets to retransmit, otherwise return the next retransmission interval (not less than 5ms).
  341. return _nack_send_status.empty() ? 0 : _rtt;
  342. }
  343. } // namespace mediakit