Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / rtp_rtcp / source / rtcp_receiver_unittest.cc
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
12 /*
13  * This file includes unit tests for the RTCPReceiver.
14  */
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 // Note: This file has no directory. Lint warning must be ignored.
19 #include "webrtc/common_types.h"
20 #include "webrtc/modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_observer.h"
21 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
22 #include "webrtc/modules/rtp_rtcp/source/rtcp_packet.h"
23 #include "webrtc/modules/rtp_rtcp/source/rtcp_receiver.h"
24 #include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
25 #include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
26 #include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
27
28 namespace webrtc {
29
30 namespace {  // Anonymous namespace; hide utility functions and classes.
31
32 // This test transport verifies that no functions get called.
33 class TestTransport : public Transport,
34                       public NullRtpData {
35  public:
36   explicit TestTransport()
37       : rtcp_receiver_(NULL) {
38   }
39   void SetRTCPReceiver(RTCPReceiver* rtcp_receiver) {
40     rtcp_receiver_ = rtcp_receiver;
41   }
42   virtual int SendPacket(int /*ch*/, const void* /*data*/, int /*len*/) {
43     ADD_FAILURE();  // FAIL() gives a compile error.
44     return -1;
45   }
46
47   // Injects an RTCP packet into the receiver.
48   virtual int SendRTCPPacket(int /* ch */, const void *packet, int packet_len) {
49     ADD_FAILURE();
50     return 0;
51   }
52
53   virtual int OnReceivedPayloadData(const uint8_t* payloadData,
54                                     const uint16_t payloadSize,
55                                     const WebRtcRTPHeader* rtpHeader) {
56     ADD_FAILURE();
57     return 0;
58   }
59   RTCPReceiver* rtcp_receiver_;
60 };
61
62 class RtcpReceiverTest : public ::testing::Test {
63  protected:
64   static const uint32_t kRemoteBitrateEstimatorMinBitrateBps = 30000;
65
66   RtcpReceiverTest()
67       : over_use_detector_options_(),
68         system_clock_(1335900000),
69         remote_bitrate_observer_(),
70         remote_bitrate_estimator_(
71             RemoteBitrateEstimatorFactory().Create(
72                 &remote_bitrate_observer_,
73                 &system_clock_,
74                 kMimdControl,
75                 kRemoteBitrateEstimatorMinBitrateBps)) {
76     test_transport_ = new TestTransport();
77
78     RtpRtcp::Configuration configuration;
79     configuration.id = 0;
80     configuration.audio = false;
81     configuration.clock = &system_clock_;
82     configuration.outgoing_transport = test_transport_;
83     configuration.remote_bitrate_estimator = remote_bitrate_estimator_.get();
84     rtp_rtcp_impl_ = new ModuleRtpRtcpImpl(configuration);
85     rtcp_receiver_ = new RTCPReceiver(0, &system_clock_, rtp_rtcp_impl_);
86     test_transport_->SetRTCPReceiver(rtcp_receiver_);
87   }
88   ~RtcpReceiverTest() {
89     delete rtcp_receiver_;
90     delete rtp_rtcp_impl_;
91     delete test_transport_;
92   }
93
94   // Injects an RTCP packet into the receiver.
95   // Returns 0 for OK, non-0 for failure.
96   int InjectRtcpPacket(const uint8_t* packet,
97                        uint16_t packet_len) {
98     RTCPUtility::RTCPParserV2 rtcpParser(packet,
99                                          packet_len,
100                                          true);  // Allow non-compound RTCP
101
102     RTCPHelp::RTCPPacketInformation rtcpPacketInformation;
103     EXPECT_EQ(0, rtcp_receiver_->IncomingRTCPPacket(rtcpPacketInformation,
104                                                     &rtcpParser));
105     rtcp_receiver_->TriggerCallbacksFromRTCPPacket(rtcpPacketInformation);
106     // The NACK list is on purpose not copied below as it isn't needed by the
107     // test.
108     rtcp_packet_info_.rtcpPacketTypeFlags =
109         rtcpPacketInformation.rtcpPacketTypeFlags;
110     rtcp_packet_info_.remoteSSRC = rtcpPacketInformation.remoteSSRC;
111     rtcp_packet_info_.applicationSubType =
112         rtcpPacketInformation.applicationSubType;
113     rtcp_packet_info_.applicationName = rtcpPacketInformation.applicationName;
114     rtcp_packet_info_.report_blocks = rtcpPacketInformation.report_blocks;
115     rtcp_packet_info_.rtt = rtcpPacketInformation.rtt;
116     rtcp_packet_info_.interArrivalJitter =
117         rtcpPacketInformation.interArrivalJitter;
118     rtcp_packet_info_.sliPictureId = rtcpPacketInformation.sliPictureId;
119     rtcp_packet_info_.rpsiPictureId = rtcpPacketInformation.rpsiPictureId;
120     rtcp_packet_info_.receiverEstimatedMaxBitrate =
121         rtcpPacketInformation.receiverEstimatedMaxBitrate;
122     rtcp_packet_info_.ntp_secs = rtcpPacketInformation.ntp_secs;
123     rtcp_packet_info_.ntp_frac = rtcpPacketInformation.ntp_frac;
124     rtcp_packet_info_.rtp_timestamp = rtcpPacketInformation.rtp_timestamp;
125     rtcp_packet_info_.xr_dlrr_item = rtcpPacketInformation.xr_dlrr_item;
126     if (rtcpPacketInformation.VoIPMetric) {
127       rtcp_packet_info_.AddVoIPMetric(rtcpPacketInformation.VoIPMetric);
128     }
129     return 0;
130   }
131
132   OverUseDetectorOptions over_use_detector_options_;
133   SimulatedClock system_clock_;
134   ModuleRtpRtcpImpl* rtp_rtcp_impl_;
135   RTCPReceiver* rtcp_receiver_;
136   TestTransport* test_transport_;
137   RTCPHelp::RTCPPacketInformation rtcp_packet_info_;
138   MockRemoteBitrateObserver remote_bitrate_observer_;
139   scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
140 };
141
142
143 TEST_F(RtcpReceiverTest, BrokenPacketIsIgnored) {
144   const uint8_t bad_packet[] = {0, 0, 0, 0};
145   EXPECT_EQ(0, InjectRtcpPacket(bad_packet, sizeof(bad_packet)));
146   EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags);
147 }
148
149 TEST_F(RtcpReceiverTest, InjectSrPacket) {
150   const uint32_t kSenderSsrc = 0x10203;
151   rtcp::SenderReport sr;
152   sr.From(kSenderSsrc);
153   rtcp::RawPacket p = sr.Build();
154   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
155   // The parser will note the remote SSRC on a SR from other than his
156   // expected peer, but will not flag that he's gotten a packet.
157   EXPECT_EQ(kSenderSsrc, rtcp_packet_info_.remoteSSRC);
158   EXPECT_EQ(0U,
159             kRtcpSr & rtcp_packet_info_.rtcpPacketTypeFlags);
160 }
161
162 TEST_F(RtcpReceiverTest, XrPacketWithZeroReportBlocksIgnored) {
163   rtcp::Xr xr;
164   xr.From(0x2345);
165   rtcp::RawPacket p = xr.Build();
166   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
167   EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags);
168 }
169
170 TEST_F(RtcpReceiverTest, InjectXrVoipPacket) {
171   const uint32_t kSourceSsrc = 0x123456;
172   std::set<uint32_t> ssrcs;
173   ssrcs.insert(kSourceSsrc);
174   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
175
176   const uint8_t kLossRate = 123;
177   rtcp::VoipMetric voip_metric;
178   voip_metric.To(kSourceSsrc);
179   voip_metric.LossRate(kLossRate);
180   rtcp::Xr xr;
181   xr.From(0x2345);
182   xr.WithVoipMetric(&voip_metric);
183   rtcp::RawPacket p = xr.Build();
184   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
185   ASSERT_TRUE(rtcp_packet_info_.VoIPMetric != NULL);
186   EXPECT_EQ(kLossRate, rtcp_packet_info_.VoIPMetric->lossRate);
187   EXPECT_EQ(kRtcpXrVoipMetric, rtcp_packet_info_.rtcpPacketTypeFlags);
188 }
189
190 TEST_F(RtcpReceiverTest, XrVoipPacketNotToUsIgnored) {
191   const uint32_t kSourceSsrc = 0x123456;
192   std::set<uint32_t> ssrcs;
193   ssrcs.insert(kSourceSsrc);
194   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
195
196   rtcp::VoipMetric voip_metric;
197   voip_metric.To(kSourceSsrc + 1);
198   rtcp::Xr xr;
199   xr.From(0x2345);
200   xr.WithVoipMetric(&voip_metric);
201   rtcp::RawPacket p = xr.Build();
202   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
203   EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags);
204 }
205
206 TEST_F(RtcpReceiverTest, InjectXrReceiverReferenceTimePacket) {
207   rtcp::Rrtr rrtr;
208   rrtr.WithNtpSec(0x10203);
209   rrtr.WithNtpFrac(0x40506);
210   rtcp::Xr xr;
211   xr.From(0x2345);
212   xr.WithRrtr(&rrtr);
213
214   rtcp::RawPacket p = xr.Build();
215   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
216   EXPECT_EQ(kRtcpXrReceiverReferenceTime,
217             rtcp_packet_info_.rtcpPacketTypeFlags);
218 }
219
220 TEST_F(RtcpReceiverTest, XrDlrrPacketNotToUsIgnored) {
221   const uint32_t kSourceSsrc = 0x123456;
222   std::set<uint32_t> ssrcs;
223   ssrcs.insert(kSourceSsrc);
224   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
225
226   rtcp::Dlrr dlrr;
227   dlrr.WithDlrrItem(kSourceSsrc + 1, 0x12345, 0x67890);
228   rtcp::Xr xr;
229   xr.From(0x2345);
230   xr.WithDlrr(&dlrr);
231   rtcp::RawPacket p = xr.Build();
232   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
233   EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags);
234   EXPECT_FALSE(rtcp_packet_info_.xr_dlrr_item);
235 }
236
237 TEST_F(RtcpReceiverTest, InjectXrDlrrPacketWithSubBlock) {
238   const uint32_t kSourceSsrc = 0x123456;
239   std::set<uint32_t> ssrcs;
240   ssrcs.insert(kSourceSsrc);
241   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
242
243   rtcp::Dlrr dlrr;
244   dlrr.WithDlrrItem(kSourceSsrc, 0x12345, 0x67890);
245   rtcp::Xr xr;
246   xr.From(0x2345);
247   xr.WithDlrr(&dlrr);
248   rtcp::RawPacket p = xr.Build();
249   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
250   // The parser should note the DLRR report block item, but not flag the packet
251   // since the RTT is not estimated.
252   EXPECT_TRUE(rtcp_packet_info_.xr_dlrr_item);
253 }
254
255 TEST_F(RtcpReceiverTest, InjectXrDlrrPacketWithMultipleSubBlocks) {
256   const uint32_t kSourceSsrc = 0x123456;
257   std::set<uint32_t> ssrcs;
258   ssrcs.insert(kSourceSsrc);
259   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
260
261   rtcp::Dlrr dlrr;
262   dlrr.WithDlrrItem(kSourceSsrc + 1, 0x12345, 0x67890);
263   dlrr.WithDlrrItem(kSourceSsrc + 2, 0x12345, 0x67890);
264   dlrr.WithDlrrItem(kSourceSsrc,     0x12345, 0x67890);
265   rtcp::Xr xr;
266   xr.From(0x2345);
267   xr.WithDlrr(&dlrr);
268   rtcp::RawPacket p = xr.Build();
269   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
270   // The parser should note the DLRR report block item, but not flag the packet
271   // since the RTT is not estimated.
272   EXPECT_TRUE(rtcp_packet_info_.xr_dlrr_item);
273 }
274
275 TEST_F(RtcpReceiverTest, InjectXrPacketWithMultipleReportBlocks) {
276   const uint32_t kSourceSsrc = 0x123456;
277   std::set<uint32_t> ssrcs;
278   ssrcs.insert(kSourceSsrc);
279   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
280
281   rtcp::Rrtr rrtr;
282   rtcp::Dlrr dlrr;
283   dlrr.WithDlrrItem(kSourceSsrc, 0x12345, 0x67890);
284   rtcp::VoipMetric metric;
285   metric.To(kSourceSsrc);
286   rtcp::Xr xr;
287   xr.From(0x2345);
288   xr.WithRrtr(&rrtr);
289   xr.WithDlrr(&dlrr);
290   xr.WithVoipMetric(&metric);
291   rtcp::RawPacket p = xr.Build();
292   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
293   EXPECT_EQ(static_cast<unsigned int>(kRtcpXrReceiverReferenceTime +
294                                       kRtcpXrVoipMetric),
295             rtcp_packet_info_.rtcpPacketTypeFlags);
296   // The parser should note the DLRR report block item, but not flag the packet
297   // since the RTT is not estimated.
298   EXPECT_TRUE(rtcp_packet_info_.xr_dlrr_item);
299 }
300
301 TEST_F(RtcpReceiverTest, InjectXrPacketWithUnknownReportBlock) {
302   const uint32_t kSourceSsrc = 0x123456;
303   std::set<uint32_t> ssrcs;
304   ssrcs.insert(kSourceSsrc);
305   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
306   std::vector<uint32_t> remote_ssrcs;
307   remote_ssrcs.push_back(kSourceSsrc);
308
309   rtcp::Rrtr rrtr;
310   rtcp::Dlrr dlrr;
311   dlrr.WithDlrrItem(kSourceSsrc, 0x12345, 0x67890);
312   rtcp::VoipMetric metric;
313   metric.To(kSourceSsrc);
314   rtcp::Xr xr;
315   xr.From(0x2345);
316   xr.WithRrtr(&rrtr);
317   xr.WithDlrr(&dlrr);
318   xr.WithVoipMetric(&metric);
319   rtcp::RawPacket p = xr.Build();
320   // Modify the DLRR block to have an unsupported block type, from 5 to 6.
321   uint8_t* buffer = const_cast<uint8_t*>(p.buffer());
322   EXPECT_EQ(5, buffer[20]);
323   buffer[20] = 6;
324
325   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
326   EXPECT_EQ(static_cast<unsigned int>(kRtcpXrReceiverReferenceTime +
327                                       kRtcpXrVoipMetric),
328             rtcp_packet_info_.rtcpPacketTypeFlags);
329   EXPECT_FALSE(rtcp_packet_info_.xr_dlrr_item);
330 }
331
332 TEST(RtcpUtilityTest, MidNtp) {
333   const uint32_t kNtpSec = 0x12345678;
334   const uint32_t kNtpFrac = 0x23456789;
335   const uint32_t kNtpMid = 0x56782345;
336   EXPECT_EQ(kNtpMid, RTCPUtility::MidNtp(kNtpSec, kNtpFrac));
337 }
338
339 TEST_F(RtcpReceiverTest, TestXrRrRttInitiallyFalse) {
340   uint16_t rtt_ms;
341   EXPECT_FALSE(rtcp_receiver_->GetAndResetXrRrRtt(&rtt_ms));
342 }
343
344 TEST_F(RtcpReceiverTest, LastReceivedXrReferenceTimeInfoInitiallyFalse) {
345   RtcpReceiveTimeInfo info;
346   EXPECT_FALSE(rtcp_receiver_->LastReceivedXrReferenceTimeInfo(&info));
347 }
348
349 TEST_F(RtcpReceiverTest, GetLastReceivedXrReferenceTimeInfo) {
350   const uint32_t kSenderSsrc = 0x123456;
351   const uint32_t kNtpSec = 0x10203;
352   const uint32_t kNtpFrac = 0x40506;
353   const uint32_t kNtpMid = RTCPUtility::MidNtp(kNtpSec, kNtpFrac);
354
355   rtcp::Rrtr rrtr;
356   rrtr.WithNtpSec(kNtpSec);
357   rrtr.WithNtpFrac(kNtpFrac);
358   rtcp::Xr xr;
359   xr.From(kSenderSsrc);
360   xr.WithRrtr(&rrtr);
361   rtcp::RawPacket p = xr.Build();
362   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
363   EXPECT_EQ(kRtcpXrReceiverReferenceTime,
364       rtcp_packet_info_.rtcpPacketTypeFlags);
365
366   RtcpReceiveTimeInfo info;
367   EXPECT_TRUE(rtcp_receiver_->LastReceivedXrReferenceTimeInfo(&info));
368   EXPECT_EQ(kSenderSsrc, info.sourceSSRC);
369   EXPECT_EQ(kNtpMid, info.lastRR);
370   EXPECT_EQ(0U, info.delaySinceLastRR);
371
372   system_clock_.AdvanceTimeMilliseconds(1000);
373   EXPECT_TRUE(rtcp_receiver_->LastReceivedXrReferenceTimeInfo(&info));
374   EXPECT_EQ(65536U, info.delaySinceLastRR);
375 }
376
377 TEST_F(RtcpReceiverTest, ReceiveReportTimeout) {
378   const uint32_t kSenderSsrc = 0x10203;
379   const uint32_t kSourceSsrc = 0x40506;
380   const int64_t kRtcpIntervalMs = 1000;
381
382   std::set<uint32_t> ssrcs;
383   ssrcs.insert(kSourceSsrc);
384   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
385
386   const uint16_t kSequenceNumber = 1234;
387   system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs);
388
389   // No RR received, shouldn't trigger a timeout.
390   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
391   EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
392
393   // Add a RR and advance the clock just enough to not trigger a timeout.
394   rtcp::ReportBlock rb1;
395   rb1.To(kSourceSsrc);
396   rb1.WithExtHighestSeqNum(kSequenceNumber);
397   rtcp::ReceiverReport rr1;
398   rr1.From(kSenderSsrc);
399   rr1.WithReportBlock(&rb1);
400   rtcp::RawPacket p1 = rr1.Build();
401   EXPECT_EQ(0, InjectRtcpPacket(p1.buffer(), p1.buffer_length()));
402   system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs - 1);
403   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
404   EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
405
406   // Add a RR with the same extended max as the previous RR to trigger a
407   // sequence number timeout, but not a RR timeout.
408   EXPECT_EQ(0, InjectRtcpPacket(p1.buffer(), p1.buffer_length()));
409   system_clock_.AdvanceTimeMilliseconds(2);
410   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
411   EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
412
413   // Advance clock enough to trigger an RR timeout too.
414   system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs);
415   EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
416
417   // We should only get one timeout even though we still haven't received a new
418   // RR.
419   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
420   EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
421
422   // Add a new RR with increase sequence number to reset timers.
423   rtcp::ReportBlock rb2;
424   rb2.To(kSourceSsrc);
425   rb2.WithExtHighestSeqNum(kSequenceNumber + 1);
426   rtcp::ReceiverReport rr2;
427   rr2.From(kSenderSsrc);
428   rr2.WithReportBlock(&rb2);
429   rtcp::RawPacket p2 = rr2.Build();
430   EXPECT_EQ(0, InjectRtcpPacket(p2.buffer(), p2.buffer_length()));
431   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
432   EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
433
434   // Verify we can get a timeout again once we've received new RR.
435   system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs);
436   EXPECT_EQ(0, InjectRtcpPacket(p2.buffer(), p2.buffer_length()));
437   system_clock_.AdvanceTimeMilliseconds(kRtcpIntervalMs + 1);
438   EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
439   EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
440   system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs);
441   EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
442 }
443
444 TEST_F(RtcpReceiverTest, TmmbrReceivedWithNoIncomingPacket) {
445   // This call is expected to fail because no data has arrived.
446   EXPECT_EQ(-1, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
447 }
448
449 TEST_F(RtcpReceiverTest, TwoReportBlocks) {
450   const uint32_t kSenderSsrc = 0x10203;
451   const uint32_t kSourceSsrcs[] = {0x40506, 0x50607};
452   const uint16_t kSequenceNumbers[] = {10, 12423};
453   const int kNumSsrcs = sizeof(kSourceSsrcs) / sizeof(kSourceSsrcs[0]);
454
455   std::set<uint32_t> ssrcs(kSourceSsrcs, kSourceSsrcs + kNumSsrcs);
456   rtcp_receiver_->SetSsrcs(kSourceSsrcs[0], ssrcs);
457
458   rtcp::ReportBlock rb1;
459   rb1.To(kSourceSsrcs[0]);
460   rb1.WithExtHighestSeqNum(kSequenceNumbers[0]);
461   rb1.WithFractionLost(10);
462   rb1.WithCumulativeLost(5);
463
464   rtcp::ReportBlock rb2;
465   rb2.To(kSourceSsrcs[1]);
466   rb2.WithExtHighestSeqNum(kSequenceNumbers[1]);
467
468   rtcp::ReceiverReport rr1;
469   rr1.From(kSenderSsrc);
470   rr1.WithReportBlock(&rb1);
471   rr1.WithReportBlock(&rb2);
472
473   rtcp::RawPacket p1 = rr1.Build();
474   EXPECT_EQ(0, InjectRtcpPacket(p1.buffer(), p1.buffer_length()));
475   ASSERT_EQ(2u, rtcp_packet_info_.report_blocks.size());
476   EXPECT_EQ(10, rtcp_packet_info_.report_blocks.front().fractionLost);
477   EXPECT_EQ(0, rtcp_packet_info_.report_blocks.back().fractionLost);
478
479   rtcp::ReportBlock rb3;
480   rb3.To(kSourceSsrcs[0]);
481   rb3.WithExtHighestSeqNum(kSequenceNumbers[0]);
482
483   rtcp::ReportBlock rb4;
484   rb4.To(kSourceSsrcs[1]);
485   rb4.WithExtHighestSeqNum(kSequenceNumbers[1]);
486   rb4.WithFractionLost(20);
487   rb4.WithCumulativeLost(10);
488
489   rtcp::ReceiverReport rr2;
490   rr2.From(kSenderSsrc);
491   rr2.WithReportBlock(&rb3);
492   rr2.WithReportBlock(&rb4);
493
494   rtcp::RawPacket p2 = rr2.Build();
495   EXPECT_EQ(0, InjectRtcpPacket(p2.buffer(), p2.buffer_length()));
496   ASSERT_EQ(2u, rtcp_packet_info_.report_blocks.size());
497   EXPECT_EQ(0, rtcp_packet_info_.report_blocks.front().fractionLost);
498   EXPECT_EQ(20, rtcp_packet_info_.report_blocks.back().fractionLost);
499 }
500
501 TEST_F(RtcpReceiverTest, TmmbrPacketAccepted) {
502   const uint32_t kMediaFlowSsrc = 0x2040608;
503   const uint32_t kSenderSsrc = 0x10203;
504   std::set<uint32_t> ssrcs;
505   ssrcs.insert(kMediaFlowSsrc);  // Matches "media source" above.
506   rtcp_receiver_->SetSsrcs(kMediaFlowSsrc, ssrcs);
507
508   rtcp::Tmmbr tmmbr;
509   tmmbr.From(kSenderSsrc);
510   tmmbr.To(kMediaFlowSsrc);
511   tmmbr.WithBitrateKbps(30);
512
513   rtcp::SenderReport sr;
514   sr.From(kSenderSsrc);
515   sr.Append(&tmmbr);
516   rtcp::RawPacket p = sr.Build();
517   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
518
519   EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
520   TMMBRSet candidate_set;
521   candidate_set.VerifyAndAllocateSet(1);
522   EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(1, 0, &candidate_set));
523   EXPECT_LT(0U, candidate_set.Tmmbr(0));
524   EXPECT_EQ(kSenderSsrc, candidate_set.Ssrc(0));
525 }
526
527 TEST_F(RtcpReceiverTest, TmmbrPacketNotForUsIgnored) {
528   const uint32_t kMediaFlowSsrc = 0x2040608;
529   const uint32_t kSenderSsrc = 0x10203;
530
531   rtcp::Tmmbr tmmbr;
532   tmmbr.From(kSenderSsrc);
533   tmmbr.To(kMediaFlowSsrc + 1);  // This SSRC is not what we are sending.
534   tmmbr.WithBitrateKbps(30);
535
536   rtcp::SenderReport sr;
537   sr.From(kSenderSsrc);
538   sr.Append(&tmmbr);
539   rtcp::RawPacket p = sr.Build();
540
541   std::set<uint32_t> ssrcs;
542   ssrcs.insert(kMediaFlowSsrc);
543   rtcp_receiver_->SetSsrcs(kMediaFlowSsrc, ssrcs);
544   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
545   EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
546 }
547
548 TEST_F(RtcpReceiverTest, TmmbrPacketZeroRateIgnored) {
549   const uint32_t kMediaFlowSsrc = 0x2040608;
550   const uint32_t kSenderSsrc = 0x10203;
551   std::set<uint32_t> ssrcs;
552   ssrcs.insert(kMediaFlowSsrc);  // Matches "media source" above.
553   rtcp_receiver_->SetSsrcs(kMediaFlowSsrc, ssrcs);
554
555   rtcp::Tmmbr tmmbr;
556   tmmbr.From(kSenderSsrc);
557   tmmbr.To(kMediaFlowSsrc);
558   tmmbr.WithBitrateKbps(0);
559
560   rtcp::SenderReport sr;
561   sr.From(kSenderSsrc);
562   sr.Append(&tmmbr);
563   rtcp::RawPacket p = sr.Build();
564
565   EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
566   EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
567 }
568
569 TEST_F(RtcpReceiverTest, TmmbrThreeConstraintsTimeOut) {
570   const uint32_t kMediaFlowSsrc = 0x2040608;
571   const uint32_t kSenderSsrc = 0x10203;
572   std::set<uint32_t> ssrcs;
573   ssrcs.insert(kMediaFlowSsrc);  // Matches "media source" above.
574   rtcp_receiver_->SetSsrcs(kMediaFlowSsrc, ssrcs);
575
576   // Inject 3 packets "from" kSenderSsrc, kSenderSsrc+1, kSenderSsrc+2.
577   // The times of arrival are starttime + 0, starttime + 5 and starttime + 10.
578   for (uint32_t ssrc = kSenderSsrc; ssrc < kSenderSsrc + 3; ++ssrc) {
579     rtcp::Tmmbr tmmbr;
580     tmmbr.From(ssrc);
581     tmmbr.To(kMediaFlowSsrc);
582     tmmbr.WithBitrateKbps(30);
583
584     rtcp::SenderReport sr;
585     sr.From(ssrc);
586     sr.Append(&tmmbr);
587     rtcp::RawPacket p = sr.Build();
588     EXPECT_EQ(0, InjectRtcpPacket(p.buffer(), p.buffer_length()));
589     // 5 seconds between each packet.
590     system_clock_.AdvanceTimeMilliseconds(5000);
591   }
592   // It is now starttime + 15.
593   EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
594   TMMBRSet candidate_set;
595   candidate_set.VerifyAndAllocateSet(3);
596   EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set));
597   EXPECT_LT(0U, candidate_set.Tmmbr(0));
598   // We expect the timeout to be 25 seconds. Advance the clock by 12
599   // seconds, timing out the first packet.
600   system_clock_.AdvanceTimeMilliseconds(12000);
601   // Odd behaviour: Just counting them does not trigger the timeout.
602   EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
603   EXPECT_EQ(2, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set));
604   EXPECT_EQ(kSenderSsrc + 1, candidate_set.Ssrc(0));
605 }
606
607 TEST_F(RtcpReceiverTest, Callbacks) {
608   class RtcpCallbackImpl : public RtcpStatisticsCallback {
609    public:
610     RtcpCallbackImpl() : RtcpStatisticsCallback(), ssrc_(0) {}
611     virtual ~RtcpCallbackImpl() {}
612
613     virtual void StatisticsUpdated(const RtcpStatistics& statistics,
614                                    uint32_t ssrc) {
615       stats_ = statistics;
616       ssrc_ = ssrc;
617     }
618
619     bool Matches(uint32_t ssrc, uint32_t extended_max, uint8_t fraction_loss,
620                  uint32_t cumulative_loss, uint32_t jitter) {
621       return ssrc_ == ssrc &&
622           stats_.fraction_lost == fraction_loss &&
623           stats_.cumulative_lost == cumulative_loss &&
624           stats_.extended_max_sequence_number == extended_max &&
625           stats_.jitter == jitter;
626     }
627
628     RtcpStatistics stats_;
629     uint32_t ssrc_;
630   } callback;
631
632   rtcp_receiver_->RegisterRtcpStatisticsCallback(&callback);
633
634   const uint32_t kSenderSsrc = 0x10203;
635   const uint32_t kSourceSsrc = 0x123456;
636   const uint8_t kFractionLoss = 3;
637   const uint32_t kCumulativeLoss = 7;
638   const uint32_t kJitter = 9;
639   const uint16_t kSequenceNumber = 1234;
640
641   std::set<uint32_t> ssrcs;
642   ssrcs.insert(kSourceSsrc);
643   rtcp_receiver_->SetSsrcs(kSourceSsrc, ssrcs);
644
645   // First packet, all numbers should just propagate.
646   rtcp::ReportBlock rb1;
647   rb1.To(kSourceSsrc);
648   rb1.WithExtHighestSeqNum(kSequenceNumber);
649   rb1.WithFractionLost(kFractionLoss);
650   rb1.WithCumulativeLost(kCumulativeLoss);
651   rb1.WithJitter(kJitter);
652
653   rtcp::ReceiverReport rr1;
654   rr1.From(kSenderSsrc);
655   rr1.WithReportBlock(&rb1);
656   rtcp::RawPacket p1 = rr1.Build();
657   EXPECT_EQ(0, InjectRtcpPacket(p1.buffer(), p1.buffer_length()));
658   EXPECT_TRUE(callback.Matches(kSourceSsrc, kSequenceNumber, kFractionLoss,
659                                kCumulativeLoss, kJitter));
660
661   rtcp_receiver_->RegisterRtcpStatisticsCallback(NULL);
662
663   // Add arbitrary numbers, callback should not be called (retain old values).
664   rtcp::ReportBlock rb2;
665   rb2.To(kSourceSsrc);
666   rb2.WithExtHighestSeqNum(kSequenceNumber + 1);
667   rb2.WithFractionLost(42);
668   rb2.WithCumulativeLost(137);
669   rb2.WithJitter(4711);
670
671   rtcp::ReceiverReport rr2;
672   rr2.From(kSenderSsrc);
673   rr2.WithReportBlock(&rb2);
674   rtcp::RawPacket p2 = rr2.Build();
675   EXPECT_EQ(0, InjectRtcpPacket(p2.buffer(), p2.buffer_length()));
676   EXPECT_TRUE(callback.Matches(kSourceSsrc, kSequenceNumber, kFractionLoss,
677                                kCumulativeLoss, kJitter));
678 }
679
680 }  // Anonymous namespace
681
682 }  // namespace webrtc