Index: webrtc/p2p/base/packetlossestimator.cc |
diff --git a/webrtc/p2p/base/packetlossestimator.cc b/webrtc/p2p/base/packetlossestimator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..74967c7cdd06d6075c24e55041b64f6e37ced4ef |
--- /dev/null |
+++ b/webrtc/p2p/base/packetlossestimator.cc |
@@ -0,0 +1,81 @@ |
+/* |
+ * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
Taylor Brandstetter
2017/03/07 18:16:31
nit: 2017
Zach Stein
2017/03/08 06:21:14
Done.
|
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/p2p/base/packetlossestimator.h" |
+ |
+namespace cricket { |
+ |
+PacketLossEstimator::PacketLossEstimator(int64_t consider_lost_after, |
+ int64_t forget_after) |
+ : consider_lost_after_(consider_lost_after), forget_after_(forget_after) {} |
Taylor Brandstetter
2017/03/07 18:16:32
Could add an "RTC_DCHECK_LT(consider_lost_after, f
Zach Stein
2017/03/08 06:21:14
Done.
|
+ |
+void PacketLossEstimator::ExpectResponse(std::string id, int64_t sent_time) { |
+ tracked_packets_[id] = PacketInfo{sent_time, false}; |
+ |
+ UpdateResponseRateIfStale(sent_time); |
Taylor Brandstetter
2017/03/07 18:16:32
Is this called just to ensure the the map doesn't
Zach Stein
2017/03/08 06:21:14
Having ForgetOldRequests not update the response r
Taylor Brandstetter
2017/03/08 17:48:20
How to test that the map doesn't grow indefinitely
Zach Stein
2017/03/08 23:00:08
Done.
|
+} |
+ |
+void PacketLossEstimator::ReceivedResponse(std::string id, |
+ int64_t received_time) { |
+ auto iter = tracked_packets_.find(id); |
+ if (iter != tracked_packets_.end()) { |
+ auto& packet_info = iter->second; |
+ packet_info.response_received = true; |
+ } |
+ |
+ UpdateResponseRateIfStale(received_time); |
+} |
+ |
+void PacketLossEstimator::UpdateResponseRate(int64_t now) { |
+ int responses_expected = 0; |
+ int responses_received = 0; |
+ |
+ for (auto iter = tracked_packets_.begin(); iter != tracked_packets_.end();) { |
+ const auto& packet_info = iter->second; |
+ if (Forget(packet_info, now)) { |
+ iter = tracked_packets_.erase(iter); |
+ continue; |
+ } |
+ if (packet_info.response_received) { |
+ responses_expected += 1; |
+ responses_received += 1; |
+ } else if (ConsiderLost(packet_info, now)) { |
+ responses_expected += 1; |
+ } |
+ ++iter; |
+ } |
+ |
+ if (responses_expected > 0) { |
+ response_rate_ = |
+ static_cast<double>(responses_received) / responses_expected; |
+ } else { |
+ response_rate_ = 1.0; |
+ } |
+ |
+ last_update_time_ = now; |
+} |
+ |
+void PacketLossEstimator::UpdateResponseRateIfStale(int64_t now) { |
+ if (now - last_update_time_ > forget_after_) { |
+ UpdateResponseRate(now); |
+ } |
+} |
+ |
+bool PacketLossEstimator::ConsiderLost(const PacketInfo& packet_info, |
+ int64_t now) const { |
+ return packet_info.sent_time < now - consider_lost_after_; |
+} |
+ |
+bool PacketLossEstimator::Forget(const PacketInfo& packet_info, |
+ int64_t now) const { |
+ return now - packet_info.sent_time > forget_after_; |
+} |
+ |
+} // namespace cricket |