blob: ca7af09d5f98b554aae4e0d18b47ffe39a6286fa [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;
127
128 TestFrameBuffer2()
129 : clock_(0),
130 timing_(&clock_),
131 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200132 buffer_(new FrameBuffer(&clock_,
133 &jitter_estimator_,
134 &timing_,
135 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200136 rand_(0x34678213),
137 tear_down_(false),
Niels Möllerc572ff32018-11-07 08:43:50 +0100138 extract_thread_(&ExtractLoop, this, "Extract Thread") {}
philipelbe7a9e52016-05-19 12:19:35 +0200139
140 void SetUp() override { extract_thread_.Start(); }
141
142 void TearDown() override {
143 tear_down_ = true;
144 trigger_extract_event_.Set();
145 extract_thread_.Stop();
146 }
147
148 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200149 int InsertFrame(uint16_t picture_id,
150 uint8_t spatial_layer,
151 int64_t ts_ms,
152 bool inter_layer_predicted,
153 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200154 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100155 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700156 std::array<uint16_t, sizeof...(refs)> references = {
157 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200158
philipelb4d31082016-07-11 08:46:29 -0700159 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100160 frame->id.picture_id = picture_id;
161 frame->id.spatial_layer = spatial_layer;
Niels Möller23775882018-08-16 10:24:12 +0200162 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200163 frame->num_references = references.size();
164 frame->inter_layer_predicted = inter_layer_predicted;
165 for (size_t r = 0; r < references.size(); ++r)
166 frame->references[r] = references[r];
167
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200168 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200169 }
170
philipel3042c2d2017-08-18 04:55:02 -0700171 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipelbe7a9e52016-05-19 12:19:35 +0200172 crit_.Enter();
173 if (max_wait_time == 0) {
philipele7c891f2018-02-22 14:35:06 +0100174 std::unique_ptr<EncodedFrame> frame;
philipel3042c2d2017-08-18 04:55:02 -0700175 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200176 buffer_->NextFrame(0, &frame, keyframe_required);
philipel75562822016-09-05 10:57:41 +0200177 if (res != FrameBuffer::ReturnReason::kStopped)
178 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200179 crit_.Leave();
180 } else {
181 max_wait_time_ = max_wait_time;
182 trigger_extract_event_.Set();
183 crit_.Leave();
184 // Make sure |crit_| is aquired by |extract_thread_| before returning.
185 crit_acquired_event_.Wait(rtc::Event::kForever);
186 }
187 }
188
189 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
190 rtc::CritScope lock(&crit_);
191 ASSERT_LT(index, frames_.size());
192 ASSERT_TRUE(frames_[index]);
philipel0fa82a62018-03-19 15:34:53 +0100193 ASSERT_EQ(picture_id, frames_[index]->id.picture_id);
194 ASSERT_EQ(spatial_layer, frames_[index]->id.spatial_layer);
philipelbe7a9e52016-05-19 12:19:35 +0200195 }
196
197 void CheckNoFrame(size_t index) {
198 rtc::CritScope lock(&crit_);
199 ASSERT_LT(index, frames_.size());
200 ASSERT_FALSE(frames_[index]);
201 }
202
tommi0f8b4032017-02-22 11:22:05 -0800203 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200204 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
205 while (true) {
206 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
207 {
208 rtc::CritScope lock(&tfb->crit_);
209 tfb->crit_acquired_event_.Set();
210 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800211 return;
philipelbe7a9e52016-05-19 12:19:35 +0200212
philipele7c891f2018-02-22 14:35:06 +0100213 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200214 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200215 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200216 if (res != FrameBuffer::ReturnReason::kStopped)
217 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200218 }
219 }
220 }
221
222 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
223
224 SimulatedClock clock_;
225 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200226 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200227 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100228 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200229 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800230 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200231
232 int64_t max_wait_time_;
233 bool tear_down_;
234 rtc::PlatformThread extract_thread_;
235 rtc::Event trigger_extract_event_;
236 rtc::Event crit_acquired_event_;
237 rtc::CriticalSection crit_;
238};
239
philipel6e8224f2016-05-19 17:07:44 +0200240// Following tests are timing dependent. Either the timeouts have to
241// be increased by a large margin, which would slow down all trybots,
242// or we disable them for the very slow ones, like we do here.
243#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200244TEST_F(TestFrameBuffer2, WaitForFrame) {
245 uint16_t pid = Rand();
246 uint32_t ts = Rand();
247
philipel6e8224f2016-05-19 17:07:44 +0200248 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200249 InsertFrame(pid, 0, ts, false);
250 CheckFrame(0, pid, 0);
251}
252
253TEST_F(TestFrameBuffer2, OneSuperFrame) {
254 uint16_t pid = Rand();
255 uint32_t ts = Rand();
256
philipele0b2f152016-09-28 10:23:49 +0200257 InsertFrame(pid, 0, ts, false);
258 ExtractFrame();
259 InsertFrame(pid, 1, ts, true);
260 ExtractFrame();
261
262 CheckFrame(0, pid, 0);
263 CheckFrame(1, pid, 1);
264}
265
gnishb2a318b2017-05-10 09:21:33 -0700266TEST_F(TestFrameBuffer2, SetPlayoutDelay) {
267 const PlayoutDelay kPlayoutDelayMs = {123, 321};
268 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100269 test_frame->id.picture_id = 0;
gnishb2a318b2017-05-10 09:21:33 -0700270 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200271 buffer_->InsertFrame(std::move(test_frame));
gnishb2a318b2017-05-10 09:21:33 -0700272 EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_.min_playout_delay());
273 EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_.max_playout_delay());
274}
275
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200276TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
277 VCMTiming timing(&clock_);
278 buffer_.reset(
279 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
280 const PlayoutDelay kPlayoutDelayMs = {0, 0};
281 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
282 test_frame->id.picture_id = 0;
283 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
284 buffer_->InsertFrame(std::move(test_frame));
285 ExtractFrame(0, false);
286 CheckFrame(0, 0, 0);
287 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
288}
289
aleloi3e005282017-01-26 05:38:00 -0800290// Flaky test, see bugs.webrtc.org/7068.
291TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200292 uint16_t pid = Rand();
293 uint32_t ts = Rand();
294
philipel6e8224f2016-05-19 17:07:44 +0200295 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200296 InsertFrame(pid, 1, ts, true);
297 InsertFrame(pid, 0, ts, false);
298 ExtractFrame();
299
300 CheckFrame(0, pid, 0);
301 CheckFrame(1, pid, 1);
302}
303
philipel94616b32016-05-20 10:43:01 +0200304TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200305 uint16_t pid = Rand();
306 uint32_t ts = Rand();
307
308 InsertFrame(pid, 0, ts, false);
309 ExtractFrame();
310 CheckFrame(0, pid, 0);
311 for (int i = 1; i < 10; i += 2) {
312 ExtractFrame(50);
313 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
314 clock_.AdvanceTimeMilliseconds(kFps10);
315 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
316 clock_.AdvanceTimeMilliseconds(kFps10);
317 ExtractFrame();
318 CheckFrame(i, pid + i, 0);
319 CheckFrame(i + 1, pid + i + 1, 0);
320 }
321}
322#endif // Timing dependent tests.
323
324TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
325 ExtractFrame();
326 CheckNoFrame(0);
327}
328
philipel93e451b2016-10-06 12:25:13 +0200329TEST_F(TestFrameBuffer2, MissingFrame) {
330 uint16_t pid = Rand();
331 uint32_t ts = Rand();
332
333 InsertFrame(pid, 0, ts, false);
334 InsertFrame(pid + 2, 0, ts, false, pid);
335 InsertFrame(pid + 3, 0, ts, false, pid + 1, pid + 2);
336 ExtractFrame();
337 ExtractFrame();
338 ExtractFrame();
339
340 CheckFrame(0, pid, 0);
341 CheckFrame(1, pid + 2, 0);
342 CheckNoFrame(2);
343}
344
philipelbe7a9e52016-05-19 12:19:35 +0200345TEST_F(TestFrameBuffer2, OneLayerStream) {
346 uint16_t pid = Rand();
347 uint32_t ts = Rand();
348
349 InsertFrame(pid, 0, ts, false);
350 ExtractFrame();
351 CheckFrame(0, pid, 0);
352 for (int i = 1; i < 10; ++i) {
353 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
354 ExtractFrame();
355 clock_.AdvanceTimeMilliseconds(kFps10);
356 CheckFrame(i, pid + i, 0);
357 }
358}
359
philipelbe7a9e52016-05-19 12:19:35 +0200360TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
361 uint16_t pid = Rand();
362 uint32_t ts = Rand();
363
364 InsertFrame(pid, 0, ts, false);
philipelfd5a20f2016-11-15 00:57:57 -0800365 InsertFrame(pid + 1, 0, ts + kFps20, false, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200366 for (int i = 2; i < 10; i += 2) {
367 uint32_t ts_tl0 = ts + i / 2 * kFps10;
368 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
369 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
370 }
371
372 for (int i = 0; i < 10; ++i) {
373 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100374 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200375 }
376
377 CheckFrame(0, pid, 0);
378 CheckFrame(1, pid + 1, 0);
379 CheckFrame(2, pid + 2, 0);
380 CheckFrame(3, pid + 4, 0);
381 CheckFrame(4, pid + 6, 0);
382 CheckFrame(5, pid + 8, 0);
383 CheckNoFrame(6);
384 CheckNoFrame(7);
385 CheckNoFrame(8);
386 CheckNoFrame(9);
387}
388
389TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
390 uint16_t pid = Rand();
391 uint32_t ts = Rand();
392
393 InsertFrame(pid, 0, ts, false);
394 InsertFrame(pid, 1, ts, false);
395 for (int i = 1; i < 6; ++i) {
396 uint32_t ts_tl0 = ts + i * kFps10;
397 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
398 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
399 }
400
401 ExtractFrame();
402 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100403 clock_.AdvanceTimeMilliseconds(57);
philipelbe7a9e52016-05-19 12:19:35 +0200404 for (int i = 2; i < 12; ++i) {
405 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100406 clock_.AdvanceTimeMilliseconds(57);
philipelbe7a9e52016-05-19 12:19:35 +0200407 }
408
409 CheckFrame(0, pid, 0);
410 CheckFrame(1, pid, 1);
411 CheckFrame(2, pid + 1, 0);
412 CheckFrame(3, pid + 1, 1);
413 CheckFrame(4, pid + 2, 0);
414 CheckFrame(5, pid + 2, 1);
415 CheckFrame(6, pid + 3, 0);
416 CheckFrame(7, pid + 4, 0);
417 CheckFrame(8, pid + 5, 0);
418 CheckNoFrame(9);
419 CheckNoFrame(10);
420 CheckNoFrame(11);
421}
422
423TEST_F(TestFrameBuffer2, InsertLateFrame) {
424 uint16_t pid = Rand();
425 uint32_t ts = Rand();
426
427 InsertFrame(pid, 0, ts, false);
428 ExtractFrame();
429 InsertFrame(pid + 2, 0, ts, false);
430 ExtractFrame();
431 InsertFrame(pid + 1, 0, ts, false, pid);
432 ExtractFrame();
433
434 CheckFrame(0, pid, 0);
435 CheckFrame(1, pid + 2, 0);
436 CheckNoFrame(2);
437}
438
philipel4f6cd6a2016-08-03 10:59:32 +0200439TEST_F(TestFrameBuffer2, ProtectionMode) {
440 uint16_t pid = Rand();
441 uint32_t ts = Rand();
442
443 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
444 InsertFrame(pid, 0, ts, false);
445 ExtractFrame();
446
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200447 buffer_->SetProtectionMode(kProtectionNackFEC);
philipel4f6cd6a2016-08-03 10:59:32 +0200448 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
449 InsertFrame(pid + 1, 0, ts, false);
450 ExtractFrame();
451}
452
philipele0b2f152016-09-28 10:23:49 +0200453TEST_F(TestFrameBuffer2, NoContinuousFrame) {
454 uint16_t pid = Rand();
455 uint32_t ts = Rand();
456
457 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, pid));
458}
459
460TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
461 uint16_t pid = Rand();
462 uint32_t ts = Rand();
463
464 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
465 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
466 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, pid));
467 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, pid + 3));
468 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false));
469}
470
471TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
472 uint16_t pid = Rand();
473 uint32_t ts = Rand();
474
475 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
476 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true));
477 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, pid));
478 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
479 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, pid + 1));
480 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, pid + 2));
481 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, pid));
482 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, pid + 2));
483}
484
philipelfcc60062017-01-18 05:35:20 -0800485TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
486 uint16_t pid = Rand();
487 uint32_t ts = Rand();
488
489 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
490 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, pid));
491 ExtractFrame();
492 CheckFrame(0, pid, 0);
493
494 // Jump back in pid but increase ts.
495 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false));
496 ExtractFrame();
497 ExtractFrame();
498 CheckFrame(1, pid - 1, 0);
499 CheckNoFrame(2);
500}
501
philipela45102f2017-02-22 05:30:39 -0800502TEST_F(TestFrameBuffer2, StatsCallback) {
503 uint16_t pid = Rand();
504 uint32_t ts = Rand();
505 const int kFrameSize = 5000;
506
ilnik6d5b4d62017-08-30 03:32:14 -0700507 EXPECT_CALL(stats_callback_,
508 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800509 EXPECT_CALL(stats_callback_,
510 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
511
512 {
513 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
514 frame->SetSize(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100515 frame->id.picture_id = pid;
516 frame->id.spatial_layer = 0;
Niels Möller23775882018-08-16 10:24:12 +0200517 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800518 frame->num_references = 0;
519 frame->inter_layer_predicted = false;
520
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200521 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800522 }
523
524 ExtractFrame();
525 CheckFrame(0, pid, 0);
526}
527
philipel146a48b2017-04-20 04:04:38 -0700528TEST_F(TestFrameBuffer2, ForwardJumps) {
529 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false));
530 ExtractFrame();
531 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, 5453));
532 ExtractFrame();
533 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false));
534 ExtractFrame();
535 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false));
536 ExtractFrame();
537 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, 29804));
538 ExtractFrame();
539 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, 29805));
540 ExtractFrame();
541 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false));
542 ExtractFrame();
543 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false));
544 ExtractFrame();
545}
546
philipelf6842692017-04-28 03:29:15 -0700547TEST_F(TestFrameBuffer2, DuplicateFrames) {
548 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false));
549 ExtractFrame();
550 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false));
551}
552
philipel112adf92017-06-15 09:06:21 -0700553// TODO(philipel): implement more unittests related to invalid references.
554TEST_F(TestFrameBuffer2, InvalidReferences) {
555 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, 2));
556 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false));
557 ExtractFrame();
558 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, 1));
559}
560
philipel3042c2d2017-08-18 04:55:02 -0700561TEST_F(TestFrameBuffer2, KeyframeRequired) {
562 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false));
563 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, 1));
564 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false));
565 ExtractFrame();
566 ExtractFrame(0, true);
567 ExtractFrame();
568
569 CheckFrame(0, 1, 0);
570 CheckFrame(1, 3, 0);
571 CheckNoFrame(2);
572}
573
philipel9771c502018-03-02 11:06:27 +0100574TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
575 const int kMaxBufferSize = 600;
576
577 for (int i = 1; i <= kMaxBufferSize; ++i)
578 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, i - 1));
579 ExtractFrame();
580 CheckNoFrame(0);
581
582 EXPECT_EQ(
583 kMaxBufferSize + 1,
584 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000, false));
585 ExtractFrame();
586 CheckFrame(1, kMaxBufferSize + 1, 0);
587}
588
philipel798b2822018-06-11 13:10:14 +0200589TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
590 InsertFrame(1, 0, 0, false);
591 ExtractFrame(0, true);
592 InsertFrame(3, 0, 0, false, 2, 0);
593 InsertFrame(3, 0, 0, false, 0);
594 InsertFrame(2, 0, 0, false);
595 ExtractFrame(0, true);
596 ExtractFrame(0, true);
597}
598
philipel6d216502018-10-22 14:36:45 +0200599TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
600 InsertFrame(2, 0, 1, false);
601 InsertFrame(1, 0, 2, false); // Older picture id but newer timestamp.
602 ExtractFrame(0);
603 ExtractFrame(0);
604 CheckFrame(0, 1, 0);
605 CheckNoFrame(1);
606
607 InsertFrame(3, 0, 4, false);
608 InsertFrame(4, 0, 3, false); // Newer picture id but older timestamp.
609 ExtractFrame(0);
610 ExtractFrame(0);
611 CheckFrame(2, 3, 0);
612 CheckNoFrame(3);
613}
614
philipelbe7a9e52016-05-19 12:19:35 +0200615} // namespace video_coding
616} // namespace webrtc