Revert of Implement RTCConfiguration.iceCandidatePoolSize. (patchset #7 id:120001 of https://codereview.webrtc.org/1956453003/ )

Reason for revert:
Breaks remoting_unittests. They defined their own operator== which conflicts with this one.

I'll remove the operator== in a roll CL. But until it's approved, I'm reverting this so the FYI bots will pass.

Original issue's description:
> Implement RTCConfiguration.iceCandidatePoolSize.
>
> 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

TBR=pthatcher@webrtc.org,honghaiz@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true

Review-Url: https://codereview.webrtc.org/1972043004
Cr-Commit-Position: refs/heads/master@{#12709}
diff --git a/webrtc/api/peerconnection.cc b/webrtc/api/peerconnection.cc
index 5284db3..506a215 100644
--- a/webrtc/api/peerconnection.cc
+++ b/webrtc/api/peerconnection.cc
@@ -376,23 +376,6 @@
   }
 }
 
-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 {
@@ -553,14 +536,6 @@
   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(
@@ -577,12 +552,35 @@
 
   port_allocator_ = std::move(allocator);
 
-  // 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))) {
+  cricket::ServerAddresses stun_servers;
+  std::vector<cricket::RelayServerConfig> turn_servers;
+  if (!ParseIceServers(configuration.servers, &stun_servers, &turn_servers)) {
     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));
@@ -1160,19 +1158,18 @@
   signaling_thread()->Post(this, MSG_SET_SESSIONDESCRIPTION_SUCCESS, msg);
 }
 
-bool PeerConnection::SetConfiguration(const RTCConfiguration& configuration) {
+bool PeerConnection::SetConfiguration(const RTCConfiguration& config) {
   TRACE_EVENT0("webrtc", "PeerConnection::SetConfiguration");
   if (port_allocator_) {
-    if (!worker_thread()->Invoke<bool>(
-            rtc::Bind(&PeerConnection::ReconfigurePortAllocator_w, this,
-                      configuration))) {
+    cricket::ServerAddresses stun_servers;
+    std::vector<cricket::RelayServerConfig> turn_servers;
+    if (!ParseIceServers(config.servers, &stun_servers, &turn_servers)) {
       return false;
     }
+    port_allocator_->SetIceServers(stun_servers, turn_servers);
   }
-
-  // TODO(deadbeef): Shouldn't have to hop to the worker thread twice...
-  session_->SetIceConfig(session_->ParseIceConfig(configuration));
-  return true;
+  session_->SetIceConfig(session_->ParseIceConfig(config));
+  return session_->SetIceTransports(config.type);
 }
 
 bool PeerConnection::AddIceCandidate(
@@ -2087,60 +2084,4 @@
   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/webrtc/api/peerconnection.h b/webrtc/api/peerconnection.h
index 15ecc3f..862c6fb 100644
--- a/webrtc/api/peerconnection.h
+++ b/webrtc/api/peerconnection.h
@@ -131,7 +131,7 @@
   void SetRemoteDescription(SetSessionDescriptionObserver* observer,
                             SessionDescriptionInterface* desc) override;
   bool SetConfiguration(
-      const PeerConnectionInterface::RTCConfiguration& configuration) override;
+      const PeerConnectionInterface::RTCConfiguration& config) override;
   bool AddIceCandidate(const IceCandidateInterface* candidate) override;
   bool RemoveIceCandidates(
       const std::vector<cricket::Candidate>& candidates) override;
@@ -210,8 +210,6 @@
     return factory_->signaling_thread();
   }
 
-  rtc::Thread* worker_thread() const { return factory_->worker_thread(); }
-
   void PostSetSessionDescriptionFailure(SetSessionDescriptionObserver* observer,
                                         const std::string& error);
   void PostCreateSessionDescriptionFailure(
@@ -353,12 +351,6 @@
   // 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.
@@ -405,7 +397,11 @@
   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/webrtc/api/peerconnection_unittest.cc b/webrtc/api/peerconnection_unittest.cc
index 1c3d073..521486f 100644
--- a/webrtc/api/peerconnection_unittest.cc
+++ b/webrtc/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/webrtc/api/peerconnectionfactory.cc b/webrtc/api/peerconnectionfactory.cc
index 5d8f79c..8c8fb6f 100644
--- a/webrtc/api/peerconnectionfactory.cc
+++ b/webrtc/api/peerconnectionfactory.cc
@@ -283,9 +283,7 @@
     allocator.reset(new cricket::BasicPortAllocator(
         default_network_manager_.get(), default_socket_factory_.get()));
   }
-  worker_thread_->Invoke<void>(
-      rtc::Bind(&cricket::PortAllocator::SetNetworkIgnoreMask, allocator.get(),
-                options_.network_ignore_mask));
+  allocator->SetNetworkIgnoreMask(options_.network_ignore_mask);
 
   rtc::scoped_refptr<PeerConnection> pc(
       new rtc::RefCountedObject<PeerConnection>(this));
diff --git a/webrtc/api/peerconnectionfactory_unittest.cc b/webrtc/api/peerconnectionfactory_unittest.cc
index 963f1fe..833242a 100644
--- a/webrtc/api/peerconnectionfactory_unittest.cc
+++ b/webrtc/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/base/fakeportallocator.h"
+#include "webrtc/p2p/client/fakeportallocator.h"
 
 using webrtc::DataChannelInterface;
 using webrtc::DtlsIdentityStoreInterface;
diff --git a/webrtc/api/peerconnectioninterface.h b/webrtc/api/peerconnectioninterface.h
index fe0dc1d..94d2c00 100644
--- a/webrtc/api/peerconnectioninterface.h
+++ b/webrtc/api/peerconnectioninterface.h
@@ -270,30 +270,42 @@
     static const int kAudioJitterBufferMaxPackets = 50;
     // TODO(pthatcher): Rename this ice_transport_type, but update
     // Chromium at the same time.
-    IceTransportsType type = kAll;
+    IceTransportsType type;
     // TODO(pthatcher): Rename this ice_servers, but update Chromium
     // at the same time.
     IceServers servers;
-    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;
+    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;
     std::vector<rtc::scoped_refptr<rtc::RTCCertificate>> certificates;
-    bool prioritize_most_likely_ice_candidate_pairs = false;
+    bool prioritize_most_likely_ice_candidate_pairs;
     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 = false;
-    bool enable_rtp_data_channel = false;
+    bool disable_ipv6;
+    bool enable_rtp_data_channel;
     rtc::Optional<int> screencast_min_bitrate;
     rtc::Optional<bool> combined_audio_video_bwe;
     rtc::Optional<bool> enable_dtls_srtp;
-    int ice_candidate_pool_size = 0;
+    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) {}
   };
 
   struct RTCOfferAnswerOptions {
diff --git a/webrtc/api/peerconnectioninterface_unittest.cc b/webrtc/api/peerconnectioninterface_unittest.cc
index 68c932b..795a66d 100644
--- a/webrtc/api/peerconnectioninterface_unittest.cc
+++ b/webrtc/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/base/fakeportallocator.h"
+#include "webrtc/p2p/client/fakeportallocator.h"
 #include "webrtc/pc/mediasession.h"
 
 static const char kStreamLabel1[] = "local_stream_1";
@@ -551,33 +551,24 @@
   }
 
   void CreatePeerConnection() {
-    CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(), nullptr);
+    CreatePeerConnection("", "", NULL);
   }
 
   void CreatePeerConnection(webrtc::MediaConstraintsInterface* constraints) {
-    CreatePeerConnection(PeerConnectionInterface::RTCConfiguration(),
-                         constraints);
+    CreatePeerConnection("", "", 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) {
+  void CreatePeerConnection(const std::string& uri,
+                            const std::string& password,
+                            webrtc::MediaConstraintsInterface* constraints) {
     PeerConnectionInterface::RTCConfiguration config;
     PeerConnectionInterface::IceServer server;
-    server.uri = uri;
-    server.password = password;
-    config.servers.push_back(server);
-    CreatePeerConnection(config, nullptr);
-  }
+    if (!uri.empty()) {
+      server.uri = uri;
+      server.password = password;
+      config.servers.push_back(server);
+    }
 
-  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();
@@ -622,7 +613,7 @@
   }
 
   void CreatePeerConnectionWithDifferentConfigurations() {
-    CreatePeerConnectionWithIceServer(kStunAddressOnly, "");
+    CreatePeerConnection(kStunAddressOnly, "", NULL);
     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());
@@ -633,7 +624,7 @@
     CreatePeerConnectionExpectFail(kStunAddressPortAndMore1);
     CreatePeerConnectionExpectFail(kStunAddressPortAndMore2);
 
-    CreatePeerConnectionWithIceServer(kTurnIceServerUri, kTurnPassword);
+    CreatePeerConnection(kTurnIceServerUri, kTurnPassword, NULL);
     EXPECT_EQ(0u, port_allocator_->stun_servers().size());
     EXPECT_EQ(1u, port_allocator_->turn_servers().size());
     EXPECT_EQ(kTurnUsername,
@@ -1023,44 +1014,6 @@
   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);
@@ -1954,35 +1907,6 @@
             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/webrtc/api/test/peerconnectiontestwrapper.cc b/webrtc/api/test/peerconnectiontestwrapper.cc
index 1ec2b47..717c48a 100644
--- a/webrtc/api/test/peerconnectiontestwrapper.cc
+++ b/webrtc/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/base/fakeportallocator.h"
+#include "webrtc/p2p/client/fakeportallocator.h"
 
 static const char kStreamLabelBase[] = "stream_label";
 static const char kVideoTrackLabelBase[] = "video_track";
diff --git a/webrtc/api/webrtcsession.cc b/webrtc/api/webrtcsession.cc
index 2b146c6..c3ea1c8 100644
--- a/webrtc/api/webrtcsession.cc
+++ b/webrtc/api/webrtcsession.cc
@@ -421,6 +421,22 @@
   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,
@@ -459,6 +475,7 @@
                              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.
@@ -587,6 +604,8 @@
   if (options.disable_encryption) {
     webrtc_session_desc_factory_->SetSdesPolicy(cricket::SEC_DISABLED);
   }
+  port_allocator()->set_candidate_filter(
+      ConvertIceTransportTypeToCandidateFilter(rtc_configuration.type));
 
   return true;
 }
@@ -1126,6 +1145,12 @@
   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/webrtc/api/webrtcsession.h b/webrtc/api/webrtcsession.h
index 8a32d78f..1408b22 100644
--- a/webrtc/api/webrtcsession.h
+++ b/webrtc/api/webrtcsession.h
@@ -146,6 +146,7 @@
   // 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_; }
@@ -213,6 +214,8 @@
   bool RemoveRemoteIceCandidates(
       const std::vector<cricket::Candidate>& candidates);
 
+  bool SetIceTransports(PeerConnectionInterface::IceTransportsType type);
+
   cricket::IceConfig ParseIceConfig(
       const PeerConnectionInterface::RTCConfiguration& config) const;
 
@@ -466,6 +469,7 @@
 
   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/webrtc/api/webrtcsession_unittest.cc b/webrtc/api/webrtcsession_unittest.cc
index cd5e784..45ffc36 100644
--- a/webrtc/api/webrtcsession_unittest.cc
+++ b/webrtc/api/webrtcsession_unittest.cc
@@ -407,6 +407,12 @@
 
   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;
@@ -1523,6 +1529,50 @@
   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;