Add support to not use turn server as stun server.

If a stun server is already there, the benefit of adding turn servers as stun servers is small,
and it may create unnecessary stun candidates.

Bug: webrtc:11059
Change-Id: Ia37b43b787180af4d91c1c07c866ccbf1db80262
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158680
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29644}
diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc
index b49e2f8..b2cc99a 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 @@
     : 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 @@
       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 10188ba..274b89d 100644
--- a/p2p/client/basic_port_allocator.h
+++ b/p2p/client/basic_port_allocator.h
@@ -290,6 +290,7 @@
   ServerAddresses stun_servers;
   std::string username;
   std::string password;
+  bool use_turn_server_as_stun_server_disabled = false;
 
   typedef std::vector<RelayServerConfig> RelayList;
   RelayList relays;
diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc
index 31877ff..1822432 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 @@
     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 @@
     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 @@
   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