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