rtcp_receiver_unittest.cc revision 2f44673d665899ca788ae44247a9a7f4764f5e2b
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 <gmock/gmock.h> 16#include <gtest/gtest.h> 17 18// Note: This file has no directory. Lint warning must be ignored. 19#include "common_types.h" 20#include "modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h" 21#include "modules/remote_bitrate_estimator/include/mock/mock_remote_bitrate_observer.h" 22#include "modules/rtp_rtcp/source/rtp_utility.h" 23#include "modules/rtp_rtcp/source/rtcp_sender.h" 24#include "modules/rtp_rtcp/source/rtcp_receiver.h" 25#include "modules/rtp_rtcp/source/rtp_rtcp_impl.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 RemoteBitrateEstimator::Create( 175 over_use_detector_options_, 176 RemoteBitrateEstimator::kSingleStreamEstimation, 177 &remote_bitrate_observer_, 178 &system_clock_)) { 179 test_transport_ = new TestTransport(); 180 181 RtpRtcp::Configuration configuration; 182 configuration.id = 0; 183 configuration.audio = false; 184 configuration.clock = &system_clock_; 185 configuration.outgoing_transport = test_transport_; 186 configuration.remote_bitrate_estimator = remote_bitrate_estimator_.get(); 187 rtp_rtcp_impl_ = new ModuleRtpRtcpImpl(configuration); 188 rtcp_receiver_ = new RTCPReceiver(0, &system_clock_, rtp_rtcp_impl_); 189 test_transport_->SetRTCPReceiver(rtcp_receiver_); 190 } 191 ~RtcpReceiverTest() { 192 delete rtcp_receiver_; 193 delete rtp_rtcp_impl_; 194 delete test_transport_; 195 } 196 197 // Injects an RTCP packet into the receiver. 198 // Returns 0 for OK, non-0 for failure. 199 int InjectRtcpPacket(const uint8_t* packet, 200 uint16_t packet_len) { 201 RTCPUtility::RTCPParserV2 rtcpParser(packet, 202 packet_len, 203 true); // Allow non-compound RTCP 204 205 RTCPHelp::RTCPPacketInformation rtcpPacketInformation; 206 int result = rtcp_receiver_->IncomingRTCPPacket(rtcpPacketInformation, 207 &rtcpParser); 208 // The NACK list is on purpose not copied below as it isn't needed by the 209 // test. 210 rtcp_packet_info_.rtcpPacketTypeFlags = 211 rtcpPacketInformation.rtcpPacketTypeFlags; 212 rtcp_packet_info_.remoteSSRC = rtcpPacketInformation.remoteSSRC; 213 rtcp_packet_info_.applicationSubType = 214 rtcpPacketInformation.applicationSubType; 215 rtcp_packet_info_.applicationName = rtcpPacketInformation.applicationName; 216 rtcp_packet_info_.reportBlock = rtcpPacketInformation.reportBlock; 217 rtcp_packet_info_.fractionLost = rtcpPacketInformation.fractionLost; 218 rtcp_packet_info_.roundTripTime = rtcpPacketInformation.roundTripTime; 219 rtcp_packet_info_.lastReceivedExtendedHighSeqNum = 220 rtcpPacketInformation.lastReceivedExtendedHighSeqNum; 221 rtcp_packet_info_.jitter = rtcpPacketInformation.jitter; 222 rtcp_packet_info_.interArrivalJitter = 223 rtcpPacketInformation.interArrivalJitter; 224 rtcp_packet_info_.sliPictureId = rtcpPacketInformation.sliPictureId; 225 rtcp_packet_info_.rpsiPictureId = rtcpPacketInformation.rpsiPictureId; 226 rtcp_packet_info_.receiverEstimatedMaxBitrate = 227 rtcpPacketInformation.receiverEstimatedMaxBitrate; 228 rtcp_packet_info_.ntp_secs = rtcpPacketInformation.ntp_secs; 229 rtcp_packet_info_.ntp_frac = rtcpPacketInformation.ntp_frac; 230 rtcp_packet_info_.rtp_timestamp = rtcpPacketInformation.rtp_timestamp; 231 return result; 232 } 233 234 OverUseDetectorOptions over_use_detector_options_; 235 SimulatedClock system_clock_; 236 ModuleRtpRtcpImpl* rtp_rtcp_impl_; 237 RTCPReceiver* rtcp_receiver_; 238 TestTransport* test_transport_; 239 RTCPHelp::RTCPPacketInformation rtcp_packet_info_; 240 MockRemoteBitrateObserver remote_bitrate_observer_; 241 scoped_ptr<RemoteBitrateEstimator> remote_bitrate_estimator_; 242}; 243 244 245TEST_F(RtcpReceiverTest, BrokenPacketIsIgnored) { 246 const uint8_t bad_packet[] = {0, 0, 0, 0}; 247 EXPECT_EQ(0, InjectRtcpPacket(bad_packet, sizeof(bad_packet))); 248 EXPECT_EQ(0U, rtcp_packet_info_.rtcpPacketTypeFlags); 249} 250 251TEST_F(RtcpReceiverTest, InjectSrPacket) { 252 const uint32_t kSenderSsrc = 0x10203; 253 PacketBuilder p; 254 p.AddSrPacket(kSenderSsrc); 255 EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); 256 // The parser will note the remote SSRC on a SR from other than his 257 // expected peer, but will not flag that he's gotten a packet. 258 EXPECT_EQ(kSenderSsrc, rtcp_packet_info_.remoteSSRC); 259 EXPECT_EQ(0U, 260 kRtcpSr & rtcp_packet_info_.rtcpPacketTypeFlags); 261} 262 263TEST_F(RtcpReceiverTest, ReceiveReportTimeout) { 264 const uint32_t kSenderSsrc = 0x10203; 265 const uint32_t kSourceSsrc = 0x40506; 266 const int64_t kRtcpIntervalMs = 1000; 267 268 rtcp_receiver_->SetSSRC(kSourceSsrc); 269 270 uint32_t sequence_number = 1234; 271 system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs); 272 273 // No RR received, shouldn't trigger a timeout. 274 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 275 EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 276 277 // Add a RR and advance the clock just enough to not trigger a timeout. 278 PacketBuilder p1; 279 p1.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number); 280 EXPECT_EQ(0, InjectRtcpPacket(p1.packet(), p1.length())); 281 system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs - 1); 282 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 283 EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 284 285 // Add a RR with the same extended max as the previous RR to trigger a 286 // sequence number timeout, but not a RR timeout. 287 PacketBuilder p2; 288 p2.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number); 289 EXPECT_EQ(0, InjectRtcpPacket(p2.packet(), p2.length())); 290 system_clock_.AdvanceTimeMilliseconds(2); 291 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 292 EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 293 294 // Advance clock enough to trigger an RR timeout too. 295 system_clock_.AdvanceTimeMilliseconds(3 * kRtcpIntervalMs); 296 EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 297 298 // We should only get one timeout even though we still haven't received a new 299 // RR. 300 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 301 EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 302 303 // Add a new RR with increase sequence number to reset timers. 304 PacketBuilder p3; 305 sequence_number++; 306 p2.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number); 307 EXPECT_EQ(0, InjectRtcpPacket(p2.packet(), p2.length())); 308 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 309 EXPECT_FALSE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 310 311 // Verify we can get a timeout again once we've received new RR. 312 system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs); 313 PacketBuilder p4; 314 p4.AddRrPacket(kSenderSsrc, kSourceSsrc, sequence_number); 315 EXPECT_EQ(0, InjectRtcpPacket(p4.packet(), p4.length())); 316 system_clock_.AdvanceTimeMilliseconds(kRtcpIntervalMs + 1); 317 EXPECT_FALSE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 318 EXPECT_TRUE(rtcp_receiver_->RtcpRrSequenceNumberTimeout(kRtcpIntervalMs)); 319 system_clock_.AdvanceTimeMilliseconds(2 * kRtcpIntervalMs); 320 EXPECT_TRUE(rtcp_receiver_->RtcpRrTimeout(kRtcpIntervalMs)); 321} 322 323TEST_F(RtcpReceiverTest, TmmbrReceivedWithNoIncomingPacket) { 324 // This call is expected to fail because no data has arrived. 325 EXPECT_EQ(-1, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 326} 327 328TEST_F(RtcpReceiverTest, TmmbrPacketAccepted) { 329 const uint32_t kMediaFlowSsrc = 0x2040608; 330 const uint32_t kSenderSsrc = 0x10203; 331 const uint32_t kMediaRecipientSsrc = 0x101; 332 rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. 333 334 PacketBuilder p; 335 p.AddSrPacket(kSenderSsrc); 336 // TMMBR packet. 337 p.AddRtcpHeader(205, 3); 338 p.Add32(kSenderSsrc); 339 p.Add32(kMediaRecipientSsrc); 340 p.Add32(kMediaFlowSsrc); 341 p.AddTmmbrBandwidth(30000, 0, 0); // 30 Kbits/sec bandwidth, no overhead. 342 343 EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); 344 EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 345 TMMBRSet candidate_set; 346 candidate_set.VerifyAndAllocateSet(1); 347 EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(1, 0, &candidate_set)); 348 EXPECT_LT(0U, candidate_set.Tmmbr(0)); 349 EXPECT_EQ(kMediaRecipientSsrc, candidate_set.Ssrc(0)); 350} 351 352TEST_F(RtcpReceiverTest, TmmbrPacketNotForUsIgnored) { 353 const uint32_t kMediaFlowSsrc = 0x2040608; 354 const uint32_t kSenderSsrc = 0x10203; 355 const uint32_t kMediaRecipientSsrc = 0x101; 356 const uint32_t kOtherMediaFlowSsrc = 0x9999; 357 358 PacketBuilder p; 359 p.AddSrPacket(kSenderSsrc); 360 // TMMBR packet. 361 p.AddRtcpHeader(205, 3); 362 p.Add32(kSenderSsrc); 363 p.Add32(kMediaRecipientSsrc); 364 p.Add32(kOtherMediaFlowSsrc); // This SSRC is not what we're sending. 365 p.AddTmmbrBandwidth(30000, 0, 0); 366 367 rtcp_receiver_->SetSSRC(kMediaFlowSsrc); 368 EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); 369 EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 370} 371 372TEST_F(RtcpReceiverTest, TmmbrPacketZeroRateIgnored) { 373 const uint32_t kMediaFlowSsrc = 0x2040608; 374 const uint32_t kSenderSsrc = 0x10203; 375 const uint32_t kMediaRecipientSsrc = 0x101; 376 rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. 377 378 PacketBuilder p; 379 p.AddSrPacket(kSenderSsrc); 380 // TMMBR packet. 381 p.AddRtcpHeader(205, 3); 382 p.Add32(kSenderSsrc); 383 p.Add32(kMediaRecipientSsrc); 384 p.Add32(kMediaFlowSsrc); 385 p.AddTmmbrBandwidth(0, 0, 0); // Rate zero. 386 387 EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); 388 EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 389} 390 391TEST_F(RtcpReceiverTest, TmmbrThreeConstraintsTimeOut) { 392 const uint32_t kMediaFlowSsrc = 0x2040608; 393 const uint32_t kSenderSsrc = 0x10203; 394 const uint32_t kMediaRecipientSsrc = 0x101; 395 rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. 396 397 // Inject 3 packets "from" kMediaRecipientSsrc, Ssrc+1, Ssrc+2. 398 // The times of arrival are starttime + 0, starttime + 5 and starttime + 10. 399 for (uint32_t ssrc = kMediaRecipientSsrc; 400 ssrc < kMediaRecipientSsrc+3; ++ssrc) { 401 PacketBuilder p; 402 p.AddSrPacket(kSenderSsrc); 403 // TMMBR packet. 404 p.AddRtcpHeader(205, 3); 405 p.Add32(kSenderSsrc); 406 p.Add32(ssrc); 407 p.Add32(kMediaFlowSsrc); 408 p.AddTmmbrBandwidth(30000, 0, 0); // 30 Kbits/sec bandwidth, no overhead. 409 410 EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); 411 // 5 seconds between each packet. 412 system_clock_.AdvanceTimeMilliseconds(5000); 413 } 414 // It is now starttime+15. 415 EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 416 TMMBRSet candidate_set; 417 candidate_set.VerifyAndAllocateSet(3); 418 EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set)); 419 EXPECT_LT(0U, candidate_set.Tmmbr(0)); 420 // We expect the timeout to be 25 seconds. Advance the clock by 12 421 // seconds, timing out the first packet. 422 system_clock_.AdvanceTimeMilliseconds(12000); 423 // Odd behaviour: Just counting them does not trigger the timeout. 424 EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); 425 // Odd behaviour: There's only one left after timeout, not 2. 426 EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set)); 427 EXPECT_EQ(kMediaRecipientSsrc + 2, candidate_set.Ssrc(0)); 428} 429 430 431} // Anonymous namespace 432 433} // namespace webrtc 434