blob: d5432b889ae743fe93b6ec3a9a188224e9429d3d [file] [log] [blame]
philipelc707ab72016-04-01 02:01:54 -07001/*
2 * Copyright (c) 2016 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#include <cstring>
philipel02447bc2016-05-13 06:01:03 -070012#include <map>
13#include <set>
philipela1059872016-05-09 11:41:48 +020014#include <utility>
philipelc707ab72016-04-01 02:01:54 -070015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_video/h264/h264_common.h"
17#include "modules/video_coding/frame_object.h"
18#include "modules/video_coding/packet_buffer.h"
19#include "rtc_base/random.h"
20#include "system_wrappers/include/clock.h"
Rasmus Brandtedf4ff72017-10-24 10:07:48 +020021#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "test/gtest.h"
philipelc707ab72016-04-01 02:01:54 -070023
24namespace webrtc {
25namespace video_coding {
26
27class TestPacketBuffer : public ::testing::Test,
Elad Alonb4643ad2019-02-22 11:19:50 +010028 public OnAssembledFrameCallback {
philipelc707ab72016-04-01 02:01:54 -070029 protected:
Rasmus Brandt88f080a2017-11-02 14:28:06 +010030 TestPacketBuffer() : TestPacketBuffer("") {}
31 explicit TestPacketBuffer(std::string field_trials)
32 : scoped_field_trials_(field_trials),
33 rand_(0x7732213),
philipelb4d31082016-07-11 08:46:29 -070034 clock_(new SimulatedClock(0)),
philipel17deeb42016-08-11 15:09:26 +020035 packet_buffer_(
36 PacketBuffer::Create(clock_.get(), kStartSize, kMaxSize, this)) {}
philipelc707ab72016-04-01 02:01:54 -070037
philipel17deeb42016-08-11 15:09:26 +020038 uint16_t Rand() { return rand_.Rand<uint16_t>(); }
philipelc707ab72016-04-01 02:01:54 -070039
Elad Alonb4643ad2019-02-22 11:19:50 +010040 void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame) override {
philipel17deeb42016-08-11 15:09:26 +020041 uint16_t first_seq_num = frame->first_seq_num();
42 if (frames_from_callback_.find(first_seq_num) !=
43 frames_from_callback_.end()) {
44 ADD_FAILURE() << "Already received frame with first sequence number "
45 << first_seq_num << ".";
philipelf4139332016-04-20 10:26:34 +020046 return;
47 }
philipel2c9f9f22017-06-13 02:47:28 -070048
philipelf4139332016-04-20 10:26:34 +020049 frames_from_callback_.insert(
philipel17deeb42016-08-11 15:09:26 +020050 std::make_pair(frame->first_seq_num(), std::move(frame)));
philipelc707ab72016-04-01 02:01:54 -070051 }
52
philipel17deeb42016-08-11 15:09:26 +020053 enum IsKeyFrame { kKeyFrame, kDeltaFrame };
54 enum IsFirst { kFirst, kNotFirst };
55 enum IsLast { kLast, kNotLast };
philipelc707ab72016-04-01 02:01:54 -070056
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010057 bool Insert(uint16_t seq_num, // packet sequence number
58 IsKeyFrame keyframe, // is keyframe
59 IsFirst first, // is first packet of frame
60 IsLast last, // is last packet of frame
61 int data_size = 0, // size of data
62 uint8_t* data = nullptr, // data pointer
63 uint32_t timestamp = 123u) { // rtp timestamp
philipelf4139332016-04-20 10:26:34 +020064 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +010065 packet.video_header.codec = kVideoCodecGeneric;
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010066 packet.timestamp = timestamp;
philipelf4139332016-04-20 10:26:34 +020067 packet.seqNum = seq_num;
Niels Möllerabbc50e2019-04-24 09:41:16 +020068 packet.video_header.frame_type = keyframe == kKeyFrame
69 ? VideoFrameType::kVideoFrameKey
70 : VideoFrameType::kVideoFrameDelta;
Niels Möllerd5e02f02019-02-20 13:12:21 +010071 packet.video_header.is_first_packet_in_frame = first == kFirst;
72 packet.video_header.is_last_packet_in_frame = last == kLast;
philipelf4139332016-04-20 10:26:34 +020073 packet.sizeBytes = data_size;
74 packet.dataPtr = data;
75
philipel759e0b72016-11-30 01:32:05 -080076 return packet_buffer_->InsertPacket(&packet);
philipelf4139332016-04-20 10:26:34 +020077 }
78
philipel17deeb42016-08-11 15:09:26 +020079 void CheckFrame(uint16_t first_seq_num) {
80 auto frame_it = frames_from_callback_.find(first_seq_num);
81 ASSERT_FALSE(frame_it == frames_from_callback_.end())
82 << "Could not find frame with first sequence number " << first_seq_num
83 << ".";
philipelf4139332016-04-20 10:26:34 +020084 }
85
Johannes Kron957c62e2018-10-01 14:53:01 +020086 void DeleteFrame(uint16_t first_seq_num) {
87 auto frame_it = frames_from_callback_.find(first_seq_num);
88 ASSERT_FALSE(frame_it == frames_from_callback_.end())
89 << "Could not find frame with first sequence number " << first_seq_num
90 << ".";
91 frames_from_callback_.erase(frame_it);
92 }
93
philipel227f8b92017-08-04 06:39:31 -070094 static constexpr int kStartSize = 16;
95 static constexpr int kMaxSize = 64;
philipelc707ab72016-04-01 02:01:54 -070096
Rasmus Brandt88f080a2017-11-02 14:28:06 +010097 const test::ScopedFieldTrials scoped_field_trials_;
98
philipelc707ab72016-04-01 02:01:54 -070099 Random rand_;
philipel3184f8e2017-05-18 08:08:53 -0700100 std::unique_ptr<SimulatedClock> clock_;
philipel17deeb42016-08-11 15:09:26 +0200101 rtc::scoped_refptr<PacketBuffer> packet_buffer_;
102 std::map<uint16_t, std::unique_ptr<RtpFrameObject>> frames_from_callback_;
philipelc707ab72016-04-01 02:01:54 -0700103};
104
105TEST_F(TestPacketBuffer, InsertOnePacket) {
philipelaee3e0e2016-11-01 11:45:34 +0100106 const uint16_t seq_num = Rand();
107 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700108}
109
110TEST_F(TestPacketBuffer, InsertMultiplePackets) {
philipelaee3e0e2016-11-01 11:45:34 +0100111 const uint16_t seq_num = Rand();
112 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
113 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast));
114 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kFirst, kLast));
115 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700116}
117
118TEST_F(TestPacketBuffer, InsertDuplicatePacket) {
philipelaee3e0e2016-11-01 11:45:34 +0100119 const uint16_t seq_num = Rand();
120 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
121 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
122}
123
Artem Titarenko97e35ce2018-10-30 10:17:54 +0000124TEST_F(TestPacketBuffer, SeqNumWrapOneFrame) {
philipel2c2f34c2017-01-03 05:55:34 -0800125 EXPECT_TRUE(Insert(0xFFFF, kKeyFrame, kFirst, kNotLast));
126 EXPECT_TRUE(Insert(0x0, kKeyFrame, kNotFirst, kLast));
127
128 CheckFrame(0xFFFF);
129}
130
131TEST_F(TestPacketBuffer, SeqNumWrapTwoFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100132 EXPECT_TRUE(Insert(0xFFFF, kKeyFrame, kFirst, kLast));
133 EXPECT_TRUE(Insert(0x0, kKeyFrame, kFirst, kLast));
134
135 CheckFrame(0xFFFF);
philipel2c2f34c2017-01-03 05:55:34 -0800136 CheckFrame(0x0);
philipelaee3e0e2016-11-01 11:45:34 +0100137}
138
139TEST_F(TestPacketBuffer, InsertOldPackets) {
140 const uint16_t seq_num = Rand();
141
142 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
143 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
144 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast));
145 ASSERT_EQ(2UL, frames_from_callback_.size());
146
147 frames_from_callback_.erase(seq_num + 2);
148 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
149 ASSERT_EQ(1UL, frames_from_callback_.size());
150
151 frames_from_callback_.erase(frames_from_callback_.find(seq_num));
152 ASSERT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
153 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
154
155 packet_buffer_->ClearTo(seq_num + 2);
156 EXPECT_FALSE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
157 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
158 ASSERT_EQ(2UL, frames_from_callback_.size());
philipelc707ab72016-04-01 02:01:54 -0700159}
160
philipel5ceaaae2016-05-24 10:20:47 +0200161TEST_F(TestPacketBuffer, NackCount) {
philipelaee3e0e2016-11-01 11:45:34 +0100162 const uint16_t seq_num = Rand();
philipel5ceaaae2016-05-24 10:20:47 +0200163
164 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100165 packet.video_header.codec = kVideoCodecGeneric;
philipel5ceaaae2016-05-24 10:20:47 +0200166 packet.seqNum = seq_num;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200167 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100168 packet.video_header.is_first_packet_in_frame = true;
169 packet.video_header.is_last_packet_in_frame = false;
philipel5ceaaae2016-05-24 10:20:47 +0200170 packet.timesNacked = 0;
171
philipel759e0b72016-11-30 01:32:05 -0800172 packet_buffer_->InsertPacket(&packet);
philipel5ceaaae2016-05-24 10:20:47 +0200173
174 packet.seqNum++;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100175 packet.video_header.is_first_packet_in_frame = false;
philipel5ceaaae2016-05-24 10:20:47 +0200176 packet.timesNacked = 1;
philipel759e0b72016-11-30 01:32:05 -0800177 packet_buffer_->InsertPacket(&packet);
philipel5ceaaae2016-05-24 10:20:47 +0200178
179 packet.seqNum++;
180 packet.timesNacked = 3;
philipel759e0b72016-11-30 01:32:05 -0800181 packet_buffer_->InsertPacket(&packet);
philipel5ceaaae2016-05-24 10:20:47 +0200182
183 packet.seqNum++;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100184 packet.video_header.is_last_packet_in_frame = true;
philipel5ceaaae2016-05-24 10:20:47 +0200185 packet.timesNacked = 1;
philipel759e0b72016-11-30 01:32:05 -0800186 packet_buffer_->InsertPacket(&packet);
philipel5ceaaae2016-05-24 10:20:47 +0200187
philipel5ceaaae2016-05-24 10:20:47 +0200188 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200189 RtpFrameObject* frame = frames_from_callback_.begin()->second.get();
190 EXPECT_EQ(3, frame->times_nacked());
philipel5ceaaae2016-05-24 10:20:47 +0200191}
192
193TEST_F(TestPacketBuffer, FrameSize) {
philipelaee3e0e2016-11-01 11:45:34 +0100194 const uint16_t seq_num = Rand();
philipel41b8ca02016-11-07 15:42:24 +0100195 uint8_t* data1 = new uint8_t[5]();
196 uint8_t* data2 = new uint8_t[5]();
197 uint8_t* data3 = new uint8_t[5]();
198 uint8_t* data4 = new uint8_t[5]();
philipel5ceaaae2016-05-24 10:20:47 +0200199
philipel41b8ca02016-11-07 15:42:24 +0100200 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast, 5, data1));
201 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast, 5, data2));
202 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kNotLast, 5, data3));
203 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, 5, data4));
philipel5ceaaae2016-05-24 10:20:47 +0200204
205 ASSERT_EQ(1UL, frames_from_callback_.size());
nisse37abf532016-10-28 00:37:29 -0700206 EXPECT_EQ(20UL, frames_from_callback_.begin()->second->size());
philipel5ceaaae2016-05-24 10:20:47 +0200207}
208
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100209TEST_F(TestPacketBuffer, CountsUniqueFrames) {
210 const uint16_t seq_num = Rand();
211
212 ASSERT_EQ(0, packet_buffer_->GetUniqueFramesSeen());
213
214 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, 100));
215 ASSERT_EQ(1, packet_buffer_->GetUniqueFramesSeen());
216 // Still the same frame.
217 EXPECT_TRUE(
218 Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100));
219 ASSERT_EQ(1, packet_buffer_->GetUniqueFramesSeen());
220
221 // Second frame.
222 EXPECT_TRUE(
223 Insert(seq_num + 2, kKeyFrame, kFirst, kNotLast, 0, nullptr, 200));
224 ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen());
225 EXPECT_TRUE(
226 Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, 0, nullptr, 200));
227 ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen());
228
229 // Old packet.
230 EXPECT_TRUE(
231 Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100));
232 ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen());
233
234 // Missing middle packet.
235 EXPECT_TRUE(
236 Insert(seq_num + 4, kKeyFrame, kFirst, kNotLast, 0, nullptr, 300));
237 EXPECT_TRUE(
238 Insert(seq_num + 6, kKeyFrame, kNotFirst, kLast, 0, nullptr, 300));
239 ASSERT_EQ(3, packet_buffer_->GetUniqueFramesSeen());
240}
241
242TEST_F(TestPacketBuffer, HasHistoryOfUniqueFrames) {
243 const int kNumFrames = 1500;
244 const int kRequiredHistoryLength = 1000;
245 const uint16_t seq_num = Rand();
246 const uint32_t timestamp = 0xFFFFFFF0; // Large enough to cause wrap-around.
247
248 for (int i = 0; i < kNumFrames; ++i) {
249 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr,
250 timestamp + 10 * i));
251 }
252 ASSERT_EQ(kNumFrames, packet_buffer_->GetUniqueFramesSeen());
253
254 // Old packets within history should not affect number of seen unique frames.
255 for (int i = kNumFrames - kRequiredHistoryLength; i < kNumFrames; ++i) {
256 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr,
257 timestamp + 10 * i));
258 }
259 ASSERT_EQ(kNumFrames, packet_buffer_->GetUniqueFramesSeen());
260
261 // Very old packets should be treated as unique.
262 EXPECT_TRUE(
263 Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp));
264 ASSERT_EQ(kNumFrames + 1, packet_buffer_->GetUniqueFramesSeen());
265}
266
philipelc707ab72016-04-01 02:01:54 -0700267TEST_F(TestPacketBuffer, ExpandBuffer) {
philipelaee3e0e2016-11-01 11:45:34 +0100268 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700269
270 for (int i = 0; i < kStartSize + 1; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100271 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700272 }
273}
274
philipelaee3e0e2016-11-01 11:45:34 +0100275TEST_F(TestPacketBuffer, SingleFrameExpandsBuffer) {
276 const uint16_t seq_num = Rand();
277
278 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
279 for (int i = 1; i < kStartSize; ++i)
280 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast));
281 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast));
282
283 ASSERT_EQ(1UL, frames_from_callback_.size());
284 CheckFrame(seq_num);
285}
286
philipelc707ab72016-04-01 02:01:54 -0700287TEST_F(TestPacketBuffer, ExpandBufferOverflow) {
philipelaee3e0e2016-11-01 11:45:34 +0100288 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700289
philipelaee3e0e2016-11-01 11:45:34 +0100290 for (int i = 0; i < kMaxSize; ++i)
291 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast));
philipelc703dc22017-03-23 06:50:37 -0700292 EXPECT_TRUE(Insert(seq_num + kMaxSize + 1, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700293}
294
philipel17deeb42016-08-11 15:09:26 +0200295TEST_F(TestPacketBuffer, OnePacketOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100296 const uint16_t seq_num = Rand();
297 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200298 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200299 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700300}
301
philipel17deeb42016-08-11 15:09:26 +0200302TEST_F(TestPacketBuffer, TwoPacketsTwoFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100303 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200304
philipelaee3e0e2016-11-01 11:45:34 +0100305 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
306 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200307
philipelc707ab72016-04-01 02:01:54 -0700308 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200309 CheckFrame(seq_num);
310 CheckFrame(seq_num + 1);
philipelc707ab72016-04-01 02:01:54 -0700311}
312
philipel17deeb42016-08-11 15:09:26 +0200313TEST_F(TestPacketBuffer, TwoPacketsOneFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100314 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200315
philipelaee3e0e2016-11-01 11:45:34 +0100316 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
317 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200318
philipelc707ab72016-04-01 02:01:54 -0700319 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200320 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700321}
322
philipel17deeb42016-08-11 15:09:26 +0200323TEST_F(TestPacketBuffer, ThreePacketReorderingOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100324 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700325
philipelaee3e0e2016-11-01 11:45:34 +0100326 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
327 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kLast));
328 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast));
philipelf4139332016-04-20 10:26:34 +0200329
philipelc707ab72016-04-01 02:01:54 -0700330 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200331 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700332}
333
philipel17deeb42016-08-11 15:09:26 +0200334TEST_F(TestPacketBuffer, Frames) {
philipelaee3e0e2016-11-01 11:45:34 +0100335 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200336
philipelaee3e0e2016-11-01 11:45:34 +0100337 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
338 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
339 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
340 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200341
342 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200343 CheckFrame(seq_num);
344 CheckFrame(seq_num + 1);
345 CheckFrame(seq_num + 2);
346 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200347}
348
philipelaee3e0e2016-11-01 11:45:34 +0100349TEST_F(TestPacketBuffer, ClearSinglePacket) {
350 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200351
philipelaee3e0e2016-11-01 11:45:34 +0100352 for (int i = 0; i < kMaxSize; ++i)
353 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kFirst, kLast));
354
355 packet_buffer_->ClearTo(seq_num);
356 EXPECT_TRUE(Insert(seq_num + kMaxSize, kDeltaFrame, kFirst, kLast));
357}
358
philipelc5fb4682017-08-02 04:28:57 -0700359TEST_F(TestPacketBuffer, ClearFullBuffer) {
360 for (int i = 0; i < kMaxSize; ++i)
361 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
362
363 packet_buffer_->ClearTo(kMaxSize - 1);
364
365 for (int i = kMaxSize; i < 2 * kMaxSize; ++i)
366 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
367}
368
369TEST_F(TestPacketBuffer, DontClearNewerPacket) {
370 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kLast));
371 packet_buffer_->ClearTo(0);
372 EXPECT_TRUE(Insert(2 * kStartSize, kKeyFrame, kFirst, kLast));
373 EXPECT_TRUE(Insert(3 * kStartSize + 1, kKeyFrame, kFirst, kNotLast));
374 packet_buffer_->ClearTo(2 * kStartSize);
375 EXPECT_TRUE(Insert(3 * kStartSize + 2, kKeyFrame, kNotFirst, kLast));
376
377 ASSERT_EQ(3UL, frames_from_callback_.size());
378 CheckFrame(0);
379 CheckFrame(2 * kStartSize);
380 CheckFrame(3 * kStartSize + 1);
381}
382
philipelaee3e0e2016-11-01 11:45:34 +0100383TEST_F(TestPacketBuffer, OneIncompleteFrame) {
384 const uint16_t seq_num = Rand();
385
386 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
387 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kLast));
388 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
389
390 ASSERT_EQ(1UL, frames_from_callback_.size());
391 CheckFrame(seq_num);
392}
393
394TEST_F(TestPacketBuffer, TwoIncompleteFramesFullBuffer) {
395 const uint16_t seq_num = Rand();
396
397 for (int i = 1; i < kMaxSize - 1; ++i)
398 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kNotFirst, kNotLast));
399 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
400 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
401
402 ASSERT_EQ(0UL, frames_from_callback_.size());
403}
404
405TEST_F(TestPacketBuffer, FramesReordered) {
406 const uint16_t seq_num = Rand();
407
408 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
409 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
410 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
411 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200412
413 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200414 CheckFrame(seq_num);
415 CheckFrame(seq_num + 1);
416 CheckFrame(seq_num + 2);
417 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200418}
419
philipel36928452016-11-07 10:42:36 +0100420TEST_F(TestPacketBuffer, GetBitstream) {
philipelc707ab72016-04-01 02:01:54 -0700421 // "many bitstream, such data" with null termination.
philipel41b8ca02016-11-07 15:42:24 +0100422 uint8_t many_data[] = {0x6d, 0x61, 0x6e, 0x79, 0x20};
423 uint8_t bitstream_data[] = {0x62, 0x69, 0x74, 0x73, 0x74, 0x72,
424 0x65, 0x61, 0x6d, 0x2c, 0x20};
425 uint8_t such_data[] = {0x73, 0x75, 0x63, 0x68, 0x20};
426 uint8_t data_data[] = {0x64, 0x61, 0x74, 0x61, 0x0};
427
428 uint8_t* many = new uint8_t[sizeof(many_data)];
429 uint8_t* bitstream = new uint8_t[sizeof(bitstream_data)];
430 uint8_t* such = new uint8_t[sizeof(such_data)];
431 uint8_t* data = new uint8_t[sizeof(data_data)];
432
433 memcpy(many, many_data, sizeof(many_data));
434 memcpy(bitstream, bitstream_data, sizeof(bitstream_data));
435 memcpy(such, such_data, sizeof(such_data));
436 memcpy(data, data_data, sizeof(data_data));
437
Niels Möller648a7ce2018-11-28 15:14:54 +0100438 const size_t result_length = sizeof(many_data) + sizeof(bitstream_data) +
439 sizeof(such_data) + sizeof(data_data);
philipelc707ab72016-04-01 02:01:54 -0700440
philipelaee3e0e2016-11-01 11:45:34 +0100441 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700442
philipelaee3e0e2016-11-01 11:45:34 +0100443 EXPECT_TRUE(
philipel41b8ca02016-11-07 15:42:24 +0100444 Insert(seq_num, kKeyFrame, kFirst, kNotLast, sizeof(many_data), many));
445 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast,
446 sizeof(bitstream_data), bitstream));
447 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kNotLast,
448 sizeof(such_data), such));
449 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kNotFirst, kLast,
450 sizeof(data_data), data));
philipelf4139332016-04-20 10:26:34 +0200451
452 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200453 CheckFrame(seq_num);
Niels Möller648a7ce2018-11-28 15:14:54 +0100454 EXPECT_EQ(frames_from_callback_[seq_num]->size(), result_length);
Niels Möller9c843902019-01-11 10:21:35 +0100455 EXPECT_EQ(memcmp(frames_from_callback_[seq_num]->data(),
Niels Möller648a7ce2018-11-28 15:14:54 +0100456 "many bitstream, such data", result_length),
457 0);
philipelc707ab72016-04-01 02:01:54 -0700458}
459
philipel227f8b92017-08-04 06:39:31 -0700460TEST_F(TestPacketBuffer, GetBitstreamOneFrameOnePacket) {
461 uint8_t bitstream_data[] = "All the bitstream data for this frame!";
philipel227f8b92017-08-04 06:39:31 -0700462 uint8_t* data = new uint8_t[sizeof(bitstream_data)];
463 memcpy(data, bitstream_data, sizeof(bitstream_data));
464
465 EXPECT_TRUE(
466 Insert(0, kKeyFrame, kFirst, kLast, sizeof(bitstream_data), data));
467
468 ASSERT_EQ(1UL, frames_from_callback_.size());
469 CheckFrame(0);
470 EXPECT_EQ(frames_from_callback_[0]->size(), sizeof(bitstream_data));
Niels Möller648a7ce2018-11-28 15:14:54 +0100471 EXPECT_EQ(
Niels Möller9c843902019-01-11 10:21:35 +0100472 memcmp(frames_from_callback_[0]->data(), data, sizeof(bitstream_data)),
Niels Möller648a7ce2018-11-28 15:14:54 +0100473 0);
philipel227f8b92017-08-04 06:39:31 -0700474}
475
476TEST_F(TestPacketBuffer, GetBitstreamOneFrameFullBuffer) {
477 uint8_t* data_arr[kStartSize];
478 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700479
480 for (uint8_t i = 0; i < kStartSize; ++i) {
481 data_arr[i] = new uint8_t[1];
482 data_arr[i][0] = i;
483 expected[i] = i;
484 }
485
486 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kNotLast, 1, data_arr[0]));
487 for (uint8_t i = 1; i < kStartSize - 1; ++i)
488 EXPECT_TRUE(Insert(i, kKeyFrame, kNotFirst, kNotLast, 1, data_arr[i]));
489 EXPECT_TRUE(Insert(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1,
490 data_arr[kStartSize - 1]));
491
492 ASSERT_EQ(1UL, frames_from_callback_.size());
493 CheckFrame(0);
494 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100495 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700496}
497
Johannes Kron957c62e2018-10-01 14:53:01 +0200498TEST_F(TestPacketBuffer, InsertPacketAfterOldFrameObjectIsRemoved) {
499 uint16_t kFirstSeqNum = 0;
500 uint32_t kTimestampDelta = 100;
501 uint32_t timestamp = 10000;
502 uint16_t seq_num = kFirstSeqNum;
503
504 // Loop until seq_num wraps around.
Philip Eliasson1f850a62019-03-19 12:15:00 +0000505 SeqNumUnwrapper<uint16_t> unwrapper;
Johannes Kron957c62e2018-10-01 14:53:01 +0200506 while (unwrapper.Unwrap(seq_num) < std::numeric_limits<uint16_t>::max()) {
507 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
508 for (int i = 0; i < 5; ++i) {
509 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
510 }
511 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
512 timestamp += kTimestampDelta;
513 }
514
515 size_t number_of_frames = frames_from_callback_.size();
516 // Delete old frame object while receiving frame with overlapping sequence
517 // numbers.
518 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
519 for (int i = 0; i < 5; ++i) {
520 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
521 }
522 // Delete FrameObject connected to packets that have already been cleared.
523 DeleteFrame(kFirstSeqNum);
524 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
525
526 // Regardless of the initial size, the number of frames should be constant
527 // after removing and then adding a new frame object.
528 EXPECT_EQ(number_of_frames, frames_from_callback_.size());
529}
530
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100531// If |sps_pps_idr_is_keyframe| is true, we require keyframes to contain
532// SPS/PPS/IDR and the keyframes we create as part of the test do contain
533// SPS/PPS/IDR. If |sps_pps_idr_is_keyframe| is false, we only require and
534// create keyframes containing only IDR.
535class TestPacketBufferH264 : public TestPacketBuffer {
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200536 protected:
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200537 explicit TestPacketBufferH264(bool sps_pps_idr_is_keyframe)
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100538 : TestPacketBuffer(sps_pps_idr_is_keyframe
539 ? "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/"
540 : ""),
541 sps_pps_idr_is_keyframe_(sps_pps_idr_is_keyframe) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200542
543 bool InsertH264(uint16_t seq_num, // packet sequence number
544 IsKeyFrame keyframe, // is keyframe
545 IsFirst first, // is first packet of frame
546 IsLast last, // is last packet of frame
547 uint32_t timestamp, // rtp timestamp
548 int data_size = 0, // size of data
549 uint8_t* data = nullptr) { // data pointer
550 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100551 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200552 auto& h264_header =
553 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200554 packet.seqNum = seq_num;
555 packet.timestamp = timestamp;
556 if (keyframe == kKeyFrame) {
557 if (sps_pps_idr_is_keyframe_) {
philipel7d745e52018-08-02 14:03:53 +0200558 h264_header.nalus[0].type = H264::NaluType::kSps;
559 h264_header.nalus[1].type = H264::NaluType::kPps;
560 h264_header.nalus[2].type = H264::NaluType::kIdr;
561 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200562 } else {
philipel7d745e52018-08-02 14:03:53 +0200563 h264_header.nalus[0].type = H264::NaluType::kIdr;
564 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200565 }
566 }
Niels Möllerd5e02f02019-02-20 13:12:21 +0100567 packet.video_header.is_first_packet_in_frame = first == kFirst;
568 packet.video_header.is_last_packet_in_frame = last == kLast;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200569 packet.sizeBytes = data_size;
570 packet.dataPtr = data;
571
572 return packet_buffer_->InsertPacket(&packet);
573 }
574
575 const bool sps_pps_idr_is_keyframe_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100576};
577
578// This fixture is used to test the general behaviour of the packet buffer
579// in both configurations.
580class TestPacketBufferH264Parameterized
581 : public ::testing::WithParamInterface<bool>,
582 public TestPacketBufferH264 {
583 protected:
584 TestPacketBufferH264Parameterized() : TestPacketBufferH264(GetParam()) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200585};
586
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100587INSTANTIATE_TEST_SUITE_P(SpsPpsIdrIsKeyframe,
588 TestPacketBufferH264Parameterized,
589 ::testing::Values(false, true));
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200590
philipelbc5a4082017-12-06 10:41:08 +0100591TEST_P(TestPacketBufferH264Parameterized, DontRemoveMissingPacketOnClearTo) {
592 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kLast, 0));
593 EXPECT_TRUE(InsertH264(2, kDeltaFrame, kFirst, kNotLast, 2));
594 packet_buffer_->ClearTo(0);
595 EXPECT_TRUE(InsertH264(3, kDeltaFrame, kNotFirst, kLast, 2));
596
597 ASSERT_EQ(1UL, frames_from_callback_.size());
598 CheckFrame(0);
599}
600
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100601TEST_P(TestPacketBufferH264Parameterized, GetBitstreamOneFrameFullBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700602 uint8_t* data_arr[kStartSize];
603 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700604
605 for (uint8_t i = 0; i < kStartSize; ++i) {
606 data_arr[i] = new uint8_t[1];
607 data_arr[i][0] = i;
608 expected[i] = i;
609 }
610
611 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kNotLast, 1, 1, data_arr[0]));
612 for (uint8_t i = 1; i < kStartSize - 1; ++i) {
613 EXPECT_TRUE(
614 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1, 1, data_arr[i]));
615 }
616 EXPECT_TRUE(InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1, 1,
617 data_arr[kStartSize - 1]));
618
619 ASSERT_EQ(1UL, frames_from_callback_.size());
620 CheckFrame(0);
621 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100622 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700623}
624
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100625TEST_P(TestPacketBufferH264Parameterized, GetBitstreamBufferPadding) {
philipel36928452016-11-07 10:42:36 +0100626 uint16_t seq_num = Rand();
philipel41b8ca02016-11-07 15:42:24 +0100627 uint8_t data_data[] = "some plain old data";
628 uint8_t* data = new uint8_t[sizeof(data_data)];
629 memcpy(data, data_data, sizeof(data_data));
philipel36928452016-11-07 10:42:36 +0100630
philipel36928452016-11-07 10:42:36 +0100631 VCMPacket packet;
philipel7d745e52018-08-02 14:03:53 +0200632 auto& h264_header =
633 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
634 h264_header.nalus_length = 1;
635 h264_header.nalus[0].type = H264::NaluType::kIdr;
636 h264_header.packetization_type = kH264SingleNalu;
philipel36928452016-11-07 10:42:36 +0100637 packet.seqNum = seq_num;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100638 packet.video_header.codec = kVideoCodecH264;
philipel36928452016-11-07 10:42:36 +0100639 packet.insertStartCode = true;
philipel36928452016-11-07 10:42:36 +0100640 packet.dataPtr = data;
philipel41b8ca02016-11-07 15:42:24 +0100641 packet.sizeBytes = sizeof(data_data);
Niels Möllerd5e02f02019-02-20 13:12:21 +0100642 packet.video_header.is_first_packet_in_frame = true;
643 packet.video_header.is_last_packet_in_frame = true;
philipel759e0b72016-11-30 01:32:05 -0800644 packet_buffer_->InsertPacket(&packet);
philipel36928452016-11-07 10:42:36 +0100645
646 ASSERT_EQ(1UL, frames_from_callback_.size());
Niels Möller77536a22019-01-15 08:50:01 +0100647 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().size(),
philipel41b8ca02016-11-07 15:42:24 +0100648 sizeof(data_data));
Niels Möller48a79462018-12-07 16:21:18 +0100649 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().capacity(),
Niels Möller009ab3c2019-03-08 11:26:58 +0100650 sizeof(data_data));
Niels Möller648a7ce2018-11-28 15:14:54 +0100651 EXPECT_EQ(
Niels Möller9c843902019-01-11 10:21:35 +0100652 memcmp(frames_from_callback_[seq_num]->data(), data, sizeof(data_data)),
Niels Möller648a7ce2018-11-28 15:14:54 +0100653 0);
philipel36928452016-11-07 10:42:36 +0100654}
655
philipelc707ab72016-04-01 02:01:54 -0700656TEST_F(TestPacketBuffer, FreeSlotsOnFrameDestruction) {
philipelaee3e0e2016-11-01 11:45:34 +0100657 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200658
philipelaee3e0e2016-11-01 11:45:34 +0100659 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
660 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
661 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700662 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200663 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700664
665 frames_from_callback_.clear();
666
philipel17deeb42016-08-11 15:09:26 +0200667 // Insert frame that fills the whole buffer.
philipelaee3e0e2016-11-01 11:45:34 +0100668 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kNotLast));
philipel17deeb42016-08-11 15:09:26 +0200669 for (int i = 0; i < kMaxSize - 2; ++i)
philipelaee3e0e2016-11-01 11:45:34 +0100670 EXPECT_TRUE(Insert(seq_num + i + 4, kDeltaFrame, kNotFirst, kNotLast));
671 EXPECT_TRUE(Insert(seq_num + kMaxSize + 2, kKeyFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700672 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200673 CheckFrame(seq_num + 3);
philipelc707ab72016-04-01 02:01:54 -0700674}
675
philipel02447bc2016-05-13 06:01:03 -0700676TEST_F(TestPacketBuffer, Clear) {
philipelaee3e0e2016-11-01 11:45:34 +0100677 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200678
philipelaee3e0e2016-11-01 11:45:34 +0100679 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
680 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
681 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200682 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200683 CheckFrame(seq_num);
philipelf4139332016-04-20 10:26:34 +0200684
philipel02447bc2016-05-13 06:01:03 -0700685 packet_buffer_->Clear();
philipelf4139332016-04-20 10:26:34 +0200686
philipelaee3e0e2016-11-01 11:45:34 +0100687 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kFirst, kNotLast));
688 EXPECT_TRUE(
689 Insert(seq_num + kStartSize + 1, kDeltaFrame, kNotFirst, kNotLast));
690 EXPECT_TRUE(Insert(seq_num + kStartSize + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700691 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200692 CheckFrame(seq_num + kStartSize);
philipelc707ab72016-04-01 02:01:54 -0700693}
694
philipel20dce342016-11-28 16:14:57 +0100695TEST_F(TestPacketBuffer, FramesAfterClear) {
696 Insert(9025, kDeltaFrame, kFirst, kLast);
697 Insert(9024, kKeyFrame, kFirst, kLast);
698 packet_buffer_->ClearTo(9025);
699 Insert(9057, kDeltaFrame, kFirst, kLast);
700 Insert(9026, kDeltaFrame, kFirst, kLast);
701
702 CheckFrame(9024);
703 CheckFrame(9025);
704 CheckFrame(9026);
705 CheckFrame(9057);
706}
707
philipel8b6995b2019-01-09 12:39:18 +0100708TEST_F(TestPacketBuffer, SameFrameDifferentTimestamps) {
709 Insert(0, kKeyFrame, kFirst, kNotLast, 0, nullptr, 1000);
710 Insert(1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 1001);
711
712 ASSERT_EQ(0UL, frames_from_callback_.size());
713}
714
philipel759e0b72016-11-30 01:32:05 -0800715TEST_F(TestPacketBuffer, DontLeakPayloadData) {
716 // NOTE! Any eventual leak is suppose to be detected by valgrind
717 // or any other similar tool.
718 uint8_t* data1 = new uint8_t[5];
719 uint8_t* data2 = new uint8_t[5];
720 uint8_t* data3 = new uint8_t[5];
721 uint8_t* data4 = new uint8_t[5];
722
723 // Expected to free data1 upon PacketBuffer destruction.
724 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data1));
725
726 // Expect to free data2 upon insertion.
727 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data2));
728
729 // Expect to free data3 upon insertion (old packet).
730 packet_buffer_->ClearTo(1);
731 EXPECT_FALSE(Insert(1, kKeyFrame, kFirst, kNotLast, 5, data3));
732
733 // Expect to free data4 upon insertion (packet buffer is full).
philipelc703dc22017-03-23 06:50:37 -0700734 EXPECT_TRUE(Insert(2 + kMaxSize, kKeyFrame, kFirst, kNotLast, 5, data4));
philipel759e0b72016-11-30 01:32:05 -0800735}
736
philipelea142f82017-01-11 02:01:56 -0800737TEST_F(TestPacketBuffer, ContinuousSeqNumDoubleMarkerBit) {
738 Insert(2, kKeyFrame, kNotFirst, kNotLast);
739 Insert(1, kKeyFrame, kFirst, kLast);
740 frames_from_callback_.clear();
741 Insert(3, kKeyFrame, kNotFirst, kLast);
742
743 EXPECT_EQ(0UL, frames_from_callback_.size());
744}
745
philipel3184f8e2017-05-18 08:08:53 -0700746TEST_F(TestPacketBuffer, PacketTimestamps) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200747 absl::optional<int64_t> packet_ms;
748 absl::optional<int64_t> packet_keyframe_ms;
philipel3184f8e2017-05-18 08:08:53 -0700749
750 packet_ms = packet_buffer_->LastReceivedPacketMs();
751 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
752 EXPECT_FALSE(packet_ms);
753 EXPECT_FALSE(packet_keyframe_ms);
754
755 int64_t keyframe_ms = clock_->TimeInMilliseconds();
756 EXPECT_TRUE(Insert(100, kKeyFrame, kFirst, kLast));
757 packet_ms = packet_buffer_->LastReceivedPacketMs();
758 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
759 EXPECT_TRUE(packet_ms);
760 EXPECT_TRUE(packet_keyframe_ms);
761 EXPECT_EQ(keyframe_ms, *packet_ms);
762 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
763
764 clock_->AdvanceTimeMilliseconds(100);
765 int64_t delta_ms = clock_->TimeInMilliseconds();
766 EXPECT_TRUE(Insert(101, kDeltaFrame, kFirst, kLast));
767 packet_ms = packet_buffer_->LastReceivedPacketMs();
768 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
769 EXPECT_TRUE(packet_ms);
770 EXPECT_TRUE(packet_keyframe_ms);
771 EXPECT_EQ(delta_ms, *packet_ms);
772 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
773
774 packet_buffer_->Clear();
775 packet_ms = packet_buffer_->LastReceivedPacketMs();
776 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
777 EXPECT_FALSE(packet_ms);
778 EXPECT_FALSE(packet_keyframe_ms);
779}
780
philipel09133af2018-05-17 14:11:09 +0200781TEST_F(TestPacketBuffer, IncomingCodecChange) {
782 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100783 packet.video_header.is_first_packet_in_frame = true;
784 packet.video_header.is_last_packet_in_frame = true;
philipel09133af2018-05-17 14:11:09 +0200785 packet.sizeBytes = 0;
786 packet.dataPtr = nullptr;
787
Niels Möllerd5e02f02019-02-20 13:12:21 +0100788 packet.video_header.codec = kVideoCodecVP8;
789 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200790 packet.timestamp = 1;
791 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200792 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
philipel09133af2018-05-17 14:11:09 +0200793 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
794
Niels Möllerd5e02f02019-02-20 13:12:21 +0100795 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200796 auto& h264_header =
797 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
798 h264_header.nalus_length = 1;
philipel09133af2018-05-17 14:11:09 +0200799 packet.timestamp = 3;
800 packet.seqNum = 3;
801 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
802
Niels Möllerd5e02f02019-02-20 13:12:21 +0100803 packet.video_header.codec = kVideoCodecVP8;
804 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200805 packet.timestamp = 2;
806 packet.seqNum = 2;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200807 packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
philipel09133af2018-05-17 14:11:09 +0200808
809 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
810
811 EXPECT_EQ(3UL, frames_from_callback_.size());
812}
813
814TEST_F(TestPacketBuffer, TooManyNalusInPacket) {
815 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100816 packet.video_header.codec = kVideoCodecH264;
philipel09133af2018-05-17 14:11:09 +0200817 packet.timestamp = 1;
818 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200819 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100820 packet.video_header.is_first_packet_in_frame = true;
821 packet.video_header.is_last_packet_in_frame = true;
philipel7d745e52018-08-02 14:03:53 +0200822 auto& h264_header =
823 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
824 h264_header.nalus_length = kMaxNalusPerPacket;
philipel09133af2018-05-17 14:11:09 +0200825 packet.sizeBytes = 0;
826 packet.dataPtr = nullptr;
827 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
828
829 EXPECT_EQ(0UL, frames_from_callback_.size());
830}
831
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100832TEST_P(TestPacketBufferH264Parameterized, OneFrameFillBuffer) {
philipel2c9f9f22017-06-13 02:47:28 -0700833 InsertH264(0, kKeyFrame, kFirst, kNotLast, 1000);
834 for (int i = 1; i < kStartSize - 1; ++i)
835 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1000);
836 InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1000);
837
838 EXPECT_EQ(1UL, frames_from_callback_.size());
839 CheckFrame(0);
840}
841
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100842TEST_P(TestPacketBufferH264Parameterized, CreateFramesAfterFilledBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700843 InsertH264(kStartSize - 2, kKeyFrame, kFirst, kLast, 0);
844 ASSERT_EQ(1UL, frames_from_callback_.size());
845 frames_from_callback_.clear();
846
847 InsertH264(kStartSize, kDeltaFrame, kFirst, kNotLast, 2000);
848 for (int i = 1; i < kStartSize; ++i)
849 InsertH264(kStartSize + i, kDeltaFrame, kNotFirst, kNotLast, 2000);
850 InsertH264(kStartSize + kStartSize, kDeltaFrame, kNotFirst, kLast, 2000);
851 ASSERT_EQ(0UL, frames_from_callback_.size());
852
853 InsertH264(kStartSize - 1, kKeyFrame, kFirst, kLast, 1000);
854 ASSERT_EQ(2UL, frames_from_callback_.size());
855 CheckFrame(kStartSize - 1);
856 CheckFrame(kStartSize);
857}
858
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100859TEST_P(TestPacketBufferH264Parameterized, OneFrameMaxSeqNum) {
philipel2c9f9f22017-06-13 02:47:28 -0700860 InsertH264(65534, kKeyFrame, kFirst, kNotLast, 1000);
861 InsertH264(65535, kKeyFrame, kNotFirst, kLast, 1000);
862
863 EXPECT_EQ(1UL, frames_from_callback_.size());
864 CheckFrame(65534);
865}
866
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100867TEST_P(TestPacketBufferH264Parameterized, ClearMissingPacketsOnKeyframe) {
philipel2c9f9f22017-06-13 02:47:28 -0700868 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
869 InsertH264(2, kKeyFrame, kFirst, kLast, 3000);
870 InsertH264(3, kDeltaFrame, kFirst, kNotLast, 4000);
871 InsertH264(4, kDeltaFrame, kNotFirst, kLast, 4000);
872
873 ASSERT_EQ(3UL, frames_from_callback_.size());
874
875 InsertH264(kStartSize + 1, kKeyFrame, kFirst, kLast, 18000);
876
877 ASSERT_EQ(4UL, frames_from_callback_.size());
878 CheckFrame(0);
879 CheckFrame(2);
880 CheckFrame(3);
881 CheckFrame(kStartSize + 1);
882}
883
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100884TEST_P(TestPacketBufferH264Parameterized, FindFramesOnPadding) {
philipel2c9f9f22017-06-13 02:47:28 -0700885 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
886 InsertH264(2, kDeltaFrame, kFirst, kLast, 1000);
887
888 ASSERT_EQ(1UL, frames_from_callback_.size());
889 packet_buffer_->PaddingReceived(1);
890 ASSERT_EQ(2UL, frames_from_callback_.size());
891 CheckFrame(0);
892 CheckFrame(2);
893}
894
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200895class TestPacketBufferH264XIsKeyframe : public TestPacketBufferH264 {
896 protected:
897 const uint16_t kSeqNum = 5;
898
899 explicit TestPacketBufferH264XIsKeyframe(bool sps_pps_idr_is_keyframe)
900 : TestPacketBufferH264(sps_pps_idr_is_keyframe) {
Niels Möllerd5e02f02019-02-20 13:12:21 +0100901 packet_.video_header.codec = kVideoCodecH264;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200902 packet_.seqNum = kSeqNum;
903
Niels Möllerd5e02f02019-02-20 13:12:21 +0100904 packet_.video_header.is_first_packet_in_frame = true;
905 packet_.video_header.is_last_packet_in_frame = true;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200906 }
907
908 VCMPacket packet_;
909};
910
911class TestPacketBufferH264IdrIsKeyframe
912 : public TestPacketBufferH264XIsKeyframe {
913 protected:
914 TestPacketBufferH264IdrIsKeyframe()
915 : TestPacketBufferH264XIsKeyframe(false) {}
916};
917
918TEST_F(TestPacketBufferH264IdrIsKeyframe, IdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200919 auto& h264_header =
920 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
921 h264_header.nalus[0].type = H264::NaluType::kIdr;
922 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200923 packet_buffer_->InsertPacket(&packet_);
924
925 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100926 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
927 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200928}
929
930TEST_F(TestPacketBufferH264IdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200931 auto& h264_header =
932 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
933 h264_header.nalus[0].type = H264::NaluType::kSps;
934 h264_header.nalus[1].type = H264::NaluType::kPps;
935 h264_header.nalus[2].type = H264::NaluType::kIdr;
936 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200937
938 packet_buffer_->InsertPacket(&packet_);
939
940 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100941 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
942 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200943}
944
945class TestPacketBufferH264SpsPpsIdrIsKeyframe
946 : public TestPacketBufferH264XIsKeyframe {
947 protected:
948 TestPacketBufferH264SpsPpsIdrIsKeyframe()
949 : TestPacketBufferH264XIsKeyframe(true) {}
950};
951
952TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, IdrIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200953 auto& h264_header =
954 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
955 h264_header.nalus[0].type = H264::NaluType::kIdr;
956 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200957
958 packet_buffer_->InsertPacket(&packet_);
959
960 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100961 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
962 frames_from_callback_[5]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200963}
964
965TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200966 auto& h264_header =
967 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
968 h264_header.nalus[0].type = H264::NaluType::kSps;
969 h264_header.nalus[1].type = H264::NaluType::kPps;
970 h264_header.nalus_length = 2;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200971
972 packet_buffer_->InsertPacket(&packet_);
973
974 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100975 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
976 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200977}
978
979TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200980 auto& h264_header =
981 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
982 h264_header.nalus[0].type = H264::NaluType::kSps;
983 h264_header.nalus[1].type = H264::NaluType::kPps;
984 h264_header.nalus[2].type = H264::NaluType::kIdr;
985 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200986
987 packet_buffer_->InsertPacket(&packet_);
988
989 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100990 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
991 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200992}
993
philipelc707ab72016-04-01 02:01:54 -0700994} // namespace video_coding
995} // namespace webrtc