aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/rtp_rtcp/source/rtp_packet_history_unittest.cc
blob: 00a6ac7ed213f0aee24d4edb66660bbfeb4f89bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 *
 * This file includes unit tests for the RTPPacketHistory.
 */

#include "testing/gtest/include/gtest/gtest.h"

#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
#include "webrtc/modules/rtp_rtcp/source/rtp_packet_history.h"
#include "webrtc/system_wrappers/include/clock.h"
#include "webrtc/video_engine/vie_defines.h"
#include "webrtc/typedefs.h"

namespace webrtc {

class RtpPacketHistoryTest : public ::testing::Test {
 protected:
  RtpPacketHistoryTest()
     : fake_clock_(123456),
       hist_(new RTPPacketHistory(&fake_clock_)) {
  }
  ~RtpPacketHistoryTest() {
    delete hist_;
  }
  
  SimulatedClock fake_clock_;
  RTPPacketHistory* hist_;
  enum {kPayload = 127};
  enum {kSsrc = 12345678};
  enum {kSeqNum = 88};
  enum {kTimestamp = 127};
  enum {kMaxPacketLength = 1500};
  uint8_t packet_[kMaxPacketLength];
  uint8_t packet_out_[kMaxPacketLength];

  void CreateRtpPacket(uint16_t seq_num, uint32_t ssrc, uint8_t payload,
      uint32_t timestamp, uint8_t* array, size_t* cur_pos) {
    array[(*cur_pos)++] = 0x80;
    array[(*cur_pos)++] = payload;
    array[(*cur_pos)++] = seq_num >> 8;
    array[(*cur_pos)++] = seq_num;
    array[(*cur_pos)++] = timestamp >> 24;
    array[(*cur_pos)++] = timestamp >> 16;
    array[(*cur_pos)++] = timestamp >> 8;
    array[(*cur_pos)++] = timestamp;
    array[(*cur_pos)++] = ssrc >> 24;
    array[(*cur_pos)++] = ssrc >> 16;
    array[(*cur_pos)++] = ssrc >> 8;
    array[(*cur_pos)++] = ssrc;
  } 
};

TEST_F(RtpPacketHistoryTest, SetStoreStatus) {
  EXPECT_FALSE(hist_->StorePackets());
  hist_->SetStorePacketsStatus(true, 10);
  EXPECT_TRUE(hist_->StorePackets());
  hist_->SetStorePacketsStatus(false, 0);
  EXPECT_FALSE(hist_->StorePackets());
}

TEST_F(RtpPacketHistoryTest, NoStoreStatus) {
  EXPECT_FALSE(hist_->StorePackets());
  size_t len = 0;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                   kAllowRetransmission));
  // Packet should not be stored.
  len = kMaxPacketLength;
  int64_t time;
  EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_, &len,
                                              &time));
}

TEST_F(RtpPacketHistoryTest, PutRtpPacket_TooLargePacketLength) {
  hist_->SetStorePacketsStatus(true, 10);
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  EXPECT_EQ(-1, hist_->PutRTPPacket(packet_, kMaxPacketLength + 1,
                                    capture_time_ms, kAllowRetransmission));
}

TEST_F(RtpPacketHistoryTest, GetRtpPacket_NotStored) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len = kMaxPacketLength;
  int64_t time;
  EXPECT_FALSE(hist_->GetPacketAndSetSendTime(0, 0, false, packet_, &len,
                                              &time));
}

TEST_F(RtpPacketHistoryTest, PutRtpPacket) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);

  EXPECT_FALSE(hist_->HasRTPPacket(kSeqNum));
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                   kAllowRetransmission));
  EXPECT_TRUE(hist_->HasRTPPacket(kSeqNum));
}

TEST_F(RtpPacketHistoryTest, GetRtpPacket) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  int64_t capture_time_ms = 1;
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                   kAllowRetransmission));

  size_t len_out = kMaxPacketLength;
  int64_t time;
  EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_,
                                             &len_out, &time));
  EXPECT_EQ(len, len_out);
  EXPECT_EQ(capture_time_ms, time);
  for (size_t i = 0; i < len; i++)  {
    EXPECT_EQ(packet_[i], packet_out_[i]);
  }
}

TEST_F(RtpPacketHistoryTest, NoCaptureTime) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  fake_clock_.AdvanceTimeMilliseconds(1);
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, -1, kAllowRetransmission));

  size_t len_out = kMaxPacketLength;
  int64_t time;
  EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_,
                                             &len_out, &time));
  EXPECT_EQ(len, len_out);
  EXPECT_EQ(capture_time_ms, time);
  for (size_t i = 0; i < len; i++)  {
    EXPECT_EQ(packet_[i], packet_out_[i]);
  }
}

TEST_F(RtpPacketHistoryTest, DontRetransmit) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(
      0, hist_->PutRTPPacket(packet_, len, capture_time_ms, kDontRetransmit));

  size_t len_out = kMaxPacketLength;
  int64_t time;
  EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_out_,
                                             &len_out, &time));
  EXPECT_EQ(len, len_out);
  EXPECT_EQ(capture_time_ms, time);
}

TEST_F(RtpPacketHistoryTest, MinResendTime) {
  static const int64_t kMinRetransmitIntervalMs = 100;

  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                   kAllowRetransmission));

  // First transmission: TimeToSendPacket() call from pacer.
  int64_t time;
  len = kMaxPacketLength;
  EXPECT_TRUE(
      hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_, &len, &time));

  fake_clock_.AdvanceTimeMilliseconds(kMinRetransmitIntervalMs);
  // Time has elapsed.
  len = kMaxPacketLength;
  EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, kMinRetransmitIntervalMs,
                                             true, packet_, &len, &time));
  EXPECT_GT(len, 0u);
  EXPECT_EQ(capture_time_ms, time);

  fake_clock_.AdvanceTimeMilliseconds(kMinRetransmitIntervalMs - 1);
  // Time has not elapsed. Packet should be found, but no bytes copied.
  len = kMaxPacketLength;
  EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, kMinRetransmitIntervalMs,
                                              true, packet_, &len, &time));
}

TEST_F(RtpPacketHistoryTest, EarlyFirstResend) {
  static const int64_t kMinRetransmitIntervalMs = 100;

  hist_->SetStorePacketsStatus(true, 10);
  size_t len = 0;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  CreateRtpPacket(kSeqNum, kSsrc, kPayload, kTimestamp, packet_, &len);
  EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                   kAllowRetransmission));

  // First transmission: TimeToSendPacket() call from pacer.
  int64_t time;
  len = kMaxPacketLength;
  EXPECT_TRUE(
      hist_->GetPacketAndSetSendTime(kSeqNum, 0, false, packet_, &len, &time));

  fake_clock_.AdvanceTimeMilliseconds(kMinRetransmitIntervalMs - 1);
  // Time has not elapsed, but this is the first retransmission request so
  // allow anyway.
  len = kMaxPacketLength;
  EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum, kMinRetransmitIntervalMs,
                                             true, packet_, &len, &time));
  EXPECT_GT(len, 0u);
  EXPECT_EQ(capture_time_ms, time);

  fake_clock_.AdvanceTimeMilliseconds(kMinRetransmitIntervalMs - 1);
  // Time has not elapsed. Packet should be found, but no bytes copied.
  len = kMaxPacketLength;
  EXPECT_FALSE(hist_->GetPacketAndSetSendTime(kSeqNum, kMinRetransmitIntervalMs,
                                              true, packet_, &len, &time));
}

TEST_F(RtpPacketHistoryTest, DynamicExpansion) {
  hist_->SetStorePacketsStatus(true, 10);
  size_t len;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  int64_t time;

  // Add 4 packets, and then send them.
  for (int i = 0; i < 4; ++i) {
    len = 0;
    CreateRtpPacket(kSeqNum + i, kSsrc, kPayload, kTimestamp, packet_, &len);
    EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                     kAllowRetransmission));
  }
  for (int i = 0; i < 4; ++i) {
    len = kMaxPacketLength;
    EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum + i, 100, false, packet_,
                                               &len, &time));
  }
  capture_time_ms += 33;

  // Add 16 packets, and then send them. History should expand to make this
  // work.
  for (int i = 4; i < 20; ++i) {
    len = 0;
    CreateRtpPacket(kSeqNum + i, kSsrc, kPayload, kTimestamp, packet_, &len);
    EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                     kAllowRetransmission));
  }
  for (int i = 4; i < 20; ++i) {
    len = kMaxPacketLength;
    EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum + i, 100, false, packet_,
                                               &len, &time));
  }

  fake_clock_.AdvanceTimeMilliseconds(100);

  // Retransmit last 16 packets.
  for (int i = 4; i < 20; ++i) {
    len = kMaxPacketLength;
    EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum + i, 100, false, packet_,
                                               &len, &time));
  }
}

TEST_F(RtpPacketHistoryTest, FullExpansion) {
  hist_->SetStorePacketsStatus(true, kSendSidePacketHistorySize);
  size_t len;
  int64_t capture_time_ms = fake_clock_.TimeInMilliseconds();
  int64_t time;
  for (size_t i = 0; i < kMaxHistoryCapacity + 1; ++i) {
    len = 0;
    CreateRtpPacket(kSeqNum + i, kSsrc, kPayload, kTimestamp, packet_, &len);
    EXPECT_EQ(0, hist_->PutRTPPacket(packet_, len, capture_time_ms,
                                     kAllowRetransmission));
  }

  fake_clock_.AdvanceTimeMilliseconds(100);

  // Retransmit all packets currently in buffer.
  for (size_t i = 1; i < kMaxHistoryCapacity + 1; ++i) {
    len = kMaxPacketLength;
    EXPECT_TRUE(hist_->GetPacketAndSetSendTime(kSeqNum + i, 100, false, packet_,
                                               &len, &time));
  }
}

}  // namespace webrtc