From 404843e6e574ef53e0e2a81fa21f92e3c56564d8 Mon Sep 17 00:00:00 2001 From: "hta@webrtc.org" Date: Wed, 2 May 2012 09:56:45 +0000 Subject: [PATCH] Timeout tests for TMMBR Added a test that injects 3 packets and then times out. Rewrote the packet construction in test to use a builder format. This makes tests a lot more readable. Odd behaviour of timeout found; documented as test. BUG= TEST= Review URL: https://webrtc-codereview.appspot.com/553004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@2161 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../rtp_rtcp/source/rtcp_receiver_test.cc | 284 +++++++++++++----- 1 file changed, 216 insertions(+), 68 deletions(-) diff --git a/src/modules/rtp_rtcp/source/rtcp_receiver_test.cc b/src/modules/rtp_rtcp/source/rtcp_receiver_test.cc index e5660787d..ff9ed1a75 100644 --- a/src/modules/rtp_rtcp/source/rtcp_receiver_test.cc +++ b/src/modules/rtp_rtcp/source/rtcp_receiver_test.cc @@ -25,6 +25,128 @@ namespace webrtc { namespace { // Anonymous namespace; hide utility functions and classes. +// A very simple packet builder class for building RTCP packets. +class PacketBuilder { + public: + static const int kMaxPacketSize = 1024; + + PacketBuilder() + : pos_(0), + pos_of_len_(0) { + } + + + void Add8(WebRtc_UWord8 byte) { + EXPECT_LT(pos_, kMaxPacketSize - 1); + buffer_[pos_] = byte; + ++ pos_; + } + + void Add16(WebRtc_UWord16 word) { + Add8(word >> 8); + Add8(word & 0xFF); + } + + void Add32(WebRtc_UWord32 word) { + Add8(word >> 24); + Add8((word >> 16) & 0xFF); + Add8((word >> 8) & 0xFF); + Add8(word & 0xFF); + } + + void Add64(WebRtc_UWord32 upper_half, WebRtc_UWord32 lower_half) { + Add32(upper_half); + Add32(lower_half); + } + + // Set the 5-bit value in the 1st byte of the header + // and the payload type. Set aside room for the length field, + // and make provision for backpatching it. + // Note: No way to set the padding bit. + void AddRtcpHeader(int payload, int format_or_count) { + PatchLengthField(); + Add8(0x80 | (format_or_count & 0x1F)); + Add8(payload); + pos_of_len_ = pos_; + Add16(0xDEAD); // Initialize length to "clearly illegal". + } + + void AddTmmbrBandwidth(int mantissa, int exponent, int overhead) { + // 6 bits exponent, 17 bits mantissa, 9 bits overhead. + WebRtc_UWord32 word = 0; + word |= (exponent << 26); + word |= ((mantissa & 0x1FFFF) << 9); + word |= (overhead & 0x1FF); + Add32(word); + } + + void AddSrPacket(WebRtc_UWord32 sender_ssrc) { + AddRtcpHeader(200, 0); + Add32(sender_ssrc); + Add64(0x10203, 0x4050607); // NTP timestamp + Add32(0x10203); // RTP timestamp + Add32(0); // Sender's packet count + Add32(0); // Sender's octet count + } + + const WebRtc_UWord8* packet() { + PatchLengthField(); + return buffer_; + } + + unsigned int length() { + return pos_; + } + private: + void PatchLengthField() { + if (pos_of_len_ > 0) { + // Backpatch the packet length. The client must have taken + // care of proper padding to 32-bit words. + int this_packet_length = (pos_ - pos_of_len_ - 2); + ASSERT_EQ(0, this_packet_length % 4) + << "Packets must be a multiple of 32 bits long" + << " pos " << pos_ << " pos_of_len " << pos_of_len_; + buffer_[pos_of_len_] = this_packet_length >> 10; + buffer_[pos_of_len_+1] = (this_packet_length >> 2) & 0xFF; + pos_of_len_ = 0; + } + } + + int pos_; + // Where the length field of the current packet is. + // Note that 0 is not a legal value, so is used for "uninitialized". + int pos_of_len_; + WebRtc_UWord8 buffer_[kMaxPacketSize]; +}; + +// Fake system clock, controllable to the millisecond. +// The Epoch for this clock is Jan 1, 1970, as evidenced +// by the NTP calculation. +class FakeSystemClock : public RtpRtcpClock { + public: + FakeSystemClock() + : time_in_ms_(1335900000) {} // A nonzero, but fake, value. + + virtual WebRtc_UWord32 GetTimeInMS() { + return time_in_ms_; + } + + virtual void CurrentNTP(WebRtc_UWord32& secs, + WebRtc_UWord32& frac) { + secs = (time_in_ms_ / 1000) + ModuleRTPUtility::NTP_JAN_1970; + // NTP_FRAC is 2^32 - number of ticks per second in the NTP fraction. + frac = (WebRtc_UWord32)((time_in_ms_ % 1000) + * ModuleRTPUtility::NTP_FRAC / 1000); + } + + void AdvanceClock(int ms_to_advance) { + time_in_ms_ += ms_to_advance; + } + private: + WebRtc_UWord32 time_in_ms_; +}; + + // This test transport verifies that no functions get called. class TestTransport : public Transport, public RtpData { @@ -56,7 +178,8 @@ class TestTransport : public Transport, class RtcpReceiverTest : public ::testing::Test { protected: RtcpReceiverTest() { - system_clock_ = ModuleRTPUtility::GetSystemClock(); + // system_clock_ = ModuleRTPUtility::GetSystemClock(); + system_clock_ = new FakeSystemClock(); rtp_rtcp_impl_ = new ModuleRtpRtcpImpl(0, false, system_clock_); rtcp_receiver_ = new RTCPReceiver(0, system_clock_, rtp_rtcp_impl_); test_transport_ = new TestTransport(rtcp_receiver_); @@ -84,7 +207,7 @@ class RtcpReceiverTest : public ::testing::Test { return result; } - RtpRtcpClock* system_clock_; + FakeSystemClock* system_clock_; ModuleRtpRtcpImpl* rtp_rtcp_impl_; RTCPReceiver* rtcp_receiver_; TestTransport* test_transport_; @@ -99,19 +222,13 @@ TEST_F(RtcpReceiverTest, BrokenPacketIsIgnored) { } TEST_F(RtcpReceiverTest, InjectSrPacket) { - const WebRtc_UWord8 sr_packet[] = { - 0x81, 200, // Type 200, report count = 0 - 0, 6, // length - 0, 1, 2, 3, // SSRC of sender - 0, 1, 2, 3, 4, 5, 6, 7, // NTP timestamp - 0, 1, 2, 3, // RTP timestamp - 0, 0, 0, 0, // Sender's packet count - 0, 0, 0, 0 // Sender's octet count - }; - EXPECT_EQ(0, InjectRtcpPacket(sr_packet, sizeof(sr_packet))); + const WebRtc_UWord32 kSenderSsrc = 0x10203; + PacketBuilder p; + p.AddSrPacket(kSenderSsrc); + EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); // The parser will note the remote SSRC on a SR from other than his // expected peer, but will not flag that he's gotten a packet. - EXPECT_EQ(0x010203U, rtcp_packet_info_.remoteSSRC); + EXPECT_EQ(kSenderSsrc, rtcp_packet_info_.remoteSSRC); EXPECT_EQ(0U, kRtcpSr & rtcp_packet_info_.rtcpPacketTypeFlags); } @@ -122,76 +239,107 @@ TEST_F(RtcpReceiverTest, TmmbrReceivedWithNoIncomingPacket) { } TEST_F(RtcpReceiverTest, TmmbrPacketAccepted) { - const WebRtc_UWord8 tmmbr_packet[] = { - 0x81, 200, // Type 200 SR, report count = 0 - 0, 6, // length - 0, 1, 2, 3, // SSRC of sender - 0, 1, 2, 3, 4, 5, 6, 7, // NTP timestamp - 0, 1, 2, 3, // RTP timestamp - 0, 0, 0, 0, // Sender's packet count - 0, 0, 0, 0, // Sender's octet count - // TMMBR - 0x83, 205, // Type 205 RTPFB, FMT 3 TMMBR - 0, 4, // length - 0, 1, 2, 3, // SSRC of sender - 0, 0, 1, 1, // SSRC of media source - 2, 4, 6, 8, // SSRC we ask to rate-control. Must match "our" SSRC. - 0, 55, 0, 0 // MxTBR - }; - rtcp_receiver_->SetSSRC(0x2040608); // Matches "media source" above. - EXPECT_EQ(0, InjectRtcpPacket(tmmbr_packet, sizeof(tmmbr_packet))); + const WebRtc_UWord32 kMediaFlowSsrc = 0x2040608; + const WebRtc_UWord32 kSenderSsrc = 0x10203; + const WebRtc_UWord32 kMediaRecipientSsrc = 0x101; + rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. + + PacketBuilder p; + p.AddSrPacket(kSenderSsrc); + // TMMBR packet. + p.AddRtcpHeader(205, 3); + p.Add32(kSenderSsrc); + p.Add32(kMediaRecipientSsrc); + p.Add32(kMediaFlowSsrc); + p.AddTmmbrBandwidth(30000, 0, 0); // 30 Kbits/sec bandwidth, no overhead. + + EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); TMMBRSet candidate_set; candidate_set.VerifyAndAllocateSet(1); EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(1, 0, &candidate_set)); EXPECT_LT(0U, candidate_set.ptrTmmbrSet[0]); - EXPECT_EQ(0x101U, candidate_set.ptrSsrcSet[0]); + EXPECT_EQ(kMediaRecipientSsrc, candidate_set.ptrSsrcSet[0]); } TEST_F(RtcpReceiverTest, TmmbrPacketNotForUsIgnored) { - const WebRtc_UWord8 tmmbr_packet[] = { - 0x81, 200, // Type 200 SR, report count = 0 - 0, 6, // length - 0, 1, 2, 3, // SSRC of sender - 0, 1, 2, 3, 4, 5, 6, 7, // NTP timestamp - 0, 1, 2, 3, // RTP timestamp - 0, 0, 0, 0, // Sender's packet count - 0, 0, 0, 0, // Sender's octet count - // TMMBR - 0x83, 205, // Type 205 RTPFB, FMT 3 TMMBR - 0, 4, // length - 0, 1, 2, 3, // SSRC of sender - 0, 0, 1, 1, // SSRC of media source - 99, 99, 99, 99, // SSRC we ask to rate-control. Different from 0x2040608. - 0, 55, 0, 0 // MxTBR - }; - rtcp_receiver_->SetSSRC(0x2040608); // Matches "media source" above. - EXPECT_EQ(0, InjectRtcpPacket(tmmbr_packet, sizeof(tmmbr_packet))); + const WebRtc_UWord32 kMediaFlowSsrc = 0x2040608; + const WebRtc_UWord32 kSenderSsrc = 0x10203; + const WebRtc_UWord32 kMediaRecipientSsrc = 0x101; + const WebRtc_UWord32 kOtherMediaFlowSsrc = 0x9999; + + PacketBuilder p; + p.AddSrPacket(kSenderSsrc); + // TMMBR packet. + p.AddRtcpHeader(205, 3); + p.Add32(kSenderSsrc); + p.Add32(kMediaRecipientSsrc); + p.Add32(kOtherMediaFlowSsrc); // This SSRC is not what we're sending. + p.AddTmmbrBandwidth(30000, 0, 0); + + rtcp_receiver_->SetSSRC(kMediaFlowSsrc); + EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); } TEST_F(RtcpReceiverTest, TmmbrPacketZeroRateIgnored) { - const WebRtc_UWord8 tmmbr_packet[] = { - 0x81, 200, // Type 200 SR, report count = 0 - 0, 6, // length - 0, 1, 2, 3, // SSRC of sender - 0, 1, 2, 3, 4, 5, 6, 7, // NTP timestamp - 0, 1, 2, 3, // RTP timestamp - 0, 0, 0, 0, // Sender's packet count - 0, 0, 0, 0, // Sender's octet count - // TMMBR - 0x83, 205, // Type 205 RTPFB, FMT 3 TMMBR - 0, 4, // length - 0, 1, 2, 3, // SSRC of sender - 0, 0, 1, 1, // SSRC of media source - 2, 4, 6, 8, // SSRC we ask to rate-control. Must match "our" SSRC. - 0, 0, 0, 0 // MxTBR == zero - }; - rtcp_receiver_->SetSSRC(0x2040608); // Matches "media source" above. - EXPECT_EQ(0, InjectRtcpPacket(tmmbr_packet, sizeof(tmmbr_packet))); + const WebRtc_UWord32 kMediaFlowSsrc = 0x2040608; + const WebRtc_UWord32 kSenderSsrc = 0x10203; + const WebRtc_UWord32 kMediaRecipientSsrc = 0x101; + rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. + + PacketBuilder p; + p.AddSrPacket(kSenderSsrc); + // TMMBR packet. + p.AddRtcpHeader(205, 3); + p.Add32(kSenderSsrc); + p.Add32(kMediaRecipientSsrc); + p.Add32(kMediaFlowSsrc); + p.AddTmmbrBandwidth(0, 0, 0); // Rate zero. + + EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); EXPECT_EQ(0, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); } +TEST_F(RtcpReceiverTest, TmmbrThreeConstraintsTimeOut) { + const WebRtc_UWord32 kMediaFlowSsrc = 0x2040608; + const WebRtc_UWord32 kSenderSsrc = 0x10203; + const WebRtc_UWord32 kMediaRecipientSsrc = 0x101; + rtcp_receiver_->SetSSRC(kMediaFlowSsrc); // Matches "media source" above. + + // Inject 3 packets "from" kMediaRecipientSsrc, Ssrc+1, Ssrc+2. + // The times of arrival are starttime + 0, starttime + 5 and starttime + 10. + for (WebRtc_UWord32 ssrc = kMediaRecipientSsrc; + ssrc < kMediaRecipientSsrc+3; ++ssrc) { + PacketBuilder p; + p.AddSrPacket(kSenderSsrc); + // TMMBR packet. + p.AddRtcpHeader(205, 3); + p.Add32(kSenderSsrc); + p.Add32(ssrc); + p.Add32(kMediaFlowSsrc); + p.AddTmmbrBandwidth(30000, 0, 0); // 30 Kbits/sec bandwidth, no overhead. + + EXPECT_EQ(0, InjectRtcpPacket(p.packet(), p.length())); + system_clock_->AdvanceClock(5000); // 5 seconds between each packet. + } + // It is now starttime+15. + EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); + TMMBRSet candidate_set; + candidate_set.VerifyAndAllocateSet(3); + EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set)); + EXPECT_LT(0U, candidate_set.ptrTmmbrSet[0]); + // We expect the timeout to be 25 seconds. Advance the clock by 12 + // seconds, timing out the first packet. + system_clock_->AdvanceClock(12000); + // Odd behaviour: Just counting them does not trigger the timeout. + EXPECT_EQ(3, rtcp_receiver_->TMMBRReceived(0, 0, NULL)); + // Odd behaviour: There's only one left after timeout, not 2. + EXPECT_EQ(1, rtcp_receiver_->TMMBRReceived(3, 0, &candidate_set)); + EXPECT_EQ(kMediaRecipientSsrc + 2, candidate_set.ptrSsrcSet[0]); +} + + } // Anonymous namespace } // namespace webrtc