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
;
82 static struct timeval initial_delay_barrier
;
85 * the task for handling audiod commands
87 * \sa struct task, struct sched
90 /** the local listening socket */
92 /** the associated task structure */
97 * task for signal handling
100 /** the signal pipe */
102 /** the number of the most recent signal */
104 /** the associated task structure */
108 /** iterate over all supported audio formats */
109 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
112 * get the audio format number
113 * \param name the name of the audio format
115 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
116 * \a name is not a supported audio format.
118 int get_audio_format_num(char *name
)
121 FOR_EACH_AUDIO_FORMAT(i
)
122 if (!strcmp(name
, audio_formats
[i
]))
124 return -E_UNSUPPORTED_AUDIO_FORMAT
;
128 * log function. first argument is loglevel.
130 void para_log(int ll
, const char* fmt
,...)
136 char str
[MAXLINE
] = "";
137 static char *hostname
;
139 if (ll
< conf
.loglevel_arg
)
142 hostname
= para_hostname();
143 outfd
= logfile
? logfile
: stderr
;
146 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
147 fprintf(outfd
, "%s %s ", str
, hostname
);
148 if (conf
.loglevel_arg
<= INFO
)
149 fprintf(outfd
, "%i ", ll
);
151 vfprintf(outfd
, fmt
, argp
);
155 static char *configfile_exists(void)
157 static char *config_file
;
160 char *home
= para_homedir();
161 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
164 return file_exists(config_file
)? config_file
: NULL
;
167 static void setup_signal_handling(void)
169 sig_task
->fd
= para_signal_init();
170 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
171 para_install_sighandler(SIGINT
);
172 para_install_sighandler(SIGTERM
);
173 para_install_sighandler(SIGHUP
);
174 signal(SIGPIPE
, SIG_IGN
);
177 static void clear_slot(int slot_num
)
179 struct slot_info
*s
= &slot
[slot_num
];
181 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
182 memset(s
, 0, sizeof(struct slot_info
));
186 static void close_receiver(int slot_num
)
188 struct slot_info
*s
= &slot
[slot_num
];
189 struct audio_format_info
*a
;
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 a
->receiver
->close(s
->receiver_node
);
197 free(s
->receiver_node
);
198 s
->receiver_node
= NULL
;
201 static void kill_all_decoders(void)
206 struct slot_info
*s
= &slot
[i
];
207 if (s
->wng
&& !s
->wng
->eof
) {
208 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
210 wng_unregister(s
->wng
);
213 if (s
->fc
&& !s
->fc
->eof
) {
214 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i
);
215 unregister_task(&s
->fc
->task
);
218 if (s
->receiver_node
&& !s
->receiver_node
->eof
) {
219 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i
);
220 unregister_task(&s
->receiver_node
->task
);
221 s
->receiver_node
->eof
= 1;
226 static int get_empty_slot(void)
237 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
242 return -E_NO_MORE_SLOTS
;
245 static void close_stat_pipe(void)
251 client_close(stat_task
->pcd
);
252 stat_task
->pcd
= NULL
;
253 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
254 free(stat_task
->stat_item_values
[i
]);
255 stat_task
->stat_item_values
[i
] = NULL
;
258 stat_task
->length_seconds
= 0;
259 stat_task
->offset_seconds
= 0;
260 audiod_status_dump();
261 stat_task
->playing
= 0;
262 stat_task
->stat_item_values
[SI_STATUS_BAR
] = make_message(
263 "%s:no connection to para_server\n",
264 status_item_list
[SI_STATUS_BAR
]);
265 stat_client_write(stat_task
->stat_item_values
[SI_STATUS_BAR
], SI_STATUS_BAR
);
268 void __noreturn
clean_exit(int status
, const char *msg
)
270 PARA_EMERG_LOG("%s\n", msg
);
278 * get the number of filters
280 * \param audio_format_num the number identifying the audio format
282 * \return the number of filters for the given audio format
286 int num_filters(int audio_format_num
)
288 return afi
[audio_format_num
].num_filters
;
291 static void filter_event_handler(struct task
*t
)
293 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
294 struct filter_chain
*fc
= t
->private_data
;
299 static void open_filters(int slot_num
)
301 struct slot_info
*s
= &slot
[slot_num
];
302 struct audio_format_info
*a
= &afi
[s
->format
];
303 int nf
= a
->num_filters
;
309 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
310 s
->fc
= para_calloc(sizeof(struct filter_chain
));
311 INIT_LIST_HEAD(&s
->fc
->filters
);
312 s
->fc
->inbuf
= s
->receiver_node
->buf
;
313 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
314 s
->fc
->input_eof
= &s
->receiver_node
->eof
;
315 s
->fc
->task
.pre_select
= filter_pre_select
;
316 s
->fc
->task
.event_handler
= filter_event_handler
;
317 s
->fc
->task
.private_data
= s
->fc
;
320 s
->receiver_node
->output_eof
= &s
->fc
->eof
;
321 sprintf(s
->fc
->task
.status
, "filter chain");
322 for (i
= 0; i
< nf
; i
++) {
323 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
324 fn
->conf
= a
->filter_conf
[i
];
326 fn
->filter
= a
->filters
[i
];
327 INIT_LIST_HEAD(&fn
->callbacks
);
328 list_add_tail(&fn
->node
, &s
->fc
->filters
);
329 fn
->filter
->open(fn
);
330 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
331 audio_formats
[s
->format
], i
+ 1, nf
,
332 fn
->filter
->name
, slot_num
);
333 s
->fc
->outbuf
= fn
->buf
;
334 s
->fc
->out_loaded
= &fn
->loaded
;
336 register_task(&s
->fc
->task
);
339 static void wng_event_handler(struct task
*t
)
341 struct writer_node_group
*wng
= t
->private_data
;
343 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
348 static void open_writers(int slot_num
)
351 struct slot_info
*s
= &slot
[slot_num
];
352 struct audio_format_info
*a
= &afi
[s
->format
];
354 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
356 s
->wng
= setup_default_wng();
358 s
->wng
= wng_new(a
->num_writers
);
360 s
->wng
->buf
= s
->fc
->outbuf
;
361 s
->wng
->loaded
= s
->fc
->out_loaded
;
362 s
->wng
->input_eof
= &s
->fc
->eof
;
363 s
->wng
->channels
= &s
->fc
->channels
;
364 s
->wng
->samplerate
= &s
->fc
->samplerate
;
365 s
->fc
->output_eof
= &s
->wng
->eof
;
366 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
368 s
->wng
->buf
= s
->receiver_node
->buf
;
369 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
370 s
->wng
->input_eof
= &s
->receiver_node
->eof
;
372 s
->wng
->task
.event_handler
= wng_event_handler
;
373 for (i
= 0; i
< a
->num_writers
; i
++) {
374 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
375 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
377 ret
= wng_open(s
->wng
);
379 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
383 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fc
->filters
);
386 static void rn_event_handler(struct task
*t
)
388 struct receiver_node
*rn
= t
->private_data
;
389 const struct timeval restart_delay
= {0, 10 * 1000};
392 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
395 /* set restart barrier */
397 if (slot
[i
].receiver_node
!= rn
)
399 tv_add(now
, &restart_delay
, &afi
[slot
[i
].format
].restart_barrier
);
403 static void open_receiver(int format
)
405 struct audio_format_info
*a
= &afi
[format
];
408 struct receiver_node
*rn
;
410 slot_num
= get_empty_slot();
412 clean_exit(EXIT_FAILURE
, PARA_STRERROR(-slot_num
));
415 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
416 rn
= s
->receiver_node
;
417 rn
->receiver
= a
->receiver
;
418 rn
->conf
= a
->receiver_conf
;
419 ret
= a
->receiver
->open(s
->receiver_node
);
421 PARA_ERROR_LOG("failed to open receiver (%s)\n",
422 PARA_STRERROR(-ret
));
423 free(s
->receiver_node
);
424 s
->receiver_node
= NULL
;
427 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
428 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
429 rn
->task
.private_data
= s
->receiver_node
;
430 rn
->task
.pre_select
= a
->receiver
->pre_select
;
431 rn
->task
.post_select
= a
->receiver
->post_select
;
432 rn
->task
.event_handler
= rn_event_handler
;
433 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
434 register_task(&rn
->task
);
437 static int receiver_running(int format
)
442 struct slot_info
*s
= &slot
[i
];
443 if (s
->format
== format
&& s
->receiver_node
444 && !s
->receiver_node
->eof
)
450 static int open_current_receiver(struct sched
*s
)
455 if (!stat_task
->af_status
|| !stat_task
->pcd
)
457 i
= get_audio_format_num(stat_task
->af_status
);
460 if (receiver_running(i
))
462 if (tv_diff(now
, &afi
[i
].restart_barrier
, &diff
) < 0) {
470 static void compute_time_diff(const struct timeval
*status_time
)
472 struct timeval tmp
, diff
;
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;
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);
482 sa_time_diff_sign
= sign
;
483 stat_task
->sa_time_diff
= diff
;
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",
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
,
498 stat_task
->sa_time_diff
= tmp
;
499 PARA_DEBUG_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
502 sa_time_diff_sign
? "+" : "-",
503 tv2ms(&stat_task
->sa_time_diff
)
507 static void check_stat_line(char *line
)
511 long unsigned sec
, usec
;
514 // PARA_INFO_LOG("line: %s\n", line);
517 itemnum
= stat_line_valid(line
);
519 PARA_WARNING_LOG("invalid status line: %s\n", line
);
522 tmp
= make_message("%s\n", line
);
523 stat_client_write(tmp
, itemnum
);
525 free(stat_task
->stat_item_values
[itemnum
]);
526 stat_task
->stat_item_values
[itemnum
] = para_strdup(line
);
527 ilen
= strlen(status_item_list
[itemnum
]);
530 stat_task
->playing
= strstr(line
, "playing")? 1 : 0;
533 free(stat_task
->af_status
);
534 stat_task
->af_status
= para_strdup(line
+ ilen
+ 1);
537 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
540 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
542 case SI_STREAM_START
:
543 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
544 struct timeval tmp
, 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
, &tmp
);
553 tv_diff(&stat_task
->server_stream_start
,
554 &stat_task
->sa_time_diff
, &tmp
);
555 tv_add(&tmp
, &delay
, &initial_delay_barrier
);
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
);
567 static void handle_signal(int sig
)
573 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
574 clean_exit(EXIT_FAILURE
, "caught deadly signal");
579 static void try_to_close_slot(int slot_num
)
581 struct slot_info
*s
= &slot
[slot_num
];
585 if (s
->receiver_node
&& !s
->receiver_node
->eof
)
587 if (s
->fc
&& !s
->fc
->eof
)
589 if (s
->wng
&& !s
->wng
->eof
)
591 PARA_INFO_LOG("closing slot %d \n", slot_num
);
593 close_filters(s
->fc
);
595 close_receiver(slot_num
);
596 clear_slot(slot_num
);
600 * Check if any receivers/filters/writers need to be started and do so if
601 * neccessary. Since the pre_select function didn't have a chance yet to put
602 * file descriptors into the fd sets given by s, make the upcoming select()
603 * return immediately to avoid a long timeout in case we started something.
605 static void audiod_pre_select(struct sched
*s
, __a_unused
struct task
*t
)
608 struct timeval min_delay
= {0, 1};
611 if (audiod_status
!= AUDIOD_ON
|| !stat_task
->playing
)
612 return kill_all_decoders();
613 if (open_current_receiver(s
))
614 s
->timeout
= min_delay
;
616 struct slot_info
*sl
= &slot
[i
];
617 struct audio_format_info
*a
;
622 a
= &afi
[sl
->format
];
623 if (!sl
->receiver_node
)
625 if (!a
->num_filters
) {
626 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
628 s
->timeout
= min_delay
;
632 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
634 s
->timeout
= min_delay
;
637 if (!sl
->fc
|| !*sl
->fc
->out_loaded
|| sl
->wng
)
639 if (tv_diff(now
, &initial_delay_barrier
, &diff
) > 0) {
640 PARA_INFO_LOG("barrier: %lu:%lu, now: %lu, %lu\n",
641 initial_delay_barrier
.tv_sec
,
642 initial_delay_barrier
.tv_usec
,
643 now
->tv_sec
, now
->tv_usec
);
645 s
->timeout
= min_delay
;
648 PARA_INFO_LOG("inital delay: %lu ms left\n", tv2ms(&diff
));
649 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0) {
655 static void audiod_post_select(__a_unused
struct sched
*s
,
656 __a_unused
struct task
*t
)
660 /* save away the current time for other users */
663 try_to_close_slot(i
);
666 static void init_audiod_task(struct task
*t
)
668 t
->pre_select
= audiod_pre_select
;
669 t
->post_select
= audiod_post_select
;
670 t
->event_handler
= NULL
;
672 sprintf(t
->status
, "audiod task");
675 static int parse_stream_command(const char *txt
, char **cmd
)
677 char *p
= strchr(txt
, ':');
681 return -E_MISSING_COLON
;
683 FOR_EACH_AUDIO_FORMAT(i
) {
684 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
689 return -E_UNSUPPORTED_AUDIO_FORMAT
;
692 static int add_filter(int format
, char *cmdline
)
694 struct audio_format_info
*a
= &afi
[format
];
695 int filter_num
, nf
= a
->num_filters
;
697 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
700 a
->filters
[nf
] = &filters
[filter_num
];
702 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
703 a
->filters
[nf
]->name
);
707 static int init_writers(void)
711 struct audio_format_info
*a
;
713 init_supported_writers();
714 nw
= PARA_MAX(1, conf
.writer_given
);
715 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
716 FOR_EACH_AUDIO_FORMAT(i
) {
718 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
719 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
722 for (i
= 0; i
< conf
.writer_given
; i
++) {
725 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
730 wconf
= check_writer_arg(cmd
, &writer_num
);
735 a
->writers
[nw
] = &writers
[writer_num
];
736 a
->writer_conf
[nw
] = wconf
;
737 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
738 nw
, writer_names
[writer_num
]);
746 static int init_receivers(void)
748 int i
, ret
, receiver_num
;
750 struct audio_format_info
*a
;
752 for (i
= 0; receivers
[i
].name
; i
++) {
753 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
754 receivers
[i
].init(&receivers
[i
]);
756 for (i
= conf
.receiver_given
- 1; i
>= 0; i
--) {
757 char *arg
= conf
.receiver_arg
[i
];
758 char *recv
= strchr(arg
, ':');
759 PARA_INFO_LOG("arg: %s\n", arg
);
760 ret
= -E_MISSING_COLON
;
765 ret
= get_audio_format_num(arg
);
768 afi
[ret
].receiver_conf
= check_receiver_arg(recv
, &receiver_num
);
769 if (!afi
[ret
].receiver_conf
) {
770 ret
= -E_RECV_SYNTAX
;
773 afi
[ret
].receiver
= &receivers
[receiver_num
];
775 /* use the first available receiver with no arguments
776 * for those audio formats for which no receiver
779 cmd
= para_strdup(receivers
[0].name
);
780 FOR_EACH_AUDIO_FORMAT(i
) {
782 if (a
->receiver_conf
)
784 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
785 if (!a
->receiver_conf
)
786 return -E_RECV_SYNTAX
;
787 a
->receiver
= &receivers
[receiver_num
];
795 static int init_default_filters(void)
799 FOR_EACH_AUDIO_FORMAT(i
) {
800 struct audio_format_info
*a
= &afi
[i
];
805 continue; /* no default -- nothing to to */
806 /* add "dec" to audio format name */
807 tmp
= make_message("%sdec", audio_formats
[i
]);
808 for (j
= 0; filters
[j
].name
; j
++)
809 if (!strcmp(tmp
, filters
[j
].name
))
812 ret
= -E_UNSUPPORTED_FILTER
;
813 if (!filters
[j
].name
)
815 tmp
= para_strdup(filters
[j
].name
);
816 ret
= add_filter(i
, tmp
);
820 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
827 static int init_filters(void)
831 filter_init(filters
);
832 nf
= PARA_MAX(1, conf
.filter_given
);
833 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
834 FOR_EACH_AUDIO_FORMAT(i
) {
835 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
836 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
838 if (!conf
.no_default_filters_given
)
839 return init_default_filters();
840 for (i
= 0; i
< conf
.filter_given
; i
++) {
841 char *arg
= conf
.filter_arg
[i
];
842 char *filter_name
= strchr(arg
, ':');
843 ret
= -E_MISSING_COLON
;
848 ret
= get_audio_format_num(arg
);
851 ret
= add_filter(ret
, filter_name
);
855 ret
= init_default_filters(); /* use default values for the rest */
860 static int init_stream_io(void)
864 ret
= init_writers();
867 ret
= init_receivers();
870 ret
= init_filters();
876 static int audiod_get_socket(void)
878 struct sockaddr_un unix_addr
;
881 if (conf
.socket_given
)
882 socket_name
= para_strdup(conf
.socket_arg
);
884 char *hn
= para_hostname();
885 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
889 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
890 if (conf
.force_given
)
892 fd
= create_pf_socket(socket_name
, &unix_addr
,
893 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
895 PARA_EMERG_LOG("%s", "can not connect to socket\n");
896 exit(EXIT_FAILURE
); /* do not unlink socket */
898 if (listen(fd
, 5) < 0) {
899 PARA_EMERG_LOG("%s", "can not listen on socket\n");
900 exit(EXIT_FAILURE
); /* do not unlink socket */
902 mark_fd_nonblock(fd
);
906 static void signal_event_handler(struct task
*t
)
908 struct signal_task
*st
= t
->private_data
;
910 if (t
->ret
!= -E_SIGNAL_CAUGHT
)
911 PARA_ERROR_LOG("%s (ignored)\n", PARA_STRERROR(-t
->ret
));
913 handle_signal(st
->signum
);
916 static void signal_pre_select(struct sched
*s
, struct task
*t
)
918 struct signal_task
*st
= t
->private_data
;
920 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
923 static void signal_post_select(struct sched
*s
, struct task
*t
)
925 struct signal_task
*st
= t
->private_data
;
927 if (!FD_ISSET(st
->fd
, &s
->rfds
))
929 t
->ret
= -E_SIGNAL_CAUGHT
;
930 st
->signum
= para_next_signal();
933 static void signal_setup_default(struct signal_task
*st
)
935 st
->task
.pre_select
= signal_pre_select
;
936 st
->task
.post_select
= signal_post_select
;
937 st
->task
.private_data
= st
;
938 sprintf(st
->task
.status
, "signal task");
941 static void command_pre_select(struct sched
*s
, struct task
*t
)
943 struct command_task
*ct
= t
->private_data
;
945 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
949 static void command_post_select(struct sched
*s
, struct task
*t
)
952 struct command_task
*ct
= t
->private_data
;
954 t
->ret
= 1; /* always successful */
955 if (audiod_status
!= AUDIOD_OFF
)
956 audiod_status_dump();
957 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
959 ret
= handle_connect(ct
->fd
);
961 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
964 static void init_command_task(struct command_task
*ct
)
966 ct
->task
.pre_select
= command_pre_select
;
967 ct
->task
.post_select
= command_post_select
;
968 ct
->task
.event_handler
= NULL
;
969 ct
->task
.private_data
= ct
;
970 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
971 sprintf(ct
->task
.status
, "command task");
974 static void client_task_event_handler(__a_unused
struct task
*t
)
976 struct private_client_data
*pcd
= t
->private_data
;
977 if (t
->ret
== -E_HANDSHAKE_COMPLETE
)
983 static void status_event_handler(__a_unused
struct task
*t
)
985 struct timeval delay
= {1, 0};
987 struct status_task
*st
= t
->private_data
;
989 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
991 /* avoid busy loop if server is down */
992 tv_add(now
, &delay
, &st
->restart_barrier
);
993 FOR_EACH_AUDIO_FORMAT(i
)
994 afi
[i
].restart_barrier
= st
->restart_barrier
;
997 static void status_pre_select(struct sched
*s
, struct task
*t
)
999 struct status_task
*st
= t
->private_data
;
1001 char *argv
[] = {"audiod", "stat", NULL
};
1003 if (st
->pcd
&& (audiod_status
== AUDIOD_OFF
|| st
->pcd
->eof
))
1005 if (!st
->pcd
&& audiod_status
!= AUDIOD_OFF
1006 && tv_diff(now
, &st
->restart_barrier
, NULL
) > 0) {
1007 t
->ret
= client_parse_config(argc
, argv
, &st
->pcd
);
1010 t
->ret
= client_open(st
->pcd
);
1013 st
->pcd
->task
.event_handler
= client_task_event_handler
;
1014 s
->timeout
.tv_sec
= 0;
1015 s
->timeout
.tv_usec
= 1;
1019 static void status_post_select(__a_unused
struct sched
*s
, struct task
*t
)
1021 struct status_task
*st
= t
->private_data
;
1024 if (!st
->pcd
|| !st
->pcd
->loaded
1025 || st
->pcd
->status
!= CL_RECEIVING
)
1027 st
->pcd
->loaded
= for_each_line(st
->pcd
->buf
, st
->pcd
->loaded
,
1031 static void init_status_task(struct status_task
*st
)
1033 memset(st
, 0, sizeof(struct status_task
));
1034 st
->task
.pre_select
= status_pre_select
;
1035 st
->task
.post_select
= status_post_select
;
1036 st
->task
.event_handler
= status_event_handler
;
1037 st
->task
.private_data
= st
;
1038 st
->sa_time_diff_sign
= 1;
1039 sprintf(st
->task
.status
, "status task");
1042 static void set_initial_status(void)
1044 audiod_status
= AUDIOD_ON
;
1045 if (!conf
.mode_given
)
1047 if (!strcmp(conf
.mode_arg
, "sb")) {
1048 audiod_status
= AUDIOD_STANDBY
;
1051 if (!strcmp(conf
.mode_arg
, "off")) {
1052 audiod_status
= AUDIOD_OFF
;
1055 if (strcmp(conf
.mode_arg
, "on"))
1056 PARA_WARNING_LOG("%s", "invalid mode\n");
1059 int main(int argc
, char *argv
[])
1064 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1065 struct task audiod_task_struct
, *audiod_task
= &audiod_task_struct
;
1068 audiod_cmdline_parser(argc
, argv
, &conf
);
1069 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1070 cf
= configfile_exists();
1072 if (audiod_cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
1073 PARA_EMERG_LOG("%s", "parse error in config file\n");
1077 if (conf
.logfile_given
)
1078 logfile
= open_log(conf
.logfile_arg
);
1079 log_welcome("para_audiod", conf
.loglevel_arg
);
1080 i
= init_stream_io();
1082 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1085 server_uptime(UPTIME_SET
);
1086 set_initial_status();
1090 setup_signal_handling();
1091 signal_setup_default(sig_task
);
1092 sig_task
->task
.event_handler
= signal_event_handler
;
1094 init_status_task(stat_task
);
1095 init_command_task(cmd_task
);
1096 init_audiod_task(audiod_task
);
1098 if (conf
.daemon_given
)
1101 register_task(&sig_task
->task
);
1102 register_task(&cmd_task
->task
);
1103 register_task(&stat_task
->task
);
1104 register_task(audiod_task
);
1105 s
.default_timeout
.tv_sec
= 0;
1106 s
.default_timeout
.tv_usec
= 99 * 1000;
1109 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
1110 return EXIT_FAILURE
;