Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: webrtc/modules/video_coding/frame_buffer2_unittest.cc

Issue 1969403007: FrameBuffer for the new jitter buffer. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Implemented FrameKey and changed decoded_frames_ to a std::set<FrameKey>, also feedback fixes. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "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/random.h"
21 #include "webrtc/base/platform_thread.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
28 namespace webrtc {
29 namespace video_coding {
30
31 class 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
64 class 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
75 class FrameObjectMock : public FrameObject {
76 public:
77 MOCK_CONST_METHOD1(GetBitstream, bool(uint8_t* destination));
78 };
79
80 class 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 for (size_t r = 0; r < references.size(); ++r)
122 frame->references[r] = references[r];
123
124 buffer_.InsertFrame(std::move(frame));
125 }
126
127 void ExtractFrame(int64_t max_wait_time = 0) {
128 crit_.Enter();
129 if (max_wait_time == 0) {
130 frames_.emplace_back(buffer_.NextFrame(0));
131 crit_.Leave();
132 } else {
133 max_wait_time_ = max_wait_time;
134 trigger_extract_event_.Set();
135 crit_.Leave();
136 // Make sure |crit_| is aquired by |extract_thread_| before returning.
137 crit_acquired_event_.Wait(rtc::Event::kForever);
138 }
139 }
140
141 void CheckFrame(size_t index, int picture_id, int spatial_layer) {
142 rtc::CritScope lock(&crit_);
143 ASSERT_LT(index, frames_.size());
144 ASSERT_TRUE(frames_[index]);
145 ASSERT_EQ(picture_id, frames_[index]->picture_id);
146 ASSERT_EQ(spatial_layer, frames_[index]->spatial_layer);
147 }
148
149 void CheckNoFrame(size_t index) {
150 rtc::CritScope lock(&crit_);
151 ASSERT_LT(index, frames_.size());
152 ASSERT_FALSE(frames_[index]);
153 }
154
155 static bool ExtractLoop(void* obj) {
156 TestFrameBuffer2* tfb = static_cast<TestFrameBuffer2*>(obj);
157 while (true) {
158 tfb->trigger_extract_event_.Wait(rtc::Event::kForever);
159 {
160 rtc::CritScope lock(&tfb->crit_);
161 tfb->crit_acquired_event_.Set();
162 if (tfb->tear_down_)
163 return false;
164
165 tfb->frames_.emplace_back(tfb->buffer_.NextFrame(tfb->max_wait_time_));
166 }
167 }
168 }
169
170 uint32_t Rand() { return rand_.Rand<uint32_t>(); }
171
172 SimulatedClock clock_;
173 VCMTimingFake timing_;
174 VCMJitterEstimatorMock jitter_estimator_;
175 FrameBuffer buffer_;
176 std::vector<std::unique_ptr<FrameObject>> frames_;
177 Random rand_;
178
179 int64_t max_wait_time_;
180 bool tear_down_;
181 rtc::PlatformThread extract_thread_;
182 rtc::Event trigger_extract_event_;
183 rtc::Event crit_acquired_event_;
184 rtc::CriticalSection crit_;
185 };
186
187 TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) {
188 ExtractFrame();
189 CheckNoFrame(0);
190 }
191
192 TEST_F(TestFrameBuffer2, WaitForFrame) {
193 uint16_t pid = Rand();
194 uint32_t ts = Rand();
195
196 ExtractFrame(20);
197 InsertFrame(pid, 0, ts, false);
198 CheckFrame(0, pid, 0);
199 }
200
201 TEST_F(TestFrameBuffer2, OneSuperFrame) {
202 uint16_t pid = Rand();
203 uint32_t ts = Rand();
204
205 ExtractFrame(20);
206 InsertFrame(pid, 1, ts, false);
207 InsertFrame(pid, 0, ts, false);
208 ExtractFrame();
209
210 CheckFrame(0, pid, 0);
211 CheckFrame(1, pid, 1);
212 }
213
214 TEST_F(TestFrameBuffer2, OneLayerStream) {
215 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) {
222 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
223 ExtractFrame();
224 clock_.AdvanceTimeMilliseconds(kFps10);
225 CheckFrame(i, pid + i, 0);
226 }
227 }
228
229 TEST_F(TestFrameBuffer2, OneLayerStreamReordered) {
230 uint16_t pid = Rand();
231 uint32_t ts = Rand();
232
233 InsertFrame(pid, 0, ts, false);
234 ExtractFrame();
235 CheckFrame(0, pid, 0);
236 for (int i = 1; i < 10; i += 2) {
237 ExtractFrame(15);
238 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i);
239 clock_.AdvanceTimeMilliseconds(kFps10);
240 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1);
241 clock_.AdvanceTimeMilliseconds(kFps10);
242 ExtractFrame();
243 CheckFrame(i, pid + i, 0);
244 CheckFrame(i + 1, pid + i + 1, 0);
245 }
246 }
247
248 TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) {
249 uint16_t pid = Rand();
250 uint32_t ts = Rand();
251
252 InsertFrame(pid, 0, ts, false);
253 InsertFrame(pid + 1, 0, ts + kFps20, false);
254 for (int i = 2; i < 10; i += 2) {
255 uint32_t ts_tl0 = ts + i / 2 * kFps10;
256 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2);
257 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1);
258 }
259
260 for (int i = 0; i < 10; ++i) {
261 ExtractFrame();
262 clock_.AdvanceTimeMilliseconds(60);
263 }
264
265 CheckFrame(0, pid, 0);
266 CheckFrame(1, pid + 1, 0);
267 CheckFrame(2, pid + 2, 0);
268 CheckFrame(3, pid + 4, 0);
269 CheckFrame(4, pid + 6, 0);
270 CheckFrame(5, pid + 8, 0);
271 CheckNoFrame(6);
272 CheckNoFrame(7);
273 CheckNoFrame(8);
274 CheckNoFrame(9);
275 }
276
277 TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) {
278 uint16_t pid = Rand();
279 uint32_t ts = Rand();
280
281 InsertFrame(pid, 0, ts, false);
282 InsertFrame(pid, 1, ts, false);
283 for (int i = 1; i < 6; ++i) {
284 uint32_t ts_tl0 = ts + i * kFps10;
285 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1);
286 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1);
287 }
288
289 ExtractFrame();
290 ExtractFrame();
291 clock_.AdvanceTimeMilliseconds(55);
292 for (int i = 2; i < 12; ++i) {
293 ExtractFrame();
294 clock_.AdvanceTimeMilliseconds(55);
295 }
296
297 CheckFrame(0, pid, 0);
298 CheckFrame(1, pid, 1);
299 CheckFrame(2, pid + 1, 0);
300 CheckFrame(3, pid + 1, 1);
301 CheckFrame(4, pid + 2, 0);
302 CheckFrame(5, pid + 2, 1);
303 CheckFrame(6, pid + 3, 0);
304 CheckFrame(7, pid + 4, 0);
305 CheckFrame(8, pid + 5, 0);
306 CheckNoFrame(9);
307 CheckNoFrame(10);
308 CheckNoFrame(11);
309 }
310
311 TEST_F(TestFrameBuffer2, InsertLateFrame) {
312 uint16_t pid = Rand();
313 uint32_t ts = Rand();
314
315 InsertFrame(pid, 0, ts, false);
316 ExtractFrame();
317 InsertFrame(pid + 2, 0, ts, false);
318 ExtractFrame();
319 InsertFrame(pid + 1, 0, ts, false, pid);
320 ExtractFrame();
321
322 CheckFrame(0, pid, 0);
323 CheckFrame(1, pid + 2, 0);
324 CheckNoFrame(2);
325 }
326
327 } // namespace video_coding
328 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698