module/mpd: elapsed and duration are now tracked in millseconds

This commit is contained in:
Daniel Eklöf 2018-12-28 14:06:13 +01:00
parent 3027b6cd9e
commit f74f2ad031

View file

@ -39,10 +39,10 @@ struct private {
char *title; char *title;
struct { struct {
unsigned value; uint64_t value;
time_t when; struct timespec when;
} elapsed; } elapsed;
unsigned duration; uint64_t duration;
int refresh_abort_fd; int refresh_abort_fd;
}; };
@ -67,14 +67,33 @@ destroy(struct module *mod)
module_default_destroy(mod); module_default_destroy(mod);
} }
static uint64_t
timespec_diff_milli_seconds(const struct timespec *a, const struct timespec *b)
{
/* TODO */
uint64_t nsecs_a = a->tv_sec * 1000000000 + a->tv_nsec;
uint64_t nsecs_b = b->tv_sec * 1000000000 + b->tv_nsec;
assert(nsecs_a >= nsecs_b);
uint64_t nsec_diff = nsecs_a - nsecs_b;
return nsec_diff / 1000000;
}
static struct exposable * static struct exposable *
content(struct module *mod) content(struct module *mod)
{ {
struct private *m = mod->private; struct private *m = mod->private;
/* Calculate what elapsed is now */ /* Calculate what elapsed is now */
time_t now = time(NULL);
unsigned elapsed = m->elapsed.value + (now - m->elapsed.when); struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
uint64_t elapsed = m->elapsed.value +
timespec_diff_milli_seconds(&now, &m->elapsed.when);
unsigned elapsed_secs = elapsed / 1000;
unsigned duration_secs = m->duration / 1000;
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
@ -88,22 +107,23 @@ content(struct module *mod)
char pos[16], end[16]; char pos[16], end[16];
if (elapsed >= 60 * 60) if (elapsed_secs >= 60 * 60)
snprintf(pos, sizeof(pos), "%02u:%02u:%02u", snprintf(pos, sizeof(pos), "%02u:%02u:%02u",
elapsed / (60 * 60), elapsed_secs / (60 * 60),
elapsed % (60 * 60) / 60, elapsed_secs % (60 * 60) / 60,
elapsed % 60); elapsed_secs % 60);
else else
snprintf(pos, sizeof(pos), "%02u:%02u", elapsed / 60, elapsed % 60); snprintf(pos, sizeof(pos), "%02u:%02u",
elapsed_secs / 60, elapsed_secs % 60);
if (m->duration >= 60 * 60) if (duration_secs >= 60 * 60)
snprintf(end, sizeof(end), "%02u:%02u:%02u", snprintf(end, sizeof(end), "%02u:%02u:%02u",
m->duration / (60 * 60), duration_secs / (60 * 60),
m->duration % (60 * 60) / 60, duration_secs % (60 * 60) / 60,
m->duration % 60); duration_secs % 60);
else else
snprintf(end, sizeof(end), "%02u:%02u", snprintf(end, sizeof(end), "%02u:%02u",
m->duration / 60, m->duration % 60); duration_secs / 60, duration_secs % 60);
struct tag_set tags = { struct tag_set tags = {
.tags = (struct tag *[]){ .tags = (struct tag *[]){
@ -116,7 +136,7 @@ content(struct module *mod)
tag_new_int(mod, "duration", m->duration), tag_new_int(mod, "duration", m->duration),
tag_new_int_realtime( tag_new_int_realtime(
mod, "elapsed", elapsed, 0, m->duration, mod, "elapsed", elapsed, 0, m->duration,
m->state == STATE_PLAY ? TAG_REALTIME_SECONDS : TAG_REALTIME_NONE), m->state == STATE_PLAY ? TAG_REALTIME_MSECS : TAG_REALTIME_NONE),
}, },
.count = 8, .count = 8,
}; };
@ -165,12 +185,13 @@ update_status(struct module *mod)
return false; return false;
} }
time_t now = time(NULL); struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
mtx_lock(&mod->lock); mtx_lock(&mod->lock);
m->state = mpd_status_get_state(status); m->state = mpd_status_get_state(status);
m->duration = mpd_status_get_total_time(status); m->duration = mpd_status_get_total_time(status) * 1000;
m->elapsed.value = mpd_status_get_elapsed_time(status); m->elapsed.value = mpd_status_get_elapsed_ms(status);
m->elapsed.when = now; m->elapsed.when = now;
mtx_unlock(&mod->lock); mtx_unlock(&mod->lock);
@ -236,7 +257,8 @@ run(struct module_run_context *ctx)
free(m->artist); m->artist = NULL; free(m->artist); m->artist = NULL;
free(m->title); m->title = NULL; free(m->title); m->title = NULL;
m->state = STATE_OFFLINE; m->state = STATE_OFFLINE;
m->elapsed.value = m->elapsed.when = m->duration = 0; m->elapsed.value = m->duration = 0;
m->elapsed.when.tv_sec = m->elapsed.when.tv_nsec = 0;
mtx_unlock(&mod->lock); mtx_unlock(&mod->lock);
/* Keep trying to connect, until we succeed */ /* Keep trying to connect, until we succeed */
@ -422,7 +444,7 @@ module_mpd(const char *host, uint16_t port, struct particle *label)
priv->artist = NULL; priv->artist = NULL;
priv->title = NULL; priv->title = NULL;
priv->elapsed.value = 0; priv->elapsed.value = 0;
priv->elapsed.when = 0; priv->elapsed.when.tv_sec = priv->elapsed.when.tv_nsec = 0;
priv->duration = 0; priv->duration = 0;
priv->refresh_abort_fd = 0; priv->refresh_abort_fd = 0;