Rename SetChannel to SetChannelForTest and inline logic

Rename the RtpTransceiver::SetChannel method to SetChannelForTest since
it now is only needed for legacy tests. The production implementation
now inlines the channel assignment and transport lookup logic.

Bug: webrtc:42222804
Change-Id: I45f8a5619e7fe3bf82bc09e628b5ba8b73bab103
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/462940
Commit-Queue: Tomas Gunnarsson <tommi@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#47388}
diff --git a/pc/rtp_transceiver.cc b/pc/rtp_transceiver.cc
index 9c21128..1c87507 100644
--- a/pc/rtp_transceiver.cc
+++ b/pc/rtp_transceiver.cc
@@ -535,10 +535,33 @@
               srtp_required, crypto_options, std::move(callbacks));
     }
   });
-  return SetChannel(std::move(new_channel), std::move(transport_lookup));
+
+  channel_ = std::move(new_channel);
+  transport_name_ = std::nullopt;
+
+  std::optional<std::string> transport_name;
+  RTCError err = context()->network_thread()->BlockingCall(
+      [&, flag = signaling_thread_safety_, channel = channel_.get()]() {
+        RtpTransportInternal* transport =
+            std::move(transport_lookup)(channel->mid());
+        if (!channel->SetRtpTransport(transport)) {
+          return RTCError::InvalidParameter()
+                 << "Invalid transport for mid=" << channel->mid();
+        }
+        if (transport) {
+          transport_name = transport->transport_name();
+        }
+        return RTCError::OK();
+      });
+
+  if (err.ok()) {
+    transport_name_ = std::move(transport_name);
+  }
+
+  return err;
 }
 
-RTCError RtpTransceiver::SetChannel(
+RTCError RtpTransceiver::SetChannelForTest(
     std::unique_ptr<ChannelInterface> channel,
     absl::AnyInvocable<RtpTransportInternal*(const std::string&) &&>
         transport_lookup) {
diff --git a/pc/rtp_transceiver.h b/pc/rtp_transceiver.h
index 16ae3e9..d115396 100644
--- a/pc/rtp_transceiver.h
+++ b/pc/rtp_transceiver.h
@@ -187,7 +187,7 @@
   //     The callback allows us to combine the transport lookup with network
   //     state initialization of the channel object.
   // ClearChannel() must be used before calling SetChannel() again.
-  RTCError SetChannel(
+  RTCError SetChannelForTest(
       std::unique_ptr<ChannelInterface> channel,
       absl::AnyInvocable<RtpTransportInternal*(const std::string&) &&>
           transport_lookup);
diff --git a/pc/rtp_transceiver_unittest.cc b/pc/rtp_transceiver_unittest.cc
index 07e7c1f..f736ab8 100644
--- a/pc/rtp_transceiver_unittest.cc
+++ b/pc/rtp_transceiver_unittest.cc
@@ -133,10 +133,11 @@
   EXPECT_CALL(*channel1, SetFirstPacketSentCallback_n(_)).Times(0);
   EXPECT_CALL(*channel1, SetPacketReceivedCallback_n(_)).Times(0);
 
-  transceiver->SetChannel(std::move(channel1), [&](const std::string& mid) {
-    EXPECT_EQ(mid, content_name);
-    return nullptr;
-  });
+  transceiver->SetChannelForTest(std::move(channel1),
+                                 [&](const std::string& mid) {
+                                   EXPECT_EQ(mid, content_name);
+                                   return nullptr;
+                                 });
   EXPECT_TRUE(transceiver->HasChannel());
 
   // Stop the transceiver.
@@ -146,7 +147,7 @@
   auto channel2 = std::make_unique<NiceMock<MockChannelInterface>>();
   EXPECT_CALL(*channel2, media_type()).WillRepeatedly(Return(MediaType::AUDIO));
 
-  // Clear the current channel - required to allow SetChannel()
+  // Clear the current channel - required to allow SetChannelForTest()
   // ClearChannel calls callbacks with nullptr, so we expect it.
   EXPECT_CALL(*channel1_ptr, SetFirstPacketReceivedCallback_n(_))
       .WillOnce([](auto&& cb) { EXPECT_FALSE(cb); });
@@ -159,8 +160,8 @@
   ASSERT_FALSE(transceiver->HasChannel());
 
   // Channel can no longer be set, so this call should be a no-op.
-  transceiver->SetChannel(std::move(channel2),
-                          [](const std::string&) { return nullptr; });
+  transceiver->SetChannelForTest(std::move(channel2),
+                                 [](const std::string&) { return nullptr; });
   EXPECT_FALSE(transceiver->HasChannel());
 }
 
@@ -180,10 +181,11 @@
   EXPECT_CALL(*channel, SetFirstPacketSentCallback_n(_)).Times(0);
   EXPECT_CALL(*channel, SetPacketReceivedCallback_n(_)).Times(0);
 
-  transceiver->SetChannel(std::move(channel), [&](const std::string& mid) {
-    EXPECT_EQ(mid, content_name);
-    return nullptr;
-  });
+  transceiver->SetChannelForTest(std::move(channel),
+                                 [&](const std::string& mid) {
+                                   EXPECT_EQ(mid, content_name);
+                                   return nullptr;
+                                 });
   EXPECT_TRUE(transceiver->HasChannel());
 
   // Stop the transceiver.
@@ -223,7 +225,7 @@
   EXPECT_CALL(*channel, SetFirstPacketSentCallback_n(_)).Times(0);
   EXPECT_CALL(*channel, SetPacketReceivedCallback_n(_)).Times(0);
 
-  auto result = transceiver->SetChannel(
+  auto result = transceiver->SetChannelForTest(
       std::move(channel), [&](const std::string& mid) -> RtpTransportInternal* {
         return rtp_transport.get();
       });
@@ -795,8 +797,8 @@
   EXPECT_CALL(*mock_channel, SetFirstPacketSentCallback_n(_)).Times(0);
   EXPECT_CALL(*mock_channel, SetPacketReceivedCallback_n(_)).Times(0);
 
-  transceiver_->SetChannel(std::move(mock_channel),
-                           [](const std::string&) { return nullptr; });
+  transceiver_->SetChannelForTest(std::move(mock_channel),
+                                  [](const std::string&) { return nullptr; });
   EXPECT_THAT(transceiver_->GetNegotiatedHeaderExtensions(),
               ElementsAre(Field(&RtpHeaderExtensionCapability::direction,
                                 RtpTransceiverDirection::kStopped),
@@ -838,8 +840,8 @@
   description.set_rtp_header_extensions(extensions);
   transceiver_->OnNegotiationUpdate(SdpType::kAnswer, &description);
 
-  transceiver_->SetChannel(std::move(mock_channel),
-                           [](const std::string&) { return nullptr; });
+  transceiver_->SetChannelForTest(std::move(mock_channel),
+                                  [](const std::string&) { return nullptr; });
 
   EXPECT_THAT(transceiver_->GetNegotiatedHeaderExtensions(),
               ElementsAre(Field(&RtpHeaderExtensionCapability::direction,
@@ -882,8 +884,8 @@
   description.set_rtp_header_extensions(extensions);
   transceiver_->OnNegotiationUpdate(SdpType::kPrAnswer, &description);
 
-  transceiver_->SetChannel(std::move(mock_channel),
-                           [](const std::string&) { return nullptr; });
+  transceiver_->SetChannelForTest(std::move(mock_channel),
+                                  [](const std::string&) { return nullptr; });
 
   EXPECT_THAT(transceiver_->GetNegotiatedHeaderExtensions(),
               ElementsAre(Field(&RtpHeaderExtensionCapability::direction,
@@ -920,8 +922,8 @@
   EXPECT_CALL(*mock_channel, SetFirstPacketSentCallback_n(_)).Times(0);
   EXPECT_CALL(*mock_channel, SetPacketReceivedCallback_n(_)).Times(0);
 
-  transceiver_->SetChannel(std::move(mock_channel),
-                           [](const std::string&) { return nullptr; });
+  transceiver_->SetChannelForTest(std::move(mock_channel),
+                                  [](const std::string&) { return nullptr; });
 
   AudioContentDescription description_pr_answer;
   description_pr_answer.set_rtp_header_extensions({RtpExtension("uri1", 1)});
diff --git a/pc/test/fake_peer_connection_for_stats.h b/pc/test/fake_peer_connection_for_stats.h
index 5c4ef44..6dcfbfb 100644
--- a/pc/test/fake_peer_connection_for_stats.h
+++ b/pc/test/fake_peer_connection_for_stats.h
@@ -666,9 +666,10 @@
       std::unique_ptr<ChannelInterface> channel,
       MediaSendChannelInterface* send_channel,
       MediaReceiveChannelInterface* receive_channel) {
-    transceiver->SetChannel(std::move(channel), [this](const std::string& mid) {
-      return transport_controller_->GetRtpTransport(mid);
-    });
+    transceiver->SetChannelForTest(
+        std::move(channel), [this](const std::string& mid) {
+          return transport_controller_->GetRtpTransport(mid);
+        });
     RTC_ALLOW_PLAN_B_DEPRECATION_BEGIN()
     for (const auto& sender : transceiver->senders()) {
       sender->internal()->SetMediaChannel(send_channel);