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 <map>
12#include <set>
13
14#include "testing/gmock/include/gmock/gmock.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17#include "webrtc/common_types.h"
18#include "webrtc/modules/rtp_rtcp/include/rtp_header_parser.h"
19#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
20#include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
21#include "webrtc/modules/rtp_rtcp/source/rtcp_packet/nack.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
23#include "webrtc/system_wrappers/include/scoped_vector.h"
24#include "webrtc/test/rtcp_packet_parser.h"
25
26using ::testing::_;
27using ::testing::ElementsAre;
28using ::testing::NiceMock;
29using ::testing::Return;
30using ::testing::SaveArg;
31
32namespace webrtc {
33namespace {
34const uint32_t kSenderSsrc = 0x12345;
35const uint32_t kReceiverSsrc = 0x23456;
36const int64_t kOneWayNetworkDelayMs = 100;
37const uint8_t kBaseLayerTid = 0;
38const uint8_t kHigherLayerTid = 1;
39const uint16_t kSequenceNumber = 100;
40
41class RtcpRttStatsTestImpl : public RtcpRttStats {
42 public:
43  RtcpRttStatsTestImpl() : rtt_ms_(0) {}
44  virtual ~RtcpRttStatsTestImpl() {}
45
46  void OnRttUpdate(int64_t rtt_ms) override { rtt_ms_ = rtt_ms; }
47  int64_t LastProcessedRtt() const override { return rtt_ms_; }
48  int64_t rtt_ms_;
49};
50
51class SendTransport : public Transport,
52                      public NullRtpData {
53 public:
54  SendTransport()
55      : receiver_(NULL),
56        clock_(NULL),
57        delay_ms_(0),
58        rtp_packets_sent_(0) {
59  }
60
61  void SetRtpRtcpModule(ModuleRtpRtcpImpl* receiver) {
62    receiver_ = receiver;
63  }
64  void SimulateNetworkDelay(int64_t delay_ms, SimulatedClock* clock) {
65    clock_ = clock;
66    delay_ms_ = delay_ms;
67  }
68  bool SendRtp(const uint8_t* data,
69               size_t len,
70               const PacketOptions& options) override {
71    RTPHeader header;
72    rtc::scoped_ptr<RtpHeaderParser> parser(RtpHeaderParser::Create());
73    EXPECT_TRUE(parser->Parse(static_cast<const uint8_t*>(data), len, &header));
74    ++rtp_packets_sent_;
75    last_rtp_header_ = header;
76    return true;
77  }
78  bool SendRtcp(const uint8_t* data, size_t len) override {
79    test::RtcpPacketParser parser;
80    parser.Parse(static_cast<const uint8_t*>(data), len);
81    last_nack_list_ = parser.nack_item()->last_nack_list();
82
83    if (clock_) {
84      clock_->AdvanceTimeMilliseconds(delay_ms_);
85    }
86    EXPECT_TRUE(receiver_ != NULL);
87    EXPECT_EQ(0, receiver_->IncomingRtcpPacket(
88        static_cast<const uint8_t*>(data), len));
89    return true;
90  }
91  ModuleRtpRtcpImpl* receiver_;
92  SimulatedClock* clock_;
93  int64_t delay_ms_;
94  int rtp_packets_sent_;
95  RTPHeader last_rtp_header_;
96  std::vector<uint16_t> last_nack_list_;
97};
98
99class RtpRtcpModule : public RtcpPacketTypeCounterObserver {
100 public:
101  explicit RtpRtcpModule(SimulatedClock* clock)
102      : receive_statistics_(ReceiveStatistics::Create(clock)) {
103    RtpRtcp::Configuration config;
104    config.audio = false;
105    config.clock = clock;
106    config.outgoing_transport = &transport_;
107    config.receive_statistics = receive_statistics_.get();
108    config.rtcp_packet_type_counter_observer = this;
109    config.rtt_stats = &rtt_stats_;
110
111    impl_.reset(new ModuleRtpRtcpImpl(config));
112    impl_->SetRTCPStatus(RtcpMode::kCompound);
113
114    transport_.SimulateNetworkDelay(kOneWayNetworkDelayMs, clock);
115  }
116
117  RtcpPacketTypeCounter packets_sent_;
118  RtcpPacketTypeCounter packets_received_;
119  rtc::scoped_ptr<ReceiveStatistics> receive_statistics_;
120  SendTransport transport_;
121  RtcpRttStatsTestImpl rtt_stats_;
122  rtc::scoped_ptr<ModuleRtpRtcpImpl> impl_;
123  uint32_t remote_ssrc_;
124
125  void SetRemoteSsrc(uint32_t ssrc) {
126    remote_ssrc_ = ssrc;
127    impl_->SetRemoteSSRC(ssrc);
128  }
129
130  void RtcpPacketTypesCounterUpdated(
131      uint32_t ssrc,
132      const RtcpPacketTypeCounter& packet_counter) override {
133    counter_map_[ssrc] = packet_counter;
134  }
135
136  RtcpPacketTypeCounter RtcpSent() {
137    // RTCP counters for remote SSRC.
138    return counter_map_[remote_ssrc_];
139  }
140
141  RtcpPacketTypeCounter RtcpReceived() {
142    // Received RTCP stats for (own) local SSRC.
143    return counter_map_[impl_->SSRC()];
144  }
145  int RtpSent() {
146    return transport_.rtp_packets_sent_;
147  }
148  uint16_t LastRtpSequenceNumber() {
149    return transport_.last_rtp_header_.sequenceNumber;
150  }
151  std::vector<uint16_t> LastNackListSent() {
152    return transport_.last_nack_list_;
153  }
154
155 private:
156  std::map<uint32_t, RtcpPacketTypeCounter> counter_map_;
157};
158}  // namespace
159
160class RtpRtcpImplTest : public ::testing::Test {
161 protected:
162  RtpRtcpImplTest()
163      : clock_(133590000000000),
164        sender_(&clock_),
165        receiver_(&clock_) {
166    // Send module.
167    EXPECT_EQ(0, sender_.impl_->SetSendingStatus(true));
168    sender_.impl_->SetSendingMediaStatus(true);
169    sender_.impl_->SetSSRC(kSenderSsrc);
170    sender_.SetRemoteSsrc(kReceiverSsrc);
171    sender_.impl_->SetSequenceNumber(kSequenceNumber);
172    sender_.impl_->SetStorePacketsStatus(true, 100);
173
174    memset(&codec_, 0, sizeof(VideoCodec));
175    codec_.plType = 100;
176    strncpy(codec_.plName, "VP8", 3);
177    codec_.width = 320;
178    codec_.height = 180;
179    EXPECT_EQ(0, sender_.impl_->RegisterSendPayload(codec_));
180
181    // Receive module.
182    EXPECT_EQ(0, receiver_.impl_->SetSendingStatus(false));
183    receiver_.impl_->SetSendingMediaStatus(false);
184    receiver_.impl_->SetSSRC(kReceiverSsrc);
185    receiver_.SetRemoteSsrc(kSenderSsrc);
186    // Transport settings.
187    sender_.transport_.SetRtpRtcpModule(receiver_.impl_.get());
188    receiver_.transport_.SetRtpRtcpModule(sender_.impl_.get());
189  }
190  SimulatedClock clock_;
191  RtpRtcpModule sender_;
192  RtpRtcpModule receiver_;
193  VideoCodec codec_;
194
195  void SendFrame(const RtpRtcpModule* module, uint8_t tid) {
196    RTPVideoHeaderVP8 vp8_header = {};
197    vp8_header.temporalIdx = tid;
198    RTPVideoHeader rtp_video_header = {codec_.width,
199                                       codec_.height,
200                                       kVideoRotation_0,
201                                       true,
202                                       0,
203                                       kRtpVideoVp8,
204                                       {vp8_header}};
205
206    const uint8_t payload[100] = {0};
207    EXPECT_EQ(0, module->impl_->SendOutgoingData(kVideoFrameKey,
208                                                 codec_.plType,
209                                                 0,
210                                                 0,
211                                                 payload,
212                                                 sizeof(payload),
213                                                 NULL,
214                                                 &rtp_video_header));
215  }
216
217  void IncomingRtcpNack(const RtpRtcpModule* module, uint16_t sequence_number) {
218    bool sender = module->impl_->SSRC() == kSenderSsrc;
219    rtcp::Nack nack;
220    uint16_t list[1];
221    list[0] = sequence_number;
222    const uint16_t kListLength = sizeof(list) / sizeof(list[0]);
223    nack.From(sender ? kReceiverSsrc : kSenderSsrc);
224    nack.To(sender ? kSenderSsrc : kReceiverSsrc);
225    nack.WithList(list, kListLength);
226    rtc::scoped_ptr<rtcp::RawPacket> packet(nack.Build());
227    EXPECT_EQ(0, module->impl_->IncomingRtcpPacket(packet->Buffer(),
228                                                   packet->Length()));
229  }
230};
231
232TEST_F(RtpRtcpImplTest, SetSelectiveRetransmissions_BaseLayer) {
233  sender_.impl_->SetSelectiveRetransmissions(kRetransmitBaseLayer);
234  EXPECT_EQ(kRetransmitBaseLayer, sender_.impl_->SelectiveRetransmissions());
235
236  // Send frames.
237  EXPECT_EQ(0, sender_.RtpSent());
238  SendFrame(&sender_, kBaseLayerTid);    // kSequenceNumber
239  SendFrame(&sender_, kHigherLayerTid);  // kSequenceNumber + 1
240  SendFrame(&sender_, kNoTemporalIdx);   // kSequenceNumber + 2
241  EXPECT_EQ(3, sender_.RtpSent());
242  EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
243
244  // Min required delay until retransmit = 5 + RTT ms (RTT = 0).
245  clock_.AdvanceTimeMilliseconds(5);
246
247  // Frame with kBaseLayerTid re-sent.
248  IncomingRtcpNack(&sender_, kSequenceNumber);
249  EXPECT_EQ(4, sender_.RtpSent());
250  EXPECT_EQ(kSequenceNumber, sender_.LastRtpSequenceNumber());
251  // Frame with kHigherLayerTid not re-sent.
252  IncomingRtcpNack(&sender_, kSequenceNumber + 1);
253  EXPECT_EQ(4, sender_.RtpSent());
254  // Frame with kNoTemporalIdx re-sent.
255  IncomingRtcpNack(&sender_, kSequenceNumber + 2);
256  EXPECT_EQ(5, sender_.RtpSent());
257  EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
258}
259
260TEST_F(RtpRtcpImplTest, SetSelectiveRetransmissions_HigherLayers) {
261  const uint8_t kSetting = kRetransmitBaseLayer + kRetransmitHigherLayers;
262  sender_.impl_->SetSelectiveRetransmissions(kSetting);
263  EXPECT_EQ(kSetting, sender_.impl_->SelectiveRetransmissions());
264
265  // Send frames.
266  EXPECT_EQ(0, sender_.RtpSent());
267  SendFrame(&sender_, kBaseLayerTid);    // kSequenceNumber
268  SendFrame(&sender_, kHigherLayerTid);  // kSequenceNumber + 1
269  SendFrame(&sender_, kNoTemporalIdx);   // kSequenceNumber + 2
270  EXPECT_EQ(3, sender_.RtpSent());
271  EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
272
273  // Min required delay until retransmit = 5 + RTT ms (RTT = 0).
274  clock_.AdvanceTimeMilliseconds(5);
275
276  // Frame with kBaseLayerTid re-sent.
277  IncomingRtcpNack(&sender_, kSequenceNumber);
278  EXPECT_EQ(4, sender_.RtpSent());
279  EXPECT_EQ(kSequenceNumber, sender_.LastRtpSequenceNumber());
280  // Frame with kHigherLayerTid re-sent.
281  IncomingRtcpNack(&sender_, kSequenceNumber + 1);
282  EXPECT_EQ(5, sender_.RtpSent());
283  EXPECT_EQ(kSequenceNumber + 1, sender_.LastRtpSequenceNumber());
284  // Frame with kNoTemporalIdx re-sent.
285  IncomingRtcpNack(&sender_, kSequenceNumber + 2);
286  EXPECT_EQ(6, sender_.RtpSent());
287  EXPECT_EQ(kSequenceNumber + 2, sender_.LastRtpSequenceNumber());
288}
289
290TEST_F(RtpRtcpImplTest, Rtt) {
291  RTPHeader header;
292  header.timestamp = 1;
293  header.sequenceNumber = 123;
294  header.ssrc = kSenderSsrc;
295  header.headerLength = 12;
296  receiver_.receive_statistics_->IncomingPacket(header, 100, false);
297
298  // Sender module should send a SR.
299  EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
300
301  // Receiver module should send a RR with a response to the last received SR.
302  clock_.AdvanceTimeMilliseconds(1000);
303  EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
304
305  // Verify RTT.
306  int64_t rtt;
307  int64_t avg_rtt;
308  int64_t min_rtt;
309  int64_t max_rtt;
310  EXPECT_EQ(0,
311      sender_.impl_->RTT(kReceiverSsrc, &rtt, &avg_rtt, &min_rtt, &max_rtt));
312  EXPECT_EQ(2 * kOneWayNetworkDelayMs, rtt);
313  EXPECT_EQ(2 * kOneWayNetworkDelayMs, avg_rtt);
314  EXPECT_EQ(2 * kOneWayNetworkDelayMs, min_rtt);
315  EXPECT_EQ(2 * kOneWayNetworkDelayMs, max_rtt);
316
317  // No RTT from other ssrc.
318  EXPECT_EQ(-1,
319      sender_.impl_->RTT(kReceiverSsrc+1, &rtt, &avg_rtt, &min_rtt, &max_rtt));
320
321  // Verify RTT from rtt_stats config.
322  EXPECT_EQ(0, sender_.rtt_stats_.LastProcessedRtt());
323  EXPECT_EQ(0, sender_.impl_->rtt_ms());
324  sender_.impl_->Process();
325  EXPECT_EQ(2 * kOneWayNetworkDelayMs, sender_.rtt_stats_.LastProcessedRtt());
326  EXPECT_EQ(2 * kOneWayNetworkDelayMs, sender_.impl_->rtt_ms());
327}
328
329TEST_F(RtpRtcpImplTest, SetRtcpXrRrtrStatus) {
330  EXPECT_FALSE(receiver_.impl_->RtcpXrRrtrStatus());
331  receiver_.impl_->SetRtcpXrRrtrStatus(true);
332  EXPECT_TRUE(receiver_.impl_->RtcpXrRrtrStatus());
333}
334
335TEST_F(RtpRtcpImplTest, RttForReceiverOnly) {
336  receiver_.impl_->SetRtcpXrRrtrStatus(true);
337
338  // Receiver module should send a Receiver time reference report (RTRR).
339  EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpReport));
340
341  // Sender module should send a response to the last received RTRR (DLRR).
342  clock_.AdvanceTimeMilliseconds(1000);
343  EXPECT_EQ(0, sender_.impl_->SendRTCP(kRtcpReport));
344
345  // Verify RTT.
346  EXPECT_EQ(0, receiver_.rtt_stats_.LastProcessedRtt());
347  EXPECT_EQ(0, receiver_.impl_->rtt_ms());
348  receiver_.impl_->Process();
349  EXPECT_EQ(2 * kOneWayNetworkDelayMs, receiver_.rtt_stats_.LastProcessedRtt());
350  EXPECT_EQ(2 * kOneWayNetworkDelayMs, receiver_.impl_->rtt_ms());
351}
352
353TEST_F(RtpRtcpImplTest, NoSrBeforeMedia) {
354  // Ignore fake transport delays in this test.
355  sender_.transport_.SimulateNetworkDelay(0, &clock_);
356  receiver_.transport_.SimulateNetworkDelay(0, &clock_);
357
358  sender_.impl_->Process();
359  EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
360
361  // Verify no SR is sent before media has been sent, RR should still be sent
362  // from the receiving module though.
363  clock_.AdvanceTimeMilliseconds(2000);
364  int64_t current_time = clock_.TimeInMilliseconds();
365  sender_.impl_->Process();
366  receiver_.impl_->Process();
367  EXPECT_EQ(-1, sender_.RtcpSent().first_packet_time_ms);
368  EXPECT_EQ(receiver_.RtcpSent().first_packet_time_ms, current_time);
369
370  SendFrame(&sender_, kBaseLayerTid);
371  EXPECT_EQ(sender_.RtcpSent().first_packet_time_ms, current_time);
372}
373
374TEST_F(RtpRtcpImplTest, RtcpPacketTypeCounter_Nack) {
375  EXPECT_EQ(-1, receiver_.RtcpSent().first_packet_time_ms);
376  EXPECT_EQ(-1, sender_.RtcpReceived().first_packet_time_ms);
377  EXPECT_EQ(0U, sender_.RtcpReceived().nack_packets);
378  EXPECT_EQ(0U, receiver_.RtcpSent().nack_packets);
379
380  // Receive module sends a NACK.
381  const uint16_t kNackLength = 1;
382  uint16_t nack_list[kNackLength] = {123};
383  EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list, kNackLength));
384  EXPECT_EQ(1U, receiver_.RtcpSent().nack_packets);
385  EXPECT_GT(receiver_.RtcpSent().first_packet_time_ms, -1);
386
387  // Send module receives the NACK.
388  EXPECT_EQ(1U, sender_.RtcpReceived().nack_packets);
389  EXPECT_GT(sender_.RtcpReceived().first_packet_time_ms, -1);
390}
391
392TEST_F(RtpRtcpImplTest, RtcpPacketTypeCounter_FirAndPli) {
393  EXPECT_EQ(0U, sender_.RtcpReceived().fir_packets);
394  EXPECT_EQ(0U, receiver_.RtcpSent().fir_packets);
395  // Receive module sends a FIR.
396  EXPECT_EQ(0, receiver_.impl_->SendRTCP(kRtcpFir));
397  EXPECT_EQ(1U, receiver_.RtcpSent().fir_packets);
398  // Send module receives the FIR.
399  EXPECT_EQ(1U, sender_.RtcpReceived().fir_packets);
400
401  // Receive module sends a FIR and PLI.
402  std::set<RTCPPacketType> packet_types;
403  packet_types.insert(kRtcpFir);
404  packet_types.insert(kRtcpPli);
405  EXPECT_EQ(0, receiver_.impl_->SendCompoundRTCP(packet_types));
406  EXPECT_EQ(2U, receiver_.RtcpSent().fir_packets);
407  EXPECT_EQ(1U, receiver_.RtcpSent().pli_packets);
408  // Send module receives the FIR and PLI.
409  EXPECT_EQ(2U, sender_.RtcpReceived().fir_packets);
410  EXPECT_EQ(1U, sender_.RtcpReceived().pli_packets);
411}
412
413TEST_F(RtpRtcpImplTest, AddStreamDataCounters) {
414  StreamDataCounters rtp;
415  const int64_t kStartTimeMs = 1;
416  rtp.first_packet_time_ms = kStartTimeMs;
417  rtp.transmitted.packets = 1;
418  rtp.transmitted.payload_bytes = 1;
419  rtp.transmitted.header_bytes = 2;
420  rtp.transmitted.padding_bytes = 3;
421  EXPECT_EQ(rtp.transmitted.TotalBytes(), rtp.transmitted.payload_bytes +
422                                          rtp.transmitted.header_bytes +
423                                          rtp.transmitted.padding_bytes);
424
425  StreamDataCounters rtp2;
426  rtp2.first_packet_time_ms = -1;
427  rtp2.transmitted.packets = 10;
428  rtp2.transmitted.payload_bytes = 10;
429  rtp2.retransmitted.header_bytes = 4;
430  rtp2.retransmitted.payload_bytes = 5;
431  rtp2.retransmitted.padding_bytes = 6;
432  rtp2.retransmitted.packets = 7;
433  rtp2.fec.packets = 8;
434
435  StreamDataCounters sum = rtp;
436  sum.Add(rtp2);
437  EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms);
438  EXPECT_EQ(11U, sum.transmitted.packets);
439  EXPECT_EQ(11U, sum.transmitted.payload_bytes);
440  EXPECT_EQ(2U, sum.transmitted.header_bytes);
441  EXPECT_EQ(3U, sum.transmitted.padding_bytes);
442  EXPECT_EQ(4U, sum.retransmitted.header_bytes);
443  EXPECT_EQ(5U, sum.retransmitted.payload_bytes);
444  EXPECT_EQ(6U, sum.retransmitted.padding_bytes);
445  EXPECT_EQ(7U, sum.retransmitted.packets);
446  EXPECT_EQ(8U, sum.fec.packets);
447  EXPECT_EQ(sum.transmitted.TotalBytes(),
448            rtp.transmitted.TotalBytes() + rtp2.transmitted.TotalBytes());
449
450  StreamDataCounters rtp3;
451  rtp3.first_packet_time_ms = kStartTimeMs + 10;
452  sum.Add(rtp3);
453  EXPECT_EQ(kStartTimeMs, sum.first_packet_time_ms);  // Holds oldest time.
454}
455
456TEST_F(RtpRtcpImplTest, SendsInitialNackList) {
457  // Send module sends a NACK.
458  const uint16_t kNackLength = 1;
459  uint16_t nack_list[kNackLength] = {123};
460  EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
461  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
462  EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
463  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
464}
465
466TEST_F(RtpRtcpImplTest, SendsExtendedNackList) {
467  // Send module sends a NACK.
468  const uint16_t kNackLength = 1;
469  uint16_t nack_list[kNackLength] = {123};
470  EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
471  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
472  EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
473  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
474
475  // Same list not re-send.
476  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
477  EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
478  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123));
479
480  // Only extended list sent.
481  const uint16_t kNackExtLength = 2;
482  uint16_t nack_list_ext[kNackExtLength] = {123, 124};
483  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list_ext, kNackExtLength));
484  EXPECT_EQ(2U, sender_.RtcpSent().nack_packets);
485  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(124));
486}
487
488TEST_F(RtpRtcpImplTest, ReSendsNackListAfterRttMs) {
489  sender_.transport_.SimulateNetworkDelay(0, &clock_);
490  // Send module sends a NACK.
491  const uint16_t kNackLength = 2;
492  uint16_t nack_list[kNackLength] = {123, 125};
493  EXPECT_EQ(0U, sender_.RtcpSent().nack_packets);
494  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
495  EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
496  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123, 125));
497
498  // Same list not re-send, rtt interval has not passed.
499  const int kStartupRttMs = 100;
500  clock_.AdvanceTimeMilliseconds(kStartupRttMs);
501  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
502  EXPECT_EQ(1U, sender_.RtcpSent().nack_packets);
503
504  // Rtt interval passed, full list sent.
505  clock_.AdvanceTimeMilliseconds(1);
506  EXPECT_EQ(0, sender_.impl_->SendNACK(nack_list, kNackLength));
507  EXPECT_EQ(2U, sender_.RtcpSent().nack_packets);
508  EXPECT_THAT(sender_.LastNackListSent(), ElementsAre(123, 125));
509}
510
511TEST_F(RtpRtcpImplTest, UniqueNackRequests) {
512  receiver_.transport_.SimulateNetworkDelay(0, &clock_);
513  EXPECT_EQ(0U, receiver_.RtcpSent().nack_packets);
514  EXPECT_EQ(0U, receiver_.RtcpSent().nack_requests);
515  EXPECT_EQ(0U, receiver_.RtcpSent().unique_nack_requests);
516  EXPECT_EQ(0, receiver_.RtcpSent().UniqueNackRequestsInPercent());
517
518  // Receive module sends NACK request.
519  const uint16_t kNackLength = 4;
520  uint16_t nack_list[kNackLength] = {10, 11, 13, 18};
521  EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list, kNackLength));
522  EXPECT_EQ(1U, receiver_.RtcpSent().nack_packets);
523  EXPECT_EQ(4U, receiver_.RtcpSent().nack_requests);
524  EXPECT_EQ(4U, receiver_.RtcpSent().unique_nack_requests);
525  EXPECT_THAT(receiver_.LastNackListSent(), ElementsAre(10, 11, 13, 18));
526
527  // Send module receives the request.
528  EXPECT_EQ(1U, sender_.RtcpReceived().nack_packets);
529  EXPECT_EQ(4U, sender_.RtcpReceived().nack_requests);
530  EXPECT_EQ(4U, sender_.RtcpReceived().unique_nack_requests);
531  EXPECT_EQ(100, sender_.RtcpReceived().UniqueNackRequestsInPercent());
532
533  // Receive module sends new request with duplicated packets.
534  const int kStartupRttMs = 100;
535  clock_.AdvanceTimeMilliseconds(kStartupRttMs + 1);
536  const uint16_t kNackLength2 = 4;
537  uint16_t nack_list2[kNackLength2] = {11, 18, 20, 21};
538  EXPECT_EQ(0, receiver_.impl_->SendNACK(nack_list2, kNackLength2));
539  EXPECT_EQ(2U, receiver_.RtcpSent().nack_packets);
540  EXPECT_EQ(8U, receiver_.RtcpSent().nack_requests);
541  EXPECT_EQ(6U, receiver_.RtcpSent().unique_nack_requests);
542  EXPECT_THAT(receiver_.LastNackListSent(), ElementsAre(11, 18, 20, 21));
543
544  // Send module receives the request.
545  EXPECT_EQ(2U, sender_.RtcpReceived().nack_packets);
546  EXPECT_EQ(8U, sender_.RtcpReceived().nack_requests);
547  EXPECT_EQ(6U, sender_.RtcpReceived().unique_nack_requests);
548  EXPECT_EQ(75, sender_.RtcpReceived().UniqueNackRequestsInPercent());
549}
550}  // namespace webrtc
551