• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2013 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_rtcp_impl.h"
12 
13 #include <map>
14 #include <memory>
15 #include <set>
16 
17 #include "api/transport/field_trial_based_config.h"
18 #include "api/video_codecs/video_codec.h"
19 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
20 #include "modules/rtp_rtcp/source/rtcp_packet.h"
21 #include "modules/rtp_rtcp/source/rtcp_packet/nack.h"
22 #include "modules/rtp_rtcp/source/rtp_packet_received.h"
23 #include "modules/rtp_rtcp/source/rtp_sender_video.h"
24 #include "rtc_base/rate_limiter.h"
25 #include "test/gmock.h"
26 #include "test/gtest.h"
27 #include "test/rtcp_packet_parser.h"
28 #include "test/rtp_header_parser.h"
29 
30 using ::testing::ElementsAre;
31 
32 namespace webrtc {
33 namespace {
34 const uint32_t kSenderSsrc = 0x12345;
35 const uint32_t kReceiverSsrc = 0x23456;
36 const int64_t kOneWayNetworkDelayMs = 100;
37 const uint8_t kBaseLayerTid = 0;
38 const uint8_t kHigherLayerTid = 1;
39 const uint16_t kSequenceNumber = 100;
40 
41 class RtcpRttStatsTestImpl : public RtcpRttStats {
42  public:
RtcpRttStatsTestImpl()43   RtcpRttStatsTestImpl() : rtt_ms_(0) {}
44   ~RtcpRttStatsTestImpl() override = default;
45 
OnRttUpdate(int64_t rtt_ms)46   void OnRttUpdate(int64_t rtt_ms) override { rtt_ms_ = rtt_ms; }
LastProcessedRtt() const47   int64_t LastProcessedRtt() const override { return rtt_ms_; }
48   int64_t rtt_ms_;
49 };
50 
51 class SendTransport : public Transport {
52  public:
SendTransport()53   SendTransport()
54       : receiver_(nullptr),
55         clock_(nullptr),
56         delay_ms_(0),
57         rtp_packets_sent_(0),
58         rtcp_packets_sent_(0) {}
59 
SetRtpRtcpModule(ModuleRtpRtcpImpl * receiver)60   void SetRtpRtcpModule(ModuleRtpRtcpImpl* receiver) { receiver_ = receiver; }
SimulateNetworkDelay(int64_t delay_ms,SimulatedClock * clock)61   void SimulateNetworkDelay(int64_t delay_ms, SimulatedClock* clock) {
62     clock_ = clock;
63     delay_ms_ = delay_ms;
64   }
SendRtp(const uint8_t * data,size_t len,const PacketOptions & options)65   bool SendRtp(const uint8_t* data,
66                size_t len,
67                const PacketOptions& options) override {
68     RTPHeader header;
69     std::unique_ptr<RtpHeaderParser> parser(RtpHeaderParser::CreateForTest());
70     EXPECT_TRUE(parser->Parse(static_cast<const uint8_t*>(data), len, &header));
71     ++rtp_packets_sent_;
72     last_rtp_header_ = header;
73     return true;
74   }
SendRtcp(const uint8_t * data,size_t len)75   bool SendRtcp(const uint8_t* data, size_t len) override {
76     test::RtcpPacketParser parser;
77     parser.Parse(data, len);
78     last_nack_list_ = parser.nack()->packet_ids();
79 
80     if (clock_) {
81       clock_->AdvanceTimeMilliseconds(delay_ms_);
82     }
83     EXPECT_TRUE(receiver_);
84     receiver_->IncomingRtcpPacket(data, len);
85     ++rtcp_packets_sent_;
86     return true;
87   }
NumRtcpSent()88   size_t NumRtcpSent() { return rtcp_packets_sent_; }
89   ModuleRtpRtcpImpl* receiver_;
90   SimulatedClock* clock_;
91   int64_t delay_ms_;
92   int rtp_packets_sent_;
93   size_t rtcp_packets_sent_;
94   RTPHeader last_rtp_header_;
95   std::vector<uint16_t> last_nack_list_;
96 };
97 
98 class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
99  public:
RtpRtcpModule(SimulatedClock * clock,bool is_sender)100   RtpRtcpModule(SimulatedClock* clock, bool is_sender)
101       : is_sender_(is_sender),
102         receive_statistics_(ReceiveStatistics::Create(clock)),
103         clock_(clock) {
104     CreateModuleImpl();
105     transport_.SimulateNetworkDelay(kOneWayNetworkDelayMs, clock);
106   }
107 
108   const bool is_sender_;
109   RtcpPacketTypeCounter packets_sent_;
110   RtcpPacketTypeCounter packets_received_;
111   std::unique_ptr<ReceiveStatistics> receive_statistics_;
112   SendTransport transport_;
113   RtcpRttStatsTestImpl rtt_stats_;
114   std::unique_ptr<ModuleRtpRtcpImpl> impl_;
115   int rtcp_report_interval_ms_ = 0;
116 
RtcpPacketTypesCounterUpdated(uint32_t ssrc,const RtcpPacketTypeCounter & packet_counter)117   void RtcpPacketTypesCounterUpdated(
118       uint32_t ssrc,
119       const RtcpPacketTypeCounter& packet_counter) override {
120     counter_map_[ssrc] = packet_counter;
121   }
122 
RtcpSent()123   RtcpPacketTypeCounter RtcpSent() {
124     // RTCP counters for remote SSRC.
125     return counter_map_[is_sender_ ? kReceiverSsrc : kSenderSsrc];
126   }
127 
RtcpReceived()128   RtcpPacketTypeCounter RtcpReceived() {
129     // Received RTCP stats for (own) local SSRC.
130     return counter_map_[impl_->SSRC()];
131   }
RtpSent()132   int RtpSent() { return transport_.rtp_packets_sent_; }
LastRtpSequenceNumber()133   uint16_t LastRtpSequenceNumber() {
134     return transport_.last_rtp_header_.sequenceNumber;
135   }
LastNackListSent()136   std::vector<uint16_t> LastNackListSent() {
137     return transport_.last_nack_list_;
138   }
SetRtcpReportIntervalAndReset(int rtcp_report_interval_ms)139   void SetRtcpReportIntervalAndReset(int rtcp_report_interval_ms) {
140     rtcp_report_interval_ms_ = rtcp_report_interval_ms;
141     CreateModuleImpl();
142   }
143 
144  private:
CreateModuleImpl()145   void CreateModuleImpl() {
146     RtpRtcpInterface::Configuration config;
147     config.audio = false;
148     config.clock = clock_;
149     config.outgoing_transport = &transport_;
150     config.receive_statistics = receive_statistics_.get();
151     config.rtcp_packet_type_counter_observer = this;
152     config.rtt_stats = &rtt_stats_;
153     config.rtcp_report_interval_ms = rtcp_report_interval_ms_;
154     config.local_media_ssrc = is_sender_ ? kSenderSsrc : kReceiverSsrc;
155     config.need_rtp_packet_infos = true;
156 
157     impl_.reset(new ModuleRtpRtcpImpl(config));
158     impl_->SetRemoteSSRC(is_sender_ ? kReceiverSsrc : kSenderSsrc);
159     impl_->SetRTCPStatus(RtcpMode::kCompound);
160   }
161 
162   SimulatedClock* const clock_;
163   std::map<uint32_t, RtcpPacketTypeCounter> counter_map_;
164 };
165 }  // namespace
166 
167 class RtpRtcpImplTest : public ::testing::Test {
168  protected:
RtpRtcpImplTest()169   RtpRtcpImplTest()
170       : clock_(133590000000000),
171         sender_(&clock_, /*is_sender=*/true),
172         receiver_(&clock_, /*is_sender=*/false) {}
173 
SetUp()174   void SetUp() override {
175     // Send module.
176     EXPECT_EQ(0, sender_.impl_->SetSendingStatus(true));
177     sender_.impl_->SetSendingMediaStatus(true);
178     sender_.impl_->SetSequenceNumber(kSequenceNumber);
179     sender_.impl_->SetStorePacketsStatus(true, 100);
180 
181     FieldTrialBasedConfig field_trials;
182     RTPSenderVideo::Config video_config;
183     video_config.clock = &clock_;
184     video_config.rtp_sender = sender_.impl_->RtpSender();
185     video_config.field_trials = &field_trials;
186     sender_video_ = std::make_unique<RTPSenderVideo>(video_config);
187 
188     memset(&codec_, 0, sizeof(VideoCodec));
189     codec_.plType = 100;
190     codec_.width = 320;
191     codec_.height = 180;
192 
193     // Receive module.
194     EXPECT_EQ(0, receiver_.impl_->SetSendingStatus(false));
195     receiver_.impl_->SetSendingMediaStatus(false);
196     // Transport settings.
197     sender_.transport_.SetRtpRtcpModule(receiver_.impl_.get());
198     receiver_.transport_.SetRtpRtcpModule(sender_.impl_.get());
199   }
200 
201   SimulatedClock clock_;
202   RtpRtcpModule sender_;
203   std::unique_ptr<RTPSenderVideo> sender_video_;
204   RtpRtcpModule receiver_;
205   VideoCodec codec_;
206 
SendFrame(const RtpRtcpModule * module,RTPSenderVideo * sender,uint8_t tid)207   void SendFrame(const RtpRtcpModule* module,
208                  RTPSenderVideo* sender,
209                  uint8_t tid) {
210     RTPVideoHeaderVP8 vp8_header = {};
211     vp8_header.temporalIdx = tid;
212     RTPVideoHeader rtp_video_header;
213     rtp_video_header.frame_type = VideoFrameType::kVideoFrameKey;
214     rtp_video_header.width = codec_.width;
215     rtp_video_header.height = codec_.height;
216     rtp_video_header.rotation = kVideoRotation_0;
217     rtp_video_header.content_type = VideoContentType::UNSPECIFIED;
218     rtp_video_header.playout_delay = {-1, -1};
219     rtp_video_header.is_first_packet_in_frame = true;
220     rtp_video_header.simulcastIdx = 0;
221     rtp_video_header.codec = kVideoCodecVP8;
222     rtp_video_header.video_type_header = vp8_header;
223     rtp_video_header.video_timing = {0u, 0u, 0u, 0u, 0u, 0u, false};
224 
225     const uint8_t payload[100] = {0};
226     EXPECT_TRUE(module->impl_->OnSendingRtpFrame(0, 0, codec_.plType, true));
227     EXPECT_TRUE(sender->SendVideo(codec_.plType, VideoCodecType::kVideoCodecVP8,
228                                   0, 0, payload, rtp_video_header, 0));
229   }
230 
IncomingRtcpNack(const RtpRtcpModule * module,uint16_t sequence_number)231   void IncomingRtcpNack(const RtpRtcpModule* module, uint16_t sequence_number) {
232     bool sender = module->impl_->SSRC() == kSenderSsrc;
233     rtcp::Nack nack;
234     uint16_t list[1];
235     list[0] = sequence_number;
236     const uint16_t kListLength = sizeof(list) / sizeof(list[0]);
237     nack.SetSenderSsrc(sender ? kReceiverSsrc : kSenderSsrc);
238     nack.SetMediaSsrc(sender ? kSenderSsrc : kReceiverSsrc);
239     nack.SetPacketIds(list, kListLength);
240     rtc::Buffer packet = nack.Build();
241     module->impl_->IncomingRtcpPacket(packet.data(), packet.size());
242   }
243 };
244 
TEST_F(RtpRtcpImplTest,RetransmitsAllLayers)245 TEST_F(RtpRtcpImplTest, RetransmitsAllLayers) {
246   // Send frames.
247   EXPECT_EQ(0, sender_.RtpSent());
248   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);  // kSequenceNumber
249   SendFrame(&sender_, sender_video_.get(),
250             kHigherLayerTid);  // kSequenceNumber + 1
251   SendFrame(&sender_, sender_video_.get(),
252             kNoTemporalIdx);  // kSequenceNumber + 2
253   EXPECT_EQ(3, sender_.RtpSent());
254   EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
255 
256   // Min required delay until retransmit = 5 + RTT ms (RTT = 0).
257   clock_.AdvanceTimeMilliseconds(5);
258 
259   // Frame with kBaseLayerTid re-sent.
260   IncomingRtcpNack(&sender_, kSequenceNumber);
261   EXPECT_EQ(4, sender_.RtpSent());
262   EXPECT_EQ(kSequenceNumber, sender_.LastRtpSequenceNumber());
263   // Frame with kHigherLayerTid re-sent.
264   IncomingRtcpNack(&sender_, kSequenceNumber + 1);
265   EXPECT_EQ(5, sender_.RtpSent());
266   EXPECT_EQ(kSequenceNumber + 1, sender_.LastRtpSequenceNumber());
267   // Frame with kNoTemporalIdx re-sent.
268   IncomingRtcpNack(&sender_, kSequenceNumber + 2);
269   EXPECT_EQ(6, sender_.RtpSent());
270   EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
271 }
272 
TEST_F(RtpRtcpImplTest,Rtt)273 TEST_F(RtpRtcpImplTest, Rtt) {
274   RtpPacketReceived packet;
275   packet.SetTimestamp(1);
276   packet.SetSequenceNumber(123);
277   packet.SetSsrc(kSenderSsrc);
278   packet.AllocatePayload(100 - 12);
279   receiver_.receive_statistics_->OnRtpPacket(packet);
280 
281   // Send Frame before sending an SR.
282   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
283   // Sender module should send an SR.
284   EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
285 
286   // Receiver module should send a RR with a response to the last received SR.
287   clock_.AdvanceTimeMilliseconds(1000);
288   EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
289 
290   // Verify RTT.
291   int64_t rtt;
292   int64_t avg_rtt;
293   int64_t min_rtt;
294   int64_t max_rtt;
295   EXPECT_EQ(
296       0, sender_.impl_->RTT(kReceiverSsrc, &rtt, &avg_rtt, &min_rtt, &max_rtt));
297   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, rtt, 1);
298   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, avg_rtt, 1);
299   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, min_rtt, 1);
300   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, max_rtt, 1);
301 
302   // No RTT from other ssrc.
303   EXPECT_EQ(-1, sender_.impl_->RTT(kReceiverSsrc + 1, &rtt, &avg_rtt, &min_rtt,
304                                    &max_rtt));
305 
306   // Verify RTT from rtt_stats config.
307   EXPECT_EQ(0, sender_.rtt_stats_.LastProcessedRtt());
308   EXPECT_EQ(0, sender_.impl_->rtt_ms());
309   sender_.impl_->Process();
310   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, sender_.rtt_stats_.LastProcessedRtt(),
311               1);
312   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, sender_.impl_->rtt_ms(), 1);
313 }
314 
TEST_F(RtpRtcpImplTest,SetRtcpXrRrtrStatus)315 TEST_F(RtpRtcpImplTest, SetRtcpXrRrtrStatus) {
316   EXPECT_FALSE(receiver_.impl_->RtcpXrRrtrStatus());
317   receiver_.impl_->SetRtcpXrRrtrStatus(true);
318   EXPECT_TRUE(receiver_.impl_->RtcpXrRrtrStatus());
319 }
320 
TEST_F(RtpRtcpImplTest,RttForReceiverOnly)321 TEST_F(RtpRtcpImplTest, RttForReceiverOnly) {
322   receiver_.impl_->SetRtcpXrRrtrStatus(true);
323 
324   // Receiver module should send a Receiver time reference report (RTRR).
325   EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
326 
327   // Sender module should send a response to the last received RTRR (DLRR).
328   clock_.AdvanceTimeMilliseconds(1000);
329   // Send Frame before sending a SR.
330   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
331   EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
332 
333   // Verify RTT.
334   EXPECT_EQ(0, receiver_.rtt_stats_.LastProcessedRtt());
335   EXPECT_EQ(0, receiver_.impl_->rtt_ms());
336   receiver_.impl_->Process();
337   EXPECT_NEAR(2 * kOneWayNetworkDelayMs,
338               receiver_.rtt_stats_.LastProcessedRtt(), 1);
339   EXPECT_NEAR(2 * kOneWayNetworkDelayMs, receiver_.impl_->rtt_ms(), 1);
340 }
341 
TEST_F(RtpRtcpImplTest,NoSrBeforeMedia)342 TEST_F(RtpRtcpImplTest, NoSrBeforeMedia) {
343   // Ignore fake transport delays in this test.
344   sender_.transport_.SimulateNetworkDelay(0, &clock_);
345   receiver_.transport_.SimulateNetworkDelay(0, &clock_);
346 
347   sender_.impl_->Process();
348   EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
349 
350   // Verify no SR is sent before media has been sent, RR should still be sent
351   // from the receiving module though.
352   clock_.AdvanceTimeMilliseconds(2000);
353   int64_t current_time = clock_.TimeInMilliseconds();
354   sender_.impl_->Process();
355   receiver_.impl_->Process();
356   EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
357   EXPECT_EQ(receiver_.RtcpSent().first_packet_time_ms, current_time);
358 
359   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
360   EXPECT_EQ(sender_.RtcpSent().first_packet_time_ms, current_time);
361 }
362 
TEST_F(RtpRtcpImplTest,RtcpPacketTypeCounter_Nack)363 TEST_F(RtpRtcpImplTest, RtcpPacketTypeCounter_Nack) {
364   EXPECT_EQ(-1, receiver_.RtcpSent().first_packet_time_ms);
365   EXPECT_EQ(-1, sender_.RtcpReceived().first_packet_time_ms);
366   EXPECT_EQ(0U, sender_.RtcpReceived().nack_packets);
367   EXPECT_EQ(0U, receiver_.RtcpSent().nack_packets);
368 
369   // Receive module sends a NACK.
370   const uint16_t kNackLength = 1;
371   uint16_t nack_list[kNackLength] = {123};
372   EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list, kNackLength));
373   EXPECT_EQ(1U, receiver_.RtcpSent().nack_packets);
374   EXPECT_GT(receiver_.RtcpSent().first_packet_time_ms, -1);
375 
376   // Send module receives the NACK.
377   EXPECT_EQ(1U, sender_.RtcpReceived().nack_packets);
378   EXPECT_GT(sender_.RtcpReceived().first_packet_time_ms, -1);
379 }
380 
TEST_F(RtpRtcpImplTest,AddStreamDataCounters)381 TEST_F(RtpRtcpImplTest, AddStreamDataCounters) {
382   StreamDataCounters rtp;
383   const int64_t kStartTimeMs = 1;
384   rtp.first_packet_time_ms = kStartTimeMs;
385   rtp.transmitted.packets = 1;
386   rtp.transmitted.payload_bytes = 1;
387   rtp.transmitted.header_bytes = 2;
388   rtp.transmitted.padding_bytes = 3;
389   EXPECT_EQ(rtp.transmitted.TotalBytes(), rtp.transmitted.payload_bytes +
390                                               rtp.transmitted.header_bytes +
391                                               rtp.transmitted.padding_bytes);
392 
393   StreamDataCounters rtp2;
394   rtp2.first_packet_time_ms = -1;
395   rtp2.transmitted.packets = 10;
396   rtp2.transmitted.payload_bytes = 10;
397   rtp2.retransmitted.header_bytes = 4;
398   rtp2.retransmitted.payload_bytes = 5;
399   rtp2.retransmitted.padding_bytes = 6;
400   rtp2.retransmitted.packets = 7;
401   rtp2.fec.packets = 8;
402 
403   StreamDataCounters sum = rtp;
404   sum.Add(rtp2);
405   EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms);
406   EXPECT_EQ(11U, sum.transmitted.packets);
407   EXPECT_EQ(11U, sum.transmitted.payload_bytes);
408   EXPECT_EQ(2U, sum.transmitted.header_bytes);
409   EXPECT_EQ(3U, sum.transmitted.padding_bytes);
410   EXPECT_EQ(4U, sum.retransmitted.header_bytes);
411   EXPECT_EQ(5U, sum.retransmitted.payload_bytes);
412   EXPECT_EQ(6U, sum.retransmitted.padding_bytes);
413   EXPECT_EQ(7U, sum.retransmitted.packets);
414   EXPECT_EQ(8U, sum.fec.packets);
415   EXPECT_EQ(sum.transmitted.TotalBytes(),
416             rtp.transmitted.TotalBytes() + rtp2.transmitted.TotalBytes());
417 
418   StreamDataCounters rtp3;
419   rtp3.first_packet_time_ms = kStartTimeMs + 10;
420   sum.Add(rtp3);
421   EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms);  // Holds oldest time.
422 }
423 
TEST_F(RtpRtcpImplTest,SendsInitialNackList)424 TEST_F(RtpRtcpImplTest, SendsInitialNackList) {
425   // Send module sends a NACK.
426   const uint16_t kNackLength = 1;
427   uint16_t nack_list[kNackLength] = {123};
428   EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
429   // Send Frame before sending a compound RTCP that starts with SR.
430   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
431   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
432   EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
433   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
434 }
435 
TEST_F(RtpRtcpImplTest,SendsExtendedNackList)436 TEST_F(RtpRtcpImplTest, SendsExtendedNackList) {
437   // Send module sends a NACK.
438   const uint16_t kNackLength = 1;
439   uint16_t nack_list[kNackLength] = {123};
440   EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
441   // Send Frame before sending a compound RTCP that starts with SR.
442   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
443   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
444   EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
445   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
446 
447   // Same list not re-send.
448   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
449   EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
450   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
451 
452   // Only extended list sent.
453   const uint16_t kNackExtLength = 2;
454   uint16_t nack_list_ext[kNackExtLength] = {123, 124};
455   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list_ext, kNackExtLength));
456   EXPECT_EQ(2U, sender_.RtcpSent().nack_packets);
457   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(124));
458 }
459 
TEST_F(RtpRtcpImplTest,ReSendsNackListAfterRttMs)460 TEST_F(RtpRtcpImplTest, ReSendsNackListAfterRttMs) {
461   sender_.transport_.SimulateNetworkDelay(0, &clock_);
462   // Send module sends a NACK.
463   const uint16_t kNackLength = 2;
464   uint16_t nack_list[kNackLength] = {123, 125};
465   EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
466   // Send Frame before sending a compound RTCP that starts with SR.
467   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
468   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
469   EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
470   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123, 125));
471 
472   // Same list not re-send, rtt interval has not passed.
473   const int kStartupRttMs = 100;
474   clock_.AdvanceTimeMilliseconds(kStartupRttMs);
475   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
476   EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
477 
478   // Rtt interval passed, full list sent.
479   clock_.AdvanceTimeMilliseconds(1);
480   EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
481   EXPECT_EQ(2U, sender_.RtcpSent().nack_packets);
482   EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123, 125));
483 }
484 
TEST_F(RtpRtcpImplTest,UniqueNackRequests)485 TEST_F(RtpRtcpImplTest, UniqueNackRequests) {
486   receiver_.transport_.SimulateNetworkDelay(0, &clock_);
487   EXPECT_EQ(0U, receiver_.RtcpSent().nack_packets);
488   EXPECT_EQ(0U, receiver_.RtcpSent().nack_requests);
489   EXPECT_EQ(0U, receiver_.RtcpSent().unique_nack_requests);
490   EXPECT_EQ(0, receiver_.RtcpSent().UniqueNackRequestsInPercent());
491 
492   // Receive module sends NACK request.
493   const uint16_t kNackLength = 4;
494   uint16_t nack_list[kNackLength] = {10, 11, 13, 18};
495   EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list, kNackLength));
496   EXPECT_EQ(1U, receiver_.RtcpSent().nack_packets);
497   EXPECT_EQ(4U, receiver_.RtcpSent().nack_requests);
498   EXPECT_EQ(4U, receiver_.RtcpSent().unique_nack_requests);
499   EXPECT_THAT(receiver_.LastNackListSent(), ElementsAre(10, 11, 13, 18));
500 
501   // Send module receives the request.
502   EXPECT_EQ(1U, sender_.RtcpReceived().nack_packets);
503   EXPECT_EQ(4U, sender_.RtcpReceived().nack_requests);
504   EXPECT_EQ(4U, sender_.RtcpReceived().unique_nack_requests);
505   EXPECT_EQ(100, sender_.RtcpReceived().UniqueNackRequestsInPercent());
506 
507   // Receive module sends new request with duplicated packets.
508   const int kStartupRttMs = 100;
509   clock_.AdvanceTimeMilliseconds(kStartupRttMs + 1);
510   const uint16_t kNackLength2 = 4;
511   uint16_t nack_list2[kNackLength2] = {11, 18, 20, 21};
512   EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list2, kNackLength2));
513   EXPECT_EQ(2U, receiver_.RtcpSent().nack_packets);
514   EXPECT_EQ(8U, receiver_.RtcpSent().nack_requests);
515   EXPECT_EQ(6U, receiver_.RtcpSent().unique_nack_requests);
516   EXPECT_THAT(receiver_.LastNackListSent(), ElementsAre(11, 18, 20, 21));
517 
518   // Send module receives the request.
519   EXPECT_EQ(2U, sender_.RtcpReceived().nack_packets);
520   EXPECT_EQ(8U, sender_.RtcpReceived().nack_requests);
521   EXPECT_EQ(6U, sender_.RtcpReceived().unique_nack_requests);
522   EXPECT_EQ(75, sender_.RtcpReceived().UniqueNackRequestsInPercent());
523 }
524 
TEST_F(RtpRtcpImplTest,ConfigurableRtcpReportInterval)525 TEST_F(RtpRtcpImplTest, ConfigurableRtcpReportInterval) {
526   const int kVideoReportInterval = 3000;
527 
528   // Recreate sender impl with new configuration, and redo setup.
529   sender_.SetRtcpReportIntervalAndReset(kVideoReportInterval);
530   SetUp();
531 
532   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
533 
534   // Initial state
535   sender_.impl_->Process();
536   EXPECT_EQ(sender_.RtcpSent().first_packet_time_ms, -1);
537   EXPECT_EQ(0u, sender_.transport_.NumRtcpSent());
538 
539   // Move ahead to the last ms before a rtcp is expected, no action.
540   clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2 - 1);
541   sender_.impl_->Process();
542   EXPECT_EQ(sender_.RtcpSent().first_packet_time_ms, -1);
543   EXPECT_EQ(sender_.transport_.NumRtcpSent(), 0u);
544 
545   // Move ahead to the first rtcp. Send RTCP.
546   clock_.AdvanceTimeMilliseconds(1);
547   sender_.impl_->Process();
548   EXPECT_GT(sender_.RtcpSent().first_packet_time_ms, -1);
549   EXPECT_EQ(sender_.transport_.NumRtcpSent(), 1u);
550 
551   SendFrame(&sender_, sender_video_.get(), kBaseLayerTid);
552 
553   // Move ahead to the last possible second before second rtcp is expected.
554   clock_.AdvanceTimeMilliseconds(kVideoReportInterval * 1 / 2 - 1);
555   sender_.impl_->Process();
556   EXPECT_EQ(sender_.transport_.NumRtcpSent(), 1u);
557 
558   // Move ahead into the range of second rtcp, the second rtcp may be sent.
559   clock_.AdvanceTimeMilliseconds(1);
560   sender_.impl_->Process();
561   EXPECT_GE(sender_.transport_.NumRtcpSent(), 1u);
562 
563   clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2);
564   sender_.impl_->Process();
565   EXPECT_GE(sender_.transport_.NumRtcpSent(), 1u);
566 
567   // Move out the range of second rtcp, the second rtcp must have been sent.
568   clock_.AdvanceTimeMilliseconds(kVideoReportInterval / 2);
569   sender_.impl_->Process();
570   EXPECT_EQ(sender_.transport_.NumRtcpSent(), 2u);
571 }
572 
TEST_F(RtpRtcpImplTest,StoresPacketInfoForSentPackets)573 TEST_F(RtpRtcpImplTest, StoresPacketInfoForSentPackets) {
574   const uint32_t kStartTimestamp = 1u;
575   SetUp();
576   sender_.impl_->SetStartTimestamp(kStartTimestamp);
577 
578   PacedPacketInfo pacing_info;
579   RtpPacketToSend packet(nullptr);
580   packet.set_packet_type(RtpPacketToSend::Type::kVideo);
581   packet.SetSsrc(kSenderSsrc);
582 
583   // Single-packet frame.
584   packet.SetTimestamp(1);
585   packet.SetSequenceNumber(1);
586   packet.set_first_packet_of_frame(true);
587   packet.SetMarker(true);
588   sender_.impl_->TrySendPacket(&packet, pacing_info);
589 
590   std::vector<RtpSequenceNumberMap::Info> seqno_info =
591       sender_.impl_->GetSentRtpPacketInfos(std::vector<uint16_t>{1});
592 
593   EXPECT_THAT(seqno_info, ElementsAre(RtpSequenceNumberMap::Info(
594                               /*timestamp=*/1 - kStartTimestamp,
595                               /*is_first=*/1,
596                               /*is_last=*/1)));
597 
598   // Three-packet frame.
599   packet.SetTimestamp(2);
600   packet.SetSequenceNumber(2);
601   packet.set_first_packet_of_frame(true);
602   packet.SetMarker(false);
603   sender_.impl_->TrySendPacket(&packet, pacing_info);
604 
605   packet.SetSequenceNumber(3);
606   packet.set_first_packet_of_frame(false);
607   sender_.impl_->TrySendPacket(&packet, pacing_info);
608 
609   packet.SetSequenceNumber(4);
610   packet.SetMarker(true);
611   sender_.impl_->TrySendPacket(&packet, pacing_info);
612 
613   seqno_info =
614       sender_.impl_->GetSentRtpPacketInfos(std::vector<uint16_t>{2, 3, 4});
615 
616   EXPECT_THAT(seqno_info, ElementsAre(RtpSequenceNumberMap::Info(
617                                           /*timestamp=*/2 - kStartTimestamp,
618                                           /*is_first=*/1,
619                                           /*is_last=*/0),
620                                       RtpSequenceNumberMap::Info(
621                                           /*timestamp=*/2 - kStartTimestamp,
622                                           /*is_first=*/0,
623                                           /*is_last=*/0),
624                                       RtpSequenceNumberMap::Info(
625                                           /*timestamp=*/2 - kStartTimestamp,
626                                           /*is_first=*/0,
627                                           /*is_last=*/1)));
628 }
629 
630 }  // namespace webrtc
631