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