2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
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"
25 #include "close_on_fork.h"
29 #include "grab_client.cmdline.h"
30 #include "grab_client.h"
39 #include "write_common.h"
41 /** define the array of error lists needed by para_audiod */
43 /** define the array containing all supported audio formats */
44 DEFINE_AUDIO_FORMAT_ARRAY;
47 * the possible modes of operation
49 * - off: disconnect from para_server
50 * - on: receive status information from para_server and play the audio stream
51 * - sb: only receive status information but not the audio stream
53 enum {AUDIOD_OFF, AUDIOD_ON, AUDIOD_STANDBY};
55 /** defines how to handle one supported audio format */
56 struct audio_format_info {
57 /** pointer to the receiver for this audio format */
58 struct receiver *receiver;
59 /** the receiver configuration */
61 /** the number of filters that should be activated for this audio format */
62 unsigned int num_filters;
63 /** pointer to the array of filters to be activated */
64 struct filter **filters;
65 /** pointer to the array of filter configurations */
67 /** the number of filters that should be activated for this audio format */
68 unsigned int num_writers;
69 /** pointer to the array of writers to be activated */
70 struct writer **writers;
71 /** pointer to the array of writer configurations */
73 /** do not start receiver/filters/writer before this time */
74 struct timeval restart_barrier;
78 * describes one instance of a receiver-filter-writer chain
80 * \sa receier_node, receiver, filter, filter_node, filter_chain, writer,
81 * writer_node, writer_node_group.
84 /** number of the audio format in this slot */
86 /** time of the last successful read from the receiver */
88 /** time the last write to the write fd happend */
90 /** writer start time */
91 struct timeval wstime;
92 /** the receiver info associated with this slot */
93 struct receiver_node *receiver_node;
94 /** the active filter chain */
95 struct filter_chain *fc;
96 /** the active writer node group */
97 struct writer_node_group *wng;
99 static struct slot_info slot[MAX_STREAM_SLOTS];
101 extern const char *status_item_list[NUM_STAT_ITEMS];
103 static struct gengetopt_args_info conf;
104 static struct timeval server_stream_start, sa_time_diff;
105 static int playing, current_decoder = -1,
106 audiod_status = AUDIOD_ON, offset_seconds, length_seconds,
107 sa_time_diff_sign = 1;
108 static char *af_status, /* the audio format announced in server status */
109 *socket_name, *hostname;
110 static char *stat_item_values[NUM_STAT_ITEMS];
111 static FILE *logfile;
112 static const struct timeval restart_delay = {0, 300 * 1000};
113 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
114 static struct timeval *now;
116 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
118 struct command_task {
122 static struct command_task command_task_struct, *cmd_task = &command_task_struct;
127 char buf[STRINGSIZE];
130 static struct status_task status_task_struct, *stat_task = &status_task_struct;
135 static struct audiod_task audiod_task_struct, *at = &audiod_task_struct;
144 /** defines one command of para_audiod */
145 struct audiod_command {
146 /** the name of the command */
148 /** pointer to the function that handles the command */
149 int (*handler)(int, int, char**);
150 int (*line_handler)(int, char*);
151 /** one-line description of the command */
152 const char *description;
153 /** summary of the command line options */
154 const char *synopsis;
155 /** the long help text */
158 static int com_grab(int, char *);
159 static int com_cycle(int, int, char **);
160 static int com_help(int, int, char **);
161 static int com_off(int, int, char **);
162 static int com_on(int, int, char **);
163 static int com_sb(int, int, char **);
164 static int com_stat(int, int, char **);
165 static int com_term(int, int, char **);
166 static struct audiod_command cmds[] = {
169 .handler = com_cycle,
170 .description = "switch to next mode",
174 "on -> standby -> off -> on\n"
179 .line_handler = com_grab,
180 .description = "grab the audio stream",
181 .synopsis = "-- grab [grab_options]",
184 "grab ('splice') the audio stream at any position in the filter \n"
185 "chain and send that data back to the client. Try\n"
186 "\t para_audioc -- grab -h\n"
187 "for the list of available options.\n"
193 .description = "display command list or help for given command",
194 .synopsis = "help [command]",
197 "When I was younger, so much younger than today, I never needed\n"
198 "anybody's help in any way. But now these days are gone, I'm not so\n"
199 "self assured. Now I find I've changed my mind and opened up the doors.\n"
201 " -- Beatles: Help\n"
207 .description = "deactivate para_audiod",
211 "Close connection to para_server and stop all decoders.\n"
217 .description = "activate para_audiod",
221 "Establish connection to para_server, retrieve para_server's current\n"
222 "status. If playing, start corresponding decoder. Otherwise stop\n"
229 .description = "enter standby mode",
233 "Stop all decoders but leave connection to para_server open.\n"
239 .description = "print status information",
240 .synopsis = "stat [item1 ...]",
243 "Dump given status items (all if none given) to stdout.\n"
249 .description = "terminate audiod",
253 "Stop all decoders, shut down connection to para_server and exit.\n"
261 /** iterate over all slots */
262 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
263 /** iterate over all supported audio formats */
264 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
265 /** iterate over the array of all audiod commands */
266 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
269 * get the audio format number
270 * \param name the name of the audio format
272 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
273 * \a name is not a supported audio format.
275 int get_audio_format_num(char *name)
278 FOR_EACH_AUDIO_FORMAT(i)
279 if (!strcmp(name, audio_formats[i]))
281 return -E_UNSUPPORTED_AUDIO_FORMAT;
285 * log function. first argument is loglevel.
287 void para_log(int ll, const char* fmt,...)
293 char str[MAXLINE] = "";
295 if (ll < conf.loglevel_arg)
297 outfd = logfile? logfile : stderr;
300 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
301 fprintf(outfd, "%s %s ", str, hostname);
302 if (conf.loglevel_arg <= INFO)
303 fprintf(outfd, "%i ", ll);
305 vfprintf(outfd, fmt, argp);
309 static int client_write(int fd, const char *buf)
311 size_t len = strlen(buf);
312 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
315 static char *get_time_string(struct timeval *newest_stime)
317 struct timeval diff, adj_stream_start, tmp;
318 int total = 0, use_server_time = 1;
323 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
325 if (audiod_status == AUDIOD_OFF)
327 if (sa_time_diff_sign > 0)
328 tv_diff(&server_stream_start, &sa_time_diff,
331 tv_add(&server_stream_start, &sa_time_diff,
333 tmp = adj_stream_start;
334 if (newest_stime && audiod_status == AUDIOD_ON) {
335 tv_diff(newest_stime, &adj_stream_start, &diff);
336 if (tv2ms(&diff) < 5000) {
341 tv_diff(now, &tmp, &diff);
342 total = diff.tv_sec + offset_seconds;
343 if (total > length_seconds)
344 total = length_seconds;
349 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
350 status_item_list[SI_PLAY_TIME],
351 use_server_time? "~" : "",
354 (length_seconds - total) / 60,
355 (length_seconds - total) % 60,
356 length_seconds? (total * 100 + length_seconds / 2) /
363 __malloc static char *audiod_status_string(void)
365 const char *status = (audiod_status == AUDIOD_ON)?
366 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
367 return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
370 static struct timeval *wstime(void)
373 struct timeval *max = NULL;
375 struct slot_info *s = &slot[i];
378 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
384 __malloc static char *decoder_flags(void)
387 char decoder_flags[MAX_STREAM_SLOTS + 1];
390 struct slot_info *s = &slot[i];
392 if (s->receiver_node)
396 decoder_flags[i] = flag;
398 decoder_flags[MAX_STREAM_SLOTS] = '\0';
399 return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
403 static char *configfile_exists(void)
405 static char *config_file;
408 char *home = para_homedir();
409 config_file = make_message("%s/.paraslash/audiod.conf", home);
412 return file_exists(config_file)? config_file : NULL;
415 static void setup_signal_handling(void)
417 sig_task->fd = para_signal_init();
418 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
419 para_install_sighandler(SIGINT);
420 para_install_sighandler(SIGTERM);
421 para_install_sighandler(SIGCHLD);
422 para_install_sighandler(SIGHUP);
423 signal(SIGPIPE, SIG_IGN);
426 static void audiod_status_dump(void)
428 static char *p_ts, *p_us, *p_as, *p_df;
429 struct timeval *t = wstime();
430 char *us, *tmp = get_time_string(t);
432 if (tmp && (!p_ts || strcmp(tmp, p_ts)))
433 stat_client_write(tmp, SI_PLAY_TIME);
438 tmp = make_message("%s:%s\n", status_item_list[SI_AUDIOD_UPTIME], us);
440 if (!p_us || strcmp(p_us, tmp))
441 stat_client_write(tmp, SI_AUDIOD_UPTIME);
445 tmp = audiod_status_string();
446 if (!p_as || strcmp(p_as, tmp))
447 stat_client_write(tmp, SI_AUDIOD_STATUS);
451 tmp = decoder_flags();
452 if (!p_df || strcmp(p_df, tmp))
453 stat_client_write(tmp, SI_DECODER_FLAGS);
458 static void clear_slot(int slot_num)
460 struct slot_info *s = &slot[slot_num];
462 PARA_INFO_LOG("clearing slot %d\n", slot_num);
463 memset(s, 0, sizeof(struct slot_info));
467 static void close_receiver(int slot_num)
469 struct slot_info *s = &slot[slot_num];
470 struct audio_format_info *a;
472 if (s->format < 0 || !s->receiver_node)
475 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
476 audio_formats[s->format] , slot_num, s->receiver_node->eof);
477 if (!s->receiver_node->eof)
478 unregister_task(&s->receiver_node->task);
479 a->receiver->close(s->receiver_node);
480 free(s->receiver_node);
481 s->receiver_node = NULL;
482 /* set restart barrier */
483 tv_add(now, &restart_delay, &afi[s->format].restart_barrier);
486 static void kill_all_decoders(void)
491 struct slot_info *s = &slot[i];
492 if (s->receiver_node)
493 s->receiver_node->eof = 1;
497 static int get_empty_slot(void)
508 if (s->wng || s->receiver_node || s->fc)
513 return -E_NO_MORE_SLOTS;
516 static int decoder_running(int format)
523 if (s->format == format && s->receiver_node)
525 if (s->format == format && s->wng)
531 static void close_stat_pipe(void)
535 if (stat_task->fd < 0)
537 PARA_NOTICE_LOG("%s", "closing status pipe\n");
538 close(stat_task->fd);
539 del_close_on_fork_list(stat_task->fd);
542 for (i = 0; i < NUM_STAT_ITEMS; i++) {
543 free(stat_item_values[i]);
544 stat_item_values[i] = NULL;
549 audiod_status_dump();
551 stat_item_values[SI_STATUS_BAR] = make_message(
552 "%s:no connection to para_server\n",
553 status_item_list[SI_STATUS_BAR]);
554 stat_client_write(stat_item_values[SI_STATUS_BAR], SI_STATUS_BAR);
557 static void __noreturn clean_exit(int status, const char *msg)
559 PARA_EMERG_LOG("%s\n", msg);
563 if (stat_task->fd >= 0)
569 * get the number of filters
571 * \param audio_format_num the number identifying the audio format
573 * \return the number of filters for the given audio format
577 int num_filters(int audio_format_num)
579 return afi[audio_format_num].num_filters;
582 static void filter_event_handler(struct task *t)
584 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
588 static void open_filters(int slot_num)
590 struct slot_info *s = &slot[slot_num];
591 struct audio_format_info *a = &afi[s->format];
592 int nf = a->num_filters;
598 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
599 s->fc = para_calloc(sizeof(struct filter_chain));
600 INIT_LIST_HEAD(&s->fc->filters);
601 s->fc->inbuf = s->receiver_node->buf;
602 s->fc->in_loaded = &s->receiver_node->loaded;
603 s->fc->input_eof = &s->receiver_node->eof;
605 s->fc->task.pre_select = filter_pre_select;
606 s->fc->task.event_handler = filter_event_handler;
607 s->fc->task.private_data = s->fc;
608 s->fc->task.flags = 0;
610 sprintf(s->fc->task.status, "filter chain");
611 for (i = 0; i < nf; i++) {
612 struct filter_node *fn = para_calloc(sizeof(struct filter_node));
613 fn->conf = a->filter_conf[i];
615 fn->filter = a->filters[i];
616 INIT_LIST_HEAD(&fn->callbacks);
617 list_add_tail(&fn->node, &s->fc->filters);
618 fn->filter->open(fn);
619 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
620 audio_formats[s->format], i + 1, nf,
621 fn->filter->name, slot_num);
622 s->fc->outbuf = fn->buf;
623 s->fc->out_loaded = &fn->loaded;
625 register_task(&s->fc->task);
626 // PARA_DEBUG_LOG("output loaded for filter chain %p: %p\n", s->fc,
627 // s->fc->out_loaded);
630 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
632 struct filter_node *fn;
636 struct slot_info *s = &slot[i];
637 if (s->format < 0 || !s->fc)
639 if (slot_num >= 0 && slot_num != i)
641 if (format >= 0 && s->format != format)
643 if (num_filters(i) < filternum)
647 list_for_each_entry(fn, &s->fc->filters, node)
648 if (filternum <= 0 || j++ == filternum)
655 static void wng_event_handler(struct task *t)
657 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
661 static void open_writers(int slot_num)
664 struct slot_info *s = &slot[slot_num];
665 struct audio_format_info *a = &afi[s->format];
667 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
669 s->wng = setup_default_wng();
671 s->wng = wng_new(a->num_writers);
673 s->wng->buf = s->fc->outbuf;
674 s->wng->loaded = s->fc->out_loaded;
675 s->wng->input_eof = &s->fc->eof;
676 s->fc->output_eof = &s->wng->eof;
678 s->wng->buf = s->receiver_node->buf;
679 s->wng->loaded = &s->receiver_node->loaded;
680 s->wng->input_eof = &s->receiver_node->eof;
682 s->wng->task.event_handler = wng_event_handler;
683 for (i = 0; i < a->num_writers; i++) {
684 s->wng->writer_nodes[i].conf = a->writer_conf[i];
685 s->wng->writer_nodes[i].writer = a->writers[i];
686 sprintf(s->wng->writer_nodes[i].task.status, "writer_node");
688 ret = wng_open(s->wng);
690 current_decoder = slot_num;
691 activate_inactive_grab_clients(slot_num, s->format, &s->fc->filters);
694 static void rn_event_handler(struct task *t)
696 // struct receiver_node *rn = t->private_data;
697 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
701 static void open_receiver(int format)
703 struct audio_format_info *a = &afi[format];
706 struct receiver_node *rn;
708 slot_num = get_empty_slot();
710 clean_exit(EXIT_FAILURE, PARA_STRERROR(-slot_num));
715 s->receiver_node = para_calloc(sizeof(struct receiver_node));
716 rn = s->receiver_node;
717 rn->receiver = a->receiver;
718 rn->conf = a->receiver_conf;
719 ret = a->receiver->open(s->receiver_node);
721 PARA_ERROR_LOG("failed to open receiver (%s)\n",
722 PARA_STRERROR(-ret));
723 free(s->receiver_node);
724 s->receiver_node = NULL;
727 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
728 audio_formats[s->format], a->receiver->name, slot_num);
729 rn->task.private_data = s->receiver_node;
730 rn->task.pre_select = a->receiver->pre_select;
731 rn->task.post_select = a->receiver->post_select;
732 rn->task.event_handler = rn_event_handler;
734 sprintf(rn->task.status, "receiver node");
735 register_task(&rn->task);
738 static int is_frozen(int format)
740 struct audio_format_info *a = &afi[format];
742 return (tv_diff(now, &a->restart_barrier, NULL) > 0)? 0 : 1;
745 static void open_current_receiver(void)
751 i = get_audio_format_num(af_status);
754 if (decoder_running(i) || is_frozen(i))
759 static void compute_time_diff(const struct timeval *status_time)
761 struct timeval tmp, diff;
764 const struct timeval max_deviation = {0, 500 * 1000};
765 const int time_smooth = 5;
767 sign = tv_diff(status_time, now, &diff);
768 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
769 // sign, sa_time_diff_sign);
771 sa_time_diff_sign = sign;
777 int s = tv_diff(&diff, &sa_time_diff, &tmp);
778 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
779 PARA_WARNING_LOG("time diff jump: %lims\n",
783 sa_time_diff_sign = tv_convex_combination(
784 sa_time_diff_sign * time_smooth, &sa_time_diff,
785 count > 10? sign : sign * time_smooth, &diff,
788 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
791 sa_time_diff_sign ? "+" : "-",
796 static void check_stat_line(char *line)
800 long unsigned sec, usec;
803 // PARA_INFO_LOG("line: %s\n", line);
806 itemnum = stat_line_valid(line);
808 PARA_WARNING_LOG("invalid status line: %s\n", line);
811 tmp = make_message("%s\n", line);
812 stat_client_write(tmp, itemnum);
814 free(stat_item_values[itemnum]);
815 stat_item_values[itemnum] = para_strdup(line);
816 ilen = strlen(status_item_list[itemnum]);
819 playing = strstr(line, "playing")? 1 : 0;
823 af_status = para_strdup(line + ilen + 1);
826 offset_seconds = atoi(line + ilen + 1);
829 length_seconds = atoi(line + ilen + 1);
831 case SI_STREAM_START:
832 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
833 server_stream_start.tv_sec = sec;
834 server_stream_start.tv_usec = usec;
837 case SI_CURRENT_TIME:
838 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
839 struct timeval tv = {sec, usec};
840 compute_time_diff(&tv);
846 static void handle_signal(int sig)
851 pid_t pid = para_reap_child();
854 PARA_CRIT_LOG("para_client died (pid %d)\n", pid);
859 PARA_EMERG_LOG("terminating on signal %d\n", sig);
860 clean_exit(EXIT_FAILURE, "caught deadly signal");
865 static void check_timeouts(void)
868 int slot_num, timeout = conf.stream_timeout_arg;
870 FOR_EACH_SLOT(slot_num) {
871 struct slot_info *s = &slot[slot_num];
874 /* check read time */
875 if (s->receiver_node &&
876 now->tv_sec > s->rtime.tv_sec + timeout) {
877 PARA_INFO_LOG("%s stream (slot %d) not ready\n",
878 audio_formats[s->format], slot_num);
879 s->receiver_node->eof = 1;
884 static void try_to_close_slot(int slot_num)
886 struct slot_info *s = &slot[slot_num];
890 if (s->receiver_node && !s->receiver_node->eof)
892 if (s->fc && !s->fc->eof)
894 if (s->wng && !s->wng->eof)
896 PARA_INFO_LOG("closing slot %d \n", slot_num);
899 close_filters(s->fc);
901 close_receiver(slot_num);
902 clear_slot(slot_num);
905 static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
910 if (audiod_status != AUDIOD_ON)
913 open_current_receiver();
916 struct receiver_node *rn;
918 try_to_close_slot(i);
919 if (slot[i].format < 0)
921 rn = slot[i].receiver_node;
922 if (rn && rn->loaded && !slot[i].wng) {
929 static void audiod_post_select(struct sched *s, __a_unused struct task *t)
935 struct receiver_node *rn = slot[i].receiver_node;
937 if (rn && rn->loaded)
938 slot[i].rtime = *now;
942 static void init_audiod_task(struct audiod_task *at)
944 at->task.pre_select = audiod_pre_select;
945 at->task.post_select = audiod_post_select;
946 at->task.private_data = at;
948 sprintf(at->task.status, "audiod task");
951 static int parse_stream_command(const char *txt, char **cmd)
953 char *p = strchr(txt, ':');
957 return -E_MISSING_COLON;
959 FOR_EACH_AUDIO_FORMAT(i) {
960 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
965 return -E_UNSUPPORTED_AUDIO_FORMAT;
968 static int add_filter(int format, char *cmdline)
970 struct audio_format_info *a = &afi[format];
971 int filter_num, nf = a->num_filters;
973 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
976 a->filters[nf] = &filters[filter_num];
978 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf + 1,
979 a->filters[nf]->name);
983 static int init_writers(void)
987 struct audio_format_info *a;
989 init_supported_writers();
990 nw = PARA_MAX(1, conf.writer_given);
991 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
992 FOR_EACH_AUDIO_FORMAT(i) {
994 a->writer_conf = para_malloc(nw * sizeof(void *));
995 a->writers = para_malloc(nw * sizeof(struct writer *));
998 for (i = 0; i < conf.writer_given; i++) {
1001 ret = parse_stream_command(conf.writer_arg[i], &cmd);
1005 nw = a->num_writers;
1006 wconf = check_writer_arg(cmd, &writer_num);
1011 a->writers[nw] = &writers[writer_num];
1012 a->writer_conf[nw] = wconf;
1013 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
1014 nw, writer_names[writer_num]);
1022 static int init_receivers(void)
1024 int i, ret, receiver_num;
1026 struct audio_format_info *a;
1028 for (i = 0; receivers[i].name; i++) {
1029 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
1030 receivers[i].init(&receivers[i]);
1032 for (i = 0; i < conf.receiver_given; i++) {
1033 char *arg = conf.receiver_arg[i];
1034 char *recv = strchr(arg, ':');
1035 ret = -E_MISSING_COLON;
1040 ret = get_audio_format_num(arg);
1043 afi[ret].receiver_conf = check_receiver_arg(recv, &receiver_num);
1044 if (!afi[ret].receiver_conf) {
1045 ret = -E_RECV_SYNTAX;
1048 afi[ret].receiver = &receivers[receiver_num];
1050 /* use the first available receiver with no arguments
1051 * for those audio formats for which no receiver
1054 cmd = para_strdup(receivers[0].name);
1055 FOR_EACH_AUDIO_FORMAT(i) {
1057 if (a->receiver_conf)
1059 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
1060 if (!a->receiver_conf)
1061 return -E_RECV_SYNTAX;
1062 a->receiver = &receivers[receiver_num];
1070 static int init_default_filters(void)
1074 FOR_EACH_AUDIO_FORMAT(i) {
1075 struct audio_format_info *a = &afi[i];
1080 continue; /* no default -- nothing to to */
1081 /* add "dec" to audio format name */
1082 tmp = make_message("%sdec", audio_formats[i]);
1083 for (j = 0; filters[j].name; j++)
1084 if (!strcmp(tmp, filters[j].name))
1087 ret = -E_UNSUPPORTED_FILTER;
1088 if (!filters[j].name)
1090 tmp = para_strdup(filters[j].name);
1091 ret = add_filter(i, tmp);
1095 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
1102 static int init_filters(void)
1106 filter_init(filters);
1107 nf = PARA_MAX(1, conf.filter_given);
1108 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
1109 FOR_EACH_AUDIO_FORMAT(i) {
1110 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
1111 afi[i].filters = para_malloc(nf * sizeof(struct filter *));
1113 if (!conf.no_default_filters_given)
1114 return init_default_filters();
1115 for (i = 0; i < conf.filter_given; i++) {
1116 char *arg = conf.filter_arg[i];
1117 char *filter_name = strchr(arg, ':');
1118 ret = -E_MISSING_COLON;
1121 *filter_name = '\0';
1123 ret = get_audio_format_num(arg);
1126 ret = add_filter(ret, filter_name);
1130 ret = init_default_filters(); /* use default values for the rest */
1135 static int init_stream_io(void)
1139 ret = init_writers();
1142 ret = init_receivers();
1145 ret = init_filters();
1151 static int dump_commands(int fd)
1153 char *buf = para_strdup(""), *tmp = NULL;
1157 FOR_EACH_COMMAND(i) {
1158 tmp = make_message("%s%s\t%s\n", buf, cmds[i].name,
1159 cmds[i].description);
1163 ret = client_write(fd, buf);
1169 * command handlers don't close their fd on errors (ret < 0) so that
1170 * its caller can send an error message. Otherwise (ret >= 0) it's up
1171 * to each individual command to close the fd if necessary.
1174 static int com_help(int fd, int argc, char **argv)
1178 const char *dflt = "No such command. Available commands:\n";
1181 ret = dump_commands(fd);
1184 FOR_EACH_COMMAND(i) {
1185 if (strcmp(cmds[i].name, argv[1]))
1188 "NAME\n\t%s -- %s\n"
1189 "SYNOPSIS\n\tpara_audioc %s\n"
1190 "DESCRIPTION\n%s\n",
1192 cmds[i].description,
1196 ret = client_write(fd, buf);
1200 ret = client_write(fd, dflt);
1202 ret = dump_commands(fd);
1209 static int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
1213 long unsigned mask = ~0LU;
1217 for (i = 1; i < argc; i++) {
1218 ret = stat_item_valid(argv[i]);
1224 PARA_INFO_LOG("mask: 0x%lx\n", mask);
1225 if (mask & (1 << SI_PLAY_TIME)) {
1226 struct timeval *t = wstime();
1227 char *ts = get_time_string(t);
1229 ret = client_write(fd, ts);
1235 if (mask & (1 << SI_AUDIOD_UPTIME)) {
1236 char *tmp, *us = uptime_str();
1237 tmp = make_message("%s:%s\n",
1238 status_item_list[SI_AUDIOD_UPTIME], us);
1240 ret = client_write(fd, tmp);
1245 if (mask & (1 << SI_AUDIOD_STATUS)) {
1246 char *s = audiod_status_string();
1247 ret = client_write(fd, s);
1252 if (mask & (1 << SI_DECODER_FLAGS)) {
1253 char *df =decoder_flags();
1254 ret = client_write(fd, df);
1260 for (i = 0; i < NUM_STAT_ITEMS; i++) {
1262 if (!((1 << i) & mask))
1264 v = stat_item_values[i];
1265 tmp = make_message("%s%s%s", buf? buf: "",
1266 v? v : "", v? "\n" : "");
1270 ret = client_write(fd, buf);
1273 ret = stat_client_add(fd, mask);
1278 static int com_grab(int fd, char *cmdline)
1280 struct grab_client *gc;
1281 struct filter_node *fn;
1285 gc = grab_client_new(fd, cmdline, &err);
1288 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
1290 activate_grab_client(gc, fn);
1293 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
1295 if (err == -E_GC_HELP_GIVEN) {
1296 msg = make_message("%s\n\n", grab_client_args_info_usage);
1297 for (i = 0; grab_client_args_info_help[i]; i++) {
1298 char *tmp = make_message("%s%s\n", msg,
1299 grab_client_args_info_help[i]);
1304 msg = make_message("%s %s\n",
1305 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
1306 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
1307 err = client_write(fd, msg);
1315 static int __noreturn com_term(int fd, __a_unused int argc, __a_unused char **argv)
1318 clean_exit(EXIT_SUCCESS, "terminating on user request");
1321 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
1323 audiod_status = AUDIOD_ON;
1328 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
1330 audiod_status = AUDIOD_OFF;
1335 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
1337 audiod_status = AUDIOD_STANDBY;
1342 static int com_cycle(int fd, int argc, char **argv)
1344 switch (audiod_status) {
1346 return com_sb(fd, argc, argv);
1349 return com_on(fd, argc, argv);
1351 case AUDIOD_STANDBY:
1352 return com_off(fd, argc, argv);
1359 static int check_perms(uid_t uid)
1363 if (!conf.user_allow_given)
1365 for (i = 0; i < conf.user_allow_given; i++)
1366 if (uid == conf.user_allow_arg[i])
1368 return -E_UCRED_PERM;
1371 static int handle_connect(void)
1373 int i, argc, ret, clifd = -1;
1374 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
1375 struct sockaddr_un unix_addr;
1377 ret = para_accept(cmd_task->fd, &unix_addr, sizeof(struct sockaddr_un));
1381 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
1384 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
1385 ret = check_perms(ret);
1388 ret = -E_INVALID_AUDIOD_CMD;
1389 cmd = para_strdup(buf);
1390 p = strchr(cmd, '\n');
1397 for (i = 0; cmds[i].name; i++) {
1399 if (strcmp(cmds[i].name, cmd))
1401 if (cmds[i].handler) {
1402 argc = split_args(buf, &argv, "\n");
1403 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
1404 ret = cmds[i].handler(clifd, argc, argv);
1407 for (j = 0; p[j]; j++)
1410 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
1411 ret = cmds[i].line_handler(clifd, p);
1414 ret = -E_INVALID_AUDIOD_CMD; /* cmd not found */
1419 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
1420 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
1421 client_write(clifd, tmp);
1428 static void audiod_get_socket(void)
1430 struct sockaddr_un unix_addr;
1432 if (conf.socket_given)
1433 socket_name = para_strdup(conf.socket_arg);
1435 char *hn = para_hostname();
1436 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1440 PARA_NOTICE_LOG("connecting to local socket %s\n", socket_name);
1441 if (conf.force_given)
1442 unlink(socket_name);
1443 cmd_task->fd = create_pf_socket(socket_name, &unix_addr,
1444 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1445 if (cmd_task->fd < 0) {
1446 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1447 exit(EXIT_FAILURE); /* do not unlink socket */
1449 if (listen(cmd_task->fd , 5) < 0) {
1450 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1451 exit(EXIT_FAILURE); /* do not unlink socket */
1453 add_close_on_fork_list(cmd_task->fd);
1456 static int open_stat_pipe(void)
1458 int ret, fd[3] = {-1, 1, 0};
1460 ret = para_exec_cmdline_pid(&pid, BINDIR "/para_client stat", fd);
1463 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret);
1464 add_close_on_fork_list(ret);
1466 clean_exit(EXIT_FAILURE, "failed to open status pipe");
1470 void signal_event_handler(struct task *t)
1472 struct signal_task *st = t->private_data;
1473 handle_signal(st->signum);
1476 void signal_pre_select(struct sched *s, struct task *t)
1478 struct signal_task *st = t->private_data;
1480 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1483 void signal_post_select(struct sched *s, struct task *t)
1485 struct signal_task *st = t->private_data;
1487 if (!FD_ISSET(st->fd, &s->rfds))
1489 t->ret = -E_SIGNAL_CAUGHT;
1490 st->signum = para_next_signal();
1493 void signal_setup_default(struct signal_task *st)
1495 st->task.pre_select = signal_pre_select;
1496 st->task.post_select = signal_post_select;
1497 st->task.private_data = st;
1499 sprintf(st->task.status, "signal task");
1502 static void command_pre_select(struct sched *s, struct task *t)
1504 struct command_task *ct = t->private_data;
1505 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
1509 static void command_post_select(struct sched *s, struct task *t)
1512 struct command_task *ct = t->private_data;
1514 if (audiod_status != AUDIOD_OFF)
1515 audiod_status_dump();
1516 t->ret = 1; /* always successful */
1517 if (!FD_ISSET(ct->fd, &s->rfds))
1519 ret = handle_connect();
1521 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
1524 static void init_command_task(struct command_task *ct)
1526 ct->task.pre_select = command_pre_select;
1527 ct->task.post_select = command_post_select;
1528 ct->task.private_data = ct;
1530 sprintf(ct->task.status, "command task");
1533 static void status_pre_select(struct sched *s, struct task *t)
1535 struct status_task *st = t->private_data;
1537 if (st->fd >= 0 && audiod_status == AUDIOD_OFF)
1539 if (st->fd < 0 && audiod_status != AUDIOD_OFF) {
1540 st->fd = open_stat_pipe();
1544 if (st->fd >= 0 && audiod_status != AUDIOD_OFF)
1545 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1548 static void status_post_select(struct sched *s, struct task *t)
1550 struct status_task *st = t->private_data;
1554 if (st->fd < 0 || !FD_ISSET(st->fd, &s->rfds))
1556 ret = read(st->fd, st->buf + st->loaded,
1557 STRINGSIZE - 1 - st->loaded);
1560 /* avoid busy loop if server is down */
1561 while (sleep(1) > 0) /* FIXME */
1564 st->buf[ret + st->loaded] = '\0';
1565 st->loaded = for_each_line(st->buf, ret + st->loaded,
1570 static void init_status_task(struct status_task *st)
1572 st->task.pre_select = status_pre_select;
1573 st->task.post_select = status_post_select;
1574 st->task.private_data = st;
1579 sprintf(st->task.status, "status task");
1582 static void set_initial_status(void)
1584 audiod_status = AUDIOD_ON;
1585 if (!conf.mode_given)
1587 if (!strcmp(conf.mode_arg, "sb")) {
1588 audiod_status = AUDIOD_STANDBY;
1591 if (!strcmp(conf.mode_arg, "off")) {
1592 audiod_status = AUDIOD_OFF;
1595 if (strcmp(conf.mode_arg, "on"))
1596 PARA_WARNING_LOG("%s", "invalid mode\n");
1599 int main(int argc, char *argv[])
1608 hostname = para_hostname();
1609 cmdline_parser(argc, argv, &conf);
1610 para_drop_privileges(conf.user_arg, conf.group_arg);
1611 cf = configfile_exists();
1613 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
1614 PARA_EMERG_LOG("%s", "parse error in config file\n");
1618 if (conf.logfile_given)
1619 logfile = open_log(conf.logfile_arg);
1620 log_welcome("para_audiod", conf.loglevel_arg);
1621 i = init_stream_io();
1623 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
1626 server_uptime(UPTIME_SET);
1627 set_initial_status();
1631 setup_signal_handling();
1632 if (conf.daemon_given)
1634 audiod_get_socket(); /* doesn't return on errors */
1636 signal_setup_default(sig_task);
1637 sig_task->task.event_handler = signal_event_handler;
1639 init_status_task(stat_task);
1640 init_command_task(cmd_task);
1641 init_audiod_task(at);
1643 register_task(&sig_task->task);
1644 register_task(&cmd_task->task);
1645 register_task(&stat_task->task);
1646 register_task(&at->task);
1647 s.default_timeout.tv_sec = 0;
1648 s.default_timeout.tv_usec = 99 * 1000;
1651 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
1652 return EXIT_FAILURE;