blob: fe0123e06178e7df5f20c668dbfeadde3da29325 [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
Mirko Bonadei6a489f22019-04-09 15:11:12 +020028using ::testing::_;
29using ::testing::Return;
philipela45102f2017-02-22 05:30:39 -080030
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));
“Michaela8ae4072019-04-24 08:04:34 -050086 MOCK_METHOD2(GetJitterEstimate,
87 int(double rttMultiplier, double jitterEstCapMs));
philipelbe7a9e52016-05-19 12:19:35 +020088};
89
philipele7c891f2018-02-22 14:35:06 +010090class FrameObjectFake : public EncodedFrame {
philipelbe7a9e52016-05-19 12:19:35 +020091 public:
philipelb4d31082016-07-11 08:46:29 -070092 int64_t ReceivedTime() const override { return 0; }
93
94 int64_t RenderTime() const override { return _renderTimeMs; }
philipela45102f2017-02-22 05:30:39 -080095};
96
97class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
98 public:
ilnik6d5b4d62017-08-30 03:32:14 -070099 MOCK_METHOD3(OnCompleteFrame,
100 void(bool is_keyframe,
101 size_t size_bytes,
102 VideoContentType content_type));
philipela45102f2017-02-22 05:30:39 -0800103 MOCK_METHOD1(OnDiscardedPacketsUpdated, void(int discarded_packets));
104 MOCK_METHOD1(OnFrameCountsUpdated, void(const FrameCounts& frame_counts));
105 MOCK_METHOD7(OnFrameBufferTimingsUpdated,
106 void(int decode_ms,
107 int max_decode_ms,
108 int current_delay_ms,
109 int target_delay_ms,
110 int jitter_buffer_ms,
111 int min_playout_delay_ms,
112 int render_delay_ms));
ilnik2edc6842017-07-06 03:06:50 -0700113 MOCK_METHOD1(OnTimingFrameInfoUpdated, void(const TimingFrameInfo& info));
philipelbe7a9e52016-05-19 12:19:35 +0200114};
115
116class TestFrameBuffer2 : public ::testing::Test {
117 protected:
118 static constexpr int kMaxReferences = 5;
119 static constexpr int kFps1 = 1000;
120 static constexpr int kFps10 = kFps1 / 10;
121 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100122 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200123
124 TestFrameBuffer2()
125 : clock_(0),
126 timing_(&clock_),
127 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200128 buffer_(new FrameBuffer(&clock_,
129 &jitter_estimator_,
130 &timing_,
131 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200132 rand_(0x34678213),
133 tear_down_(false),
Niels Möllerc572ff32018-11-07 08:43:50 +0100134 extract_thread_(&ExtractLoop, this, "Extract Thread") {}
philipelbe7a9e52016-05-19 12:19:35 +0200135
136 void SetUp() override { extract_thread_.Start(); }
137
138 void TearDown() override {
139 tear_down_ = true;
140 trigger_extract_event_.Set();
141 extract_thread_.Stop();
142 }
143
144 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200145 int InsertFrame(uint16_t picture_id,
146 uint8_t spatial_layer,
147 int64_t ts_ms,
148 bool inter_layer_predicted,
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100149 bool last_spatial_layer,
philipele0b2f152016-09-28 10:23:49 +0200150 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200151 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100152 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700153 std::array<uint16_t, sizeof...(refs)> references = {
154 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200155
philipelb4d31082016-07-11 08:46:29 -0700156 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100157 frame->id.picture_id = picture_id;
158 frame->id.spatial_layer = spatial_layer;
Sergey Silkin61832dd2018-12-20 14:32:14 +0100159 frame->SetSpatialIndex(spatial_layer);
Niels Möller23775882018-08-16 10:24:12 +0200160 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200161 frame->num_references = references.size();
162 frame->inter_layer_predicted = inter_layer_predicted;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100163 frame->is_last_spatial_layer = last_spatial_layer;
164 // Add some data to buffer.
165 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100166 frame->set_size(kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200167 for (size_t r = 0; r < references.size(); ++r)
168 frame->references[r] = references[r];
169
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200170 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200171 }
172
philipel3042c2d2017-08-18 04:55:02 -0700173 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipelbe7a9e52016-05-19 12:19:35 +0200174 crit_.Enter();
175 if (max_wait_time == 0) {
philipele7c891f2018-02-22 14:35:06 +0100176 std::unique_ptr<EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700177 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200178 buffer_->NextFrame(0, &frame, keyframe_required);
philipel75562822016-09-05 10:57:41 +0200179 if (res != FrameBuffer::ReturnReason::kStopped)
180 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200181 crit_.Leave();
182 } else {
183 max_wait_time_ = max_wait_time;
184 trigger_extract_event_.Set();
185 crit_.Leave();
186 // Make sure |crit_| is aquired by |extract_thread_| before returning.
187 crit_acquired_event_.Wait(rtc::Event::kForever);
188 }
189 }
190
191 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
192 rtc::CritScope lock(&crit_);
193 ASSERT_LT(index, frames_.size());
194 ASSERT_TRUE(frames_[index]);
philipel0fa82a62018-03-19 15:34:53 +0100195 ASSERT_EQ(picture_id, frames_[index]->id.picture_id);
196 ASSERT_EQ(spatial_layer, frames_[index]->id.spatial_layer);
philipelbe7a9e52016-05-19 12:19:35 +0200197 }
198
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100199 void CheckFrameSize(size_t index, size_t size) {
200 rtc::CritScope lock(&crit_);
201 ASSERT_LT(index, frames_.size());
202 ASSERT_TRUE(frames_[index]);
203 ASSERT_EQ(frames_[index]->size(), size);
204 }
205
philipelbe7a9e52016-05-19 12:19:35 +0200206 void CheckNoFrame(size_t index) {
207 rtc::CritScope lock(&crit_);
208 ASSERT_LT(index, frames_.size());
209 ASSERT_FALSE(frames_[index]);
210 }
211
tommi0f8b4032017-02-22 11:22:05 -0800212 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200213 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
214 while (true) {
215 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
216 {
217 rtc::CritScope lock(&tfb->crit_);
218 tfb->crit_acquired_event_.Set();
219 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800220 return;
philipelbe7a9e52016-05-19 12:19:35 +0200221
philipele7c891f2018-02-22 14:35:06 +0100222 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200223 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200224 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200225 if (res != FrameBuffer::ReturnReason::kStopped)
226 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200227 }
228 }
229 }
230
231 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
232
233 SimulatedClock clock_;
234 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200235 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200236 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100237 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200238 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800239 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200240
241 int64_t max_wait_time_;
242 bool tear_down_;
243 rtc::PlatformThread extract_thread_;
244 rtc::Event trigger_extract_event_;
245 rtc::Event crit_acquired_event_;
246 rtc::CriticalSection crit_;
247};
248
philipel6e8224f2016-05-19 17:07:44 +0200249// Following tests are timing dependent. Either the timeouts have to
250// be increased by a large margin, which would slow down all trybots,
251// or we disable them for the very slow ones, like we do here.
252#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200253TEST_F(TestFrameBuffer2, WaitForFrame) {
254 uint16_t pid = Rand();
255 uint32_t ts = Rand();
256
philipel6e8224f2016-05-19 17:07:44 +0200257 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100258 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200259 CheckFrame(0, pid, 0);
260}
261
262TEST_F(TestFrameBuffer2, OneSuperFrame) {
263 uint16_t pid = Rand();
264 uint32_t ts = Rand();
265
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100266 InsertFrame(pid, 0, ts, false, false);
267 InsertFrame(pid, 1, ts, true, true);
philipele0b2f152016-09-28 10:23:49 +0200268 ExtractFrame();
269
Sergey Silkin61832dd2018-12-20 14:32:14 +0100270 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200271}
272
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200273TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
274 VCMTiming timing(&clock_);
275 buffer_.reset(
276 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
277 const PlayoutDelay kPlayoutDelayMs = {0, 0};
278 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
279 test_frame->id.picture_id = 0;
280 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
281 buffer_->InsertFrame(std::move(test_frame));
282 ExtractFrame(0, false);
283 CheckFrame(0, 0, 0);
284 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
285}
286
aleloi3e005282017-01-26 05:38:00 -0800287// Flaky test, see bugs.webrtc.org/7068.
288TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200289 uint16_t pid = Rand();
290 uint32_t ts = Rand();
291
philipel6e8224f2016-05-19 17:07:44 +0200292 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100293 InsertFrame(pid, 1, ts, true, true);
294 InsertFrame(pid, 0, ts, false, false);
philipelbe7a9e52016-05-19 12:19:35 +0200295 ExtractFrame();
296
297 CheckFrame(0, pid, 0);
298 CheckFrame(1, pid, 1);
299}
300
philipel94616b32016-05-20 10:43:01 +0200301TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200302 uint16_t pid = Rand();
303 uint32_t ts = Rand();
304
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100305 InsertFrame(pid, 0, ts, false, true);
philipel6e8224f2016-05-19 17:07:44 +0200306 ExtractFrame();
307 CheckFrame(0, pid, 0);
308 for (int i = 1; i < 10; i += 2) {
309 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100310 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, true, pid + i);
philipel6e8224f2016-05-19 17:07:44 +0200311 clock_.AdvanceTimeMilliseconds(kFps10);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100312 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipel6e8224f2016-05-19 17:07:44 +0200313 clock_.AdvanceTimeMilliseconds(kFps10);
314 ExtractFrame();
315 CheckFrame(i, pid + i, 0);
316 CheckFrame(i + 1, pid + i + 1, 0);
317 }
318}
319#endif // Timing dependent tests.
320
321TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
322 ExtractFrame();
323 CheckNoFrame(0);
324}
325
philipel93e451b2016-10-06 12:25:13 +0200326TEST_F(TestFrameBuffer2, MissingFrame) {
327 uint16_t pid = Rand();
328 uint32_t ts = Rand();
329
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100330 InsertFrame(pid, 0, ts, false, true);
331 InsertFrame(pid + 2, 0, ts, false, true, pid);
332 InsertFrame(pid + 3, 0, ts, false, true, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200333 ExtractFrame();
334 ExtractFrame();
335 ExtractFrame();
336
337 CheckFrame(0, pid, 0);
338 CheckFrame(1, pid + 2, 0);
339 CheckNoFrame(2);
340}
341
philipelbe7a9e52016-05-19 12:19:35 +0200342TEST_F(TestFrameBuffer2, OneLayerStream) {
343 uint16_t pid = Rand();
344 uint32_t ts = Rand();
345
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100346 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200347 ExtractFrame();
348 CheckFrame(0, pid, 0);
349 for (int i = 1; i < 10; ++i) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100350 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200351 ExtractFrame();
352 clock_.AdvanceTimeMilliseconds(kFps10);
353 CheckFrame(i, pid + i, 0);
354 }
355}
356
philipelbe7a9e52016-05-19 12:19:35 +0200357TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
358 uint16_t pid = Rand();
359 uint32_t ts = Rand();
360
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100361 InsertFrame(pid, 0, ts, false, true);
362 InsertFrame(pid + 1, 0, ts + kFps20, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200363 for (int i = 2; i < 10; i += 2) {
364 uint32_t ts_tl0 = ts + i / 2 * kFps10;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100365 InsertFrame(pid + i, 0, ts_tl0, false, true, pid + i - 2);
366 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, true, pid + i,
367 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200368 }
369
370 for (int i = 0; i < 10; ++i) {
371 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100372 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200373 }
374
375 CheckFrame(0, pid, 0);
376 CheckFrame(1, pid + 1, 0);
377 CheckFrame(2, pid + 2, 0);
378 CheckFrame(3, pid + 4, 0);
379 CheckFrame(4, pid + 6, 0);
380 CheckFrame(5, pid + 8, 0);
381 CheckNoFrame(6);
382 CheckNoFrame(7);
383 CheckNoFrame(8);
384 CheckNoFrame(9);
385}
386
philipelbe7a9e52016-05-19 12:19:35 +0200387TEST_F(TestFrameBuffer2, InsertLateFrame) {
388 uint16_t pid = Rand();
389 uint32_t ts = Rand();
390
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100391 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200392 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100393 InsertFrame(pid + 2, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200394 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100395 InsertFrame(pid + 1, 0, ts, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200396 ExtractFrame();
397
398 CheckFrame(0, pid, 0);
399 CheckFrame(1, pid + 2, 0);
400 CheckNoFrame(2);
401}
402
philipel4f6cd6a2016-08-03 10:59:32 +0200403TEST_F(TestFrameBuffer2, ProtectionMode) {
404 uint16_t pid = Rand();
405 uint32_t ts = Rand();
406
“Michaela8ae4072019-04-24 08:04:34 -0500407 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0, 300.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100408 InsertFrame(pid, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200409 ExtractFrame();
410
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200411 buffer_->SetProtectionMode(kProtectionNackFEC);
“Michaela8ae4072019-04-24 08:04:34 -0500412 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0, 300.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100413 InsertFrame(pid + 1, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200414 ExtractFrame();
415}
416
philipele0b2f152016-09-28 10:23:49 +0200417TEST_F(TestFrameBuffer2, NoContinuousFrame) {
418 uint16_t pid = Rand();
419 uint32_t ts = Rand();
420
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100421 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, true, pid));
philipele0b2f152016-09-28 10:23:49 +0200422}
423
424TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
425 uint16_t pid = Rand();
426 uint32_t ts = Rand();
427
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100428 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
429 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, true, pid + 1));
430 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, true, pid));
431 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, true, pid + 3));
432 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false, true));
philipele0b2f152016-09-28 10:23:49 +0200433}
434
435TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
436 uint16_t pid = Rand();
437 uint32_t ts = Rand();
438
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100439 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, false));
440 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true, true));
441 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, true, pid));
442 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, false, pid + 1));
443 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, true, pid + 1));
444 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, false, pid + 2));
445 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, false, pid));
446 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, true, pid + 2));
philipele0b2f152016-09-28 10:23:49 +0200447}
448
philipelfcc60062017-01-18 05:35:20 -0800449TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
450 uint16_t pid = Rand();
451 uint32_t ts = Rand();
452
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100453 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
454 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, true, pid));
philipelfcc60062017-01-18 05:35:20 -0800455 ExtractFrame();
456 CheckFrame(0, pid, 0);
457
458 // Jump back in pid but increase ts.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100459 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false, true));
philipelfcc60062017-01-18 05:35:20 -0800460 ExtractFrame();
461 ExtractFrame();
462 CheckFrame(1, pid - 1, 0);
463 CheckNoFrame(2);
464}
465
philipela45102f2017-02-22 05:30:39 -0800466TEST_F(TestFrameBuffer2, StatsCallback) {
467 uint16_t pid = Rand();
468 uint32_t ts = Rand();
469 const int kFrameSize = 5000;
470
ilnik6d5b4d62017-08-30 03:32:14 -0700471 EXPECT_CALL(stats_callback_,
472 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800473 EXPECT_CALL(stats_callback_,
474 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
475
476 {
477 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100478 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100479 frame->set_size(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100480 frame->id.picture_id = pid;
481 frame->id.spatial_layer = 0;
Niels Möller23775882018-08-16 10:24:12 +0200482 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800483 frame->num_references = 0;
484 frame->inter_layer_predicted = false;
485
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200486 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800487 }
488
489 ExtractFrame();
490 CheckFrame(0, pid, 0);
491}
492
philipel146a48b2017-04-20 04:04:38 -0700493TEST_F(TestFrameBuffer2, ForwardJumps) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100494 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700495 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100496 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, true, 5453));
philipel146a48b2017-04-20 04:04:38 -0700497 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100498 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700499 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100500 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700501 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100502 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, true, 29804));
philipel146a48b2017-04-20 04:04:38 -0700503 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100504 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, true, 29805));
philipel146a48b2017-04-20 04:04:38 -0700505 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100506 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700507 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100508 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700509 ExtractFrame();
510}
511
philipelf6842692017-04-28 03:29:15 -0700512TEST_F(TestFrameBuffer2, DuplicateFrames) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100513 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700514 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100515 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700516}
517
philipel112adf92017-06-15 09:06:21 -0700518// TODO(philipel): implement more unittests related to invalid references.
519TEST_F(TestFrameBuffer2, InvalidReferences) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100520 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, true, 2));
521 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false, true));
philipel112adf92017-06-15 09:06:21 -0700522 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100523 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, true, 1));
philipel112adf92017-06-15 09:06:21 -0700524}
525
philipel3042c2d2017-08-18 04:55:02 -0700526TEST_F(TestFrameBuffer2, KeyframeRequired) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100527 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false, true));
528 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, true, 1));
529 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false, true));
philipel3042c2d2017-08-18 04:55:02 -0700530 ExtractFrame();
531 ExtractFrame(0, true);
532 ExtractFrame();
533
534 CheckFrame(0, 1, 0);
535 CheckFrame(1, 3, 0);
536 CheckNoFrame(2);
537}
538
philipel9771c502018-03-02 11:06:27 +0100539TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
540 const int kMaxBufferSize = 600;
541
542 for (int i = 1; i <= kMaxBufferSize; ++i)
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100543 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, true, i - 1));
philipel9771c502018-03-02 11:06:27 +0100544 ExtractFrame();
545 CheckNoFrame(0);
546
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100547 EXPECT_EQ(kMaxBufferSize + 1,
548 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
549 false, true));
philipel9771c502018-03-02 11:06:27 +0100550 ExtractFrame();
551 CheckFrame(1, kMaxBufferSize + 1, 0);
552}
553
philipel798b2822018-06-11 13:10:14 +0200554TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100555 InsertFrame(1, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200556 ExtractFrame(0, true);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100557 InsertFrame(3, 0, 0, false, true, 2, 0);
558 InsertFrame(3, 0, 0, false, true, 0);
559 InsertFrame(2, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200560 ExtractFrame(0, true);
561 ExtractFrame(0, true);
562}
563
philipel6d216502018-10-22 14:36:45 +0200564TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100565 InsertFrame(2, 0, 1, false, true);
566 InsertFrame(1, 0, 2, false, true); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200567 ExtractFrame(0);
568 ExtractFrame(0);
569 CheckFrame(0, 1, 0);
570 CheckNoFrame(1);
571
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100572 InsertFrame(3, 0, 4, false, true);
573 InsertFrame(4, 0, 3, false, true); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200574 ExtractFrame(0);
575 ExtractFrame(0);
576 CheckFrame(2, 3, 0);
577 CheckNoFrame(3);
578}
579
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100580TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
581 uint16_t pid = Rand();
582 uint32_t ts = Rand();
583
584 InsertFrame(pid, 0, ts, false, false);
585 InsertFrame(pid, 1, ts, true, true);
586 ExtractFrame(0);
587 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100588 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100589 CheckNoFrame(1);
590 // Two frames should be combined and returned together.
591 CheckFrameSize(0, kFrameSize * 2);
592}
593
594TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
595 uint16_t pid = Rand();
596 uint32_t ts = Rand();
597
598 InsertFrame(pid, 0, ts, false, false);
599 InsertFrame(pid, 1, ts, true, true);
600
601 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100602 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100603
604 InsertFrame(pid + 1, 1, ts + kFps20, false, true, pid);
605 InsertFrame(pid + 2, 0, ts + kFps10, false, false, pid);
606 InsertFrame(pid + 2, 1, ts + kFps10, true, true, pid + 1);
607
608 clock_.AdvanceTimeMilliseconds(1000);
609 // Frame pid+1 is decodable but too late.
610 // In superframe pid+2 frame sid=0 is decodable, but frame sid=1 is not.
611 // Incorrect implementation might skip pid+1 frame and output undecodable
612 // pid+2 instead.
613 ExtractFrame();
614 ExtractFrame();
615 CheckFrame(1, pid + 1, 1);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100616 CheckFrame(2, pid + 2, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100617}
618
philipelbe7a9e52016-05-19 12:19:35 +0200619} // namespace video_coding
620} // namespace webrtc