output/Timer: use std::chrono
This commit is contained in:
		| @@ -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); | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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(); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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(); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -361,7 +361,7 @@ HttpdOutput::Delay() const | ||||
| 	} | ||||
|  | ||||
| 	return timer->IsStarted() | ||||
| 		? std::chrono::milliseconds(timer->GetDelay()) | ||||
| 		? timer->GetDelay() | ||||
| 		: std::chrono::steady_clock::duration::zero(); | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann