output/Timer: use std::chrono
This commit is contained in:
		| @@ -33,7 +33,7 @@ Timer::Timer(const AudioFormat af) | |||||||
|  |  | ||||||
| void Timer::Start() | void Timer::Start() | ||||||
| { | { | ||||||
| 	time = MonotonicClockUS(); | 	time = Now(); | ||||||
| 	started = true; | 	started = true; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -49,19 +49,17 @@ Timer::Add(size_t size) | |||||||
|  |  | ||||||
| 	// (size samples) / (rate samples per second) = duration seconds | 	// (size samples) / (rate samples per second) = duration seconds | ||||||
| 	// duration seconds * 1000000 = duration us | 	// 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); | 	assert(started); | ||||||
|  |  | ||||||
| 	int64_t delay = (int64_t)(time - MonotonicClockUS()) / 1000; | 	const auto delay = time - Now(); | ||||||
| 	if (delay < 0) | 	if (delay < Time::zero()) | ||||||
| 		return 0; | 		return Time::zero(); | ||||||
|  |  | ||||||
| 	if (delay > std::numeric_limits<int>::max()) | 	return std::chrono::duration_cast<std::chrono::steady_clock::duration>(delay); | ||||||
| 		delay = std::numeric_limits<int>::max(); |  | ||||||
|  |  | ||||||
| 	return delay; |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -20,13 +20,14 @@ | |||||||
| #ifndef MPD_TIMER_HXX | #ifndef MPD_TIMER_HXX | ||||||
| #define MPD_TIMER_HXX | #define MPD_TIMER_HXX | ||||||
|  |  | ||||||
| #include <stdint.h> | #include <chrono> | ||||||
| #include <stddef.h> |  | ||||||
|  |  | ||||||
| struct AudioFormat; | struct AudioFormat; | ||||||
|  |  | ||||||
| class Timer { | class Timer { | ||||||
| 	uint64_t time; | 	typedef std::chrono::microseconds Time; | ||||||
|  |  | ||||||
|  | 	Time time; | ||||||
| 	bool started = false; | 	bool started = false; | ||||||
| 	const int rate; | 	const int rate; | ||||||
| public: | public: | ||||||
| @@ -40,9 +41,14 @@ public: | |||||||
| 	void Add(size_t size); | 	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 | #endif | ||||||
|   | |||||||
| @@ -208,7 +208,7 @@ inline std::chrono::steady_clock::duration | |||||||
| FifoOutput::Delay() const | FifoOutput::Delay() const | ||||||
| { | { | ||||||
| 	return timer->IsStarted() | 	return timer->IsStarted() | ||||||
| 		? std::chrono::milliseconds(timer->GetDelay()) | 		? timer->GetDelay() | ||||||
| 		: std::chrono::steady_clock::duration::zero(); | 		: std::chrono::steady_clock::duration::zero(); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -51,7 +51,7 @@ public: | |||||||
|  |  | ||||||
| 	std::chrono::steady_clock::duration Delay() const { | 	std::chrono::steady_clock::duration Delay() const { | ||||||
| 		return sync && timer->IsStarted() | 		return sync && timer->IsStarted() | ||||||
| 			? std::chrono::milliseconds(timer->GetDelay()) | 			? timer->GetDelay() | ||||||
| 			: std::chrono::steady_clock::duration::zero(); | 			: std::chrono::steady_clock::duration::zero(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
| @@ -361,7 +361,7 @@ HttpdOutput::Delay() const | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return timer->IsStarted() | 	return timer->IsStarted() | ||||||
| 		? std::chrono::milliseconds(timer->GetDelay()) | 		? timer->GetDelay() | ||||||
| 		: std::chrono::steady_clock::duration::zero(); | 		: std::chrono::steady_clock::duration::zero(); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann