• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 #include "modules/video_coding/packet_buffer.h"
11 
12 #include <cstring>
13 #include <limits>
14 #include <ostream>
15 #include <string>
16 #include <utility>
17 
18 #include "api/array_view.h"
19 #include "common_video/h264/h264_common.h"
20 #include "modules/video_coding/frame_object.h"
21 #include "rtc_base/random.h"
22 #include "system_wrappers/include/clock.h"
23 #include "test/field_trial.h"
24 #include "test/gmock.h"
25 #include "test/gtest.h"
26 
27 namespace webrtc {
28 namespace video_coding {
29 namespace {
30 
31 using ::testing::ElementsAre;
32 using ::testing::ElementsAreArray;
33 using ::testing::IsEmpty;
34 using ::testing::Matches;
35 using ::testing::Pointee;
36 using ::testing::SizeIs;
37 
38 constexpr int kStartSize = 16;
39 constexpr int kMaxSize = 64;
40 
IgnoreResult(PacketBuffer::InsertResult)41 void IgnoreResult(PacketBuffer::InsertResult /*result*/) {}
42 
43 // Validates frame boundaries are valid and returns first sequence_number for
44 // each frame.
StartSeqNums(rtc::ArrayView<const std::unique_ptr<PacketBuffer::Packet>> packets)45 std::vector<uint16_t> StartSeqNums(
46     rtc::ArrayView<const std::unique_ptr<PacketBuffer::Packet>> packets) {
47   std::vector<uint16_t> result;
48   bool frame_boundary = true;
49   for (const auto& packet : packets) {
50     EXPECT_EQ(frame_boundary, packet->is_first_packet_in_frame());
51     if (packet->is_first_packet_in_frame()) {
52       result.push_back(packet->seq_num);
53     }
54     frame_boundary = packet->is_last_packet_in_frame();
55   }
56   EXPECT_TRUE(frame_boundary);
57   return result;
58 }
59 
60 MATCHER_P(StartSeqNumsAre, seq_num, "") {
61   return Matches(ElementsAre(seq_num))(StartSeqNums(arg.packets));
62 }
63 
64 MATCHER_P2(StartSeqNumsAre, seq_num1, seq_num2, "") {
65   return Matches(ElementsAre(seq_num1, seq_num2))(StartSeqNums(arg.packets));
66 }
67 
68 MATCHER(KeyFrame, "") {
69   return arg->is_first_packet_in_frame() &&
70          arg->video_header.frame_type == VideoFrameType::kVideoFrameKey;
71 }
72 
73 MATCHER(DeltaFrame, "") {
74   return arg->is_first_packet_in_frame() &&
75          arg->video_header.frame_type == VideoFrameType::kVideoFrameDelta;
76 }
77 
78 struct PacketBufferInsertResult : public PacketBuffer::InsertResult {
PacketBufferInsertResultwebrtc::video_coding::__anonfe1905180111::PacketBufferInsertResult79   explicit PacketBufferInsertResult(PacketBuffer::InsertResult result)
80       : InsertResult(std::move(result)) {}
81 };
82 
PrintTo(const PacketBufferInsertResult & result,std::ostream * os)83 void PrintTo(const PacketBufferInsertResult& result, std::ostream* os) {
84   *os << "frames: { ";
85   for (const auto& packet : result.packets) {
86     if (packet->is_first_packet_in_frame() &&
87         packet->is_last_packet_in_frame()) {
88       *os << "{sn: " << packet->seq_num << " }";
89     } else if (packet->is_first_packet_in_frame()) {
90       *os << "{sn: [" << packet->seq_num << "-";
91     } else if (packet->is_last_packet_in_frame()) {
92       *os << packet->seq_num << "] }, ";
93     }
94   }
95   *os << " }";
96   if (result.buffer_cleared) {
97     *os << ", buffer_cleared";
98   }
99 }
100 
101 class PacketBufferTest : public ::testing::Test {
102  protected:
PacketBufferTest(std::string field_trials="")103   explicit PacketBufferTest(std::string field_trials = "")
104       : scoped_field_trials_(field_trials),
105         rand_(0x7732213),
106         clock_(0),
107         packet_buffer_(&clock_, kStartSize, kMaxSize) {}
108 
Rand()109   uint16_t Rand() { return rand_.Rand<uint16_t>(); }
110 
111   enum IsKeyFrame { kKeyFrame, kDeltaFrame };
112   enum IsFirst { kFirst, kNotFirst };
113   enum IsLast { kLast, kNotLast };
114 
Insert(uint16_t seq_num,IsKeyFrame keyframe,IsFirst first,IsLast last,rtc::ArrayView<const uint8_t> data={},uint32_t timestamp=123u)115   PacketBufferInsertResult Insert(uint16_t seq_num,  // packet sequence number
116                                   IsKeyFrame keyframe,  // is keyframe
117                                   IsFirst first,  // is first packet of frame
118                                   IsLast last,    // is last packet of frame
119                                   rtc::ArrayView<const uint8_t> data = {},
120                                   uint32_t timestamp = 123u) {  // rtp timestamp
121     auto packet = std::make_unique<PacketBuffer::Packet>();
122     packet->video_header.codec = kVideoCodecGeneric;
123     packet->timestamp = timestamp;
124     packet->seq_num = seq_num;
125     packet->video_header.frame_type = keyframe == kKeyFrame
126                                           ? VideoFrameType::kVideoFrameKey
127                                           : VideoFrameType::kVideoFrameDelta;
128     packet->video_header.is_first_packet_in_frame = first == kFirst;
129     packet->video_header.is_last_packet_in_frame = last == kLast;
130     packet->video_payload.SetData(data.data(), data.size());
131 
132     return PacketBufferInsertResult(
133         packet_buffer_.InsertPacket(std::move(packet)));
134   }
135 
136   const test::ScopedFieldTrials scoped_field_trials_;
137   Random rand_;
138   SimulatedClock clock_;
139   PacketBuffer packet_buffer_;
140 };
141 
TEST_F(PacketBufferTest,InsertOnePacket)142 TEST_F(PacketBufferTest, InsertOnePacket) {
143   const uint16_t seq_num = Rand();
144   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast).packets, SizeIs(1));
145 }
146 
TEST_F(PacketBufferTest,InsertMultiplePackets)147 TEST_F(PacketBufferTest, InsertMultiplePackets) {
148   const uint16_t seq_num = Rand();
149   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast).packets, SizeIs(1));
150   EXPECT_THAT(Insert(seq_num + 1, kKeyFrame, kFirst, kLast).packets, SizeIs(1));
151   EXPECT_THAT(Insert(seq_num + 2, kKeyFrame, kFirst, kLast).packets, SizeIs(1));
152   EXPECT_THAT(Insert(seq_num + 3, kKeyFrame, kFirst, kLast).packets, SizeIs(1));
153 }
154 
TEST_F(PacketBufferTest,InsertDuplicatePacket)155 TEST_F(PacketBufferTest, InsertDuplicatePacket) {
156   const uint16_t seq_num = Rand();
157   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
158   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
159   EXPECT_THAT(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast).packets,
160               SizeIs(2));
161 }
162 
TEST_F(PacketBufferTest,SeqNumWrapOneFrame)163 TEST_F(PacketBufferTest, SeqNumWrapOneFrame) {
164   Insert(0xFFFF, kKeyFrame, kFirst, kNotLast);
165   EXPECT_THAT(Insert(0x0, kKeyFrame, kNotFirst, kLast),
166               StartSeqNumsAre(0xFFFF));
167 }
168 
TEST_F(PacketBufferTest,SeqNumWrapTwoFrames)169 TEST_F(PacketBufferTest, SeqNumWrapTwoFrames) {
170   EXPECT_THAT(Insert(0xFFFF, kKeyFrame, kFirst, kLast),
171               StartSeqNumsAre(0xFFFF));
172   EXPECT_THAT(Insert(0x0, kKeyFrame, kFirst, kLast), StartSeqNumsAre(0x0));
173 }
174 
TEST_F(PacketBufferTest,InsertOldPackets)175 TEST_F(PacketBufferTest, InsertOldPackets) {
176   EXPECT_THAT(Insert(100, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
177   EXPECT_THAT(Insert(102, kDeltaFrame, kFirst, kLast).packets, SizeIs(1));
178   EXPECT_THAT(Insert(101, kKeyFrame, kNotFirst, kLast).packets, SizeIs(2));
179 
180   EXPECT_THAT(Insert(100, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
181   EXPECT_THAT(Insert(100, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
182   EXPECT_THAT(Insert(102, kDeltaFrame, kFirst, kLast).packets, SizeIs(1));
183 
184   packet_buffer_.ClearTo(102);
185   EXPECT_THAT(Insert(102, kDeltaFrame, kFirst, kLast).packets, IsEmpty());
186   EXPECT_THAT(Insert(103, kDeltaFrame, kFirst, kLast).packets, SizeIs(1));
187 }
188 
TEST_F(PacketBufferTest,FrameSize)189 TEST_F(PacketBufferTest, FrameSize) {
190   const uint16_t seq_num = Rand();
191   uint8_t data1[5] = {};
192   uint8_t data2[5] = {};
193   uint8_t data3[5] = {};
194   uint8_t data4[5] = {};
195 
196   Insert(seq_num, kKeyFrame, kFirst, kNotLast, data1);
197   Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast, data2);
198   Insert(seq_num + 2, kKeyFrame, kNotFirst, kNotLast, data3);
199   auto packets =
200       Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, data4).packets;
201   // Expect one frame of 4 packets.
202   EXPECT_THAT(StartSeqNums(packets), ElementsAre(seq_num));
203   EXPECT_THAT(packets, SizeIs(4));
204 }
205 
TEST_F(PacketBufferTest,ExpandBuffer)206 TEST_F(PacketBufferTest, ExpandBuffer) {
207   const uint16_t seq_num = Rand();
208 
209   Insert(seq_num, kKeyFrame, kFirst, kNotLast);
210   for (int i = 1; i < kStartSize; ++i)
211     EXPECT_FALSE(
212         Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast).buffer_cleared);
213 
214   // Already inserted kStartSize number of packets, inserting the last packet
215   // should increase the buffer size and also result in an assembled frame.
216   EXPECT_FALSE(
217       Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast).buffer_cleared);
218 }
219 
TEST_F(PacketBufferTest,SingleFrameExpandsBuffer)220 TEST_F(PacketBufferTest, SingleFrameExpandsBuffer) {
221   const uint16_t seq_num = Rand();
222 
223   Insert(seq_num, kKeyFrame, kFirst, kNotLast);
224   for (int i = 1; i < kStartSize; ++i)
225     Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast);
226   EXPECT_THAT(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast),
227               StartSeqNumsAre(seq_num));
228 }
229 
TEST_F(PacketBufferTest,ExpandBufferOverflow)230 TEST_F(PacketBufferTest, ExpandBufferOverflow) {
231   const uint16_t seq_num = Rand();
232 
233   EXPECT_FALSE(Insert(seq_num, kKeyFrame, kFirst, kNotLast).buffer_cleared);
234   for (int i = 1; i < kMaxSize; ++i)
235     EXPECT_FALSE(
236         Insert(seq_num + i, kKeyFrame, kNotFirst, kNotLast).buffer_cleared);
237 
238   // Already inserted kMaxSize number of packets, inserting the last packet
239   // should overflow the buffer and result in false being returned.
240   EXPECT_TRUE(
241       Insert(seq_num + kMaxSize, kKeyFrame, kNotFirst, kLast).buffer_cleared);
242 }
243 
TEST_F(PacketBufferTest,OnePacketOneFrame)244 TEST_F(PacketBufferTest, OnePacketOneFrame) {
245   const uint16_t seq_num = Rand();
246   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast),
247               StartSeqNumsAre(seq_num));
248 }
249 
TEST_F(PacketBufferTest,TwoPacketsTwoFrames)250 TEST_F(PacketBufferTest, TwoPacketsTwoFrames) {
251   const uint16_t seq_num = Rand();
252 
253   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast),
254               StartSeqNumsAre(seq_num));
255   EXPECT_THAT(Insert(seq_num + 1, kKeyFrame, kFirst, kLast),
256               StartSeqNumsAre(seq_num + 1));
257 }
258 
TEST_F(PacketBufferTest,TwoPacketsOneFrames)259 TEST_F(PacketBufferTest, TwoPacketsOneFrames) {
260   const uint16_t seq_num = Rand();
261 
262   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
263   EXPECT_THAT(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast),
264               StartSeqNumsAre(seq_num));
265 }
266 
TEST_F(PacketBufferTest,ThreePacketReorderingOneFrame)267 TEST_F(PacketBufferTest, ThreePacketReorderingOneFrame) {
268   const uint16_t seq_num = Rand();
269 
270   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kNotLast).packets, IsEmpty());
271   EXPECT_THAT(Insert(seq_num + 2, kKeyFrame, kNotFirst, kLast).packets,
272               IsEmpty());
273   EXPECT_THAT(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast),
274               StartSeqNumsAre(seq_num));
275 }
276 
TEST_F(PacketBufferTest,Frames)277 TEST_F(PacketBufferTest, Frames) {
278   const uint16_t seq_num = Rand();
279 
280   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast),
281               StartSeqNumsAre(seq_num));
282   EXPECT_THAT(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast),
283               StartSeqNumsAre(seq_num + 1));
284   EXPECT_THAT(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast),
285               StartSeqNumsAre(seq_num + 2));
286   EXPECT_THAT(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast),
287               StartSeqNumsAre(seq_num + 3));
288 }
289 
TEST_F(PacketBufferTest,ClearSinglePacket)290 TEST_F(PacketBufferTest, ClearSinglePacket) {
291   const uint16_t seq_num = Rand();
292 
293   for (int i = 0; i < kMaxSize; ++i)
294     Insert(seq_num + i, kDeltaFrame, kFirst, kLast);
295 
296   packet_buffer_.ClearTo(seq_num);
297   EXPECT_FALSE(
298       Insert(seq_num + kMaxSize, kDeltaFrame, kFirst, kLast).buffer_cleared);
299 }
300 
TEST_F(PacketBufferTest,ClearFullBuffer)301 TEST_F(PacketBufferTest, ClearFullBuffer) {
302   for (int i = 0; i < kMaxSize; ++i)
303     Insert(i, kDeltaFrame, kFirst, kLast);
304 
305   packet_buffer_.ClearTo(kMaxSize - 1);
306 
307   for (int i = kMaxSize; i < 2 * kMaxSize; ++i)
308     EXPECT_FALSE(Insert(i, kDeltaFrame, kFirst, kLast).buffer_cleared);
309 }
310 
TEST_F(PacketBufferTest,DontClearNewerPacket)311 TEST_F(PacketBufferTest, DontClearNewerPacket) {
312   EXPECT_THAT(Insert(0, kKeyFrame, kFirst, kLast), StartSeqNumsAre(0));
313   packet_buffer_.ClearTo(0);
314   EXPECT_THAT(Insert(2 * kStartSize, kKeyFrame, kFirst, kLast),
315               StartSeqNumsAre(2 * kStartSize));
316   EXPECT_THAT(Insert(3 * kStartSize + 1, kKeyFrame, kFirst, kNotLast).packets,
317               IsEmpty());
318   packet_buffer_.ClearTo(2 * kStartSize);
319   EXPECT_THAT(Insert(3 * kStartSize + 2, kKeyFrame, kNotFirst, kLast),
320               StartSeqNumsAre(3 * kStartSize + 1));
321 }
322 
TEST_F(PacketBufferTest,OneIncompleteFrame)323 TEST_F(PacketBufferTest, OneIncompleteFrame) {
324   const uint16_t seq_num = Rand();
325 
326   EXPECT_THAT(Insert(seq_num, kDeltaFrame, kFirst, kNotLast).packets,
327               IsEmpty());
328   EXPECT_THAT(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kLast),
329               StartSeqNumsAre(seq_num));
330   EXPECT_THAT(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast).packets,
331               IsEmpty());
332 }
333 
TEST_F(PacketBufferTest,TwoIncompleteFramesFullBuffer)334 TEST_F(PacketBufferTest, TwoIncompleteFramesFullBuffer) {
335   const uint16_t seq_num = Rand();
336 
337   for (int i = 1; i < kMaxSize - 1; ++i)
338     Insert(seq_num + i, kDeltaFrame, kNotFirst, kNotLast);
339   EXPECT_THAT(Insert(seq_num, kDeltaFrame, kFirst, kNotLast).packets,
340               IsEmpty());
341   EXPECT_THAT(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast).packets,
342               IsEmpty());
343 }
344 
TEST_F(PacketBufferTest,FramesReordered)345 TEST_F(PacketBufferTest, FramesReordered) {
346   const uint16_t seq_num = Rand();
347 
348   EXPECT_THAT(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast),
349               StartSeqNumsAre(seq_num + 1));
350   EXPECT_THAT(Insert(seq_num, kKeyFrame, kFirst, kLast),
351               StartSeqNumsAre(seq_num));
352   EXPECT_THAT(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast),
353               StartSeqNumsAre(seq_num + 3));
354   EXPECT_THAT(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast),
355               StartSeqNumsAre(seq_num + 2));
356 }
357 
TEST_F(PacketBufferTest,InsertPacketAfterSequenceNumberWrapAround)358 TEST_F(PacketBufferTest, InsertPacketAfterSequenceNumberWrapAround) {
359   uint16_t kFirstSeqNum = 0;
360   uint32_t kTimestampDelta = 100;
361   uint32_t timestamp = 10000;
362   uint16_t seq_num = kFirstSeqNum;
363 
364   // Loop until seq_num wraps around.
365   SeqNumUnwrapper<uint16_t> unwrapper;
366   while (unwrapper.Unwrap(seq_num) < std::numeric_limits<uint16_t>::max()) {
367     Insert(seq_num++, kKeyFrame, kFirst, kNotLast, {}, timestamp);
368     for (int i = 0; i < 5; ++i) {
369       Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, {}, timestamp);
370     }
371     Insert(seq_num++, kKeyFrame, kNotFirst, kLast, {}, timestamp);
372     timestamp += kTimestampDelta;
373   }
374 
375   // Receive frame with overlapping sequence numbers.
376   Insert(seq_num++, kKeyFrame, kFirst, kNotLast, {}, timestamp);
377   for (int i = 0; i < 5; ++i) {
378     Insert(seq_num++, kKeyFrame, kNotFirst, kNotLast, {}, timestamp);
379   }
380   auto packets =
381       Insert(seq_num++, kKeyFrame, kNotFirst, kLast, {}, timestamp).packets;
382   // One frame of 7 packets.
383   EXPECT_THAT(StartSeqNums(packets), SizeIs(1));
384   EXPECT_THAT(packets, SizeIs(7));
385 }
386 
387 // If |sps_pps_idr_is_keyframe| is true, we require keyframes to contain
388 // SPS/PPS/IDR and the keyframes we create as part of the test do contain
389 // SPS/PPS/IDR. If |sps_pps_idr_is_keyframe| is false, we only require and
390 // create keyframes containing only IDR.
391 class PacketBufferH264Test : public PacketBufferTest {
392  protected:
PacketBufferH264Test(bool sps_pps_idr_is_keyframe)393   explicit PacketBufferH264Test(bool sps_pps_idr_is_keyframe)
394       : PacketBufferTest(sps_pps_idr_is_keyframe
395                              ? "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/"
396                              : ""),
397         sps_pps_idr_is_keyframe_(sps_pps_idr_is_keyframe) {}
398 
InsertH264(uint16_t seq_num,IsKeyFrame keyframe,IsFirst first,IsLast last,uint32_t timestamp,rtc::ArrayView<const uint8_t> data={},uint32_t width=0,uint32_t height=0)399   PacketBufferInsertResult InsertH264(
400       uint16_t seq_num,     // packet sequence number
401       IsKeyFrame keyframe,  // is keyframe
402       IsFirst first,        // is first packet of frame
403       IsLast last,          // is last packet of frame
404       uint32_t timestamp,   // rtp timestamp
405       rtc::ArrayView<const uint8_t> data = {},
406       uint32_t width = 0,     // width of frame (SPS/IDR)
407       uint32_t height = 0) {  // height of frame (SPS/IDR)
408     auto packet = std::make_unique<PacketBuffer::Packet>();
409     packet->video_header.codec = kVideoCodecH264;
410     auto& h264_header =
411         packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
412     packet->seq_num = seq_num;
413     packet->timestamp = timestamp;
414     if (keyframe == kKeyFrame) {
415       if (sps_pps_idr_is_keyframe_) {
416         h264_header.nalus[0].type = H264::NaluType::kSps;
417         h264_header.nalus[1].type = H264::NaluType::kPps;
418         h264_header.nalus[2].type = H264::NaluType::kIdr;
419         h264_header.nalus_length = 3;
420       } else {
421         h264_header.nalus[0].type = H264::NaluType::kIdr;
422         h264_header.nalus_length = 1;
423       }
424     }
425     packet->video_header.width = width;
426     packet->video_header.height = height;
427     packet->video_header.is_first_packet_in_frame = first == kFirst;
428     packet->video_header.is_last_packet_in_frame = last == kLast;
429     packet->video_payload.SetData(data.data(), data.size());
430 
431     return PacketBufferInsertResult(
432         packet_buffer_.InsertPacket(std::move(packet)));
433   }
434 
InsertH264KeyFrameWithAud(uint16_t seq_num,IsKeyFrame keyframe,IsFirst first,IsLast last,uint32_t timestamp,rtc::ArrayView<const uint8_t> data={},uint32_t width=0,uint32_t height=0)435   PacketBufferInsertResult InsertH264KeyFrameWithAud(
436       uint16_t seq_num,     // packet sequence number
437       IsKeyFrame keyframe,  // is keyframe
438       IsFirst first,        // is first packet of frame
439       IsLast last,          // is last packet of frame
440       uint32_t timestamp,   // rtp timestamp
441       rtc::ArrayView<const uint8_t> data = {},
442       uint32_t width = 0,     // width of frame (SPS/IDR)
443       uint32_t height = 0) {  // height of frame (SPS/IDR)
444     auto packet = std::make_unique<PacketBuffer::Packet>();
445     packet->video_header.codec = kVideoCodecH264;
446     auto& h264_header =
447         packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
448     packet->seq_num = seq_num;
449     packet->timestamp = timestamp;
450 
451     // this should be the start of frame.
452     RTC_CHECK(first == kFirst);
453 
454     // Insert a AUD NALU / packet without width/height.
455     h264_header.nalus[0].type = H264::NaluType::kAud;
456     h264_header.nalus_length = 1;
457     packet->video_header.is_first_packet_in_frame = true;
458     packet->video_header.is_last_packet_in_frame = false;
459     IgnoreResult(packet_buffer_.InsertPacket(std::move(packet)));
460     // insert IDR
461     return InsertH264(seq_num + 1, keyframe, kNotFirst, last, timestamp, data,
462                       width, height);
463   }
464 
465   const bool sps_pps_idr_is_keyframe_;
466 };
467 
468 // This fixture is used to test the general behaviour of the packet buffer
469 // in both configurations.
470 class PacketBufferH264ParameterizedTest
471     : public ::testing::WithParamInterface<bool>,
472       public PacketBufferH264Test {
473  protected:
PacketBufferH264ParameterizedTest()474   PacketBufferH264ParameterizedTest() : PacketBufferH264Test(GetParam()) {}
475 };
476 
477 INSTANTIATE_TEST_SUITE_P(SpsPpsIdrIsKeyframe,
478                          PacketBufferH264ParameterizedTest,
479                          ::testing::Bool());
480 
TEST_P(PacketBufferH264ParameterizedTest,DontRemoveMissingPacketOnClearTo)481 TEST_P(PacketBufferH264ParameterizedTest, DontRemoveMissingPacketOnClearTo) {
482   InsertH264(0, kKeyFrame, kFirst, kLast, 0);
483   InsertH264(2, kDeltaFrame, kFirst, kNotLast, 2);
484   packet_buffer_.ClearTo(0);
485   // Expect no frame because of missing of packet #1
486   EXPECT_THAT(InsertH264(3, kDeltaFrame, kNotFirst, kLast, 2).packets,
487               IsEmpty());
488 }
489 
TEST_P(PacketBufferH264ParameterizedTest,GetBitstreamOneFrameFullBuffer)490 TEST_P(PacketBufferH264ParameterizedTest, GetBitstreamOneFrameFullBuffer) {
491   uint8_t data_arr[kStartSize][1];
492   uint8_t expected[kStartSize];
493 
494   for (uint8_t i = 0; i < kStartSize; ++i) {
495     data_arr[i][0] = i;
496     expected[i] = i;
497   }
498 
499   InsertH264(0, kKeyFrame, kFirst, kNotLast, 1, data_arr[0]);
500   for (uint8_t i = 1; i < kStartSize - 1; ++i) {
501     InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1, data_arr[i]);
502   }
503 
504   auto packets = InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1,
505                             data_arr[kStartSize - 1])
506                      .packets;
507   ASSERT_THAT(StartSeqNums(packets), ElementsAre(0));
508   EXPECT_THAT(packets, SizeIs(kStartSize));
509   for (size_t i = 0; i < packets.size(); ++i) {
510     EXPECT_THAT(packets[i]->video_payload, SizeIs(1)) << "Packet #" << i;
511   }
512 }
513 
TEST_P(PacketBufferH264ParameterizedTest,GetBitstreamBufferPadding)514 TEST_P(PacketBufferH264ParameterizedTest, GetBitstreamBufferPadding) {
515   uint16_t seq_num = Rand();
516   rtc::CopyOnWriteBuffer data = "some plain old data";
517 
518   auto packet = std::make_unique<PacketBuffer::Packet>();
519   auto& h264_header =
520       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
521   h264_header.nalus_length = 1;
522   h264_header.nalus[0].type = H264::NaluType::kIdr;
523   h264_header.packetization_type = kH264SingleNalu;
524   packet->seq_num = seq_num;
525   packet->video_header.codec = kVideoCodecH264;
526   packet->video_payload = data;
527   packet->video_header.is_first_packet_in_frame = true;
528   packet->video_header.is_last_packet_in_frame = true;
529   auto frames = packet_buffer_.InsertPacket(std::move(packet)).packets;
530 
531   ASSERT_THAT(frames, SizeIs(1));
532   EXPECT_EQ(frames[0]->seq_num, seq_num);
533   EXPECT_EQ(frames[0]->video_payload, data);
534 }
535 
TEST_P(PacketBufferH264ParameterizedTest,FrameResolution)536 TEST_P(PacketBufferH264ParameterizedTest, FrameResolution) {
537   uint16_t seq_num = 100;
538   uint8_t data[] = "some plain old data";
539   uint32_t width = 640;
540   uint32_t height = 360;
541   uint32_t timestamp = 1000;
542 
543   auto packets = InsertH264(seq_num, kKeyFrame, kFirst, kLast, timestamp, data,
544                             width, height)
545                      .packets;
546 
547   ASSERT_THAT(packets, SizeIs(1));
548   EXPECT_EQ(packets[0]->video_header.width, width);
549   EXPECT_EQ(packets[0]->video_header.height, height);
550 }
551 
TEST_P(PacketBufferH264ParameterizedTest,FrameResolutionNaluBeforeSPS)552 TEST_P(PacketBufferH264ParameterizedTest, FrameResolutionNaluBeforeSPS) {
553   uint16_t seq_num = 100;
554   uint8_t data[] = "some plain old data";
555   uint32_t width = 640;
556   uint32_t height = 360;
557   uint32_t timestamp = 1000;
558 
559   auto packets = InsertH264KeyFrameWithAud(seq_num, kKeyFrame, kFirst, kLast,
560                                            timestamp, data, width, height)
561                      .packets;
562 
563   ASSERT_THAT(StartSeqNums(packets), ElementsAre(seq_num));
564   EXPECT_EQ(packets[0]->video_header.width, width);
565   EXPECT_EQ(packets[0]->video_header.height, height);
566 }
567 
TEST_F(PacketBufferTest,FreeSlotsOnFrameCreation)568 TEST_F(PacketBufferTest, FreeSlotsOnFrameCreation) {
569   const uint16_t seq_num = Rand();
570 
571   Insert(seq_num, kKeyFrame, kFirst, kNotLast);
572   Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast);
573   EXPECT_THAT(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast),
574               StartSeqNumsAre(seq_num));
575 
576   // Insert frame that fills the whole buffer.
577   Insert(seq_num + 3, kKeyFrame, kFirst, kNotLast);
578   for (int i = 0; i < kMaxSize - 2; ++i)
579     Insert(seq_num + i + 4, kDeltaFrame, kNotFirst, kNotLast);
580   EXPECT_THAT(Insert(seq_num + kMaxSize + 2, kKeyFrame, kNotFirst, kLast),
581               StartSeqNumsAre(seq_num + 3));
582 }
583 
TEST_F(PacketBufferTest,Clear)584 TEST_F(PacketBufferTest, Clear) {
585   const uint16_t seq_num = Rand();
586 
587   Insert(seq_num, kKeyFrame, kFirst, kNotLast);
588   Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast);
589   EXPECT_THAT(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast),
590               StartSeqNumsAre(seq_num));
591 
592   packet_buffer_.Clear();
593 
594   Insert(seq_num + kStartSize, kKeyFrame, kFirst, kNotLast);
595   Insert(seq_num + kStartSize + 1, kDeltaFrame, kNotFirst, kNotLast);
596   EXPECT_THAT(Insert(seq_num + kStartSize + 2, kDeltaFrame, kNotFirst, kLast),
597               StartSeqNumsAre(seq_num + kStartSize));
598 }
599 
TEST_F(PacketBufferTest,FramesAfterClear)600 TEST_F(PacketBufferTest, FramesAfterClear) {
601   Insert(9025, kDeltaFrame, kFirst, kLast);
602   Insert(9024, kKeyFrame, kFirst, kLast);
603   packet_buffer_.ClearTo(9025);
604   EXPECT_THAT(Insert(9057, kDeltaFrame, kFirst, kLast).packets, SizeIs(1));
605   EXPECT_THAT(Insert(9026, kDeltaFrame, kFirst, kLast).packets, SizeIs(1));
606 }
607 
TEST_F(PacketBufferTest,SameFrameDifferentTimestamps)608 TEST_F(PacketBufferTest, SameFrameDifferentTimestamps) {
609   Insert(0, kKeyFrame, kFirst, kNotLast, {}, 1000);
610   EXPECT_THAT(Insert(1, kKeyFrame, kNotFirst, kLast, {}, 1001).packets,
611               IsEmpty());
612 }
613 
TEST_F(PacketBufferTest,ContinuousSeqNumDoubleMarkerBit)614 TEST_F(PacketBufferTest, ContinuousSeqNumDoubleMarkerBit) {
615   Insert(2, kKeyFrame, kNotFirst, kNotLast);
616   Insert(1, kKeyFrame, kFirst, kLast);
617   EXPECT_THAT(Insert(3, kKeyFrame, kNotFirst, kLast).packets, IsEmpty());
618 }
619 
TEST_F(PacketBufferTest,PacketTimestamps)620 TEST_F(PacketBufferTest, PacketTimestamps) {
621   absl::optional<int64_t> packet_ms;
622   absl::optional<int64_t> packet_keyframe_ms;
623 
624   packet_ms = packet_buffer_.LastReceivedPacketMs();
625   packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
626   EXPECT_FALSE(packet_ms);
627   EXPECT_FALSE(packet_keyframe_ms);
628 
629   int64_t keyframe_ms = clock_.TimeInMilliseconds();
630   Insert(100, kKeyFrame, kFirst, kLast, {}, /*timestamp=*/1000);
631   packet_ms = packet_buffer_.LastReceivedPacketMs();
632   packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
633   EXPECT_TRUE(packet_ms);
634   EXPECT_TRUE(packet_keyframe_ms);
635   EXPECT_EQ(keyframe_ms, *packet_ms);
636   EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
637 
638   clock_.AdvanceTimeMilliseconds(100);
639   int64_t delta_ms = clock_.TimeInMilliseconds();
640   Insert(101, kDeltaFrame, kFirst, kLast, {}, /*timestamp=*/2000);
641   packet_ms = packet_buffer_.LastReceivedPacketMs();
642   packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
643   EXPECT_TRUE(packet_ms);
644   EXPECT_TRUE(packet_keyframe_ms);
645   EXPECT_EQ(delta_ms, *packet_ms);
646   EXPECT_EQ(keyframe_ms, *packet_keyframe_ms);
647 
648   packet_buffer_.Clear();
649   packet_ms = packet_buffer_.LastReceivedPacketMs();
650   packet_keyframe_ms = packet_buffer_.LastReceivedKeyframePacketMs();
651   EXPECT_FALSE(packet_ms);
652   EXPECT_FALSE(packet_keyframe_ms);
653 }
654 
TEST_F(PacketBufferTest,LastReceivedKeyFrameReturnsReceiveTimeOfALastReceivedPacketOfAKeyFrame)655 TEST_F(PacketBufferTest,
656        LastReceivedKeyFrameReturnsReceiveTimeOfALastReceivedPacketOfAKeyFrame) {
657   clock_.AdvanceTimeMilliseconds(100);
658   Insert(/*seq_num=*/100, kKeyFrame, kFirst, kNotLast, {}, /*timestamp=*/1000);
659   EXPECT_EQ(packet_buffer_.LastReceivedKeyframePacketMs(),
660             clock_.TimeInMilliseconds());
661 
662   clock_.AdvanceTimeMilliseconds(100);
663   Insert(/*seq_num=*/102, kDeltaFrame, kNotFirst, kLast, {},
664          /*timestamp=*/1000);
665   EXPECT_EQ(packet_buffer_.LastReceivedKeyframePacketMs(),
666             clock_.TimeInMilliseconds());
667 
668   clock_.AdvanceTimeMilliseconds(100);
669   Insert(/*seq_num=*/101, kDeltaFrame, kNotFirst, kNotLast, {},
670          /*timestamp=*/1000);
671   EXPECT_EQ(packet_buffer_.LastReceivedKeyframePacketMs(),
672             clock_.TimeInMilliseconds());
673 
674   clock_.AdvanceTimeMilliseconds(100);
675   Insert(/*seq_num=*/103, kDeltaFrame, kFirst, kNotLast, {},
676          /*timestamp=*/2000);
677   EXPECT_EQ(packet_buffer_.LastReceivedKeyframePacketMs(),
678             clock_.TimeInMilliseconds() - 100);
679 }
680 
TEST_F(PacketBufferTest,IncomingCodecChange)681 TEST_F(PacketBufferTest, IncomingCodecChange) {
682   auto packet = std::make_unique<PacketBuffer::Packet>();
683   packet->video_header.is_first_packet_in_frame = true;
684   packet->video_header.is_last_packet_in_frame = true;
685   packet->video_header.codec = kVideoCodecVP8;
686   packet->video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
687   packet->timestamp = 1;
688   packet->seq_num = 1;
689   packet->video_header.frame_type = VideoFrameType::kVideoFrameKey;
690   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
691               SizeIs(1));
692 
693   packet = std::make_unique<PacketBuffer::Packet>();
694   packet->video_header.is_first_packet_in_frame = true;
695   packet->video_header.is_last_packet_in_frame = true;
696   packet->video_header.codec = kVideoCodecH264;
697   auto& h264_header =
698       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
699   h264_header.nalus_length = 1;
700   packet->timestamp = 3;
701   packet->seq_num = 3;
702   packet->video_header.frame_type = VideoFrameType::kVideoFrameKey;
703   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
704               IsEmpty());
705 
706   packet = std::make_unique<PacketBuffer::Packet>();
707   packet->video_header.is_first_packet_in_frame = true;
708   packet->video_header.is_last_packet_in_frame = true;
709   packet->video_header.codec = kVideoCodecVP8;
710   packet->video_header.video_type_header.emplace<RTPVideoHeaderVP8>();
711   packet->timestamp = 2;
712   packet->seq_num = 2;
713   packet->video_header.frame_type = VideoFrameType::kVideoFrameDelta;
714   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
715               SizeIs(2));
716 }
717 
TEST_F(PacketBufferTest,TooManyNalusInPacket)718 TEST_F(PacketBufferTest, TooManyNalusInPacket) {
719   auto packet = std::make_unique<PacketBuffer::Packet>();
720   packet->video_header.codec = kVideoCodecH264;
721   packet->timestamp = 1;
722   packet->seq_num = 1;
723   packet->video_header.frame_type = VideoFrameType::kVideoFrameKey;
724   packet->video_header.is_first_packet_in_frame = true;
725   packet->video_header.is_last_packet_in_frame = true;
726   auto& h264_header =
727       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
728   h264_header.nalus_length = kMaxNalusPerPacket;
729   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
730               IsEmpty());
731 }
732 
TEST_P(PacketBufferH264ParameterizedTest,OneFrameFillBuffer)733 TEST_P(PacketBufferH264ParameterizedTest, OneFrameFillBuffer) {
734   InsertH264(0, kKeyFrame, kFirst, kNotLast, 1000);
735   for (int i = 1; i < kStartSize - 1; ++i)
736     InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1000);
737   EXPECT_THAT(InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1000),
738               StartSeqNumsAre(0));
739 }
740 
TEST_P(PacketBufferH264ParameterizedTest,CreateFramesAfterFilledBuffer)741 TEST_P(PacketBufferH264ParameterizedTest, CreateFramesAfterFilledBuffer) {
742   EXPECT_THAT(InsertH264(kStartSize - 2, kKeyFrame, kFirst, kLast, 0).packets,
743               SizeIs(1));
744 
745   InsertH264(kStartSize, kDeltaFrame, kFirst, kNotLast, 2000);
746   for (int i = 1; i < kStartSize; ++i)
747     InsertH264(kStartSize + i, kDeltaFrame, kNotFirst, kNotLast, 2000);
748   EXPECT_THAT(
749       InsertH264(kStartSize + kStartSize, kDeltaFrame, kNotFirst, kLast, 2000)
750           .packets,
751       IsEmpty());
752 
753   EXPECT_THAT(InsertH264(kStartSize - 1, kKeyFrame, kFirst, kLast, 1000),
754               StartSeqNumsAre(kStartSize - 1, kStartSize));
755 }
756 
TEST_P(PacketBufferH264ParameterizedTest,OneFrameMaxSeqNum)757 TEST_P(PacketBufferH264ParameterizedTest, OneFrameMaxSeqNum) {
758   InsertH264(65534, kKeyFrame, kFirst, kNotLast, 1000);
759   EXPECT_THAT(InsertH264(65535, kKeyFrame, kNotFirst, kLast, 1000),
760               StartSeqNumsAre(65534));
761 }
762 
TEST_P(PacketBufferH264ParameterizedTest,ClearMissingPacketsOnKeyframe)763 TEST_P(PacketBufferH264ParameterizedTest, ClearMissingPacketsOnKeyframe) {
764   InsertH264(0, kKeyFrame, kFirst, kLast, 1000);
765   InsertH264(2, kKeyFrame, kFirst, kLast, 3000);
766   InsertH264(3, kDeltaFrame, kFirst, kNotLast, 4000);
767   InsertH264(4, kDeltaFrame, kNotFirst, kLast, 4000);
768 
769   EXPECT_THAT(InsertH264(kStartSize + 1, kKeyFrame, kFirst, kLast, 18000),
770               StartSeqNumsAre(kStartSize + 1));
771 }
772 
TEST_P(PacketBufferH264ParameterizedTest,FindFramesOnPadding)773 TEST_P(PacketBufferH264ParameterizedTest, FindFramesOnPadding) {
774   EXPECT_THAT(InsertH264(0, kKeyFrame, kFirst, kLast, 1000),
775               StartSeqNumsAre(0));
776   EXPECT_THAT(InsertH264(2, kDeltaFrame, kFirst, kLast, 1000).packets,
777               IsEmpty());
778 
779   EXPECT_THAT(packet_buffer_.InsertPadding(1), StartSeqNumsAre(2));
780 }
781 
782 class PacketBufferH264XIsKeyframeTest : public PacketBufferH264Test {
783  protected:
784   const uint16_t kSeqNum = 5;
785 
PacketBufferH264XIsKeyframeTest(bool sps_pps_idr_is_keyframe)786   explicit PacketBufferH264XIsKeyframeTest(bool sps_pps_idr_is_keyframe)
787       : PacketBufferH264Test(sps_pps_idr_is_keyframe) {}
788 
CreatePacket()789   std::unique_ptr<PacketBuffer::Packet> CreatePacket() {
790     auto packet = std::make_unique<PacketBuffer::Packet>();
791     packet->video_header.codec = kVideoCodecH264;
792     packet->seq_num = kSeqNum;
793 
794     packet->video_header.is_first_packet_in_frame = true;
795     packet->video_header.is_last_packet_in_frame = true;
796     return packet;
797   }
798 };
799 
800 class PacketBufferH264IdrIsKeyframeTest
801     : public PacketBufferH264XIsKeyframeTest {
802  protected:
PacketBufferH264IdrIsKeyframeTest()803   PacketBufferH264IdrIsKeyframeTest()
804       : PacketBufferH264XIsKeyframeTest(false) {}
805 };
806 
TEST_F(PacketBufferH264IdrIsKeyframeTest,IdrIsKeyframe)807 TEST_F(PacketBufferH264IdrIsKeyframeTest, IdrIsKeyframe) {
808   auto packet = CreatePacket();
809   auto& h264_header =
810       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
811   h264_header.nalus[0].type = H264::NaluType::kIdr;
812   h264_header.nalus_length = 1;
813   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
814               ElementsAre(KeyFrame()));
815 }
816 
TEST_F(PacketBufferH264IdrIsKeyframeTest,SpsPpsIdrIsKeyframe)817 TEST_F(PacketBufferH264IdrIsKeyframeTest, SpsPpsIdrIsKeyframe) {
818   auto packet = CreatePacket();
819   auto& h264_header =
820       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
821   h264_header.nalus[0].type = H264::NaluType::kSps;
822   h264_header.nalus[1].type = H264::NaluType::kPps;
823   h264_header.nalus[2].type = H264::NaluType::kIdr;
824   h264_header.nalus_length = 3;
825 
826   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
827               ElementsAre(KeyFrame()));
828 }
829 
830 class PacketBufferH264SpsPpsIdrIsKeyframeTest
831     : public PacketBufferH264XIsKeyframeTest {
832  protected:
PacketBufferH264SpsPpsIdrIsKeyframeTest()833   PacketBufferH264SpsPpsIdrIsKeyframeTest()
834       : PacketBufferH264XIsKeyframeTest(true) {}
835 };
836 
TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest,IdrIsNotKeyframe)837 TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest, IdrIsNotKeyframe) {
838   auto packet = CreatePacket();
839   auto& h264_header =
840       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
841   h264_header.nalus[0].type = H264::NaluType::kIdr;
842   h264_header.nalus_length = 1;
843 
844   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
845               ElementsAre(DeltaFrame()));
846 }
847 
TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest,SpsPpsIsNotKeyframe)848 TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest, SpsPpsIsNotKeyframe) {
849   auto packet = CreatePacket();
850   auto& h264_header =
851       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
852   h264_header.nalus[0].type = H264::NaluType::kSps;
853   h264_header.nalus[1].type = H264::NaluType::kPps;
854   h264_header.nalus_length = 2;
855 
856   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
857               ElementsAre(DeltaFrame()));
858 }
859 
TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest,SpsPpsIdrIsKeyframe)860 TEST_F(PacketBufferH264SpsPpsIdrIsKeyframeTest, SpsPpsIdrIsKeyframe) {
861   auto packet = CreatePacket();
862   auto& h264_header =
863       packet->video_header.video_type_header.emplace<RTPVideoHeaderH264>();
864   h264_header.nalus[0].type = H264::NaluType::kSps;
865   h264_header.nalus[1].type = H264::NaluType::kPps;
866   h264_header.nalus[2].type = H264::NaluType::kIdr;
867   h264_header.nalus_length = 3;
868 
869   EXPECT_THAT(packet_buffer_.InsertPacket(std::move(packet)).packets,
870               ElementsAre(KeyFrame()));
871 }
872 
873 }  // namespace
874 }  // namespace video_coding
875 }  // namespace webrtc
876