Index: webrtc/p2p/base/portallocator.cc |
diff --git a/webrtc/p2p/base/portallocator.cc b/webrtc/p2p/base/portallocator.cc |
index 5c4243abf6b66e07c845448c24eed398e49edd02..8f296cbe5cb4e663609e76a81a67264a355a66d4 100644 |
--- a/webrtc/p2p/base/portallocator.cc |
+++ b/webrtc/p2p/base/portallocator.cc |
@@ -18,14 +18,51 @@ PortAllocatorSession::PortAllocatorSession(const std::string& content_name, |
const std::string& ice_ufrag, |
const std::string& ice_pwd, |
uint32_t flags) |
- : content_name_(content_name), |
- component_(component), |
- flags_(flags), |
+ : flags_(flags), |
generation_(0), |
+ content_name_(content_name), |
+ component_(component), |
ice_ufrag_(ice_ufrag), |
ice_pwd_(ice_pwd) { |
- RTC_DCHECK(!ice_ufrag.empty()); |
- RTC_DCHECK(!ice_pwd.empty()); |
+ // Pooled sessions are allowed to be created with empty content name, |
+ // component, ufrag and password. |
+ RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty()); |
+} |
+ |
+void PortAllocator::SetConfiguration( |
+ const ServerAddresses& stun_servers, |
+ const std::vector<RelayServerConfig>& turn_servers, |
+ int candidate_pool_size) { |
+ bool ice_servers_changed = |
+ (stun_servers != stun_servers_ || turn_servers != turn_servers_); |
+ stun_servers_ = stun_servers; |
+ turn_servers_ = turn_servers; |
+ |
+ // If ICE servers changed, throw away any existing pooled sessions and create |
+ // new ones. |
+ if (ice_servers_changed) { |
+ pooled_sessions_.clear(); |
+ allocated_pooled_sessions_ = 0; |
+ } |
+ |
+ // If |size| is less than the number of allocated sessions, get rid of the |
+ // extras. |
+ while (allocated_pooled_sessions_ > candidate_pool_size && |
+ !pooled_sessions_.empty()) { |
+ pooled_sessions_.front().reset(nullptr); |
+ pooled_sessions_.pop_front(); |
+ --allocated_pooled_sessions_; |
+ } |
+ // If |size| is greater than the number of allocated sessions, create new |
+ // sessions. |
+ while (allocated_pooled_sessions_ < candidate_pool_size) { |
+ PortAllocatorSession* pooled_session = CreateSessionInternal("", 0, "", ""); |
+ pooled_session->StartGettingPorts(); |
+ pooled_sessions_.push_back( |
+ std::unique_ptr<PortAllocatorSession>(pooled_session)); |
+ ++allocated_pooled_sessions_; |
+ } |
+ candidate_pool_size_ = candidate_pool_size; |
} |
PortAllocatorSession* PortAllocator::CreateSession( |
@@ -37,4 +74,27 @@ PortAllocatorSession* PortAllocator::CreateSession( |
return CreateSessionInternal(content_name, component, ice_ufrag, ice_pwd); |
} |
+PortAllocatorSession* PortAllocator::GetPooledSession( |
+ const std::string& content_name, |
+ int component, |
+ const std::string& ice_ufrag, |
+ const std::string& ice_pwd) { |
+ RTC_DCHECK(!ice_ufrag.empty()); |
+ RTC_DCHECK(!ice_pwd.empty()); |
+ if (pooled_sessions_.empty()) { |
+ return nullptr; |
+ } |
+ PortAllocatorSession* ret = pooled_sessions_.front().release(); |
+ ret->SetTransportInformation(content_name, component, ice_ufrag, ice_pwd); |
+ pooled_sessions_.pop_front(); |
+ return ret; |
+} |
+ |
+const PortAllocatorSession* PortAllocator::PeekPooledSession() const { |
+ if (pooled_sessions_.empty()) { |
+ return nullptr; |
+ } |
+ return pooled_sessions_.front().get(); |
+} |
+ |
} // namespace cricket |