use sigslot trampoline for Port ready signal

Bug: webrtc:42222066
Change-Id: I55ba46ecffbb39e18e801c87e0a644be42d4cade
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/407800
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Lena Kaplan <lenakaplan@meta.com>
Cr-Commit-Position: refs/heads/main@{#45586}
diff --git a/p2p/BUILD.gn b/p2p/BUILD.gn
index 7c5433b..c0384f1 100644
--- a/p2p/BUILD.gn
+++ b/p2p/BUILD.gn
@@ -634,6 +634,7 @@
     "../rtc_base:checks",
     "../rtc_base:crypto_random",
     "../rtc_base:network",
+    "../rtc_base:sigslot_trampoline",
     "../rtc_base:socket_address",
     "../rtc_base:ssl",
     "../rtc_base:threading",
diff --git a/p2p/base/p2p_transport_channel.cc b/p2p/base/p2p_transport_channel.cc
index 1b12cbc..466b932 100644
--- a/p2p/base/p2p_transport_channel.cc
+++ b/p2p/base/p2p_transport_channel.cc
@@ -255,16 +255,35 @@
   RTC_DCHECK_RUN_ON(network_thread_);
 
   session->set_generation(static_cast<uint32_t>(allocator_sessions_.size()));
-  session->SignalPortReady.connect(this, &P2PTransportChannel::OnPortReady);
-  session->SignalPortsPruned.connect(this, &P2PTransportChannel::OnPortsPruned);
-  session->SignalCandidatesReady.connect(
-      this, &P2PTransportChannel::OnCandidatesReady);
-  session->SignalCandidateError.connect(this,
-                                        &P2PTransportChannel::OnCandidateError);
-  session->SignalCandidatesRemoved.connect(
-      this, &P2PTransportChannel::OnCandidatesRemoved);
-  session->SignalCandidatesAllocationDone.connect(
-      this, &P2PTransportChannel::OnCandidatesAllocationDone);
+  session->SubscribePortReady(
+      [this](PortAllocatorSession* session, PortInterface* port) {
+        OnPortReady(session, port);
+      });
+
+  session->SubscribePortsPruned(
+      [this](PortAllocatorSession* session,
+             const std::vector<PortInterface*>& ports) {
+        OnPortsPruned(session, ports);
+      });
+
+  session->SubscribeCandidatesReady(
+      [this](PortAllocatorSession* session,
+             const std::vector<Candidate>& candidate) {
+        OnCandidatesReady(session, candidate);
+      });
+  session->SubscribeCandidateError([this](PortAllocatorSession* session,
+                                          const IceCandidateErrorEvent& event) {
+    OnCandidateError(session, event);
+  });
+  session->SubscribeCandidatesRemoved(
+      [this](PortAllocatorSession* session,
+             const std::vector<Candidate>& candidates) {
+        OnCandidatesRemoved(session, candidates);
+      });
+  session->SubscribeCandidatesAllocationDone(
+      [this](PortAllocatorSession* session) {
+        OnCandidatesAllocationDone(session);
+      });
   if (!allocator_sessions_.empty()) {
     allocator_session()->PruneAllPorts();
   }
diff --git a/p2p/base/p2p_transport_channel_unittest.cc b/p2p/base/p2p_transport_channel_unittest.cc
index 58d5b50..ed80899 100644
--- a/p2p/base/p2p_transport_channel_unittest.cc
+++ b/p2p/base/p2p_transport_channel_unittest.cc
@@ -465,10 +465,16 @@
     ep2_.cd1_.ch_->SetIceConfig(ep2_config);
     ep1_.cd1_.ch_->MaybeStartGathering();
     ep2_.cd1_.ch_->MaybeStartGathering();
-    ep1_.cd1_.ch_->allocator_session()->SignalIceRegathering.connect(
-        &ep1_, &Endpoint::OnIceRegathering);
-    ep2_.cd1_.ch_->allocator_session()->SignalIceRegathering.connect(
-        &ep2_, &Endpoint::OnIceRegathering);
+    ep1_.cd1_.ch_->allocator_session()->SubscribeIceRegathering(
+        [this](PortAllocatorSession* allocator_session,
+               IceRegatheringReason reason) {
+          ep1_.OnIceRegathering(allocator_session, reason);
+        });
+    ep2_.cd1_.ch_->allocator_session()->SubscribeIceRegathering(
+        [this](PortAllocatorSession* allocator_session,
+               IceRegatheringReason reason) {
+          ep2_.OnIceRegathering(allocator_session, reason);
+        });
   }
 
   void CreateChannels(const Environment& env) {
diff --git a/p2p/base/port_allocator.cc b/p2p/base/port_allocator.cc
index 7e8b34a..adfcc1d 100644
--- a/p2p/base/port_allocator.cc
+++ b/p2p/base/port_allocator.cc
@@ -77,7 +77,14 @@
       content_name_(content_name),
       component_(component),
       ice_ufrag_(ice_ufrag),
-      ice_pwd_(ice_pwd) {
+      ice_pwd_(ice_pwd),
+      port_ready_trampoline_(this),
+      ports_pruned_trampoline_(this),
+      candidates_ready_trampoline_(this),
+      candidate_error_trampoline_(this),
+      candidates_removed_trampoline_(this),
+      candidates_allocation_done_trampoline_(this),
+      ice_regathering_trampoline_(this) {
   // Pooled sessions are allowed to be created with empty content name,
   // component, ufrag and password.
   RTC_DCHECK(ice_ufrag.empty() == ice_pwd.empty());
diff --git a/p2p/base/port_allocator.h b/p2p/base/port_allocator.h
index f192209..abd9b39 100644
--- a/p2p/base/port_allocator.h
+++ b/p2p/base/port_allocator.h
@@ -15,6 +15,7 @@
 #include <memory>
 #include <optional>
 #include <string>
+#include <utility>
 #include <vector>
 
 #include "absl/strings/string_view.h"
@@ -262,24 +263,59 @@
   virtual void PruneAllPorts() {}
 
   sigslot::signal2<PortAllocatorSession*, PortInterface*> SignalPortReady;
+  void SubscribePortReady(absl::AnyInvocable<void(PortAllocatorSession*,
+                                                  PortInterface*)> callback) {
+    port_ready_trampoline_.Subscribe(std::move(callback));
+  }
+
   // Fires this signal when the network of the ports failed (either because the
   // interface is down, or because there is no connection on the interface),
   // or when TURN ports are pruned because a higher-priority TURN port becomes
   // ready(pairable).
   sigslot::signal2<PortAllocatorSession*, const std::vector<PortInterface*>&>
       SignalPortsPruned;
+  void SubscribePortsPruned(
+      absl::AnyInvocable<void(PortAllocatorSession*,
+                              const std::vector<PortInterface*>&)> callback) {
+    ports_pruned_trampoline_.Subscribe(std::move(callback));
+  }
+
   sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
       SignalCandidatesReady;
+  void SubscribeCandidatesReady(
+      absl::AnyInvocable<void(PortAllocatorSession*,
+                              const std::vector<Candidate>&)> callback) {
+    candidates_ready_trampoline_.Subscribe(std::move(callback));
+  }
   sigslot::signal2<PortAllocatorSession*, const IceCandidateErrorEvent&>
       SignalCandidateError;
+  void SubscribeCandidateError(
+      absl::AnyInvocable<void(PortAllocatorSession*,
+                              const IceCandidateErrorEvent&)> callback) {
+    candidate_error_trampoline_.Subscribe(std::move(callback));
+  }
   // Candidates should be signaled to be removed when the port that generated
   // the candidates is removed.
   sigslot::signal2<PortAllocatorSession*, const std::vector<Candidate>&>
       SignalCandidatesRemoved;
+  void SubscribeCandidatesRemoved(
+      absl::AnyInvocable<void(PortAllocatorSession*,
+                              const std::vector<Candidate>&)> callback) {
+    candidates_removed_trampoline_.Subscribe(std::move(callback));
+  }
   sigslot::signal1<PortAllocatorSession*> SignalCandidatesAllocationDone;
+  void SubscribeCandidatesAllocationDone(
+      absl::AnyInvocable<void(PortAllocatorSession*)> callback) {
+    candidates_allocation_done_trampoline_.Subscribe(std::move(callback));
+  }
 
   sigslot::signal2<PortAllocatorSession*, IceRegatheringReason>
       SignalIceRegathering;
+  void SubscribeIceRegathering(
+      absl::AnyInvocable<void(PortAllocatorSession*, IceRegatheringReason)>
+          callback) {
+    ice_regathering_trampoline_.Subscribe(std::move(callback));
+  }
 
   virtual uint32_t generation();
   virtual void set_generation(uint32_t generation);
@@ -332,6 +368,27 @@
   // SetIceParameters is an implementation detail which only PortAllocator
   // should be able to call.
   friend class PortAllocator;
+  SignalTrampoline<PortAllocatorSession, &PortAllocatorSession::SignalPortReady>
+      port_ready_trampoline_;
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalPortsPruned>
+      ports_pruned_trampoline_;
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalCandidatesReady>
+      candidates_ready_trampoline_;
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalCandidateError>
+      candidate_error_trampoline_;
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalCandidatesRemoved>
+      candidates_removed_trampoline_;
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalCandidatesAllocationDone>
+      candidates_allocation_done_trampoline_;
+
+  SignalTrampoline<PortAllocatorSession,
+                   &PortAllocatorSession::SignalIceRegathering>
+      ice_regathering_trampoline_;
 };
 
 // Every method of PortAllocator (including the destructor) must be called on
diff --git a/p2p/base/regathering_controller_unittest.cc b/p2p/base/regathering_controller_unittest.cc
index 51f8214..3243086 100644
--- a/p2p/base/regathering_controller_unittest.cc
+++ b/p2p/base/regathering_controller_unittest.cc
@@ -82,8 +82,11 @@
     // call of StartGettingPorts is blocking. We will not ClearGettingPorts
     // prematurely.
     allocator_session_->StartGettingPorts();
-    allocator_session_->SignalIceRegathering.connect(
-        this, &RegatheringControllerTest::OnIceRegathering);
+    allocator_session_->SubscribeIceRegathering(
+        [this](PortAllocatorSession* allocator_session,
+               IceRegatheringReason reason) {
+          OnIceRegathering(allocator_session, reason);
+        });
     regathering_controller_->set_allocator_session(allocator_session_.get());
   }
 
diff --git a/p2p/client/basic_port_allocator_unittest.cc b/p2p/client/basic_port_allocator_unittest.cc
index 48eaff1..a8ef7cb 100644
--- a/p2p/client/basic_port_allocator_unittest.cc
+++ b/p2p/client/basic_port_allocator_unittest.cc
@@ -288,16 +288,29 @@
       absl::string_view ice_pwd) {
     std::unique_ptr<PortAllocatorSession> session =
         allocator_->CreateSession(content_name, component, ice_ufrag, ice_pwd);
-    session->SignalPortReady.connect(this,
-                                     &BasicPortAllocatorTestBase::OnPortReady);
-    session->SignalPortsPruned.connect(
-        this, &BasicPortAllocatorTestBase::OnPortsPruned);
-    session->SignalCandidatesReady.connect(
-        this, &BasicPortAllocatorTestBase::OnCandidatesReady);
-    session->SignalCandidatesRemoved.connect(
-        this, &BasicPortAllocatorTestBase::OnCandidatesRemoved);
-    session->SignalCandidatesAllocationDone.connect(
-        this, &BasicPortAllocatorTestBase::OnCandidatesAllocationDone);
+    session->SubscribePortReady(
+        [this](PortAllocatorSession* session, PortInterface* port) {
+          OnPortReady(session, port);
+        });
+    session->SubscribePortsPruned(
+        [this](PortAllocatorSession* session,
+               const std::vector<PortInterface*>& ports) {
+          OnPortsPruned(session, ports);
+        });
+    session->SubscribeCandidatesReady(
+        [this](PortAllocatorSession* session,
+               const std::vector<Candidate>& candidate) {
+          OnCandidatesReady(session, candidate);
+        });
+    session->SubscribeCandidatesRemoved(
+        [this](PortAllocatorSession* session,
+               const std::vector<Candidate>& removed_candidates) {
+          OnCandidatesRemoved(session, removed_candidates);
+        });
+    session->SubscribeCandidatesAllocationDone(
+        [this](PortAllocatorSession* session) {
+          OnCandidatesAllocationDone(session);
+        });
     return session;
   }