blob: 7dc8cb29ed1c372674bd1e330992d696277f81b4 [file] [log] [blame]
philipelbe7a9e52016-05-19 12:19:35 +02001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/video_coding/frame_buffer2.h"
philipelbe7a9e52016-05-19 12:19:35 +020012
13#include <algorithm>
14#include <cstring>
15#include <limits>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/frame_object.h"
19#include "modules/video_coding/jitter_estimator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/video_coding/timing.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020021#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/platform_thread.h"
23#include "rtc_base/random.h"
24#include "system_wrappers/include/clock.h"
25#include "test/gmock.h"
26#include "test/gtest.h"
philipelbe7a9e52016-05-19 12:19:35 +020027
philipela45102f2017-02-22 05:30:39 -080028using testing::_;
29using testing::Return;
30
philipelbe7a9e52016-05-19 12:19:35 +020031namespace webrtc {
32namespace video_coding {
33
34class VCMTimingFake : public VCMTiming {
35 public:
36 explicit VCMTimingFake(Clock* clock) : VCMTiming(clock) {}
37
38 int64_t RenderTimeMs(uint32_t frame_timestamp,
39 int64_t now_ms) const override {
40 if (last_ms_ == -1) {
41 last_ms_ = now_ms + kDelayMs;
42 last_timestamp_ = frame_timestamp;
43 }
44
45 uint32_t diff = MinDiff(frame_timestamp, last_timestamp_);
46 if (AheadOf(frame_timestamp, last_timestamp_))
47 last_ms_ += diff / 90;
48 else
49 last_ms_ -= diff / 90;
50
51 last_timestamp_ = frame_timestamp;
52 return last_ms_;
53 }
54
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +010055 int64_t MaxWaitingTime(int64_t render_time_ms,
56 int64_t now_ms) const override {
57 return render_time_ms - now_ms - kDecodeTime;
philipelbe7a9e52016-05-19 12:19:35 +020058 }
59
philipela45102f2017-02-22 05:30:39 -080060 bool GetTimings(int* decode_ms,
61 int* max_decode_ms,
62 int* current_delay_ms,
63 int* target_delay_ms,
64 int* jitter_buffer_ms,
65 int* min_playout_delay_ms,
66 int* render_delay_ms) const override {
67 return true;
68 }
69
philipelbe7a9e52016-05-19 12:19:35 +020070 private:
71 static constexpr int kDelayMs = 50;
72 static constexpr int kDecodeTime = kDelayMs / 2;
73 mutable uint32_t last_timestamp_ = 0;
74 mutable int64_t last_ms_ = -1;
75};
76
77class VCMJitterEstimatorMock : public VCMJitterEstimator {
78 public:
79 explicit VCMJitterEstimatorMock(Clock* clock) : VCMJitterEstimator(clock) {}
80
81 MOCK_METHOD1(UpdateRtt, void(int64_t rttMs));
82 MOCK_METHOD3(UpdateEstimate,
83 void(int64_t frameDelayMs,
84 uint32_t frameSizeBytes,
85 bool incompleteFrame));
philipel4f6cd6a2016-08-03 10:59:32 +020086 MOCK_METHOD1(GetJitterEstimate, int(double rttMultiplier));
philipelbe7a9e52016-05-19 12:19:35 +020087};
88
philipele7c891f2018-02-22 14:35:06 +010089class FrameObjectFake : public EncodedFrame {
philipelbe7a9e52016-05-19 12:19:35 +020090 public:
philipelb4d31082016-07-11 08:46:29 -070091 int64_t ReceivedTime() const override { return 0; }
92
93 int64_t RenderTime() const override { return _renderTimeMs; }
philipela45102f2017-02-22 05:30:39 -080094};
95
96class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
97 public:
98 MOCK_METHOD2(OnReceiveRatesUpdated,
99 void(uint32_t bitRate, uint32_t frameRate));
ilnik6d5b4d62017-08-30 03:32:14 -0700100 MOCK_METHOD3(OnCompleteFrame,
101 void(bool is_keyframe,
102 size_t size_bytes,
103 VideoContentType content_type));
philipela45102f2017-02-22 05:30:39 -0800104 MOCK_METHOD1(OnDiscardedPacketsUpdated, void(int discarded_packets));
105 MOCK_METHOD1(OnFrameCountsUpdated, void(const FrameCounts& frame_counts));
106 MOCK_METHOD7(OnFrameBufferTimingsUpdated,
107 void(int decode_ms,
108 int max_decode_ms,
109 int current_delay_ms,
110 int target_delay_ms,
111 int jitter_buffer_ms,
112 int min_playout_delay_ms,
113 int render_delay_ms));
ilnik2edc6842017-07-06 03:06:50 -0700114 MOCK_METHOD1(OnTimingFrameInfoUpdated, void(const TimingFrameInfo& info));
philipelbe7a9e52016-05-19 12:19:35 +0200115};
116
117class TestFrameBuffer2 : public ::testing::Test {
118 protected:
119 static constexpr int kMaxReferences = 5;
120 static constexpr int kFps1 = 1000;
121 static constexpr int kFps10 = kFps1 / 10;
122 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100123 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200124
125 TestFrameBuffer2()
126 : clock_(0),
127 timing_(&clock_),
128 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200129 buffer_(new FrameBuffer(&clock_,
130 &jitter_estimator_,
131 &timing_,
132 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200133 rand_(0x34678213),
134 tear_down_(false),
Niels Möllerc572ff32018-11-07 08:43:50 +0100135 extract_thread_(&ExtractLoop, this, "Extract Thread") {}
philipelbe7a9e52016-05-19 12:19:35 +0200136
137 void SetUp() override { extract_thread_.Start(); }
138
139 void TearDown() override {
140 tear_down_ = true;
141 trigger_extract_event_.Set();
142 extract_thread_.Stop();
143 }
144
145 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200146 int InsertFrame(uint16_t picture_id,
147 uint8_t spatial_layer,
148 int64_t ts_ms,
149 bool inter_layer_predicted,
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100150 bool last_spatial_layer,
philipele0b2f152016-09-28 10:23:49 +0200151 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200152 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100153 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700154 std::array<uint16_t, sizeof...(refs)> references = {
155 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200156
philipelb4d31082016-07-11 08:46:29 -0700157 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100158 frame->id.picture_id = picture_id;
159 frame->id.spatial_layer = spatial_layer;
Sergey Silkin61832dd2018-12-20 14:32:14 +0100160 frame->SetSpatialIndex(spatial_layer);
Niels Möller23775882018-08-16 10:24:12 +0200161 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200162 frame->num_references = references.size();
163 frame->inter_layer_predicted = inter_layer_predicted;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100164 frame->is_last_spatial_layer = last_spatial_layer;
165 // Add some data to buffer.
166 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100167 frame->set_size(kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200168 for (size_t r = 0; r < references.size(); ++r)
169 frame->references[r] = references[r];
170
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200171 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200172 }
173
philipel3042c2d2017-08-18 04:55:02 -0700174 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipelbe7a9e52016-05-19 12:19:35 +0200175 crit_.Enter();
176 if (max_wait_time == 0) {
philipele7c891f2018-02-22 14:35:06 +0100177 std::unique_ptr<EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700178 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200179 buffer_->NextFrame(0, &frame, keyframe_required);
philipel75562822016-09-05 10:57:41 +0200180 if (res != FrameBuffer::ReturnReason::kStopped)
181 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200182 crit_.Leave();
183 } else {
184 max_wait_time_ = max_wait_time;
185 trigger_extract_event_.Set();
186 crit_.Leave();
187 // Make sure |crit_| is aquired by |extract_thread_| before returning.
188 crit_acquired_event_.Wait(rtc::Event::kForever);
189 }
190 }
191
192 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
193 rtc::CritScope lock(&crit_);
194 ASSERT_LT(index, frames_.size());
195 ASSERT_TRUE(frames_[index]);
philipel0fa82a62018-03-19 15:34:53 +0100196 ASSERT_EQ(picture_id, frames_[index]->id.picture_id);
197 ASSERT_EQ(spatial_layer, frames_[index]->id.spatial_layer);
philipelbe7a9e52016-05-19 12:19:35 +0200198 }
199
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100200 void CheckFrameSize(size_t index, size_t size) {
201 rtc::CritScope lock(&crit_);
202 ASSERT_LT(index, frames_.size());
203 ASSERT_TRUE(frames_[index]);
204 ASSERT_EQ(frames_[index]->size(), size);
205 }
206
philipelbe7a9e52016-05-19 12:19:35 +0200207 void CheckNoFrame(size_t index) {
208 rtc::CritScope lock(&crit_);
209 ASSERT_LT(index, frames_.size());
210 ASSERT_FALSE(frames_[index]);
211 }
212
tommi0f8b4032017-02-22 11:22:05 -0800213 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200214 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
215 while (true) {
216 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
217 {
218 rtc::CritScope lock(&tfb->crit_);
219 tfb->crit_acquired_event_.Set();
220 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800221 return;
philipelbe7a9e52016-05-19 12:19:35 +0200222
philipele7c891f2018-02-22 14:35:06 +0100223 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200224 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200225 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200226 if (res != FrameBuffer::ReturnReason::kStopped)
227 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200228 }
229 }
230 }
231
232 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
233
234 SimulatedClock clock_;
235 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200236 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200237 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100238 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200239 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800240 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200241
242 int64_t max_wait_time_;
243 bool tear_down_;
244 rtc::PlatformThread extract_thread_;
245 rtc::Event trigger_extract_event_;
246 rtc::Event crit_acquired_event_;
247 rtc::CriticalSection crit_;
248};
249
philipel6e8224f2016-05-19 17:07:44 +0200250// Following tests are timing dependent. Either the timeouts have to
251// be increased by a large margin, which would slow down all trybots,
252// or we disable them for the very slow ones, like we do here.
253#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200254TEST_F(TestFrameBuffer2, WaitForFrame) {
255 uint16_t pid = Rand();
256 uint32_t ts = Rand();
257
philipel6e8224f2016-05-19 17:07:44 +0200258 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100259 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200260 CheckFrame(0, pid, 0);
261}
262
263TEST_F(TestFrameBuffer2, OneSuperFrame) {
264 uint16_t pid = Rand();
265 uint32_t ts = Rand();
266
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100267 InsertFrame(pid, 0, ts, false, false);
268 InsertFrame(pid, 1, ts, true, true);
philipele0b2f152016-09-28 10:23:49 +0200269 ExtractFrame();
270
Sergey Silkin61832dd2018-12-20 14:32:14 +0100271 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200272}
273
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200274TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
275 VCMTiming timing(&clock_);
276 buffer_.reset(
277 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
278 const PlayoutDelay kPlayoutDelayMs = {0, 0};
279 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
280 test_frame->id.picture_id = 0;
281 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
282 buffer_->InsertFrame(std::move(test_frame));
283 ExtractFrame(0, false);
284 CheckFrame(0, 0, 0);
285 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
286}
287
aleloi3e005282017-01-26 05:38:00 -0800288// Flaky test, see bugs.webrtc.org/7068.
289TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200290 uint16_t pid = Rand();
291 uint32_t ts = Rand();
292
philipel6e8224f2016-05-19 17:07:44 +0200293 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100294 InsertFrame(pid, 1, ts, true, true);
295 InsertFrame(pid, 0, ts, false, false);
philipelbe7a9e52016-05-19 12:19:35 +0200296 ExtractFrame();
297
298 CheckFrame(0, pid, 0);
299 CheckFrame(1, pid, 1);
300}
301
philipel94616b32016-05-20 10:43:01 +0200302TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200303 uint16_t pid = Rand();
304 uint32_t ts = Rand();
305
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100306 InsertFrame(pid, 0, ts, false, true);
philipel6e8224f2016-05-19 17:07:44 +0200307 ExtractFrame();
308 CheckFrame(0, pid, 0);
309 for (int i = 1; i < 10; i += 2) {
310 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100311 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, true, pid + i);
philipel6e8224f2016-05-19 17:07:44 +0200312 clock_.AdvanceTimeMilliseconds(kFps10);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100313 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipel6e8224f2016-05-19 17:07:44 +0200314 clock_.AdvanceTimeMilliseconds(kFps10);
315 ExtractFrame();
316 CheckFrame(i, pid + i, 0);
317 CheckFrame(i + 1, pid + i + 1, 0);
318 }
319}
320#endif // Timing dependent tests.
321
322TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
323 ExtractFrame();
324 CheckNoFrame(0);
325}
326
philipel93e451b2016-10-06 12:25:13 +0200327TEST_F(TestFrameBuffer2, MissingFrame) {
328 uint16_t pid = Rand();
329 uint32_t ts = Rand();
330
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100331 InsertFrame(pid, 0, ts, false, true);
332 InsertFrame(pid + 2, 0, ts, false, true, pid);
333 InsertFrame(pid + 3, 0, ts, false, true, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200334 ExtractFrame();
335 ExtractFrame();
336 ExtractFrame();
337
338 CheckFrame(0, pid, 0);
339 CheckFrame(1, pid + 2, 0);
340 CheckNoFrame(2);
341}
342
philipelbe7a9e52016-05-19 12:19:35 +0200343TEST_F(TestFrameBuffer2, OneLayerStream) {
344 uint16_t pid = Rand();
345 uint32_t ts = Rand();
346
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100347 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200348 ExtractFrame();
349 CheckFrame(0, pid, 0);
350 for (int i = 1; i < 10; ++i) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100351 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200352 ExtractFrame();
353 clock_.AdvanceTimeMilliseconds(kFps10);
354 CheckFrame(i, pid + i, 0);
355 }
356}
357
philipelbe7a9e52016-05-19 12:19:35 +0200358TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
359 uint16_t pid = Rand();
360 uint32_t ts = Rand();
361
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100362 InsertFrame(pid, 0, ts, false, true);
363 InsertFrame(pid + 1, 0, ts + kFps20, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200364 for (int i = 2; i < 10; i += 2) {
365 uint32_t ts_tl0 = ts + i / 2 * kFps10;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100366 InsertFrame(pid + i, 0, ts_tl0, false, true, pid + i - 2);
367 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, true, pid + i,
368 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200369 }
370
371 for (int i = 0; i < 10; ++i) {
372 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100373 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200374 }
375
376 CheckFrame(0, pid, 0);
377 CheckFrame(1, pid + 1, 0);
378 CheckFrame(2, pid + 2, 0);
379 CheckFrame(3, pid + 4, 0);
380 CheckFrame(4, pid + 6, 0);
381 CheckFrame(5, pid + 8, 0);
382 CheckNoFrame(6);
383 CheckNoFrame(7);
384 CheckNoFrame(8);
385 CheckNoFrame(9);
386}
387
philipelbe7a9e52016-05-19 12:19:35 +0200388TEST_F(TestFrameBuffer2, InsertLateFrame) {
389 uint16_t pid = Rand();
390 uint32_t ts = Rand();
391
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100392 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200393 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100394 InsertFrame(pid + 2, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200395 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100396 InsertFrame(pid + 1, 0, ts, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200397 ExtractFrame();
398
399 CheckFrame(0, pid, 0);
400 CheckFrame(1, pid + 2, 0);
401 CheckNoFrame(2);
402}
403
philipel4f6cd6a2016-08-03 10:59:32 +0200404TEST_F(TestFrameBuffer2, ProtectionMode) {
405 uint16_t pid = Rand();
406 uint32_t ts = Rand();
407
408 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100409 InsertFrame(pid, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200410 ExtractFrame();
411
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200412 buffer_->SetProtectionMode(kProtectionNackFEC);
philipel4f6cd6a2016-08-03 10:59:32 +0200413 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100414 InsertFrame(pid + 1, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200415 ExtractFrame();
416}
417
philipele0b2f152016-09-28 10:23:49 +0200418TEST_F(TestFrameBuffer2, NoContinuousFrame) {
419 uint16_t pid = Rand();
420 uint32_t ts = Rand();
421
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100422 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, true, pid));
philipele0b2f152016-09-28 10:23:49 +0200423}
424
425TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
426 uint16_t pid = Rand();
427 uint32_t ts = Rand();
428
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100429 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
430 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, true, pid + 1));
431 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, true, pid));
432 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, true, pid + 3));
433 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false, true));
philipele0b2f152016-09-28 10:23:49 +0200434}
435
436TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
437 uint16_t pid = Rand();
438 uint32_t ts = Rand();
439
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100440 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, false));
441 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true, true));
442 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, true, pid));
443 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, false, pid + 1));
444 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, true, pid + 1));
445 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, false, pid + 2));
446 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, false, pid));
447 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, true, pid + 2));
philipele0b2f152016-09-28 10:23:49 +0200448}
449
philipelfcc60062017-01-18 05:35:20 -0800450TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
451 uint16_t pid = Rand();
452 uint32_t ts = Rand();
453
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100454 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
455 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, true, pid));
philipelfcc60062017-01-18 05:35:20 -0800456 ExtractFrame();
457 CheckFrame(0, pid, 0);
458
459 // Jump back in pid but increase ts.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100460 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false, true));
philipelfcc60062017-01-18 05:35:20 -0800461 ExtractFrame();
462 ExtractFrame();
463 CheckFrame(1, pid - 1, 0);
464 CheckNoFrame(2);
465}
466
philipela45102f2017-02-22 05:30:39 -0800467TEST_F(TestFrameBuffer2, StatsCallback) {
468 uint16_t pid = Rand();
469 uint32_t ts = Rand();
470 const int kFrameSize = 5000;
471
ilnik6d5b4d62017-08-30 03:32:14 -0700472 EXPECT_CALL(stats_callback_,
473 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800474 EXPECT_CALL(stats_callback_,
475 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
476
477 {
478 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100479 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100480 frame->set_size(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100481 frame->id.picture_id = pid;
482 frame->id.spatial_layer = 0;
Niels Möller23775882018-08-16 10:24:12 +0200483 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800484 frame->num_references = 0;
485 frame->inter_layer_predicted = false;
486
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200487 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800488 }
489
490 ExtractFrame();
491 CheckFrame(0, pid, 0);
492}
493
philipel146a48b2017-04-20 04:04:38 -0700494TEST_F(TestFrameBuffer2, ForwardJumps) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100495 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700496 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100497 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, true, 5453));
philipel146a48b2017-04-20 04:04:38 -0700498 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100499 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700500 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100501 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700502 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100503 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, true, 29804));
philipel146a48b2017-04-20 04:04:38 -0700504 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100505 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, true, 29805));
philipel146a48b2017-04-20 04:04:38 -0700506 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100507 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700508 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100509 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700510 ExtractFrame();
511}
512
philipelf6842692017-04-28 03:29:15 -0700513TEST_F(TestFrameBuffer2, DuplicateFrames) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100514 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700515 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100516 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700517}
518
philipel112adf92017-06-15 09:06:21 -0700519// TODO(philipel): implement more unittests related to invalid references.
520TEST_F(TestFrameBuffer2, InvalidReferences) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100521 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, true, 2));
522 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false, true));
philipel112adf92017-06-15 09:06:21 -0700523 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100524 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, true, 1));
philipel112adf92017-06-15 09:06:21 -0700525}
526
philipel3042c2d2017-08-18 04:55:02 -0700527TEST_F(TestFrameBuffer2, KeyframeRequired) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100528 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false, true));
529 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, true, 1));
530 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false, true));
philipel3042c2d2017-08-18 04:55:02 -0700531 ExtractFrame();
532 ExtractFrame(0, true);
533 ExtractFrame();
534
535 CheckFrame(0, 1, 0);
536 CheckFrame(1, 3, 0);
537 CheckNoFrame(2);
538}
539
philipel9771c502018-03-02 11:06:27 +0100540TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
541 const int kMaxBufferSize = 600;
542
543 for (int i = 1; i <= kMaxBufferSize; ++i)
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100544 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, true, i - 1));
philipel9771c502018-03-02 11:06:27 +0100545 ExtractFrame();
546 CheckNoFrame(0);
547
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100548 EXPECT_EQ(kMaxBufferSize + 1,
549 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
550 false, true));
philipel9771c502018-03-02 11:06:27 +0100551 ExtractFrame();
552 CheckFrame(1, kMaxBufferSize + 1, 0);
553}
554
philipel798b2822018-06-11 13:10:14 +0200555TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100556 InsertFrame(1, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200557 ExtractFrame(0, true);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100558 InsertFrame(3, 0, 0, false, true, 2, 0);
559 InsertFrame(3, 0, 0, false, true, 0);
560 InsertFrame(2, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200561 ExtractFrame(0, true);
562 ExtractFrame(0, true);
563}
564
philipel6d216502018-10-22 14:36:45 +0200565TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100566 InsertFrame(2, 0, 1, false, true);
567 InsertFrame(1, 0, 2, false, true); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200568 ExtractFrame(0);
569 ExtractFrame(0);
570 CheckFrame(0, 1, 0);
571 CheckNoFrame(1);
572
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100573 InsertFrame(3, 0, 4, false, true);
574 InsertFrame(4, 0, 3, false, true); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200575 ExtractFrame(0);
576 ExtractFrame(0);
577 CheckFrame(2, 3, 0);
578 CheckNoFrame(3);
579}
580
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100581TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
582 uint16_t pid = Rand();
583 uint32_t ts = Rand();
584
585 InsertFrame(pid, 0, ts, false, false);
586 InsertFrame(pid, 1, ts, true, true);
587 ExtractFrame(0);
588 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100589 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100590 CheckNoFrame(1);
591 // Two frames should be combined and returned together.
592 CheckFrameSize(0, kFrameSize * 2);
593}
594
595TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
596 uint16_t pid = Rand();
597 uint32_t ts = Rand();
598
599 InsertFrame(pid, 0, ts, false, false);
600 InsertFrame(pid, 1, ts, true, true);
601
602 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100603 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100604
605 InsertFrame(pid + 1, 1, ts + kFps20, false, true, pid);
606 InsertFrame(pid + 2, 0, ts + kFps10, false, false, pid);
607 InsertFrame(pid + 2, 1, ts + kFps10, true, true, pid + 1);
608
609 clock_.AdvanceTimeMilliseconds(1000);
610 // Frame pid+1 is decodable but too late.
611 // In superframe pid+2 frame sid=0 is decodable, but frame sid=1 is not.
612 // Incorrect implementation might skip pid+1 frame and output undecodable
613 // pid+2 instead.
614 ExtractFrame();
615 ExtractFrame();
616 CheckFrame(1, pid + 1, 1);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100617 CheckFrame(2, pid + 2, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100618}
619
philipelbe7a9e52016-05-19 12:19:35 +0200620} // namespace video_coding
621} // namespace webrtc