nack_module.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_DEPRECATED_NACK_MODULE_H_
  11. #define MODULES_VIDEO_CODING_DEPRECATED_NACK_MODULE_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.h"
  18. #include "modules/include/module_common_types.h"
  19. #include "modules/video_coding/histogram.h"
  20. #include "rtc_base/deprecation.h"
  21. #include "rtc_base/numerics/sequence_number_util.h"
  22. #include "rtc_base/synchronization/mutex.h"
  23. #include "rtc_base/thread_annotations.h"
  24. #include "system_wrappers/include/clock.h"
  25. namespace webrtc {
  26. class DEPRECATED_NackModule : public Module {
  27. public:
  28. DEPRECATED_NackModule(Clock* clock,
  29. NackSender* nack_sender,
  30. KeyFrameRequestSender* keyframe_request_sender);
  31. int OnReceivedPacket(uint16_t seq_num, bool is_keyframe);
  32. int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered);
  33. void ClearUpTo(uint16_t seq_num);
  34. void UpdateRtt(int64_t rtt_ms);
  35. void Clear();
  36. // Module implementation
  37. int64_t TimeUntilNextProcess() override;
  38. void Process() override;
  39. private:
  40. // Which fields to consider when deciding which packet to nack in
  41. // GetNackBatch.
  42. enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime };
  43. // This class holds the sequence number of the packet that is in the nack list
  44. // as well as the meta data about when it should be nacked and how many times
  45. // we have tried to nack this packet.
  46. struct NackInfo {
  47. NackInfo();
  48. NackInfo(uint16_t seq_num,
  49. uint16_t send_at_seq_num,
  50. int64_t created_at_time);
  51. uint16_t seq_num;
  52. uint16_t send_at_seq_num;
  53. int64_t created_at_time;
  54. int64_t sent_at_time;
  55. int retries;
  56. };
  57. struct BackoffSettings {
  58. BackoffSettings(TimeDelta min_retry, TimeDelta max_rtt, double base);
  59. static absl::optional<BackoffSettings> ParseFromFieldTrials();
  60. // Min time between nacks.
  61. const TimeDelta min_retry_interval;
  62. // Upper bound on link-delay considered for exponential backoff.
  63. const TimeDelta max_rtt;
  64. // Base for the exponential backoff.
  65. const double base;
  66. };
  67. void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end)
  68. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  69. // Removes packets from the nack list until the next keyframe. Returns true
  70. // if packets were removed.
  71. bool RemovePacketsUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  72. std::vector<uint16_t> GetNackBatch(NackFilterOptions options)
  73. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  74. // Update the reordering distribution.
  75. void UpdateReorderingStatistics(uint16_t seq_num)
  76. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  77. // Returns how many packets we have to wait in order to receive the packet
  78. // with probability |probabilty| or higher.
  79. int WaitNumberOfPackets(float probability) const
  80. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  81. Mutex mutex_;
  82. Clock* const clock_;
  83. NackSender* const nack_sender_;
  84. KeyFrameRequestSender* const keyframe_request_sender_;
  85. // TODO(philipel): Some of the variables below are consistently used on a
  86. // known thread (e.g. see |initialized_|). Those probably do not need
  87. // synchronized access.
  88. std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_
  89. RTC_GUARDED_BY(mutex_);
  90. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_
  91. RTC_GUARDED_BY(mutex_);
  92. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_
  93. RTC_GUARDED_BY(mutex_);
  94. video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(mutex_);
  95. bool initialized_ RTC_GUARDED_BY(mutex_);
  96. int64_t rtt_ms_ RTC_GUARDED_BY(mutex_);
  97. uint16_t newest_seq_num_ RTC_GUARDED_BY(mutex_);
  98. // Only touched on the process thread.
  99. int64_t next_process_time_ms_;
  100. // Adds a delay before send nack on packet received.
  101. const int64_t send_nack_delay_ms_;
  102. const absl::optional<BackoffSettings> backoff_settings_;
  103. };
  104. using NackModule = RTC_DEPRECATED DEPRECATED_NackModule;
  105. } // namespace webrtc
  106. #endif // MODULES_VIDEO_CODING_DEPRECATED_NACK_MODULE_H_