blob: efe2eccd29b8f5fa66443c2f2096249beb1b1f3e [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)),
Danil Chapovalovf7457e52019-09-20 17:57:15 +020035 packet_buffer_(clock_.get(), kStartSize, kMaxSize, this) {}
philipelc707ab72016-04-01 02:01:54 -070036
philipel17deeb42016-08-11 15:09:26 +020037 uint16_t Rand() { return rand_.Rand<uint16_t>(); }
philipelc707ab72016-04-01 02:01:54 -070038
Elad Alonb4643ad2019-02-22 11:19:50 +010039 void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame) override {
philipel17deeb42016-08-11 15:09:26 +020040 uint16_t first_seq_num = frame->first_seq_num();
41 if (frames_from_callback_.find(first_seq_num) !=
42 frames_from_callback_.end()) {
43 ADD_FAILURE() << "Already received frame with first sequence number "
44 << first_seq_num << ".";
philipelf4139332016-04-20 10:26:34 +020045 return;
46 }
philipel2c9f9f22017-06-13 02:47:28 -070047
philipelf4139332016-04-20 10:26:34 +020048 frames_from_callback_.insert(
philipel17deeb42016-08-11 15:09:26 +020049 std::make_pair(frame->first_seq_num(), std::move(frame)));
philipelc707ab72016-04-01 02:01:54 -070050 }
51
philipel17deeb42016-08-11 15:09:26 +020052 enum IsKeyFrame { kKeyFrame, kDeltaFrame };
53 enum IsFirst { kFirst, kNotFirst };
54 enum IsLast { kLast, kNotLast };
philipelc707ab72016-04-01 02:01:54 -070055
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010056 bool Insert(uint16_t seq_num, // packet sequence number
57 IsKeyFrame keyframe, // is keyframe
58 IsFirst first, // is first packet of frame
59 IsLast last, // is last packet of frame
60 int data_size = 0, // size of data
61 uint8_t* data = nullptr, // data pointer
62 uint32_t timestamp = 123u) { // rtp timestamp
philipelf4139332016-04-20 10:26:34 +020063 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +010064 packet.video_header.codec = kVideoCodecGeneric;
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +010065 packet.timestamp = timestamp;
philipelf4139332016-04-20 10:26:34 +020066 packet.seqNum = seq_num;
Niels Möllerabbc50e2019-04-24 09:41:16 +020067 packet.video_header.frame_type = keyframe == kKeyFrame
68 ? VideoFrameType::kVideoFrameKey
69 : VideoFrameType::kVideoFrameDelta;
Niels Möllerd5e02f02019-02-20 13:12:21 +010070 packet.video_header.is_first_packet_in_frame = first == kFirst;
71 packet.video_header.is_last_packet_in_frame = last == kLast;
philipelf4139332016-04-20 10:26:34 +020072 packet.sizeBytes = data_size;
73 packet.dataPtr = data;
74
Danil Chapovalovf7457e52019-09-20 17:57:15 +020075 return packet_buffer_.InsertPacket(&packet);
philipelf4139332016-04-20 10:26:34 +020076 }
77
philipel17deeb42016-08-11 15:09:26 +020078 void CheckFrame(uint16_t first_seq_num) {
79 auto frame_it = frames_from_callback_.find(first_seq_num);
80 ASSERT_FALSE(frame_it == frames_from_callback_.end())
81 << "Could not find frame with first sequence number " << first_seq_num
82 << ".";
philipelf4139332016-04-20 10:26:34 +020083 }
84
Johannes Kron957c62e2018-10-01 14:53:01 +020085 void DeleteFrame(uint16_t first_seq_num) {
86 auto frame_it = frames_from_callback_.find(first_seq_num);
87 ASSERT_FALSE(frame_it == frames_from_callback_.end())
88 << "Could not find frame with first sequence number " << first_seq_num
89 << ".";
90 frames_from_callback_.erase(frame_it);
91 }
92
philipel227f8b92017-08-04 06:39:31 -070093 static constexpr int kStartSize = 16;
94 static constexpr int kMaxSize = 64;
philipelc707ab72016-04-01 02:01:54 -070095
Rasmus Brandt88f080a2017-11-02 14:28:06 +010096 const test::ScopedFieldTrials scoped_field_trials_;
97
philipelc707ab72016-04-01 02:01:54 -070098 Random rand_;
philipel3184f8e2017-05-18 08:08:53 -070099 std::unique_ptr<SimulatedClock> clock_;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200100 PacketBuffer packet_buffer_;
philipel17deeb42016-08-11 15:09:26 +0200101 std::map<uint16_t, std::unique_ptr<RtpFrameObject>> frames_from_callback_;
philipelc707ab72016-04-01 02:01:54 -0700102};
103
104TEST_F(TestPacketBuffer, InsertOnePacket) {
philipelaee3e0e2016-11-01 11:45:34 +0100105 const uint16_t seq_num = Rand();
106 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700107}
108
109TEST_F(TestPacketBuffer, InsertMultiplePackets) {
philipelaee3e0e2016-11-01 11:45:34 +0100110 const uint16_t seq_num = Rand();
111 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
112 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast));
113 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kFirst, kLast));
114 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700115}
116
117TEST_F(TestPacketBuffer, InsertDuplicatePacket) {
philipelaee3e0e2016-11-01 11:45:34 +0100118 const uint16_t seq_num = Rand();
Johannes Krona3705562019-08-26 16:37:11 +0200119 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
120 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
121 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast));
philipelaee3e0e2016-11-01 11:45:34 +0100122}
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
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200155 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
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200172 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;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200177 packet_buffer_.InsertPacket(&packet);
philipel5ceaaae2016-05-24 10:20:47 +0200178
179 packet.seqNum++;
180 packet.timesNacked = 3;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200181 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;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200186 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
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200212 ASSERT_EQ(0, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100213
214 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, 100));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200215 ASSERT_EQ(1, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100216 // Still the same frame.
217 EXPECT_TRUE(
218 Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200219 ASSERT_EQ(1, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100220
221 // Second frame.
222 EXPECT_TRUE(
223 Insert(seq_num + 2, kKeyFrame, kFirst, kNotLast, 0, nullptr, 200));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200224 ASSERT_EQ(2, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100225 EXPECT_TRUE(
226 Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, 0, nullptr, 200));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200227 ASSERT_EQ(2, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100228
229 // Old packet.
230 EXPECT_TRUE(
231 Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200232 ASSERT_EQ(2, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100233
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));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200239 ASSERT_EQ(3, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100240}
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 }
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200252 ASSERT_EQ(kNumFrames, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100253
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 }
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200259 ASSERT_EQ(kNumFrames, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100260
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);
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200263 ASSERT_EQ(kNumFrames + 1, packet_buffer_.GetUniqueFramesSeen());
Ilya Nikolaevskiyd397a0d2018-02-21 15:57:09 +0100264}
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
Johannes Krona3705562019-08-26 16:37:11 +0200269 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
270 for (int i = 1; i < kStartSize; ++i)
271 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast));
272
273 // Already inserted kStartSize number of packets, inserting the last packet
274 // should increase the buffer size and also result in an assembled frame.
275 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700276}
277
philipelaee3e0e2016-11-01 11:45:34 +0100278TEST_F(TestPacketBuffer, SingleFrameExpandsBuffer) {
279 const uint16_t seq_num = Rand();
280
281 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
282 for (int i = 1; i < kStartSize; ++i)
283 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast));
284 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast));
285
286 ASSERT_EQ(1UL, frames_from_callback_.size());
287 CheckFrame(seq_num);
288}
289
philipelc707ab72016-04-01 02:01:54 -0700290TEST_F(TestPacketBuffer, ExpandBufferOverflow) {
philipelaee3e0e2016-11-01 11:45:34 +0100291 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700292
Johannes Krona3705562019-08-26 16:37:11 +0200293 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
294 for (int i = 1; i < kMaxSize; ++i)
295 EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast));
296
297 // Already inserted kMaxSize number of packets, inserting the last packet
298 // should overflow the buffer and result in false being returned.
299 EXPECT_FALSE(Insert(seq_num + kMaxSize, kKeyFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700300}
301
philipel17deeb42016-08-11 15:09:26 +0200302TEST_F(TestPacketBuffer, OnePacketOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100303 const uint16_t seq_num = Rand();
304 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200305 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200306 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700307}
308
philipel17deeb42016-08-11 15:09:26 +0200309TEST_F(TestPacketBuffer, TwoPacketsTwoFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100310 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200311
philipelaee3e0e2016-11-01 11:45:34 +0100312 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
313 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200314
philipelc707ab72016-04-01 02:01:54 -0700315 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200316 CheckFrame(seq_num);
317 CheckFrame(seq_num + 1);
philipelc707ab72016-04-01 02:01:54 -0700318}
319
philipel17deeb42016-08-11 15:09:26 +0200320TEST_F(TestPacketBuffer, TwoPacketsOneFrames) {
philipelaee3e0e2016-11-01 11:45:34 +0100321 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200322
philipelaee3e0e2016-11-01 11:45:34 +0100323 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
324 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200325
philipelc707ab72016-04-01 02:01:54 -0700326 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200327 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700328}
329
philipel17deeb42016-08-11 15:09:26 +0200330TEST_F(TestPacketBuffer, ThreePacketReorderingOneFrame) {
philipelaee3e0e2016-11-01 11:45:34 +0100331 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700332
philipelaee3e0e2016-11-01 11:45:34 +0100333 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
334 EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kLast));
335 EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast));
philipelf4139332016-04-20 10:26:34 +0200336
philipelc707ab72016-04-01 02:01:54 -0700337 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200338 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700339}
340
philipel17deeb42016-08-11 15:09:26 +0200341TEST_F(TestPacketBuffer, Frames) {
philipelaee3e0e2016-11-01 11:45:34 +0100342 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200343
philipelaee3e0e2016-11-01 11:45:34 +0100344 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
345 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
346 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
347 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200348
349 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200350 CheckFrame(seq_num);
351 CheckFrame(seq_num + 1);
352 CheckFrame(seq_num + 2);
353 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200354}
355
philipelaee3e0e2016-11-01 11:45:34 +0100356TEST_F(TestPacketBuffer, ClearSinglePacket) {
357 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200358
philipelaee3e0e2016-11-01 11:45:34 +0100359 for (int i = 0; i < kMaxSize; ++i)
360 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kFirst, kLast));
361
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200362 packet_buffer_.ClearTo(seq_num);
philipelaee3e0e2016-11-01 11:45:34 +0100363 EXPECT_TRUE(Insert(seq_num + kMaxSize, kDeltaFrame, kFirst, kLast));
364}
365
philipelc5fb4682017-08-02 04:28:57 -0700366TEST_F(TestPacketBuffer, ClearFullBuffer) {
367 for (int i = 0; i < kMaxSize; ++i)
368 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
369
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200370 packet_buffer_.ClearTo(kMaxSize - 1);
philipelc5fb4682017-08-02 04:28:57 -0700371
372 for (int i = kMaxSize; i < 2 * kMaxSize; ++i)
373 EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast));
374}
375
376TEST_F(TestPacketBuffer, DontClearNewerPacket) {
377 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kLast));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200378 packet_buffer_.ClearTo(0);
philipelc5fb4682017-08-02 04:28:57 -0700379 EXPECT_TRUE(Insert(2 * kStartSize, kKeyFrame, kFirst, kLast));
380 EXPECT_TRUE(Insert(3 * kStartSize + 1, kKeyFrame, kFirst, kNotLast));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200381 packet_buffer_.ClearTo(2 * kStartSize);
philipelc5fb4682017-08-02 04:28:57 -0700382 EXPECT_TRUE(Insert(3 * kStartSize + 2, kKeyFrame, kNotFirst, kLast));
383
384 ASSERT_EQ(3UL, frames_from_callback_.size());
385 CheckFrame(0);
386 CheckFrame(2 * kStartSize);
387 CheckFrame(3 * kStartSize + 1);
388}
389
philipelaee3e0e2016-11-01 11:45:34 +0100390TEST_F(TestPacketBuffer, OneIncompleteFrame) {
391 const uint16_t seq_num = Rand();
392
393 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
394 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kLast));
395 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
396
397 ASSERT_EQ(1UL, frames_from_callback_.size());
398 CheckFrame(seq_num);
399}
400
401TEST_F(TestPacketBuffer, TwoIncompleteFramesFullBuffer) {
402 const uint16_t seq_num = Rand();
403
404 for (int i = 1; i < kMaxSize - 1; ++i)
405 EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kNotFirst, kNotLast));
406 EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast));
407 EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast));
408
409 ASSERT_EQ(0UL, frames_from_callback_.size());
410}
411
412TEST_F(TestPacketBuffer, FramesReordered) {
413 const uint16_t seq_num = Rand();
414
415 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast));
416 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast));
417 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast));
418 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200419
420 ASSERT_EQ(4UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200421 CheckFrame(seq_num);
422 CheckFrame(seq_num + 1);
423 CheckFrame(seq_num + 2);
424 CheckFrame(seq_num + 3);
philipelf4139332016-04-20 10:26:34 +0200425}
426
philipel36928452016-11-07 10:42:36 +0100427TEST_F(TestPacketBuffer, GetBitstream) {
philipelc707ab72016-04-01 02:01:54 -0700428 // "many bitstream, such data" with null termination.
philipel41b8ca02016-11-07 15:42:24 +0100429 uint8_t many_data[] = {0x6d, 0x61, 0x6e, 0x79, 0x20};
430 uint8_t bitstream_data[] = {0x62, 0x69, 0x74, 0x73, 0x74, 0x72,
431 0x65, 0x61, 0x6d, 0x2c, 0x20};
432 uint8_t such_data[] = {0x73, 0x75, 0x63, 0x68, 0x20};
433 uint8_t data_data[] = {0x64, 0x61, 0x74, 0x61, 0x0};
434
435 uint8_t* many = new uint8_t[sizeof(many_data)];
436 uint8_t* bitstream = new uint8_t[sizeof(bitstream_data)];
437 uint8_t* such = new uint8_t[sizeof(such_data)];
438 uint8_t* data = new uint8_t[sizeof(data_data)];
439
440 memcpy(many, many_data, sizeof(many_data));
441 memcpy(bitstream, bitstream_data, sizeof(bitstream_data));
442 memcpy(such, such_data, sizeof(such_data));
443 memcpy(data, data_data, sizeof(data_data));
444
Niels Möller648a7ce2018-11-28 15:14:54 +0100445 const size_t result_length = sizeof(many_data) + sizeof(bitstream_data) +
446 sizeof(such_data) + sizeof(data_data);
philipelc707ab72016-04-01 02:01:54 -0700447
philipelaee3e0e2016-11-01 11:45:34 +0100448 const uint16_t seq_num = Rand();
philipelc707ab72016-04-01 02:01:54 -0700449
philipelaee3e0e2016-11-01 11:45:34 +0100450 EXPECT_TRUE(
philipel41b8ca02016-11-07 15:42:24 +0100451 Insert(seq_num, kKeyFrame, kFirst, kNotLast, sizeof(many_data), many));
452 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast,
453 sizeof(bitstream_data), bitstream));
454 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kNotLast,
455 sizeof(such_data), such));
456 EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kNotFirst, kLast,
457 sizeof(data_data), data));
philipelf4139332016-04-20 10:26:34 +0200458
459 ASSERT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200460 CheckFrame(seq_num);
Niels Möller648a7ce2018-11-28 15:14:54 +0100461 EXPECT_EQ(frames_from_callback_[seq_num]->size(), result_length);
Niels Möller9c843902019-01-11 10:21:35 +0100462 EXPECT_EQ(memcmp(frames_from_callback_[seq_num]->data(),
Niels Möller648a7ce2018-11-28 15:14:54 +0100463 "many bitstream, such data", result_length),
464 0);
philipelc707ab72016-04-01 02:01:54 -0700465}
466
philipel227f8b92017-08-04 06:39:31 -0700467TEST_F(TestPacketBuffer, GetBitstreamOneFrameOnePacket) {
468 uint8_t bitstream_data[] = "All the bitstream data for this frame!";
philipel227f8b92017-08-04 06:39:31 -0700469 uint8_t* data = new uint8_t[sizeof(bitstream_data)];
470 memcpy(data, bitstream_data, sizeof(bitstream_data));
471
472 EXPECT_TRUE(
473 Insert(0, kKeyFrame, kFirst, kLast, sizeof(bitstream_data), data));
474
475 ASSERT_EQ(1UL, frames_from_callback_.size());
476 CheckFrame(0);
477 EXPECT_EQ(frames_from_callback_[0]->size(), sizeof(bitstream_data));
Johannes Krona3705562019-08-26 16:37:11 +0200478 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), bitstream_data,
479 sizeof(bitstream_data)),
480 0);
philipel227f8b92017-08-04 06:39:31 -0700481}
482
483TEST_F(TestPacketBuffer, GetBitstreamOneFrameFullBuffer) {
484 uint8_t* data_arr[kStartSize];
485 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700486
487 for (uint8_t i = 0; i < kStartSize; ++i) {
488 data_arr[i] = new uint8_t[1];
489 data_arr[i][0] = i;
490 expected[i] = i;
491 }
492
493 EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kNotLast, 1, data_arr[0]));
494 for (uint8_t i = 1; i < kStartSize - 1; ++i)
495 EXPECT_TRUE(Insert(i, kKeyFrame, kNotFirst, kNotLast, 1, data_arr[i]));
496 EXPECT_TRUE(Insert(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1,
497 data_arr[kStartSize - 1]));
498
499 ASSERT_EQ(1UL, frames_from_callback_.size());
500 CheckFrame(0);
501 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100502 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700503}
504
Johannes Kron957c62e2018-10-01 14:53:01 +0200505TEST_F(TestPacketBuffer, InsertPacketAfterOldFrameObjectIsRemoved) {
506 uint16_t kFirstSeqNum = 0;
507 uint32_t kTimestampDelta = 100;
508 uint32_t timestamp = 10000;
509 uint16_t seq_num = kFirstSeqNum;
510
511 // Loop until seq_num wraps around.
Philip Eliasson1f850a62019-03-19 12:15:00 +0000512 SeqNumUnwrapper<uint16_t> unwrapper;
Johannes Kron957c62e2018-10-01 14:53:01 +0200513 while (unwrapper.Unwrap(seq_num) < std::numeric_limits<uint16_t>::max()) {
514 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
515 for (int i = 0; i < 5; ++i) {
516 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
517 }
518 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
519 timestamp += kTimestampDelta;
520 }
521
522 size_t number_of_frames = frames_from_callback_.size();
523 // Delete old frame object while receiving frame with overlapping sequence
524 // numbers.
525 Insert(seq_num++, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp);
526 for (int i = 0; i < 5; ++i) {
527 Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, 0, nullptr, timestamp);
528 }
529 // Delete FrameObject connected to packets that have already been cleared.
530 DeleteFrame(kFirstSeqNum);
531 Insert(seq_num++, kKeyFrame, kNotFirst, kLast, 0, nullptr, timestamp);
532
533 // Regardless of the initial size, the number of frames should be constant
534 // after removing and then adding a new frame object.
535 EXPECT_EQ(number_of_frames, frames_from_callback_.size());
536}
537
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100538// If |sps_pps_idr_is_keyframe| is true, we require keyframes to contain
539// SPS/PPS/IDR and the keyframes we create as part of the test do contain
540// SPS/PPS/IDR. If |sps_pps_idr_is_keyframe| is false, we only require and
541// create keyframes containing only IDR.
542class TestPacketBufferH264 : public TestPacketBuffer {
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200543 protected:
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200544 explicit TestPacketBufferH264(bool sps_pps_idr_is_keyframe)
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100545 : TestPacketBuffer(sps_pps_idr_is_keyframe
546 ? "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/"
547 : ""),
548 sps_pps_idr_is_keyframe_(sps_pps_idr_is_keyframe) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200549
550 bool InsertH264(uint16_t seq_num, // packet sequence number
551 IsKeyFrame keyframe, // is keyframe
552 IsFirst first, // is first packet of frame
553 IsLast last, // is last packet of frame
554 uint32_t timestamp, // rtp timestamp
555 int data_size = 0, // size of data
556 uint8_t* data = nullptr) { // data pointer
557 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100558 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200559 auto& h264_header =
560 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200561 packet.seqNum = seq_num;
562 packet.timestamp = timestamp;
563 if (keyframe == kKeyFrame) {
564 if (sps_pps_idr_is_keyframe_) {
philipel7d745e52018-08-02 14:03:53 +0200565 h264_header.nalus[0].type = H264::NaluType::kSps;
566 h264_header.nalus[1].type = H264::NaluType::kPps;
567 h264_header.nalus[2].type = H264::NaluType::kIdr;
568 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200569 } else {
philipel7d745e52018-08-02 14:03:53 +0200570 h264_header.nalus[0].type = H264::NaluType::kIdr;
571 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200572 }
573 }
Niels Möllerd5e02f02019-02-20 13:12:21 +0100574 packet.video_header.is_first_packet_in_frame = first == kFirst;
575 packet.video_header.is_last_packet_in_frame = last == kLast;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200576 packet.sizeBytes = data_size;
577 packet.dataPtr = data;
578
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200579 return packet_buffer_.InsertPacket(&packet);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200580 }
581
582 const bool sps_pps_idr_is_keyframe_;
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100583};
584
585// This fixture is used to test the general behaviour of the packet buffer
586// in both configurations.
587class TestPacketBufferH264Parameterized
588 : public ::testing::WithParamInterface<bool>,
589 public TestPacketBufferH264 {
590 protected:
591 TestPacketBufferH264Parameterized() : TestPacketBufferH264(GetParam()) {}
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200592};
593
Mirko Bonadeic84f6612019-01-31 12:20:57 +0100594INSTANTIATE_TEST_SUITE_P(SpsPpsIdrIsKeyframe,
595 TestPacketBufferH264Parameterized,
596 ::testing::Values(false, true));
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200597
philipelbc5a4082017-12-06 10:41:08 +0100598TEST_P(TestPacketBufferH264Parameterized, DontRemoveMissingPacketOnClearTo) {
599 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kLast, 0));
600 EXPECT_TRUE(InsertH264(2, kDeltaFrame, kFirst, kNotLast, 2));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200601 packet_buffer_.ClearTo(0);
philipelbc5a4082017-12-06 10:41:08 +0100602 EXPECT_TRUE(InsertH264(3, kDeltaFrame, kNotFirst, kLast, 2));
603
604 ASSERT_EQ(1UL, frames_from_callback_.size());
605 CheckFrame(0);
606}
607
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100608TEST_P(TestPacketBufferH264Parameterized, GetBitstreamOneFrameFullBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700609 uint8_t* data_arr[kStartSize];
610 uint8_t expected[kStartSize];
philipel227f8b92017-08-04 06:39:31 -0700611
612 for (uint8_t i = 0; i < kStartSize; ++i) {
613 data_arr[i] = new uint8_t[1];
614 data_arr[i][0] = i;
615 expected[i] = i;
616 }
617
618 EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kNotLast, 1, 1, data_arr[0]));
619 for (uint8_t i = 1; i < kStartSize - 1; ++i) {
620 EXPECT_TRUE(
621 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1, 1, data_arr[i]));
622 }
623 EXPECT_TRUE(InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1, 1,
624 data_arr[kStartSize - 1]));
625
626 ASSERT_EQ(1UL, frames_from_callback_.size());
627 CheckFrame(0);
628 EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize));
Niels Möller9c843902019-01-11 10:21:35 +0100629 EXPECT_EQ(memcmp(frames_from_callback_[0]->data(), expected, kStartSize), 0);
philipel227f8b92017-08-04 06:39:31 -0700630}
631
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100632TEST_P(TestPacketBufferH264Parameterized, GetBitstreamBufferPadding) {
philipel36928452016-11-07 10:42:36 +0100633 uint16_t seq_num = Rand();
philipel41b8ca02016-11-07 15:42:24 +0100634 uint8_t data_data[] = "some plain old data";
635 uint8_t* data = new uint8_t[sizeof(data_data)];
636 memcpy(data, data_data, sizeof(data_data));
philipel36928452016-11-07 10:42:36 +0100637
philipel36928452016-11-07 10:42:36 +0100638 VCMPacket packet;
philipel7d745e52018-08-02 14:03:53 +0200639 auto& h264_header =
640 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
641 h264_header.nalus_length = 1;
642 h264_header.nalus[0].type = H264::NaluType::kIdr;
643 h264_header.packetization_type = kH264SingleNalu;
philipel36928452016-11-07 10:42:36 +0100644 packet.seqNum = seq_num;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100645 packet.video_header.codec = kVideoCodecH264;
philipel36928452016-11-07 10:42:36 +0100646 packet.insertStartCode = true;
philipel36928452016-11-07 10:42:36 +0100647 packet.dataPtr = data;
philipel41b8ca02016-11-07 15:42:24 +0100648 packet.sizeBytes = sizeof(data_data);
Niels Möllerd5e02f02019-02-20 13:12:21 +0100649 packet.video_header.is_first_packet_in_frame = true;
650 packet.video_header.is_last_packet_in_frame = true;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200651 packet_buffer_.InsertPacket(&packet);
philipel36928452016-11-07 10:42:36 +0100652
653 ASSERT_EQ(1UL, frames_from_callback_.size());
Niels Möller77536a22019-01-15 08:50:01 +0100654 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().size(),
philipel41b8ca02016-11-07 15:42:24 +0100655 sizeof(data_data));
Niels Möller48a79462018-12-07 16:21:18 +0100656 EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage().capacity(),
Niels Möller009ab3c2019-03-08 11:26:58 +0100657 sizeof(data_data));
Johannes Krona3705562019-08-26 16:37:11 +0200658 EXPECT_EQ(memcmp(frames_from_callback_[seq_num]->data(), data_data,
659 sizeof(data_data)),
660 0);
philipel36928452016-11-07 10:42:36 +0100661}
662
Johannes Krona3705562019-08-26 16:37:11 +0200663TEST_F(TestPacketBuffer, FreeSlotsOnFrameCreation) {
philipelaee3e0e2016-11-01 11:45:34 +0100664 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200665
philipelaee3e0e2016-11-01 11:45:34 +0100666 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
667 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
668 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700669 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200670 CheckFrame(seq_num);
philipelc707ab72016-04-01 02:01:54 -0700671
philipel17deeb42016-08-11 15:09:26 +0200672 // Insert frame that fills the whole buffer.
philipelaee3e0e2016-11-01 11:45:34 +0100673 EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kNotLast));
philipel17deeb42016-08-11 15:09:26 +0200674 for (int i = 0; i < kMaxSize - 2; ++i)
philipelaee3e0e2016-11-01 11:45:34 +0100675 EXPECT_TRUE(Insert(seq_num + i + 4, kDeltaFrame, kNotFirst, kNotLast));
676 EXPECT_TRUE(Insert(seq_num + kMaxSize + 2, kKeyFrame, kNotFirst, kLast));
Johannes Krona3705562019-08-26 16:37:11 +0200677 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200678 CheckFrame(seq_num + 3);
Johannes Krona3705562019-08-26 16:37:11 +0200679
680 frames_from_callback_.clear();
philipelc707ab72016-04-01 02:01:54 -0700681}
682
philipel02447bc2016-05-13 06:01:03 -0700683TEST_F(TestPacketBuffer, Clear) {
philipelaee3e0e2016-11-01 11:45:34 +0100684 const uint16_t seq_num = Rand();
philipelf4139332016-04-20 10:26:34 +0200685
philipelaee3e0e2016-11-01 11:45:34 +0100686 EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast));
687 EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast));
688 EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast));
philipelf4139332016-04-20 10:26:34 +0200689 EXPECT_EQ(1UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200690 CheckFrame(seq_num);
philipelf4139332016-04-20 10:26:34 +0200691
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200692 packet_buffer_.Clear();
philipelf4139332016-04-20 10:26:34 +0200693
philipelaee3e0e2016-11-01 11:45:34 +0100694 EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kFirst, kNotLast));
695 EXPECT_TRUE(
696 Insert(seq_num + kStartSize + 1, kDeltaFrame, kNotFirst, kNotLast));
697 EXPECT_TRUE(Insert(seq_num + kStartSize + 2, kDeltaFrame, kNotFirst, kLast));
philipelc707ab72016-04-01 02:01:54 -0700698 EXPECT_EQ(2UL, frames_from_callback_.size());
philipel17deeb42016-08-11 15:09:26 +0200699 CheckFrame(seq_num + kStartSize);
philipelc707ab72016-04-01 02:01:54 -0700700}
701
philipel20dce342016-11-28 16:14:57 +0100702TEST_F(TestPacketBuffer, FramesAfterClear) {
703 Insert(9025, kDeltaFrame, kFirst, kLast);
704 Insert(9024, kKeyFrame, kFirst, kLast);
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200705 packet_buffer_.ClearTo(9025);
philipel20dce342016-11-28 16:14:57 +0100706 Insert(9057, kDeltaFrame, kFirst, kLast);
707 Insert(9026, kDeltaFrame, kFirst, kLast);
708
709 CheckFrame(9024);
710 CheckFrame(9025);
711 CheckFrame(9026);
712 CheckFrame(9057);
713}
714
philipel8b6995b2019-01-09 12:39:18 +0100715TEST_F(TestPacketBuffer, SameFrameDifferentTimestamps) {
716 Insert(0, kKeyFrame, kFirst, kNotLast, 0, nullptr, 1000);
717 Insert(1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 1001);
718
719 ASSERT_EQ(0UL, frames_from_callback_.size());
720}
721
philipel759e0b72016-11-30 01:32:05 -0800722TEST_F(TestPacketBuffer, DontLeakPayloadData) {
723 // NOTE! Any eventual leak is suppose to be detected by valgrind
724 // or any other similar tool.
725 uint8_t* data1 = new uint8_t[5];
726 uint8_t* data2 = new uint8_t[5];
727 uint8_t* data3 = new uint8_t[5];
728 uint8_t* data4 = new uint8_t[5];
729
730 // Expected to free data1 upon PacketBuffer destruction.
731 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data1));
732
733 // Expect to free data2 upon insertion.
734 EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data2));
735
736 // Expect to free data3 upon insertion (old packet).
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200737 packet_buffer_.ClearTo(1);
Johannes Kronbd3f3052019-08-01 15:45:54 +0200738 EXPECT_TRUE(Insert(1, kKeyFrame, kFirst, kNotLast, 5, data3));
philipel759e0b72016-11-30 01:32:05 -0800739
740 // Expect to free data4 upon insertion (packet buffer is full).
Johannes Kronbd3f3052019-08-01 15:45:54 +0200741 EXPECT_FALSE(Insert(2 + kMaxSize, kKeyFrame, kFirst, kNotLast, 5, data4));
philipel759e0b72016-11-30 01:32:05 -0800742}
743
philipelea142f82017-01-11 02:01:56 -0800744TEST_F(TestPacketBuffer, ContinuousSeqNumDoubleMarkerBit) {
745 Insert(2, kKeyFrame, kNotFirst, kNotLast);
746 Insert(1, kKeyFrame, kFirst, kLast);
747 frames_from_callback_.clear();
748 Insert(3, kKeyFrame, kNotFirst, kLast);
749
750 EXPECT_EQ(0UL, frames_from_callback_.size());
751}
752
philipel3184f8e2017-05-18 08:08:53 -0700753TEST_F(TestPacketBuffer, PacketTimestamps) {
Danil Chapovalov0040b662018-06-18 10:48:16 +0200754 absl::optional<int64_t> packet_ms;
755 absl::optional<int64_t> packet_keyframe_ms;
philipel3184f8e2017-05-18 08:08:53 -0700756
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200757 packet_ms = packet_buffer_.LastReceivedPacketMs();
758 packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700759 EXPECT_FALSE(packet_ms);
760 EXPECT_FALSE(packet_keyframe_ms);
761
762 int64_t keyframe_ms = clock_->TimeInMilliseconds();
763 EXPECT_TRUE(Insert(100, kKeyFrame, kFirst, kLast));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200764 packet_ms = packet_buffer_.LastReceivedPacketMs();
765 packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700766 EXPECT_TRUE(packet_ms);
767 EXPECT_TRUE(packet_keyframe_ms);
768 EXPECT_EQ(keyframe_ms, *packet_ms);
769 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
770
771 clock_->AdvanceTimeMilliseconds(100);
772 int64_t delta_ms = clock_->TimeInMilliseconds();
773 EXPECT_TRUE(Insert(101, kDeltaFrame, kFirst, kLast));
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200774 packet_ms = packet_buffer_.LastReceivedPacketMs();
775 packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700776 EXPECT_TRUE(packet_ms);
777 EXPECT_TRUE(packet_keyframe_ms);
778 EXPECT_EQ(delta_ms, *packet_ms);
779 EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
780
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200781 packet_buffer_.Clear();
782 packet_ms = packet_buffer_.LastReceivedPacketMs();
783 packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
philipel3184f8e2017-05-18 08:08:53 -0700784 EXPECT_FALSE(packet_ms);
785 EXPECT_FALSE(packet_keyframe_ms);
786}
787
philipel09133af2018-05-17 14:11:09 +0200788TEST_F(TestPacketBuffer, IncomingCodecChange) {
789 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100790 packet.video_header.is_first_packet_in_frame = true;
791 packet.video_header.is_last_packet_in_frame = true;
philipel09133af2018-05-17 14:11:09 +0200792 packet.sizeBytes = 0;
793 packet.dataPtr = nullptr;
794
Niels Möllerd5e02f02019-02-20 13:12:21 +0100795 packet.video_header.codec = kVideoCodecVP8;
796 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200797 packet.timestamp = 1;
798 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200799 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200800 EXPECT_TRUE(packet_buffer_.InsertPacket(&packet));
philipel09133af2018-05-17 14:11:09 +0200801
Niels Möllerd5e02f02019-02-20 13:12:21 +0100802 packet.video_header.codec = kVideoCodecH264;
philipel7d745e52018-08-02 14:03:53 +0200803 auto& h264_header =
804 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
805 h264_header.nalus_length = 1;
philipel09133af2018-05-17 14:11:09 +0200806 packet.timestamp = 3;
807 packet.seqNum = 3;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200808 EXPECT_TRUE(packet_buffer_.InsertPacket(&packet));
philipel09133af2018-05-17 14:11:09 +0200809
Niels Möllerd5e02f02019-02-20 13:12:21 +0100810 packet.video_header.codec = kVideoCodecVP8;
811 packet.video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
philipel09133af2018-05-17 14:11:09 +0200812 packet.timestamp = 2;
813 packet.seqNum = 2;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200814 packet.video_header.frame_type = VideoFrameType::kVideoFrameDelta;
philipel09133af2018-05-17 14:11:09 +0200815
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200816 EXPECT_TRUE(packet_buffer_.InsertPacket(&packet));
philipel09133af2018-05-17 14:11:09 +0200817
818 EXPECT_EQ(3UL, frames_from_callback_.size());
819}
820
821TEST_F(TestPacketBuffer, TooManyNalusInPacket) {
822 VCMPacket packet;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100823 packet.video_header.codec = kVideoCodecH264;
philipel09133af2018-05-17 14:11:09 +0200824 packet.timestamp = 1;
825 packet.seqNum = 1;
Niels Möllerabbc50e2019-04-24 09:41:16 +0200826 packet.video_header.frame_type = VideoFrameType::kVideoFrameKey;
Niels Möllerd5e02f02019-02-20 13:12:21 +0100827 packet.video_header.is_first_packet_in_frame = true;
828 packet.video_header.is_last_packet_in_frame = true;
philipel7d745e52018-08-02 14:03:53 +0200829 auto& h264_header =
830 packet.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
831 h264_header.nalus_length = kMaxNalusPerPacket;
philipel09133af2018-05-17 14:11:09 +0200832 packet.sizeBytes = 0;
833 packet.dataPtr = nullptr;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200834 EXPECT_TRUE(packet_buffer_.InsertPacket(&packet));
philipel09133af2018-05-17 14:11:09 +0200835
836 EXPECT_EQ(0UL, frames_from_callback_.size());
837}
838
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100839TEST_P(TestPacketBufferH264Parameterized, OneFrameFillBuffer) {
philipel2c9f9f22017-06-13 02:47:28 -0700840 InsertH264(0, kKeyFrame, kFirst, kNotLast, 1000);
841 for (int i = 1; i < kStartSize - 1; ++i)
842 InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1000);
843 InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1000);
844
845 EXPECT_EQ(1UL, frames_from_callback_.size());
846 CheckFrame(0);
847}
848
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100849TEST_P(TestPacketBufferH264Parameterized, CreateFramesAfterFilledBuffer) {
philipel227f8b92017-08-04 06:39:31 -0700850 InsertH264(kStartSize - 2, kKeyFrame, kFirst, kLast, 0);
851 ASSERT_EQ(1UL, frames_from_callback_.size());
852 frames_from_callback_.clear();
853
854 InsertH264(kStartSize, kDeltaFrame, kFirst, kNotLast, 2000);
855 for (int i = 1; i < kStartSize; ++i)
856 InsertH264(kStartSize + i, kDeltaFrame, kNotFirst, kNotLast, 2000);
857 InsertH264(kStartSize + kStartSize, kDeltaFrame, kNotFirst, kLast, 2000);
858 ASSERT_EQ(0UL, frames_from_callback_.size());
859
860 InsertH264(kStartSize - 1, kKeyFrame, kFirst, kLast, 1000);
861 ASSERT_EQ(2UL, frames_from_callback_.size());
862 CheckFrame(kStartSize - 1);
863 CheckFrame(kStartSize);
864}
865
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100866TEST_P(TestPacketBufferH264Parameterized, OneFrameMaxSeqNum) {
philipel2c9f9f22017-06-13 02:47:28 -0700867 InsertH264(65534, kKeyFrame, kFirst, kNotLast, 1000);
868 InsertH264(65535, kKeyFrame, kNotFirst, kLast, 1000);
869
870 EXPECT_EQ(1UL, frames_from_callback_.size());
871 CheckFrame(65534);
872}
873
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100874TEST_P(TestPacketBufferH264Parameterized, ClearMissingPacketsOnKeyframe) {
philipel2c9f9f22017-06-13 02:47:28 -0700875 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
876 InsertH264(2, kKeyFrame, kFirst, kLast, 3000);
877 InsertH264(3, kDeltaFrame, kFirst, kNotLast, 4000);
878 InsertH264(4, kDeltaFrame, kNotFirst, kLast, 4000);
879
880 ASSERT_EQ(3UL, frames_from_callback_.size());
881
882 InsertH264(kStartSize + 1, kKeyFrame, kFirst, kLast, 18000);
883
884 ASSERT_EQ(4UL, frames_from_callback_.size());
885 CheckFrame(0);
886 CheckFrame(2);
887 CheckFrame(3);
888 CheckFrame(kStartSize + 1);
889}
890
Rasmus Brandt88f080a2017-11-02 14:28:06 +0100891TEST_P(TestPacketBufferH264Parameterized, FindFramesOnPadding) {
philipel2c9f9f22017-06-13 02:47:28 -0700892 InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
893 InsertH264(2, kDeltaFrame, kFirst, kLast, 1000);
894
895 ASSERT_EQ(1UL, frames_from_callback_.size());
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200896 packet_buffer_.PaddingReceived(1);
philipel2c9f9f22017-06-13 02:47:28 -0700897 ASSERT_EQ(2UL, frames_from_callback_.size());
898 CheckFrame(0);
899 CheckFrame(2);
900}
901
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200902class TestPacketBufferH264XIsKeyframe : public TestPacketBufferH264 {
903 protected:
904 const uint16_t kSeqNum = 5;
905
906 explicit TestPacketBufferH264XIsKeyframe(bool sps_pps_idr_is_keyframe)
907 : TestPacketBufferH264(sps_pps_idr_is_keyframe) {
Niels Möllerd5e02f02019-02-20 13:12:21 +0100908 packet_.video_header.codec = kVideoCodecH264;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200909 packet_.seqNum = kSeqNum;
910
Niels Möllerd5e02f02019-02-20 13:12:21 +0100911 packet_.video_header.is_first_packet_in_frame = true;
912 packet_.video_header.is_last_packet_in_frame = true;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200913 }
914
915 VCMPacket packet_;
916};
917
918class TestPacketBufferH264IdrIsKeyframe
919 : public TestPacketBufferH264XIsKeyframe {
920 protected:
921 TestPacketBufferH264IdrIsKeyframe()
922 : TestPacketBufferH264XIsKeyframe(false) {}
923};
924
925TEST_F(TestPacketBufferH264IdrIsKeyframe, IdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200926 auto& h264_header =
927 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
928 h264_header.nalus[0].type = H264::NaluType::kIdr;
929 h264_header.nalus_length = 1;
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200930 packet_buffer_.InsertPacket(&packet_);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200931
932 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100933 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
934 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200935}
936
937TEST_F(TestPacketBufferH264IdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200938 auto& h264_header =
939 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
940 h264_header.nalus[0].type = H264::NaluType::kSps;
941 h264_header.nalus[1].type = H264::NaluType::kPps;
942 h264_header.nalus[2].type = H264::NaluType::kIdr;
943 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200944
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200945 packet_buffer_.InsertPacket(&packet_);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200946
947 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100948 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
949 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200950}
951
952class TestPacketBufferH264SpsPpsIdrIsKeyframe
953 : public TestPacketBufferH264XIsKeyframe {
954 protected:
955 TestPacketBufferH264SpsPpsIdrIsKeyframe()
956 : TestPacketBufferH264XIsKeyframe(true) {}
957};
958
959TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, IdrIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200960 auto& h264_header =
961 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
962 h264_header.nalus[0].type = H264::NaluType::kIdr;
963 h264_header.nalus_length = 1;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200964
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200965 packet_buffer_.InsertPacket(&packet_);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200966
967 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100968 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
969 frames_from_callback_[5]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200970}
971
972TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIsNotKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200973 auto& h264_header =
974 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
975 h264_header.nalus[0].type = H264::NaluType::kSps;
976 h264_header.nalus[1].type = H264::NaluType::kPps;
977 h264_header.nalus_length = 2;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200978
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200979 packet_buffer_.InsertPacket(&packet_);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200980
981 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100982 EXPECT_EQ(VideoFrameType::kVideoFrameDelta,
983 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200984}
985
986TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIdrIsKeyframe) {
philipel7d745e52018-08-02 14:03:53 +0200987 auto& h264_header =
988 packet_.video_header.video_type_header.emplace<RTPVideoHeaderH264>();
989 h264_header.nalus[0].type = H264::NaluType::kSps;
990 h264_header.nalus[1].type = H264::NaluType::kPps;
991 h264_header.nalus[2].type = H264::NaluType::kIdr;
992 h264_header.nalus_length = 3;
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200993
Danil Chapovalovf7457e52019-09-20 17:57:15 +0200994 packet_buffer_.InsertPacket(&packet_);
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200995
996 ASSERT_EQ(1u, frames_from_callback_.size());
Niels Möller8f7ce222019-03-21 15:43:58 +0100997 EXPECT_EQ(VideoFrameType::kVideoFrameKey,
998 frames_from_callback_[kSeqNum]->frame_type());
Rasmus Brandtedf4ff72017-10-24 10:07:48 +0200999}
1000
philipelc707ab72016-04-01 02:01:54 -07001001} // namespace video_coding
1002} // namespace webrtc