2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file audiod.c the paraslash's audio daemon */
23 #include "audiod.cmdline.h"
28 #include "grab_client.cmdline.h"
29 #include "grab_client.h"
30 #include "client.cmdline.h"
38 #include "write_common.h"
42 /** define the array of error lists needed by para_audiod */
44 /** define the array containing all supported audio formats */
45 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
47 /** defines how to handle one supported audio format */
48 struct audio_format_info {
49 /** pointer to the receiver for this audio format */
50 struct receiver *receiver;
51 /** the receiver configuration */
53 /** the number of filters that should be activated for this audio format */
54 unsigned int num_filters;
55 /** pointer to the array of filters to be activated */
56 struct filter **filters;
57 /** pointer to the array of filter configurations */
59 /** the number of filters that should be activated for this audio format */
60 unsigned int num_writers;
61 /** pointer to the array of writers to be activated */
62 struct writer **writers;
63 /** pointer to the array of writer configurations */
65 /** do not start receiver/filters/writer before this time */
66 struct timeval restart_barrier;
70 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
71 * be associated with a receiver/filter/writer triple. This array holds all
72 * information on the status of these slots.
74 * \sa struct slot_info
76 struct slot_info slot[MAX_STREAM_SLOTS];
80 * the current mode of operation of which can be changed by the on/off/cycle
81 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
83 int audiod_status = AUDIOD_ON;
86 * the gengetopt args_info struct that holds information on all command line
89 struct audiod_args_info conf;
91 static char *socket_name;
93 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
95 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
97 static struct status_task status_task_struct;
100 * the task that calls the status command of para_server
102 * \sa struct status_task
104 struct status_task *stat_task = &status_task_struct;
105 static struct timeval initial_delay_barrier;
108 * the task for handling audiod commands
110 * \sa struct task, struct sched
112 struct command_task {
113 /** the local listening socket */
115 /** the associated task structure */
120 * task for signal handling
123 /** the signal pipe */
125 /** the number of the most recent signal */
127 /** the associated task structure */
131 /** iterate over all supported audio formats */
132 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
135 * get the audio format number
136 * \param name the name of the audio format
138 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
139 * \a name is not a supported audio format.
141 int get_audio_format_num(char *name)
144 FOR_EACH_AUDIO_FORMAT(i)
145 if (!strcmp(name, audio_formats[i]))
147 return -E_UNSUPPORTED_AUDIO_FORMAT;
151 * the log function of para_audiod
154 * \param fmt the format string
156 void para_log(int ll, const char* fmt,...)
162 char str[MAXLINE] = "";
163 static char *hostname;
165 if (ll < conf.loglevel_arg)
168 hostname = para_hostname();
169 outfd = logfile? logfile : stderr;
172 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
173 fprintf(outfd, "%s %s ", str, hostname);
174 if (conf.loglevel_arg <= INFO)
175 fprintf(outfd, "%i ", ll);
177 vfprintf(outfd, fmt, argp);
181 static char *configfile_exists(void)
183 static char *config_file;
186 char *home = para_homedir();
187 config_file = make_message("%s/.paraslash/audiod.conf", home);
190 return file_exists(config_file)? config_file : NULL;
193 static void setup_signal_handling(void)
195 sig_task->fd = para_signal_init();
196 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
197 para_install_sighandler(SIGINT);
198 para_install_sighandler(SIGTERM);
199 para_install_sighandler(SIGHUP);
200 signal(SIGPIPE, SIG_IGN);
203 static void clear_slot(int slot_num)
205 struct slot_info *s = &slot[slot_num];
207 PARA_INFO_LOG("clearing slot %d\n", slot_num);
208 memset(s, 0, sizeof(struct slot_info));
212 static void close_receiver(int slot_num)
214 struct slot_info *s = &slot[slot_num];
215 struct audio_format_info *a;
217 if (s->format < 0 || !s->receiver_node)
220 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
221 audio_formats[s->format] , slot_num, s->receiver_node->eof);
222 a->receiver->close(s->receiver_node);
223 free(s->receiver_node);
224 s->receiver_node = NULL;
227 static void kill_all_decoders(void)
232 struct slot_info *s = &slot[i];
233 if (s->wng && !s->wng->eof) {
234 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
236 wng_unregister(s->wng);
239 if (s->fc && !s->fc->eof) {
240 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i);
241 unregister_task(&s->fc->task);
244 if (s->receiver_node && !s->receiver_node->eof) {
245 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i);
246 unregister_task(&s->receiver_node->task);
247 s->receiver_node->eof = 1;
252 static int get_empty_slot(void)
263 if (s->wng || s->receiver_node || s->fc)
268 return -E_NO_MORE_SLOTS;
272 * get the number of filters
274 * \param audio_format_num the number identifying the audio format
276 * \return the number of filters for the given audio format
280 int num_filters(int audio_format_num)
282 return afi[audio_format_num].num_filters;
285 static void filter_event_handler(struct task *t)
287 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
288 struct filter_chain *fc = t->private_data;
293 static void open_filters(int slot_num)
295 struct slot_info *s = &slot[slot_num];
296 struct audio_format_info *a = &afi[s->format];
297 int nf = a->num_filters;
303 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
304 s->fc = para_calloc(sizeof(struct filter_chain));
305 INIT_LIST_HEAD(&s->fc->filters);
306 s->fc->inbuf = s->receiver_node->buf;
307 s->fc->in_loaded = &s->receiver_node->loaded;
308 s->fc->input_eof = &s->receiver_node->eof;
309 s->fc->task.pre_select = filter_pre_select;
310 s->fc->task.event_handler = filter_event_handler;
311 s->fc->task.private_data = s->fc;
314 s->receiver_node->output_eof = &s->fc->eof;
315 sprintf(s->fc->task.status, "filter chain");
316 for (i = 0; i < nf; i++) {
317 struct filter_node *fn = para_calloc(sizeof(struct filter_node));
318 fn->conf = a->filter_conf[i];
320 fn->filter = a->filters[i];
321 INIT_LIST_HEAD(&fn->callbacks);
322 list_add_tail(&fn->node, &s->fc->filters);
323 fn->filter->open(fn);
324 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
325 audio_formats[s->format], i + 1, nf,
326 fn->filter->name, slot_num);
327 s->fc->outbuf = fn->buf;
328 s->fc->out_loaded = &fn->loaded;
330 register_task(&s->fc->task);
333 static void wng_event_handler(struct task *t)
335 struct writer_node_group *wng = t->private_data;
337 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
342 static void open_writers(int slot_num)
345 struct slot_info *s = &slot[slot_num];
346 struct audio_format_info *a = &afi[s->format];
348 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
350 s->wng = setup_default_wng();
352 s->wng = wng_new(a->num_writers);
354 s->wng->buf = s->fc->outbuf;
355 s->wng->loaded = s->fc->out_loaded;
356 s->wng->input_eof = &s->fc->eof;
357 s->wng->channels = &s->fc->channels;
358 s->wng->samplerate = &s->fc->samplerate;
359 s->fc->output_eof = &s->wng->eof;
360 PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
362 s->wng->buf = s->receiver_node->buf;
363 s->wng->loaded = &s->receiver_node->loaded;
364 s->wng->input_eof = &s->receiver_node->eof;
366 s->wng->task.event_handler = wng_event_handler;
367 for (i = 0; i < a->num_writers; i++) {
368 s->wng->writer_nodes[i].conf = a->writer_conf[i];
369 s->wng->writer_nodes[i].writer = a->writers[i];
371 ret = wng_open(s->wng);
373 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
377 activate_inactive_grab_clients(slot_num, s->format, &s->fc->filters);
380 static void rn_event_handler(struct task *t)
382 struct receiver_node *rn = t->private_data;
383 const struct timeval restart_delay = {0, 10 * 1000};
386 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
389 /* set restart barrier */
391 if (slot[i].receiver_node != rn)
393 tv_add(now, &restart_delay, &afi[slot[i].format].restart_barrier);
397 static int open_receiver(int format)
399 struct audio_format_info *a = &afi[format];
402 struct receiver_node *rn;
403 const struct timeval restart_delay = {1, 0};
405 ret = get_empty_slot();
411 s->receiver_node = para_calloc(sizeof(struct receiver_node));
412 rn = s->receiver_node;
413 rn->receiver = a->receiver;
414 rn->conf = a->receiver_conf;
415 ret = a->receiver->open(s->receiver_node);
417 free(s->receiver_node);
418 s->receiver_node = NULL;
421 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
422 audio_formats[s->format], a->receiver->name, slot_num);
423 rn->task.private_data = s->receiver_node;
424 rn->task.pre_select = a->receiver->pre_select;
425 rn->task.post_select = a->receiver->post_select;
426 rn->task.event_handler = rn_event_handler;
427 sprintf(rn->task.status, "%s receiver node", rn->receiver->name);
428 register_task(&rn->task);
431 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
432 tv_add(now, &restart_delay, &afi[format].restart_barrier);
436 static int receiver_running(int format)
441 struct slot_info *s = &slot[i];
442 if (s->format == format && s->receiver_node
443 && !s->receiver_node->eof)
449 static int open_current_receiver(struct sched *s)
453 char *audio_format = stat_task->stat_item_values[SI_FORMAT];
455 if (!audio_format || !stat_task->pcd)
457 i = get_audio_format_num(audio_format + strlen(
458 status_item_list[SI_FORMAT]) + 1);
461 if (receiver_running(i))
463 if (tv_diff(now, &afi[i].restart_barrier, &diff) < 0) {
467 return open_receiver(i) < 0? 0 : 1;
470 static unsigned compute_time_diff(const struct timeval *status_time)
472 struct timeval tmp, diff;
473 static unsigned count;
474 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
475 const struct timeval max_deviation = {0, 500 * 1000};
476 const int time_smooth = 5;
480 sign = tv_diff(status_time, now, &diff);
481 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
482 // sign, sa_time_diff_sign);
484 sa_time_diff_sign = sign;
485 stat_task->sa_time_diff = diff;
490 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
491 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
492 PARA_WARNING_LOG("time diff jump: %lims\n",
496 sa_time_diff_sign = tv_convex_combination(
497 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
498 count > 10? sign : sign * time_smooth, &diff,
500 stat_task->sa_time_diff = tmp;
501 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
504 sa_time_diff_sign ? "+" : "-",
505 tv2ms(&stat_task->sa_time_diff)
508 stat_task->sa_time_diff_sign = sa_time_diff_sign;
512 static void check_stat_line(char *line)
516 long unsigned sec, usec;
519 // PARA_INFO_LOG("line: %s\n", line);
522 itemnum = stat_line_valid(line);
524 PARA_WARNING_LOG("invalid status line: %s\n", line);
527 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
529 tmp = make_message("%s\n", line);
530 stat_client_write(tmp, itemnum);
532 free(stat_task->stat_item_values[itemnum]);
533 stat_task->stat_item_values[itemnum] = para_strdup(line);
534 ilen = strlen(status_item_list[itemnum]);
537 stat_task->playing = strstr(line, "playing")? 1 : 0;
540 stat_task->offset_seconds = atoi(line + ilen + 1);
543 stat_task->length_seconds = atoi(line + ilen + 1);
545 case SI_STREAM_START:
546 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
547 struct timeval a_start, delay;
548 delay.tv_sec = conf.stream_delay_arg / 1000;
549 delay.tv_usec = (conf.stream_delay_arg % 1000) * 1000;
550 stat_task->server_stream_start.tv_sec = sec;
551 stat_task->server_stream_start.tv_usec = usec;
552 if (compute_time_diff(NULL) > 2) {
553 if (stat_task->sa_time_diff_sign < 0)
554 tv_add(&stat_task->server_stream_start,
555 &stat_task->sa_time_diff, &a_start);
557 tv_diff(&stat_task->server_stream_start,
558 &stat_task->sa_time_diff, &a_start);
559 tv_add(&a_start, &delay, &initial_delay_barrier);
563 case SI_CURRENT_TIME:
564 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
565 struct timeval tv = {sec, usec};
566 compute_time_diff(&tv);
568 if (stat_task->clock_diff_count)
569 stat_task->clock_diff_count--;
574 static void try_to_close_slot(int slot_num)
576 struct slot_info *s = &slot[slot_num];
580 if (s->receiver_node && !s->receiver_node->eof)
582 if (s->fc && !s->fc->eof)
584 if (s->wng && !s->wng->eof)
586 PARA_INFO_LOG("closing slot %d \n", slot_num);
588 close_filters(s->fc);
590 close_receiver(slot_num);
591 clear_slot(slot_num);
595 * Check if any receivers/filters/writers need to be started and do so if
596 * neccessary. Since the pre_select function didn't have a chance yet to put
597 * file descriptors into the fd sets given by s, make the upcoming select()
598 * return immediately to avoid a long timeout in case we started something.
600 static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
603 struct timeval min_delay = {0, 1};
606 if (audiod_status != AUDIOD_ON || !stat_task->playing)
607 return kill_all_decoders();
608 if (open_current_receiver(s))
609 s->timeout = min_delay;
611 struct slot_info *sl = &slot[i];
612 struct audio_format_info *a;
617 a = &afi[sl->format];
618 if (!sl->receiver_node)
620 if (!a->num_filters) {
621 if (sl->receiver_node->loaded && !sl->wng) {
623 s->timeout = min_delay;
627 if (sl->receiver_node->loaded && !sl->fc) {
629 s->timeout = min_delay;
632 if (!sl->fc || !*sl->fc->out_loaded || sl->wng)
634 if (tv_diff(now, &initial_delay_barrier, &diff) > 0) {
636 s->timeout = min_delay;
639 PARA_INFO_LOG("inital delay: %lu ms left\n", tv2ms(&diff));
640 if (tv_diff(&s->timeout, &diff, NULL) > 0) {
646 static void audiod_post_select(__a_unused struct sched *s,
647 __a_unused struct task *t)
653 try_to_close_slot(i);
656 static void init_audiod_task(struct task *t)
658 t->pre_select = audiod_pre_select;
659 t->post_select = audiod_post_select;
660 t->event_handler = NULL;
662 sprintf(t->status, "audiod task");
665 static int parse_stream_command(const char *txt, char **cmd)
667 char *p = strchr(txt, ':');
671 return -E_MISSING_COLON;
673 FOR_EACH_AUDIO_FORMAT(i) {
674 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
679 return -E_UNSUPPORTED_AUDIO_FORMAT;
682 static int add_filter(int format, char *cmdline)
684 struct audio_format_info *a = &afi[format];
685 int filter_num, nf = a->num_filters;
687 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
690 a->filters[nf] = &filters[filter_num];
692 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf + 1,
693 a->filters[nf]->name);
697 static int init_writers(void)
701 struct audio_format_info *a;
703 init_supported_writers();
704 nw = PARA_MAX(1, conf.writer_given);
705 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
706 FOR_EACH_AUDIO_FORMAT(i) {
708 a->writer_conf = para_malloc(nw * sizeof(void *));
709 a->writers = para_malloc(nw * sizeof(struct writer *));
712 for (i = 0; i < conf.writer_given; i++) {
715 ret = parse_stream_command(conf.writer_arg[i], &cmd);
720 wconf = check_writer_arg(cmd, &writer_num);
725 a->writers[nw] = &writers[writer_num];
726 a->writer_conf[nw] = wconf;
727 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
728 nw, writer_names[writer_num]);
736 static int init_receivers(void)
738 int i, ret, receiver_num;
740 struct audio_format_info *a;
742 for (i = 0; receivers[i].name; i++) {
743 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
744 receivers[i].init(&receivers[i]);
746 for (i = conf.receiver_given - 1; i >= 0; i--) {
747 char *arg = conf.receiver_arg[i];
748 char *recv_arg = strchr(arg, ':');
749 ret = -E_MISSING_COLON;
754 ret = get_audio_format_num(arg);
757 afi[ret].receiver_conf = check_receiver_arg(recv_arg, &receiver_num);
758 if (!afi[ret].receiver_conf) {
759 ret = -E_RECV_SYNTAX;
762 afi[ret].receiver = &receivers[receiver_num];
764 /* use the first available receiver with no arguments
765 * for those audio formats for which no receiver
768 cmd = para_strdup(receivers[0].name);
769 FOR_EACH_AUDIO_FORMAT(i) {
771 if (a->receiver_conf)
773 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
774 if (!a->receiver_conf)
775 return -E_RECV_SYNTAX;
776 a->receiver = &receivers[receiver_num];
784 static int init_default_filters(void)
788 FOR_EACH_AUDIO_FORMAT(i) {
789 struct audio_format_info *a = &afi[i];
794 continue; /* no default -- nothing to to */
795 /* add "dec" to audio format name */
796 tmp = make_message("%sdec", audio_formats[i]);
797 for (j = 0; filters[j].name; j++)
798 if (!strcmp(tmp, filters[j].name))
801 ret = -E_UNSUPPORTED_FILTER;
802 if (!filters[j].name)
804 tmp = para_strdup(filters[j].name);
805 ret = add_filter(i, tmp);
809 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
816 static int init_filters(void)
820 filter_init(filters);
821 nf = PARA_MAX(1, conf.filter_given);
822 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
823 FOR_EACH_AUDIO_FORMAT(i) {
824 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
825 afi[i].filters = para_malloc(nf * sizeof(struct filter *));
827 if (!conf.no_default_filters_given)
828 return init_default_filters();
829 for (i = 0; i < conf.filter_given; i++) {
830 char *arg = conf.filter_arg[i];
831 char *filter_name = strchr(arg, ':');
832 ret = -E_MISSING_COLON;
837 ret = get_audio_format_num(arg);
840 ret = add_filter(ret, filter_name);
844 ret = init_default_filters(); /* use default values for the rest */
849 static int init_stream_io(void)
853 ret = init_writers();
856 ret = init_receivers();
859 ret = init_filters();
865 static int audiod_get_socket(void)
867 struct sockaddr_un unix_addr;
870 if (conf.socket_given)
871 socket_name = para_strdup(conf.socket_arg);
873 char *hn = para_hostname();
874 socket_name = make_message("/var/paraslash/audiod_socket.%s",
878 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
879 if (conf.force_given)
881 fd = create_pf_socket(socket_name, &unix_addr,
882 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
884 PARA_EMERG_LOG("%s", "can not connect to socket\n");
885 exit(EXIT_FAILURE); /* do not unlink socket */
887 if (listen(fd , 5) < 0) {
888 PARA_EMERG_LOG("%s", "can not listen on socket\n");
889 exit(EXIT_FAILURE); /* do not unlink socket */
891 mark_fd_nonblock(fd);
895 static void signal_event_handler(struct task *t)
897 struct signal_task *st = t->private_data;
899 switch (st->signum) {
903 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
904 clean_exit(EXIT_FAILURE, "caught deadly signal");
908 static void signal_pre_select(struct sched *s, struct task *t)
910 struct signal_task *st = t->private_data;
912 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
915 static void signal_post_select(struct sched *s, struct task *t)
917 struct signal_task *st = t->private_data;
919 if (!FD_ISSET(st->fd, &s->rfds))
921 t->ret = -E_SIGNAL_CAUGHT;
922 st->signum = para_next_signal();
925 static void signal_setup_default(struct signal_task *st)
927 st->task.pre_select = signal_pre_select;
928 st->task.post_select = signal_post_select;
929 st->task.private_data = st;
930 sprintf(st->task.status, "signal task");
933 static void command_pre_select(struct sched *s, struct task *t)
935 struct command_task *ct = t->private_data;
937 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
941 static void command_post_select(struct sched *s, struct task *t)
944 struct command_task *ct = t->private_data;
946 t->ret = 1; /* always successful */
947 audiod_status_dump();
948 if (!FD_ISSET(ct->fd, &s->rfds))
950 ret = handle_connect(ct->fd);
952 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
955 static void init_command_task(struct command_task *ct)
957 ct->task.pre_select = command_pre_select;
958 ct->task.post_select = command_post_select;
959 ct->task.event_handler = NULL;
960 ct->task.private_data = ct;
961 ct->fd = audiod_get_socket(); /* doesn't return on errors */
962 sprintf(ct->task.status, "command task");
965 static void close_stat_pipe(void)
971 client_close(stat_task->pcd);
972 stat_task->pcd = NULL;
973 for (i = 0; i < NUM_STAT_ITEMS; i++) {
974 free(stat_task->stat_item_values[i]);
975 stat_task->stat_item_values[i] = NULL;
978 stat_task->length_seconds = 0;
979 stat_task->offset_seconds = 0;
980 audiod_status_dump();
981 stat_task->playing = 0;
982 stat_task->stat_item_values[SI_STATUS_BAR] = make_message(
983 "%s:no connection to para_server\n",
984 status_item_list[SI_STATUS_BAR]);
985 stat_client_write(stat_task->stat_item_values[SI_STATUS_BAR],
987 if (stat_task->clock_diff_count) {
988 stat_task->clock_diff_barrier.tv_sec = now->tv_sec + 1;
989 stat_task->clock_diff_barrier.tv_usec = now->tv_usec;
994 * close the connection to para_server and exit
996 * \param status the exit status which is passed to exit(3)
997 * \param msg the log message
999 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
1000 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
1005 void __noreturn clean_exit(int status, const char *msg)
1007 PARA_EMERG_LOG("%s\n", msg);
1009 unlink(socket_name);
1015 static void client_task_event_handler(__a_unused struct task *t)
1018 struct timeval delay = {1, 0};
1020 if (t->ret == -E_HANDSHAKE_COMPLETE)
1024 if (t->ret != -E_SERVER_EOF)
1025 stat_task->clock_diff_count = conf.clock_diff_count_arg;
1026 /* avoid busy loop if server is down */
1027 tv_add(now, &delay, &stat_task->restart_barrier);
1028 FOR_EACH_AUDIO_FORMAT(i)
1029 afi[i].restart_barrier = stat_task->restart_barrier;
1032 static void status_pre_select(struct sched *s, struct task *t)
1034 struct status_task *st = t->private_data;
1037 t->ret = 1; /* always successful */
1038 if (st->pcd || audiod_status == AUDIOD_OFF)
1040 if (!st->clock_diff_count && tv_diff(now, &st->restart_barrier, NULL)
1043 if (st->clock_diff_count) {
1044 char *argv[] = {"audiod", "stat", "1", NULL};
1046 if (tv_diff(now, &st->clock_diff_barrier, NULL) < 0)
1048 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1049 ret = client_open(argc, argv, &st->pcd);
1052 char *argv[] = {"audiod", "stat", NULL};
1054 ret = client_open(argc, argv, &st->pcd);
1058 st->pcd->task.event_handler = client_task_event_handler;
1059 s->timeout.tv_sec = 0;
1060 s->timeout.tv_usec = 1;
1063 static void status_post_select(__a_unused struct sched *s, struct task *t)
1065 struct status_task *st = t->private_data;
1066 unsigned bytes_left;
1069 if (!st->pcd || st->pcd->status != CL_RECEIVING)
1071 if (st->pcd && audiod_status == AUDIOD_OFF) {
1072 unregister_task(&st->pcd->task);
1074 st->clock_diff_count = conf.clock_diff_count_arg;
1077 bytes_left = for_each_line(st->pcd->buf, st->pcd->loaded,
1079 if (st->pcd->loaded != bytes_left) {
1080 st->last_status_read = *now;
1081 st->pcd->loaded = bytes_left;
1083 struct timeval diff;
1084 tv_diff(now, &st->last_status_read, &diff);
1085 if (diff.tv_sec > 61)
1090 static void init_status_task(struct status_task *st)
1092 memset(st, 0, sizeof(struct status_task));
1093 st->task.pre_select = status_pre_select;
1094 st->task.post_select = status_post_select;
1095 st->task.private_data = st;
1096 st->sa_time_diff_sign = 1;
1097 st->clock_diff_count = conf.clock_diff_count_arg;
1098 sprintf(st->task.status, "status task");
1101 static void set_initial_status(void)
1103 audiod_status = AUDIOD_ON;
1104 if (!conf.mode_given)
1106 if (!strcmp(conf.mode_arg, "sb")) {
1107 audiod_status = AUDIOD_STANDBY;
1110 if (!strcmp(conf.mode_arg, "off")) {
1111 audiod_status = AUDIOD_OFF;
1114 if (strcmp(conf.mode_arg, "on"))
1115 PARA_WARNING_LOG("%s", "invalid mode\n");
1119 * the main function of para_audiod
1121 * \param argc usual argument count
1122 * \param argv usual argument vector
1124 * \return EXIT_SUCCESS or EXIT_FAILURE
1126 * \sa para_audiod(1)
1128 int main(int argc, char *argv[])
1133 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1134 struct task audiod_task_struct, *audiod_task = &audiod_task_struct;
1137 audiod_cmdline_parser(argc, argv, &conf);
1138 HANDLE_VERSION_FLAG("audiod", conf);
1139 para_drop_privileges(conf.user_arg, conf.group_arg);
1140 cf = configfile_exists();
1142 if (audiod_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
1143 PARA_EMERG_LOG("%s", "parse error in config file\n");
1147 if (conf.logfile_given)
1148 logfile = open_log(conf.logfile_arg);
1149 log_welcome("para_audiod", conf.loglevel_arg);
1150 i = init_stream_io();
1152 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
1155 server_uptime(UPTIME_SET);
1156 set_initial_status();
1160 setup_signal_handling();
1161 signal_setup_default(sig_task);
1162 sig_task->task.event_handler = signal_event_handler;
1164 init_status_task(stat_task);
1165 init_command_task(cmd_task);
1166 init_audiod_task(audiod_task);
1168 if (conf.daemon_given)
1171 register_task(&sig_task->task);
1172 register_task(&cmd_task->task);
1173 register_task(&stat_task->task);
1174 register_task(audiod_task);
1175 s.default_timeout.tv_sec = 0;
1176 s.default_timeout.tv_usec = 99 * 1000;
1179 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
1180 return EXIT_FAILURE;