| OLD | NEW |
| (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/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)); | |
| 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 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 | |
| 188 TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) { | |
| 189 ExtractFrame(); | |
| 190 CheckNoFrame(0); | |
| 191 } | |
| 192 | |
| 193 TEST_F(TestFrameBuffer2, WaitForFrame) { | |
| 194 uint16_t pid = Rand(); | |
| 195 uint32_t ts = Rand(); | |
| 196 | |
| 197 ExtractFrame(20); | |
| 198 InsertFrame(pid, 0, ts, false); | |
| 199 CheckFrame(0, pid, 0); | |
| 200 } | |
| 201 | |
| 202 TEST_F(TestFrameBuffer2, OneSuperFrame) { | |
| 203 uint16_t pid = Rand(); | |
| 204 uint32_t ts = Rand(); | |
| 205 | |
| 206 ExtractFrame(20); | |
| 207 InsertFrame(pid, 1, ts, true); | |
| 208 InsertFrame(pid, 0, ts, false); | |
| 209 ExtractFrame(); | |
| 210 | |
| 211 CheckFrame(0, pid, 0); | |
| 212 CheckFrame(1, pid, 1); | |
| 213 } | |
| 214 | |
| 215 TEST_F(TestFrameBuffer2, OneLayerStream) { | |
| 216 uint16_t pid = Rand(); | |
| 217 uint32_t ts = Rand(); | |
| 218 | |
| 219 InsertFrame(pid, 0, ts, false); | |
| 220 ExtractFrame(); | |
| 221 CheckFrame(0, pid, 0); | |
| 222 for (int i = 1; i < 10; ++i) { | |
| 223 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1); | |
| 224 ExtractFrame(); | |
| 225 clock_.AdvanceTimeMilliseconds(kFps10); | |
| 226 CheckFrame(i, pid + i, 0); | |
| 227 } | |
| 228 } | |
| 229 | |
| 230 TEST_F(TestFrameBuffer2, OneLayerStreamReordered) { | |
| 231 uint16_t pid = Rand(); | |
| 232 uint32_t ts = Rand(); | |
| 233 | |
| 234 InsertFrame(pid, 0, ts, false); | |
| 235 ExtractFrame(); | |
| 236 CheckFrame(0, pid, 0); | |
| 237 for (int i = 1; i < 10; i += 2) { | |
| 238 ExtractFrame(15); | |
| 239 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i); | |
| 240 clock_.AdvanceTimeMilliseconds(kFps10); | |
| 241 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1); | |
| 242 clock_.AdvanceTimeMilliseconds(kFps10); | |
| 243 ExtractFrame(); | |
| 244 CheckFrame(i, pid + i, 0); | |
| 245 CheckFrame(i + 1, pid + i + 1, 0); | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) { | |
| 250 uint16_t pid = Rand(); | |
| 251 uint32_t ts = Rand(); | |
| 252 | |
| 253 InsertFrame(pid, 0, ts, false); | |
| 254 InsertFrame(pid + 1, 0, ts + kFps20, false); | |
| 255 for (int i = 2; i < 10; i += 2) { | |
| 256 uint32_t ts_tl0 = ts + i / 2 * kFps10; | |
| 257 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 2); | |
| 258 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1); | |
| 259 } | |
| 260 | |
| 261 for (int i = 0; i < 10; ++i) { | |
| 262 ExtractFrame(); | |
| 263 clock_.AdvanceTimeMilliseconds(60); | |
| 264 } | |
| 265 | |
| 266 CheckFrame(0, pid, 0); | |
| 267 CheckFrame(1, pid + 1, 0); | |
| 268 CheckFrame(2, pid + 2, 0); | |
| 269 CheckFrame(3, pid + 4, 0); | |
| 270 CheckFrame(4, pid + 6, 0); | |
| 271 CheckFrame(5, pid + 8, 0); | |
| 272 CheckNoFrame(6); | |
| 273 CheckNoFrame(7); | |
| 274 CheckNoFrame(8); | |
| 275 CheckNoFrame(9); | |
| 276 } | |
| 277 | |
| 278 TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) { | |
| 279 uint16_t pid = Rand(); | |
| 280 uint32_t ts = Rand(); | |
| 281 | |
| 282 InsertFrame(pid, 0, ts, false); | |
| 283 InsertFrame(pid, 1, ts, false); | |
| 284 for (int i = 1; i < 6; ++i) { | |
| 285 uint32_t ts_tl0 = ts + i * kFps10; | |
| 286 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1); | |
| 287 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1); | |
| 288 } | |
| 289 | |
| 290 ExtractFrame(); | |
| 291 ExtractFrame(); | |
| 292 clock_.AdvanceTimeMilliseconds(55); | |
| 293 for (int i = 2; i < 12; ++i) { | |
| 294 ExtractFrame(); | |
| 295 clock_.AdvanceTimeMilliseconds(55); | |
| 296 } | |
| 297 | |
| 298 CheckFrame(0, pid, 0); | |
| 299 CheckFrame(1, pid, 1); | |
| 300 CheckFrame(2, pid + 1, 0); | |
| 301 CheckFrame(3, pid + 1, 1); | |
| 302 CheckFrame(4, pid + 2, 0); | |
| 303 CheckFrame(5, pid + 2, 1); | |
| 304 CheckFrame(6, pid + 3, 0); | |
| 305 CheckFrame(7, pid + 4, 0); | |
| 306 CheckFrame(8, pid + 5, 0); | |
| 307 CheckNoFrame(9); | |
| 308 CheckNoFrame(10); | |
| 309 CheckNoFrame(11); | |
| 310 } | |
| 311 | |
| 312 TEST_F(TestFrameBuffer2, InsertLateFrame) { | |
| 313 uint16_t pid = Rand(); | |
| 314 uint32_t ts = Rand(); | |
| 315 | |
| 316 InsertFrame(pid, 0, ts, false); | |
| 317 ExtractFrame(); | |
| 318 InsertFrame(pid + 2, 0, ts, false); | |
| 319 ExtractFrame(); | |
| 320 InsertFrame(pid + 1, 0, ts, false, pid); | |
| 321 ExtractFrame(); | |
| 322 | |
| 323 CheckFrame(0, pid, 0); | |
| 324 CheckFrame(1, pid + 2, 0); | |
| 325 CheckNoFrame(2); | |
| 326 } | |
| 327 | |
| 328 } // namespace video_coding | |
| 329 } // namespace webrtc | |
| OLD | NEW |