blob: 13ad66c5c350a30019b9e7a9f2c833ffea2b9c27 [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>
Mirko Bonadei317a1f02019-09-17 17:06:18 +020016#include <memory>
philipelbe7a9e52016-05-19 12:19:35 +020017#include <vector>
18
Danil Chapovalov03f8b8a2022-07-18 13:11:42 +020019#include "api/task_queue/task_queue_base.h"
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010020#include "api/units/time_delta.h"
21#include "api/units/timestamp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/frame_object.h"
Rasmus Brandt10944e62022-05-25 10:12:42 +020023#include "modules/video_coding/timing/jitter_estimator.h"
Rasmus Brandtc4d253c2022-05-25 12:03:35 +020024#include "modules/video_coding/timing/timing.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020025#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/platform_thread.h"
27#include "rtc_base/random.h"
28#include "system_wrappers/include/clock.h"
Niels Möller7cca0422019-04-29 16:12:19 +020029#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "test/gmock.h"
31#include "test/gtest.h"
Jonas Orelande02f9ee2022-03-25 12:43:14 +010032#include "test/scoped_key_value_config.h"
philipel19053972020-01-29 17:36:11 +010033#include "test/time_controller/simulated_time_controller.h"
philipelbe7a9e52016-05-19 12:19:35 +020034
Mirko Bonadei6a489f22019-04-09 15:11:12 +020035using ::testing::_;
Evan Shrubsole0b565632021-11-05 09:58:20 +010036using ::testing::IsEmpty;
Mirko Bonadei6a489f22019-04-09 15:11:12 +020037using ::testing::Return;
Evan Shrubsole0b565632021-11-05 09:58:20 +010038using ::testing::SizeIs;
philipela45102f2017-02-22 05:30:39 -080039
philipelbe7a9e52016-05-19 12:19:35 +020040namespace webrtc {
41namespace video_coding {
42
43class VCMTimingFake : public VCMTiming {
44 public:
Jonas Orelande62c2f22022-03-29 11:04:48 +020045 explicit VCMTimingFake(Clock* clock, const FieldTrialsView& field_trials)
Jonas Orelande02f9ee2022-03-25 12:43:14 +010046 : VCMTiming(clock, field_trials) {}
philipelbe7a9e52016-05-19 12:19:35 +020047
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010048 Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const override {
49 if (last_render_time_.IsMinusInfinity()) {
50 last_render_time_ = now + kDelay;
philipelbe7a9e52016-05-19 12:19:35 +020051 last_timestamp_ = frame_timestamp;
52 }
53
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010054 auto diff = MinDiff(frame_timestamp, last_timestamp_);
55 auto timeDiff = TimeDelta::Millis(diff / 90);
philipelbe7a9e52016-05-19 12:19:35 +020056 if (AheadOf(frame_timestamp, last_timestamp_))
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010057 last_render_time_ += timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020058 else
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010059 last_render_time_ -= timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020060
61 last_timestamp_ = frame_timestamp;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010062 return last_render_time_;
philipelbe7a9e52016-05-19 12:19:35 +020063 }
64
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010065 TimeDelta MaxWaitingTime(Timestamp render_time,
66 Timestamp now,
67 bool too_many_frames_queued) const override {
68 return render_time - now - kDecodeTime;
philipelbe7a9e52016-05-19 12:19:35 +020069 }
70
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010071 TimeDelta GetCurrentJitter() {
Evan Shrubsole92e89d72022-03-22 10:55:15 +010072 return VCMTiming::GetTimings().jitter_buffer_delay;
Niels Möller7cca0422019-04-29 16:12:19 +020073 }
74
philipelbe7a9e52016-05-19 12:19:35 +020075 private:
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010076 static constexpr TimeDelta kDelay = TimeDelta::Millis(50);
77 const TimeDelta kDecodeTime = kDelay / 2;
philipelbe7a9e52016-05-19 12:19:35 +020078 mutable uint32_t last_timestamp_ = 0;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010079 mutable Timestamp last_render_time_ = Timestamp::MinusInfinity();
philipelbe7a9e52016-05-19 12:19:35 +020080};
81
philipele7c891f2018-02-22 14:35:06 +010082class FrameObjectFake : public EncodedFrame {
philipelbe7a9e52016-05-19 12:19:35 +020083 public:
philipelb4d31082016-07-11 08:46:29 -070084 int64_t ReceivedTime() const override { return 0; }
85
86 int64_t RenderTime() const override { return _renderTimeMs; }
Niels Möller7cca0422019-04-29 16:12:19 +020087
88 bool delayed_by_retransmission() const override {
89 return delayed_by_retransmission_;
90 }
91 void set_delayed_by_retransmission(bool delayed) {
92 delayed_by_retransmission_ = delayed;
93 }
94
95 private:
96 bool delayed_by_retransmission_ = false;
philipela45102f2017-02-22 05:30:39 -080097};
98
99class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
100 public:
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200101 MOCK_METHOD(void,
102 OnCompleteFrame,
103 (bool is_keyframe,
104 size_t size_bytes,
105 VideoContentType content_type),
106 (override));
107 MOCK_METHOD(void, OnDroppedFrames, (uint32_t frames_dropped), (override));
108 MOCK_METHOD(void,
109 OnFrameBufferTimingsUpdated,
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100110 (int max_decode,
111 int current_delay,
112 int target_delay,
113 int jitter_buffer,
114 int min_playout_delay,
115 int render_delay),
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200116 (override));
117 MOCK_METHOD(void,
118 OnTimingFrameInfoUpdated,
119 (const TimingFrameInfo& info),
120 (override));
philipelbe7a9e52016-05-19 12:19:35 +0200121};
122
123class TestFrameBuffer2 : public ::testing::Test {
124 protected:
125 static constexpr int kMaxReferences = 5;
126 static constexpr int kFps1 = 1000;
127 static constexpr int kFps10 = kFps1 / 10;
128 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100129 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200130
131 TestFrameBuffer2()
Evan Shrubsole9146d762021-10-13 11:19:48 +0200132 : time_controller_(Timestamp::Seconds(0)),
philipel19053972020-01-29 17:36:11 +0100133 time_task_queue_(
134 time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
135 "extract queue",
136 TaskQueueFactory::Priority::NORMAL)),
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100137 timing_(time_controller_.GetClock(), field_trials_),
philipel19053972020-01-29 17:36:11 +0100138 buffer_(new FrameBuffer(time_controller_.GetClock(),
139 &timing_,
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100140 &stats_callback_,
141 field_trials_)),
philipel19053972020-01-29 17:36:11 +0100142 rand_(0x34678213) {}
philipelbe7a9e52016-05-19 12:19:35 +0200143
144 template <typename... T>
Niels Möller7cca0422019-04-29 16:12:19 +0200145 std::unique_ptr<FrameObjectFake> CreateFrame(uint16_t picture_id,
146 uint8_t spatial_layer,
147 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200148 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200149 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200150 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200151 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100152 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700153 std::array<uint16_t, sizeof...(refs)> references = {
154 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200155
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200156 auto frame = std::make_unique<FrameObjectFake>();
philipel9aa9b8d2021-02-15 13:31:29 +0100157 frame->SetId(picture_id);
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();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100161 frame->is_last_spatial_layer = last_spatial_layer;
162 // Add some data to buffer.
Niels Möllerb9bfe652019-10-03 08:43:53 +0200163 frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes));
philipelbe7a9e52016-05-19 12:19:35 +0200164 for (size_t r = 0; r < references.size(); ++r)
165 frame->references[r] = references[r];
Niels Möller7cca0422019-04-29 16:12:19 +0200166 return frame;
167 }
philipelbe7a9e52016-05-19 12:19:35 +0200168
Niels Möller7cca0422019-04-29 16:12:19 +0200169 template <typename... T>
170 int InsertFrame(uint16_t picture_id,
171 uint8_t spatial_layer,
172 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200173 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200174 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200175 T... refs) {
philipelcb327d92020-12-10 10:49:20 +0100176 return buffer_->InsertFrame(CreateFrame(picture_id, spatial_layer, ts_ms,
177 last_spatial_layer,
178 frame_size_bytes, refs...));
Niels Möller7cca0422019-04-29 16:12:19 +0200179 }
180
181 int InsertNackedFrame(uint16_t picture_id, int64_t ts_ms) {
182 std::unique_ptr<FrameObjectFake> frame =
philipelcb327d92020-12-10 10:49:20 +0100183 CreateFrame(picture_id, 0, ts_ms, true, kFrameSize);
Niels Möller7cca0422019-04-29 16:12:19 +0200184 frame->set_delayed_by_retransmission(true);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200185 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200186 }
187
philipel3042c2d2017-08-18 04:55:02 -0700188 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
Danil Chapovalov03f8b8a2022-07-18 13:11:42 +0200189 time_task_queue_->PostTask([this, max_wait_time, keyframe_required]() {
190 buffer_->NextFrame(max_wait_time, keyframe_required,
191 time_task_queue_.get(),
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100192 [this](std::unique_ptr<EncodedFrame> frame) {
193 frames_.emplace_back(std::move(frame));
194 });
philipel19053972020-01-29 17:36:11 +0100195 });
philipelbe7a9e52016-05-19 12:19:35 +0200196 if (max_wait_time == 0) {
Philipp Hanckea204ad22022-07-08 18:43:25 +0200197 time_controller_.AdvanceTime(TimeDelta::Zero());
philipelbe7a9e52016-05-19 12:19:35 +0200198 }
199 }
200
201 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
philipelbe7a9e52016-05-19 12:19:35 +0200202 ASSERT_LT(index, frames_.size());
203 ASSERT_TRUE(frames_[index]);
philipel9aa9b8d2021-02-15 13:31:29 +0100204 ASSERT_EQ(picture_id, frames_[index]->Id());
philipelcb327d92020-12-10 10:49:20 +0100205 ASSERT_EQ(spatial_layer, frames_[index]->SpatialIndex().value_or(0));
philipelbe7a9e52016-05-19 12:19:35 +0200206 }
207
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100208 void CheckFrameSize(size_t index, size_t size) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100209 ASSERT_LT(index, frames_.size());
210 ASSERT_TRUE(frames_[index]);
211 ASSERT_EQ(frames_[index]->size(), size);
212 }
213
philipelbe7a9e52016-05-19 12:19:35 +0200214 void CheckNoFrame(size_t index) {
philipelbe7a9e52016-05-19 12:19:35 +0200215 ASSERT_LT(index, frames_.size());
216 ASSERT_FALSE(frames_[index]);
217 }
218
philipelbe7a9e52016-05-19 12:19:35 +0200219 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
220
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100221 test::ScopedKeyValueConfig field_trials_;
philipel19053972020-01-29 17:36:11 +0100222 webrtc::GlobalSimulatedTimeController time_controller_;
Danil Chapovalov03f8b8a2022-07-18 13:11:42 +0200223 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> time_task_queue_;
philipelbe7a9e52016-05-19 12:19:35 +0200224 VCMTimingFake timing_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200225 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100226 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200227 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800228 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200229};
230
Sergey Silkin2799e632019-05-17 09:51:39 +0200231// From https://en.cppreference.com/w/cpp/language/static: "If ... a constexpr
232// static data member (since C++11) is odr-used, a definition at namespace scope
233// is still required... This definition is deprecated for constexpr data members
234// since C++17."
235// kFrameSize is odr-used since it is passed by reference to EXPECT_EQ().
236#if __cplusplus < 201703L
237constexpr size_t TestFrameBuffer2::kFrameSize;
238#endif
239
philipelbe7a9e52016-05-19 12:19:35 +0200240TEST_F(TestFrameBuffer2, WaitForFrame) {
241 uint16_t pid = Rand();
242 uint32_t ts = Rand();
243
philipel6e8224f2016-05-19 17:07:44 +0200244 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100245 InsertFrame(pid, 0, ts, true, kFrameSize);
Danil Chapovalov55284022020-02-07 14:53:52 +0100246 time_controller_.AdvanceTime(TimeDelta::Millis(50));
philipelbe7a9e52016-05-19 12:19:35 +0200247 CheckFrame(0, pid, 0);
248}
249
Evan Shrubsole0b565632021-11-05 09:58:20 +0100250TEST_F(TestFrameBuffer2, ClearWhileWaitingForFrame) {
251 const uint16_t pid = Rand();
252
253 // Insert a frame and wait for it for max 100ms.
254 InsertFrame(pid, 0, 25, true, kFrameSize);
255 ExtractFrame(100);
256 // After 10ms, clear the buffer.
257 time_controller_.AdvanceTime(TimeDelta::Millis(10));
258 buffer_->Clear();
259 // Confirm that the frame was not sent for rendering.
260 time_controller_.AdvanceTime(TimeDelta::Millis(15));
261 EXPECT_THAT(frames_, IsEmpty());
262
263 // We are still waiting for a frame, since 100ms has not passed. Insert a new
264 // frame. This new frame should be the one that is returned as the old frame
265 // was cleared.
266 const uint16_t new_pid = pid + 1;
267 InsertFrame(new_pid, 0, 50, true, kFrameSize);
268 time_controller_.AdvanceTime(TimeDelta::Millis(25));
269 ASSERT_THAT(frames_, SizeIs(1));
270 CheckFrame(0, new_pid, 0);
271}
272
philipelbe7a9e52016-05-19 12:19:35 +0200273TEST_F(TestFrameBuffer2, OneSuperFrame) {
274 uint16_t pid = Rand();
275 uint32_t ts = Rand();
276
philipelcb327d92020-12-10 10:49:20 +0100277 InsertFrame(pid, 0, ts, false, kFrameSize);
278 InsertFrame(pid + 1, 1, ts, true, kFrameSize);
philipele0b2f152016-09-28 10:23:49 +0200279 ExtractFrame();
280
Sergey Silkin61832dd2018-12-20 14:32:14 +0100281 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200282}
283
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200284TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100285 test::ScopedKeyValueConfig field_trials;
286 VCMTiming timing(time_controller_.GetClock(), field_trials);
287 buffer_.reset(new FrameBuffer(time_controller_.GetClock(), &timing,
288 &stats_callback_, field_trials));
Niels Möllerd381eed2020-09-02 15:34:40 +0200289 const VideoPlayoutDelay kPlayoutDelayMs = {0, 0};
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200290 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel9aa9b8d2021-02-15 13:31:29 +0100291 test_frame->SetId(0);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200292 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
293 buffer_->InsertFrame(std::move(test_frame));
294 ExtractFrame(0, false);
295 CheckFrame(0, 0, 0);
296 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
297}
298
aleloi3e005282017-01-26 05:38:00 -0800299// Flaky test, see bugs.webrtc.org/7068.
300TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200301 uint16_t pid = Rand();
302 uint32_t ts = Rand();
303
philipel6e8224f2016-05-19 17:07:44 +0200304 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100305 InsertFrame(pid, 1, ts, true, kFrameSize);
306 InsertFrame(pid, 0, ts, false, kFrameSize);
Philipp Hanckea204ad22022-07-08 18:43:25 +0200307 time_controller_.AdvanceTime(TimeDelta::Zero());
philipelbe7a9e52016-05-19 12:19:35 +0200308
309 CheckFrame(0, pid, 0);
310 CheckFrame(1, pid, 1);
311}
312
philipel94616b32016-05-20 10:43:01 +0200313TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200314 uint16_t pid = Rand();
315 uint32_t ts = Rand();
316
Sergey Silkin2799e632019-05-17 09:51:39 +0200317 InsertFrame(pid, 0, ts, false, true, kFrameSize);
philipel6e8224f2016-05-19 17:07:44 +0200318 ExtractFrame();
319 CheckFrame(0, pid, 0);
320 for (int i = 1; i < 10; i += 2) {
321 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100322 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, true, kFrameSize,
Sergey Silkin2799e632019-05-17 09:51:39 +0200323 pid + i);
Danil Chapovalov55284022020-02-07 14:53:52 +0100324 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelcb327d92020-12-10 10:49:20 +0100325 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100326 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipel6e8224f2016-05-19 17:07:44 +0200327 ExtractFrame();
328 CheckFrame(i, pid + i, 0);
329 CheckFrame(i + 1, pid + i + 1, 0);
330 }
331}
philipel6e8224f2016-05-19 17:07:44 +0200332
333TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
334 ExtractFrame();
335 CheckNoFrame(0);
336}
337
philipel93e451b2016-10-06 12:25:13 +0200338TEST_F(TestFrameBuffer2, MissingFrame) {
339 uint16_t pid = Rand();
340 uint32_t ts = Rand();
341
philipelcb327d92020-12-10 10:49:20 +0100342 InsertFrame(pid, 0, ts, true, kFrameSize);
343 InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid);
344 InsertFrame(pid + 3, 0, ts, true, kFrameSize, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200345 ExtractFrame();
346 ExtractFrame();
347 ExtractFrame();
348
349 CheckFrame(0, pid, 0);
350 CheckFrame(1, pid + 2, 0);
351 CheckNoFrame(2);
352}
353
philipelbe7a9e52016-05-19 12:19:35 +0200354TEST_F(TestFrameBuffer2, OneLayerStream) {
355 uint16_t pid = Rand();
356 uint32_t ts = Rand();
357
philipelcb327d92020-12-10 10:49:20 +0100358 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200359 ExtractFrame();
360 CheckFrame(0, pid, 0);
361 for (int i = 1; i < 10; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100362 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200363 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100364 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelbe7a9e52016-05-19 12:19:35 +0200365 CheckFrame(i, pid + i, 0);
366 }
367}
368
philipelbe7a9e52016-05-19 12:19:35 +0200369TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
370 uint16_t pid = Rand();
371 uint32_t ts = Rand();
372
philipelcb327d92020-12-10 10:49:20 +0100373 InsertFrame(pid, 0, ts, true, kFrameSize);
374 InsertFrame(pid + 1, 0, ts + kFps20, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200375 for (int i = 2; i < 10; i += 2) {
376 uint32_t ts_tl0 = ts + i / 2 * kFps10;
philipelcb327d92020-12-10 10:49:20 +0100377 InsertFrame(pid + i, 0, ts_tl0, true, kFrameSize, pid + i - 2);
378 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, true, kFrameSize, pid + i,
379 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200380 }
381
Johannes Kron0c141c52019-08-26 15:04:43 +0200382 EXPECT_CALL(stats_callback_, OnDroppedFrames(1)).Times(3);
383
philipelbe7a9e52016-05-19 12:19:35 +0200384 for (int i = 0; i < 10; ++i) {
385 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100386 time_controller_.AdvanceTime(TimeDelta::Millis(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
Johannes Kron0c141c52019-08-26 15:04:43 +0200401TEST_F(TestFrameBuffer2, DropFramesIfSystemIsStalled) {
402 uint16_t pid = Rand();
403 uint32_t ts = Rand();
404
philipelcb327d92020-12-10 10:49:20 +0100405 InsertFrame(pid, 0, ts, true, kFrameSize);
406 InsertFrame(pid + 1, 0, ts + 1 * kFps10, true, kFrameSize, pid);
407 InsertFrame(pid + 2, 0, ts + 2 * kFps10, true, kFrameSize, pid + 1);
408 InsertFrame(pid + 3, 0, ts + 3 * kFps10, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200409
410 ExtractFrame();
411 // Jump forward in time, simulating the system being stalled for some reason.
Danil Chapovalov55284022020-02-07 14:53:52 +0100412 time_controller_.AdvanceTime(TimeDelta::Millis(3) * kFps10);
Johannes Kron0c141c52019-08-26 15:04:43 +0200413 // Extract one more frame, expect second and third frame to be dropped.
414 EXPECT_CALL(stats_callback_, OnDroppedFrames(2)).Times(1);
415 ExtractFrame();
416
417 CheckFrame(0, pid + 0, 0);
418 CheckFrame(1, pid + 3, 0);
419}
420
421TEST_F(TestFrameBuffer2, DroppedFramesCountedOnClear) {
422 uint16_t pid = Rand();
423 uint32_t ts = Rand();
424
philipelcb327d92020-12-10 10:49:20 +0100425 InsertFrame(pid, 0, ts, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200426 for (int i = 1; i < 5; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100427 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Johannes Kron0c141c52019-08-26 15:04:43 +0200428 }
429
430 // All frames should be dropped when Clear is called.
431 EXPECT_CALL(stats_callback_, OnDroppedFrames(5)).Times(1);
432 buffer_->Clear();
433}
434
philipelbe7a9e52016-05-19 12:19:35 +0200435TEST_F(TestFrameBuffer2, InsertLateFrame) {
436 uint16_t pid = Rand();
437 uint32_t ts = Rand();
438
philipelcb327d92020-12-10 10:49:20 +0100439 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200440 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100441 InsertFrame(pid + 2, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200442 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100443 InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200444 ExtractFrame();
445
446 CheckFrame(0, pid, 0);
447 CheckFrame(1, pid + 2, 0);
448 CheckNoFrame(2);
449}
450
Niels Möller7cca0422019-04-29 16:12:19 +0200451TEST_F(TestFrameBuffer2, ProtectionModeNackFEC) {
philipel4f6cd6a2016-08-03 10:59:32 +0200452 uint16_t pid = Rand();
453 uint32_t ts = Rand();
Niels Möller7cca0422019-04-29 16:12:19 +0200454 constexpr int64_t kRttMs = 200;
455 buffer_->UpdateRtt(kRttMs);
philipel4f6cd6a2016-08-03 10:59:32 +0200456
Niels Möller7cca0422019-04-29 16:12:19 +0200457 // Jitter estimate unaffected by RTT in this protection mode.
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200458 buffer_->SetProtectionMode(kProtectionNackFEC);
Niels Möller7cca0422019-04-29 16:12:19 +0200459 InsertNackedFrame(pid, ts);
460 InsertNackedFrame(pid + 1, ts + 100);
461 InsertNackedFrame(pid + 2, ts + 200);
philipelcb327d92020-12-10 10:49:20 +0100462 InsertFrame(pid + 3, 0, ts + 300, true, kFrameSize);
philipel4f6cd6a2016-08-03 10:59:32 +0200463 ExtractFrame();
Niels Möller7cca0422019-04-29 16:12:19 +0200464 ExtractFrame();
465 ExtractFrame();
466 ExtractFrame();
467 ASSERT_EQ(4u, frames_.size());
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100468 EXPECT_LT(timing_.GetCurrentJitter().ms(), kRttMs);
Niels Möller7cca0422019-04-29 16:12:19 +0200469}
470
philipele0b2f152016-09-28 10:23:49 +0200471TEST_F(TestFrameBuffer2, NoContinuousFrame) {
472 uint16_t pid = Rand();
473 uint32_t ts = Rand();
474
philipelcb327d92020-12-10 10:49:20 +0100475 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
philipele0b2f152016-09-28 10:23:49 +0200476}
477
478TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
479 uint16_t pid = Rand();
480 uint32_t ts = Rand();
481
philipelcb327d92020-12-10 10:49:20 +0100482 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
483 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid + 1));
484 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
485 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, true, kFrameSize, pid + 3));
486 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, true, kFrameSize));
philipele0b2f152016-09-28 10:23:49 +0200487}
488
489TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
490 uint16_t pid = Rand();
491 uint32_t ts = Rand();
492
philipelcb327d92020-12-10 10:49:20 +0100493 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, kFrameSize));
494 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 1, ts, true, kFrameSize));
495 EXPECT_EQ(pid + 1,
496 InsertFrame(pid + 3, 1, ts, true, kFrameSize, pid + 1, pid + 2));
497 EXPECT_EQ(pid + 1, InsertFrame(pid + 4, 0, ts, false, kFrameSize, pid + 2));
498 EXPECT_EQ(pid + 1,
499 InsertFrame(pid + 5, 1, ts, true, kFrameSize, pid + 3, pid + 4));
500 EXPECT_EQ(pid + 1, InsertFrame(pid + 6, 0, ts, false, kFrameSize, pid + 4));
501 EXPECT_EQ(pid + 6, InsertFrame(pid + 2, 0, ts, false, kFrameSize, pid));
502 EXPECT_EQ(pid + 7,
503 InsertFrame(pid + 7, 1, ts, true, kFrameSize, pid + 5, pid + 6));
philipele0b2f152016-09-28 10:23:49 +0200504}
505
philipelfcc60062017-01-18 05:35:20 -0800506TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
507 uint16_t pid = Rand();
508 uint32_t ts = Rand();
509
philipelcb327d92020-12-10 10:49:20 +0100510 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
511 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, true, kFrameSize, pid));
philipelfcc60062017-01-18 05:35:20 -0800512 ExtractFrame();
513 CheckFrame(0, pid, 0);
514
515 // Jump back in pid but increase ts.
philipelcb327d92020-12-10 10:49:20 +0100516 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, true, kFrameSize));
philipelfcc60062017-01-18 05:35:20 -0800517 ExtractFrame();
518 ExtractFrame();
519 CheckFrame(1, pid - 1, 0);
520 CheckNoFrame(2);
521}
522
philipela45102f2017-02-22 05:30:39 -0800523TEST_F(TestFrameBuffer2, StatsCallback) {
524 uint16_t pid = Rand();
525 uint32_t ts = Rand();
526 const int kFrameSize = 5000;
527
ilnik6d5b4d62017-08-30 03:32:14 -0700528 EXPECT_CALL(stats_callback_,
529 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
Johannes Kronbfd343b2019-07-01 10:07:50 +0200530 EXPECT_CALL(stats_callback_, OnFrameBufferTimingsUpdated(_, _, _, _, _, _));
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100531 // Stats callback requires a previously decoded frame.
532 timing_.StopDecodeTimer(TimeDelta::Millis(1), Timestamp::Zero());
philipela45102f2017-02-22 05:30:39 -0800533
534 {
535 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Niels Möllerb9bfe652019-10-03 08:43:53 +0200536 frame->SetEncodedData(EncodedImageBuffer::Create(kFrameSize));
philipel9aa9b8d2021-02-15 13:31:29 +0100537 frame->SetId(pid);
Niels Möller23775882018-08-16 10:24:12 +0200538 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800539 frame->num_references = 0;
philipela45102f2017-02-22 05:30:39 -0800540
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200541 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800542 }
543
544 ExtractFrame();
545 CheckFrame(0, pid, 0);
546}
547
philipel146a48b2017-04-20 04:04:38 -0700548TEST_F(TestFrameBuffer2, ForwardJumps) {
philipelcb327d92020-12-10 10:49:20 +0100549 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700550 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100551 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, true, kFrameSize, 5453));
philipel146a48b2017-04-20 04:04:38 -0700552 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100553 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700554 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100555 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700556 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100557 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, true, kFrameSize, 29804));
philipel146a48b2017-04-20 04:04:38 -0700558 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100559 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, true, kFrameSize, 29805));
philipel146a48b2017-04-20 04:04:38 -0700560 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100561 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700562 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100563 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700564 ExtractFrame();
565}
566
philipelf6842692017-04-28 03:29:15 -0700567TEST_F(TestFrameBuffer2, DuplicateFrames) {
philipelcb327d92020-12-10 10:49:20 +0100568 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700569 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100570 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700571}
572
philipel112adf92017-06-15 09:06:21 -0700573// TODO(philipel): implement more unittests related to invalid references.
574TEST_F(TestFrameBuffer2, InvalidReferences) {
philipelcb327d92020-12-10 10:49:20 +0100575 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, true, kFrameSize, 2));
576 EXPECT_EQ(1, InsertFrame(1, 0, 2000, true, kFrameSize));
philipel112adf92017-06-15 09:06:21 -0700577 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100578 EXPECT_EQ(2, InsertFrame(2, 0, 3000, true, kFrameSize, 1));
philipel112adf92017-06-15 09:06:21 -0700579}
580
philipel3042c2d2017-08-18 04:55:02 -0700581TEST_F(TestFrameBuffer2, KeyframeRequired) {
philipelcb327d92020-12-10 10:49:20 +0100582 EXPECT_EQ(1, InsertFrame(1, 0, 1000, true, kFrameSize));
583 EXPECT_EQ(2, InsertFrame(2, 0, 2000, true, kFrameSize, 1));
584 EXPECT_EQ(3, InsertFrame(3, 0, 3000, true, kFrameSize));
philipel3042c2d2017-08-18 04:55:02 -0700585 ExtractFrame();
586 ExtractFrame(0, true);
587 ExtractFrame();
588
589 CheckFrame(0, 1, 0);
590 CheckFrame(1, 3, 0);
591 CheckNoFrame(2);
592}
593
philipel9771c502018-03-02 11:06:27 +0100594TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
595 const int kMaxBufferSize = 600;
596
597 for (int i = 1; i <= kMaxBufferSize; ++i)
philipelcb327d92020-12-10 10:49:20 +0100598 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, true, kFrameSize, i - 1));
philipel9771c502018-03-02 11:06:27 +0100599 ExtractFrame();
600 CheckNoFrame(0);
601
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100602 EXPECT_EQ(kMaxBufferSize + 1,
603 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
philipelcb327d92020-12-10 10:49:20 +0100604 true, kFrameSize));
philipel9771c502018-03-02 11:06:27 +0100605 ExtractFrame();
606 CheckFrame(1, kMaxBufferSize + 1, 0);
607}
608
philipel798b2822018-06-11 13:10:14 +0200609TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
philipelcb327d92020-12-10 10:49:20 +0100610 InsertFrame(1, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200611 ExtractFrame(0, true);
philipelcb327d92020-12-10 10:49:20 +0100612 InsertFrame(3, 0, 0, true, kFrameSize, 2, 0);
613 InsertFrame(3, 0, 0, true, kFrameSize, 0);
614 InsertFrame(2, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200615 ExtractFrame(0, true);
616 ExtractFrame(0, true);
617}
618
philipel6d216502018-10-22 14:36:45 +0200619TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
philipelcb327d92020-12-10 10:49:20 +0100620 InsertFrame(2, 0, 1, true, kFrameSize);
621 InsertFrame(1, 0, 2, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200622 kFrameSize); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200623 ExtractFrame(0);
624 ExtractFrame(0);
625 CheckFrame(0, 1, 0);
626 CheckNoFrame(1);
627
philipelcb327d92020-12-10 10:49:20 +0100628 InsertFrame(3, 0, 4, true, kFrameSize);
629 InsertFrame(4, 0, 3, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200630 kFrameSize); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200631 ExtractFrame(0);
632 ExtractFrame(0);
633 CheckFrame(2, 3, 0);
634 CheckNoFrame(3);
635}
636
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100637TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
638 uint16_t pid = Rand();
639 uint32_t ts = Rand();
640
philipelcb327d92020-12-10 10:49:20 +0100641 InsertFrame(pid, 0, ts, false, kFrameSize);
642 InsertFrame(pid + 1, 1, ts, true, 2 * kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100643 ExtractFrame(0);
644 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100645 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100646 CheckNoFrame(1);
647 // Two frames should be combined and returned together.
Sergey Silkin2799e632019-05-17 09:51:39 +0200648 CheckFrameSize(0, 3 * kFrameSize);
649
650 EXPECT_EQ(frames_[0]->SpatialIndex(), 1);
651 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(0), kFrameSize);
652 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(1), 2 * kFrameSize);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100653}
654
655TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
656 uint16_t pid = Rand();
657 uint32_t ts = Rand();
658
philipelcb327d92020-12-10 10:49:20 +0100659 InsertFrame(pid, 0, ts, false, kFrameSize);
660 InsertFrame(pid + 1, 1, ts, true, kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100661
662 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100663 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100664
philipelcb327d92020-12-10 10:49:20 +0100665 InsertFrame(pid + 3, 1, ts + kFps20, true, kFrameSize, pid);
666 InsertFrame(pid + 4, 0, ts + kFps10, false, kFrameSize, pid);
667 InsertFrame(pid + 5, 1, ts + kFps10, true, kFrameSize, pid + 3, pid + 4);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100668
Danil Chapovalov55284022020-02-07 14:53:52 +0100669 time_controller_.AdvanceTime(TimeDelta::Millis(1000));
philipelcb327d92020-12-10 10:49:20 +0100670 // Frame pid+3 is decodable but too late.
671 // In superframe pid+4 is decodable, but frame pid+5 is not.
672 // Incorrect implementation might skip pid+2 frame and output undecodable
673 // pid+5 instead.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100674 ExtractFrame();
675 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100676 CheckFrame(1, pid + 3, 1);
677 CheckFrame(2, pid + 4, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100678}
679
Evan Shrubsole0b565632021-11-05 09:58:20 +0100680TEST_F(TestFrameBuffer2, StopWhileWaitingForFrame) {
681 uint16_t pid = Rand();
682 uint32_t ts = Rand();
683
684 InsertFrame(pid, 0, ts, true, kFrameSize);
685 ExtractFrame(10);
686 buffer_->Stop();
687 time_controller_.AdvanceTime(TimeDelta::Millis(10));
688 EXPECT_THAT(frames_, IsEmpty());
689
690 // A new frame request should exit immediately and return no new frame.
691 ExtractFrame(0);
692 EXPECT_THAT(frames_, IsEmpty());
693}
694
philipelbe7a9e52016-05-19 12:19:35 +0200695} // namespace video_coding
696} // namespace webrtc