diff options
| author | Andre Noll <maan@tuebingen.mpg.de> | 2026-11-27 22:44:01 +0100 |
|---|---|---|
| committer | Andre Noll <maan@tuebingen.mpg.de> | 2026-11-27 22:44:01 +0100 |
| commit | 7c8ebfeb9bf9153763fbc744fb990469f402e733 (patch) | |
| tree | 11450b4814233f69af5a4e182fda378492b2fd0f | |
| parent | 089b2df7df2a0f4b6eea33cdaca506e111ec0fa8 (diff) | |
| parent | 1acc099bdede18c1452de8fc2e691d7f6ebf57a1 (diff) | |
Merge topic branch t/status_items into pupu
Revamp the status items.
------------------------
First step towards getting rid of the status items which depend on the
current time.
* t/status_items:
f
f
New status item: writer_start time.
audiod: Move the global afi struct into struct status_task.
audiod: Make the status task struct local.
audiod: Don't dump status items from command context.
audiod: Replace server_stream start slot info fields.
gui: Fix return value of colorful_blackness_print_status_items().
| -rw-r--r-- | audiod.c | 308 | ||||
| -rw-r--r-- | gui.c | 4 | ||||
| -rw-r--r-- | gui.h | 2 | ||||
| -rw-r--r-- | gui_theme.c | 59 | ||||
| -rw-r--r-- | para.h | 10 | ||||
| -rw-r--r-- | stat.c | 72 | ||||
| -rw-r--r-- | time.c | 35 |
7 files changed, 310 insertions, 180 deletions
@@ -89,10 +89,6 @@ struct audio_format_info { struct slot_info { /* Number of the audio format in this slot. */ int format; - /* The stream_start status item announced by para_server. */ - struct timeval server_stream_start; - /* The offset status item announced by para_server. */ - unsigned offset_seconds; /* The receiver info associated with this slot. */ struct receiver_node *receiver_node; /* The array of filter nodes. */ @@ -134,7 +130,7 @@ enum vss_status_flags { struct sched *sched; /* The task for obtaining para_server's status (para_client stat). */ -static struct status_task { +struct status_task { /* The associated task structure of audiod. */ struct task *task; /* Client data associated with the stat task. */ @@ -143,6 +139,8 @@ static struct status_task { struct timeval client_restart_barrier; /* do not start receiver/filters/writer before this time */ struct timeval slot_restart_barrier; + /* The server_stream_start status item value at decoder start time */ + struct timeval decoder_start; /* Last time we received status data from para_server. */ struct timeval last_status_read; /* Minimal input queue size. */ @@ -157,7 +155,8 @@ static struct status_task { enum vss_status_flags vss_status; /* The status task btrn is the child of the client task. */ struct btr_node *btrn; -} status_task_struct, *stat_task = &status_task_struct; + struct audio_format_info afi[NUM_AUDIO_FORMATS]; +}; /** The array of status items sent by para_server. */ char *stat_item_values[NUM_STAT_ITEMS] = {NULL}; @@ -168,7 +167,6 @@ char *stat_item_values[NUM_STAT_ITEMS] = {NULL}; */ int audiod_status = AUDIOD_ON; -static struct audio_format_info afi[NUM_AUDIO_FORMATS]; static uid_t *uid_whitelist; struct command_task { @@ -243,79 +241,28 @@ struct slot_info *get_play_time_slot(void) static struct timeval status_dump_barrier; -/* - * Compute the play time based on information of the current slot. - * - * 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 "~". - * - * 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. - */ -static __malloc char *get_time_string(void) +static __malloc char *get_time_string(struct status_task *stat_task) { - int ret, seconds = 0, length = stat_task->length_seconds; - struct timeval *sss = &stat_task->server_stream_start, tmp, wstime; + struct timeval *sss = &stat_task->server_stream_start, wstime, + delay, *p = NULL; struct slot_info *s = get_play_time_slot(); - bool writer_active = s && s->wns && s->wns[0].btrn; - - if (audiod_status == AUDIOD_OFF) - return para_strdup(NULL); - if (sss->tv_sec == 0 || length == 0) { /* no stream */ - if (stat_task->vss_status & VSS_STATUS_FLAG_PLAYING) - return NULL; /* repositioning or about to change file */ - if (length > 0) { /* paused */ - seconds = stat_task->offset_seconds; - goto out; - } - return para_strdup(NULL); /* stopped */ - } - if (!writer_active) { - struct timeval diff; - tv_diff(now, sss, &diff); - seconds = diff.tv_sec + stat_task->offset_seconds; - goto out; - } - btr_get_node_start(s->wns[0].btrn, &wstime); - if (wstime.tv_sec == 0) - return NULL; - if (s->server_stream_start.tv_sec == 0) { - /* copy status info to slot */ - s->offset_seconds = stat_task->offset_seconds; - s->server_stream_start = *sss; + + if (!s || !s->wns || !s->wns[0].btrn) { + delay.tv_sec = 1; + delay.tv_usec = 0; + p = NULL; + } else { + p = &wstime; + btr_get_node_start(s->wns[0].btrn, p); + tv_diff(&wstime, sss, &delay); + /* compute time of next time string change */ + if (delay.tv_usec / 1000 > 500) + ms2tv(1500 - delay.tv_usec / 1000, &delay); + else + ms2tv(500 - delay.tv_usec / 1000, &delay); } - seconds = s->offset_seconds; - /* check if audiod was started in the middle of the stream */ - ret = tv_diff(&wstime, &s->server_stream_start, &tmp); - if (ret > 0 && tv2ms(&tmp) > 2000) /* assume it was */ - tv_diff(now, &s->server_stream_start, &tmp); - 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); - return make_message( - "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)", - writer_active? "" : "~", - seconds / 60, - seconds % 60, - (length - seconds) / 60, - (length - seconds) % 60, - length? (seconds * 100 + length / 2) / length : 0, - length / 60, - length % 60 - ); + tv_add(now, &delay, &status_dump_barrier); + return make_time_string(sss, p, stat_item_values); } static void parse_config_or_die(void) @@ -362,14 +309,14 @@ static void clear_slot(int slot_num) s->format = -1; } -static void close_receiver(int slot_num) +static void close_receiver(int slot_num, struct status_task *stat_task) { struct slot_info *s = &slot[slot_num]; struct audio_format_info *a; if (s->format < 0 || !s->receiver_node) return; - a = &afi[s->format]; + a = stat_task->afi + s->format; PARA_NOTICE_LOG("closing %s receiver in slot %d\n", audio_formats[s->format], slot_num); RECEIVER(a)->close(s->receiver_node); @@ -391,7 +338,7 @@ static void writer_cleanup(struct writer_node *wn) task_reap(&wn->task); } -static void close_writers(struct slot_info *s) +static void close_writers(struct slot_info *s, struct status_task *stat_task) { struct audio_format_info *a; int i; @@ -399,7 +346,7 @@ static void close_writers(struct slot_info *s) if (s->format < 0) return; assert(s->wns); - a = afi + s->format; + a = stat_task->afi + s->format; if (a->num_writers == 0) writer_cleanup(s->wns); else { @@ -408,9 +355,10 @@ static void close_writers(struct slot_info *s) } free(s->wns); s->wns = NULL; + stat_task->decoder_start.tv_sec = stat_task->decoder_start.tv_usec = 0; } -static void notify_writers(int error) +static void notify_writers(int error, struct status_task *stat_task) { int i; @@ -421,16 +369,16 @@ static void notify_writers(int error) if (s->format < 0) continue; - a = afi + s->format; + a = stat_task->afi + s->format; for (j = 0; j < a->num_writers; j++) task_notify(s->wns[j].task, error); } } -static void close_filters(struct slot_info *s) +static void close_filters(struct slot_info *s, struct status_task *stat_task) { int i; - struct audio_format_info *a = afi + s->format; + struct audio_format_info *a = stat_task->afi + s->format; if (a->num_filters == 0) return; for (i = a->num_filters - 1; i >= 0; i--) { @@ -463,9 +411,9 @@ static void notify_receivers(int error) } } -static void open_filters(struct slot_info *s) +static void open_filters(struct slot_info *s, struct status_task *stat_task) { - struct audio_format_info *a = afi + s->format; + struct audio_format_info *a = stat_task->afi + s->format; struct filter_node *fn; int nf = a->num_filters; struct btr_node *parent; @@ -505,10 +453,10 @@ static void open_filters(struct slot_info *s) } } -static void open_writers(struct slot_info *s) +static void open_writers(struct slot_info *s, struct status_task *stat_task) { int i; - struct audio_format_info *a = afi + s->format; + struct audio_format_info *a = stat_task->afi + s->format; struct writer_node *wn; struct btr_node *parent = s->fns[a->num_filters - 1].btrn; @@ -526,9 +474,9 @@ static void open_writers(struct slot_info *s) } /* returns slot num on success */ -static int open_receiver(int format) +static int open_receiver(int format, struct status_task *stat_task) { - struct audio_format_info *a = &afi[format]; + struct audio_format_info *a = stat_task->afi + format; struct slot_info *s; int ret, slot_num; const struct receiver *r = RECEIVER(a); @@ -566,25 +514,6 @@ static int open_receiver(int format) return slot_num; } -static bool receiver_running(void) -{ - int i; - long unsigned ss1 = stat_task->server_stream_start.tv_sec; - - FOR_EACH_SLOT(i) { - struct slot_info *s = &slot[i]; - long unsigned ss2 = s->server_stream_start.tv_sec; - - if (!s->receiver_node) - continue; - if (task_status(s->receiver_node->task) >= 0) - return true; - if (ss1 == ss2) - return true; - } - return false; -} - /** * Return the root node of the current buffer tree. * @@ -618,7 +547,7 @@ struct btr_node *audiod_get_btr_root(void) } /* always initializes num */ -static bool must_start_decoder(int *num) +static bool must_start_decoder(int *num, struct status_task *stat_task) { unsigned vs = stat_task->vss_status; @@ -633,7 +562,9 @@ static bool must_start_decoder(int *num) return false; if (!(vs & VSS_STATUS_FLAG_PLAYING)) return false; - if (receiver_running()) + if (stat_task->server_stream_start.tv_sec == 0) + return false; + if (tveq(&stat_task->server_stream_start, &stat_task->decoder_start)) return false; if (tv_diff(now, &stat_task->slot_restart_barrier, NULL) < 0) return false; @@ -644,15 +575,30 @@ static bool must_start_decoder(int *num) * 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) +static void dump_audiod_status_items(bool force, struct status_task *stat_task) { char *old, *new; + struct slot_info *s = get_play_time_slot(); + struct timeval wstime = {.tv_sec = 0}; + static struct timeval old_wstime; + if (s && s->wns && s->wns[0].btrn) + btr_get_node_start(s->wns[0].btrn, &wstime); + if (force || !tveq(&wstime, &old_wstime)) { + free(stat_item_values[SI_writer_start_time]); + stat_item_values[SI_writer_start_time] = make_message("%lu.%lu", + (long unsigned)wstime.tv_sec, + (long unsigned)wstime.tv_usec + ); + stat_client_write_item(SI_writer_start_time); + old_wstime = wstime; + } + /* This can be removed after 0.9.0. */ 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 */ + new = get_time_string(stat_task); /* may overwrite status_dump_barrier */ if (new && (force || !old || strcmp(old, new))) { free(old); stat_item_values[SI_play_time] = new; @@ -670,12 +616,11 @@ static void dump_audiod_status_items(bool force) free(new); } -static void update_items(uint64_t mask) +static void update_items(uint64_t mask, struct status_task *stat_task) { int i; FOR_EACH_STATUS_ITEM(i) { - long unsigned sec, usec; char *buf = stat_item_values[i]; if (!status_item_bit_set(i, mask)) continue; @@ -695,20 +640,18 @@ static void update_items(uint64_t mask) stat_task->length_seconds = atoi(buf); break; case SI_stream_start: - if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) { - stat_task->server_stream_start.tv_sec = sec; - stat_task->server_stream_start.tv_usec = usec; - } + tv_scan(buf, &stat_task->server_stream_start); break; } } if (status_item_bit_set(SI_offset, mask)) - dump_audiod_status_items(false); + dump_audiod_status_items(false, stat_task); } -static int add_filter(int format, const char *cmdline) +static int add_filter(int format, const char *cmdline, + struct status_task *stat_task) { - struct audio_format_info *a = &afi[format]; + struct audio_format_info *a = stat_task->afi + format; int ret, filter_num, nf = a->num_filters; void *cfg; struct lls_parse_result *flpr; @@ -756,7 +699,7 @@ static int parse_stream_command(const char *txt, const char **cmd) return ret; } -static int parse_writer_args(void) +static int parse_writer_args(struct status_task *stat_task) { int i, ret; const char *cmd; @@ -771,7 +714,7 @@ static int parse_writer_args(void) return ret; af_mask = ret; FOR_EACH_AUDIO_FORMAT(j) { - a = afi + j; + a = stat_task->afi + j; if ((af_mask & (1 << j)) == 0) /* no match */ continue; nw = a->num_writers; @@ -787,7 +730,7 @@ static int parse_writer_args(void) } /* Use default writer for audio formats which are not yet set up. */ FOR_EACH_AUDIO_FORMAT(i) { - a = afi + i; + a = stat_task->afi + i; if (a->num_writers > 0) continue; /* already set up */ a->num_writers = 1; @@ -800,14 +743,14 @@ static int parse_writer_args(void) return 1; } -static int parse_receiver_args(void) +static int parse_receiver_args(struct status_task *stat_task) { int i, ret; const char *arg; struct audio_format_info *a; FOR_EACH_AUDIO_FORMAT(i) - afi[i].receiver_num = -1; + stat_task->afi[i].receiver_num = -1; for (i = OPT_GIVEN(RECEIVER) - 1; i >= 0; i--) { int j, af_mask; @@ -817,7 +760,7 @@ static int parse_receiver_args(void) goto out; af_mask = ret; FOR_EACH_AUDIO_FORMAT(j) { - a = afi + j; + a = stat_task->afi + j; if ((af_mask & (1 << j)) == 0) /* no match */ continue; /* @@ -835,13 +778,13 @@ static int parse_receiver_args(void) * receiver was specified. */ FOR_EACH_AUDIO_FORMAT(i) { - a = afi + i; + a = stat_task->afi + i; if (a->receiver_num >= 0) continue; a->receiver_num = check_receiver_arg(NULL, &a->receiver_lpr); } FOR_EACH_AUDIO_FORMAT(i) { - a = afi + i; + a = stat_task->afi + i; PARA_INFO_LOG("receiving %s streams via %s receiver\n", audio_formats[i], lls_command_name(RECEIVER_CMD(a))); } @@ -850,12 +793,12 @@ out: return ret; } -static int init_default_filters(void) +static int init_default_filters(struct status_task *stat_task) { int i, ret = 1; FOR_EACH_AUDIO_FORMAT(i) { - struct audio_format_info *a = &afi[i]; + struct audio_format_info *a = &stat_task->afi[i]; const char *name = lls_command_name(RECEIVER_CMD(a)); char *tmp; int j; @@ -868,7 +811,7 @@ static int init_default_filters(void) */ if (strcmp(name, "udp") == 0) { tmp = para_strdup("fecdec"); - ret = add_filter(i, tmp); + ret = add_filter(i, tmp, stat_task); free(tmp); if (ret < 0) goto out; @@ -883,7 +826,7 @@ static int init_default_filters(void) if (!filter_get(j)) goto out; tmp = para_strdup(filter_name(j)); - ret = add_filter(i, tmp); + ret = add_filter(i, tmp, stat_task); free(tmp); if (ret < 0) goto out; @@ -894,7 +837,7 @@ out: return ret; } -static int parse_filter_args(void) +static int parse_filter_args(struct status_task *stat_task) { int i, j, ret, af_mask, num_matches; @@ -909,7 +852,7 @@ static int parse_filter_args(void) FOR_EACH_AUDIO_FORMAT(j) { if ((af_mask & (1 << j)) == 0) /* no match */ continue; - ret = add_filter(j, arg); + ret = add_filter(j, arg, stat_task); if (ret < 0) goto out; num_matches++; @@ -918,22 +861,23 @@ static int parse_filter_args(void) PARA_WARNING_LOG("ignoring filter spec: %s\n", lls_string_val(i, OPT_RESULT(FILTER))); } - ret = init_default_filters(); /* use default values for the rest */ + /* use default values for the rest */ + ret = init_default_filters(stat_task); out: return ret; } -static void parse_stream_args(void) +static void parse_stream_args(struct status_task *stat_task) { int ret; - ret = parse_receiver_args(); + ret = parse_receiver_args(stat_task); if (ret < 0) goto fail; - ret = parse_filter_args(); + ret = parse_filter_args(stat_task); if (ret < 0) goto fail; - ret = parse_writer_args(); + ret = parse_writer_args(stat_task); if (ret < 0) goto fail; return; @@ -1006,11 +950,10 @@ static int command_post_monitor(struct sched *s, void *context) return ret; } } - dump_audiod_status_items(false); return 1; } -static void close_status_connection(void) +static void close_status_connection(struct status_task *stat_task) { int i; @@ -1023,8 +966,8 @@ static void close_status_connection(void) switch (i) { case SI_decoder_flags: case SI_audiod_status: - case SI_play_time: case SI_audiod_uptime: + case SI_writer_start_time: continue; } free(stat_item_values[i]); @@ -1034,13 +977,15 @@ static void close_status_connection(void) stat_task->length_seconds = 0; stat_task->offset_seconds = 0; stat_task->vss_status = 0; - dump_audiod_status_items(true); + stat_task->server_stream_start.tv_sec = 0; + stat_task->server_stream_start.tv_usec = 0; + dump_audiod_status_items(true, stat_task); } -static bool must_close_slot(int slot_num) +static bool must_close_slot(int slot_num, struct status_task *stat_task) { struct slot_info *s = &slot[slot_num]; - struct audio_format_info *a = afi + s->format; + struct audio_format_info *a = stat_task->afi + s->format; int i; if (s->format < 0) @@ -1061,33 +1006,33 @@ static bool must_close_slot(int slot_num) return true; } -static void close_unused_slots(void) +static void close_unused_slots(struct status_task *stat_task) { int i; bool dump = false; FOR_EACH_SLOT(i) { struct slot_info *s = slot + i; - if (!must_close_slot(i)) + if (!must_close_slot(i, stat_task)) continue; PARA_INFO_LOG("closing slot %d\n", i); - close_writers(s); - close_filters(s); - close_receiver(i); + close_writers(s, stat_task); + close_filters(s, stat_task); + close_receiver(i, stat_task); clear_slot(i); audiod_set_decoder_flags(s); dump = true; } if (dump) - dump_audiod_status_items(true); + dump_audiod_status_items(true, stat_task); } -static void audiod_cleanup(void) +static void audiod_cleanup(struct status_task *stat_task) { int i, j; FOR_EACH_AUDIO_FORMAT(i) { - struct audio_format_info *a = afi + i; + struct audio_format_info *a = stat_task->afi + i; unsigned idx; const struct lls_command *cmd; for (j = 0; j < a->num_filters; j++) { @@ -1107,43 +1052,52 @@ static void audiod_cleanup(void) free(a->writer_lpr); lls_free_parse_result(a->receiver_lpr, RECEIVER_CMD(a)); } - close_status_connection(); - close_unused_slots(); + close_status_connection(stat_task); + close_unused_slots(stat_task); close_stat_clients(); 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]); + free(stat_item_values[SI_writer_start_time]); } /* * Check if any receivers/filters/writers need to be started and do so if * necessary. */ -static void start_stop_decoders(void) +static void start_stop_decoders(struct status_task *stat_task) { int ret, num; struct slot_info *sl; - close_unused_slots(); + close_unused_slots(stat_task); if (audiod_status != AUDIOD_ON) - return notify_writers(E_NOT_PLAYING); + return notify_writers(E_NOT_PLAYING, stat_task); if (!(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)) return notify_receivers(E_NOT_PLAYING); - if (!must_start_decoder(&num)) + ret = 0; + FOR_EACH_SLOT(num) + ret += slot[num].format >= 0; + if (ret == 0) { + stat_task->decoder_start.tv_sec = 0; + stat_task->decoder_start.tv_usec = 0; + } + if (!must_start_decoder(&num, stat_task)) return; - ret = open_receiver(num); + ret = open_receiver(num, stat_task); if (ret < 0) { PARA_ERROR_LOG("%s\n", para_strerror(-ret)); return; } sl = slot + ret; - open_filters(sl); - open_writers(sl); + open_filters(sl, stat_task); + open_writers(sl, stat_task); + stat_task->decoder_start = stat_task->server_stream_start; activate_grab_clients(sched); btr_log_tree(sl->receiver_node->btrn, LL_NOTICE); - dump_audiod_status_items(true); + dump_audiod_status_items(true, stat_task); audiod_set_decoder_flags(sl); } @@ -1152,10 +1106,10 @@ static void status_pre_monitor(struct sched *s, void *context) struct status_task *st = context; int i, ret, num; - if (must_start_decoder(&num)) + if (must_start_decoder(&num, st)) goto min_delay; FOR_EACH_SLOT(i) - if (must_close_slot(i)) + if (must_close_slot(i, st)) goto min_delay; ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF); if (ret > 0) @@ -1191,7 +1145,7 @@ static int status_post_monitor(struct sched *s, void *context) task_notify(st->ct->task, E_AUDIOD_OFF); goto out; } - close_status_connection(); + close_status_connection(st); goto out; } if (st->ct) { @@ -1201,7 +1155,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_status_connection(); + close_status_connection(st); goto out; } if (st->ct->status != CL_EXECUTING) @@ -1219,7 +1173,7 @@ static int status_post_monitor(struct sched *s, void *context) btr_consume(st->btrn, sz); st->last_status_read = *now; st->min_iqs = 0; - update_items(mask); + update_items(mask, st); goto out; } btr_drain(st->btrn); @@ -1233,11 +1187,12 @@ static int status_post_monitor(struct sched *s, void *context) tv_add(now, &(struct timeval){5, 0}, &st->client_restart_barrier); st->last_status_read = *now; out: - start_stop_decoders(); + dump_audiod_status_items(false, st); + start_stop_decoders(st); return 0; } -static int init_tasks_and_schedule(void) +static int init_tasks_and_schedule(struct status_task *stat_task) { struct signal_task signal_task; struct command_task command_task; @@ -1344,6 +1299,7 @@ int main(int argc, char *argv[]) { int ret, i; char *errctx; + struct status_task stat_task = {.task = NULL}; valid_fd_012(); ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx)); @@ -1371,7 +1327,7 @@ int main(int argc, char *argv[]) daemon_set_logfile(OPT_STRING_VAL(LOGFILE)); daemon_open_log_or_die(); } - parse_stream_args(); + parse_stream_args(&stat_task); daemon_log_welcome("audiod"); daemon_set_start_time(); audiod_status = OPT_UINT32_VAL(MODE); @@ -1381,8 +1337,8 @@ int main(int argc, char *argv[]) clear_slot(i); audiod_set_decoder_flags(slot + i); } - ret = init_tasks_and_schedule(); - audiod_cleanup(); + ret = init_tasks_and_schedule(&stat_task); + audiod_cleanup(&stat_task); sched_shutdown(sched); out: lls_free_parse_result(lpr, CMD_PTR); @@ -31,6 +31,7 @@ #include "gui.lsg.h" #include "para.h" +#include "sched.h" #include "gui.h" #include "lsu.h" #include "string.h" @@ -38,7 +39,6 @@ #include "fd.h" #include "error.h" #include "list.h" -#include "sched.h" #include "signal.h" #include "audioc.h" #include "sideband.h" @@ -627,6 +627,8 @@ static void status_pre_monitor(struct sched *s, void *context) sched_monitor_readfd(st->fd, s); else sched_request_barrier_or_min_delay(&st->connect_barrier, s); + if (theme->pre_monitor) + theme->pre_monitor(s); } static int recv_status(struct sched *s, struct status_task *st) @@ -31,6 +31,8 @@ struct gui_theme { int cols_min; /** Called when status items change, it should update the top window. */ bool (*print_status_items)(uint64_t mask, char **stat_content, WINDOW *win); + + void (*pre_monitor)(struct sched *s); /** Color of the bar that contains the version string. */ struct gui_color_spec vbar; /** Color of the name and args of the executing process. */ diff --git a/gui_theme.c b/gui_theme.c index 5af032da..952e9ecb 100644 --- a/gui_theme.c +++ b/gui_theme.c @@ -5,6 +5,7 @@ #include <curses.h> #include "para.h" +#include "sched.h" #include "gui.h" #include "error.h" #include "string.h" @@ -172,7 +173,7 @@ struct stat_item_data { }; static const struct stat_item_data colorful_blackness_data[NUM_STAT_ITEMS] = { - [SI_play_time] = { + [SI_writer_start_time] = { .prefix = "", .postfix = "", .color.fg = COLOR_CYAN, @@ -436,11 +437,60 @@ static const struct stat_item_data colorful_blackness_data[NUM_STAT_ITEMS] = { } }; +static struct timeval play_time_barrier; + +static void colorful_blackness_pre_monitor(struct sched *s) +{ + sched_request_barrier(&play_time_barrier, s); +} + +static bool print_play_time(uint64_t mask, char **items, WINDOW *win) +{ + char *i1, *i2, *pts; + struct timeval sss = {.tv_sec = 0}, wst = sss, delay; + struct stat_item_data d = colorful_blackness_data[SI_writer_start_time]; + + i1 = items[SI_stream_start]; + i2 = items[SI_writer_start_time]; + if (status_item_bit_set(SI_writer_start_time, mask)) + goto print; + if (status_item_bit_set(SI_stream_start, mask)) + goto print; + if (tv_diff(now, &play_time_barrier, NULL) < 0) + return false; +print: + if (i1 && *i1 && tv_scan(i1, &sss) < 0) + goto fail; + if (i2) { /* new audiod */ + if (*i2 && tv_scan(i2, &wst) < 0) + goto fail; + pts = make_time_string(&sss, wst.tv_sec? &wst : NULL, items); + } else + pts = items[SI_play_time]; + wmove(win, d.y * getmaxy(win) / 100, d.x * COLS / 100); + wattron(win, COLOR_PAIR(SI_writer_start_time + 1)); + align_str(win, pts, d.len * COLS / 100, d.align); + if (i2) + free(pts); + /* compute time of next play time string change */ + tv_diff(now, &wst, &delay); + if (delay.tv_usec / 1000 > 500) + ms2tv(1500 - delay.tv_usec / 1000, &delay); + else + ms2tv(500 - delay.tv_usec / 1000, &delay); + tv_add(now, &delay, &play_time_barrier); + return true; +fail: + PARA_WARNING_LOG("status item parse error, i1=%s, i2=%s\n\n", i1, i2); + return false; +} + static bool colorful_blackness_print_status_items(uint64_t mask, char **items, WINDOW *win) { static bool initialized; int i, top_lines = getmaxy(win); + bool printed_an_item = false; if (!initialized) { FOR_EACH_STATUS_ITEM(i) { @@ -451,6 +501,7 @@ static bool colorful_blackness_print_status_items(uint64_t mask, char **items, } initialized = true; } + printed_an_item = print_play_time(mask, items, win); FOR_EACH_STATUS_ITEM(i) { struct stat_item_data d = colorful_blackness_data[i]; char *c = items[i]; @@ -461,15 +512,18 @@ static bool colorful_blackness_print_status_items(uint64_t mask, char **items, continue; if (i == SI_status_flags && c[0] == '\0') c = "down"; + if (i == SI_writer_start_time || i == SI_stream_start) + continue; if (d.len > 0) { char *tmp = make_message("%s%s%s", d.prefix, c, d.postfix); wmove(win, d.y * top_lines / 100, d.x * COLS / 100); wattron(win, COLOR_PAIR(i + 1)); align_str(win, tmp, d.len * COLS / 100, d.align); free(tmp); + printed_an_item = true; } } - return true; + return printed_an_item; } static const struct gui_theme themes[] = { @@ -500,6 +554,7 @@ static const struct gui_theme themes[] = { .dflt.bg = COLOR_BLACK, .dflt.fg = COLOR_MAGENTA, .print_status_items = colorful_blackness_print_status_items, + .pre_monitor = colorful_blackness_pre_monitor, }, { .name = "simple", .lines_min = 5, @@ -132,6 +132,13 @@ void compute_chunk_time(long unsigned chunk_num, struct timeval *result); struct timeval *clock_get_realtime(struct timeval *tv); +static inline bool tveq(const struct timeval *tv1, const struct timeval *tv2) +{ + return tv1->tv_sec == tv2->tv_sec && tv1->tv_usec == tv2->tv_usec; +} + +int tv_scan(const char *buf, struct timeval *tv); + /** * Return a random non-negative integer in an interval. * @@ -266,6 +273,7 @@ enum loglevels {LOGLEVELS, NUM_LOGLEVELS}; STATUS_ITEM(album) \ STATUS_ITEM(comment) \ STATUS_ITEM(max_chunk_size) \ + STATUS_ITEM(writer_start_time) \ #define STATUS_ITEM(_name) SI_ ##_name, enum status_items {STATUS_ITEMS NUM_STAT_ITEMS}; @@ -277,6 +285,8 @@ extern const char *status_item_list[]; /** Loop over each status item. */ #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++) uint64_t parse_status_items(char *item_buf, size_t num_bytes, char **items); +__malloc char *make_time_string(struct timeval *sss, struct timeval *wstime, + char **items); static inline bool status_item_bit_set(int item_num, uint64_t mask) { return mask & ((uint64_t)1 << item_num); @@ -8,6 +8,7 @@ #include "para.h" #include "error.h" #include "string.h" +#include "sched.h" /** The minimal length of a status item buffer. */ #define MIN_STAT_ITEM_LEN 9 /* 5 + 2 + 2, e.g. '0005 00:\n' */ @@ -110,3 +111,74 @@ parse_error: PARA_ERROR_LOG("status item parse error\n"); return mask; } + +/* + * This function can go to gui_theme.c once the play_time status item has + * been disabled. + */ + +/* + * Compute the play time based on information of the current slot. + * + * 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 "~". + * + * 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. + */ +__malloc char *make_time_string(struct timeval *sss, struct timeval *wstime, + char **items) +{ + int length, seconds, offset; + struct timeval tmp; + char *si_seconds = items[SI_seconds_total], + *si_flags = items[SI_status_flags], + *si_offset = items[SI_offset]; + + if (!sss || !si_seconds || !si_flags || !si_offset) + return para_strdup(NULL); + length = atoi(items[SI_seconds_total]); + if (sss->tv_sec == 0 || length == 0) { /* no stream */ + if (strchr(items[SI_status_flags], 'P')) + return NULL; /* repositioning or about to change file */ + if (length > 0) { /* paused */ + seconds = atoi(items[SI_offset]); + goto out; + } + return para_strdup(NULL); /* stopped */ + } + offset = atoi(items[SI_offset]); + if (!wstime) { + struct timeval diff; + tv_diff(now, sss, &diff); + seconds = diff.tv_sec + offset; + goto out; + } + if (wstime->tv_sec == 0) + return NULL; + seconds = offset; + /* check if audiod was started in the middle of the stream */ + if (tv_diff(wstime, sss, &tmp) > 0 && tv2ms(&tmp) > 2000) { + tv_diff(now, sss, &tmp); /* assume it was */ + } else + tv_diff(now, wstime, &tmp); + seconds += tmp.tv_sec + (tmp.tv_usec + 500 * 1000) / 1000 / 1000; +out: + seconds = PARA_MIN(seconds, length); + seconds = PARA_MAX(seconds, 0); + return make_message( + "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)", + wstime? "" : "~", + seconds / 60, + seconds % 60, + (length - seconds) / 60, + (length - seconds) % 60, + length? (seconds * 100 + length / 2) / length : 0, + length / 60, + length % 60 + ); +} @@ -1,8 +1,10 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#include "para.h" /** \file time.c Helper functions for dealing with time values. */ +#include "para.h" +#include "error.h" + /** * Convert struct timeval to milliseconds. * @@ -205,3 +207,34 @@ struct timeval *clock_get_realtime(struct timeval *tv) tv->tv_usec = t.tv_nsec / 1000; return tv; } + +/** + * Initialize a time value from a string of the form seconds.useconds. + * + * \param buf The string representation (e.g., "1795636187.626095"). + * \param tv Result pointer. + * + * The seconds part may be negative, but the microseconds part must be + * in the range [0-999999]. + * + * \return Standard. + */ +int tv_scan(const char *buf, struct timeval *tv) +{ + /* + * Since neither field of struct timeval has a type for which there + * exists a scanf length modifier, use temporary variables of type + * (u)intmax_t to scan into the fields. See system_data_types(7). + */ + intmax_t sec; + uintmax_t usec; + int ret = sscanf(buf, "%jd.%ju", &sec, &usec); + + if (ret != 2) + return -ERRNO_TO_PARA_ERROR(EINVAL); + if (usec > 999999) + return -ERRNO_TO_PARA_ERROR(EINVAL); + tv->tv_sec = sec; + tv->tv_usec = usec; + return 1; +} |
