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 <algorithm> |
| 12 #include <cstring> |
| 13 #include <limits> |
| 14 #include <vector> |
| 15 |
| 16 #include "webrtc/modules/video_coding/frame_buffer2.h" |
| 17 |
| 18 #include "webrtc/base/random.h" |
| 19 #include "webrtc/base/platform_thread.h" |
| 20 #include "webrtc/base/thread.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "webrtc/modules/video_coding/timing.h" |
| 24 #include "webrtc/modules/video_coding/jitter_estimator.h" |
| 25 |
| 26 namespace webrtc { |
| 27 namespace video_coding { |
| 28 |
| 29 class VCMTimingMock : public VCMTiming { |
| 30 public: |
| 31 explicit VCMTimingMock(Clock* clock) : VCMTiming(clock) {} |
| 32 |
| 33 int64_t |
| 34 RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const override { |
| 35 if (last_ms_ == -1) { |
| 36 last_ms_ = now_ms + kDelayMs; |
| 37 last_timestamp_ = frame_timestamp; |
| 38 } |
| 39 |
| 40 uint32_t diff = MinDiff(frame_timestamp, last_timestamp_); |
| 41 if (AheadOf(frame_timestamp, last_timestamp_)) |
| 42 last_ms_ += diff / 90; |
| 43 else |
| 44 last_ms_ -= diff / 90; |
| 45 |
| 46 last_timestamp_ = frame_timestamp; |
| 47 return last_ms_; |
| 48 } |
| 49 |
| 50 |
| 51 uint32_t |
| 52 MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const override { |
| 53 return std::max<int>(0, render_time_ms - now_ms - kDecodeTime); |
| 54 } |
| 55 |
| 56 static const int kDelayMs = 50; |
| 57 static const int kDecodeTime = kDelayMs/2; |
| 58 mutable uint32_t last_timestamp_ = 0; |
| 59 mutable int64_t last_ms_ = -1; |
| 60 }; |
| 61 |
| 62 class VCMJitterEstimatorMock : public VCMJitterEstimator { |
| 63 public: |
| 64 explicit VCMJitterEstimatorMock(Clock* clock) : VCMJitterEstimator(clock) {} |
| 65 |
| 66 MOCK_METHOD1(UpdateRtt, void(int64_t rttMs)); |
| 67 MOCK_METHOD3(UpdateEstimate, void(int64_t frameDelayMs, |
| 68 uint32_t frameSizeBytes, |
| 69 bool incompleteFrame)); |
| 70 }; |
| 71 |
| 72 class FrameObjectMock : public FrameObject { |
| 73 public: |
| 74 MOCK_CONST_METHOD1(GetBitstream, bool(uint8_t* destination)); |
| 75 }; |
| 76 |
| 77 class TestFrameBuffer2 : public ::testing::Test { |
| 78 protected: |
| 79 TestFrameBuffer2() : |
| 80 clock_(0), |
| 81 timing_(&clock_), |
| 82 jitter_estimator_(&clock_), |
| 83 buffer_(&clock_, &jitter_estimator_, &timing_), |
| 84 rand_(0x34678213), |
| 85 tear_down_(false), |
| 86 extract_thread_(&ExtractLoop, this, "Extract Thread"), |
| 87 trigger_extract_event_(false, false), |
| 88 crit_acquired_event_(false, false) {} |
| 89 |
| 90 void SetUp() override { |
| 91 extract_thread_.Start(); |
| 92 } |
| 93 |
| 94 void TearDown() override { |
| 95 tear_down_ = true; |
| 96 trigger_extract_event_.Set(); |
| 97 extract_thread_.Stop(); |
| 98 } |
| 99 |
| 100 template <typename... T> |
| 101 void InsertFrame(uint16_t picture_id, |
| 102 uint8_t spatial_layer, |
| 103 int64_t ts_ms, |
| 104 bool inter_layer_predicted, |
| 105 T... refs) { |
| 106 static_assert(sizeof...(refs) <= kMaxReferences, |
| 107 "To many references specified for FrameObject."); |
| 108 std::array<uint16_t, sizeof...(refs)> references = {{refs...}}; |
| 109 |
| 110 std::unique_ptr<FrameObjectMock> frame(new FrameObjectMock()); |
| 111 frame->picture_id = picture_id; |
| 112 frame->spatial_layer = spatial_layer; |
| 113 frame->timestamp = ts_ms * 90; |
| 114 frame->num_references = references.size(); |
| 115 for (size_t r = 0; r < references.size(); ++r) |
| 116 frame->references[r] = references[r]; |
| 117 |
| 118 buffer_.InsertFrame(std::move(frame)); |
| 119 } |
| 120 |
| 121 void ExtractFrame(int64_t max_wait_time = 0) { |
| 122 crit_.Enter(); |
| 123 if (max_wait_time == 0) { |
| 124 frames_.emplace_back(buffer_.NextFrame(0)); |
| 125 crit_.Leave(); |
| 126 } else { |
| 127 max_wait_time_ = max_wait_time; |
| 128 trigger_extract_event_.Set(); |
| 129 crit_.Leave(); |
| 130 // Make sure |crit_| is aquired by |extract_thread_| before returning. |
| 131 crit_acquired_event_.Wait(rtc::Thread::kForever); |
| 132 } |
| 133 } |
| 134 |
| 135 void CheckFrame(size_t index, |
| 136 int picture_id = -1, |
| 137 int spatial_layer = -1) { |
| 138 rtc::CritScope lock(&crit_); |
| 139 if (frames_.size() <= index) { |
| 140 ADD_FAILURE() << "Expected frame at index " << index |
| 141 << " but vector does not contain element at index " |
| 142 << index; |
| 143 return; |
| 144 } |
| 145 |
| 146 |
| 147 if (picture_id == -1) { |
| 148 ASSERT_EQ(nullptr, frames_[index].get()); |
| 149 } else { |
| 150 ASSERT_TRUE(frames_[index]); |
| 151 ASSERT_EQ(picture_id, frames_[index]->picture_id); |
| 152 ASSERT_EQ(spatial_layer, frames_[index]->spatial_layer); |
| 153 } |
| 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::Thread::kForever); |
| 160 tfb->crit_.Enter(); |
| 161 tfb->crit_acquired_event_.Set(); |
| 162 if (tfb->tear_down_) { |
| 163 tfb->crit_.Leave(); |
| 164 return false; |
| 165 } |
| 166 |
| 167 tfb->frames_.emplace_back(tfb->buffer_.NextFrame(tfb->max_wait_time_)); |
| 168 tfb->crit_.Leave(); |
| 169 } |
| 170 } |
| 171 |
| 172 uint16_t Rand() { return rand_.Rand(std::numeric_limits<uint32_t>::max()); } |
| 173 |
| 174 static const int kMaxReferences = 5; |
| 175 static const int kFps1 = 1000; |
| 176 static const int kFps10 = kFps1/10; |
| 177 static const int kFps20 = kFps1/20; |
| 178 |
| 179 SimulatedClock clock_; |
| 180 VCMTimingMock 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 |
| 194 TEST_F(TestFrameBuffer2, ExtractFromEmptyBuffer) { |
| 195 ExtractFrame(); |
| 196 CheckFrame(0); |
| 197 } |
| 198 |
| 199 TEST_F(TestFrameBuffer2, WaitForFrame) { |
| 200 uint16_t pid = Rand(); |
| 201 uint32_t ts = Rand(); |
| 202 |
| 203 ExtractFrame(20); |
| 204 InsertFrame(pid, 0, ts, false); |
| 205 CheckFrame(0, pid, 0); |
| 206 } |
| 207 |
| 208 TEST_F(TestFrameBuffer2, OneSuperFrame) { |
| 209 uint16_t pid = Rand(); |
| 210 uint32_t ts = Rand(); |
| 211 |
| 212 ExtractFrame(20); |
| 213 InsertFrame(pid, 1, ts, false); |
| 214 InsertFrame(pid, 0, ts, false); |
| 215 ExtractFrame(); |
| 216 |
| 217 CheckFrame(0, pid, 0); |
| 218 CheckFrame(1, pid, 1); |
| 219 } |
| 220 |
| 221 TEST_F(TestFrameBuffer2, OneLayerStream) { |
| 222 uint16_t pid = Rand(); |
| 223 uint32_t ts = Rand(); |
| 224 |
| 225 InsertFrame(pid, 0, ts, false); |
| 226 ExtractFrame(); |
| 227 CheckFrame(0, pid, 0); |
| 228 for (int i = 1; i < 10; ++i) { |
| 229 InsertFrame(pid + i, 0, ts + i * kFps10, false, pid + i - 1); |
| 230 ExtractFrame(); |
| 231 clock_.AdvanceTimeMilliseconds(kFps10); |
| 232 CheckFrame(i, pid + i, 0); |
| 233 } |
| 234 } |
| 235 |
| 236 TEST_F(TestFrameBuffer2, OneLayerStreamReordered) { |
| 237 uint16_t pid = Rand(); |
| 238 uint32_t ts = Rand(); |
| 239 |
| 240 InsertFrame(pid, 0, ts, false); |
| 241 ExtractFrame(); |
| 242 CheckFrame(0, pid, 0); |
| 243 for (int i = 1; i < 10; i += 2) { |
| 244 ExtractFrame(15); |
| 245 InsertFrame(pid + i + 1, 0, ts + (i + 1) * kFps10, false, pid + i); |
| 246 clock_.AdvanceTimeMilliseconds(kFps10); |
| 247 InsertFrame(pid + i , 0, ts + i * kFps10, false, pid + i - 1); |
| 248 clock_.AdvanceTimeMilliseconds(kFps10); |
| 249 ExtractFrame(); |
| 250 CheckFrame(i , pid + i, 0); |
| 251 CheckFrame(i + 1, pid + i + 1, 0); |
| 252 } |
| 253 } |
| 254 |
| 255 TEST_F(TestFrameBuffer2, DropTemporalLayerSlowDecoder) { |
| 256 uint16_t pid = Rand(); |
| 257 uint32_t ts = Rand(); |
| 258 |
| 259 InsertFrame(pid, 0, ts, false); |
| 260 InsertFrame(pid + 1, 0, ts + kFps20, false); |
| 261 for (int i = 2; i < 10; i += 2) { |
| 262 uint32_t ts_tl0 = ts + i/2 * kFps10; |
| 263 InsertFrame(pid + i , 0, ts_tl0 , false, pid + i - 2); |
| 264 InsertFrame(pid + i + 1, 0, ts_tl0 + kFps20, false, pid + i, pid + i - 1); |
| 265 } |
| 266 |
| 267 for (int i = 0; i < 10; ++i) { |
| 268 ExtractFrame(); |
| 269 clock_.AdvanceTimeMilliseconds(60); |
| 270 } |
| 271 |
| 272 CheckFrame(0, pid , 0); |
| 273 CheckFrame(1, pid + 1, 0); |
| 274 CheckFrame(2, pid + 2, 0); |
| 275 CheckFrame(3, pid + 4, 0); |
| 276 CheckFrame(4, pid + 6, 0); |
| 277 CheckFrame(5, pid + 8, 0); |
| 278 CheckFrame(6); |
| 279 CheckFrame(7); |
| 280 CheckFrame(8); |
| 281 CheckFrame(9); |
| 282 } |
| 283 |
| 284 TEST_F(TestFrameBuffer2, DropSpatialLayerSlowDecoder) { |
| 285 uint16_t pid = Rand(); |
| 286 uint32_t ts = Rand(); |
| 287 |
| 288 InsertFrame(pid, 0, ts, false); |
| 289 InsertFrame(pid, 1, ts, false); |
| 290 for (int i = 1; i < 6; ++i) { |
| 291 uint32_t ts_tl0 = ts + i * kFps10; |
| 292 InsertFrame(pid + i, 0, ts_tl0, false, pid + i - 1); |
| 293 InsertFrame(pid + i, 1, ts_tl0, false, pid + i - 1); |
| 294 } |
| 295 |
| 296 ExtractFrame(); |
| 297 ExtractFrame(); |
| 298 clock_.AdvanceTimeMilliseconds(55); |
| 299 for (int i = 2; i < 12; ++i) { |
| 300 ExtractFrame(); |
| 301 clock_.AdvanceTimeMilliseconds(55); |
| 302 } |
| 303 |
| 304 CheckFrame(0, pid , 0); |
| 305 CheckFrame(1, pid , 1); |
| 306 CheckFrame(2, pid + 1, 0); |
| 307 CheckFrame(3, pid + 1, 1); |
| 308 CheckFrame(4, pid + 2, 0); |
| 309 CheckFrame(5, pid + 2, 1); |
| 310 CheckFrame(6, pid + 3, 0); |
| 311 CheckFrame(7, pid + 4, 0); |
| 312 CheckFrame(8, pid + 5, 0); |
| 313 CheckFrame(9); |
| 314 CheckFrame(10); |
| 315 CheckFrame(11); |
| 316 } |
| 317 |
| 318 |
| 319 TEST_F(TestFrameBuffer2, InsertLateFrame) { |
| 320 uint16_t pid = Rand(); |
| 321 uint32_t ts = Rand(); |
| 322 |
| 323 InsertFrame(pid, 0, ts, false); |
| 324 ExtractFrame(); |
| 325 InsertFrame(pid + 2, 0, ts, false); |
| 326 ExtractFrame(); |
| 327 InsertFrame(pid + 1, 0, ts, false, pid); |
| 328 ExtractFrame(); |
| 329 |
| 330 CheckFrame(0, pid, 0); |
| 331 CheckFrame(1, pid + 2, 0); |
| 332 CheckFrame(2); |
| 333 } |
| 334 |
| 335 } // namespace video_coding |
| 336 } // namespace webrtc |
OLD | NEW |