1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/rtp_rtcp/source/rtp_sender.h"
12 
13 #include <memory>
14 #include <vector>
15 
16 #include "api/rtc_event_log/rtc_event.h"
17 #include "api/transport/field_trial_based_config.h"
18 #include "api/video/video_codec_constants.h"
19 #include "api/video/video_timing.h"
20 #include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
21 #include "modules/rtp_rtcp/include/rtp_cvo.h"
22 #include "modules/rtp_rtcp/include/rtp_header_extension_map.h"
23 #include "modules/rtp_rtcp/include/rtp_packet_sender.h"
24 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
25 #include "modules/rtp_rtcp/source/rtcp_packet/transport_feedback.h"
26 #include "modules/rtp_rtcp/source/rtp_format_video_generic.h"
27 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor.h"
28 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
29 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
30 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
31 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
32 #include "modules/rtp_rtcp/source/rtp_sender_egress.h"
33 #include "modules/rtp_rtcp/source/rtp_sender_video.h"
34 #include "modules/rtp_rtcp/source/rtp_utility.h"
35 #include "modules/rtp_rtcp/source/video_fec_generator.h"
36 #include "rtc_base/arraysize.h"
37 #include "rtc_base/rate_limiter.h"
38 #include "rtc_base/strings/string_builder.h"
39 #include "rtc_base/task_utils/to_queued_task.h"
40 #include "test/field_trial.h"
41 #include "test/gmock.h"
42 #include "test/gtest.h"
43 #include "test/mock_transport.h"
44 #include "test/rtp_header_parser.h"
45 #include "test/time_controller/simulated_time_controller.h"
46 
47 namespace webrtc {
48 
49 namespace {
50 enum : int {  // The first valid value is 1.
51   kAbsoluteSendTimeExtensionId = 1,
52   kAudioLevelExtensionId,
53   kGenericDescriptorId,
54   kMidExtensionId,
55   kRepairedRidExtensionId,
56   kRidExtensionId,
57   kTransmissionTimeOffsetExtensionId,
58   kTransportSequenceNumberExtensionId,
59   kVideoRotationExtensionId,
60   kVideoTimingExtensionId,
61 };
62 
63 const int kPayload = 100;
64 const int kRtxPayload = 98;
65 const uint32_t kTimestamp = 10;
66 const uint16_t kSeqNum = 33;
67 const uint32_t kSsrc = 725242;
68 const uint32_t kRtxSsrc = 12345;
69 const uint32_t kFlexFecSsrc = 45678;
70 const uint16_t kTransportSequenceNumber = 1;
71 const uint64_t kStartTime = 123456789;
72 const size_t kMaxPaddingSize = 224u;
73 const uint8_t kPayloadData[] = {47, 11, 32, 93, 89};
74 const int64_t kDefaultExpectedRetransmissionTimeMs = 125;
75 const char kNoRid[] = "";
76 const char kNoMid[] = "";
77 
78 using ::testing::_;
79 using ::testing::AllOf;
80 using ::testing::Contains;
81 using ::testing::Each;
82 using ::testing::ElementsAreArray;
83 using ::testing::Eq;
84 using ::testing::Field;
85 using ::testing::Gt;
86 using ::testing::IsEmpty;
87 using ::testing::NiceMock;
88 using ::testing::Not;
89 using ::testing::Pointee;
90 using ::testing::Property;
91 using ::testing::Return;
92 using ::testing::SizeIs;
93 using ::testing::StrictMock;
94 
ConvertMsToAbsSendTime(int64_t time_ms)95 uint64_t ConvertMsToAbsSendTime(int64_t time_ms) {
96   return (((time_ms << 18) + 500) / 1000) & 0x00ffffff;
97 }
98 
99 class LoopbackTransportTest : public webrtc::Transport {
100  public:
LoopbackTransportTest()101   LoopbackTransportTest() : total_bytes_sent_(0) {
102     receivers_extensions_.Register<TransmissionOffset>(
103         kTransmissionTimeOffsetExtensionId);
104     receivers_extensions_.Register<AbsoluteSendTime>(
105         kAbsoluteSendTimeExtensionId);
106     receivers_extensions_.Register<TransportSequenceNumber>(
107         kTransportSequenceNumberExtensionId);
108     receivers_extensions_.Register<VideoOrientation>(kVideoRotationExtensionId);
109     receivers_extensions_.Register<AudioLevel>(kAudioLevelExtensionId);
110     receivers_extensions_.Register<VideoTimingExtension>(
111         kVideoTimingExtensionId);
112     receivers_extensions_.Register<RtpMid>(kMidExtensionId);
113     receivers_extensions_.Register<RtpGenericFrameDescriptorExtension00>(
114         kGenericDescriptorId);
115     receivers_extensions_.Register<RtpStreamId>(kRidExtensionId);
116     receivers_extensions_.Register<RepairedRtpStreamId>(
117         kRepairedRidExtensionId);
118   }
119 
SendRtp(const uint8_t * data,size_t len,const PacketOptions & options)120   bool SendRtp(const uint8_t* data,
121                size_t len,
122                const PacketOptions& options) override {
123     last_options_ = options;
124     total_bytes_sent_ += len;
125     sent_packets_.push_back(RtpPacketReceived(&receivers_extensions_));
126     EXPECT_TRUE(sent_packets_.back().Parse(data, len));
127     return true;
128   }
SendRtcp(const uint8_t * data,size_t len)129   bool SendRtcp(const uint8_t* data, size_t len) override { return false; }
last_sent_packet()130   const RtpPacketReceived& last_sent_packet() { return sent_packets_.back(); }
packets_sent()131   int packets_sent() { return sent_packets_.size(); }
132 
133   size_t total_bytes_sent_;
134   PacketOptions last_options_;
135   std::vector<RtpPacketReceived> sent_packets_;
136 
137  private:
138   RtpHeaderExtensionMap receivers_extensions_;
139 };
140 
141 MATCHER_P(SameRtcEventTypeAs, value, "") {
142   return value == arg->GetType();
143 }
144 
145 struct TestConfig {
TestConfigwebrtc::__anonc0b34bd50111::TestConfig146   TestConfig(bool with_overhead, bool deferred_fec)
147       : with_overhead(with_overhead), deferred_fec(deferred_fec) {}
148   bool with_overhead = false;
149   bool deferred_fec = false;
150 };
151 
152 class MockRtpPacketPacer : public RtpPacketSender {
153  public:
MockRtpPacketPacer()154   MockRtpPacketPacer() {}
~MockRtpPacketPacer()155   virtual ~MockRtpPacketPacer() {}
156 
157   MOCK_METHOD(void,
158               EnqueuePackets,
159               (std::vector<std::unique_ptr<RtpPacketToSend>>),
160               (override));
161 };
162 
163 class MockSendSideDelayObserver : public SendSideDelayObserver {
164  public:
165   MOCK_METHOD(void,
166               SendSideDelayUpdated,
167               (int, int, uint64_t, uint32_t),
168               (override));
169 };
170 
171 class MockSendPacketObserver : public SendPacketObserver {
172  public:
173   MOCK_METHOD(void, OnSendPacket, (uint16_t, int64_t, uint32_t), (override));
174 };
175 
176 class MockTransportFeedbackObserver : public TransportFeedbackObserver {
177  public:
178   MOCK_METHOD(void, OnAddPacket, (const RtpPacketSendInfo&), (override));
179   MOCK_METHOD(void,
180               OnTransportFeedback,
181               (const rtcp::TransportFeedback&),
182               (override));
183 };
184 
185 class StreamDataTestCallback : public StreamDataCountersCallback {
186  public:
StreamDataTestCallback()187   StreamDataTestCallback()
188       : StreamDataCountersCallback(), ssrc_(0), counters_() {}
189   ~StreamDataTestCallback() override = default;
190 
DataCountersUpdated(const StreamDataCounters & counters,uint32_t ssrc)191   void DataCountersUpdated(const StreamDataCounters& counters,
192                            uint32_t ssrc) override {
193     ssrc_ = ssrc;
194     counters_ = counters;
195   }
196 
197   uint32_t ssrc_;
198   StreamDataCounters counters_;
199 
MatchPacketCounter(const RtpPacketCounter & expected,const RtpPacketCounter & actual)200   void MatchPacketCounter(const RtpPacketCounter& expected,
201                           const RtpPacketCounter& actual) {
202     EXPECT_EQ(expected.payload_bytes, actual.payload_bytes);
203     EXPECT_EQ(expected.header_bytes, actual.header_bytes);
204     EXPECT_EQ(expected.padding_bytes, actual.padding_bytes);
205     EXPECT_EQ(expected.packets, actual.packets);
206   }
207 
Matches(uint32_t ssrc,const StreamDataCounters & counters)208   void Matches(uint32_t ssrc, const StreamDataCounters& counters) {
209     EXPECT_EQ(ssrc, ssrc_);
210     MatchPacketCounter(counters.transmitted, counters_.transmitted);
211     MatchPacketCounter(counters.retransmitted, counters_.retransmitted);
212     EXPECT_EQ(counters.fec.packets, counters_.fec.packets);
213   }
214 };
215 
216 class TaskQueuePacketSender : public RtpPacketSender {
217  public:
TaskQueuePacketSender(TimeController * time_controller,std::unique_ptr<RtpPacketSender> packet_sender)218   TaskQueuePacketSender(TimeController* time_controller,
219                         std::unique_ptr<RtpPacketSender> packet_sender)
220       : time_controller_(time_controller),
221         packet_sender_(std::move(packet_sender)),
222         queue_(time_controller_->CreateTaskQueueFactory()->CreateTaskQueue(
223             "PacerQueue",
224             TaskQueueFactory::Priority::NORMAL)) {}
225 
EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets)226   void EnqueuePackets(
227       std::vector<std::unique_ptr<RtpPacketToSend>> packets) override {
228     queue_->PostTask(ToQueuedTask([sender = packet_sender_.get(),
229                                    packets_ = std::move(packets)]() mutable {
230       sender->EnqueuePackets(std::move(packets_));
231     }));
232     // Trigger task we just enqueued to be executed by updating the simulated
233     // time controller.
234     time_controller_->AdvanceTime(TimeDelta::Zero());
235   }
236 
task_queue() const237   TaskQueueBase* task_queue() const { return queue_.get(); }
238 
239   TimeController* const time_controller_;
240   std::unique_ptr<RtpPacketSender> packet_sender_;
241   std::unique_ptr<TaskQueueBase, TaskQueueDeleter> queue_;
242 };
243 
244 // Mimics ModuleRtpRtcp::RtpSenderContext.
245 // TODO(sprang): Split up unit tests and test these components individually
246 // wherever possible.
247 struct RtpSenderContext : public SequenceNumberAssigner {
RtpSenderContextwebrtc::__anonc0b34bd50111::RtpSenderContext248   RtpSenderContext(const RtpRtcpInterface::Configuration& config,
249                    TimeController* time_controller)
250       : time_controller_(time_controller),
251         packet_history_(config.clock, config.enable_rtx_padding_prioritization),
252         packet_sender_(config, &packet_history_),
253         pacer_(time_controller,
254                std::make_unique<RtpSenderEgress::NonPacedPacketSender>(
255                    &packet_sender_,
256                    this)),
257         packet_generator_(config,
258                           &packet_history_,
259                           config.paced_sender ? config.paced_sender : &pacer_) {
260   }
AssignSequenceNumberwebrtc::__anonc0b34bd50111::RtpSenderContext261   void AssignSequenceNumber(RtpPacketToSend* packet) override {
262     packet_generator_.AssignSequenceNumber(packet);
263   }
264   // Inject packet straight into RtpSenderEgress without passing through the
265   // pacer, but while still running on the pacer task queue.
InjectPacketwebrtc::__anonc0b34bd50111::RtpSenderContext266   void InjectPacket(std::unique_ptr<RtpPacketToSend> packet,
267                     const PacedPacketInfo& packet_info) {
268     pacer_.task_queue()->PostTask(
269         ToQueuedTask([sender_ = &packet_sender_, packet_ = std::move(packet),
270                       packet_info]() mutable {
271           sender_->SendPacket(packet_.get(), packet_info);
272         }));
273     time_controller_->AdvanceTime(TimeDelta::Zero());
274   }
275   TimeController* time_controller_;
276   RtpPacketHistory packet_history_;
277   RtpSenderEgress packet_sender_;
278   TaskQueuePacketSender pacer_;
279   RTPSender packet_generator_;
280 };
281 
282 class FieldTrialConfig : public WebRtcKeyValueConfig {
283  public:
FieldTrialConfig()284   FieldTrialConfig()
285       : overhead_enabled_(false),
286         deferred_fec_(false),
287         max_padding_factor_(1200) {}
~FieldTrialConfig()288   ~FieldTrialConfig() override {}
289 
SetOverHeadEnabled(bool enabled)290   void SetOverHeadEnabled(bool enabled) { overhead_enabled_ = enabled; }
UseDeferredFec(bool enabled)291   void UseDeferredFec(bool enabled) { deferred_fec_ = enabled; }
SetMaxPaddingFactor(double factor)292   void SetMaxPaddingFactor(double factor) { max_padding_factor_ = factor; }
293 
Lookup(absl::string_view key) const294   std::string Lookup(absl::string_view key) const override {
295     if (key == "WebRTC-LimitPaddingSize") {
296       char string_buf[32];
297       rtc::SimpleStringBuilder ssb(string_buf);
298       ssb << "factor:" << max_padding_factor_;
299       return ssb.str();
300     } else if (key == "WebRTC-SendSideBwe-WithOverhead") {
301       return overhead_enabled_ ? "Enabled" : "Disabled";
302     } else if (key == "WebRTC-DeferredFecGeneration") {
303       return deferred_fec_ ? "Enabled" : "Disabled";
304     }
305     return "";
306   }
307 
308  private:
309   bool overhead_enabled_;
310   bool deferred_fec_;
311   double max_padding_factor_;
312 };
313 
314 }  // namespace
315 
316 class RtpSenderTest : public ::testing::TestWithParam<TestConfig> {
317  protected:
RtpSenderTest()318   RtpSenderTest()
319       : time_controller_(Timestamp::Millis(kStartTime)),
320         clock_(time_controller_.GetClock()),
321         retransmission_rate_limiter_(clock_, 1000),
322         flexfec_sender_(0,
323                         kFlexFecSsrc,
324                         kSsrc,
325                         "",
326                         std::vector<RtpExtension>(),
327                         std::vector<RtpExtensionSize>(),
328                         nullptr,
329                         clock_),
330         kMarkerBit(true) {
331     field_trials_.SetOverHeadEnabled(GetParam().with_overhead);
332     field_trials_.UseDeferredFec(GetParam().deferred_fec);
333   }
334 
SetUp()335   void SetUp() override { SetUpRtpSender(true, false, false); }
336 
rtp_sender()337   RTPSender* rtp_sender() {
338     RTC_DCHECK(rtp_sender_context_);
339     return &rtp_sender_context_->packet_generator_;
340   }
341 
rtp_egress()342   RtpSenderEgress* rtp_egress() {
343     RTC_DCHECK(rtp_sender_context_);
344     return &rtp_sender_context_->packet_sender_;
345   }
346 
SetUpRtpSender(bool pacer,bool populate_network2,bool always_send_mid_and_rid)347   void SetUpRtpSender(bool pacer,
348                       bool populate_network2,
349                       bool always_send_mid_and_rid) {
350     SetUpRtpSender(pacer, populate_network2, always_send_mid_and_rid,
351                    &flexfec_sender_);
352   }
353 
SetUpRtpSender(bool pacer,bool populate_network2,bool always_send_mid_and_rid,VideoFecGenerator * fec_generator)354   void SetUpRtpSender(bool pacer,
355                       bool populate_network2,
356                       bool always_send_mid_and_rid,
357                       VideoFecGenerator* fec_generator) {
358     RtpRtcpInterface::Configuration config;
359     config.clock = clock_;
360     config.outgoing_transport = &transport_;
361     config.local_media_ssrc = kSsrc;
362     config.rtx_send_ssrc = kRtxSsrc;
363     config.fec_generator = fec_generator;
364     config.event_log = &mock_rtc_event_log_;
365     config.send_packet_observer = &send_packet_observer_;
366     config.retransmission_rate_limiter = &retransmission_rate_limiter_;
367     config.paced_sender = pacer ? &mock_paced_sender_ : nullptr;
368     config.populate_network2_timestamp = populate_network2;
369     config.rtp_stats_callback = &rtp_stats_callback_;
370     config.always_send_mid_and_rid = always_send_mid_and_rid;
371     config.field_trials = &field_trials_;
372 
373     rtp_sender_context_ =
374         std::make_unique<RtpSenderContext>(config, &time_controller_);
375     rtp_sender()->SetSequenceNumber(kSeqNum);
376     rtp_sender()->SetTimestampOffset(0);
377   }
378 
379   GlobalSimulatedTimeController time_controller_;
380   Clock* const clock_;
381   NiceMock<MockRtcEventLog> mock_rtc_event_log_;
382   MockRtpPacketPacer mock_paced_sender_;
383   StrictMock<MockSendPacketObserver> send_packet_observer_;
384   StrictMock<MockTransportFeedbackObserver> feedback_observer_;
385   RateLimiter retransmission_rate_limiter_;
386   FlexfecSender flexfec_sender_;
387 
388   std::unique_ptr<RtpSenderContext> rtp_sender_context_;
389 
390   LoopbackTransportTest transport_;
391   const bool kMarkerBit;
392   FieldTrialConfig field_trials_;
393   StreamDataTestCallback rtp_stats_callback_;
394 
BuildRtpPacket(int payload_type,bool marker_bit,uint32_t timestamp,int64_t capture_time_ms)395   std::unique_ptr<RtpPacketToSend> BuildRtpPacket(int payload_type,
396                                                   bool marker_bit,
397                                                   uint32_t timestamp,
398                                                   int64_t capture_time_ms) {
399     auto packet = rtp_sender()->AllocatePacket();
400     packet->SetPayloadType(payload_type);
401     packet->set_packet_type(RtpPacketMediaType::kVideo);
402     packet->SetMarker(marker_bit);
403     packet->SetTimestamp(timestamp);
404     packet->set_capture_time_ms(capture_time_ms);
405     EXPECT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
406     return packet;
407   }
408 
SendPacket(int64_t capture_time_ms,int payload_length)409   std::unique_ptr<RtpPacketToSend> SendPacket(int64_t capture_time_ms,
410                                               int payload_length) {
411     uint32_t timestamp = capture_time_ms * 90;
412     auto packet =
413         BuildRtpPacket(kPayload, kMarkerBit, timestamp, capture_time_ms);
414     packet->AllocatePayload(payload_length);
415     packet->set_allow_retransmission(true);
416 
417     // Packet should be stored in a send bucket.
418     EXPECT_TRUE(rtp_sender()->SendToNetwork(
419         std::make_unique<RtpPacketToSend>(*packet)));
420     return packet;
421   }
422 
SendGenericPacket()423   std::unique_ptr<RtpPacketToSend> SendGenericPacket() {
424     const int64_t kCaptureTimeMs = clock_->TimeInMilliseconds();
425     return SendPacket(kCaptureTimeMs, sizeof(kPayloadData));
426   }
427 
GenerateAndSendPadding(size_t target_size_bytes)428   size_t GenerateAndSendPadding(size_t target_size_bytes) {
429     size_t generated_bytes = 0;
430     for (auto& packet :
431          rtp_sender()->GeneratePadding(target_size_bytes, true)) {
432       generated_bytes += packet->payload_size() + packet->padding_size();
433       rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
434     }
435     return generated_bytes;
436   }
437 
438   // The following are helpers for configuring the RTPSender. They must be
439   // called before sending any packets.
440 
441   // Enable the retransmission stream with sizable packet storage.
EnableRtx()442   void EnableRtx() {
443     // RTX needs to be able to read the source packets from the packet store.
444     // Pick a number of packets to store big enough for any unit test.
445     constexpr uint16_t kNumberOfPacketsToStore = 100;
446     rtp_sender_context_->packet_history_.SetStorePacketsStatus(
447         RtpPacketHistory::StorageMode::kStoreAndCull, kNumberOfPacketsToStore);
448     rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
449     rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
450   }
451 
452   // Enable sending of the MID header extension for both the primary SSRC and
453   // the RTX SSRC.
EnableMidSending(const std::string & mid)454   void EnableMidSending(const std::string& mid) {
455     rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionMid, kMidExtensionId);
456     rtp_sender()->SetMid(mid);
457   }
458 
459   // Enable sending of the RSID header extension for the primary SSRC and the
460   // RRSID header extension for the RTX SSRC.
EnableRidSending(const std::string & rid)461   void EnableRidSending(const std::string& rid) {
462     rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionRtpStreamId,
463                                              kRidExtensionId);
464     rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionRepairedRtpStreamId,
465                                              kRepairedRidExtensionId);
466     rtp_sender()->SetRid(rid);
467   }
468 };
469 
470 // TODO(pbos): Move tests over from WithoutPacer to RtpSenderTest as this is our
471 // default code path.
472 class RtpSenderTestWithoutPacer : public RtpSenderTest {
473  public:
SetUp()474   void SetUp() override { SetUpRtpSender(false, false, false); }
475 };
476 
TEST_P(RtpSenderTestWithoutPacer,AllocatePacketSetCsrc)477 TEST_P(RtpSenderTestWithoutPacer, AllocatePacketSetCsrc) {
478   // Configure rtp_sender with csrc.
479   std::vector<uint32_t> csrcs;
480   csrcs.push_back(0x23456789);
481   rtp_sender()->SetCsrcs(csrcs);
482 
483   auto packet = rtp_sender()->AllocatePacket();
484 
485   ASSERT_TRUE(packet);
486   EXPECT_EQ(rtp_sender()->SSRC(), packet->Ssrc());
487   EXPECT_EQ(csrcs, packet->Csrcs());
488 }
489 
TEST_P(RtpSenderTestWithoutPacer,AllocatePacketReserveExtensions)490 TEST_P(RtpSenderTestWithoutPacer, AllocatePacketReserveExtensions) {
491   // Configure rtp_sender with extensions.
492   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
493                    kRtpExtensionTransmissionTimeOffset,
494                    kTransmissionTimeOffsetExtensionId));
495   ASSERT_EQ(0,
496             rtp_sender()->RegisterRtpHeaderExtension(
497                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
498   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
499                    kRtpExtensionAudioLevel, kAudioLevelExtensionId));
500   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
501                    kRtpExtensionTransportSequenceNumber,
502                    kTransportSequenceNumberExtensionId));
503   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
504                    kRtpExtensionVideoRotation, kVideoRotationExtensionId));
505 
506   auto packet = rtp_sender()->AllocatePacket();
507 
508   ASSERT_TRUE(packet);
509   // Preallocate BWE extensions RtpSender set itself.
510   EXPECT_TRUE(packet->HasExtension<TransmissionOffset>());
511   EXPECT_TRUE(packet->HasExtension<AbsoluteSendTime>());
512   EXPECT_TRUE(packet->HasExtension<TransportSequenceNumber>());
513   // Do not allocate media specific extensions.
514   EXPECT_FALSE(packet->HasExtension<AudioLevel>());
515   EXPECT_FALSE(packet->HasExtension<VideoOrientation>());
516 }
517 
TEST_P(RtpSenderTestWithoutPacer,AssignSequenceNumberAdvanceSequenceNumber)518 TEST_P(RtpSenderTestWithoutPacer, AssignSequenceNumberAdvanceSequenceNumber) {
519   auto packet = rtp_sender()->AllocatePacket();
520   ASSERT_TRUE(packet);
521   const uint16_t sequence_number = rtp_sender()->SequenceNumber();
522 
523   EXPECT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
524 
525   EXPECT_EQ(sequence_number, packet->SequenceNumber());
526   EXPECT_EQ(sequence_number + 1, rtp_sender()->SequenceNumber());
527 }
528 
TEST_P(RtpSenderTestWithoutPacer,AssignSequenceNumberFailsOnNotSending)529 TEST_P(RtpSenderTestWithoutPacer, AssignSequenceNumberFailsOnNotSending) {
530   auto packet = rtp_sender()->AllocatePacket();
531   ASSERT_TRUE(packet);
532 
533   rtp_sender()->SetSendingMediaStatus(false);
534   EXPECT_FALSE(rtp_sender()->AssignSequenceNumber(packet.get()));
535 }
536 
TEST_P(RtpSenderTestWithoutPacer,AssignSequenceNumberMayAllowPaddingOnVideo)537 TEST_P(RtpSenderTestWithoutPacer, AssignSequenceNumberMayAllowPaddingOnVideo) {
538   constexpr size_t kPaddingSize = 100;
539   auto packet = rtp_sender()->AllocatePacket();
540   ASSERT_TRUE(packet);
541 
542   ASSERT_TRUE(rtp_sender()->GeneratePadding(kPaddingSize, true).empty());
543   packet->SetMarker(false);
544   ASSERT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
545   // Packet without marker bit doesn't allow padding on video stream.
546   ASSERT_TRUE(rtp_sender()->GeneratePadding(kPaddingSize, true).empty());
547 
548   packet->SetMarker(true);
549   ASSERT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
550   // Packet with marker bit allows send padding.
551   ASSERT_FALSE(rtp_sender()->GeneratePadding(kPaddingSize, true).empty());
552 }
553 
TEST_P(RtpSenderTest,AssignSequenceNumberAllowsPaddingOnAudio)554 TEST_P(RtpSenderTest, AssignSequenceNumberAllowsPaddingOnAudio) {
555   MockTransport transport;
556   RtpRtcpInterface::Configuration config;
557   config.audio = true;
558   config.clock = clock_;
559   config.outgoing_transport = &transport;
560   config.paced_sender = &mock_paced_sender_;
561   config.local_media_ssrc = kSsrc;
562   config.event_log = &mock_rtc_event_log_;
563   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
564   rtp_sender_context_ =
565       std::make_unique<RtpSenderContext>(config, &time_controller_);
566 
567   rtp_sender()->SetTimestampOffset(0);
568 
569   std::unique_ptr<RtpPacketToSend> audio_packet =
570       rtp_sender()->AllocatePacket();
571   // Padding on audio stream allowed regardless of marker in the last packet.
572   audio_packet->SetMarker(false);
573   audio_packet->SetPayloadType(kPayload);
574   rtp_sender()->AssignSequenceNumber(audio_packet.get());
575 
576   const size_t kPaddingSize = 59;
577   EXPECT_CALL(transport, SendRtp(_, kPaddingSize + kRtpHeaderSize, _))
578       .WillOnce(Return(true));
579   EXPECT_EQ(kPaddingSize, GenerateAndSendPadding(kPaddingSize));
580 
581   // Requested padding size is too small, will send a larger one.
582   const size_t kMinPaddingSize = 50;
583   EXPECT_CALL(transport, SendRtp(_, kMinPaddingSize + kRtpHeaderSize, _))
584       .WillOnce(Return(true));
585   EXPECT_EQ(kMinPaddingSize, GenerateAndSendPadding(kMinPaddingSize - 5));
586 }
587 
TEST_P(RtpSenderTestWithoutPacer,AssignSequenceNumberSetPaddingTimestamps)588 TEST_P(RtpSenderTestWithoutPacer, AssignSequenceNumberSetPaddingTimestamps) {
589   constexpr size_t kPaddingSize = 100;
590   auto packet = rtp_sender()->AllocatePacket();
591   ASSERT_TRUE(packet);
592   packet->SetMarker(true);
593   packet->SetTimestamp(kTimestamp);
594 
595   ASSERT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
596   auto padding_packets = rtp_sender()->GeneratePadding(kPaddingSize, true);
597 
598   ASSERT_EQ(1u, padding_packets.size());
599   // Verify padding packet timestamp.
600   EXPECT_EQ(kTimestamp, padding_packets[0]->Timestamp());
601 }
602 
TEST_P(RtpSenderTestWithoutPacer,TransportFeedbackObserverGetsCorrectByteCount)603 TEST_P(RtpSenderTestWithoutPacer,
604        TransportFeedbackObserverGetsCorrectByteCount) {
605   constexpr size_t kRtpOverheadBytesPerPacket = 12 + 8;
606 
607   RtpRtcpInterface::Configuration config;
608   config.clock = clock_;
609   config.outgoing_transport = &transport_;
610   config.local_media_ssrc = kSsrc;
611   config.transport_feedback_callback = &feedback_observer_;
612   config.event_log = &mock_rtc_event_log_;
613   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
614   config.field_trials = &field_trials_;
615   rtp_sender_context_ =
616       std::make_unique<RtpSenderContext>(config, &time_controller_);
617 
618   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
619                    kRtpExtensionTransportSequenceNumber,
620                    kTransportSequenceNumberExtensionId));
621 
622   const size_t expected_bytes =
623       GetParam().with_overhead
624           ? sizeof(kPayloadData) + kRtpOverheadBytesPerPacket
625           : sizeof(kPayloadData);
626 
627   EXPECT_CALL(feedback_observer_,
628               OnAddPacket(AllOf(
629                   Field(&RtpPacketSendInfo::ssrc, rtp_sender()->SSRC()),
630                   Field(&RtpPacketSendInfo::transport_sequence_number,
631                         kTransportSequenceNumber),
632                   Field(&RtpPacketSendInfo::rtp_sequence_number,
633                         rtp_sender()->SequenceNumber()),
634                   Field(&RtpPacketSendInfo::length, expected_bytes),
635                   Field(&RtpPacketSendInfo::pacing_info, PacedPacketInfo()))))
636       .Times(1);
637   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(),
638             kRtpOverheadBytesPerPacket);
639   SendGenericPacket();
640 }
641 
TEST_P(RtpSenderTestWithoutPacer,SendsPacketsWithTransportSequenceNumber)642 TEST_P(RtpSenderTestWithoutPacer, SendsPacketsWithTransportSequenceNumber) {
643   RtpRtcpInterface::Configuration config;
644   config.clock = clock_;
645   config.outgoing_transport = &transport_;
646   config.local_media_ssrc = kSsrc;
647   config.transport_feedback_callback = &feedback_observer_;
648   config.event_log = &mock_rtc_event_log_;
649   config.send_packet_observer = &send_packet_observer_;
650   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
651   rtp_sender_context_ =
652       std::make_unique<RtpSenderContext>(config, &time_controller_);
653 
654   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
655                    kRtpExtensionTransportSequenceNumber,
656                    kTransportSequenceNumberExtensionId));
657 
658   EXPECT_CALL(send_packet_observer_,
659               OnSendPacket(kTransportSequenceNumber, _, _))
660       .Times(1);
661 
662   EXPECT_CALL(feedback_observer_,
663               OnAddPacket(AllOf(
664                   Field(&RtpPacketSendInfo::ssrc, rtp_sender()->SSRC()),
665                   Field(&RtpPacketSendInfo::transport_sequence_number,
666                         kTransportSequenceNumber),
667                   Field(&RtpPacketSendInfo::rtp_sequence_number,
668                         rtp_sender()->SequenceNumber()),
669                   Field(&RtpPacketSendInfo::pacing_info, PacedPacketInfo()))))
670       .Times(1);
671 
672   SendGenericPacket();
673 
674   const auto& packet = transport_.last_sent_packet();
675   uint16_t transport_seq_no;
676   ASSERT_TRUE(packet.GetExtension<TransportSequenceNumber>(&transport_seq_no));
677   EXPECT_EQ(kTransportSequenceNumber, transport_seq_no);
678   EXPECT_EQ(transport_.last_options_.packet_id, transport_seq_no);
679   EXPECT_TRUE(transport_.last_options_.included_in_allocation);
680 }
681 
TEST_P(RtpSenderTestWithoutPacer,PacketOptionsNoRetransmission)682 TEST_P(RtpSenderTestWithoutPacer, PacketOptionsNoRetransmission) {
683   RtpRtcpInterface::Configuration config;
684   config.clock = clock_;
685   config.outgoing_transport = &transport_;
686   config.local_media_ssrc = kSsrc;
687   config.transport_feedback_callback = &feedback_observer_;
688   config.event_log = &mock_rtc_event_log_;
689   config.send_packet_observer = &send_packet_observer_;
690   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
691   rtp_sender_context_ =
692       std::make_unique<RtpSenderContext>(config, &time_controller_);
693 
694   SendGenericPacket();
695 
696   EXPECT_FALSE(transport_.last_options_.is_retransmit);
697 }
698 
TEST_P(RtpSenderTestWithoutPacer,SetsIncludedInFeedbackWhenTransportSequenceNumberExtensionIsRegistered)699 TEST_P(RtpSenderTestWithoutPacer,
700        SetsIncludedInFeedbackWhenTransportSequenceNumberExtensionIsRegistered) {
701   SetUpRtpSender(false, false, false);
702   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber,
703                                            kTransportSequenceNumberExtensionId);
704   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
705   SendGenericPacket();
706   EXPECT_TRUE(transport_.last_options_.included_in_feedback);
707 }
708 
TEST_P(RtpSenderTestWithoutPacer,SetsIncludedInAllocationWhenTransportSequenceNumberExtensionIsRegistered)709 TEST_P(
710     RtpSenderTestWithoutPacer,
711     SetsIncludedInAllocationWhenTransportSequenceNumberExtensionIsRegistered) {
712   SetUpRtpSender(false, false, false);
713   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionTransportSequenceNumber,
714                                            kTransportSequenceNumberExtensionId);
715   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
716   SendGenericPacket();
717   EXPECT_TRUE(transport_.last_options_.included_in_allocation);
718 }
719 
TEST_P(RtpSenderTestWithoutPacer,SetsIncludedInAllocationWhenForcedAsPartOfAllocation)720 TEST_P(RtpSenderTestWithoutPacer,
721        SetsIncludedInAllocationWhenForcedAsPartOfAllocation) {
722   SetUpRtpSender(false, false, false);
723   rtp_egress()->ForceIncludeSendPacketsInAllocation(true);
724   SendGenericPacket();
725   EXPECT_FALSE(transport_.last_options_.included_in_feedback);
726   EXPECT_TRUE(transport_.last_options_.included_in_allocation);
727 }
728 
TEST_P(RtpSenderTestWithoutPacer,DoesnSetIncludedInAllocationByDefault)729 TEST_P(RtpSenderTestWithoutPacer, DoesnSetIncludedInAllocationByDefault) {
730   SetUpRtpSender(false, false, false);
731   SendGenericPacket();
732   EXPECT_FALSE(transport_.last_options_.included_in_feedback);
733   EXPECT_FALSE(transport_.last_options_.included_in_allocation);
734 }
735 
TEST_P(RtpSenderTestWithoutPacer,OnSendSideDelayUpdated)736 TEST_P(RtpSenderTestWithoutPacer, OnSendSideDelayUpdated) {
737   StrictMock<MockSendSideDelayObserver> send_side_delay_observer_;
738 
739   RtpRtcpInterface::Configuration config;
740   config.clock = clock_;
741   config.outgoing_transport = &transport_;
742   config.local_media_ssrc = kSsrc;
743   config.send_side_delay_observer = &send_side_delay_observer_;
744   config.event_log = &mock_rtc_event_log_;
745   rtp_sender_context_ =
746       std::make_unique<RtpSenderContext>(config, &time_controller_);
747 
748   FieldTrialBasedConfig field_trials;
749   RTPSenderVideo::Config video_config;
750   video_config.clock = clock_;
751   video_config.rtp_sender = rtp_sender();
752   video_config.field_trials = &field_trials;
753   RTPSenderVideo rtp_sender_video(video_config);
754 
755   const uint8_t kPayloadType = 127;
756   const absl::optional<VideoCodecType> kCodecType =
757       VideoCodecType::kVideoCodecGeneric;
758 
759   const uint32_t kCaptureTimeMsToRtpTimestamp = 90;  // 90 kHz clock
760   RTPVideoHeader video_header;
761 
762   // Send packet with 10 ms send-side delay. The average, max and total should
763   // be 10 ms.
764   EXPECT_CALL(send_side_delay_observer_,
765               SendSideDelayUpdated(10, 10, 10, kSsrc))
766       .Times(1);
767   int64_t capture_time_ms = clock_->TimeInMilliseconds();
768   time_controller_.AdvanceTime(TimeDelta::Millis(10));
769   video_header.frame_type = VideoFrameType::kVideoFrameKey;
770   EXPECT_TRUE(rtp_sender_video.SendVideo(
771       kPayloadType, kCodecType, capture_time_ms * kCaptureTimeMsToRtpTimestamp,
772       capture_time_ms, kPayloadData, video_header,
773       kDefaultExpectedRetransmissionTimeMs));
774 
775   // Send another packet with 20 ms delay. The average, max and total should be
776   // 15, 20 and 30 ms respectively.
777   EXPECT_CALL(send_side_delay_observer_,
778               SendSideDelayUpdated(15, 20, 30, kSsrc))
779       .Times(1);
780   time_controller_.AdvanceTime(TimeDelta::Millis(10));
781   video_header.frame_type = VideoFrameType::kVideoFrameKey;
782   EXPECT_TRUE(rtp_sender_video.SendVideo(
783       kPayloadType, kCodecType, capture_time_ms * kCaptureTimeMsToRtpTimestamp,
784       capture_time_ms, kPayloadData, video_header,
785       kDefaultExpectedRetransmissionTimeMs));
786 
787   // Send another packet at the same time, which replaces the last packet.
788   // Since this packet has 0 ms delay, the average is now 5 ms and max is 10 ms.
789   // The total counter stays the same though.
790   // TODO(terelius): Is is not clear that this is the right behavior.
791   EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(5, 10, 30, kSsrc))
792       .Times(1);
793   capture_time_ms = clock_->TimeInMilliseconds();
794   video_header.frame_type = VideoFrameType::kVideoFrameKey;
795   EXPECT_TRUE(rtp_sender_video.SendVideo(
796       kPayloadType, kCodecType, capture_time_ms * kCaptureTimeMsToRtpTimestamp,
797       capture_time_ms, kPayloadData, video_header,
798       kDefaultExpectedRetransmissionTimeMs));
799 
800   // Send a packet 1 second later. The earlier packets should have timed
801   // out, so both max and average should be the delay of this packet. The total
802   // keeps increasing.
803   time_controller_.AdvanceTime(TimeDelta::Millis(1000));
804   capture_time_ms = clock_->TimeInMilliseconds();
805   time_controller_.AdvanceTime(TimeDelta::Millis(1));
806   EXPECT_CALL(send_side_delay_observer_, SendSideDelayUpdated(1, 1, 31, kSsrc))
807       .Times(1);
808   video_header.frame_type = VideoFrameType::kVideoFrameKey;
809   EXPECT_TRUE(rtp_sender_video.SendVideo(
810       kPayloadType, kCodecType, capture_time_ms * kCaptureTimeMsToRtpTimestamp,
811       capture_time_ms, kPayloadData, video_header,
812       kDefaultExpectedRetransmissionTimeMs));
813 }
814 
TEST_P(RtpSenderTestWithoutPacer,OnSendPacketUpdated)815 TEST_P(RtpSenderTestWithoutPacer, OnSendPacketUpdated) {
816   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
817                    kRtpExtensionTransportSequenceNumber,
818                    kTransportSequenceNumberExtensionId));
819   EXPECT_CALL(send_packet_observer_,
820               OnSendPacket(kTransportSequenceNumber, _, _))
821       .Times(1);
822 
823   SendGenericPacket();
824 }
825 
TEST_P(RtpSenderTest,SendsPacketsWithTransportSequenceNumber)826 TEST_P(RtpSenderTest, SendsPacketsWithTransportSequenceNumber) {
827   RtpRtcpInterface::Configuration config;
828   config.clock = clock_;
829   config.outgoing_transport = &transport_;
830   config.paced_sender = &mock_paced_sender_;
831   config.local_media_ssrc = kSsrc;
832   config.transport_feedback_callback = &feedback_observer_;
833   config.event_log = &mock_rtc_event_log_;
834   config.send_packet_observer = &send_packet_observer_;
835   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
836   rtp_sender_context_ =
837       std::make_unique<RtpSenderContext>(config, &time_controller_);
838 
839   rtp_sender()->SetSequenceNumber(kSeqNum);
840   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
841       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
842   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
843                    kRtpExtensionTransportSequenceNumber,
844                    kTransportSequenceNumberExtensionId));
845 
846   EXPECT_CALL(send_packet_observer_,
847               OnSendPacket(kTransportSequenceNumber, _, _))
848       .Times(1);
849   EXPECT_CALL(feedback_observer_,
850               OnAddPacket(AllOf(
851                   Field(&RtpPacketSendInfo::ssrc, rtp_sender()->SSRC()),
852                   Field(&RtpPacketSendInfo::transport_sequence_number,
853                         kTransportSequenceNumber),
854                   Field(&RtpPacketSendInfo::rtp_sequence_number,
855                         rtp_sender()->SequenceNumber()),
856                   Field(&RtpPacketSendInfo::pacing_info, PacedPacketInfo()))))
857       .Times(1);
858 
859   EXPECT_CALL(
860       mock_paced_sender_,
861       EnqueuePackets(Contains(AllOf(
862           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
863           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
864   auto packet = SendGenericPacket();
865   packet->set_packet_type(RtpPacketMediaType::kVideo);
866   // Transport sequence number is set by PacketRouter, before SendPacket().
867   packet->SetExtension<TransportSequenceNumber>(kTransportSequenceNumber);
868   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
869 
870   uint16_t transport_seq_no;
871   EXPECT_TRUE(
872       transport_.last_sent_packet().GetExtension<TransportSequenceNumber>(
873           &transport_seq_no));
874   EXPECT_EQ(kTransportSequenceNumber, transport_seq_no);
875   EXPECT_EQ(transport_.last_options_.packet_id, transport_seq_no);
876 }
877 
TEST_P(RtpSenderTest,WritesPacerExitToTimingExtension)878 TEST_P(RtpSenderTest, WritesPacerExitToTimingExtension) {
879   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
880       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
881   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
882                    kRtpExtensionVideoTiming, kVideoTimingExtensionId));
883   int64_t capture_time_ms = clock_->TimeInMilliseconds();
884   auto packet = rtp_sender()->AllocatePacket();
885   packet->SetPayloadType(kPayload);
886   packet->SetMarker(true);
887   packet->SetTimestamp(kTimestamp);
888   packet->set_capture_time_ms(capture_time_ms);
889   const VideoSendTiming kVideoTiming = {0u, 0u, 0u, 0u, 0u, 0u, true};
890   packet->SetExtension<VideoTimingExtension>(kVideoTiming);
891   EXPECT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
892   size_t packet_size = packet->size();
893 
894   const int kStoredTimeInMs = 100;
895   packet->set_packet_type(RtpPacketMediaType::kVideo);
896   packet->set_allow_retransmission(true);
897   EXPECT_CALL(mock_paced_sender_, EnqueuePackets(Contains(Pointee(Property(
898                                       &RtpPacketToSend::Ssrc, kSsrc)))));
899   EXPECT_TRUE(
900       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
901   time_controller_.AdvanceTime(TimeDelta::Millis(kStoredTimeInMs));
902   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
903   EXPECT_EQ(1, transport_.packets_sent());
904   EXPECT_EQ(packet_size, transport_.last_sent_packet().size());
905 
906   VideoSendTiming video_timing;
907   EXPECT_TRUE(transport_.last_sent_packet().GetExtension<VideoTimingExtension>(
908       &video_timing));
909   EXPECT_EQ(kStoredTimeInMs, video_timing.pacer_exit_delta_ms);
910 }
911 
TEST_P(RtpSenderTest,WritesNetwork2ToTimingExtensionWithPacer)912 TEST_P(RtpSenderTest, WritesNetwork2ToTimingExtensionWithPacer) {
913   SetUpRtpSender(/*pacer=*/true, /*populate_network2=*/true, false);
914   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
915       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
916   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
917                    kRtpExtensionVideoTiming, kVideoTimingExtensionId));
918   int64_t capture_time_ms = clock_->TimeInMilliseconds();
919   auto packet = rtp_sender()->AllocatePacket();
920   packet->SetPayloadType(kPayload);
921   packet->SetMarker(true);
922   packet->SetTimestamp(kTimestamp);
923   packet->set_capture_time_ms(capture_time_ms);
924   const uint16_t kPacerExitMs = 1234u;
925   const VideoSendTiming kVideoTiming = {0u, 0u, 0u, kPacerExitMs, 0u, 0u, true};
926   packet->SetExtension<VideoTimingExtension>(kVideoTiming);
927   EXPECT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
928   size_t packet_size = packet->size();
929 
930   const int kStoredTimeInMs = 100;
931 
932   packet->set_packet_type(RtpPacketMediaType::kVideo);
933   packet->set_allow_retransmission(true);
934   EXPECT_CALL(mock_paced_sender_, EnqueuePackets(Contains(Pointee(Property(
935                                       &RtpPacketToSend::Ssrc, kSsrc)))));
936   EXPECT_TRUE(
937       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
938   time_controller_.AdvanceTime(TimeDelta::Millis(kStoredTimeInMs));
939   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
940 
941   EXPECT_EQ(1, transport_.packets_sent());
942   EXPECT_EQ(packet_size, transport_.last_sent_packet().size());
943 
944   VideoSendTiming video_timing;
945   EXPECT_TRUE(transport_.last_sent_packet().GetExtension<VideoTimingExtension>(
946       &video_timing));
947   EXPECT_EQ(kStoredTimeInMs, video_timing.network2_timestamp_delta_ms);
948   EXPECT_EQ(kPacerExitMs, video_timing.pacer_exit_delta_ms);
949 }
950 
TEST_P(RtpSenderTest,WritesNetwork2ToTimingExtensionWithoutPacer)951 TEST_P(RtpSenderTest, WritesNetwork2ToTimingExtensionWithoutPacer) {
952   SetUpRtpSender(/*pacer=*/false, /*populate_network2=*/true, false);
953   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
954                    kRtpExtensionVideoTiming, kVideoTimingExtensionId));
955   auto packet = rtp_sender()->AllocatePacket();
956   packet->SetMarker(true);
957   packet->set_capture_time_ms(clock_->TimeInMilliseconds());
958   const VideoSendTiming kVideoTiming = {0u, 0u, 0u, 0u, 0u, 0u, true};
959   packet->SetExtension<VideoTimingExtension>(kVideoTiming);
960   packet->set_allow_retransmission(true);
961   EXPECT_TRUE(rtp_sender()->AssignSequenceNumber(packet.get()));
962   packet->set_packet_type(RtpPacketMediaType::kVideo);
963 
964   const int kPropagateTimeMs = 10;
965   time_controller_.AdvanceTime(TimeDelta::Millis(kPropagateTimeMs));
966 
967   EXPECT_TRUE(rtp_sender()->SendToNetwork(std::move(packet)));
968 
969   EXPECT_EQ(1, transport_.packets_sent());
970   absl::optional<VideoSendTiming> video_timing =
971       transport_.last_sent_packet().GetExtension<VideoTimingExtension>();
972   ASSERT_TRUE(video_timing);
973   EXPECT_EQ(kPropagateTimeMs, video_timing->network2_timestamp_delta_ms);
974 }
975 
TEST_P(RtpSenderTest,TrafficSmoothingWithExtensions)976 TEST_P(RtpSenderTest, TrafficSmoothingWithExtensions) {
977   EXPECT_CALL(mock_rtc_event_log_,
978               LogProxy(SameRtcEventTypeAs(RtcEvent::Type::RtpPacketOutgoing)));
979 
980   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
981       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
982   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
983                    kRtpExtensionTransmissionTimeOffset,
984                    kTransmissionTimeOffsetExtensionId));
985   EXPECT_EQ(0,
986             rtp_sender()->RegisterRtpHeaderExtension(
987                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
988   int64_t capture_time_ms = clock_->TimeInMilliseconds();
989   auto packet =
990       BuildRtpPacket(kPayload, kMarkerBit, kTimestamp, capture_time_ms);
991   size_t packet_size = packet->size();
992 
993   const int kStoredTimeInMs = 100;
994   EXPECT_CALL(
995       mock_paced_sender_,
996       EnqueuePackets(Contains(AllOf(
997           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
998           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
999   packet->set_packet_type(RtpPacketMediaType::kVideo);
1000   packet->set_allow_retransmission(true);
1001   EXPECT_TRUE(
1002       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
1003   EXPECT_EQ(0, transport_.packets_sent());
1004   time_controller_.AdvanceTime(TimeDelta::Millis(kStoredTimeInMs));
1005   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1006 
1007   // Process send bucket. Packet should now be sent.
1008   EXPECT_EQ(1, transport_.packets_sent());
1009   EXPECT_EQ(packet_size, transport_.last_sent_packet().size());
1010 
1011   webrtc::RTPHeader rtp_header;
1012   transport_.last_sent_packet().GetHeader(&rtp_header);
1013 
1014   // Verify transmission time offset.
1015   EXPECT_EQ(kStoredTimeInMs * 90, rtp_header.extension.transmissionTimeOffset);
1016   uint64_t expected_send_time =
1017       ConvertMsToAbsSendTime(clock_->TimeInMilliseconds());
1018   EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
1019 }
1020 
TEST_P(RtpSenderTest,TrafficSmoothingRetransmits)1021 TEST_P(RtpSenderTest, TrafficSmoothingRetransmits) {
1022   EXPECT_CALL(mock_rtc_event_log_,
1023               LogProxy(SameRtcEventTypeAs(RtcEvent::Type::RtpPacketOutgoing)));
1024 
1025   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1026       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
1027   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
1028                    kRtpExtensionTransmissionTimeOffset,
1029                    kTransmissionTimeOffsetExtensionId));
1030   EXPECT_EQ(0,
1031             rtp_sender()->RegisterRtpHeaderExtension(
1032                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
1033   int64_t capture_time_ms = clock_->TimeInMilliseconds();
1034   auto packet =
1035       BuildRtpPacket(kPayload, kMarkerBit, kTimestamp, capture_time_ms);
1036   size_t packet_size = packet->size();
1037 
1038   // Packet should be stored in a send bucket.
1039   EXPECT_CALL(
1040       mock_paced_sender_,
1041       EnqueuePackets(Contains(AllOf(
1042           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1043           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
1044   packet->set_packet_type(RtpPacketMediaType::kVideo);
1045   packet->set_allow_retransmission(true);
1046   EXPECT_TRUE(
1047       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
1048   // Immediately process send bucket and send packet.
1049   rtp_sender_context_->InjectPacket(std::make_unique<RtpPacketToSend>(*packet),
1050                                     PacedPacketInfo());
1051 
1052   EXPECT_EQ(1, transport_.packets_sent());
1053 
1054   // Retransmit packet.
1055   const int kStoredTimeInMs = 100;
1056   time_controller_.AdvanceTime(TimeDelta::Millis(kStoredTimeInMs));
1057 
1058   EXPECT_CALL(mock_rtc_event_log_,
1059               LogProxy(SameRtcEventTypeAs(RtcEvent::Type::RtpPacketOutgoing)));
1060   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
1061   packet->set_retransmitted_sequence_number(kSeqNum);
1062   EXPECT_CALL(
1063       mock_paced_sender_,
1064       EnqueuePackets(Contains(AllOf(
1065           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1066           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
1067   EXPECT_EQ(static_cast<int>(packet_size), rtp_sender()->ReSendPacket(kSeqNum));
1068   EXPECT_EQ(1, transport_.packets_sent());
1069   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1070 
1071   // Process send bucket. Packet should now be sent.
1072   EXPECT_EQ(2, transport_.packets_sent());
1073   EXPECT_EQ(packet_size, transport_.last_sent_packet().size());
1074 
1075   webrtc::RTPHeader rtp_header;
1076   transport_.last_sent_packet().GetHeader(&rtp_header);
1077 
1078   // Verify transmission time offset.
1079   EXPECT_EQ(kStoredTimeInMs * 90, rtp_header.extension.transmissionTimeOffset);
1080   uint64_t expected_send_time =
1081       ConvertMsToAbsSendTime(clock_->TimeInMilliseconds());
1082   EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
1083 }
1084 
1085 // This test sends 1 regular video packet, then 4 padding packets, and then
1086 // 1 more regular packet.
TEST_P(RtpSenderTest,SendPadding)1087 TEST_P(RtpSenderTest, SendPadding) {
1088   // Make all (non-padding) packets go to send queue.
1089   EXPECT_CALL(mock_rtc_event_log_,
1090               LogProxy(SameRtcEventTypeAs(RtcEvent::Type::RtpPacketOutgoing)))
1091       .Times(1 + 4 + 1);
1092 
1093   uint16_t seq_num = kSeqNum;
1094   uint32_t timestamp = kTimestamp;
1095   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1096       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
1097   size_t rtp_header_len = kRtpHeaderSize;
1098   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
1099                    kRtpExtensionTransmissionTimeOffset,
1100                    kTransmissionTimeOffsetExtensionId));
1101   rtp_header_len += 4;  // 4 bytes extension.
1102   EXPECT_EQ(0,
1103             rtp_sender()->RegisterRtpHeaderExtension(
1104                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
1105   rtp_header_len += 4;  // 4 bytes extension.
1106   rtp_header_len += 4;  // 4 extra bytes common to all extension headers.
1107 
1108   webrtc::RTPHeader rtp_header;
1109 
1110   int64_t capture_time_ms = clock_->TimeInMilliseconds();
1111   auto packet =
1112       BuildRtpPacket(kPayload, kMarkerBit, timestamp, capture_time_ms);
1113   const uint32_t media_packet_timestamp = timestamp;
1114   size_t packet_size = packet->size();
1115   int total_packets_sent = 0;
1116   const int kStoredTimeInMs = 100;
1117 
1118   // Packet should be stored in a send bucket.
1119   EXPECT_CALL(
1120       mock_paced_sender_,
1121       EnqueuePackets(Contains(AllOf(
1122           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1123           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
1124   packet->set_packet_type(RtpPacketMediaType::kVideo);
1125   packet->set_allow_retransmission(true);
1126   EXPECT_TRUE(
1127       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
1128   EXPECT_EQ(total_packets_sent, transport_.packets_sent());
1129   time_controller_.AdvanceTime(TimeDelta::Millis(kStoredTimeInMs));
1130   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1131   ++seq_num;
1132 
1133   // Packet should now be sent. This test doesn't verify the regular video
1134   // packet, since it is tested in another test.
1135   EXPECT_EQ(++total_packets_sent, transport_.packets_sent());
1136   timestamp += 90 * kStoredTimeInMs;
1137 
1138   // Send padding 4 times, waiting 50 ms between each.
1139   for (int i = 0; i < 4; ++i) {
1140     const int kPaddingPeriodMs = 50;
1141     const size_t kPaddingBytes = 100;
1142     const size_t kMaxPaddingLength = 224;  // Value taken from rtp_sender.cc.
1143     // Padding will be forced to full packets.
1144     EXPECT_EQ(kMaxPaddingLength, GenerateAndSendPadding(kPaddingBytes));
1145 
1146     // Process send bucket. Padding should now be sent.
1147     EXPECT_EQ(++total_packets_sent, transport_.packets_sent());
1148     EXPECT_EQ(kMaxPaddingLength + rtp_header_len,
1149               transport_.last_sent_packet().size());
1150 
1151     transport_.last_sent_packet().GetHeader(&rtp_header);
1152     EXPECT_EQ(kMaxPaddingLength, rtp_header.paddingLength);
1153 
1154     // Verify sequence number and timestamp. The timestamp should be the same
1155     // as the last media packet.
1156     EXPECT_EQ(seq_num++, rtp_header.sequenceNumber);
1157     EXPECT_EQ(media_packet_timestamp, rtp_header.timestamp);
1158     // Verify transmission time offset.
1159     int offset = timestamp - media_packet_timestamp;
1160     EXPECT_EQ(offset, rtp_header.extension.transmissionTimeOffset);
1161     uint64_t expected_send_time =
1162         ConvertMsToAbsSendTime(clock_->TimeInMilliseconds());
1163     EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
1164     time_controller_.AdvanceTime(TimeDelta::Millis(kPaddingPeriodMs));
1165     timestamp += 90 * kPaddingPeriodMs;
1166   }
1167 
1168   // Send a regular video packet again.
1169   capture_time_ms = clock_->TimeInMilliseconds();
1170   packet = BuildRtpPacket(kPayload, kMarkerBit, timestamp, capture_time_ms);
1171   packet_size = packet->size();
1172 
1173   packet->set_packet_type(RtpPacketMediaType::kVideo);
1174   packet->set_allow_retransmission(true);
1175   EXPECT_CALL(
1176       mock_paced_sender_,
1177       EnqueuePackets(Contains(AllOf(
1178           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1179           Pointee(Property(&RtpPacketToSend::SequenceNumber, seq_num))))));
1180   EXPECT_TRUE(
1181       rtp_sender()->SendToNetwork(std::make_unique<RtpPacketToSend>(*packet)));
1182   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1183 
1184   // Process send bucket.
1185   EXPECT_EQ(++total_packets_sent, transport_.packets_sent());
1186   EXPECT_EQ(packet_size, transport_.last_sent_packet().size());
1187   transport_.last_sent_packet().GetHeader(&rtp_header);
1188 
1189   // Verify sequence number and timestamp.
1190   EXPECT_EQ(seq_num, rtp_header.sequenceNumber);
1191   EXPECT_EQ(timestamp, rtp_header.timestamp);
1192   // Verify transmission time offset. This packet is sent without delay.
1193   EXPECT_EQ(0, rtp_header.extension.transmissionTimeOffset);
1194   uint64_t expected_send_time =
1195       ConvertMsToAbsSendTime(clock_->TimeInMilliseconds());
1196   EXPECT_EQ(expected_send_time, rtp_header.extension.absoluteSendTime);
1197 }
1198 
TEST_P(RtpSenderTest,OnSendPacketUpdated)1199 TEST_P(RtpSenderTest, OnSendPacketUpdated) {
1200   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
1201                    kRtpExtensionTransportSequenceNumber,
1202                    kTransportSequenceNumberExtensionId));
1203   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1204       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
1205 
1206   EXPECT_CALL(send_packet_observer_,
1207               OnSendPacket(kTransportSequenceNumber, _, _))
1208       .Times(1);
1209 
1210   EXPECT_CALL(
1211       mock_paced_sender_,
1212       EnqueuePackets(Contains(AllOf(
1213           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1214           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
1215   auto packet = SendGenericPacket();
1216   packet->set_packet_type(RtpPacketMediaType::kVideo);
1217   packet->SetExtension<TransportSequenceNumber>(kTransportSequenceNumber);
1218   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1219 
1220   EXPECT_EQ(1, transport_.packets_sent());
1221 }
1222 
TEST_P(RtpSenderTest,OnSendPacketNotUpdatedForRetransmits)1223 TEST_P(RtpSenderTest, OnSendPacketNotUpdatedForRetransmits) {
1224   EXPECT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
1225                    kRtpExtensionTransportSequenceNumber,
1226                    kTransportSequenceNumberExtensionId));
1227   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1228       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
1229 
1230   EXPECT_CALL(send_packet_observer_, OnSendPacket(_, _, _)).Times(0);
1231 
1232   EXPECT_CALL(
1233       mock_paced_sender_,
1234       EnqueuePackets(Contains(AllOf(
1235           Pointee(Property(&RtpPacketToSend::Ssrc, kSsrc)),
1236           Pointee(Property(&RtpPacketToSend::SequenceNumber, kSeqNum))))));
1237   auto packet = SendGenericPacket();
1238   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
1239   packet->SetExtension<TransportSequenceNumber>(kTransportSequenceNumber);
1240   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
1241 
1242   EXPECT_EQ(1, transport_.packets_sent());
1243   EXPECT_TRUE(transport_.last_options_.is_retransmit);
1244 }
1245 
TEST_P(RtpSenderTestWithoutPacer,SendGenericVideo)1246 TEST_P(RtpSenderTestWithoutPacer, SendGenericVideo) {
1247   const uint8_t kPayloadType = 127;
1248   const VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1249   FieldTrialBasedConfig field_trials;
1250   RTPSenderVideo::Config video_config;
1251   video_config.clock = clock_;
1252   video_config.rtp_sender = rtp_sender();
1253   video_config.field_trials = &field_trials;
1254   RTPSenderVideo rtp_sender_video(video_config);
1255   uint8_t payload[] = {47, 11, 32, 93, 89};
1256 
1257   // Send keyframe
1258   RTPVideoHeader video_header;
1259   video_header.frame_type = VideoFrameType::kVideoFrameKey;
1260   ASSERT_TRUE(rtp_sender_video.SendVideo(kPayloadType, kCodecType, 1234, 4321,
1261                                          payload, video_header,
1262                                          kDefaultExpectedRetransmissionTimeMs));
1263 
1264   auto sent_payload = transport_.last_sent_packet().payload();
1265   uint8_t generic_header = sent_payload[0];
1266   EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kKeyFrameBit);
1267   EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kFirstPacketBit);
1268   EXPECT_THAT(sent_payload.subview(1), ElementsAreArray(payload));
1269 
1270   // Send delta frame
1271   payload[0] = 13;
1272   payload[1] = 42;
1273   payload[4] = 13;
1274 
1275   video_header.frame_type = VideoFrameType::kVideoFrameDelta;
1276   ASSERT_TRUE(rtp_sender_video.SendVideo(kPayloadType, kCodecType, 1234, 4321,
1277                                          payload, video_header,
1278                                          kDefaultExpectedRetransmissionTimeMs));
1279 
1280   sent_payload = transport_.last_sent_packet().payload();
1281   generic_header = sent_payload[0];
1282   EXPECT_FALSE(generic_header & RtpFormatVideoGeneric::kKeyFrameBit);
1283   EXPECT_TRUE(generic_header & RtpFormatVideoGeneric::kFirstPacketBit);
1284   EXPECT_THAT(sent_payload.subview(1), ElementsAreArray(payload));
1285 }
1286 
TEST_P(RtpSenderTestWithoutPacer,SendRawVideo)1287 TEST_P(RtpSenderTestWithoutPacer, SendRawVideo) {
1288   const uint8_t kPayloadType = 111;
1289   const uint8_t payload[] = {11, 22, 33, 44, 55};
1290 
1291   FieldTrialBasedConfig field_trials;
1292   RTPSenderVideo::Config video_config;
1293   video_config.clock = clock_;
1294   video_config.rtp_sender = rtp_sender();
1295   video_config.field_trials = &field_trials;
1296   RTPSenderVideo rtp_sender_video(video_config);
1297 
1298   // Send a frame.
1299   RTPVideoHeader video_header;
1300   video_header.frame_type = VideoFrameType::kVideoFrameKey;
1301   ASSERT_TRUE(rtp_sender_video.SendVideo(kPayloadType, absl::nullopt, 1234,
1302                                          4321, payload, video_header,
1303                                          kDefaultExpectedRetransmissionTimeMs));
1304 
1305   auto sent_payload = transport_.last_sent_packet().payload();
1306   EXPECT_THAT(sent_payload, ElementsAreArray(payload));
1307 }
1308 
TEST_P(RtpSenderTest,SendFlexfecPackets)1309 TEST_P(RtpSenderTest, SendFlexfecPackets) {
1310   constexpr uint32_t kTimestamp = 1234;
1311   constexpr int kMediaPayloadType = 127;
1312   constexpr VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1313   constexpr int kFlexfecPayloadType = 118;
1314   const std::vector<RtpExtension> kNoRtpExtensions;
1315   const std::vector<RtpExtensionSize> kNoRtpExtensionSizes;
1316   FlexfecSender flexfec_sender(kFlexfecPayloadType, kFlexFecSsrc, kSsrc, kNoMid,
1317                                kNoRtpExtensions, kNoRtpExtensionSizes,
1318                                nullptr /* rtp_state */, clock_);
1319 
1320   // Reset |rtp_sender_| to use FlexFEC.
1321   RtpRtcpInterface::Configuration config;
1322   config.clock = clock_;
1323   config.outgoing_transport = &transport_;
1324   config.paced_sender = &mock_paced_sender_;
1325   config.local_media_ssrc = kSsrc;
1326   config.fec_generator = &flexfec_sender_;
1327   config.event_log = &mock_rtc_event_log_;
1328   config.send_packet_observer = &send_packet_observer_;
1329   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
1330   config.field_trials = &field_trials_;
1331   rtp_sender_context_ =
1332       std::make_unique<RtpSenderContext>(config, &time_controller_);
1333 
1334   rtp_sender()->SetSequenceNumber(kSeqNum);
1335   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1336       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
1337 
1338   FieldTrialBasedConfig field_trials;
1339   RTPSenderVideo::Config video_config;
1340   video_config.clock = clock_;
1341   video_config.rtp_sender = rtp_sender();
1342   if (!GetParam().deferred_fec) {
1343     video_config.fec_generator = &flexfec_sender;
1344   }
1345   video_config.fec_type = flexfec_sender.GetFecType();
1346   video_config.fec_overhead_bytes = flexfec_sender.MaxPacketOverhead();
1347   video_config.fec_type = flexfec_sender.GetFecType();
1348   video_config.fec_overhead_bytes = flexfec_sender.MaxPacketOverhead();
1349   video_config.field_trials = &field_trials;
1350   RTPSenderVideo rtp_sender_video(video_config);
1351 
1352   // Parameters selected to generate a single FEC packet per media packet.
1353   FecProtectionParams params;
1354   params.fec_rate = 15;
1355   params.max_fec_frames = 1;
1356   params.fec_mask_type = kFecMaskRandom;
1357   flexfec_sender.SetProtectionParameters(params, params);
1358 
1359   uint16_t flexfec_seq_num;
1360   RTPVideoHeader video_header;
1361 
1362   std::unique_ptr<RtpPacketToSend> media_packet;
1363   std::unique_ptr<RtpPacketToSend> fec_packet;
1364 
1365   EXPECT_CALL(mock_paced_sender_, EnqueuePackets)
1366       .WillOnce([&](std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
1367         for (auto& packet : packets) {
1368           if (packet->packet_type() == RtpPacketMediaType::kVideo) {
1369             EXPECT_EQ(packet->Ssrc(), kSsrc);
1370             EXPECT_EQ(packet->SequenceNumber(), kSeqNum);
1371             media_packet = std::move(packet);
1372             if (GetParam().deferred_fec) {
1373               // Simulate RtpSenderEgress adding packet to fec generator.
1374               flexfec_sender.AddPacketAndGenerateFec(*media_packet);
1375               auto fec_packets = flexfec_sender.GetFecPackets();
1376               EXPECT_EQ(fec_packets.size(), 1u);
1377               fec_packet = std::move(fec_packets[0]);
1378               EXPECT_EQ(fec_packet->packet_type(),
1379                         RtpPacketMediaType::kForwardErrorCorrection);
1380               EXPECT_EQ(fec_packet->Ssrc(), kFlexFecSsrc);
1381             }
1382           } else {
1383             EXPECT_EQ(packet->packet_type(),
1384                       RtpPacketMediaType::kForwardErrorCorrection);
1385             fec_packet = std::move(packet);
1386             EXPECT_EQ(fec_packet->Ssrc(), kFlexFecSsrc);
1387           }
1388         }
1389       });
1390 
1391   video_header.frame_type = VideoFrameType::kVideoFrameKey;
1392   EXPECT_TRUE(rtp_sender_video.SendVideo(
1393       kMediaPayloadType, kCodecType, kTimestamp, clock_->TimeInMilliseconds(),
1394       kPayloadData, video_header, kDefaultExpectedRetransmissionTimeMs));
1395   ASSERT_TRUE(media_packet != nullptr);
1396   ASSERT_TRUE(fec_packet != nullptr);
1397 
1398   flexfec_seq_num = fec_packet->SequenceNumber();
1399   rtp_sender_context_->InjectPacket(std::move(media_packet), PacedPacketInfo());
1400   rtp_sender_context_->InjectPacket(std::move(fec_packet), PacedPacketInfo());
1401 
1402   ASSERT_EQ(2, transport_.packets_sent());
1403   const RtpPacketReceived& sent_media_packet = transport_.sent_packets_[0];
1404   EXPECT_EQ(kMediaPayloadType, sent_media_packet.PayloadType());
1405   EXPECT_EQ(kSeqNum, sent_media_packet.SequenceNumber());
1406   EXPECT_EQ(kSsrc, sent_media_packet.Ssrc());
1407   const RtpPacketReceived& sent_flexfec_packet = transport_.sent_packets_[1];
1408   EXPECT_EQ(kFlexfecPayloadType, sent_flexfec_packet.PayloadType());
1409   EXPECT_EQ(flexfec_seq_num, sent_flexfec_packet.SequenceNumber());
1410   EXPECT_EQ(kFlexFecSsrc, sent_flexfec_packet.Ssrc());
1411 }
1412 
TEST_P(RtpSenderTestWithoutPacer,SendFlexfecPackets)1413 TEST_P(RtpSenderTestWithoutPacer, SendFlexfecPackets) {
1414   constexpr uint32_t kTimestamp = 1234;
1415   constexpr int kMediaPayloadType = 127;
1416   constexpr VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1417   constexpr int kFlexfecPayloadType = 118;
1418   const std::vector<RtpExtension> kNoRtpExtensions;
1419   const std::vector<RtpExtensionSize> kNoRtpExtensionSizes;
1420   FlexfecSender flexfec_sender(kFlexfecPayloadType, kFlexFecSsrc, kSsrc, kNoMid,
1421                                kNoRtpExtensions, kNoRtpExtensionSizes,
1422                                nullptr /* rtp_state */, clock_);
1423 
1424   // Reset |rtp_sender_| to use FlexFEC.
1425   RtpRtcpInterface::Configuration config;
1426   config.clock = clock_;
1427   config.outgoing_transport = &transport_;
1428   config.local_media_ssrc = kSsrc;
1429   config.fec_generator = &flexfec_sender;
1430   config.event_log = &mock_rtc_event_log_;
1431   config.send_packet_observer = &send_packet_observer_;
1432   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
1433   config.field_trials = &field_trials_;
1434   rtp_sender_context_ =
1435       std::make_unique<RtpSenderContext>(config, &time_controller_);
1436 
1437   rtp_sender()->SetSequenceNumber(kSeqNum);
1438 
1439   FieldTrialBasedConfig field_trials;
1440   RTPSenderVideo::Config video_config;
1441   video_config.clock = clock_;
1442   video_config.rtp_sender = rtp_sender();
1443   if (!GetParam().deferred_fec) {
1444     video_config.fec_generator = &flexfec_sender;
1445   }
1446   video_config.fec_type = flexfec_sender.GetFecType();
1447   video_config.fec_overhead_bytes = flexfec_sender_.MaxPacketOverhead();
1448   video_config.field_trials = &field_trials;
1449   RTPSenderVideo rtp_sender_video(video_config);
1450 
1451   // Parameters selected to generate a single FEC packet per media packet.
1452   FecProtectionParams params;
1453   params.fec_rate = 15;
1454   params.max_fec_frames = 1;
1455   params.fec_mask_type = kFecMaskRandom;
1456   if (GetParam().deferred_fec) {
1457     rtp_egress()->SetFecProtectionParameters(params, params);
1458   } else {
1459     flexfec_sender.SetProtectionParameters(params, params);
1460   }
1461 
1462   EXPECT_CALL(mock_rtc_event_log_,
1463               LogProxy(SameRtcEventTypeAs(RtcEvent::Type::RtpPacketOutgoing)))
1464       .Times(2);
1465   RTPVideoHeader video_header;
1466   video_header.frame_type = VideoFrameType::kVideoFrameKey;
1467   EXPECT_TRUE(rtp_sender_video.SendVideo(
1468       kMediaPayloadType, kCodecType, kTimestamp, clock_->TimeInMilliseconds(),
1469       kPayloadData, video_header, kDefaultExpectedRetransmissionTimeMs));
1470 
1471   ASSERT_EQ(2, transport_.packets_sent());
1472   const RtpPacketReceived& media_packet = transport_.sent_packets_[0];
1473   EXPECT_EQ(kMediaPayloadType, media_packet.PayloadType());
1474   EXPECT_EQ(kSsrc, media_packet.Ssrc());
1475   const RtpPacketReceived& flexfec_packet = transport_.sent_packets_[1];
1476   EXPECT_EQ(kFlexfecPayloadType, flexfec_packet.PayloadType());
1477   EXPECT_EQ(kFlexFecSsrc, flexfec_packet.Ssrc());
1478 }
1479 
1480 // Test that the MID header extension is included on sent packets when
1481 // configured.
TEST_P(RtpSenderTestWithoutPacer,MidIncludedOnSentPackets)1482 TEST_P(RtpSenderTestWithoutPacer, MidIncludedOnSentPackets) {
1483   const char kMid[] = "mid";
1484 
1485   EnableMidSending(kMid);
1486 
1487   // Send a couple packets.
1488   SendGenericPacket();
1489   SendGenericPacket();
1490 
1491   // Expect both packets to have the MID set.
1492   ASSERT_EQ(2u, transport_.sent_packets_.size());
1493   for (const RtpPacketReceived& packet : transport_.sent_packets_) {
1494     std::string mid;
1495     ASSERT_TRUE(packet.GetExtension<RtpMid>(&mid));
1496     EXPECT_EQ(kMid, mid);
1497   }
1498 }
1499 
TEST_P(RtpSenderTestWithoutPacer,RidIncludedOnSentPackets)1500 TEST_P(RtpSenderTestWithoutPacer, RidIncludedOnSentPackets) {
1501   const char kRid[] = "f";
1502 
1503   EnableRidSending(kRid);
1504 
1505   SendGenericPacket();
1506 
1507   ASSERT_EQ(1u, transport_.sent_packets_.size());
1508   const RtpPacketReceived& packet = transport_.sent_packets_[0];
1509   std::string rid;
1510   ASSERT_TRUE(packet.GetExtension<RtpStreamId>(&rid));
1511   EXPECT_EQ(kRid, rid);
1512 }
1513 
TEST_P(RtpSenderTestWithoutPacer,RidIncludedOnRtxSentPackets)1514 TEST_P(RtpSenderTestWithoutPacer, RidIncludedOnRtxSentPackets) {
1515   const char kRid[] = "f";
1516 
1517   EnableRtx();
1518   EnableRidSending(kRid);
1519 
1520   SendGenericPacket();
1521   ASSERT_EQ(1u, transport_.sent_packets_.size());
1522   const RtpPacketReceived& packet = transport_.sent_packets_[0];
1523   std::string rid;
1524   ASSERT_TRUE(packet.GetExtension<RtpStreamId>(&rid));
1525   EXPECT_EQ(kRid, rid);
1526   rid = kNoRid;
1527   EXPECT_FALSE(packet.HasExtension<RepairedRtpStreamId>());
1528 
1529   uint16_t packet_id = packet.SequenceNumber();
1530   rtp_sender()->ReSendPacket(packet_id);
1531   ASSERT_EQ(2u, transport_.sent_packets_.size());
1532   const RtpPacketReceived& rtx_packet = transport_.sent_packets_[1];
1533   ASSERT_TRUE(rtx_packet.GetExtension<RepairedRtpStreamId>(&rid));
1534   EXPECT_EQ(kRid, rid);
1535   EXPECT_FALSE(rtx_packet.HasExtension<RtpStreamId>());
1536 }
1537 
TEST_P(RtpSenderTestWithoutPacer,MidAndRidNotIncludedOnSentPacketsAfterAck)1538 TEST_P(RtpSenderTestWithoutPacer, MidAndRidNotIncludedOnSentPacketsAfterAck) {
1539   const char kMid[] = "mid";
1540   const char kRid[] = "f";
1541 
1542   EnableMidSending(kMid);
1543   EnableRidSending(kRid);
1544 
1545   // This first packet should include both MID and RID.
1546   auto first_built_packet = SendGenericPacket();
1547 
1548   rtp_sender()->OnReceivedAckOnSsrc(first_built_packet->SequenceNumber());
1549 
1550   // The second packet should include neither since an ack was received.
1551   SendGenericPacket();
1552 
1553   ASSERT_EQ(2u, transport_.sent_packets_.size());
1554 
1555   const RtpPacketReceived& first_packet = transport_.sent_packets_[0];
1556   std::string mid, rid;
1557   ASSERT_TRUE(first_packet.GetExtension<RtpMid>(&mid));
1558   EXPECT_EQ(kMid, mid);
1559   ASSERT_TRUE(first_packet.GetExtension<RtpStreamId>(&rid));
1560   EXPECT_EQ(kRid, rid);
1561 
1562   const RtpPacketReceived& second_packet = transport_.sent_packets_[1];
1563   EXPECT_FALSE(second_packet.HasExtension<RtpMid>());
1564   EXPECT_FALSE(second_packet.HasExtension<RtpStreamId>());
1565 }
1566 
TEST_P(RtpSenderTestWithoutPacer,MidAndRidAlwaysIncludedOnSentPacketsWhenConfigured)1567 TEST_P(RtpSenderTestWithoutPacer,
1568        MidAndRidAlwaysIncludedOnSentPacketsWhenConfigured) {
1569   SetUpRtpSender(false, false, /*always_send_mid_and_rid=*/true);
1570   const char kMid[] = "mid";
1571   const char kRid[] = "f";
1572   EnableMidSending(kMid);
1573   EnableRidSending(kRid);
1574 
1575   // Send two media packets: one before and one after the ack.
1576   auto first_packet = SendGenericPacket();
1577   rtp_sender()->OnReceivedAckOnSsrc(first_packet->SequenceNumber());
1578   SendGenericPacket();
1579 
1580   // Due to the configuration, both sent packets should contain MID and RID.
1581   ASSERT_EQ(2u, transport_.sent_packets_.size());
1582   for (const RtpPacketReceived& packet : transport_.sent_packets_) {
1583     EXPECT_EQ(packet.GetExtension<RtpMid>(), kMid);
1584     EXPECT_EQ(packet.GetExtension<RtpStreamId>(), kRid);
1585   }
1586 }
1587 
1588 // Test that the first RTX packet includes both MID and RRID even if the packet
1589 // being retransmitted did not have MID or RID. The MID and RID are needed on
1590 // the first packets for a given SSRC, and RTX packets are sent on a separate
1591 // SSRC.
TEST_P(RtpSenderTestWithoutPacer,MidAndRidIncludedOnFirstRtxPacket)1592 TEST_P(RtpSenderTestWithoutPacer, MidAndRidIncludedOnFirstRtxPacket) {
1593   const char kMid[] = "mid";
1594   const char kRid[] = "f";
1595 
1596   EnableRtx();
1597   EnableMidSending(kMid);
1598   EnableRidSending(kRid);
1599 
1600   // This first packet will include both MID and RID.
1601   auto first_built_packet = SendGenericPacket();
1602   rtp_sender()->OnReceivedAckOnSsrc(first_built_packet->SequenceNumber());
1603 
1604   // The second packet will include neither since an ack was received.
1605   auto second_built_packet = SendGenericPacket();
1606 
1607   // The first RTX packet should include MID and RRID.
1608   ASSERT_LT(0,
1609             rtp_sender()->ReSendPacket(second_built_packet->SequenceNumber()));
1610 
1611   ASSERT_EQ(3u, transport_.sent_packets_.size());
1612 
1613   const RtpPacketReceived& rtx_packet = transport_.sent_packets_[2];
1614   std::string mid, rrid;
1615   ASSERT_TRUE(rtx_packet.GetExtension<RtpMid>(&mid));
1616   EXPECT_EQ(kMid, mid);
1617   ASSERT_TRUE(rtx_packet.GetExtension<RepairedRtpStreamId>(&rrid));
1618   EXPECT_EQ(kRid, rrid);
1619 }
1620 
1621 // Test that the RTX packets sent after receving an ACK on the RTX SSRC does
1622 // not include either MID or RRID even if the packet being retransmitted did
1623 // had a MID or RID.
TEST_P(RtpSenderTestWithoutPacer,MidAndRidNotIncludedOnRtxPacketsAfterAck)1624 TEST_P(RtpSenderTestWithoutPacer, MidAndRidNotIncludedOnRtxPacketsAfterAck) {
1625   const char kMid[] = "mid";
1626   const char kRid[] = "f";
1627 
1628   EnableRtx();
1629   EnableMidSending(kMid);
1630   EnableRidSending(kRid);
1631 
1632   // This first packet will include both MID and RID.
1633   auto first_built_packet = SendGenericPacket();
1634   rtp_sender()->OnReceivedAckOnSsrc(first_built_packet->SequenceNumber());
1635 
1636   // The second packet will include neither since an ack was received.
1637   auto second_built_packet = SendGenericPacket();
1638 
1639   // The first RTX packet will include MID and RRID.
1640   ASSERT_LT(0,
1641             rtp_sender()->ReSendPacket(second_built_packet->SequenceNumber()));
1642 
1643   ASSERT_EQ(3u, transport_.sent_packets_.size());
1644   const RtpPacketReceived& first_rtx_packet = transport_.sent_packets_[2];
1645 
1646   rtp_sender()->OnReceivedAckOnRtxSsrc(first_rtx_packet.SequenceNumber());
1647 
1648   // The second and third RTX packets should not include MID nor RRID.
1649   ASSERT_LT(0,
1650             rtp_sender()->ReSendPacket(first_built_packet->SequenceNumber()));
1651   ASSERT_LT(0,
1652             rtp_sender()->ReSendPacket(second_built_packet->SequenceNumber()));
1653 
1654   ASSERT_EQ(5u, transport_.sent_packets_.size());
1655 
1656   const RtpPacketReceived& second_rtx_packet = transport_.sent_packets_[3];
1657   EXPECT_FALSE(second_rtx_packet.HasExtension<RtpMid>());
1658   EXPECT_FALSE(second_rtx_packet.HasExtension<RepairedRtpStreamId>());
1659 
1660   const RtpPacketReceived& third_rtx_packet = transport_.sent_packets_[4];
1661   EXPECT_FALSE(third_rtx_packet.HasExtension<RtpMid>());
1662   EXPECT_FALSE(third_rtx_packet.HasExtension<RepairedRtpStreamId>());
1663 }
1664 
TEST_P(RtpSenderTestWithoutPacer,MidAndRidAlwaysIncludedOnRtxPacketsWhenConfigured)1665 TEST_P(RtpSenderTestWithoutPacer,
1666        MidAndRidAlwaysIncludedOnRtxPacketsWhenConfigured) {
1667   SetUpRtpSender(false, false, /*always_send_mid_and_rid=*/true);
1668   const char kMid[] = "mid";
1669   const char kRid[] = "f";
1670   EnableRtx();
1671   EnableMidSending(kMid);
1672   EnableRidSending(kRid);
1673 
1674   // Send two media packets: one before and one after the ack.
1675   auto media_packet1 = SendGenericPacket();
1676   rtp_sender()->OnReceivedAckOnSsrc(media_packet1->SequenceNumber());
1677   auto media_packet2 = SendGenericPacket();
1678 
1679   // Send three RTX packets with different combinations of orders w.r.t. the
1680   // media and RTX acks.
1681   ASSERT_LT(0, rtp_sender()->ReSendPacket(media_packet2->SequenceNumber()));
1682   ASSERT_EQ(3u, transport_.sent_packets_.size());
1683   rtp_sender()->OnReceivedAckOnRtxSsrc(
1684       transport_.sent_packets_[2].SequenceNumber());
1685   ASSERT_LT(0, rtp_sender()->ReSendPacket(media_packet1->SequenceNumber()));
1686   ASSERT_LT(0, rtp_sender()->ReSendPacket(media_packet2->SequenceNumber()));
1687 
1688   // Due to the configuration, all sent packets should contain MID
1689   // and either RID (media) or RRID (RTX).
1690   ASSERT_EQ(5u, transport_.sent_packets_.size());
1691   for (const auto& packet : transport_.sent_packets_) {
1692     EXPECT_EQ(packet.GetExtension<RtpMid>(), kMid);
1693   }
1694   for (size_t i = 0; i < 2; ++i) {
1695     const RtpPacketReceived& packet = transport_.sent_packets_[i];
1696     EXPECT_EQ(packet.GetExtension<RtpStreamId>(), kRid);
1697   }
1698   for (size_t i = 2; i < transport_.sent_packets_.size(); ++i) {
1699     const RtpPacketReceived& packet = transport_.sent_packets_[i];
1700     EXPECT_EQ(packet.GetExtension<RepairedRtpStreamId>(), kRid);
1701   }
1702 }
1703 
1704 // Test that if the RtpState indicates an ACK has been received on that SSRC
1705 // then neither the MID nor RID header extensions will be sent.
TEST_P(RtpSenderTestWithoutPacer,MidAndRidNotIncludedOnSentPacketsAfterRtpStateRestored)1706 TEST_P(RtpSenderTestWithoutPacer,
1707        MidAndRidNotIncludedOnSentPacketsAfterRtpStateRestored) {
1708   const char kMid[] = "mid";
1709   const char kRid[] = "f";
1710 
1711   EnableMidSending(kMid);
1712   EnableRidSending(kRid);
1713 
1714   RtpState state = rtp_sender()->GetRtpState();
1715   EXPECT_FALSE(state.ssrc_has_acked);
1716   state.ssrc_has_acked = true;
1717   rtp_sender()->SetRtpState(state);
1718 
1719   SendGenericPacket();
1720 
1721   ASSERT_EQ(1u, transport_.sent_packets_.size());
1722   const RtpPacketReceived& packet = transport_.sent_packets_[0];
1723   EXPECT_FALSE(packet.HasExtension<RtpMid>());
1724   EXPECT_FALSE(packet.HasExtension<RtpStreamId>());
1725 }
1726 
1727 // Test that if the RTX RtpState indicates an ACK has been received on that
1728 // RTX SSRC then neither the MID nor RRID header extensions will be sent on
1729 // RTX packets.
TEST_P(RtpSenderTestWithoutPacer,MidAndRridNotIncludedOnRtxPacketsAfterRtpStateRestored)1730 TEST_P(RtpSenderTestWithoutPacer,
1731        MidAndRridNotIncludedOnRtxPacketsAfterRtpStateRestored) {
1732   const char kMid[] = "mid";
1733   const char kRid[] = "f";
1734 
1735   EnableRtx();
1736   EnableMidSending(kMid);
1737   EnableRidSending(kRid);
1738 
1739   RtpState rtx_state = rtp_sender()->GetRtxRtpState();
1740   EXPECT_FALSE(rtx_state.ssrc_has_acked);
1741   rtx_state.ssrc_has_acked = true;
1742   rtp_sender()->SetRtxRtpState(rtx_state);
1743 
1744   auto built_packet = SendGenericPacket();
1745   ASSERT_LT(0, rtp_sender()->ReSendPacket(built_packet->SequenceNumber()));
1746 
1747   ASSERT_EQ(2u, transport_.sent_packets_.size());
1748   const RtpPacketReceived& rtx_packet = transport_.sent_packets_[1];
1749   EXPECT_FALSE(rtx_packet.HasExtension<RtpMid>());
1750   EXPECT_FALSE(rtx_packet.HasExtension<RepairedRtpStreamId>());
1751 }
1752 
TEST_P(RtpSenderTest,FecOverheadRate)1753 TEST_P(RtpSenderTest, FecOverheadRate) {
1754   constexpr uint32_t kTimestamp = 1234;
1755   constexpr int kMediaPayloadType = 127;
1756   constexpr VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1757   constexpr int kFlexfecPayloadType = 118;
1758   const std::vector<RtpExtension> kNoRtpExtensions;
1759   const std::vector<RtpExtensionSize> kNoRtpExtensionSizes;
1760   FlexfecSender flexfec_sender(kFlexfecPayloadType, kFlexFecSsrc, kSsrc, kNoMid,
1761                                kNoRtpExtensions, kNoRtpExtensionSizes,
1762                                nullptr /* rtp_state */, clock_);
1763 
1764   // Reset |rtp_sender_| to use this FlexFEC instance.
1765   SetUpRtpSender(false, false, false, &flexfec_sender);
1766 
1767   FieldTrialBasedConfig field_trials;
1768   RTPSenderVideo::Config video_config;
1769   video_config.clock = clock_;
1770   video_config.rtp_sender = rtp_sender();
1771   if (!GetParam().deferred_fec) {
1772     video_config.fec_generator = &flexfec_sender;
1773   }
1774   video_config.fec_type = flexfec_sender.GetFecType();
1775   video_config.fec_overhead_bytes = flexfec_sender.MaxPacketOverhead();
1776   video_config.field_trials = &field_trials;
1777   RTPSenderVideo rtp_sender_video(video_config);
1778   // Parameters selected to generate a single FEC packet per media packet.
1779   FecProtectionParams params;
1780   params.fec_rate = 15;
1781   params.max_fec_frames = 1;
1782   params.fec_mask_type = kFecMaskRandom;
1783   if (GetParam().deferred_fec) {
1784     rtp_egress()->SetFecProtectionParameters(params, params);
1785   } else {
1786     flexfec_sender.SetProtectionParameters(params, params);
1787   }
1788 
1789   constexpr size_t kNumMediaPackets = 10;
1790   constexpr size_t kNumFecPackets = kNumMediaPackets;
1791   constexpr int64_t kTimeBetweenPacketsMs = 10;
1792   for (size_t i = 0; i < kNumMediaPackets; ++i) {
1793     RTPVideoHeader video_header;
1794 
1795     video_header.frame_type = VideoFrameType::kVideoFrameKey;
1796     EXPECT_TRUE(rtp_sender_video.SendVideo(
1797         kMediaPayloadType, kCodecType, kTimestamp, clock_->TimeInMilliseconds(),
1798         kPayloadData, video_header, kDefaultExpectedRetransmissionTimeMs));
1799 
1800     time_controller_.AdvanceTime(TimeDelta::Millis(kTimeBetweenPacketsMs));
1801   }
1802   constexpr size_t kRtpHeaderLength = 12;
1803   constexpr size_t kFlexfecHeaderLength = 20;
1804   constexpr size_t kGenericCodecHeaderLength = 1;
1805   constexpr size_t kPayloadLength = sizeof(kPayloadData);
1806   constexpr size_t kPacketLength = kRtpHeaderLength + kFlexfecHeaderLength +
1807                                    kGenericCodecHeaderLength + kPayloadLength;
1808 
1809   if (GetParam().deferred_fec) {
1810     EXPECT_NEAR(
1811         kNumFecPackets * kPacketLength * 8 /
1812             (kNumFecPackets * kTimeBetweenPacketsMs / 1000.0f),
1813         rtp_egress()
1814             ->GetSendRates()[RtpPacketMediaType::kForwardErrorCorrection]
1815             .bps<double>(),
1816         500);
1817   } else {
1818     EXPECT_NEAR(kNumFecPackets * kPacketLength * 8 /
1819                     (kNumFecPackets * kTimeBetweenPacketsMs / 1000.0f),
1820                 flexfec_sender.CurrentFecRate().bps<double>(), 500);
1821   }
1822 }
1823 
TEST_P(RtpSenderTest,BitrateCallbacks)1824 TEST_P(RtpSenderTest, BitrateCallbacks) {
1825   class TestCallback : public BitrateStatisticsObserver {
1826    public:
1827     TestCallback()
1828         : BitrateStatisticsObserver(),
1829           num_calls_(0),
1830           ssrc_(0),
1831           total_bitrate_(0),
1832           retransmit_bitrate_(0) {}
1833     ~TestCallback() override = default;
1834 
1835     void Notify(uint32_t total_bitrate,
1836                 uint32_t retransmit_bitrate,
1837                 uint32_t ssrc) override {
1838       ++num_calls_;
1839       ssrc_ = ssrc;
1840       total_bitrate_ = total_bitrate;
1841       retransmit_bitrate_ = retransmit_bitrate;
1842     }
1843 
1844     uint32_t num_calls_;
1845     uint32_t ssrc_;
1846     uint32_t total_bitrate_;
1847     uint32_t retransmit_bitrate_;
1848   } callback;
1849 
1850   RtpRtcpInterface::Configuration config;
1851   config.clock = clock_;
1852   config.outgoing_transport = &transport_;
1853   config.local_media_ssrc = kSsrc;
1854   config.send_bitrate_observer = &callback;
1855   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
1856   rtp_sender_context_ =
1857       std::make_unique<RtpSenderContext>(config, &time_controller_);
1858 
1859   FieldTrialBasedConfig field_trials;
1860   RTPSenderVideo::Config video_config;
1861   video_config.clock = clock_;
1862   video_config.rtp_sender = rtp_sender();
1863   video_config.field_trials = &field_trials;
1864   RTPSenderVideo rtp_sender_video(video_config);
1865   const VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1866   const uint8_t kPayloadType = 127;
1867 
1868   // Simulate kNumPackets sent with kPacketInterval ms intervals, with the
1869   // number of packets selected so that we fill (but don't overflow) the one
1870   // second averaging window.
1871   const uint32_t kWindowSizeMs = 1000;
1872   const uint32_t kPacketInterval = 20;
1873   const uint32_t kNumPackets =
1874       (kWindowSizeMs - kPacketInterval) / kPacketInterval;
1875   // Overhead = 12 bytes RTP header + 1 byte generic header.
1876   const uint32_t kPacketOverhead = 13;
1877 
1878   uint8_t payload[] = {47, 11, 32, 93, 89};
1879   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1880       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
1881   uint32_t ssrc = rtp_sender()->SSRC();
1882 
1883   // Send a few frames.
1884   RTPVideoHeader video_header;
1885   for (uint32_t i = 0; i < kNumPackets; ++i) {
1886     video_header.frame_type = VideoFrameType::kVideoFrameKey;
1887     ASSERT_TRUE(rtp_sender_video.SendVideo(
1888         kPayloadType, kCodecType, 1234, 4321, payload, video_header,
1889         kDefaultExpectedRetransmissionTimeMs));
1890     time_controller_.AdvanceTime(TimeDelta::Millis(kPacketInterval));
1891   }
1892 
1893   // We get one call for every stats updated, thus two calls since both the
1894   // stream stats and the retransmit stats are updated once.
1895   EXPECT_EQ(kNumPackets, callback.num_calls_);
1896   EXPECT_EQ(ssrc, callback.ssrc_);
1897   const uint32_t kTotalPacketSize = kPacketOverhead + sizeof(payload);
1898   // Bitrate measured over delta between last and first timestamp, plus one.
1899   const uint32_t kExpectedWindowMs = (kNumPackets - 1) * kPacketInterval + 1;
1900   const uint32_t kExpectedBitsAccumulated = kTotalPacketSize * kNumPackets * 8;
1901   const uint32_t kExpectedRateBps =
1902       (kExpectedBitsAccumulated * 1000 + (kExpectedWindowMs / 2)) /
1903       kExpectedWindowMs;
1904   EXPECT_EQ(kExpectedRateBps, callback.total_bitrate_);
1905 }
1906 
TEST_P(RtpSenderTestWithoutPacer,StreamDataCountersCallbacks)1907 TEST_P(RtpSenderTestWithoutPacer, StreamDataCountersCallbacks) {
1908   const uint8_t kPayloadType = 127;
1909   const VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1910   FieldTrialBasedConfig field_trials;
1911   RTPSenderVideo::Config video_config;
1912   video_config.clock = clock_;
1913   video_config.rtp_sender = rtp_sender();
1914   video_config.field_trials = &field_trials;
1915   RTPSenderVideo rtp_sender_video(video_config);
1916   uint8_t payload[] = {47, 11, 32, 93, 89};
1917   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1918       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
1919   uint32_t ssrc = rtp_sender()->SSRC();
1920 
1921   // Send a frame.
1922   RTPVideoHeader video_header;
1923   video_header.frame_type = VideoFrameType::kVideoFrameKey;
1924   ASSERT_TRUE(rtp_sender_video.SendVideo(kPayloadType, kCodecType, 1234, 4321,
1925                                          payload, video_header,
1926                                          kDefaultExpectedRetransmissionTimeMs));
1927   StreamDataCounters expected;
1928   expected.transmitted.payload_bytes = 6;
1929   expected.transmitted.header_bytes = 12;
1930   expected.transmitted.padding_bytes = 0;
1931   expected.transmitted.packets = 1;
1932   expected.retransmitted.payload_bytes = 0;
1933   expected.retransmitted.header_bytes = 0;
1934   expected.retransmitted.padding_bytes = 0;
1935   expected.retransmitted.packets = 0;
1936   expected.fec.packets = 0;
1937   rtp_stats_callback_.Matches(ssrc, expected);
1938 
1939   // Retransmit a frame.
1940   uint16_t seqno = rtp_sender()->SequenceNumber() - 1;
1941   rtp_sender()->ReSendPacket(seqno);
1942   expected.transmitted.payload_bytes = 12;
1943   expected.transmitted.header_bytes = 24;
1944   expected.transmitted.packets = 2;
1945   expected.retransmitted.payload_bytes = 6;
1946   expected.retransmitted.header_bytes = 12;
1947   expected.retransmitted.padding_bytes = 0;
1948   expected.retransmitted.packets = 1;
1949   rtp_stats_callback_.Matches(ssrc, expected);
1950 
1951   // Send padding.
1952   GenerateAndSendPadding(kMaxPaddingSize);
1953   expected.transmitted.payload_bytes = 12;
1954   expected.transmitted.header_bytes = 36;
1955   expected.transmitted.padding_bytes = kMaxPaddingSize;
1956   expected.transmitted.packets = 3;
1957   rtp_stats_callback_.Matches(ssrc, expected);
1958 }
1959 
TEST_P(RtpSenderTestWithoutPacer,StreamDataCountersCallbacksUlpfec)1960 TEST_P(RtpSenderTestWithoutPacer, StreamDataCountersCallbacksUlpfec) {
1961   const uint8_t kRedPayloadType = 96;
1962   const uint8_t kUlpfecPayloadType = 97;
1963   const uint8_t kPayloadType = 127;
1964   const VideoCodecType kCodecType = VideoCodecType::kVideoCodecGeneric;
1965 
1966   UlpfecGenerator ulpfec_generator(kRedPayloadType, kUlpfecPayloadType, clock_);
1967   SetUpRtpSender(false, false, false, &ulpfec_generator);
1968   RTPSenderVideo::Config video_config;
1969   video_config.clock = clock_;
1970   video_config.rtp_sender = rtp_sender();
1971   video_config.field_trials = &field_trials_;
1972   video_config.red_payload_type = kRedPayloadType;
1973   if (!GetParam().deferred_fec) {
1974     video_config.fec_generator = &ulpfec_generator;
1975   }
1976   video_config.fec_type = ulpfec_generator.GetFecType();
1977   video_config.fec_overhead_bytes = ulpfec_generator.MaxPacketOverhead();
1978   RTPSenderVideo rtp_sender_video(video_config);
1979   uint8_t payload[] = {47, 11, 32, 93, 89};
1980   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
1981       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
1982   uint32_t ssrc = rtp_sender()->SSRC();
1983 
1984   RTPVideoHeader video_header;
1985   StreamDataCounters expected;
1986 
1987   // Send ULPFEC.
1988   FecProtectionParams fec_params;
1989   fec_params.fec_mask_type = kFecMaskRandom;
1990   fec_params.fec_rate = 1;
1991   fec_params.max_fec_frames = 1;
1992   if (GetParam().deferred_fec) {
1993     rtp_egress()->SetFecProtectionParameters(fec_params, fec_params);
1994   } else {
1995     ulpfec_generator.SetProtectionParameters(fec_params, fec_params);
1996   }
1997   video_header.frame_type = VideoFrameType::kVideoFrameDelta;
1998   ASSERT_TRUE(rtp_sender_video.SendVideo(kPayloadType, kCodecType, 1234, 4321,
1999                                          payload, video_header,
2000                                          kDefaultExpectedRetransmissionTimeMs));
2001   expected.transmitted.payload_bytes = 28;
2002   expected.transmitted.header_bytes = 24;
2003   expected.transmitted.packets = 2;
2004   expected.fec.packets = 1;
2005   rtp_stats_callback_.Matches(ssrc, expected);
2006 }
2007 
TEST_P(RtpSenderTestWithoutPacer,BytesReportedCorrectly)2008 TEST_P(RtpSenderTestWithoutPacer, BytesReportedCorrectly) {
2009   // XXX const char* kPayloadName = "GENERIC";
2010   const uint8_t kPayloadType = 127;
2011   rtp_sender()->SetRtxPayloadType(kPayloadType - 1, kPayloadType);
2012   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2013 
2014   SendGenericPacket();
2015   // Will send 2 full-size padding packets.
2016   GenerateAndSendPadding(1);
2017   GenerateAndSendPadding(1);
2018 
2019   StreamDataCounters rtp_stats;
2020   StreamDataCounters rtx_stats;
2021   rtp_egress()->GetDataCounters(&rtp_stats, &rtx_stats);
2022 
2023   // Payload
2024   EXPECT_GT(rtp_stats.first_packet_time_ms, -1);
2025   EXPECT_EQ(rtp_stats.transmitted.payload_bytes, sizeof(kPayloadData));
2026   EXPECT_EQ(rtp_stats.transmitted.header_bytes, 12u);
2027   EXPECT_EQ(rtp_stats.transmitted.padding_bytes, 0u);
2028   EXPECT_EQ(rtx_stats.transmitted.payload_bytes, 0u);
2029   EXPECT_EQ(rtx_stats.transmitted.header_bytes, 24u);
2030   EXPECT_EQ(rtx_stats.transmitted.padding_bytes, 2 * kMaxPaddingSize);
2031 
2032   EXPECT_EQ(rtp_stats.transmitted.TotalBytes(),
2033             rtp_stats.transmitted.payload_bytes +
2034                 rtp_stats.transmitted.header_bytes +
2035                 rtp_stats.transmitted.padding_bytes);
2036   EXPECT_EQ(rtx_stats.transmitted.TotalBytes(),
2037             rtx_stats.transmitted.payload_bytes +
2038                 rtx_stats.transmitted.header_bytes +
2039                 rtx_stats.transmitted.padding_bytes);
2040 
2041   EXPECT_EQ(
2042       transport_.total_bytes_sent_,
2043       rtp_stats.transmitted.TotalBytes() + rtx_stats.transmitted.TotalBytes());
2044 }
2045 
TEST_P(RtpSenderTestWithoutPacer,RespectsNackBitrateLimit)2046 TEST_P(RtpSenderTestWithoutPacer, RespectsNackBitrateLimit) {
2047   const int32_t kPacketSize = 1400;
2048   const int32_t kNumPackets = 30;
2049 
2050   retransmission_rate_limiter_.SetMaxRate(kPacketSize * kNumPackets * 8);
2051 
2052   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2053       RtpPacketHistory::StorageMode::kStoreAndCull, kNumPackets);
2054   const uint16_t kStartSequenceNumber = rtp_sender()->SequenceNumber();
2055   std::vector<uint16_t> sequence_numbers;
2056   for (int32_t i = 0; i < kNumPackets; ++i) {
2057     sequence_numbers.push_back(kStartSequenceNumber + i);
2058     time_controller_.AdvanceTime(TimeDelta::Millis(1));
2059     SendPacket(clock_->TimeInMilliseconds(), kPacketSize);
2060   }
2061   EXPECT_EQ(kNumPackets, transport_.packets_sent());
2062 
2063   time_controller_.AdvanceTime(TimeDelta::Millis(1000 - kNumPackets));
2064 
2065   // Resending should work - brings the bandwidth up to the limit.
2066   // NACK bitrate is capped to the same bitrate as the encoder, since the max
2067   // protection overhead is 50% (see MediaOptimization::SetTargetRates).
2068   rtp_sender()->OnReceivedNack(sequence_numbers, 0);
2069   EXPECT_EQ(kNumPackets * 2, transport_.packets_sent());
2070 
2071   // Must be at least 5ms in between retransmission attempts.
2072   time_controller_.AdvanceTime(TimeDelta::Millis(5));
2073 
2074   // Resending should not work, bandwidth exceeded.
2075   rtp_sender()->OnReceivedNack(sequence_numbers, 0);
2076   EXPECT_EQ(kNumPackets * 2, transport_.packets_sent());
2077 }
2078 
TEST_P(RtpSenderTest,UpdatingCsrcsUpdatedOverhead)2079 TEST_P(RtpSenderTest, UpdatingCsrcsUpdatedOverhead) {
2080   RtpRtcpInterface::Configuration config;
2081   config.clock = clock_;
2082   config.outgoing_transport = &transport_;
2083   config.local_media_ssrc = kSsrc;
2084   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
2085   rtp_sender_context_ =
2086       std::make_unique<RtpSenderContext>(config, &time_controller_);
2087 
2088   // Base RTP overhead is 12B.
2089   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2090 
2091   // Adding two csrcs adds 2*4 bytes to the header.
2092   rtp_sender()->SetCsrcs({1, 2});
2093   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 20u);
2094 }
2095 
TEST_P(RtpSenderTest,OnOverheadChanged)2096 TEST_P(RtpSenderTest, OnOverheadChanged) {
2097   RtpRtcpInterface::Configuration config;
2098   config.clock = clock_;
2099   config.outgoing_transport = &transport_;
2100   config.local_media_ssrc = kSsrc;
2101   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
2102   rtp_sender_context_ =
2103       std::make_unique<RtpSenderContext>(config, &time_controller_);
2104 
2105   // Base RTP overhead is 12B.
2106   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2107 
2108   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset,
2109                                            kTransmissionTimeOffsetExtensionId);
2110 
2111   // TransmissionTimeOffset extension has a size of 3B, but with the addition
2112   // of header index and rounding to 4 byte boundary we end up with 20B total.
2113   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 20u);
2114 }
2115 
TEST_P(RtpSenderTest,CountMidOnlyUntilAcked)2116 TEST_P(RtpSenderTest, CountMidOnlyUntilAcked) {
2117   RtpRtcpInterface::Configuration config;
2118   config.clock = clock_;
2119   config.outgoing_transport = &transport_;
2120   config.local_media_ssrc = kSsrc;
2121   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
2122   rtp_sender_context_ =
2123       std::make_unique<RtpSenderContext>(config, &time_controller_);
2124 
2125   // Base RTP overhead is 12B.
2126   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2127 
2128   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionMid, kMidExtensionId);
2129   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionRtpStreamId,
2130                                            kRidExtensionId);
2131 
2132   // Counted only if set.
2133   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2134   rtp_sender()->SetMid("foo");
2135   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 36u);
2136   rtp_sender()->SetRid("bar");
2137   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 52u);
2138 
2139   // Ack received, mid/rid no longer sent.
2140   rtp_sender()->OnReceivedAckOnSsrc(0);
2141   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2142 }
2143 
TEST_P(RtpSenderTest,DontCountVolatileExtensionsIntoOverhead)2144 TEST_P(RtpSenderTest, DontCountVolatileExtensionsIntoOverhead) {
2145   RtpRtcpInterface::Configuration config;
2146   config.clock = clock_;
2147   config.outgoing_transport = &transport_;
2148   config.local_media_ssrc = kSsrc;
2149   config.retransmission_rate_limiter = &retransmission_rate_limiter_;
2150   rtp_sender_context_ =
2151       std::make_unique<RtpSenderContext>(config, &time_controller_);
2152 
2153   // Base RTP overhead is 12B.
2154   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2155 
2156   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionInbandComfortNoise, 1);
2157   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionAbsoluteCaptureTime, 2);
2158   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionVideoRotation, 3);
2159   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionPlayoutDelay, 4);
2160   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionVideoContentType, 5);
2161   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionVideoTiming, 6);
2162   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionRepairedRtpStreamId, 7);
2163   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionColorSpace, 8);
2164 
2165   // Still only 12B counted since can't count on above being sent.
2166   EXPECT_EQ(rtp_sender()->ExpectedPerPacketOverhead(), 12u);
2167 }
2168 
TEST_P(RtpSenderTest,SendPacketMatchesVideo)2169 TEST_P(RtpSenderTest, SendPacketMatchesVideo) {
2170   std::unique_ptr<RtpPacketToSend> packet =
2171       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2172   packet->set_packet_type(RtpPacketMediaType::kVideo);
2173 
2174   // Verify sent with correct SSRC.
2175   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2176   packet->SetSsrc(kSsrc);
2177   packet->set_packet_type(RtpPacketMediaType::kVideo);
2178   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2179   EXPECT_EQ(transport_.packets_sent(), 1);
2180 }
2181 
TEST_P(RtpSenderTest,SendPacketMatchesAudio)2182 TEST_P(RtpSenderTest, SendPacketMatchesAudio) {
2183   std::unique_ptr<RtpPacketToSend> packet =
2184       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2185   packet->set_packet_type(RtpPacketMediaType::kAudio);
2186 
2187   // Verify sent with correct SSRC.
2188   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2189   packet->SetSsrc(kSsrc);
2190   packet->set_packet_type(RtpPacketMediaType::kAudio);
2191   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2192   EXPECT_EQ(transport_.packets_sent(), 1);
2193 }
2194 
TEST_P(RtpSenderTest,SendPacketMatchesRetransmissions)2195 TEST_P(RtpSenderTest, SendPacketMatchesRetransmissions) {
2196   std::unique_ptr<RtpPacketToSend> packet =
2197       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2198   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2199 
2200   // Verify sent with correct SSRC (non-RTX).
2201   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2202   packet->SetSsrc(kSsrc);
2203   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2204   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2205   EXPECT_EQ(transport_.packets_sent(), 1);
2206 
2207   // RTX retransmission.
2208   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2209   packet->SetSsrc(kRtxSsrc);
2210   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2211   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2212   EXPECT_EQ(transport_.packets_sent(), 2);
2213 }
2214 
TEST_P(RtpSenderTest,SendPacketMatchesPadding)2215 TEST_P(RtpSenderTest, SendPacketMatchesPadding) {
2216   std::unique_ptr<RtpPacketToSend> packet =
2217       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2218   packet->set_packet_type(RtpPacketMediaType::kPadding);
2219 
2220   // Verify sent with correct SSRC (non-RTX).
2221   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2222   packet->SetSsrc(kSsrc);
2223   packet->set_packet_type(RtpPacketMediaType::kPadding);
2224   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2225   EXPECT_EQ(transport_.packets_sent(), 1);
2226 
2227   // RTX padding.
2228   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2229   packet->SetSsrc(kRtxSsrc);
2230   packet->set_packet_type(RtpPacketMediaType::kPadding);
2231   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2232   EXPECT_EQ(transport_.packets_sent(), 2);
2233 }
2234 
TEST_P(RtpSenderTest,SendPacketMatchesFlexfec)2235 TEST_P(RtpSenderTest, SendPacketMatchesFlexfec) {
2236   std::unique_ptr<RtpPacketToSend> packet =
2237       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2238   packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
2239 
2240   // Verify sent with correct SSRC.
2241   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2242   packet->SetSsrc(kFlexFecSsrc);
2243   packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
2244   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2245   EXPECT_EQ(transport_.packets_sent(), 1);
2246 }
2247 
TEST_P(RtpSenderTest,SendPacketMatchesUlpfec)2248 TEST_P(RtpSenderTest, SendPacketMatchesUlpfec) {
2249   std::unique_ptr<RtpPacketToSend> packet =
2250       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2251   packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
2252 
2253   // Verify sent with correct SSRC.
2254   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2255   packet->SetSsrc(kSsrc);
2256   packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
2257   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2258   EXPECT_EQ(transport_.packets_sent(), 1);
2259 }
2260 
TEST_P(RtpSenderTest,SendPacketHandlesRetransmissionHistory)2261 TEST_P(RtpSenderTest, SendPacketHandlesRetransmissionHistory) {
2262   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2263       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
2264 
2265   // Ignore calls to EnqueuePackets() for this test.
2266   EXPECT_CALL(mock_paced_sender_, EnqueuePackets).WillRepeatedly(Return());
2267 
2268   // Build a media packet and send it.
2269   std::unique_ptr<RtpPacketToSend> packet =
2270       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2271   const uint16_t media_sequence_number = packet->SequenceNumber();
2272   packet->set_packet_type(RtpPacketMediaType::kVideo);
2273   packet->set_allow_retransmission(true);
2274   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2275 
2276   // Simulate retransmission request.
2277   time_controller_.AdvanceTime(TimeDelta::Millis(30));
2278   EXPECT_GT(rtp_sender()->ReSendPacket(media_sequence_number), 0);
2279 
2280   // Packet already pending, retransmission not allowed.
2281   time_controller_.AdvanceTime(TimeDelta::Millis(30));
2282   EXPECT_EQ(rtp_sender()->ReSendPacket(media_sequence_number), 0);
2283 
2284   // Packet exiting pacer, mark as not longer pending.
2285   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2286   EXPECT_NE(packet->SequenceNumber(), media_sequence_number);
2287   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2288   packet->SetSsrc(kRtxSsrc);
2289   packet->set_retransmitted_sequence_number(media_sequence_number);
2290   packet->set_allow_retransmission(false);
2291   uint16_t seq_no = packet->SequenceNumber();
2292   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2293 
2294   // Retransmissions allowed again.
2295   time_controller_.AdvanceTime(TimeDelta::Millis(30));
2296   EXPECT_GT(rtp_sender()->ReSendPacket(media_sequence_number), 0);
2297 
2298   // Retransmission of RTX packet should not be allowed.
2299   EXPECT_EQ(rtp_sender()->ReSendPacket(seq_no), 0);
2300 }
2301 
TEST_P(RtpSenderTest,SendPacketUpdatesExtensions)2302 TEST_P(RtpSenderTest, SendPacketUpdatesExtensions) {
2303   ASSERT_EQ(rtp_sender()->RegisterRtpHeaderExtension(
2304                 kRtpExtensionTransmissionTimeOffset,
2305                 kTransmissionTimeOffsetExtensionId),
2306             0);
2307   ASSERT_EQ(rtp_sender()->RegisterRtpHeaderExtension(
2308                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId),
2309             0);
2310   ASSERT_EQ(rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionVideoTiming,
2311                                                      kVideoTimingExtensionId),
2312             0);
2313 
2314   std::unique_ptr<RtpPacketToSend> packet =
2315       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2316   packet->set_packetization_finish_time_ms(clock_->TimeInMilliseconds());
2317 
2318   const int32_t kDiffMs = 10;
2319   time_controller_.AdvanceTime(TimeDelta::Millis(kDiffMs));
2320 
2321   packet->set_packet_type(RtpPacketMediaType::kVideo);
2322   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2323 
2324   const RtpPacketReceived& received_packet = transport_.last_sent_packet();
2325 
2326   EXPECT_EQ(received_packet.GetExtension<TransmissionOffset>(), kDiffMs * 90);
2327 
2328   EXPECT_EQ(received_packet.GetExtension<AbsoluteSendTime>(),
2329             AbsoluteSendTime::MsTo24Bits(clock_->TimeInMilliseconds()));
2330 
2331   VideoSendTiming timing;
2332   EXPECT_TRUE(received_packet.GetExtension<VideoTimingExtension>(&timing));
2333   EXPECT_EQ(timing.pacer_exit_delta_ms, kDiffMs);
2334 }
2335 
TEST_P(RtpSenderTest,SendPacketSetsPacketOptions)2336 TEST_P(RtpSenderTest, SendPacketSetsPacketOptions) {
2337   const uint16_t kPacketId = 42;
2338   ASSERT_EQ(rtp_sender()->RegisterRtpHeaderExtension(
2339                 kRtpExtensionTransportSequenceNumber,
2340                 kTransportSequenceNumberExtensionId),
2341             0);
2342   std::unique_ptr<RtpPacketToSend> packet =
2343       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2344   packet->SetExtension<TransportSequenceNumber>(kPacketId);
2345 
2346   packet->set_packet_type(RtpPacketMediaType::kVideo);
2347   EXPECT_CALL(send_packet_observer_, OnSendPacket);
2348   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2349 
2350   EXPECT_EQ(transport_.last_options_.packet_id, kPacketId);
2351   EXPECT_TRUE(transport_.last_options_.included_in_allocation);
2352   EXPECT_TRUE(transport_.last_options_.included_in_feedback);
2353   EXPECT_FALSE(transport_.last_options_.is_retransmit);
2354 
2355   // Send another packet as retransmission, verify options are populated.
2356   packet = BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2357   packet->SetExtension<TransportSequenceNumber>(kPacketId + 1);
2358   packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2359   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2360   EXPECT_TRUE(transport_.last_options_.is_retransmit);
2361 }
2362 
TEST_P(RtpSenderTest,SendPacketUpdatesStats)2363 TEST_P(RtpSenderTest, SendPacketUpdatesStats) {
2364   const size_t kPayloadSize = 1000;
2365 
2366   StrictMock<MockSendSideDelayObserver> send_side_delay_observer;
2367 
2368   RtpRtcpInterface::Configuration config;
2369   config.clock = clock_;
2370   config.outgoing_transport = &transport_;
2371   config.local_media_ssrc = kSsrc;
2372   config.rtx_send_ssrc = kRtxSsrc;
2373   config.fec_generator = &flexfec_sender_;
2374   config.send_side_delay_observer = &send_side_delay_observer;
2375   config.event_log = &mock_rtc_event_log_;
2376   config.send_packet_observer = &send_packet_observer_;
2377   rtp_sender_context_ =
2378       std::make_unique<RtpSenderContext>(config, &time_controller_);
2379   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2380                    kRtpExtensionTransportSequenceNumber,
2381                    kTransportSequenceNumberExtensionId));
2382 
2383   const int64_t capture_time_ms = clock_->TimeInMilliseconds();
2384 
2385   std::unique_ptr<RtpPacketToSend> video_packet =
2386       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2387   video_packet->set_packet_type(RtpPacketMediaType::kVideo);
2388   video_packet->SetPayloadSize(kPayloadSize);
2389   video_packet->SetExtension<TransportSequenceNumber>(1);
2390 
2391   std::unique_ptr<RtpPacketToSend> rtx_packet =
2392       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2393   rtx_packet->SetSsrc(kRtxSsrc);
2394   rtx_packet->set_packet_type(RtpPacketMediaType::kRetransmission);
2395   rtx_packet->SetPayloadSize(kPayloadSize);
2396   rtx_packet->SetExtension<TransportSequenceNumber>(2);
2397 
2398   std::unique_ptr<RtpPacketToSend> fec_packet =
2399       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2400   fec_packet->SetSsrc(kFlexFecSsrc);
2401   fec_packet->set_packet_type(RtpPacketMediaType::kForwardErrorCorrection);
2402   fec_packet->SetPayloadSize(kPayloadSize);
2403   fec_packet->SetExtension<TransportSequenceNumber>(3);
2404 
2405   const int64_t kDiffMs = 25;
2406   time_controller_.AdvanceTime(TimeDelta::Millis(kDiffMs));
2407 
2408   EXPECT_CALL(send_side_delay_observer,
2409               SendSideDelayUpdated(kDiffMs, kDiffMs, kDiffMs, kSsrc));
2410   EXPECT_CALL(
2411       send_side_delay_observer,
2412       SendSideDelayUpdated(kDiffMs, kDiffMs, 2 * kDiffMs, kFlexFecSsrc));
2413 
2414   EXPECT_CALL(send_packet_observer_, OnSendPacket(1, capture_time_ms, kSsrc));
2415 
2416   rtp_sender_context_->InjectPacket(std::move(video_packet), PacedPacketInfo());
2417 
2418   // Send packet observer not called for padding/retransmissions.
2419   EXPECT_CALL(send_packet_observer_, OnSendPacket(2, _, _)).Times(0);
2420   rtp_sender_context_->InjectPacket(std::move(rtx_packet), PacedPacketInfo());
2421 
2422   EXPECT_CALL(send_packet_observer_,
2423               OnSendPacket(3, capture_time_ms, kFlexFecSsrc));
2424   rtp_sender_context_->InjectPacket(std::move(fec_packet), PacedPacketInfo());
2425 
2426   StreamDataCounters rtp_stats;
2427   StreamDataCounters rtx_stats;
2428   rtp_egress()->GetDataCounters(&rtp_stats, &rtx_stats);
2429   EXPECT_EQ(rtp_stats.transmitted.packets, 2u);
2430   EXPECT_EQ(rtp_stats.fec.packets, 1u);
2431   EXPECT_EQ(rtx_stats.retransmitted.packets, 1u);
2432 }
2433 
TEST_P(RtpSenderTest,GeneratedPaddingHasBweExtensions)2434 TEST_P(RtpSenderTest, GeneratedPaddingHasBweExtensions) {
2435   // Min requested size in order to use RTX payload.
2436   const size_t kMinPaddingSize = 50;
2437 
2438   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2439   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2440   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2441       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
2442 
2443   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2444                    kRtpExtensionTransmissionTimeOffset,
2445                    kTransmissionTimeOffsetExtensionId));
2446   ASSERT_EQ(0,
2447             rtp_sender()->RegisterRtpHeaderExtension(
2448                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
2449   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2450                    kRtpExtensionTransportSequenceNumber,
2451                    kTransportSequenceNumberExtensionId));
2452 
2453   // Send a payload packet first, to enable padding and populate the packet
2454   // history.
2455   std::unique_ptr<RtpPacketToSend> packet =
2456       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2457   packet->set_allow_retransmission(true);
2458   packet->SetPayloadSize(kMinPaddingSize);
2459   packet->set_packet_type(RtpPacketMediaType::kVideo);
2460   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
2461   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2462 
2463   // Generate a plain padding packet, check that extensions are registered.
2464   std::vector<std::unique_ptr<RtpPacketToSend>> generated_packets =
2465       rtp_sender()->GeneratePadding(/*target_size_bytes=*/1, true);
2466   ASSERT_THAT(generated_packets, SizeIs(1));
2467   auto& plain_padding = generated_packets.front();
2468   EXPECT_GT(plain_padding->padding_size(), 0u);
2469   EXPECT_TRUE(plain_padding->HasExtension<TransportSequenceNumber>());
2470   EXPECT_TRUE(plain_padding->HasExtension<AbsoluteSendTime>());
2471   EXPECT_TRUE(plain_padding->HasExtension<TransmissionOffset>());
2472 
2473   // Verify all header extensions have been written.
2474   rtp_sender_context_->InjectPacket(std::move(plain_padding),
2475                                     PacedPacketInfo());
2476   const auto& sent_plain_padding = transport_.last_sent_packet();
2477   EXPECT_TRUE(sent_plain_padding.HasExtension<TransportSequenceNumber>());
2478   EXPECT_TRUE(sent_plain_padding.HasExtension<AbsoluteSendTime>());
2479   EXPECT_TRUE(sent_plain_padding.HasExtension<TransmissionOffset>());
2480   webrtc::RTPHeader rtp_header;
2481   sent_plain_padding.GetHeader(&rtp_header);
2482   EXPECT_TRUE(rtp_header.extension.hasAbsoluteSendTime);
2483   EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
2484   EXPECT_TRUE(rtp_header.extension.hasTransportSequenceNumber);
2485 
2486   // Generate a payload padding packets, check that extensions are registered.
2487   generated_packets = rtp_sender()->GeneratePadding(kMinPaddingSize, true);
2488   ASSERT_EQ(generated_packets.size(), 1u);
2489   auto& payload_padding = generated_packets.front();
2490   EXPECT_EQ(payload_padding->padding_size(), 0u);
2491   EXPECT_TRUE(payload_padding->HasExtension<TransportSequenceNumber>());
2492   EXPECT_TRUE(payload_padding->HasExtension<AbsoluteSendTime>());
2493   EXPECT_TRUE(payload_padding->HasExtension<TransmissionOffset>());
2494 
2495   // Verify all header extensions have been written.
2496   rtp_sender_context_->InjectPacket(std::move(payload_padding),
2497                                     PacedPacketInfo());
2498   const auto& sent_payload_padding = transport_.last_sent_packet();
2499   EXPECT_TRUE(sent_payload_padding.HasExtension<TransportSequenceNumber>());
2500   EXPECT_TRUE(sent_payload_padding.HasExtension<AbsoluteSendTime>());
2501   EXPECT_TRUE(sent_payload_padding.HasExtension<TransmissionOffset>());
2502   sent_payload_padding.GetHeader(&rtp_header);
2503   EXPECT_TRUE(rtp_header.extension.hasAbsoluteSendTime);
2504   EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
2505   EXPECT_TRUE(rtp_header.extension.hasTransportSequenceNumber);
2506 }
2507 
TEST_P(RtpSenderTest,GeneratePaddingResendsOldPacketsWithRtx)2508 TEST_P(RtpSenderTest, GeneratePaddingResendsOldPacketsWithRtx) {
2509   // Min requested size in order to use RTX payload.
2510   const size_t kMinPaddingSize = 50;
2511 
2512   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2513   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2514   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2515       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
2516 
2517   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2518                    kRtpExtensionTransportSequenceNumber,
2519                    kTransportSequenceNumberExtensionId));
2520 
2521   const size_t kPayloadPacketSize = kMinPaddingSize;
2522   std::unique_ptr<RtpPacketToSend> packet =
2523       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2524   packet->set_allow_retransmission(true);
2525   packet->SetPayloadSize(kPayloadPacketSize);
2526   packet->set_packet_type(RtpPacketMediaType::kVideo);
2527 
2528   // Send a dummy video packet so it ends up in the packet history.
2529   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
2530   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2531 
2532   // Generated padding has large enough budget that the video packet should be
2533   // retransmitted as padding.
2534   std::vector<std::unique_ptr<RtpPacketToSend>> generated_packets =
2535       rtp_sender()->GeneratePadding(kMinPaddingSize, true);
2536   ASSERT_EQ(generated_packets.size(), 1u);
2537   auto& padding_packet = generated_packets.front();
2538   EXPECT_EQ(padding_packet->packet_type(), RtpPacketMediaType::kPadding);
2539   EXPECT_EQ(padding_packet->Ssrc(), kRtxSsrc);
2540   EXPECT_EQ(padding_packet->payload_size(),
2541             kPayloadPacketSize + kRtxHeaderSize);
2542 
2543   // Not enough budged for payload padding, use plain padding instead.
2544   const size_t kPaddingBytesRequested = kMinPaddingSize - 1;
2545 
2546   size_t padding_bytes_generated = 0;
2547   generated_packets =
2548       rtp_sender()->GeneratePadding(kPaddingBytesRequested, true);
2549   EXPECT_EQ(generated_packets.size(), 1u);
2550   for (auto& packet : generated_packets) {
2551     EXPECT_EQ(packet->packet_type(), RtpPacketMediaType::kPadding);
2552     EXPECT_EQ(packet->Ssrc(), kRtxSsrc);
2553     EXPECT_EQ(packet->payload_size(), 0u);
2554     EXPECT_GT(packet->padding_size(), 0u);
2555     padding_bytes_generated += packet->padding_size();
2556   }
2557 
2558   EXPECT_EQ(padding_bytes_generated, kMaxPaddingSize);
2559 }
2560 
TEST_P(RtpSenderTest,LimitsPayloadPaddingSize)2561 TEST_P(RtpSenderTest, LimitsPayloadPaddingSize) {
2562   // Limit RTX payload padding to 2x target size.
2563   const double kFactor = 2.0;
2564   field_trials_.SetMaxPaddingFactor(kFactor);
2565   SetUpRtpSender(true, false, false);
2566   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2567   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2568   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2569       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
2570 
2571   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2572                    kRtpExtensionTransportSequenceNumber,
2573                    kTransportSequenceNumberExtensionId));
2574 
2575   // Send a dummy video packet so it ends up in the packet history.
2576   const size_t kPayloadPacketSize = 1234u;
2577   std::unique_ptr<RtpPacketToSend> packet =
2578       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2579   packet->set_allow_retransmission(true);
2580   packet->SetPayloadSize(kPayloadPacketSize);
2581   packet->set_packet_type(RtpPacketMediaType::kVideo);
2582   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
2583   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2584 
2585   // Smallest target size that will result in the sent packet being returned as
2586   // padding.
2587   const size_t kMinTargerSizeForPayload =
2588       (kPayloadPacketSize + kRtxHeaderSize) / kFactor;
2589 
2590   // Generated padding has large enough budget that the video packet should be
2591   // retransmitted as padding.
2592   EXPECT_THAT(
2593       rtp_sender()->GeneratePadding(kMinTargerSizeForPayload, true),
2594       AllOf(Not(IsEmpty()),
2595             Each(Pointee(Property(&RtpPacketToSend::padding_size, Eq(0u))))));
2596 
2597   // If payload padding is > 2x requested size, plain padding is returned
2598   // instead.
2599   EXPECT_THAT(
2600       rtp_sender()->GeneratePadding(kMinTargerSizeForPayload - 1, true),
2601       AllOf(Not(IsEmpty()),
2602             Each(Pointee(Property(&RtpPacketToSend::padding_size, Gt(0u))))));
2603 }
2604 
TEST_P(RtpSenderTest,GeneratePaddingCreatesPurePaddingWithoutRtx)2605 TEST_P(RtpSenderTest, GeneratePaddingCreatesPurePaddingWithoutRtx) {
2606   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2607       RtpPacketHistory::StorageMode::kStoreAndCull, 1);
2608   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2609                    kRtpExtensionTransmissionTimeOffset,
2610                    kTransmissionTimeOffsetExtensionId));
2611   ASSERT_EQ(0,
2612             rtp_sender()->RegisterRtpHeaderExtension(
2613                 kRtpExtensionAbsoluteSendTime, kAbsoluteSendTimeExtensionId));
2614   ASSERT_EQ(0, rtp_sender()->RegisterRtpHeaderExtension(
2615                    kRtpExtensionTransportSequenceNumber,
2616                    kTransportSequenceNumberExtensionId));
2617 
2618   const size_t kPayloadPacketSize = 1234;
2619   // Send a dummy video packet so it ends up in the packet history. Since we
2620   // are not using RTX, it should never be used as padding.
2621   std::unique_ptr<RtpPacketToSend> packet =
2622       BuildRtpPacket(kPayload, true, 0, clock_->TimeInMilliseconds());
2623   packet->set_allow_retransmission(true);
2624   packet->SetPayloadSize(kPayloadPacketSize);
2625   packet->set_packet_type(RtpPacketMediaType::kVideo);
2626   EXPECT_CALL(send_packet_observer_, OnSendPacket).Times(1);
2627   rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2628 
2629   // Payload padding not available without RTX, only generate plain padding on
2630   // the media SSRC.
2631   // Number of padding packets is the requested padding size divided by max
2632   // padding packet size, rounded up. Pure padding packets are always of the
2633   // maximum size.
2634   const size_t kPaddingBytesRequested = kPayloadPacketSize + kRtxHeaderSize;
2635   const size_t kExpectedNumPaddingPackets =
2636       (kPaddingBytesRequested + kMaxPaddingSize - 1) / kMaxPaddingSize;
2637   size_t padding_bytes_generated = 0;
2638   std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets =
2639       rtp_sender()->GeneratePadding(kPaddingBytesRequested, true);
2640   EXPECT_EQ(padding_packets.size(), kExpectedNumPaddingPackets);
2641   for (auto& packet : padding_packets) {
2642     EXPECT_EQ(packet->packet_type(), RtpPacketMediaType::kPadding);
2643     EXPECT_EQ(packet->Ssrc(), kSsrc);
2644     EXPECT_EQ(packet->payload_size(), 0u);
2645     EXPECT_GT(packet->padding_size(), 0u);
2646     padding_bytes_generated += packet->padding_size();
2647     EXPECT_TRUE(packet->HasExtension<TransportSequenceNumber>());
2648     EXPECT_TRUE(packet->HasExtension<AbsoluteSendTime>());
2649     EXPECT_TRUE(packet->HasExtension<TransmissionOffset>());
2650 
2651     // Verify all header extensions are received.
2652     rtp_sender_context_->InjectPacket(std::move(packet), PacedPacketInfo());
2653     webrtc::RTPHeader rtp_header;
2654     transport_.last_sent_packet().GetHeader(&rtp_header);
2655     EXPECT_TRUE(rtp_header.extension.hasAbsoluteSendTime);
2656     EXPECT_TRUE(rtp_header.extension.hasTransmissionTimeOffset);
2657     EXPECT_TRUE(rtp_header.extension.hasTransportSequenceNumber);
2658   }
2659 
2660   EXPECT_EQ(padding_bytes_generated,
2661             kExpectedNumPaddingPackets * kMaxPaddingSize);
2662 }
2663 
TEST_P(RtpSenderTest,SupportsPadding)2664 TEST_P(RtpSenderTest, SupportsPadding) {
2665   bool kSendingMediaStats[] = {true, false};
2666   bool kEnableRedundantPayloads[] = {true, false};
2667   RTPExtensionType kBweExtensionTypes[] = {
2668       kRtpExtensionTransportSequenceNumber,
2669       kRtpExtensionTransportSequenceNumber02, kRtpExtensionAbsoluteSendTime,
2670       kRtpExtensionTransmissionTimeOffset};
2671   const int kExtensionsId = 7;
2672 
2673   for (bool sending_media : kSendingMediaStats) {
2674     rtp_sender()->SetSendingMediaStatus(sending_media);
2675     for (bool redundant_payloads : kEnableRedundantPayloads) {
2676       int rtx_mode = kRtxRetransmitted;
2677       if (redundant_payloads) {
2678         rtx_mode |= kRtxRedundantPayloads;
2679       }
2680       rtp_sender()->SetRtxStatus(rtx_mode);
2681 
2682       for (auto extension_type : kBweExtensionTypes) {
2683         EXPECT_FALSE(rtp_sender()->SupportsPadding());
2684         rtp_sender()->RegisterRtpHeaderExtension(extension_type, kExtensionsId);
2685         if (!sending_media) {
2686           EXPECT_FALSE(rtp_sender()->SupportsPadding());
2687         } else {
2688           EXPECT_TRUE(rtp_sender()->SupportsPadding());
2689           if (redundant_payloads) {
2690             EXPECT_TRUE(rtp_sender()->SupportsRtxPayloadPadding());
2691           } else {
2692             EXPECT_FALSE(rtp_sender()->SupportsRtxPayloadPadding());
2693           }
2694         }
2695         rtp_sender()->DeregisterRtpHeaderExtension(extension_type);
2696         EXPECT_FALSE(rtp_sender()->SupportsPadding());
2697       }
2698     }
2699   }
2700 }
2701 
TEST_P(RtpSenderTest,SetsCaptureTimeAndPopulatesTransmissionOffset)2702 TEST_P(RtpSenderTest, SetsCaptureTimeAndPopulatesTransmissionOffset) {
2703   rtp_sender()->RegisterRtpHeaderExtension(kRtpExtensionTransmissionTimeOffset,
2704                                            kTransmissionTimeOffsetExtensionId);
2705 
2706   rtp_sender()->SetSendingMediaStatus(true);
2707   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2708   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2709   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2710       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
2711 
2712   const int64_t kMissingCaptureTimeMs = 0;
2713   const uint32_t kTimestampTicksPerMs = 90;
2714   const int64_t kOffsetMs = 10;
2715 
2716   auto packet =
2717       BuildRtpPacket(kPayload, kMarkerBit, clock_->TimeInMilliseconds(),
2718                      kMissingCaptureTimeMs);
2719   packet->set_packet_type(RtpPacketMediaType::kVideo);
2720   packet->ReserveExtension<TransmissionOffset>();
2721   packet->AllocatePayload(sizeof(kPayloadData));
2722 
2723   std::unique_ptr<RtpPacketToSend> packet_to_pace;
2724   EXPECT_CALL(mock_paced_sender_, EnqueuePackets)
2725       .WillOnce([&](std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
2726         EXPECT_EQ(packets.size(), 1u);
2727         EXPECT_GT(packets[0]->capture_time_ms(), 0);
2728         packet_to_pace = std::move(packets[0]);
2729       });
2730 
2731   packet->set_allow_retransmission(true);
2732   EXPECT_TRUE(rtp_sender()->SendToNetwork(std::move(packet)));
2733 
2734   time_controller_.AdvanceTime(TimeDelta::Millis(kOffsetMs));
2735 
2736   rtp_sender_context_->InjectPacket(std::move(packet_to_pace),
2737                                     PacedPacketInfo());
2738 
2739   EXPECT_EQ(1, transport_.packets_sent());
2740   absl::optional<int32_t> transmission_time_extension =
2741       transport_.sent_packets_.back().GetExtension<TransmissionOffset>();
2742   ASSERT_TRUE(transmission_time_extension.has_value());
2743   EXPECT_EQ(*transmission_time_extension, kOffsetMs * kTimestampTicksPerMs);
2744 
2745   // Retransmit packet. The RTX packet should get the same capture time as the
2746   // original packet, so offset is delta from original packet to now.
2747   time_controller_.AdvanceTime(TimeDelta::Millis(kOffsetMs));
2748 
2749   std::unique_ptr<RtpPacketToSend> rtx_packet_to_pace;
2750   EXPECT_CALL(mock_paced_sender_, EnqueuePackets)
2751       .WillOnce([&](std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
2752         EXPECT_GT(packets[0]->capture_time_ms(), 0);
2753         rtx_packet_to_pace = std::move(packets[0]);
2754       });
2755 
2756   EXPECT_GT(rtp_sender()->ReSendPacket(kSeqNum), 0);
2757   rtp_sender_context_->InjectPacket(std::move(rtx_packet_to_pace),
2758                                     PacedPacketInfo());
2759 
2760   EXPECT_EQ(2, transport_.packets_sent());
2761   transmission_time_extension =
2762       transport_.sent_packets_.back().GetExtension<TransmissionOffset>();
2763   ASSERT_TRUE(transmission_time_extension.has_value());
2764   EXPECT_EQ(*transmission_time_extension, 2 * kOffsetMs * kTimestampTicksPerMs);
2765 }
2766 
TEST_P(RtpSenderTestWithoutPacer,ClearHistoryOnSequenceNumberCange)2767 TEST_P(RtpSenderTestWithoutPacer, ClearHistoryOnSequenceNumberCange) {
2768   const int64_t kRtt = 10;
2769 
2770   rtp_sender()->SetSendingMediaStatus(true);
2771   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2772   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2773   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2774       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
2775   rtp_sender_context_->packet_history_.SetRtt(kRtt);
2776 
2777   // Send a packet and record its sequence numbers.
2778   SendGenericPacket();
2779   ASSERT_EQ(1u, transport_.sent_packets_.size());
2780   const uint16_t packet_seqence_number =
2781       transport_.sent_packets_.back().SequenceNumber();
2782 
2783   // Advance time and make sure it can be retransmitted, even if we try to set
2784   // the ssrc the what it already is.
2785   rtp_sender()->SetSequenceNumber(rtp_sender()->SequenceNumber());
2786   time_controller_.AdvanceTime(TimeDelta::Millis(kRtt));
2787   EXPECT_GT(rtp_sender()->ReSendPacket(packet_seqence_number), 0);
2788 
2789   // Change the sequence number, then move the time and try to retransmit again.
2790   // The old packet should now be gone.
2791   rtp_sender()->SetSequenceNumber(rtp_sender()->SequenceNumber() - 1);
2792   time_controller_.AdvanceTime(TimeDelta::Millis(kRtt));
2793   EXPECT_EQ(rtp_sender()->ReSendPacket(packet_seqence_number), 0);
2794 }
2795 
TEST_P(RtpSenderTest,IgnoresNackAfterDisablingMedia)2796 TEST_P(RtpSenderTest, IgnoresNackAfterDisablingMedia) {
2797   const int64_t kRtt = 10;
2798 
2799   rtp_sender()->SetSendingMediaStatus(true);
2800   rtp_sender()->SetRtxStatus(kRtxRetransmitted | kRtxRedundantPayloads);
2801   rtp_sender()->SetRtxPayloadType(kRtxPayload, kPayload);
2802   rtp_sender_context_->packet_history_.SetStorePacketsStatus(
2803       RtpPacketHistory::StorageMode::kStoreAndCull, 10);
2804   rtp_sender_context_->packet_history_.SetRtt(kRtt);
2805 
2806   // Send a packet so it is in the packet history.
2807     std::unique_ptr<RtpPacketToSend> packet_to_pace;
2808     EXPECT_CALL(mock_paced_sender_, EnqueuePackets)
2809         .WillOnce([&](std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
2810           packet_to_pace = std::move(packets[0]);
2811         });
2812 
2813     SendGenericPacket();
2814     rtp_sender_context_->InjectPacket(std::move(packet_to_pace),
2815                                       PacedPacketInfo());
2816 
2817     ASSERT_EQ(1u, transport_.sent_packets_.size());
2818 
2819     // Disable media sending and try to retransmit the packet, it should fail.
2820     rtp_sender()->SetSendingMediaStatus(false);
2821     time_controller_.AdvanceTime(TimeDelta::Millis(kRtt));
2822     EXPECT_LT(rtp_sender()->ReSendPacket(kSeqNum), 0);
2823 }
2824 
2825 INSTANTIATE_TEST_SUITE_P(WithAndWithoutOverhead,
2826                          RtpSenderTest,
2827                          ::testing::Values(TestConfig{false, false},
2828                                            TestConfig{false, true},
2829                                            TestConfig{true, false},
2830                                            TestConfig{false, false}));
2831 
2832 INSTANTIATE_TEST_SUITE_P(WithAndWithoutOverhead,
2833                          RtpSenderTestWithoutPacer,
2834                          ::testing::Values(TestConfig{false, false},
2835                                            TestConfig{false, true},
2836                                            TestConfig{true, false},
2837                                            TestConfig{false, false}));
2838 
2839 }  // namespace webrtc
2840