Merge branch 't/lib_arg_with'
[paraslash.git] / audiod.c
1 /*
2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file audiod.c The paraslash's audio daemon. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16 #include <signal.h>
17
18 #include "para.h"
19 #include "error.h"
20 #include "crypt.h"
21 #include "audiod.cmdline.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "ggo.h"
25 #include "buffer_tree.h"
26 #include "recv.h"
27 #include "filter.h"
28 #include "grab_client.h"
29 #include "client.cmdline.h"
30 #include "client.h"
31 #include "audiod.h"
32 #include "net.h"
33 #include "daemon.h"
34 #include "string.h"
35 #include "fd.h"
36 #include "write.h"
37 #include "write_common.h"
38 #include "signal.h"
39 #include "version.h"
40
41 __printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log;
42 /** define the array of error lists needed by para_audiod */
43 INIT_AUDIOD_ERRLISTS;
44 /** define the array containing all supported audio formats */
45 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
46
47 DEFINE_RECEIVER_ARRAY;
48
49 /** Defines how audiod handles one supported audio format. */
50 struct audio_format_info {
51 /** pointer to the receiver for this audio format */
52 struct receiver *receiver;
53 /** the receiver configuration */
54 void *receiver_conf;
55 /** the number of filters that should be activated for this audio format */
56 unsigned int num_filters;
57 /** Array of filter numbers to be activated. */
58 unsigned *filter_nums;
59 /** Pointer to the array of filter configurations. */
60 void **filter_conf;
61 /** the number of filters that should be activated for this audio format */
62 unsigned int num_writers;
63 /** Array of writer numbers to be activated. */
64 int *writer_nums;
65 /** pointer to the array of writer configurations */
66 void **writer_conf;
67 /** do not start receiver/filters/writer before this time */
68 struct timeval restart_barrier;
69 };
70
71 /* Describes one instance of a receiver-filter-writer chain. */
72 struct slot_info {
73 /* Number of the audio format in this slot. */
74 int format;
75 /* The stream_start status item announced by para_server. */
76 struct timeval server_stream_start;
77 /* The offset status item announced by para_server. */
78 unsigned offset_seconds;
79 /* The seconds_total status item announced by para_server. */
80 unsigned seconds_total;
81 /* The receiver info associated with this slot. */
82 struct receiver_node *receiver_node;
83 /* The array of filter nodes. */
84 struct filter_node *fns;
85 /* The array of writers attached to the last filter. */
86 struct writer_node *wns;
87 };
88
89 /** Maximal number of simultaneous instances. */
90 #define MAX_STREAM_SLOTS 5
91
92 /** Iterate over all slots. */
93 #define FOR_EACH_SLOT(_slot) for (_slot = 0; _slot < MAX_STREAM_SLOTS; _slot++)
94
95 /**
96 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
97 * be associated with a receiver/filter/writer triple. This array holds all
98 * information on the status of these slots.
99 *
100 * \sa struct slot_info
101 * */
102 struct slot_info slot[MAX_STREAM_SLOTS];
103
104 /** The vss status flags audiod is interested in. */
105 enum vss_status_flags {
106 /** Whether the 'N' flag is set. */
107 VSS_STATUS_FLAG_NEXT = 1,
108 /** The 'P' flag is set. */
109 VSS_STATUS_FLAG_PLAYING = 2,
110 };
111
112 /**
113 * The scheduler instance of para_audiod.
114 *
115 * This is needed also in audiod_command.c (for the tasks command), so it can
116 * not be made static.
117 */
118 struct sched sched = {.max_fileno = 0};
119
120 /* The task for obtaining para_server's status (para_client stat). */
121 struct status_task {
122 /** The associated task structure of audiod. */
123 struct task *task;
124 /** Client data associated with the stat task. */
125 struct client_task *ct;
126 /** Do not restart client command until this time. */
127 struct timeval restart_barrier;
128 /** Last time we received status data from para_server. */
129 struct timeval last_status_read;
130 size_t min_iqs;
131 /** The offset value announced by para_server. */
132 int offset_seconds;
133 /** The length of the current audio file as announced by para_server. */
134 int length_seconds;
135 /** The start of the current stream from the view of para_server. */
136 struct timeval server_stream_start;
137 /** The average time deviation between para_server and para_audiod. */
138 struct timeval sa_time_diff;
139 /** Whether client time is ahead of server time. */
140 int sa_time_diff_sign;
141 /** The 'P' and the 'N' flags as announced by para_server. */
142 enum vss_status_flags vss_status;
143 /** Number of times the clock difference is to be checked. */
144 unsigned clock_diff_count;
145 /** When to start the next check for clock difference. */
146 struct timeval clock_diff_barrier;
147 /** Number of the audio format as announced by para_server. */
148 int current_audio_format_num;
149 /* The status task btrn is the child of the client task. */
150 struct btr_node *btrn;
151 };
152
153 /** The array of status items sent by para_server. */
154 char *stat_item_values[NUM_STAT_ITEMS] = {NULL};
155
156 /**
157 * the current mode of operation of which can be changed by the on/off/cycle
158 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
159 */
160 int audiod_status = AUDIOD_ON;
161
162 /**
163 * the gengetopt args_info struct that holds information on all command line
164 * arguments
165 */
166 struct audiod_args_info conf;
167
168 static char *socket_name;
169 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
170
171 static struct signal_task *signal_task;
172
173 static struct status_task status_task_struct;
174
175 /**
176 * the task that calls the status command of para_server
177 *
178 * \sa struct status_task
179 */
180 static struct status_task *stat_task = &status_task_struct;
181
182 /*
183 * The task for handling audiod commands.
184 *
185 * We need two listening sockets for backward compability: on Linux systems
186 * fd[0] is an abstract socket (more precisely, a socket bound to an address in
187 * the abstract namespace), and fd[1] is the usual pathname socket. On other
188 * systems, fd[0] is negative, and only the pathname socket is used.
189 *
190 * For 0.5.x we accept connections on both sockets to make sure that old
191 * para_audioc versions can still connect. New versions use only the abstract
192 * socket. Hence after v0.6.0 we can go back to a single socket, either an
193 * abstract one (Linux) or a pathname socket (all other systems).
194 */
195 struct command_task {
196 /** The local listening sockets. */
197 int fd[2];
198 /** the associated task structure */
199 struct task *task;
200 };
201
202 /** iterate over all supported audio formats */
203 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
204
205 /**
206 * Get the audio format number.
207 *
208 * \param name The name of the audio format.
209 *
210 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
211 * \a name is not a supported audio format.
212 */
213 static int get_audio_format_num(const char *name)
214 {
215 int i;
216
217 while (para_isspace(*name))
218 name++;
219 FOR_EACH_AUDIO_FORMAT(i)
220 if (!strcmp(name, audio_formats[i]))
221 return i;
222 return -E_UNSUPPORTED_AUDIO_FORMAT;
223 }
224
225 /**
226 * Return the flags for the \a decoder_flags status item.
227 *
228 * Allocates a string which contains one octal digit per slot. Bit zero (value
229 * 1) is set if a receiver is active. Bit one (value 2) and bit three (value 4)
230 * have the analogous meaning for filter and writer, respectively.
231 *
232 * \return String that must be freed by the caller.
233 */
234 __malloc char *audiod_get_decoder_flags(void)
235 {
236 int i;
237 char flags[MAX_STREAM_SLOTS + 1];
238
239 FOR_EACH_SLOT(i) {
240 struct slot_info *s = &slot[i];
241 char flag = '0';
242 if (s->receiver_node)
243 flag += 1;
244 if (s->fns)
245 flag += 2;
246 if (s->wns)
247 flag += 4;
248 flags[i] = flag;
249 }
250 flags[MAX_STREAM_SLOTS] = '\0';
251 return para_strdup(flags);
252 }
253
254 static int get_matching_audio_format_nums(const char *re)
255 {
256 int i, ret;
257 regex_t preg;
258
259 ret = para_regcomp(&preg, re, REG_EXTENDED | REG_NOSUB);
260 if (ret < 0)
261 return ret;
262 ret = 0;
263 FOR_EACH_AUDIO_FORMAT(i)
264 if (regexec(&preg, audio_formats[i], 0, NULL, 0) != REG_NOMATCH)
265 ret |= (1 << i);
266 regfree(&preg);
267 return ret;
268 }
269
270 static int get_play_time_slot_num(void)
271 {
272 int i, oldest_slot = -1;
273 struct timeval oldest_wstime = {0, 0};
274
275 FOR_EACH_SLOT(i) {
276 struct slot_info *s = &slot[i];
277 struct timeval wstime;
278 if (!s->wns || !s->wns[0].btrn)
279 continue;
280 btr_get_node_start(s->wns[0].btrn, &wstime);
281 if (oldest_slot >= 0 && tv_diff(&wstime, &oldest_wstime, NULL) > 0)
282 continue;
283 oldest_wstime = wstime;
284 oldest_slot = i;
285 }
286 return oldest_slot;
287 }
288
289 /**
290 * Compute the play time based on information of the current slot.
291 *
292 * This computes a string of the form "0:07 [3:33] (3%/3:40)" using information
293 * from the status items received from para_server and the start time of the
294 * (first) writer of the current slot.
295 *
296 * It has to take into account that the stream was probably not started at
297 * the beginning of the file, that the clock between the server and the client
298 * host may differ and that playback of the stream was delayed, e.g. because
299 * the prebuffer filter is used in the filter configuration.
300 *
301 * If no writer is active, for example because para_audiod runs in standby
302 * mode, an approximation based only on the status items is computed and the
303 * returned string is prefixed with "~".
304 *
305 * \return A string that must be freed by the caller.
306 */
307 char *get_time_string(void)
308 {
309 int ret, seconds = 0, length;
310 struct timeval *tmp, sum, sss, /* server stream start */
311 rstime, /* receiver start time */
312 wstime, /* writer start time */
313 wtime, /* now - writer start */
314 rskip; /* receiver start - sss */
315 int slot_num = get_play_time_slot_num();
316 struct slot_info *s = slot_num < 0? NULL : &slot[slot_num];
317 char *msg;
318
319 if (audiod_status == AUDIOD_OFF)
320 goto empty;
321 if (!(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)) {
322 if (stat_task->length_seconds) /* paused */
323 return NULL;
324 goto empty; /* stopped */
325 }
326 if (audiod_status == AUDIOD_ON && !s)
327 goto empty;
328 /*
329 * Valid status items and playing, set length and tmp to the stream
330 * start. We use the slot info and fall back to the info from current
331 * status items if no slot info is available.
332 */
333 length = stat_task->length_seconds;
334 tmp = &stat_task->server_stream_start;
335 if (s && s->wns && s->wns[0].btrn) { /* writer active in this slot */
336 btr_get_node_start(s->wns[0].btrn, &wstime);
337 if (wstime.tv_sec != 0) { /* writer wrote something */
338 if (s->server_stream_start.tv_sec == 0) {
339 /* copy status info to slot */
340 s->server_stream_start = stat_task->server_stream_start;
341 s->offset_seconds = stat_task->offset_seconds;
342 s->seconds_total = stat_task->length_seconds;
343 }
344 length = s->seconds_total;
345 tmp = &s->server_stream_start;
346 }
347 }
348 if (stat_task->sa_time_diff_sign > 0)
349 tv_diff(tmp, &stat_task->sa_time_diff, &sss);
350 else
351 tv_add(tmp, &stat_task->sa_time_diff, &sss);
352 if (!s || !s->wns || !s->wns[0].btrn) {
353 struct timeval diff;
354 tv_diff(now, &sss, &diff);
355 seconds = diff.tv_sec + stat_task->offset_seconds;
356 goto out;
357 }
358 tv_diff(now, &wstime, &wtime);
359 //PARA_CRIT_LOG("offset %d\n", s->offset_seconds);
360 seconds = s->offset_seconds;
361 if (s->receiver_node->btrn) {
362 btr_get_node_start(s->receiver_node->btrn, &rstime);
363 ret = tv_diff(&rstime, &sss, &rskip);
364 if (ret > 0) { /* audiod was started in the middle of the stream */
365 tv_add(&wtime, &rskip, &sum);
366 seconds += sum.tv_sec;
367 } else
368 seconds += wtime.tv_sec;
369 } else
370 seconds += wtime.tv_sec;
371 out:
372 seconds = PARA_MIN(seconds, length);
373 seconds = PARA_MAX(seconds, 0);
374 msg = make_message(
375 "%s%d:%02d [%d:%02d] (%d%%/%d:%02d)",
376 s? "" : "~",
377 seconds / 60,
378 seconds % 60,
379 (length - seconds) / 60,
380 (length - seconds) % 60,
381 length? (seconds * 100 + length / 2) / length : 0,
382 length / 60,
383 length % 60
384 );
385 //PARA_DEBUG_LOG("slot %d: %s\n", slot_num, msg);
386 return msg;
387 empty:
388 return para_strdup(NULL);
389 }
390
391 static void parse_config_or_die(void)
392 {
393 int ret;
394 char *config_file;
395 struct audiod_cmdline_parser_params params = {
396 .override = 0,
397 .initialize = 0,
398 .check_required = 1,
399 .check_ambiguity = 0,
400 .print_errors = 1
401 };
402
403 if (conf.config_file_given)
404 config_file = para_strdup(conf.config_file_arg);
405 else {
406 char *home = para_homedir();
407 config_file = make_message("%s/.paraslash/audiod.conf", home);
408 free(home);
409 }
410 ret = file_exists(config_file);
411 if (conf.config_file_given && !ret) {
412 PARA_EMERG_LOG("can not read config file %s\n", config_file);
413 goto err;
414 }
415 if (ret) {
416 audiod_cmdline_parser_config_file(config_file, &conf, &params);
417 daemon_set_loglevel(conf.loglevel_arg);
418 }
419 free(config_file);
420 return;
421 err:
422 free(config_file);
423 exit(EXIT_FAILURE);
424 }
425
426 static void setup_signal_handling(void)
427 {
428 signal_task = signal_init_or_die();
429 para_install_sighandler(SIGINT);
430 para_install_sighandler(SIGTERM);
431 para_install_sighandler(SIGHUP);
432 para_sigaction(SIGPIPE, SIG_IGN);
433 }
434
435 static void clear_slot(int slot_num)
436 {
437 struct slot_info *s = &slot[slot_num];
438
439 PARA_INFO_LOG("clearing slot %d\n", slot_num);
440 memset(s, 0, sizeof(struct slot_info));
441 s->format = -1;
442 }
443
444 static void close_receiver(int slot_num)
445 {
446 struct slot_info *s = &slot[slot_num];
447 struct audio_format_info *a;
448
449 if (s->format < 0 || !s->receiver_node)
450 return;
451 a = &afi[s->format];
452 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
453 audio_formats[s->format], slot_num);
454 a->receiver->close(s->receiver_node);
455 btr_remove_node(&s->receiver_node->btrn);
456 task_reap(&s->receiver_node->task);
457 free(s->receiver_node);
458 s->receiver_node = NULL;
459 stat_task->current_audio_format_num = -1;
460 tv_add(now, &(struct timeval)EMBRACE(0, 200 * 1000),
461 &a->restart_barrier);
462 }
463
464 static void writer_cleanup(struct writer_node *wn)
465 {
466 struct writer *w;
467
468 if (!wn)
469 return;
470 w = writers + wn->writer_num;
471 PARA_INFO_LOG("closing %s\n", writer_names[wn->writer_num]);
472 w->close(wn);
473 btr_remove_node(&wn->btrn);
474 task_reap(&wn->task);
475 }
476
477 static void close_writers(struct slot_info *s)
478 {
479 struct audio_format_info *a;
480 int i;
481
482 if (s->format < 0)
483 return;
484 assert(s->wns);
485 a = afi + s->format;
486 if (a->num_writers == 0)
487 writer_cleanup(s->wns);
488 else {
489 for (i = 0; i < a->num_writers; i++)
490 writer_cleanup(s->wns + i);
491 }
492 free(s->wns);
493 s->wns = NULL;
494 }
495
496 static void close_filters(struct slot_info *s)
497 {
498 int i;
499 struct audio_format_info *a = afi + s->format;
500 if (a->num_filters == 0)
501 return;
502 for (i = a->num_filters - 1; i >= 0; i--) {
503 struct filter_node *fn = s->fns + i;
504 struct filter *f;
505
506 if (!fn)
507 continue;
508 f = filters + fn->filter_num;
509 if (f->close)
510 f->close(fn);
511 btr_remove_node(&fn->btrn);
512 task_reap(&fn->task);
513 }
514 free(s->fns);
515 s->fns = NULL;
516 }
517
518 static void notify_receivers(int error)
519 {
520 int i;
521
522 FOR_EACH_SLOT(i) {
523 struct slot_info *s = slot + i;
524 if (s->format < 0)
525 continue;
526 if (!s->receiver_node)
527 continue;
528 task_notify(s->receiver_node->task, error);
529 }
530 }
531
532 static int get_empty_slot(void)
533 {
534 int i;
535 struct slot_info *s;
536
537 FOR_EACH_SLOT(i) {
538 s = &slot[i];
539 if (s->format < 0) {
540 clear_slot(i);
541 return i;
542 }
543 if (s->wns || s->receiver_node || s->fns)
544 continue;
545 clear_slot(i);
546 return i;
547 }
548 return -E_NO_MORE_SLOTS;
549 }
550
551 static void open_filters(struct slot_info *s)
552 {
553 struct audio_format_info *a = afi + s->format;
554 struct filter_node *fn;
555 int nf = a->num_filters;
556 struct btr_node *parent;
557 int i;
558
559 if (nf == 0)
560 return;
561 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
562 assert(s->fns == NULL);
563 s->fns = para_calloc(nf * sizeof(struct filter_node));
564 parent = s->receiver_node->btrn;
565 for (i = 0; i < nf; i++) {
566 char buf[20];
567 struct filter *f = filters + a->filter_nums[i];
568 fn = s->fns + i;
569 fn->filter_num = a->filter_nums[i];
570 fn->conf = a->filter_conf[i];
571 fn->btrn = btr_new_node(&(struct btr_node_description)
572 EMBRACE(.name = f->name, .parent = parent,
573 .handler = f->execute, .context = fn));
574
575 f->open(fn);
576 sprintf(buf, "%s (slot %d)", f->name, (int)(s - slot));
577 fn->task = task_register(&(struct task_info) {
578 .name = buf,
579 .pre_select = f->pre_select,
580 .post_select = f->post_select,
581 .context = fn,
582 }, &sched);
583 parent = fn->btrn;
584 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
585 audio_formats[s->format], i, nf, f->name, (int)(s - slot));
586 }
587 }
588
589 static void open_writers(struct slot_info *s)
590 {
591 int i;
592 struct audio_format_info *a = afi + s->format;
593 struct writer_node *wn;
594 struct btr_node *parent = s->fns[a->num_filters - 1].btrn;
595
596 assert(s->wns == NULL);
597 s->wns = para_calloc(PARA_MAX(1U, a->num_writers)
598 * sizeof(struct writer_node));
599 for (i = 0; i < a->num_writers; i++) {
600 wn = s->wns + i;
601 wn->conf = a->writer_conf[i];
602 wn->writer_num = a->writer_nums[i];
603 register_writer_node(wn, parent, &sched);
604 PARA_NOTICE_LOG("%s writer started in slot %d\n",
605 writer_names[a->writer_nums[i]], (int)(s - slot));
606 }
607 }
608
609 /* returns slot num on success */
610 static int open_receiver(int format)
611 {
612 struct audio_format_info *a = &afi[format];
613 struct slot_info *s;
614 int ret, slot_num;
615 struct receiver *r = a->receiver;
616 struct receiver_node *rn;
617
618 tv_add(now, &(struct timeval)EMBRACE(2, 0), &a->restart_barrier);
619 ret = get_empty_slot();
620 if (ret < 0)
621 return ret;
622 slot_num = ret;
623 rn = para_calloc(sizeof(*rn));
624 rn->receiver = r;
625 rn->conf = a->receiver_conf;
626 rn->btrn = btr_new_node(&(struct btr_node_description)
627 EMBRACE(.name = r->name, .context = rn));
628 ret = r->open(rn);
629 if (ret < 0) {
630 btr_remove_node(&rn->btrn);
631 free(rn);
632 return ret;
633 }
634 s = &slot[slot_num];
635 s->format = format;
636 s->receiver_node = rn;
637 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
638 audio_formats[format], r->name, slot_num);
639 rn->task = task_register(&(struct task_info) {
640 .name = r->name,
641 .pre_select = r->pre_select,
642 .post_select = r->post_select,
643 .context = rn,
644 }, &sched);
645 return slot_num;
646 }
647
648 static bool receiver_running(void)
649 {
650 int i;
651 long unsigned ss1 = stat_task->server_stream_start.tv_sec;
652
653 FOR_EACH_SLOT(i) {
654 struct slot_info *s = &slot[i];
655 long unsigned ss2 = s->server_stream_start.tv_sec;
656
657 if (!s->receiver_node)
658 continue;
659 if (task_status(s->receiver_node->task) >= 0)
660 return true;
661 if (ss1 == ss2)
662 return true;
663 }
664 return false;
665 }
666
667 /**
668 * Return the root node of the current buffer tree.
669 *
670 * This is only used for stream grabbing.
671 *
672 * \return \p NULL if no slot is currently active. If more than one buffer tree
673 * exists, the node corresponding to the most recently started receiver is
674 * returned.
675 */
676 struct btr_node *audiod_get_btr_root(void)
677 {
678 int i, newest_slot = -1;
679 struct timeval newest_rstime = {0, 0};
680
681 FOR_EACH_SLOT(i) {
682 struct slot_info *s = &slot[i];
683 struct timeval rstime;
684 if (!s->receiver_node)
685 continue;
686 if (task_status(s->receiver_node->task) < 0)
687 continue;
688 btr_get_node_start(s->receiver_node->btrn, &rstime);
689 if (newest_slot >= 0 && tv_diff(&rstime, &newest_rstime, NULL) < 0)
690 continue;
691 newest_rstime = rstime;
692 newest_slot = i;
693 }
694 if (newest_slot == -1)
695 return NULL;
696 return slot[newest_slot].receiver_node->btrn;
697 }
698
699 /* whether a new instance of a decoder should be started. */
700 static bool must_start_decoder(void)
701 {
702 int cafn = stat_task->current_audio_format_num;
703 unsigned vs = stat_task->vss_status;
704
705 if (audiod_status != AUDIOD_ON)
706 return false;
707 if (cafn < 0)
708 return false;
709 if (!stat_task->ct)
710 return false;
711 if (vs & VSS_STATUS_FLAG_NEXT)
712 return false;
713 if (!(vs & VSS_STATUS_FLAG_PLAYING))
714 return false;
715 if (receiver_running())
716 return false;
717 if (tv_diff(now, &afi[cafn].restart_barrier, NULL) < 0)
718 return false;
719 return true;
720 }
721
722 static void compute_time_diff(const struct timeval *status_time)
723 {
724 struct timeval tmp, diff;
725 static unsigned count;
726 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
727 const struct timeval max_deviation = {0, 500 * 1000};
728 const int time_smooth = 5;
729
730 sign = tv_diff(status_time, now, &diff);
731 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
732 // sign, sa_time_diff_sign);
733 if (!count) {
734 sa_time_diff_sign = sign;
735 stat_task->sa_time_diff = diff;
736 count++;
737 goto out;
738 }
739 if (count > 5) {
740 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
741 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
742 PARA_WARNING_LOG("time diff jump: %lims\n",
743 s * tv2ms(&tmp));
744 }
745 count++;
746 sa_time_diff_sign = tv_convex_combination(
747 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
748 count > 10? sign : sign * time_smooth, &diff,
749 &tmp);
750 stat_task->sa_time_diff = tmp;
751 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
752 sign < 0? "-" : "+",
753 tv2ms(&diff),
754 sa_time_diff_sign < 0? "-" : "+",
755 tv2ms(&stat_task->sa_time_diff)
756 );
757 out:
758 stat_task->sa_time_diff_sign = sa_time_diff_sign;
759 }
760
761 static int update_item(int itemnum, char *buf)
762 {
763 long unsigned sec, usec;
764
765 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
766 return 1;
767 free(stat_item_values[itemnum]);
768 stat_item_values[itemnum] = para_strdup(buf);
769 stat_client_write_item(itemnum);
770 switch (itemnum) {
771 case SI_STATUS_FLAGS:
772 stat_task->vss_status = 0;
773 if (strchr(buf, 'N'))
774 stat_task->vss_status |= VSS_STATUS_FLAG_NEXT;
775 if (strchr(buf, 'P'))
776 stat_task->vss_status |= VSS_STATUS_FLAG_PLAYING;
777 break;
778 case SI_OFFSET:
779 stat_task->offset_seconds = atoi(buf);
780 break;
781 case SI_SECONDS_TOTAL:
782 stat_task->length_seconds = atoi(buf);
783 break;
784 case SI_STREAM_START:
785 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
786 stat_task->server_stream_start.tv_sec = sec;
787 stat_task->server_stream_start.tv_usec = usec;
788 }
789 break;
790 case SI_CURRENT_TIME:
791 if (sscanf(buf, "%lu.%lu", &sec, &usec) == 2) {
792 struct timeval tv = {sec, usec};
793 compute_time_diff(&tv);
794 }
795 break;
796 case SI_FORMAT:
797 stat_task->current_audio_format_num
798 = get_audio_format_num(buf);
799 }
800 return 1;
801 }
802
803 static int parse_stream_command(const char *txt, char **cmd)
804 {
805 int ret, len;
806 char *re, *p = strchr(txt, ':');
807
808 if (!p)
809 return -E_MISSING_COLON;
810 *cmd = p + 1;
811 len = p - txt;
812 re = malloc(len + 1);
813 strncpy(re, txt, len);
814 re[len] = '\0';
815 ret = get_matching_audio_format_nums(re);
816 free(re);
817 return ret;
818 }
819
820 static int add_filter(int format, char *cmdline)
821 {
822 struct audio_format_info *a = &afi[format];
823 int filter_num, nf = a->num_filters;
824 void *cfg;
825
826 filter_num = check_filter_arg(cmdline, &cfg);
827 if (filter_num < 0)
828 return filter_num;
829 a->filter_conf = para_realloc(a->filter_conf,
830 (nf + 1) * sizeof(void *));
831 a->filter_nums = para_realloc(a->filter_nums,
832 (nf + 1) * sizeof(unsigned));
833 a->filter_nums[nf] = filter_num;
834 a->filter_conf[nf] = cfg;
835 a->num_filters++;
836 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf,
837 filters[filter_num].name);
838 return filter_num;
839 }
840
841 static int parse_writer_args(void)
842 {
843 int i, ret;
844 char *cmd;
845 struct audio_format_info *a;
846
847 for (i = 0; i < conf.writer_given; i++) {
848 void *wconf;
849 int j, nw, writer_num, af_mask;
850
851 ret = parse_stream_command(conf.writer_arg[i], &cmd);
852 if (ret < 0)
853 return ret;
854 af_mask = ret;
855 FOR_EACH_AUDIO_FORMAT(j) {
856 a = afi + j;
857 if ((af_mask & (1 << j)) == 0) /* no match */
858 continue;
859 wconf = check_writer_arg_or_die(cmd, &writer_num);
860 nw = a->num_writers;
861 a->writer_nums = para_realloc(a->writer_nums, (nw + 1) * sizeof(int));
862 a->writer_conf = para_realloc(a->writer_conf, (nw + 1) * sizeof(void *));
863 a->writer_nums[nw] = writer_num;
864 a->writer_conf[nw] = wconf;
865 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[j],
866 nw, writer_names[writer_num]);
867 a->num_writers++;
868 }
869 }
870 /* Use default writer for audio formats which are not yet set up. */
871 FOR_EACH_AUDIO_FORMAT(i) {
872 void *writer_conf;
873 int writer_num;
874 a = afi + i;
875 if (a->num_writers > 0)
876 continue; /* already set up */
877 writer_conf = check_writer_arg_or_die(NULL, &writer_num);
878 a->writer_nums = para_malloc(sizeof(int));
879 a->writer_nums[0] = writer_num;
880 a->writer_conf = para_malloc(sizeof(void *));
881 a->writer_conf[0] = writer_conf;
882 a->num_writers = 1;
883 PARA_INFO_LOG("%s writer: %s (default)\n", audio_formats[i],
884 writer_names[writer_num]);
885 }
886 return 1;
887 }
888
889 static int parse_receiver_args(void)
890 {
891 int i, ret, receiver_num;
892 char *cmd = NULL;
893 struct audio_format_info *a;
894
895 for (i = conf.receiver_given - 1; i >= 0; i--) {
896 char *arg;
897 int j, af_mask;
898
899 ret = parse_stream_command(conf.receiver_arg[i], &arg);
900 if (ret < 0)
901 goto out;
902 af_mask = ret;
903 FOR_EACH_AUDIO_FORMAT(j) {
904 a = afi + j;
905 if ((af_mask & (1 << j)) == 0) /* no match */
906 continue;
907 /*
908 * If multiple receivers are given for this audio format, the
909 * last one wins and we have to free the previous receiver
910 * config here. Since we are iterating backwards, the winning
911 * receiver arg is in fact the first one given.
912 */
913 if (a->receiver_conf)
914 a->receiver->free_config(a->receiver_conf);
915 a->receiver_conf = check_receiver_arg(arg, &receiver_num);
916 ret = -E_RECV_SYNTAX;
917 if (!a->receiver_conf)
918 goto out;
919 a->receiver = receivers + receiver_num;
920 }
921 }
922 /*
923 * Use the first available receiver with no arguments for those audio
924 * formats for which no receiver was specified.
925 */
926 cmd = para_strdup(receivers[0].name);
927 FOR_EACH_AUDIO_FORMAT(i) {
928 a = &afi[i];
929 if (a->receiver_conf)
930 continue;
931 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
932 if (!a->receiver_conf)
933 return -E_RECV_SYNTAX;
934 a->receiver = &receivers[receiver_num];
935 }
936 FOR_EACH_AUDIO_FORMAT(i) {
937 a = afi + i;
938 PARA_INFO_LOG("receiving %s streams via %s receiver\n",
939 audio_formats[i], a->receiver->name);
940 }
941 ret = 1;
942 out:
943 free(cmd);
944 return ret;
945 }
946
947 static int init_default_filters(void)
948 {
949 int i, ret = 1;
950
951 FOR_EACH_AUDIO_FORMAT(i) {
952 struct audio_format_info *a = &afi[i];
953 char *tmp;
954 int j;
955
956 if (a->num_filters)
957 continue; /* no default -- nothing to to */
958 /*
959 * udp and dccp streams are fec-encoded, so add fecdec as the
960 * first filter.
961 */
962 if (strcmp(afi[i].receiver->name, "udp") == 0 ||
963 strcmp(afi[i].receiver->name, "dccp") == 0) {
964 tmp = para_strdup("fecdec");
965 add_filter(i, tmp);
966 free(tmp);
967 if (ret < 0)
968 goto out;
969 }
970 /* add "dec" to audio format name */
971 tmp = make_message("%sdec", audio_formats[i]);
972 for (j = 0; filters[j].name; j++)
973 if (!strcmp(tmp, filters[j].name))
974 break;
975 free(tmp);
976 ret = -E_UNSUPPORTED_FILTER;
977 if (!filters[j].name)
978 goto out;
979 tmp = para_strdup(filters[j].name);
980 ret = add_filter(i, tmp);
981 free(tmp);
982 if (ret < 0)
983 goto out;
984 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
985 filters[j].name);
986 }
987 out:
988 return ret;
989 }
990
991 static int parse_filter_args(void)
992 {
993 int i, j, ret, af_mask, num_matches;
994
995 for (i = 0; i < conf.filter_given; i++) {
996 char *arg;
997 ret = parse_stream_command(conf.filter_arg[i], &arg);
998 if (ret < 0)
999 goto out;
1000 af_mask = ret;
1001 num_matches = 0;
1002 FOR_EACH_AUDIO_FORMAT(j) {
1003 if ((af_mask & (1 << j)) == 0) /* no match */
1004 continue;
1005 ret = add_filter(j, arg);
1006 if (ret < 0)
1007 goto out;
1008 num_matches++;
1009 }
1010 if (num_matches == 0)
1011 PARA_WARNING_LOG("ignoring filter spec: %s\n",
1012 conf.filter_arg[i]);
1013 }
1014 ret = init_default_filters(); /* use default values for the rest */
1015 out:
1016 return ret;
1017 }
1018
1019 static int parse_stream_args(void)
1020 {
1021 int ret;
1022
1023 ret = parse_receiver_args();
1024 if (ret < 0)
1025 return ret;
1026 ret = parse_filter_args();
1027 if (ret < 0)
1028 return ret;
1029 ret = parse_writer_args();
1030 if (ret < 0)
1031 return ret;
1032 return 1;
1033 }
1034
1035 /* does not unlink socket on errors */
1036 static void init_local_sockets(struct command_task *ct)
1037 {
1038 if (conf.socket_given)
1039 socket_name = para_strdup(conf.socket_arg);
1040 else {
1041 char *hn = para_hostname();
1042 socket_name = make_message("/var/paraslash/audiod_socket.%s",
1043 hn);
1044 free(hn);
1045 }
1046 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
1047 if (conf.force_given)
1048 unlink(socket_name);
1049 ct->fd[0] = create_local_socket(socket_name, 0);
1050 ct->fd[1] = create_local_socket(socket_name,
1051 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
1052 if (ct->fd[0] >= 0 || ct->fd[1] >= 0)
1053 return;
1054 PARA_EMERG_LOG("%s\n", para_strerror(-ct->fd[1]));
1055 exit(EXIT_FAILURE);
1056 }
1057
1058 static int signal_post_select(struct sched *s, void *context)
1059 {
1060 struct signal_task *st = context;
1061 int ret, signum;
1062
1063 ret = task_get_notification(st->task);
1064 if (ret < 0)
1065 return ret;
1066 signum = para_next_signal(&s->rfds);
1067 switch (signum) {
1068 case SIGINT:
1069 case SIGTERM:
1070 case SIGHUP:
1071 PARA_NOTICE_LOG("received signal %d\n", signum);
1072 task_notify_all(s, E_AUDIOD_SIGNAL);
1073 return -E_AUDIOD_SIGNAL;
1074 }
1075 return 0;
1076 }
1077
1078 static void command_pre_select(struct sched *s, void *context)
1079 {
1080 struct command_task *ct = context;
1081 int i;
1082
1083 for (i = 0; i < 2; i++)
1084 if (ct->fd[i] >= 0)
1085 para_fd_set(ct->fd[i], &s->rfds, &s->max_fileno);
1086 }
1087
1088 static int command_post_select(struct sched *s, void *context)
1089 {
1090 int ret, i;
1091 struct command_task *ct = context;
1092 static struct timeval last_status_dump;
1093 struct timeval tmp, delay;
1094 bool force = false;
1095
1096 ret = task_get_notification(ct->task);
1097 if (ret < 0)
1098 return ret;
1099 for (i = 0; i < 2; i++) {
1100 if (ct->fd[i] < 0)
1101 continue;
1102 ret = handle_connect(ct->fd[i], &s->rfds);
1103 if (ret < 0) {
1104 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1105 if (ret == -E_AUDIOD_TERM) {
1106 task_notify_all(s, -ret);
1107 return ret;
1108 }
1109 } else if (ret > 0)
1110 force = true;
1111 }
1112 if (force == true)
1113 goto dump;
1114
1115 /* if last status dump was less than 500ms ago, do nothing */
1116 delay.tv_sec = 0;
1117 delay.tv_usec = 500 * 1000;
1118 tv_add(&last_status_dump, &delay, &tmp);
1119 if (tv_diff(now, &tmp, NULL) < 0)
1120 return 0;
1121
1122 /*
1123 * If last status dump was more than 5s ago, force update. Otherwise,
1124 * update only those items that have changed.
1125 */
1126 delay.tv_sec = 5;
1127 delay.tv_usec = 0;
1128 tv_add(&last_status_dump, &delay, &tmp);
1129 if (tv_diff(now, &tmp, NULL) > 0)
1130 force = true;
1131 dump:
1132 audiod_status_dump(force);
1133 last_status_dump = *now;
1134 return 1;
1135 }
1136
1137 static void init_command_task(struct command_task *ct)
1138 {
1139 init_local_sockets(ct); /* doesn't return on errors */
1140
1141 ct->task = task_register(&(struct task_info) {
1142 .name = "command",
1143 .pre_select = command_pre_select,
1144 .post_select = command_post_select,
1145 .context = ct,
1146 }, &sched);
1147 }
1148
1149 static void close_stat_pipe(void)
1150 {
1151 if (!stat_task->ct)
1152 return;
1153 task_reap(&stat_task->ct->task);
1154 client_close(stat_task->ct);
1155 stat_task->ct = NULL;
1156 clear_and_dump_items();
1157 stat_task->length_seconds = 0;
1158 stat_task->offset_seconds = 0;
1159 stat_task->vss_status = 0;
1160 stat_task->current_audio_format_num = -1;
1161 audiod_status_dump(true);
1162 }
1163
1164 /* avoid busy loop if server is down */
1165 static void set_stat_task_restart_barrier(unsigned seconds)
1166 {
1167 struct timeval delay = {seconds, 0};
1168 tv_add(now, &delay, &stat_task->restart_barrier);
1169 }
1170
1171 static bool must_close_slot(int slot_num)
1172 {
1173 struct slot_info *s = &slot[slot_num];
1174 struct audio_format_info *a = afi + s->format;
1175 int i;
1176
1177 if (s->format < 0)
1178 return false;
1179 if (s->receiver_node && task_status(s->receiver_node->task) >= 0)
1180 return false;
1181 for (i = 0; i < a->num_filters; i++)
1182 if (s->fns && task_status(s->fns[i].task) >= 0)
1183 return false;
1184 if (a->num_writers > 0) {
1185 for (i = 0; i < a->num_writers; i++)
1186 if (s->wns && task_status(s->wns[i].task) >= 0)
1187 return false;
1188 } else {
1189 if (s->wns && task_status(s->wns[0].task) >= 0)
1190 return false;
1191 }
1192 return true;
1193 }
1194
1195 static void close_slot(int slot_num)
1196 {
1197 struct slot_info *s = slot + slot_num;
1198
1199 PARA_INFO_LOG("closing slot %d\n", slot_num);
1200 close_writers(s);
1201 close_filters(s);
1202 close_receiver(slot_num);
1203 clear_slot(slot_num);
1204 }
1205
1206 static void close_unused_slots(void)
1207 {
1208 int i;
1209
1210 FOR_EACH_SLOT(i)
1211 if (must_close_slot(i))
1212 close_slot(i);
1213 }
1214
1215 /*
1216 * Cleanup all resources.
1217 *
1218 * This performs various cleanups, removes the audiod socket and closes the
1219 * connection to para_server.
1220 */
1221 static void audiod_cleanup(void)
1222 {
1223 if (socket_name)
1224 unlink(socket_name);
1225 close_stat_pipe();
1226 close_unused_slots();
1227 audiod_cmdline_parser_free(&conf);
1228 close_stat_clients();
1229 }
1230
1231 /*
1232 * Check if any receivers/filters/writers need to be started and do so if
1233 * necessary.
1234 */
1235 static void start_stop_decoders(void)
1236 {
1237 int ret;
1238 struct slot_info *sl;
1239
1240 close_unused_slots();
1241 if (audiod_status != AUDIOD_ON ||
1242 !(stat_task->vss_status & VSS_STATUS_FLAG_PLAYING))
1243 return notify_receivers(E_NOT_PLAYING);
1244 if (!must_start_decoder())
1245 return;
1246 ret = open_receiver(stat_task->current_audio_format_num);
1247 if (ret < 0) {
1248 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
1249 return;
1250 }
1251 sl = slot + ret;
1252 open_filters(sl);
1253 open_writers(sl);
1254 activate_grab_clients(&sched);
1255 btr_log_tree(sl->receiver_node->btrn, LL_NOTICE);
1256 }
1257
1258 static void status_pre_select(struct sched *s, void *context)
1259 {
1260 struct status_task *st = context;
1261 int i, ret, cafn = stat_task->current_audio_format_num;
1262
1263 if (must_start_decoder())
1264 goto min_delay;
1265 FOR_EACH_SLOT(i)
1266 if (must_close_slot(i))
1267 goto min_delay;
1268 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1269 if (ret > 0)
1270 goto min_delay;
1271 if (st->ct && audiod_status == AUDIOD_OFF)
1272 goto min_delay;
1273 if (!st->ct && audiod_status != AUDIOD_OFF)
1274 sched_request_barrier_or_min_delay(&st->restart_barrier, s);
1275 if (cafn >= 0)
1276 sched_request_barrier(&afi[cafn].restart_barrier, s);
1277 /*
1278 * If para_server is playing we'd like to have a smooth time display
1279 * even if we are running in standby mode. So we request a timeout that
1280 * expires at the next full second.
1281 */
1282 if (stat_task->vss_status & VSS_STATUS_FLAG_PLAYING)
1283 sched_request_timeout_ms(1000 - now->tv_usec / 1000, s);
1284 return;
1285 min_delay:
1286 sched_min_delay(s);
1287 }
1288
1289 /* restart the client task if necessary */
1290 static int status_post_select(struct sched *s, void *context)
1291 {
1292 struct status_task *st = context;
1293 int ret;
1294
1295 ret = task_get_notification(st->task);
1296 if (ret < 0)
1297 return ret;
1298 if (audiod_status == AUDIOD_OFF) {
1299 if (!st->ct)
1300 goto out;
1301 if (task_status(st->ct->task) >= 0) {
1302 task_notify(st->ct->task, E_AUDIOD_OFF);
1303 goto out;
1304 }
1305 close_stat_pipe();
1306 st->clock_diff_count = conf.clock_diff_count_arg;
1307 goto out;
1308 }
1309 if (st->ct) {
1310 char *buf;
1311 size_t sz;
1312
1313 ret = btr_node_status(st->btrn, st->min_iqs, BTR_NT_LEAF);
1314 if (ret < 0) {
1315 close_stat_pipe();
1316 goto out;
1317 }
1318 if (st->ct->status != CL_EXECUTING)
1319 goto out;
1320 if (ret == 0) {
1321 struct timeval diff;
1322 tv_diff(now, &st->last_status_read, &diff);
1323 if (diff.tv_sec > 61)
1324 task_notify(st->ct->task, E_STATUS_TIMEOUT);
1325 goto out;
1326 }
1327 btr_merge(st->btrn, st->min_iqs);
1328 sz = btr_next_buffer(st->btrn, &buf);
1329 ret = for_each_stat_item(buf, sz, update_item);
1330 if (ret < 0) {
1331 task_notify(st->ct->task, -ret);
1332 goto out;
1333 }
1334 if (sz != ret) {
1335 btr_consume(st->btrn, sz - ret);
1336 st->last_status_read = *now;
1337 st->min_iqs = 0;
1338 } else /* current status item crosses buffers */
1339 st->min_iqs = sz + 1;
1340 goto out;
1341 }
1342 btr_drain(st->btrn);
1343 st->current_audio_format_num = -1;
1344 if (tv_diff(now, &st->restart_barrier, NULL) < 0)
1345 goto out;
1346 if (st->clock_diff_count) { /* get status only one time */
1347 char *argv[] = {"audiod", "--", "stat", "-p", "-n=1", NULL};
1348 int argc = 5;
1349 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1350 st->clock_diff_count--;
1351 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1352 set_stat_task_restart_barrier(2);
1353
1354 } else {
1355 char *argv[] = {"audiod", "--", "stat", "-p", NULL};
1356 int argc = 4;
1357 client_open(argc, argv, &st->ct, NULL, NULL, st->btrn, s);
1358 set_stat_task_restart_barrier(5);
1359 }
1360 free(stat_item_values[SI_BASENAME]);
1361 stat_item_values[SI_BASENAME] = para_strdup(
1362 "no connection to para_server");
1363 stat_client_write_item(SI_BASENAME);
1364 st->last_status_read = *now;
1365 out:
1366 start_stop_decoders();
1367 return 0;
1368 }
1369
1370 static void init_status_task(struct status_task *st)
1371 {
1372 memset(st, 0, sizeof(struct status_task));
1373 st->sa_time_diff_sign = 1;
1374 st->clock_diff_count = conf.clock_diff_count_arg;
1375 st->current_audio_format_num = -1;
1376 st->btrn = btr_new_node(&(struct btr_node_description)
1377 EMBRACE(.name = "stat"));
1378
1379 stat_task->task = task_register(&(struct task_info) {
1380 .name = "stat",
1381 .pre_select = status_pre_select,
1382 .post_select = status_post_select,
1383 .context = stat_task,
1384 }, &sched);
1385 }
1386
1387 static void set_initial_status(void)
1388 {
1389 audiod_status = AUDIOD_ON;
1390 if (!conf.mode_given)
1391 return;
1392 if (!strcmp(conf.mode_arg, "sb")) {
1393 audiod_status = AUDIOD_STANDBY;
1394 return;
1395 }
1396 if (!strcmp(conf.mode_arg, "off")) {
1397 audiod_status = AUDIOD_OFF;
1398 return;
1399 }
1400 if (strcmp(conf.mode_arg, "on"))
1401 PARA_WARNING_LOG("invalid mode\n");
1402 }
1403
1404 __noreturn static void print_help_and_die(void)
1405 {
1406 struct ggo_help h = DEFINE_GGO_HELP(audiod);
1407 bool d = conf.detailed_help_given;
1408 unsigned flags;
1409
1410 flags = d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS;
1411 ggo_print_help(&h, flags);
1412
1413 flags = d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS;
1414 print_receiver_helps(flags);
1415 print_filter_helps(flags);
1416 print_writer_helps(flags);
1417 exit(0);
1418 }
1419
1420 /**
1421 * the main function of para_audiod
1422 *
1423 * \param argc usual argument count
1424 * \param argv usual argument vector
1425 *
1426 * \return EXIT_SUCCESS or EXIT_FAILURE
1427 *
1428 * \sa para_audiod(1)
1429 * */
1430 int main(int argc, char *argv[])
1431 {
1432 int ret, i;
1433 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1434 struct audiod_cmdline_parser_params params = {
1435 .override = 0,
1436 .initialize = 1,
1437 .check_required = 0,
1438 .check_ambiguity = 0,
1439 .print_errors = 1
1440 };
1441
1442 valid_fd_012();
1443 audiod_cmdline_parser_ext(argc, argv, &conf, &params);
1444 daemon_set_loglevel(conf.loglevel_arg);
1445 version_handle_flag("audiod", conf.version_given);
1446 /* init receivers/filters/writers early to make help work */
1447 recv_init();
1448 filter_init();
1449 writer_init();
1450 if (conf.help_given || conf.detailed_help_given)
1451 print_help_and_die();
1452 daemon_drop_privileges_or_die(conf.user_arg, conf.group_arg);
1453 parse_config_or_die();
1454 daemon_init_colors_or_die(conf.color_arg, color_arg_auto, color_arg_no,
1455 conf.logfile_given, conf.log_color_arg, conf.log_color_given);
1456 init_random_seed_or_die();
1457 daemon_set_flag(DF_LOG_TIME);
1458 daemon_set_flag(DF_LOG_HOSTNAME);
1459 daemon_set_flag(DF_LOG_LL);
1460 if (conf.log_timing_given)
1461 daemon_set_flag(DF_LOG_TIMING);
1462 if (conf.logfile_given) {
1463 daemon_set_logfile(conf.logfile_arg);
1464 daemon_open_log_or_die();
1465 }
1466 ret = parse_stream_args();
1467 if (ret < 0) {
1468 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1469 exit(EXIT_FAILURE);
1470 }
1471 daemon_log_welcome("para_audiod");
1472 daemon_set_start_time();
1473 set_initial_status();
1474 FOR_EACH_SLOT(i)
1475 clear_slot(i);
1476 setup_signal_handling();
1477
1478 init_status_task(stat_task);
1479 init_command_task(cmd_task);
1480
1481 if (conf.daemon_given)
1482 daemonize(false /* parent exits immediately */);
1483
1484 signal_task->task = task_register(&(struct task_info) {
1485 .name = "signal",
1486 .pre_select = signal_pre_select,
1487 .post_select = signal_post_select,
1488 .context = signal_task,
1489 }, &sched);
1490
1491 sched.default_timeout.tv_sec = 2;
1492 sched.default_timeout.tv_usec = 999 * 1000;
1493 ret = schedule(&sched);
1494 audiod_cleanup();
1495 sched_shutdown(&sched);
1496 signal_shutdown(signal_task);
1497
1498 if (ret < 0)
1499 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
1500 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
1501 }