2 * Copyright (C) 2005-2009 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"
20 #include "grab_client.cmdline.h"
21 #include "grab_client.h"
22 #include "client.cmdline.h"
30 #include "write_common.h"
33 /** define the array of error lists needed by para_audiod */
35 /** define the array containing all supported audio formats */
36 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
38 /** Defines how audiod handles one supported audio format. */
39 struct audio_format_info {
40 /** pointer to the receiver for this audio format */
41 struct receiver *receiver;
42 /** the receiver configuration */
44 /** the number of filters that should be activated for this audio format */
45 unsigned int num_filters;
46 /** Array of filter numbers to be activated. */
47 unsigned *filter_nums;
48 /** Pointer to the array of filter configurations. */
50 /** the number of filters that should be activated for this audio format */
51 unsigned int num_writers;
52 /** pointer to the array of writers to be activated */
53 struct writer **writers;
54 /** pointer to the array of writer configurations */
56 /** do not start receiver/filters/writer before this time */
57 struct timeval restart_barrier;
61 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
62 * be associated with a receiver/filter/writer triple. This array holds all
63 * information on the status of these slots.
65 * \sa struct slot_info
67 struct slot_info slot[MAX_STREAM_SLOTS];
70 /** The array of status items sent by para_server. */
71 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
74 * the current mode of operation of which can be changed by the on/off/cycle
75 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
77 int audiod_status = AUDIOD_ON;
80 * the gengetopt args_info struct that holds information on all command line
83 struct audiod_args_info conf;
85 static char *socket_name;
86 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
88 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
90 static struct status_task status_task_struct;
93 * the task that calls the status command of para_server
95 * \sa struct status_task
97 static struct status_task *stat_task = &status_task_struct;
98 static struct timeval initial_delay_barrier;
101 * the task for handling audiod commands
103 * \sa struct task, struct sched
105 struct command_task {
106 /** the local listening socket */
108 /** the associated task structure */
112 /** iterate over all supported audio formats */
113 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
116 * get the audio format number
117 * \param name the name of the audio format
119 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
120 * \a name is not a supported audio format.
122 int get_audio_format_num(char *name)
126 while (para_isspace(*name))
128 FOR_EACH_AUDIO_FORMAT(i)
129 if (!strcmp(name, audio_formats[i]))
131 return -E_UNSUPPORTED_AUDIO_FORMAT;
134 char *get_time_string(struct timeval *newest_stime)
136 struct timeval diff, adj_stream_start, tmp;
137 int total = 0, use_server_time = 1,
138 length_seconds = stat_task->length_seconds;
140 if (!stat_task->playing) {
143 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
145 if (audiod_status == AUDIOD_OFF)
147 if (stat_task->sa_time_diff_sign > 0)
148 tv_diff(&stat_task->server_stream_start, &stat_task->sa_time_diff,
151 tv_add(&stat_task->server_stream_start, &stat_task->sa_time_diff,
153 tmp = adj_stream_start;
154 if (newest_stime && audiod_status == AUDIOD_ON) {
155 tv_diff(newest_stime, &adj_stream_start, &diff);
156 if (tv2ms(&diff) < 5000) {
161 tv_diff(now, &tmp, &diff);
162 total = diff.tv_sec + stat_task->offset_seconds;
163 if (total > length_seconds)
164 total = length_seconds;
169 "%s: %s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
170 status_item_list[SI_PLAY_TIME],
171 use_server_time? "~" : "",
174 (length_seconds - total) / 60,
175 (length_seconds - total) % 60,
176 length_seconds? (total * 100 + length_seconds / 2) /
183 static int want_colors(void)
185 if (conf.color_arg == color_arg_no)
187 if (conf.color_arg == color_arg_yes)
189 if (conf.logfile_given)
191 return isatty(STDERR_FILENO);
194 static void parse_config_or_die(void)
198 struct audiod_cmdline_parser_params params = {
202 .check_ambiguity = 0,
206 if (conf.config_file_given)
207 config_file = para_strdup(conf.config_file_arg);
209 char *home = para_homedir();
210 config_file = make_message("%s/.paraslash/audiod.conf", home);
213 ret = file_exists(config_file);
214 if (conf.config_file_given && !ret) {
215 PARA_EMERG_LOG("can not read config file %s\n", config_file);
219 audiod_cmdline_parser_config_file(config_file, &conf, ¶ms);
221 daemon_set_loglevel(conf.loglevel_arg);
228 static void setup_signal_handling(void)
230 sig_task->fd = para_signal_init();
231 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
232 para_install_sighandler(SIGINT);
233 para_install_sighandler(SIGTERM);
234 para_install_sighandler(SIGHUP);
235 signal(SIGPIPE, SIG_IGN);
238 static void clear_slot(int slot_num)
240 struct slot_info *s = &slot[slot_num];
242 PARA_INFO_LOG("clearing slot %d\n", slot_num);
243 memset(s, 0, sizeof(struct slot_info));
247 static void close_receiver(int slot_num)
249 struct slot_info *s = &slot[slot_num];
250 struct audio_format_info *a;
252 if (s->format < 0 || !s->receiver_node)
255 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
256 audio_formats[s->format], slot_num);
257 a->receiver->close(s->receiver_node);
258 free(s->receiver_node);
259 s->receiver_node = NULL;
262 static void kill_all_decoders(int error)
267 struct slot_info *s = &slot[i];
268 if (s->wng && s->wng->task.error >= 0) {
269 PARA_INFO_LOG("deactivating wng in slot %d\n",
271 s->wng->task.error = error;
273 if (s->fc && s->fc->task.error >= 0) {
274 PARA_INFO_LOG("deactivatimg filter chain in slot %d\n", i);
275 s->fc->task.error = error;
277 if (s->receiver_node && s->receiver_node->task.error >= 0) {
278 PARA_INFO_LOG("deactivating receiver_node in slot %d\n", i);
279 s->receiver_node->task.error = error;
284 static int get_empty_slot(void)
295 if (s->wng || s->receiver_node || s->fc)
300 return -E_NO_MORE_SLOTS;
304 * get the number of filters
306 * \param audio_format_num the number identifying the audio format
308 * \return the number of filters for the given audio format
312 int num_filters(int audio_format_num)
314 return afi[audio_format_num].num_filters;
317 static void open_filters(int slot_num)
319 struct slot_info *s = &slot[slot_num];
320 struct audio_format_info *a = &afi[s->format];
321 struct filter_node *fn;
322 int nf = a->num_filters;
328 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
329 s->fc = para_calloc(sizeof(struct filter_chain));
330 s->fc->filter_nodes = para_malloc(nf * sizeof(struct filter_node));
331 s->fc->inbuf = s->receiver_node->buf;
332 s->fc->in_loaded = &s->receiver_node->loaded;
333 s->fc->input_error = &s->receiver_node->task.error;
334 s->fc->task.pre_select = filter_pre_select;
335 s->fc->task.post_select = NULL;
336 s->fc->task.error = 0;
337 s->fc->num_filters = nf;
339 s->receiver_node->output_error = &s->fc->task.error;
340 sprintf(s->fc->task.status, "filter chain");
341 FOR_EACH_FILTER_NODE(fn, s->fc, i) {
342 struct filter *f = filters + a->filter_nums[i];
343 fn->filter_num = a->filter_nums[i];
344 fn->conf = a->filter_conf[i];
347 INIT_LIST_HEAD(&fn->callbacks);
349 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
350 audio_formats[s->format], i, nf, f->name, slot_num);
351 s->fc->outbuf = fn->buf;
352 s->fc->out_loaded = &fn->loaded;
354 register_task(&s->fc->task);
357 static void open_writers(int slot_num)
360 struct slot_info *s = &slot[slot_num];
361 struct audio_format_info *a = &afi[s->format];
363 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
365 s->wng = setup_default_wng();
367 s->wng = wng_new(a->num_writers);
369 s->wng->buf = s->fc->outbuf;
370 s->wng->loaded = s->fc->out_loaded;
371 s->wng->input_error = &s->fc->task.error;
372 s->wng->channels = &s->fc->channels;
373 s->wng->samplerate = &s->fc->samplerate;
374 s->fc->output_error = &s->wng->task.error;
375 PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
377 s->wng->buf = s->receiver_node->buf;
378 s->wng->loaded = &s->receiver_node->loaded;
379 s->wng->input_error = &s->receiver_node->task.error;
381 for (i = 0; i < a->num_writers; i++) {
382 s->wng->writer_nodes[i].conf = a->writer_conf[i];
383 s->wng->writer_nodes[i].writer = a->writers[i];
385 ret = wng_open(s->wng);
387 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
391 activate_inactive_grab_clients(slot_num, s->format, s->fc);
394 static int open_receiver(int format)
396 struct audio_format_info *a = &afi[format];
399 struct receiver_node *rn;
400 const struct timeval restart_delay = {2, 0};
402 ret = get_empty_slot();
408 s->receiver_node = para_calloc(sizeof(struct receiver_node));
409 rn = s->receiver_node;
410 rn->receiver = a->receiver;
411 rn->conf = a->receiver_conf;
412 ret = a->receiver->open(s->receiver_node);
414 free(s->receiver_node);
415 s->receiver_node = NULL;
418 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
419 audio_formats[s->format], a->receiver->name, slot_num);
420 rn->task.pre_select = a->receiver->pre_select;
421 rn->task.post_select = a->receiver->post_select;
422 sprintf(rn->task.status, "%s receiver node", rn->receiver->name);
423 register_task(&rn->task);
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
428 tv_add(now, &restart_delay, &afi[format].restart_barrier);
432 static int receiver_running(int format)
437 struct slot_info *s = &slot[i];
438 if (s->format == format && s->receiver_node)
444 static int open_current_receiver(struct sched *s)
447 int cafn = stat_task->current_audio_format_num;
449 if (cafn < 0 || !stat_task->ct)
451 if (receiver_running(cafn))
453 if (tv_diff(now, &afi[cafn].restart_barrier, &diff) < 0) {
457 return open_receiver(cafn) < 0? 0 : 1;
460 static unsigned compute_time_diff(const struct timeval *status_time)
462 struct timeval tmp, diff;
463 static unsigned count;
464 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
465 const struct timeval max_deviation = {0, 500 * 1000};
466 const int time_smooth = 5;
470 sign = tv_diff(status_time, now, &diff);
471 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
472 // sign, sa_time_diff_sign);
474 sa_time_diff_sign = sign;
475 stat_task->sa_time_diff = diff;
480 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
481 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
482 PARA_WARNING_LOG("time diff jump: %lims\n",
486 sa_time_diff_sign = tv_convex_combination(
487 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
488 count > 10? sign : sign * time_smooth, &diff,
490 stat_task->sa_time_diff = tmp;
491 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
494 sa_time_diff_sign ? "+" : "-",
495 tv2ms(&stat_task->sa_time_diff)
498 stat_task->sa_time_diff_sign = sa_time_diff_sign;
502 static int check_stat_line(char *line, __a_unused void *data)
506 long unsigned sec, usec;
509 //PARA_INFO_LOG("line: %s\n", line);
512 itemnum = stat_line_valid(line);
514 PARA_WARNING_LOG("invalid status line: %s\n", line);
517 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
519 tmp = make_message("%s\n", line);
520 stat_client_write(tmp, itemnum);
522 free(stat_item_values[itemnum]);
523 stat_item_values[itemnum] = para_strdup(line);
524 ilen = strlen(status_item_list[itemnum]);
527 stat_task->playing = strstr(line, "playing")? 1 : 0;
528 PARA_INFO_LOG("stat task playing: %d\n", stat_task->playing);
531 stat_task->offset_seconds = atoi(line + ilen + 1);
533 case SI_SECONDS_TOTAL:
534 stat_task->length_seconds = atoi(line + ilen + 1);
536 case SI_STREAM_START:
537 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
538 struct timeval a_start, delay;
539 delay.tv_sec = conf.stream_delay_arg / 1000;
540 delay.tv_usec = (conf.stream_delay_arg % 1000) * 1000;
541 stat_task->server_stream_start.tv_sec = sec;
542 stat_task->server_stream_start.tv_usec = usec;
543 if (compute_time_diff(NULL) > 2) {
544 if (stat_task->sa_time_diff_sign < 0)
545 tv_add(&stat_task->server_stream_start,
546 &stat_task->sa_time_diff, &a_start);
548 tv_diff(&stat_task->server_stream_start,
549 &stat_task->sa_time_diff, &a_start);
550 tv_add(&a_start, &delay, &initial_delay_barrier);
554 case SI_CURRENT_TIME:
555 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
556 struct timeval tv = {sec, usec};
557 compute_time_diff(&tv);
561 stat_task->current_audio_format_num = get_audio_format_num(
567 static int parse_stream_command(const char *txt, char **cmd)
569 char *p = strchr(txt, ':');
573 return -E_MISSING_COLON;
575 FOR_EACH_AUDIO_FORMAT(i) {
576 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
581 return -E_UNSUPPORTED_AUDIO_FORMAT;
584 static int add_filter(int format, char *cmdline)
586 struct audio_format_info *a = &afi[format];
587 int filter_num, nf = a->num_filters;
589 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
592 a->filter_nums[nf] = filter_num;
594 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
595 filters[filter_num].name);
599 static int parse_writer_args(void)
603 struct audio_format_info *a;
605 nw = PARA_MAX(1U, conf.writer_given);
606 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
607 FOR_EACH_AUDIO_FORMAT(i) {
609 a->writer_conf = para_malloc(nw * sizeof(void *));
610 a->writers = para_malloc(nw * sizeof(struct writer *));
613 for (i = 0; i < conf.writer_given; i++) {
616 ret = parse_stream_command(conf.writer_arg[i], &cmd);
621 wconf = check_writer_arg(cmd, &writer_num);
626 a->writers[nw] = &writers[writer_num];
627 a->writer_conf[nw] = wconf;
628 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
629 nw, writer_names[writer_num]);
637 static int parse_receiver_args(void)
639 int i, ret, receiver_num;
641 struct audio_format_info *a;
643 for (i = conf.receiver_given - 1; i >= 0; i--) {
644 char *arg = conf.receiver_arg[i];
645 char *recv_arg = strchr(arg, ':');
646 ret = -E_MISSING_COLON;
651 ret = get_audio_format_num(arg);
654 afi[ret].receiver_conf = check_receiver_arg(recv_arg, &receiver_num);
655 if (!afi[ret].receiver_conf) {
656 ret = -E_RECV_SYNTAX;
659 afi[ret].receiver = &receivers[receiver_num];
661 /* use the first available receiver with no arguments
662 * for those audio formats for which no receiver
665 cmd = para_strdup(receivers[0].name);
666 FOR_EACH_AUDIO_FORMAT(i) {
668 if (a->receiver_conf)
670 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
671 if (!a->receiver_conf)
672 return -E_RECV_SYNTAX;
673 a->receiver = &receivers[receiver_num];
681 static int init_default_filters(void)
685 FOR_EACH_AUDIO_FORMAT(i) {
686 struct audio_format_info *a = &afi[i];
691 continue; /* no default -- nothing to to */
692 /* add "dec" to audio format name */
693 tmp = make_message("%sdec", audio_formats[i]);
694 for (j = 0; filters[j].name; j++)
695 if (!strcmp(tmp, filters[j].name))
698 ret = -E_UNSUPPORTED_FILTER;
699 if (!filters[j].name)
701 tmp = para_strdup(filters[j].name);
702 ret = add_filter(i, tmp);
706 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
713 static int parse_filter_args(void)
717 nf = PARA_MAX(1U, conf.filter_given);
718 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
719 FOR_EACH_AUDIO_FORMAT(i) {
720 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
721 afi[i].filter_nums = para_malloc(nf * sizeof(unsigned));
723 if (!conf.no_default_filters_given)
724 return init_default_filters();
725 for (i = 0; i < conf.filter_given; i++) {
726 char *arg = conf.filter_arg[i];
727 char *filter_name = strchr(arg, ':');
728 ret = -E_MISSING_COLON;
733 ret = get_audio_format_num(arg);
736 ret = add_filter(ret, filter_name);
740 ret = init_default_filters(); /* use default values for the rest */
745 static int parse_stream_args(void)
749 ret = parse_receiver_args();
752 ret = parse_filter_args();
755 ret = parse_writer_args();
761 /* does not unlink socket on errors */
762 static int audiod_get_socket(void)
764 struct sockaddr_un unix_addr;
767 if (conf.socket_given)
768 socket_name = para_strdup(conf.socket_arg);
770 char *hn = para_hostname();
771 socket_name = make_message("/var/paraslash/audiod_socket.%s",
775 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
776 if (conf.force_given)
778 ret = create_local_socket(socket_name, &unix_addr,
779 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
783 if (listen(fd , 5) < 0) {
784 ret = -ERRNO_TO_PARA_ERROR(errno);
787 ret = mark_fd_nonblocking(fd);
792 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
796 static void signal_pre_select(struct sched *s, struct task *t)
798 struct signal_task *st = container_of(t, struct signal_task, task);
799 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
802 static void signal_post_select(struct sched *s, struct task *t)
804 struct signal_task *st = container_of(t, struct signal_task, task);
806 if (!FD_ISSET(st->fd, &s->rfds))
809 st->signum = para_next_signal();
810 switch (st->signum) {
814 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
815 clean_exit(EXIT_FAILURE, "caught deadly signal");
819 static void signal_setup_default(struct signal_task *st)
821 st->task.pre_select = signal_pre_select;
822 st->task.post_select = signal_post_select;
823 sprintf(st->task.status, "signal task");
826 static void command_pre_select(struct sched *s, struct task *t)
828 struct command_task *ct = container_of(t, struct command_task, task);
829 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
832 static void command_post_select(struct sched *s, struct task *t)
835 struct command_task *ct = container_of(t, struct command_task, task);
837 audiod_status_dump();
838 if (!FD_ISSET(ct->fd, &s->rfds))
840 ret = handle_connect(ct->fd);
842 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
845 static void init_command_task(struct command_task *ct)
847 ct->task.pre_select = command_pre_select;
848 ct->task.post_select = command_post_select;
850 ct->fd = audiod_get_socket(); /* doesn't return on errors */
851 sprintf(ct->task.status, "command task");
854 static void close_stat_pipe(void)
860 client_close(stat_task->ct);
861 stat_task->ct = NULL;
862 FOR_EACH_STATUS_ITEM(i) {
863 free(stat_item_values[i]);
864 stat_item_values[i] = NULL;
867 stat_task->length_seconds = 0;
868 stat_task->offset_seconds = 0;
869 audiod_status_dump();
870 stat_task->playing = 0;
874 * close the connection to para_server and exit
876 * \param status the exit status which is passed to exit(3)
877 * \param msg the log message
879 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
880 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
885 void __noreturn clean_exit(int status, const char *msg)
887 PARA_EMERG_LOG("%s\n", msg);
894 /* avoid busy loop if server is down */
895 static void set_stat_task_restart_barrier(unsigned seconds)
897 struct timeval delay = {seconds, 0};
898 tv_add(now, &delay, &stat_task->restart_barrier);
901 static void try_to_close_slot(int slot_num)
903 struct slot_info *s = &slot[slot_num];
907 if (s->receiver_node && s->receiver_node->task.error != -E_TASK_UNREGISTERED)
909 if (s->fc && s->fc->task.error != -E_TASK_UNREGISTERED)
911 if (s->wng && s->wng->task.error != -E_TASK_UNREGISTERED)
913 PARA_INFO_LOG("closing slot %d\n", slot_num);
915 close_filters(s->fc);
917 close_receiver(slot_num);
918 clear_slot(slot_num);
922 * Check if any receivers/filters/writers need to be started and do so if
925 static void start_stop_decoders(struct sched *s)
930 try_to_close_slot(i);
931 if (audiod_status != AUDIOD_ON || !stat_task->playing)
932 return kill_all_decoders(-E_NOT_PLAYING);
933 open_current_receiver(s);
935 struct slot_info *sl = &slot[i];
936 struct audio_format_info *a;
941 a = &afi[sl->format];
942 if (!sl->receiver_node)
944 if ((!a->num_filters || sl->fc) && sl->wng)
945 continue; /* everything already started */
946 if (!a->num_filters) {
947 if (sl->receiver_node->loaded && !sl->wng) {
952 if (sl->receiver_node->loaded && !sl->fc) {
956 if (sl->wng || !sl->fc || !*sl->fc->out_loaded)
958 if (tv_diff(now, &initial_delay_barrier, &diff) > 0) {
962 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff));
963 if (tv_diff(&s->timeout, &diff, NULL) > 0) {
970 /* restart the client task if necessary */
971 static void status_pre_select(struct sched *s, struct task *t)
973 struct status_task *st = container_of(t, struct status_task, task);
975 if (audiod_status == AUDIOD_OFF) {
978 if (st->ct->task.error >= 0) {
979 st->ct->task.error = -E_AUDIOD_OFF;
982 if (st->ct->task.error != -E_TASK_UNREGISTERED)
985 st->clock_diff_count = conf.clock_diff_count_arg;
990 if (st->ct->task.error < 0) {
991 if (st->ct->task.error != -E_TASK_UNREGISTERED)
996 if (st->ct->status != CL_RECEIVING)
998 bytes_left = for_each_line(st->ct->buf, st->ct->loaded,
999 &check_stat_line, NULL);
1000 if (st->ct->loaded != bytes_left) {
1001 st->last_status_read = *now;
1002 st->ct->loaded = bytes_left;
1004 struct timeval diff;
1005 tv_diff(now, &st->last_status_read, &diff);
1006 if (diff.tv_sec > 61)
1007 st->ct->task.error = -E_STATUS_TIMEOUT;
1011 if (tv_diff(now, &st->restart_barrier, NULL) < 0)
1013 if (st->clock_diff_count) { /* get status only one time */
1014 char *argv[] = {"audiod", "stat", "1", NULL};
1016 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1017 st->clock_diff_count--;
1018 client_open(argc, argv, &st->ct, NULL);
1019 set_stat_task_restart_barrier(2);
1022 char *argv[] = {"audiod", "stat", NULL};
1024 client_open(argc, argv, &st->ct, NULL);
1025 set_stat_task_restart_barrier(5);
1027 free(stat_item_values[SI_BASENAME]);
1028 stat_item_values[SI_BASENAME] = make_message(
1029 "%s: no connection to para_server\n",
1030 status_item_list[SI_BASENAME]);
1031 stat_client_write(stat_item_values[SI_BASENAME],
1033 st->last_status_read = *now;
1035 start_stop_decoders(s);
1038 static void init_status_task(struct status_task *st)
1040 memset(st, 0, sizeof(struct status_task));
1041 st->task.pre_select = status_pre_select;
1042 st->sa_time_diff_sign = 1;
1043 st->clock_diff_count = conf.clock_diff_count_arg;
1044 st->current_audio_format_num = -1;
1045 sprintf(st->task.status, "status task");
1048 static void set_initial_status(void)
1050 audiod_status = AUDIOD_ON;
1051 if (!conf.mode_given)
1053 if (!strcmp(conf.mode_arg, "sb")) {
1054 audiod_status = AUDIOD_STANDBY;
1057 if (!strcmp(conf.mode_arg, "off")) {
1058 audiod_status = AUDIOD_OFF;
1061 if (strcmp(conf.mode_arg, "on"))
1062 PARA_WARNING_LOG("invalid mode\n");
1065 __noreturn static void print_help_and_die(void)
1067 int d = conf.detailed_help_given;
1068 const char **p = d? audiod_args_info_detailed_help
1069 : audiod_args_info_help;
1071 printf_or_die("%s\n\n", AUDIOD_CMDLINE_PARSER_PACKAGE "-"
1072 AUDIOD_CMDLINE_PARSER_VERSION);
1073 printf_or_die("%s\n\n", audiod_args_info_usage);
1075 printf_or_die("%s\n", *p);
1076 print_receiver_helps(d);
1077 print_filter_helps(d);
1078 print_writer_helps(d);
1082 static void init_colors_or_die(void)
1088 daemon_set_default_log_colors();
1089 daemon_set_flag(DF_COLOR_LOG);
1090 for (i = 0; i < conf.log_color_given; i++) {
1091 ret = daemon_set_log_color(conf.log_color_arg[i]);
1098 * the main function of para_audiod
1100 * \param argc usual argument count
1101 * \param argv usual argument vector
1103 * \return EXIT_SUCCESS or EXIT_FAILURE
1105 * \sa para_audiod(1)
1107 int main(int argc, char *argv[])
1110 static struct sched s;
1111 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1112 struct audiod_cmdline_parser_params params = {
1115 .check_required = 0,
1116 .check_ambiguity = 0,
1121 if (audiod_cmdline_parser_ext(argc, argv, &conf, ¶ms))
1123 HANDLE_VERSION_FLAG("audiod", conf);
1124 /* init receivers/filters/writers early to make help work */
1128 if (conf.help_given || conf.detailed_help_given)
1129 print_help_and_die();
1130 drop_privileges_or_die(conf.user_arg, conf.group_arg);
1131 parse_config_or_die();
1132 init_colors_or_die();
1133 daemon_set_flag(DF_LOG_TIME);
1134 daemon_set_flag(DF_LOG_HOSTNAME);
1135 daemon_set_flag(DF_LOG_LL);
1136 if (conf.logfile_given) {
1137 daemon_set_logfile(conf.logfile_arg);
1138 daemon_open_log_or_die();
1140 ret = parse_stream_args();
1142 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1145 log_welcome("para_audiod");
1146 server_uptime(UPTIME_SET);
1147 set_initial_status();
1151 setup_signal_handling();
1152 signal_setup_default(sig_task);
1154 init_status_task(stat_task);
1155 init_command_task(cmd_task);
1157 if (conf.daemon_given)
1160 register_task(&sig_task->task);
1161 register_task(&cmd_task->task);
1162 register_task(&stat_task->task);
1163 s.default_timeout.tv_sec = 0;
1164 s.default_timeout.tv_usec = 99 * 1000;
1167 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1168 return EXIT_FAILURE;