summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndre Noll <maan@tuebingen.mpg.de>2026-11-23 23:56:00 +0100
committerAndre Noll <maan@tuebingen.mpg.de>2026-11-23 23:58:33 +0100
commit0c03d5e41026717f25288ca1e7af86e7c576b91d (patch)
tree44b078914a1f394c0edc51e805a52b3c7905f969
parentcbbe3abf2eca134b9d539f8b92345410d7964b9a (diff)
parentcaafd2c1688f68e4782f37b7d4f3ff49578d667a (diff)
Merge topic branch t/audiod into masterHEADmaster
para_audiod improvements ------------------------ This fixes an old bug that crashes para_audiod, removes a bunch of unneeded cruft from audiod.c and has a few other audiod-related goodies. * t/audiod: audiod: Improve SI_decoder_flags handling. audiod: Reduce time string update frequency. audiod: Improve SI_audiod_status handling. audiod: Improve and rename close_stat_pipe(). audiod: Avoid unnecessary allocation/free. audiod: Only check for connections if socket is readable. audiod: Drop unnecessary call to clear_status_items(). audiod: Rename audiod_status_dump() -> dump_audiod_status_items(). audiod: Merge audiod_status_string() into its single caller. audiod: Move two functions and make them static. audiod: Fail if the local socket cannot be created. audiod: Remove compute_time_diff(). audiod: Fix --mode.
-rw-r--r--NEWS.md6
-rw-r--r--audiod.c247
-rw-r--r--audiod.h7
-rw-r--r--audiod_command.c78
4 files changed, 126 insertions, 212 deletions
diff --git a/NEWS.md b/NEWS.md
index 103b3996..8dcbe53b 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,6 +1,12 @@
NEWS
====
+--------------------------------------------
+0.8.5 (20xx-xx-xx) "immediate incandescence"
+--------------------------------------------
+
+- Substantial audiod cleanup, fixes and improvements.
+
--------------------------------
0.8.4 (2026-07-22) "frozen lava"
--------------------------------
diff --git a/audiod.c b/audiod.c
index 7665c838..9ad875a2 100644
--- a/audiod.c
+++ b/audiod.c
@@ -154,10 +154,6 @@ static struct status_task {
int length_seconds;
/* The start of the current stream from the view of para_server. */
struct timeval server_stream_start;
- /* The average time deviation between para_server and para_audiod. */
- struct timeval sa_time_diff;
- /* Whether client time is ahead of server time. */
- int sa_time_diff_sign;
/* The 'P' and the 'N' flags as announced by para_server. */
enum vss_status_flags vss_status;
/* The status task btrn is the child of the client task. */
@@ -202,33 +198,28 @@ static int get_audio_format_num(void)
return -E_UNSUPPORTED_AUDIO_FORMAT;
}
-/**
- * Return the flags for the decoder_flags status item.
- *
- * Allocates a string which contains one octal digit per slot. Bit zero (value
- * 1) is set if a receiver is active. Bit one (value 2) and bit three (value 4)
- * have the analogous meaning for filter and writer, respectively.
- *
- * \return String that must be freed by the caller.
+/*
+ * One octal digit per slot. The value (0-7) indicates whether receiver (1),
+ * filter (2), writer (4) is active in this slot.
*/
-__malloc char *audiod_get_decoder_flags(void)
+static void audiod_set_decoder_flags(struct slot_info *s)
{
- int i;
- char flags[MAX_STREAM_SLOTS + 1];
+ char flag = '0', *p = stat_item_values[SI_decoder_flags];
- FOR_EACH_SLOT(i) {
- struct slot_info *s = &slot[i];
- char flag = '0';
- if (s->receiver_node)
- flag += 1;
- if (s->fns)
- flag += 2;
- if (s->wns)
- flag += 4;
- flags[i] = flag;
+ if (!p) {
+ p = alloc(MAX_STREAM_SLOTS + 1);
+ memset(p, '0', MAX_STREAM_SLOTS); /* '0', not '\0' */
+ p[MAX_STREAM_SLOTS] = '\0';
+ stat_item_values[SI_decoder_flags] = p;
}
- flags[MAX_STREAM_SLOTS] = '\0';
- return para_strdup(flags);
+ if (s->receiver_node)
+ flag += 1;
+ if (s->fns)
+ flag += 2;
+ if (s->wns)
+ flag += 4;
+ p[s - slot] = flag;
+ stat_client_write_item(SI_decoder_flags);
}
struct slot_info *get_play_time_slot(void)
@@ -251,26 +242,22 @@ struct slot_info *get_play_time_slot(void)
return oldest_slot;
}
-/**
+static struct timeval status_dump_barrier;
+
+/*
* Compute the play time based on information of the current slot.
*
- * This computes a string of the form "0:07 [3:33] (3%/3:40)" using information
- * from the status items received from para_server and the start time of the
- * (first) writer of the current slot.
- *
- * It has to take into account that the receiver might have been started in
- * the middle of the stream and that playback is delayed due to the prebuffer
- * filter. If audiod operates in standby mode, no writer is active. In this
- * case an approximation based only on the status items is computed and the
- * returned string is prefixed with "~".
+ * Compute a string of the form "0:07 [3:33] (3%/3:40)". If there is a writer
+ * in any slot, use its start time. If audiod operates in standby mode,
+ * fall back to an approximation that is based on the status items only and
+ * prefix the returned string with "~".
*
- * \return A string as described above on success, the empty string if no
+ * Returns a string as described above on success, the empty string if no
* stream is currently announced or if para_audiod is running in "off" mode,
- * NULL if the stream is currently paused or repositioned. The memory
- * for the returned string is allocated on the heap and must be freed by
- * the caller.
+ * NULL if the stream is currently paused or repositioned. The memory for
+ * the returned string is allocated on the heap and must be freed by the caller.
*/
-__malloc char *get_time_string(void)
+static __malloc char *get_time_string(void)
{
int ret, seconds = 0, length = stat_task->length_seconds;
struct timeval *sss = &stat_task->server_stream_start, tmp, wstime;
@@ -310,6 +297,12 @@ __malloc char *get_time_string(void)
else
tv_diff(now, &wstime, &tmp);
seconds += tmp.tv_sec + (tmp.tv_usec + 500 * 1000) / 1000 / 1000;
+ /* compute time of next time string change */
+ if (tmp.tv_usec / 1000 > 500)
+ ms2tv(1500 - tmp.tv_usec / 1000, &tmp);
+ else
+ ms2tv(500 - tmp.tv_usec / 1000, &tmp);
+ tv_add(now, &tmp, &status_dump_barrier);
out:
seconds = PARA_MIN(seconds, length);
seconds = PARA_MAX(seconds, 0);
@@ -648,43 +641,34 @@ static bool must_start_decoder(int *num)
return true;
}
-static void compute_time_diff(const struct timeval *status_time)
+/*
+ * Send the current audiod status to all connected stat clients. If the
+ * force flag is given, also write unchanged items.
+ */
+static void dump_audiod_status_items(bool force)
{
- struct timeval tmp, diff;
- static unsigned count;
- int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
- const struct timeval max_deviation = {0, 500 * 1000};
- const int time_smooth = 5;
-
- sign = tv_diff(status_time, now, &diff);
-// PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
-// sign, sa_time_diff_sign);
- if (!count) {
- sa_time_diff_sign = sign;
- stat_task->sa_time_diff = diff;
- count++;
- goto out;
- }
- if (count > 5) {
- int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
- if (tv_diff(&max_deviation, &tmp, NULL) < 0)
- PARA_WARNING_LOG("time diff jump: %c%lums\n",
- s < 0? '-' : '+', tv2ms(&tmp));
+ char *old, *new;
+
+ if (tv_diff(now, &status_dump_barrier, NULL) > 0) {
+ status_dump_barrier = *now;
+ status_dump_barrier.tv_sec += 1;
+ old = stat_item_values[SI_play_time];
+ new = get_time_string(); /* may overwrite status_dump_barrier */
+ if (new && (force || !old || strcmp(old, new))) {
+ free(old);
+ stat_item_values[SI_play_time] = new;
+ stat_client_write_item(SI_play_time);
+ }
}
- count++;
- sa_time_diff_sign = tv_convex_combination(
- sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
- count > 10? sign : sign * time_smooth, &diff,
- &tmp);
- stat_task->sa_time_diff = tmp;
- PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
- sign < 0? "-" : "+",
- tv2ms(&diff),
- sa_time_diff_sign < 0? "-" : "+",
- tv2ms(&stat_task->sa_time_diff)
- );
-out:
- stat_task->sa_time_diff_sign = sa_time_diff_sign;
+
+ new = daemon_get_uptime_str(now);
+ old = stat_item_values[SI_audiod_uptime];
+ if (force || !old || strcmp(old, new)) {
+ free(old);
+ stat_item_values[SI_audiod_uptime] = new;
+ stat_client_write_item(SI_audiod_uptime);
+ } else
+ free(new);
}
static void update_items(uint64_t mask)
@@ -713,25 +697,14 @@ static void update_items(uint64_t mask)
break;
case SI_stream_start:
if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
- struct timeval tmp = {.tv_sec = sec, .tv_usec = usec};
- if (stat_task->sa_time_diff_sign > 0)
- tv_diff(&tmp, &stat_task->sa_time_diff,
- &stat_task->server_stream_start);
- else
- tv_add(&tmp, &stat_task->sa_time_diff,
- &stat_task->server_stream_start);
- }
- break;
- case SI_current_time:
- if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
- struct timeval tv = {sec, usec};
- compute_time_diff(&tv);
+ stat_task->server_stream_start.tv_sec = sec;
+ stat_task->server_stream_start.tv_usec = usec;
}
break;
}
}
if (status_item_bit_set(SI_offset, mask))
- audiod_status_dump(false);
+ dump_audiod_status_items(false);
}
static int add_filter(int format, const char *cmdline)
@@ -1014,19 +987,19 @@ static void command_pre_monitor(struct sched *s, void *context)
{
struct command_task *ct = context;
sched_monitor_readfd(ct->fd, s);
+ sched_request_barrier(&status_dump_barrier, sched);
}
static int command_post_monitor(struct sched *s, void *context)
{
int ret;
struct command_task *ct = context;
- static struct timeval last_status_dump;
- struct timeval tmp, delay;
ret = task_get_notification(ct->task);
if (ret < 0)
return ret;
- ret = dispatch_local_connection(ct->fd);
+ if (sched_read_ok(ct->fd, s))
+ ret = dispatch_local_connection(ct->fd);
if (ret < 0) {
PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
if (ret == -E_AUDIOD_TERM) {
@@ -1034,29 +1007,11 @@ static int command_post_monitor(struct sched *s, void *context)
return ret;
}
}
- /* if last status dump was less than 50ms ago, do nothing */
- delay.tv_sec = 0;
- delay.tv_usec = 50 * 1000;
- tv_add(&last_status_dump, &delay, &tmp);
- if (tv_diff(now, &tmp, NULL) < 0)
- return 0;
- audiod_status_dump(false);
- last_status_dump = *now;
+ dump_audiod_status_items(false);
return 1;
}
-/* Free all status items and reset the corresponding pointers. */
-static void clear_status_items(void)
-{
- int i;
-
- FOR_EACH_STATUS_ITEM(i) {
- free(stat_item_values[i]);
- stat_item_values[i] = NULL;
- }
-}
-
-static void close_stat_pipe(void)
+static void close_status_connection(void)
{
int i;
@@ -1065,13 +1020,22 @@ static void close_stat_pipe(void)
task_reap(&stat_task->ct->task);
client_close(stat_task->ct);
stat_task->ct = NULL;
- clear_status_items();
+ FOR_EACH_STATUS_ITEM(i) {
+ switch (i) {
+ case SI_decoder_flags:
+ case SI_audiod_status:
+ case SI_play_time:
+ case SI_audiod_uptime:
+ continue;
+ }
+ free(stat_item_values[i]);
+ stat_item_values[i] = NULL;
+ stat_client_write_item(i);
+ }
stat_task->length_seconds = 0;
stat_task->offset_seconds = 0;
stat_task->vss_status = 0;
- FOR_EACH_STATUS_ITEM(i)
- stat_client_write_item(i);
- audiod_status_dump(true);
+ dump_audiod_status_items(true);
}
static bool must_close_slot(int slot_num)
@@ -1112,10 +1076,11 @@ static void close_unused_slots(void)
close_filters(s);
close_receiver(i);
clear_slot(i);
+ audiod_set_decoder_flags(s);
dump = true;
}
if (dump)
- audiod_status_dump(true);
+ dump_audiod_status_items(true);
}
static void audiod_cleanup(void)
@@ -1143,11 +1108,14 @@ static void audiod_cleanup(void)
free(a->writer_lpr);
lls_free_parse_result(a->receiver_lpr, RECEIVER_CMD(a));
}
- close_stat_pipe();
+ close_status_connection();
close_unused_slots();
close_stat_clients();
- clear_status_items();
free(uid_whitelist);
+ free(stat_item_values[SI_decoder_flags]);
+ free(stat_item_values[SI_audiod_status]);
+ free(stat_item_values[SI_play_time]);
+ free(stat_item_values[SI_audiod_uptime]);
}
/*
@@ -1176,7 +1144,8 @@ static void start_stop_decoders(void)
open_writers(sl);
activate_grab_clients(sched);
btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
- audiod_status_dump(true);
+ dump_audiod_status_items(true);
+ audiod_set_decoder_flags(sl);
}
static void status_pre_monitor(struct sched *s, void *context)
@@ -1223,7 +1192,7 @@ static int status_post_monitor(struct sched *s, void *context)
task_notify(st->ct->task, E_AUDIOD_OFF);
goto out;
}
- close_stat_pipe();
+ close_status_connection();
goto out;
}
if (st->ct) {
@@ -1233,7 +1202,7 @@ static int status_post_monitor(struct sched *s, void *context)
ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
if (ret < 0) {
- close_stat_pipe();
+ close_status_connection();
goto out;
}
if (st->ct->status != CL_EXECUTING)
@@ -1277,7 +1246,6 @@ static int init_tasks_and_schedule(void)
int ret;
sched = sched_new(NULL);
- stat_task->sa_time_diff_sign = 1;
stat_task->btrn = btr_new_node(&(struct btr_node_description)
EMBRACE(.name = "stat"));
if (OPT_GIVEN(CLOCK_DIFF_COUNT))
@@ -1308,29 +1276,14 @@ static int init_tasks_and_schedule(void)
.post_monitor = signal_post_monitor,
.context = &signal_task,
}, sched);
+ if (OPT_GIVEN(DAEMON))
+ daemonize(false /* parent exits immediately */);
ret = schedule(sched);
unlink(socket_name);
free(socket_name);
return ret;
}
-static void set_initial_status(void)
-{
- audiod_status = AUDIOD_ON;
- if (!OPT_GIVEN(MODE))
- return;
- if (!strcmp(OPT_STRING_VAL(MODE), "sb")) {
- audiod_status = AUDIOD_STANDBY;
- return;
- }
- if (!strcmp(OPT_STRING_VAL(MODE), "off")) {
- audiod_status = AUDIOD_OFF;
- return;
- }
- if (strcmp(OPT_STRING_VAL(MODE), "on"))
- PARA_WARNING_LOG("invalid mode\n");
-}
-
/**
* Lookup the given UID in the whitelist.
*
@@ -1423,11 +1376,13 @@ int main(int argc, char *argv[])
parse_stream_args();
daemon_log_welcome("audiod");
daemon_set_start_time();
- set_initial_status();
- FOR_EACH_SLOT(i)
+ audiod_status = OPT_UINT32_VAL(MODE);
+ stat_item_values[SI_audiod_status] =
+ para_strdup(mode_string(audiod_status));
+ FOR_EACH_SLOT(i) {
clear_slot(i);
- if (OPT_GIVEN(DAEMON))
- daemonize(false /* parent exits immediately */);
+ audiod_set_decoder_flags(slot + i);
+ }
ret = init_tasks_and_schedule();
audiod_cleanup();
sched_shutdown(sched);
diff --git a/audiod.h b/audiod.h
index ef09a80a..e90cc289 100644
--- a/audiod.h
+++ b/audiod.h
@@ -6,13 +6,14 @@ extern int audiod_status;
/* defined in audiod.c */
struct btr_node *audiod_get_btr_root(void);
-__malloc char *audiod_get_decoder_flags(void);
-__malloc char *get_time_string(void);
bool uid_is_whitelisted(uid_t uid);
int client_write_bin(int fd, char *buf, size_t len, enum sb_designator band);
/* defined in audiod_command.c */
-void audiod_status_dump(bool force);
void close_stat_clients(void);
int dispatch_local_connection(int accept_fd);
void stat_client_write_item(int item_num);
+
+/** The possible values of the --mode option (const char *) */
+#define mode_string(mode) lls_enum_string_val((mode), \
+ lls_opt(LSG_AUDIOD_PARA_AUDIOD_OPT_MODE, lls_cmd(0, audiod_suite)))
diff --git a/audiod_command.c b/audiod_command.c
index 86732005..947d9e9e 100644
--- a/audiod_command.c
+++ b/audiod_command.c
@@ -227,13 +227,6 @@ static int stat_item_valid(const char *item)
return -E_UNKNOWN_STAT_ITEM;
}
-__malloc static char *audiod_status_string(void)
-{
- const char *status = (audiod_status == AUDIOD_ON)?
- "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
- return para_strdup(status);
-}
-
static int com_help(int fd, struct lls_parse_result *lpr)
{
char *buf;
@@ -360,33 +353,39 @@ static int com_term(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
}
EXPORT_AUDIOD_CMD_HANDLER(term)
-static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
+static int set_audiod_status(unsigned mode)
{
- audiod_status = AUDIOD_ON;
+ free(stat_item_values[SI_audiod_status]);
+ stat_item_values[SI_audiod_status] = para_strdup(mode_string(mode));
+ audiod_status = mode;
+ stat_client_write_item(SI_audiod_status);
return 1;
}
+
+static int com_on(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
+{
+ return set_audiod_status(AUDIOD_ON);
+}
EXPORT_AUDIOD_CMD_HANDLER(on)
static int com_off(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
{
- audiod_status = AUDIOD_OFF;
- return 1;
+ return set_audiod_status(AUDIOD_OFF);
}
EXPORT_AUDIOD_CMD_HANDLER(off)
static int com_sb(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
{
- audiod_status = AUDIOD_STANDBY;
- return 1;
+ return set_audiod_status(AUDIOD_STANDBY);
}
EXPORT_AUDIOD_CMD_HANDLER(sb)
static int com_cycle(__a_unused int fd, __a_unused struct lls_parse_result *lpr)
{
switch (audiod_status) {
- case AUDIOD_ON: audiod_status = AUDIOD_STANDBY; break;
- case AUDIOD_OFF: audiod_status = AUDIOD_ON; break;
- case AUDIOD_STANDBY: audiod_status = AUDIOD_OFF; break;
+ case AUDIOD_ON: return set_audiod_status(AUDIOD_STANDBY);
+ case AUDIOD_STANDBY: return set_audiod_status(AUDIOD_OFF);
+ case AUDIOD_OFF: return set_audiod_status(AUDIOD_ON);
}
return 1;
}
@@ -568,50 +567,3 @@ out:
return ret;
}
-/**
- * Send the current audiod status to all connected stat clients.
- *
- * \param force Whether to write unchanged items.
- */
-void audiod_status_dump(bool force)
-{
- char *old, *new;
-
- old = stat_item_values[SI_play_time];
- new = get_time_string();
- if (new) {
- if (force || !old || strcmp(old, new)) {
- free(old);
- stat_item_values[SI_play_time] = new;
- stat_client_write_item(SI_play_time);
- } else
- free(new);
- }
-
- new = daemon_get_uptime_str(now);
- old = stat_item_values[SI_audiod_uptime];
- if (force || !old || strcmp(old, new)) {
- free(old);
- stat_item_values[SI_audiod_uptime] = new;
- stat_client_write_item(SI_audiod_uptime);
- } else
- free(new);
-
- old = stat_item_values[SI_audiod_status];
- new = audiod_status_string();
- if (force || !old || strcmp(old, new)) {
- free(old);
- stat_item_values[SI_audiod_status] = new;
- stat_client_write_item(SI_audiod_status);
- } else
- free(new);
-
- old = stat_item_values[SI_decoder_flags];
- new = audiod_get_decoder_flags();
- if (force || !old || strcmp(old, new)) {
- free(old);
- stat_item_values[SI_decoder_flags] = new;
- stat_client_write_item(SI_decoder_flags);
- } else
- free(new);
-}