Use pass-by-value in delayed scheduling of OnSentPacket

The change in the test failed to trigger an error on msan, but making
the change anyway out of an abundance of caution.

Bug: chromium:1496240
Change-Id: Ifa1b632f4e9ddb413f0eb23aba3f5b321b287b06
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/326080
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Victor Boivie <boivie@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41083}
diff --git a/pc/rtp_transport.cc b/pc/rtp_transport.cc
index cddfaca..42828a7 100644
--- a/pc/rtp_transport.cc
+++ b/pc/rtp_transport.cc
@@ -228,7 +228,7 @@
              packet_transport == rtcp_packet_transport_);
   if (processing_sent_packet_) {
     TaskQueueBase::Current()->PostTask(SafeTask(
-        safety_.flag(), [this, &sent_packet] { SendSentPacket(sent_packet); }));
+        safety_.flag(), [this, sent_packet] { SendSentPacket(sent_packet); }));
     return;
   }
   processing_sent_packet_ = true;
diff --git a/pc/rtp_transport_unittest.cc b/pc/rtp_transport_unittest.cc
index d3eb666..6b8e616 100644
--- a/pc/rtp_transport_unittest.cc
+++ b/pc/rtp_transport_unittest.cc
@@ -359,14 +359,15 @@
   TransportObserver observer(&transport);
   const rtc::PacketOptions options;
   const int flags = 0;
-  rtc::CopyOnWriteBuffer rtp_data(kRtpData, kRtpLen);
 
   fake_rtp.SetWritable(true);
   observer.SetActionOnSentPacket([&]() {
+    rtc::CopyOnWriteBuffer rtp_data(kRtpData, kRtpLen);
     if (observer.sent_packet_count() < 2) {
       transport.SendRtpPacket(&rtp_data, options, flags);
     }
   });
+  rtc::CopyOnWriteBuffer rtp_data(kRtpData, kRtpLen);
   transport.SendRtpPacket(&rtp_data, options, flags);
   EXPECT_EQ(observer.sent_packet_count(), 1);
   EXPECT_EQ_WAIT(observer.sent_packet_count(), 2, kShortTimeout);