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