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