diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc index b49e2f842b..b2cc99a2b7 100644 --- a/p2p/client/basic_port_allocator.cc +++ b/p2p/client/basic_port_allocator.cc @@ -28,6 +28,7 @@ #include "rtc_base/checks.h" #include "rtc_base/helpers.h" #include "rtc_base/logging.h" +#include "system_wrappers/include/field_trial.h" #include "system_wrappers/include/metrics.h" using rtc::CreateRandomId; @@ -1699,6 +1700,9 @@ PortConfiguration::PortConfiguration(const ServerAddresses& stun_servers, : stun_servers(stun_servers), username(username), password(password) { if (!stun_servers.empty()) stun_address = *(stun_servers.begin()); + // Note that this won't change once the config is initialized. + use_turn_server_as_stun_server_disabled = + webrtc::field_trial::IsDisabled("WebRTC-UseTurnServerAsStunServer"); } PortConfiguration::~PortConfiguration() = default; @@ -1708,7 +1712,14 @@ ServerAddresses PortConfiguration::StunServers() { stun_servers.find(stun_address) == stun_servers.end()) { stun_servers.insert(stun_address); } - // Every UDP TURN server should also be used as a STUN server. + + if (!stun_servers.empty() && use_turn_server_as_stun_server_disabled) { + return stun_servers; + } + + // Every UDP TURN server should also be used as a STUN server if + // use_turn_server_as_stun_server is not disabled or the stun servers are + // empty. ServerAddresses turn_servers = GetRelayServerAddresses(RELAY_TURN, PROTO_UDP); for (const rtc::SocketAddress& turn_server : turn_servers) { if (stun_servers.find(turn_server) == stun_servers.end()) { diff --git a/p2p/client/basic_port_allocator.h b/p2p/client/basic_port_allocator.h index 10188bafca..274b89da48 100644 --- a/p2p/client/basic_port_allocator.h +++ b/p2p/client/basic_port_allocator.h @@ -290,6 +290,7 @@ struct RTC_EXPORT PortConfiguration : public rtc::MessageData { ServerAddresses stun_servers; std::string username; std::string password; + bool use_turn_server_as_stun_server_disabled = false; typedef std::vector RelayList; RelayList relays; diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc index 31877ff8ab..1822432686 100644 --- a/p2p/client/basic_port_allocator_unittest.cc +++ b/p2p/client/basic_port_allocator_unittest.cc @@ -43,6 +43,7 @@ #include "rtc_base/thread.h" #include "rtc_base/virtual_socket_server.h" #include "system_wrappers/include/metrics.h" +#include "test/field_trial.h" #include "test/gmock.h" #include "test/gtest.h" @@ -215,8 +216,8 @@ class BasicPortAllocatorTestBase : public ::testing::Test, AddTurnServers(udp_turn, tcp_turn); } - void AddTurnServers(const rtc::SocketAddress& udp_turn, - const rtc::SocketAddress& tcp_turn) { + RelayServerConfig CreateTurnServers(const rtc::SocketAddress& udp_turn, + const rtc::SocketAddress& tcp_turn) { RelayServerConfig turn_server(RELAY_TURN); RelayCredentials credentials(kTurnUsername, kTurnPassword); turn_server.credentials = credentials; @@ -227,6 +228,12 @@ class BasicPortAllocatorTestBase : public ::testing::Test, if (!tcp_turn.IsNil()) { turn_server.ports.push_back(ProtocolAddress(tcp_turn, PROTO_TCP)); } + return turn_server; + } + + void AddTurnServers(const rtc::SocketAddress& udp_turn, + const rtc::SocketAddress& tcp_turn) { + RelayServerConfig turn_server = CreateTurnServers(udp_turn, tcp_turn); allocator_->AddTurnServer(turn_server); } @@ -2428,4 +2435,29 @@ TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) { EXPECT_EQ(2, num_relay_candidates); } +TEST_F(BasicPortAllocatorTest, TestUseTurnServerAsStunSever) { + ServerAddresses stun_servers; + stun_servers.insert(kStunAddr); + PortConfiguration port_config(stun_servers, "", ""); + RelayServerConfig turn_servers = + CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); + port_config.AddRelay(turn_servers); + + EXPECT_EQ(2U, port_config.StunServers().size()); +} + +TEST_F(BasicPortAllocatorTest, TestDoNotUseTurnServerAsStunSever) { + webrtc::test::ScopedFieldTrials field_trials( + "WebRTC-UseTurnServerAsStunServer/Disabled/"); + ServerAddresses stun_servers; + stun_servers.insert(kStunAddr); + PortConfiguration port_config(stun_servers, "" /* user_name */, + "" /* password */); + RelayServerConfig turn_servers = + CreateTurnServers(kTurnUdpIntAddr, kTurnTcpIntAddr); + port_config.AddRelay(turn_servers); + + EXPECT_EQ(1U, port_config.StunServers().size()); +} + } // namespace cricket