blob: 5c81d0284d4d34956aea4499e8631b5cb96a931c [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));
73};
74
philipelb4d31082016-07-11 08:46:29 -070075class FrameObjectFake : public FrameObject {
philipelbe7a9e52016-05-19 12:19:35 +020076 public:
philipelb4d31082016-07-11 08:46:29 -070077 bool GetBitstream(uint8_t* destination) const override { return true; }
78
79 uint32_t Timestamp() const override { return timestamp; }
80
81 int64_t ReceivedTime() const override { return 0; }
82
83 int64_t RenderTime() const override { return _renderTimeMs; }
philipelbe7a9e52016-05-19 12:19:35 +020084};
85
86class TestFrameBuffer2 : public ::testing::Test {
87 protected:
88 static constexpr int kMaxReferences = 5;
89 static constexpr int kFps1 = 1000;
90 static constexpr int kFps10 = kFps1 / 10;
91 static constexpr int kFps20 = kFps1 / 20;
92
93 TestFrameBuffer2()
94 : clock_(0),
95 timing_(&clock_),
96 jitter_estimator_(&clock_),
97 buffer_(&clock_, &jitter_estimator_, &timing_),
98 rand_(0x34678213),
99 tear_down_(false),
100 extract_thread_(&ExtractLoop, this, "Extract Thread"),
101 trigger_extract_event_(false, false),
102 crit_acquired_event_(false, false) {}
103
104 void SetUp() override { extract_thread_.Start(); }
105
106 void TearDown() override {
107 tear_down_ = true;
108 trigger_extract_event_.Set();
109 extract_thread_.Stop();
110 }
111
112 template <typename... T>
113 void InsertFrame(uint16_t picture_id,
114 uint8_t spatial_layer,
115 int64_t ts_ms,
116 bool inter_layer_predicted,
117 T... refs) {
118 static_assert(sizeof...(refs) <= kMaxReferences,
119 "To many references specified for FrameObject.");
120 std::array<uint16_t, sizeof...(refs)> references = {{refs...}};
121
philipelb4d31082016-07-11 08:46:29 -0700122 std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake());
philipelbe7a9e52016-05-19 12:19:35 +0200123 frame->picture_id = picture_id;
124 frame->spatial_layer = spatial_layer;
125 frame->timestamp = ts_ms * 90;
126 frame->num_references = references.size();
127 frame->inter_layer_predicted = inter_layer_predicted;
128 for (size_t r = 0; r < references.size(); ++r)
129 frame->references[r] = references[r];
130
131 buffer_.InsertFrame(std::move(frame));
132 }
133
134 void ExtractFrame(int64_t max_wait_time = 0) {
135 crit_.Enter();
136 if (max_wait_time == 0) {
137 frames_.emplace_back(buffer_.NextFrame(0));
138 crit_.Leave();
139 } else {
140 max_wait_time_ = max_wait_time;
141 trigger_extract_event_.Set();
142 crit_.Leave();
143 // Make sure |crit_| is aquired by |extract_thread_| before returning.
144 crit_acquired_event_.Wait(rtc::Event::kForever);
145 }
146 }
147
148 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
149 rtc::CritScope lock(&crit_);
150 ASSERT_LT(index, frames_.size());
151 ASSERT_TRUE(frames_[index]);
152 ASSERT_EQ(picture_id, frames_[index]->picture_id);
153 ASSERT_EQ(spatial_layer, frames_[index]->spatial_layer);
154 }
155
156 void CheckNoFrame(size_t index) {
157 rtc::CritScope lock(&crit_);
158 ASSERT_LT(index, frames_.size());
159 ASSERT_FALSE(frames_[index]);
160 }
161
162 static bool ExtractLoop(void* obj) {
163 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
164 while (true) {
165 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
166 {
167 rtc::CritScope lock(&tfb->crit_);
168 tfb->crit_acquired_event_.Set();
169 if (tfb->tear_down_)
170 return false;
171
172 tfb->frames_.emplace_back(tfb->buffer_.NextFrame(tfb->max_wait_time_));
173 }
174 }
175 }
176
177 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
178
179 SimulatedClock clock_;
180 VCMTimingFake timing_;
181 VCMJitterEstimatorMock jitter_estimator_;
182 FrameBuffer buffer_;
183 std::vector<std::unique_ptr<FrameObject>> frames_;
184 Random rand_;
185
186 int64_t max_wait_time_;
187 bool tear_down_;
188 rtc::PlatformThread extract_thread_;
189 rtc::Event trigger_extract_event_;
190 rtc::Event crit_acquired_event_;
191 rtc::CriticalSection crit_;
192};
193
philipel6e8224f2016-05-19 17:07:44 +0200194// Following tests are timing dependent. Either the timeouts have to
195// be increased by a large margin, which would slow down all trybots,
196// or we disable them for the very slow ones, like we do here.
197#if !defined(ADDRESS_SANITIZER) && !defined(MEMORY_SANITIZER)
philipelbe7a9e52016-05-19 12:19:35 +0200198TEST_F(TestFrameBuffer2, WaitForFrame) {
199 uint16_t pid = Rand();
200 uint32_t ts = Rand();
201
philipel6e8224f2016-05-19 17:07:44 +0200202 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200203 InsertFrame(pid, 0, ts, false);
204 CheckFrame(0, pid, 0);
205}
206
207TEST_F(TestFrameBuffer2, OneSuperFrame) {
208 uint16_t pid = Rand();
209 uint32_t ts = Rand();
210
philipel6e8224f2016-05-19 17:07:44 +0200211 ExtractFrame(50);
philipelbe7a9e52016-05-19 12:19:35 +0200212 InsertFrame(pid, 1, ts, true);
213 InsertFrame(pid, 0, ts, false);
214 ExtractFrame();
215
216 CheckFrame(0, pid, 0);
217 CheckFrame(1, pid, 1);
218}
219
philipel94616b32016-05-20 10:43:01 +0200220TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) {
philipel6e8224f2016-05-19 17:07:44 +0200221 uint16_t pid = Rand();
222 uint32_t ts = Rand();
223
224 InsertFrame(pid, 0, ts, false);
225 ExtractFrame();
226 CheckFrame(0, pid, 0);
227 for (int i = 1; i < 10; i += 2) {
228 ExtractFrame(50);
229 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
230 clock_.AdvanceTimeMilliseconds(kFps10);
231 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
232 clock_.AdvanceTimeMilliseconds(kFps10);
233 ExtractFrame();
234 CheckFrame(i, pid + i, 0);
235 CheckFrame(i + 1, pid + i + 1, 0);
236 }
237}
238#endif // Timing dependent tests.
239
240TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
241 ExtractFrame();
242 CheckNoFrame(0);
243}
244
philipelbe7a9e52016-05-19 12:19:35 +0200245TEST_F(TestFrameBuffer2, OneLayerStream) {
246 uint16_t pid = Rand();
247 uint32_t ts = Rand();
248
249 InsertFrame(pid, 0, ts, false);
250 ExtractFrame();
251 CheckFrame(0, pid, 0);
252 for (int i = 1; i < 10; ++i) {
253 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
254 ExtractFrame();
255 clock_.AdvanceTimeMilliseconds(kFps10);
256 CheckFrame(i, pid + i, 0);
257 }
258}
259
philipelbe7a9e52016-05-19 12:19:35 +0200260TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
261 uint16_t pid = Rand();
262 uint32_t ts = Rand();
263
264 InsertFrame(pid, 0, ts, false);
265 InsertFrame(pid + 1, 0, ts + kFps20, false);
266 for (int i = 2; i < 10; i += 2) {
267 uint32_t ts_tl0 = ts + i / 2 * kFps10;
268 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
269 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
270 }
271
272 for (int i = 0; i < 10; ++i) {
273 ExtractFrame();
274 clock_.AdvanceTimeMilliseconds(60);
275 }
276
277 CheckFrame(0, pid, 0);
278 CheckFrame(1, pid + 1, 0);
279 CheckFrame(2, pid + 2, 0);
280 CheckFrame(3, pid + 4, 0);
281 CheckFrame(4, pid + 6, 0);
282 CheckFrame(5, pid + 8, 0);
283 CheckNoFrame(6);
284 CheckNoFrame(7);
285 CheckNoFrame(8);
286 CheckNoFrame(9);
287}
288
289TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
290 uint16_t pid = Rand();
291 uint32_t ts = Rand();
292
293 InsertFrame(pid, 0, ts, false);
294 InsertFrame(pid, 1, ts, false);
295 for (int i = 1; i < 6; ++i) {
296 uint32_t ts_tl0 = ts + i * kFps10;
297 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
298 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
299 }
300
301 ExtractFrame();
302 ExtractFrame();
303 clock_.AdvanceTimeMilliseconds(55);
304 for (int i = 2; i < 12; ++i) {
305 ExtractFrame();
306 clock_.AdvanceTimeMilliseconds(55);
307 }
308
309 CheckFrame(0, pid, 0);
310 CheckFrame(1, pid, 1);
311 CheckFrame(2, pid + 1, 0);
312 CheckFrame(3, pid + 1, 1);
313 CheckFrame(4, pid + 2, 0);
314 CheckFrame(5, pid + 2, 1);
315 CheckFrame(6, pid + 3, 0);
316 CheckFrame(7, pid + 4, 0);
317 CheckFrame(8, pid + 5, 0);
318 CheckNoFrame(9);
319 CheckNoFrame(10);
320 CheckNoFrame(11);
321}
322
323TEST_F(TestFrameBuffer2, InsertLateFrame) {
324 uint16_t pid = Rand();
325 uint32_t ts = Rand();
326
327 InsertFrame(pid, 0, ts, false);
328 ExtractFrame();
329 InsertFrame(pid + 2, 0, ts, false);
330 ExtractFrame();
331 InsertFrame(pid + 1, 0, ts, false, pid);
332 ExtractFrame();
333
334 CheckFrame(0, pid, 0);
335 CheckFrame(1, pid + 2, 0);
336 CheckNoFrame(2);
337}
338
339} // namespace video_coding
340} // namespace webrtc