blob: 304910a9a5a8fba84ac9546e87c2f0972d4c6c78 [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 bool GetBitstream(uint8_t* destination) const override { return true; }
92
93 uint32_t Timestamp() const override { return timestamp; }
94
95 int64_t ReceivedTime() const override { return 0; }
96
97 int64_t RenderTime() const override { return _renderTimeMs; }
philipela45102f2017-02-22 05:30:39 -080098
99 // In EncodedImage |_length| is used to descibe its size and |_size| to
100 // describe its capacity.
101 void SetSize(int size) { _length = size; }
102};
103
104class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
105 public:
106 MOCK_METHOD2(OnReceiveRatesUpdated,
107 void(uint32_t bitRate, uint32_t frameRate));
ilnik6d5b4d62017-08-30 03:32:14 -0700108 MOCK_METHOD3(OnCompleteFrame,
109 void(bool is_keyframe,
110 size_t size_bytes,
111 VideoContentType content_type));
philipela45102f2017-02-22 05:30:39 -0800112 MOCK_METHOD1(OnDiscardedPacketsUpdated, void(int discarded_packets));
113 MOCK_METHOD1(OnFrameCountsUpdated, void(const FrameCounts& frame_counts));
114 MOCK_METHOD7(OnFrameBufferTimingsUpdated,
115 void(int decode_ms,
116 int max_decode_ms,
117 int current_delay_ms,
118 int target_delay_ms,
119 int jitter_buffer_ms,
120 int min_playout_delay_ms,
121 int render_delay_ms));
ilnik2edc6842017-07-06 03:06:50 -0700122 MOCK_METHOD1(OnTimingFrameInfoUpdated, void(const TimingFrameInfo& info));
philipelbe7a9e52016-05-19 12:19:35 +0200123};
124
125class TestFrameBuffer2 : public ::testing::Test {
126 protected:
127 static constexpr int kMaxReferences = 5;
128 static constexpr int kFps1 = 1000;
129 static constexpr int kFps10 = kFps1 / 10;
130 static constexpr int kFps20 = kFps1 / 20;
131
132 TestFrameBuffer2()
133 : clock_(0),
134 timing_(&clock_),
135 jitter_estimator_(&clock_),
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200136 buffer_(new FrameBuffer(&clock_,
137 &jitter_estimator_,
138 &timing_,
139 &stats_callback_)),
philipelbe7a9e52016-05-19 12:19:35 +0200140 rand_(0x34678213),
141 tear_down_(false),
142 extract_thread_(&ExtractLoop, this, "Extract Thread"),
143 trigger_extract_event_(false, false),
144 crit_acquired_event_(false, false) {}
145
146 void SetUp() override { extract_thread_.Start(); }
147
148 void TearDown() override {
149 tear_down_ = true;
150 trigger_extract_event_.Set();
151 extract_thread_.Stop();
152 }
153
154 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200155 int InsertFrame(uint16_t picture_id,
156 uint8_t spatial_layer,
157 int64_t ts_ms,
158 bool inter_layer_predicted,
159 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200160 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100161 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700162 std::array<uint16_t, sizeof...(refs)> references = {
163 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200164
philipelb4d31082016-07-11 08:46:29 -0700165 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100166 frame->id.picture_id = picture_id;
167 frame->id.spatial_layer = spatial_layer;
philipelbe7a9e52016-05-19 12:19:35 +0200168 frame->timestamp = ts_ms * 90;
169 frame->num_references = references.size();
170 frame->inter_layer_predicted = inter_layer_predicted;
171 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
203 void CheckNoFrame(size_t index) {
204 rtc::CritScope lock(&crit_);
205 ASSERT_LT(index, frames_.size());
206 ASSERT_FALSE(frames_[index]);
207 }
208
tommi0f8b4032017-02-22 11:22:05 -0800209 static void ExtractLoop(void* obj) {
philipelbe7a9e52016-05-19 12:19:35 +0200210 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
211 while (true) {
212 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
213 {
214 rtc::CritScope lock(&tfb->crit_);
215 tfb->crit_acquired_event_.Set();
216 if (tfb->tear_down_)
tommi0f8b4032017-02-22 11:22:05 -0800217 return;
philipelbe7a9e52016-05-19 12:19:35 +0200218
philipele7c891f2018-02-22 14:35:06 +0100219 std::unique_ptr<EncodedFrame> frame;
philipel75562822016-09-05 10:57:41 +0200220 FrameBuffer::ReturnReason res =
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200221 tfb->buffer_->NextFrame(tfb->max_wait_time_, &frame);
philipel75562822016-09-05 10:57:41 +0200222 if (res != FrameBuffer::ReturnReason::kStopped)
223 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200224 }
225 }
226 }
227
228 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
229
230 SimulatedClock clock_;
231 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200232 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200233 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100234 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200235 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800236 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200237
238 int64_t max_wait_time_;
239 bool tear_down_;
240 rtc::PlatformThread extract_thread_;
241 rtc::Event trigger_extract_event_;
242 rtc::Event crit_acquired_event_;
243 rtc::CriticalSection crit_;
244};
245
philipel6e8224f2016-05-19 17:07:44 +0200246// Following tests are timing dependent. Either the timeouts have to
247// be increased by a large margin, which would slow down all trybots,
248// or we disable them for the very slow ones, like we do here.
249#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200250TEST_F(TestFrameBuffer2, WaitForFrame) {
251 uint16_t pid = Rand();
252 uint32_t ts = Rand();
253
philipel6e8224f2016-05-19 17:07:44 +0200254 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200255 InsertFrame(pid, 0, ts, false);
256 CheckFrame(0, pid, 0);
257}
258
259TEST_F(TestFrameBuffer2, OneSuperFrame) {
260 uint16_t pid = Rand();
261 uint32_t ts = Rand();
262
philipele0b2f152016-09-28 10:23:49 +0200263 InsertFrame(pid, 0, ts, false);
264 ExtractFrame();
265 InsertFrame(pid, 1, ts, true);
266 ExtractFrame();
267
268 CheckFrame(0, pid, 0);
269 CheckFrame(1, pid, 1);
270}
271
gnishb2a318b2017-05-10 09:21:33 -0700272TEST_F(TestFrameBuffer2, SetPlayoutDelay) {
273 const PlayoutDelay kPlayoutDelayMs = {123, 321};
274 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel0fa82a62018-03-19 15:34:53 +0100275 test_frame->id.picture_id = 0;
gnishb2a318b2017-05-10 09:21:33 -0700276 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200277 buffer_->InsertFrame(std::move(test_frame));
gnishb2a318b2017-05-10 09:21:33 -0700278 EXPECT_EQ(kPlayoutDelayMs.min_ms, timing_.min_playout_delay());
279 EXPECT_EQ(kPlayoutDelayMs.max_ms, timing_.max_playout_delay());
280}
281
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200282TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
283 VCMTiming timing(&clock_);
284 buffer_.reset(
285 new FrameBuffer(&clock_, &jitter_estimator_, &timing, &stats_callback_));
286 const PlayoutDelay kPlayoutDelayMs = {0, 0};
287 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
288 test_frame->id.picture_id = 0;
289 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
290 buffer_->InsertFrame(std::move(test_frame));
291 ExtractFrame(0, false);
292 CheckFrame(0, 0, 0);
293 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
294}
295
aleloi3e005282017-01-26 05:38:00 -0800296// Flaky test, see bugs.webrtc.org/7068.
297TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200298 uint16_t pid = Rand();
299 uint32_t ts = Rand();
300
philipel6e8224f2016-05-19 17:07:44 +0200301 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200302 InsertFrame(pid, 1, ts, true);
303 InsertFrame(pid, 0, ts, false);
304 ExtractFrame();
305
306 CheckFrame(0, pid, 0);
307 CheckFrame(1, pid, 1);
308}
309
philipel94616b32016-05-20 10:43:01 +0200310TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200311 uint16_t pid = Rand();
312 uint32_t ts = Rand();
313
314 InsertFrame(pid, 0, ts, false);
315 ExtractFrame();
316 CheckFrame(0, pid, 0);
317 for (int i = 1; i < 10; i += 2) {
318 ExtractFrame(50);
319 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
320 clock_.AdvanceTimeMilliseconds(kFps10);
321 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
322 clock_.AdvanceTimeMilliseconds(kFps10);
323 ExtractFrame();
324 CheckFrame(i, pid + i, 0);
325 CheckFrame(i + 1, pid + i + 1, 0);
326 }
327}
328#endif // Timing dependent tests.
329
330TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
331 ExtractFrame();
332 CheckNoFrame(0);
333}
334
philipel93e451b2016-10-06 12:25:13 +0200335TEST_F(TestFrameBuffer2, MissingFrame) {
336 uint16_t pid = Rand();
337 uint32_t ts = Rand();
338
339 InsertFrame(pid, 0, ts, false);
340 InsertFrame(pid + 2, 0, ts, false, pid);
341 InsertFrame(pid + 3, 0, ts, false, pid + 1, pid + 2);
342 ExtractFrame();
343 ExtractFrame();
344 ExtractFrame();
345
346 CheckFrame(0, pid, 0);
347 CheckFrame(1, pid + 2, 0);
348 CheckNoFrame(2);
349}
350
philipelbe7a9e52016-05-19 12:19:35 +0200351TEST_F(TestFrameBuffer2, OneLayerStream) {
352 uint16_t pid = Rand();
353 uint32_t ts = Rand();
354
355 InsertFrame(pid, 0, ts, false);
356 ExtractFrame();
357 CheckFrame(0, pid, 0);
358 for (int i = 1; i < 10; ++i) {
359 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
360 ExtractFrame();
361 clock_.AdvanceTimeMilliseconds(kFps10);
362 CheckFrame(i, pid + i, 0);
363 }
364}
365
philipelbe7a9e52016-05-19 12:19:35 +0200366TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
367 uint16_t pid = Rand();
368 uint32_t ts = Rand();
369
370 InsertFrame(pid, 0, ts, false);
philipelfd5a20f2016-11-15 00:57:57 -0800371 InsertFrame(pid + 1, 0, ts + kFps20, false, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200372 for (int i = 2; i < 10; i += 2) {
373 uint32_t ts_tl0 = ts + i / 2 * kFps10;
374 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
375 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
376 }
377
378 for (int i = 0; i < 10; ++i) {
379 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100380 clock_.AdvanceTimeMilliseconds(70);
philipelbe7a9e52016-05-19 12:19:35 +0200381 }
382
383 CheckFrame(0, pid, 0);
384 CheckFrame(1, pid + 1, 0);
385 CheckFrame(2, pid + 2, 0);
386 CheckFrame(3, pid + 4, 0);
387 CheckFrame(4, pid + 6, 0);
388 CheckFrame(5, pid + 8, 0);
389 CheckNoFrame(6);
390 CheckNoFrame(7);
391 CheckNoFrame(8);
392 CheckNoFrame(9);
393}
394
395TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
396 uint16_t pid = Rand();
397 uint32_t ts = Rand();
398
399 InsertFrame(pid, 0, ts, false);
400 InsertFrame(pid, 1, ts, false);
401 for (int i = 1; i < 6; ++i) {
402 uint32_t ts_tl0 = ts + i * kFps10;
403 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
404 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
405 }
406
407 ExtractFrame();
408 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100409 clock_.AdvanceTimeMilliseconds(57);
philipelbe7a9e52016-05-19 12:19:35 +0200410 for (int i = 2; i < 12; ++i) {
411 ExtractFrame();
Ilya Nikolaevskiy8c4fe162018-02-27 15:49:47 +0100412 clock_.AdvanceTimeMilliseconds(57);
philipelbe7a9e52016-05-19 12:19:35 +0200413 }
414
415 CheckFrame(0, pid, 0);
416 CheckFrame(1, pid, 1);
417 CheckFrame(2, pid + 1, 0);
418 CheckFrame(3, pid + 1, 1);
419 CheckFrame(4, pid + 2, 0);
420 CheckFrame(5, pid + 2, 1);
421 CheckFrame(6, pid + 3, 0);
422 CheckFrame(7, pid + 4, 0);
423 CheckFrame(8, pid + 5, 0);
424 CheckNoFrame(9);
425 CheckNoFrame(10);
426 CheckNoFrame(11);
427}
428
429TEST_F(TestFrameBuffer2, InsertLateFrame) {
430 uint16_t pid = Rand();
431 uint32_t ts = Rand();
432
433 InsertFrame(pid, 0, ts, false);
434 ExtractFrame();
435 InsertFrame(pid + 2, 0, ts, false);
436 ExtractFrame();
437 InsertFrame(pid + 1, 0, ts, false, pid);
438 ExtractFrame();
439
440 CheckFrame(0, pid, 0);
441 CheckFrame(1, pid + 2, 0);
442 CheckNoFrame(2);
443}
444
philipel4f6cd6a2016-08-03 10:59:32 +0200445TEST_F(TestFrameBuffer2, ProtectionMode) {
446 uint16_t pid = Rand();
447 uint32_t ts = Rand();
448
449 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
450 InsertFrame(pid, 0, ts, false);
451 ExtractFrame();
452
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200453 buffer_->SetProtectionMode(kProtectionNackFEC);
philipel4f6cd6a2016-08-03 10:59:32 +0200454 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
455 InsertFrame(pid + 1, 0, ts, false);
456 ExtractFrame();
457}
458
philipele0b2f152016-09-28 10:23:49 +0200459TEST_F(TestFrameBuffer2, NoContinuousFrame) {
460 uint16_t pid = Rand();
461 uint32_t ts = Rand();
462
463 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, pid));
464}
465
466TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
467 uint16_t pid = Rand();
468 uint32_t ts = Rand();
469
470 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
471 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
472 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, pid));
473 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, pid + 3));
474 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false));
475}
476
477TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
478 uint16_t pid = Rand();
479 uint32_t ts = Rand();
480
481 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
482 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true));
483 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, pid));
484 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
485 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, pid + 1));
486 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, pid + 2));
487 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, pid));
488 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, pid + 2));
489}
490
philipelfcc60062017-01-18 05:35:20 -0800491TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
492 uint16_t pid = Rand();
493 uint32_t ts = Rand();
494
495 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
496 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, false, pid));
497 ExtractFrame();
498 CheckFrame(0, pid, 0);
499
500 // Jump back in pid but increase ts.
501 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, false));
502 ExtractFrame();
503 ExtractFrame();
504 CheckFrame(1, pid - 1, 0);
505 CheckNoFrame(2);
506}
507
philipela45102f2017-02-22 05:30:39 -0800508TEST_F(TestFrameBuffer2, StatsCallback) {
509 uint16_t pid = Rand();
510 uint32_t ts = Rand();
511 const int kFrameSize = 5000;
512
ilnik6d5b4d62017-08-30 03:32:14 -0700513 EXPECT_CALL(stats_callback_,
514 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
philipela45102f2017-02-22 05:30:39 -0800515 EXPECT_CALL(stats_callback_,
516 OnFrameBufferTimingsUpdated(_, _, _, _, _, _, _));
517
518 {
519 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
520 frame->SetSize(kFrameSize);
philipel0fa82a62018-03-19 15:34:53 +0100521 frame->id.picture_id = pid;
522 frame->id.spatial_layer = 0;
philipela45102f2017-02-22 05:30:39 -0800523 frame->timestamp = ts;
524 frame->num_references = 0;
525 frame->inter_layer_predicted = false;
526
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200527 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800528 }
529
530 ExtractFrame();
531 CheckFrame(0, pid, 0);
532}
533
philipel146a48b2017-04-20 04:04:38 -0700534TEST_F(TestFrameBuffer2, ForwardJumps) {
535 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, false));
536 ExtractFrame();
537 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, false, 5453));
538 ExtractFrame();
539 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, false));
540 ExtractFrame();
541 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, false));
542 ExtractFrame();
543 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, false, 29804));
544 ExtractFrame();
545 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, false, 29805));
546 ExtractFrame();
547 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, false));
548 ExtractFrame();
549 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, false));
550 ExtractFrame();
551}
552
philipelf6842692017-04-28 03:29:15 -0700553TEST_F(TestFrameBuffer2, DuplicateFrames) {
554 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false));
555 ExtractFrame();
556 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, false));
557}
558
philipel112adf92017-06-15 09:06:21 -0700559// TODO(philipel): implement more unittests related to invalid references.
560TEST_F(TestFrameBuffer2, InvalidReferences) {
561 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, false, 2));
562 EXPECT_EQ(1, InsertFrame(1, 0, 2000, false));
563 ExtractFrame();
564 EXPECT_EQ(2, InsertFrame(2, 0, 3000, false, 1));
565}
566
philipel3042c2d2017-08-18 04:55:02 -0700567TEST_F(TestFrameBuffer2, KeyframeRequired) {
568 EXPECT_EQ(1, InsertFrame(1, 0, 1000, false));
569 EXPECT_EQ(2, InsertFrame(2, 0, 2000, false, 1));
570 EXPECT_EQ(3, InsertFrame(3, 0, 3000, false));
571 ExtractFrame();
572 ExtractFrame(0, true);
573 ExtractFrame();
574
575 CheckFrame(0, 1, 0);
576 CheckFrame(1, 3, 0);
577 CheckNoFrame(2);
578}
579
philipel9771c502018-03-02 11:06:27 +0100580TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
581 const int kMaxBufferSize = 600;
582
583 for (int i = 1; i <= kMaxBufferSize; ++i)
584 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, false, i - 1));
585 ExtractFrame();
586 CheckNoFrame(0);
587
588 EXPECT_EQ(
589 kMaxBufferSize + 1,
590 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000, false));
591 ExtractFrame();
592 CheckFrame(1, kMaxBufferSize + 1, 0);
593}
594
philipelbe7a9e52016-05-19 12:19:35 +0200595} // namespace video_coding
596} // namespace webrtc