Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 рядки
3.1KB

  1. #include "NackContext.hpp"
  2. namespace SRT {
  3. void NackContext::update(TimePoint now, std::list<PacketQueue::LostPair> &lostlist) {
  4. for (auto item : lostlist) {
  5. mergeItem(now, item);
  6. }
  7. }
  8. void NackContext::getLostList(
  9. TimePoint now, uint32_t rtt, uint32_t rtt_variance, std::list<PacketQueue::LostPair> &lostlist) {
  10. lostlist.clear();
  11. std::list<uint32_t> tmp_list;
  12. for (auto it = _nack_map.begin(); it != _nack_map.end(); ++it) {
  13. if (!it->second._is_nack) {
  14. tmp_list.push_back(it->first);
  15. it->second._ts = now;
  16. it->second._is_nack = true;
  17. } else {
  18. if (DurationCountMicroseconds(now - it->second._ts) > rtt) {
  19. tmp_list.push_back(it->first);
  20. it->second._ts = now;
  21. }
  22. }
  23. }
  24. tmp_list.sort();
  25. if (tmp_list.empty()) {
  26. return;
  27. }
  28. uint32_t min = *tmp_list.begin();
  29. uint32_t max = *tmp_list.rbegin();
  30. if ((max - min) >= (MAX_SEQ >> 1)) {
  31. while ((max - tmp_list.front()) > (MAX_SEQ >> 1)) {
  32. tmp_list.push_back(tmp_list.front());
  33. tmp_list.pop_front();
  34. }
  35. }
  36. PacketQueue::LostPair lost;
  37. bool finish = true;
  38. for (auto cur = tmp_list.begin(); cur != tmp_list.end(); ++cur) {
  39. if (finish) {
  40. lost.first = *cur;
  41. lost.second = genExpectedSeq(*cur + 1);
  42. finish = false;
  43. } else {
  44. if (lost.second == *cur) {
  45. lost.second = genExpectedSeq(*cur + 1);
  46. } else {
  47. finish = true;
  48. lostlist.push_back(lost);
  49. }
  50. }
  51. }
  52. }
  53. void NackContext::drop(uint32_t seq) {
  54. if (_nack_map.empty())
  55. return;
  56. uint32_t min = _nack_map.begin()->first;
  57. uint32_t max = _nack_map.rbegin()->first;
  58. bool is_cycle = false;
  59. if ((max - min) >= (MAX_SEQ >> 1)) {
  60. is_cycle = true;
  61. }
  62. for (auto it = _nack_map.begin(); it != _nack_map.end();) {
  63. if (!is_cycle) {
  64. // 不回环
  65. if (it->first <= seq) {
  66. it = _nack_map.erase(it);
  67. } else {
  68. it++;
  69. }
  70. } else {
  71. if (it->first <= seq) {
  72. if ((seq - it->first) >= (MAX_SEQ >> 1)) {
  73. WarnL << "cycle seq " << seq << " " << it->first;
  74. it++;
  75. } else {
  76. it = _nack_map.erase(it);
  77. }
  78. } else {
  79. if ((it->first - seq) >= (MAX_SEQ >> 1)) {
  80. it = _nack_map.erase(it);
  81. WarnL << "cycle seq " << seq << " " << it->first;
  82. } else {
  83. it++;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. void NackContext::mergeItem(TimePoint now, PacketQueue::LostPair &item) {
  90. for (uint32_t i = item.first; i < item.second; ++i) {
  91. auto it = _nack_map.find(i);
  92. if (it != _nack_map.end()) {
  93. } else {
  94. NackItem tmp;
  95. tmp._is_nack = false;
  96. _nack_map.emplace(i, tmp);
  97. }
  98. }
  99. }
  100. } // namespace SRT