philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 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 | |
| 11 | #include <cstring> |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 12 | #include <map> |
| 13 | #include <set> |
philipel | a105987 | 2016-05-09 11:41:48 +0200 | [diff] [blame] | 14 | #include <utility> |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #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 Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 21 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "test/gtest.h" |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace video_coding { |
| 26 | |
| 27 | class TestPacketBuffer : public ::testing::Test, |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 28 | public OnReceivedFrameCallback { |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 29 | protected: |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 30 | TestPacketBuffer() : TestPacketBuffer("") {} |
| 31 | explicit TestPacketBuffer(std::string field_trials) |
| 32 | : scoped_field_trials_(field_trials), |
| 33 | rand_(0x7732213), |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 34 | clock_(new SimulatedClock(0)), |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 35 | packet_buffer_( |
| 36 | PacketBuffer::Create(clock_.get(), kStartSize, kMaxSize, this)) {} |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 37 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 38 | uint16_t Rand() { return rand_.Rand<uint16_t>(); } |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 39 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 40 | void OnReceivedFrame(std::unique_ptr<RtpFrameObject> frame) override { |
| 41 | uint16_t first_seq_num = frame->first_seq_num(); |
| 42 | if (frames_from_callback_.find(first_seq_num) != |
| 43 | frames_from_callback_.end()) { |
| 44 | ADD_FAILURE() << "Already received frame with first sequence number " |
| 45 | << first_seq_num << "."; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 46 | return; |
| 47 | } |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 48 | |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 49 | frames_from_callback_.insert( |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 50 | std::make_pair(frame->first_seq_num(), std::move(frame))); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 51 | } |
| 52 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 53 | enum IsKeyFrame { kKeyFrame, kDeltaFrame }; |
| 54 | enum IsFirst { kFirst, kNotFirst }; |
| 55 | enum IsLast { kLast, kNotLast }; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 56 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 57 | bool Insert(uint16_t seq_num, // packet sequence number |
| 58 | IsKeyFrame keyframe, // is keyframe |
| 59 | IsFirst first, // is first packet of frame |
| 60 | IsLast last, // is last packet of frame |
| 61 | int data_size = 0, // size of data |
| 62 | uint8_t* data = nullptr, // data pointer |
| 63 | uint32_t timestamp = 123u) { // rtp timestamp |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 64 | VCMPacket packet; |
| 65 | packet.codec = kVideoCodecGeneric; |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 66 | packet.timestamp = timestamp; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 67 | packet.seqNum = seq_num; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 68 | packet.frameType = |
| 69 | keyframe == kKeyFrame ? kVideoFrameKey : kVideoFrameDelta; |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 70 | packet.is_first_packet_in_frame = first == kFirst; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 71 | packet.markerBit = last == kLast; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 72 | packet.sizeBytes = data_size; |
| 73 | packet.dataPtr = data; |
| 74 | |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 75 | return packet_buffer_->InsertPacket(&packet); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 76 | } |
| 77 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 78 | 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 | << "."; |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 83 | } |
| 84 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 85 | static constexpr int kStartSize = 16; |
| 86 | static constexpr int kMaxSize = 64; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 87 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 88 | const test::ScopedFieldTrials scoped_field_trials_; |
| 89 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 90 | Random rand_; |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 91 | std::unique_ptr<SimulatedClock> clock_; |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 92 | rtc::scoped_refptr<PacketBuffer> packet_buffer_; |
| 93 | std::map<uint16_t, std::unique_ptr<RtpFrameObject>> frames_from_callback_; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | TEST_F(TestPacketBuffer, InsertOnePacket) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 97 | const uint16_t seq_num = Rand(); |
| 98 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | TEST_F(TestPacketBuffer, InsertMultiplePackets) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 102 | const uint16_t seq_num = Rand(); |
| 103 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 104 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast)); |
| 105 | EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kFirst, kLast)); |
| 106 | EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(TestPacketBuffer, InsertDuplicatePacket) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 110 | const uint16_t seq_num = Rand(); |
| 111 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 112 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 113 | } |
| 114 | |
philipel | 2c2f34c | 2017-01-03 05:55:34 -0800 | [diff] [blame] | 115 | TEST_F(TestPacketBuffer, SeqNumWrapOneFrame) { |
| 116 | EXPECT_TRUE(Insert(0xFFFF, kKeyFrame, kFirst, kNotLast)); |
| 117 | EXPECT_TRUE(Insert(0x0, kKeyFrame, kNotFirst, kLast)); |
| 118 | |
| 119 | CheckFrame(0xFFFF); |
| 120 | } |
| 121 | |
| 122 | TEST_F(TestPacketBuffer, SeqNumWrapTwoFrames) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 123 | EXPECT_TRUE(Insert(0xFFFF, kKeyFrame, kFirst, kLast)); |
| 124 | EXPECT_TRUE(Insert(0x0, kKeyFrame, kFirst, kLast)); |
| 125 | |
| 126 | CheckFrame(0xFFFF); |
philipel | 2c2f34c | 2017-01-03 05:55:34 -0800 | [diff] [blame] | 127 | CheckFrame(0x0); |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | TEST_F(TestPacketBuffer, InsertOldPackets) { |
| 131 | const uint16_t seq_num = Rand(); |
| 132 | |
| 133 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 134 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast)); |
| 135 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast)); |
| 136 | ASSERT_EQ(2UL, frames_from_callback_.size()); |
| 137 | |
| 138 | frames_from_callback_.erase(seq_num + 2); |
| 139 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 140 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 141 | |
| 142 | frames_from_callback_.erase(frames_from_callback_.find(seq_num)); |
| 143 | ASSERT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 144 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast)); |
| 145 | |
| 146 | packet_buffer_->ClearTo(seq_num + 2); |
| 147 | EXPECT_FALSE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast)); |
| 148 | EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast)); |
| 149 | ASSERT_EQ(2UL, frames_from_callback_.size()); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 150 | } |
| 151 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 152 | TEST_F(TestPacketBuffer, NackCount) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 153 | const uint16_t seq_num = Rand(); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 154 | |
| 155 | VCMPacket packet; |
| 156 | packet.codec = kVideoCodecGeneric; |
| 157 | packet.seqNum = seq_num; |
| 158 | packet.frameType = kVideoFrameKey; |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 159 | packet.is_first_packet_in_frame = true; |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 160 | packet.markerBit = false; |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 161 | packet.timesNacked = 0; |
| 162 | |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 163 | packet_buffer_->InsertPacket(&packet); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 164 | |
| 165 | packet.seqNum++; |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 166 | packet.is_first_packet_in_frame = false; |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 167 | packet.timesNacked = 1; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 168 | packet_buffer_->InsertPacket(&packet); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 169 | |
| 170 | packet.seqNum++; |
| 171 | packet.timesNacked = 3; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 172 | packet_buffer_->InsertPacket(&packet); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 173 | |
| 174 | packet.seqNum++; |
| 175 | packet.markerBit = true; |
| 176 | packet.timesNacked = 1; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 177 | packet_buffer_->InsertPacket(&packet); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 178 | |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 179 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 180 | RtpFrameObject* frame = frames_from_callback_.begin()->second.get(); |
| 181 | EXPECT_EQ(3, frame->times_nacked()); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_F(TestPacketBuffer, FrameSize) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 185 | const uint16_t seq_num = Rand(); |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 186 | uint8_t* data1 = new uint8_t[5](); |
| 187 | uint8_t* data2 = new uint8_t[5](); |
| 188 | uint8_t* data3 = new uint8_t[5](); |
| 189 | uint8_t* data4 = new uint8_t[5](); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 190 | |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 191 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast, 5, data1)); |
| 192 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast, 5, data2)); |
| 193 | EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kNotLast, 5, data3)); |
| 194 | EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, 5, data4)); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 195 | |
| 196 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
nisse | 37abf53 | 2016-10-28 00:37:29 -0700 | [diff] [blame] | 197 | EXPECT_EQ(20UL, frames_from_callback_.begin()->second->size()); |
philipel | 5ceaaae | 2016-05-24 10:20:47 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Ilya Nikolaevskiy | d397a0d | 2018-02-21 15:57:09 +0100 | [diff] [blame] | 200 | TEST_F(TestPacketBuffer, CountsUniqueFrames) { |
| 201 | const uint16_t seq_num = Rand(); |
| 202 | |
| 203 | ASSERT_EQ(0, packet_buffer_->GetUniqueFramesSeen()); |
| 204 | |
| 205 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, 100)); |
| 206 | ASSERT_EQ(1, packet_buffer_->GetUniqueFramesSeen()); |
| 207 | // Still the same frame. |
| 208 | EXPECT_TRUE( |
| 209 | Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100)); |
| 210 | ASSERT_EQ(1, packet_buffer_->GetUniqueFramesSeen()); |
| 211 | |
| 212 | // Second frame. |
| 213 | EXPECT_TRUE( |
| 214 | Insert(seq_num + 2, kKeyFrame, kFirst, kNotLast, 0, nullptr, 200)); |
| 215 | ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen()); |
| 216 | EXPECT_TRUE( |
| 217 | Insert(seq_num + 3, kKeyFrame, kNotFirst, kLast, 0, nullptr, 200)); |
| 218 | ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen()); |
| 219 | |
| 220 | // Old packet. |
| 221 | EXPECT_TRUE( |
| 222 | Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast, 0, nullptr, 100)); |
| 223 | ASSERT_EQ(2, packet_buffer_->GetUniqueFramesSeen()); |
| 224 | |
| 225 | // Missing middle packet. |
| 226 | EXPECT_TRUE( |
| 227 | Insert(seq_num + 4, kKeyFrame, kFirst, kNotLast, 0, nullptr, 300)); |
| 228 | EXPECT_TRUE( |
| 229 | Insert(seq_num + 6, kKeyFrame, kNotFirst, kLast, 0, nullptr, 300)); |
| 230 | ASSERT_EQ(3, packet_buffer_->GetUniqueFramesSeen()); |
| 231 | } |
| 232 | |
| 233 | TEST_F(TestPacketBuffer, HasHistoryOfUniqueFrames) { |
| 234 | const int kNumFrames = 1500; |
| 235 | const int kRequiredHistoryLength = 1000; |
| 236 | const uint16_t seq_num = Rand(); |
| 237 | const uint32_t timestamp = 0xFFFFFFF0; // Large enough to cause wrap-around. |
| 238 | |
| 239 | for (int i = 0; i < kNumFrames; ++i) { |
| 240 | EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr, |
| 241 | timestamp + 10 * i)); |
| 242 | } |
| 243 | ASSERT_EQ(kNumFrames, packet_buffer_->GetUniqueFramesSeen()); |
| 244 | |
| 245 | // Old packets within history should not affect number of seen unique frames. |
| 246 | for (int i = kNumFrames - kRequiredHistoryLength; i < kNumFrames; ++i) { |
| 247 | EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kNotLast, 0, nullptr, |
| 248 | timestamp + 10 * i)); |
| 249 | } |
| 250 | ASSERT_EQ(kNumFrames, packet_buffer_->GetUniqueFramesSeen()); |
| 251 | |
| 252 | // Very old packets should be treated as unique. |
| 253 | EXPECT_TRUE( |
| 254 | Insert(seq_num, kKeyFrame, kFirst, kNotLast, 0, nullptr, timestamp)); |
| 255 | ASSERT_EQ(kNumFrames + 1, packet_buffer_->GetUniqueFramesSeen()); |
| 256 | } |
| 257 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 258 | TEST_F(TestPacketBuffer, ExpandBuffer) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 259 | const uint16_t seq_num = Rand(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 260 | |
| 261 | for (int i = 0; i < kStartSize + 1; ++i) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 262 | EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 266 | TEST_F(TestPacketBuffer, SingleFrameExpandsBuffer) { |
| 267 | const uint16_t seq_num = Rand(); |
| 268 | |
| 269 | 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 | EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kNotFirst, kLast)); |
| 273 | |
| 274 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 275 | CheckFrame(seq_num); |
| 276 | } |
| 277 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 278 | TEST_F(TestPacketBuffer, ExpandBufferOverflow) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 279 | const uint16_t seq_num = Rand(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 280 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 281 | for (int i = 0; i < kMaxSize; ++i) |
| 282 | EXPECT_TRUE(Insert(seq_num + i, kKeyFrame, kFirst, kLast)); |
philipel | c703dc2 | 2017-03-23 06:50:37 -0700 | [diff] [blame] | 283 | EXPECT_TRUE(Insert(seq_num + kMaxSize + 1, kKeyFrame, kFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 284 | } |
| 285 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 286 | TEST_F(TestPacketBuffer, OnePacketOneFrame) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 287 | const uint16_t seq_num = Rand(); |
| 288 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 289 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 290 | CheckFrame(seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 291 | } |
| 292 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 293 | TEST_F(TestPacketBuffer, TwoPacketsTwoFrames) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 294 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 295 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 296 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 297 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 298 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 299 | EXPECT_EQ(2UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 300 | CheckFrame(seq_num); |
| 301 | CheckFrame(seq_num + 1); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 302 | } |
| 303 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 304 | TEST_F(TestPacketBuffer, TwoPacketsOneFrames) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 305 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 306 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 307 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 308 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 309 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 310 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 311 | CheckFrame(seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 312 | } |
| 313 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 314 | TEST_F(TestPacketBuffer, ThreePacketReorderingOneFrame) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 315 | const uint16_t seq_num = Rand(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 316 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 317 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 318 | EXPECT_TRUE(Insert(seq_num + 2, kKeyFrame, kNotFirst, kLast)); |
| 319 | EXPECT_TRUE(Insert(seq_num + 1, kKeyFrame, kNotFirst, kNotLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 320 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 321 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 322 | CheckFrame(seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 323 | } |
| 324 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 325 | TEST_F(TestPacketBuffer, Frames) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 326 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 327 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 328 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 329 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast)); |
| 330 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast)); |
| 331 | EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 332 | |
| 333 | ASSERT_EQ(4UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 334 | CheckFrame(seq_num); |
| 335 | CheckFrame(seq_num + 1); |
| 336 | CheckFrame(seq_num + 2); |
| 337 | CheckFrame(seq_num + 3); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 338 | } |
| 339 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 340 | TEST_F(TestPacketBuffer, ClearSinglePacket) { |
| 341 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 342 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 343 | for (int i = 0; i < kMaxSize; ++i) |
| 344 | EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kFirst, kLast)); |
| 345 | |
| 346 | packet_buffer_->ClearTo(seq_num); |
| 347 | EXPECT_TRUE(Insert(seq_num + kMaxSize, kDeltaFrame, kFirst, kLast)); |
| 348 | } |
| 349 | |
philipel | c5fb468 | 2017-08-02 04:28:57 -0700 | [diff] [blame] | 350 | TEST_F(TestPacketBuffer, ClearFullBuffer) { |
| 351 | for (int i = 0; i < kMaxSize; ++i) |
| 352 | EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast)); |
| 353 | |
| 354 | packet_buffer_->ClearTo(kMaxSize - 1); |
| 355 | |
| 356 | for (int i = kMaxSize; i < 2 * kMaxSize; ++i) |
| 357 | EXPECT_TRUE(Insert(i, kDeltaFrame, kFirst, kLast)); |
| 358 | } |
| 359 | |
| 360 | TEST_F(TestPacketBuffer, DontClearNewerPacket) { |
| 361 | EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kLast)); |
| 362 | packet_buffer_->ClearTo(0); |
| 363 | EXPECT_TRUE(Insert(2 * kStartSize, kKeyFrame, kFirst, kLast)); |
| 364 | EXPECT_TRUE(Insert(3 * kStartSize + 1, kKeyFrame, kFirst, kNotLast)); |
| 365 | packet_buffer_->ClearTo(2 * kStartSize); |
| 366 | EXPECT_TRUE(Insert(3 * kStartSize + 2, kKeyFrame, kNotFirst, kLast)); |
| 367 | |
| 368 | ASSERT_EQ(3UL, frames_from_callback_.size()); |
| 369 | CheckFrame(0); |
| 370 | CheckFrame(2 * kStartSize); |
| 371 | CheckFrame(3 * kStartSize + 1); |
| 372 | } |
| 373 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 374 | TEST_F(TestPacketBuffer, OneIncompleteFrame) { |
| 375 | const uint16_t seq_num = Rand(); |
| 376 | |
| 377 | EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast)); |
| 378 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kLast)); |
| 379 | EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast)); |
| 380 | |
| 381 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 382 | CheckFrame(seq_num); |
| 383 | } |
| 384 | |
| 385 | TEST_F(TestPacketBuffer, TwoIncompleteFramesFullBuffer) { |
| 386 | const uint16_t seq_num = Rand(); |
| 387 | |
| 388 | for (int i = 1; i < kMaxSize - 1; ++i) |
| 389 | EXPECT_TRUE(Insert(seq_num + i, kDeltaFrame, kNotFirst, kNotLast)); |
| 390 | EXPECT_TRUE(Insert(seq_num, kDeltaFrame, kFirst, kNotLast)); |
| 391 | EXPECT_TRUE(Insert(seq_num - 1, kDeltaFrame, kNotFirst, kLast)); |
| 392 | |
| 393 | ASSERT_EQ(0UL, frames_from_callback_.size()); |
| 394 | } |
| 395 | |
| 396 | TEST_F(TestPacketBuffer, FramesReordered) { |
| 397 | const uint16_t seq_num = Rand(); |
| 398 | |
| 399 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kFirst, kLast)); |
| 400 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
| 401 | EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kFirst, kLast)); |
| 402 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 403 | |
| 404 | ASSERT_EQ(4UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 405 | CheckFrame(seq_num); |
| 406 | CheckFrame(seq_num + 1); |
| 407 | CheckFrame(seq_num + 2); |
| 408 | CheckFrame(seq_num + 3); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 409 | } |
| 410 | |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 411 | TEST_F(TestPacketBuffer, GetBitstream) { |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 412 | // "many bitstream, such data" with null termination. |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 413 | uint8_t many_data[] = {0x6d, 0x61, 0x6e, 0x79, 0x20}; |
| 414 | uint8_t bitstream_data[] = {0x62, 0x69, 0x74, 0x73, 0x74, 0x72, |
| 415 | 0x65, 0x61, 0x6d, 0x2c, 0x20}; |
| 416 | uint8_t such_data[] = {0x73, 0x75, 0x63, 0x68, 0x20}; |
| 417 | uint8_t data_data[] = {0x64, 0x61, 0x74, 0x61, 0x0}; |
| 418 | |
| 419 | uint8_t* many = new uint8_t[sizeof(many_data)]; |
| 420 | uint8_t* bitstream = new uint8_t[sizeof(bitstream_data)]; |
| 421 | uint8_t* such = new uint8_t[sizeof(such_data)]; |
| 422 | uint8_t* data = new uint8_t[sizeof(data_data)]; |
| 423 | |
| 424 | memcpy(many, many_data, sizeof(many_data)); |
| 425 | memcpy(bitstream, bitstream_data, sizeof(bitstream_data)); |
| 426 | memcpy(such, such_data, sizeof(such_data)); |
| 427 | memcpy(data, data_data, sizeof(data_data)); |
| 428 | |
| 429 | uint8_t result[sizeof(many_data) + sizeof(bitstream_data) + |
| 430 | sizeof(such_data) + sizeof(data_data)]; |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 431 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 432 | const uint16_t seq_num = Rand(); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 433 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 434 | EXPECT_TRUE( |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 435 | Insert(seq_num, kKeyFrame, kFirst, kNotLast, sizeof(many_data), many)); |
| 436 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast, |
| 437 | sizeof(bitstream_data), bitstream)); |
| 438 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kNotLast, |
| 439 | sizeof(such_data), such)); |
| 440 | EXPECT_TRUE(Insert(seq_num + 3, kDeltaFrame, kNotFirst, kLast, |
| 441 | sizeof(data_data), data)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 442 | |
| 443 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 444 | CheckFrame(seq_num); |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 445 | EXPECT_EQ(frames_from_callback_[seq_num]->size(), sizeof(result)); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 446 | EXPECT_TRUE(frames_from_callback_[seq_num]->GetBitstream(result)); |
philipel | 9b2ce6b | 2016-07-05 05:04:46 -0700 | [diff] [blame] | 447 | EXPECT_EQ(memcmp(result, "many bitstream, such data", sizeof(result)), 0); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 448 | } |
| 449 | |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 450 | TEST_F(TestPacketBuffer, GetBitstreamOneFrameOnePacket) { |
| 451 | uint8_t bitstream_data[] = "All the bitstream data for this frame!"; |
| 452 | uint8_t result[sizeof(bitstream_data)]; |
| 453 | uint8_t* data = new uint8_t[sizeof(bitstream_data)]; |
| 454 | memcpy(data, bitstream_data, sizeof(bitstream_data)); |
| 455 | |
| 456 | EXPECT_TRUE( |
| 457 | Insert(0, kKeyFrame, kFirst, kLast, sizeof(bitstream_data), data)); |
| 458 | |
| 459 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 460 | CheckFrame(0); |
| 461 | EXPECT_EQ(frames_from_callback_[0]->size(), sizeof(bitstream_data)); |
| 462 | EXPECT_TRUE(frames_from_callback_[0]->GetBitstream(result)); |
| 463 | EXPECT_EQ(memcmp(result, data, sizeof(bitstream_data)), 0); |
| 464 | } |
| 465 | |
| 466 | TEST_F(TestPacketBuffer, GetBitstreamOneFrameFullBuffer) { |
| 467 | uint8_t* data_arr[kStartSize]; |
| 468 | uint8_t expected[kStartSize]; |
| 469 | uint8_t result[kStartSize]; |
| 470 | |
| 471 | for (uint8_t i = 0; i < kStartSize; ++i) { |
| 472 | data_arr[i] = new uint8_t[1]; |
| 473 | data_arr[i][0] = i; |
| 474 | expected[i] = i; |
| 475 | } |
| 476 | |
| 477 | EXPECT_TRUE(Insert(0, kKeyFrame, kFirst, kNotLast, 1, data_arr[0])); |
| 478 | for (uint8_t i = 1; i < kStartSize - 1; ++i) |
| 479 | EXPECT_TRUE(Insert(i, kKeyFrame, kNotFirst, kNotLast, 1, data_arr[i])); |
| 480 | EXPECT_TRUE(Insert(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1, |
| 481 | data_arr[kStartSize - 1])); |
| 482 | |
| 483 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 484 | CheckFrame(0); |
| 485 | EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize)); |
| 486 | EXPECT_TRUE(frames_from_callback_[0]->GetBitstream(result)); |
| 487 | EXPECT_EQ(memcmp(result, expected, kStartSize), 0); |
| 488 | } |
| 489 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 490 | // If |sps_pps_idr_is_keyframe| is true, we require keyframes to contain |
| 491 | // SPS/PPS/IDR and the keyframes we create as part of the test do contain |
| 492 | // SPS/PPS/IDR. If |sps_pps_idr_is_keyframe| is false, we only require and |
| 493 | // create keyframes containing only IDR. |
| 494 | class TestPacketBufferH264 : public TestPacketBuffer { |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 495 | protected: |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 496 | explicit TestPacketBufferH264(bool sps_pps_idr_is_keyframe) |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 497 | : TestPacketBuffer(sps_pps_idr_is_keyframe |
| 498 | ? "WebRTC-SpsPpsIdrIsH264Keyframe/Enabled/" |
| 499 | : ""), |
| 500 | sps_pps_idr_is_keyframe_(sps_pps_idr_is_keyframe) {} |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 501 | |
| 502 | bool InsertH264(uint16_t seq_num, // packet sequence number |
| 503 | IsKeyFrame keyframe, // is keyframe |
| 504 | IsFirst first, // is first packet of frame |
| 505 | IsLast last, // is last packet of frame |
| 506 | uint32_t timestamp, // rtp timestamp |
| 507 | int data_size = 0, // size of data |
| 508 | uint8_t* data = nullptr) { // data pointer |
| 509 | VCMPacket packet; |
| 510 | packet.codec = kVideoCodecH264; |
| 511 | packet.seqNum = seq_num; |
| 512 | packet.timestamp = timestamp; |
| 513 | if (keyframe == kKeyFrame) { |
| 514 | if (sps_pps_idr_is_keyframe_) { |
| 515 | packet.video_header.codecHeader.H264.nalus[0].type = |
| 516 | H264::NaluType::kSps; |
| 517 | packet.video_header.codecHeader.H264.nalus[1].type = |
| 518 | H264::NaluType::kPps; |
| 519 | packet.video_header.codecHeader.H264.nalus[2].type = |
| 520 | H264::NaluType::kIdr; |
| 521 | packet.video_header.codecHeader.H264.nalus_length = 3; |
| 522 | } else { |
| 523 | packet.video_header.codecHeader.H264.nalus[0].type = |
| 524 | H264::NaluType::kIdr; |
| 525 | packet.video_header.codecHeader.H264.nalus_length = 1; |
| 526 | } |
| 527 | } |
| 528 | packet.is_first_packet_in_frame = first == kFirst; |
| 529 | packet.markerBit = last == kLast; |
| 530 | packet.sizeBytes = data_size; |
| 531 | packet.dataPtr = data; |
| 532 | |
| 533 | return packet_buffer_->InsertPacket(&packet); |
| 534 | } |
| 535 | |
| 536 | const bool sps_pps_idr_is_keyframe_; |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | // This fixture is used to test the general behaviour of the packet buffer |
| 540 | // in both configurations. |
| 541 | class TestPacketBufferH264Parameterized |
| 542 | : public ::testing::WithParamInterface<bool>, |
| 543 | public TestPacketBufferH264 { |
| 544 | protected: |
| 545 | TestPacketBufferH264Parameterized() : TestPacketBufferH264(GetParam()) {} |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | INSTANTIATE_TEST_CASE_P(SpsPpsIdrIsKeyframe, |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 549 | TestPacketBufferH264Parameterized, |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 550 | ::testing::Values(false, true)); |
| 551 | |
philipel | bc5a408 | 2017-12-06 10:41:08 +0100 | [diff] [blame] | 552 | TEST_P(TestPacketBufferH264Parameterized, DontRemoveMissingPacketOnClearTo) { |
| 553 | EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kLast, 0)); |
| 554 | EXPECT_TRUE(InsertH264(2, kDeltaFrame, kFirst, kNotLast, 2)); |
| 555 | packet_buffer_->ClearTo(0); |
| 556 | EXPECT_TRUE(InsertH264(3, kDeltaFrame, kNotFirst, kLast, 2)); |
| 557 | |
| 558 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 559 | CheckFrame(0); |
| 560 | } |
| 561 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 562 | TEST_P(TestPacketBufferH264Parameterized, GetBitstreamOneFrameFullBuffer) { |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 563 | uint8_t* data_arr[kStartSize]; |
| 564 | uint8_t expected[kStartSize]; |
| 565 | uint8_t result[kStartSize]; |
| 566 | |
| 567 | for (uint8_t i = 0; i < kStartSize; ++i) { |
| 568 | data_arr[i] = new uint8_t[1]; |
| 569 | data_arr[i][0] = i; |
| 570 | expected[i] = i; |
| 571 | } |
| 572 | |
| 573 | EXPECT_TRUE(InsertH264(0, kKeyFrame, kFirst, kNotLast, 1, 1, data_arr[0])); |
| 574 | for (uint8_t i = 1; i < kStartSize - 1; ++i) { |
| 575 | EXPECT_TRUE( |
| 576 | InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1, 1, data_arr[i])); |
| 577 | } |
| 578 | EXPECT_TRUE(InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1, 1, |
| 579 | data_arr[kStartSize - 1])); |
| 580 | |
| 581 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 582 | CheckFrame(0); |
| 583 | EXPECT_EQ(frames_from_callback_[0]->size(), static_cast<size_t>(kStartSize)); |
| 584 | EXPECT_TRUE(frames_from_callback_[0]->GetBitstream(result)); |
| 585 | EXPECT_EQ(memcmp(result, expected, kStartSize), 0); |
| 586 | } |
| 587 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 588 | TEST_P(TestPacketBufferH264Parameterized, GetBitstreamBufferPadding) { |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 589 | uint16_t seq_num = Rand(); |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 590 | uint8_t data_data[] = "some plain old data"; |
| 591 | uint8_t* data = new uint8_t[sizeof(data_data)]; |
| 592 | memcpy(data, data_data, sizeof(data_data)); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 593 | |
| 594 | // EncodedImage::kBufferPaddingBytesH264 is unknown at compile time. |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 595 | std::unique_ptr<uint8_t[]> result( |
| 596 | new uint8_t[sizeof(data_data) + EncodedImage::kBufferPaddingBytesH264]); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 597 | |
| 598 | VCMPacket packet; |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 599 | packet.video_header.codecHeader.H264.nalus_length = 1; |
| 600 | packet.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kIdr; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 601 | packet.seqNum = seq_num; |
| 602 | packet.codec = kVideoCodecH264; |
| 603 | packet.insertStartCode = true; |
| 604 | packet.video_header.codecHeader.H264.packetization_type = kH264SingleNalu; |
| 605 | packet.dataPtr = data; |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 606 | packet.sizeBytes = sizeof(data_data); |
johan | 0d1b2b6 | 2017-01-10 04:21:35 -0800 | [diff] [blame] | 607 | packet.is_first_packet_in_frame = true; |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 608 | packet.markerBit = true; |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 609 | packet_buffer_->InsertPacket(&packet); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 610 | |
| 611 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 612 | EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage()._length, |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 613 | sizeof(data_data)); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 614 | EXPECT_EQ(frames_from_callback_[seq_num]->EncodedImage()._size, |
philipel | 41b8ca0 | 2016-11-07 15:42:24 +0100 | [diff] [blame] | 615 | sizeof(data_data) + EncodedImage::kBufferPaddingBytesH264); |
| 616 | EXPECT_TRUE(frames_from_callback_[seq_num]->GetBitstream(result.get())); |
| 617 | EXPECT_EQ(memcmp(result.get(), data, sizeof(data_data)), 0); |
philipel | 3692845 | 2016-11-07 10:42:36 +0100 | [diff] [blame] | 618 | } |
| 619 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 620 | TEST_F(TestPacketBuffer, FreeSlotsOnFrameDestruction) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 621 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 622 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 623 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 624 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast)); |
| 625 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 626 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 627 | CheckFrame(seq_num); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 628 | |
| 629 | frames_from_callback_.clear(); |
| 630 | |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 631 | // Insert frame that fills the whole buffer. |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 632 | EXPECT_TRUE(Insert(seq_num + 3, kKeyFrame, kFirst, kNotLast)); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 633 | for (int i = 0; i < kMaxSize - 2; ++i) |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 634 | EXPECT_TRUE(Insert(seq_num + i + 4, kDeltaFrame, kNotFirst, kNotLast)); |
| 635 | EXPECT_TRUE(Insert(seq_num + kMaxSize + 2, kKeyFrame, kNotFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 636 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 637 | CheckFrame(seq_num + 3); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 638 | } |
| 639 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 640 | TEST_F(TestPacketBuffer, Clear) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 641 | const uint16_t seq_num = Rand(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 642 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 643 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kNotLast)); |
| 644 | EXPECT_TRUE(Insert(seq_num + 1, kDeltaFrame, kNotFirst, kNotLast)); |
| 645 | EXPECT_TRUE(Insert(seq_num + 2, kDeltaFrame, kNotFirst, kLast)); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 646 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 647 | CheckFrame(seq_num); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 648 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 649 | packet_buffer_->Clear(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 650 | |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 651 | EXPECT_TRUE(Insert(seq_num + kStartSize, kKeyFrame, kFirst, kNotLast)); |
| 652 | EXPECT_TRUE( |
| 653 | Insert(seq_num + kStartSize + 1, kDeltaFrame, kNotFirst, kNotLast)); |
| 654 | EXPECT_TRUE(Insert(seq_num + kStartSize + 2, kDeltaFrame, kNotFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 655 | EXPECT_EQ(2UL, frames_from_callback_.size()); |
philipel | 17deeb4 | 2016-08-11 15:09:26 +0200 | [diff] [blame] | 656 | CheckFrame(seq_num + kStartSize); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 657 | } |
| 658 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 659 | TEST_F(TestPacketBuffer, InvalidateFrameByClearing) { |
philipel | aee3e0e | 2016-11-01 11:45:34 +0100 | [diff] [blame] | 660 | const uint16_t seq_num = Rand(); |
| 661 | |
| 662 | EXPECT_TRUE(Insert(seq_num, kKeyFrame, kFirst, kLast)); |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 663 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 664 | |
philipel | 02447bc | 2016-05-13 06:01:03 -0700 | [diff] [blame] | 665 | packet_buffer_->Clear(); |
philipel | f413933 | 2016-04-20 10:26:34 +0200 | [diff] [blame] | 666 | EXPECT_FALSE(frames_from_callback_.begin()->second->GetBitstream(nullptr)); |
| 667 | } |
| 668 | |
philipel | 20dce34 | 2016-11-28 16:14:57 +0100 | [diff] [blame] | 669 | TEST_F(TestPacketBuffer, FramesAfterClear) { |
| 670 | Insert(9025, kDeltaFrame, kFirst, kLast); |
| 671 | Insert(9024, kKeyFrame, kFirst, kLast); |
| 672 | packet_buffer_->ClearTo(9025); |
| 673 | Insert(9057, kDeltaFrame, kFirst, kLast); |
| 674 | Insert(9026, kDeltaFrame, kFirst, kLast); |
| 675 | |
| 676 | CheckFrame(9024); |
| 677 | CheckFrame(9025); |
| 678 | CheckFrame(9026); |
| 679 | CheckFrame(9057); |
| 680 | } |
| 681 | |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 682 | TEST_F(TestPacketBuffer, DontLeakPayloadData) { |
| 683 | // NOTE! Any eventual leak is suppose to be detected by valgrind |
| 684 | // or any other similar tool. |
| 685 | uint8_t* data1 = new uint8_t[5]; |
| 686 | uint8_t* data2 = new uint8_t[5]; |
| 687 | uint8_t* data3 = new uint8_t[5]; |
| 688 | uint8_t* data4 = new uint8_t[5]; |
| 689 | |
| 690 | // Expected to free data1 upon PacketBuffer destruction. |
| 691 | EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data1)); |
| 692 | |
| 693 | // Expect to free data2 upon insertion. |
| 694 | EXPECT_TRUE(Insert(2, kKeyFrame, kFirst, kNotLast, 5, data2)); |
| 695 | |
| 696 | // Expect to free data3 upon insertion (old packet). |
| 697 | packet_buffer_->ClearTo(1); |
| 698 | EXPECT_FALSE(Insert(1, kKeyFrame, kFirst, kNotLast, 5, data3)); |
| 699 | |
| 700 | // Expect to free data4 upon insertion (packet buffer is full). |
philipel | c703dc2 | 2017-03-23 06:50:37 -0700 | [diff] [blame] | 701 | EXPECT_TRUE(Insert(2 + kMaxSize, kKeyFrame, kFirst, kNotLast, 5, data4)); |
philipel | 759e0b7 | 2016-11-30 01:32:05 -0800 | [diff] [blame] | 702 | } |
| 703 | |
philipel | ea142f8 | 2017-01-11 02:01:56 -0800 | [diff] [blame] | 704 | TEST_F(TestPacketBuffer, ContinuousSeqNumDoubleMarkerBit) { |
| 705 | Insert(2, kKeyFrame, kNotFirst, kNotLast); |
| 706 | Insert(1, kKeyFrame, kFirst, kLast); |
| 707 | frames_from_callback_.clear(); |
| 708 | Insert(3, kKeyFrame, kNotFirst, kLast); |
| 709 | |
| 710 | EXPECT_EQ(0UL, frames_from_callback_.size()); |
| 711 | } |
| 712 | |
philipel | 3184f8e | 2017-05-18 08:08:53 -0700 | [diff] [blame] | 713 | TEST_F(TestPacketBuffer, PacketTimestamps) { |
| 714 | rtc::Optional<int64_t> packet_ms; |
| 715 | rtc::Optional<int64_t> packet_keyframe_ms; |
| 716 | |
| 717 | packet_ms = packet_buffer_->LastReceivedPacketMs(); |
| 718 | packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs(); |
| 719 | EXPECT_FALSE(packet_ms); |
| 720 | EXPECT_FALSE(packet_keyframe_ms); |
| 721 | |
| 722 | int64_t keyframe_ms = clock_->TimeInMilliseconds(); |
| 723 | EXPECT_TRUE(Insert(100, kKeyFrame, kFirst, kLast)); |
| 724 | packet_ms = packet_buffer_->LastReceivedPacketMs(); |
| 725 | packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs(); |
| 726 | EXPECT_TRUE(packet_ms); |
| 727 | EXPECT_TRUE(packet_keyframe_ms); |
| 728 | EXPECT_EQ(keyframe_ms, *packet_ms); |
| 729 | EXPECT_EQ(keyframe_ms, *packet_keyframe_ms); |
| 730 | |
| 731 | clock_->AdvanceTimeMilliseconds(100); |
| 732 | int64_t delta_ms = clock_->TimeInMilliseconds(); |
| 733 | EXPECT_TRUE(Insert(101, kDeltaFrame, kFirst, kLast)); |
| 734 | packet_ms = packet_buffer_->LastReceivedPacketMs(); |
| 735 | packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs(); |
| 736 | EXPECT_TRUE(packet_ms); |
| 737 | EXPECT_TRUE(packet_keyframe_ms); |
| 738 | EXPECT_EQ(delta_ms, *packet_ms); |
| 739 | EXPECT_EQ(keyframe_ms, *packet_keyframe_ms); |
| 740 | |
| 741 | packet_buffer_->Clear(); |
| 742 | packet_ms = packet_buffer_->LastReceivedPacketMs(); |
| 743 | packet_keyframe_ms = packet_buffer_->LastReceivedKeyframePacketMs(); |
| 744 | EXPECT_FALSE(packet_ms); |
| 745 | EXPECT_FALSE(packet_keyframe_ms); |
| 746 | } |
| 747 | |
philipel | 09133af | 2018-05-17 14:11:09 +0200 | [diff] [blame] | 748 | TEST_F(TestPacketBuffer, IncomingCodecChange) { |
| 749 | VCMPacket packet; |
| 750 | packet.is_first_packet_in_frame = true; |
| 751 | packet.markerBit = true; |
| 752 | packet.sizeBytes = 0; |
| 753 | packet.dataPtr = nullptr; |
| 754 | |
| 755 | packet.codec = kVideoCodecVP8; |
| 756 | packet.timestamp = 1; |
| 757 | packet.seqNum = 1; |
| 758 | packet.frameType = kVideoFrameKey; |
| 759 | EXPECT_TRUE(packet_buffer_->InsertPacket(&packet)); |
| 760 | |
| 761 | packet.codec = kVideoCodecH264; |
| 762 | packet.video_header.codecHeader.H264.nalus_length = 1; |
| 763 | packet.timestamp = 3; |
| 764 | packet.seqNum = 3; |
| 765 | EXPECT_TRUE(packet_buffer_->InsertPacket(&packet)); |
| 766 | |
| 767 | packet.codec = kVideoCodecVP8; |
| 768 | packet.timestamp = 2; |
| 769 | packet.seqNum = 2; |
| 770 | packet.frameType = kVideoFrameDelta; |
| 771 | |
| 772 | EXPECT_TRUE(packet_buffer_->InsertPacket(&packet)); |
| 773 | |
| 774 | EXPECT_EQ(3UL, frames_from_callback_.size()); |
| 775 | } |
| 776 | |
| 777 | TEST_F(TestPacketBuffer, TooManyNalusInPacket) { |
| 778 | VCMPacket packet; |
| 779 | packet.codec = kVideoCodecH264; |
| 780 | packet.timestamp = 1; |
| 781 | packet.seqNum = 1; |
| 782 | packet.frameType = kVideoFrameKey; |
| 783 | packet.is_first_packet_in_frame = true; |
| 784 | packet.markerBit = true; |
| 785 | packet.video_header.codecHeader.H264.nalus_length = kMaxNalusPerPacket; |
| 786 | packet.sizeBytes = 0; |
| 787 | packet.dataPtr = nullptr; |
| 788 | EXPECT_TRUE(packet_buffer_->InsertPacket(&packet)); |
| 789 | |
| 790 | EXPECT_EQ(0UL, frames_from_callback_.size()); |
| 791 | } |
| 792 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 793 | TEST_P(TestPacketBufferH264Parameterized, OneFrameFillBuffer) { |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 794 | InsertH264(0, kKeyFrame, kFirst, kNotLast, 1000); |
| 795 | for (int i = 1; i < kStartSize - 1; ++i) |
| 796 | InsertH264(i, kKeyFrame, kNotFirst, kNotLast, 1000); |
| 797 | InsertH264(kStartSize - 1, kKeyFrame, kNotFirst, kLast, 1000); |
| 798 | |
| 799 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
| 800 | CheckFrame(0); |
| 801 | } |
| 802 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 803 | TEST_P(TestPacketBufferH264Parameterized, CreateFramesAfterFilledBuffer) { |
philipel | 227f8b9 | 2017-08-04 06:39:31 -0700 | [diff] [blame] | 804 | InsertH264(kStartSize - 2, kKeyFrame, kFirst, kLast, 0); |
| 805 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 806 | frames_from_callback_.clear(); |
| 807 | |
| 808 | InsertH264(kStartSize, kDeltaFrame, kFirst, kNotLast, 2000); |
| 809 | for (int i = 1; i < kStartSize; ++i) |
| 810 | InsertH264(kStartSize + i, kDeltaFrame, kNotFirst, kNotLast, 2000); |
| 811 | InsertH264(kStartSize + kStartSize, kDeltaFrame, kNotFirst, kLast, 2000); |
| 812 | ASSERT_EQ(0UL, frames_from_callback_.size()); |
| 813 | |
| 814 | InsertH264(kStartSize - 1, kKeyFrame, kFirst, kLast, 1000); |
| 815 | ASSERT_EQ(2UL, frames_from_callback_.size()); |
| 816 | CheckFrame(kStartSize - 1); |
| 817 | CheckFrame(kStartSize); |
| 818 | } |
| 819 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 820 | TEST_P(TestPacketBufferH264Parameterized, OneFrameMaxSeqNum) { |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 821 | InsertH264(65534, kKeyFrame, kFirst, kNotLast, 1000); |
| 822 | InsertH264(65535, kKeyFrame, kNotFirst, kLast, 1000); |
| 823 | |
| 824 | EXPECT_EQ(1UL, frames_from_callback_.size()); |
| 825 | CheckFrame(65534); |
| 826 | } |
| 827 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 828 | TEST_P(TestPacketBufferH264Parameterized, ClearMissingPacketsOnKeyframe) { |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 829 | InsertH264(0, kKeyFrame, kFirst, kLast, 1000); |
| 830 | InsertH264(2, kKeyFrame, kFirst, kLast, 3000); |
| 831 | InsertH264(3, kDeltaFrame, kFirst, kNotLast, 4000); |
| 832 | InsertH264(4, kDeltaFrame, kNotFirst, kLast, 4000); |
| 833 | |
| 834 | ASSERT_EQ(3UL, frames_from_callback_.size()); |
| 835 | |
| 836 | InsertH264(kStartSize + 1, kKeyFrame, kFirst, kLast, 18000); |
| 837 | |
| 838 | ASSERT_EQ(4UL, frames_from_callback_.size()); |
| 839 | CheckFrame(0); |
| 840 | CheckFrame(2); |
| 841 | CheckFrame(3); |
| 842 | CheckFrame(kStartSize + 1); |
| 843 | } |
| 844 | |
Rasmus Brandt | 88f080a | 2017-11-02 14:28:06 +0100 | [diff] [blame] | 845 | TEST_P(TestPacketBufferH264Parameterized, FindFramesOnPadding) { |
philipel | 2c9f9f2 | 2017-06-13 02:47:28 -0700 | [diff] [blame] | 846 | InsertH264(0, kKeyFrame, kFirst, kLast, 1000); |
| 847 | InsertH264(2, kDeltaFrame, kFirst, kLast, 1000); |
| 848 | |
| 849 | ASSERT_EQ(1UL, frames_from_callback_.size()); |
| 850 | packet_buffer_->PaddingReceived(1); |
| 851 | ASSERT_EQ(2UL, frames_from_callback_.size()); |
| 852 | CheckFrame(0); |
| 853 | CheckFrame(2); |
| 854 | } |
| 855 | |
Rasmus Brandt | edf4ff7 | 2017-10-24 10:07:48 +0200 | [diff] [blame] | 856 | class TestPacketBufferH264XIsKeyframe : public TestPacketBufferH264 { |
| 857 | protected: |
| 858 | const uint16_t kSeqNum = 5; |
| 859 | |
| 860 | explicit TestPacketBufferH264XIsKeyframe(bool sps_pps_idr_is_keyframe) |
| 861 | : TestPacketBufferH264(sps_pps_idr_is_keyframe) { |
| 862 | packet_.codec = kVideoCodecH264; |
| 863 | packet_.seqNum = kSeqNum; |
| 864 | |
| 865 | packet_.is_first_packet_in_frame = true; |
| 866 | packet_.markerBit = true; |
| 867 | } |
| 868 | |
| 869 | VCMPacket packet_; |
| 870 | }; |
| 871 | |
| 872 | class TestPacketBufferH264IdrIsKeyframe |
| 873 | : public TestPacketBufferH264XIsKeyframe { |
| 874 | protected: |
| 875 | TestPacketBufferH264IdrIsKeyframe() |
| 876 | : TestPacketBufferH264XIsKeyframe(false) {} |
| 877 | }; |
| 878 | |
| 879 | TEST_F(TestPacketBufferH264IdrIsKeyframe, IdrIsKeyframe) { |
| 880 | packet_.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kIdr; |
| 881 | packet_.video_header.codecHeader.H264.nalus_length = 1; |
| 882 | |
| 883 | packet_buffer_->InsertPacket(&packet_); |
| 884 | |
| 885 | ASSERT_EQ(1u, frames_from_callback_.size()); |
| 886 | EXPECT_EQ(kVideoFrameKey, frames_from_callback_[kSeqNum]->frame_type()); |
| 887 | } |
| 888 | |
| 889 | TEST_F(TestPacketBufferH264IdrIsKeyframe, SpsPpsIdrIsKeyframe) { |
| 890 | packet_.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kSps; |
| 891 | packet_.video_header.codecHeader.H264.nalus[1].type = H264::NaluType::kPps; |
| 892 | packet_.video_header.codecHeader.H264.nalus[2].type = H264::NaluType::kIdr; |
| 893 | packet_.video_header.codecHeader.H264.nalus_length = 3; |
| 894 | |
| 895 | packet_buffer_->InsertPacket(&packet_); |
| 896 | |
| 897 | ASSERT_EQ(1u, frames_from_callback_.size()); |
| 898 | EXPECT_EQ(kVideoFrameKey, frames_from_callback_[kSeqNum]->frame_type()); |
| 899 | } |
| 900 | |
| 901 | class TestPacketBufferH264SpsPpsIdrIsKeyframe |
| 902 | : public TestPacketBufferH264XIsKeyframe { |
| 903 | protected: |
| 904 | TestPacketBufferH264SpsPpsIdrIsKeyframe() |
| 905 | : TestPacketBufferH264XIsKeyframe(true) {} |
| 906 | }; |
| 907 | |
| 908 | TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, IdrIsNotKeyframe) { |
| 909 | packet_.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kIdr; |
| 910 | packet_.video_header.codecHeader.H264.nalus_length = 1; |
| 911 | |
| 912 | packet_buffer_->InsertPacket(&packet_); |
| 913 | |
| 914 | ASSERT_EQ(1u, frames_from_callback_.size()); |
| 915 | EXPECT_EQ(kVideoFrameDelta, frames_from_callback_[5]->frame_type()); |
| 916 | } |
| 917 | |
| 918 | TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIsNotKeyframe) { |
| 919 | packet_.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kSps; |
| 920 | packet_.video_header.codecHeader.H264.nalus[1].type = H264::NaluType::kPps; |
| 921 | packet_.video_header.codecHeader.H264.nalus_length = 2; |
| 922 | |
| 923 | packet_buffer_->InsertPacket(&packet_); |
| 924 | |
| 925 | ASSERT_EQ(1u, frames_from_callback_.size()); |
| 926 | EXPECT_EQ(kVideoFrameDelta, frames_from_callback_[kSeqNum]->frame_type()); |
| 927 | } |
| 928 | |
| 929 | TEST_F(TestPacketBufferH264SpsPpsIdrIsKeyframe, SpsPpsIdrIsKeyframe) { |
| 930 | packet_.video_header.codecHeader.H264.nalus[0].type = H264::NaluType::kSps; |
| 931 | packet_.video_header.codecHeader.H264.nalus[1].type = H264::NaluType::kPps; |
| 932 | packet_.video_header.codecHeader.H264.nalus[2].type = H264::NaluType::kIdr; |
| 933 | packet_.video_header.codecHeader.H264.nalus_length = 3; |
| 934 | |
| 935 | packet_buffer_->InsertPacket(&packet_); |
| 936 | |
| 937 | ASSERT_EQ(1u, frames_from_callback_.size()); |
| 938 | EXPECT_EQ(kVideoFrameKey, frames_from_callback_[kSeqNum]->frame_type()); |
| 939 | } |
| 940 | |
philipel | c707ab7 | 2016-04-01 02:01:54 -0700 | [diff] [blame] | 941 | } // namespace video_coding |
| 942 | } // namespace webrtc |