rtcp_receiver_unittest.cc revision aa4d96a134a03f998d52fb9699845d9c644eb24b
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_receiver.h"
23#include "webrtc/modules/rtp_rtcp/source/rtcp_sender.h"
24#include "webrtc/modules/rtp_rtcp/source/rtp_rtcp_impl.h"
25#include "webrtc/modules/rtp_rtcp/source/rtp_utility.h"
26
27namespace webrtc {
28
29namespace {  // Anonymous namespace; hide utility functions and classes.
30
31// A very simple packet builder class for building RTCP packets.
32class PacketBuilder {
33 public:
34  static const int kMaxPacketSize = 1024;
35
36  PacketBuilder()
37      : pos_(0),
38        pos_of_len_(0) {
39  }
40
41
42  void Add8(uint8_t byte) {
43    EXPECT_LT(pos_, kMaxPacketSize - 1);
44    buffer_[pos_] = byte;
45    ++ pos_;
46  }
47
48  void Add16(uint16_t word) {
49    Add8(word >> 8);
50    Add8(word & 0xFF);
51  }
52
53  void Add32(uint32_t word) {
54    Add8(word >> 24);
55    Add8((word >> 16) & 0xFF);
56    Add8((word >> 8) & 0xFF);
57    Add8(word & 0xFF);
58  }
59
60  void Add64(uint32_t upper_half, uint32_t lower_half) {
61    Add32(upper_half);
62    Add32(lower_half);
63  }
64
65  // Set the 5-bit value in the 1st byte of the header
66  // and the payload type. Set aside room for the length field,
67  // and make provision for backpatching it.
68  // Note: No way to set the padding bit.
69  void AddRtcpHeader(int payload, int format_or_count) {
70    PatchLengthField();
71    Add8(0x80 | (format_or_count & 0x1F));
72    Add8(payload);
73    pos_of_len_ = pos_;
74    Add16(0xDEAD);  // Initialize length to "clearly illegal".
75  }
76
77  void AddTmmbrBandwidth(int mantissa, int exponent, int overhead) {
78    // 6 bits exponent, 17 bits mantissa, 9 bits overhead.
79    uint32_t word = 0;
80    word |= (exponent << 26);
81    word |= ((mantissa & 0x1FFFF) << 9);
82    word |= (overhead & 0x1FF);
83    Add32(word);
84  }
85
86  void AddSrPacket(uint32_t sender_ssrc) {
87    AddRtcpHeader(200, 0);
88    Add32(sender_ssrc);
89    Add64(0x10203, 0x4050607);  // NTP timestamp
90    Add32(0x10203);  // RTP timestamp
91    Add32(0);  // Sender's packet count
92    Add32(0);  // Sender's octet count
93  }
94
95  void AddRrPacket(uint32_t sender_ssrc, uint32_t rtp_ssrc,
96                   uint32_t extended_max) {
97    AddRtcpHeader(201, 1);
98    Add32(sender_ssrc);
99    Add32(rtp_ssrc);
100    Add32(0);  // No loss.
101    Add32(extended_max);
102    Add32(0);  // Jitter.
103    Add32(0);  // Last SR.
104    Add32(0);  // Delay since last SR.
105  }
106
107  const uint8_t* packet() {
108    PatchLengthField();
109    return buffer_;
110  }
111
112  unsigned int length() {
113    return pos_;
114  }
115 private:
116  void PatchLengthField() {
117    if (pos_of_len_ > 0) {
118      // Backpatch the packet length. The client must have taken
119      // care of proper padding to 32-bit words.
120      int this_packet_length = (pos_ - pos_of_len_ - 2);
121      ASSERT_EQ(0, this_packet_length % 4)
122          << "Packets must be a multiple of 32 bits long"
123          << " pos " << pos_ << " pos_of_len " << pos_of_len_;
124      buffer_[pos_of_len_] = this_packet_length >> 10;
125      buffer_[pos_of_len_+1] = (this_packet_length >> 2) & 0xFF;
126      pos_of_len_ = 0;
127    }
128  }
129
130  int pos_;
131  // Where the length field of the current packet is.
132  // Note that 0 is not a legal value, so is used for "uninitialized".
133  int pos_of_len_;
134  uint8_t buffer_[kMaxPacketSize];
135};
136
137// This test transport verifies that no functions get called.
138class TestTransport : public Transport,
139                      public RtpData {
140 public:
141  explicit TestTransport()
142      : rtcp_receiver_(NULL) {
143  }
144  void SetRTCPReceiver(RTCPReceiver* rtcp_receiver) {
145    rtcp_receiver_ = rtcp_receiver;
146  }
147  virtual int SendPacket(int /*ch*/, const void* /*data*/, int /*len*/) {
148    ADD_FAILURE();  // FAIL() gives a compile error.
149    return -1;
150  }
151
152  // Injects an RTCP packet into the receiver.
153  virtual int SendRTCPPacket(int /* ch */, const void *packet, int packet_len) {
154    ADD_FAILURE();
155    return 0;
156  }
157
158  virtual int OnReceivedPayloadData(const uint8_t* payloadData,
159                                    const uint16_t payloadSize,
160                                    const WebRtcRTPHeader* rtpHeader) {
161    ADD_FAILURE();
162    return 0;
163  }
164  RTCPReceiver* rtcp_receiver_;
165};
166
167class RtcpReceiverTest : public ::testing::Test {
168 protected:
169  RtcpReceiverTest()
170      : over_use_detector_options_(),
171        system_clock_(1335900000),
172        remote_bitrate_observer_(),
173        remote_bitrate_estimator_(
174            RemoteBitrateEstimatorFactory().Create(
175                &remote_bitrate_observer_,
176                &system_clock_)) {
177    test_transport_ = new TestTransport();
178
179    RtpRtcp::Configuration configuration;
180    configuration.id = 0;
181    configuration.audio = false;
182    configuration.clock = &system_clock_;
183    configuration.outgoing_transport = test_transport_;
184    configuration.remote_bitrate_estimator = remote_bitrate_estimator_.get();
185    rtp_rtcp_impl_ = new ModuleRtpRtcpImpl(configuration);
186    rtcp_receiver_ = new RTCPReceiver(0, &system_clock_, rtp_rtcp_impl_);
187    test_transport_->SetRTCPReceiver(rtcp_receiver_);
188  }
189  ~RtcpReceiverTest() {
190    delete rtcp_receiver_;
191    delete rtp_rtcp_impl_;
192    delete test_transport_;
193  }
194
195  // Injects an RTCP packet into the receiver.
196  // Returns 0 for OK, non-0 for failure.
197  int InjectRtcpPacket(const uint8_t* packet,
198                        uint16_t packet_len) {
199    RTCPUtility::RTCPParserV2 rtcpParser(packet,
200                                         packet_len,
201                                         true);  // Allow non-compound RTCP
202
203    RTCPHelp::RTCPPacketInformation rtcpPacketInformation;
204    int result = rtcp_receiver_->IncomingRTCPPacket(rtcpPacketInformation,
205                                                    &rtcpParser);
206    // The NACK list is on purpose not copied below as it isn't needed by the
207    // test.
208    rtcp_packet_info_.rtcpPacketTypeFlags =
209        rtcpPacketInformation.rtcpPacketTypeFlags;
210    rtcp_packet_info_.remoteSSRC = rtcpPacketInformation.remoteSSRC;
211    rtcp_packet_info_.applicationSubType =
212        rtcpPacketInformation.applicationSubType;
213    rtcp_packet_info_.applicationName = rtcpPacketInformation.applicationName;
214    rtcp_packet_info_.reportBlock = rtcpPacketInformation.reportBlock;
215    rtcp_packet_info_.fractionLost = rtcpPacketInformation.fractionLost;
216    rtcp_packet_info_.roundTripTime = rtcpPacketInformation.roundTripTime;
217    rtcp_packet_info_.lastReceivedExtendedHighSeqNum =
218        rtcpPacketInformation.lastReceivedExtendedHighSeqNum;
219    rtcp_packet_info_.jitter = rtcpPacketInformation.jitter;
220    rtcp_packet_info_.interArrivalJitter =
221        rtcpPacketInformation.interArrivalJitter;
222    rtcp_packet_info_.sliPictureId = rtcpPacketInformation.sliPictureId;
223    rtcp_packet_info_.rpsiPictureId = rtcpPacketInformation.rpsiPictureId;
224    rtcp_packet_info_.receiverEstimatedMaxBitrate =
225        rtcpPacketInformation.receiverEstimatedMaxBitrate;
226    rtcp_packet_info_.ntp_secs = rtcpPacketInformation.ntp_secs;
227    rtcp_packet_info_.ntp_frac = rtcpPacketInformation.ntp_frac;
228    rtcp_packet_info_.rtp_timestamp = rtcpPacketInformation.rtp_timestamp;
229    return result;
230  }
231
232  OverUseDetectorOptions over_use_detector_options_;
233  SimulatedClock system_clock_;
234  ModuleRtpRtcpImpl* rtp_rtcp_impl_;
235  RTCPReceiver* rtcp_receiver_;
236  TestTransport* test_transport_;
237  RTCPHelp::RTCPPacketInformation rtcp_packet_info_;
238  MockRemoteBitrateObserver remote_bitrate_observer_;
239  scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_;
240};
241
242
243TEST_F(RtcpReceiverTest, BrokenPacketIsIgnored) {
244  const uint8_t bad_packet[] = {0, 0, 0, 0};
245  EXPECT_EQ(0, InjectRtcpPacket(bad_packet, sizeof(bad_packet)));
246  EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags);
247}
248
249TEST_F(RtcpReceiverTest, InjectSrPacket) {
250  const uint32_t kSenderSsrc = 0x10203;
251  PacketBuilder p;
252  p.AddSrPacket(kSenderSsrc);
253  EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length()));
254  // The parser will note the remote SSRC on a SR from other than his
255  // expected peer, but will not flag that he's gotten a packet.
256  EXPECT_EQ(kSenderSsrc, rtcp_packet_info_.remoteSSRC);
257  EXPECT_EQ(0U,
258            kRtcpSr & rtcp_packet_info_.rtcpPacketTypeFlags);
259}
260
261TEST_F(RtcpReceiverTest, ReceiveReportTimeout) {
262  const uint32_t kSenderSsrc = 0x10203;
263  const uint32_t kSourceSsrc = 0x40506;
264  const int64_t kRtcpIntervalMs = 1000;
265
266  rtcp_receiver_->SetSSRC(kSourceSsrc);
267
268  uint32_t sequence_number = 1234;
269  system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs);
270
271  // No RR received, shouldn't trigger a timeout.
272  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
273  EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
274
275  // Add a RR and advance the clock just enough to not trigger a timeout.
276  PacketBuilder p1;
277  p1.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number);
278  EXPECT_EQ(0, InjectRtcpPacket(p1.packet(), p1.length()));
279  system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs - 1);
280  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
281  EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
282
283  // Add a RR with the same extended max as the previous RR to trigger a
284  // sequence number timeout, but not a RR timeout.
285  PacketBuilder p2;
286  p2.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number);
287  EXPECT_EQ(0, InjectRtcpPacket(p2.packet(), p2.length()));
288  system_clock_.AdvanceTimeMilliseconds(2);
289  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
290  EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
291
292  // Advance clock enough to trigger an RR timeout too.
293  system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs);
294  EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
295
296  // We should only get one timeout even though we still haven't received a new
297  // RR.
298  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
299  EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
300
301  // Add a new RR with increase sequence number to reset timers.
302  PacketBuilder p3;
303  sequence_number++;
304  p2.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number);
305  EXPECT_EQ(0, InjectRtcpPacket(p2.packet(), p2.length()));
306  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
307  EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
308
309  // Verify we can get a timeout again once we've received new RR.
310  system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs);
311  PacketBuilder p4;
312  p4.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number);
313  EXPECT_EQ(0, InjectRtcpPacket(p4.packet(), p4.length()));
314  system_clock_.AdvanceTimeMilliseconds(kRtcpIntervalMs + 1);
315  EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
316  EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs));
317  system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs);
318  EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs));
319}
320
321TEST_F(RtcpReceiverTest, TmmbrReceivedWithNoIncomingPacket) {
322  // This call is expected to fail because no data has arrived.
323  EXPECT_EQ(-1, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
324}
325
326TEST_F(RtcpReceiverTest, TmmbrPacketAccepted) {
327  const uint32_t kMediaFlowSsrc = 0x2040608;
328  const uint32_t kSenderSsrc = 0x10203;
329  const uint32_t kMediaRecipientSsrc = 0x101;
330  rtcp_receiver_->SetSSRC(kMediaFlowSsrc);  // Matches "media source" above.
331
332  PacketBuilder p;
333  p.AddSrPacket(kSenderSsrc);
334  // TMMBR packet.
335  p.AddRtcpHeader(205, 3);
336  p.Add32(kSenderSsrc);
337  p.Add32(kMediaRecipientSsrc);
338  p.Add32(kMediaFlowSsrc);
339  p.AddTmmbrBandwidth(30000, 0, 0);  // 30 Kbits/sec bandwidth, no overhead.
340
341  EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length()));
342  EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
343  TMMBRSet candidate_set;
344  candidate_set.VerifyAndAllocateSet(1);
345  EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(1, 0, &candidate_set));
346  EXPECT_LT(0U, candidate_set.Tmmbr(0));
347  EXPECT_EQ(kMediaRecipientSsrc, candidate_set.Ssrc(0));
348}
349
350TEST_F(RtcpReceiverTest, TmmbrPacketNotForUsIgnored) {
351  const uint32_t kMediaFlowSsrc = 0x2040608;
352  const uint32_t kSenderSsrc = 0x10203;
353  const uint32_t kMediaRecipientSsrc = 0x101;
354  const uint32_t kOtherMediaFlowSsrc = 0x9999;
355
356  PacketBuilder p;
357  p.AddSrPacket(kSenderSsrc);
358  // TMMBR packet.
359  p.AddRtcpHeader(205, 3);
360  p.Add32(kSenderSsrc);
361  p.Add32(kMediaRecipientSsrc);
362  p.Add32(kOtherMediaFlowSsrc);  // This SSRC is not what we're sending.
363  p.AddTmmbrBandwidth(30000, 0, 0);
364
365  rtcp_receiver_->SetSSRC(kMediaFlowSsrc);
366  EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length()));
367  EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
368}
369
370TEST_F(RtcpReceiverTest, TmmbrPacketZeroRateIgnored) {
371  const uint32_t kMediaFlowSsrc = 0x2040608;
372  const uint32_t kSenderSsrc = 0x10203;
373  const uint32_t kMediaRecipientSsrc = 0x101;
374  rtcp_receiver_->SetSSRC(kMediaFlowSsrc);  // Matches "media source" above.
375
376  PacketBuilder p;
377  p.AddSrPacket(kSenderSsrc);
378  // TMMBR packet.
379  p.AddRtcpHeader(205, 3);
380  p.Add32(kSenderSsrc);
381  p.Add32(kMediaRecipientSsrc);
382  p.Add32(kMediaFlowSsrc);
383  p.AddTmmbrBandwidth(0, 0, 0);  // Rate zero.
384
385  EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length()));
386  EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
387}
388
389TEST_F(RtcpReceiverTest, TmmbrThreeConstraintsTimeOut) {
390  const uint32_t kMediaFlowSsrc = 0x2040608;
391  const uint32_t kSenderSsrc = 0x10203;
392  const uint32_t kMediaRecipientSsrc = 0x101;
393  rtcp_receiver_->SetSSRC(kMediaFlowSsrc);  // Matches "media source" above.
394
395  // Inject 3 packets "from" kMediaRecipientSsrc, Ssrc+1, Ssrc+2.
396  // The times of arrival are starttime + 0, starttime + 5 and starttime + 10.
397  for (uint32_t ssrc = kMediaRecipientSsrc;
398       ssrc < kMediaRecipientSsrc+3; ++ssrc) {
399    PacketBuilder p;
400    p.AddSrPacket(kSenderSsrc);
401    // TMMBR packet.
402    p.AddRtcpHeader(205, 3);
403    p.Add32(kSenderSsrc);
404    p.Add32(ssrc);
405    p.Add32(kMediaFlowSsrc);
406    p.AddTmmbrBandwidth(30000, 0, 0);  // 30 Kbits/sec bandwidth, no overhead.
407
408    EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length()));
409    // 5 seconds between each packet.
410    system_clock_.AdvanceTimeMilliseconds(5000);
411  }
412  // It is now starttime+15.
413  EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
414  TMMBRSet candidate_set;
415  candidate_set.VerifyAndAllocateSet(3);
416  EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set));
417  EXPECT_LT(0U, candidate_set.Tmmbr(0));
418  // We expect the timeout to be 25 seconds. Advance the clock by 12
419  // seconds, timing out the first packet.
420  system_clock_.AdvanceTimeMilliseconds(12000);
421  // Odd behaviour: Just counting them does not trigger the timeout.
422  EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL));
423  // Odd behaviour: There's only one left after timeout, not 2.
424  EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set));
425  EXPECT_EQ(kMediaRecipientSsrc + 2, candidate_set.Ssrc(0));
426}
427
428
429}  // Anonymous namespace
430
431}  // namespace webrtc
432