replace standard GPL header by a one-line pointer to COPYING
[paraslash.git] / audiod.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
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 "para.h"
10
11 #include "audiod.cmdline.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "recv.h"
15 #include "filter.h"
16 #include "grab_client.cmdline.h"
17 #include "grab_client.h"
18 #include "client.cmdline.h"
19 #include "client.h"
20 #include "audiod.h"
21 #include "net.h"
22 #include "daemon.h"
23 #include "string.h"
24 #include "fd.h"
25 #include "write.h"
26 #include "write_common.h"
27 #include "error.h"
28 #include "signal.h"
29
30 /** define the array of error lists needed by para_audiod */
31 INIT_AUDIOD_ERRLISTS;
32 /** define the array containing all supported audio formats */
33 const char *audio_formats[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL};
34
35 /** defines how to handle one supported audio format */
36 struct audio_format_info {
37 /** pointer to the receiver for this audio format */
38 struct receiver *receiver;
39 /** the receiver configuration */
40 void *receiver_conf;
41 /** the number of filters that should be activated for this audio format */
42 unsigned int num_filters;
43 /** pointer to the array of filters to be activated */
44 struct filter **filters;
45 /** pointer to the array of filter configurations */
46 void **filter_conf;
47 /** the number of filters that should be activated for this audio format */
48 unsigned int num_writers;
49 /** pointer to the array of writers to be activated */
50 struct writer **writers;
51 /** pointer to the array of writer configurations */
52 void **writer_conf;
53 /** do not start receiver/filters/writer before this time */
54 struct timeval restart_barrier;
55 };
56
57 /**
58 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
59 * be associated with a receiver/filter/writer triple. This array holds all
60 * information on the status of these slots.
61 *
62 * \sa struct slot_info
63 * */
64 struct slot_info slot[MAX_STREAM_SLOTS];
65
66
67 /**
68 * the current mode of operation of which can be changed by the on/off/cycle
69 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
70 */
71 int audiod_status = AUDIOD_ON;
72
73 /**
74 * the gengetopt args_info struct that holds information on all command line
75 * arguments
76 */
77 struct audiod_args_info conf;
78
79 static char *socket_name;
80 static FILE *logfile;
81 static struct audio_format_info afi[NUM_AUDIO_FORMATS];
82
83 static struct signal_task signal_task_struct, *sig_task = &signal_task_struct;
84
85 static struct status_task status_task_struct;
86
87 /**
88 * the task that calls the status command of para_server
89 *
90 * \sa struct status_task
91 */
92 struct status_task *stat_task = &status_task_struct;
93 static struct timeval initial_delay_barrier;
94
95 /**
96 * the task for handling audiod commands
97 *
98 * \sa struct task, struct sched
99 */
100 struct command_task {
101 /** the local listening socket */
102 int fd;
103 /** the associated task structure */
104 struct task task;
105 };
106
107 /**
108 * task for signal handling
109 */
110 struct signal_task {
111 /** the signal pipe */
112 int fd;
113 /** the number of the most recent signal */
114 int signum;
115 /** the associated task structure */
116 struct task task;
117 };
118
119 /** iterate over all supported audio formats */
120 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
121
122 /**
123 * get the audio format number
124 * \param name the name of the audio format
125 *
126 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
127 * \a name is not a supported audio format.
128 */
129 int get_audio_format_num(char *name)
130 {
131 int i;
132 FOR_EACH_AUDIO_FORMAT(i)
133 if (!strcmp(name, audio_formats[i]))
134 return i;
135 return -E_UNSUPPORTED_AUDIO_FORMAT;
136 }
137
138 /**
139 * the log function of para_audiod
140 *
141 * \param ll loglevel
142 * \param fmt the format string
143 */
144 void para_log(int ll, const char* fmt,...)
145 {
146 va_list argp;
147 FILE *outfd;
148 struct tm *tm;
149 time_t t1;
150 char str[MAXLINE] = "";
151 static char *hostname;
152
153 if (ll < conf.loglevel_arg)
154 return;
155 if (!hostname)
156 hostname = para_hostname();
157 outfd = logfile? logfile : stderr;
158 time(&t1);
159 tm = localtime(&t1);
160 strftime(str, MAXLINE, "%b %d %H:%M:%S", tm);
161 fprintf(outfd, "%s %s ", str, hostname);
162 if (conf.loglevel_arg <= INFO)
163 fprintf(outfd, "%i ", ll);
164 va_start(argp, fmt);
165 vfprintf(outfd, fmt, argp);
166 va_end(argp);
167 }
168
169 static char *configfile_exists(void)
170 {
171 static char *config_file;
172
173 if (!config_file) {
174 char *home = para_homedir();
175 config_file = make_message("%s/.paraslash/audiod.conf", home);
176 free(home);
177 }
178 return file_exists(config_file)? config_file : NULL;
179 }
180
181 static void setup_signal_handling(void)
182 {
183 sig_task->fd = para_signal_init();
184 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task->fd);
185 para_install_sighandler(SIGINT);
186 para_install_sighandler(SIGTERM);
187 para_install_sighandler(SIGHUP);
188 signal(SIGPIPE, SIG_IGN);
189 }
190
191 static void clear_slot(int slot_num)
192 {
193 struct slot_info *s = &slot[slot_num];
194
195 PARA_INFO_LOG("clearing slot %d\n", slot_num);
196 memset(s, 0, sizeof(struct slot_info));
197 s->format = -1;
198 }
199
200 static void close_receiver(int slot_num)
201 {
202 struct slot_info *s = &slot[slot_num];
203 struct audio_format_info *a;
204
205 if (s->format < 0 || !s->receiver_node)
206 return;
207 a = &afi[s->format];
208 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
209 audio_formats[s->format] , slot_num, s->receiver_node->eof);
210 a->receiver->close(s->receiver_node);
211 free(s->receiver_node);
212 s->receiver_node = NULL;
213 }
214
215 static void kill_all_decoders(void)
216 {
217 int i;
218
219 FOR_EACH_SLOT(i) {
220 struct slot_info *s = &slot[i];
221 if (s->wng && !s->wng->eof) {
222 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
223 i);
224 wng_unregister(s->wng);
225 s->wng->eof = 1;
226 }
227 if (s->fc && !s->fc->eof) {
228 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i);
229 unregister_task(&s->fc->task);
230 s->fc->eof = 1;
231 }
232 if (s->receiver_node && !s->receiver_node->eof) {
233 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i);
234 unregister_task(&s->receiver_node->task);
235 s->receiver_node->eof = 1;
236 }
237 }
238 }
239
240 static int get_empty_slot(void)
241 {
242 int i;
243 struct slot_info *s;
244
245 FOR_EACH_SLOT(i) {
246 s = &slot[i];
247 if (s->format < 0) {
248 clear_slot(i);
249 return i;
250 }
251 if (s->wng || s->receiver_node || s->fc)
252 continue;
253 clear_slot(i);
254 return i;
255 }
256 return -E_NO_MORE_SLOTS;
257 }
258
259 /**
260 * get the number of filters
261 *
262 * \param audio_format_num the number identifying the audio format
263 *
264 * \return the number of filters for the given audio format
265 *
266 * \sa struct filter;
267 */
268 int num_filters(int audio_format_num)
269 {
270 return afi[audio_format_num].num_filters;
271 }
272
273 static void filter_event_handler(struct task *t)
274 {
275 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
276 struct filter_chain *fc = t->private_data;
277 fc->eof = 1;
278 unregister_task(t);
279 }
280
281 static void open_filters(int slot_num)
282 {
283 struct slot_info *s = &slot[slot_num];
284 struct audio_format_info *a = &afi[s->format];
285 int nf = a->num_filters;
286 int i;
287
288 s->fc = NULL;
289 if (!nf)
290 return;
291 PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
292 s->fc = para_calloc(sizeof(struct filter_chain));
293 INIT_LIST_HEAD(&s->fc->filters);
294 s->fc->inbuf = s->receiver_node->buf;
295 s->fc->in_loaded = &s->receiver_node->loaded;
296 s->fc->input_eof = &s->receiver_node->eof;
297 s->fc->task.pre_select = filter_pre_select;
298 s->fc->task.event_handler = filter_event_handler;
299 s->fc->task.private_data = s->fc;
300 s->fc->eof = 0;
301
302 s->receiver_node->output_eof = &s->fc->eof;
303 sprintf(s->fc->task.status, "filter chain");
304 for (i = 0; i < nf; i++) {
305 struct filter_node *fn = para_calloc(sizeof(struct filter_node));
306 fn->conf = a->filter_conf[i];
307 fn->fc = s->fc;
308 fn->filter = a->filters[i];
309 INIT_LIST_HEAD(&fn->callbacks);
310 list_add_tail(&fn->node, &s->fc->filters);
311 fn->filter->open(fn);
312 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
313 audio_formats[s->format], i + 1, nf,
314 fn->filter->name, slot_num);
315 s->fc->outbuf = fn->buf;
316 s->fc->out_loaded = &fn->loaded;
317 }
318 register_task(&s->fc->task);
319 }
320
321 static void wng_event_handler(struct task *t)
322 {
323 struct writer_node_group *wng = t->private_data;
324
325 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
326 wng->eof = 1;
327 wng_unregister(wng);
328 }
329
330 static void open_writers(int slot_num)
331 {
332 int ret, i;
333 struct slot_info *s = &slot[slot_num];
334 struct audio_format_info *a = &afi[s->format];
335
336 PARA_INFO_LOG("opening %s writers\n", audio_formats[s->format]);
337 if (!a->num_writers)
338 s->wng = setup_default_wng();
339 else
340 s->wng = wng_new(a->num_writers);
341 if (s->fc) {
342 s->wng->buf = s->fc->outbuf;
343 s->wng->loaded = s->fc->out_loaded;
344 s->wng->input_eof = &s->fc->eof;
345 s->wng->channels = &s->fc->channels;
346 s->wng->samplerate = &s->fc->samplerate;
347 s->fc->output_eof = &s->wng->eof;
348 PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
349 } else {
350 s->wng->buf = s->receiver_node->buf;
351 s->wng->loaded = &s->receiver_node->loaded;
352 s->wng->input_eof = &s->receiver_node->eof;
353 }
354 s->wng->task.event_handler = wng_event_handler;
355 for (i = 0; i < a->num_writers; i++) {
356 s->wng->writer_nodes[i].conf = a->writer_conf[i];
357 s->wng->writer_nodes[i].writer = a->writers[i];
358 }
359 ret = wng_open(s->wng);
360 if (ret < 0) {
361 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
362 return;
363 }
364 s->wstime = *now;
365 activate_inactive_grab_clients(slot_num, s->format, &s->fc->filters);
366 }
367
368 static void rn_event_handler(struct task *t)
369 {
370 struct receiver_node *rn = t->private_data;
371 const struct timeval restart_delay = {0, 10 * 1000};
372 int i;
373
374 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t->ret));
375 unregister_task(t);
376 rn->eof = 1;
377 /* set restart barrier */
378 FOR_EACH_SLOT(i) {
379 if (slot[i].receiver_node != rn)
380 continue;
381 tv_add(now, &restart_delay, &afi[slot[i].format].restart_barrier);
382 }
383 }
384
385 static int open_receiver(int format)
386 {
387 struct audio_format_info *a = &afi[format];
388 struct slot_info *s;
389 int ret, slot_num;
390 struct receiver_node *rn;
391 const struct timeval restart_delay = {1, 0};
392
393 ret = get_empty_slot();
394 if (ret < 0)
395 goto err;
396 slot_num = ret;
397 s = &slot[slot_num];
398 s->format = format;
399 s->receiver_node = para_calloc(sizeof(struct receiver_node));
400 rn = s->receiver_node;
401 rn->receiver = a->receiver;
402 rn->conf = a->receiver_conf;
403 ret = a->receiver->open(s->receiver_node);
404 if (ret < 0) {
405 free(s->receiver_node);
406 s->receiver_node = NULL;
407 goto err;
408 }
409 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
410 audio_formats[s->format], a->receiver->name, slot_num);
411 rn->task.private_data = s->receiver_node;
412 rn->task.pre_select = a->receiver->pre_select;
413 rn->task.post_select = a->receiver->post_select;
414 rn->task.event_handler = rn_event_handler;
415 sprintf(rn->task.status, "%s receiver node", rn->receiver->name);
416 register_task(&rn->task);
417 return 1;
418 err:
419 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
420 tv_add(now, &restart_delay, &afi[format].restart_barrier);
421 return ret;
422 }
423
424 static int receiver_running(int format)
425 {
426 int i;
427
428 FOR_EACH_SLOT(i) {
429 struct slot_info *s = &slot[i];
430 if (s->format == format && s->receiver_node
431 && !s->receiver_node->eof)
432 return 1;
433 }
434 return 0;
435 }
436
437 static int open_current_receiver(struct sched *s)
438 {
439 int i;
440 struct timeval diff;
441 char *audio_format = stat_task->stat_item_values[SI_FORMAT];
442
443 if (!audio_format || !stat_task->pcd)
444 return 0;
445 i = get_audio_format_num(audio_format + strlen(
446 status_item_list[SI_FORMAT]) + 1);
447 if (i < 0)
448 return 0;
449 if (receiver_running(i))
450 return 0;
451 if (tv_diff(now, &afi[i].restart_barrier, &diff) < 0) {
452 s->timeout = diff;
453 return 0;
454 }
455 return open_receiver(i) < 0? 0 : 1;
456 }
457
458 static unsigned compute_time_diff(const struct timeval *status_time)
459 {
460 struct timeval tmp, diff;
461 static unsigned count;
462 int sign, sa_time_diff_sign = stat_task->sa_time_diff_sign;
463 const struct timeval max_deviation = {0, 500 * 1000};
464 const int time_smooth = 5;
465
466 if (!status_time)
467 return count;
468 sign = tv_diff(status_time, now, &diff);
469 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
470 // sign, sa_time_diff_sign);
471 if (!count) {
472 sa_time_diff_sign = sign;
473 stat_task->sa_time_diff = diff;
474 count++;
475 goto out;
476 }
477 if (count > 5) {
478 int s = tv_diff(&diff, &stat_task->sa_time_diff, &tmp);
479 if (tv_diff(&max_deviation, &tmp, NULL) < 0)
480 PARA_WARNING_LOG("time diff jump: %lims\n",
481 s * tv2ms(&tmp));
482 }
483 count++;
484 sa_time_diff_sign = tv_convex_combination(
485 sa_time_diff_sign * time_smooth, &stat_task->sa_time_diff,
486 count > 10? sign : sign * time_smooth, &diff,
487 &tmp);
488 stat_task->sa_time_diff = tmp;
489 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
490 sign > 0? "+" : "-",
491 tv2ms(&diff),
492 sa_time_diff_sign ? "+" : "-",
493 tv2ms(&stat_task->sa_time_diff)
494 );
495 out:
496 stat_task->sa_time_diff_sign = sa_time_diff_sign;
497 return count;
498 }
499
500 static void check_stat_line(char *line)
501 {
502 int itemnum;
503 size_t ilen = 0;
504 long unsigned sec, usec;
505 char *tmp;
506
507 // PARA_INFO_LOG("line: %s\n", line);
508 if (!line)
509 return;
510 itemnum = stat_line_valid(line);
511 if (itemnum < 0) {
512 PARA_WARNING_LOG("invalid status line: %s\n", line);
513 return;
514 }
515 if (stat_task->clock_diff_count && itemnum != SI_CURRENT_TIME)
516 return;
517 tmp = make_message("%s\n", line);
518 stat_client_write(tmp, itemnum);
519 free(tmp);
520 free(stat_task->stat_item_values[itemnum]);
521 stat_task->stat_item_values[itemnum] = para_strdup(line);
522 ilen = strlen(status_item_list[itemnum]);
523 switch (itemnum) {
524 case SI_STATUS:
525 stat_task->playing = strstr(line, "playing")? 1 : 0;
526 break;
527 case SI_OFFSET:
528 stat_task->offset_seconds = atoi(line + ilen + 1);
529 break;
530 case SI_LENGTH:
531 stat_task->length_seconds = atoi(line + ilen + 1);
532 break;
533 case SI_STREAM_START:
534 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
535 struct timeval a_start, delay;
536 delay.tv_sec = conf.stream_delay_arg / 1000;
537 delay.tv_usec = (conf.stream_delay_arg % 1000) * 1000;
538 stat_task->server_stream_start.tv_sec = sec;
539 stat_task->server_stream_start.tv_usec = usec;
540 if (compute_time_diff(NULL) > 2) {
541 if (stat_task->sa_time_diff_sign < 0)
542 tv_add(&stat_task->server_stream_start,
543 &stat_task->sa_time_diff, &a_start);
544 else
545 tv_diff(&stat_task->server_stream_start,
546 &stat_task->sa_time_diff, &a_start);
547 tv_add(&a_start, &delay, &initial_delay_barrier);
548 }
549 }
550 break;
551 case SI_CURRENT_TIME:
552 if (sscanf(line + ilen + 1, "%lu.%lu", &sec, &usec) == 2) {
553 struct timeval tv = {sec, usec};
554 compute_time_diff(&tv);
555 }
556 if (stat_task->clock_diff_count)
557 stat_task->clock_diff_count--;
558 break;
559 }
560 }
561
562 static void try_to_close_slot(int slot_num)
563 {
564 struct slot_info *s = &slot[slot_num];
565
566 if (s->format < 0)
567 return;
568 if (s->receiver_node && !s->receiver_node->eof)
569 return;
570 if (s->fc && !s->fc->eof)
571 return;
572 if (s->wng && !s->wng->eof)
573 return;
574 PARA_INFO_LOG("closing slot %d \n", slot_num);
575 wng_close(s->wng);
576 close_filters(s->fc);
577 free(s->fc);
578 close_receiver(slot_num);
579 clear_slot(slot_num);
580 }
581
582 /*
583 * Check if any receivers/filters/writers need to be started and do so if
584 * neccessary. Since the pre_select function didn't have a chance yet to put
585 * file descriptors into the fd sets given by s, make the upcoming select()
586 * return immediately to avoid a long timeout in case we started something.
587 */
588 static void audiod_pre_select(struct sched *s, __a_unused struct task *t)
589 {
590 int i;
591 struct timeval min_delay = {0, 1};
592
593 t->ret = 1;
594 if (audiod_status != AUDIOD_ON || !stat_task->playing)
595 return kill_all_decoders();
596 if (open_current_receiver(s))
597 s->timeout = min_delay;
598 FOR_EACH_SLOT(i) {
599 struct slot_info *sl = &slot[i];
600 struct audio_format_info *a;
601 struct timeval diff;
602
603 if (sl->format < 0)
604 continue;
605 a = &afi[sl->format];
606 if (!sl->receiver_node)
607 continue;
608 if (!a->num_filters) {
609 if (sl->receiver_node->loaded && !sl->wng) {
610 open_writers(i);
611 s->timeout = min_delay;
612 }
613 continue;
614 }
615 if (sl->receiver_node->loaded && !sl->fc) {
616 open_filters(i);
617 s->timeout = min_delay;
618 continue;
619 }
620 if (!sl->fc || !*sl->fc->out_loaded || sl->wng)
621 continue;
622 if (tv_diff(now, &initial_delay_barrier, &diff) > 0) {
623 open_writers(i);
624 s->timeout = min_delay;
625 continue;
626 }
627 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff));
628 if (tv_diff(&s->timeout, &diff, NULL) > 0) {
629 s->timeout = diff;
630 }
631 }
632 }
633
634 static void audiod_post_select(__a_unused struct sched *s,
635 __a_unused struct task *t)
636 {
637 int i;
638
639 t->ret = 1;
640 FOR_EACH_SLOT(i)
641 try_to_close_slot(i);
642 }
643
644 static void init_audiod_task(struct task *t)
645 {
646 t->pre_select = audiod_pre_select;
647 t->post_select = audiod_post_select;
648 t->event_handler = NULL;
649 t->private_data = t;
650 sprintf(t->status, "audiod task");
651 }
652
653 static int parse_stream_command(const char *txt, char **cmd)
654 {
655 char *p = strchr(txt, ':');
656 int i;
657
658 if (!p)
659 return -E_MISSING_COLON;
660 p++;
661 FOR_EACH_AUDIO_FORMAT(i) {
662 if (strncmp(txt, audio_formats[i], strlen(audio_formats[i])))
663 continue;
664 *cmd = p;
665 return i;
666 }
667 return -E_UNSUPPORTED_AUDIO_FORMAT;
668 }
669
670 static int add_filter(int format, char *cmdline)
671 {
672 struct audio_format_info *a = &afi[format];
673 int filter_num, nf = a->num_filters;
674
675 filter_num = check_filter_arg(cmdline, &a->filter_conf[nf]);
676 if (filter_num < 0)
677 return filter_num;
678 a->filters[nf] = &filters[filter_num];
679 a->num_filters++;
680 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats[format], nf + 1,
681 a->filters[nf]->name);
682 return filter_num;
683 }
684
685 static int init_writers(void)
686 {
687 int i, ret, nw;
688 char *cmd;
689 struct audio_format_info *a;
690
691 init_supported_writers();
692 nw = PARA_MAX(1, conf.writer_given);
693 PARA_INFO_LOG("maximal number of writers: %d\n", nw);
694 FOR_EACH_AUDIO_FORMAT(i) {
695 a = &afi[i];
696 a->writer_conf = para_malloc(nw * sizeof(void *));
697 a->writers = para_malloc(nw * sizeof(struct writer *));
698 a->num_writers = 0;
699 }
700 for (i = 0; i < conf.writer_given; i++) {
701 void *wconf;
702 int writer_num;
703 ret = parse_stream_command(conf.writer_arg[i], &cmd);
704 if (ret < 0)
705 goto out;
706 a = &afi[ret];
707 nw = a->num_writers;
708 wconf = check_writer_arg(cmd, &writer_num);
709 if (!wconf) {
710 ret = writer_num;
711 goto out;
712 }
713 a->writers[nw] = &writers[writer_num];
714 a->writer_conf[nw] = wconf;
715 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats[ret],
716 nw, writer_names[writer_num]);
717 a->num_writers++;
718 }
719 ret = 1;
720 out:
721 return ret;
722 }
723
724 static int init_receivers(void)
725 {
726 int i, ret, receiver_num;
727 char *cmd = NULL;
728 struct audio_format_info *a;
729
730 for (i = 0; receivers[i].name; i++) {
731 PARA_INFO_LOG("initializing %s receiver\n", receivers[i].name);
732 receivers[i].init(&receivers[i]);
733 }
734 for (i = conf.receiver_given - 1; i >= 0; i--) {
735 char *arg = conf.receiver_arg[i];
736 char *recv_arg = strchr(arg, ':');
737 ret = -E_MISSING_COLON;
738 if (!recv_arg)
739 goto out;
740 *recv_arg = '\0';
741 recv_arg++;
742 ret = get_audio_format_num(arg);
743 if (ret < 0)
744 goto out;
745 afi[ret].receiver_conf = check_receiver_arg(recv_arg, &receiver_num);
746 if (!afi[ret].receiver_conf) {
747 ret = -E_RECV_SYNTAX;
748 goto out;
749 }
750 afi[ret].receiver = &receivers[receiver_num];
751 }
752 /* use the first available receiver with no arguments
753 * for those audio formats for which no receiver
754 * was specified
755 */
756 cmd = para_strdup(receivers[0].name);
757 FOR_EACH_AUDIO_FORMAT(i) {
758 a = &afi[i];
759 if (a->receiver_conf)
760 continue;
761 a->receiver_conf = check_receiver_arg(cmd, &receiver_num);
762 if (!a->receiver_conf)
763 return -E_RECV_SYNTAX;
764 a->receiver = &receivers[receiver_num];
765 }
766 ret = 1;
767 out:
768 free(cmd);
769 return ret;
770 }
771
772 static int init_default_filters(void)
773 {
774 int i, ret = 1;
775
776 FOR_EACH_AUDIO_FORMAT(i) {
777 struct audio_format_info *a = &afi[i];
778 char *tmp;
779 int j;
780
781 if (a->num_filters)
782 continue; /* no default -- nothing to to */
783 /* add "dec" to audio format name */
784 tmp = make_message("%sdec", audio_formats[i]);
785 for (j = 0; filters[j].name; j++)
786 if (!strcmp(tmp, filters[j].name))
787 break;
788 free(tmp);
789 ret = -E_UNSUPPORTED_FILTER;
790 if (!filters[j].name)
791 goto out;
792 tmp = para_strdup(filters[j].name);
793 ret = add_filter(i, tmp);
794 free(tmp);
795 if (ret < 0)
796 goto out;
797 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats[i],
798 filters[j].name);
799 }
800 out:
801 return ret;
802 }
803
804 static int init_filters(void)
805 {
806 int i, ret, nf;
807
808 filter_init(filters);
809 nf = PARA_MAX(1, conf.filter_given);
810 PARA_INFO_LOG("maximal number of filters: %d\n", nf);
811 FOR_EACH_AUDIO_FORMAT(i) {
812 afi[i].filter_conf = para_malloc(nf * sizeof(void *));
813 afi[i].filters = para_malloc(nf * sizeof(struct filter *));
814 }
815 if (!conf.no_default_filters_given)
816 return init_default_filters();
817 for (i = 0; i < conf.filter_given; i++) {
818 char *arg = conf.filter_arg[i];
819 char *filter_name = strchr(arg, ':');
820 ret = -E_MISSING_COLON;
821 if (!filter_name)
822 goto out;
823 *filter_name = '\0';
824 filter_name++;
825 ret = get_audio_format_num(arg);
826 if (ret < 0)
827 goto out;
828 ret = add_filter(ret, filter_name);
829 if (ret < 0)
830 goto out;
831 }
832 ret = init_default_filters(); /* use default values for the rest */
833 out:
834 return ret;
835 }
836
837 static int init_stream_io(void)
838 {
839 int ret;
840
841 ret = init_writers();
842 if (ret < 0)
843 return ret;
844 ret = init_receivers();
845 if (ret < 0)
846 return ret;
847 ret = init_filters();
848 if (ret < 0)
849 return ret;
850 return 1;
851 }
852
853 static int audiod_get_socket(void)
854 {
855 struct sockaddr_un unix_addr;
856 int fd;
857
858 if (conf.socket_given)
859 socket_name = para_strdup(conf.socket_arg);
860 else {
861 char *hn = para_hostname();
862 socket_name = make_message("/var/paraslash/audiod_socket.%s",
863 hn);
864 free(hn);
865 }
866 PARA_NOTICE_LOG("local socket: %s\n", socket_name);
867 if (conf.force_given)
868 unlink(socket_name);
869 fd = create_pf_socket(socket_name, &unix_addr,
870 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IWOTH);
871 if (fd < 0) {
872 PARA_EMERG_LOG("%s", "can not connect to socket\n");
873 exit(EXIT_FAILURE); /* do not unlink socket */
874 }
875 if (listen(fd , 5) < 0) {
876 PARA_EMERG_LOG("%s", "can not listen on socket\n");
877 exit(EXIT_FAILURE); /* do not unlink socket */
878 }
879 mark_fd_nonblock(fd);
880 return fd;
881 }
882
883 static void signal_event_handler(struct task *t)
884 {
885 struct signal_task *st = t->private_data;
886
887 switch (st->signum) {
888 case SIGINT:
889 case SIGTERM:
890 case SIGHUP:
891 PARA_EMERG_LOG("terminating on signal %d\n", st->signum);
892 clean_exit(EXIT_FAILURE, "caught deadly signal");
893 }
894 }
895
896 static void signal_pre_select(struct sched *s, struct task *t)
897 {
898 struct signal_task *st = t->private_data;
899 t->ret = 1;
900 para_fd_set(st->fd, &s->rfds, &s->max_fileno);
901 }
902
903 static void signal_post_select(struct sched *s, struct task *t)
904 {
905 struct signal_task *st = t->private_data;
906 t->ret = 1;
907 if (!FD_ISSET(st->fd, &s->rfds))
908 return;
909 t->ret = -E_SIGNAL_CAUGHT;
910 st->signum = para_next_signal();
911 }
912
913 static void signal_setup_default(struct signal_task *st)
914 {
915 st->task.pre_select = signal_pre_select;
916 st->task.post_select = signal_post_select;
917 st->task.private_data = st;
918 sprintf(st->task.status, "signal task");
919 }
920
921 static void command_pre_select(struct sched *s, struct task *t)
922 {
923 struct command_task *ct = t->private_data;
924 t->ret = 1;
925 para_fd_set(ct->fd, &s->rfds, &s->max_fileno);
926
927 }
928
929 static void command_post_select(struct sched *s, struct task *t)
930 {
931 int ret;
932 struct command_task *ct = t->private_data;
933
934 t->ret = 1; /* always successful */
935 audiod_status_dump();
936 if (!FD_ISSET(ct->fd, &s->rfds))
937 return;
938 ret = handle_connect(ct->fd);
939 if (ret < 0)
940 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
941 }
942
943 static void init_command_task(struct command_task *ct)
944 {
945 ct->task.pre_select = command_pre_select;
946 ct->task.post_select = command_post_select;
947 ct->task.event_handler = NULL;
948 ct->task.private_data = ct;
949 ct->fd = audiod_get_socket(); /* doesn't return on errors */
950 sprintf(ct->task.status, "command task");
951 }
952
953 static void close_stat_pipe(void)
954 {
955 int i;
956
957 if (!stat_task->pcd)
958 return;
959 client_close(stat_task->pcd);
960 stat_task->pcd = NULL;
961 for (i = 0; i < NUM_STAT_ITEMS; i++) {
962 free(stat_task->stat_item_values[i]);
963 stat_task->stat_item_values[i] = NULL;
964 }
965 dump_empty_status();
966 stat_task->length_seconds = 0;
967 stat_task->offset_seconds = 0;
968 audiod_status_dump();
969 stat_task->playing = 0;
970 stat_task->stat_item_values[SI_STATUS_BAR] = make_message(
971 "%s:no connection to para_server\n",
972 status_item_list[SI_STATUS_BAR]);
973 stat_client_write(stat_task->stat_item_values[SI_STATUS_BAR],
974 SI_STATUS_BAR);
975 if (stat_task->clock_diff_count) {
976 stat_task->clock_diff_barrier.tv_sec = now->tv_sec + 1;
977 stat_task->clock_diff_barrier.tv_usec = now->tv_usec;
978 }
979 }
980
981 /**
982 * close the connection to para_server and exit
983 *
984 * \param status the exit status which is passed to exit(3)
985 * \param msg the log message
986 *
987 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
988 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
989 * EXIT_FAILURE.
990 *
991 * \sa exit(3)
992 */
993 void __noreturn clean_exit(int status, const char *msg)
994 {
995 PARA_EMERG_LOG("%s\n", msg);
996 if (socket_name)
997 unlink(socket_name);
998 close_stat_pipe();
999 exit(status);
1000 }
1001
1002 /* avoid busy loop if server is down */
1003 static void set_stat_task_restart_barrier(void)
1004 {
1005 struct timeval delay = {5, 0};
1006 tv_add(now, &delay, &stat_task->restart_barrier);
1007 }
1008
1009 static void client_task_event_handler(__a_unused struct task *t)
1010 {
1011 int i;
1012
1013 if (t->ret == -E_HANDSHAKE_COMPLETE)
1014 return;
1015 unregister_task(t);
1016 close_stat_pipe();
1017 if (t->ret != -E_SERVER_EOF)
1018 stat_task->clock_diff_count = conf.clock_diff_count_arg;
1019 set_stat_task_restart_barrier();
1020 FOR_EACH_AUDIO_FORMAT(i)
1021 afi[i].restart_barrier = stat_task->restart_barrier;
1022 }
1023
1024 static void status_pre_select(struct sched *s, struct task *t)
1025 {
1026 struct status_task *st = t->private_data;
1027 int ret;
1028
1029 t->ret = 1; /* always successful */
1030 if (st->pcd || audiod_status == AUDIOD_OFF)
1031 return;
1032 if (!st->clock_diff_count && tv_diff(now, &st->restart_barrier, NULL)
1033 < 0)
1034 return;
1035 if (st->clock_diff_count) {
1036 char *argv[] = {"audiod", "stat", "1", NULL};
1037 int argc = 3;
1038 if (tv_diff(now, &st->clock_diff_barrier, NULL) < 0)
1039 return;
1040 PARA_INFO_LOG("clock diff count: %d\n", st->clock_diff_count);
1041 ret = client_open(argc, argv, &st->pcd);
1042
1043 } else {
1044 char *argv[] = {"audiod", "stat", NULL};
1045 int argc = 2;
1046 ret = client_open(argc, argv, &st->pcd);
1047 }
1048 set_stat_task_restart_barrier();
1049 if (ret < 0)
1050 return;
1051 st->pcd->task.event_handler = client_task_event_handler;
1052 s->timeout.tv_sec = 0;
1053 s->timeout.tv_usec = 1;
1054 }
1055
1056 static void status_post_select(__a_unused struct sched *s, struct task *t)
1057 {
1058 struct status_task *st = t->private_data;
1059 unsigned bytes_left;
1060
1061 t->ret = 1;
1062 if (!st->pcd || st->pcd->status != CL_RECEIVING)
1063 return;
1064 if (st->pcd && audiod_status == AUDIOD_OFF) {
1065 unregister_task(&st->pcd->task);
1066 close_stat_pipe();
1067 st->clock_diff_count = conf.clock_diff_count_arg;
1068 return;
1069 }
1070 bytes_left = for_each_line(st->pcd->buf, st->pcd->loaded,
1071 &check_stat_line);
1072 if (st->pcd->loaded != bytes_left) {
1073 st->last_status_read = *now;
1074 st->pcd->loaded = bytes_left;
1075 } else {
1076 struct timeval diff;
1077 tv_diff(now, &st->last_status_read, &diff);
1078 if (diff.tv_sec > 61)
1079 close_stat_pipe();
1080 }
1081 }
1082
1083 static void init_status_task(struct status_task *st)
1084 {
1085 memset(st, 0, sizeof(struct status_task));
1086 st->task.pre_select = status_pre_select;
1087 st->task.post_select = status_post_select;
1088 st->task.private_data = st;
1089 st->sa_time_diff_sign = 1;
1090 st->clock_diff_count = conf.clock_diff_count_arg;
1091 sprintf(st->task.status, "status task");
1092 }
1093
1094 static void set_initial_status(void)
1095 {
1096 audiod_status = AUDIOD_ON;
1097 if (!conf.mode_given)
1098 return;
1099 if (!strcmp(conf.mode_arg, "sb")) {
1100 audiod_status = AUDIOD_STANDBY;
1101 return;
1102 }
1103 if (!strcmp(conf.mode_arg, "off")) {
1104 audiod_status = AUDIOD_OFF;
1105 return;
1106 }
1107 if (strcmp(conf.mode_arg, "on"))
1108 PARA_WARNING_LOG("%s", "invalid mode\n");
1109 }
1110
1111 /**
1112 * the main function of para_audiod
1113 *
1114 * \param argc usual argument count
1115 * \param argv usual argument vector
1116 *
1117 * \return EXIT_SUCCESS or EXIT_FAILURE
1118 *
1119 * \sa para_audiod(1)
1120 * */
1121 int main(int argc, char *argv[])
1122 {
1123 char *cf;
1124 int ret, i;
1125 struct sched s;
1126 struct command_task command_task_struct, *cmd_task = &command_task_struct;
1127 struct task audiod_task_struct, *audiod_task = &audiod_task_struct;
1128
1129 valid_fd_012();
1130 audiod_cmdline_parser(argc, argv, &conf);
1131 HANDLE_VERSION_FLAG("audiod", conf);
1132 para_drop_privileges(conf.user_arg, conf.group_arg);
1133 cf = configfile_exists();
1134 if (cf) {
1135 if (audiod_cmdline_parser_configfile(cf, &conf, 0, 0, 0)) {
1136 PARA_EMERG_LOG("%s", "parse error in config file\n");
1137 exit(EXIT_FAILURE);
1138 }
1139 }
1140 if (conf.logfile_given)
1141 logfile = open_log(conf.logfile_arg);
1142 log_welcome("para_audiod", conf.loglevel_arg);
1143 i = init_stream_io();
1144 if (i < 0) {
1145 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i));
1146 exit(EXIT_FAILURE);
1147 }
1148 server_uptime(UPTIME_SET);
1149 set_initial_status();
1150 FOR_EACH_SLOT(i)
1151 clear_slot(i);
1152 init_grabbing();
1153 setup_signal_handling();
1154 signal_setup_default(sig_task);
1155 sig_task->task.event_handler = signal_event_handler;
1156
1157 init_status_task(stat_task);
1158 init_command_task(cmd_task);
1159 init_audiod_task(audiod_task);
1160
1161 if (conf.daemon_given)
1162 daemon_init();
1163
1164 register_task(&sig_task->task);
1165 register_task(&cmd_task->task);
1166 register_task(&stat_task->task);
1167 register_task(audiod_task);
1168 s.default_timeout.tv_sec = 0;
1169 s.default_timeout.tv_usec = 99 * 1000;
1170 ret = sched(&s);
1171
1172 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
1173 return EXIT_FAILURE;
1174 }