make CreateOffer/CreateAnswer use ice credentials of pooled sessions.

This patch make CreateOffer/CreateAnswer use the ice credentials
of pooled sessions (if any).

BUG=webrtc:9807

Change-Id: I51e0578f2ff0d4faa93d9666bd6b2c15461e8985
Reviewed-on: https://webrtc-review.googlesource.com/c/102923
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25132}
diff --git a/p2p/base/portallocator.h b/p2p/base/portallocator.h
index 8bd7096..988447c 100644
--- a/p2p/base/portallocator.h
+++ b/p2p/base/portallocator.h
@@ -201,7 +201,7 @@
   int component() const { return component_; }
   const std::string& ice_ufrag() const { return ice_ufrag_; }
   const std::string& ice_pwd() const { return ice_pwd_; }
-  bool pooled() const { return ice_ufrag_.empty(); }
+  bool pooled() const { return pooled_; }
 
   // Setting this filter should affect not only candidates gathered in the
   // future, but candidates already gathered and ports already "ready",
@@ -309,6 +309,8 @@
     UpdateIceParametersInternal();
   }
 
+  void set_pooled(bool value) { pooled_ = value; }
+
   uint32_t flags_;
   uint32_t generation_;
   std::string content_name_;
@@ -316,6 +318,8 @@
   std::string ice_ufrag_;
   std::string ice_pwd_;
 
+  bool pooled_ = false;
+
   // SetIceParameters is an implementation detail which only PortAllocator
   // should be able to call.
   friend class PortAllocator;
@@ -335,6 +339,11 @@
   // constructing and configuring the PortAllocator subclasses.
   virtual void Initialize();
 
+  // Set to true if some Ports need to know the ICE credentials when they are
+  // created. This will ensure that the PortAllocator will only match pooled
+  // allocator sessions to the ICE transport with the same credentials.
+  virtual void set_restrict_ice_credentials_change(bool value);
+
   // Set STUN and TURN servers to be used in future sessions, and set
   // candidate pool size, as described in JSEP.
   //
@@ -392,6 +401,8 @@
   //
   // Caller takes ownership of the returned session.
   //
+  // If restrict_ice_credentials_change is TRUE, then it will only
+  //   return a pooled session with matching ice credentials.
   // If no pooled sessions are available, returns null.
   std::unique_ptr<PortAllocatorSession> TakePooledSession(
       const std::string& content_name,
@@ -399,8 +410,10 @@
       const std::string& ice_ufrag,
       const std::string& ice_pwd);
 
-  // Returns the next session that would be returned by TakePooledSession.
-  const PortAllocatorSession* GetPooledSession() const;
+  // Returns the next session that would be returned by TakePooledSession
+  // optionally restricting it to sessions with specified ice credentials.
+  const PortAllocatorSession* GetPooledSession(
+      const IceParameters* ice_credentials = nullptr) const;
 
   // After FreezeCandidatePool is called, changing the candidate pool size will
   // no longer be allowed, and changing ICE servers will not cause pooled
@@ -548,6 +561,9 @@
   virtual void GetCandidateStatsFromPooledSessions(
       CandidateStatsList* candidate_stats_list);
 
+  // Return IceParameters of the pooled sessions.
+  std::vector<IceParameters> GetPooledIceCredentials();
+
  protected:
   virtual PortAllocatorSession* CreateSessionInternal(
       const std::string& content_name,
@@ -555,7 +571,7 @@
       const std::string& ice_ufrag,
       const std::string& ice_pwd) = 0;
 
-  const std::deque<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
+  const std::vector<std::unique_ptr<PortAllocatorSession>>& pooled_sessions() {
     return pooled_sessions_;
   }
 
@@ -586,7 +602,7 @@
   ServerAddresses stun_servers_;
   std::vector<RelayServerConfig> turn_servers_;
   int candidate_pool_size_ = 0;  // Last value passed into SetConfiguration.
-  std::deque<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
+  std::vector<std::unique_ptr<PortAllocatorSession>> pooled_sessions_;
   bool candidate_pool_frozen_ = false;
   bool prune_turn_ports_ = false;
 
@@ -596,6 +612,15 @@
   webrtc::TurnCustomizer* turn_customizer_ = nullptr;
 
   absl::optional<int> stun_candidate_keepalive_interval_;
+
+  // If true, TakePooledSession() will only return sessions that has same ice
+  // credentials as requested.
+  bool restrict_ice_credentials_change_ = false;
+
+  // Returns iterator to pooled session with specified ice_credentials or first
+  // if ice_credentials is nullptr.
+  std::vector<std::unique_ptr<PortAllocatorSession>>::const_iterator
+  FindPooledSession(const IceParameters* ice_credentials = nullptr) const;
 };
 
 }  // namespace cricket