blob: 1397aebb32120ae818d8c40fd7ac9d98ce513d0c [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
11#include "webrtc/modules/video_coding/frame_buffer2.h"
12
13#include <algorithm>
14#include <cstring>
15#include <limits>
16#include <vector>
17
kwiberg77eab702016-09-28 17:42:01 -070018#include "webrtc/test/gmock.h"
19#include "webrtc/test/gtest.h"
philipelbe7a9e52016-05-19 12:19:35 +020020#include "webrtc/base/platform_thread.h"
21#include "webrtc/base/random.h"
22#include "webrtc/modules/video_coding/frame_object.h"
23#include "webrtc/modules/video_coding/jitter_estimator.h"
24#include "webrtc/modules/video_coding/sequence_number_util.h"
25#include "webrtc/modules/video_coding/timing.h"
26#include "webrtc/system_wrappers/include/clock.h"
27
28namespace webrtc {
29namespace video_coding {
30
31class VCMTimingFake : public VCMTiming {
32 public:
33 explicit VCMTimingFake(Clock* clock) : VCMTiming(clock) {}
34
35 int64_t RenderTimeMs(uint32_t frame_timestamp,
36 int64_t now_ms) const override {
37 if (last_ms_ == -1) {
38 last_ms_ = now_ms + kDelayMs;
39 last_timestamp_ = frame_timestamp;
40 }
41
42 uint32_t diff = MinDiff(frame_timestamp, last_timestamp_);
43 if (AheadOf(frame_timestamp, last_timestamp_))
44 last_ms_ += diff / 90;
45 else
46 last_ms_ -= diff / 90;
47
48 last_timestamp_ = frame_timestamp;
49 return last_ms_;
50 }
51
52 uint32_t MaxWaitingTime(int64_t render_time_ms,
53 int64_t now_ms) const override {
54 return std::max<int>(0, render_time_ms - now_ms - kDecodeTime);
55 }
56
57 private:
58 static constexpr int kDelayMs = 50;
59 static constexpr int kDecodeTime = kDelayMs / 2;
60 mutable uint32_t last_timestamp_ = 0;
61 mutable int64_t last_ms_ = -1;
62};
63
64class VCMJitterEstimatorMock : public VCMJitterEstimator {
65 public:
66 explicit VCMJitterEstimatorMock(Clock* clock) : VCMJitterEstimator(clock) {}
67
68 MOCK_METHOD1(UpdateRtt, void(int64_t rttMs));
69 MOCK_METHOD3(UpdateEstimate,
70 void(int64_t frameDelayMs,
71 uint32_t frameSizeBytes,
72 bool incompleteFrame));
philipel4f6cd6a2016-08-03 10:59:32 +020073 MOCK_METHOD1(GetJitterEstimate, int(double rttMultiplier));
philipelbe7a9e52016-05-19 12:19:35 +020074};
75
philipelb4d31082016-07-11 08:46:29 -070076class FrameObjectFake : public FrameObject {
philipelbe7a9e52016-05-19 12:19:35 +020077 public:
philipelb4d31082016-07-11 08:46:29 -070078 bool GetBitstream(uint8_t* destination) const override { return true; }
79
80 uint32_t Timestamp() const override { return timestamp; }
81
82 int64_t ReceivedTime() const override { return 0; }
83
84 int64_t RenderTime() const override { return _renderTimeMs; }
philipelbe7a9e52016-05-19 12:19:35 +020085};
86
87class TestFrameBuffer2 : public ::testing::Test {
88 protected:
89 static constexpr int kMaxReferences = 5;
90 static constexpr int kFps1 = 1000;
91 static constexpr int kFps10 = kFps1 / 10;
92 static constexpr int kFps20 = kFps1 / 20;
93
94 TestFrameBuffer2()
95 : clock_(0),
96 timing_(&clock_),
97 jitter_estimator_(&clock_),
98 buffer_(&clock_, &jitter_estimator_, &timing_),
99 rand_(0x34678213),
100 tear_down_(false),
101 extract_thread_(&ExtractLoop, this, "Extract Thread"),
102 trigger_extract_event_(false, false),
103 crit_acquired_event_(false, false) {}
104
105 void SetUp() override { extract_thread_.Start(); }
106
107 void TearDown() override {
108 tear_down_ = true;
109 trigger_extract_event_.Set();
110 extract_thread_.Stop();
111 }
112
113 template <typename... T>
philipele0b2f152016-09-28 10:23:49 +0200114 int InsertFrame(uint16_t picture_id,
115 uint8_t spatial_layer,
116 int64_t ts_ms,
117 bool inter_layer_predicted,
118 T... refs) {
philipelbe7a9e52016-05-19 12:19:35 +0200119 static_assert(sizeof...(refs) <= kMaxReferences,
120 "To many references specified for FrameObject.");
121 std::array<uint16_t, sizeof...(refs)> references = {{refs...}};
122
philipelb4d31082016-07-11 08:46:29 -0700123 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipelbe7a9e52016-05-19 12:19:35 +0200124 frame->picture_id = picture_id;
125 frame->spatial_layer = spatial_layer;
126 frame->timestamp = ts_ms * 90;
127 frame->num_references = references.size();
128 frame->inter_layer_predicted = inter_layer_predicted;
129 for (size_t r = 0; r < references.size(); ++r)
130 frame->references[r] = references[r];
131
philipele0b2f152016-09-28 10:23:49 +0200132 return buffer_.InsertFrame(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200133 }
134
135 void ExtractFrame(int64_t max_wait_time = 0) {
136 crit_.Enter();
137 if (max_wait_time == 0) {
philipel75562822016-09-05 10:57:41 +0200138 std::unique_ptr<FrameObject> frame;
139 FrameBuffer::ReturnReason res = buffer_.NextFrame(0, &frame);
140 if (res != FrameBuffer::ReturnReason::kStopped)
141 frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200142 crit_.Leave();
143 } else {
144 max_wait_time_ = max_wait_time;
145 trigger_extract_event_.Set();
146 crit_.Leave();
147 // Make sure |crit_| is aquired by |extract_thread_| before returning.
148 crit_acquired_event_.Wait(rtc::Event::kForever);
149 }
150 }
151
152 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
153 rtc::CritScope lock(&crit_);
154 ASSERT_LT(index, frames_.size());
155 ASSERT_TRUE(frames_[index]);
156 ASSERT_EQ(picture_id, frames_[index]->picture_id);
157 ASSERT_EQ(spatial_layer, frames_[index]->spatial_layer);
158 }
159
160 void CheckNoFrame(size_t index) {
161 rtc::CritScope lock(&crit_);
162 ASSERT_LT(index, frames_.size());
163 ASSERT_FALSE(frames_[index]);
164 }
165
166 static bool ExtractLoop(void* obj) {
167 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
168 while (true) {
169 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
170 {
171 rtc::CritScope lock(&tfb->crit_);
172 tfb->crit_acquired_event_.Set();
173 if (tfb->tear_down_)
174 return false;
175
philipel75562822016-09-05 10:57:41 +0200176 std::unique_ptr<FrameObject> frame;
177 FrameBuffer::ReturnReason res =
178 tfb->buffer_.NextFrame(tfb->max_wait_time_, &frame);
179 if (res != FrameBuffer::ReturnReason::kStopped)
180 tfb->frames_.emplace_back(std::move(frame));
philipelbe7a9e52016-05-19 12:19:35 +0200181 }
182 }
183 }
184
185 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
186
187 SimulatedClock clock_;
188 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200189 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
philipelbe7a9e52016-05-19 12:19:35 +0200190 FrameBuffer buffer_;
191 std::vector<std::unique_ptr<FrameObject>> frames_;
192 Random rand_;
193
194 int64_t max_wait_time_;
195 bool tear_down_;
196 rtc::PlatformThread extract_thread_;
197 rtc::Event trigger_extract_event_;
198 rtc::Event crit_acquired_event_;
199 rtc::CriticalSection crit_;
200};
201
philipel6e8224f2016-05-19 17:07:44 +0200202// Following tests are timing dependent. Either the timeouts have to
203// be increased by a large margin, which would slow down all trybots,
204// or we disable them for the very slow ones, like we do here.
205#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200206TEST_F(TestFrameBuffer2, WaitForFrame) {
207 uint16_t pid = Rand();
208 uint32_t ts = Rand();
209
philipel6e8224f2016-05-19 17:07:44 +0200210 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200211 InsertFrame(pid, 0, ts, false);
212 CheckFrame(0, pid, 0);
213}
214
215TEST_F(TestFrameBuffer2, OneSuperFrame) {
216 uint16_t pid = Rand();
217 uint32_t ts = Rand();
218
philipele0b2f152016-09-28 10:23:49 +0200219 InsertFrame(pid, 0, ts, false);
220 ExtractFrame();
221 InsertFrame(pid, 1, ts, true);
222 ExtractFrame();
223
224 CheckFrame(0, pid, 0);
225 CheckFrame(1, pid, 1);
226}
227
228TEST_F(TestFrameBuffer2, OneUnorderedSuperFrame) {
229 uint16_t pid = Rand();
230 uint32_t ts = Rand();
231
philipel6e8224f2016-05-19 17:07:44 +0200232 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200233 InsertFrame(pid, 1, ts, true);
234 InsertFrame(pid, 0, ts, false);
235 ExtractFrame();
236
237 CheckFrame(0, pid, 0);
238 CheckFrame(1, pid, 1);
239}
240
philipel94616b32016-05-20 10:43:01 +0200241TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200242 uint16_t pid = Rand();
243 uint32_t ts = Rand();
244
245 InsertFrame(pid, 0, ts, false);
246 ExtractFrame();
247 CheckFrame(0, pid, 0);
248 for (int i = 1; i < 10; i += 2) {
249 ExtractFrame(50);
250 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
251 clock_.AdvanceTimeMilliseconds(kFps10);
252 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
253 clock_.AdvanceTimeMilliseconds(kFps10);
254 ExtractFrame();
255 CheckFrame(i, pid + i, 0);
256 CheckFrame(i + 1, pid + i + 1, 0);
257 }
258}
259#endif // Timing dependent tests.
260
261TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
262 ExtractFrame();
263 CheckNoFrame(0);
264}
265
philipelbe7a9e52016-05-19 12:19:35 +0200266TEST_F(TestFrameBuffer2, OneLayerStream) {
267 uint16_t pid = Rand();
268 uint32_t ts = Rand();
269
270 InsertFrame(pid, 0, ts, false);
271 ExtractFrame();
272 CheckFrame(0, pid, 0);
273 for (int i = 1; i < 10; ++i) {
274 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
275 ExtractFrame();
276 clock_.AdvanceTimeMilliseconds(kFps10);
277 CheckFrame(i, pid + i, 0);
278 }
279}
280
philipelbe7a9e52016-05-19 12:19:35 +0200281TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
282 uint16_t pid = Rand();
283 uint32_t ts = Rand();
284
285 InsertFrame(pid, 0, ts, false);
286 InsertFrame(pid + 1, 0, ts + kFps20, false);
287 for (int i = 2; i < 10; i += 2) {
288 uint32_t ts_tl0 = ts + i / 2 * kFps10;
289 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
290 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
291 }
292
293 for (int i = 0; i < 10; ++i) {
294 ExtractFrame();
295 clock_.AdvanceTimeMilliseconds(60);
296 }
297
298 CheckFrame(0, pid, 0);
299 CheckFrame(1, pid + 1, 0);
300 CheckFrame(2, pid + 2, 0);
301 CheckFrame(3, pid + 4, 0);
302 CheckFrame(4, pid + 6, 0);
303 CheckFrame(5, pid + 8, 0);
304 CheckNoFrame(6);
305 CheckNoFrame(7);
306 CheckNoFrame(8);
307 CheckNoFrame(9);
308}
309
310TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
311 uint16_t pid = Rand();
312 uint32_t ts = Rand();
313
314 InsertFrame(pid, 0, ts, false);
315 InsertFrame(pid, 1, ts, false);
316 for (int i = 1; i < 6; ++i) {
317 uint32_t ts_tl0 = ts + i * kFps10;
318 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
319 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
320 }
321
322 ExtractFrame();
323 ExtractFrame();
324 clock_.AdvanceTimeMilliseconds(55);
325 for (int i = 2; i < 12; ++i) {
326 ExtractFrame();
327 clock_.AdvanceTimeMilliseconds(55);
328 }
329
330 CheckFrame(0, pid, 0);
331 CheckFrame(1, pid, 1);
332 CheckFrame(2, pid + 1, 0);
333 CheckFrame(3, pid + 1, 1);
334 CheckFrame(4, pid + 2, 0);
335 CheckFrame(5, pid + 2, 1);
336 CheckFrame(6, pid + 3, 0);
337 CheckFrame(7, pid + 4, 0);
338 CheckFrame(8, pid + 5, 0);
339 CheckNoFrame(9);
340 CheckNoFrame(10);
341 CheckNoFrame(11);
342}
343
344TEST_F(TestFrameBuffer2, InsertLateFrame) {
345 uint16_t pid = Rand();
346 uint32_t ts = Rand();
347
348 InsertFrame(pid, 0, ts, false);
349 ExtractFrame();
350 InsertFrame(pid + 2, 0, ts, false);
351 ExtractFrame();
352 InsertFrame(pid + 1, 0, ts, false, pid);
353 ExtractFrame();
354
355 CheckFrame(0, pid, 0);
356 CheckFrame(1, pid + 2, 0);
357 CheckNoFrame(2);
358}
359
philipel4f6cd6a2016-08-03 10:59:32 +0200360TEST_F(TestFrameBuffer2, ProtectionMode) {
361 uint16_t pid = Rand();
362 uint32_t ts = Rand();
363
364 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
365 InsertFrame(pid, 0, ts, false);
366 ExtractFrame();
367
368 buffer_.SetProtectionMode(kProtectionNackFEC);
369 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
370 InsertFrame(pid + 1, 0, ts, false);
371 ExtractFrame();
372}
373
philipele0b2f152016-09-28 10:23:49 +0200374TEST_F(TestFrameBuffer2, NoContinuousFrame) {
375 uint16_t pid = Rand();
376 uint32_t ts = Rand();
377
378 EXPECT_EQ(-1, InsertFrame(pid + 1, 0, ts, false, pid));
379}
380
381TEST_F(TestFrameBuffer2, LastContinuousFrameSingleLayer) {
382 uint16_t pid = Rand();
383 uint32_t ts = Rand();
384
385 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
386 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
387 EXPECT_EQ(pid + 2, InsertFrame(pid + 1, 0, ts, false, pid));
388 EXPECT_EQ(pid + 2, InsertFrame(pid + 4, 0, ts, false, pid + 3));
389 EXPECT_EQ(pid + 5, InsertFrame(pid + 5, 0, ts, false));
390}
391
392TEST_F(TestFrameBuffer2, LastContinuousFrameTwoLayers) {
393 uint16_t pid = Rand();
394 uint32_t ts = Rand();
395
396 EXPECT_EQ(pid, InsertFrame(pid, 0, ts, false));
397 EXPECT_EQ(pid, InsertFrame(pid, 1, ts, true));
398 EXPECT_EQ(pid, InsertFrame(pid + 1, 1, ts, true, pid));
399 EXPECT_EQ(pid, InsertFrame(pid + 2, 0, ts, false, pid + 1));
400 EXPECT_EQ(pid, InsertFrame(pid + 2, 1, ts, true, pid + 1));
401 EXPECT_EQ(pid, InsertFrame(pid + 3, 0, ts, false, pid + 2));
402 EXPECT_EQ(pid + 3, InsertFrame(pid + 1, 0, ts, false, pid));
403 EXPECT_EQ(pid + 3, InsertFrame(pid + 3, 1, ts, true, pid + 2));
404}
405
philipelbe7a9e52016-05-19 12:19:35 +0200406} // namespace video_coding
407} // namespace webrtc