Relanding: Implement RTCConfiguration.iceCandidatePoolSize.

Depends on this CL in order to work in Chromium:
https://codereview.chromium.org/1976673002/

It works by creating pooled PortAllocatorSessions which can be picked up
by a P2PTransportChannel when needed (after a local description is set).

This can optimize candidate gathering time when there is some time between
creating a PeerConnection and setting a local description.

R=pthatcher@webrtc.org

Committed: https://chromium.googlesource.com/external/webrtc/+/48e9d05f510b1616c81303944008f75825971802

Review URL: https://codereview.webrtc.org/1956453003 .

Cr-Original-Commit-Position: refs/heads/master@{#12729}
Cr-Mirrored-From: https://chromium.googlesource.com/external/webrtc
Cr-Mirrored-Commit: a1c303535fe7a29b87879047996efa2952f9701b
diff --git a/api/peerconnection.cc b/api/peerconnection.cc
index 506a215..5284db3 100644
--- a/api/peerconnection.cc
+++ b/api/peerconnection.cc
@@ -376,6 +376,23 @@
   }
 }
 
+uint32_t ConvertIceTransportTypeToCandidateFilter(
+    PeerConnectionInterface::IceTransportsType type) {
+  switch (type) {
+    case PeerConnectionInterface::kNone:
+      return cricket::CF_NONE;
+    case PeerConnectionInterface::kRelay:
+      return cricket::CF_RELAY;
+    case PeerConnectionInterface::kNoHost:
+      return (cricket::CF_ALL & ~cricket::CF_HOST);
+    case PeerConnectionInterface::kAll:
+      return cricket::CF_ALL;
+    default:
+      ASSERT(false);
+  }
+  return cricket::CF_NONE;
+}
+
 }  // namespace
 
 namespace webrtc {
@@ -536,6 +553,14 @@
   for (const auto& receiver : receivers_) {
     receiver->Stop();
   }
+  // Destroy stats_ because it depends on session_.
+  stats_.reset(nullptr);
+  // Now destroy session_ before destroying other members,
+  // because its destruction fires signals (such as VoiceChannelDestroyed)
+  // which will trigger some final actions in PeerConnection...
+  session_.reset(nullptr);
+  // port_allocator_ lives on the worker thread and should be destroyed there.
+  worker_thread()->Invoke<void>([this] { port_allocator_.reset(nullptr); });
 }
 
 bool PeerConnection::Initialize(
@@ -552,35 +577,12 @@
 
   port_allocator_ = std::move(allocator);
 
-  cricket::ServerAddresses stun_servers;
-  std::vector<cricket::RelayServerConfig> turn_servers;
-  if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
+  // The port allocator lives on the worker thread and should be initialized
+  // there.
+  if (!worker_thread()->Invoke<bool>(rtc::Bind(
+          &PeerConnection::InitializePortAllocator_w, this, configuration))) {
     return false;
   }
-  port_allocator_->SetIceServers(stun_servers, turn_servers);
-
-  // To handle both internal and externally created port allocator, we will
-  // enable BUNDLE here.
-  int portallocator_flags = port_allocator_->flags();
-  portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
-                         cricket::PORTALLOCATOR_ENABLE_IPV6;
-  // If the disable-IPv6 flag was specified, we'll not override it
-  // by experiment.
-  if (configuration.disable_ipv6) {
-    portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
-  } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") ==
-             "Disabled") {
-    portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
-  }
-
-  if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
-    portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
-    LOG(LS_INFO) << "TCP candidates are disabled.";
-  }
-
-  port_allocator_->set_flags(portallocator_flags);
-  // No step delay is used while allocating ports.
-  port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
 
   media_controller_.reset(
       factory_->CreateMediaController(configuration.media_config));
@@ -1158,18 +1160,19 @@
   signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
 }
 
-bool PeerConnection::SetConfiguration(const RTCConfiguration& config) {
+bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration) {
   TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
   if (port_allocator_) {
-    cricket::ServerAddresses stun_servers;
-    std::vector<cricket::RelayServerConfig> turn_servers;
-    if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) {
+    if (!worker_thread()->Invoke<bool>(
+            rtc::Bind(&PeerConnection::ReconfigurePortAllocator_w, this,
+                      configuration))) {
       return false;
     }
-    port_allocator_->SetIceServers(stun_servers, turn_servers);
   }
-  session_->SetIceConfig(session_->ParseIceConfig(config));
-  return session_->SetIceTransports(config.type);
+
+  // TODO(deadbeef): Shouldn't have to hop to the worker thread twice...
+  session_->SetIceConfig(session_->ParseIceConfig(configuration));
+  return true;
 }
 
 bool PeerConnection::AddIceCandidate(
@@ -2084,4 +2087,60 @@
   return nullptr;
 }
 
+bool PeerConnection::InitializePortAllocator_w(
+    const RTCConfiguration& configuration) {
+  cricket::ServerAddresses stun_servers;
+  std::vector<cricket::RelayServerConfig> turn_servers;
+  if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
+    return false;
+  }
+
+  // To handle both internal and externally created port allocator, we will
+  // enable BUNDLE here.
+  int portallocator_flags = port_allocator_->flags();
+  portallocator_flags |= cricket::PORTALLOCATOR_ENABLE_SHARED_SOCKET |
+                         cricket::PORTALLOCATOR_ENABLE_IPV6;
+  // If the disable-IPv6 flag was specified, we'll not override it
+  // by experiment.
+  if (configuration.disable_ipv6) {
+    portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
+  } else if (webrtc::field_trial::FindFullName("WebRTC-IPv6Default") ==
+             "Disabled") {
+    portallocator_flags &= ~(cricket::PORTALLOCATOR_ENABLE_IPV6);
+  }
+
+  if (configuration.tcp_candidate_policy == kTcpCandidatePolicyDisabled) {
+    portallocator_flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
+    LOG(LS_INFO) << "TCP candidates are disabled.";
+  }
+
+  port_allocator_->set_flags(portallocator_flags);
+  // No step delay is used while allocating ports.
+  port_allocator_->set_step_delay(cricket::kMinimumStepDelay);
+  port_allocator_->set_candidate_filter(
+      ConvertIceTransportTypeToCandidateFilter(configuration.type));
+
+  // Call this last since it may create pooled allocator sessions using the
+  // properties set above.
+  port_allocator_->SetConfiguration(stun_servers, turn_servers,
+                                    configuration.ice_candidate_pool_size);
+  return true;
+}
+
+bool PeerConnection::ReconfigurePortAllocator_w(
+    const RTCConfiguration& configuration) {
+  cricket::ServerAddresses stun_servers;
+  std::vector<cricket::RelayServerConfig> turn_servers;
+  if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
+    return false;
+  }
+  port_allocator_->set_candidate_filter(
+      ConvertIceTransportTypeToCandidateFilter(configuration.type));
+  // Call this last since it may create pooled allocator sessions using the
+  // candidate filter set above.
+  port_allocator_->SetConfiguration(stun_servers, turn_servers,
+                                    configuration.ice_candidate_pool_size);
+  return true;
+}
+
 }  // namespace webrtc
diff --git a/api/peerconnection.h b/api/peerconnection.h
index 862c6fb..15ecc3f 100644
--- a/api/peerconnection.h
+++ b/api/peerconnection.h
@@ -131,7 +131,7 @@
   void SetRemoteDescription(SetSessionDescriptionObserver* observer,
                             SessionDescriptionInterface* desc) override;
   bool SetConfiguration(
-      const PeerConnectionInterface::RTCConfiguration& config) override;
+      const PeerConnectionInterface::RTCConfiguration& configuration) override;
   bool AddIceCandidate(const IceCandidateInterface* candidate) override;
   bool RemoveIceCandidates(
       const std::vector<cricket::Candidate>& candidates) override;
@@ -210,6 +210,8 @@
     return factory_->signaling_thread();
   }
 
+  rtc::Thread* worker_thread() const { return factory_->worker_thread(); }
+
   void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
                                         const std::string& error);
   void PostCreateSessionDescriptionFailure(
@@ -351,6 +353,12 @@
   // or nullptr if not found.
   DataChannel* FindDataChannelBySid(int sid) const;
 
+  // Called when first configuring the port allocator.
+  bool InitializePortAllocator_w(const RTCConfiguration& configuration);
+  // Called when SetConfiguration is called. Only a subset of the configuration
+  // is applied.
+  bool ReconfigurePortAllocator_w(const RTCConfiguration& configuration);
+
   // Storing the factory as a scoped reference pointer ensures that the memory
   // in the PeerConnectionFactoryImpl remains available as long as the
   // PeerConnection is running. It is passed to PeerConnection as a raw pointer.
@@ -397,11 +405,7 @@
   std::vector<rtc::scoped_refptr<RtpSenderInterface>> senders_;
   std::vector<rtc::scoped_refptr<RtpReceiverInterface>> receivers_;
 
-  // The session_ unique_ptr is declared at the bottom of PeerConnection
-  // because its destruction fires signals (such as VoiceChannelDestroyed)
-  // which will trigger some final actions in PeerConnection...
   std::unique_ptr<WebRtcSession> session_;
-  // ... But stats_ depends on session_ so it should be destroyed even earlier.
   std::unique_ptr<StatsCollector> stats_;
 };
 
diff --git a/api/peerconnection_unittest.cc b/api/peerconnection_unittest.cc
index 521486f..1c3d073 100644
--- a/api/peerconnection_unittest.cc
+++ b/api/peerconnection_unittest.cc
@@ -37,9 +37,9 @@
 #include "webrtc/base/thread.h"
 #include "webrtc/base/virtualsocketserver.h"
 #include "webrtc/media/engine/fakewebrtcvideoengine.h"
+#include "webrtc/p2p/base/fakeportallocator.h"
 #include "webrtc/p2p/base/p2pconstants.h"
 #include "webrtc/p2p/base/sessiondescription.h"
-#include "webrtc/p2p/client/fakeportallocator.h"
 #include "webrtc/pc/mediasession.h"
 
 #define MAYBE_SKIP_TEST(feature)                    \
diff --git a/api/peerconnectionfactory.cc b/api/peerconnectionfactory.cc
index 8c8fb6f..5d8f79c 100644
--- a/api/peerconnectionfactory.cc
+++ b/api/peerconnectionfactory.cc
@@ -283,7 +283,9 @@
     allocator.reset(new cricket::BasicPortAllocator(
         default_network_manager_.get(), default_socket_factory_.get()));
   }
-  allocator->SetNetworkIgnoreMask(options_.network_ignore_mask);
+  worker_thread_->Invoke<void>(
+      rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, allocator.get(),
+                options_.network_ignore_mask));
 
   rtc::scoped_refptr<PeerConnection> pc(
       new rtc::RefCountedObject<PeerConnection>(this));
diff --git a/api/peerconnectionfactory_unittest.cc b/api/peerconnectionfactory_unittest.cc
index 833242a..963f1fe 100644
--- a/api/peerconnectionfactory_unittest.cc
+++ b/api/peerconnectionfactory_unittest.cc
@@ -24,7 +24,7 @@
 #include "webrtc/media/base/fakevideocapturer.h"
 #include "webrtc/media/engine/webrtccommon.h"
 #include "webrtc/media/engine/webrtcvoe.h"
-#include "webrtc/p2p/client/fakeportallocator.h"
+#include "webrtc/p2p/base/fakeportallocator.h"
 
 using webrtc::DataChannelInterface;
 using webrtc::DtlsIdentityStoreInterface;
diff --git a/api/peerconnectioninterface.h b/api/peerconnectioninterface.h
index 94d2c00..fe0dc1d 100644
--- a/api/peerconnectioninterface.h
+++ b/api/peerconnectioninterface.h
@@ -270,42 +270,30 @@
     static const int kAudioJitterBufferMaxPackets = 50;
     // TODO(pthatcher): Rename this ice_transport_type, but update
     // Chromium at the same time.
-    IceTransportsType type;
+    IceTransportsType type = kAll;
     // TODO(pthatcher): Rename this ice_servers, but update Chromium
     // at the same time.
     IceServers servers;
-    BundlePolicy bundle_policy;
-    RtcpMuxPolicy rtcp_mux_policy;
-    TcpCandidatePolicy tcp_candidate_policy;
-    int audio_jitter_buffer_max_packets;
-    bool audio_jitter_buffer_fast_accelerate;
-    int ice_connection_receiving_timeout;         // ms
-    int ice_backup_candidate_pair_ping_interval;  // ms
-    ContinualGatheringPolicy continual_gathering_policy;
+    BundlePolicy bundle_policy = kBundlePolicyBalanced;
+    RtcpMuxPolicy rtcp_mux_policy = kRtcpMuxPolicyNegotiate;
+    TcpCandidatePolicy tcp_candidate_policy = kTcpCandidatePolicyEnabled;
+    int audio_jitter_buffer_max_packets = kAudioJitterBufferMaxPackets;
+    bool audio_jitter_buffer_fast_accelerate = false;
+    int ice_connection_receiving_timeout = kUndefined;         // ms
+    int ice_backup_candidate_pair_ping_interval = kUndefined;  // ms
+    ContinualGatheringPolicy continual_gathering_policy = GATHER_ONCE;
     std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
-    bool prioritize_most_likely_ice_candidate_pairs;
+    bool prioritize_most_likely_ice_candidate_pairs = false;
     struct cricket::MediaConfig media_config;
     // Flags corresponding to values set by constraint flags.
     // rtc::Optional flags can be "missing", in which case the webrtc
     // default applies.
-    bool disable_ipv6;
-    bool enable_rtp_data_channel;
+    bool disable_ipv6 = false;
+    bool enable_rtp_data_channel = false;
     rtc::Optional<int> screencast_min_bitrate;
     rtc::Optional<bool> combined_audio_video_bwe;
     rtc::Optional<bool> enable_dtls_srtp;
-    RTCConfiguration()
-        : type(kAll),
-          bundle_policy(kBundlePolicyBalanced),
-          rtcp_mux_policy(kRtcpMuxPolicyNegotiate),
-          tcp_candidate_policy(kTcpCandidatePolicyEnabled),
-          audio_jitter_buffer_max_packets(kAudioJitterBufferMaxPackets),
-          audio_jitter_buffer_fast_accelerate(false),
-          ice_connection_receiving_timeout(kUndefined),
-          ice_backup_candidate_pair_ping_interval(kUndefined),
-          continual_gathering_policy(GATHER_ONCE),
-          prioritize_most_likely_ice_candidate_pairs(false),
-          disable_ipv6(false),
-          enable_rtp_data_channel(false) {}
+    int ice_candidate_pool_size = 0;
   };
 
   struct RTCOfferAnswerOptions {
diff --git a/api/peerconnectioninterface_unittest.cc b/api/peerconnectioninterface_unittest.cc
index 795a66d..68c932b 100644
--- a/api/peerconnectioninterface_unittest.cc
+++ b/api/peerconnectioninterface_unittest.cc
@@ -39,7 +39,7 @@
 #include "webrtc/base/thread.h"
 #include "webrtc/media/base/fakevideocapturer.h"
 #include "webrtc/media/sctp/sctpdataengine.h"
-#include "webrtc/p2p/client/fakeportallocator.h"
+#include "webrtc/p2p/base/fakeportallocator.h"
 #include "webrtc/pc/mediasession.h"
 
 static const char kStreamLabel1[] = "local_stream_1";
@@ -551,24 +551,33 @@
   }
 
   void CreatePeerConnection() {
-    CreatePeerConnection("", "", NULL);
+    CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
   }
 
   void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
-    CreatePeerConnection("", "", constraints);
+    CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
+                         constraints);
   }
 
-  void CreatePeerConnection(const std::string& uri,
-                            const std::string& password,
-                            webrtc::MediaConstraintsInterface* constraints) {
+  void CreatePeerConnectionWithIceTransportsType(
+      PeerConnectionInterface::IceTransportsType type) {
+    PeerConnectionInterface::RTCConfiguration config;
+    config.type = type;
+    return CreatePeerConnection(config, nullptr);
+  }
+
+  void CreatePeerConnectionWithIceServer(const std::string& uri,
+                                         const std::string& password) {
     PeerConnectionInterface::RTCConfiguration config;
     PeerConnectionInterface::IceServer server;
-    if (!uri.empty()) {
-      server.uri = uri;
-      server.password = password;
-      config.servers.push_back(server);
-    }
+    server.uri = uri;
+    server.password = password;
+    config.servers.push_back(server);
+    CreatePeerConnection(config, nullptr);
+  }
 
+  void CreatePeerConnection(PeerConnectionInterface::RTCConfiguration config,
+                            webrtc::MediaConstraintsInterface* constraints) {
     std::unique_ptr<cricket::FakePortAllocator> port_allocator(
         new cricket::FakePortAllocator(rtc::Thread::Current(), nullptr));
     port_allocator_ = port_allocator.get();
@@ -613,7 +622,7 @@
   }
 
   void CreatePeerConnectionWithDifferentConfigurations() {
-    CreatePeerConnection(kStunAddressOnly, "", NULL);
+    CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
     EXPECT_EQ(1u, port_allocator_->stun_servers().size());
     EXPECT_EQ(0u, port_allocator_->turn_servers().size());
     EXPECT_EQ("address", port_allocator_->stun_servers().begin()->hostname());
@@ -624,7 +633,7 @@
     CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
     CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
 
-    CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
+    CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
     EXPECT_EQ(0u, port_allocator_->stun_servers().size());
     EXPECT_EQ(1u, port_allocator_->turn_servers().size());
     EXPECT_EQ(kTurnUsername,
@@ -1014,6 +1023,44 @@
   CreatePeerConnectionWithDifferentConfigurations();
 }
 
+TEST_F(PeerConnectionInterfaceTest,
+       CreatePeerConnectionWithDifferentIceTransportsTypes) {
+  CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNone);
+  EXPECT_EQ(cricket::CF_NONE, port_allocator_->candidate_filter());
+  CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kRelay);
+  EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
+  CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kNoHost);
+  EXPECT_EQ(cricket::CF_ALL & ~cricket::CF_HOST,
+            port_allocator_->candidate_filter());
+  CreatePeerConnectionWithIceTransportsType(PeerConnectionInterface::kAll);
+  EXPECT_EQ(cricket::CF_ALL, port_allocator_->candidate_filter());
+}
+
+// Test that when a PeerConnection is created with a nonzero candidate pool
+// size, the pooled PortAllocatorSession is created with all the attributes
+// in the RTCConfiguration.
+TEST_F(PeerConnectionInterfaceTest, CreatePeerConnectionWithPooledCandidates) {
+  PeerConnectionInterface::RTCConfiguration config;
+  PeerConnectionInterface::IceServer server;
+  server.uri = kStunAddressOnly;
+  config.servers.push_back(server);
+  config.type = PeerConnectionInterface::kRelay;
+  config.disable_ipv6 = true;
+  config.tcp_candidate_policy =
+      PeerConnectionInterface::kTcpCandidatePolicyDisabled;
+  config.ice_candidate_pool_size = 1;
+  CreatePeerConnection(config, nullptr);
+
+  const cricket::FakePortAllocatorSession* session =
+      static_cast<const cricket::FakePortAllocatorSession*>(
+          port_allocator_->GetPooledSession());
+  ASSERT_NE(nullptr, session);
+  EXPECT_EQ(1UL, session->stun_servers().size());
+  EXPECT_EQ(0U, session->flags() & cricket::PORTALLOCATOR_ENABLE_IPV6);
+  EXPECT_LT(0U, session->flags() & cricket::PORTALLOCATOR_DISABLE_TCP);
+  EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
+}
+
 TEST_F(PeerConnectionInterfaceTest, AddStreams) {
   CreatePeerConnection();
   AddVideoStream(kStreamLabel1);
@@ -1907,6 +1954,35 @@
             port_allocator_->stun_servers().begin()->hostname());
 }
 
+TEST_F(PeerConnectionInterfaceTest, SetConfigurationChangesCandidateFilter) {
+  CreatePeerConnection();
+  PeerConnectionInterface::RTCConfiguration config;
+  config.type = PeerConnectionInterface::kRelay;
+  EXPECT_TRUE(pc_->SetConfiguration(config));
+  EXPECT_EQ(cricket::CF_RELAY, port_allocator_->candidate_filter());
+}
+
+// Test that when SetConfiguration changes both the pool size and other
+// attributes, the pooled session is created with the updated attributes.
+TEST_F(PeerConnectionInterfaceTest,
+       SetConfigurationCreatesPooledSessionCorrectly) {
+  CreatePeerConnection();
+  PeerConnectionInterface::RTCConfiguration config;
+  config.ice_candidate_pool_size = 1;
+  PeerConnectionInterface::IceServer server;
+  server.uri = kStunAddressOnly;
+  config.servers.push_back(server);
+  config.type = PeerConnectionInterface::kRelay;
+  CreatePeerConnection(config, nullptr);
+
+  const cricket::FakePortAllocatorSession* session =
+      static_cast<const cricket::FakePortAllocatorSession*>(
+          port_allocator_->GetPooledSession());
+  ASSERT_NE(nullptr, session);
+  EXPECT_EQ(1UL, session->stun_servers().size());
+  EXPECT_EQ(cricket::CF_RELAY, session->candidate_filter());
+}
+
 // Test that PeerConnection::Close changes the states to closed and all remote
 // tracks change state to ended.
 TEST_F(PeerConnectionInterfaceTest, CloseAndTestStreamsAndStates) {
diff --git a/api/test/peerconnectiontestwrapper.cc b/api/test/peerconnectiontestwrapper.cc
index 717c48a..1ec2b47 100644
--- a/api/test/peerconnectiontestwrapper.cc
+++ b/api/test/peerconnectiontestwrapper.cc
@@ -15,7 +15,7 @@
 #include "webrtc/api/test/mockpeerconnectionobservers.h"
 #include "webrtc/api/test/peerconnectiontestwrapper.h"
 #include "webrtc/base/gunit.h"
-#include "webrtc/p2p/client/fakeportallocator.h"
+#include "webrtc/p2p/base/fakeportallocator.h"
 
 static const char kStreamLabelBase[] = "stream_label";
 static const char kVideoTrackLabelBase[] = "video_track";
diff --git a/api/webrtcsession.cc b/api/webrtcsession.cc
index c3ea1c8..2b146c6 100644
--- a/api/webrtcsession.cc
+++ b/api/webrtcsession.cc
@@ -421,22 +421,6 @@
   return MakeErrorString(kPushDownTDFailed, desc);
 }
 
-uint32_t ConvertIceTransportTypeToCandidateFilter(
-    PeerConnectionInterface::IceTransportsType type) {
-  switch (type) {
-    case PeerConnectionInterface::kNone:
-        return cricket::CF_NONE;
-    case PeerConnectionInterface::kRelay:
-        return cricket::CF_RELAY;
-    case PeerConnectionInterface::kNoHost:
-        return (cricket::CF_ALL & ~cricket::CF_HOST);
-    case PeerConnectionInterface::kAll:
-        return cricket::CF_ALL;
-    default: ASSERT(false);
-  }
-  return cricket::CF_NONE;
-}
-
 // Returns true if |new_desc| requests an ICE restart (i.e., new ufrag/pwd).
 bool CheckForRemoteIceRestart(const SessionDescriptionInterface* old_desc,
                               const SessionDescriptionInterface* new_desc,
@@ -475,7 +459,6 @@
                              cricket::PortAllocator* port_allocator)
     : signaling_thread_(signaling_thread),
       worker_thread_(worker_thread),
-      port_allocator_(port_allocator),
       // RFC 3264: The numeric value of the session id and version in the
       // o line MUST be representable with a "64 bit signed integer".
       // Due to this constraint session id |sid_| is max limited to LLONG_MAX.
@@ -604,8 +587,6 @@
   if (options.disable_encryption) {
     webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
   }
-  port_allocator()->set_candidate_filter(
-      ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
 
   return true;
 }
@@ -1145,12 +1126,6 @@
   return true;
 }
 
-bool WebRtcSession::SetIceTransports(
-    PeerConnectionInterface::IceTransportsType type) {
-  return port_allocator()->set_candidate_filter(
-        ConvertIceTransportTypeToCandidateFilter(type));
-}
-
 cricket::IceConfig WebRtcSession::ParseIceConfig(
     const PeerConnectionInterface::RTCConfiguration& config) const {
   cricket::IceConfig ice_config;
diff --git a/api/webrtcsession.h b/api/webrtcsession.h
index 1408b22..8a32d78f 100644
--- a/api/webrtcsession.h
+++ b/api/webrtcsession.h
@@ -146,7 +146,6 @@
   // These are const to allow them to be called from const methods.
   rtc::Thread* signaling_thread() const { return signaling_thread_; }
   rtc::Thread* worker_thread() const { return worker_thread_; }
-  cricket::PortAllocator* port_allocator() const { return port_allocator_; }
 
   // The ID of this session.
   const std::string& id() const { return sid_; }
@@ -214,8 +213,6 @@
   bool RemoveRemoteIceCandidates(
       const std::vector<cricket::Candidate>& candidates);
 
-  bool SetIceTransports(PeerConnectionInterface::IceTransportsType type);
-
   cricket::IceConfig ParseIceConfig(
       const PeerConnectionInterface::RTCConfiguration& config) const;
 
@@ -469,7 +466,6 @@
 
   rtc::Thread* const signaling_thread_;
   rtc::Thread* const worker_thread_;
-  cricket::PortAllocator* const port_allocator_;
 
   State state_ = STATE_INIT;
   Error error_ = ERROR_NONE;
diff --git a/api/webrtcsession_unittest.cc b/api/webrtcsession_unittest.cc
index 45ffc36..cd5e784 100644
--- a/api/webrtcsession_unittest.cc
+++ b/api/webrtcsession_unittest.cc
@@ -407,12 +407,6 @@
 
   void Init() { Init(nullptr); }
 
-  void InitWithIceTransport(
-      PeerConnectionInterface::IceTransportsType ice_transport_type) {
-    configuration_.type = ice_transport_type;
-    Init();
-  }
-
   void InitWithBundlePolicy(
       PeerConnectionInterface::BundlePolicy bundle_policy) {
     configuration_.bundle_policy = bundle_policy;
@@ -1529,50 +1523,6 @@
   EXPECT_EQ(6u, observer_.mline_1_candidates_.size());
 }
 
-// Test session delivers no candidates gathered when constraint set to "none".
-TEST_F(WebRtcSessionTest, TestIceTransportsNone) {
-  AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
-  InitWithIceTransport(PeerConnectionInterface::kNone);
-  SendAudioVideoStream1();
-  InitiateCall();
-  EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
-  EXPECT_EQ(0u, observer_.mline_0_candidates_.size());
-  EXPECT_EQ(0u, observer_.mline_1_candidates_.size());
-}
-
-// Test session delivers only relay candidates gathered when constaint set to
-// "relay".
-TEST_F(WebRtcSessionTest, TestIceTransportsRelay) {
-  AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
-  ConfigureAllocatorWithTurn();
-  InitWithIceTransport(PeerConnectionInterface::kRelay);
-  SendAudioVideoStream1();
-  InitiateCall();
-  EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
-  EXPECT_EQ(2u, observer_.mline_0_candidates_.size());
-  EXPECT_EQ(2u, observer_.mline_1_candidates_.size());
-  for (size_t i = 0; i < observer_.mline_0_candidates_.size(); ++i) {
-    EXPECT_EQ(cricket::RELAY_PORT_TYPE,
-              observer_.mline_0_candidates_[i].type());
-  }
-  for (size_t i = 0; i < observer_.mline_1_candidates_.size(); ++i) {
-    EXPECT_EQ(cricket::RELAY_PORT_TYPE,
-              observer_.mline_1_candidates_[i].type());
-  }
-}
-
-// Test session delivers all candidates gathered when constaint set to "all".
-TEST_F(WebRtcSessionTest, TestIceTransportsAll) {
-  AddInterface(rtc::SocketAddress(kClientAddrHost1, kClientAddrPort));
-  InitWithIceTransport(PeerConnectionInterface::kAll);
-  SendAudioVideoStream1();
-  InitiateCall();
-  EXPECT_TRUE_WAIT(observer_.oncandidatesready_, kIceCandidatesTimeout);
-  // Host + STUN. By default allocator is disabled to gather relay candidates.
-  EXPECT_EQ(4u, observer_.mline_0_candidates_.size());
-  EXPECT_EQ(4u, observer_.mline_1_candidates_.size());
-}
-
 TEST_F(WebRtcSessionTest, SetSdpFailedOnInvalidSdp) {
   Init();
   SessionDescriptionInterface* offer = NULL;