1 /*
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
11 #include "modules/video_coding/frame_buffer2.h"
12
13 #include <algorithm>
14 #include <cstring>
15 #include <limits>
16 #include <memory>
17 #include <vector>
18
19 #include "api/task_queue/task_queue_base.h"
20 #include "api/units/time_delta.h"
21 #include "api/units/timestamp.h"
22 #include "modules/video_coding/frame_object.h"
23 #include "modules/video_coding/timing/jitter_estimator.h"
24 #include "modules/video_coding/timing/timing.h"
25 #include "rtc_base/numerics/sequence_number_util.h"
26 #include "rtc_base/platform_thread.h"
27 #include "rtc_base/random.h"
28 #include "system_wrappers/include/clock.h"
29 #include "test/field_trial.h"
30 #include "test/gmock.h"
31 #include "test/gtest.h"
32 #include "test/scoped_key_value_config.h"
33 #include "test/time_controller/simulated_time_controller.h"
34
35 using ::testing::_;
36 using ::testing::IsEmpty;
37 using ::testing::Return;
38 using ::testing::SizeIs;
39
40 namespace webrtc {
41 namespace video_coding {
42
43 class VCMTimingFake : public VCMTiming {
44 public:
VCMTimingFake(Clock * clock,const FieldTrialsView & field_trials)45 explicit VCMTimingFake(Clock* clock, const FieldTrialsView& field_trials)
46 : VCMTiming(clock, field_trials) {}
47
RenderTime(uint32_t frame_timestamp,Timestamp now) const48 Timestamp RenderTime(uint32_t frame_timestamp, Timestamp now) const override {
49 if (last_render_time_.IsMinusInfinity()) {
50 last_render_time_ = now + kDelay;
51 last_timestamp_ = frame_timestamp;
52 }
53
54 auto diff = MinDiff(frame_timestamp, last_timestamp_);
55 auto timeDiff = TimeDelta::Millis(diff / 90);
56 if (AheadOf(frame_timestamp, last_timestamp_))
57 last_render_time_ += timeDiff;
58 else
59 last_render_time_ -= timeDiff;
60
61 last_timestamp_ = frame_timestamp;
62 return last_render_time_;
63 }
64
MaxWaitingTime(Timestamp render_time,Timestamp now,bool too_many_frames_queued) const65 TimeDelta MaxWaitingTime(Timestamp render_time,
66 Timestamp now,
67 bool too_many_frames_queued) const override {
68 return render_time - now - kDecodeTime;
69 }
70
GetCurrentJitter()71 TimeDelta GetCurrentJitter() {
72 return VCMTiming::GetTimings().jitter_buffer_delay;
73 }
74
75 private:
76 static constexpr TimeDelta kDelay = TimeDelta::Millis(50);
77 const TimeDelta kDecodeTime = kDelay / 2;
78 mutable uint32_t last_timestamp_ = 0;
79 mutable Timestamp last_render_time_ = Timestamp::MinusInfinity();
80 };
81
82 class FrameObjectFake : public EncodedFrame {
83 public:
ReceivedTime() const84 int64_t ReceivedTime() const override { return 0; }
85
RenderTime() const86 int64_t RenderTime() const override { return _renderTimeMs; }
87
delayed_by_retransmission() const88 bool delayed_by_retransmission() const override {
89 return delayed_by_retransmission_;
90 }
set_delayed_by_retransmission(bool delayed)91 void set_delayed_by_retransmission(bool delayed) {
92 delayed_by_retransmission_ = delayed;
93 }
94
95 private:
96 bool delayed_by_retransmission_ = false;
97 };
98
99 class VCMReceiveStatisticsCallbackMock : public VCMReceiveStatisticsCallback {
100 public:
101 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,
110 (int max_decode,
111 int current_delay,
112 int target_delay,
113 int jitter_buffer,
114 int min_playout_delay,
115 int render_delay),
116 (override));
117 MOCK_METHOD(void,
118 OnTimingFrameInfoUpdated,
119 (const TimingFrameInfo& info),
120 (override));
121 };
122
123 class 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;
129 static constexpr size_t kFrameSize = 10;
130
TestFrameBuffer2()131 TestFrameBuffer2()
132 : time_controller_(Timestamp::Seconds(0)),
133 time_task_queue_(
134 time_controller_.GetTaskQueueFactory()->CreateTaskQueue(
135 "extract queue",
136 TaskQueueFactory::Priority::NORMAL)),
137 timing_(time_controller_.GetClock(), field_trials_),
138 buffer_(new FrameBuffer(time_controller_.GetClock(),
139 &timing_,
140 field_trials_)),
141 rand_(0x34678213) {}
142
143 template <typename... T>
CreateFrame(uint16_t picture_id,uint8_t spatial_layer,int64_t ts_ms,bool last_spatial_layer,size_t frame_size_bytes,T...refs)144 std::unique_ptr<FrameObjectFake> CreateFrame(uint16_t picture_id,
145 uint8_t spatial_layer,
146 int64_t ts_ms,
147 bool last_spatial_layer,
148 size_t frame_size_bytes,
149 T... refs) {
150 static_assert(sizeof...(refs) <= kMaxReferences,
151 "To many references specified for EncodedFrame.");
152 std::array<uint16_t, sizeof...(refs)> references = {
153 {rtc::checked_cast<uint16_t>(refs)...}};
154
155 auto frame = std::make_unique<FrameObjectFake>();
156 frame->SetId(picture_id);
157 frame->SetSpatialIndex(spatial_layer);
158 frame->SetTimestamp(ts_ms * 90);
159 frame->num_references = references.size();
160 frame->is_last_spatial_layer = last_spatial_layer;
161 // Add some data to buffer.
162 frame->SetEncodedData(EncodedImageBuffer::Create(frame_size_bytes));
163 for (size_t r = 0; r < references.size(); ++r)
164 frame->references[r] = references[r];
165 return frame;
166 }
167
168 template <typename... T>
InsertFrame(uint16_t picture_id,uint8_t spatial_layer,int64_t ts_ms,bool last_spatial_layer,size_t frame_size_bytes,T...refs)169 int InsertFrame(uint16_t picture_id,
170 uint8_t spatial_layer,
171 int64_t ts_ms,
172 bool last_spatial_layer,
173 size_t frame_size_bytes,
174 T... refs) {
175 return buffer_->InsertFrame(CreateFrame(picture_id, spatial_layer, ts_ms,
176 last_spatial_layer,
177 frame_size_bytes, refs...));
178 }
179
InsertNackedFrame(uint16_t picture_id,int64_t ts_ms)180 int InsertNackedFrame(uint16_t picture_id, int64_t ts_ms) {
181 std::unique_ptr<FrameObjectFake> frame =
182 CreateFrame(picture_id, 0, ts_ms, true, kFrameSize);
183 frame->set_delayed_by_retransmission(true);
184 return buffer_->InsertFrame(std::move(frame));
185 }
186
ExtractFrame(int64_t max_wait_time=0,bool keyframe_required=false)187 void ExtractFrame(int64_t max_wait_time = 0, bool keyframe_required = false) {
188 time_task_queue_->PostTask([this, max_wait_time, keyframe_required]() {
189 buffer_->NextFrame(max_wait_time, keyframe_required,
190 time_task_queue_.get(),
191 [this](std::unique_ptr<EncodedFrame> frame) {
192 frames_.emplace_back(std::move(frame));
193 });
194 });
195 if (max_wait_time == 0) {
196 time_controller_.AdvanceTime(TimeDelta::Zero());
197 }
198 }
199
CheckFrame(size_t index,int picture_id,int spatial_layer)200 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
201 ASSERT_LT(index, frames_.size());
202 ASSERT_TRUE(frames_[index]);
203 ASSERT_EQ(picture_id, frames_[index]->Id());
204 ASSERT_EQ(spatial_layer, frames_[index]->SpatialIndex().value_or(0));
205 }
206
CheckFrameSize(size_t index,size_t size)207 void CheckFrameSize(size_t index, size_t size) {
208 ASSERT_LT(index, frames_.size());
209 ASSERT_TRUE(frames_[index]);
210 ASSERT_EQ(frames_[index]->size(), size);
211 }
212
CheckNoFrame(size_t index)213 void CheckNoFrame(size_t index) {
214 ASSERT_LT(index, frames_.size());
215 ASSERT_FALSE(frames_[index]);
216 }
217
Rand()218 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
219
220 test::ScopedKeyValueConfig field_trials_;
221 webrtc::GlobalSimulatedTimeController time_controller_;
222 std::unique_ptr<TaskQueueBase, TaskQueueDeleter> time_task_queue_;
223 VCMTimingFake timing_;
224 std::unique_ptr<FrameBuffer> buffer_;
225 std::vector<std::unique_ptr<EncodedFrame>> frames_;
226 Random rand_;
227 };
228
229 // 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
235 constexpr size_t TestFrameBuffer2::kFrameSize;
236 #endif
237
TEST_F(TestFrameBuffer2,WaitForFrame)238 TEST_F(TestFrameBuffer2, WaitForFrame) {
239 uint16_t pid = Rand();
240 uint32_t ts = Rand();
241
242 ExtractFrame(50);
243 InsertFrame(pid, 0, ts, true, kFrameSize);
244 time_controller_.AdvanceTime(TimeDelta::Millis(50));
245 CheckFrame(0, pid, 0);
246 }
247
TEST_F(TestFrameBuffer2,ClearWhileWaitingForFrame)248 TEST_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
TEST_F(TestFrameBuffer2,OneSuperFrame)271 TEST_F(TestFrameBuffer2, OneSuperFrame) {
272 uint16_t pid = Rand();
273 uint32_t ts = Rand();
274
275 InsertFrame(pid, 0, ts, false, kFrameSize);
276 InsertFrame(pid + 1, 1, ts, true, kFrameSize);
277 ExtractFrame();
278
279 CheckFrame(0, pid, 1);
280 }
281
TEST_F(TestFrameBuffer2,ZeroPlayoutDelay)282 TEST_F(TestFrameBuffer2, ZeroPlayoutDelay) {
283 test::ScopedKeyValueConfig field_trials;
284 VCMTiming timing(time_controller_.GetClock(), field_trials);
285 buffer_ = std::make_unique<FrameBuffer>(time_controller_.GetClock(), &timing,
286 field_trials);
287 const VideoPlayoutDelay kPlayoutDelayMs = {0, 0};
288 std::unique_ptr<FrameObjectFake> test_frame(new FrameObjectFake());
289 test_frame->SetId(0);
290 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
297 // Flaky test, see bugs.webrtc.org/7068.
TEST_F(TestFrameBuffer2,DISABLED_OneUnorderedSuperFrame)298 TEST_F(TestFrameBuffer2, DISABLED_OneUnorderedSuperFrame) {
299 uint16_t pid = Rand();
300 uint32_t ts = Rand();
301
302 ExtractFrame(50);
303 InsertFrame(pid, 1, ts, true, kFrameSize);
304 InsertFrame(pid, 0, ts, false, kFrameSize);
305 time_controller_.AdvanceTime(TimeDelta::Zero());
306
307 CheckFrame(0, pid, 0);
308 CheckFrame(1, pid, 1);
309 }
310
TEST_F(TestFrameBuffer2,DISABLED_OneLayerStreamReordered)311 TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
312 uint16_t pid = Rand();
313 uint32_t ts = Rand();
314
315 InsertFrame(pid, 0, ts, false, true, kFrameSize);
316 ExtractFrame();
317 CheckFrame(0, pid, 0);
318 for (int i = 1; i < 10; i += 2) {
319 ExtractFrame(50);
320 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, true, kFrameSize,
321 pid + i);
322 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
323 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
324 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
325 ExtractFrame();
326 CheckFrame(i, pid + i, 0);
327 CheckFrame(i + 1, pid + i + 1, 0);
328 }
329 }
330
TEST_F(TestFrameBuffer2,ExtractFromEmptyBuffer)331 TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
332 ExtractFrame();
333 CheckNoFrame(0);
334 }
335
TEST_F(TestFrameBuffer2,MissingFrame)336 TEST_F(TestFrameBuffer2, MissingFrame) {
337 uint16_t pid = Rand();
338 uint32_t ts = Rand();
339
340 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);
343 ExtractFrame();
344 ExtractFrame();
345 ExtractFrame();
346
347 CheckFrame(0, pid, 0);
348 CheckFrame(1, pid + 2, 0);
349 CheckNoFrame(2);
350 }
351
TEST_F(TestFrameBuffer2,OneLayerStream)352 TEST_F(TestFrameBuffer2, OneLayerStream) {
353 uint16_t pid = Rand();
354 uint32_t ts = Rand();
355
356 InsertFrame(pid, 0, ts, true, kFrameSize);
357 ExtractFrame();
358 CheckFrame(0, pid, 0);
359 for (int i = 1; i < 10; ++i) {
360 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
361 ExtractFrame();
362 time_controller_.AdvanceTime(TimeDelta::Millis(kFps10));
363 CheckFrame(i, pid + i, 0);
364 }
365 }
366
TEST_F(TestFrameBuffer2,DropTemporalLayerSlowDecoder)367 TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
368 uint16_t pid = Rand();
369 uint32_t ts = Rand();
370
371 InsertFrame(pid, 0, ts, true, kFrameSize);
372 InsertFrame(pid + 1, 0, ts + kFps20, true, kFrameSize, pid);
373 for (int i = 2; i < 10; i += 2) {
374 uint32_t ts_tl0 = ts + i / 2 * kFps10;
375 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);
378 }
379
380 for (int i = 0; i < 10; ++i) {
381 ExtractFrame();
382 time_controller_.AdvanceTime(TimeDelta::Millis(70));
383 }
384
385 CheckFrame(0, pid, 0);
386 CheckFrame(1, pid + 1, 0);
387 CheckFrame(2, pid + 2, 0);
388 CheckFrame(3, pid + 4, 0);
389 CheckFrame(4, pid + 6, 0);
390 CheckFrame(5, pid + 8, 0);
391 CheckNoFrame(6);
392 CheckNoFrame(7);
393 CheckNoFrame(8);
394 CheckNoFrame(9);
395 }
396
TEST_F(TestFrameBuffer2,DropFramesIfSystemIsStalled)397 TEST_F(TestFrameBuffer2, DropFramesIfSystemIsStalled) {
398 uint16_t pid = Rand();
399 uint32_t ts = Rand();
400
401 InsertFrame(pid, 0, ts, true, kFrameSize);
402 InsertFrame(pid + 1, 0, ts + 1 * kFps10, true, kFrameSize, pid);
403 InsertFrame(pid + 2, 0, ts + 2 * kFps10, true, kFrameSize, pid + 1);
404 InsertFrame(pid + 3, 0, ts + 3 * kFps10, true, kFrameSize);
405
406 ExtractFrame();
407 // Jump forward in time, simulating the system being stalled for some reason.
408 time_controller_.AdvanceTime(TimeDelta::Millis(3) * kFps10);
409 // Extract one more frame, expect second and third frame to be dropped.
410 ExtractFrame();
411
412 CheckFrame(0, pid + 0, 0);
413 CheckFrame(1, pid + 3, 0);
414 }
415
TEST_F(TestFrameBuffer2,DroppedFramesCountedOnClear)416 TEST_F(TestFrameBuffer2, DroppedFramesCountedOnClear) {
417 uint16_t pid = Rand();
418 uint32_t ts = Rand();
419
420 InsertFrame(pid, 0, ts, true, kFrameSize);
421 for (int i = 1; i < 5; ++i) {
422 InsertFrame(pid + i, 0, ts + i * kFps10, true, kFrameSize, pid + i - 1);
423 }
424
425 // All frames should be dropped when Clear is called.
426 buffer_->Clear();
427 }
428
TEST_F(TestFrameBuffer2,InsertLateFrame)429 TEST_F(TestFrameBuffer2, InsertLateFrame) {
430 uint16_t pid = Rand();
431 uint32_t ts = Rand();
432
433 InsertFrame(pid, 0, ts, true, kFrameSize);
434 ExtractFrame();
435 InsertFrame(pid + 2, 0, ts, true, kFrameSize);
436 ExtractFrame();
437 InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid);
438 ExtractFrame();
439
440 CheckFrame(0, pid, 0);
441 CheckFrame(1, pid + 2, 0);
442 CheckNoFrame(2);
443 }
444
TEST_F(TestFrameBuffer2,ProtectionModeNackFEC)445 TEST_F(TestFrameBuffer2, ProtectionModeNackFEC) {
446 uint16_t pid = Rand();
447 uint32_t ts = Rand();
448 constexpr int64_t kRttMs = 200;
449 buffer_->UpdateRtt(kRttMs);
450
451 // Jitter estimate unaffected by RTT in this protection mode.
452 buffer_->SetProtectionMode(kProtectionNackFEC);
453 InsertNackedFrame(pid, ts);
454 InsertNackedFrame(pid + 1, ts + 100);
455 InsertNackedFrame(pid + 2, ts + 200);
456 InsertFrame(pid + 3, 0, ts + 300, true, kFrameSize);
457 ExtractFrame();
458 ExtractFrame();
459 ExtractFrame();
460 ExtractFrame();
461 ASSERT_EQ(4u, frames_.size());
462 EXPECT_LT(timing_.GetCurrentJitter().ms(), kRttMs);
463 }
464
TEST_F(TestFrameBuffer2,NoContinuousFrame)465 TEST_F(TestFrameBuffer2, NoContinuousFrame) {
466 uint16_t pid = Rand();
467 uint32_t ts = Rand();
468
469 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
470 }
471
TEST_F(TestFrameBuffer2,LastContinuousFrameSingleLayer)472 TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
473 uint16_t pid = Rand();
474 uint32_t ts = Rand();
475
476 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
477 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, true, kFrameSize, pid + 1));
478 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, true, kFrameSize, pid));
479 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, true, kFrameSize, pid + 3));
480 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, true, kFrameSize));
481 }
482
TEST_F(TestFrameBuffer2,LastContinuousFrameTwoLayers)483 TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
484 uint16_t pid = Rand();
485 uint32_t ts = Rand();
486
487 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false, kFrameSize));
488 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 1, ts, true, kFrameSize));
489 EXPECT_EQ(pid + 1,
490 InsertFrame(pid + 3, 1, ts, true, kFrameSize, pid + 1, pid + 2));
491 EXPECT_EQ(pid + 1, InsertFrame(pid + 4, 0, ts, false, kFrameSize, pid + 2));
492 EXPECT_EQ(pid + 1,
493 InsertFrame(pid + 5, 1, ts, true, kFrameSize, pid + 3, pid + 4));
494 EXPECT_EQ(pid + 1, InsertFrame(pid + 6, 0, ts, false, kFrameSize, pid + 4));
495 EXPECT_EQ(pid + 6, InsertFrame(pid + 2, 0, ts, false, kFrameSize, pid));
496 EXPECT_EQ(pid + 7,
497 InsertFrame(pid + 7, 1, ts, true, kFrameSize, pid + 5, pid + 6));
498 }
499
TEST_F(TestFrameBuffer2,PictureIdJumpBack)500 TEST_F(TestFrameBuffer2, PictureIdJumpBack) {
501 uint16_t pid = Rand();
502 uint32_t ts = Rand();
503
504 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, true, kFrameSize));
505 EXPECT_EQ(pid + 1, InsertFrame(pid + 1, 0, ts + 1, true, kFrameSize, pid));
506 ExtractFrame();
507 CheckFrame(0, pid, 0);
508
509 // Jump back in pid but increase ts.
510 EXPECT_EQ(pid - 1, InsertFrame(pid - 1, 0, ts + 2, true, kFrameSize));
511 ExtractFrame();
512 ExtractFrame();
513 CheckFrame(1, pid - 1, 0);
514 CheckNoFrame(2);
515 }
516
TEST_F(TestFrameBuffer2,ForwardJumps)517 TEST_F(TestFrameBuffer2, ForwardJumps) {
518 EXPECT_EQ(5453, InsertFrame(5453, 0, 1, true, kFrameSize));
519 ExtractFrame();
520 EXPECT_EQ(5454, InsertFrame(5454, 0, 1, true, kFrameSize, 5453));
521 ExtractFrame();
522 EXPECT_EQ(15670, InsertFrame(15670, 0, 1, true, kFrameSize));
523 ExtractFrame();
524 EXPECT_EQ(29804, InsertFrame(29804, 0, 1, true, kFrameSize));
525 ExtractFrame();
526 EXPECT_EQ(29805, InsertFrame(29805, 0, 1, true, kFrameSize, 29804));
527 ExtractFrame();
528 EXPECT_EQ(29806, InsertFrame(29806, 0, 1, true, kFrameSize, 29805));
529 ExtractFrame();
530 EXPECT_EQ(33819, InsertFrame(33819, 0, 1, true, kFrameSize));
531 ExtractFrame();
532 EXPECT_EQ(41248, InsertFrame(41248, 0, 1, true, kFrameSize));
533 ExtractFrame();
534 }
535
TEST_F(TestFrameBuffer2,DuplicateFrames)536 TEST_F(TestFrameBuffer2, DuplicateFrames) {
537 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
538 ExtractFrame();
539 EXPECT_EQ(22256, InsertFrame(22256, 0, 1, true, kFrameSize));
540 }
541
542 // TODO(philipel): implement more unittests related to invalid references.
TEST_F(TestFrameBuffer2,InvalidReferences)543 TEST_F(TestFrameBuffer2, InvalidReferences) {
544 EXPECT_EQ(-1, InsertFrame(0, 0, 1000, true, kFrameSize, 2));
545 EXPECT_EQ(1, InsertFrame(1, 0, 2000, true, kFrameSize));
546 ExtractFrame();
547 EXPECT_EQ(2, InsertFrame(2, 0, 3000, true, kFrameSize, 1));
548 }
549
TEST_F(TestFrameBuffer2,KeyframeRequired)550 TEST_F(TestFrameBuffer2, KeyframeRequired) {
551 EXPECT_EQ(1, InsertFrame(1, 0, 1000, true, kFrameSize));
552 EXPECT_EQ(2, InsertFrame(2, 0, 2000, true, kFrameSize, 1));
553 EXPECT_EQ(3, InsertFrame(3, 0, 3000, true, kFrameSize));
554 ExtractFrame();
555 ExtractFrame(0, true);
556 ExtractFrame();
557
558 CheckFrame(0, 1, 0);
559 CheckFrame(1, 3, 0);
560 CheckNoFrame(2);
561 }
562
TEST_F(TestFrameBuffer2,KeyframeClearsFullBuffer)563 TEST_F(TestFrameBuffer2, KeyframeClearsFullBuffer) {
564 const int kMaxBufferSize = 600;
565
566 for (int i = 1; i <= kMaxBufferSize; ++i)
567 EXPECT_EQ(-1, InsertFrame(i, 0, i * 1000, true, kFrameSize, i - 1));
568 ExtractFrame();
569 CheckNoFrame(0);
570
571 EXPECT_EQ(kMaxBufferSize + 1,
572 InsertFrame(kMaxBufferSize + 1, 0, (kMaxBufferSize + 1) * 1000,
573 true, kFrameSize));
574 ExtractFrame();
575 CheckFrame(1, kMaxBufferSize + 1, 0);
576 }
577
TEST_F(TestFrameBuffer2,DontUpdateOnUndecodableFrame)578 TEST_F(TestFrameBuffer2, DontUpdateOnUndecodableFrame) {
579 InsertFrame(1, 0, 0, true, kFrameSize);
580 ExtractFrame(0, true);
581 InsertFrame(3, 0, 0, true, kFrameSize, 2, 0);
582 InsertFrame(3, 0, 0, true, kFrameSize, 0);
583 InsertFrame(2, 0, 0, true, kFrameSize);
584 ExtractFrame(0, true);
585 ExtractFrame(0, true);
586 }
587
TEST_F(TestFrameBuffer2,DontDecodeOlderTimestamp)588 TEST_F(TestFrameBuffer2, DontDecodeOlderTimestamp) {
589 InsertFrame(2, 0, 1, true, kFrameSize);
590 InsertFrame(1, 0, 2, true,
591 kFrameSize); // Older picture id but newer timestamp.
592 ExtractFrame(0);
593 ExtractFrame(0);
594 CheckFrame(0, 1, 0);
595 CheckNoFrame(1);
596
597 InsertFrame(3, 0, 4, true, kFrameSize);
598 InsertFrame(4, 0, 3, true,
599 kFrameSize); // Newer picture id but older timestamp.
600 ExtractFrame(0);
601 ExtractFrame(0);
602 CheckFrame(2, 3, 0);
603 CheckNoFrame(3);
604 }
605
TEST_F(TestFrameBuffer2,CombineFramesToSuperframe)606 TEST_F(TestFrameBuffer2, CombineFramesToSuperframe) {
607 uint16_t pid = Rand();
608 uint32_t ts = Rand();
609
610 InsertFrame(pid, 0, ts, false, kFrameSize);
611 InsertFrame(pid + 1, 1, ts, true, 2 * kFrameSize, pid);
612 ExtractFrame(0);
613 ExtractFrame(0);
614 CheckFrame(0, pid, 1);
615 CheckNoFrame(1);
616 // Two frames should be combined and returned together.
617 CheckFrameSize(0, 3 * kFrameSize);
618
619 EXPECT_EQ(frames_[0]->SpatialIndex(), 1);
620 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(0), kFrameSize);
621 EXPECT_EQ(frames_[0]->SpatialLayerFrameSize(1), 2 * kFrameSize);
622 }
623
TEST_F(TestFrameBuffer2,HigherSpatialLayerNonDecodable)624 TEST_F(TestFrameBuffer2, HigherSpatialLayerNonDecodable) {
625 uint16_t pid = Rand();
626 uint32_t ts = Rand();
627
628 InsertFrame(pid, 0, ts, false, kFrameSize);
629 InsertFrame(pid + 1, 1, ts, true, kFrameSize, pid);
630
631 ExtractFrame(0);
632 CheckFrame(0, pid, 1);
633
634 InsertFrame(pid + 3, 1, ts + kFps20, true, kFrameSize, pid);
635 InsertFrame(pid + 4, 0, ts + kFps10, false, kFrameSize, pid);
636 InsertFrame(pid + 5, 1, ts + kFps10, true, kFrameSize, pid + 3, pid + 4);
637
638 time_controller_.AdvanceTime(TimeDelta::Millis(1000));
639 // Frame pid+3 is decodable but too late.
640 // In superframe pid+4 is decodable, but frame pid+5 is not.
641 // Incorrect implementation might skip pid+2 frame and output undecodable
642 // pid+5 instead.
643 ExtractFrame();
644 ExtractFrame();
645 CheckFrame(1, pid + 3, 1);
646 CheckFrame(2, pid + 4, 1);
647 }
648
TEST_F(TestFrameBuffer2,StopWhileWaitingForFrame)649 TEST_F(TestFrameBuffer2, StopWhileWaitingForFrame) {
650 uint16_t pid = Rand();
651 uint32_t ts = Rand();
652
653 InsertFrame(pid, 0, ts, true, kFrameSize);
654 ExtractFrame(10);
655 buffer_->Stop();
656 time_controller_.AdvanceTime(TimeDelta::Millis(10));
657 EXPECT_THAT(frames_, IsEmpty());
658
659 // A new frame request should exit immediately and return no new frame.
660 ExtractFrame(0);
661 EXPECT_THAT(frames_, IsEmpty());
662 }
663
664 } // namespace video_coding
665 } // namespace webrtc
666