timer: convert to class
This commit is contained in:
parent
21dac6c05d
commit
49a3845135
@ -94,7 +94,7 @@ mpd_headers = \
|
|||||||
src/tag_ape.h \
|
src/tag_ape.h \
|
||||||
src/tag_id3.h \
|
src/tag_id3.h \
|
||||||
src/tag_rva2.h \
|
src/tag_rva2.h \
|
||||||
src/timer.h \
|
src/Timer.hxx \
|
||||||
src/mpd_error.h
|
src/mpd_error.h
|
||||||
|
|
||||||
src_mpd_SOURCES = \
|
src_mpd_SOURCES = \
|
||||||
@ -243,7 +243,7 @@ src_mpd_SOURCES = \
|
|||||||
src/SongFilter.cxx src/SongFilter.hxx \
|
src/SongFilter.cxx src/SongFilter.hxx \
|
||||||
src/SongPointer.hxx \
|
src/SongPointer.hxx \
|
||||||
src/PlaylistFile.cxx src/PlaylistFile.hxx \
|
src/PlaylistFile.cxx src/PlaylistFile.hxx \
|
||||||
src/timer.c
|
src/Timer.cxx
|
||||||
|
|
||||||
#
|
#
|
||||||
# Windows resource file
|
# Windows resource file
|
||||||
@ -1299,7 +1299,7 @@ test_run_output_SOURCES = test/run_output.cxx \
|
|||||||
src/audio_check.c \
|
src/audio_check.c \
|
||||||
src/audio_format.c \
|
src/audio_format.c \
|
||||||
src/AudioParser.cxx \
|
src/AudioParser.cxx \
|
||||||
src/timer.c src/clock.c \
|
src/Timer.cxx src/clock.c \
|
||||||
src/Tag.cxx src/TagNames.c src/TagPool.cxx \
|
src/Tag.cxx src/TagNames.c src/TagPool.cxx \
|
||||||
src/Page.cxx \
|
src/Page.cxx \
|
||||||
src/SocketUtil.cxx \
|
src/SocketUtil.cxx \
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "timer.h"
|
#include "Timer.hxx"
|
||||||
#include "audio_format.h"
|
#include "audio_format.h"
|
||||||
#include "clock.h"
|
#include "clock.h"
|
||||||
|
|
||||||
@ -28,46 +28,37 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct timer *timer_new(const struct audio_format *af)
|
Timer::Timer(const struct audio_format &af)
|
||||||
|
: time(0),
|
||||||
|
started(false),
|
||||||
|
rate(af.sample_rate * audio_format_frame_size(&af))
|
||||||
{
|
{
|
||||||
struct timer *timer = g_new(struct timer, 1);
|
|
||||||
timer->time = 0; // us
|
|
||||||
timer->started = 0; // false
|
|
||||||
timer->rate = af->sample_rate * audio_format_frame_size(af); // samples per second
|
|
||||||
|
|
||||||
return timer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer_free(struct timer *timer)
|
void Timer::Start()
|
||||||
{
|
{
|
||||||
g_free(timer);
|
time = monotonic_clock_us();
|
||||||
|
started = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer_start(struct timer *timer)
|
void Timer::Reset()
|
||||||
{
|
{
|
||||||
timer->time = monotonic_clock_us();
|
time = 0;
|
||||||
timer->started = 1;
|
started = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer_reset(struct timer *timer)
|
void Timer::Add(int size)
|
||||||
{
|
{
|
||||||
timer->time = 0;
|
assert(started);
|
||||||
timer->started = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void timer_add(struct timer *timer, int size)
|
|
||||||
{
|
|
||||||
assert(timer->started);
|
|
||||||
|
|
||||||
// (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
|
||||||
timer->time += ((uint64_t)size * 1000000) / timer->rate;
|
time += ((uint64_t)size * 1000000) / rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned
|
unsigned Timer::GetDelay() const
|
||||||
timer_delay(const struct timer *timer)
|
|
||||||
{
|
{
|
||||||
int64_t delay = (int64_t)(timer->time - monotonic_clock_us()) / 1000;
|
int64_t delay = (int64_t)(time - monotonic_clock_us()) / 1000;
|
||||||
if (delay < 0)
|
if (delay < 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -77,13 +68,13 @@ timer_delay(const struct timer *timer)
|
|||||||
return delay;
|
return delay;
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer_sync(struct timer *timer)
|
void Timer::Synchronize() const
|
||||||
{
|
{
|
||||||
int64_t sleep_duration;
|
int64_t sleep_duration;
|
||||||
|
|
||||||
assert(timer->started);
|
assert(started);
|
||||||
|
|
||||||
sleep_duration = timer->time - monotonic_clock_us();
|
sleep_duration = time - monotonic_clock_us();
|
||||||
if (sleep_duration > 0)
|
if (sleep_duration > 0)
|
||||||
g_usleep(sleep_duration);
|
g_usleep(sleep_duration);
|
||||||
}
|
}
|
@ -17,43 +17,33 @@
|
|||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef MPD_TIMER_H
|
#ifndef MPD_TIMER_HXX
|
||||||
#define MPD_TIMER_H
|
#define MPD_TIMER_HXX
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct audio_format;
|
struct audio_format;
|
||||||
|
|
||||||
struct timer {
|
class Timer {
|
||||||
uint64_t time;
|
uint64_t time;
|
||||||
int started;
|
bool started;
|
||||||
int rate;
|
const int rate;
|
||||||
|
public:
|
||||||
|
explicit Timer(const struct audio_format& af);
|
||||||
|
|
||||||
|
bool IsStarted() const { return started; }
|
||||||
|
|
||||||
|
void Start();
|
||||||
|
void Reset();
|
||||||
|
|
||||||
|
void Add(int size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of milliseconds to sleep to get back to sync.
|
||||||
|
*/
|
||||||
|
unsigned GetDelay() const;
|
||||||
|
|
||||||
|
void Synchronize() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct timer *timer_new(const struct audio_format *af);
|
|
||||||
|
|
||||||
void timer_free(struct timer *timer);
|
|
||||||
|
|
||||||
void timer_start(struct timer *timer);
|
|
||||||
|
|
||||||
void timer_reset(struct timer *timer);
|
|
||||||
|
|
||||||
void timer_add(struct timer *timer, int size);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the number of milliseconds to sleep to get back to sync.
|
|
||||||
*/
|
|
||||||
unsigned
|
|
||||||
timer_delay(const struct timer *timer);
|
|
||||||
|
|
||||||
void timer_sync(struct timer *timer);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -24,6 +24,10 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the value of a monotonic clock in milliseconds.
|
* Returns the value of a monotonic clock in milliseconds.
|
||||||
*/
|
*/
|
||||||
@ -38,4 +42,8 @@ gcc_pure
|
|||||||
uint64_t
|
uint64_t
|
||||||
monotonic_clock_us(void);
|
monotonic_clock_us(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "FifoOutputPlugin.hxx"
|
#include "FifoOutputPlugin.hxx"
|
||||||
#include "output_api.h"
|
#include "output_api.h"
|
||||||
#include "timer.h"
|
#include "Timer.hxx"
|
||||||
#include "fd_util.h"
|
#include "fd_util.h"
|
||||||
#include "open.h"
|
#include "open.h"
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ struct FifoOutput {
|
|||||||
int input;
|
int input;
|
||||||
int output;
|
int output;
|
||||||
bool created;
|
bool created;
|
||||||
struct timer *timer;
|
Timer *timer;
|
||||||
|
|
||||||
FifoOutput()
|
FifoOutput()
|
||||||
:path(nullptr), input(-1), output(-1), created(false) {}
|
:path(nullptr), input(-1), output(-1), created(false) {}
|
||||||
@ -232,7 +232,7 @@ fifo_output_open(struct audio_output *ao, struct audio_format *audio_format,
|
|||||||
{
|
{
|
||||||
FifoOutput *fd = (FifoOutput *)ao;
|
FifoOutput *fd = (FifoOutput *)ao;
|
||||||
|
|
||||||
fd->timer = timer_new(audio_format);
|
fd->timer = new Timer(*audio_format);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -242,7 +242,7 @@ fifo_output_close(struct audio_output *ao)
|
|||||||
{
|
{
|
||||||
FifoOutput *fd = (FifoOutput *)ao;
|
FifoOutput *fd = (FifoOutput *)ao;
|
||||||
|
|
||||||
timer_free(fd->timer);
|
delete fd->timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -252,7 +252,7 @@ fifo_output_cancel(struct audio_output *ao)
|
|||||||
char buf[FIFO_BUFFER_SIZE];
|
char buf[FIFO_BUFFER_SIZE];
|
||||||
int bytes = 1;
|
int bytes = 1;
|
||||||
|
|
||||||
timer_reset(fd->timer);
|
fd->timer->Reset();
|
||||||
|
|
||||||
while (bytes > 0 && errno != EINTR)
|
while (bytes > 0 && errno != EINTR)
|
||||||
bytes = read(fd->input, buf, FIFO_BUFFER_SIZE);
|
bytes = read(fd->input, buf, FIFO_BUFFER_SIZE);
|
||||||
@ -268,8 +268,8 @@ fifo_output_delay(struct audio_output *ao)
|
|||||||
{
|
{
|
||||||
FifoOutput *fd = (FifoOutput *)ao;
|
FifoOutput *fd = (FifoOutput *)ao;
|
||||||
|
|
||||||
return fd->timer->started
|
return fd->timer->IsStarted()
|
||||||
? timer_delay(fd->timer)
|
? fd->timer->GetDelay()
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,9 +280,9 @@ fifo_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
|||||||
FifoOutput *fd = (FifoOutput *)ao;
|
FifoOutput *fd = (FifoOutput *)ao;
|
||||||
ssize_t bytes;
|
ssize_t bytes;
|
||||||
|
|
||||||
if (!fd->timer->started)
|
if (!fd->timer->IsStarted())
|
||||||
timer_start(fd->timer);
|
fd->timer->Start();
|
||||||
timer_add(fd->timer, size);
|
fd->timer->Add(size);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
bytes = write(fd->output, chunk, size);
|
bytes = write(fd->output, chunk, size);
|
||||||
|
@ -26,7 +26,7 @@
|
|||||||
#define MPD_OUTPUT_HTTPD_INTERNAL_H
|
#define MPD_OUTPUT_HTTPD_INTERNAL_H
|
||||||
|
|
||||||
#include "OutputInternal.hxx"
|
#include "OutputInternal.hxx"
|
||||||
#include "timer.h"
|
#include "Timer.hxx"
|
||||||
#include "thread/Mutex.hxx"
|
#include "thread/Mutex.hxx"
|
||||||
#include "event/ServerSocket.hxx"
|
#include "event/ServerSocket.hxx"
|
||||||
|
|
||||||
@ -72,10 +72,10 @@ struct HttpdOutput final : private ServerSocket {
|
|||||||
mutable Mutex mutex;
|
mutable Mutex mutex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A #timer object to synchronize this output with the
|
* A #Timer object to synchronize this output with the
|
||||||
* wallclock.
|
* wallclock.
|
||||||
*/
|
*/
|
||||||
struct timer *timer;
|
Timer *timer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The header page, which is sent to every client on connect.
|
* The header page, which is sent to every client on connect.
|
||||||
|
@ -320,7 +320,7 @@ HttpdOutput::Open(struct audio_format *audio_format, GError **error_r)
|
|||||||
/* initialize other attributes */
|
/* initialize other attributes */
|
||||||
|
|
||||||
clients_cnt = 0;
|
clients_cnt = 0;
|
||||||
timer = timer_new(audio_format);
|
timer = new Timer(*audio_format);
|
||||||
|
|
||||||
open = true;
|
open = true;
|
||||||
|
|
||||||
@ -346,7 +346,7 @@ HttpdOutput::Close()
|
|||||||
|
|
||||||
open = false;
|
open = false;
|
||||||
|
|
||||||
timer_free(timer);
|
delete timer;
|
||||||
|
|
||||||
clients.clear();
|
clients.clear();
|
||||||
|
|
||||||
@ -398,7 +398,7 @@ httpd_output_delay(struct audio_output *ao)
|
|||||||
then httpd_output_pause() will not do anything, it
|
then httpd_output_pause() will not do anything, it
|
||||||
will not fill the buffer and it will not update the
|
will not fill the buffer and it will not update the
|
||||||
timer; therefore, we reset the timer here */
|
timer; therefore, we reset the timer here */
|
||||||
timer_reset(httpd->timer);
|
httpd->timer->Reset();
|
||||||
|
|
||||||
/* some arbitrary delay that is long enough to avoid
|
/* some arbitrary delay that is long enough to avoid
|
||||||
consuming too much CPU, and short enough to notice
|
consuming too much CPU, and short enough to notice
|
||||||
@ -406,8 +406,8 @@ httpd_output_delay(struct audio_output *ao)
|
|||||||
return 1000;
|
return 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpd->timer->started
|
return httpd->timer->IsStarted()
|
||||||
? timer_delay(httpd->timer)
|
? httpd->timer->GetDelay()
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -463,9 +463,9 @@ httpd_output_play(struct audio_output *ao, const void *chunk, size_t size,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!httpd->timer->started)
|
if (!httpd->timer->IsStarted())
|
||||||
timer_start(httpd->timer);
|
httpd->timer->Start();
|
||||||
timer_add(httpd->timer, size);
|
httpd->timer->Add(size);
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "NullOutputPlugin.hxx"
|
#include "NullOutputPlugin.hxx"
|
||||||
#include "output_api.h"
|
#include "output_api.h"
|
||||||
#include "timer.h"
|
#include "Timer.hxx"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ struct NullOutput {
|
|||||||
|
|
||||||
bool sync;
|
bool sync;
|
||||||
|
|
||||||
struct timer *timer;
|
Timer *timer;
|
||||||
|
|
||||||
bool Initialize(const config_param *param, GError **error_r) {
|
bool Initialize(const config_param *param, GError **error_r) {
|
||||||
return ao_base_init(&base, &null_output_plugin, param,
|
return ao_base_init(&base, &null_output_plugin, param,
|
||||||
@ -72,7 +72,7 @@ null_open(struct audio_output *ao, struct audio_format *audio_format,
|
|||||||
NullOutput *nd = (NullOutput *)ao;
|
NullOutput *nd = (NullOutput *)ao;
|
||||||
|
|
||||||
if (nd->sync)
|
if (nd->sync)
|
||||||
nd->timer = timer_new(audio_format);
|
nd->timer = new Timer(*audio_format);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -83,7 +83,7 @@ null_close(struct audio_output *ao)
|
|||||||
NullOutput *nd = (NullOutput *)ao;
|
NullOutput *nd = (NullOutput *)ao;
|
||||||
|
|
||||||
if (nd->sync)
|
if (nd->sync)
|
||||||
timer_free(nd->timer);
|
delete nd->timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned
|
static unsigned
|
||||||
@ -91,8 +91,8 @@ null_delay(struct audio_output *ao)
|
|||||||
{
|
{
|
||||||
NullOutput *nd = (NullOutput *)ao;
|
NullOutput *nd = (NullOutput *)ao;
|
||||||
|
|
||||||
return nd->sync && nd->timer->started
|
return nd->sync && nd->timer->IsStarted()
|
||||||
? timer_delay(nd->timer)
|
? nd->timer->GetDelay()
|
||||||
: 0;
|
: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,14 +101,14 @@ null_play(struct audio_output *ao, gcc_unused const void *chunk, size_t size,
|
|||||||
gcc_unused GError **error)
|
gcc_unused GError **error)
|
||||||
{
|
{
|
||||||
NullOutput *nd = (NullOutput *)ao;
|
NullOutput *nd = (NullOutput *)ao;
|
||||||
struct timer *timer = nd->timer;
|
Timer *timer = nd->timer;
|
||||||
|
|
||||||
if (!nd->sync)
|
if (!nd->sync)
|
||||||
return size;
|
return size;
|
||||||
|
|
||||||
if (!timer->started)
|
if (!timer->IsStarted())
|
||||||
timer_start(timer);
|
timer->Start();
|
||||||
timer_add(timer, size);
|
timer->Add(size);
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
@ -121,7 +121,7 @@ null_cancel(struct audio_output *ao)
|
|||||||
if (!nd->sync)
|
if (!nd->sync)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
timer_reset(nd->timer);
|
nd->timer->Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
const struct audio_output_plugin null_output_plugin = {
|
const struct audio_output_plugin null_output_plugin = {
|
||||||
|
Loading…
Reference in New Issue
Block a user