Chromium Code Reviews| 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" | |
|
stefan-webrtc
2016/02/23 12:29:00
I tend to prefer short names. Should we call this
philipel
2016/02/25 15:09:31
Acknowledged.
| |
| 17 #include "webrtc/modules/utility/include/process_thread.h" | |
| 18 | |
| 19 namespace webrtc { | |
| 20 | |
| 21 // =========================================================== | |
| 22 // || || | |
| 23 // || NackModule || | |
| 24 // || || | |
| 25 // =========================================================== | |
|
stefan-webrtc
2016/02/23 12:29:00
I prefer not having comments like this.
philipel
2016/02/25 15:09:31
Done.
| |
| 26 | |
| 27 // Needs to be defined in the .cc file since these values has | |
| 28 // to be referenced. Actual values can be found in .h file. | |
| 29 const uint16_t NackModule::kMaxReorderedPackets; | |
| 30 const uint16_t NackModule::kNumReorderingBuckets; | |
| 31 const uint16_t NackModule::kMaxNackPackets; | |
| 32 | |
| 33 NackModule::NackModule(CriticalSectionWrapper* critical_section, | |
| 34 Clock* clock, | |
| 35 VCMNackSender* nack_sender, | |
| 36 VCMKeyFrameRequestSender* keyframe_request_sender) | |
| 37 : critical_section_(critical_section), | |
| 38 clock_(clock), | |
| 39 nack_sender_(nack_sender), | |
| 40 keyframe_request_sender_(keyframe_request_sender), | |
| 41 running_(true), | |
| 42 last_seq_num_(0), | |
| 43 initialized_(false), | |
| 44 rtt_ms_(kDefaultRttMs), | |
| 45 last_process_timestamp_ms_(0), | |
|
stefan-webrtc
2016/02/23 12:29:01
Initialize to -1 instead. 0 is a bad initial value
philipel
2016/02/25 15:09:32
Done.
| |
| 46 last_keyframe_seq_num_(0), | |
| 47 reordering_count_(0) { | |
| 48 memset(reordering_occurences_, -1, sizeof(reordering_occurences_)); | |
| 49 memset(reordering_buckets_, 0, sizeof(reordering_buckets_)); | |
| 50 RTC_DCHECK(critical_section_ != nullptr); | |
| 51 RTC_DCHECK(clock_ != nullptr); | |
| 52 RTC_DCHECK(nack_sender_ != nullptr); | |
| 53 RTC_DCHECK(keyframe_request_sender_ != nullptr); | |
|
stefan-webrtc
2016/02/23 12:29:01
You don't need != nullptr.
Just do RTC_DCHECK(key
philipel
2016/02/25 15:09:31
Done.
| |
| 54 } | |
| 55 | |
| 56 NackModule::~NackModule() { | |
| 57 CriticalSectionScoped cs(critical_section_); | |
|
stefan-webrtc
2016/02/23 12:29:01
Seems odd that we would have to take the critical
philipel
2016/02/25 15:09:31
If some thread is inside another function then the
| |
| 58 running_ = false; | |
| 59 } | |
| 60 | |
| 61 void NackModule::Notify(const VCMPacket& packet) { | |
| 62 CriticalSectionScoped cs(critical_section_); | |
|
stefan-webrtc
2016/02/23 12:29:01
Name this lock instead of cs, and use rtc::CritSco
philipel
2016/02/25 15:09:31
Done.
| |
| 63 if (!running_) | |
| 64 return; | |
| 65 uint16_t seq_num(packet.seqNum); | |
|
stefan-webrtc
2016/02/23 12:29:00
uint16_t seq_num = packet.seqNum;
or use packet.s
philipel
2016/02/25 15:09:31
If we change packet class then we only have to cha
| |
| 66 // TODO(philipel): When the packet includes information whether it is | |
| 67 // retransmitted or not, use that value instead. For | |
| 68 // now set it to true, which will cause the reordering | |
| 69 // statistics to never be updated. | |
| 70 bool is_retransmitted = true; | |
| 71 bool is_keyframe = packet.isFirstPacket && packet.frameType == kVideoFrameKey; | |
| 72 | |
| 73 if (!initialized_) { | |
| 74 last_seq_num_ = seq_num; | |
| 75 last_keyframe_seq_num_ = seq_num; | |
| 76 initialized_ = true; | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 if (seq_num == last_seq_num_) | |
| 81 return; | |
| 82 | |
| 83 if (AheadOf(seq_num, last_seq_num_)) { | |
| 84 AddPacketsToNack(last_seq_num_ + 1, seq_num); | |
| 85 last_seq_num_ = seq_num; | |
| 86 if (is_keyframe) | |
| 87 last_keyframe_seq_num_ = seq_num; | |
| 88 std::vector<uint16_t> nack_batch = GetNackBatch(true, false); | |
|
stefan-webrtc
2016/02/23 12:29:01
I think this call is a bit hard to read since I ne
philipel
2016/02/25 15:09:31
Done.
| |
| 89 if (!nack_batch.empty() && nack_sender_ != nullptr) | |
|
stefan-webrtc
2016/02/23 12:29:01
Can we make sure nack_sender_ is always set so tha
philipel
2016/02/25 15:09:31
Done.
| |
| 90 nack_sender_->SendNack(nack_batch.data(), nack_batch.size()); | |
| 91 } else { | |
| 92 // An out of order packet has been received. | |
| 93 RemovePacketFromNack(seq_num); | |
| 94 if (!is_retransmitted) | |
| 95 UpdateReorderingStatistics(seq_num); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void NackModule::UpdateRtt(int64_t rtt_ms) { | |
| 100 CriticalSectionScoped cs(critical_section_); | |
| 101 rtt_ms_ = rtt_ms; | |
| 102 } | |
| 103 | |
| 104 void NackModule::Stop() { | |
| 105 CriticalSectionScoped cs(critical_section_); | |
| 106 running_ = false; | |
| 107 } | |
| 108 | |
| 109 int64_t NackModule::TimeUntilNextProcess() { | |
| 110 CriticalSectionScoped cs(critical_section_); | |
| 111 return last_process_timestamp_ms_ + kProcessIntervalMs - | |
| 112 clock_->TimeInMilliseconds(); | |
|
stefan-webrtc
2016/02/23 12:29:01
Maybe use max(0, expr) to ensure it doesn't get ne
philipel
2016/02/25 15:09:31
Done.
| |
| 113 } | |
| 114 | |
| 115 int32_t NackModule::Process() { | |
| 116 CriticalSectionScoped cs(critical_section_); | |
| 117 if (!running_) | |
| 118 return 0; | |
| 119 | |
| 120 // Update the last_process_timestamp_ms_ in intervals to achieve | |
| 121 // the targeted frequency over time. Also add multiple intervals | |
| 122 // in case of a skip in time as to not make uneccessary | |
| 123 // calls to Process in order to catch up. | |
| 124 int64_t now_ms = clock_->TimeInMilliseconds(); | |
| 125 if (last_process_timestamp_ms_ == 0) | |
|
stefan-webrtc
2016/02/23 12:29:00
s/timestamp/time
Use brackets {} for multi-line i
philipel
2016/02/25 15:09:31
Done.
| |
| 126 last_process_timestamp_ms_ = now_ms + kProcessIntervalMs; | |
| 127 else | |
| 128 last_process_timestamp_ms_ = last_process_timestamp_ms_ + | |
| 129 kProcessIntervalMs + | |
| 130 (now_ms - 1 - last_process_timestamp_ms_) / | |
| 131 kProcessIntervalMs * kProcessIntervalMs; | |
|
stefan-webrtc
2016/02/23 12:29:00
All of this looks really strange to me. Why not si
philipel
2016/02/25 15:09:32
The idea was that to make sure we send at a certai
stefan-webrtc
2016/02/26 09:26:54
Ok, but isn't it weird to not set last_process_tim
philipel
2016/02/26 15:12:06
The name last_process_timestamp_ms_ is not perfect
| |
| 132 | |
| 133 std::vector<uint16_t> nack_batch = GetNackBatch(false, true); | |
| 134 if (!nack_batch.empty() && nack_sender_ != nullptr) | |
| 135 nack_sender_->SendNack(nack_batch.data(), nack_batch.size()); | |
| 136 | |
| 137 return 0; | |
| 138 } | |
| 139 | |
| 140 void NackModule::AddPacketsToNack(uint16_t seq_num_start, | |
| 141 uint16_t seq_num_end) { | |
| 142 // If the nack list is too large, remove packets from the nack list until | |
| 143 // the latest first packet of a keyframe. If the list is still too large, | |
| 144 // clear it and request a keyframe. | |
| 145 uint16_t num_new_nacks = PositiveDiff(seq_num_start, seq_num_end); | |
|
stefan-webrtc
2016/02/23 12:29:00
Are these sequence numbers guaranteed not to alrea
philipel
2016/02/25 15:09:30
This function is called from Notify (now OnReceive
stefan-webrtc
2016/02/26 09:26:54
Would it make sense to dcheck on that somewhere in
philipel
2016/02/26 15:12:06
It would, will add it for the next patch.
| |
| 146 if (nack_list_.size() + num_new_nacks >= kMaxNackPackets) { | |
| 147 NackList::iterator nack_it = nack_list_.lower_bound(last_keyframe_seq_num_); | |
|
stefan-webrtc
2016/02/23 12:29:01
What if last_keyframe_seq_num_ is 0xffff and the l
stefan-webrtc
2016/02/23 12:29:01
auto instead of NackList::iterator, and just call
philipel
2016/02/25 15:09:31
Changed the implementation completely, now we clea
| |
| 148 if (nack_it != nack_list_.end()) | |
| 149 nack_list_.erase(nack_list_.begin(), nack_it); | |
| 150 | |
| 151 if (nack_list_.size() + num_new_nacks >= kMaxNackPackets) { | |
|
stefan-webrtc
2016/02/23 12:29:00
Are the new sequence numbers guaranteed to be late
| |
| 152 nack_list_.clear(); | |
| 153 keyframe_request_sender_->RequestKeyFrame(); | |
|
stefan-webrtc
2016/02/23 12:29:01
What do you think of instead returning kRequestKey
philipel
2016/02/25 15:09:31
Don't we need to do the callback anyway, only in t
stefan-webrtc
2016/02/26 09:26:54
Ok! At some point we will have to call back, yes.
| |
| 154 return; | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) { | |
| 159 NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5)); | |
| 160 nack_list_[seq_num] = nack_info; | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void NackModule::RemovePacketFromNack(uint16_t seq_num) { | |
| 165 nack_list_.erase(seq_num); | |
| 166 } | |
| 167 | |
| 168 std::vector<uint16_t> NackModule::GetNackBatch(bool consider_seq_num, | |
| 169 bool consider_timestamp) { | |
| 170 int64_t now_ms = clock_->TimeInMilliseconds(); | |
| 171 std::vector<uint16_t> nack_batch; | |
| 172 NackList::iterator nack_it = nack_list_.begin(); | |
| 173 while (nack_it != nack_list_.end()) { | |
| 174 if (consider_seq_num && nack_it->second.send_at_timestamp == 0 && | |
| 175 AheadOrAt(last_seq_num_, nack_it->second.send_at_seq_num)) { | |
| 176 nack_batch.emplace_back(nack_it->second.seq_num); | |
| 177 nack_it->second.retries++; | |
|
stefan-webrtc
2016/02/23 12:29:01
++...retries;
philipel
2016/02/25 15:09:31
Done.
| |
| 178 nack_it->second.send_at_timestamp = now_ms + rtt_ms_; | |
| 179 if (nack_it->second.retries >= kMaxNackRetries) | |
|
stefan-webrtc
2016/02/23 12:29:01
Brackets
| |
| 180 nack_it = nack_list_.erase(nack_it); | |
|
stefan-webrtc
2016/02/23 12:29:00
If we decide to stop trying, should we then make s
philipel
2016/02/25 15:09:31
I think a ClearUpToSequenceNumber(uint16_t seq_num
stefan-webrtc
2016/02/26 09:26:54
So in that case we will be waiting for the jitter
philipel
2016/02/26 15:12:06
The idea with this nack module is that it should n
| |
| 181 else | |
| 182 ++nack_it; | |
| 183 continue; | |
| 184 } | |
| 185 | |
| 186 if (consider_timestamp && nack_it->second.send_at_timestamp > 0 && | |
| 187 nack_it->second.send_at_timestamp <= now_ms) { | |
| 188 nack_batch.emplace_back(nack_it->second.seq_num); | |
| 189 nack_it->second.retries++; | |
| 190 nack_it->second.send_at_timestamp = now_ms + rtt_ms_; | |
| 191 if (nack_it->second.retries >= kMaxNackRetries) | |
|
stefan-webrtc
2016/02/23 12:29:01
See above
philipel
2016/02/25 15:09:31
Done.
| |
| 192 nack_it = nack_list_.erase(nack_it); | |
| 193 else | |
| 194 ++nack_it; | |
| 195 continue; | |
| 196 } | |
| 197 ++nack_it; | |
| 198 } | |
| 199 return nack_batch; | |
| 200 } | |
| 201 | |
| 202 void NackModule::UpdateReorderingStatistics(uint16_t seq_num) { | |
|
stefan-webrtc
2016/02/23 12:29:01
I think I would recommend breaking out the histogr
philipel
2016/02/25 15:09:31
I agree, will do it for the next upload.
| |
| 203 RTC_DCHECK(AheadOf(last_seq_num_, seq_num)); | |
| 204 | |
| 205 int8_t remove_from_bucket = reordering_occurences_[reordering_index_]; | |
|
stefan-webrtc
2016/02/23 12:29:01
int
philipel
2016/02/25 15:09:31
Done.
| |
| 206 if (remove_from_bucket != -1) | |
| 207 reordering_buckets_[remove_from_bucket] -= 1; | |
| 208 | |
|
stefan-webrtc
2016/02/23 12:29:01
DCHECK_GE(remove_from_bucket, 0);
and maybe also D
| |
| 209 uint16_t diff = NegativeDiff(last_seq_num_, seq_num); | |
|
stefan-webrtc
2016/02/23 12:29:00
Is this the same as PositiveDiff(seq_num, last_seq
philipel
2016/02/25 15:09:32
NegativeDiff(a,b) == PositiveDiff(b,a)
Added comme
| |
| 210 uint16_t add_to_bucket = | |
| 211 std::min(static_cast<uint16_t>(kNumReorderingBuckets - 1), | |
| 212 static_cast<uint16_t>(diff - 1)); | |
| 213 reordering_buckets_[add_to_bucket] += 1; | |
| 214 if (reordering_occurences_[reordering_index_] == -1) | |
| 215 ++reordering_count_; | |
| 216 reordering_occurences_[reordering_index_] = add_to_bucket; | |
| 217 reordering_index_ = Add<kMaxReorderedPackets>(reordering_index_, 1); | |
| 218 } | |
| 219 | |
| 220 uint8_t NackModule::WaitNumberOfPackets(float probability) const { | |
|
stefan-webrtc
2016/02/23 12:29:01
return int
philipel
2016/02/25 15:09:31
Done.
| |
| 221 RTC_DCHECK_GE(probability, 0.f); | |
| 222 RTC_DCHECK_LE(probability, 1.f); | |
| 223 | |
| 224 // Until there are enough values we default to 0. Also | |
| 225 // protect against divide by zero later in the code. | |
| 226 if (reordering_count_ < 20) | |
| 227 return 0; | |
| 228 | |
| 229 uint8_t bucket = 0; | |
| 230 float accumelated_probability = 0; | |
|
stefan-webrtc
2016/02/23 12:29:01
accumulated
philipel
2016/02/25 15:09:31
Done.
| |
| 231 while (accumelated_probability < probability && | |
| 232 bucket < kNumReorderingBuckets) { | |
| 233 accumelated_probability += | |
| 234 static_cast<float>(reordering_buckets_[bucket]) / reordering_count_; | |
| 235 ++bucket; | |
| 236 } | |
| 237 return bucket + 1; | |
| 238 } | |
| 239 | |
| 240 // =========================================================== | |
| 241 // || || | |
| 242 // || NackModule::NackInfo || | |
| 243 // || || | |
| 244 // =========================================================== | |
|
stefan-webrtc
2016/02/23 12:29:01
Remove
philipel
2016/02/25 15:09:31
Done.
| |
| 245 NackModule::NackInfo::NackInfo() | |
| 246 : seq_num(0), send_at_seq_num(0), send_at_timestamp(0), retries(0) {} | |
| 247 | |
| 248 NackModule::NackInfo::NackInfo(uint16_t seq_num, uint16_t send_at_seq_num) | |
| 249 : seq_num(seq_num), | |
| 250 send_at_seq_num(send_at_seq_num), | |
| 251 send_at_timestamp(0), | |
| 252 retries(0) {} | |
|
stefan-webrtc
2016/02/23 12:29:00
Maybe put these methods at the top instead.
philipel
2016/02/25 15:09:30
Done.
| |
| 253 | |
| 254 } // namespace webrtc | |
| OLD | NEW |