2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
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.
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.
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.
19 /** \file audiod.c the paraslash's audio daemon */
23 #include "audiod.cmdline.h"
28 #include "grab_client.cmdline.h"
29 #include "grab_client.h"
30 #include "client.cmdline.h"
38 #include "write_common.h"
41 /** define the array of error lists needed by para_audiod */
43 /** define the array containing all supported audio formats */
44 DEFINE_AUDIO_FORMAT_ARRAY
;
46 /** defines how to handle one supported audio format */
47 struct audio_format_info
{
48 /** pointer to the receiver for this audio format */
49 struct receiver
*receiver
;
50 /** the receiver configuration */
52 /** the number of filters that should be activated for this audio format */
53 unsigned int num_filters
;
54 /** pointer to the array of filters to be activated */
55 struct filter
**filters
;
56 /** pointer to the array of filter configurations */
58 /** the number of filters that should be activated for this audio format */
59 unsigned int num_writers
;
60 /** pointer to the array of writers to be activated */
61 struct writer
**writers
;
62 /** pointer to the array of writer configurations */
64 /** do not start receiver/filters/writer before this time */
65 struct timeval restart_barrier
;
68 struct slot_info slot
[MAX_STREAM_SLOTS
];
71 int audiod_status
= AUDIOD_ON
;
73 struct audiod_args_info conf
;
74 static char *socket_name
;
76 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
78 static struct signal_task signal_task_struct
, *sig_task
= &signal_task_struct
;
80 static struct status_task status_task_struct
;
81 struct status_task
*stat_task
= &status_task_struct
;
84 * the task for handling audiod commands
86 * \sa struct task, struct sched
89 /** the local listening socket */
91 /** the associated task structure */
96 * task for signal handling
99 /** the signal pipe */
101 /** the number of the most recent signal */
103 /** the associated task structure */
107 /** iterate over all supported audio formats */
108 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
111 * get the audio format number
112 * \param name the name of the audio format
114 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
115 * \a name is not a supported audio format.
117 int get_audio_format_num(char *name
)
120 FOR_EACH_AUDIO_FORMAT(i
)
121 if (!strcmp(name
, audio_formats
[i
]))
123 return -E_UNSUPPORTED_AUDIO_FORMAT
;
127 * log function. first argument is loglevel.
129 void para_log(int ll
, const char* fmt
,...)
135 char str
[MAXLINE
] = "";
136 static char *hostname
;
138 if (ll
< conf
.loglevel_arg
)
141 hostname
= para_hostname();
142 outfd
= logfile
? logfile
: stderr
;
145 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
146 fprintf(outfd
, "%s %s ", str
, hostname
);
147 if (conf
.loglevel_arg
<= INFO
)
148 fprintf(outfd
, "%i ", ll
);
150 vfprintf(outfd
, fmt
, argp
);
154 static char *configfile_exists(void)
156 static char *config_file
;
159 char *home
= para_homedir();
160 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
163 return file_exists(config_file
)? config_file
: NULL
;
166 static void setup_signal_handling(void)
168 sig_task
->fd
= para_signal_init();
169 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
170 para_install_sighandler(SIGINT
);
171 para_install_sighandler(SIGTERM
);
172 para_install_sighandler(SIGHUP
);
173 signal(SIGPIPE
, SIG_IGN
);
176 static void clear_slot(int slot_num
)
178 struct slot_info
*s
= &slot
[slot_num
];
180 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
181 memset(s
, 0, sizeof(struct slot_info
));
185 static void close_receiver(int slot_num
)
187 struct slot_info
*s
= &slot
[slot_num
];
188 struct audio_format_info
*a
;
189 const struct timeval restart_delay
= {0, 200 * 1000};
191 if (s
->format
< 0 || !s
->receiver_node
)
194 PARA_NOTICE_LOG("closing %s receiver in slot %d (eof = %d)\n",
195 audio_formats
[s
->format
] , slot_num
, s
->receiver_node
->eof
);
196 // if (!s->receiver_node->eof)
197 // unregister_task(&s->receiver_node->task);
198 a
->receiver
->close(s
->receiver_node
);
199 free(s
->receiver_node
);
200 s
->receiver_node
= NULL
;
201 /* set restart barrier */
202 tv_add(now
, &restart_delay
, &afi
[s
->format
].restart_barrier
);
205 static void kill_all_decoders(void)
210 struct slot_info
*s
= &slot
[i
];
211 if (s
->wng
&& !s
->wng
->eof
) {
212 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
214 wng_unregister(s
->wng
);
217 if (s
->fc
&& !s
->fc
->eof
) {
218 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i
);
219 unregister_task(&s
->fc
->task
);
222 if (s
->receiver_node
&& !s
->receiver_node
->eof
) {
223 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i
);
224 unregister_task(&s
->receiver_node
->task
);
225 s
->receiver_node
->eof
= 1;
230 static int get_empty_slot(void)
241 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
246 return -E_NO_MORE_SLOTS
;
249 static int decoder_running(int format
)
256 if (s
->format
== format
&& s
->receiver_node
)
258 if (s
->format
== format
&& s
->wng
)
264 static void close_stat_pipe(void)
270 client_close(stat_task
->pcd
);
271 stat_task
->pcd
= NULL
;
272 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
273 free(stat_task
->stat_item_values
[i
]);
274 stat_task
->stat_item_values
[i
] = NULL
;
277 stat_task
->length_seconds
= 0;
278 stat_task
->offset_seconds
= 0;
279 audiod_status_dump();
280 stat_task
->playing
= 0;
281 stat_task
->stat_item_values
[SI_STATUS_BAR
] = make_message(
282 "%s:no connection to para_server\n",
283 status_item_list
[SI_STATUS_BAR
]);
284 stat_client_write(stat_task
->stat_item_values
[SI_STATUS_BAR
], SI_STATUS_BAR
);
287 void __noreturn
clean_exit(int status
, const char *msg
)
289 PARA_EMERG_LOG("%s\n", msg
);
297 * get the number of filters
299 * \param audio_format_num the number identifying the audio format
301 * \return the number of filters for the given audio format
305 int num_filters(int audio_format_num
)
307 return afi
[audio_format_num
].num_filters
;
310 static void filter_event_handler(struct task
*t
)
312 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
313 struct filter_chain
*fc
= t
->private_data
;
318 static void open_filters(int slot_num
)
320 struct slot_info
*s
= &slot
[slot_num
];
321 struct audio_format_info
*a
= &afi
[s
->format
];
322 int nf
= a
->num_filters
;
328 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
329 s
->fc
= para_calloc(sizeof(struct filter_chain
));
330 INIT_LIST_HEAD(&s
->fc
->filters
);
331 s
->fc
->inbuf
= s
->receiver_node
->buf
;
332 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
333 s
->fc
->input_eof
= &s
->receiver_node
->eof
;
334 s
->fc
->task
.pre_select
= filter_pre_select
;
335 s
->fc
->task
.event_handler
= filter_event_handler
;
336 s
->fc
->task
.private_data
= s
->fc
;
339 s
->receiver_node
->output_eof
= &s
->fc
->eof
;
340 sprintf(s
->fc
->task
.status
, "filter chain");
341 for (i
= 0; i
< nf
; i
++) {
342 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
343 fn
->conf
= a
->filter_conf
[i
];
345 fn
->filter
= a
->filters
[i
];
346 INIT_LIST_HEAD(&fn
->callbacks
);
347 list_add_tail(&fn
->node
, &s
->fc
->filters
);
348 fn
->filter
->open(fn
);
349 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
350 audio_formats
[s
->format
], i
+ 1, nf
,
351 fn
->filter
->name
, slot_num
);
352 s
->fc
->outbuf
= fn
->buf
;
353 s
->fc
->out_loaded
= &fn
->loaded
;
355 register_task(&s
->fc
->task
);
358 static void wng_event_handler(struct task
*t
)
360 struct writer_node_group
*wng
= t
->private_data
;
362 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
367 static void open_writers(int slot_num
)
370 struct slot_info
*s
= &slot
[slot_num
];
371 struct audio_format_info
*a
= &afi
[s
->format
];
373 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
375 s
->wng
= setup_default_wng();
377 s
->wng
= wng_new(a
->num_writers
);
379 s
->wng
->buf
= s
->fc
->outbuf
;
380 s
->wng
->loaded
= s
->fc
->out_loaded
;
381 s
->wng
->input_eof
= &s
->fc
->eof
;
382 s
->wng
->channels
= &s
->fc
->channels
;
383 s
->wng
->samplerate
= &s
->fc
->samplerate
;
384 s
->fc
->output_eof
= &s
->wng
->eof
;
385 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
387 s
->wng
->buf
= s
->receiver_node
->buf
;
388 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
389 s
->wng
->input_eof
= &s
->receiver_node
->eof
;
391 s
->wng
->task
.event_handler
= wng_event_handler
;
392 for (i
= 0; i
< a
->num_writers
; i
++) {
393 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
394 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
396 ret
= wng_open(s
->wng
);
398 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
402 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fc
->filters
);
405 static void rn_event_handler(struct task
*t
)
407 struct receiver_node
*rn
= t
->private_data
;
409 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
414 static void open_receiver(int format
)
416 struct audio_format_info
*a
= &afi
[format
];
419 struct receiver_node
*rn
;
421 slot_num
= get_empty_slot();
423 clean_exit(EXIT_FAILURE
, PARA_STRERROR(-slot_num
));
426 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
427 rn
= s
->receiver_node
;
428 rn
->receiver
= a
->receiver
;
429 rn
->conf
= a
->receiver_conf
;
430 ret
= a
->receiver
->open(s
->receiver_node
);
432 PARA_ERROR_LOG("failed to open receiver (%s)\n",
433 PARA_STRERROR(-ret
));
434 free(s
->receiver_node
);
435 s
->receiver_node
= NULL
;
438 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
439 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
440 rn
->task
.private_data
= s
->receiver_node
;
441 rn
->task
.pre_select
= a
->receiver
->pre_select
;
442 rn
->task
.post_select
= a
->receiver
->post_select
;
443 rn
->task
.event_handler
= rn_event_handler
;
444 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
445 register_task(&rn
->task
);
448 static int open_current_receiver(struct sched
*s
)
453 if (!stat_task
->af_status
|| !stat_task
->pcd
)
455 i
= get_audio_format_num(stat_task
->af_status
);
458 if (decoder_running(i
))
460 if (tv_diff(now
, &afi
[i
].restart_barrier
, &diff
) < 0) {
468 static void compute_time_diff(const struct timeval
*status_time
)
470 struct timeval tmp
, diff
;
472 int sign
, sa_time_diff_sign
= stat_task
->sa_time_diff_sign
;
473 const struct timeval max_deviation
= {0, 500 * 1000};
474 const int time_smooth
= 5;
476 sign
= tv_diff(status_time
, now
, &diff
);
477 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
478 // sign, sa_time_diff_sign);
480 sa_time_diff_sign
= sign
;
481 stat_task
->sa_time_diff
= diff
;
486 int s
= tv_diff(&diff
, &stat_task
->sa_time_diff
, &tmp
);
487 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
488 PARA_WARNING_LOG("time diff jump: %lims\n",
492 sa_time_diff_sign
= tv_convex_combination(
493 sa_time_diff_sign
* time_smooth
, &stat_task
->sa_time_diff
,
494 count
> 10? sign
: sign
* time_smooth
, &diff
,
496 stat_task
->sa_time_diff
= tmp
;
497 PARA_DEBUG_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
500 sa_time_diff_sign
? "+" : "-",
501 tv2ms(&stat_task
->sa_time_diff
)
505 static void check_stat_line(char *line
)
509 long unsigned sec
, usec
;
512 // PARA_INFO_LOG("line: %s\n", line);
515 itemnum
= stat_line_valid(line
);
517 PARA_WARNING_LOG("invalid status line: %s\n", line
);
520 tmp
= make_message("%s\n", line
);
521 stat_client_write(tmp
, itemnum
);
523 free(stat_task
->stat_item_values
[itemnum
]);
524 stat_task
->stat_item_values
[itemnum
] = para_strdup(line
);
525 ilen
= strlen(status_item_list
[itemnum
]);
528 stat_task
->playing
= strstr(line
, "playing")? 1 : 0;
531 free(stat_task
->af_status
);
532 stat_task
->af_status
= para_strdup(line
+ ilen
+ 1);
535 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
538 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
540 case SI_STREAM_START
:
541 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
542 stat_task
->server_stream_start
.tv_sec
= sec
;
543 stat_task
->server_stream_start
.tv_usec
= usec
;
546 case SI_CURRENT_TIME
:
547 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
548 struct timeval tv
= {sec
, usec
};
549 compute_time_diff(&tv
);
555 static void handle_signal(int sig
)
561 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
562 clean_exit(EXIT_FAILURE
, "caught deadly signal");
567 static void try_to_close_slot(int slot_num
)
569 struct slot_info
*s
= &slot
[slot_num
];
573 if (s
->receiver_node
&& !s
->receiver_node
->eof
)
575 if (s
->fc
&& !s
->fc
->eof
)
577 if (s
->wng
&& !s
->wng
->eof
)
579 PARA_INFO_LOG("closing slot %d \n", slot_num
);
581 close_filters(s
->fc
);
583 close_receiver(slot_num
);
584 clear_slot(slot_num
);
588 * Check if any receivers/filters/writers need to be started and do so if
589 * neccessary. Since the pre_select function didn't have a chance yet to put
590 * file descriptors into the fd sets given by s, make the upcoming select()
591 * return immediately to avoid a long timeout in case we started something.
593 static void audiod_pre_select(struct sched
*s
, __a_unused
struct task
*t
)
596 struct timeval min_delay
= {0, 1};
599 if (audiod_status
!= AUDIOD_ON
|| !stat_task
->playing
)
600 return kill_all_decoders();
601 if (open_current_receiver(s
))
602 s
->timeout
= min_delay
;
604 struct slot_info
*sl
= &slot
[i
];
605 struct audio_format_info
*a
;
609 a
= &afi
[sl
->format
];
610 if (!sl
->receiver_node
)
612 if (!a
->num_filters
) {
613 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
615 s
->timeout
= min_delay
;
619 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
621 s
->timeout
= min_delay
;
624 if (sl
->fc
&& *sl
->fc
->out_loaded
&& !sl
->wng
) {
626 s
->timeout
= min_delay
;
631 static void audiod_post_select(__a_unused
struct sched
*s
,
632 __a_unused
struct task
*t
)
636 /* save away the current time for other users */
639 try_to_close_slot(i
);
642 static void init_audiod_task(struct task
*t
)
644 t
->pre_select
= audiod_pre_select
;
645 t
->post_select
= audiod_post_select
;
646 t
->event_handler
= NULL
;
648 sprintf(t
->status
, "audiod task");
651 static int parse_stream_command(const char *txt
, char **cmd
)
653 char *p
= strchr(txt
, ':');
657 return -E_MISSING_COLON
;
659 FOR_EACH_AUDIO_FORMAT(i
) {
660 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
665 return -E_UNSUPPORTED_AUDIO_FORMAT
;
668 static int add_filter(int format
, char *cmdline
)
670 struct audio_format_info
*a
= &afi
[format
];
671 int filter_num
, nf
= a
->num_filters
;
673 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
676 a
->filters
[nf
] = &filters
[filter_num
];
678 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
679 a
->filters
[nf
]->name
);
683 static int init_writers(void)
687 struct audio_format_info
*a
;
689 init_supported_writers();
690 nw
= PARA_MAX(1, conf
.writer_given
);
691 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
692 FOR_EACH_AUDIO_FORMAT(i
) {
694 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
695 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
698 for (i
= 0; i
< conf
.writer_given
; i
++) {
701 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
706 wconf
= check_writer_arg(cmd
, &writer_num
);
711 a
->writers
[nw
] = &writers
[writer_num
];
712 a
->writer_conf
[nw
] = wconf
;
713 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
714 nw
, writer_names
[writer_num
]);
722 static int init_receivers(void)
724 int i
, ret
, receiver_num
;
726 struct audio_format_info
*a
;
728 for (i
= 0; receivers
[i
].name
; i
++) {
729 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
730 receivers
[i
].init(&receivers
[i
]);
732 for (i
= 0; i
< conf
.receiver_given
; i
++) {
733 char *arg
= conf
.receiver_arg
[i
];
734 char *recv
= strchr(arg
, ':');
735 ret
= -E_MISSING_COLON
;
740 ret
= get_audio_format_num(arg
);
743 afi
[ret
].receiver_conf
= check_receiver_arg(recv
, &receiver_num
);
744 if (!afi
[ret
].receiver_conf
) {
745 ret
= -E_RECV_SYNTAX
;
748 afi
[ret
].receiver
= &receivers
[receiver_num
];
750 /* use the first available receiver with no arguments
751 * for those audio formats for which no receiver
754 cmd
= para_strdup(receivers
[0].name
);
755 FOR_EACH_AUDIO_FORMAT(i
) {
757 if (a
->receiver_conf
)
759 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
760 if (!a
->receiver_conf
)
761 return -E_RECV_SYNTAX
;
762 a
->receiver
= &receivers
[receiver_num
];
770 static int init_default_filters(void)
774 FOR_EACH_AUDIO_FORMAT(i
) {
775 struct audio_format_info
*a
= &afi
[i
];
780 continue; /* no default -- nothing to to */
781 /* add "dec" to audio format name */
782 tmp
= make_message("%sdec", audio_formats
[i
]);
783 for (j
= 0; filters
[j
].name
; j
++)
784 if (!strcmp(tmp
, filters
[j
].name
))
787 ret
= -E_UNSUPPORTED_FILTER
;
788 if (!filters
[j
].name
)
790 tmp
= para_strdup(filters
[j
].name
);
791 ret
= add_filter(i
, tmp
);
795 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
802 static int init_filters(void)
806 filter_init(filters
);
807 nf
= PARA_MAX(1, conf
.filter_given
);
808 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
809 FOR_EACH_AUDIO_FORMAT(i
) {
810 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
811 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
813 if (!conf
.no_default_filters_given
)
814 return init_default_filters();
815 for (i
= 0; i
< conf
.filter_given
; i
++) {
816 char *arg
= conf
.filter_arg
[i
];
817 char *filter_name
= strchr(arg
, ':');
818 ret
= -E_MISSING_COLON
;
823 ret
= get_audio_format_num(arg
);
826 ret
= add_filter(ret
, filter_name
);
830 ret
= init_default_filters(); /* use default values for the rest */
835 static int init_stream_io(void)
839 ret
= init_writers();
842 ret
= init_receivers();
845 ret
= init_filters();
851 static int audiod_get_socket(void)
853 struct sockaddr_un unix_addr
;
856 if (conf
.socket_given
)
857 socket_name
= para_strdup(conf
.socket_arg
);
859 char *hn
= para_hostname();
860 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
864 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
865 if (conf
.force_given
)
867 fd
= create_pf_socket(socket_name
, &unix_addr
,
868 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
870 PARA_EMERG_LOG("%s", "can not connect to socket\n");
871 exit(EXIT_FAILURE
); /* do not unlink socket */
873 if (listen(fd
, 5) < 0) {
874 PARA_EMERG_LOG("%s", "can not listen on socket\n");
875 exit(EXIT_FAILURE
); /* do not unlink socket */
877 mark_fd_nonblock(fd
);
881 static void signal_event_handler(struct task
*t
)
883 struct signal_task
*st
= t
->private_data
;
885 if (t
->ret
!= -E_SIGNAL_CAUGHT
)
886 PARA_ERROR_LOG("%s (ignored)\n", PARA_STRERROR(-t
->ret
));
888 handle_signal(st
->signum
);
891 static void signal_pre_select(struct sched
*s
, struct task
*t
)
893 struct signal_task
*st
= t
->private_data
;
895 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
898 static void signal_post_select(struct sched
*s
, struct task
*t
)
900 struct signal_task
*st
= t
->private_data
;
902 if (!FD_ISSET(st
->fd
, &s
->rfds
))
904 t
->ret
= -E_SIGNAL_CAUGHT
;
905 st
->signum
= para_next_signal();
908 static void signal_setup_default(struct signal_task
*st
)
910 st
->task
.pre_select
= signal_pre_select
;
911 st
->task
.post_select
= signal_post_select
;
912 st
->task
.private_data
= st
;
913 sprintf(st
->task
.status
, "signal task");
916 static void command_pre_select(struct sched
*s
, struct task
*t
)
918 struct command_task
*ct
= t
->private_data
;
920 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
924 static void command_post_select(struct sched
*s
, struct task
*t
)
927 struct command_task
*ct
= t
->private_data
;
929 t
->ret
= 1; /* always successful */
930 if (audiod_status
!= AUDIOD_OFF
)
931 audiod_status_dump();
932 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
934 ret
= handle_connect(ct
->fd
);
936 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
939 static void init_command_task(struct command_task
*ct
)
941 ct
->task
.pre_select
= command_pre_select
;
942 ct
->task
.post_select
= command_post_select
;
943 ct
->task
.event_handler
= NULL
;
944 ct
->task
.private_data
= ct
;
945 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
946 sprintf(ct
->task
.status
, "command task");
949 static void client_task_event_handler(__a_unused
struct task
*t
)
951 struct private_client_data
*pcd
= t
->private_data
;
952 if (t
->ret
== -E_HANDSHAKE_COMPLETE
)
958 static void status_event_handler(__a_unused
struct task
*t
)
960 struct timeval delay
= {1, 0};
962 struct status_task
*st
= t
->private_data
;
964 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
966 /* avoid busy loop if server is down */
967 tv_add(now
, &delay
, &st
->restart_barrier
);
968 FOR_EACH_AUDIO_FORMAT(i
)
969 afi
[i
].restart_barrier
= st
->restart_barrier
;
972 static void status_pre_select(struct sched
*s
, struct task
*t
)
974 struct status_task
*st
= t
->private_data
;
976 char *argv
[] = {"audiod", "stat", NULL
};
978 if (st
->pcd
&& (audiod_status
== AUDIOD_OFF
|| st
->pcd
->eof
))
980 if (!st
->pcd
&& audiod_status
!= AUDIOD_OFF
981 && tv_diff(now
, &st
->restart_barrier
, NULL
) > 0) {
982 t
->ret
= client_parse_config(argc
, argv
, &st
->pcd
);
985 t
->ret
= client_open(st
->pcd
);
988 st
->pcd
->task
.event_handler
= client_task_event_handler
;
989 s
->timeout
.tv_sec
= 0;
990 s
->timeout
.tv_usec
= 1;
994 static void status_post_select(__a_unused
struct sched
*s
, struct task
*t
)
996 struct status_task
*st
= t
->private_data
;
999 if (!st
->pcd
|| !st
->pcd
->loaded
1000 || st
->pcd
->status
!= CL_RECEIVING
)
1002 st
->pcd
->loaded
= for_each_line(st
->pcd
->buf
, st
->pcd
->loaded
,
1006 static void init_status_task(struct status_task
*st
)
1008 memset(st
, 0, sizeof(struct status_task
));
1009 st
->task
.pre_select
= status_pre_select
;
1010 st
->task
.post_select
= status_post_select
;
1011 st
->task
.event_handler
= status_event_handler
;
1012 st
->task
.private_data
= st
;
1013 st
->sa_time_diff_sign
= 1;
1014 sprintf(st
->task
.status
, "status task");
1017 static void set_initial_status(void)
1019 audiod_status
= AUDIOD_ON
;
1020 if (!conf
.mode_given
)
1022 if (!strcmp(conf
.mode_arg
, "sb")) {
1023 audiod_status
= AUDIOD_STANDBY
;
1026 if (!strcmp(conf
.mode_arg
, "off")) {
1027 audiod_status
= AUDIOD_OFF
;
1030 if (strcmp(conf
.mode_arg
, "on"))
1031 PARA_WARNING_LOG("%s", "invalid mode\n");
1034 int main(int argc
, char *argv
[])
1039 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1040 struct task audiod_task_struct
, *audiod_task
= &audiod_task_struct
;
1043 audiod_cmdline_parser(argc
, argv
, &conf
);
1044 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1045 cf
= configfile_exists();
1047 if (audiod_cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
1048 PARA_EMERG_LOG("%s", "parse error in config file\n");
1052 if (conf
.logfile_given
)
1053 logfile
= open_log(conf
.logfile_arg
);
1054 log_welcome("para_audiod", conf
.loglevel_arg
);
1055 i
= init_stream_io();
1057 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1060 server_uptime(UPTIME_SET
);
1061 set_initial_status();
1065 setup_signal_handling();
1066 signal_setup_default(sig_task
);
1067 sig_task
->task
.event_handler
= signal_event_handler
;
1069 init_status_task(stat_task
);
1070 init_command_task(cmd_task
);
1071 init_audiod_task(audiod_task
);
1073 if (conf
.daemon_given
)
1076 register_task(&sig_task
->task
);
1077 register_task(&cmd_task
->task
);
1078 register_task(&stat_task
->task
);
1079 register_task(audiod_task
);
1080 s
.default_timeout
.tv_sec
= 0;
1081 s
.default_timeout
.tv_usec
= 99 * 1000;
1084 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
1085 return EXIT_FAILURE
;