// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine Master // License Agreement. #include "test_sleep.h" #include #include #include "clock.h" namespace wvcdm { bool TestSleep::real_sleep_ = true; TestSleep::CallBack* TestSleep::callback_ = nullptr; void TestSleep::Sleep(unsigned int seconds) { int64_t milliseconds = 1000 * seconds; if (real_sleep_) { // This next bit of logic is to avoid slow drift apart of the real clock and // the fake clock. We compute how far from the real clock has advanced in // total since the start, and then compare to a running total of sleep // calls. We sleep for approximately x second, and then advance the clock by // the amount of time that has actually passed. static auto start_real = std::chrono::steady_clock().now(); static int64_t fake_clock = 0; sleep(seconds); auto now_real = std::chrono::steady_clock().now(); int64_t total_real = (now_real - start_real) / std::chrono::milliseconds(1); // We want to advance the fake clock by the difference between the real // clock, and the previous value on the fake clock. milliseconds = total_real - fake_clock; fake_clock += milliseconds; } if (callback_ != nullptr) callback_->ElapseTime(milliseconds); } void TestSleep::SyncFakeClock() { // Syncing can be done by sleeping 0 seconds. Sleep(0); } } // namespace wvcdm