philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 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/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 | |
| 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)); |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 73 | MOCK_METHOD1(GetJitterEstimate, int(double rttMultiplier)); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 76 | class FrameObjectFake : public FrameObject { |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 77 | public: |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 78 | 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; } |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | class 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 | |
philipel | b4d3108 | 2016-07-11 08:46:29 -0700 | [diff] [blame] | 123 | std::unique_ptr<FrameObjectFake> frame(new FrameObjectFake()); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 124 | 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) { |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 138 | 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)); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 142 | 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 | |
philipel | 7556282 | 2016-09-05 10:57:41 +0200 | [diff] [blame] | 176 | 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)); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | uint32_t Rand() { return rand_.Rand<uint32_t>(); } |
| 186 | |
| 187 | SimulatedClock clock_; |
| 188 | VCMTimingFake timing_; |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 189 | ::testing::NiceMock<VCMJitterEstimatorMock> jitter_estimator_; |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 190 | 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 | |
philipel | 6e8224f | 2016-05-19 17:07:44 +0200 | [diff] [blame] | 202 | // 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) |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 206 | TEST_F(TestFrameBuffer2, WaitForFrame) { |
| 207 | uint16_t pid = Rand(); |
| 208 | uint32_t ts = Rand(); |
| 209 | |
philipel | 6e8224f | 2016-05-19 17:07:44 +0200 | [diff] [blame] | 210 | ExtractFrame(50); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 211 | InsertFrame(pid, 0, ts, false); |
| 212 | CheckFrame(0, pid, 0); |
| 213 | } |
| 214 | |
| 215 | TEST_F(TestFrameBuffer2, OneSuperFrame) { |
| 216 | uint16_t pid = Rand(); |
| 217 | uint32_t ts = Rand(); |
| 218 | |
philipel | 6e8224f | 2016-05-19 17:07:44 +0200 | [diff] [blame] | 219 | ExtractFrame(50); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 220 | InsertFrame(pid, 1, ts, true); |
| 221 | InsertFrame(pid, 0, ts, false); |
| 222 | ExtractFrame(); |
| 223 | |
| 224 | CheckFrame(0, pid, 0); |
| 225 | CheckFrame(1, pid, 1); |
| 226 | } |
| 227 | |
philipel | 94616b3 | 2016-05-20 10:43:01 +0200 | [diff] [blame] | 228 | TEST_F(TestFrameBuffer2, DISABLED_OneLayerStreamReordered) { |
philipel | 6e8224f | 2016-05-19 17:07:44 +0200 | [diff] [blame] | 229 | uint16_t pid = Rand(); |
| 230 | uint32_t ts = Rand(); |
| 231 | |
| 232 | InsertFrame(pid, 0, ts, false); |
| 233 | ExtractFrame(); |
| 234 | CheckFrame(0, pid, 0); |
| 235 | for (int i = 1; i < 10; i += 2) { |
| 236 | ExtractFrame(50); |
| 237 | InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i); |
| 238 | clock_.AdvanceTimeMilliseconds(kFps10); |
| 239 | InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1); |
| 240 | clock_.AdvanceTimeMilliseconds(kFps10); |
| 241 | ExtractFrame(); |
| 242 | CheckFrame(i, pid + i, 0); |
| 243 | CheckFrame(i + 1, pid + i + 1, 0); |
| 244 | } |
| 245 | } |
| 246 | #endif // Timing dependent tests. |
| 247 | |
| 248 | TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) { |
| 249 | ExtractFrame(); |
| 250 | CheckNoFrame(0); |
| 251 | } |
| 252 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 253 | TEST_F(TestFrameBuffer2, OneLayerStream) { |
| 254 | uint16_t pid = Rand(); |
| 255 | uint32_t ts = Rand(); |
| 256 | |
| 257 | InsertFrame(pid, 0, ts, false); |
| 258 | ExtractFrame(); |
| 259 | CheckFrame(0, pid, 0); |
| 260 | for (int i = 1; i < 10; ++i) { |
| 261 | InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1); |
| 262 | ExtractFrame(); |
| 263 | clock_.AdvanceTimeMilliseconds(kFps10); |
| 264 | CheckFrame(i, pid + i, 0); |
| 265 | } |
| 266 | } |
| 267 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 268 | TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) { |
| 269 | uint16_t pid = Rand(); |
| 270 | uint32_t ts = Rand(); |
| 271 | |
| 272 | InsertFrame(pid, 0, ts, false); |
| 273 | InsertFrame(pid + 1, 0, ts + kFps20, false); |
| 274 | for (int i = 2; i < 10; i += 2) { |
| 275 | uint32_t ts_tl0 = ts + i / 2 * kFps10; |
| 276 | InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2); |
| 277 | InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1); |
| 278 | } |
| 279 | |
| 280 | for (int i = 0; i < 10; ++i) { |
| 281 | ExtractFrame(); |
| 282 | clock_.AdvanceTimeMilliseconds(60); |
| 283 | } |
| 284 | |
| 285 | CheckFrame(0, pid, 0); |
| 286 | CheckFrame(1, pid + 1, 0); |
| 287 | CheckFrame(2, pid + 2, 0); |
| 288 | CheckFrame(3, pid + 4, 0); |
| 289 | CheckFrame(4, pid + 6, 0); |
| 290 | CheckFrame(5, pid + 8, 0); |
| 291 | CheckNoFrame(6); |
| 292 | CheckNoFrame(7); |
| 293 | CheckNoFrame(8); |
| 294 | CheckNoFrame(9); |
| 295 | } |
| 296 | |
| 297 | TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) { |
| 298 | uint16_t pid = Rand(); |
| 299 | uint32_t ts = Rand(); |
| 300 | |
| 301 | InsertFrame(pid, 0, ts, false); |
| 302 | InsertFrame(pid, 1, ts, false); |
| 303 | for (int i = 1; i < 6; ++i) { |
| 304 | uint32_t ts_tl0 = ts + i * kFps10; |
| 305 | InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1); |
| 306 | InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1); |
| 307 | } |
| 308 | |
| 309 | ExtractFrame(); |
| 310 | ExtractFrame(); |
| 311 | clock_.AdvanceTimeMilliseconds(55); |
| 312 | for (int i = 2; i < 12; ++i) { |
| 313 | ExtractFrame(); |
| 314 | clock_.AdvanceTimeMilliseconds(55); |
| 315 | } |
| 316 | |
| 317 | CheckFrame(0, pid, 0); |
| 318 | CheckFrame(1, pid, 1); |
| 319 | CheckFrame(2, pid + 1, 0); |
| 320 | CheckFrame(3, pid + 1, 1); |
| 321 | CheckFrame(4, pid + 2, 0); |
| 322 | CheckFrame(5, pid + 2, 1); |
| 323 | CheckFrame(6, pid + 3, 0); |
| 324 | CheckFrame(7, pid + 4, 0); |
| 325 | CheckFrame(8, pid + 5, 0); |
| 326 | CheckNoFrame(9); |
| 327 | CheckNoFrame(10); |
| 328 | CheckNoFrame(11); |
| 329 | } |
| 330 | |
| 331 | TEST_F(TestFrameBuffer2, InsertLateFrame) { |
| 332 | uint16_t pid = Rand(); |
| 333 | uint32_t ts = Rand(); |
| 334 | |
| 335 | InsertFrame(pid, 0, ts, false); |
| 336 | ExtractFrame(); |
| 337 | InsertFrame(pid + 2, 0, ts, false); |
| 338 | ExtractFrame(); |
| 339 | InsertFrame(pid + 1, 0, ts, false, pid); |
| 340 | ExtractFrame(); |
| 341 | |
| 342 | CheckFrame(0, pid, 0); |
| 343 | CheckFrame(1, pid + 2, 0); |
| 344 | CheckNoFrame(2); |
| 345 | } |
| 346 | |
philipel | 4f6cd6a | 2016-08-03 10:59:32 +0200 | [diff] [blame] | 347 | TEST_F(TestFrameBuffer2, ProtectionMode) { |
| 348 | uint16_t pid = Rand(); |
| 349 | uint32_t ts = Rand(); |
| 350 | |
| 351 | EXPECT_CALL(jitter_estimator_, GetJitterEstimate(1.0)); |
| 352 | InsertFrame(pid, 0, ts, false); |
| 353 | ExtractFrame(); |
| 354 | |
| 355 | buffer_.SetProtectionMode(kProtectionNackFEC); |
| 356 | EXPECT_CALL(jitter_estimator_, GetJitterEstimate(0.0)); |
| 357 | InsertFrame(pid + 1, 0, ts, false); |
| 358 | ExtractFrame(); |
| 359 | } |
| 360 | |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 361 | } // namespace video_coding |
| 362 | } // namespace webrtc |