You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "modules/video_coding/nack_module.h"
  11. #include <algorithm>
  12. #include <limits>
  13. #include "modules/utility/include/process_thread.h"
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/logging.h"
  16. #include "system_wrappers/include/field_trial.h"
  17. namespace webrtc {
  18. namespace {
  19. const int kMaxPacketAge = 10000;
  20. const int kMaxNackPackets = 1000;
  21. const int kDefaultRttMs = 100;
  22. const int kMaxNackRetries = 10;
  23. const int kProcessFrequency = 50;
  24. const int kProcessIntervalMs = 1000 / kProcessFrequency;
  25. const int kMaxReorderedPackets = 128;
  26. const int kNumReorderingBuckets = 10;
  27. const int kDefaultSendNackDelayMs = 0;
  28. int64_t GetSendNackDelay() {
  29. int64_t delay_ms = strtol(
  30. webrtc::field_trial::FindFullName("WebRTC-SendNackDelayMs").c_str(),
  31. nullptr, 10);
  32. if (delay_ms > 0 && delay_ms <= 20) {
  33. RTC_LOG(LS_INFO) << "SendNackDelay is set to " << delay_ms;
  34. return delay_ms;
  35. }
  36. return kDefaultSendNackDelayMs;
  37. }
  38. } // namespace
  39. NackModule::NackInfo::NackInfo()
  40. : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {}
  41. NackModule::NackInfo::NackInfo(uint16_t seq_num,
  42. uint16_t send_at_seq_num,
  43. int64_t created_at_time)
  44. : seq_num(seq_num),
  45. send_at_seq_num(send_at_seq_num),
  46. created_at_time(created_at_time),
  47. sent_at_time(-1),
  48. retries(0) {}
  49. NackModule::NackModule(Clock* clock,
  50. NackSender* nack_sender,
  51. KeyFrameRequestSender* keyframe_request_sender)
  52. : clock_(clock),
  53. nack_sender_(nack_sender),
  54. keyframe_request_sender_(keyframe_request_sender),
  55. reordering_histogram_(kNumReorderingBuckets, kMaxReorderedPackets),
  56. initialized_(false),
  57. rtt_ms_(kDefaultRttMs),
  58. newest_seq_num_(0),
  59. next_process_time_ms_(-1),
  60. send_nack_delay_ms_(GetSendNackDelay()) {
  61. RTC_DCHECK(clock_);
  62. RTC_DCHECK(nack_sender_);
  63. RTC_DCHECK(keyframe_request_sender_);
  64. }
  65. int NackModule::OnReceivedPacket(uint16_t seq_num, bool is_keyframe) {
  66. return OnReceivedPacket(seq_num, is_keyframe, false);
  67. }
  68. int NackModule::OnReceivedPacket(uint16_t seq_num,
  69. bool is_keyframe,
  70. bool is_recovered) {
  71. rtc::CritScope lock(&crit_);
  72. // TODO(philipel): When the packet includes information whether it is
  73. // retransmitted or not, use that value instead. For
  74. // now set it to true, which will cause the reordering
  75. // statistics to never be updated.
  76. bool is_retransmitted = true;
  77. if (!initialized_) {
  78. newest_seq_num_ = seq_num;
  79. if (is_keyframe)
  80. keyframe_list_.insert(seq_num);
  81. initialized_ = true;
  82. return 0;
  83. }
  84. // Since the |newest_seq_num_| is a packet we have actually received we know
  85. // that packet has never been Nacked.
  86. if (seq_num == newest_seq_num_)
  87. return 0;
  88. if (AheadOf(newest_seq_num_, seq_num)) {
  89. // An out of order packet has been received.
  90. auto nack_list_it = nack_list_.find(seq_num);
  91. int nacks_sent_for_packet = 0;
  92. if (nack_list_it != nack_list_.end()) {
  93. nacks_sent_for_packet = nack_list_it->second.retries;
  94. nack_list_.erase(nack_list_it);
  95. }
  96. if (!is_retransmitted)
  97. UpdateReorderingStatistics(seq_num);
  98. return nacks_sent_for_packet;
  99. }
  100. // Keep track of new keyframes.
  101. if (is_keyframe)
  102. keyframe_list_.insert(seq_num);
  103. // And remove old ones so we don't accumulate keyframes.
  104. auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
  105. if (it != keyframe_list_.begin())
  106. keyframe_list_.erase(keyframe_list_.begin(), it);
  107. if (is_recovered) {
  108. recovered_list_.insert(seq_num);
  109. // Remove old ones so we don't accumulate recovered packets.
  110. auto it = recovered_list_.lower_bound(seq_num - kMaxPacketAge);
  111. if (it != recovered_list_.begin())
  112. recovered_list_.erase(recovered_list_.begin(), it);
  113. // Do not send nack for packets recovered by FEC or RTX.
  114. return 0;
  115. }
  116. AddPacketsToNack(newest_seq_num_ + 1, seq_num);
  117. newest_seq_num_ = seq_num;
  118. // Are there any nacks that are waiting for this seq_num.
  119. std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
  120. if (!nack_batch.empty()) {
  121. // This batch of NACKs is triggered externally; the initiator can
  122. // batch them with other feedback messages.
  123. nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/true);
  124. }
  125. return 0;
  126. }
  127. void NackModule::ClearUpTo(uint16_t seq_num) {
  128. rtc::CritScope lock(&crit_);
  129. nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num));
  130. keyframe_list_.erase(keyframe_list_.begin(),
  131. keyframe_list_.lower_bound(seq_num));
  132. recovered_list_.erase(recovered_list_.begin(),
  133. recovered_list_.lower_bound(seq_num));
  134. }
  135. void NackModule::UpdateRtt(int64_t rtt_ms) {
  136. rtc::CritScope lock(&crit_);
  137. rtt_ms_ = rtt_ms;
  138. }
  139. void NackModule::Clear() {
  140. rtc::CritScope lock(&crit_);
  141. nack_list_.clear();
  142. keyframe_list_.clear();
  143. recovered_list_.clear();
  144. }
  145. int64_t NackModule::TimeUntilNextProcess() {
  146. return std::max<int64_t>(next_process_time_ms_ - clock_->TimeInMilliseconds(),
  147. 0);
  148. }
  149. void NackModule::Process() {
  150. if (nack_sender_) {
  151. std::vector<uint16_t> nack_batch;
  152. {
  153. rtc::CritScope lock(&crit_);
  154. nack_batch = GetNackBatch(kTimeOnly);
  155. }
  156. if (!nack_batch.empty()) {
  157. // This batch of NACKs is triggered externally; there is no external
  158. // initiator who can batch them with other feedback messages.
  159. nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/false);
  160. }
  161. }
  162. // Update the next_process_time_ms_ in intervals to achieve
  163. // the targeted frequency over time. Also add multiple intervals
  164. // in case of a skip in time as to not make uneccessary
  165. // calls to Process in order to catch up.
  166. int64_t now_ms = clock_->TimeInMilliseconds();
  167. if (next_process_time_ms_ == -1) {
  168. next_process_time_ms_ = now_ms + kProcessIntervalMs;
  169. } else {
  170. next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs +
  171. (now_ms - next_process_time_ms_) /
  172. kProcessIntervalMs * kProcessIntervalMs;
  173. }
  174. }
  175. bool NackModule::RemovePacketsUntilKeyFrame() {
  176. while (!keyframe_list_.empty()) {
  177. auto it = nack_list_.lower_bound(*keyframe_list_.begin());
  178. if (it != nack_list_.begin()) {
  179. // We have found a keyframe that actually is newer than at least one
  180. // packet in the nack list.
  181. nack_list_.erase(nack_list_.begin(), it);
  182. return true;
  183. }
  184. // If this keyframe is so old it does not remove any packets from the list,
  185. // remove it from the list of keyframes and try the next keyframe.
  186. keyframe_list_.erase(keyframe_list_.begin());
  187. }
  188. return false;
  189. }
  190. void NackModule::AddPacketsToNack(uint16_t seq_num_start,
  191. uint16_t seq_num_end) {
  192. // Remove old packets.
  193. auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
  194. nack_list_.erase(nack_list_.begin(), it);
  195. // If the nack list is too large, remove packets from the nack list until
  196. // the latest first packet of a keyframe. If the list is still too large,
  197. // clear it and request a keyframe.
  198. uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
  199. if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
  200. while (RemovePacketsUntilKeyFrame() &&
  201. nack_list_.size() + num_new_nacks > kMaxNackPackets) {
  202. }
  203. if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
  204. nack_list_.clear();
  205. RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
  206. " list and requesting keyframe.";
  207. keyframe_request_sender_->RequestKeyFrame();
  208. return;
  209. }
  210. }
  211. for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
  212. // Do not send nack for packets that are already recovered by FEC or RTX
  213. if (recovered_list_.find(seq_num) != recovered_list_.end())
  214. continue;
  215. NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5),
  216. clock_->TimeInMilliseconds());
  217. RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
  218. nack_list_[seq_num] = nack_info;
  219. }
  220. }
  221. std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) {
  222. bool consider_seq_num = options != kTimeOnly;
  223. bool consider_timestamp = options != kSeqNumOnly;
  224. int64_t now_ms = clock_->TimeInMilliseconds();
  225. std::vector<uint16_t> nack_batch;
  226. auto it = nack_list_.begin();
  227. while (it != nack_list_.end()) {
  228. bool delay_timed_out =
  229. now_ms - it->second.created_at_time >= send_nack_delay_ms_;
  230. bool nack_on_rtt_passed = now_ms - it->second.sent_at_time >= rtt_ms_;
  231. bool nack_on_seq_num_passed =
  232. it->second.sent_at_time == -1 &&
  233. AheadOrAt(newest_seq_num_, it->second.send_at_seq_num);
  234. if (delay_timed_out && ((consider_seq_num && nack_on_seq_num_passed) ||
  235. (consider_timestamp && nack_on_rtt_passed))) {
  236. nack_batch.emplace_back(it->second.seq_num);
  237. ++it->second.retries;
  238. it->second.sent_at_time = now_ms;
  239. if (it->second.retries >= kMaxNackRetries) {
  240. RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
  241. << " removed from NACK list due to max retries.";
  242. it = nack_list_.erase(it);
  243. } else {
  244. ++it;
  245. }
  246. continue;
  247. }
  248. ++it;
  249. }
  250. return nack_batch;
  251. }
  252. void NackModule::UpdateReorderingStatistics(uint16_t seq_num) {
  253. RTC_DCHECK(AheadOf(newest_seq_num_, seq_num));
  254. uint16_t diff = ReverseDiff(newest_seq_num_, seq_num);
  255. reordering_histogram_.Add(diff);
  256. }
  257. int NackModule::WaitNumberOfPackets(float probability) const {
  258. if (reordering_histogram_.NumValues() == 0)
  259. return 0;
  260. return reordering_histogram_.InverseCdf(probability);
  261. }
  262. } // namespace webrtc