output/Timer: use std::chrono

This commit is contained in:
Max Kellermann 2016-12-28 10:11:07 +01:00
parent d5e422970c
commit 71e7d32b08
5 changed files with 22 additions and 18 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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();
}

View File

@ -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();
}

View File

@ -361,7 +361,7 @@ HttpdOutput::Delay() const
}
return timer->IsStarted()
? std::chrono::milliseconds(timer->GetDelay())
? timer->GetDelay()
: std::chrono::steady_clock::duration::zero();
}