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