blob: 53482699d3f78d8fe61c8b07df91e6457d88d728 [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:
ilnik6d5b4d62017-08-30 03:32:14 -070098 MOCK_METHOD3(OnCompleteFrame,
99 void(bool is_keyframe,
100 size_t size_bytes,
101 VideoContentType content_type));
philipela45102f2017-02-22 05:30:39 -0800102 MOCK_METHOD1(OnDiscardedPacketsUpdated, void(int discarded_packets));
103 MOCK_METHOD1(OnFrameCountsUpdated, void(const FrameCounts& frame_counts));
104 MOCK_METHOD7(OnFrameBufferTimingsUpdated,
105 void(int decode_ms,
106 int max_decode_ms,
107 int current_delay_ms,
108 int target_delay_ms,
109 int jitter_buffer_ms,
110 int min_playout_delay_ms,
111 int render_delay_ms));
ilnik2edc6842017-07-06 03:06:50 -0700112 MOCK_METHOD1(OnTimingFrameInfoUpdated, void(const TimingFrameInfo& info));
philipelbe7a9e52016-05-19 12:19:35 +0200113};
114
115class TestFrameBuffer2 : public ::testing::Test {
116 protected:
117 static constexpr int kMaxReferences = 5;
118 static constexpr int kFps1 = 1000;
119 static constexpr int kFps10 = kFps1 / 10;
120 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100121 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200122
123 TestFrameBuffer2()
124 : clock_(0),
125 timing_(&clock_),
126 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200127 buffer_(new FrameBuffer(&clock_,
128 &jitter_estimator_,
129 &timing_,
130 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200131 rand_(0x34678213),
132 tear_down_(false),
Niels Möllerc572ff32018-11-07 08:43:50 +0100133 extract_thread_(&ExtractLoop, this, "Extract Thread") {}
philipelbe7a9e52016-05-19 12:19:35 +0200134
135 void SetUp() override { extract_thread_.Start(); }
136
137 void TearDown() override {
138 tear_down_ = true;
139 trigger_extract_event_.Set();
140 extract_thread_.Stop();
141 }
142
143 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200144 int InsertFrame(uint16_t picture_id,
145 uint8_t spatial_layer,
146 int64_t ts_ms,
147 bool inter_layer_predicted,
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100148 bool last_spatial_layer,
philipele0b2f152016-09-28 10:23:49 +0200149 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200150 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100151 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700152 std::array<uint16_t, sizeof...(refs)> references = {
153 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200154
philipelb4d31082016-07-11 08:46:29 -0700155 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100156 frame->id.picture_id = picture_id;
157 frame->id.spatial_layer = spatial_layer;
Sergey Silkin61832dd2018-12-20 14:32:14 +0100158 frame->SetSpatialIndex(spatial_layer);
Niels Möller23775882018-08-16 10:24:12 +0200159 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200160 frame->num_references = references.size();
161 frame->inter_layer_predicted = inter_layer_predicted;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100162 frame->is_last_spatial_layer = last_spatial_layer;
163 // Add some data to buffer.
164 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100165 frame->set_size(kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200166 for (size_t r = 0; r < references.size(); ++r)
167 frame->references[r] = references[r];
168
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200169 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200170 }
171
philipel3042c2d2017-08-18 04:55:02 -0700172 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipelbe7a9e52016-05-19 12:19:35 +0200173 crit_.Enter();
174 if (max_wait_time == 0) {
philipele7c891f2018-02-22 14:35:06 +0100175 std::unique_ptr<EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700176 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200177 buffer_->NextFrame(0, &frame, keyframe_required);
philipel75562822016-09-05 10:57:41 +0200178 if (res != FrameBuffer::ReturnReason::kStopped)
179 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200180 crit_.Leave();
181 } else {
182 max_wait_time_ = max_wait_time;
183 trigger_extract_event_.Set();
184 crit_.Leave();
185 // Make sure |crit_| is aquired by |extract_thread_| before returning.
186 crit_acquired_event_.Wait(rtc::Event::kForever);
187 }
188 }
189
190 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
191 rtc::CritScope lock(&crit_);
192 ASSERT_LT(index, frames_.size());
193 ASSERT_TRUE(frames_[index]);
philipel0fa82a62018-03-19 15:34:53 +0100194 ASSERT_EQ(picture_id, frames_[index]->id.picture_id);
195 ASSERT_EQ(spatial_layer, frames_[index]->id.spatial_layer);
philipelbe7a9e52016-05-19 12:19:35 +0200196 }
197
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100198 void CheckFrameSize(size_t index, size_t size) {
199 rtc::CritScope lock(&crit_);
200 ASSERT_LT(index, frames_.size());
201 ASSERT_TRUE(frames_[index]);
202 ASSERT_EQ(frames_[index]->size(), size);
203 }
204
philipelbe7a9e52016-05-19 12:19:35 +0200205 void CheckNoFrame(size_t index) {
206 rtc::CritScope lock(&crit_);
207 ASSERT_LT(index, frames_.size());
208 ASSERT_FALSE(frames_[index]);
209 }
210
tommi0f8b4032017-02-22 11:22:05 -0800211 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200212 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
213 while (true) {
214 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
215 {
216 rtc::CritScope lock(&tfb->crit_);
217 tfb->crit_acquired_event_.Set();
218 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800219 return;
philipelbe7a9e52016-05-19 12:19:35 +0200220
philipele7c891f2018-02-22 14:35:06 +0100221 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200222 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200223 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200224 if (res != FrameBuffer::ReturnReason::kStopped)
225 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200226 }
227 }
228 }
229
230 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
231
232 SimulatedClock clock_;
233 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200234 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200235 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100236 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200237 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800238 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200239
240 int64_t max_wait_time_;
241 bool tear_down_;
242 rtc::PlatformThread extract_thread_;
243 rtc::Event trigger_extract_event_;
244 rtc::Event crit_acquired_event_;
245 rtc::CriticalSection crit_;
246};
247
philipel6e8224f2016-05-19 17:07:44 +0200248// Following tests are timing dependent. Either the timeouts have to
249// be increased by a large margin, which would slow down all trybots,
250// or we disable them for the very slow ones, like we do here.
251#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200252TEST_F(TestFrameBuffer2, WaitForFrame) {
253 uint16_t pid = Rand();
254 uint32_t ts = Rand();
255
philipel6e8224f2016-05-19 17:07:44 +0200256 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100257 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200258 CheckFrame(0, pid, 0);
259}
260
261TEST_F(TestFrameBuffer2, OneSuperFrame) {
262 uint16_t pid = Rand();
263 uint32_t ts = Rand();
264
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100265 InsertFrame(pid, 0, ts, false, false);
266 InsertFrame(pid, 1, ts, true, true);
philipele0b2f152016-09-28 10:23:49 +0200267 ExtractFrame();
268
Sergey Silkin61832dd2018-12-20 14:32:14 +0100269 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200270}
271
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200272TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
273 VCMTiming timing(&clock_);
274 buffer_.reset(
275 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
276 const PlayoutDelay kPlayoutDelayMs = {0, 0};
277 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
278 test_frame->id.picture_id = 0;
279 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
280 buffer_->InsertFrame(std::move(test_frame));
281 ExtractFrame(0, false);
282 CheckFrame(0, 0, 0);
283 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
284}
285
aleloi3e005282017-01-26 05:38:00 -0800286// Flaky test, see bugs.webrtc.org/7068.
287TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200288 uint16_t pid = Rand();
289 uint32_t ts = Rand();
290
philipel6e8224f2016-05-19 17:07:44 +0200291 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100292 InsertFrame(pid, 1, ts, true, true);
293 InsertFrame(pid, 0, ts, false, false);
philipelbe7a9e52016-05-19 12:19:35 +0200294 ExtractFrame();
295
296 CheckFrame(0, pid, 0);
297 CheckFrame(1, pid, 1);
298}
299
philipel94616b32016-05-20 10:43:01 +0200300TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200301 uint16_t pid = Rand();
302 uint32_t ts = Rand();
303
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100304 InsertFrame(pid, 0, ts, false, true);
philipel6e8224f2016-05-19 17:07:44 +0200305 ExtractFrame();
306 CheckFrame(0, pid, 0);
307 for (int i = 1; i < 10; i += 2) {
308 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100309 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, true, pid + i);
philipel6e8224f2016-05-19 17:07:44 +0200310 clock_.AdvanceTimeMilliseconds(kFps10);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100311 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipel6e8224f2016-05-19 17:07:44 +0200312 clock_.AdvanceTimeMilliseconds(kFps10);
313 ExtractFrame();
314 CheckFrame(i, pid + i, 0);
315 CheckFrame(i + 1, pid + i + 1, 0);
316 }
317}
318#endif // Timing dependent tests.
319
320TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
321 ExtractFrame();
322 CheckNoFrame(0);
323}
324
philipel93e451b2016-10-06 12:25:13 +0200325TEST_F(TestFrameBuffer2, MissingFrame) {
326 uint16_t pid = Rand();
327 uint32_t ts = Rand();
328
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100329 InsertFrame(pid, 0, ts, false, true);
330 InsertFrame(pid + 2, 0, ts, false, true, pid);
331 InsertFrame(pid + 3, 0, ts, false, true, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200332 ExtractFrame();
333 ExtractFrame();
334 ExtractFrame();
335
336 CheckFrame(0, pid, 0);
337 CheckFrame(1, pid + 2, 0);
338 CheckNoFrame(2);
339}
340
philipelbe7a9e52016-05-19 12:19:35 +0200341TEST_F(TestFrameBuffer2, OneLayerStream) {
342 uint16_t pid = Rand();
343 uint32_t ts = Rand();
344
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100345 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200346 ExtractFrame();
347 CheckFrame(0, pid, 0);
348 for (int i = 1; i < 10; ++i) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100349 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200350 ExtractFrame();
351 clock_.AdvanceTimeMilliseconds(kFps10);
352 CheckFrame(i, pid + i, 0);
353 }
354}
355
philipelbe7a9e52016-05-19 12:19:35 +0200356TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
357 uint16_t pid = Rand();
358 uint32_t ts = Rand();
359
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100360 InsertFrame(pid, 0, ts, false, true);
361 InsertFrame(pid + 1, 0, ts + kFps20, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200362 for (int i = 2; i < 10; i += 2) {
363 uint32_t ts_tl0 = ts + i / 2 * kFps10;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100364 InsertFrame(pid + i, 0, ts_tl0, false, true, pid + i - 2);
365 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, true, pid + i,
366 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200367 }
368
369 for (int i = 0; i < 10; ++i) {
370 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100371 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200372 }
373
374 CheckFrame(0, pid, 0);
375 CheckFrame(1, pid + 1, 0);
376 CheckFrame(2, pid + 2, 0);
377 CheckFrame(3, pid + 4, 0);
378 CheckFrame(4, pid + 6, 0);
379 CheckFrame(5, pid + 8, 0);
380 CheckNoFrame(6);
381 CheckNoFrame(7);
382 CheckNoFrame(8);
383 CheckNoFrame(9);
384}
385
philipelbe7a9e52016-05-19 12:19:35 +0200386TEST_F(TestFrameBuffer2, InsertLateFrame) {
387 uint16_t pid = Rand();
388 uint32_t ts = Rand();
389
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100390 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200391 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100392 InsertFrame(pid + 2, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200393 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100394 InsertFrame(pid + 1, 0, ts, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200395 ExtractFrame();
396
397 CheckFrame(0, pid, 0);
398 CheckFrame(1, pid + 2, 0);
399 CheckNoFrame(2);
400}
401
philipel4f6cd6a2016-08-03 10:59:32 +0200402TEST_F(TestFrameBuffer2, ProtectionMode) {
403 uint16_t pid = Rand();
404 uint32_t ts = Rand();
405
406 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100407 InsertFrame(pid, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200408 ExtractFrame();
409
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200410 buffer_->SetProtectionMode(kProtectionNackFEC);
philipel4f6cd6a2016-08-03 10:59:32 +0200411 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100412 InsertFrame(pid + 1, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200413 ExtractFrame();
414}
415
philipele0b2f152016-09-28 10:23:49 +0200416TEST_F(TestFrameBuffer2, NoContinuousFrame) {
417 uint16_t pid = Rand();
418 uint32_t ts = Rand();
419
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100420 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, true, pid));
philipele0b2f152016-09-28 10:23:49 +0200421}
422
423TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
424 uint16_t pid = Rand();
425 uint32_t ts = Rand();
426
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100427 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
428 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, true, pid + 1));
429 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, true, pid));
430 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, true, pid + 3));
431 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false, true));
philipele0b2f152016-09-28 10:23:49 +0200432}
433
434TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
435 uint16_t pid = Rand();
436 uint32_t ts = Rand();
437
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100438 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, false));
439 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true, true));
440 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, true, pid));
441 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, false, pid + 1));
442 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, true, pid + 1));
443 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, false, pid + 2));
444 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, false, pid));
445 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, true, pid + 2));
philipele0b2f152016-09-28 10:23:49 +0200446}
447
philipelfcc60062017-01-18 05:35:20 -0800448TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
449 uint16_t pid = Rand();
450 uint32_t ts = Rand();
451
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100452 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
453 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, true, pid));
philipelfcc60062017-01-18 05:35:20 -0800454 ExtractFrame();
455 CheckFrame(0, pid, 0);
456
457 // Jump back in pid but increase ts.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100458 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false, true));
philipelfcc60062017-01-18 05:35:20 -0800459 ExtractFrame();
460 ExtractFrame();
461 CheckFrame(1, pid - 1, 0);
462 CheckNoFrame(2);
463}
464
philipela45102f2017-02-22 05:30:39 -0800465TEST_F(TestFrameBuffer2, StatsCallback) {
466 uint16_t pid = Rand();
467 uint32_t ts = Rand();
468 const int kFrameSize = 5000;
469
ilnik6d5b4d62017-08-30 03:32:14 -0700470 EXPECT_CALL(stats_callback_,
471 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800472 EXPECT_CALL(stats_callback_,
473 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
474
475 {
476 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100477 frame->VerifyAndAllocate(kFrameSize);
Niels Möller77536a22019-01-15 08:50:01 +0100478 frame->set_size(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100479 frame->id.picture_id = pid;
480 frame->id.spatial_layer = 0;
Niels Möller23775882018-08-16 10:24:12 +0200481 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800482 frame->num_references = 0;
483 frame->inter_layer_predicted = false;
484
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200485 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800486 }
487
488 ExtractFrame();
489 CheckFrame(0, pid, 0);
490}
491
philipel146a48b2017-04-20 04:04:38 -0700492TEST_F(TestFrameBuffer2, ForwardJumps) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100493 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700494 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100495 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, true, 5453));
philipel146a48b2017-04-20 04:04:38 -0700496 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100497 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700498 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100499 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700500 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100501 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, true, 29804));
philipel146a48b2017-04-20 04:04:38 -0700502 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100503 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, true, 29805));
philipel146a48b2017-04-20 04:04:38 -0700504 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100505 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700506 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100507 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700508 ExtractFrame();
509}
510
philipelf6842692017-04-28 03:29:15 -0700511TEST_F(TestFrameBuffer2, DuplicateFrames) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100512 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700513 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100514 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700515}
516
philipel112adf92017-06-15 09:06:21 -0700517// TODO(philipel): implement more unittests related to invalid references.
518TEST_F(TestFrameBuffer2, InvalidReferences) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100519 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, true, 2));
520 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false, true));
philipel112adf92017-06-15 09:06:21 -0700521 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100522 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, true, 1));
philipel112adf92017-06-15 09:06:21 -0700523}
524
philipel3042c2d2017-08-18 04:55:02 -0700525TEST_F(TestFrameBuffer2, KeyframeRequired) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100526 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false, true));
527 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, true, 1));
528 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false, true));
philipel3042c2d2017-08-18 04:55:02 -0700529 ExtractFrame();
530 ExtractFrame(0, true);
531 ExtractFrame();
532
533 CheckFrame(0, 1, 0);
534 CheckFrame(1, 3, 0);
535 CheckNoFrame(2);
536}
537
philipel9771c502018-03-02 11:06:27 +0100538TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
539 const int kMaxBufferSize = 600;
540
541 for (int i = 1; i <= kMaxBufferSize; ++i)
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100542 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, true, i - 1));
philipel9771c502018-03-02 11:06:27 +0100543 ExtractFrame();
544 CheckNoFrame(0);
545
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100546 EXPECT_EQ(kMaxBufferSize + 1,
547 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
548 false, true));
philipel9771c502018-03-02 11:06:27 +0100549 ExtractFrame();
550 CheckFrame(1, kMaxBufferSize + 1, 0);
551}
552
philipel798b2822018-06-11 13:10:14 +0200553TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100554 InsertFrame(1, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200555 ExtractFrame(0, true);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100556 InsertFrame(3, 0, 0, false, true, 2, 0);
557 InsertFrame(3, 0, 0, false, true, 0);
558 InsertFrame(2, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200559 ExtractFrame(0, true);
560 ExtractFrame(0, true);
561}
562
philipel6d216502018-10-22 14:36:45 +0200563TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100564 InsertFrame(2, 0, 1, false, true);
565 InsertFrame(1, 0, 2, false, true); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200566 ExtractFrame(0);
567 ExtractFrame(0);
568 CheckFrame(0, 1, 0);
569 CheckNoFrame(1);
570
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100571 InsertFrame(3, 0, 4, false, true);
572 InsertFrame(4, 0, 3, false, true); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200573 ExtractFrame(0);
574 ExtractFrame(0);
575 CheckFrame(2, 3, 0);
576 CheckNoFrame(3);
577}
578
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100579TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
580 uint16_t pid = Rand();
581 uint32_t ts = Rand();
582
583 InsertFrame(pid, 0, ts, false, false);
584 InsertFrame(pid, 1, ts, true, true);
585 ExtractFrame(0);
586 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100587 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100588 CheckNoFrame(1);
589 // Two frames should be combined and returned together.
590 CheckFrameSize(0, kFrameSize * 2);
591}
592
593TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
594 uint16_t pid = Rand();
595 uint32_t ts = Rand();
596
597 InsertFrame(pid, 0, ts, false, false);
598 InsertFrame(pid, 1, ts, true, true);
599
600 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100601 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100602
603 InsertFrame(pid + 1, 1, ts + kFps20, false, true, pid);
604 InsertFrame(pid + 2, 0, ts + kFps10, false, false, pid);
605 InsertFrame(pid + 2, 1, ts + kFps10, true, true, pid + 1);
606
607 clock_.AdvanceTimeMilliseconds(1000);
608 // Frame pid+1 is decodable but too late.
609 // In superframe pid+2 frame sid=0 is decodable, but frame sid=1 is not.
610 // Incorrect implementation might skip pid+1 frame and output undecodable
611 // pid+2 instead.
612 ExtractFrame();
613 ExtractFrame();
614 CheckFrame(1, pid + 1, 1);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100615 CheckFrame(2, pid + 2, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100616}
617
philipelbe7a9e52016-05-19 12:19:35 +0200618} // namespace video_coding
619} // namespace webrtc