OLD | NEW |
(Empty) | |
| 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 #include <algorithm> |
| 12 #include <limits> |
| 13 |
| 14 #include "webrtc/modules/video_coding/nack_module.h" |
| 15 |
| 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/mod_ops.h" |
| 18 #include "webrtc/modules/utility/include/process_thread.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 namespace { |
| 23 const int kMaxPacketAge = 10000; |
| 24 const int kMaxNackPackets = 1000; |
| 25 const int kDefaultRttMs = 100; |
| 26 const int kMaxNackRetries = 10; |
| 27 const int kProcessFrequency = 50; |
| 28 const int kProcessIntervalMs = 1000 / kProcessFrequency; |
| 29 const int kMaxReorderedPackets = 128; |
| 30 const int kNumReorderingBuckets = 10; |
| 31 } // namespace |
| 32 |
| 33 NackModule::NackInfo::NackInfo() |
| 34 : seq_num(0), send_at_seq_num(0), sent_at_time(-1), retries(0) {} |
| 35 |
| 36 NackModule::NackInfo::NackInfo(uint16_t seq_num, uint16_t send_at_seq_num) |
| 37 : seq_num(seq_num), |
| 38 send_at_seq_num(send_at_seq_num), |
| 39 sent_at_time(-1), |
| 40 retries(0) {} |
| 41 |
| 42 NackModule::NackModule(Clock* clock, |
| 43 NackSender* nack_sender, |
| 44 KeyFrameRequestSender* keyframe_request_sender) |
| 45 : clock_(clock), |
| 46 nack_sender_(nack_sender), |
| 47 keyframe_request_sender_(keyframe_request_sender), |
| 48 reordering_histogram_(kNumReorderingBuckets, kMaxReorderedPackets), |
| 49 running_(true), |
| 50 initialized_(false), |
| 51 rtt_ms_(kDefaultRttMs), |
| 52 last_seq_num_(0), |
| 53 next_process_time_ms_(-1) { |
| 54 RTC_DCHECK(clock_); |
| 55 RTC_DCHECK(nack_sender_); |
| 56 RTC_DCHECK(keyframe_request_sender_); |
| 57 } |
| 58 |
| 59 void NackModule::OnReceivedPacket(const VCMPacket& packet) { |
| 60 rtc::CritScope lock(&crit_); |
| 61 if (!running_) |
| 62 return; |
| 63 uint16_t seq_num = packet.seqNum; |
| 64 // TODO(philipel): When the packet includes information whether it is |
| 65 // retransmitted or not, use that value instead. For |
| 66 // now set it to true, which will cause the reordering |
| 67 // statistics to never be updated. |
| 68 bool is_retransmitted = true; |
| 69 bool is_keyframe = packet.isFirstPacket && packet.frameType == kVideoFrameKey; |
| 70 |
| 71 if (!initialized_) { |
| 72 last_seq_num_ = seq_num; |
| 73 if (is_keyframe) |
| 74 keyframe_list_.insert(seq_num); |
| 75 initialized_ = true; |
| 76 return; |
| 77 } |
| 78 |
| 79 if (seq_num == last_seq_num_) |
| 80 return; |
| 81 |
| 82 if (AheadOf(last_seq_num_, seq_num)) { |
| 83 // An out of order packet has been received. |
| 84 nack_list_.erase(seq_num); |
| 85 if (!is_retransmitted) |
| 86 UpdateReorderingStatistics(seq_num); |
| 87 return; |
| 88 } else { |
| 89 AddPacketsToNack(last_seq_num_ + 1, seq_num); |
| 90 last_seq_num_ = seq_num; |
| 91 |
| 92 // Keep track of new keyframes. |
| 93 if (is_keyframe) |
| 94 keyframe_list_.insert(seq_num); |
| 95 |
| 96 // And remove old ones so we don't accumulate keyframes. |
| 97 auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge); |
| 98 if (it != keyframe_list_.begin()) |
| 99 keyframe_list_.erase(keyframe_list_.begin(), it); |
| 100 |
| 101 // Are there any nacks that are waiting for this seq_num. |
| 102 std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly); |
| 103 if (!nack_batch.empty()) |
| 104 nack_sender_->SendNack(nack_batch); |
| 105 } |
| 106 } |
| 107 |
| 108 void NackModule::ClearUpTo(uint16_t seq_num) { |
| 109 rtc::CritScope lock(&crit_); |
| 110 nack_list_.erase(nack_list_.begin(), nack_list_.lower_bound(seq_num)); |
| 111 keyframe_list_.erase(keyframe_list_.begin(), |
| 112 keyframe_list_.lower_bound(seq_num)); |
| 113 } |
| 114 |
| 115 void NackModule::UpdateRtt(int64_t rtt_ms) { |
| 116 rtc::CritScope lock(&crit_); |
| 117 rtt_ms_ = rtt_ms; |
| 118 } |
| 119 |
| 120 void NackModule::Stop() { |
| 121 rtc::CritScope lock(&crit_); |
| 122 running_ = false; |
| 123 } |
| 124 |
| 125 int64_t NackModule::TimeUntilNextProcess() { |
| 126 rtc::CritScope lock(&crit_); |
| 127 return std::max(next_process_time_ms_ - clock_->TimeInMilliseconds(), 0l); |
| 128 } |
| 129 |
| 130 void NackModule::Process() { |
| 131 rtc::CritScope lock(&crit_); |
| 132 if (!running_) |
| 133 return; |
| 134 |
| 135 // Update the next_process_time_ms_ in intervals to achieve |
| 136 // the targeted frequency over time. Also add multiple intervals |
| 137 // in case of a skip in time as to not make uneccessary |
| 138 // calls to Process in order to catch up. |
| 139 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 140 if (next_process_time_ms_ == -1) { |
| 141 next_process_time_ms_ = now_ms + kProcessIntervalMs; |
| 142 } else { |
| 143 next_process_time_ms_ = next_process_time_ms_ + kProcessIntervalMs + |
| 144 (now_ms - next_process_time_ms_) / |
| 145 kProcessIntervalMs * kProcessIntervalMs; |
| 146 } |
| 147 |
| 148 std::vector<uint16_t> nack_batch = GetNackBatch(kTimeOnly); |
| 149 if (!nack_batch.empty() && nack_sender_ != nullptr) |
| 150 nack_sender_->SendNack(nack_batch); |
| 151 } |
| 152 |
| 153 bool NackModule::RemovePacketsUntilKeyFrame() { |
| 154 while (!keyframe_list_.empty()) { |
| 155 auto it = nack_list_.lower_bound(*keyframe_list_.begin()); |
| 156 |
| 157 if (it != nack_list_.begin()) { |
| 158 // We have found a keyframe that actually is newer than at least one |
| 159 // packet in the nack list. |
| 160 RTC_DCHECK(it != nack_list_.end()); |
| 161 nack_list_.erase(nack_list_.begin(), it); |
| 162 return true; |
| 163 } |
| 164 |
| 165 // If this keyframe is so old it does not remove any packets from the list, |
| 166 // remove it from the list of keyframes and try the next keyframe. |
| 167 keyframe_list_.erase(keyframe_list_.begin()); |
| 168 } |
| 169 return false; |
| 170 } |
| 171 |
| 172 void NackModule::AddPacketsToNack(uint16_t seq_num_start, |
| 173 uint16_t seq_num_end) { |
| 174 // Remove old packets. |
| 175 auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge); |
| 176 nack_list_.erase(nack_list_.begin(), it); |
| 177 |
| 178 // If the nack list is too large, remove packets from the nack list until |
| 179 // the latest first packet of a keyframe. If the list is still too large, |
| 180 // clear it and request a keyframe. |
| 181 uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end); |
| 182 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 183 while (RemovePacketsUntilKeyFrame() && |
| 184 nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 185 } |
| 186 |
| 187 if (nack_list_.size() + num_new_nacks > kMaxNackPackets) { |
| 188 nack_list_.clear(); |
| 189 keyframe_request_sender_->RequestKeyFrame(); |
| 190 return; |
| 191 } |
| 192 } |
| 193 |
| 194 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) { |
| 195 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5)); |
| 196 RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end()); |
| 197 nack_list_[seq_num] = nack_info; |
| 198 } |
| 199 } |
| 200 |
| 201 std::vector<uint16_t> NackModule::GetNackBatch(NackFilterOptions options) { |
| 202 bool consider_seq_num = options != kTimeOnly; |
| 203 bool consider_timestamp = options != kSeqNumOnly; |
| 204 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 205 std::vector<uint16_t> nack_batch; |
| 206 auto it = nack_list_.begin(); |
| 207 while (it != nack_list_.end()) { |
| 208 if (consider_seq_num && it->second.sent_at_time == -1 && |
| 209 AheadOrAt(last_seq_num_, it->second.send_at_seq_num)) { |
| 210 nack_batch.emplace_back(it->second.seq_num); |
| 211 ++it->second.retries; |
| 212 it->second.sent_at_time = now_ms; |
| 213 if (it->second.retries >= kMaxNackRetries) { |
| 214 it = nack_list_.erase(it); |
| 215 } else { |
| 216 ++it; |
| 217 } |
| 218 continue; |
| 219 } |
| 220 |
| 221 if (consider_timestamp && it->second.sent_at_time + rtt_ms_ <= now_ms) { |
| 222 nack_batch.emplace_back(it->second.seq_num); |
| 223 ++it->second.retries; |
| 224 it->second.sent_at_time = now_ms; |
| 225 if (it->second.retries >= kMaxNackRetries) { |
| 226 it = nack_list_.erase(it); |
| 227 } else { |
| 228 ++it; |
| 229 } |
| 230 continue; |
| 231 } |
| 232 ++it; |
| 233 } |
| 234 return nack_batch; |
| 235 } |
| 236 |
| 237 void NackModule::UpdateReorderingStatistics(uint16_t seq_num) { |
| 238 RTC_DCHECK(AheadOf(last_seq_num_, seq_num)); |
| 239 uint16_t diff = ReverseDiff(last_seq_num_, seq_num); |
| 240 reordering_histogram_.Add(diff); |
| 241 } |
| 242 |
| 243 int NackModule::WaitNumberOfPackets(float probability) const { |
| 244 if (reordering_histogram_.NumValues() == 0) |
| 245 return 0; |
| 246 return reordering_histogram_.InverseCdf(probability); |
| 247 } |
| 248 |
| 249 } // namespace webrtc |
OLD | NEW |