OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 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 "webrtc/p2p/base/packetlossestimator.h" |
| 12 |
| 13 #include "webrtc/base/checks.h" |
| 14 |
| 15 namespace cricket { |
| 16 |
| 17 PacketLossEstimator::PacketLossEstimator(int64_t consider_lost_after_ms, |
| 18 int64_t forget_after_ms) |
| 19 : consider_lost_after_ms_(consider_lost_after_ms), |
| 20 forget_after_ms_(forget_after_ms) { |
| 21 RTC_DCHECK_LT(consider_lost_after_ms, forget_after_ms); |
| 22 } |
| 23 |
| 24 void PacketLossEstimator::ExpectResponse(std::string id, int64_t sent_time) { |
| 25 tracked_packets_[id] = PacketInfo{sent_time, false}; |
| 26 |
| 27 // Called to free memory in case the client hasn't called UpdateResponseRate |
| 28 // in a while. |
| 29 MaybeForgetOldRequests(sent_time); |
| 30 } |
| 31 |
| 32 void PacketLossEstimator::ReceivedResponse(std::string id, |
| 33 int64_t received_time) { |
| 34 auto iter = tracked_packets_.find(id); |
| 35 if (iter != tracked_packets_.end()) { |
| 36 auto& packet_info = iter->second; |
| 37 packet_info.response_received = true; |
| 38 } |
| 39 |
| 40 // Called to free memory in case the client hasn't called UpdateResponseRate |
| 41 // in a while. |
| 42 MaybeForgetOldRequests(received_time); |
| 43 } |
| 44 |
| 45 void PacketLossEstimator::UpdateResponseRate(int64_t now) { |
| 46 int responses_expected = 0; |
| 47 int responses_received = 0; |
| 48 |
| 49 for (auto iter = tracked_packets_.begin(); iter != tracked_packets_.end();) { |
| 50 const auto& packet_info = iter->second; |
| 51 if (Forget(packet_info, now)) { |
| 52 iter = tracked_packets_.erase(iter); |
| 53 continue; |
| 54 } |
| 55 if (packet_info.response_received) { |
| 56 responses_expected += 1; |
| 57 responses_received += 1; |
| 58 } else if (ConsiderLost(packet_info, now)) { |
| 59 responses_expected += 1; |
| 60 } |
| 61 ++iter; |
| 62 } |
| 63 |
| 64 if (responses_expected > 0) { |
| 65 response_rate_ = |
| 66 static_cast<double>(responses_received) / responses_expected; |
| 67 } else { |
| 68 response_rate_ = 1.0; |
| 69 } |
| 70 |
| 71 last_forgot_at_ = now; |
| 72 } |
| 73 |
| 74 void PacketLossEstimator::MaybeForgetOldRequests(int64_t now) { |
| 75 if (now - last_forgot_at_ <= forget_after_ms_) { |
| 76 return; |
| 77 } |
| 78 |
| 79 for (auto iter = tracked_packets_.begin(); iter != tracked_packets_.end();) { |
| 80 const auto& packet_info = iter->second; |
| 81 if (Forget(packet_info, now)) { |
| 82 iter = tracked_packets_.erase(iter); |
| 83 } else { |
| 84 ++iter; |
| 85 } |
| 86 } |
| 87 |
| 88 last_forgot_at_ = now; |
| 89 } |
| 90 |
| 91 bool PacketLossEstimator::ConsiderLost(const PacketInfo& packet_info, |
| 92 int64_t now) const { |
| 93 return packet_info.sent_time < now - consider_lost_after_ms_; |
| 94 } |
| 95 |
| 96 bool PacketLossEstimator::Forget(const PacketInfo& packet_info, |
| 97 int64_t now) const { |
| 98 return now - packet_info.sent_time > forget_after_ms_; |
| 99 } |
| 100 |
| 101 } // namespace cricket |
OLD | NEW |