blob: 674ae29bfc9c8945da69224bdadef33e0cc5e7c3 [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
18#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
20#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>
114 void InsertFrame(uint16_t picture_id,
115 uint8_t spatial_layer,
116 int64_t ts_ms,
117 bool inter_layer_predicted,
118 T... refs) {
119 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
132 buffer_.InsertFrame(std::move(frame));
133 }
134
135 void ExtractFrame(int64_t max_wait_time = 0) {
136 crit_.Enter();
137 if (max_wait_time == 0) {
138 frames_.emplace_back(buffer_.NextFrame(0));
139 crit_.Leave();
140 } else {
141 max_wait_time_ = max_wait_time;
142 trigger_extract_event_.Set();
143 crit_.Leave();
144 // Make sure |crit_| is aquired by |extract_thread_| before returning.
145 crit_acquired_event_.Wait(rtc::Event::kForever);
146 }
147 }
148
149 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
150 rtc::CritScope lock(&crit_);
151 ASSERT_LT(index, frames_.size());
152 ASSERT_TRUE(frames_[index]);
153 ASSERT_EQ(picture_id, frames_[index]->picture_id);
154 ASSERT_EQ(spatial_layer, frames_[index]->spatial_layer);
155 }
156
157 void CheckNoFrame(size_t index) {
158 rtc::CritScope lock(&crit_);
159 ASSERT_LT(index, frames_.size());
160 ASSERT_FALSE(frames_[index]);
161 }
162
163 static bool ExtractLoop(void* obj) {
164 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
165 while (true) {
166 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
167 {
168 rtc::CritScope lock(&tfb->crit_);
169 tfb->crit_acquired_event_.Set();
170 if (tfb->tear_down_)
171 return false;
172
173 tfb->frames_.emplace_back(tfb->buffer_.NextFrame(tfb->max_wait_time_));
174 }
175 }
176 }
177
178 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
179
180 SimulatedClock clock_;
181 VCMTimingFake timing_;
philipel4f6cd6a2016-08-03 10:59:32 +0200182 ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_;
philipelbe7a9e52016-05-19 12:19:35 +0200183 FrameBuffer buffer_;
184 std::vector<std::unique_ptr<FrameObject>> frames_;
185 Random rand_;
186
187 int64_t max_wait_time_;
188 bool tear_down_;
189 rtc::PlatformThread extract_thread_;
190 rtc::Event trigger_extract_event_;
191 rtc::Event crit_acquired_event_;
192 rtc::CriticalSection crit_;
193};
194
philipel6e8224f2016-05-19 17:07:44 +0200195// Following tests are timing dependent. Either the timeouts have to
196// be increased by a large margin, which would slow down all trybots,
197// or we disable them for the very slow ones, like we do here.
198#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200199TEST_F(TestFrameBuffer2, WaitForFrame) {
200 uint16_t pid = Rand();
201 uint32_t ts = Rand();
202
philipel6e8224f2016-05-19 17:07:44 +0200203 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200204 InsertFrame(pid, 0, ts, false);
205 CheckFrame(0, pid, 0);
206}
207
208TEST_F(TestFrameBuffer2, OneSuperFrame) {
209 uint16_t pid = Rand();
210 uint32_t ts = Rand();
211
philipel6e8224f2016-05-19 17:07:44 +0200212 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200213 InsertFrame(pid, 1, ts, true);
214 InsertFrame(pid, 0, ts, false);
215 ExtractFrame();
216
217 CheckFrame(0, pid, 0);
218 CheckFrame(1, pid, 1);
219}
220
philipel94616b32016-05-20 10:43:01 +0200221TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200222 uint16_t pid = Rand();
223 uint32_t ts = Rand();
224
225 InsertFrame(pid, 0, ts, false);
226 ExtractFrame();
227 CheckFrame(0, pid, 0);
228 for (int i = 1; i < 10; i += 2) {
229 ExtractFrame(50);
230 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
231 clock_.AdvanceTimeMilliseconds(kFps10);
232 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
233 clock_.AdvanceTimeMilliseconds(kFps10);
234 ExtractFrame();
235 CheckFrame(i, pid + i, 0);
236 CheckFrame(i + 1, pid + i + 1, 0);
237 }
238}
239#endif // Timing dependent tests.
240
241TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
242 ExtractFrame();
243 CheckNoFrame(0);
244}
245
philipelbe7a9e52016-05-19 12:19:35 +0200246TEST_F(TestFrameBuffer2, OneLayerStream) {
247 uint16_t pid = Rand();
248 uint32_t ts = Rand();
249
250 InsertFrame(pid, 0, ts, false);
251 ExtractFrame();
252 CheckFrame(0, pid, 0);
253 for (int i = 1; i < 10; ++i) {
254 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
255 ExtractFrame();
256 clock_.AdvanceTimeMilliseconds(kFps10);
257 CheckFrame(i, pid + i, 0);
258 }
259}
260
philipelbe7a9e52016-05-19 12:19:35 +0200261TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
262 uint16_t pid = Rand();
263 uint32_t ts = Rand();
264
265 InsertFrame(pid, 0, ts, false);
266 InsertFrame(pid + 1, 0, ts + kFps20, false);
267 for (int i = 2; i < 10; i += 2) {
268 uint32_t ts_tl0 = ts + i / 2 * kFps10;
269 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
270 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
271 }
272
273 for (int i = 0; i < 10; ++i) {
274 ExtractFrame();
275 clock_.AdvanceTimeMilliseconds(60);
276 }
277
278 CheckFrame(0, pid, 0);
279 CheckFrame(1, pid + 1, 0);
280 CheckFrame(2, pid + 2, 0);
281 CheckFrame(3, pid + 4, 0);
282 CheckFrame(4, pid + 6, 0);
283 CheckFrame(5, pid + 8, 0);
284 CheckNoFrame(6);
285 CheckNoFrame(7);
286 CheckNoFrame(8);
287 CheckNoFrame(9);
288}
289
290TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
291 uint16_t pid = Rand();
292 uint32_t ts = Rand();
293
294 InsertFrame(pid, 0, ts, false);
295 InsertFrame(pid, 1, ts, false);
296 for (int i = 1; i < 6; ++i) {
297 uint32_t ts_tl0 = ts + i * kFps10;
298 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
299 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
300 }
301
302 ExtractFrame();
303 ExtractFrame();
304 clock_.AdvanceTimeMilliseconds(55);
305 for (int i = 2; i < 12; ++i) {
306 ExtractFrame();
307 clock_.AdvanceTimeMilliseconds(55);
308 }
309
310 CheckFrame(0, pid, 0);
311 CheckFrame(1, pid, 1);
312 CheckFrame(2, pid + 1, 0);
313 CheckFrame(3, pid + 1, 1);
314 CheckFrame(4, pid + 2, 0);
315 CheckFrame(5, pid + 2, 1);
316 CheckFrame(6, pid + 3, 0);
317 CheckFrame(7, pid + 4, 0);
318 CheckFrame(8, pid + 5, 0);
319 CheckNoFrame(9);
320 CheckNoFrame(10);
321 CheckNoFrame(11);
322}
323
324TEST_F(TestFrameBuffer2, InsertLateFrame) {
325 uint16_t pid = Rand();
326 uint32_t ts = Rand();
327
328 InsertFrame(pid, 0, ts, false);
329 ExtractFrame();
330 InsertFrame(pid + 2, 0, ts, false);
331 ExtractFrame();
332 InsertFrame(pid + 1, 0, ts, false, pid);
333 ExtractFrame();
334
335 CheckFrame(0, pid, 0);
336 CheckFrame(1, pid + 2, 0);
337 CheckNoFrame(2);
338}
339
philipel4f6cd6a2016-08-03 10:59:32 +0200340TEST_F(TestFrameBuffer2, ProtectionMode) {
341 uint16_t pid = Rand();
342 uint32_t ts = Rand();
343
344 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0));
345 InsertFrame(pid, 0, ts, false);
346 ExtractFrame();
347
348 buffer_.SetProtectionMode(kProtectionNackFEC);
349 EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0));
350 InsertFrame(pid + 1, 0, ts, false);
351 ExtractFrame();
352}
353
philipelbe7a9e52016-05-19 12:19:35 +0200354} // namespace video_coding
355} // namespace webrtc