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 /** writer start time */
87 struct timeval wstime;
88 /** the receiver info associated with this slot */
89 struct receiver_node *receiver_node;
90 /** the active filter chain */
91 struct filter_chain *fc;
92 /** the active writer node group */
93 struct writer_node_group *wng;
95 static struct slot_info slot[MAX_STREAM_SLOTS];
97 extern const char *status_item_list[NUM_STAT_ITEMS];
99 static struct gengetopt_args_info conf;
100 static struct timeval server_stream_start, sa_time_diff;
101 static int playing, current_decoder = -1,
102 audiod_status = AUDIOD_ON, offset_seconds, length_seconds,
103 sa_time_diff_sign = 1;
104 static char *af_status, /* the audio format announced in server status */
105 *socket_name, *hostname;
106 static char *stat_item_values[NUM_STAT_ITEMS];
107 static FILE *logfile;
108 static const struct timeval restart_delay = {0, 200 * 1000};
109 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
110 static struct timeval *now;
112 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
115 * the task for handling audiod commands
117 * \sa struct task, struct sched
119 struct command_task {
120 /** the local listening socket */
122 /** the associated task structure */
127 * the task for audiod's child (para_client stat)
129 * \sa struct task, struct sched
132 /** the output of the stat command is read from this fd */
134 /** stat data is stored here */
135 char buf[STRINGSIZE];
136 /** number of bytes loaded in \a buf */
138 /** the associated task structure */
141 static struct status_task status_task_struct, *stat_task = &status_task_struct;
149 /** defines one command of para_audiod */
150 struct audiod_command {
151 /** the name of the command */
153 /** pointer to the function that handles the command */
154 int (*handler)(int, int, char**);
156 * if the command prefers to handle the full line (rather than the usual
157 * argv[] array), it stores a pointer to the corresponding line handling
158 * function here. In this case, the above \a handler pointer must be NULL.
160 int (*line_handler)(int, char*);
161 /** one-line description of the command */
162 const char *description;
163 /** summary of the command line options */
164 const char *synopsis;
165 /** the long help text */
168 static int com_grab(int, char *);
169 static int com_cycle(int, int, char **);
170 static int com_help(int, int, char **);
171 static int com_off(int, int, char **);
172 static int com_on(int, int, char **);
173 static int com_sb(int, int, char **);
174 static int com_stat(int, int, char **);
175 static int com_term(int, int, char **);
176 static struct audiod_command cmds[] = {
179 .handler = com_cycle,
180 .description = "switch to next mode",
184 "on -> standby -> off -> on\n"
189 .line_handler = com_grab,
190 .description = "grab the audio stream",
191 .synopsis = "-- grab [grab_options]",
194 "grab ('splice') the audio stream at any position in the filter \n"
195 "chain and send that data back to the client. Try\n"
196 "\t para_audioc -- grab -h\n"
197 "for the list of available options.\n"
203 .description = "display command list or help for given command",
204 .synopsis = "help [command]",
207 "When I was younger, so much younger than today, I never needed\n"
208 "anybody's help in any way. But now these days are gone, I'm not so\n"
209 "self assured. Now I find I've changed my mind and opened up the doors.\n"
211 " -- Beatles: Help\n"
217 .description = "deactivate para_audiod",
221 "Close connection to para_server and stop all decoders.\n"
227 .description = "activate para_audiod",
231 "Establish connection to para_server, retrieve para_server's current\n"
232 "status. If playing, start corresponding decoder. Otherwise stop\n"
239 .description = "enter standby mode",
243 "Stop all decoders but leave connection to para_server open.\n"
249 .description = "print status information",
250 .synopsis = "stat [item1 ...]",
253 "Dump given status items (all if none given) to stdout.\n"
259 .description = "terminate audiod",
263 "Stop all decoders, shut down connection to para_server and exit.\n"
271 /** iterate over all slots */
272 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
273 /** iterate over all supported audio formats */
274 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
275 /** iterate over the array of all audiod commands */
276 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
279 * get the audio format number
280 * \param name the name of the audio format
282 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
283 * \a name is not a supported audio format.
285 int get_audio_format_num(char *name)
288 FOR_EACH_AUDIO_FORMAT(i)
289 if (!strcmp(name, audio_formats[i]))
291 return -E_UNSUPPORTED_AUDIO_FORMAT;
295 * log function. first argument is loglevel.
297 void para_log(int ll, const char* fmt,...)
303 char str[MAXLINE] = "";
305 if (ll < conf.loglevel_arg)
307 outfd = logfile? logfile : stderr;
310 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
311 fprintf(outfd, "%s %s ", str, hostname);
312 if (conf.loglevel_arg <= INFO)
313 fprintf(outfd, "%i ", ll);
315 vfprintf(outfd, fmt, argp);
319 static int client_write(int fd, const char *buf)
321 size_t len = strlen(buf);
322 return write(fd, buf, len) != len? -E_CLIENT_WRITE: 1;
325 static char *get_time_string(struct timeval *newest_stime)
327 struct timeval diff, adj_stream_start, tmp;
328 int total = 0, use_server_time = 1;
333 return make_message("%s:\n", status_item_list[SI_PLAY_TIME]);
335 if (audiod_status == AUDIOD_OFF)
337 if (sa_time_diff_sign > 0)
338 tv_diff(&server_stream_start, &sa_time_diff,
341 tv_add(&server_stream_start, &sa_time_diff,
343 tmp = adj_stream_start;
344 if (newest_stime && audiod_status == AUDIOD_ON) {
345 tv_diff(newest_stime, &adj_stream_start, &diff);
346 if (tv2ms(&diff) < 5000) {
351 tv_diff(now, &tmp, &diff);
352 total = diff.tv_sec + offset_seconds;
353 if (total > length_seconds)
354 total = length_seconds;
359 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
360 status_item_list[SI_PLAY_TIME],
361 use_server_time? "~" : "",
364 (length_seconds - total) / 60,
365 (length_seconds - total) % 60,
366 length_seconds? (total * 100 + length_seconds / 2) /
373 __malloc static char *audiod_status_string(void)
375 const char *status = (audiod_status == AUDIOD_ON)?
376 "on" : (audiod_status == AUDIOD_OFF)? "off": "sb";
377 return make_message("%s:%s\n", status_item_list[SI_AUDIOD_STATUS], status);
380 static struct timeval *wstime(void)
383 struct timeval *max = NULL;
385 struct slot_info *s = &slot[i];
388 if (max && tv_diff(&s->wstime, max, NULL) <= 0)
394 __malloc static char *decoder_flags(void)
397 char decoder_flags[MAX_STREAM_SLOTS + 1];
400 struct slot_info *s = &slot[i];
402 if (s->receiver_node)
406 decoder_flags[i] = flag;
408 decoder_flags[MAX_STREAM_SLOTS] = '\0';
409 return make_message("%s:%s\n", status_item_list[SI_DECODER_FLAGS],
413 static char *configfile_exists(void)
415 static char *config_file;
418 char *home = para_homedir();
419 config_file = make_message("%s/.paraslash/audiod.conf", home);
422 return file_exists(config_file)? config_file : NULL;
425 static void setup_signal_handling(void)
427 sig_task->fd = para_signal_init();
428 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
429 para_install_sighandler(SIGINT);
430 para_install_sighandler(SIGTERM);
431 para_install_sighandler(SIGCHLD);
432 para_install_sighandler(SIGHUP);
433 signal(SIGPIPE, SIG_IGN);
436 static void audiod_status_dump(void)
438 static char *p_ts, *p_us, *p_as, *p_df;
439 struct timeval *t = wstime();
440 char *us, *tmp = get_time_string(t);
442 if (tmp && (!p_ts || strcmp(tmp, p_ts)))
443 stat_client_write(tmp, SI_PLAY_TIME);
448 tmp = make_message("%s:%s\n", status_item_list[SI_AUDIOD_UPTIME], us);
450 if (!p_us || strcmp(p_us, tmp))
451 stat_client_write(tmp, SI_AUDIOD_UPTIME);
455 tmp = audiod_status_string();
456 if (!p_as || strcmp(p_as, tmp))
457 stat_client_write(tmp, SI_AUDIOD_STATUS);
461 tmp = decoder_flags();
462 if (!p_df || strcmp(p_df, tmp))
463 stat_client_write(tmp, SI_DECODER_FLAGS);
468 static void clear_slot(int slot_num)
470 struct slot_info *s = &slot[slot_num];
472 PARA_INFO_LOG("clearing slot %d\n", slot_num);
473 memset(s, 0, sizeof(struct slot_info));
477 static void close_receiver(int slot_num)
479 struct slot_info *s = &slot[slot_num];
480 struct audio_format_info *a;
482 if (s->format < 0 || !s->receiver_node)
485 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
486 audio_formats[s->format] , slot_num, s->receiver_node->eof);
487 if (!s->receiver_node->eof)
488 unregister_task(&s->receiver_node->task);
489 a->receiver->close(s->receiver_node);
490 free(s->receiver_node);
491 s->receiver_node = NULL;
492 /* set restart barrier */
493 tv_add(now, &restart_delay, &afi[s->format].restart_barrier);
496 static void kill_all_decoders(void)
501 struct slot_info *s = &slot[i];
502 if (s->wng && !s->wng->eof) {
503 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
505 wng_unregister(s->wng);
508 if (s->fc && !s->fc->eof) {
509 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i);
510 unregister_task(&s->fc->task);
513 if (s->receiver_node && !s->receiver_node->eof) {
514 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i);
515 unregister_task(&s->receiver_node->task);
516 s->receiver_node->eof = 1;
521 static int get_empty_slot(void)
532 if (s->wng || s->receiver_node || s->fc)
537 return -E_NO_MORE_SLOTS;
540 static int decoder_running(int format)
547 if (s->format == format && s->receiver_node)
549 if (s->format == format && s->wng)
555 static void close_stat_pipe(void)
559 if (stat_task->fd < 0)
561 PARA_NOTICE_LOG("%s", "closing status pipe\n");
562 close(stat_task->fd);
563 del_close_on_fork_list(stat_task->fd);
566 for (i = 0; i < NUM_STAT_ITEMS; i++) {
567 free(stat_item_values[i]);
568 stat_item_values[i] = NULL;
573 audiod_status_dump();
575 stat_item_values[SI_STATUS_BAR] = make_message(
576 "%s:no connection to para_server\n",
577 status_item_list[SI_STATUS_BAR]);
578 stat_client_write(stat_item_values[SI_STATUS_BAR], SI_STATUS_BAR);
581 static void __noreturn clean_exit(int status, const char *msg)
583 PARA_EMERG_LOG("%s\n", msg);
586 if (stat_task->fd >= 0)
592 * get the number of filters
594 * \param audio_format_num the number identifying the audio format
596 * \return the number of filters for the given audio format
600 int num_filters(int audio_format_num)
602 return afi[audio_format_num].num_filters;
605 static void filter_event_handler(struct task *t)
607 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
611 static void open_filters(int slot_num)
613 struct slot_info *s = &slot[slot_num];
614 struct audio_format_info *a = &afi[s->format];
615 int nf = a->num_filters;
621 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
622 s->fc = para_calloc(sizeof(struct filter_chain));
623 INIT_LIST_HEAD(&s->fc->filters);
624 s->fc->inbuf = s->receiver_node->buf;
625 s->fc->in_loaded = &s->receiver_node->loaded;
626 s->fc->input_eof = &s->receiver_node->eof;
627 s->fc->task.pre_select = filter_pre_select;
628 s->fc->task.event_handler = filter_event_handler;
629 s->fc->task.private_data = s->fc;
630 s->fc->task.flags = 0;
633 s->receiver_node->output_eof = &s->fc->eof;
634 sprintf(s->fc->task.status, "filter chain");
635 for (i = 0; i < nf; i++) {
636 struct filter_node *fn = para_calloc(sizeof(struct filter_node));
637 fn->conf = a->filter_conf[i];
639 fn->filter = a->filters[i];
640 INIT_LIST_HEAD(&fn->callbacks);
641 list_add_tail(&fn->node, &s->fc->filters);
642 fn->filter->open(fn);
643 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
644 audio_formats[s->format], i + 1, nf,
645 fn->filter->name, slot_num);
646 s->fc->outbuf = fn->buf;
647 s->fc->out_loaded = &fn->loaded;
649 register_task(&s->fc->task);
652 static struct filter_node *find_filter_node(int slot_num, int format, int filternum)
654 struct filter_node *fn;
658 struct slot_info *s = &slot[i];
659 if (s->format < 0 || !s->fc)
661 if (slot_num >= 0 && slot_num != i)
663 if (format >= 0 && s->format != format)
665 if (num_filters(i) < filternum)
669 list_for_each_entry(fn, &s->fc->filters, node)
670 if (filternum <= 0 || j++ == filternum)
677 static void wng_event_handler(struct task *t)
679 struct writer_node_group *wng = t->private_data;
684 struct slot_info *s = &slot[i];
687 PARA_INFO_LOG("slot %d: %s\n", i, PARA_STRERROR(-t->ret));
691 static void open_writers(int slot_num)
694 struct slot_info *s = &slot[slot_num];
695 struct audio_format_info *a = &afi[s->format];
697 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
699 s->wng = setup_default_wng();
701 s->wng = wng_new(a->num_writers);
703 s->wng->buf = s->fc->outbuf;
704 s->wng->loaded = s->fc->out_loaded;
705 s->wng->input_eof = &s->fc->eof;
706 s->wng->channels = &s->fc->channels;
707 s->wng->samplerate = &s->fc->samplerate;
708 s->fc->output_eof = &s->wng->eof;
709 PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
711 s->wng->buf = s->receiver_node->buf;
712 s->wng->loaded = &s->receiver_node->loaded;
713 s->wng->input_eof = &s->receiver_node->eof;
715 s->wng->task.event_handler = wng_event_handler;
716 for (i = 0; i < a->num_writers; i++) {
717 s->wng->writer_nodes[i].conf = a->writer_conf[i];
718 s->wng->writer_nodes[i].writer = a->writers[i];
719 sprintf(s->wng->writer_nodes[i].task.status, "writer_node");
721 ret = wng_open(s->wng);
723 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
727 current_decoder = slot_num;
728 activate_inactive_grab_clients(slot_num, s->format, &s->fc->filters);
731 static void rn_event_handler(struct task *t)
733 // struct receiver_node *rn = t->private_data;
734 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
738 static void open_receiver(int format)
740 struct audio_format_info *a = &afi[format];
743 struct receiver_node *rn;
745 slot_num = get_empty_slot();
747 clean_exit(EXIT_FAILURE, PARA_STRERROR(-slot_num));
750 s->receiver_node = para_calloc(sizeof(struct receiver_node));
751 rn = s->receiver_node;
752 rn->receiver = a->receiver;
753 rn->conf = a->receiver_conf;
754 ret = a->receiver->open(s->receiver_node);
756 PARA_ERROR_LOG("failed to open receiver (%s)\n",
757 PARA_STRERROR(-ret));
758 free(s->receiver_node);
759 s->receiver_node = NULL;
762 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
763 audio_formats[s->format], a->receiver->name, slot_num);
764 rn->task.private_data = s->receiver_node;
765 rn->task.pre_select = a->receiver->pre_select;
766 rn->task.post_select = a->receiver->post_select;
767 rn->task.event_handler = rn_event_handler;
769 sprintf(rn->task.status, "receiver node");
770 register_task(&rn->task);
773 static int is_frozen(int format)
775 struct audio_format_info *a = &afi[format];
777 return (tv_diff(now, &a->restart_barrier, NULL) > 0)? 0 : 1;
780 static void open_current_receiver(void)
786 i = get_audio_format_num(af_status);
789 if (decoder_running(i) || is_frozen(i))
794 static void compute_time_diff(const struct timeval *status_time)
796 struct timeval tmp, diff;
799 const struct timeval max_deviation = {0, 500 * 1000};
800 const int time_smooth = 5;
802 sign = tv_diff(status_time, now, &diff);
803 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
804 // sign, sa_time_diff_sign);
806 sa_time_diff_sign = sign;
812 int s = tv_diff(&diff, &sa_time_diff, &tmp);
813 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
814 PARA_WARNING_LOG("time diff jump: %lims\n",
818 sa_time_diff_sign = tv_convex_combination(
819 sa_time_diff_sign * time_smooth, &sa_time_diff,
820 count > 10? sign : sign * time_smooth, &diff,
823 PARA_DEBUG_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
826 sa_time_diff_sign ? "+" : "-",
831 static void check_stat_line(char *line)
835 long unsigned sec, usec;
838 // PARA_INFO_LOG("line: %s\n", line);
841 itemnum = stat_line_valid(line);
843 PARA_WARNING_LOG("invalid status line: %s\n", line);
846 tmp = make_message("%s\n", line);
847 stat_client_write(tmp, itemnum);
849 free(stat_item_values[itemnum]);
850 stat_item_values[itemnum] = para_strdup(line);
851 ilen = strlen(status_item_list[itemnum]);
854 playing = strstr(line, "playing")? 1 : 0;
858 af_status = para_strdup(line + ilen + 1);
861 offset_seconds = atoi(line + ilen + 1);
864 length_seconds = atoi(line + ilen + 1);
866 case SI_STREAM_START:
867 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
868 server_stream_start.tv_sec = sec;
869 server_stream_start.tv_usec = usec;
872 case SI_CURRENT_TIME:
873 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
874 struct timeval tv = {sec, usec};
875 compute_time_diff(&tv);
881 static void handle_signal(int sig)
886 pid_t pid = para_reap_child();
889 PARA_CRIT_LOG("para_client died (pid %d)\n", pid);
894 PARA_EMERG_LOG("terminating on signal %d\n", sig);
895 clean_exit(EXIT_FAILURE, "caught deadly signal");
900 static void try_to_close_slot(int slot_num)
902 struct slot_info *s = &slot[slot_num];
906 if (s->receiver_node && !s->receiver_node->eof)
908 if (s->fc && !s->fc->eof)
910 if (s->wng && !s->wng->eof)
912 PARA_INFO_LOG("closing slot %d \n", slot_num);
914 close_filters(s->fc);
916 close_receiver(slot_num);
917 clear_slot(slot_num);
920 static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
926 if (audiod_status != AUDIOD_ON)
929 open_current_receiver();
931 struct slot_info *s = &slot[i];
932 struct audio_format_info *a;
934 try_to_close_slot(i);
938 if (!s->receiver_node)
940 if (!a->num_filters) {
941 if (s->receiver_node->loaded && !s->wng)
945 if (s->receiver_node->loaded && !s->fc) {
949 if (s->fc && *s->fc->out_loaded && !s->wng)
954 static void audiod_post_select(struct sched *s, __a_unused struct task *t)
956 /* only save away the current time for other users */
961 static void init_audiod_task(struct task *t)
963 t->pre_select = audiod_pre_select;
964 t->post_select = audiod_post_select;
965 t->event_handler = NULL;
968 sprintf(t->status, "audiod task");
971 static int parse_stream_command(const char *txt, char **cmd)
973 char *p = strchr(txt, ':');
977 return -E_MISSING_COLON;
979 FOR_EACH_AUDIO_FORMAT(i) {
980 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
985 return -E_UNSUPPORTED_AUDIO_FORMAT;
988 static int add_filter(int format, char *cmdline)
990 struct audio_format_info *a = &afi[format];
991 int filter_num, nf = a->num_filters;
993 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
996 a->filters[nf] = &filters[filter_num];
998 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf + 1,
999 a->filters[nf]->name);
1003 static int init_writers(void)
1007 struct audio_format_info *a;
1009 init_supported_writers();
1010 nw = PARA_MAX(1, conf.writer_given);
1011 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
1012 FOR_EACH_AUDIO_FORMAT(i) {
1014 a->writer_conf = para_malloc(nw * sizeof(void *));
1015 a->writers = para_malloc(nw * sizeof(struct writer *));
1018 for (i = 0; i < conf.writer_given; i++) {
1021 ret = parse_stream_command(conf.writer_arg[i], &cmd);
1025 nw = a->num_writers;
1026 wconf = check_writer_arg(cmd, &writer_num);
1031 a->writers[nw] = &writers[writer_num];
1032 a->writer_conf[nw] = wconf;
1033 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
1034 nw, writer_names[writer_num]);
1042 static int init_receivers(void)
1044 int i, ret, receiver_num;
1046 struct audio_format_info *a;
1048 for (i = 0; receivers[i].name; i++) {
1049 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
1050 receivers[i].init(&receivers[i]);
1052 for (i = 0; i < conf.receiver_given; i++) {
1053 char *arg = conf.receiver_arg[i];
1054 char *recv = strchr(arg, ':');
1055 ret = -E_MISSING_COLON;
1060 ret = get_audio_format_num(arg);
1063 afi[ret].receiver_conf = check_receiver_arg(recv, &receiver_num);
1064 if (!afi[ret].receiver_conf) {
1065 ret = -E_RECV_SYNTAX;
1068 afi[ret].receiver = &receivers[receiver_num];
1070 /* use the first available receiver with no arguments
1071 * for those audio formats for which no receiver
1074 cmd = para_strdup(receivers[0].name);
1075 FOR_EACH_AUDIO_FORMAT(i) {
1077 if (a->receiver_conf)
1079 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
1080 if (!a->receiver_conf)
1081 return -E_RECV_SYNTAX;
1082 a->receiver = &receivers[receiver_num];
1090 static int init_default_filters(void)
1094 FOR_EACH_AUDIO_FORMAT(i) {
1095 struct audio_format_info *a = &afi[i];
1100 continue; /* no default -- nothing to to */
1101 /* add "dec" to audio format name */
1102 tmp = make_message("%sdec", audio_formats[i]);
1103 for (j = 0; filters[j].name; j++)
1104 if (!strcmp(tmp, filters[j].name))
1107 ret = -E_UNSUPPORTED_FILTER;
1108 if (!filters[j].name)
1110 tmp = para_strdup(filters[j].name);
1111 ret = add_filter(i, tmp);
1115 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
1122 static int init_filters(void)
1126 filter_init(filters);
1127 nf = PARA_MAX(1, conf.filter_given);
1128 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
1129 FOR_EACH_AUDIO_FORMAT(i) {
1130 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
1131 afi[i].filters = para_malloc(nf * sizeof(struct filter *));
1133 if (!conf.no_default_filters_given)
1134 return init_default_filters();
1135 for (i = 0; i < conf.filter_given; i++) {
1136 char *arg = conf.filter_arg[i];
1137 char *filter_name = strchr(arg, ':');
1138 ret = -E_MISSING_COLON;
1141 *filter_name = '\0';
1143 ret = get_audio_format_num(arg);
1146 ret = add_filter(ret, filter_name);
1150 ret = init_default_filters(); /* use default values for the rest */
1155 static int init_stream_io(void)
1159 ret = init_writers();
1162 ret = init_receivers();
1165 ret = init_filters();
1171 static int dump_commands(int fd)
1173 char *buf = para_strdup(""), *tmp = NULL;
1177 FOR_EACH_COMMAND(i) {
1178 tmp = make_message("%s%s\t%s\n", buf, cmds[i].name,
1179 cmds[i].description);
1183 ret = client_write(fd, buf);
1189 * command handlers don't close their fd on errors (ret < 0) so that
1190 * its caller can send an error message. Otherwise (ret >= 0) it's up
1191 * to each individual command to close the fd if necessary.
1194 static int com_help(int fd, int argc, char **argv)
1198 const char *dflt = "No such command. Available commands:\n";
1201 ret = dump_commands(fd);
1204 FOR_EACH_COMMAND(i) {
1205 if (strcmp(cmds[i].name, argv[1]))
1208 "NAME\n\t%s -- %s\n"
1209 "SYNOPSIS\n\tpara_audioc %s\n"
1210 "DESCRIPTION\n%s\n",
1212 cmds[i].description,
1216 ret = client_write(fd, buf);
1220 ret = client_write(fd, dflt);
1222 ret = dump_commands(fd);
1229 static int com_stat(int fd, __a_unused int argc, __a_unused char **argv)
1233 long unsigned mask = ~0LU;
1237 for (i = 1; i < argc; i++) {
1238 ret = stat_item_valid(argv[i]);
1244 PARA_INFO_LOG("mask: 0x%lx\n", mask);
1245 if (mask & (1 << SI_PLAY_TIME)) {
1246 struct timeval *t = wstime();
1247 char *ts = get_time_string(t);
1249 ret = client_write(fd, ts);
1255 if (mask & (1 << SI_AUDIOD_UPTIME)) {
1256 char *tmp, *us = uptime_str();
1257 tmp = make_message("%s:%s\n",
1258 status_item_list[SI_AUDIOD_UPTIME], us);
1260 ret = client_write(fd, tmp);
1265 if (mask & (1 << SI_AUDIOD_STATUS)) {
1266 char *s = audiod_status_string();
1267 ret = client_write(fd, s);
1272 if (mask & (1 << SI_DECODER_FLAGS)) {
1273 char *df =decoder_flags();
1274 ret = client_write(fd, df);
1280 for (i = 0; i < NUM_STAT_ITEMS; i++) {
1282 if (!((1 << i) & mask))
1284 v = stat_item_values[i];
1285 tmp = make_message("%s%s%s", buf? buf: "",
1286 v? v : "", v? "\n" : "");
1290 ret = client_write(fd, buf);
1293 ret = stat_client_add(fd, mask);
1298 static int com_grab(int fd, char *cmdline)
1300 struct grab_client *gc;
1301 struct filter_node *fn;
1305 gc = grab_client_new(fd, cmdline, &err);
1308 fn = find_filter_node(gc->conf->slot_arg, gc->audio_format_num, gc->conf->filter_num_arg);
1310 activate_grab_client(gc, fn);
1313 if (err != -E_GC_HELP_GIVEN && err != -E_GC_VERSION_GIVEN)
1315 if (err == -E_GC_HELP_GIVEN) {
1316 msg = make_message("%s\n\n", grab_client_args_info_usage);
1317 for (i = 0; grab_client_args_info_help[i]; i++) {
1318 char *tmp = make_message("%s%s\n", msg,
1319 grab_client_args_info_help[i]);
1324 msg = make_message("%s %s\n",
1325 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE,
1326 GRAB_CLIENT_CMDLINE_PARSER_VERSION);
1327 err = client_write(fd, msg);
1335 static int __noreturn com_term(int fd, __a_unused int argc, __a_unused char **argv)
1338 clean_exit(EXIT_SUCCESS, "terminating on user request");
1341 static int com_on(int fd, __a_unused int argc, __a_unused char **argv)
1343 audiod_status = AUDIOD_ON;
1348 static int com_off(int fd, __a_unused int argc, __a_unused char **argv)
1350 audiod_status = AUDIOD_OFF;
1355 static int com_sb(int fd, __a_unused int argc, __a_unused char **argv)
1357 audiod_status = AUDIOD_STANDBY;
1362 static int com_cycle(int fd, int argc, char **argv)
1364 switch (audiod_status) {
1366 return com_sb(fd, argc, argv);
1369 return com_on(fd, argc, argv);
1371 case AUDIOD_STANDBY:
1372 return com_off(fd, argc, argv);
1379 static int check_perms(uid_t uid)
1383 if (!conf.user_allow_given)
1385 for (i = 0; i < conf.user_allow_given; i++)
1386 if (uid == conf.user_allow_arg[i])
1388 return -E_UCRED_PERM;
1391 static int handle_connect(int accept_fd)
1393 int i, argc, ret, clifd = -1;
1394 char *cmd = NULL, *p, *buf = para_calloc(MAXLINE), **argv = NULL;
1395 struct sockaddr_un unix_addr;
1397 ret = para_accept(accept_fd, &unix_addr, sizeof(struct sockaddr_un));
1401 ret = recv_cred_buffer(clifd, buf, MAXLINE - 1);
1404 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret, buf);
1405 ret = check_perms(ret);
1408 ret = -E_INVALID_AUDIOD_CMD;
1409 cmd = para_strdup(buf);
1410 p = strchr(cmd, '\n');
1417 for (i = 0; cmds[i].name; i++) {
1419 if (strcmp(cmds[i].name, cmd))
1421 if (cmds[i].handler) {
1422 argc = split_args(buf, &argv, "\n");
1423 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv[0], argc);
1424 ret = cmds[i].handler(clifd, argc, argv);
1427 for (j = 0; p[j]; j++)
1430 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd, p);
1431 ret = cmds[i].line_handler(clifd, p);
1434 ret = -E_INVALID_AUDIOD_CMD; /* cmd not found */
1439 if (clifd > 0 && ret < 0 && ret != -E_CLIENT_WRITE) {
1440 char *tmp = make_message("%s\n", PARA_STRERROR(-ret));
1441 client_write(clifd, tmp);
1448 static int audiod_get_socket(void)
1450 struct sockaddr_un unix_addr;
1453 if (conf.socket_given)
1454 socket_name = para_strdup(conf.socket_arg);
1456 char *hn = para_hostname();
1457 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1461 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
1462 if (conf.force_given)
1463 unlink(socket_name);
1464 fd = create_pf_socket(socket_name, &unix_addr,
1465 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1467 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1468 exit(EXIT_FAILURE); /* do not unlink socket */
1470 if (listen(fd , 5) < 0) {
1471 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1472 exit(EXIT_FAILURE); /* do not unlink socket */
1474 add_close_on_fork_list(fd);
1478 static int open_stat_pipe(void)
1480 int ret, fd[3] = {-1, 1, 0};
1482 ret = para_exec_cmdline_pid(&pid, BINDIR "/para_client stat", fd);
1485 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret);
1486 add_close_on_fork_list(ret);
1488 clean_exit(EXIT_FAILURE, "failed to open status pipe");
1492 void signal_event_handler(struct task *t)
1494 struct signal_task *st = t->private_data;
1495 handle_signal(st->signum);
1498 void signal_pre_select(struct sched *s, struct task *t)
1500 struct signal_task *st = t->private_data;
1502 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1505 void signal_post_select(struct sched *s, struct task *t)
1507 struct signal_task *st = t->private_data;
1509 if (!FD_ISSET(st->fd, &s->rfds))
1511 t->ret = -E_SIGNAL_CAUGHT;
1512 st->signum = para_next_signal();
1515 void signal_setup_default(struct signal_task *st)
1517 st->task.pre_select = signal_pre_select;
1518 st->task.post_select = signal_post_select;
1519 st->task.private_data = st;
1521 sprintf(st->task.status, "signal task");
1524 static void command_pre_select(struct sched *s, struct task *t)
1526 struct command_task *ct = t->private_data;
1528 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
1532 static void command_post_select(struct sched *s, struct task *t)
1535 struct command_task *ct = t->private_data;
1537 t->ret = 1; /* always successful */
1538 if (audiod_status != AUDIOD_OFF)
1539 audiod_status_dump();
1540 if (!FD_ISSET(ct->fd, &s->rfds))
1542 ret = handle_connect(ct->fd);
1544 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
1547 static void init_command_task(struct command_task *ct)
1549 ct->task.pre_select = command_pre_select;
1550 ct->task.post_select = command_post_select;
1551 ct->task.event_handler = NULL;
1552 ct->task.private_data = ct;
1554 ct->fd = audiod_get_socket(); /* doesn't return on errors */
1555 sprintf(ct->task.status, "command task");
1558 static void status_pre_select(struct sched *s, struct task *t)
1560 struct status_task *st = t->private_data;
1562 if (st->fd >= 0 && audiod_status == AUDIOD_OFF)
1564 if (st->fd < 0 && audiod_status != AUDIOD_OFF) {
1565 st->fd = open_stat_pipe();
1569 if (st->fd >= 0 && audiod_status != AUDIOD_OFF)
1570 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
1573 static void status_post_select(struct sched *s, struct task *t)
1575 struct status_task *st = t->private_data;
1579 if (st->fd < 0 || !FD_ISSET(st->fd, &s->rfds))
1581 ret = read(st->fd, st->buf + st->loaded,
1582 STRINGSIZE - 1 - st->loaded);
1585 /* avoid busy loop if server is down */
1586 while (sleep(1) > 0) /* FIXME */
1589 st->buf[ret + st->loaded] = '\0';
1590 st->loaded = for_each_line(st->buf, ret + st->loaded,
1595 static void init_status_task(struct status_task *st)
1597 st->task.pre_select = status_pre_select;
1598 st->task.post_select = status_post_select;
1599 st->task.private_data = st;
1604 sprintf(st->task.status, "status task");
1607 static void set_initial_status(void)
1609 audiod_status = AUDIOD_ON;
1610 if (!conf.mode_given)
1612 if (!strcmp(conf.mode_arg, "sb")) {
1613 audiod_status = AUDIOD_STANDBY;
1616 if (!strcmp(conf.mode_arg, "off")) {
1617 audiod_status = AUDIOD_OFF;
1620 if (strcmp(conf.mode_arg, "on"))
1621 PARA_WARNING_LOG("%s", "invalid mode\n");
1624 int main(int argc, char *argv[])
1629 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1630 struct task audiod_task_struct, *audiod_task = &audiod_task_struct;
1635 hostname = para_hostname();
1636 cmdline_parser(argc, argv, &conf);
1637 para_drop_privileges(conf.user_arg, conf.group_arg);
1638 cf = configfile_exists();
1640 if (cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
1641 PARA_EMERG_LOG("%s", "parse error in config file\n");
1645 if (conf.logfile_given)
1646 logfile = open_log(conf.logfile_arg);
1647 log_welcome("para_audiod", conf.loglevel_arg);
1648 i = init_stream_io();
1650 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
1653 server_uptime(UPTIME_SET);
1654 set_initial_status();
1658 setup_signal_handling();
1659 signal_setup_default(sig_task);
1660 sig_task->task.event_handler = signal_event_handler;
1662 init_status_task(stat_task);
1663 init_command_task(cmd_task);
1664 init_audiod_task(audiod_task);
1666 if (conf.daemon_given)
1669 register_task(&sig_task->task);
1670 register_task(&cmd_task->task);
1671 register_task(&stat_task->task);
1672 register_task(audiod_task);
1673 s.default_timeout.tv_sec = 0;
1674 s.default_timeout.tv_usec = 99 * 1000;
1677 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
1678 return EXIT_FAILURE;