2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod.c the paraslash's audio daemon */
14 #include "audiod.cmdline.h"
19 #include "grab_client.cmdline.h"
20 #include "grab_client.h"
21 #include "client.cmdline.h"
29 #include "write_common.h"
32 /** define the array of error lists needed by para_audiod */
34 /** define the array containing all supported audio formats */
35 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
37 /** Defines how audiod handles one supported audio format. */
38 struct audio_format_info {
39 /** pointer to the receiver for this audio format */
40 struct receiver *receiver;
41 /** the receiver configuration */
43 /** the number of filters that should be activated for this audio format */
44 unsigned int num_filters;
45 /** Array of filter numbers to be activated. */
46 unsigned *filter_nums;
47 /** Pointer to the array of filter configurations. */
49 /** the number of filters that should be activated for this audio format */
50 unsigned int num_writers;
51 /** pointer to the array of writers to be activated */
52 struct writer **writers;
53 /** pointer to the array of writer configurations */
55 /** do not start receiver/filters/writer before this time */
56 struct timeval restart_barrier;
60 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
61 * be associated with a receiver/filter/writer triple. This array holds all
62 * information on the status of these slots.
64 * \sa struct slot_info
66 struct slot_info slot[MAX_STREAM_SLOTS];
70 * the current mode of operation of which can be changed by the on/off/cycle
71 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
73 int audiod_status = AUDIOD_ON;
76 * the gengetopt args_info struct that holds information on all command line
79 struct audiod_args_info conf;
81 static char *socket_name;
83 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
85 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
87 static struct status_task status_task_struct;
90 * the task that calls the status command of para_server
92 * \sa struct status_task
94 struct status_task *stat_task = &status_task_struct;
95 static struct timeval initial_delay_barrier;
98 * the task for handling audiod commands
100 * \sa struct task, struct sched
102 struct command_task {
103 /** the local listening socket */
105 /** the associated task structure */
109 /** iterate over all supported audio formats */
110 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
113 * get the audio format number
114 * \param name the name of the audio format
116 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
117 * \a name is not a supported audio format.
119 int get_audio_format_num(char *name)
123 while (para_isspace(*name))
125 FOR_EACH_AUDIO_FORMAT(i)
126 if (!strcmp(name, audio_formats[i]))
128 return -E_UNSUPPORTED_AUDIO_FORMAT;
132 * the log function of para_audiod
135 * \param fmt the format string
137 void para_log(int ll, const char* fmt,...)
143 char str[MAXLINE] = "";
144 static char *hostname;
146 if (ll < conf.loglevel_arg)
148 if (!logfile && conf.daemon_given)
151 hostname = para_hostname();
152 outfd = logfile? logfile : stderr;
155 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
156 fprintf(outfd, "%s %s ", str, hostname);
157 if (conf.loglevel_arg <= INFO)
158 fprintf(outfd, "%i ", ll);
160 vfprintf(outfd, fmt, argp);
164 static char *configfile_exists(void)
166 char *home = para_homedir();
167 char *config_file = make_message("%s/.paraslash/audiod.conf",
170 if (file_exists(config_file))
176 static void setup_signal_handling(void)
178 sig_task->fd = para_signal_init();
179 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
180 para_install_sighandler(SIGINT);
181 para_install_sighandler(SIGTERM);
182 para_install_sighandler(SIGHUP);
183 signal(SIGPIPE, SIG_IGN);
186 static void clear_slot(int slot_num)
188 struct slot_info *s = &slot[slot_num];
190 PARA_INFO_LOG("clearing slot %d\n", slot_num);
191 memset(s, 0, sizeof(struct slot_info));
195 static void close_receiver(int slot_num)
197 struct slot_info *s = &slot[slot_num];
198 struct audio_format_info *a;
200 if (s->format < 0 || !s->receiver_node)
203 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
204 audio_formats[s->format], slot_num);
205 a->receiver->close(s->receiver_node);
206 free(s->receiver_node);
207 s->receiver_node = NULL;
210 static void kill_all_decoders(int error)
215 struct slot_info *s = &slot[i];
216 if (s->wng && s->wng->task.error >= 0) {
217 PARA_INFO_LOG("deactivating wng in slot %d\n",
219 s->wng->task.error = error;
221 if (s->fc && s->fc->task.error >= 0) {
222 PARA_INFO_LOG("deactivatimg filter chain in slot %d\n", i);
223 s->fc->task.error = error;
225 if (s->receiver_node && s->receiver_node->task.error >= 0) {
226 PARA_INFO_LOG("deactivating receiver_node in slot %d\n", i);
227 s->receiver_node->task.error = error;
232 static int get_empty_slot(void)
243 if (s->wng || s->receiver_node || s->fc)
248 return -E_NO_MORE_SLOTS;
252 * get the number of filters
254 * \param audio_format_num the number identifying the audio format
256 * \return the number of filters for the given audio format
260 int num_filters(int audio_format_num)
262 return afi[audio_format_num].num_filters;
265 static void open_filters(int slot_num)
267 struct slot_info *s = &slot[slot_num];
268 struct audio_format_info *a = &afi[s->format];
269 struct filter_node *fn;
270 int nf = a->num_filters;
276 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
277 s->fc = para_calloc(sizeof(struct filter_chain));
278 s->fc->filter_nodes = para_malloc(nf * sizeof(struct filter_node));
279 s->fc->inbuf = s->receiver_node->buf;
280 s->fc->in_loaded = &s->receiver_node->loaded;
281 s->fc->input_error = &s->receiver_node->task.error;
282 s->fc->task.pre_select = filter_pre_select;
283 s->fc->task.post_select = NULL;
284 s->fc->task.error = 0;
285 s->fc->num_filters = nf;
287 s->receiver_node->output_error = &s->fc->task.error;
288 sprintf(s->fc->task.status, "filter chain");
289 FOR_EACH_FILTER_NODE(fn, s->fc, i) {
290 struct filter *f = filters + a->filter_nums[i];
291 fn->filter_num = a->filter_nums[i];
292 fn->conf = a->filter_conf[i];
295 INIT_LIST_HEAD(&fn->callbacks);
297 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
298 audio_formats[s->format], i, nf, f->name, slot_num);
299 s->fc->outbuf = fn->buf;
300 s->fc->out_loaded = &fn->loaded;
302 register_task(&s->fc->task);
305 static void open_writers(int slot_num)
308 struct slot_info *s = &slot[slot_num];
309 struct audio_format_info *a = &afi[s->format];
311 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
313 s->wng = setup_default_wng();
315 s->wng = wng_new(a->num_writers);
317 s->wng->buf = s->fc->outbuf;
318 s->wng->loaded = s->fc->out_loaded;
319 s->wng->input_error = &s->fc->task.error;
320 s->wng->channels = &s->fc->channels;
321 s->wng->samplerate = &s->fc->samplerate;
322 s->fc->output_error = &s->wng->task.error;
323 PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
325 s->wng->buf = s->receiver_node->buf;
326 s->wng->loaded = &s->receiver_node->loaded;
327 s->wng->input_error = &s->receiver_node->task.error;
329 for (i = 0; i < a->num_writers; i++) {
330 s->wng->writer_nodes[i].conf = a->writer_conf[i];
331 s->wng->writer_nodes[i].writer = a->writers[i];
333 ret = wng_open(s->wng);
335 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
339 activate_inactive_grab_clients(slot_num, s->format, s->fc);
342 static int open_receiver(int format)
344 struct audio_format_info *a = &afi[format];
347 struct receiver_node *rn;
348 const struct timeval restart_delay = {2, 0};
350 ret = get_empty_slot();
356 s->receiver_node = para_calloc(sizeof(struct receiver_node));
357 rn = s->receiver_node;
358 rn->receiver = a->receiver;
359 rn->conf = a->receiver_conf;
360 ret = a->receiver->open(s->receiver_node);
362 free(s->receiver_node);
363 s->receiver_node = NULL;
366 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
367 audio_formats[s->format], a->receiver->name, slot_num);
368 rn->task.pre_select = a->receiver->pre_select;
369 rn->task.post_select = a->receiver->post_select;
370 sprintf(rn->task.status, "%s receiver node", rn->receiver->name);
371 register_task(&rn->task);
375 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
376 tv_add(now, &restart_delay, &afi[format].restart_barrier);
380 static int receiver_running(int format)
385 struct slot_info *s = &slot[i];
386 if (s->format == format && s->receiver_node)
392 static int open_current_receiver(struct sched *s)
395 int cafn = stat_task->current_audio_format_num;
397 if (cafn < 0 || !stat_task->ct)
399 if (receiver_running(cafn))
401 if (tv_diff(now, &afi[cafn].restart_barrier, &diff) < 0) {
405 return open_receiver(cafn) < 0? 0 : 1;
408 static unsigned compute_time_diff(const struct timeval *status_time)
410 struct timeval tmp, diff;
411 static unsigned count;
412 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
413 const struct timeval max_deviation = {0, 500 * 1000};
414 const int time_smooth = 5;
418 sign = tv_diff(status_time, now, &diff);
419 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
420 // sign, sa_time_diff_sign);
422 sa_time_diff_sign = sign;
423 stat_task->sa_time_diff = diff;
428 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
429 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
430 PARA_WARNING_LOG("time diff jump: %lims\n",
434 sa_time_diff_sign = tv_convex_combination(
435 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
436 count > 10? sign : sign * time_smooth, &diff,
438 stat_task->sa_time_diff = tmp;
439 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
442 sa_time_diff_sign ? "+" : "-",
443 tv2ms(&stat_task->sa_time_diff)
446 stat_task->sa_time_diff_sign = sa_time_diff_sign;
450 static int check_stat_line(char *line, __a_unused void *data)
454 long unsigned sec, usec;
457 //PARA_INFO_LOG("line: %s\n", line);
460 itemnum = stat_line_valid(line);
462 PARA_WARNING_LOG("invalid status line: %s\n", line);
465 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
467 tmp = make_message("%s\n", line);
468 stat_client_write(tmp, itemnum);
470 free(stat_task->stat_item_values[itemnum]);
471 stat_task->stat_item_values[itemnum] = para_strdup(line);
472 ilen = strlen(status_item_list[itemnum]);
475 stat_task->playing = strstr(line, "playing")? 1 : 0;
476 PARA_INFO_LOG("stat task playing: %d\n", stat_task->playing);
479 stat_task->offset_seconds = atoi(line + ilen + 1);
481 case SI_SECONDS_TOTAL:
482 stat_task->length_seconds = atoi(line + ilen + 1);
484 case SI_STREAM_START:
485 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
486 struct timeval a_start, delay;
487 delay.tv_sec = conf.stream_delay_arg / 1000;
488 delay.tv_usec = (conf.stream_delay_arg % 1000) * 1000;
489 stat_task->server_stream_start.tv_sec = sec;
490 stat_task->server_stream_start.tv_usec = usec;
491 if (compute_time_diff(NULL) > 2) {
492 if (stat_task->sa_time_diff_sign < 0)
493 tv_add(&stat_task->server_stream_start,
494 &stat_task->sa_time_diff, &a_start);
496 tv_diff(&stat_task->server_stream_start,
497 &stat_task->sa_time_diff, &a_start);
498 tv_add(&a_start, &delay, &initial_delay_barrier);
502 case SI_CURRENT_TIME:
503 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
504 struct timeval tv = {sec, usec};
505 compute_time_diff(&tv);
509 stat_task->current_audio_format_num = get_audio_format_num(
515 static int parse_stream_command(const char *txt, char **cmd)
517 char *p = strchr(txt, ':');
521 return -E_MISSING_COLON;
523 FOR_EACH_AUDIO_FORMAT(i) {
524 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
529 return -E_UNSUPPORTED_AUDIO_FORMAT;
532 static int add_filter(int format, char *cmdline)
534 struct audio_format_info *a = &afi[format];
535 int filter_num, nf = a->num_filters;
537 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
540 a->filter_nums[nf] = filter_num;
542 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
543 filters[filter_num].name);
547 static int init_writers(void)
551 struct audio_format_info *a;
553 init_supported_writers();
554 nw = PARA_MAX(1, conf.writer_given);
555 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
556 FOR_EACH_AUDIO_FORMAT(i) {
558 a->writer_conf = para_malloc(nw * sizeof(void *));
559 a->writers = para_malloc(nw * sizeof(struct writer *));
562 for (i = 0; i < conf.writer_given; i++) {
565 ret = parse_stream_command(conf.writer_arg[i], &cmd);
570 wconf = check_writer_arg(cmd, &writer_num);
575 a->writers[nw] = &writers[writer_num];
576 a->writer_conf[nw] = wconf;
577 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
578 nw, writer_names[writer_num]);
586 static int init_receivers(void)
588 int i, ret, receiver_num;
590 struct audio_format_info *a;
592 for (i = 0; receivers[i].name; i++) {
593 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
594 receivers[i].init(&receivers[i]);
596 for (i = conf.receiver_given - 1; i >= 0; i--) {
597 char *arg = conf.receiver_arg[i];
598 char *recv_arg = strchr(arg, ':');
599 ret = -E_MISSING_COLON;
604 ret = get_audio_format_num(arg);
607 afi[ret].receiver_conf = check_receiver_arg(recv_arg, &receiver_num);
608 if (!afi[ret].receiver_conf) {
609 ret = -E_RECV_SYNTAX;
612 afi[ret].receiver = &receivers[receiver_num];
614 /* use the first available receiver with no arguments
615 * for those audio formats for which no receiver
618 cmd = para_strdup(receivers[0].name);
619 FOR_EACH_AUDIO_FORMAT(i) {
621 if (a->receiver_conf)
623 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
624 if (!a->receiver_conf)
625 return -E_RECV_SYNTAX;
626 a->receiver = &receivers[receiver_num];
634 static int init_default_filters(void)
638 FOR_EACH_AUDIO_FORMAT(i) {
639 struct audio_format_info *a = &afi[i];
644 continue; /* no default -- nothing to to */
645 /* add "dec" to audio format name */
646 tmp = make_message("%sdec", audio_formats[i]);
647 for (j = 0; filters[j].name; j++)
648 if (!strcmp(tmp, filters[j].name))
651 ret = -E_UNSUPPORTED_FILTER;
652 if (!filters[j].name)
654 tmp = para_strdup(filters[j].name);
655 ret = add_filter(i, tmp);
659 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
666 static int init_filters(void)
670 filter_init(filters);
671 nf = PARA_MAX(1, conf.filter_given);
672 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
673 FOR_EACH_AUDIO_FORMAT(i) {
674 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
675 afi[i].filter_nums = para_malloc(nf * sizeof(unsigned));
677 if (!conf.no_default_filters_given)
678 return init_default_filters();
679 for (i = 0; i < conf.filter_given; i++) {
680 char *arg = conf.filter_arg[i];
681 char *filter_name = strchr(arg, ':');
682 ret = -E_MISSING_COLON;
687 ret = get_audio_format_num(arg);
690 ret = add_filter(ret, filter_name);
694 ret = init_default_filters(); /* use default values for the rest */
699 static int init_stream_io(void)
703 ret = init_writers();
706 ret = init_receivers();
709 ret = init_filters();
715 /* does not unlink socket on errors */
716 static int audiod_get_socket(void)
718 struct sockaddr_un unix_addr;
721 if (conf.socket_given)
722 socket_name = para_strdup(conf.socket_arg);
724 char *hn = para_hostname();
725 socket_name = make_message("/var/paraslash/audiod_socket.%s",
729 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
730 if (conf.force_given)
732 ret = create_local_socket(socket_name, &unix_addr,
733 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
737 if (listen(fd , 5) < 0) {
738 ret = -ERRNO_TO_PARA_ERROR(errno);
741 ret = mark_fd_nonblocking(fd);
746 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
750 static void signal_pre_select(struct sched *s, struct task *t)
752 struct signal_task *st = container_of(t, struct signal_task, task);
753 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
756 static void signal_post_select(struct sched *s, struct task *t)
758 struct signal_task *st = container_of(t, struct signal_task, task);
760 if (!FD_ISSET(st->fd, &s->rfds))
763 st->signum = para_next_signal();
764 switch (st->signum) {
768 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
769 clean_exit(EXIT_FAILURE, "caught deadly signal");
773 static void signal_setup_default(struct signal_task *st)
775 st->task.pre_select = signal_pre_select;
776 st->task.post_select = signal_post_select;
777 sprintf(st->task.status, "signal task");
780 static void command_pre_select(struct sched *s, struct task *t)
782 struct command_task *ct = container_of(t, struct command_task, task);
783 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
786 static void command_post_select(struct sched *s, struct task *t)
789 struct command_task *ct = container_of(t, struct command_task, task);
791 audiod_status_dump();
792 if (!FD_ISSET(ct->fd, &s->rfds))
794 ret = handle_connect(ct->fd);
796 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
799 static void init_command_task(struct command_task *ct)
801 ct->task.pre_select = command_pre_select;
802 ct->task.post_select = command_post_select;
804 ct->fd = audiod_get_socket(); /* doesn't return on errors */
805 sprintf(ct->task.status, "command task");
808 static void close_stat_pipe(void)
814 client_close(stat_task->ct);
815 stat_task->ct = NULL;
816 FOR_EACH_STATUS_ITEM(i) {
817 free(stat_task->stat_item_values[i]);
818 stat_task->stat_item_values[i] = NULL;
821 stat_task->length_seconds = 0;
822 stat_task->offset_seconds = 0;
823 audiod_status_dump();
824 stat_task->playing = 0;
825 stat_task->stat_item_values[SI_BASENAME] = make_message(
826 "%s: no connection to para_server\n",
827 status_item_list[SI_BASENAME]);
828 stat_client_write(stat_task->stat_item_values[SI_BASENAME],
833 * close the connection to para_server and exit
835 * \param status the exit status which is passed to exit(3)
836 * \param msg the log message
838 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
839 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
844 void __noreturn clean_exit(int status, const char *msg)
846 PARA_EMERG_LOG("%s\n", msg);
853 /* avoid busy loop if server is down */
854 static void set_stat_task_restart_barrier(unsigned seconds)
856 struct timeval delay = {seconds, 0};
857 tv_add(now, &delay, &stat_task->restart_barrier);
860 static void try_to_close_slot(int slot_num)
862 struct slot_info *s = &slot[slot_num];
866 if (s->receiver_node && s->receiver_node->task.error != -E_TASK_UNREGISTERED)
868 if (s->fc && s->fc->task.error != -E_TASK_UNREGISTERED)
870 if (s->wng && s->wng->task.error != -E_TASK_UNREGISTERED)
872 PARA_INFO_LOG("closing slot %d\n", slot_num);
874 close_filters(s->fc);
876 close_receiver(slot_num);
877 clear_slot(slot_num);
881 * Check if any receivers/filters/writers need to be started and do so if
884 static void start_stop_decoders(struct sched *s)
889 try_to_close_slot(i);
890 if (audiod_status != AUDIOD_ON || !stat_task->playing)
891 return kill_all_decoders(-E_NOT_PLAYING);
892 open_current_receiver(s);
894 struct slot_info *sl = &slot[i];
895 struct audio_format_info *a;
900 a = &afi[sl->format];
901 if (!sl->receiver_node)
903 if ((!a->num_filters || sl->fc) && sl->wng)
904 continue; /* everything already started */
905 if (!a->num_filters) {
906 if (sl->receiver_node->loaded && !sl->wng) {
911 if (sl->receiver_node->loaded && !sl->fc) {
915 if (sl->wng || !sl->fc || !*sl->fc->out_loaded)
917 if (tv_diff(now, &initial_delay_barrier, &diff) > 0) {
921 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff));
922 if (tv_diff(&s->timeout, &diff, NULL) > 0) {
929 /* restart the client task if necessary */
930 static void status_pre_select(struct sched *s, struct task *t)
932 struct status_task *st = container_of(t, struct status_task, task);
934 if (audiod_status == AUDIOD_OFF) {
937 if (st->ct->task.error >= 0) {
938 st->ct->task.error = -E_AUDIOD_OFF;
941 if (st->ct->task.error != -E_TASK_UNREGISTERED)
944 st->clock_diff_count = conf.clock_diff_count_arg;
949 if (st->ct->task.error < 0) {
950 if (st->ct->task.error != -E_TASK_UNREGISTERED)
955 if (st->ct->status != CL_RECEIVING)
957 bytes_left = for_each_line(st->ct->buf, st->ct->loaded,
958 &check_stat_line, NULL);
959 if (st->ct->loaded != bytes_left) {
960 st->last_status_read = *now;
961 st->ct->loaded = bytes_left;
964 tv_diff(now, &st->last_status_read, &diff);
965 if (diff.tv_sec > 61)
966 st->ct->task.error = -E_STATUS_TIMEOUT;
970 if (tv_diff(now, &st->restart_barrier, NULL) < 0)
972 if (st->clock_diff_count) { /* get status only one time */
973 char *argv[] = {"audiod", "stat", "1", NULL};
975 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
976 st->clock_diff_count--;
977 client_open(argc, argv, &st->ct);
978 set_stat_task_restart_barrier(2);
981 char *argv[] = {"audiod", "stat", NULL};
983 client_open(argc, argv, &st->ct);
984 set_stat_task_restart_barrier(5);
986 st->last_status_read = *now;
988 start_stop_decoders(s);
991 static void init_status_task(struct status_task *st)
993 memset(st, 0, sizeof(struct status_task));
994 st->task.pre_select = status_pre_select;
995 st->sa_time_diff_sign = 1;
996 st->clock_diff_count = conf.clock_diff_count_arg;
997 st->current_audio_format_num = -1;
998 sprintf(st->task.status, "status task");
1001 static void set_initial_status(void)
1003 audiod_status = AUDIOD_ON;
1004 if (!conf.mode_given)
1006 if (!strcmp(conf.mode_arg, "sb")) {
1007 audiod_status = AUDIOD_STANDBY;
1010 if (!strcmp(conf.mode_arg, "off")) {
1011 audiod_status = AUDIOD_OFF;
1014 if (strcmp(conf.mode_arg, "on"))
1015 PARA_WARNING_LOG("invalid mode\n");
1019 * the main function of para_audiod
1021 * \param argc usual argument count
1022 * \param argv usual argument vector
1024 * \return EXIT_SUCCESS or EXIT_FAILURE
1026 * \sa para_audiod(1)
1028 int main(int argc, char *argv[])
1032 static struct sched s;
1033 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1034 struct audiod_cmdline_parser_params params = {
1037 .check_required = 0,
1038 .check_ambiguity = 0,
1043 audiod_cmdline_parser_ext(argc, argv, &conf, ¶ms);
1044 HANDLE_VERSION_FLAG("audiod", conf);
1045 para_drop_privileges(conf.user_arg, conf.group_arg);
1046 config_file = configfile_exists();
1048 params.override = 0;
1049 params.initialize = 0;
1050 params.check_required = 1;
1051 params.check_ambiguity = 0;
1052 params.print_errors = 1;
1053 if (audiod_cmdline_parser_config_file(config_file, &conf, ¶ms)) {
1054 PARA_EMERG_LOG("parse error in config file\n");
1059 if (conf.logfile_given)
1060 logfile = open_log(conf.logfile_arg);
1061 log_welcome("para_audiod", conf.loglevel_arg);
1062 i = init_stream_io();
1064 PARA_EMERG_LOG("init stream io error: %s\n", para_strerror(-i));
1067 server_uptime(UPTIME_SET);
1068 set_initial_status();
1072 setup_signal_handling();
1073 signal_setup_default(sig_task);
1075 init_status_task(stat_task);
1076 init_command_task(cmd_task);
1078 if (conf.daemon_given)
1081 register_task(&sig_task->task);
1082 register_task(&cmd_task->task);
1083 register_task(&stat_task->task);
1084 s.default_timeout.tv_sec = 0;
1085 s.default_timeout.tv_usec = 99 * 1000;
1088 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1089 return EXIT_FAILURE;