diff options
author | Max Kellermann <max@musicpd.org> | 2016-12-28 10:11:07 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2016-12-28 22:00:33 +0100 |
commit | 71e7d32b081aeb049258ab5f8a4752b68b5a39ae (patch) | |
tree | b03e40292bc021009701a0287a0a9ca71042a1ba /src | |
parent | d5e422970c8973947686ae698a366e07840afaa2 (diff) |
output/Timer: use std::chrono
Diffstat (limited to 'src')
-rw-r--r-- | src/output/Timer.cxx | 18 | ||||
-rw-r--r-- | src/output/Timer.hxx | 16 | ||||
-rw-r--r-- | src/output/plugins/FifoOutputPlugin.cxx | 2 | ||||
-rw-r--r-- | src/output/plugins/NullOutputPlugin.cxx | 2 | ||||
-rw-r--r-- | src/output/plugins/httpd/HttpdOutputPlugin.cxx | 2 |
5 files changed, 22 insertions, 18 deletions
diff --git a/src/output/Timer.cxx b/src/output/Timer.cxx index 1207100f0..6567f7464 100644 --- a/src/output/Timer.cxx +++ b/src/output/Timer.cxx @@ -33,7 +33,7 @@ Timer::Timer(const AudioFormat af) void Timer::Start() { - time = MonotonicClockUS(); + time = Now(); started = true; } @@ -49,19 +49,17 @@ Timer::Add(size_t size) // (size samples) / (rate samples per second) = duration seconds // duration seconds * 1000000 = duration us - time += ((uint64_t)size * 1000000) / rate; + time += Time(((uint64_t)size * Time::period::den) / (Time::period::num * rate)); } -unsigned Timer::GetDelay() const +std::chrono::steady_clock::duration +Timer::GetDelay() const { assert(started); - int64_t delay = (int64_t)(time - MonotonicClockUS()) / 1000; - if (delay < 0) - return 0; + const auto delay = time - Now(); + if (delay < Time::zero()) + return Time::zero(); - if (delay > std::numeric_limits<int>::max()) - delay = std::numeric_limits<int>::max(); - - return delay; + return std::chrono::duration_cast<std::chrono::steady_clock::duration>(delay); } diff --git a/src/output/Timer.hxx b/src/output/Timer.hxx index 40665a07f..0dfabcf35 100644 --- a/src/output/Timer.hxx +++ b/src/output/Timer.hxx @@ -20,13 +20,14 @@ #ifndef MPD_TIMER_HXX #define MPD_TIMER_HXX -#include <stdint.h> -#include <stddef.h> +#include <chrono> struct AudioFormat; class Timer { - uint64_t time; + typedef std::chrono::microseconds Time; + + Time time; bool started = false; const int rate; public: @@ -40,9 +41,14 @@ public: void Add(size_t size); /** - * Returns the number of milliseconds to sleep to get back to sync. + * Returns the duration to sleep to get back to sync. */ - unsigned GetDelay() const; + std::chrono::steady_clock::duration GetDelay() const; + +private: + static Time Now() { + return std::chrono::duration_cast<Time>(std::chrono::steady_clock::now().time_since_epoch()); + } }; #endif diff --git a/src/output/plugins/FifoOutputPlugin.cxx b/src/output/plugins/FifoOutputPlugin.cxx index 24105441b..aef881be8 100644 --- a/src/output/plugins/FifoOutputPlugin.cxx +++ b/src/output/plugins/FifoOutputPlugin.cxx @@ -208,7 +208,7 @@ inline std::chrono::steady_clock::duration FifoOutput::Delay() const { return timer->IsStarted() - ? std::chrono::milliseconds(timer->GetDelay()) + ? timer->GetDelay() : std::chrono::steady_clock::duration::zero(); } diff --git a/src/output/plugins/NullOutputPlugin.cxx b/src/output/plugins/NullOutputPlugin.cxx index b2b3ec951..984a02549 100644 --- a/src/output/plugins/NullOutputPlugin.cxx +++ b/src/output/plugins/NullOutputPlugin.cxx @@ -51,7 +51,7 @@ public: std::chrono::steady_clock::duration Delay() const { return sync && timer->IsStarted() - ? std::chrono::milliseconds(timer->GetDelay()) + ? timer->GetDelay() : std::chrono::steady_clock::duration::zero(); } diff --git a/src/output/plugins/httpd/HttpdOutputPlugin.cxx b/src/output/plugins/httpd/HttpdOutputPlugin.cxx index 8d9c3112e..0d0433e32 100644 --- a/src/output/plugins/httpd/HttpdOutputPlugin.cxx +++ b/src/output/plugins/httpd/HttpdOutputPlugin.cxx @@ -361,7 +361,7 @@ HttpdOutput::Delay() const } return timer->IsStarted() - ? std::chrono::milliseconds(timer->GetDelay()) + ? timer->GetDelay() : std::chrono::steady_clock::duration::zero(); } |