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