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 * This file includes unit tests for the RTPPacketHistory. 11 */ 12 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h" 16 #include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h" 17 #include "webrtc/system_wrappers/interface/clock.h" 18 #include "webrtc/typedefs.h" 19 20 namespace webrtc { 21 22 class RtpPacketHistoryTest : public ::testing::Test { 23 protected: 24 RtpPacketHistoryTest() 25 : fake_clock_(123456), 26 hist_(new RTPPacketHistory(&fake_clock_)) { 27 } 28 ~RtpPacketHistoryTest() { 29 delete hist_; 30 } 31 32 SimulatedClock fake_clock_; 33 RTPPacketHistory* hist_; 34 enum {kPayload = 127}; 35 enum {kSsrc = 12345678}; 36 enum {kSeqNum = 88}; 37 enum {kTimestamp = 127}; 38 enum {kMaxPacketLength = 1500}; 39 uint8_t packet_[kMaxPacketLength]; 40 uint8_t packet_out_[kMaxPacketLength]; 41 42 void CreateRtpPacket(uint16_t seq_num, uint32_t ssrc, uint8_t payload, 43 uint32_t timestamp, uint8_t* array, uint16_t* cur_pos) { 44 array[(*cur_pos)++] = 0x80; 45 array[(*cur_pos)++] = payload; 46 array[(*cur_pos)++] = seq_num >> 8; 47 array[(*cur_pos)++] = seq_num; 48 array[(*cur_pos)++] = timestamp >> 24; 49 array[(*cur_pos)++] = timestamp >> 16; 50 array[(*cur_pos)++] = timestamp >> 8; 51 array[(*cur_pos)++] = timestamp; 52 array[(*cur_pos)++] = ssrc >> 24; 53 array[(*cur_pos)++] = ssrc >> 16; 54 array[(*cur_pos)++] = ssrc >> 8; 55 array[(*cur_pos)++] = ssrc; 56 } 57 }; 58 59 TEST_F(RtpPacketHistoryTest, SetStoreStatus) { 60 EXPECT_FALSE(hist_->StorePackets()); 61 hist_->SetStorePacketsStatus(true, 10); 62 EXPECT_TRUE(hist_->StorePackets()); 63 hist_->SetStorePacketsStatus(false, 0); 64 EXPECT_FALSE(hist_->StorePackets()); 65 } 66 67 TEST_F(RtpPacketHistoryTest, NoStoreStatus) { 68 EXPECT_FALSE(hist_->StorePackets()); 69 uint16_t len = 0; 70 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 71 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 72 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 73 capture_time_ms, kAllowRetransmission)); 74 // Packet should not be stored. 75 len = kMaxPacketLength; 76 int64_t time; 77 EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_, &len, 78 &time)); 79 } 80 81 TEST_F(RtpPacketHistoryTest, DontStore) { 82 hist_->SetStorePacketsStatus(true, 10); 83 uint16_t len = 0; 84 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 85 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 86 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 87 capture_time_ms, kDontStore)); 88 89 // Packet should not be stored. 90 len = kMaxPacketLength; 91 int64_t time; 92 EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_, &len, 93 &time)); 94 } 95 96 TEST_F(RtpPacketHistoryTest, PutRtpPacket_TooLargePacketLength) { 97 hist_->SetStorePacketsStatus(true, 10); 98 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 99 EXPECT_EQ(-1, hist_->PutRTPPacket(packet_, 100 kMaxPacketLength + 1, 101 kMaxPacketLength, 102 capture_time_ms, 103 kAllowRetransmission)); 104 } 105 106 TEST_F(RtpPacketHistoryTest, GetRtpPacket_NotStored) { 107 hist_->SetStorePacketsStatus(true, 10); 108 uint16_t len = kMaxPacketLength; 109 int64_t time; 110 EXPECT_FALSE(hist_->GetPacketAndSetSendTime(0, 0, false, packet_, &len, 111 &time)); 112 } 113 114 TEST_F(RtpPacketHistoryTest, PutRtpPacket) { 115 hist_->SetStorePacketsStatus(true, 10); 116 uint16_t len = 0; 117 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 118 119 EXPECT_FALSE(hist_->HasRTPPacket(kSeqNum)); 120 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 121 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 122 capture_time_ms, kAllowRetransmission)); 123 EXPECT_TRUE(hist_->HasRTPPacket(kSeqNum)); 124 } 125 126 TEST_F(RtpPacketHistoryTest, GetRtpPacket) { 127 hist_->SetStorePacketsStatus(true, 10); 128 uint16_t len = 0; 129 int64_t capture_time_ms = 1; 130 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 131 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 132 capture_time_ms, kAllowRetransmission)); 133 134 uint16_t len_out = kMaxPacketLength; 135 int64_t time; 136 EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_, 137 &len_out, &time)); 138 EXPECT_EQ(len, len_out); 139 EXPECT_EQ(capture_time_ms, time); 140 for (int i = 0; i < len; i++) { 141 EXPECT_EQ(packet_[i], packet_out_[i]); 142 } 143 } 144 145 TEST_F(RtpPacketHistoryTest, NoCaptureTime) { 146 hist_->SetStorePacketsStatus(true, 10); 147 uint16_t len = 0; 148 fake_clock_.AdvanceTimeMilliseconds(1); 149 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 150 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 151 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 152 -1, kAllowRetransmission)); 153 154 uint16_t len_out = kMaxPacketLength; 155 int64_t time; 156 EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_, 157 &len_out, &time)); 158 EXPECT_EQ(len, len_out); 159 EXPECT_EQ(capture_time_ms, time); 160 for (int i = 0; i < len; i++) { 161 EXPECT_EQ(packet_[i], packet_out_[i]); 162 } 163 } 164 165 TEST_F(RtpPacketHistoryTest, DontRetransmit) { 166 hist_->SetStorePacketsStatus(true, 10); 167 uint16_t len = 0; 168 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 169 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 170 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 171 capture_time_ms, kDontRetransmit)); 172 173 uint16_t len_out = kMaxPacketLength; 174 int64_t time; 175 EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_, 176 &len_out, &time)); 177 EXPECT_EQ(len, len_out); 178 EXPECT_EQ(capture_time_ms, time); 179 } 180 181 TEST_F(RtpPacketHistoryTest, MinResendTime) { 182 hist_->SetStorePacketsStatus(true, 10); 183 uint16_t len = 0; 184 int64_t capture_time_ms = fake_clock_.TimeInMilliseconds(); 185 CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len); 186 EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, kMaxPacketLength, 187 capture_time_ms, kAllowRetransmission)); 188 189 int64_t time; 190 len = kMaxPacketLength; 191 EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 100, false, packet_, &len, 192 &time)); 193 fake_clock_.AdvanceTimeMilliseconds(100); 194 // Time has elapsed. 195 len = kMaxPacketLength; 196 EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 100, false, packet_, &len, 197 &time)); 198 EXPECT_GT(len, 0); 199 EXPECT_EQ(capture_time_ms, time); 200 201 // Time has not elapsed. Packet should be found, but no bytes copied. 202 len = kMaxPacketLength; 203 EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, 101, false, packet_, 204 &len, &time)); 205 } 206 } // namespace webrtc 207