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