blob: 4811635d6476175e42baeaabe7288d81e969dc5f [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
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010019#include "api/units/time_delta.h"
20#include "api/units/timestamp.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/video_coding/frame_object.h"
22#include "modules/video_coding/jitter_estimator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/timing.h"
Bjorn Tereliusa194e582017-10-25 13:07:09 +020024#include "rtc_base/numerics/sequence_number_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "rtc_base/platform_thread.h"
26#include "rtc_base/random.h"
27#include "system_wrappers/include/clock.h"
Niels Möller7cca0422019-04-29 16:12:19 +020028#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "test/gmock.h"
30#include "test/gtest.h"
philipel19053972020-01-29 17:36:11 +010031#include "test/time_controller/simulated_time_controller.h"
philipelbe7a9e52016-05-19 12:19:35 +020032
Mirko Bonadei6a489f22019-04-09 15:11:12 +020033using ::testing::_;
Evan Shrubsole0b565632021-11-05 09:58:20 +010034using ::testing::IsEmpty;
Mirko Bonadei6a489f22019-04-09 15:11:12 +020035using ::testing::Return;
Evan Shrubsole0b565632021-11-05 09:58:20 +010036using ::testing::SizeIs;
philipela45102f2017-02-22 05:30:39 -080037
philipelbe7a9e52016-05-19 12:19:35 +020038namespace webrtc {
39namespace video_coding {
40
41class VCMTimingFake : public VCMTiming {
42 public:
43 explicit VCMTimingFake(Clock* clock) : VCMTiming(clock) {}
44
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010045 Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const override {
46 if (last_render_time_.IsMinusInfinity()) {
47 last_render_time_ = now + kDelay;
philipelbe7a9e52016-05-19 12:19:35 +020048 last_timestamp_ = frame_timestamp;
49 }
50
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010051 auto diff = MinDiff(frame_timestamp, last_timestamp_);
52 auto timeDiff = TimeDelta::Millis(diff / 90);
philipelbe7a9e52016-05-19 12:19:35 +020053 if (AheadOf(frame_timestamp, last_timestamp_))
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010054 last_render_time_ += timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020055 else
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010056 last_render_time_ -= timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020057
58 last_timestamp_ = frame_timestamp;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010059 return last_render_time_;
philipelbe7a9e52016-05-19 12:19:35 +020060 }
61
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010062 TimeDelta MaxWaitingTime(Timestamp render_time,
63 Timestamp now,
64 bool too_many_frames_queued) const override {
65 return render_time - now - kDecodeTime;
philipelbe7a9e52016-05-19 12:19:35 +020066 }
67
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010068 bool GetTimings(TimeDelta* max_decode,
69 TimeDelta* current_delay,
70 TimeDelta* target_delay,
71 TimeDelta* jitter_buffer,
72 TimeDelta* min_playout_delay,
73 TimeDelta* render_delay) const override {
philipela45102f2017-02-22 05:30:39 -080074 return true;
75 }
76
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010077 TimeDelta GetCurrentJitter() {
78 TimeDelta max_decode = TimeDelta::Zero();
79 TimeDelta current_delay = TimeDelta::Zero();
80 TimeDelta target_delay = TimeDelta::Zero();
81 TimeDelta jitter_buffer = TimeDelta::Zero();
82 TimeDelta min_playout_delay = TimeDelta::Zero();
83 TimeDelta render_delay = TimeDelta::Zero();
84 VCMTiming::GetTimings(&max_decode, &current_delay, &target_delay,
85 &jitter_buffer, &min_playout_delay, &render_delay);
86 return jitter_buffer;
Niels Möller7cca0422019-04-29 16:12:19 +020087 }
88
philipelbe7a9e52016-05-19 12:19:35 +020089 private:
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010090 static constexpr TimeDelta kDelay = TimeDelta::Millis(50);
91 const TimeDelta kDecodeTime = kDelay / 2;
philipelbe7a9e52016-05-19 12:19:35 +020092 mutable uint32_t last_timestamp_ = 0;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010093 mutable Timestamp last_render_time_ = Timestamp::MinusInfinity();
philipelbe7a9e52016-05-19 12:19:35 +020094};
95
philipele7c891f2018-02-22 14:35:06 +010096class FrameObjectFake : public EncodedFrame {
philipelbe7a9e52016-05-19 12:19:35 +020097 public:
philipelb4d31082016-07-11 08:46:29 -070098 int64_t ReceivedTime() const override { return 0; }
99
100 int64_t RenderTime() const override { return _renderTimeMs; }
Niels Möller7cca0422019-04-29 16:12:19 +0200101
102 bool delayed_by_retransmission() const override {
103 return delayed_by_retransmission_;
104 }
105 void set_delayed_by_retransmission(bool delayed) {
106 delayed_by_retransmission_ = delayed;
107 }
108
109 private:
110 bool delayed_by_retransmission_ = false;
philipela45102f2017-02-22 05:30:39 -0800111};
112
113class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
114 public:
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200115 MOCK_METHOD(void,
116 OnCompleteFrame,
117 (bool is_keyframe,
118 size_t size_bytes,
119 VideoContentType content_type),
120 (override));
121 MOCK_METHOD(void, OnDroppedFrames, (uint32_t frames_dropped), (override));
122 MOCK_METHOD(void,
123 OnFrameBufferTimingsUpdated,
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100124 (int max_decode,
125 int current_delay,
126 int target_delay,
127 int jitter_buffer,
128 int min_playout_delay,
129 int render_delay),
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200130 (override));
131 MOCK_METHOD(void,
132 OnTimingFrameInfoUpdated,
133 (const TimingFrameInfo& info),
134 (override));
philipelbe7a9e52016-05-19 12:19:35 +0200135};
136
137class TestFrameBuffer2 : public ::testing::Test {
138 protected:
139 static constexpr int kMaxReferences = 5;
140 static constexpr int kFps1 = 1000;
141 static constexpr int kFps10 = kFps1 / 10;
142 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100143 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200144
145 TestFrameBuffer2()
Evan Shrubsole9146d762021-10-13 11:19:48 +0200146 : time_controller_(Timestamp::Seconds(0)),
philipel19053972020-01-29 17:36:11 +0100147 time_task_queue_(
148 time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
149 "extract queue",
150 TaskQueueFactory::Priority::NORMAL)),
151 timing_(time_controller_.GetClock()),
152 buffer_(new FrameBuffer(time_controller_.GetClock(),
153 &timing_,
154 &stats_callback_)),
155 rand_(0x34678213) {}
philipelbe7a9e52016-05-19 12:19:35 +0200156
157 template <typename... T>
Niels Möller7cca0422019-04-29 16:12:19 +0200158 std::unique_ptr<FrameObjectFake> CreateFrame(uint16_t picture_id,
159 uint8_t spatial_layer,
160 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200161 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200162 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200163 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200164 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100165 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700166 std::array<uint16_t, sizeof...(refs)> references = {
167 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200168
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200169 auto frame = std::make_unique<FrameObjectFake>();
philipel9aa9b8d2021-02-15 13:31:29 +0100170 frame->SetId(picture_id);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100171 frame->SetSpatialIndex(spatial_layer);
Niels Möller23775882018-08-16 10:24:12 +0200172 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200173 frame->num_references = references.size();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100174 frame->is_last_spatial_layer = last_spatial_layer;
175 // Add some data to buffer.
Niels Möllerb9bfe652019-10-03 08:43:53 +0200176 frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes));
philipelbe7a9e52016-05-19 12:19:35 +0200177 for (size_t r = 0; r < references.size(); ++r)
178 frame->references[r] = references[r];
Niels Möller7cca0422019-04-29 16:12:19 +0200179 return frame;
180 }
philipelbe7a9e52016-05-19 12:19:35 +0200181
Niels Möller7cca0422019-04-29 16:12:19 +0200182 template <typename... T>
183 int InsertFrame(uint16_t picture_id,
184 uint8_t spatial_layer,
185 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200186 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200187 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200188 T... refs) {
philipelcb327d92020-12-10 10:49:20 +0100189 return buffer_->InsertFrame(CreateFrame(picture_id, spatial_layer, ts_ms,
190 last_spatial_layer,
191 frame_size_bytes, refs...));
Niels Möller7cca0422019-04-29 16:12:19 +0200192 }
193
194 int InsertNackedFrame(uint16_t picture_id, int64_t ts_ms) {
195 std::unique_ptr<FrameObjectFake> frame =
philipelcb327d92020-12-10 10:49:20 +0100196 CreateFrame(picture_id, 0, ts_ms, true, kFrameSize);
Niels Möller7cca0422019-04-29 16:12:19 +0200197 frame->set_delayed_by_retransmission(true);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200198 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200199 }
200
philipel3042c2d2017-08-18 04:55:02 -0700201 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipel19053972020-01-29 17:36:11 +0100202 time_task_queue_.PostTask([this, max_wait_time, keyframe_required]() {
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100203 buffer_->NextFrame(max_wait_time, keyframe_required, &time_task_queue_,
204 [this](std::unique_ptr<EncodedFrame> frame) {
205 frames_.emplace_back(std::move(frame));
206 });
philipel19053972020-01-29 17:36:11 +0100207 });
philipelbe7a9e52016-05-19 12:19:35 +0200208 if (max_wait_time == 0) {
Danil Chapovalov55284022020-02-07 14:53:52 +0100209 time_controller_.AdvanceTime(TimeDelta::Millis(0));
philipelbe7a9e52016-05-19 12:19:35 +0200210 }
211 }
212
213 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
philipelbe7a9e52016-05-19 12:19:35 +0200214 ASSERT_LT(index, frames_.size());
215 ASSERT_TRUE(frames_[index]);
philipel9aa9b8d2021-02-15 13:31:29 +0100216 ASSERT_EQ(picture_id, frames_[index]->Id());
philipelcb327d92020-12-10 10:49:20 +0100217 ASSERT_EQ(spatial_layer, frames_[index]->SpatialIndex().value_or(0));
philipelbe7a9e52016-05-19 12:19:35 +0200218 }
219
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100220 void CheckFrameSize(size_t index, size_t size) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100221 ASSERT_LT(index, frames_.size());
222 ASSERT_TRUE(frames_[index]);
223 ASSERT_EQ(frames_[index]->size(), size);
224 }
225
philipelbe7a9e52016-05-19 12:19:35 +0200226 void CheckNoFrame(size_t index) {
philipelbe7a9e52016-05-19 12:19:35 +0200227 ASSERT_LT(index, frames_.size());
228 ASSERT_FALSE(frames_[index]);
229 }
230
philipelbe7a9e52016-05-19 12:19:35 +0200231 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
232
philipel19053972020-01-29 17:36:11 +0100233 webrtc::GlobalSimulatedTimeController time_controller_;
234 rtc::TaskQueue time_task_queue_;
philipelbe7a9e52016-05-19 12:19:35 +0200235 VCMTimingFake timing_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200236 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100237 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200238 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800239 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200240};
241
Sergey Silkin2799e632019-05-17 09:51:39 +0200242// From https://en.cppreference.com/w/cpp/language/static: "If ... a constexpr
243// static data member (since C++11) is odr-used, a definition at namespace scope
244// is still required... This definition is deprecated for constexpr data members
245// since C++17."
246// kFrameSize is odr-used since it is passed by reference to EXPECT_EQ().
247#if __cplusplus < 201703L
248constexpr size_t TestFrameBuffer2::kFrameSize;
249#endif
250
philipelbe7a9e52016-05-19 12:19:35 +0200251TEST_F(TestFrameBuffer2, WaitForFrame) {
252 uint16_t pid = Rand();
253 uint32_t ts = Rand();
254
philipel6e8224f2016-05-19 17:07:44 +0200255 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100256 InsertFrame(pid, 0, ts, true, kFrameSize);
Danil Chapovalov55284022020-02-07 14:53:52 +0100257 time_controller_.AdvanceTime(TimeDelta::Millis(50));
philipelbe7a9e52016-05-19 12:19:35 +0200258 CheckFrame(0, pid, 0);
259}
260
Evan Shrubsole0b565632021-11-05 09:58:20 +0100261TEST_F(TestFrameBuffer2, ClearWhileWaitingForFrame) {
262 const uint16_t pid = Rand();
263
264 // Insert a frame and wait for it for max 100ms.
265 InsertFrame(pid, 0, 25, true, kFrameSize);
266 ExtractFrame(100);
267 // After 10ms, clear the buffer.
268 time_controller_.AdvanceTime(TimeDelta::Millis(10));
269 buffer_->Clear();
270 // Confirm that the frame was not sent for rendering.
271 time_controller_.AdvanceTime(TimeDelta::Millis(15));
272 EXPECT_THAT(frames_, IsEmpty());
273
274 // We are still waiting for a frame, since 100ms has not passed. Insert a new
275 // frame. This new frame should be the one that is returned as the old frame
276 // was cleared.
277 const uint16_t new_pid = pid + 1;
278 InsertFrame(new_pid, 0, 50, true, kFrameSize);
279 time_controller_.AdvanceTime(TimeDelta::Millis(25));
280 ASSERT_THAT(frames_, SizeIs(1));
281 CheckFrame(0, new_pid, 0);
282}
283
philipelbe7a9e52016-05-19 12:19:35 +0200284TEST_F(TestFrameBuffer2, OneSuperFrame) {
285 uint16_t pid = Rand();
286 uint32_t ts = Rand();
287
philipelcb327d92020-12-10 10:49:20 +0100288 InsertFrame(pid, 0, ts, false, kFrameSize);
289 InsertFrame(pid + 1, 1, ts, true, kFrameSize);
philipele0b2f152016-09-28 10:23:49 +0200290 ExtractFrame();
291
Sergey Silkin61832dd2018-12-20 14:32:14 +0100292 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200293}
294
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200295TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
philipel19053972020-01-29 17:36:11 +0100296 VCMTiming timing(time_controller_.GetClock());
297 buffer_.reset(
298 new FrameBuffer(time_controller_.GetClock(), &timing, &stats_callback_));
Niels Möllerd381eed2020-09-02 15:34:40 +0200299 const VideoPlayoutDelay kPlayoutDelayMs = {0, 0};
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200300 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel9aa9b8d2021-02-15 13:31:29 +0100301 test_frame->SetId(0);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200302 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
303 buffer_->InsertFrame(std::move(test_frame));
304 ExtractFrame(0, false);
305 CheckFrame(0, 0, 0);
306 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
307}
308
aleloi3e005282017-01-26 05:38:00 -0800309// Flaky test, see bugs.webrtc.org/7068.
310TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200311 uint16_t pid = Rand();
312 uint32_t ts = Rand();
313
philipel6e8224f2016-05-19 17:07:44 +0200314 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100315 InsertFrame(pid, 1, ts, true, kFrameSize);
316 InsertFrame(pid, 0, ts, false, kFrameSize);
Danil Chapovalov55284022020-02-07 14:53:52 +0100317 time_controller_.AdvanceTime(TimeDelta::Millis(0));
philipelbe7a9e52016-05-19 12:19:35 +0200318
319 CheckFrame(0, pid, 0);
320 CheckFrame(1, pid, 1);
321}
322
philipel94616b32016-05-20 10:43:01 +0200323TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200324 uint16_t pid = Rand();
325 uint32_t ts = Rand();
326
Sergey Silkin2799e632019-05-17 09:51:39 +0200327 InsertFrame(pid, 0, ts, false, true, kFrameSize);
philipel6e8224f2016-05-19 17:07:44 +0200328 ExtractFrame();
329 CheckFrame(0, pid, 0);
330 for (int i = 1; i < 10; i += 2) {
331 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100332 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, true, kFrameSize,
Sergey Silkin2799e632019-05-17 09:51:39 +0200333 pid + i);
Danil Chapovalov55284022020-02-07 14:53:52 +0100334 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelcb327d92020-12-10 10:49:20 +0100335 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100336 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipel6e8224f2016-05-19 17:07:44 +0200337 ExtractFrame();
338 CheckFrame(i, pid + i, 0);
339 CheckFrame(i + 1, pid + i + 1, 0);
340 }
341}
philipel6e8224f2016-05-19 17:07:44 +0200342
343TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
344 ExtractFrame();
345 CheckNoFrame(0);
346}
347
philipel93e451b2016-10-06 12:25:13 +0200348TEST_F(TestFrameBuffer2, MissingFrame) {
349 uint16_t pid = Rand();
350 uint32_t ts = Rand();
351
philipelcb327d92020-12-10 10:49:20 +0100352 InsertFrame(pid, 0, ts, true, kFrameSize);
353 InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid);
354 InsertFrame(pid + 3, 0, ts, true, kFrameSize, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200355 ExtractFrame();
356 ExtractFrame();
357 ExtractFrame();
358
359 CheckFrame(0, pid, 0);
360 CheckFrame(1, pid + 2, 0);
361 CheckNoFrame(2);
362}
363
philipelbe7a9e52016-05-19 12:19:35 +0200364TEST_F(TestFrameBuffer2, OneLayerStream) {
365 uint16_t pid = Rand();
366 uint32_t ts = Rand();
367
philipelcb327d92020-12-10 10:49:20 +0100368 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200369 ExtractFrame();
370 CheckFrame(0, pid, 0);
371 for (int i = 1; i < 10; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100372 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200373 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100374 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelbe7a9e52016-05-19 12:19:35 +0200375 CheckFrame(i, pid + i, 0);
376 }
377}
378
philipelbe7a9e52016-05-19 12:19:35 +0200379TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
380 uint16_t pid = Rand();
381 uint32_t ts = Rand();
382
philipelcb327d92020-12-10 10:49:20 +0100383 InsertFrame(pid, 0, ts, true, kFrameSize);
384 InsertFrame(pid + 1, 0, ts + kFps20, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200385 for (int i = 2; i < 10; i += 2) {
386 uint32_t ts_tl0 = ts + i / 2 * kFps10;
philipelcb327d92020-12-10 10:49:20 +0100387 InsertFrame(pid + i, 0, ts_tl0, true, kFrameSize, pid + i - 2);
388 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, true, kFrameSize, pid + i,
389 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200390 }
391
Johannes Kron0c141c52019-08-26 15:04:43 +0200392 EXPECT_CALL(stats_callback_, OnDroppedFrames(1)).Times(3);
393
philipelbe7a9e52016-05-19 12:19:35 +0200394 for (int i = 0; i < 10; ++i) {
395 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100396 time_controller_.AdvanceTime(TimeDelta::Millis(70));
philipelbe7a9e52016-05-19 12:19:35 +0200397 }
398
399 CheckFrame(0, pid, 0);
400 CheckFrame(1, pid + 1, 0);
401 CheckFrame(2, pid + 2, 0);
402 CheckFrame(3, pid + 4, 0);
403 CheckFrame(4, pid + 6, 0);
404 CheckFrame(5, pid + 8, 0);
405 CheckNoFrame(6);
406 CheckNoFrame(7);
407 CheckNoFrame(8);
408 CheckNoFrame(9);
409}
410
Johannes Kron0c141c52019-08-26 15:04:43 +0200411TEST_F(TestFrameBuffer2, DropFramesIfSystemIsStalled) {
412 uint16_t pid = Rand();
413 uint32_t ts = Rand();
414
philipelcb327d92020-12-10 10:49:20 +0100415 InsertFrame(pid, 0, ts, true, kFrameSize);
416 InsertFrame(pid + 1, 0, ts + 1 * kFps10, true, kFrameSize, pid);
417 InsertFrame(pid + 2, 0, ts + 2 * kFps10, true, kFrameSize, pid + 1);
418 InsertFrame(pid + 3, 0, ts + 3 * kFps10, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200419
420 ExtractFrame();
421 // Jump forward in time, simulating the system being stalled for some reason.
Danil Chapovalov55284022020-02-07 14:53:52 +0100422 time_controller_.AdvanceTime(TimeDelta::Millis(3) * kFps10);
Johannes Kron0c141c52019-08-26 15:04:43 +0200423 // Extract one more frame, expect second and third frame to be dropped.
424 EXPECT_CALL(stats_callback_, OnDroppedFrames(2)).Times(1);
425 ExtractFrame();
426
427 CheckFrame(0, pid + 0, 0);
428 CheckFrame(1, pid + 3, 0);
429}
430
431TEST_F(TestFrameBuffer2, DroppedFramesCountedOnClear) {
432 uint16_t pid = Rand();
433 uint32_t ts = Rand();
434
philipelcb327d92020-12-10 10:49:20 +0100435 InsertFrame(pid, 0, ts, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200436 for (int i = 1; i < 5; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100437 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Johannes Kron0c141c52019-08-26 15:04:43 +0200438 }
439
440 // All frames should be dropped when Clear is called.
441 EXPECT_CALL(stats_callback_, OnDroppedFrames(5)).Times(1);
442 buffer_->Clear();
443}
444
philipelbe7a9e52016-05-19 12:19:35 +0200445TEST_F(TestFrameBuffer2, InsertLateFrame) {
446 uint16_t pid = Rand();
447 uint32_t ts = Rand();
448
philipelcb327d92020-12-10 10:49:20 +0100449 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200450 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100451 InsertFrame(pid + 2, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200452 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100453 InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200454 ExtractFrame();
455
456 CheckFrame(0, pid, 0);
457 CheckFrame(1, pid + 2, 0);
458 CheckNoFrame(2);
459}
460
Niels Möller7cca0422019-04-29 16:12:19 +0200461TEST_F(TestFrameBuffer2, ProtectionModeNackFEC) {
philipel4f6cd6a2016-08-03 10:59:32 +0200462 uint16_t pid = Rand();
463 uint32_t ts = Rand();
Niels Möller7cca0422019-04-29 16:12:19 +0200464 constexpr int64_t kRttMs = 200;
465 buffer_->UpdateRtt(kRttMs);
philipel4f6cd6a2016-08-03 10:59:32 +0200466
Niels Möller7cca0422019-04-29 16:12:19 +0200467 // Jitter estimate unaffected by RTT in this protection mode.
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200468 buffer_->SetProtectionMode(kProtectionNackFEC);
Niels Möller7cca0422019-04-29 16:12:19 +0200469 InsertNackedFrame(pid, ts);
470 InsertNackedFrame(pid + 1, ts + 100);
471 InsertNackedFrame(pid + 2, ts + 200);
philipelcb327d92020-12-10 10:49:20 +0100472 InsertFrame(pid + 3, 0, ts + 300, true, kFrameSize);
philipel4f6cd6a2016-08-03 10:59:32 +0200473 ExtractFrame();
Niels Möller7cca0422019-04-29 16:12:19 +0200474 ExtractFrame();
475 ExtractFrame();
476 ExtractFrame();
477 ASSERT_EQ(4u, frames_.size());
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100478 EXPECT_LT(timing_.GetCurrentJitter().ms(), kRttMs);
Niels Möller7cca0422019-04-29 16:12:19 +0200479}
480
philipele0b2f152016-09-28 10:23:49 +0200481TEST_F(TestFrameBuffer2, NoContinuousFrame) {
482 uint16_t pid = Rand();
483 uint32_t ts = Rand();
484
philipelcb327d92020-12-10 10:49:20 +0100485 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
philipele0b2f152016-09-28 10:23:49 +0200486}
487
488TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
489 uint16_t pid = Rand();
490 uint32_t ts = Rand();
491
philipelcb327d92020-12-10 10:49:20 +0100492 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
493 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid + 1));
494 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
495 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, true, kFrameSize, pid + 3));
496 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, true, kFrameSize));
philipele0b2f152016-09-28 10:23:49 +0200497}
498
499TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
500 uint16_t pid = Rand();
501 uint32_t ts = Rand();
502
philipelcb327d92020-12-10 10:49:20 +0100503 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, kFrameSize));
504 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 1, ts, true, kFrameSize));
505 EXPECT_EQ(pid + 1,
506 InsertFrame(pid + 3, 1, ts, true, kFrameSize, pid + 1, pid + 2));
507 EXPECT_EQ(pid + 1, InsertFrame(pid + 4, 0, ts, false, kFrameSize, pid + 2));
508 EXPECT_EQ(pid + 1,
509 InsertFrame(pid + 5, 1, ts, true, kFrameSize, pid + 3, pid + 4));
510 EXPECT_EQ(pid + 1, InsertFrame(pid + 6, 0, ts, false, kFrameSize, pid + 4));
511 EXPECT_EQ(pid + 6, InsertFrame(pid + 2, 0, ts, false, kFrameSize, pid));
512 EXPECT_EQ(pid + 7,
513 InsertFrame(pid + 7, 1, ts, true, kFrameSize, pid + 5, pid + 6));
philipele0b2f152016-09-28 10:23:49 +0200514}
515
philipelfcc60062017-01-18 05:35:20 -0800516TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
517 uint16_t pid = Rand();
518 uint32_t ts = Rand();
519
philipelcb327d92020-12-10 10:49:20 +0100520 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
521 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, true, kFrameSize, pid));
philipelfcc60062017-01-18 05:35:20 -0800522 ExtractFrame();
523 CheckFrame(0, pid, 0);
524
525 // Jump back in pid but increase ts.
philipelcb327d92020-12-10 10:49:20 +0100526 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, true, kFrameSize));
philipelfcc60062017-01-18 05:35:20 -0800527 ExtractFrame();
528 ExtractFrame();
529 CheckFrame(1, pid - 1, 0);
530 CheckNoFrame(2);
531}
532
philipela45102f2017-02-22 05:30:39 -0800533TEST_F(TestFrameBuffer2, StatsCallback) {
534 uint16_t pid = Rand();
535 uint32_t ts = Rand();
536 const int kFrameSize = 5000;
537
ilnik6d5b4d62017-08-30 03:32:14 -0700538 EXPECT_CALL(stats_callback_,
539 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
Johannes Kronbfd343b2019-07-01 10:07:50 +0200540 EXPECT_CALL(stats_callback_, OnFrameBufferTimingsUpdated(_, _, _, _, _, _));
philipela45102f2017-02-22 05:30:39 -0800541
542 {
543 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Niels Möllerb9bfe652019-10-03 08:43:53 +0200544 frame->SetEncodedData(EncodedImageBuffer::Create(kFrameSize));
philipel9aa9b8d2021-02-15 13:31:29 +0100545 frame->SetId(pid);
Niels Möller23775882018-08-16 10:24:12 +0200546 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800547 frame->num_references = 0;
philipela45102f2017-02-22 05:30:39 -0800548
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200549 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800550 }
551
552 ExtractFrame();
553 CheckFrame(0, pid, 0);
554}
555
philipel146a48b2017-04-20 04:04:38 -0700556TEST_F(TestFrameBuffer2, ForwardJumps) {
philipelcb327d92020-12-10 10:49:20 +0100557 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700558 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100559 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, true, kFrameSize, 5453));
philipel146a48b2017-04-20 04:04:38 -0700560 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100561 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700562 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100563 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700564 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100565 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, true, kFrameSize, 29804));
philipel146a48b2017-04-20 04:04:38 -0700566 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100567 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, true, kFrameSize, 29805));
philipel146a48b2017-04-20 04:04:38 -0700568 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100569 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700570 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100571 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700572 ExtractFrame();
573}
574
philipelf6842692017-04-28 03:29:15 -0700575TEST_F(TestFrameBuffer2, DuplicateFrames) {
philipelcb327d92020-12-10 10:49:20 +0100576 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700577 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100578 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700579}
580
philipel112adf92017-06-15 09:06:21 -0700581// TODO(philipel): implement more unittests related to invalid references.
582TEST_F(TestFrameBuffer2, InvalidReferences) {
philipelcb327d92020-12-10 10:49:20 +0100583 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, true, kFrameSize, 2));
584 EXPECT_EQ(1, InsertFrame(1, 0, 2000, true, kFrameSize));
philipel112adf92017-06-15 09:06:21 -0700585 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100586 EXPECT_EQ(2, InsertFrame(2, 0, 3000, true, kFrameSize, 1));
philipel112adf92017-06-15 09:06:21 -0700587}
588
philipel3042c2d2017-08-18 04:55:02 -0700589TEST_F(TestFrameBuffer2, KeyframeRequired) {
philipelcb327d92020-12-10 10:49:20 +0100590 EXPECT_EQ(1, InsertFrame(1, 0, 1000, true, kFrameSize));
591 EXPECT_EQ(2, InsertFrame(2, 0, 2000, true, kFrameSize, 1));
592 EXPECT_EQ(3, InsertFrame(3, 0, 3000, true, kFrameSize));
philipel3042c2d2017-08-18 04:55:02 -0700593 ExtractFrame();
594 ExtractFrame(0, true);
595 ExtractFrame();
596
597 CheckFrame(0, 1, 0);
598 CheckFrame(1, 3, 0);
599 CheckNoFrame(2);
600}
601
philipel9771c502018-03-02 11:06:27 +0100602TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
603 const int kMaxBufferSize = 600;
604
605 for (int i = 1; i <= kMaxBufferSize; ++i)
philipelcb327d92020-12-10 10:49:20 +0100606 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, true, kFrameSize, i - 1));
philipel9771c502018-03-02 11:06:27 +0100607 ExtractFrame();
608 CheckNoFrame(0);
609
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100610 EXPECT_EQ(kMaxBufferSize + 1,
611 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
philipelcb327d92020-12-10 10:49:20 +0100612 true, kFrameSize));
philipel9771c502018-03-02 11:06:27 +0100613 ExtractFrame();
614 CheckFrame(1, kMaxBufferSize + 1, 0);
615}
616
philipel798b2822018-06-11 13:10:14 +0200617TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
philipelcb327d92020-12-10 10:49:20 +0100618 InsertFrame(1, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200619 ExtractFrame(0, true);
philipelcb327d92020-12-10 10:49:20 +0100620 InsertFrame(3, 0, 0, true, kFrameSize, 2, 0);
621 InsertFrame(3, 0, 0, true, kFrameSize, 0);
622 InsertFrame(2, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200623 ExtractFrame(0, true);
624 ExtractFrame(0, true);
625}
626
philipel6d216502018-10-22 14:36:45 +0200627TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
philipelcb327d92020-12-10 10:49:20 +0100628 InsertFrame(2, 0, 1, true, kFrameSize);
629 InsertFrame(1, 0, 2, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200630 kFrameSize); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200631 ExtractFrame(0);
632 ExtractFrame(0);
633 CheckFrame(0, 1, 0);
634 CheckNoFrame(1);
635
philipelcb327d92020-12-10 10:49:20 +0100636 InsertFrame(3, 0, 4, true, kFrameSize);
637 InsertFrame(4, 0, 3, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200638 kFrameSize); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200639 ExtractFrame(0);
640 ExtractFrame(0);
641 CheckFrame(2, 3, 0);
642 CheckNoFrame(3);
643}
644
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100645TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
646 uint16_t pid = Rand();
647 uint32_t ts = Rand();
648
philipelcb327d92020-12-10 10:49:20 +0100649 InsertFrame(pid, 0, ts, false, kFrameSize);
650 InsertFrame(pid + 1, 1, ts, true, 2 * kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100651 ExtractFrame(0);
652 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100653 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100654 CheckNoFrame(1);
655 // Two frames should be combined and returned together.
Sergey Silkin2799e632019-05-17 09:51:39 +0200656 CheckFrameSize(0, 3 * kFrameSize);
657
658 EXPECT_EQ(frames_[0]->SpatialIndex(), 1);
659 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(0), kFrameSize);
660 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(1), 2 * kFrameSize);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100661}
662
663TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
664 uint16_t pid = Rand();
665 uint32_t ts = Rand();
666
philipelcb327d92020-12-10 10:49:20 +0100667 InsertFrame(pid, 0, ts, false, kFrameSize);
668 InsertFrame(pid + 1, 1, ts, true, kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100669
670 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100671 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100672
philipelcb327d92020-12-10 10:49:20 +0100673 InsertFrame(pid + 3, 1, ts + kFps20, true, kFrameSize, pid);
674 InsertFrame(pid + 4, 0, ts + kFps10, false, kFrameSize, pid);
675 InsertFrame(pid + 5, 1, ts + kFps10, true, kFrameSize, pid + 3, pid + 4);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100676
Danil Chapovalov55284022020-02-07 14:53:52 +0100677 time_controller_.AdvanceTime(TimeDelta::Millis(1000));
philipelcb327d92020-12-10 10:49:20 +0100678 // Frame pid+3 is decodable but too late.
679 // In superframe pid+4 is decodable, but frame pid+5 is not.
680 // Incorrect implementation might skip pid+2 frame and output undecodable
681 // pid+5 instead.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100682 ExtractFrame();
683 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100684 CheckFrame(1, pid + 3, 1);
685 CheckFrame(2, pid + 4, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100686}
687
Evan Shrubsole0b565632021-11-05 09:58:20 +0100688TEST_F(TestFrameBuffer2, StopWhileWaitingForFrame) {
689 uint16_t pid = Rand();
690 uint32_t ts = Rand();
691
692 InsertFrame(pid, 0, ts, true, kFrameSize);
693 ExtractFrame(10);
694 buffer_->Stop();
695 time_controller_.AdvanceTime(TimeDelta::Millis(10));
696 EXPECT_THAT(frames_, IsEmpty());
697
698 // A new frame request should exit immediately and return no new frame.
699 ExtractFrame(0);
700 EXPECT_THAT(frames_, IsEmpty());
701}
702
philipelbe7a9e52016-05-19 12:19:35 +0200703} // namespace video_coding
704} // namespace webrtc