Improve help/man page of para_recv.
[paraslash.git] / audiod.c
index 3c737226c8bf57b94be0716ae019e2961d488c99..22611eb0db41f98c6ece8cfc70422eee478b5d63 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -7,12 +7,14 @@
 /** \file audiod.c the paraslash's audio daemon */
 #include <sys/types.h>
 #include <dirent.h>
+#include <signal.h>
 
 #include "para.h"
 #include "error.h"
 #include "audiod.cmdline.h"
 #include "list.h"
 #include "sched.h"
+#include "ggo.h"
 #include "recv.h"
 #include "filter.h"
 #include "grab_client.cmdline.h"
@@ -65,6 +67,9 @@ struct audio_format_info {
 struct slot_info slot[MAX_STREAM_SLOTS];
 
 
+/** The array of status items sent by para_server. */
+char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
+
 /**
  * the current mode of operation of which can be changed by the on/off/cycle
  * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
@@ -90,7 +95,7 @@ static struct status_task status_task_struct;
  *
  * \sa struct status_task
  */
-struct status_task *stat_task = &status_task_struct;
+static struct status_task *stat_task = &status_task_struct;
 static struct timeval initial_delay_barrier;
 
 /**
@@ -127,13 +132,62 @@ int get_audio_format_num(char *name)
        return -E_UNSUPPORTED_AUDIO_FORMAT;
 }
 
+char *get_time_string(struct timeval *newest_stime)
+{
+       struct timeval diff, adj_stream_start, tmp;
+       int total = 0, use_server_time = 1,
+               length_seconds = stat_task->length_seconds;
+
+       if (!stat_task->playing) {
+               if (length_seconds)
+                       return NULL;
+               return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
+       }
+       if (audiod_status == AUDIOD_OFF)
+               goto out;
+       if (stat_task->sa_time_diff_sign > 0)
+               tv_diff(&stat_task->server_stream_start, &stat_task->sa_time_diff,
+                       &adj_stream_start);
+       else
+               tv_add(&stat_task->server_stream_start, &stat_task->sa_time_diff,
+                       &adj_stream_start);
+       tmp = adj_stream_start;
+       if (newest_stime && audiod_status == AUDIOD_ON) {
+               tv_diff(newest_stime, &adj_stream_start, &diff);
+               if (tv2ms(&diff) < 5000) {
+                       tmp = *newest_stime;
+                       use_server_time = 0;
+               }
+       }
+       tv_diff(now, &tmp, &diff);
+       total = diff.tv_sec + stat_task->offset_seconds;
+       if (total > length_seconds)
+               total = length_seconds;
+       if (total < 0)
+               total = 0;
+out:
+       return make_message(
+               "%s: %s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
+               status_item_list[SI_PLAY_TIME],
+               use_server_time? "~" : "",
+               total / 60,
+               total % 60,
+               (length_seconds - total) / 60,
+               (length_seconds - total) % 60,
+               length_seconds? (total * 100 + length_seconds / 2) /
+                       length_seconds : 0,
+               length_seconds / 60,
+               length_seconds % 60
+       );
+}
+
 /**
  * the log function of para_audiod
  *
  * \param ll loglevel
  * \param fmt the format string
  */
-void para_log(int ll, const char* fmt,...)
+__printf_2_3 void para_log(int ll, const char* fmt,...)
 {
        va_list argp;
        FILE *outfd;
@@ -274,7 +328,7 @@ static void open_filters(int slot_num)
                return;
        PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
        s->fc = para_calloc(sizeof(struct filter_chain));
-       s->fc->filter_nodes = para_malloc(nf * sizeof(struct filter_chain));
+       s->fc->filter_nodes = para_malloc(nf * sizeof(struct filter_node));
        s->fc->inbuf = s->receiver_node->buf;
        s->fc->in_loaded = &s->receiver_node->loaded;
        s->fc->input_error = &s->receiver_node->task.error;
@@ -382,8 +436,7 @@ static int receiver_running(int format)
 
        FOR_EACH_SLOT(i) {
                struct slot_info *s = &slot[i];
-               if (s->format == format && s->receiver_node
-                               && s->receiver_node->task.error >= 0)
+               if (s->format == format && s->receiver_node)
                        return 1;
        }
        return 0;
@@ -467,8 +520,8 @@ static int check_stat_line(char *line, __a_unused void *data)
        tmp = make_message("%s\n", line);
        stat_client_write(tmp, itemnum);
        free(tmp);
-       free(stat_task->stat_item_values[itemnum]);
-       stat_task->stat_item_values[itemnum] = para_strdup(line);
+       free(stat_item_values[itemnum]);
+       stat_item_values[itemnum] = para_strdup(line);
        ilen = strlen(status_item_list[itemnum]);
        switch (itemnum) {
        case SI_STATUS:
@@ -551,7 +604,7 @@ static int init_writers(void)
        struct audio_format_info *a;
 
        init_supported_writers();
-       nw = PARA_MAX(1, conf.writer_given);
+       nw = PARA_MAX(1U, conf.writer_given);
        PARA_INFO_LOG("maximal number of writers: %d\n", nw);
        FOR_EACH_AUDIO_FORMAT(i) {
                a = &afi[i];
@@ -668,7 +721,7 @@ static int init_filters(void)
        int i, ret, nf;
 
        filter_init(filters);
-       nf = PARA_MAX(1 conf.filter_given);
+       nf = PARA_MAX(1U, conf.filter_given);
        PARA_INFO_LOG("maximal number of filters: %d\n", nf);
        FOR_EACH_AUDIO_FORMAT(i) {
                afi[i].filter_conf = para_malloc(nf * sizeof(void *));
@@ -756,13 +809,12 @@ static void signal_pre_select(struct sched *s, struct task *t)
 static void signal_post_select(struct sched *s, struct task *t)
 {
        struct signal_task *st = container_of(t, struct signal_task, task);
-       int signum;
 
        if (!FD_ISSET(st->fd, &s->rfds))
                return;
 
-       signum = para_next_signal();
-       switch (signum) {
+       st->signum = para_next_signal();
+       switch (st->signum) {
        case SIGINT:
        case SIGTERM:
        case SIGHUP:
@@ -815,19 +867,14 @@ static void close_stat_pipe(void)
        client_close(stat_task->ct);
        stat_task->ct = NULL;
        FOR_EACH_STATUS_ITEM(i) {
-               free(stat_task->stat_item_values[i]);
-               stat_task->stat_item_values[i] = NULL;
+               free(stat_item_values[i]);
+               stat_item_values[i] = NULL;
        }
        dump_empty_status();
        stat_task->length_seconds = 0;
        stat_task->offset_seconds = 0;
        audiod_status_dump();
        stat_task->playing = 0;
-       stat_task->stat_item_values[SI_BASENAME] = make_message(
-               "%s: no connection to para_server\n",
-               status_item_list[SI_BASENAME]);
-       stat_client_write(stat_task->stat_item_values[SI_BASENAME],
-               SI_BASENAME);
 }
 
 /**
@@ -984,6 +1031,12 @@ static void status_pre_select(struct sched *s, struct task *t)
                client_open(argc, argv, &st->ct);
                set_stat_task_restart_barrier(5);
        }
+       free(stat_item_values[SI_BASENAME]);
+       stat_item_values[SI_BASENAME] = make_message(
+               "%s: no connection to para_server\n",
+               status_item_list[SI_BASENAME]);
+       stat_client_write(stat_item_values[SI_BASENAME],
+               SI_BASENAME);
        st->last_status_read = *now;
 out:
        start_stop_decoders(s);
@@ -1030,22 +1083,27 @@ int main(int argc, char *argv[])
 {
        char *config_file;
        int ret, i;
-       struct sched s;
+       static struct sched s;
        struct command_task command_task_struct, *cmd_task = &command_task_struct;
+       struct audiod_cmdline_parser_params params = {
+               .override = 0,
+               .initialize = 1,
+               .check_required = 0,
+               .check_ambiguity = 0,
+               .print_errors = 1
+       };
 
        valid_fd_012();
-       audiod_cmdline_parser(argc, argv, &conf);
+       audiod_cmdline_parser_ext(argc, argv, &conf, &params);
        HANDLE_VERSION_FLAG("audiod", conf);
        para_drop_privileges(conf.user_arg, conf.group_arg);
        config_file = configfile_exists();
        if (config_file) {
-               struct audiod_cmdline_parser_params params = {
-                       .override = 0,
-                       .initialize = 0,
-                       .check_required = 0,
-                       .check_ambiguity = 0
-
-               };
+               params.override = 0;
+               params.initialize = 0;
+               params.check_required = 1;
+               params.check_ambiguity = 0;
+               params.print_errors = 1;
                if (audiod_cmdline_parser_config_file(config_file, &conf, &params)) {
                        PARA_EMERG_LOG("parse error in config file\n");
                        exit(EXIT_FAILURE);