Replace all RTPSender::SendToNetwork with EnqueuePackets

Bug: None
Change-Id: I1bcfbd9c16b329f3aa3f95d8ed61b82131e0da1d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/316922
Commit-Queue: Erik Språng <sprang@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Auto-Submit: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40591}
diff --git a/modules/rtp_rtcp/source/rtp_sender.cc b/modules/rtp_rtcp/source/rtp_sender.cc
index bb65b98..d899b4f 100644
--- a/modules/rtp_rtcp/source/rtp_sender.cc
+++ b/modules/rtp_rtcp/source/rtp_sender.cc
@@ -461,22 +461,6 @@
   return padding_packets;
 }
 
-bool RTPSender::SendToNetwork(std::unique_ptr<RtpPacketToSend> packet) {
-  RTC_DCHECK(packet);
-  auto packet_type = packet->packet_type();
-  RTC_CHECK(packet_type) << "Packet type must be set before sending.";
-
-  if (packet->capture_time() <= Timestamp::Zero()) {
-    packet->set_capture_time(clock_->CurrentTime());
-  }
-
-  std::vector<std::unique_ptr<RtpPacketToSend>> packets;
-  packets.emplace_back(std::move(packet));
-  paced_sender_->EnqueuePackets(std::move(packets));
-
-  return true;
-}
-
 void RTPSender::EnqueuePackets(
     std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
   RTC_DCHECK(!packets.empty());
diff --git a/modules/rtp_rtcp/source/rtp_sender.h b/modules/rtp_rtcp/source/rtp_sender.h
index b6a04a7..a398f16 100644
--- a/modules/rtp_rtcp/source/rtp_sender.h
+++ b/modules/rtp_rtcp/source/rtp_sender.h
@@ -144,11 +144,6 @@
     return flexfec_ssrc_;
   }
 
-  // Sends packet to `transport_` or to the pacer, depending on configuration.
-  // TODO(bugs.webrtc.org/XXX): Remove in favor of EnqueuePackets().
-  bool SendToNetwork(std::unique_ptr<RtpPacketToSend> packet)
-      RTC_LOCKS_EXCLUDED(send_mutex_);
-
   // Pass a set of packets to RtpPacketSender instance, for paced or immediate
   // sending to the network.
   void EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets)
diff --git a/modules/rtp_rtcp/source/rtp_sender_audio.cc b/modules/rtp_rtcp/source/rtp_sender_audio.cc
index a0f1af5..0fac440 100644
--- a/modules/rtp_rtcp/source/rtp_sender_audio.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_audio.cc
@@ -14,6 +14,7 @@
 
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "absl/strings/match.h"
 #include "absl/types/optional.h"
@@ -300,11 +301,13 @@
                          packet->SequenceNumber());
   packet->set_packet_type(RtpPacketMediaType::kAudio);
   packet->set_allow_retransmission(true);
-  bool send_result = rtp_sender_->SendToNetwork(std::move(packet));
+  std::vector<std::unique_ptr<RtpPacketToSend>> packets(1);
+  packets[0] = std::move(packet);
+  rtp_sender_->EnqueuePackets(std::move(packets));
   if (first_packet_sent_()) {
     RTC_LOG(LS_INFO) << "First audio RTP packet sent to pacer";
   }
-  return send_result;
+  return true;
 }
 
 // Audio level magnitude and voice activity flag are set for each RTP packet
@@ -340,19 +343,16 @@
                                               uint32_t dtmf_timestamp,
                                               uint16_t duration,
                                               bool marker_bit) {
-  uint8_t send_count = 1;
-  bool result = true;
+  size_t send_count = ended ? 3 : 1;
 
-  if (ended) {
-    // resend last packet in an event 3 times
-    send_count = 3;
-  }
-  do {
+  std::vector<std::unique_ptr<RtpPacketToSend>> packets;
+  packets.reserve(send_count);
+  for (size_t i = 0; i < send_count; ++i) {
     // Send DTMF data.
     constexpr RtpPacketToSend::ExtensionManager* kNoExtensions = nullptr;
     constexpr size_t kDtmfSize = 4;
-    std::unique_ptr<RtpPacketToSend> packet(
-        new RtpPacketToSend(kNoExtensions, kRtpHeaderSize + kDtmfSize));
+    auto packet = std::make_unique<RtpPacketToSend>(kNoExtensions,
+                                                    kRtpHeaderSize + kDtmfSize);
     packet->SetPayloadType(dtmf_current_event_.payload_type);
     packet->SetMarker(marker_bit);
     packet->SetSsrc(rtp_sender_->SSRC());
@@ -383,10 +383,9 @@
 
     packet->set_packet_type(RtpPacketMediaType::kAudio);
     packet->set_allow_retransmission(true);
-    result = rtp_sender_->SendToNetwork(std::move(packet));
-    send_count--;
-  } while (send_count > 0 && result);
-
-  return result;
+    packets.push_back(std::move(packet));
+  }
+  rtp_sender_->EnqueuePackets(std::move(packets));
+  return true;
 }
 }  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/rtp_sender_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
index 65c49f7..c47edfc 100644
--- a/modules/rtp_rtcp/source/rtp_sender_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_unittest.cc
@@ -201,8 +201,9 @@
     packet->set_allow_retransmission(true);
 
     // Packet should be stored in a send bucket.
-    EXPECT_TRUE(
-        rtp_sender_->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
+    std::vector<std::unique_ptr<RtpPacketToSend>> packets(1);
+    packets[0] = std::make_unique<RtpPacketToSend>(*packet);
+    rtp_sender_->EnqueuePackets(std::move(packets));
     return packet;
   }
 
@@ -232,10 +233,12 @@
 
   size_t GenerateAndSendPadding(size_t target_size_bytes) {
     size_t generated_bytes = 0;
+    std::vector<std::unique_ptr<RtpPacketToSend>> packets;
     for (auto& packet : GeneratePadding(target_size_bytes)) {
       generated_bytes += packet->payload_size() + packet->padding_size();
-      rtp_sender_->SendToNetwork(std::move(packet));
+      packets.push_back(std::move(packet));
     }
+    rtp_sender_->EnqueuePackets(std::move(packets));
     return generated_bytes;
   }
 
@@ -339,7 +342,8 @@
 }
 
 TEST_F(RtpSenderTest, SendToNetworkForwardsPacketsToPacer) {
-  auto packet =
+  std::vector<std::unique_ptr<RtpPacketToSend>> packets(1);
+  packets[0] =
       BuildRtpPacket(kPayload, kMarkerBit, kTimestamp, Timestamp::Zero());
   Timestamp now = clock_->CurrentTime();
 
@@ -347,7 +351,8 @@
               EnqueuePackets(ElementsAre(AllOf(
                   Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
                   Pointee(Property(&RtpPacketToSend::capture_time, now))))));
-  EXPECT_TRUE(rtp_sender_->SendToNetwork(std::move(packet)));
+
+  rtp_sender_->EnqueuePackets(std::move(packets));
 }
 
 TEST_F(RtpSenderTest, ReSendPacketForwardsPacketsToPacer) {
@@ -398,7 +403,7 @@
     std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets =
         Sequence(GeneratePadding(kPaddingTargetBytes));
     ASSERT_THAT(padding_packets, SizeIs(1));
-    rtp_sender_->SendToNetwork(std::move(padding_packets[0]));
+    rtp_sender_->EnqueuePackets(std::move(padding_packets));
   }
 
   // Send a regular video packet again.