blob: f8d0bb85a5da2b2e3a1c71ddbb9a78e3303aa2eb [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);
Johannes Kronbd3f3052019-08-01 15:45:54 +0200156 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
philipelaee3e0e2016-11-01 11:45:34 +0100157 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) {
Johannes Kronbd3f3052019-08-01 15:45:54 +0200249 Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr,
250 timestamp + 10 * i);
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100251 }
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) {
Johannes Kronbd3f3052019-08-01 15:45:54 +0200256 Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr,
257 timestamp + 10 * i);
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100258 }
259 ASSERT_EQ(kNumFrames, packet_buffer_->GetUniqueFramesSeen());
260
261 // Very old packets should be treated as unique.
Johannes Kronbd3f3052019-08-01 15:45:54 +0200262 Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100263 ASSERT_EQ(kNumFrames + 1, packet_buffer_->GetUniqueFramesSeen());
264}
265
philipelc707ab72016-04-01 02:01:54 -0700266TEST_F(TestPacketBuffer, ExpandBuffer) {
philipelaee3e0e2016-11-01 11:45:34 +0100267 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700268
269 for (int i = 0; i < kStartSize + 1; ++i) {
philipelaee3e0e2016-11-01 11:45:34 +0100270 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700271 }
272}
273
philipelaee3e0e2016-11-01 11:45:34 +0100274TEST_F(TestPacketBuffer, SingleFrameExpandsBuffer) {
275 const uint16_t seq_num = Rand();
276
277 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
278 for (int i = 1; i < kStartSize; ++i)
279 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast));
280 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast));
281
282 ASSERT_EQ(1UL, frames_from_callback_.size());
283 CheckFrame(seq_num);
284}
285
philipelc707ab72016-04-01 02:01:54 -0700286TEST_F(TestPacketBuffer, ExpandBufferOverflow) {
philipelaee3e0e2016-11-01 11:45:34 +0100287 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700288
philipelaee3e0e2016-11-01 11:45:34 +0100289 for (int i = 0; i < kMaxSize; ++i)
290 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast));
Johannes Kronbd3f3052019-08-01 15:45:54 +0200291 EXPECT_FALSE(Insert(seq_num + kMaxSize + 1, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700292}
293
philipel17deeb42016-08-11 15:09:26 +0200294TEST_F(TestPacketBuffer, OnePacketOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100295 const uint16_t seq_num = Rand();
296 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200297 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200298 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700299}
300
philipel17deeb42016-08-11 15:09:26 +0200301TEST_F(TestPacketBuffer, TwoPacketsTwoFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100302 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200303
philipelaee3e0e2016-11-01 11:45:34 +0100304 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
305 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200306
philipelc707ab72016-04-01 02:01:54 -0700307 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200308 CheckFrame(seq_num);
309 CheckFrame(seq_num + 1);
philipelc707ab72016-04-01 02:01:54 -0700310}
311
philipel17deeb42016-08-11 15:09:26 +0200312TEST_F(TestPacketBuffer, TwoPacketsOneFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100313 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200314
philipelaee3e0e2016-11-01 11:45:34 +0100315 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
316 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200317
philipelc707ab72016-04-01 02:01:54 -0700318 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200319 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700320}
321
philipel17deeb42016-08-11 15:09:26 +0200322TEST_F(TestPacketBuffer, ThreePacketReorderingOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100323 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700324
philipelaee3e0e2016-11-01 11:45:34 +0100325 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
326 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kLast));
327 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast));
philipelf4139332016-04-20 10:26:34 +0200328
philipelc707ab72016-04-01 02:01:54 -0700329 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200330 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700331}
332
philipel17deeb42016-08-11 15:09:26 +0200333TEST_F(TestPacketBuffer, Frames) {
philipelaee3e0e2016-11-01 11:45:34 +0100334 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200335
philipelaee3e0e2016-11-01 11:45:34 +0100336 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
337 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
338 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
339 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200340
341 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200342 CheckFrame(seq_num);
343 CheckFrame(seq_num + 1);
344 CheckFrame(seq_num + 2);
345 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200346}
347
philipelaee3e0e2016-11-01 11:45:34 +0100348TEST_F(TestPacketBuffer, ClearSinglePacket) {
349 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200350
philipelaee3e0e2016-11-01 11:45:34 +0100351 for (int i = 0; i < kMaxSize; ++i)
352 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kFirst, kLast));
353
354 packet_buffer_->ClearTo(seq_num);
355 EXPECT_TRUE(Insert(seq_num + kMaxSize, kDeltaFrame, kFirst, kLast));
356}
357
philipelc5fb4682017-08-02 04:28:57 -0700358TEST_F(TestPacketBuffer, ClearFullBuffer) {
359 for (int i = 0; i < kMaxSize; ++i)
360 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
361
362 packet_buffer_->ClearTo(kMaxSize - 1);
363
364 for (int i = kMaxSize; i < 2 * kMaxSize; ++i)
365 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
366}
367
368TEST_F(TestPacketBuffer, DontClearNewerPacket) {
369 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kLast));
370 packet_buffer_->ClearTo(0);
371 EXPECT_TRUE(Insert(2 * kStartSize, kKeyFrame, kFirst, kLast));
372 EXPECT_TRUE(Insert(3 * kStartSize + 1, kKeyFrame, kFirst, kNotLast));
373 packet_buffer_->ClearTo(2 * kStartSize);
374 EXPECT_TRUE(Insert(3 * kStartSize + 2, kKeyFrame, kNotFirst, kLast));
375
376 ASSERT_EQ(3UL, frames_from_callback_.size());
377 CheckFrame(0);
378 CheckFrame(2 * kStartSize);
379 CheckFrame(3 * kStartSize + 1);
380}
381
philipelaee3e0e2016-11-01 11:45:34 +0100382TEST_F(TestPacketBuffer, OneIncompleteFrame) {
383 const uint16_t seq_num = Rand();
384
385 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
386 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kLast));
387 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
388
389 ASSERT_EQ(1UL, frames_from_callback_.size());
390 CheckFrame(seq_num);
391}
392
393TEST_F(TestPacketBuffer, TwoIncompleteFramesFullBuffer) {
394 const uint16_t seq_num = Rand();
395
396 for (int i = 1; i < kMaxSize - 1; ++i)
397 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kNotFirst, kNotLast));
398 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
399 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
400
401 ASSERT_EQ(0UL, frames_from_callback_.size());
402}
403
404TEST_F(TestPacketBuffer, FramesReordered) {
405 const uint16_t seq_num = Rand();
406
407 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
408 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
409 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
410 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200411
412 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200413 CheckFrame(seq_num);
414 CheckFrame(seq_num + 1);
415 CheckFrame(seq_num + 2);
416 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200417}
418
philipel36928452016-11-07 10:42:36 +0100419TEST_F(TestPacketBuffer, GetBitstream) {
philipelc707ab72016-04-01 02:01:54 -0700420 // "many bitstream, such data" with null termination.
philipel41b8ca02016-11-07 15:42:24 +0100421 uint8_t many_data[] = {0x6d, 0x61, 0x6e, 0x79, 0x20};
422 uint8_t bitstream_data[] = {0x62, 0x69, 0x74, 0x73, 0x74, 0x72,
423 0x65, 0x61, 0x6d, 0x2c, 0x20};
424 uint8_t such_data[] = {0x73, 0x75, 0x63, 0x68, 0x20};
425 uint8_t data_data[] = {0x64, 0x61, 0x74, 0x61, 0x0};
426
427 uint8_t* many = new uint8_t[sizeof(many_data)];
428 uint8_t* bitstream = new uint8_t[sizeof(bitstream_data)];
429 uint8_t* such = new uint8_t[sizeof(such_data)];
430 uint8_t* data = new uint8_t[sizeof(data_data)];
431
432 memcpy(many, many_data, sizeof(many_data));
433 memcpy(bitstream, bitstream_data, sizeof(bitstream_data));
434 memcpy(such, such_data, sizeof(such_data));
435 memcpy(data, data_data, sizeof(data_data));
436
Niels Möller648a7ce2018-11-28 15:14:54 +0100437 const size_t result_length = sizeof(many_data) + sizeof(bitstream_data) +
438 sizeof(such_data) + sizeof(data_data);
philipelc707ab72016-04-01 02:01:54 -0700439
philipelaee3e0e2016-11-01 11:45:34 +0100440 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700441
philipelaee3e0e2016-11-01 11:45:34 +0100442 EXPECT_TRUE(
philipel41b8ca02016-11-07 15:42:24 +0100443 Insert(seq_num, kKeyFrame, kFirst, kNotLast, sizeof(many_data), many));
444 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast,
445 sizeof(bitstream_data), bitstream));
446 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kNotLast,
447 sizeof(such_data), such));
448 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kNotFirst, kLast,
449 sizeof(data_data), data));
philipelf4139332016-04-20 10:26:34 +0200450
451 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200452 CheckFrame(seq_num);
Niels Möller648a7ce2018-11-28 15:14:54 +0100453 EXPECT_EQ(frames_from_callback_[seq_num]->size(), result_length);
Niels Möller9c843902019-01-11 10:21:35 +0100454 EXPECT_EQ(memcmp(frames_from_callback_[seq_num]->data(),
Niels Möller648a7ce2018-11-28 15:14:54 +0100455 "many bitstream, such data", result_length),
456 0);
philipelc707ab72016-04-01 02:01:54 -0700457}
458
philipel227f8b92017-08-04 06:39:31 -0700459TEST_F(TestPacketBuffer, GetBitstreamOneFrameOnePacket) {
460 uint8_t bitstream_data[] = "All the bitstream data for this frame!";
philipel227f8b92017-08-04 06:39:31 -0700461 uint8_t* data = new uint8_t[sizeof(bitstream_data)];
462 memcpy(data, bitstream_data, sizeof(bitstream_data));
463
464 EXPECT_TRUE(
465 Insert(0, kKeyFrame, kFirst, kLast, sizeof(bitstream_data), data));
466
467 ASSERT_EQ(1UL, frames_from_callback_.size());
468 CheckFrame(0);
469 EXPECT_EQ(frames_from_callback_[0]->size(), sizeof(bitstream_data));
Niels Möller648a7ce2018-11-28 15:14:54 +0100470 EXPECT_EQ(
Niels Möller9c843902019-01-11 10:21:35 +0100471 memcmp(frames_from_callback_[0]->data(), data, sizeof(bitstream_data)),
Niels Möller648a7ce2018-11-28 15:14:54 +0100472 0);
philipel227f8b92017-08-04 06:39:31 -0700473}
474
475TEST_F(TestPacketBuffer, GetBitstreamOneFrameFullBuffer) {
476 uint8_t* data_arr[kStartSize];
477 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700478
479 for (uint8_t i = 0; i < kStartSize; ++i) {
480 data_arr[i] = new uint8_t[1];
481 data_arr[i][0] = i;
482 expected[i] = i;
483 }
484
485 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kNotLast, 1, data_arr[0]));
486 for (uint8_t i = 1; i < kStartSize - 1; ++i)
487 EXPECT_TRUE(Insert(i, kKeyFrame, kNotFirst, kNotLast, 1, data_arr[i]));
488 EXPECT_TRUE(Insert(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1,
489 data_arr[kStartSize - 1]));
490
491 ASSERT_EQ(1UL, frames_from_callback_.size());
492 CheckFrame(0);
493 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100494 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700495}
496
Johannes Kron957c62e2018-10-01 14:53:01 +0200497TEST_F(TestPacketBuffer, InsertPacketAfterOldFrameObjectIsRemoved) {
498 uint16_t kFirstSeqNum = 0;
499 uint32_t kTimestampDelta = 100;
500 uint32_t timestamp = 10000;
501 uint16_t seq_num = kFirstSeqNum;
502
503 // Loop until seq_num wraps around.
Philip Eliasson1f850a62019-03-19 12:15:00 +0000504 SeqNumUnwrapper<uint16_t> unwrapper;
Johannes Kron957c62e2018-10-01 14:53:01 +0200505 while (unwrapper.Unwrap(seq_num) < std::numeric_limits<uint16_t>::max()) {
506 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
507 for (int i = 0; i < 5; ++i) {
508 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
509 }
510 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
511 timestamp += kTimestampDelta;
512 }
513
514 size_t number_of_frames = frames_from_callback_.size();
515 // Delete old frame object while receiving frame with overlapping sequence
516 // numbers.
517 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
518 for (int i = 0; i < 5; ++i) {
519 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
520 }
521 // Delete FrameObject connected to packets that have already been cleared.
522 DeleteFrame(kFirstSeqNum);
523 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
524
525 // Regardless of the initial size, the number of frames should be constant
526 // after removing and then adding a new frame object.
527 EXPECT_EQ(number_of_frames, frames_from_callback_.size());
528}
529
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100530// If |sps_pps_idr_is_keyframe| is true, we require keyframes to contain
531// SPS/PPS/IDR and the keyframes we create as part of the test do contain
532// SPS/PPS/IDR. If |sps_pps_idr_is_keyframe| is false, we only require and
533// create keyframes containing only IDR.
534class TestPacketBufferH264 : public TestPacketBuffer {
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200535 protected:
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200536 explicit TestPacketBufferH264(bool sps_pps_idr_is_keyframe)
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100537 : TestPacketBuffer(sps_pps_idr_is_keyframe
538 ? "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/"
539 : ""),
540 sps_pps_idr_is_keyframe_(sps_pps_idr_is_keyframe) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200541
542 bool InsertH264(uint16_t seq_num, // packet sequence number
543 IsKeyFrame keyframe, // is keyframe
544 IsFirst first, // is first packet of frame
545 IsLast last, // is last packet of frame
546 uint32_t timestamp, // rtp timestamp
547 int data_size = 0, // size of data
548 uint8_t* data = nullptr) { // data pointer
549 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100550 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200551 auto& h264_header =
552 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200553 packet.seqNum = seq_num;
554 packet.timestamp = timestamp;
555 if (keyframe == kKeyFrame) {
556 if (sps_pps_idr_is_keyframe_) {
philipel7d745e52018-08-02 14:03:53 +0200557 h264_header.nalus[0].type = H264::NaluType::kSps;
558 h264_header.nalus[1].type = H264::NaluType::kPps;
559 h264_header.nalus[2].type = H264::NaluType::kIdr;
560 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200561 } else {
philipel7d745e52018-08-02 14:03:53 +0200562 h264_header.nalus[0].type = H264::NaluType::kIdr;
563 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200564 }
565 }
Niels Möllerd5e02f02019-02-20 13:12:21 +0100566 packet.video_header.is_first_packet_in_frame = first == kFirst;
567 packet.video_header.is_last_packet_in_frame = last == kLast;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200568 packet.sizeBytes = data_size;
569 packet.dataPtr = data;
570
571 return packet_buffer_->InsertPacket(&packet);
572 }
573
574 const bool sps_pps_idr_is_keyframe_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100575};
576
577// This fixture is used to test the general behaviour of the packet buffer
578// in both configurations.
579class TestPacketBufferH264Parameterized
580 : public ::testing::WithParamInterface<bool>,
581 public TestPacketBufferH264 {
582 protected:
583 TestPacketBufferH264Parameterized() : TestPacketBufferH264(GetParam()) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200584};
585
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100586INSTANTIATE_TEST_SUITE_P(SpsPpsIdrIsKeyframe,
587 TestPacketBufferH264Parameterized,
588 ::testing::Values(false, true));
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200589
philipelbc5a4082017-12-06 10:41:08 +0100590TEST_P(TestPacketBufferH264Parameterized, DontRemoveMissingPacketOnClearTo) {
591 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kLast, 0));
592 EXPECT_TRUE(InsertH264(2, kDeltaFrame, kFirst, kNotLast, 2));
593 packet_buffer_->ClearTo(0);
594 EXPECT_TRUE(InsertH264(3, kDeltaFrame, kNotFirst, kLast, 2));
595
596 ASSERT_EQ(1UL, frames_from_callback_.size());
597 CheckFrame(0);
598}
599
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100600TEST_P(TestPacketBufferH264Parameterized, GetBitstreamOneFrameFullBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700601 uint8_t* data_arr[kStartSize];
602 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700603
604 for (uint8_t i = 0; i < kStartSize; ++i) {
605 data_arr[i] = new uint8_t[1];
606 data_arr[i][0] = i;
607 expected[i] = i;
608 }
609
610 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kNotLast, 1, 1, data_arr[0]));
611 for (uint8_t i = 1; i < kStartSize - 1; ++i) {
612 EXPECT_TRUE(
613 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1, 1, data_arr[i]));
614 }
615 EXPECT_TRUE(InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1, 1,
616 data_arr[kStartSize - 1]));
617
618 ASSERT_EQ(1UL, frames_from_callback_.size());
619 CheckFrame(0);
620 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100621 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700622}
623
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100624TEST_P(TestPacketBufferH264Parameterized, GetBitstreamBufferPadding) {
philipel36928452016-11-07 10:42:36 +0100625 uint16_t seq_num = Rand();
philipel41b8ca02016-11-07 15:42:24 +0100626 uint8_t data_data[] = "some plain old data";
627 uint8_t* data = new uint8_t[sizeof(data_data)];
628 memcpy(data, data_data, sizeof(data_data));
philipel36928452016-11-07 10:42:36 +0100629
philipel36928452016-11-07 10:42:36 +0100630 VCMPacket packet;
philipel7d745e52018-08-02 14:03:53 +0200631 auto& h264_header =
632 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
633 h264_header.nalus_length = 1;
634 h264_header.nalus[0].type = H264::NaluType::kIdr;
635 h264_header.packetization_type = kH264SingleNalu;
philipel36928452016-11-07 10:42:36 +0100636 packet.seqNum = seq_num;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100637 packet.video_header.codec = kVideoCodecH264;
philipel36928452016-11-07 10:42:36 +0100638 packet.insertStartCode = true;
philipel36928452016-11-07 10:42:36 +0100639 packet.dataPtr = data;
philipel41b8ca02016-11-07 15:42:24 +0100640 packet.sizeBytes = sizeof(data_data);
Niels Möllerd5e02f02019-02-20 13:12:21 +0100641 packet.video_header.is_first_packet_in_frame = true;
642 packet.video_header.is_last_packet_in_frame = true;
philipel759e0b72016-11-30 01:32:05 -0800643 packet_buffer_->InsertPacket(&packet);
philipel36928452016-11-07 10:42:36 +0100644
645 ASSERT_EQ(1UL, frames_from_callback_.size());
Niels Möller77536a22019-01-15 08:50:01 +0100646 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().size(),
philipel41b8ca02016-11-07 15:42:24 +0100647 sizeof(data_data));
Niels Möller48a79462018-12-07 16:21:18 +0100648 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().capacity(),
Niels Möller009ab3c2019-03-08 11:26:58 +0100649 sizeof(data_data));
Niels Möller648a7ce2018-11-28 15:14:54 +0100650 EXPECT_EQ(
Niels Möller9c843902019-01-11 10:21:35 +0100651 memcmp(frames_from_callback_[seq_num]->data(), data, sizeof(data_data)),
Niels Möller648a7ce2018-11-28 15:14:54 +0100652 0);
philipel36928452016-11-07 10:42:36 +0100653}
654
philipelc707ab72016-04-01 02:01:54 -0700655TEST_F(TestPacketBuffer, FreeSlotsOnFrameDestruction) {
philipelaee3e0e2016-11-01 11:45:34 +0100656 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200657
philipelaee3e0e2016-11-01 11:45:34 +0100658 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
659 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
660 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700661 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200662 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700663
664 frames_from_callback_.clear();
665
philipel17deeb42016-08-11 15:09:26 +0200666 // Insert frame that fills the whole buffer.
philipelaee3e0e2016-11-01 11:45:34 +0100667 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kNotLast));
philipel17deeb42016-08-11 15:09:26 +0200668 for (int i = 0; i < kMaxSize - 2; ++i)
philipelaee3e0e2016-11-01 11:45:34 +0100669 EXPECT_TRUE(Insert(seq_num + i + 4, kDeltaFrame, kNotFirst, kNotLast));
670 EXPECT_TRUE(Insert(seq_num + kMaxSize + 2, kKeyFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700671 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200672 CheckFrame(seq_num + 3);
philipelc707ab72016-04-01 02:01:54 -0700673}
674
philipel02447bc2016-05-13 06:01:03 -0700675TEST_F(TestPacketBuffer, Clear) {
philipelaee3e0e2016-11-01 11:45:34 +0100676 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200677
philipelaee3e0e2016-11-01 11:45:34 +0100678 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
679 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
680 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200681 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200682 CheckFrame(seq_num);
philipelf4139332016-04-20 10:26:34 +0200683
philipel02447bc2016-05-13 06:01:03 -0700684 packet_buffer_->Clear();
philipelf4139332016-04-20 10:26:34 +0200685
philipelaee3e0e2016-11-01 11:45:34 +0100686 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kFirst, kNotLast));
687 EXPECT_TRUE(
688 Insert(seq_num + kStartSize + 1, kDeltaFrame, kNotFirst, kNotLast));
689 EXPECT_TRUE(Insert(seq_num + kStartSize + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700690 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200691 CheckFrame(seq_num + kStartSize);
philipelc707ab72016-04-01 02:01:54 -0700692}
693
philipel20dce342016-11-28 16:14:57 +0100694TEST_F(TestPacketBuffer, FramesAfterClear) {
695 Insert(9025, kDeltaFrame, kFirst, kLast);
696 Insert(9024, kKeyFrame, kFirst, kLast);
697 packet_buffer_->ClearTo(9025);
698 Insert(9057, kDeltaFrame, kFirst, kLast);
699 Insert(9026, kDeltaFrame, kFirst, kLast);
700
701 CheckFrame(9024);
702 CheckFrame(9025);
703 CheckFrame(9026);
704 CheckFrame(9057);
705}
706
philipel8b6995b2019-01-09 12:39:18 +0100707TEST_F(TestPacketBuffer, SameFrameDifferentTimestamps) {
708 Insert(0, kKeyFrame, kFirst, kNotLast, 0, nullptr, 1000);
709 Insert(1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 1001);
710
711 ASSERT_EQ(0UL, frames_from_callback_.size());
712}
713
philipel759e0b72016-11-30 01:32:05 -0800714TEST_F(TestPacketBuffer, DontLeakPayloadData) {
715 // NOTE! Any eventual leak is suppose to be detected by valgrind
716 // or any other similar tool.
717 uint8_t* data1 = new uint8_t[5];
718 uint8_t* data2 = new uint8_t[5];
719 uint8_t* data3 = new uint8_t[5];
720 uint8_t* data4 = new uint8_t[5];
721
722 // Expected to free data1 upon PacketBuffer destruction.
723 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data1));
724
725 // Expect to free data2 upon insertion.
726 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data2));
727
728 // Expect to free data3 upon insertion (old packet).
729 packet_buffer_->ClearTo(1);
Johannes Kronbd3f3052019-08-01 15:45:54 +0200730 EXPECT_TRUE(Insert(1, kKeyFrame, kFirst, kNotLast, 5, data3));
philipel759e0b72016-11-30 01:32:05 -0800731
732 // Expect to free data4 upon insertion (packet buffer is full).
Johannes Kronbd3f3052019-08-01 15:45:54 +0200733 EXPECT_FALSE(Insert(2 + kMaxSize, kKeyFrame, kFirst, kNotLast, 5, data4));
philipel759e0b72016-11-30 01:32:05 -0800734}
735
philipelea142f82017-01-11 02:01:56 -0800736TEST_F(TestPacketBuffer, ContinuousSeqNumDoubleMarkerBit) {
737 Insert(2, kKeyFrame, kNotFirst, kNotLast);
738 Insert(1, kKeyFrame, kFirst, kLast);
739 frames_from_callback_.clear();
740 Insert(3, kKeyFrame, kNotFirst, kLast);
741
742 EXPECT_EQ(0UL, frames_from_callback_.size());
743}
744
philipel3184f8e2017-05-18 08:08:53 -0700745TEST_F(TestPacketBuffer, PacketTimestamps) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200746 absl::optional<int64_t> packet_ms;
747 absl::optional<int64_t> packet_keyframe_ms;
philipel3184f8e2017-05-18 08:08:53 -0700748
749 packet_ms = packet_buffer_->LastReceivedPacketMs();
750 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
751 EXPECT_FALSE(packet_ms);
752 EXPECT_FALSE(packet_keyframe_ms);
753
754 int64_t keyframe_ms = clock_->TimeInMilliseconds();
755 EXPECT_TRUE(Insert(100, kKeyFrame, kFirst, kLast));
756 packet_ms = packet_buffer_->LastReceivedPacketMs();
757 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
758 EXPECT_TRUE(packet_ms);
759 EXPECT_TRUE(packet_keyframe_ms);
760 EXPECT_EQ(keyframe_ms, *packet_ms);
761 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
762
763 clock_->AdvanceTimeMilliseconds(100);
764 int64_t delta_ms = clock_->TimeInMilliseconds();
765 EXPECT_TRUE(Insert(101, kDeltaFrame, kFirst, kLast));
766 packet_ms = packet_buffer_->LastReceivedPacketMs();
767 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
768 EXPECT_TRUE(packet_ms);
769 EXPECT_TRUE(packet_keyframe_ms);
770 EXPECT_EQ(delta_ms, *packet_ms);
771 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
772
773 packet_buffer_->Clear();
774 packet_ms = packet_buffer_->LastReceivedPacketMs();
775 packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs();
776 EXPECT_FALSE(packet_ms);
777 EXPECT_FALSE(packet_keyframe_ms);
778}
779
philipel09133af2018-05-17 14:11:09 +0200780TEST_F(TestPacketBuffer, IncomingCodecChange) {
781 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100782 packet.video_header.is_first_packet_in_frame = true;
783 packet.video_header.is_last_packet_in_frame = true;
philipel09133af2018-05-17 14:11:09 +0200784 packet.sizeBytes = 0;
785 packet.dataPtr = nullptr;
786
Niels Möllerd5e02f02019-02-20 13:12:21 +0100787 packet.video_header.codec = kVideoCodecVP8;
788 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200789 packet.timestamp = 1;
790 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200791 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
philipel09133af2018-05-17 14:11:09 +0200792 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
793
Niels Möllerd5e02f02019-02-20 13:12:21 +0100794 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200795 auto& h264_header =
796 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
797 h264_header.nalus_length = 1;
philipel09133af2018-05-17 14:11:09 +0200798 packet.timestamp = 3;
799 packet.seqNum = 3;
800 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
801
Niels Möllerd5e02f02019-02-20 13:12:21 +0100802 packet.video_header.codec = kVideoCodecVP8;
803 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200804 packet.timestamp = 2;
805 packet.seqNum = 2;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200806 packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
philipel09133af2018-05-17 14:11:09 +0200807
808 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
809
810 EXPECT_EQ(3UL, frames_from_callback_.size());
811}
812
813TEST_F(TestPacketBuffer, TooManyNalusInPacket) {
814 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100815 packet.video_header.codec = kVideoCodecH264;
philipel09133af2018-05-17 14:11:09 +0200816 packet.timestamp = 1;
817 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200818 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100819 packet.video_header.is_first_packet_in_frame = true;
820 packet.video_header.is_last_packet_in_frame = true;
philipel7d745e52018-08-02 14:03:53 +0200821 auto& h264_header =
822 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
823 h264_header.nalus_length = kMaxNalusPerPacket;
philipel09133af2018-05-17 14:11:09 +0200824 packet.sizeBytes = 0;
825 packet.dataPtr = nullptr;
826 EXPECT_TRUE(packet_buffer_->InsertPacket(&packet));
827
828 EXPECT_EQ(0UL, frames_from_callback_.size());
829}
830
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100831TEST_P(TestPacketBufferH264Parameterized, OneFrameFillBuffer) {
philipel2c9f9f22017-06-13 02:47:28 -0700832 InsertH264(0, kKeyFrame, kFirst, kNotLast, 1000);
833 for (int i = 1; i < kStartSize - 1; ++i)
834 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1000);
835 InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1000);
836
837 EXPECT_EQ(1UL, frames_from_callback_.size());
838 CheckFrame(0);
839}
840
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100841TEST_P(TestPacketBufferH264Parameterized, CreateFramesAfterFilledBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700842 InsertH264(kStartSize - 2, kKeyFrame, kFirst, kLast, 0);
843 ASSERT_EQ(1UL, frames_from_callback_.size());
844 frames_from_callback_.clear();
845
846 InsertH264(kStartSize, kDeltaFrame, kFirst, kNotLast, 2000);
847 for (int i = 1; i < kStartSize; ++i)
848 InsertH264(kStartSize + i, kDeltaFrame, kNotFirst, kNotLast, 2000);
849 InsertH264(kStartSize + kStartSize, kDeltaFrame, kNotFirst, kLast, 2000);
850 ASSERT_EQ(0UL, frames_from_callback_.size());
851
852 InsertH264(kStartSize - 1, kKeyFrame, kFirst, kLast, 1000);
853 ASSERT_EQ(2UL, frames_from_callback_.size());
854 CheckFrame(kStartSize - 1);
855 CheckFrame(kStartSize);
856}
857
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100858TEST_P(TestPacketBufferH264Parameterized, OneFrameMaxSeqNum) {
philipel2c9f9f22017-06-13 02:47:28 -0700859 InsertH264(65534, kKeyFrame, kFirst, kNotLast, 1000);
860 InsertH264(65535, kKeyFrame, kNotFirst, kLast, 1000);
861
862 EXPECT_EQ(1UL, frames_from_callback_.size());
863 CheckFrame(65534);
864}
865
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100866TEST_P(TestPacketBufferH264Parameterized, ClearMissingPacketsOnKeyframe) {
philipel2c9f9f22017-06-13 02:47:28 -0700867 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
868 InsertH264(2, kKeyFrame, kFirst, kLast, 3000);
869 InsertH264(3, kDeltaFrame, kFirst, kNotLast, 4000);
870 InsertH264(4, kDeltaFrame, kNotFirst, kLast, 4000);
871
872 ASSERT_EQ(3UL, frames_from_callback_.size());
873
874 InsertH264(kStartSize + 1, kKeyFrame, kFirst, kLast, 18000);
875
876 ASSERT_EQ(4UL, frames_from_callback_.size());
877 CheckFrame(0);
878 CheckFrame(2);
879 CheckFrame(3);
880 CheckFrame(kStartSize + 1);
881}
882
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100883TEST_P(TestPacketBufferH264Parameterized, FindFramesOnPadding) {
philipel2c9f9f22017-06-13 02:47:28 -0700884 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
885 InsertH264(2, kDeltaFrame, kFirst, kLast, 1000);
886
887 ASSERT_EQ(1UL, frames_from_callback_.size());
888 packet_buffer_->PaddingReceived(1);
889 ASSERT_EQ(2UL, frames_from_callback_.size());
890 CheckFrame(0);
891 CheckFrame(2);
892}
893
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200894class TestPacketBufferH264XIsKeyframe : public TestPacketBufferH264 {
895 protected:
896 const uint16_t kSeqNum = 5;
897
898 explicit TestPacketBufferH264XIsKeyframe(bool sps_pps_idr_is_keyframe)
899 : TestPacketBufferH264(sps_pps_idr_is_keyframe) {
Niels Möllerd5e02f02019-02-20 13:12:21 +0100900 packet_.video_header.codec = kVideoCodecH264;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200901 packet_.seqNum = kSeqNum;
902
Niels Möllerd5e02f02019-02-20 13:12:21 +0100903 packet_.video_header.is_first_packet_in_frame = true;
904 packet_.video_header.is_last_packet_in_frame = true;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200905 }
906
907 VCMPacket packet_;
908};
909
910class TestPacketBufferH264IdrIsKeyframe
911 : public TestPacketBufferH264XIsKeyframe {
912 protected:
913 TestPacketBufferH264IdrIsKeyframe()
914 : TestPacketBufferH264XIsKeyframe(false) {}
915};
916
917TEST_F(TestPacketBufferH264IdrIsKeyframe, IdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200918 auto& h264_header =
919 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
920 h264_header.nalus[0].type = H264::NaluType::kIdr;
921 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200922 packet_buffer_->InsertPacket(&packet_);
923
924 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100925 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
926 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200927}
928
929TEST_F(TestPacketBufferH264IdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200930 auto& h264_header =
931 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
932 h264_header.nalus[0].type = H264::NaluType::kSps;
933 h264_header.nalus[1].type = H264::NaluType::kPps;
934 h264_header.nalus[2].type = H264::NaluType::kIdr;
935 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200936
937 packet_buffer_->InsertPacket(&packet_);
938
939 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100940 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
941 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200942}
943
944class TestPacketBufferH264SpsPpsIdrIsKeyframe
945 : public TestPacketBufferH264XIsKeyframe {
946 protected:
947 TestPacketBufferH264SpsPpsIdrIsKeyframe()
948 : TestPacketBufferH264XIsKeyframe(true) {}
949};
950
951TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, IdrIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200952 auto& h264_header =
953 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
954 h264_header.nalus[0].type = H264::NaluType::kIdr;
955 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200956
957 packet_buffer_->InsertPacket(&packet_);
958
959 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100960 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
961 frames_from_callback_[5]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200962}
963
964TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200965 auto& h264_header =
966 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
967 h264_header.nalus[0].type = H264::NaluType::kSps;
968 h264_header.nalus[1].type = H264::NaluType::kPps;
969 h264_header.nalus_length = 2;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200970
971 packet_buffer_->InsertPacket(&packet_);
972
973 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100974 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
975 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200976}
977
978TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200979 auto& h264_header =
980 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
981 h264_header.nalus[0].type = H264::NaluType::kSps;
982 h264_header.nalus[1].type = H264::NaluType::kPps;
983 h264_header.nalus[2].type = H264::NaluType::kIdr;
984 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200985
986 packet_buffer_->InsertPacket(&packet_);
987
988 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100989 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
990 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200991}
992
philipelc707ab72016-04-01 02:01:54 -0700993} // namespace video_coding
994} // namespace webrtc