Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 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 | |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_NACK_REQUESTER_H_ |
| 12 | #define MODULES_VIDEO_CODING_NACK_REQUESTER_H_ |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | #include <map> |
| 17 | #include <set> |
| 18 | #include <vector> |
| 19 | |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 21 | #include "api/units/time_delta.h" |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame^] | 22 | #include "api/webrtc_key_value_config.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 23 | #include "modules/include/module_common_types.h" |
| 24 | #include "modules/video_coding/histogram.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 25 | #include "rtc_base/numerics/sequence_number_util.h" |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 26 | #include "rtc_base/task_queue.h" |
| 27 | #include "rtc_base/task_utils/pending_task_safety_flag.h" |
| 28 | #include "rtc_base/task_utils/repeating_task.h" |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 29 | #include "rtc_base/thread_annotations.h" |
| 30 | #include "system_wrappers/include/clock.h" |
| 31 | |
| 32 | namespace webrtc { |
| 33 | |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 34 | class NackRequesterBase { |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 35 | public: |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 36 | virtual ~NackRequesterBase() = default; |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 37 | virtual void ProcessNacks() = 0; |
| 38 | }; |
| 39 | |
| 40 | class NackPeriodicProcessor { |
| 41 | public: |
| 42 | static constexpr TimeDelta kUpdateInterval = TimeDelta::Millis(20); |
| 43 | explicit NackPeriodicProcessor(TimeDelta update_interval = kUpdateInterval); |
| 44 | ~NackPeriodicProcessor(); |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 45 | void RegisterNackModule(NackRequesterBase* module); |
| 46 | void UnregisterNackModule(NackRequesterBase* module); |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | void ProcessNackModules() RTC_RUN_ON(sequence_); |
| 50 | |
| 51 | const TimeDelta update_interval_; |
| 52 | RepeatingTaskHandle repeating_task_ RTC_GUARDED_BY(sequence_); |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 53 | std::vector<NackRequesterBase*> modules_ RTC_GUARDED_BY(sequence_); |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 54 | RTC_NO_UNIQUE_ADDRESS SequenceChecker sequence_; |
| 55 | }; |
| 56 | |
| 57 | class ScopedNackPeriodicProcessorRegistration { |
| 58 | public: |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 59 | ScopedNackPeriodicProcessorRegistration(NackRequesterBase* module, |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 60 | NackPeriodicProcessor* processor); |
| 61 | ~ScopedNackPeriodicProcessorRegistration(); |
| 62 | |
| 63 | private: |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 64 | NackRequesterBase* const module_; |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 65 | NackPeriodicProcessor* const processor_; |
| 66 | }; |
| 67 | |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 68 | class NackRequester final : public NackRequesterBase { |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 69 | public: |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 70 | NackRequester(TaskQueueBase* current_queue, |
| 71 | NackPeriodicProcessor* periodic_processor, |
| 72 | Clock* clock, |
| 73 | NackSender* nack_sender, |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame^] | 74 | KeyFrameRequestSender* keyframe_request_sender, |
| 75 | const WebRtcKeyValueConfig& field_trials); |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 76 | ~NackRequester(); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 77 | |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 78 | void ProcessNacks() override; |
| 79 | |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 80 | int OnReceivedPacket(uint16_t seq_num, bool is_keyframe); |
| 81 | int OnReceivedPacket(uint16_t seq_num, bool is_keyframe, bool is_recovered); |
| 82 | |
| 83 | void ClearUpTo(uint16_t seq_num); |
| 84 | void UpdateRtt(int64_t rtt_ms); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 85 | |
| 86 | private: |
| 87 | // Which fields to consider when deciding which packet to nack in |
| 88 | // GetNackBatch. |
| 89 | enum NackFilterOptions { kSeqNumOnly, kTimeOnly, kSeqNumAndTime }; |
| 90 | |
| 91 | // This class holds the sequence number of the packet that is in the nack list |
| 92 | // as well as the meta data about when it should be nacked and how many times |
| 93 | // we have tried to nack this packet. |
| 94 | struct NackInfo { |
| 95 | NackInfo(); |
| 96 | NackInfo(uint16_t seq_num, |
| 97 | uint16_t send_at_seq_num, |
| 98 | int64_t created_at_time); |
| 99 | |
| 100 | uint16_t seq_num; |
| 101 | uint16_t send_at_seq_num; |
| 102 | int64_t created_at_time; |
| 103 | int64_t sent_at_time; |
| 104 | int retries; |
| 105 | }; |
| 106 | |
| 107 | struct BackoffSettings { |
| 108 | BackoffSettings(TimeDelta min_retry, TimeDelta max_rtt, double base); |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame^] | 109 | static absl::optional<BackoffSettings> ParseFromFieldTrials( |
| 110 | const WebRtcKeyValueConfig& field_trials); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 111 | |
| 112 | // Min time between nacks. |
| 113 | const TimeDelta min_retry_interval; |
| 114 | // Upper bound on link-delay considered for exponential backoff. |
| 115 | const TimeDelta max_rtt; |
| 116 | // Base for the exponential backoff. |
| 117 | const double base; |
| 118 | }; |
| 119 | |
| 120 | void AddPacketsToNack(uint16_t seq_num_start, uint16_t seq_num_end) |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 121 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 122 | |
| 123 | // Removes packets from the nack list until the next keyframe. Returns true |
| 124 | // if packets were removed. |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 125 | bool RemovePacketsUntilKeyFrame() |
| 126 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 127 | std::vector<uint16_t> GetNackBatch(NackFilterOptions options) |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 128 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 129 | |
| 130 | // Update the reordering distribution. |
| 131 | void UpdateReorderingStatistics(uint16_t seq_num) |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 132 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 133 | |
| 134 | // Returns how many packets we have to wait in order to receive the packet |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 135 | // with probability `probabilty` or higher. |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 136 | int WaitNumberOfPackets(float probability) const |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 137 | RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 138 | |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 139 | TaskQueueBase* const worker_thread_; |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 140 | Clock* const clock_; |
| 141 | NackSender* const nack_sender_; |
| 142 | KeyFrameRequestSender* const keyframe_request_sender_; |
| 143 | |
| 144 | // TODO(philipel): Some of the variables below are consistently used on a |
Artem Titov | dcd7fc7 | 2021-08-09 13:02:57 +0200 | [diff] [blame] | 145 | // known thread (e.g. see `initialized_`). Those probably do not need |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 146 | // synchronized access. |
| 147 | std::map<uint16_t, NackInfo, DescendingSeqNumComp<uint16_t>> nack_list_ |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 148 | RTC_GUARDED_BY(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 149 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> keyframe_list_ |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 150 | RTC_GUARDED_BY(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 151 | std::set<uint16_t, DescendingSeqNumComp<uint16_t>> recovered_list_ |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 152 | RTC_GUARDED_BY(worker_thread_); |
| 153 | video_coding::Histogram reordering_histogram_ RTC_GUARDED_BY(worker_thread_); |
| 154 | bool initialized_ RTC_GUARDED_BY(worker_thread_); |
| 155 | int64_t rtt_ms_ RTC_GUARDED_BY(worker_thread_); |
| 156 | uint16_t newest_seq_num_ RTC_GUARDED_BY(worker_thread_); |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 157 | |
| 158 | // Adds a delay before send nack on packet received. |
| 159 | const int64_t send_nack_delay_ms_; |
| 160 | |
| 161 | const absl::optional<BackoffSettings> backoff_settings_; |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 162 | |
Markus Handell | 0e62f7a | 2021-07-20 13:32:02 +0200 | [diff] [blame] | 163 | ScopedNackPeriodicProcessorRegistration processor_registration_; |
| 164 | |
Tommi | 63673fe | 2020-05-27 12:55:38 +0200 | [diff] [blame] | 165 | // Used to signal destruction to potentially pending tasks. |
| 166 | ScopedTaskSafety task_safety_; |
Tommi | d3807da | 2020-05-22 17:36:36 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | } // namespace webrtc |
| 170 | |
Markus Handell | 06a2bf0 | 2021-07-22 15:09:39 +0200 | [diff] [blame] | 171 | #endif // MODULES_VIDEO_CODING_NACK_REQUESTER_H_ |