2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod.c The paraslash's audio daemon. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
22 #include "audiod.cmdline.h"
26 #include "buffer_tree.h"
29 #include "grab_client.h"
30 #include "client.cmdline.h"
38 #include "write_common.h"
42 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
43 /** define the array of error lists needed by para_audiod */
45 /** define the array containing all supported audio formats */
46 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
48 DEFINE_RECEIVER_ARRAY;
50 /** Defines how audiod handles one supported audio format. */
51 struct audio_format_info {
52 /** pointer to the receiver for this audio format */
53 struct receiver *receiver;
54 /** the receiver configuration */
56 /** the number of filters that should be activated for this audio format */
57 unsigned int num_filters;
58 /** Array of filter numbers to be activated. */
59 unsigned *filter_nums;
60 /** Pointer to the array of filter configurations. */
62 /** the number of filters that should be activated for this audio format */
63 unsigned int num_writers;
64 /** Array of writer numbers to be activated. */
66 /** pointer to the array of writer configurations */
68 /** do not start receiver/filters/writer before this time */
69 struct timeval restart_barrier;
73 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
74 * be associated with a receiver/filter/writer triple. This array holds all
75 * information on the status of these slots.
77 * \sa struct slot_info
79 struct slot_info slot[MAX_STREAM_SLOTS];
81 /** The vss status flags audiod is interested in. */
82 enum vss_status_flags {
83 /** Whether the 'N' flag is set. */
84 VSS_STATUS_FLAG_NEXT = 1,
85 /** The 'P' flag is set. */
86 VSS_STATUS_FLAG_PLAYING = 2,
90 * The scheduler instance of para_audiod.
92 * This is needed also in audiod_command.c (for the tasks command), so it can
95 struct sched sched = {.max_fileno = 0};
97 /* The task for obtaining para_server's status (para_client stat). */
99 /** The associated task structure of audiod. */
101 /** Client data associated with the stat task. */
102 struct client_task *ct;
103 /** Do not restart client command until this time. */
104 struct timeval restart_barrier;
105 /** Last time we received status data from para_server. */
106 struct timeval last_status_read;
108 /** The offset value announced by para_server. */
110 /** The length of the current audio file as announced by para_server. */
112 /** The start of the current stream from the view of para_server. */
113 struct timeval server_stream_start;
114 /** The average time deviation between para_server and para_audiod. */
115 struct timeval sa_time_diff;
116 /** Whether client time is ahead of server time. */
117 int sa_time_diff_sign;
118 /** The 'P' and the 'N' flags as announced by para_server. */
119 enum vss_status_flags vss_status;
120 /** Number of times the clock difference is to be checked. */
121 unsigned clock_diff_count;
122 /** When to start the next check for clock difference. */
123 struct timeval clock_diff_barrier;
124 /** Number of the audio format as announced by para_server. */
125 int current_audio_format_num;
126 /* The status task btrn is the child of the client task. */
127 struct btr_node *btrn;
130 /** The array of status items sent by para_server. */
131 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
134 * the current mode of operation of which can be changed by the on/off/cycle
135 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
137 int audiod_status = AUDIOD_ON;
140 * the gengetopt args_info struct that holds information on all command line
143 struct audiod_args_info conf;
145 static char *socket_name;
146 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
148 static struct signal_task *signal_task;
150 static struct status_task status_task_struct;
152 static uid_t *uid_whitelist;
155 * the task that calls the status command of para_server
157 * \sa struct status_task
159 static struct status_task *stat_task = &status_task_struct;
162 * The task for handling audiod commands.
164 * We need two listening sockets for backward compability: on Linux systems
165 * fd[0] is an abstract socket (more precisely, a socket bound to an address in
166 * the abstract namespace), and fd[1] is the usual pathname socket. On other
167 * systems, fd[0] is negative, and only the pathname socket is used.
169 * For 0.5.x we accept connections on both sockets to make sure that old
170 * para_audioc versions can still connect. New versions use only the abstract
171 * socket. Hence after v0.6.0 we can go back to a single socket, either an
172 * abstract one (Linux) or a pathname socket (all other systems).
174 struct command_task {
175 /** The local listening sockets. */
177 /** the associated task structure */
181 /** iterate over all supported audio formats */
182 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
185 * Get the audio format number.
187 * \param name The name of the audio format.
189 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
190 * \a name is not a supported audio format.
192 static int get_audio_format_num(const char *name)
196 while (para_isspace(*name))
198 FOR_EACH_AUDIO_FORMAT(i)
199 if (!strcmp(name, audio_formats[i]))
201 return -E_UNSUPPORTED_AUDIO_FORMAT;
204 static int get_matching_audio_format_nums(const char *re)
209 ret = para_regcomp(&preg, re, REG_EXTENDED | REG_NOSUB);
213 FOR_EACH_AUDIO_FORMAT(i)
214 if (regexec(&preg, audio_formats[i], 0, NULL, 0) != REG_NOMATCH)
221 * Compute the play time based on information of the given slot.
223 * \param slot_num The slot number (negative means: no slot).
225 * This computes a string of the form "0:07 [3:33] (3%/3:40)" using information
226 * from the status items received from para_server and the start time of the
227 * (first) writer of the given slot.
229 * It has to take into account that the stream was probably not started at
230 * the beginning of the file, that the clock between the server and the client
231 * host may differ and that playback of the stream was delayed, e.g. because
232 * the prebuffer filter is used in the filter configuration of the given slot.
234 * If no writer is active in the given slot, or \a slot_num is negative
235 * (indicating that para_audiod runs in standby mode), an approximation based
236 * only on the status items is computed and the returned string is prefixed
239 * \return A string that must be freed by the caller.
241 char *get_time_string(int slot_num)
243 int ret, seconds = 0, length;
244 struct timeval *tmp, sum, sss, /* server stream start */
245 rstime, /* receiver start time */
246 wstime, /* writer start time */
247 wtime, /* now - writer start */
248 rskip; /* receiver start - sss */
249 struct slot_info *s = slot_num < 0? NULL : &slot[slot_num];
252 if (audiod_status == AUDIOD_OFF)
254 if (!(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)) {
255 if (stat_task->length_seconds) /* paused */
257 goto empty; /* stopped */
259 if (audiod_status == AUDIOD_ON && !s)
262 * Valid status items and playing, set length and tmp to the stream
263 * start. We use the slot info and fall back to the info from current
264 * status items if no slot info is available.
266 length = stat_task->length_seconds;
267 tmp = &stat_task->server_stream_start;
268 if (s && s->wns && s->wns[0].btrn) { /* writer active in this slot */
269 btr_get_node_start(s->wns[0].btrn, &wstime);
270 if (wstime.tv_sec != 0) { /* writer wrote something */
271 if (s->server_stream_start.tv_sec == 0) {
272 /* copy status info to slot */
273 s->server_stream_start = stat_task->server_stream_start;
274 s->offset_seconds = stat_task->offset_seconds;
275 s->seconds_total = stat_task->length_seconds;
277 length = s->seconds_total;
278 tmp = &s->server_stream_start;
281 if (stat_task->sa_time_diff_sign > 0)
282 tv_diff(tmp, &stat_task->sa_time_diff, &sss);
284 tv_add(tmp, &stat_task->sa_time_diff, &sss);
285 if (!s || !s->wns || !s->wns[0].btrn) {
287 tv_diff(now, &sss, &diff);
288 seconds = diff.tv_sec + stat_task->offset_seconds;
291 tv_diff(now, &wstime, &wtime);
292 //PARA_CRIT_LOG("offset %d\n", s->offset_seconds);
293 seconds = s->offset_seconds;
294 if (s->receiver_node->btrn) {
295 btr_get_node_start(s->receiver_node->btrn, &rstime);
296 ret = tv_diff(&rstime, &sss, &rskip);
297 if (ret > 0) { /* audiod was started in the middle of the stream */
298 tv_add(&wtime, &rskip, &sum);
299 seconds += sum.tv_sec;
301 seconds += wtime.tv_sec;
303 seconds += wtime.tv_sec;
305 seconds = PARA_MIN(seconds, length);
306 seconds = PARA_MAX(seconds, 0);
308 "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)",
312 (length - seconds) / 60,
313 (length - seconds) % 60,
314 length? (seconds * 100 + length / 2) / length : 0,
318 //PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
321 return para_strdup(NULL);
324 static void parse_config_or_die(void)
328 struct audiod_cmdline_parser_params params = {
332 .check_ambiguity = 0,
336 if (conf.config_file_given)
337 config_file = para_strdup(conf.config_file_arg);
339 char *home = para_homedir();
340 config_file = make_message("%s/.paraslash/audiod.conf", home);
343 ret = file_exists(config_file);
344 if (conf.config_file_given && !ret) {
345 PARA_EMERG_LOG("can not read config file %s\n", config_file);
350 audiod_cmdline_parser_config_file(config_file, &conf, ¶ms);
351 daemon_set_loglevel(conf.loglevel_arg);
354 if (conf.user_allow_given > 0) {
355 uid_whitelist = para_malloc(conf.user_allow_given
357 for (i = 0; i < conf.user_allow_given; i++) {
360 ret = para_atoi32(conf.user_allow_arg[i], &val);
362 uid_whitelist[i] = val;
365 errno = 0; /* see getpwnam(3) */
366 pw = getpwnam(conf.user_allow_arg[i]);
368 PARA_EMERG_LOG("invalid username: %s\n",
369 conf.user_allow_arg[i]);
372 uid_whitelist[i] = pw->pw_uid;
380 static void setup_signal_handling(void)
382 signal_task = signal_init_or_die();
383 para_install_sighandler(SIGINT);
384 para_install_sighandler(SIGTERM);
385 para_install_sighandler(SIGHUP);
386 para_sigaction(SIGPIPE, SIG_IGN);
389 static void clear_slot(int slot_num)
391 struct slot_info *s = &slot[slot_num];
393 PARA_INFO_LOG("clearing slot %d\n", slot_num);
394 memset(s, 0, sizeof(struct slot_info));
398 static void close_receiver(int slot_num)
400 struct slot_info *s = &slot[slot_num];
401 struct audio_format_info *a;
403 if (s->format < 0 || !s->receiver_node)
406 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
407 audio_formats[s->format], slot_num);
408 a->receiver->close(s->receiver_node);
409 btr_remove_node(&s->receiver_node->btrn);
410 task_reap(&s->receiver_node->task);
411 free(s->receiver_node);
412 s->receiver_node = NULL;
413 stat_task->current_audio_format_num = -1;
414 tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
415 &a->restart_barrier);
418 static void writer_cleanup(struct writer_node *wn)
424 w = writers + wn->writer_num;
425 PARA_INFO_LOG("closing %s\n", writer_names[wn->writer_num]);
427 btr_remove_node(&wn->btrn);
428 task_reap(&wn->task);
431 static void close_writers(struct slot_info *s)
433 struct audio_format_info *a;
440 if (a->num_writers == 0)
441 writer_cleanup(s->wns);
443 for (i = 0; i < a->num_writers; i++)
444 writer_cleanup(s->wns + i);
450 static void close_filters(struct slot_info *s)
453 struct audio_format_info *a = afi + s->format;
454 if (a->num_filters == 0)
456 for (i = a->num_filters - 1; i >= 0; i--) {
457 struct filter_node *fn = s->fns + i;
462 f = filters + fn->filter_num;
465 btr_remove_node(&fn->btrn);
466 task_reap(&fn->task);
472 static void notify_receivers(int error)
477 struct slot_info *s = slot + i;
480 if (!s->receiver_node)
482 task_notify(s->receiver_node->task, error);
486 static int get_empty_slot(void)
497 if (s->wns || s->receiver_node || s->fns)
502 return -E_NO_MORE_SLOTS;
505 static void open_filters(struct slot_info *s)
507 struct audio_format_info *a = afi + s->format;
508 struct filter_node *fn;
509 int nf = a->num_filters;
510 struct btr_node *parent;
515 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
516 assert(s->fns == NULL);
517 s->fns = para_calloc(nf * sizeof(struct filter_node));
518 parent = s->receiver_node->btrn;
519 for (i = 0; i < nf; i++) {
521 struct filter *f = filters + a->filter_nums[i];
523 fn->filter_num = a->filter_nums[i];
524 fn->conf = a->filter_conf[i];
525 fn->btrn = btr_new_node(&(struct btr_node_description)
526 EMBRACE(.name = f->name, .parent = parent,
527 .handler = f->execute, .context = fn));
530 sprintf(buf, "%s (slot %d)", f->name, (int)(s - slot));
531 fn->task = task_register(&(struct task_info) {
533 .pre_select = f->pre_select,
534 .post_select = f->post_select,
538 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
539 audio_formats[s->format], i, nf, f->name, (int)(s - slot));
543 static void open_writers(struct slot_info *s)
546 struct audio_format_info *a = afi + s->format;
547 struct writer_node *wn;
548 struct btr_node *parent = s->fns[a->num_filters - 1].btrn;
550 assert(s->wns == NULL);
551 s->wns = para_calloc(PARA_MAX(1U, a->num_writers)
552 * sizeof(struct writer_node));
553 for (i = 0; i < a->num_writers; i++) {
555 wn->conf = a->writer_conf[i];
556 wn->writer_num = a->writer_nums[i];
557 register_writer_node(wn, parent, &sched);
558 PARA_NOTICE_LOG("%s writer started in slot %d\n",
559 writer_names[a->writer_nums[i]], (int)(s - slot));
563 /* returns slot num on success */
564 static int open_receiver(int format)
566 struct audio_format_info *a = &afi[format];
569 struct receiver *r = a->receiver;
570 struct receiver_node *rn;
572 tv_add(now, &(struct timeval)EMBRACE(2, 0), &a->restart_barrier);
573 ret = get_empty_slot();
577 rn = para_calloc(sizeof(*rn));
579 rn->conf = a->receiver_conf;
580 rn->btrn = btr_new_node(&(struct btr_node_description)
581 EMBRACE(.name = r->name, .context = rn));
584 btr_remove_node(&rn->btrn);
590 s->receiver_node = rn;
591 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
592 audio_formats[format], r->name, slot_num);
593 rn->task = task_register(&(struct task_info) {
595 .pre_select = r->pre_select,
596 .post_select = r->post_select,
602 static bool receiver_running(void)
605 long unsigned ss1 = stat_task->server_stream_start.tv_sec;
608 struct slot_info *s = &slot[i];
609 long unsigned ss2 = s->server_stream_start.tv_sec;
611 if (!s->receiver_node)
613 if (task_status(s->receiver_node->task) >= 0)
622 * Return the root node of the current buffer tree.
624 * This is only used for stream grabbing.
626 * \return \p NULL if no slot is currently active. If more than one buffer tree
627 * exists, the node corresponding to the most recently started receiver is
630 struct btr_node *audiod_get_btr_root(void)
632 int i, newest_slot = -1;
633 struct timeval newest_rstime = {0, 0};
636 struct slot_info *s = &slot[i];
637 struct timeval rstime;
638 if (!s->receiver_node)
640 if (task_status(s->receiver_node->task) < 0)
642 btr_get_node_start(s->receiver_node->btrn, &rstime);
643 if (newest_slot >= 0 && tv_diff(&rstime, &newest_rstime, NULL) < 0)
645 newest_rstime = rstime;
648 if (newest_slot == -1)
650 return slot[newest_slot].receiver_node->btrn;
653 /* whether a new instance of a decoder should be started. */
654 static bool must_start_decoder(void)
656 int cafn = stat_task->current_audio_format_num;
657 unsigned vs = stat_task->vss_status;
659 if (audiod_status != AUDIOD_ON)
665 if (vs & VSS_STATUS_FLAG_NEXT)
667 if (!(vs & VSS_STATUS_FLAG_PLAYING))
669 if (receiver_running())
671 if (tv_diff(now, &afi[cafn].restart_barrier, NULL) < 0)
676 static void compute_time_diff(const struct timeval *status_time)
678 struct timeval tmp, diff;
679 static unsigned count;
680 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
681 const struct timeval max_deviation = {0, 500 * 1000};
682 const int time_smooth = 5;
684 sign = tv_diff(status_time, now, &diff);
685 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
686 // sign, sa_time_diff_sign);
688 sa_time_diff_sign = sign;
689 stat_task->sa_time_diff = diff;
694 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
695 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
696 PARA_WARNING_LOG("time diff jump: %lims\n",
700 sa_time_diff_sign = tv_convex_combination(
701 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
702 count > 10? sign : sign * time_smooth, &diff,
704 stat_task->sa_time_diff = tmp;
705 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
708 sa_time_diff_sign < 0? "-" : "+",
709 tv2ms(&stat_task->sa_time_diff)
712 stat_task->sa_time_diff_sign = sa_time_diff_sign;
715 static int update_item(int itemnum, char *buf)
717 long unsigned sec, usec;
719 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
721 free(stat_item_values[itemnum]);
722 stat_item_values[itemnum] = para_strdup(buf);
723 stat_client_write_item(itemnum);
725 case SI_STATUS_FLAGS:
726 stat_task->vss_status = 0;
727 if (strchr(buf, 'N'))
728 stat_task->vss_status |= VSS_STATUS_FLAG_NEXT;
729 if (strchr(buf, 'P'))
730 stat_task->vss_status |= VSS_STATUS_FLAG_PLAYING;
733 stat_task->offset_seconds = atoi(buf);
735 case SI_SECONDS_TOTAL:
736 stat_task->length_seconds = atoi(buf);
738 case SI_STREAM_START:
739 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
740 stat_task->server_stream_start.tv_sec = sec;
741 stat_task->server_stream_start.tv_usec = usec;
744 case SI_CURRENT_TIME:
745 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
746 struct timeval tv = {sec, usec};
747 compute_time_diff(&tv);
751 stat_task->current_audio_format_num
752 = get_audio_format_num(buf);
757 static int parse_stream_command(const char *txt, char **cmd)
760 char *re, *p = strchr(txt, ':');
763 return -E_MISSING_COLON;
766 re = malloc(len + 1);
767 strncpy(re, txt, len);
769 ret = get_matching_audio_format_nums(re);
774 static int add_filter(int format, char *cmdline)
776 struct audio_format_info *a = &afi[format];
777 int filter_num, nf = a->num_filters;
780 filter_num = check_filter_arg(cmdline, &cfg);
783 a->filter_conf = para_realloc(a->filter_conf,
784 (nf + 1) * sizeof(void *));
785 a->filter_nums = para_realloc(a->filter_nums,
786 (nf + 1) * sizeof(unsigned));
787 a->filter_nums[nf] = filter_num;
788 a->filter_conf[nf] = cfg;
790 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
791 filters[filter_num].name);
795 static int parse_writer_args(void)
799 struct audio_format_info *a;
801 for (i = 0; i < conf.writer_given; i++) {
803 int j, nw, writer_num, af_mask;
805 ret = parse_stream_command(conf.writer_arg[i], &cmd);
809 FOR_EACH_AUDIO_FORMAT(j) {
811 if ((af_mask & (1 << j)) == 0) /* no match */
813 wconf = check_writer_arg_or_die(cmd, &writer_num);
815 a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int));
816 a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *));
817 a->writer_nums[nw] = writer_num;
818 a->writer_conf[nw] = wconf;
819 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[j],
820 nw, writer_names[writer_num]);
824 /* Use default writer for audio formats which are not yet set up. */
825 FOR_EACH_AUDIO_FORMAT(i) {
829 if (a->num_writers > 0)
830 continue; /* already set up */
831 writer_conf = check_writer_arg_or_die(NULL, &writer_num);
832 a->writer_nums = para_malloc(sizeof(int));
833 a->writer_nums[0] = writer_num;
834 a->writer_conf = para_malloc(sizeof(void *));
835 a->writer_conf[0] = writer_conf;
837 PARA_INFO_LOG("%s writer: %s (default)\n", audio_formats[i],
838 writer_names[writer_num]);
843 static int parse_receiver_args(void)
845 int i, ret, receiver_num;
847 struct audio_format_info *a;
849 for (i = conf.receiver_given - 1; i >= 0; i--) {
853 ret = parse_stream_command(conf.receiver_arg[i], &arg);
857 FOR_EACH_AUDIO_FORMAT(j) {
859 if ((af_mask & (1 << j)) == 0) /* no match */
862 * If multiple receivers are given for this audio format, the
863 * last one wins and we have to free the previous receiver
864 * config here. Since we are iterating backwards, the winning
865 * receiver arg is in fact the first one given.
867 if (a->receiver_conf)
868 a->receiver->free_config(a->receiver_conf);
869 a->receiver_conf = check_receiver_arg(arg, &receiver_num);
870 ret = -E_RECV_SYNTAX;
871 if (!a->receiver_conf)
873 a->receiver = receivers + receiver_num;
877 * Use the first available receiver with no arguments for those audio
878 * formats for which no receiver was specified.
880 cmd = para_strdup(receivers[0].name);
881 FOR_EACH_AUDIO_FORMAT(i) {
883 if (a->receiver_conf)
885 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
886 if (!a->receiver_conf)
887 return -E_RECV_SYNTAX;
888 a->receiver = &receivers[receiver_num];
890 FOR_EACH_AUDIO_FORMAT(i) {
892 PARA_INFO_LOG("receiving %s streams via %s receiver\n",
893 audio_formats[i], a->receiver->name);
901 static int init_default_filters(void)
905 FOR_EACH_AUDIO_FORMAT(i) {
906 struct audio_format_info *a = &afi[i];
911 continue; /* no default -- nothing to to */
913 * udp and dccp streams are fec-encoded, so add fecdec as the
916 if (strcmp(afi[i].receiver->name, "udp") == 0 ||
917 strcmp(afi[i].receiver->name, "dccp") == 0) {
918 tmp = para_strdup("fecdec");
924 /* add "dec" to audio format name */
925 tmp = make_message("%sdec", audio_formats[i]);
926 for (j = 0; filters[j].name; j++)
927 if (!strcmp(tmp, filters[j].name))
930 ret = -E_UNSUPPORTED_FILTER;
931 if (!filters[j].name)
933 tmp = para_strdup(filters[j].name);
934 ret = add_filter(i, tmp);
938 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
945 static int parse_filter_args(void)
947 int i, j, ret, af_mask, num_matches;
949 for (i = 0; i < conf.filter_given; i++) {
951 ret = parse_stream_command(conf.filter_arg[i], &arg);
956 FOR_EACH_AUDIO_FORMAT(j) {
957 if ((af_mask & (1 << j)) == 0) /* no match */
959 ret = add_filter(j, arg);
964 if (num_matches == 0)
965 PARA_WARNING_LOG("ignoring filter spec: %s\n",
968 ret = init_default_filters(); /* use default values for the rest */
973 static int parse_stream_args(void)
977 ret = parse_receiver_args();
980 ret = parse_filter_args();
983 ret = parse_writer_args();
989 /* does not unlink socket on errors */
990 static void init_local_sockets(struct command_task *ct)
992 if (conf.socket_given)
993 socket_name = para_strdup(conf.socket_arg);
995 char *hn = para_hostname();
996 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1000 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
1001 if (conf.force_given)
1002 unlink(socket_name);
1003 ct->fd[0] = create_local_socket(socket_name, 0);
1004 ct->fd[1] = create_local_socket(socket_name,
1005 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1006 if (ct->fd[0] >= 0 || ct->fd[1] >= 0)
1008 PARA_EMERG_LOG("%s\n", para_strerror(-ct->fd[1]));
1012 static int signal_post_select(struct sched *s, void *context)
1014 struct signal_task *st = context;
1017 ret = task_get_notification(st->task);
1020 signum = para_next_signal(&s->rfds);
1025 PARA_NOTICE_LOG("received signal %d\n", signum);
1026 task_notify_all(s, E_AUDIOD_SIGNAL);
1027 return -E_AUDIOD_SIGNAL;
1032 static void command_pre_select(struct sched *s, void *context)
1034 struct command_task *ct = context;
1037 for (i = 0; i < 2; i++)
1039 para_fd_set(ct->fd[i], &s->rfds, &s->max_fileno);
1042 static int command_post_select(struct sched *s, void *context)
1045 struct command_task *ct = context;
1046 static struct timeval last_status_dump;
1047 struct timeval tmp, delay;
1050 ret = task_get_notification(ct->task);
1053 for (i = 0; i < 2; i++) {
1056 ret = handle_connect(ct->fd[i], &s->rfds, uid_whitelist);
1058 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1059 if (ret == -E_AUDIOD_TERM) {
1060 task_notify_all(s, -ret);
1069 /* if last status dump was less than 500ms ago, do nothing */
1071 delay.tv_usec = 500 * 1000;
1072 tv_add(&last_status_dump, &delay, &tmp);
1073 if (tv_diff(now, &tmp, NULL) < 0)
1077 * If last status dump was more than 5s ago, force update. Otherwise,
1078 * update only those items that have changed.
1082 tv_add(&last_status_dump, &delay, &tmp);
1083 if (tv_diff(now, &tmp, NULL) > 0)
1086 audiod_status_dump(force);
1087 last_status_dump = *now;
1091 static void init_command_task(struct command_task *ct)
1093 init_local_sockets(ct); /* doesn't return on errors */
1095 ct->task = task_register(&(struct task_info) {
1097 .pre_select = command_pre_select,
1098 .post_select = command_post_select,
1103 static void close_stat_pipe(void)
1107 task_reap(&stat_task->ct->task);
1108 client_close(stat_task->ct);
1109 stat_task->ct = NULL;
1110 clear_and_dump_items();
1111 stat_task->length_seconds = 0;
1112 stat_task->offset_seconds = 0;
1113 stat_task->vss_status = 0;
1114 stat_task->current_audio_format_num = -1;
1115 audiod_status_dump(true);
1118 /* avoid busy loop if server is down */
1119 static void set_stat_task_restart_barrier(unsigned seconds)
1121 struct timeval delay = {seconds, 0};
1122 tv_add(now, &delay, &stat_task->restart_barrier);
1125 static bool must_close_slot(int slot_num)
1127 struct slot_info *s = &slot[slot_num];
1128 struct audio_format_info *a = afi + s->format;
1133 if (s->receiver_node && task_status(s->receiver_node->task) >= 0)
1135 for (i = 0; i < a->num_filters; i++)
1136 if (s->fns && task_status(s->fns[i].task) >= 0)
1138 if (a->num_writers > 0) {
1139 for (i = 0; i < a->num_writers; i++)
1140 if (s->wns && task_status(s->wns[i].task) >= 0)
1143 if (s->wns && task_status(s->wns[0].task) >= 0)
1149 static void close_slot(int slot_num)
1151 struct slot_info *s = slot + slot_num;
1153 PARA_INFO_LOG("closing slot %d\n", slot_num);
1156 close_receiver(slot_num);
1157 clear_slot(slot_num);
1160 static void close_unused_slots(void)
1165 if (must_close_slot(i))
1170 * Cleanup all resources.
1172 * This performs various cleanups, removes the audiod socket and closes the
1173 * connection to para_server.
1175 static void audiod_cleanup(void)
1178 unlink(socket_name);
1180 close_unused_slots();
1181 audiod_cmdline_parser_free(&conf);
1182 close_stat_clients();
1183 free(uid_whitelist);
1187 * Check if any receivers/filters/writers need to be started and do so if
1190 static void start_stop_decoders(void)
1193 struct slot_info *sl;
1195 close_unused_slots();
1196 if (audiod_status != AUDIOD_ON ||
1197 !(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING))
1198 return notify_receivers(E_NOT_PLAYING);
1199 if (!must_start_decoder())
1201 ret = open_receiver(stat_task->current_audio_format_num);
1203 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1209 activate_grab_clients(&sched);
1210 btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
1213 static void status_pre_select(struct sched *s, void *context)
1215 struct status_task *st = context;
1216 int i, ret, cafn = stat_task->current_audio_format_num;
1218 if (must_start_decoder())
1221 if (must_close_slot(i))
1223 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1226 if (st->ct && audiod_status == AUDIOD_OFF)
1228 if (!st->ct && audiod_status != AUDIOD_OFF)
1229 sched_request_barrier_or_min_delay(&st->restart_barrier, s);
1231 sched_request_barrier(&afi[cafn].restart_barrier, s);
1233 * If para_server is playing we'd like to have a smooth time display
1234 * even if we are running in standby mode. So we request a timeout that
1235 * expires at the next full second.
1237 if (stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)
1238 sched_request_timeout_ms(1000 - now->tv_usec / 1000, s);
1244 /* restart the client task if necessary */
1245 static int status_post_select(struct sched *s, void *context)
1247 struct status_task *st = context;
1250 ret = task_get_notification(st->task);
1253 if (audiod_status == AUDIOD_OFF) {
1256 if (task_status(st->ct->task) >= 0) {
1257 task_notify(st->ct->task, E_AUDIOD_OFF);
1261 st->clock_diff_count = conf.clock_diff_count_arg;
1268 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1273 if (st->ct->status != CL_EXECUTING)
1276 struct timeval diff;
1277 tv_diff(now, &st->last_status_read, &diff);
1278 if (diff.tv_sec > 61)
1279 task_notify(st->ct->task, E_STATUS_TIMEOUT);
1282 btr_merge(st->btrn, st->min_iqs);
1283 sz = btr_next_buffer(st->btrn, &buf);
1284 ret = for_each_stat_item(buf, sz, update_item);
1286 task_notify(st->ct->task, -ret);
1290 btr_consume(st->btrn, sz - ret);
1291 st->last_status_read = *now;
1293 } else /* current status item crosses buffers */
1294 st->min_iqs = sz + 1;
1297 btr_drain(st->btrn);
1298 st->current_audio_format_num = -1;
1299 if (tv_diff(now, &st->restart_barrier, NULL) < 0)
1301 if (st->clock_diff_count) { /* get status only one time */
1302 char *argv[] = {"audiod", "--", "stat", "-p", "-n=1", NULL};
1304 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1305 st->clock_diff_count--;
1306 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1307 set_stat_task_restart_barrier(2);
1310 char *argv[] = {"audiod", "--", "stat", "-p", NULL};
1312 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1313 set_stat_task_restart_barrier(5);
1315 free(stat_item_values[SI_BASENAME]);
1316 stat_item_values[SI_BASENAME] = para_strdup(
1317 "no connection to para_server");
1318 stat_client_write_item(SI_BASENAME);
1319 st->last_status_read = *now;
1321 start_stop_decoders();
1325 static void init_status_task(struct status_task *st)
1327 memset(st, 0, sizeof(struct status_task));
1328 st->sa_time_diff_sign = 1;
1329 st->clock_diff_count = conf.clock_diff_count_arg;
1330 st->current_audio_format_num = -1;
1331 st->btrn = btr_new_node(&(struct btr_node_description)
1332 EMBRACE(.name = "stat"));
1334 stat_task->task = task_register(&(struct task_info) {
1336 .pre_select = status_pre_select,
1337 .post_select = status_post_select,
1338 .context = stat_task,
1342 static void set_initial_status(void)
1344 audiod_status = AUDIOD_ON;
1345 if (!conf.mode_given)
1347 if (!strcmp(conf.mode_arg, "sb")) {
1348 audiod_status = AUDIOD_STANDBY;
1351 if (!strcmp(conf.mode_arg, "off")) {
1352 audiod_status = AUDIOD_OFF;
1355 if (strcmp(conf.mode_arg, "on"))
1356 PARA_WARNING_LOG("invalid mode\n");
1359 __noreturn static void print_help_and_die(void)
1361 struct ggo_help h = DEFINE_GGO_HELP(audiod);
1362 bool d = conf.detailed_help_given;
1365 flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
1366 ggo_print_help(&h, flags);
1368 flags = d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS;
1369 print_receiver_helps(flags);
1370 print_filter_helps(flags);
1371 print_writer_helps(flags);
1376 * the main function of para_audiod
1378 * \param argc usual argument count
1379 * \param argv usual argument vector
1381 * \return EXIT_SUCCESS or EXIT_FAILURE
1383 * \sa para_audiod(1)
1385 int main(int argc, char *argv[])
1388 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1389 struct audiod_cmdline_parser_params params = {
1392 .check_required = 0,
1393 .check_ambiguity = 0,
1398 audiod_cmdline_parser_ext(argc, argv, &conf, ¶ms);
1399 daemon_set_loglevel(conf.loglevel_arg);
1400 version_handle_flag("audiod", conf.version_given);
1401 /* init receivers/filters/writers early to make help work */
1405 if (conf.help_given || conf.detailed_help_given)
1406 print_help_and_die();
1407 daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
1408 parse_config_or_die();
1409 daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no,
1410 conf.logfile_given, conf.log_color_arg, conf.log_color_given);
1411 init_random_seed_or_die();
1412 daemon_set_flag(DF_LOG_TIME);
1413 daemon_set_flag(DF_LOG_HOSTNAME);
1414 daemon_set_flag(DF_LOG_LL);
1415 if (conf.log_timing_given)
1416 daemon_set_flag(DF_LOG_TIMING);
1417 if (conf.logfile_given) {
1418 daemon_set_logfile(conf.logfile_arg);
1419 daemon_open_log_or_die();
1421 ret = parse_stream_args();
1423 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1426 daemon_log_welcome("para_audiod");
1427 daemon_set_start_time();
1428 set_initial_status();
1431 setup_signal_handling();
1433 init_status_task(stat_task);
1434 init_command_task(cmd_task);
1436 if (conf.daemon_given)
1437 daemonize(false /* parent exits immediately */);
1439 signal_task->task = task_register(&(struct task_info) {
1441 .pre_select = signal_pre_select,
1442 .post_select = signal_post_select,
1443 .context = signal_task,
1446 sched.default_timeout.tv_sec = 2;
1447 sched.default_timeout.tv_usec = 999 * 1000;
1448 ret = schedule(&sched);
1450 sched_shutdown(&sched);
1451 signal_shutdown(signal_task);
1454 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1455 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;