From d58f5263844df2e24604c7fff7a932c6baf6ade3 Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Mon, 27 Jun 2022 18:05:22 +0900 Subject: [PATCH] Always inject PacketSocketFactory in FakePortAllocator This CL removes the use of the rtc::Thread::socketserver() method in one place. Bug: webrtc:13145 Change-Id: I1a1b2501450788263d5280c43e4328ade46f4146 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/263320 Reviewed-by: Niels Moller Reviewed-by: Harald Alvestrand Commit-Queue: Daniel.L (Byoungchan) Lee Cr-Commit-Position: refs/heads/main@{#37340} --- p2p/BUILD.gn | 1 + p2p/base/fake_port_allocator.h | 40 +++---- p2p/base/p2p_transport_channel_unittest.cc | 101 ++++++++++-------- p2p/base/port_allocator_unittest.cc | 12 ++- p2p/base/regathering_controller_unittest.cc | 12 ++- pc/ice_transport_unittest.cc | 15 ++- ...r_connection_adaptation_integrationtest.cc | 3 +- pc/peer_connection_crypto_unittest.cc | 3 +- pc/peer_connection_end_to_end_unittest.cc | 4 +- pc/peer_connection_factory_unittest.cc | 17 ++- ...er_connection_header_extension_unittest.cc | 11 +- pc/peer_connection_ice_unittest.cc | 14 ++- pc/peer_connection_interface_unittest.cc | 11 +- pc/peer_connection_media_unittest.cc | 3 +- pc/rtc_stats_integrationtest.cc | 6 +- pc/test/peer_connection_test_wrapper.cc | 6 +- pc/test/peer_connection_test_wrapper.h | 2 + 17 files changed, 172 insertions(+), 89 deletions(-) diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn index 0ee345e099..29fd2891bf 100644 --- a/p2p/BUILD.gn +++ b/p2p/BUILD.gn @@ -186,6 +186,7 @@ if (rtc_include_tests) { "../rtc_base", "../rtc_base:net_helpers", "../rtc_base:threading", + "../rtc_base/memory:always_valid_pointer", "../test:scoped_key_value_config", ] absl_deps = [ "//third_party/abseil-cpp/absl/strings" ] diff --git a/p2p/base/fake_port_allocator.h b/p2p/base/fake_port_allocator.h index 66dc6a4265..ce57608824 100644 --- a/p2p/base/fake_port_allocator.h +++ b/p2p/base/fake_port_allocator.h @@ -13,12 +13,14 @@ #include #include +#include #include #include "absl/strings/string_view.h" #include "p2p/base/basic_packet_socket_factory.h" #include "p2p/base/port_allocator.h" #include "p2p/base/udp_port.h" +#include "rtc_base/memory/always_valid_pointer.h" #include "rtc_base/net_helpers.h" #include "rtc_base/thread.h" #include "test/scoped_key_value_config.h" @@ -213,23 +215,13 @@ class FakePortAllocatorSession : public PortAllocatorSession { class FakePortAllocator : public cricket::PortAllocator { public: - // TODO(bugs.webrtc.org/13145): Require non-null `factory`. FakePortAllocator(rtc::Thread* network_thread, rtc::PacketSocketFactory* factory) - : network_thread_(network_thread), factory_(factory) { - if (factory_ == NULL) { - owned_factory_.reset(new rtc::BasicPacketSocketFactory( - network_thread_ ? network_thread_->socketserver() : nullptr)); - factory_ = owned_factory_.get(); - } + : FakePortAllocator(network_thread, factory, nullptr) {} - if (network_thread_ == nullptr) { - network_thread_ = rtc::Thread::Current(); - Initialize(); - return; - } - network_thread_->Invoke(RTC_FROM_HERE, [this] { Initialize(); }); - } + FakePortAllocator(rtc::Thread* network_thread, + std::unique_ptr factory) + : FakePortAllocator(network_thread, nullptr, std::move(factory)) {} void SetNetworkIgnoreMask(int network_ignore_mask) override {} @@ -249,8 +241,8 @@ class FakePortAllocator : public cricket::PortAllocator { absl::string_view ice_ufrag, absl::string_view ice_pwd) override { return new FakePortAllocatorSession( - this, network_thread_, factory_, std::string(content_name), component, - std::string(ice_ufrag), std::string(ice_pwd), field_trials_); + this, network_thread_, factory_.get(), std::string(content_name), + component, std::string(ice_ufrag), std::string(ice_pwd), field_trials_); } bool initialized() const { return initialized_; } @@ -264,10 +256,22 @@ class FakePortAllocator : public cricket::PortAllocator { } private: + FakePortAllocator(rtc::Thread* network_thread, + rtc::PacketSocketFactory* factory, + std::unique_ptr owned_factory) + : network_thread_(network_thread), + factory_(std::move(owned_factory), factory) { + if (network_thread_ == nullptr) { + network_thread_ = rtc::Thread::Current(); + Initialize(); + return; + } + network_thread_->Invoke(RTC_FROM_HERE, [this] { Initialize(); }); + } + webrtc::test::ScopedKeyValueConfig field_trials_; rtc::Thread* network_thread_; - rtc::PacketSocketFactory* factory_; - std::unique_ptr owned_factory_; + const webrtc::AlwaysValidPointerNoDefault factory_; bool mdns_obfuscation_enabled_ = false; }; diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc index c4f914ac5e..8ff81fcd8a 100644 --- a/p2p/base/p2p_transport_channel_unittest.cc +++ b/p2p/base/p2p_transport_channel_unittest.cc @@ -32,6 +32,7 @@ #include "rtc_base/firewall_socket_server.h" #include "rtc_base/gunit.h" #include "rtc_base/helpers.h" +#include "rtc_base/internal/default_socket_server.h" #include "rtc_base/logging.h" #include "rtc_base/mdns_responder_interface.h" #include "rtc_base/nat_server.h" @@ -3387,7 +3388,10 @@ class P2PTransportChannelPingTest : public ::testing::Test, public sigslot::has_slots<> { public: P2PTransportChannelPingTest() - : vss_(new rtc::VirtualSocketServer()), thread_(vss_.get()) {} + : vss_(std::make_unique()), + packet_socket_factory_( + std::make_unique(vss_.get())), + thread_(vss_.get()) {} protected: void PrepareChannel(P2PTransportChannel* ch) { @@ -3585,8 +3589,13 @@ class P2PTransportChannelPingTest : public ::testing::Test, rtc::SocketServer* ss() const { return vss_.get(); } + rtc::PacketSocketFactory* packet_socket_factory() const { + return packet_socket_factory_.get(); + } + private: std::unique_ptr vss_; + std::unique_ptr packet_socket_factory_; rtc::AutoSocketServerThread thread_; int selected_candidate_pair_switches_ = 0; int last_sent_packet_id_ = -1; @@ -3597,7 +3606,7 @@ class P2PTransportChannelPingTest : public ::testing::Test, }; TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("trigger checks", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3621,7 +3630,7 @@ TEST_F(P2PTransportChannelPingTest, TestTriggeredChecks) { } TEST_F(P2PTransportChannelPingTest, TestAllConnectionsPingedSufficiently) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("ping sufficiently", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3649,7 +3658,7 @@ TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { int SCHEDULING_RANGE = 200; int RTT_RANGE = 10; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("TestChannel", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3740,7 +3749,7 @@ TEST_F(P2PTransportChannelPingTest, TestStunPingIntervals) { TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) { rtc::ScopedFakeClock clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("TestChannel", 1, &pa); ch.SetIceRole(ICEROLE_CONTROLLING); ch.SetIceParameters(kIceParams[0]); @@ -3776,7 +3785,7 @@ TEST_F(P2PTransportChannelPingTest, PingingStartedAsSoonAsPossible) { } TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("trigger checks", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3801,7 +3810,7 @@ TEST_F(P2PTransportChannelPingTest, TestNoTriggeredChecksWhenWritable) { } TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("Do not ping failed connections", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3818,7 +3827,7 @@ TEST_F(P2PTransportChannelPingTest, TestFailedConnectionNotPingable) { } TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("state change", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3839,7 +3848,7 @@ TEST_F(P2PTransportChannelPingTest, TestSignalStateChanged) { // parameters arrive. If a remote candidate is added with the current ICE // ufrag, its pwd and generation will be set properly. TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("add candidate", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3891,7 +3900,7 @@ TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithVariousUfrags) { } TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("connection resurrection", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -3944,7 +3953,7 @@ TEST_F(P2PTransportChannelPingTest, ConnectionResurrection) { TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { rtc::ScopedFakeClock clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa); PrepareChannel(&ch); // Default receiving timeout and checking receiving interval should not be too @@ -3973,7 +3982,7 @@ TEST_F(P2PTransportChannelPingTest, TestReceivingStateChange) { // selected connection changes and SignalReadyToSend will be fired if the new // selected connection is writable. TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4062,7 +4071,7 @@ TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBeforeNomination) { TEST_F(P2PTransportChannelPingTest, TestPingOnNomination) { webrtc::test::ScopedKeyValueConfig field_trials( "WebRTC-IceFieldTrials/send_ping_on_nomination_ice_controlled:true/"); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -4102,7 +4111,7 @@ TEST_F(P2PTransportChannelPingTest, TestPingOnNomination) { TEST_F(P2PTransportChannelPingTest, TestPingOnSwitch) { webrtc::test::ScopedKeyValueConfig field_trials( "WebRTC-IceFieldTrials/send_ping_on_switch_ice_controlling:true/"); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -4139,7 +4148,7 @@ TEST_F(P2PTransportChannelPingTest, TestPingOnSwitch) { TEST_F(P2PTransportChannelPingTest, TestPingOnSelected) { webrtc::test::ScopedKeyValueConfig field_trials( "WebRTC-IceFieldTrials/send_ping_on_selected_ice_controlling:true/"); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -4167,7 +4176,7 @@ TEST_F(P2PTransportChannelPingTest, TestPingOnSelected) { // also sends back a ping response and set the ICE pwd in the remote candidate // appropriately. TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4244,7 +4253,7 @@ TEST_F(P2PTransportChannelPingTest, TestSelectConnectionFromUnknownAddress) { // at which point the controlled side will select that connection as // the "selected connection". TEST_F(P2PTransportChannelPingTest, TestSelectConnectionBasedOnMediaReceived) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("receiving state change", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4297,7 +4306,7 @@ TEST_F(P2PTransportChannelPingTest, TestControlledAgentDataReceivingTakesHigherPrecedenceThanPriority) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4346,7 +4355,7 @@ TEST_F(P2PTransportChannelPingTest, rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4386,7 +4395,7 @@ TEST_F(P2PTransportChannelPingTest, rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4432,7 +4441,7 @@ TEST_F(P2PTransportChannelPingTest, TestEstimatedDisconnectedTime) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4491,7 +4500,7 @@ TEST_F(P2PTransportChannelPingTest, rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4509,7 +4518,7 @@ TEST_F(P2PTransportChannelPingTest, TestControlledAgentWriteStateTakesHigherPrecedenceThanNomination) { rtc::ScopedFakeClock clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("SwitchSelectedConnection", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4550,7 +4559,7 @@ TEST_F(P2PTransportChannelPingTest, // Test that if a new remote candidate has the same address and port with // an old one, it will be used to create a new connection. TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("candidate reuse", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -4590,7 +4599,7 @@ TEST_F(P2PTransportChannelPingTest, TestAddRemoteCandidateWithAddressReuse) { TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4626,7 +4635,7 @@ TEST_F(P2PTransportChannelPingTest, TestDontPruneWhenWeak) { TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { rtc::ScopedFakeClock clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4650,7 +4659,7 @@ TEST_F(P2PTransportChannelPingTest, TestDontPruneHighPriorityConnections) { TEST_F(P2PTransportChannelPingTest, TestGetState) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); EXPECT_EQ(webrtc::IceTransportState::kNew, ch.GetIceTransportState()); PrepareChannel(&ch); @@ -4691,7 +4700,7 @@ TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); PrepareChannel(&ch); IceConfig config = CreateIceConfig(1000, GATHER_ONCE); @@ -4741,7 +4750,7 @@ TEST_F(P2PTransportChannelPingTest, TestConnectionPrunedAgain) { // will all be deleted. We use Prune to simulate write_time_out. TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { rtc::ScopedFakeClock clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); PrepareChannel(&ch); ch.MaybeStartGathering(); @@ -4773,7 +4782,7 @@ TEST_F(P2PTransportChannelPingTest, TestDeleteConnectionsIfAllWriteTimedout) { // connection belonging to an old session becomes writable, it won't stop // the current port allocator session. TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa); PrepareChannel(&ch); ch.SetIceConfig(CreateIceConfig(2000, GATHER_ONCE)); @@ -4806,7 +4815,7 @@ TEST_F(P2PTransportChannelPingTest, TestStopPortAllocatorSessions) { // These ports may still have connections that need a correct role, in case that // the connections on it may still receive stun pings. TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); // Starts with ICEROLE_CONTROLLING. PrepareChannel(&ch); @@ -4831,7 +4840,7 @@ TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnRemovedPort) { // pings sent by those connections until they're replaced by newer-generation // connections. TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); // Starts with ICEROLE_CONTROLLING. PrepareChannel(&ch); @@ -4855,7 +4864,7 @@ TEST_F(P2PTransportChannelPingTest, TestIceRoleUpdatedOnPortAfterIceRestart) { TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeoutAndPruned) { rtc::ScopedFakeClock fake_clock; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", ICE_CANDIDATE_COMPONENT_DEFAULT, &pa); PrepareChannel(&ch); ch.SetIceRole(ICEROLE_CONTROLLED); @@ -4884,7 +4893,7 @@ TEST_F(P2PTransportChannelPingTest, TestPortDestroyedAfterTimeoutAndPruned) { TEST_F(P2PTransportChannelPingTest, TestMaxOutstandingPingsFieldTrial) { webrtc::test::ScopedKeyValueConfig field_trials( "WebRTC-IceFieldTrials/max_outstanding_pings:3/"); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("max", 1, &pa, &field_trials); ch.SetIceConfig(ch.config()); PrepareChannel(&ch); @@ -5145,8 +5154,11 @@ TEST_F(P2PTransportChannelMostLikelyToWorkFirstTest, TestTcpTurn) { // if the channel is not destroyed. TEST(P2PTransportChannelResolverTest, HostnameCandidateIsResolved) { ResolverFactoryFixture resolver_fixture; - rtc::AutoThread main_thread; - FakePortAllocator allocator(rtc::Thread::Current(), nullptr); + std::unique_ptr socket_server = + rtc::CreateDefaultSocketServer(); + rtc::AutoSocketServerThread main_thread(socket_server.get()); + rtc::BasicPacketSocketFactory packet_socket_factory(socket_server.get()); + FakePortAllocator allocator(rtc::Thread::Current(), &packet_socket_factory); webrtc::IceTransportInit init; init.set_port_allocator(&allocator); init.set_async_dns_resolver_factory(&resolver_fixture); @@ -5984,7 +5996,7 @@ TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampening0) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -6008,7 +6020,7 @@ TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampening) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -6032,7 +6044,7 @@ TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampeningPingReceived) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -6059,7 +6071,7 @@ TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampeningBoth) { rtc::ScopedFakeClock clock; clock.AdvanceTime(webrtc::TimeDelta::Seconds(1)); - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); P2PTransportChannel ch("test channel", 1, &pa, &field_trials); PrepareChannel(&ch); ch.SetIceConfig(ch.config()); @@ -6078,9 +6090,12 @@ TEST_F(P2PTransportChannelPingTest, TestInitialSelectDampeningBoth) { } TEST(P2PTransportChannel, InjectIceController) { - rtc::AutoThread main_thread_; + std::unique_ptr socket_server = + rtc::CreateDefaultSocketServer(); + rtc::AutoSocketServerThread main_thread(socket_server.get()); + rtc::BasicPacketSocketFactory packet_socket_factory(socket_server.get()); MockIceControllerFactory factory; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), &packet_socket_factory); EXPECT_CALL(factory, RecordIceControllerCreated()).Times(1); webrtc::IceTransportInit init; init.set_port_allocator(&pa); @@ -6133,7 +6148,7 @@ class ForgetLearnedStateControllerFactory TEST_F(P2PTransportChannelPingTest, TestForgetLearnedState) { ForgetLearnedStateControllerFactory factory; - FakePortAllocator pa(rtc::Thread::Current(), nullptr); + FakePortAllocator pa(rtc::Thread::Current(), packet_socket_factory()); webrtc::IceTransportInit init; init.set_port_allocator(&pa); init.set_ice_controller_factory(&factory); diff --git a/p2p/base/port_allocator_unittest.cc b/p2p/base/port_allocator_unittest.cc index cbac5cccaf..e566ef85a9 100644 --- a/p2p/base/port_allocator_unittest.cc +++ b/p2p/base/port_allocator_unittest.cc @@ -28,10 +28,13 @@ static const char kTurnPassword[] = "test"; class PortAllocatorTest : public ::testing::Test, public sigslot::has_slots<> { public: PortAllocatorTest() - : vss_(new rtc::VirtualSocketServer()), main_(vss_.get()) { - allocator_.reset( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); - } + : vss_(std::make_unique()), + main_(vss_.get()), + packet_socket_factory_( + std::make_unique(vss_.get())), + allocator_(std::make_unique( + rtc::Thread::Current(), + packet_socket_factory_.get())) {} protected: void SetConfigurationWithPoolSize(int candidate_pool_size) { @@ -80,6 +83,7 @@ class PortAllocatorTest : public ::testing::Test, public sigslot::has_slots<> { std::unique_ptr vss_; rtc::AutoSocketServerThread main_; + std::unique_ptr packet_socket_factory_; std::unique_ptr allocator_; rtc::SocketAddress stun_server_1{"11.11.11.11", 3478}; rtc::SocketAddress stun_server_2{"22.22.22.22", 3478}; diff --git a/p2p/base/regathering_controller_unittest.cc b/p2p/base/regathering_controller_unittest.cc index 3a487b5864..597bcf3f35 100644 --- a/p2p/base/regathering_controller_unittest.cc +++ b/p2p/base/regathering_controller_unittest.cc @@ -48,11 +48,14 @@ class RegatheringControllerTest : public ::testing::Test, public sigslot::has_slots<> { public: RegatheringControllerTest() - : vss_(new rtc::VirtualSocketServer()), + : vss_(std::make_unique()), thread_(vss_.get()), - ice_transport_(new cricket::MockIceTransport()), - allocator_( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)) { + ice_transport_(std::make_unique()), + packet_socket_factory_( + std::make_unique(vss_.get())), + allocator_(std::make_unique( + rtc::Thread::Current(), + packet_socket_factory_.get())) { BasicRegatheringController::Config regathering_config; regathering_config.regather_on_failed_networks_interval = 0; regathering_controller_.reset(new BasicRegatheringController( @@ -108,6 +111,7 @@ class RegatheringControllerTest : public ::testing::Test, rtc::AutoSocketServerThread thread_; std::unique_ptr ice_transport_; std::unique_ptr regathering_controller_; + std::unique_ptr packet_socket_factory_; std::unique_ptr allocator_; std::unique_ptr allocator_session_; std::map count_; diff --git a/pc/ice_transport_unittest.cc b/pc/ice_transport_unittest.cc index 51829dc868..0b1be27594 100644 --- a/pc/ice_transport_unittest.cc +++ b/pc/ice_transport_unittest.cc @@ -18,13 +18,22 @@ #include "api/scoped_refptr.h" #include "p2p/base/fake_ice_transport.h" #include "p2p/base/fake_port_allocator.h" +#include "rtc_base/internal/default_socket_server.h" #include "test/gtest.h" namespace webrtc { class IceTransportTest : public ::testing::Test { + protected: + IceTransportTest() + : socket_server_(rtc::CreateDefaultSocketServer()), + main_thread_(socket_server_.get()) {} + + rtc::SocketServer* socket_server() const { return socket_server_.get(); } + private: - rtc::AutoThread main_thread_; + std::unique_ptr socket_server_; + rtc::AutoSocketServerThread main_thread_; }; TEST_F(IceTransportTest, CreateNonSelfDeletingTransport) { @@ -39,7 +48,9 @@ TEST_F(IceTransportTest, CreateNonSelfDeletingTransport) { TEST_F(IceTransportTest, CreateSelfDeletingTransport) { std::unique_ptr port_allocator( - std::make_unique(nullptr, nullptr)); + std::make_unique( + nullptr, + std::make_unique(socket_server()))); IceTransportInit init; init.set_port_allocator(port_allocator.get()); auto ice_transport = CreateIceTransport(std::move(init)); diff --git a/pc/peer_connection_adaptation_integrationtest.cc b/pc/peer_connection_adaptation_integrationtest.cc index 5fbce9ec71..5922e15034 100644 --- a/pc/peer_connection_adaptation_integrationtest.cc +++ b/pc/peer_connection_adaptation_integrationtest.cc @@ -94,7 +94,8 @@ class PeerConnectionAdaptationIntegrationTest : public ::testing::Test { const char* name) { rtc::scoped_refptr pc_wrapper = rtc::make_ref_counted( - name, network_thread_.get(), worker_thread_.get()); + name, &virtual_socket_server_, network_thread_.get(), + worker_thread_.get()); PeerConnectionInterface::RTCConfiguration config; config.sdp_semantics = SdpSemantics::kUnifiedPlan; EXPECT_TRUE(pc_wrapper->CreatePc(config, CreateBuiltinAudioEncoderFactory(), diff --git a/pc/peer_connection_crypto_unittest.cc b/pc/peer_connection_crypto_unittest.cc index 5ed93431a3..059700c51c 100644 --- a/pc/peer_connection_crypto_unittest.cc +++ b/pc/peer_connection_crypto_unittest.cc @@ -96,7 +96,8 @@ class PeerConnectionCryptoBaseTest : public ::testing::Test { const RTCConfiguration& config, std::unique_ptr cert_gen) { auto fake_port_allocator = std::make_unique( - rtc::Thread::Current(), nullptr); + rtc::Thread::Current(), + std::make_unique(vss_.get())); auto observer = std::make_unique(); RTCConfiguration modified_config = config; modified_config.sdp_semantics = sdp_semantics_; diff --git a/pc/peer_connection_end_to_end_unittest.cc b/pc/peer_connection_end_to_end_unittest.cc index e6ded0ca50..89387f9401 100644 --- a/pc/peer_connection_end_to_end_unittest.cc +++ b/pc/peer_connection_end_to_end_unittest.cc @@ -86,9 +86,9 @@ class PeerConnectionEndToEndBaseTest : public sigslot::has_slots<>, RTC_CHECK(network_thread_->Start()); RTC_CHECK(worker_thread_->Start()); caller_ = rtc::make_ref_counted( - "caller", network_thread_.get(), worker_thread_.get()); + "caller", &pss_, network_thread_.get(), worker_thread_.get()); callee_ = rtc::make_ref_counted( - "callee", network_thread_.get(), worker_thread_.get()); + "callee", &pss_, network_thread_.get(), worker_thread_.get()); webrtc::PeerConnectionInterface::IceServer ice_server; ice_server.uri = "stun:stun.l.google.com:19302"; config_.servers.push_back(ice_server); diff --git a/pc/peer_connection_factory_unittest.cc b/pc/peer_connection_factory_unittest.cc index 01785ef186..7f71c761da 100644 --- a/pc/peer_connection_factory_unittest.cc +++ b/pc/peer_connection_factory_unittest.cc @@ -34,6 +34,7 @@ #include "pc/test/fake_video_track_source.h" #include "pc/test/mock_peer_connection_observers.h" #include "rtc_base/gunit.h" +#include "rtc_base/internal/default_socket_server.h" #include "rtc_base/rtc_certificate_generator.h" #include "rtc_base/socket_address.h" #include "rtc_base/time_utils.h" @@ -120,6 +121,12 @@ class MockNetworkManager : public rtc::NetworkManager { } // namespace class PeerConnectionFactoryTest : public ::testing::Test { + public: + PeerConnectionFactoryTest() + : socket_server_(rtc::CreateDefaultSocketServer()), + main_thread_(socket_server_.get()) {} + + private: void SetUp() { #ifdef WEBRTC_ANDROID webrtc::InitializeAndroidObjects(); @@ -138,8 +145,10 @@ class PeerConnectionFactoryTest : public ::testing::Test { nullptr /* audio_processing */); ASSERT_TRUE(factory_.get() != NULL); - port_allocator_.reset( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); + packet_socket_factory_.reset( + new rtc::BasicPacketSocketFactory(socket_server_.get())); + port_allocator_.reset(new cricket::FakePortAllocator( + rtc::Thread::Current(), packet_socket_factory_.get())); raw_port_allocator_ = port_allocator_.get(); } @@ -178,9 +187,11 @@ class PeerConnectionFactoryTest : public ::testing::Test { EXPECT_GT(codec.clock_rate, 0); } - rtc::AutoThread main_thread_; + std::unique_ptr socket_server_; + rtc::AutoSocketServerThread main_thread_; rtc::scoped_refptr factory_; NullPeerConnectionObserver observer_; + std::unique_ptr packet_socket_factory_; std::unique_ptr port_allocator_; // Since the PC owns the port allocator after it's been initialized, // this should only be used when known to be safe. diff --git a/pc/peer_connection_header_extension_unittest.cc b/pc/peer_connection_header_extension_unittest.cc index 060bb9b1f3..e45a7b2656 100644 --- a/pc/peer_connection_header_extension_unittest.cc +++ b/pc/peer_connection_header_extension_unittest.cc @@ -36,6 +36,7 @@ #include "pc/peer_connection_wrapper.h" #include "pc/session_description.h" #include "pc/test/mock_peer_connection_observers.h" +#include "rtc_base/internal/default_socket_server.h" #include "rtc_base/rtc_certificate_generator.h" #include "rtc_base/strings/string_builder.h" #include "rtc_base/thread.h" @@ -55,7 +56,9 @@ class PeerConnectionHeaderExtensionTest std::tuple> { protected: PeerConnectionHeaderExtensionTest() - : extensions_( + : socket_server_(rtc::CreateDefaultSocketServer()), + main_thread_(socket_server_.get()), + extensions_( {RtpHeaderExtensionCapability("uri1", 1, RtpTransceiverDirection::kStopped), @@ -96,7 +99,8 @@ class PeerConnectionHeaderExtensionTest CreateModularPeerConnectionFactory(std::move(factory_dependencies)); auto fake_port_allocator = std::make_unique( - rtc::Thread::Current(), nullptr); + rtc::Thread::Current(), + std::make_unique(socket_server_.get())); auto observer = std::make_unique(); PeerConnectionInterface::RTCConfiguration config; if (semantics) @@ -111,7 +115,8 @@ class PeerConnectionHeaderExtensionTest pc_factory, result.MoveValue(), std::move(observer)); } - rtc::AutoThread main_thread_; + std::unique_ptr socket_server_; + rtc::AutoSocketServerThread main_thread_; std::vector extensions_; }; diff --git a/pc/peer_connection_ice_unittest.cc b/pc/peer_connection_ice_unittest.cc index e3d857deae..9a822be62c 100644 --- a/pc/peer_connection_ice_unittest.cc +++ b/pc/peer_connection_ice_unittest.cc @@ -1405,6 +1405,11 @@ INSTANTIATE_TEST_SUITE_P(PeerConnectionIceTest, SdpSemantics::kUnifiedPlan)); class PeerConnectionIceConfigTest : public ::testing::Test { + public: + PeerConnectionIceConfigTest() + : socket_server_(rtc::CreateDefaultSocketServer()), + main_thread_(socket_server_.get()) {} + protected: void SetUp() override { pc_factory_ = CreatePeerConnectionFactory( @@ -1415,8 +1420,11 @@ class PeerConnectionIceConfigTest : public ::testing::Test { nullptr /* audio_processing */); } void CreatePeerConnection(const RTCConfiguration& config) { + packet_socket_factory_.reset( + new rtc::BasicPacketSocketFactory(socket_server_.get())); std::unique_ptr port_allocator( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); + new cricket::FakePortAllocator(rtc::Thread::Current(), + packet_socket_factory_.get())); port_allocator_ = port_allocator.get(); PeerConnectionDependencies pc_dependencies(&observer_); pc_dependencies.allocator = std::move(port_allocator); @@ -1426,9 +1434,11 @@ class PeerConnectionIceConfigTest : public ::testing::Test { pc_ = result.MoveValue(); } - rtc::AutoThread main_thread_; + std::unique_ptr socket_server_; + rtc::AutoSocketServerThread main_thread_; rtc::scoped_refptr pc_factory_ = nullptr; rtc::scoped_refptr pc_ = nullptr; + std::unique_ptr packet_socket_factory_; cricket::FakePortAllocator* port_allocator_ = nullptr; MockPeerConnectionObserver observer_; diff --git a/pc/peer_connection_interface_unittest.cc b/pc/peer_connection_interface_unittest.cc index 565d89801b..f195b784c9 100644 --- a/pc/peer_connection_interface_unittest.cc +++ b/pc/peer_connection_interface_unittest.cc @@ -728,7 +728,9 @@ class PeerConnectionInterfaceBaseTest : public ::testing::Test { pc_ = nullptr; } std::unique_ptr port_allocator( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); + new cricket::FakePortAllocator( + rtc::Thread::Current(), + std::make_unique(vss_.get()))); port_allocator_ = port_allocator.get(); // Create certificate generator unless DTLS constraint is explicitly set to @@ -1250,6 +1252,8 @@ class PeerConnectionInterfaceBaseTest : public ::testing::Test { } } + rtc::SocketServer* socket_server() const { return vss_.get(); } + std::unique_ptr vss_; rtc::AutoSocketServerThread main_; rtc::scoped_refptr fake_audio_capture_module_; @@ -1358,8 +1362,11 @@ TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) { TEST_P(PeerConnectionInterfaceTest, CreatePeerConnectionAppliesNetworkConfigToPortAllocator) { // Create fake port allocator. + std::unique_ptr packet_socket_factory( + new rtc::BasicPacketSocketFactory(socket_server())); std::unique_ptr port_allocator( - new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr)); + new cricket::FakePortAllocator(rtc::Thread::Current(), + packet_socket_factory.get())); cricket::FakePortAllocator* raw_port_allocator = port_allocator.get(); // Create RTCConfiguration with some network-related fields relevant to diff --git a/pc/peer_connection_media_unittest.cc b/pc/peer_connection_media_unittest.cc index 4973aed7b3..fab926522f 100644 --- a/pc/peer_connection_media_unittest.cc +++ b/pc/peer_connection_media_unittest.cc @@ -137,7 +137,8 @@ class PeerConnectionMediaBaseTest : public ::testing::Test { CreateModularPeerConnectionFactory(std::move(factory_dependencies)); auto fake_port_allocator = std::make_unique( - rtc::Thread::Current(), nullptr); + rtc::Thread::Current(), + std::make_unique(vss_.get())); auto observer = std::make_unique(); auto modified_config = config; modified_config.sdp_semantics = sdp_semantics_; diff --git a/pc/rtc_stats_integrationtest.cc b/pc/rtc_stats_integrationtest.cc index 9d2328a56d..eccc226327 100644 --- a/pc/rtc_stats_integrationtest.cc +++ b/pc/rtc_stats_integrationtest.cc @@ -111,9 +111,11 @@ class RTCStatsIntegrationTest : public ::testing::Test { RTC_CHECK(worker_thread_->Start()); caller_ = rtc::make_ref_counted( - "caller", network_thread_.get(), worker_thread_.get()); + "caller", &virtual_socket_server_, network_thread_.get(), + worker_thread_.get()); callee_ = rtc::make_ref_counted( - "callee", network_thread_.get(), worker_thread_.get()); + "callee", &virtual_socket_server_, network_thread_.get(), + worker_thread_.get()); } void StartCall() { diff --git a/pc/test/peer_connection_test_wrapper.cc b/pc/test/peer_connection_test_wrapper.cc index d814b323c6..21396104dd 100644 --- a/pc/test/peer_connection_test_wrapper.cc +++ b/pc/test/peer_connection_test_wrapper.cc @@ -75,9 +75,11 @@ void PeerConnectionTestWrapper::Connect(PeerConnectionTestWrapper* caller, PeerConnectionTestWrapper::PeerConnectionTestWrapper( const std::string& name, + rtc::SocketServer* socket_server, rtc::Thread* network_thread, rtc::Thread* worker_thread) : name_(name), + socket_server_(socket_server), network_thread_(network_thread), worker_thread_(worker_thread), pending_negotiation_(false) { @@ -100,7 +102,9 @@ bool PeerConnectionTestWrapper::CreatePc( rtc::scoped_refptr audio_encoder_factory, rtc::scoped_refptr audio_decoder_factory) { std::unique_ptr port_allocator( - new cricket::FakePortAllocator(network_thread_, nullptr)); + new cricket::FakePortAllocator( + network_thread_, + std::make_unique(socket_server_))); RTC_DCHECK_RUN_ON(&pc_thread_checker_); diff --git a/pc/test/peer_connection_test_wrapper.h b/pc/test/peer_connection_test_wrapper.h index cecd3ae8dc..6d3e7c77ec 100644 --- a/pc/test/peer_connection_test_wrapper.h +++ b/pc/test/peer_connection_test_wrapper.h @@ -40,6 +40,7 @@ class PeerConnectionTestWrapper PeerConnectionTestWrapper* callee); PeerConnectionTestWrapper(const std::string& name, + rtc::SocketServer* socket_server, rtc::Thread* network_thread, rtc::Thread* worker_thread); virtual ~PeerConnectionTestWrapper(); @@ -116,6 +117,7 @@ class PeerConnectionTestWrapper bool video); std::string name_; + rtc::SocketServer* const socket_server_; rtc::Thread* const network_thread_; rtc::Thread* const worker_thread_; webrtc::SequenceChecker pc_thread_checker_;