blob: 578734c6159f1414a12bf6eb9c47c0b463bbbdd9 [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 // In EncodedImage |_length| is used to descibe its size and |_size| to
96 // describe its capacity.
97 void SetSize(int size) { _length = size; }
98};
99
100class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
101 public:
102 MOCK_METHOD2(OnReceiveRatesUpdated,
103 void(uint32_t bitRate, uint32_t frameRate));
ilnik6d5b4d62017-08-30 03:32:14 -0700104 MOCK_METHOD3(OnCompleteFrame,
105 void(bool is_keyframe,
106 size_t size_bytes,
107 VideoContentType content_type));
philipela45102f2017-02-22 05:30:39 -0800108 MOCK_METHOD1(OnDiscardedPacketsUpdated, void(int discarded_packets));
109 MOCK_METHOD1(OnFrameCountsUpdated, void(const FrameCounts& frame_counts));
110 MOCK_METHOD7(OnFrameBufferTimingsUpdated,
111 void(int decode_ms,
112 int max_decode_ms,
113 int current_delay_ms,
114 int target_delay_ms,
115 int jitter_buffer_ms,
116 int min_playout_delay_ms,
117 int render_delay_ms));
ilnik2edc6842017-07-06 03:06:50 -0700118 MOCK_METHOD1(OnTimingFrameInfoUpdated, void(const TimingFrameInfo& info));
philipelbe7a9e52016-05-19 12:19:35 +0200119};
120
121class TestFrameBuffer2 : public ::testing::Test {
122 protected:
123 static constexpr int kMaxReferences = 5;
124 static constexpr int kFps1 = 1000;
125 static constexpr int kFps10 = kFps1 / 10;
126 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100127 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200128
129 TestFrameBuffer2()
130 : clock_(0),
131 timing_(&clock_),
132 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200133 buffer_(new FrameBuffer(&clock_,
134 &jitter_estimator_,
135 &timing_,
136 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200137 rand_(0x34678213),
138 tear_down_(false),
Niels Möllerc572ff32018-11-07 08:43:50 +0100139 extract_thread_(&ExtractLoop, this, "Extract Thread") {}
philipelbe7a9e52016-05-19 12:19:35 +0200140
141 void SetUp() override { extract_thread_.Start(); }
142
143 void TearDown() override {
144 tear_down_ = true;
145 trigger_extract_event_.Set();
146 extract_thread_.Stop();
147 }
148
149 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200150 int InsertFrame(uint16_t picture_id,
151 uint8_t spatial_layer,
152 int64_t ts_ms,
153 bool inter_layer_predicted,
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100154 bool last_spatial_layer,
philipele0b2f152016-09-28 10:23:49 +0200155 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200156 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100157 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700158 std::array<uint16_t, sizeof...(refs)> references = {
159 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200160
philipelb4d31082016-07-11 08:46:29 -0700161 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100162 frame->id.picture_id = picture_id;
163 frame->id.spatial_layer = spatial_layer;
Niels Möller23775882018-08-16 10:24:12 +0200164 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200165 frame->num_references = references.size();
166 frame->inter_layer_predicted = inter_layer_predicted;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100167 frame->is_last_spatial_layer = last_spatial_layer;
168 // Add some data to buffer.
169 frame->VerifyAndAllocate(kFrameSize);
170 frame->SetSize(kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200171 for (size_t r = 0; r < references.size(); ++r)
172 frame->references[r] = references[r];
173
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200174 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200175 }
176
philipel3042c2d2017-08-18 04:55:02 -0700177 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipelbe7a9e52016-05-19 12:19:35 +0200178 crit_.Enter();
179 if (max_wait_time == 0) {
philipele7c891f2018-02-22 14:35:06 +0100180 std::unique_ptr<EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700181 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200182 buffer_->NextFrame(0, &frame, keyframe_required);
philipel75562822016-09-05 10:57:41 +0200183 if (res != FrameBuffer::ReturnReason::kStopped)
184 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200185 crit_.Leave();
186 } else {
187 max_wait_time_ = max_wait_time;
188 trigger_extract_event_.Set();
189 crit_.Leave();
190 // Make sure |crit_| is aquired by |extract_thread_| before returning.
191 crit_acquired_event_.Wait(rtc::Event::kForever);
192 }
193 }
194
195 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
196 rtc::CritScope lock(&crit_);
197 ASSERT_LT(index, frames_.size());
198 ASSERT_TRUE(frames_[index]);
philipel0fa82a62018-03-19 15:34:53 +0100199 ASSERT_EQ(picture_id, frames_[index]->id.picture_id);
200 ASSERT_EQ(spatial_layer, frames_[index]->id.spatial_layer);
philipelbe7a9e52016-05-19 12:19:35 +0200201 }
202
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100203 void CheckFrameSize(size_t index, size_t size) {
204 rtc::CritScope lock(&crit_);
205 ASSERT_LT(index, frames_.size());
206 ASSERT_TRUE(frames_[index]);
207 ASSERT_EQ(frames_[index]->size(), size);
208 }
209
philipelbe7a9e52016-05-19 12:19:35 +0200210 void CheckNoFrame(size_t index) {
211 rtc::CritScope lock(&crit_);
212 ASSERT_LT(index, frames_.size());
213 ASSERT_FALSE(frames_[index]);
214 }
215
tommi0f8b4032017-02-22 11:22:05 -0800216 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200217 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
218 while (true) {
219 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
220 {
221 rtc::CritScope lock(&tfb->crit_);
222 tfb->crit_acquired_event_.Set();
223 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800224 return;
philipelbe7a9e52016-05-19 12:19:35 +0200225
philipele7c891f2018-02-22 14:35:06 +0100226 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200227 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200228 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200229 if (res != FrameBuffer::ReturnReason::kStopped)
230 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200231 }
232 }
233 }
234
235 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
236
237 SimulatedClock clock_;
238 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200239 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200240 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100241 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200242 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800243 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200244
245 int64_t max_wait_time_;
246 bool tear_down_;
247 rtc::PlatformThread extract_thread_;
248 rtc::Event trigger_extract_event_;
249 rtc::Event crit_acquired_event_;
250 rtc::CriticalSection crit_;
251};
252
philipel6e8224f2016-05-19 17:07:44 +0200253// Following tests are timing dependent. Either the timeouts have to
254// be increased by a large margin, which would slow down all trybots,
255// or we disable them for the very slow ones, like we do here.
256#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200257TEST_F(TestFrameBuffer2, WaitForFrame) {
258 uint16_t pid = Rand();
259 uint32_t ts = Rand();
260
philipel6e8224f2016-05-19 17:07:44 +0200261 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100262 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200263 CheckFrame(0, pid, 0);
264}
265
266TEST_F(TestFrameBuffer2, OneSuperFrame) {
267 uint16_t pid = Rand();
268 uint32_t ts = Rand();
269
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100270 InsertFrame(pid, 0, ts, false, false);
271 InsertFrame(pid, 1, ts, true, true);
philipele0b2f152016-09-28 10:23:49 +0200272 ExtractFrame();
273
274 CheckFrame(0, pid, 0);
philipele0b2f152016-09-28 10:23:49 +0200275}
276
gnishb2a318b2017-05-10 09:21:33 -0700277TEST_F(TestFrameBuffer2, SetPlayoutDelay) {
278 const PlayoutDelay kPlayoutDelayMs = {123, 321};
279 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100280 test_frame->id.picture_id = 0;
gnishb2a318b2017-05-10 09:21:33 -0700281 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200282 buffer_->InsertFrame(std::move(test_frame));
gnishb2a318b2017-05-10 09:21:33 -0700283 EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_.min_playout_delay());
284 EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_.max_playout_delay());
285}
286
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200287TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
288 VCMTiming timing(&clock_);
289 buffer_.reset(
290 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
291 const PlayoutDelay kPlayoutDelayMs = {0, 0};
292 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
293 test_frame->id.picture_id = 0;
294 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
295 buffer_->InsertFrame(std::move(test_frame));
296 ExtractFrame(0, false);
297 CheckFrame(0, 0, 0);
298 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
299}
300
aleloi3e005282017-01-26 05:38:00 -0800301// Flaky test, see bugs.webrtc.org/7068.
302TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200303 uint16_t pid = Rand();
304 uint32_t ts = Rand();
305
philipel6e8224f2016-05-19 17:07:44 +0200306 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100307 InsertFrame(pid, 1, ts, true, true);
308 InsertFrame(pid, 0, ts, false, false);
philipelbe7a9e52016-05-19 12:19:35 +0200309 ExtractFrame();
310
311 CheckFrame(0, pid, 0);
312 CheckFrame(1, pid, 1);
313}
314
philipel94616b32016-05-20 10:43:01 +0200315TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200316 uint16_t pid = Rand();
317 uint32_t ts = Rand();
318
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100319 InsertFrame(pid, 0, ts, false, true);
philipel6e8224f2016-05-19 17:07:44 +0200320 ExtractFrame();
321 CheckFrame(0, pid, 0);
322 for (int i = 1; i < 10; i += 2) {
323 ExtractFrame(50);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100324 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, true, pid + i);
philipel6e8224f2016-05-19 17:07:44 +0200325 clock_.AdvanceTimeMilliseconds(kFps10);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100326 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipel6e8224f2016-05-19 17:07:44 +0200327 clock_.AdvanceTimeMilliseconds(kFps10);
328 ExtractFrame();
329 CheckFrame(i, pid + i, 0);
330 CheckFrame(i + 1, pid + i + 1, 0);
331 }
332}
333#endif // Timing dependent tests.
334
335TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
336 ExtractFrame();
337 CheckNoFrame(0);
338}
339
philipel93e451b2016-10-06 12:25:13 +0200340TEST_F(TestFrameBuffer2, MissingFrame) {
341 uint16_t pid = Rand();
342 uint32_t ts = Rand();
343
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100344 InsertFrame(pid, 0, ts, false, true);
345 InsertFrame(pid + 2, 0, ts, false, true, pid);
346 InsertFrame(pid + 3, 0, ts, false, true, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200347 ExtractFrame();
348 ExtractFrame();
349 ExtractFrame();
350
351 CheckFrame(0, pid, 0);
352 CheckFrame(1, pid + 2, 0);
353 CheckNoFrame(2);
354}
355
philipelbe7a9e52016-05-19 12:19:35 +0200356TEST_F(TestFrameBuffer2, OneLayerStream) {
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);
philipelbe7a9e52016-05-19 12:19:35 +0200361 ExtractFrame();
362 CheckFrame(0, pid, 0);
363 for (int i = 1; i < 10; ++i) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100364 InsertFrame(pid + i, 0, ts + i * kFps10, false, true, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200365 ExtractFrame();
366 clock_.AdvanceTimeMilliseconds(kFps10);
367 CheckFrame(i, pid + i, 0);
368 }
369}
370
philipelbe7a9e52016-05-19 12:19:35 +0200371TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
372 uint16_t pid = Rand();
373 uint32_t ts = Rand();
374
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100375 InsertFrame(pid, 0, ts, false, true);
376 InsertFrame(pid + 1, 0, ts + kFps20, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200377 for (int i = 2; i < 10; i += 2) {
378 uint32_t ts_tl0 = ts + i / 2 * kFps10;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100379 InsertFrame(pid + i, 0, ts_tl0, false, true, pid + i - 2);
380 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, true, pid + i,
381 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200382 }
383
384 for (int i = 0; i < 10; ++i) {
385 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100386 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200387 }
388
389 CheckFrame(0, pid, 0);
390 CheckFrame(1, pid + 1, 0);
391 CheckFrame(2, pid + 2, 0);
392 CheckFrame(3, pid + 4, 0);
393 CheckFrame(4, pid + 6, 0);
394 CheckFrame(5, pid + 8, 0);
395 CheckNoFrame(6);
396 CheckNoFrame(7);
397 CheckNoFrame(8);
398 CheckNoFrame(9);
399}
400
philipelbe7a9e52016-05-19 12:19:35 +0200401TEST_F(TestFrameBuffer2, InsertLateFrame) {
402 uint16_t pid = Rand();
403 uint32_t ts = Rand();
404
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100405 InsertFrame(pid, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200406 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100407 InsertFrame(pid + 2, 0, ts, false, true);
philipelbe7a9e52016-05-19 12:19:35 +0200408 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100409 InsertFrame(pid + 1, 0, ts, false, true, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200410 ExtractFrame();
411
412 CheckFrame(0, pid, 0);
413 CheckFrame(1, pid + 2, 0);
414 CheckNoFrame(2);
415}
416
philipel4f6cd6a2016-08-03 10:59:32 +0200417TEST_F(TestFrameBuffer2, ProtectionMode) {
418 uint16_t pid = Rand();
419 uint32_t ts = Rand();
420
421 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100422 InsertFrame(pid, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200423 ExtractFrame();
424
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200425 buffer_->SetProtectionMode(kProtectionNackFEC);
philipel4f6cd6a2016-08-03 10:59:32 +0200426 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100427 InsertFrame(pid + 1, 0, ts, false, true);
philipel4f6cd6a2016-08-03 10:59:32 +0200428 ExtractFrame();
429}
430
philipele0b2f152016-09-28 10:23:49 +0200431TEST_F(TestFrameBuffer2, NoContinuousFrame) {
432 uint16_t pid = Rand();
433 uint32_t ts = Rand();
434
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100435 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, true, pid));
philipele0b2f152016-09-28 10:23:49 +0200436}
437
438TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
439 uint16_t pid = Rand();
440 uint32_t ts = Rand();
441
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100442 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
443 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, true, pid + 1));
444 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, true, pid));
445 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, true, pid + 3));
446 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false, true));
philipele0b2f152016-09-28 10:23:49 +0200447}
448
449TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
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, false));
454 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true, true));
455 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, true, pid));
456 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, false, pid + 1));
457 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, true, pid + 1));
458 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, false, pid + 2));
459 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, false, pid));
460 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, true, pid + 2));
philipele0b2f152016-09-28 10:23:49 +0200461}
462
philipelfcc60062017-01-18 05:35:20 -0800463TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
464 uint16_t pid = Rand();
465 uint32_t ts = Rand();
466
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100467 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, true));
468 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, true, pid));
philipelfcc60062017-01-18 05:35:20 -0800469 ExtractFrame();
470 CheckFrame(0, pid, 0);
471
472 // Jump back in pid but increase ts.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100473 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false, true));
philipelfcc60062017-01-18 05:35:20 -0800474 ExtractFrame();
475 ExtractFrame();
476 CheckFrame(1, pid - 1, 0);
477 CheckNoFrame(2);
478}
479
philipela45102f2017-02-22 05:30:39 -0800480TEST_F(TestFrameBuffer2, StatsCallback) {
481 uint16_t pid = Rand();
482 uint32_t ts = Rand();
483 const int kFrameSize = 5000;
484
ilnik6d5b4d62017-08-30 03:32:14 -0700485 EXPECT_CALL(stats_callback_,
486 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800487 EXPECT_CALL(stats_callback_,
488 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
489
490 {
491 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100492 frame->VerifyAndAllocate(kFrameSize);
philipela45102f2017-02-22 05:30:39 -0800493 frame->SetSize(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100494 frame->id.picture_id = pid;
495 frame->id.spatial_layer = 0;
Niels Möller23775882018-08-16 10:24:12 +0200496 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800497 frame->num_references = 0;
498 frame->inter_layer_predicted = false;
499
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200500 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800501 }
502
503 ExtractFrame();
504 CheckFrame(0, pid, 0);
505}
506
philipel146a48b2017-04-20 04:04:38 -0700507TEST_F(TestFrameBuffer2, ForwardJumps) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100508 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700509 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100510 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, true, 5453));
philipel146a48b2017-04-20 04:04:38 -0700511 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100512 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700513 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100514 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700515 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100516 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, true, 29804));
philipel146a48b2017-04-20 04:04:38 -0700517 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100518 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, true, 29805));
philipel146a48b2017-04-20 04:04:38 -0700519 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100520 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700521 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100522 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false, true));
philipel146a48b2017-04-20 04:04:38 -0700523 ExtractFrame();
524}
525
philipelf6842692017-04-28 03:29:15 -0700526TEST_F(TestFrameBuffer2, DuplicateFrames) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100527 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700528 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100529 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false, true));
philipelf6842692017-04-28 03:29:15 -0700530}
531
philipel112adf92017-06-15 09:06:21 -0700532// TODO(philipel): implement more unittests related to invalid references.
533TEST_F(TestFrameBuffer2, InvalidReferences) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100534 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, true, 2));
535 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false, true));
philipel112adf92017-06-15 09:06:21 -0700536 ExtractFrame();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100537 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, true, 1));
philipel112adf92017-06-15 09:06:21 -0700538}
539
philipel3042c2d2017-08-18 04:55:02 -0700540TEST_F(TestFrameBuffer2, KeyframeRequired) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100541 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false, true));
542 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, true, 1));
543 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false, true));
philipel3042c2d2017-08-18 04:55:02 -0700544 ExtractFrame();
545 ExtractFrame(0, true);
546 ExtractFrame();
547
548 CheckFrame(0, 1, 0);
549 CheckFrame(1, 3, 0);
550 CheckNoFrame(2);
551}
552
philipel9771c502018-03-02 11:06:27 +0100553TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
554 const int kMaxBufferSize = 600;
555
556 for (int i = 1; i <= kMaxBufferSize; ++i)
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100557 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, true, i - 1));
philipel9771c502018-03-02 11:06:27 +0100558 ExtractFrame();
559 CheckNoFrame(0);
560
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100561 EXPECT_EQ(kMaxBufferSize + 1,
562 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
563 false, true));
philipel9771c502018-03-02 11:06:27 +0100564 ExtractFrame();
565 CheckFrame(1, kMaxBufferSize + 1, 0);
566}
567
philipel798b2822018-06-11 13:10:14 +0200568TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100569 InsertFrame(1, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200570 ExtractFrame(0, true);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100571 InsertFrame(3, 0, 0, false, true, 2, 0);
572 InsertFrame(3, 0, 0, false, true, 0);
573 InsertFrame(2, 0, 0, false, true);
philipel798b2822018-06-11 13:10:14 +0200574 ExtractFrame(0, true);
575 ExtractFrame(0, true);
576}
577
philipel6d216502018-10-22 14:36:45 +0200578TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100579 InsertFrame(2, 0, 1, false, true);
580 InsertFrame(1, 0, 2, false, true); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200581 ExtractFrame(0);
582 ExtractFrame(0);
583 CheckFrame(0, 1, 0);
584 CheckNoFrame(1);
585
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100586 InsertFrame(3, 0, 4, false, true);
587 InsertFrame(4, 0, 3, false, true); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200588 ExtractFrame(0);
589 ExtractFrame(0);
590 CheckFrame(2, 3, 0);
591 CheckNoFrame(3);
592}
593
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100594TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
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 ExtractFrame(0);
601 ExtractFrame(0);
602 CheckFrame(0, pid, 0);
603 CheckNoFrame(1);
604 // Two frames should be combined and returned together.
605 CheckFrameSize(0, kFrameSize * 2);
606}
607
608TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
609 uint16_t pid = Rand();
610 uint32_t ts = Rand();
611
612 InsertFrame(pid, 0, ts, false, false);
613 InsertFrame(pid, 1, ts, true, true);
614
615 ExtractFrame(0);
616 CheckFrame(0, pid, 0);
617
618 InsertFrame(pid + 1, 1, ts + kFps20, false, true, pid);
619 InsertFrame(pid + 2, 0, ts + kFps10, false, false, pid);
620 InsertFrame(pid + 2, 1, ts + kFps10, true, true, pid + 1);
621
622 clock_.AdvanceTimeMilliseconds(1000);
623 // Frame pid+1 is decodable but too late.
624 // In superframe pid+2 frame sid=0 is decodable, but frame sid=1 is not.
625 // Incorrect implementation might skip pid+1 frame and output undecodable
626 // pid+2 instead.
627 ExtractFrame();
628 ExtractFrame();
629 CheckFrame(1, pid + 1, 1);
630 CheckFrame(2, pid + 2, 0);
631}
632
philipelbe7a9e52016-05-19 12:19:35 +0200633} // namespace video_coding
634} // namespace webrtc