blob: 92d9b94144c263cea4ab025e0ac21716df60148f [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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/timing.h"
Rasmus Brandt10944e62022-05-25 10:12:42 +020023#include "modules/video_coding/timing/jitter_estimator.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"
Jonas Orelande02f9ee2022-03-25 12:43:14 +010031#include "test/scoped_key_value_config.h"
philipel19053972020-01-29 17:36:11 +010032#include "test/time_controller/simulated_time_controller.h"
philipelbe7a9e52016-05-19 12:19:35 +020033
Mirko Bonadei6a489f22019-04-09 15:11:12 +020034using ::testing::_;
Evan Shrubsole0b565632021-11-05 09:58:20 +010035using ::testing::IsEmpty;
Mirko Bonadei6a489f22019-04-09 15:11:12 +020036using ::testing::Return;
Evan Shrubsole0b565632021-11-05 09:58:20 +010037using ::testing::SizeIs;
philipela45102f2017-02-22 05:30:39 -080038
philipelbe7a9e52016-05-19 12:19:35 +020039namespace webrtc {
40namespace video_coding {
41
42class VCMTimingFake : public VCMTiming {
43 public:
Jonas Orelande62c2f22022-03-29 11:04:48 +020044 explicit VCMTimingFake(Clock* clock, const FieldTrialsView& field_trials)
Jonas Orelande02f9ee2022-03-25 12:43:14 +010045 : VCMTiming(clock, field_trials) {}
philipelbe7a9e52016-05-19 12:19:35 +020046
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010047 Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const override {
48 if (last_render_time_.IsMinusInfinity()) {
49 last_render_time_ = now + kDelay;
philipelbe7a9e52016-05-19 12:19:35 +020050 last_timestamp_ = frame_timestamp;
51 }
52
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010053 auto diff = MinDiff(frame_timestamp, last_timestamp_);
54 auto timeDiff = TimeDelta::Millis(diff / 90);
philipelbe7a9e52016-05-19 12:19:35 +020055 if (AheadOf(frame_timestamp, last_timestamp_))
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010056 last_render_time_ += timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020057 else
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010058 last_render_time_ -= timeDiff;
philipelbe7a9e52016-05-19 12:19:35 +020059
60 last_timestamp_ = frame_timestamp;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010061 return last_render_time_;
philipelbe7a9e52016-05-19 12:19:35 +020062 }
63
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010064 TimeDelta MaxWaitingTime(Timestamp render_time,
65 Timestamp now,
66 bool too_many_frames_queued) const override {
67 return render_time - now - kDecodeTime;
philipelbe7a9e52016-05-19 12:19:35 +020068 }
69
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010070 TimeDelta GetCurrentJitter() {
Evan Shrubsole92e89d72022-03-22 10:55:15 +010071 return VCMTiming::GetTimings().jitter_buffer_delay;
Niels Möller7cca0422019-04-29 16:12:19 +020072 }
73
philipelbe7a9e52016-05-19 12:19:35 +020074 private:
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010075 static constexpr TimeDelta kDelay = TimeDelta::Millis(50);
76 const TimeDelta kDecodeTime = kDelay / 2;
philipelbe7a9e52016-05-19 12:19:35 +020077 mutable uint32_t last_timestamp_ = 0;
Evan Shrubsoled6cdf802022-03-02 15:13:55 +010078 mutable Timestamp last_render_time_ = Timestamp::MinusInfinity();
philipelbe7a9e52016-05-19 12:19:35 +020079};
80
philipele7c891f2018-02-22 14:35:06 +010081class FrameObjectFake : public EncodedFrame {
philipelbe7a9e52016-05-19 12:19:35 +020082 public:
philipelb4d31082016-07-11 08:46:29 -070083 int64_t ReceivedTime() const override { return 0; }
84
85 int64_t RenderTime() const override { return _renderTimeMs; }
Niels Möller7cca0422019-04-29 16:12:19 +020086
87 bool delayed_by_retransmission() const override {
88 return delayed_by_retransmission_;
89 }
90 void set_delayed_by_retransmission(bool delayed) {
91 delayed_by_retransmission_ = delayed;
92 }
93
94 private:
95 bool delayed_by_retransmission_ = false;
philipela45102f2017-02-22 05:30:39 -080096};
97
98class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
99 public:
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200100 MOCK_METHOD(void,
101 OnCompleteFrame,
102 (bool is_keyframe,
103 size_t size_bytes,
104 VideoContentType content_type),
105 (override));
106 MOCK_METHOD(void, OnDroppedFrames, (uint32_t frames_dropped), (override));
107 MOCK_METHOD(void,
108 OnFrameBufferTimingsUpdated,
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100109 (int max_decode,
110 int current_delay,
111 int target_delay,
112 int jitter_buffer,
113 int min_playout_delay,
114 int render_delay),
Danil Chapovalovf2c0f152020-05-19 13:20:27 +0200115 (override));
116 MOCK_METHOD(void,
117 OnTimingFrameInfoUpdated,
118 (const TimingFrameInfo& info),
119 (override));
philipelbe7a9e52016-05-19 12:19:35 +0200120};
121
122class TestFrameBuffer2 : public ::testing::Test {
123 protected:
124 static constexpr int kMaxReferences = 5;
125 static constexpr int kFps1 = 1000;
126 static constexpr int kFps10 = kFps1 / 10;
127 static constexpr int kFps20 = kFps1 / 20;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100128 static constexpr size_t kFrameSize = 10;
philipelbe7a9e52016-05-19 12:19:35 +0200129
130 TestFrameBuffer2()
Evan Shrubsole9146d762021-10-13 11:19:48 +0200131 : time_controller_(Timestamp::Seconds(0)),
philipel19053972020-01-29 17:36:11 +0100132 time_task_queue_(
133 time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
134 "extract queue",
135 TaskQueueFactory::Priority::NORMAL)),
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100136 timing_(time_controller_.GetClock(), field_trials_),
philipel19053972020-01-29 17:36:11 +0100137 buffer_(new FrameBuffer(time_controller_.GetClock(),
138 &timing_,
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100139 &stats_callback_,
140 field_trials_)),
philipel19053972020-01-29 17:36:11 +0100141 rand_(0x34678213) {}
philipelbe7a9e52016-05-19 12:19:35 +0200142
143 template <typename... T>
Niels Möller7cca0422019-04-29 16:12:19 +0200144 std::unique_ptr<FrameObjectFake> CreateFrame(uint16_t picture_id,
145 uint8_t spatial_layer,
146 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200147 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200148 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200149 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200150 static_assert(sizeof...(refs) <= kMaxReferences,
philipele7c891f2018-02-22 14:35:06 +0100151 "To many references specified for EncodedFrame.");
kwiberg5b9746e2017-08-16 04:52:35 -0700152 std::array<uint16_t, sizeof...(refs)> references = {
153 {rtc::checked_cast<uint16_t>(refs)...}};
philipelbe7a9e52016-05-19 12:19:35 +0200154
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200155 auto frame = std::make_unique<FrameObjectFake>();
philipel9aa9b8d2021-02-15 13:31:29 +0100156 frame->SetId(picture_id);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100157 frame->SetSpatialIndex(spatial_layer);
Niels Möller23775882018-08-16 10:24:12 +0200158 frame->SetTimestamp(ts_ms * 90);
philipelbe7a9e52016-05-19 12:19:35 +0200159 frame->num_references = references.size();
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100160 frame->is_last_spatial_layer = last_spatial_layer;
161 // Add some data to buffer.
Niels Möllerb9bfe652019-10-03 08:43:53 +0200162 frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes));
philipelbe7a9e52016-05-19 12:19:35 +0200163 for (size_t r = 0; r < references.size(); ++r)
164 frame->references[r] = references[r];
Niels Möller7cca0422019-04-29 16:12:19 +0200165 return frame;
166 }
philipelbe7a9e52016-05-19 12:19:35 +0200167
Niels Möller7cca0422019-04-29 16:12:19 +0200168 template <typename... T>
169 int InsertFrame(uint16_t picture_id,
170 uint8_t spatial_layer,
171 int64_t ts_ms,
Niels Möller7cca0422019-04-29 16:12:19 +0200172 bool last_spatial_layer,
Sergey Silkin2799e632019-05-17 09:51:39 +0200173 size_t frame_size_bytes,
Niels Möller7cca0422019-04-29 16:12:19 +0200174 T... refs) {
philipelcb327d92020-12-10 10:49:20 +0100175 return buffer_->InsertFrame(CreateFrame(picture_id, spatial_layer, ts_ms,
176 last_spatial_layer,
177 frame_size_bytes, refs...));
Niels Möller7cca0422019-04-29 16:12:19 +0200178 }
179
180 int InsertNackedFrame(uint16_t picture_id, int64_t ts_ms) {
181 std::unique_ptr<FrameObjectFake> frame =
philipelcb327d92020-12-10 10:49:20 +0100182 CreateFrame(picture_id, 0, ts_ms, true, kFrameSize);
Niels Möller7cca0422019-04-29 16:12:19 +0200183 frame->set_delayed_by_retransmission(true);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200184 return buffer_->InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200185 }
186
philipel3042c2d2017-08-18 04:55:02 -0700187 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
philipel19053972020-01-29 17:36:11 +0100188 time_task_queue_.PostTask([this, max_wait_time, keyframe_required]() {
Evan Shrubsole3d29efd2021-12-07 14:11:45 +0100189 buffer_->NextFrame(max_wait_time, keyframe_required, &time_task_queue_,
190 [this](std::unique_ptr<EncodedFrame> frame) {
191 frames_.emplace_back(std::move(frame));
192 });
philipel19053972020-01-29 17:36:11 +0100193 });
philipelbe7a9e52016-05-19 12:19:35 +0200194 if (max_wait_time == 0) {
Danil Chapovalov55284022020-02-07 14:53:52 +0100195 time_controller_.AdvanceTime(TimeDelta::Millis(0));
philipelbe7a9e52016-05-19 12:19:35 +0200196 }
197 }
198
199 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
philipelbe7a9e52016-05-19 12:19:35 +0200200 ASSERT_LT(index, frames_.size());
201 ASSERT_TRUE(frames_[index]);
philipel9aa9b8d2021-02-15 13:31:29 +0100202 ASSERT_EQ(picture_id, frames_[index]->Id());
philipelcb327d92020-12-10 10:49:20 +0100203 ASSERT_EQ(spatial_layer, frames_[index]->SpatialIndex().value_or(0));
philipelbe7a9e52016-05-19 12:19:35 +0200204 }
205
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100206 void CheckFrameSize(size_t index, size_t size) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100207 ASSERT_LT(index, frames_.size());
208 ASSERT_TRUE(frames_[index]);
209 ASSERT_EQ(frames_[index]->size(), size);
210 }
211
philipelbe7a9e52016-05-19 12:19:35 +0200212 void CheckNoFrame(size_t index) {
philipelbe7a9e52016-05-19 12:19:35 +0200213 ASSERT_LT(index, frames_.size());
214 ASSERT_FALSE(frames_[index]);
215 }
216
philipelbe7a9e52016-05-19 12:19:35 +0200217 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
218
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100219 test::ScopedKeyValueConfig field_trials_;
philipel19053972020-01-29 17:36:11 +0100220 webrtc::GlobalSimulatedTimeController time_controller_;
221 rtc::TaskQueue time_task_queue_;
philipelbe7a9e52016-05-19 12:19:35 +0200222 VCMTimingFake timing_;
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200223 std::unique_ptr<FrameBuffer> buffer_;
philipele7c891f2018-02-22 14:35:06 +0100224 std::vector<std::unique_ptr<EncodedFrame>> frames_;
philipelbe7a9e52016-05-19 12:19:35 +0200225 Random rand_;
philipela45102f2017-02-22 05:30:39 -0800226 ::testing::NiceMock<VCMReceiveStatisticsCallbackMock> stats_callback_;
philipelbe7a9e52016-05-19 12:19:35 +0200227};
228
Sergey Silkin2799e632019-05-17 09:51:39 +0200229// From https://en.cppreference.com/w/cpp/language/static: "If ... a constexpr
230// static data member (since C++11) is odr-used, a definition at namespace scope
231// is still required... This definition is deprecated for constexpr data members
232// since C++17."
233// kFrameSize is odr-used since it is passed by reference to EXPECT_EQ().
234#if __cplusplus < 201703L
235constexpr size_t TestFrameBuffer2::kFrameSize;
236#endif
237
philipelbe7a9e52016-05-19 12:19:35 +0200238TEST_F(TestFrameBuffer2, WaitForFrame) {
239 uint16_t pid = Rand();
240 uint32_t ts = Rand();
241
philipel6e8224f2016-05-19 17:07:44 +0200242 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100243 InsertFrame(pid, 0, ts, true, kFrameSize);
Danil Chapovalov55284022020-02-07 14:53:52 +0100244 time_controller_.AdvanceTime(TimeDelta::Millis(50));
philipelbe7a9e52016-05-19 12:19:35 +0200245 CheckFrame(0, pid, 0);
246}
247
Evan Shrubsole0b565632021-11-05 09:58:20 +0100248TEST_F(TestFrameBuffer2, ClearWhileWaitingForFrame) {
249 const uint16_t pid = Rand();
250
251 // Insert a frame and wait for it for max 100ms.
252 InsertFrame(pid, 0, 25, true, kFrameSize);
253 ExtractFrame(100);
254 // After 10ms, clear the buffer.
255 time_controller_.AdvanceTime(TimeDelta::Millis(10));
256 buffer_->Clear();
257 // Confirm that the frame was not sent for rendering.
258 time_controller_.AdvanceTime(TimeDelta::Millis(15));
259 EXPECT_THAT(frames_, IsEmpty());
260
261 // We are still waiting for a frame, since 100ms has not passed. Insert a new
262 // frame. This new frame should be the one that is returned as the old frame
263 // was cleared.
264 const uint16_t new_pid = pid + 1;
265 InsertFrame(new_pid, 0, 50, true, kFrameSize);
266 time_controller_.AdvanceTime(TimeDelta::Millis(25));
267 ASSERT_THAT(frames_, SizeIs(1));
268 CheckFrame(0, new_pid, 0);
269}
270
philipelbe7a9e52016-05-19 12:19:35 +0200271TEST_F(TestFrameBuffer2, OneSuperFrame) {
272 uint16_t pid = Rand();
273 uint32_t ts = Rand();
274
philipelcb327d92020-12-10 10:49:20 +0100275 InsertFrame(pid, 0, ts, false, kFrameSize);
276 InsertFrame(pid + 1, 1, ts, true, kFrameSize);
philipele0b2f152016-09-28 10:23:49 +0200277 ExtractFrame();
278
Sergey Silkin61832dd2018-12-20 14:32:14 +0100279 CheckFrame(0, pid, 1);
philipele0b2f152016-09-28 10:23:49 +0200280}
281
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200282TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
Jonas Orelande02f9ee2022-03-25 12:43:14 +0100283 test::ScopedKeyValueConfig field_trials;
284 VCMTiming timing(time_controller_.GetClock(), field_trials);
285 buffer_.reset(new FrameBuffer(time_controller_.GetClock(), &timing,
286 &stats_callback_, field_trials));
Niels Möllerd381eed2020-09-02 15:34:40 +0200287 const VideoPlayoutDelay kPlayoutDelayMs = {0, 0};
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200288 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
philipel9aa9b8d2021-02-15 13:31:29 +0100289 test_frame->SetId(0);
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200290 test_frame->SetPlayoutDelay(kPlayoutDelayMs);
291 buffer_->InsertFrame(std::move(test_frame));
292 ExtractFrame(0, false);
293 CheckFrame(0, 0, 0);
294 EXPECT_EQ(0, frames_[0]->RenderTimeMs());
295}
296
aleloi3e005282017-01-26 05:38:00 -0800297// Flaky test, see bugs.webrtc.org/7068.
298TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
philipele0b2f152016-09-28 10:23:49 +0200299 uint16_t pid = Rand();
300 uint32_t ts = Rand();
301
philipel6e8224f2016-05-19 17:07:44 +0200302 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100303 InsertFrame(pid, 1, ts, true, kFrameSize);
304 InsertFrame(pid, 0, ts, false, kFrameSize);
Danil Chapovalov55284022020-02-07 14:53:52 +0100305 time_controller_.AdvanceTime(TimeDelta::Millis(0));
philipelbe7a9e52016-05-19 12:19:35 +0200306
307 CheckFrame(0, pid, 0);
308 CheckFrame(1, pid, 1);
309}
310
philipel94616b32016-05-20 10:43:01 +0200311TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200312 uint16_t pid = Rand();
313 uint32_t ts = Rand();
314
Sergey Silkin2799e632019-05-17 09:51:39 +0200315 InsertFrame(pid, 0, ts, false, true, kFrameSize);
philipel6e8224f2016-05-19 17:07:44 +0200316 ExtractFrame();
317 CheckFrame(0, pid, 0);
318 for (int i = 1; i < 10; i += 2) {
319 ExtractFrame(50);
philipelcb327d92020-12-10 10:49:20 +0100320 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, true, kFrameSize,
Sergey Silkin2799e632019-05-17 09:51:39 +0200321 pid + i);
Danil Chapovalov55284022020-02-07 14:53:52 +0100322 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelcb327d92020-12-10 10:49:20 +0100323 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Danil Chapovalov55284022020-02-07 14:53:52 +0100324 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipel6e8224f2016-05-19 17:07:44 +0200325 ExtractFrame();
326 CheckFrame(i, pid + i, 0);
327 CheckFrame(i + 1, pid + i + 1, 0);
328 }
329}
philipel6e8224f2016-05-19 17:07:44 +0200330
331TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
332 ExtractFrame();
333 CheckNoFrame(0);
334}
335
philipel93e451b2016-10-06 12:25:13 +0200336TEST_F(TestFrameBuffer2, MissingFrame) {
337 uint16_t pid = Rand();
338 uint32_t ts = Rand();
339
philipelcb327d92020-12-10 10:49:20 +0100340 InsertFrame(pid, 0, ts, true, kFrameSize);
341 InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid);
342 InsertFrame(pid + 3, 0, ts, true, kFrameSize, pid + 1, pid + 2);
philipel93e451b2016-10-06 12:25:13 +0200343 ExtractFrame();
344 ExtractFrame();
345 ExtractFrame();
346
347 CheckFrame(0, pid, 0);
348 CheckFrame(1, pid + 2, 0);
349 CheckNoFrame(2);
350}
351
philipelbe7a9e52016-05-19 12:19:35 +0200352TEST_F(TestFrameBuffer2, OneLayerStream) {
353 uint16_t pid = Rand();
354 uint32_t ts = Rand();
355
philipelcb327d92020-12-10 10:49:20 +0100356 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200357 ExtractFrame();
358 CheckFrame(0, pid, 0);
359 for (int i = 1; i < 10; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100360 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200361 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100362 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
philipelbe7a9e52016-05-19 12:19:35 +0200363 CheckFrame(i, pid + i, 0);
364 }
365}
366
philipelbe7a9e52016-05-19 12:19:35 +0200367TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
368 uint16_t pid = Rand();
369 uint32_t ts = Rand();
370
philipelcb327d92020-12-10 10:49:20 +0100371 InsertFrame(pid, 0, ts, true, kFrameSize);
372 InsertFrame(pid + 1, 0, ts + kFps20, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200373 for (int i = 2; i < 10; i += 2) {
374 uint32_t ts_tl0 = ts + i / 2 * kFps10;
philipelcb327d92020-12-10 10:49:20 +0100375 InsertFrame(pid + i, 0, ts_tl0, true, kFrameSize, pid + i - 2);
376 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, true, kFrameSize, pid + i,
377 pid + i - 1);
philipelbe7a9e52016-05-19 12:19:35 +0200378 }
379
Johannes Kron0c141c52019-08-26 15:04:43 +0200380 EXPECT_CALL(stats_callback_, OnDroppedFrames(1)).Times(3);
381
philipelbe7a9e52016-05-19 12:19:35 +0200382 for (int i = 0; i < 10; ++i) {
383 ExtractFrame();
Danil Chapovalov55284022020-02-07 14:53:52 +0100384 time_controller_.AdvanceTime(TimeDelta::Millis(70));
philipelbe7a9e52016-05-19 12:19:35 +0200385 }
386
387 CheckFrame(0, pid, 0);
388 CheckFrame(1, pid + 1, 0);
389 CheckFrame(2, pid + 2, 0);
390 CheckFrame(3, pid + 4, 0);
391 CheckFrame(4, pid + 6, 0);
392 CheckFrame(5, pid + 8, 0);
393 CheckNoFrame(6);
394 CheckNoFrame(7);
395 CheckNoFrame(8);
396 CheckNoFrame(9);
397}
398
Johannes Kron0c141c52019-08-26 15:04:43 +0200399TEST_F(TestFrameBuffer2, DropFramesIfSystemIsStalled) {
400 uint16_t pid = Rand();
401 uint32_t ts = Rand();
402
philipelcb327d92020-12-10 10:49:20 +0100403 InsertFrame(pid, 0, ts, true, kFrameSize);
404 InsertFrame(pid + 1, 0, ts + 1 * kFps10, true, kFrameSize, pid);
405 InsertFrame(pid + 2, 0, ts + 2 * kFps10, true, kFrameSize, pid + 1);
406 InsertFrame(pid + 3, 0, ts + 3 * kFps10, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200407
408 ExtractFrame();
409 // Jump forward in time, simulating the system being stalled for some reason.
Danil Chapovalov55284022020-02-07 14:53:52 +0100410 time_controller_.AdvanceTime(TimeDelta::Millis(3) * kFps10);
Johannes Kron0c141c52019-08-26 15:04:43 +0200411 // Extract one more frame, expect second and third frame to be dropped.
412 EXPECT_CALL(stats_callback_, OnDroppedFrames(2)).Times(1);
413 ExtractFrame();
414
415 CheckFrame(0, pid + 0, 0);
416 CheckFrame(1, pid + 3, 0);
417}
418
419TEST_F(TestFrameBuffer2, DroppedFramesCountedOnClear) {
420 uint16_t pid = Rand();
421 uint32_t ts = Rand();
422
philipelcb327d92020-12-10 10:49:20 +0100423 InsertFrame(pid, 0, ts, true, kFrameSize);
Johannes Kron0c141c52019-08-26 15:04:43 +0200424 for (int i = 1; i < 5; ++i) {
philipelcb327d92020-12-10 10:49:20 +0100425 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
Johannes Kron0c141c52019-08-26 15:04:43 +0200426 }
427
428 // All frames should be dropped when Clear is called.
429 EXPECT_CALL(stats_callback_, OnDroppedFrames(5)).Times(1);
430 buffer_->Clear();
431}
432
philipelbe7a9e52016-05-19 12:19:35 +0200433TEST_F(TestFrameBuffer2, InsertLateFrame) {
434 uint16_t pid = Rand();
435 uint32_t ts = Rand();
436
philipelcb327d92020-12-10 10:49:20 +0100437 InsertFrame(pid, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200438 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100439 InsertFrame(pid + 2, 0, ts, true, kFrameSize);
philipelbe7a9e52016-05-19 12:19:35 +0200440 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100441 InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid);
philipelbe7a9e52016-05-19 12:19:35 +0200442 ExtractFrame();
443
444 CheckFrame(0, pid, 0);
445 CheckFrame(1, pid + 2, 0);
446 CheckNoFrame(2);
447}
448
Niels Möller7cca0422019-04-29 16:12:19 +0200449TEST_F(TestFrameBuffer2, ProtectionModeNackFEC) {
philipel4f6cd6a2016-08-03 10:59:32 +0200450 uint16_t pid = Rand();
451 uint32_t ts = Rand();
Niels Möller7cca0422019-04-29 16:12:19 +0200452 constexpr int64_t kRttMs = 200;
453 buffer_->UpdateRtt(kRttMs);
philipel4f6cd6a2016-08-03 10:59:32 +0200454
Niels Möller7cca0422019-04-29 16:12:19 +0200455 // Jitter estimate unaffected by RTT in this protection mode.
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200456 buffer_->SetProtectionMode(kProtectionNackFEC);
Niels Möller7cca0422019-04-29 16:12:19 +0200457 InsertNackedFrame(pid, ts);
458 InsertNackedFrame(pid + 1, ts + 100);
459 InsertNackedFrame(pid + 2, ts + 200);
philipelcb327d92020-12-10 10:49:20 +0100460 InsertFrame(pid + 3, 0, ts + 300, true, kFrameSize);
philipel4f6cd6a2016-08-03 10:59:32 +0200461 ExtractFrame();
Niels Möller7cca0422019-04-29 16:12:19 +0200462 ExtractFrame();
463 ExtractFrame();
464 ExtractFrame();
465 ASSERT_EQ(4u, frames_.size());
Evan Shrubsoled6cdf802022-03-02 15:13:55 +0100466 EXPECT_LT(timing_.GetCurrentJitter().ms(), kRttMs);
Niels Möller7cca0422019-04-29 16:12:19 +0200467}
468
philipele0b2f152016-09-28 10:23:49 +0200469TEST_F(TestFrameBuffer2, NoContinuousFrame) {
470 uint16_t pid = Rand();
471 uint32_t ts = Rand();
472
philipelcb327d92020-12-10 10:49:20 +0100473 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
philipele0b2f152016-09-28 10:23:49 +0200474}
475
476TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
477 uint16_t pid = Rand();
478 uint32_t ts = Rand();
479
philipelcb327d92020-12-10 10:49:20 +0100480 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
481 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid + 1));
482 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
483 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, true, kFrameSize, pid + 3));
484 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, true, kFrameSize));
philipele0b2f152016-09-28 10:23:49 +0200485}
486
487TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
488 uint16_t pid = Rand();
489 uint32_t ts = Rand();
490
philipelcb327d92020-12-10 10:49:20 +0100491 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, kFrameSize));
492 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 1, ts, true, kFrameSize));
493 EXPECT_EQ(pid + 1,
494 InsertFrame(pid + 3, 1, ts, true, kFrameSize, pid + 1, pid + 2));
495 EXPECT_EQ(pid + 1, InsertFrame(pid + 4, 0, ts, false, kFrameSize, pid + 2));
496 EXPECT_EQ(pid + 1,
497 InsertFrame(pid + 5, 1, ts, true, kFrameSize, pid + 3, pid + 4));
498 EXPECT_EQ(pid + 1, InsertFrame(pid + 6, 0, ts, false, kFrameSize, pid + 4));
499 EXPECT_EQ(pid + 6, InsertFrame(pid + 2, 0, ts, false, kFrameSize, pid));
500 EXPECT_EQ(pid + 7,
501 InsertFrame(pid + 7, 1, ts, true, kFrameSize, pid + 5, pid + 6));
philipele0b2f152016-09-28 10:23:49 +0200502}
503
philipelfcc60062017-01-18 05:35:20 -0800504TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
505 uint16_t pid = Rand();
506 uint32_t ts = Rand();
507
philipelcb327d92020-12-10 10:49:20 +0100508 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
509 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, true, kFrameSize, pid));
philipelfcc60062017-01-18 05:35:20 -0800510 ExtractFrame();
511 CheckFrame(0, pid, 0);
512
513 // Jump back in pid but increase ts.
philipelcb327d92020-12-10 10:49:20 +0100514 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, true, kFrameSize));
philipelfcc60062017-01-18 05:35:20 -0800515 ExtractFrame();
516 ExtractFrame();
517 CheckFrame(1, pid - 1, 0);
518 CheckNoFrame(2);
519}
520
philipela45102f2017-02-22 05:30:39 -0800521TEST_F(TestFrameBuffer2, StatsCallback) {
522 uint16_t pid = Rand();
523 uint32_t ts = Rand();
524 const int kFrameSize = 5000;
525
ilnik6d5b4d62017-08-30 03:32:14 -0700526 EXPECT_CALL(stats_callback_,
527 OnCompleteFrame(true, kFrameSize, VideoContentType::UNSPECIFIED));
Johannes Kronbfd343b2019-07-01 10:07:50 +0200528 EXPECT_CALL(stats_callback_, OnFrameBufferTimingsUpdated(_, _, _, _, _, _));
Evan Shrubsole92e89d72022-03-22 10:55:15 +0100529 // Stats callback requires a previously decoded frame.
530 timing_.StopDecodeTimer(TimeDelta::Millis(1), Timestamp::Zero());
philipela45102f2017-02-22 05:30:39 -0800531
532 {
533 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
Niels Möllerb9bfe652019-10-03 08:43:53 +0200534 frame->SetEncodedData(EncodedImageBuffer::Create(kFrameSize));
philipel9aa9b8d2021-02-15 13:31:29 +0100535 frame->SetId(pid);
Niels Möller23775882018-08-16 10:24:12 +0200536 frame->SetTimestamp(ts);
philipela45102f2017-02-22 05:30:39 -0800537 frame->num_references = 0;
philipela45102f2017-02-22 05:30:39 -0800538
Stefan Holmer812ceaf2018-05-15 13:00:10 +0200539 EXPECT_EQ(buffer_->InsertFrame(std::move(frame)), pid);
philipela45102f2017-02-22 05:30:39 -0800540 }
541
542 ExtractFrame();
543 CheckFrame(0, pid, 0);
544}
545
philipel146a48b2017-04-20 04:04:38 -0700546TEST_F(TestFrameBuffer2, ForwardJumps) {
philipelcb327d92020-12-10 10:49:20 +0100547 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700548 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100549 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, true, kFrameSize, 5453));
philipel146a48b2017-04-20 04:04:38 -0700550 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100551 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700552 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100553 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700554 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100555 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, true, kFrameSize, 29804));
philipel146a48b2017-04-20 04:04:38 -0700556 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100557 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, true, kFrameSize, 29805));
philipel146a48b2017-04-20 04:04:38 -0700558 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100559 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700560 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100561 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, true, kFrameSize));
philipel146a48b2017-04-20 04:04:38 -0700562 ExtractFrame();
563}
564
philipelf6842692017-04-28 03:29:15 -0700565TEST_F(TestFrameBuffer2, DuplicateFrames) {
philipelcb327d92020-12-10 10:49:20 +0100566 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700567 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100568 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
philipelf6842692017-04-28 03:29:15 -0700569}
570
philipel112adf92017-06-15 09:06:21 -0700571// TODO(philipel): implement more unittests related to invalid references.
572TEST_F(TestFrameBuffer2, InvalidReferences) {
philipelcb327d92020-12-10 10:49:20 +0100573 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, true, kFrameSize, 2));
574 EXPECT_EQ(1, InsertFrame(1, 0, 2000, true, kFrameSize));
philipel112adf92017-06-15 09:06:21 -0700575 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100576 EXPECT_EQ(2, InsertFrame(2, 0, 3000, true, kFrameSize, 1));
philipel112adf92017-06-15 09:06:21 -0700577}
578
philipel3042c2d2017-08-18 04:55:02 -0700579TEST_F(TestFrameBuffer2, KeyframeRequired) {
philipelcb327d92020-12-10 10:49:20 +0100580 EXPECT_EQ(1, InsertFrame(1, 0, 1000, true, kFrameSize));
581 EXPECT_EQ(2, InsertFrame(2, 0, 2000, true, kFrameSize, 1));
582 EXPECT_EQ(3, InsertFrame(3, 0, 3000, true, kFrameSize));
philipel3042c2d2017-08-18 04:55:02 -0700583 ExtractFrame();
584 ExtractFrame(0, true);
585 ExtractFrame();
586
587 CheckFrame(0, 1, 0);
588 CheckFrame(1, 3, 0);
589 CheckNoFrame(2);
590}
591
philipel9771c502018-03-02 11:06:27 +0100592TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
593 const int kMaxBufferSize = 600;
594
595 for (int i = 1; i <= kMaxBufferSize; ++i)
philipelcb327d92020-12-10 10:49:20 +0100596 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, true, kFrameSize, i - 1));
philipel9771c502018-03-02 11:06:27 +0100597 ExtractFrame();
598 CheckNoFrame(0);
599
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100600 EXPECT_EQ(kMaxBufferSize + 1,
601 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
philipelcb327d92020-12-10 10:49:20 +0100602 true, kFrameSize));
philipel9771c502018-03-02 11:06:27 +0100603 ExtractFrame();
604 CheckFrame(1, kMaxBufferSize + 1, 0);
605}
606
philipel798b2822018-06-11 13:10:14 +0200607TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
philipelcb327d92020-12-10 10:49:20 +0100608 InsertFrame(1, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200609 ExtractFrame(0, true);
philipelcb327d92020-12-10 10:49:20 +0100610 InsertFrame(3, 0, 0, true, kFrameSize, 2, 0);
611 InsertFrame(3, 0, 0, true, kFrameSize, 0);
612 InsertFrame(2, 0, 0, true, kFrameSize);
philipel798b2822018-06-11 13:10:14 +0200613 ExtractFrame(0, true);
614 ExtractFrame(0, true);
615}
616
philipel6d216502018-10-22 14:36:45 +0200617TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
philipelcb327d92020-12-10 10:49:20 +0100618 InsertFrame(2, 0, 1, true, kFrameSize);
619 InsertFrame(1, 0, 2, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200620 kFrameSize); // Older picture id but newer timestamp.
philipel6d216502018-10-22 14:36:45 +0200621 ExtractFrame(0);
622 ExtractFrame(0);
623 CheckFrame(0, 1, 0);
624 CheckNoFrame(1);
625
philipelcb327d92020-12-10 10:49:20 +0100626 InsertFrame(3, 0, 4, true, kFrameSize);
627 InsertFrame(4, 0, 3, true,
Sergey Silkin2799e632019-05-17 09:51:39 +0200628 kFrameSize); // Newer picture id but older timestamp.
philipel6d216502018-10-22 14:36:45 +0200629 ExtractFrame(0);
630 ExtractFrame(0);
631 CheckFrame(2, 3, 0);
632 CheckNoFrame(3);
633}
634
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100635TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
636 uint16_t pid = Rand();
637 uint32_t ts = Rand();
638
philipelcb327d92020-12-10 10:49:20 +0100639 InsertFrame(pid, 0, ts, false, kFrameSize);
640 InsertFrame(pid + 1, 1, ts, true, 2 * kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100641 ExtractFrame(0);
642 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100643 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100644 CheckNoFrame(1);
645 // Two frames should be combined and returned together.
Sergey Silkin2799e632019-05-17 09:51:39 +0200646 CheckFrameSize(0, 3 * kFrameSize);
647
648 EXPECT_EQ(frames_[0]->SpatialIndex(), 1);
649 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(0), kFrameSize);
650 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(1), 2 * kFrameSize);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100651}
652
653TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
654 uint16_t pid = Rand();
655 uint32_t ts = Rand();
656
philipelcb327d92020-12-10 10:49:20 +0100657 InsertFrame(pid, 0, ts, false, kFrameSize);
658 InsertFrame(pid + 1, 1, ts, true, kFrameSize, pid);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100659
660 ExtractFrame(0);
Sergey Silkin61832dd2018-12-20 14:32:14 +0100661 CheckFrame(0, pid, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100662
philipelcb327d92020-12-10 10:49:20 +0100663 InsertFrame(pid + 3, 1, ts + kFps20, true, kFrameSize, pid);
664 InsertFrame(pid + 4, 0, ts + kFps10, false, kFrameSize, pid);
665 InsertFrame(pid + 5, 1, ts + kFps10, true, kFrameSize, pid + 3, pid + 4);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100666
Danil Chapovalov55284022020-02-07 14:53:52 +0100667 time_controller_.AdvanceTime(TimeDelta::Millis(1000));
philipelcb327d92020-12-10 10:49:20 +0100668 // Frame pid+3 is decodable but too late.
669 // In superframe pid+4 is decodable, but frame pid+5 is not.
670 // Incorrect implementation might skip pid+2 frame and output undecodable
671 // pid+5 instead.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100672 ExtractFrame();
673 ExtractFrame();
philipelcb327d92020-12-10 10:49:20 +0100674 CheckFrame(1, pid + 3, 1);
675 CheckFrame(2, pid + 4, 1);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100676}
677
Evan Shrubsole0b565632021-11-05 09:58:20 +0100678TEST_F(TestFrameBuffer2, StopWhileWaitingForFrame) {
679 uint16_t pid = Rand();
680 uint32_t ts = Rand();
681
682 InsertFrame(pid, 0, ts, true, kFrameSize);
683 ExtractFrame(10);
684 buffer_->Stop();
685 time_controller_.AdvanceTime(TimeDelta::Millis(10));
686 EXPECT_THAT(frames_, IsEmpty());
687
688 // A new frame request should exit immediately and return no new frame.
689 ExtractFrame(0);
690 EXPECT_THAT(frames_, IsEmpty());
691}
692
philipelbe7a9e52016-05-19 12:19:35 +0200693} // namespace video_coding
694} // namespace webrtc