2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file audiod.c the paraslash's audio daemon */
11 #include "audiod.cmdline.h"
16 #include "grab_client.cmdline.h"
17 #include "grab_client.h"
18 #include "client.cmdline.h"
26 #include "write_common.h"
29 /** define the array of error lists needed by para_audiod */
31 /** define the array containing all supported audio formats */
32 const char *audio_formats
[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL
};
34 /** defines how to handle one supported audio format */
35 struct audio_format_info
{
36 /** pointer to the receiver for this audio format */
37 struct receiver
*receiver
;
38 /** the receiver configuration */
40 /** the number of filters that should be activated for this audio format */
41 unsigned int num_filters
;
42 /** pointer to the array of filters to be activated */
43 struct filter
**filters
;
44 /** pointer to the array of filter configurations */
46 /** the number of filters that should be activated for this audio format */
47 unsigned int num_writers
;
48 /** pointer to the array of writers to be activated */
49 struct writer
**writers
;
50 /** pointer to the array of writer configurations */
52 /** do not start receiver/filters/writer before this time */
53 struct timeval restart_barrier
;
57 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
58 * be associated with a receiver/filter/writer triple. This array holds all
59 * information on the status of these slots.
61 * \sa struct slot_info
63 struct slot_info slot
[MAX_STREAM_SLOTS
];
67 * the current mode of operation of which can be changed by the on/off/cycle
68 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
70 int audiod_status
= AUDIOD_ON
;
73 * the gengetopt args_info struct that holds information on all command line
76 struct audiod_args_info conf
;
78 static char *socket_name
;
80 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
82 static struct signal_task signal_task_struct
, *sig_task
= &signal_task_struct
;
84 static struct status_task status_task_struct
;
87 * the task that calls the status command of para_server
89 * \sa struct status_task
91 struct status_task
*stat_task
= &status_task_struct
;
92 static struct timeval initial_delay_barrier
;
95 * the task for handling audiod commands
97 * \sa struct task, struct sched
100 /** the local listening socket */
102 /** the associated task structure */
106 /** iterate over all supported audio formats */
107 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
110 * get the audio format number
111 * \param name the name of the audio format
113 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
114 * \a name is not a supported audio format.
116 int get_audio_format_num(char *name
)
119 FOR_EACH_AUDIO_FORMAT(i
)
120 if (!strcmp(name
, audio_formats
[i
]))
122 return -E_UNSUPPORTED_AUDIO_FORMAT
;
126 * the log function of para_audiod
129 * \param fmt the format string
131 void para_log(int ll
, const char* fmt
,...)
137 char str
[MAXLINE
] = "";
138 static char *hostname
;
140 if (ll
< conf
.loglevel_arg
)
143 hostname
= para_hostname();
144 outfd
= logfile
? logfile
: stderr
;
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
);
152 vfprintf(outfd
, fmt
, argp
);
156 static char *configfile_exists(void)
158 char *home
= para_homedir();
159 char *config_file
= make_message("%s/.paraslash/audiod.conf",
162 if (file_exists(config_file
))
168 static void setup_signal_handling(void)
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
);
178 static void clear_slot(int slot_num
)
180 struct slot_info
*s
= &slot
[slot_num
];
182 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
183 memset(s
, 0, sizeof(struct slot_info
));
187 static void close_receiver(int slot_num
)
189 struct slot_info
*s
= &slot
[slot_num
];
190 struct audio_format_info
*a
;
192 if (s
->format
< 0 || !s
->receiver_node
)
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
;
202 static void kill_all_decoders(void)
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",
211 wng_unregister(s
->wng
);
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
);
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;
227 static int get_empty_slot(void)
238 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
243 return -E_NO_MORE_SLOTS
;
247 * get the number of filters
249 * \param audio_format_num the number identifying the audio format
251 * \return the number of filters for the given audio format
255 int num_filters(int audio_format_num
)
257 return afi
[audio_format_num
].num_filters
;
260 static void filter_event_handler(struct task
*t
)
262 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
263 struct filter_chain
*fc
= t
->private_data
;
268 static void open_filters(int slot_num
)
270 struct slot_info
*s
= &slot
[slot_num
];
271 struct audio_format_info
*a
= &afi
[s
->format
];
272 int nf
= a
->num_filters
;
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
;
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
];
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
;
305 register_task(&s
->fc
->task
);
308 static void wng_event_handler(struct task
*t
)
310 struct writer_node_group
*wng
= t
->private_data
;
312 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
317 static void open_writers(int slot_num
)
320 struct slot_info
*s
= &slot
[slot_num
];
321 struct audio_format_info
*a
= &afi
[s
->format
];
323 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
325 s
->wng
= setup_default_wng();
327 s
->wng
= wng_new(a
->num_writers
);
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
);
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
;
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
];
346 ret
= wng_open(s
->wng
);
348 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
352 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fc
->filters
);
355 static void rn_event_handler(struct task
*t
)
357 struct receiver_node
*rn
= t
->private_data
;
358 const struct timeval restart_delay
= {0, 10 * 1000};
361 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
364 /* set restart barrier */
366 if (slot
[i
].receiver_node
!= rn
)
368 tv_add(now
, &restart_delay
, &afi
[slot
[i
].format
].restart_barrier
);
372 static int open_receiver(int format
)
374 struct audio_format_info
*a
= &afi
[format
];
377 struct receiver_node
*rn
;
378 const struct timeval restart_delay
= {1, 0};
380 ret
= get_empty_slot();
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
);
392 free(s
->receiver_node
);
393 s
->receiver_node
= NULL
;
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
);
406 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
407 tv_add(now
, &restart_delay
, &afi
[format
].restart_barrier
);
411 static int receiver_running(int format
)
416 struct slot_info
*s
= &slot
[i
];
417 if (s
->format
== format
&& s
->receiver_node
418 && !s
->receiver_node
->eof
)
424 static int open_current_receiver(struct sched
*s
)
428 char *audio_format
= stat_task
->stat_item_values
[SI_FORMAT
];
430 if (!audio_format
|| !stat_task
->pcd
)
432 i
= get_audio_format_num(audio_format
+ strlen(
433 status_item_list
[SI_FORMAT
]) + 1);
436 if (receiver_running(i
))
438 if (tv_diff(now
, &afi
[i
].restart_barrier
, &diff
) < 0) {
442 return open_receiver(i
) < 0? 0 : 1;
445 static unsigned compute_time_diff(const struct timeval
*status_time
)
447 struct timeval tmp
, diff
;
448 static unsigned 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;
455 sign
= tv_diff(status_time
, now
, &diff
);
456 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
457 // sign, sa_time_diff_sign);
459 sa_time_diff_sign
= sign
;
460 stat_task
->sa_time_diff
= diff
;
465 int s
= tv_diff(&diff
, &stat_task
->sa_time_diff
, &tmp
);
466 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
467 PARA_WARNING_LOG("time diff jump: %lims\n",
471 sa_time_diff_sign
= tv_convex_combination(
472 sa_time_diff_sign
* time_smooth
, &stat_task
->sa_time_diff
,
473 count
> 10? sign
: sign
* time_smooth
, &diff
,
475 stat_task
->sa_time_diff
= tmp
;
476 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
479 sa_time_diff_sign
? "+" : "-",
480 tv2ms(&stat_task
->sa_time_diff
)
483 stat_task
->sa_time_diff_sign
= sa_time_diff_sign
;
487 static int check_stat_line(char *line
, __a_unused
void *data
)
491 long unsigned sec
, usec
;
494 // PARA_INFO_LOG("line: %s\n", line);
497 itemnum
= stat_line_valid(line
);
499 PARA_WARNING_LOG("invalid status line: %s\n", line
);
502 if (stat_task
->clock_diff_count
&& itemnum
!= SI_CURRENT_TIME
)
504 tmp
= make_message("%s\n", line
);
505 stat_client_write(tmp
, itemnum
);
507 free(stat_task
->stat_item_values
[itemnum
]);
508 stat_task
->stat_item_values
[itemnum
] = para_strdup(line
);
509 ilen
= strlen(status_item_list
[itemnum
]);
512 stat_task
->playing
= strstr(line
, "playing")? 1 : 0;
515 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
518 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
520 case SI_STREAM_START
:
521 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
522 struct timeval a_start
, delay
;
523 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
524 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
525 stat_task
->server_stream_start
.tv_sec
= sec
;
526 stat_task
->server_stream_start
.tv_usec
= usec
;
527 if (compute_time_diff(NULL
) > 2) {
528 if (stat_task
->sa_time_diff_sign
< 0)
529 tv_add(&stat_task
->server_stream_start
,
530 &stat_task
->sa_time_diff
, &a_start
);
532 tv_diff(&stat_task
->server_stream_start
,
533 &stat_task
->sa_time_diff
, &a_start
);
534 tv_add(&a_start
, &delay
, &initial_delay_barrier
);
538 case SI_CURRENT_TIME
:
539 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
540 struct timeval tv
= {sec
, usec
};
541 compute_time_diff(&tv
);
543 if (stat_task
->clock_diff_count
)
544 stat_task
->clock_diff_count
--;
550 static void try_to_close_slot(int slot_num
)
552 struct slot_info
*s
= &slot
[slot_num
];
556 if (s
->receiver_node
&& !s
->receiver_node
->eof
)
558 if (s
->fc
&& !s
->fc
->eof
)
560 if (s
->wng
&& !s
->wng
->eof
)
562 PARA_INFO_LOG("closing slot %d \n", slot_num
);
564 close_filters(s
->fc
);
566 close_receiver(slot_num
);
567 clear_slot(slot_num
);
571 * Check if any receivers/filters/writers need to be started and do so if
572 * neccessary. Since the pre_select function didn't have a chance yet to put
573 * file descriptors into the fd sets given by s, make the upcoming select()
574 * return immediately to avoid a long timeout in case we started something.
576 static void audiod_pre_select(struct sched
*s
, __a_unused
struct task
*t
)
579 struct timeval min_delay
= {0, 1};
582 if (audiod_status
!= AUDIOD_ON
|| !stat_task
->playing
)
583 return kill_all_decoders();
584 if (open_current_receiver(s
))
585 s
->timeout
= min_delay
;
587 struct slot_info
*sl
= &slot
[i
];
588 struct audio_format_info
*a
;
593 a
= &afi
[sl
->format
];
594 if (!sl
->receiver_node
)
596 if (!a
->num_filters
) {
597 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
599 s
->timeout
= min_delay
;
603 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
605 s
->timeout
= min_delay
;
608 if (!sl
->fc
|| !*sl
->fc
->out_loaded
|| sl
->wng
)
610 if (tv_diff(now
, &initial_delay_barrier
, &diff
) > 0) {
612 s
->timeout
= min_delay
;
615 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff
));
616 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0) {
622 static void audiod_post_select(__a_unused
struct sched
*s
,
623 __a_unused
struct task
*t
)
629 try_to_close_slot(i
);
632 static void init_audiod_task(struct task
*t
)
634 t
->pre_select
= audiod_pre_select
;
635 t
->post_select
= audiod_post_select
;
636 t
->event_handler
= NULL
;
638 sprintf(t
->status
, "audiod task");
641 static int parse_stream_command(const char *txt
, char **cmd
)
643 char *p
= strchr(txt
, ':');
647 return -E_MISSING_COLON
;
649 FOR_EACH_AUDIO_FORMAT(i
) {
650 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
655 return -E_UNSUPPORTED_AUDIO_FORMAT
;
658 static int add_filter(int format
, char *cmdline
)
660 struct audio_format_info
*a
= &afi
[format
];
661 int filter_num
, nf
= a
->num_filters
;
663 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
666 a
->filters
[nf
] = &filters
[filter_num
];
668 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
669 a
->filters
[nf
]->name
);
673 static int init_writers(void)
677 struct audio_format_info
*a
;
679 init_supported_writers();
680 nw
= PARA_MAX(1, conf
.writer_given
);
681 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
682 FOR_EACH_AUDIO_FORMAT(i
) {
684 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
685 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
688 for (i
= 0; i
< conf
.writer_given
; i
++) {
691 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
696 wconf
= check_writer_arg(cmd
, &writer_num
);
701 a
->writers
[nw
] = &writers
[writer_num
];
702 a
->writer_conf
[nw
] = wconf
;
703 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
704 nw
, writer_names
[writer_num
]);
712 static int init_receivers(void)
714 int i
, ret
, receiver_num
;
716 struct audio_format_info
*a
;
718 for (i
= 0; receivers
[i
].name
; i
++) {
719 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
720 receivers
[i
].init(&receivers
[i
]);
722 for (i
= conf
.receiver_given
- 1; i
>= 0; i
--) {
723 char *arg
= conf
.receiver_arg
[i
];
724 char *recv_arg
= strchr(arg
, ':');
725 ret
= -E_MISSING_COLON
;
730 ret
= get_audio_format_num(arg
);
733 afi
[ret
].receiver_conf
= check_receiver_arg(recv_arg
, &receiver_num
);
734 if (!afi
[ret
].receiver_conf
) {
735 ret
= -E_RECV_SYNTAX
;
738 afi
[ret
].receiver
= &receivers
[receiver_num
];
740 /* use the first available receiver with no arguments
741 * for those audio formats for which no receiver
744 cmd
= para_strdup(receivers
[0].name
);
745 FOR_EACH_AUDIO_FORMAT(i
) {
747 if (a
->receiver_conf
)
749 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
750 if (!a
->receiver_conf
)
751 return -E_RECV_SYNTAX
;
752 a
->receiver
= &receivers
[receiver_num
];
760 static int init_default_filters(void)
764 FOR_EACH_AUDIO_FORMAT(i
) {
765 struct audio_format_info
*a
= &afi
[i
];
770 continue; /* no default -- nothing to to */
771 /* add "dec" to audio format name */
772 tmp
= make_message("%sdec", audio_formats
[i
]);
773 for (j
= 0; filters
[j
].name
; j
++)
774 if (!strcmp(tmp
, filters
[j
].name
))
777 ret
= -E_UNSUPPORTED_FILTER
;
778 if (!filters
[j
].name
)
780 tmp
= para_strdup(filters
[j
].name
);
781 ret
= add_filter(i
, tmp
);
785 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
792 static int init_filters(void)
796 filter_init(filters
);
797 nf
= PARA_MAX(1, conf
.filter_given
);
798 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
799 FOR_EACH_AUDIO_FORMAT(i
) {
800 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
801 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
803 if (!conf
.no_default_filters_given
)
804 return init_default_filters();
805 for (i
= 0; i
< conf
.filter_given
; i
++) {
806 char *arg
= conf
.filter_arg
[i
];
807 char *filter_name
= strchr(arg
, ':');
808 ret
= -E_MISSING_COLON
;
813 ret
= get_audio_format_num(arg
);
816 ret
= add_filter(ret
, filter_name
);
820 ret
= init_default_filters(); /* use default values for the rest */
825 static int init_stream_io(void)
829 ret
= init_writers();
832 ret
= init_receivers();
835 ret
= init_filters();
841 static int audiod_get_socket(void)
843 struct sockaddr_un unix_addr
;
846 if (conf
.socket_given
)
847 socket_name
= para_strdup(conf
.socket_arg
);
849 char *hn
= para_hostname();
850 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
854 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
855 if (conf
.force_given
)
857 fd
= create_local_socket(socket_name
, &unix_addr
,
858 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
860 PARA_EMERG_LOG("can not connect to socket\n");
861 exit(EXIT_FAILURE
); /* do not unlink socket */
863 if (listen(fd
, 5) < 0) {
864 PARA_EMERG_LOG("can not listen on socket\n");
865 exit(EXIT_FAILURE
); /* do not unlink socket */
867 mark_fd_nonblock(fd
);
871 static void signal_event_handler(struct task
*t
)
873 struct signal_task
*st
= t
->private_data
;
875 switch (st
->signum
) {
879 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
880 clean_exit(EXIT_FAILURE
, "caught deadly signal");
884 static void signal_pre_select(struct sched
*s
, struct task
*t
)
886 struct signal_task
*st
= t
->private_data
;
888 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
891 static void signal_post_select(struct sched
*s
, struct task
*t
)
893 struct signal_task
*st
= t
->private_data
;
895 if (!FD_ISSET(st
->fd
, &s
->rfds
))
897 t
->ret
= -E_SIGNAL_CAUGHT
;
898 st
->signum
= para_next_signal();
901 static void signal_setup_default(struct signal_task
*st
)
903 st
->task
.pre_select
= signal_pre_select
;
904 st
->task
.post_select
= signal_post_select
;
905 st
->task
.private_data
= st
;
906 sprintf(st
->task
.status
, "signal task");
909 static void command_pre_select(struct sched
*s
, struct task
*t
)
911 struct command_task
*ct
= t
->private_data
;
913 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
917 static void command_post_select(struct sched
*s
, struct task
*t
)
920 struct command_task
*ct
= t
->private_data
;
922 t
->ret
= 1; /* always successful */
923 audiod_status_dump();
924 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
926 ret
= handle_connect(ct
->fd
);
928 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
931 static void init_command_task(struct command_task
*ct
)
933 ct
->task
.pre_select
= command_pre_select
;
934 ct
->task
.post_select
= command_post_select
;
935 ct
->task
.event_handler
= NULL
;
936 ct
->task
.private_data
= ct
;
937 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
938 sprintf(ct
->task
.status
, "command task");
941 static void close_stat_pipe(void)
947 client_close(stat_task
->pcd
);
948 stat_task
->pcd
= NULL
;
949 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
950 free(stat_task
->stat_item_values
[i
]);
951 stat_task
->stat_item_values
[i
] = NULL
;
954 stat_task
->length_seconds
= 0;
955 stat_task
->offset_seconds
= 0;
956 audiod_status_dump();
957 stat_task
->playing
= 0;
958 stat_task
->stat_item_values
[SI_STATUS_BAR
] = make_message(
959 "%s:no connection to para_server\n",
960 status_item_list
[SI_STATUS_BAR
]);
961 stat_client_write(stat_task
->stat_item_values
[SI_STATUS_BAR
],
963 if (stat_task
->clock_diff_count
) {
964 stat_task
->clock_diff_barrier
.tv_sec
= now
->tv_sec
+ 1;
965 stat_task
->clock_diff_barrier
.tv_usec
= now
->tv_usec
;
970 * close the connection to para_server and exit
972 * \param status the exit status which is passed to exit(3)
973 * \param msg the log message
975 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
976 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
981 void __noreturn
clean_exit(int status
, const char *msg
)
983 PARA_EMERG_LOG("%s\n", msg
);
990 /* avoid busy loop if server is down */
991 static void set_stat_task_restart_barrier(void)
993 struct timeval delay
= {5, 0};
994 tv_add(now
, &delay
, &stat_task
->restart_barrier
);
997 static void client_task_event_handler(__a_unused
struct task
*t
)
1001 if (t
->ret
== -E_HANDSHAKE_COMPLETE
)
1005 if (t
->ret
!= -E_SERVER_EOF
)
1006 stat_task
->clock_diff_count
= conf
.clock_diff_count_arg
;
1007 set_stat_task_restart_barrier();
1008 FOR_EACH_AUDIO_FORMAT(i
)
1009 afi
[i
].restart_barrier
= stat_task
->restart_barrier
;
1012 static void status_pre_select(struct sched
*s
, struct task
*t
)
1014 struct status_task
*st
= t
->private_data
;
1017 t
->ret
= 1; /* always successful */
1018 if (st
->pcd
|| audiod_status
== AUDIOD_OFF
)
1020 if (!st
->clock_diff_count
&& tv_diff(now
, &st
->restart_barrier
, NULL
)
1023 if (st
->clock_diff_count
) {
1024 char *argv
[] = {"audiod", "stat", "1", NULL
};
1026 if (tv_diff(now
, &st
->clock_diff_barrier
, NULL
) < 0)
1028 PARA_INFO_LOG("clock diff count: %d\n", st
->clock_diff_count
);
1029 ret
= client_open(argc
, argv
, &st
->pcd
);
1032 char *argv
[] = {"audiod", "stat", NULL
};
1034 ret
= client_open(argc
, argv
, &st
->pcd
);
1036 set_stat_task_restart_barrier();
1039 st
->pcd
->task
.event_handler
= client_task_event_handler
;
1040 s
->timeout
.tv_sec
= 0;
1041 s
->timeout
.tv_usec
= 1;
1044 static void status_post_select(__a_unused
struct sched
*s
, struct task
*t
)
1046 struct status_task
*st
= t
->private_data
;
1047 unsigned bytes_left
;
1050 if (!st
->pcd
|| st
->pcd
->status
!= CL_RECEIVING
)
1052 if (st
->pcd
&& audiod_status
== AUDIOD_OFF
) {
1053 unregister_task(&st
->pcd
->task
);
1055 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1058 bytes_left
= for_each_line(st
->pcd
->buf
, st
->pcd
->loaded
,
1059 &check_stat_line
, NULL
);
1060 if (st
->pcd
->loaded
!= bytes_left
) {
1061 st
->last_status_read
= *now
;
1062 st
->pcd
->loaded
= bytes_left
;
1064 struct timeval diff
;
1065 tv_diff(now
, &st
->last_status_read
, &diff
);
1066 if (diff
.tv_sec
> 61)
1071 static void init_status_task(struct status_task
*st
)
1073 memset(st
, 0, sizeof(struct status_task
));
1074 st
->task
.pre_select
= status_pre_select
;
1075 st
->task
.post_select
= status_post_select
;
1076 st
->task
.private_data
= st
;
1077 st
->sa_time_diff_sign
= 1;
1078 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1079 sprintf(st
->task
.status
, "status task");
1082 static void set_initial_status(void)
1084 audiod_status
= AUDIOD_ON
;
1085 if (!conf
.mode_given
)
1087 if (!strcmp(conf
.mode_arg
, "sb")) {
1088 audiod_status
= AUDIOD_STANDBY
;
1091 if (!strcmp(conf
.mode_arg
, "off")) {
1092 audiod_status
= AUDIOD_OFF
;
1095 if (strcmp(conf
.mode_arg
, "on"))
1096 PARA_WARNING_LOG("%s", "invalid mode\n");
1100 * the main function of para_audiod
1102 * \param argc usual argument count
1103 * \param argv usual argument vector
1105 * \return EXIT_SUCCESS or EXIT_FAILURE
1107 * \sa para_audiod(1)
1109 int main(int argc
, char *argv
[])
1114 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1115 struct task audiod_task_struct
, *audiod_task
= &audiod_task_struct
;
1118 audiod_cmdline_parser(argc
, argv
, &conf
);
1119 HANDLE_VERSION_FLAG("audiod", conf
);
1120 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1121 config_file
= configfile_exists();
1123 struct audiod_cmdline_parser_params params
= {
1126 .check_required
= 0,
1127 .check_ambiguity
= 0
1130 if (audiod_cmdline_parser_config_file(config_file
, &conf
, ¶ms
)) {
1131 PARA_EMERG_LOG("%s", "parse error in config file\n");
1136 if (conf
.logfile_given
)
1137 logfile
= open_log(conf
.logfile_arg
);
1138 log_welcome("para_audiod", conf
.loglevel_arg
);
1139 i
= init_stream_io();
1141 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1144 server_uptime(UPTIME_SET
);
1145 set_initial_status();
1149 setup_signal_handling();
1150 signal_setup_default(sig_task
);
1151 sig_task
->task
.event_handler
= signal_event_handler
;
1153 init_status_task(stat_task
);
1154 init_command_task(cmd_task
);
1155 init_audiod_task(audiod_task
);
1157 if (conf
.daemon_given
)
1160 register_task(&sig_task
->task
);
1161 register_task(&cmd_task
->task
);
1162 register_task(&stat_task
->task
);
1163 register_task(audiod_task
);
1164 s
.default_timeout
.tv_sec
= 0;
1165 s
.default_timeout
.tv_usec
= 99 * 1000;
1168 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
1169 return EXIT_FAILURE
;