diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn index 6148105939..633dc95c5f 100644 --- a/p2p/BUILD.gn +++ b/p2p/BUILD.gn @@ -41,8 +41,6 @@ rtc_static_library("rtc_p2p") { "base/p2p_constants.h", "base/p2p_transport_channel.cc", "base/p2p_transport_channel.h", - "base/packet_loss_estimator.cc", - "base/packet_loss_estimator.h", "base/packet_socket_factory.cc", "base/packet_socket_factory.h", "base/packet_transport_interface.h", @@ -158,7 +156,6 @@ if (rtc_include_tests) { "base/ice_credentials_iterator_unittest.cc", "base/mdns_message_unittest.cc", "base/p2p_transport_channel_unittest.cc", - "base/packet_loss_estimator_unittest.cc", "base/port_allocator_unittest.cc", "base/port_unittest.cc", "base/pseudo_tcp_unittest.cc", diff --git a/p2p/base/packet_loss_estimator.cc b/p2p/base/packet_loss_estimator.cc deleted file mode 100644 index 435fbb0304..0000000000 --- a/p2p/base/packet_loss_estimator.cc +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2017 The WebRTC Project Authors. All rights reserved. - * - * 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 "p2p/base/packet_loss_estimator.h" - -#include "rtc_base/checks.h" - -namespace cricket { - -PacketLossEstimator::PacketLossEstimator(int64_t consider_lost_after_ms, - int64_t forget_after_ms) - : consider_lost_after_ms_(consider_lost_after_ms), - forget_after_ms_(forget_after_ms) { - RTC_DCHECK_LT(consider_lost_after_ms, forget_after_ms); -} - -PacketLossEstimator::~PacketLossEstimator() = default; - -void PacketLossEstimator::ExpectResponse(std::string id, int64_t sent_time) { - tracked_packets_[id] = PacketInfo{sent_time, false}; - - // Called to free memory in case the client hasn't called UpdateResponseRate - // in a while. - MaybeForgetOldRequests(sent_time); -} - -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; - } - - // Called to free memory in case the client hasn't called UpdateResponseRate - // in a while. - MaybeForgetOldRequests(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(responses_received) / responses_expected; - } else { - response_rate_ = 1.0; - } - - last_forgot_at_ = now; -} - -void PacketLossEstimator::MaybeForgetOldRequests(int64_t now) { - if (now - last_forgot_at_ <= forget_after_ms_) { - return; - } - - 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); - } else { - ++iter; - } - } - - last_forgot_at_ = now; -} - -bool PacketLossEstimator::ConsiderLost(const PacketInfo& packet_info, - int64_t now) const { - return packet_info.sent_time < now - consider_lost_after_ms_; -} - -bool PacketLossEstimator::Forget(const PacketInfo& packet_info, - int64_t now) const { - return now - packet_info.sent_time > forget_after_ms_; -} - -std::size_t PacketLossEstimator::tracked_packet_count_for_testing() const { - return tracked_packets_.size(); -} - -} // namespace cricket diff --git a/p2p/base/packet_loss_estimator.h b/p2p/base/packet_loss_estimator.h deleted file mode 100644 index 09a46fd378..0000000000 --- a/p2p/base/packet_loss_estimator.h +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2017 The WebRTC Project Authors. All rights reserved. - * - * 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. - */ - -#ifndef P2P_BASE_PACKET_LOSS_ESTIMATOR_H_ -#define P2P_BASE_PACKET_LOSS_ESTIMATOR_H_ - -#include -#include -#include -#include - -namespace cricket { - -// Estimates the response rate for a series of messages expecting responses. -// Messages and their corresponding responses are identified by a string id. -// -// Responses are considered lost if they are not received within -// |consider_lost_after_ms|. If a response is received after -// |consider_lost_after_ms|, it is still considered as a response. -// Messages sent more than |forget_after_ms| ago are not considered -// anymore. The response rate is initially assumed to be 1.0. -// -// If the current time is 7, |forget_after_ms| is 6, and -// |consider_lost_after_ms| is 2, the response rate considers messages sent -// between times 1 and 5, so only messages in the following window can be -// considered lost: -// -// Time: 0 1 2 3 4 5 6 7 -// Wind: <-------> | -// -// Responses received to the right of the window are still counted. -class PacketLossEstimator { - public: - explicit PacketLossEstimator(int64_t consider_lost_after_ms, - int64_t forget_after_ms); - ~PacketLossEstimator(); - - // Registers that a message with the given |id| was sent at |sent_time|. - void ExpectResponse(std::string id, int64_t sent_time); - - // Registers a response with the given |id| was received at |received_time|. - void ReceivedResponse(std::string id, int64_t received_time); - - // Calculates the current response rate based on the expected and received - // messages. Messages sent more than |forget_after| ms ago will be forgotten. - void UpdateResponseRate(int64_t now); - - // Gets the current response rate as updated by UpdateResponseRate. - double get_response_rate() const { return response_rate_; } - - std::size_t tracked_packet_count_for_testing() const; - - private: - struct PacketInfo { - int64_t sent_time; - bool response_received; - }; - - // Called periodically by ExpectResponse and ReceivedResponse to manage memory - // usage. - void MaybeForgetOldRequests(int64_t now); - - bool ConsiderLost(const PacketInfo&, int64_t now) const; - bool Forget(const PacketInfo&, int64_t now) const; - - int64_t consider_lost_after_ms_; - int64_t forget_after_ms_; - - int64_t last_forgot_at_ = 0; - - std::unordered_map tracked_packets_; - - double response_rate_ = 1.0; -}; - -} // namespace cricket - -#endif // P2P_BASE_PACKET_LOSS_ESTIMATOR_H_ diff --git a/p2p/base/packet_loss_estimator_unittest.cc b/p2p/base/packet_loss_estimator_unittest.cc deleted file mode 100644 index f45d52e745..0000000000 --- a/p2p/base/packet_loss_estimator_unittest.cc +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2017 The WebRTC Project Authors. All rights reserved. - * - * 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 -#include - -#include "p2p/base/packet_loss_estimator.h" -#include "test/gtest.h" - -using cricket::PacketLossEstimator; - -class PacketLossEstimatorTest : public testing::Test {}; - -TEST_F(PacketLossEstimatorTest, InitialResponseRate) { - PacketLossEstimator ple(5, 100); - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, InitialUpdateResponseRate) { - PacketLossEstimator ple(5, 100); - ple.UpdateResponseRate(10); - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, ResponseReceived) { - PacketLossEstimator ple(5, 100); - - ple.ExpectResponse("a", 0); - ple.ReceivedResponse("a", 1); - ple.UpdateResponseRate(2); - - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, ResponseNotConsideredLostYet) { - PacketLossEstimator ple(5, 100); - - ple.ExpectResponse("a", 0); - ple.UpdateResponseRate(2); - - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, ResponseConsideredLost) { - PacketLossEstimator ple(5, 100); - - ple.ExpectResponse("a", 0); - ple.UpdateResponseRate(10); - - EXPECT_EQ(0.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, ResponseLate) { - PacketLossEstimator ple(5, 100); - - ple.ExpectResponse("a", 0); - ple.ReceivedResponse("a", 6); - ple.UpdateResponseRate(10); - - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, ResponseForgotten) { - PacketLossEstimator ple(5, 100); - ple.ExpectResponse("a", 0); - ple.UpdateResponseRate(101); - - EXPECT_EQ(1.0, ple.get_response_rate()); -} - -TEST_F(PacketLossEstimatorTest, Lost1_Received1) { - PacketLossEstimator ple(5, 100); - - ple.ExpectResponse("a", 0); - ple.ExpectResponse("b", 2); - ple.ReceivedResponse("b", 6); - ple.UpdateResponseRate(7); - - EXPECT_EQ(0.5, ple.get_response_rate()); -} - -static const std::pair kFivePackets[5] = {{"a", 0}, - {"b", 2}, - {"c", 4}, - {"d", 6}, - {"e", 8}}; - -// Time: 0 1 2 3 4 5 6 7 8 9 10 -// Sent: a b c d e | -// Recv: b | -// Wind: --------------> | -TEST_F(PacketLossEstimatorTest, Lost3_Received1_Waiting1) { - PacketLossEstimator ple(3, 100); - - for (const auto& p : kFivePackets) { - ple.ExpectResponse(p.first, p.second); - } - ple.ReceivedResponse("b", 3); - ple.UpdateResponseRate(10); - EXPECT_EQ(0.25, ple.get_response_rate()); -} - -// Time: 0 1 2 3 4 5 6 7 8 9 10 -// Sent: a b c d e | -// Recv: e | -// Wind: --------------> | -TEST_F(PacketLossEstimatorTest, Lost4_Early1) { - PacketLossEstimator ple(3, 100); - - for (const auto& p : kFivePackets) { - ple.ExpectResponse(p.first, p.second); - } - ple.ReceivedResponse("e", 9); - ple.UpdateResponseRate(10); - // e should be considered, even though its response was received less than - // |consider_lost_after_ms| ago. - EXPECT_EQ(0.2, ple.get_response_rate()); -} - -// Time: 0 1 2 3 4 5 6 7 8 9 10 -// Sent: a b c d e | -// Recv: c | -// Wind: <-------> | -TEST_F(PacketLossEstimatorTest, Forgot2_Received1_Lost1_Waiting1) { - PacketLossEstimator ple(3, 7); - - for (const auto& p : kFivePackets) { - ple.ExpectResponse(p.first, p.second); - } - ple.ReceivedResponse("c", 5); - ple.UpdateResponseRate(10); - EXPECT_EQ(0.5, ple.get_response_rate()); -} - -// Tests that old messages are forgotten if ExpectResponse is called -// |forget_after_ms| after |last_forgot_at|. It is important that ExpectResponse -// and ReceivedResponse forget old messages to avoid |tracked_packets_| growing -// without bound if UpdateResponseRate is never called (or rarely called). -// -// Time: 0 1 2 3 4 5 6 -// Sent: a b -// Wind: <-------> -TEST_F(PacketLossEstimatorTest, ExpectResponseForgetsOldPackets) { - PacketLossEstimator ple(1, 5); - ple.ExpectResponse("a", 0); // This message will be forgotten. - ple.ExpectResponse("b", 6); // This call should trigger clean up. - // a should be forgotten, b should be tracked. - EXPECT_EQ(1u, ple.tracked_packet_count_for_testing()); -} - -// Tests that old messages are forgotten if ExpectResponse is called -// |forget_after_ms| after |last_forgot_at|. It is important that ExpectResponse -// and ReceivedResponse forget old messages to avoid |tracked_packets_| growing -// without bound if UpdateResponseRate is never called (or rarely called). -// -// Time: 0 1 2 3 4 5 6 -// Sent: a -// Recv: b -// Wind: <-------> -TEST_F(PacketLossEstimatorTest, ReceivedResponseForgetsOldPackets) { - PacketLossEstimator ple(1, 5); - ple.ExpectResponse("a", 0); // This message will be forgotten. - ple.ReceivedResponse("b", 6); // This call should trigger clean up. - // a should be forgoten, b should not be tracked (received but not sent). - EXPECT_EQ(0u, ple.tracked_packet_count_for_testing()); -} diff --git a/p2p/base/port.cc b/p2p/base/port.cc index d6e8cdd7d2..1977408d29 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -153,12 +153,6 @@ const int RTT_RATIO = 3; // 3 : 1 // it to a little higher than a total STUN timeout. const int kPortTimeoutDelay = cricket::STUN_TOTAL_TIMEOUT + 5000; -// For packet loss estimation. -const int64_t kConsiderPacketLostAfter = 3000; // 3 seconds - -// For packet loss estimation. -const int64_t kForgetPacketAfter = 30000; // 30 seconds - } // namespace namespace cricket { @@ -1120,7 +1114,6 @@ Connection::Connection(Port* port, last_ping_received_(0), last_data_received_(0), last_ping_response_received_(0), - packet_loss_estimator_(kConsiderPacketLostAfter, kForgetPacketAfter), reported_(false), state_(IceCandidatePairState::WAITING), time_created_ms_(rtc::TimeMillis()) { @@ -1513,7 +1506,6 @@ void Connection::Ping(int64_t now) { nomination = nomination_; } pings_since_last_response_.push_back(SentPing(req->id(), now, nomination)); - packet_loss_estimator_.ExpectResponse(req->id(), now); RTC_LOG(LS_VERBOSE) << ToString() << ": Sending STUN ping, id=" << rtc::hex_encode(req->id()) << ", nomination=" << nomination_; @@ -1710,9 +1702,6 @@ void Connection::OnConnectionRequestResponse(ConnectionRequest* request, } ReceivedPingResponse(rtt, request->id()); - int64_t time_received = rtc::TimeMillis(); - packet_loss_estimator_.ReceivedResponse(request->id(), time_received); - stats_.recv_ping_responses++; LogCandidatePairEvent( webrtc::IceCandidatePairEventType::kCheckResponseReceived, diff --git a/p2p/base/port.h b/p2p/base/port.h index cd8b737fb2..5f07eb4297 100644 --- a/p2p/base/port.h +++ b/p2p/base/port.h @@ -25,7 +25,6 @@ #include "logging/rtc_event_log/ice_logger.h" #include "p2p/base/candidate_pair_interface.h" #include "p2p/base/p2p_constants.h" -#include "p2p/base/packet_loss_estimator.h" #include "p2p/base/packet_socket_factory.h" #include "p2p/base/port_interface.h" #include "p2p/base/stun.h" @@ -837,8 +836,6 @@ class Connection : public CandidatePairInterface, int64_t receiving_unchanged_since_ = 0; std::vector pings_since_last_response_; - PacketLossEstimator packet_loss_estimator_; - absl::optional unwritable_timeout_; absl::optional unwritable_min_checks_; absl::optional inactive_timeout_;