nack_module2.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef MODULES_VIDEO_CODING_NACK_MODULE2_H_
  11. #define MODULES_VIDEO_CODING_NACK_MODULE2_H_
  12. #include <stdint.h>
  13. #include <map>
  14. #include <set>
  15. #include <vector>
  16. #include "api/units/time_delta.h"
  17. #include "modules/include/module_common_types.h"
  18. #include "modules/video_coding/histogram.h"
  19. #include "rtc_base/numerics/sequence_number_util.h"
  20. #include "rtc_base/synchronization/sequence_checker.h"
  21. #include "rtc_base/task_queue.h"
  22. #include "rtc_base/task_utils/pending_task_safety_flag.h"
  23. #include "rtc_base/task_utils/repeating_task.h"
  24. #include "rtc_base/thread_annotations.h"
  25. #include "system_wrappers/include/clock.h"
  26. namespace webrtc {
  27. // TODO(bugs.webrtc.org/11594): This class no longer implements the Module
  28. // interface and therefore "NackModule" may not be a descriptive name anymore.
  29. // Consider renaming to e.g. NackTracker or NackRequester.
  30. class NackModule2 final {
  31. public:
  32. static constexpr TimeDelta kUpdateInterval = TimeDelta::Millis(20);
  33. NackModule2(TaskQueueBase* current_queue,
  34. Clock* clock,
  35. NackSender* nack_sender,
  36. KeyFrameRequestSender* keyframe_request_sender,
  37. TimeDelta update_interval = kUpdateInterval);
  38. ~NackModule2();
  39. int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
  40. int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered);
  41. void ClearUpTo(uint16_t seq_num);
  42. void UpdateRtt(int64_t rtt_ms);
  43. private:
  44. // Which fields to consider when deciding which packet to nack in
  45. // GetNackBatch.
  46. enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
  47. // This class holds the sequence number of the packet that is in the nack list
  48. // as well as the meta data about when it should be nacked and how many times
  49. // we have tried to nack this packet.
  50. struct NackInfo {
  51. NackInfo();
  52. NackInfo(uint16_t seq_num,
  53. uint16_t send_at_seq_num,
  54. int64_t created_at_time);
  55. uint16_t seq_num;
  56. uint16_t send_at_seq_num;
  57. int64_t created_at_time;
  58. int64_t sent_at_time;
  59. int retries;
  60. };
  61. struct BackoffSettings {
  62. BackoffSettings(TimeDelta min_retry, TimeDelta max_rtt, double base);
  63. static absl::optional<BackoffSettings> ParseFromFieldTrials();
  64. // Min time between nacks.
  65. const TimeDelta min_retry_interval;
  66. // Upper bound on link-delay considered for exponential backoff.
  67. const TimeDelta max_rtt;
  68. // Base for the exponential backoff.
  69. const double base;
  70. };
  71. void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
  72. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
  73. // Removes packets from the nack list until the next keyframe. Returns true
  74. // if packets were removed.
  75. bool RemovePacketsUntilKeyFrame()
  76. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
  77. std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
  78. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
  79. // Update the reordering distribution.
  80. void UpdateReorderingStatistics(uint16_t seq_num)
  81. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
  82. // Returns how many packets we have to wait in order to receive the packet
  83. // with probability |probabilty| or higher.
  84. int WaitNumberOfPackets(float probability) const
  85. RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_);
  86. TaskQueueBase* const worker_thread_;
  87. // Used to regularly call SendNack if needed.
  88. RepeatingTaskHandle repeating_task_ RTC_GUARDED_BY(worker_thread_);
  89. const TimeDelta update_interval_;
  90. Clock* const clock_;
  91. NackSender* const nack_sender_;
  92. KeyFrameRequestSender* const keyframe_request_sender_;
  93. // TODO(philipel): Some of the variables below are consistently used on a
  94. // known thread (e.g. see |initialized_|). Those probably do not need
  95. // synchronized access.
  96. std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
  97. RTC_GUARDED_BY(worker_thread_);
  98. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
  99. RTC_GUARDED_BY(worker_thread_);
  100. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_
  101. RTC_GUARDED_BY(worker_thread_);
  102. video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(worker_thread_);
  103. bool initialized_ RTC_GUARDED_BY(worker_thread_);
  104. int64_t rtt_ms_ RTC_GUARDED_BY(worker_thread_);
  105. uint16_t newest_seq_num_ RTC_GUARDED_BY(worker_thread_);
  106. // Adds a delay before send nack on packet received.
  107. const int64_t send_nack_delay_ms_;
  108. const absl::optional<BackoffSettings> backoff_settings_;
  109. // Used to signal destruction to potentially pending tasks.
  110. ScopedTaskSafety task_safety_;
  111. };
  112. } // namespace webrtc
  113. #endif // MODULES_VIDEO_CODING_NACK_MODULE2_H_