Always include stdbool.h.
[paraslash.git] / audiod.c
index 9f664e178309c39e70a64d7ea55f6a29f0c5f6cf..88411af46616879040a8691308ed4ca639fc5475 100644 (file)
--- a/audiod.c
+++ b/audiod.c
@@ -8,7 +8,6 @@
 #include <regex.h>
 #include <sys/types.h>
 #include <signal.h>
-#include <stdbool.h>
 
 #include "para.h"
 #include "error.h"
@@ -33,6 +32,7 @@
 #include "signal.h"
 #include "version.h"
 
+__printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
 /** define the array of error lists needed by para_audiod */
 INIT_AUDIOD_ERRLISTS;
 /** define the array containing all supported audio formats */
@@ -77,6 +77,8 @@ enum vss_status_flags {
        VSS_STATUS_FLAG_PLAYING = 2,
 };
 
+struct sched sched = {.max_fileno = 0};
+
 /**
  * The task for obtaining para_server's status (para_client stat).
  *
@@ -178,6 +180,22 @@ static int get_audio_format_num(const char *name)
        return -E_UNSUPPORTED_AUDIO_FORMAT;
 }
 
+static int get_matching_audio_format_nums(const char *re)
+{
+       int i, ret;
+       regex_t preg;
+
+       ret = para_regcomp(&preg, re, REG_EXTENDED | REG_NOSUB);
+       if (ret < 0)
+               return ret;
+       ret = 0;
+       FOR_EACH_AUDIO_FORMAT(i)
+               if (regexec(&preg, audio_formats[i], 0, NULL, 0) != REG_NOMATCH)
+                       ret |= (1 << i);
+       regfree(&preg);
+       return ret;
+}
+
 /**
  * Compute the play time based on information of the given slot.
  *
@@ -273,7 +291,7 @@ out:
                length / 60,
                length % 60
        );
-       PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
+       //PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
        return msg;
 empty:
        return para_strdup(NULL);
@@ -429,22 +447,15 @@ static void kill_btrn(struct btr_node *btrn, struct task *t, int error)
 
 static void kill_all_decoders(int error)
 {
-       int i, j;
+       int i;
 
        FOR_EACH_SLOT(i) {
-               struct slot_info *s = &slot[i];
-               struct audio_format_info *a;
+               struct slot_info *s = slot + i;
                if (s->format < 0)
                        continue;
-               a = afi + s->format;
-               if (s->wns)
-                       for (j = 0; j < a->num_writers; j++)
-                               kill_btrn(s->wns[j].btrn, &s->wns[j].task, error);
-               if (s->fns)
-                       for (j = 0; j < a->num_writers; j++)
-                               kill_btrn(s->fns[j].btrn, &s->wns[j].task, error);
-               if (s->receiver_node)
-                       kill_btrn(s->receiver_node->btrn, &s->receiver_node->task,
+               if (!s->receiver_node)
+                       continue;
+               kill_btrn(s->receiver_node->btrn, &s->receiver_node->task,
                                error);
        }
 }
@@ -495,7 +506,7 @@ static void open_filters(struct slot_info *s)
                                .handler = f->execute, .context = fn));
 
                f->open(fn);
-               register_task(&fn->task);
+               register_task(&sched, &fn->task);
                parent = fn->btrn;
                PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
                        audio_formats[s->format], i,  nf, f->name, (int)(s - slot));
@@ -513,16 +524,12 @@ static void open_writers(struct slot_info *s)
        assert(s->wns == NULL);
        s->wns = para_calloc(PARA_MAX(1U, a->num_writers)
                * sizeof(struct writer_node));
-       if (a->num_writers == 0)
-               setup_writer_node(NULL, parent, s->wns);
-       else {
-               PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
-               for (i = 0; i < a->num_writers; i++) {
-                       wn = s->wns + i;
-                       wn->conf = a->writer_conf[i];
-                       wn->writer_num = a->writer_nums[i];
-                       register_writer_node(wn, parent);
-               }
+       PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
+       for (i = 0; i < a->num_writers; i++) {
+               wn = s->wns + i;
+               wn->conf = a->writer_conf[i];
+               wn->writer_num = a->writer_nums[i];
+               register_writer_node(wn, parent, &sched);
        }
 }
 
@@ -559,7 +566,7 @@ static int open_receiver(int format)
        rn->task.pre_select = r->pre_select;
        rn->task.post_select = r->post_select;
        sprintf(rn->task.status, "%s receiver node", r->name);
-       register_task(&rn->task);
+       register_task(&sched, &rn->task);
        return slot_num;
 }
 
@@ -723,30 +730,36 @@ static int update_item(int itemnum, char *buf)
 
 static int parse_stream_command(const char *txt, char **cmd)
 {
-       char *p = strchr(txt, ':');
-       int i;
+       int ret, len;
+       char *re, *p = strchr(txt, ':');
 
        if (!p)
                return -E_MISSING_COLON;
-       p++;
-       FOR_EACH_AUDIO_FORMAT(i) {
-               if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
-                       continue;
-               *cmd = p;
-               return i;
-       }
-       return -E_UNSUPPORTED_AUDIO_FORMAT;
+       *cmd = p + 1;
+       len = p - txt;
+       re = malloc(len + 1);
+       strncpy(re, txt, len);
+       re[len] = '\0';
+       ret = get_matching_audio_format_nums(re);
+       free(re);
+       return ret;
 }
 
 static int add_filter(int format, char *cmdline)
 {
        struct audio_format_info *a = &afi[format];
        int filter_num, nf = a->num_filters;
+       void *cfg;
 
-       filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
+       filter_num = check_filter_arg(cmdline, &cfg);
        if (filter_num < 0)
                return filter_num;
+       a->filter_conf = para_realloc(a->filter_conf,
+               (nf + 1) * sizeof(void *));
+       a->filter_nums = para_realloc(a->filter_nums,
+               (nf + 1) * sizeof(unsigned));
        a->filter_nums[nf] = filter_num;
+       a->filter_conf[nf] = cfg;
        a->num_filters++;
        PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
                filters[filter_num].name);
@@ -755,40 +768,48 @@ static int add_filter(int format, char *cmdline)
 
 static int parse_writer_args(void)
 {
-       int i, ret, nw;
+       int i, ret;
        char *cmd;
        struct audio_format_info *a;
 
-       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];
-               a->writer_conf = para_malloc(nw * sizeof(void *));
-               a->writer_nums = para_malloc(nw * sizeof(int));
-               a->num_writers = 0;
-       }
        for (i = 0; i < conf.writer_given; i++) {
                void *wconf;
-               int writer_num;
+               int j, nw, writer_num, af_mask;
+
                ret = parse_stream_command(conf.writer_arg[i], &cmd);
                if (ret < 0)
-                       goto out;
-               a = &afi[ret];
-               nw = a->num_writers;
-               wconf = check_writer_arg(cmd, &writer_num);
-               if (!wconf) {
-                       ret = writer_num;
-                       goto out;
+                       return ret;
+               af_mask = ret;
+               FOR_EACH_AUDIO_FORMAT(j) {
+                       a = afi + j;
+                       if ((af_mask & (1 << j)) == 0) /* no match */
+                               continue;
+                       wconf = check_writer_arg_or_die(cmd, &writer_num);
+                       nw = a->num_writers;
+                       a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int));
+                       a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *));
+                       a->writer_nums[nw] = writer_num;
+                       a->writer_conf[nw] = wconf;
+                       PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[j],
+                               nw, writer_names[writer_num]);
+                       a->num_writers++;
                }
-               a->writer_nums[nw] = writer_num;
-               a->writer_conf[nw] = wconf;
-               PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
-                       nw, writer_names[writer_num]);
-               a->num_writers++;
        }
-       ret = 1;
-out:
-       return ret;
+       /* Use default writer for audio formats which are not yet set up. */
+       FOR_EACH_AUDIO_FORMAT(i) {
+               struct writer *w = writers + DEFAULT_WRITER;
+               a = afi + i;
+               if (a->num_writers > 0)
+                       continue; /* already set up */
+               PARA_INFO_LOG("%s writer: %s (default)\n", audio_formats[i],
+                       writer_names[DEFAULT_WRITER]);
+               a->writer_nums = para_malloc(sizeof(int));
+               a->writer_nums[0] = DEFAULT_WRITER;
+               a->writer_conf = para_malloc(sizeof(void *));
+               a->writer_conf[0] = w->parse_config_or_die("");
+               a->num_writers = 1;
+       }
+       return 1;
 }
 
 static int parse_receiver_args(void)
@@ -798,30 +819,31 @@ static int parse_receiver_args(void)
        struct audio_format_info *a;
 
        for (i = conf.receiver_given - 1; i >= 0; i--) {
-               char *arg = conf.receiver_arg[i];
-               char *recv_arg = strchr(arg, ':');
-               ret = -E_MISSING_COLON;
-               if (!recv_arg)
-                       goto out;
-               *recv_arg = '\0';
-               recv_arg++;
-               ret = get_audio_format_num(arg);
+               char *arg;
+               int j, af_mask;
+
+               ret = parse_stream_command(conf.receiver_arg[i], &arg);
                if (ret < 0)
                        goto out;
-               /*
-                * If multiple receivers are given for this audio format, the
-                * last one wins and we have to free the previous receiver
-                * config here. Since we are iterating backwards, the winning
-                * receiver arg is in fact the first one given.
-                */
-               if (afi[ret].receiver_conf)
-                       afi[ret].receiver->free_config(afi[ret].receiver_conf);
-               afi[ret].receiver_conf = check_receiver_arg(recv_arg, &receiver_num);
-               if (!afi[ret].receiver_conf) {
+               af_mask = ret;
+               FOR_EACH_AUDIO_FORMAT(j) {
+                       a = afi + j;
+                       if ((af_mask & (1 << j)) == 0) /* no match */
+                               continue;
+                       /*
+                        * If multiple receivers are given for this audio format, the
+                        * last one wins and we have to free the previous receiver
+                        * config here. Since we are iterating backwards, the winning
+                        * receiver arg is in fact the first one given.
+                        */
+                       if (a->receiver_conf)
+                               a->receiver->free_config(a->receiver_conf);
+                       a->receiver_conf = check_receiver_arg(arg, &receiver_num);
                        ret = -E_RECV_SYNTAX;
-                       goto out;
+                       if (!a->receiver_conf)
+                               goto out;
+                       a->receiver = receivers + receiver_num;
                }
-               afi[ret].receiver = &receivers[receiver_num];
        }
        /*
         * Use the first available receiver with no arguments for those audio
@@ -837,6 +859,11 @@ static int parse_receiver_args(void)
                        return -E_RECV_SYNTAX;
                a->receiver = &receivers[receiver_num];
        }
+       FOR_EACH_AUDIO_FORMAT(i) {
+               a = afi + i;
+               PARA_INFO_LOG("receiving %s streams via %s receiver\n",
+                       audio_formats[i], a->receiver->name);
+       }
        ret = 1;
 out:
        free(cmd);
@@ -855,8 +882,8 @@ static int init_default_filters(void)
                if (a->num_filters)
                        continue; /* no default -- nothing to to */
                /*
-                * If udp is used to receive this audiod format, add fecdec as
-                * the first filter.
+                * udp and dccp streams are fec-encoded, so add fecdec as the
+                * first filter.
                 */
                if (strcmp(afi[i].receiver->name, "udp") == 0 ||
                                strcmp(afi[i].receiver->name, "dccp") == 0) {
@@ -889,30 +916,23 @@ out:
 
 static int parse_filter_args(void)
 {
-       int i, ret, nf;
+       int i, j, ret, af_mask;
 
-       nf = PARA_MAX(2U, 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 *));
-               afi[i].filter_nums = para_malloc(nf * sizeof(unsigned));
-       }
        if (!conf.no_default_filters_given)
                return init_default_filters();
        for (i = 0; i < conf.filter_given; i++) {
-               char *arg = conf.filter_arg[i];
-               char *filter_name = strchr(arg, ':');
-               ret = -E_MISSING_COLON;
-               if (!filter_name)
-                       goto out;
-               *filter_name = '\0';
-               filter_name++;
-               ret = get_audio_format_num(arg);
-               if (ret < 0)
-                       goto out;
-               ret = add_filter(ret, filter_name);
+               char *arg;
+               ret = parse_stream_command(conf.filter_arg[i], &arg);
                if (ret < 0)
                        goto out;
+               af_mask = ret;
+               FOR_EACH_AUDIO_FORMAT(j) {
+                       if ((af_mask & (1 << j)) == 0) /* no match */
+                               continue;
+                       ret = add_filter(j, arg);
+                       if (ret < 0)
+                               goto out;
+               }
        }
        ret = init_default_filters(); /* use default values for the rest */
 out:
@@ -1035,7 +1055,6 @@ static void close_stat_pipe(void)
 {
        if (!stat_task->ct)
                return;
-       btr_free_node(stat_task->ct->btrn);
        client_close(stat_task->ct);
        stat_task->ct = NULL;
        clear_and_dump_items();
@@ -1129,7 +1148,7 @@ static void start_stop_decoders(void)
        if (a->num_filters)
                open_filters(sl);
        open_writers(sl);
-       activate_grab_clients();
+       activate_grab_clients(&sched);
        btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
 }
 
@@ -1162,7 +1181,7 @@ min_delay:
 }
 
 /* restart the client task if necessary */
-static void status_post_select(__a_unused struct sched *s, struct task *t)
+static void status_post_select(struct sched *s, struct task *t)
 {
        struct status_task *st = container_of(t, struct status_task, task);
 
@@ -1224,13 +1243,13 @@ static void status_post_select(__a_unused struct sched *s, struct task *t)
                int argc = 5;
                PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
                st->clock_diff_count--;
-               client_open(argc, argv, &st->ct, NULL, NULL, st->btrn);
+               client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
                set_stat_task_restart_barrier(2);
 
        } else {
                char *argv[] = {"audiod", "--", "stat", "-p", NULL};
                int argc = 4;
-               client_open(argc, argv, &st->ct, NULL, NULL, st->btrn);
+               client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
                set_stat_task_restart_barrier(5);
        }
        free(stat_item_values[SI_BASENAME]);
@@ -1314,7 +1333,6 @@ static void init_colors_or_die(void)
 int main(int argc, char *argv[])
 {
        int ret, i;
-       static struct sched s;
        struct command_task command_task_struct, *cmd_task = &command_task_struct;
        struct audiod_cmdline_parser_params params = {
                .override = 0,
@@ -1353,7 +1371,7 @@ int main(int argc, char *argv[])
                exit(EXIT_FAILURE);
        }
        log_welcome("para_audiod");
-       server_uptime(UPTIME_SET);
+       set_server_start_time(NULL);
        set_initial_status();
        FOR_EACH_SLOT(i)
                clear_slot(i);
@@ -1364,14 +1382,14 @@ int main(int argc, char *argv[])
        init_command_task(cmd_task);
 
        if (conf.daemon_given)
-               daemonize();
-
-       register_task(&sig_task->task);
-       register_task(&cmd_task->task);
-       register_task(&stat_task->task);
-       s.default_timeout.tv_sec = 2;
-       s.default_timeout.tv_usec = 999 * 1000;
-       ret = schedule(&s);
+               daemonize(false /* parent exits immediately */);
+
+       register_task(&sched, &sig_task->task);
+       register_task(&sched, &cmd_task->task);
+       register_task(&sched, &stat_task->task);
+       sched.default_timeout.tv_sec = 2;
+       sched.default_timeout.tv_usec = 999 * 1000;
+       ret = schedule(&sched);
 
        PARA_EMERG_LOG("%s\n", para_strerror(-ret));
        return EXIT_FAILURE;