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 */
13 #include "audiod.cmdline.h"
18 #include "grab_client.cmdline.h"
19 #include "grab_client.h"
20 #include "client.cmdline.h"
28 #include "write_common.h"
31 /** define the array of error lists needed by para_audiod */
33 /** define the array containing all supported audio formats */
34 const char *audio_formats
[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL
};
36 /** Defines how audiod handles one supported audio format. */
37 struct audio_format_info
{
38 /** pointer to the receiver for this audio format */
39 struct receiver
*receiver
;
40 /** the receiver configuration */
42 /** the number of filters that should be activated for this audio format */
43 unsigned int num_filters
;
44 /** pointer to the array of filters to be activated */
45 struct filter
**filters
;
46 /** pointer to the array of filter configurations */
48 /** the number of filters that should be activated for this audio format */
49 unsigned int num_writers
;
50 /** pointer to the array of writers to be activated */
51 struct writer
**writers
;
52 /** pointer to the array of writer configurations */
54 /** do not start receiver/filters/writer before this time */
55 struct timeval restart_barrier
;
59 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
60 * be associated with a receiver/filter/writer triple. This array holds all
61 * information on the status of these slots.
63 * \sa struct slot_info
65 struct slot_info slot
[MAX_STREAM_SLOTS
];
69 * the current mode of operation of which can be changed by the on/off/cycle
70 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
72 int audiod_status
= AUDIOD_ON
;
75 * the gengetopt args_info struct that holds information on all command line
78 struct audiod_args_info conf
;
80 static char *socket_name
;
82 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
84 static struct signal_task signal_task_struct
, *sig_task
= &signal_task_struct
;
86 static struct status_task status_task_struct
;
89 * the task that calls the status command of para_server
91 * \sa struct status_task
93 struct status_task
*stat_task
= &status_task_struct
;
94 static struct timeval initial_delay_barrier
;
97 * the task for handling audiod commands
99 * \sa struct task, struct sched
101 struct command_task
{
102 /** the local listening socket */
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
)
122 while (para_isspace(*name
))
124 FOR_EACH_AUDIO_FORMAT(i
)
125 if (!strcmp(name
, audio_formats
[i
]))
127 return -E_UNSUPPORTED_AUDIO_FORMAT
;
131 * the log function of para_audiod
134 * \param fmt the format string
136 void para_log(int ll
, const char* fmt
,...)
142 char str
[MAXLINE
] = "";
143 static char *hostname
;
145 if (ll
< conf
.loglevel_arg
)
148 hostname
= para_hostname();
149 outfd
= logfile
? logfile
: stderr
;
152 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
153 fprintf(outfd
, "%s %s ", str
, hostname
);
154 if (conf
.loglevel_arg
<= INFO
)
155 fprintf(outfd
, "%i ", ll
);
157 vfprintf(outfd
, fmt
, argp
);
161 static char *configfile_exists(void)
163 char *home
= para_homedir();
164 char *config_file
= make_message("%s/.paraslash/audiod.conf",
167 if (file_exists(config_file
))
173 static void setup_signal_handling(void)
175 sig_task
->fd
= para_signal_init();
176 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
177 para_install_sighandler(SIGINT
);
178 para_install_sighandler(SIGTERM
);
179 para_install_sighandler(SIGHUP
);
180 signal(SIGPIPE
, SIG_IGN
);
183 static void clear_slot(int slot_num
)
185 struct slot_info
*s
= &slot
[slot_num
];
187 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
188 memset(s
, 0, sizeof(struct slot_info
));
192 static void close_receiver(int slot_num
)
194 struct slot_info
*s
= &slot
[slot_num
];
195 struct audio_format_info
*a
;
197 if (s
->format
< 0 || !s
->receiver_node
)
200 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
201 audio_formats
[s
->format
], slot_num
);
202 a
->receiver
->close(s
->receiver_node
);
203 free(s
->receiver_node
);
204 s
->receiver_node
= NULL
;
207 static void kill_all_decoders(int error
)
212 struct slot_info
*s
= &slot
[i
];
213 if (s
->wng
&& !s
->wng
->error
) {
214 PARA_INFO_LOG("unregistering writer node group in slot %d\n",
216 wng_unregister(s
->wng
);
217 s
->wng
->error
= error
;
219 if (s
->fc
&& !s
->fc
->error
) {
220 PARA_INFO_LOG("unregistering filter chain in slot %d\n", i
);
221 unregister_task(&s
->fc
->task
);
222 s
->fc
->error
= error
;
224 if (s
->receiver_node
&& !s
->receiver_node
->error
) {
225 PARA_INFO_LOG("unregistering receiver_node in slot %d\n", i
);
226 unregister_task(&s
->receiver_node
->task
);
227 s
->receiver_node
->error
= error
;
232 static int get_empty_slot(void)
243 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
248 return -E_NO_MORE_SLOTS
;
252 * get the number of filters
254 * \param audio_format_num the number identifying the audio format
256 * \return the number of filters for the given audio format
260 int num_filters(int audio_format_num
)
262 return afi
[audio_format_num
].num_filters
;
265 static void filter_event_handler(struct task
*t
)
267 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
268 struct filter_chain
*fc
= t
->private_data
;
273 static void open_filters(int slot_num
)
275 struct slot_info
*s
= &slot
[slot_num
];
276 struct audio_format_info
*a
= &afi
[s
->format
];
277 int nf
= a
->num_filters
;
283 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
284 s
->fc
= para_calloc(sizeof(struct filter_chain
));
285 INIT_LIST_HEAD(&s
->fc
->filters
);
286 s
->fc
->inbuf
= s
->receiver_node
->buf
;
287 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
288 s
->fc
->input_error
= &s
->receiver_node
->error
;
289 s
->fc
->task
.pre_select
= filter_pre_select
;
290 s
->fc
->task
.event_handler
= filter_event_handler
;
291 s
->fc
->task
.private_data
= s
->fc
;
294 s
->receiver_node
->output_error
= &s
->fc
->error
;
295 sprintf(s
->fc
->task
.status
, "filter chain");
296 for (i
= 0; i
< nf
; i
++) {
297 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
298 fn
->conf
= a
->filter_conf
[i
];
300 fn
->filter
= a
->filters
[i
];
301 INIT_LIST_HEAD(&fn
->callbacks
);
302 list_add_tail(&fn
->node
, &s
->fc
->filters
);
303 fn
->filter
->open(fn
);
304 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
305 audio_formats
[s
->format
], i
+ 1, nf
,
306 fn
->filter
->name
, slot_num
);
307 s
->fc
->outbuf
= fn
->buf
;
308 s
->fc
->out_loaded
= &fn
->loaded
;
310 register_task(&s
->fc
->task
);
313 static void wng_event_handler(struct task
*t
)
315 struct writer_node_group
*wng
= t
->private_data
;
317 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
322 static void open_writers(int slot_num
)
325 struct slot_info
*s
= &slot
[slot_num
];
326 struct audio_format_info
*a
= &afi
[s
->format
];
328 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
330 s
->wng
= setup_default_wng();
332 s
->wng
= wng_new(a
->num_writers
);
334 s
->wng
->buf
= s
->fc
->outbuf
;
335 s
->wng
->loaded
= s
->fc
->out_loaded
;
336 s
->wng
->input_error
= &s
->fc
->error
;
337 s
->wng
->channels
= &s
->fc
->channels
;
338 s
->wng
->samplerate
= &s
->fc
->samplerate
;
339 s
->fc
->output_error
= &s
->wng
->error
;
340 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
342 s
->wng
->buf
= s
->receiver_node
->buf
;
343 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
344 s
->wng
->input_error
= &s
->receiver_node
->error
;
346 s
->wng
->task
.event_handler
= wng_event_handler
;
347 for (i
= 0; i
< a
->num_writers
; i
++) {
348 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
349 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
351 ret
= wng_open(s
->wng
);
353 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
357 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fc
->filters
);
360 static void rn_event_handler(struct task
*t
)
362 struct receiver_node
*rn
= t
->private_data
;
365 PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-t
->ret
));
368 /* set restart barrier */
370 struct timeval restart_delay
= {0, 10 * 1000};
371 if (slot
[i
].receiver_node
!= rn
)
373 if (rn
->error
!= -E_RECV_EOF
)
374 /* don't reconnect immediately on errors */
375 restart_delay
.tv_sec
= 5;
376 tv_add(now
, &restart_delay
, &afi
[slot
[i
].format
].restart_barrier
);
380 static int open_receiver(int format
)
382 struct audio_format_info
*a
= &afi
[format
];
385 struct receiver_node
*rn
;
386 const struct timeval restart_delay
= {1, 0};
388 ret
= get_empty_slot();
394 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
395 rn
= s
->receiver_node
;
396 rn
->receiver
= a
->receiver
;
397 rn
->conf
= a
->receiver_conf
;
398 ret
= a
->receiver
->open(s
->receiver_node
);
400 free(s
->receiver_node
);
401 s
->receiver_node
= NULL
;
404 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
405 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
406 rn
->task
.private_data
= s
->receiver_node
;
407 rn
->task
.pre_select
= a
->receiver
->pre_select
;
408 rn
->task
.post_select
= a
->receiver
->post_select
;
409 rn
->task
.event_handler
= rn_event_handler
;
410 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
411 register_task(&rn
->task
);
414 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
415 tv_add(now
, &restart_delay
, &afi
[format
].restart_barrier
);
419 static int receiver_running(int format
)
424 struct slot_info
*s
= &slot
[i
];
425 if (s
->format
== format
&& s
->receiver_node
426 && !s
->receiver_node
->error
)
432 static int open_current_receiver(struct sched
*s
)
436 char *audio_format
= stat_task
->stat_item_values
[SI_FORMAT
];
438 if (!audio_format
|| !stat_task
->pcd
)
440 i
= get_audio_format_num(audio_format
+ strlen(
441 status_item_list
[SI_FORMAT
]) + 1);
444 if (receiver_running(i
))
446 if (tv_diff(now
, &afi
[i
].restart_barrier
, &diff
) < 0) {
450 return open_receiver(i
) < 0? 0 : 1;
453 static unsigned compute_time_diff(const struct timeval
*status_time
)
455 struct timeval tmp
, diff
;
456 static unsigned count
;
457 int sign
, sa_time_diff_sign
= stat_task
->sa_time_diff_sign
;
458 const struct timeval max_deviation
= {0, 500 * 1000};
459 const int time_smooth
= 5;
463 sign
= tv_diff(status_time
, now
, &diff
);
464 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
465 // sign, sa_time_diff_sign);
467 sa_time_diff_sign
= sign
;
468 stat_task
->sa_time_diff
= diff
;
473 int s
= tv_diff(&diff
, &stat_task
->sa_time_diff
, &tmp
);
474 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
475 PARA_WARNING_LOG("time diff jump: %lims\n",
479 sa_time_diff_sign
= tv_convex_combination(
480 sa_time_diff_sign
* time_smooth
, &stat_task
->sa_time_diff
,
481 count
> 10? sign
: sign
* time_smooth
, &diff
,
483 stat_task
->sa_time_diff
= tmp
;
484 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
487 sa_time_diff_sign
? "+" : "-",
488 tv2ms(&stat_task
->sa_time_diff
)
491 stat_task
->sa_time_diff_sign
= sa_time_diff_sign
;
495 static int check_stat_line(char *line
, __a_unused
void *data
)
499 long unsigned sec
, usec
;
502 // PARA_INFO_LOG("line: %s\n", line);
505 itemnum
= stat_line_valid(line
);
507 PARA_WARNING_LOG("invalid status line: %s\n", line
);
510 if (stat_task
->clock_diff_count
&& itemnum
!= SI_CURRENT_TIME
)
512 tmp
= make_message("%s\n", line
);
513 stat_client_write(tmp
, itemnum
);
515 free(stat_task
->stat_item_values
[itemnum
]);
516 stat_task
->stat_item_values
[itemnum
] = para_strdup(line
);
517 ilen
= strlen(status_item_list
[itemnum
]);
520 stat_task
->playing
= strstr(line
, "playing")? 1 : 0;
523 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
525 case SI_SECONDS_TOTAL
:
526 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
528 case SI_STREAM_START
:
529 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
530 struct timeval a_start
, delay
;
531 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
532 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
533 stat_task
->server_stream_start
.tv_sec
= sec
;
534 stat_task
->server_stream_start
.tv_usec
= usec
;
535 if (compute_time_diff(NULL
) > 2) {
536 if (stat_task
->sa_time_diff_sign
< 0)
537 tv_add(&stat_task
->server_stream_start
,
538 &stat_task
->sa_time_diff
, &a_start
);
540 tv_diff(&stat_task
->server_stream_start
,
541 &stat_task
->sa_time_diff
, &a_start
);
542 tv_add(&a_start
, &delay
, &initial_delay_barrier
);
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
);
551 if (stat_task
->clock_diff_count
)
552 stat_task
->clock_diff_count
--;
558 static void try_to_close_slot(int slot_num
)
560 struct slot_info
*s
= &slot
[slot_num
];
564 if (s
->receiver_node
&& !s
->receiver_node
->error
)
566 if (s
->fc
&& !s
->fc
->error
)
568 if (s
->wng
&& !s
->wng
->error
)
570 PARA_INFO_LOG("closing slot %d\n", slot_num
);
572 close_filters(s
->fc
);
574 close_receiver(slot_num
);
575 clear_slot(slot_num
);
579 * Check if any receivers/filters/writers need to be started and do so if
580 * necessary. Since the pre_select function didn't have a chance yet to put
581 * file descriptors into the fd sets given by s, make the upcoming select()
582 * return immediately to avoid a long timeout in case we started something.
584 static void audiod_pre_select(struct sched
*s
, __a_unused
struct task
*t
)
587 struct timeval min_delay
= {0, 1};
590 if (audiod_status
!= AUDIOD_ON
|| !stat_task
->playing
)
591 return kill_all_decoders(-E_NOT_PLAYING
);
592 if (open_current_receiver(s
))
593 s
->timeout
= min_delay
;
595 struct slot_info
*sl
= &slot
[i
];
596 struct audio_format_info
*a
;
601 a
= &afi
[sl
->format
];
602 if (!sl
->receiver_node
)
604 if (!a
->num_filters
) {
605 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
607 s
->timeout
= min_delay
;
611 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
613 s
->timeout
= min_delay
;
616 if (!sl
->fc
|| !*sl
->fc
->out_loaded
|| sl
->wng
)
618 if (tv_diff(now
, &initial_delay_barrier
, &diff
) > 0) {
620 s
->timeout
= min_delay
;
623 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff
));
624 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0) {
630 static void audiod_post_select(__a_unused
struct sched
*s
,
631 __a_unused
struct task
*t
)
637 try_to_close_slot(i
);
640 static void init_audiod_task(struct task
*t
)
642 t
->pre_select
= audiod_pre_select
;
643 t
->post_select
= audiod_post_select
;
644 t
->event_handler
= NULL
;
646 sprintf(t
->status
, "audiod task");
649 static int parse_stream_command(const char *txt
, char **cmd
)
651 char *p
= strchr(txt
, ':');
655 return -E_MISSING_COLON
;
657 FOR_EACH_AUDIO_FORMAT(i
) {
658 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
663 return -E_UNSUPPORTED_AUDIO_FORMAT
;
666 static int add_filter(int format
, char *cmdline
)
668 struct audio_format_info
*a
= &afi
[format
];
669 int filter_num
, nf
= a
->num_filters
;
671 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
674 a
->filters
[nf
] = &filters
[filter_num
];
676 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
677 a
->filters
[nf
]->name
);
681 static int init_writers(void)
685 struct audio_format_info
*a
;
687 init_supported_writers();
688 nw
= PARA_MAX(1, conf
.writer_given
);
689 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
690 FOR_EACH_AUDIO_FORMAT(i
) {
692 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
693 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
696 for (i
= 0; i
< conf
.writer_given
; i
++) {
699 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
704 wconf
= check_writer_arg(cmd
, &writer_num
);
709 a
->writers
[nw
] = &writers
[writer_num
];
710 a
->writer_conf
[nw
] = wconf
;
711 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
712 nw
, writer_names
[writer_num
]);
720 static int init_receivers(void)
722 int i
, ret
, receiver_num
;
724 struct audio_format_info
*a
;
726 for (i
= 0; receivers
[i
].name
; i
++) {
727 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
728 receivers
[i
].init(&receivers
[i
]);
730 for (i
= conf
.receiver_given
- 1; i
>= 0; i
--) {
731 char *arg
= conf
.receiver_arg
[i
];
732 char *recv_arg
= strchr(arg
, ':');
733 ret
= -E_MISSING_COLON
;
738 ret
= get_audio_format_num(arg
);
741 afi
[ret
].receiver_conf
= check_receiver_arg(recv_arg
, &receiver_num
);
742 if (!afi
[ret
].receiver_conf
) {
743 ret
= -E_RECV_SYNTAX
;
746 afi
[ret
].receiver
= &receivers
[receiver_num
];
748 /* use the first available receiver with no arguments
749 * for those audio formats for which no receiver
752 cmd
= para_strdup(receivers
[0].name
);
753 FOR_EACH_AUDIO_FORMAT(i
) {
755 if (a
->receiver_conf
)
757 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
758 if (!a
->receiver_conf
)
759 return -E_RECV_SYNTAX
;
760 a
->receiver
= &receivers
[receiver_num
];
768 static int init_default_filters(void)
772 FOR_EACH_AUDIO_FORMAT(i
) {
773 struct audio_format_info
*a
= &afi
[i
];
778 continue; /* no default -- nothing to to */
779 /* add "dec" to audio format name */
780 tmp
= make_message("%sdec", audio_formats
[i
]);
781 for (j
= 0; filters
[j
].name
; j
++)
782 if (!strcmp(tmp
, filters
[j
].name
))
785 ret
= -E_UNSUPPORTED_FILTER
;
786 if (!filters
[j
].name
)
788 tmp
= para_strdup(filters
[j
].name
);
789 ret
= add_filter(i
, tmp
);
793 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
800 static int init_filters(void)
804 filter_init(filters
);
805 nf
= PARA_MAX(1, conf
.filter_given
);
806 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
807 FOR_EACH_AUDIO_FORMAT(i
) {
808 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
809 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
811 if (!conf
.no_default_filters_given
)
812 return init_default_filters();
813 for (i
= 0; i
< conf
.filter_given
; i
++) {
814 char *arg
= conf
.filter_arg
[i
];
815 char *filter_name
= strchr(arg
, ':');
816 ret
= -E_MISSING_COLON
;
821 ret
= get_audio_format_num(arg
);
824 ret
= add_filter(ret
, filter_name
);
828 ret
= init_default_filters(); /* use default values for the rest */
833 static int init_stream_io(void)
837 ret
= init_writers();
840 ret
= init_receivers();
843 ret
= init_filters();
849 static int audiod_get_socket(void)
851 struct sockaddr_un unix_addr
;
854 if (conf
.socket_given
)
855 socket_name
= para_strdup(conf
.socket_arg
);
857 char *hn
= para_hostname();
858 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
862 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
863 if (conf
.force_given
)
865 fd
= create_local_socket(socket_name
, &unix_addr
,
866 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
868 PARA_EMERG_LOG("can not connect to socket\n");
869 exit(EXIT_FAILURE
); /* do not unlink socket */
871 if (listen(fd
, 5) < 0) {
872 PARA_EMERG_LOG("can not listen on socket\n");
873 exit(EXIT_FAILURE
); /* do not unlink socket */
875 mark_fd_nonblocking(fd
);
879 static void signal_event_handler(struct task
*t
)
881 struct signal_task
*st
= t
->private_data
;
883 switch (st
->signum
) {
887 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
888 clean_exit(EXIT_FAILURE
, "caught deadly signal");
892 static void signal_pre_select(struct sched
*s
, struct task
*t
)
894 struct signal_task
*st
= t
->private_data
;
896 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
899 static void signal_post_select(struct sched
*s
, struct task
*t
)
901 struct signal_task
*st
= t
->private_data
;
903 if (!FD_ISSET(st
->fd
, &s
->rfds
))
905 t
->ret
= -E_SIGNAL_CAUGHT
;
906 st
->signum
= para_next_signal();
909 static void signal_setup_default(struct signal_task
*st
)
911 st
->task
.pre_select
= signal_pre_select
;
912 st
->task
.post_select
= signal_post_select
;
913 st
->task
.private_data
= st
;
914 sprintf(st
->task
.status
, "signal task");
917 static void command_pre_select(struct sched
*s
, struct task
*t
)
919 struct command_task
*ct
= t
->private_data
;
921 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
925 static void command_post_select(struct sched
*s
, struct task
*t
)
928 struct command_task
*ct
= t
->private_data
;
930 t
->ret
= 1; /* always successful */
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 close_stat_pipe(void)
955 client_close(stat_task
->pcd
);
956 stat_task
->pcd
= NULL
;
957 FOR_EACH_STATUS_ITEM(i
) {
958 free(stat_task
->stat_item_values
[i
]);
959 stat_task
->stat_item_values
[i
] = NULL
;
962 stat_task
->length_seconds
= 0;
963 stat_task
->offset_seconds
= 0;
964 audiod_status_dump();
965 stat_task
->playing
= 0;
966 stat_task
->stat_item_values
[SI_BASENAME
] = make_message(
967 "%s: no connection to para_server\n",
968 status_item_list
[SI_BASENAME
]);
969 stat_client_write(stat_task
->stat_item_values
[SI_BASENAME
],
971 if (stat_task
->clock_diff_count
) {
972 stat_task
->clock_diff_barrier
.tv_sec
= now
->tv_sec
+ 1;
973 stat_task
->clock_diff_barrier
.tv_usec
= now
->tv_usec
;
978 * close the connection to para_server and exit
980 * \param status the exit status which is passed to exit(3)
981 * \param msg the log message
983 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
984 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
989 void __noreturn
clean_exit(int status
, const char *msg
)
991 PARA_EMERG_LOG("%s\n", msg
);
998 /* avoid busy loop if server is down */
999 static void set_stat_task_restart_barrier(void)
1001 struct timeval delay
= {5, 0};
1002 tv_add(now
, &delay
, &stat_task
->restart_barrier
);
1005 static void client_task_event_handler(__a_unused
struct task
*t
)
1009 if (t
->ret
== -E_HANDSHAKE_COMPLETE
)
1013 if (t
->ret
!= -E_SERVER_EOF
)
1014 stat_task
->clock_diff_count
= conf
.clock_diff_count_arg
;
1015 set_stat_task_restart_barrier();
1016 FOR_EACH_AUDIO_FORMAT(i
)
1017 afi
[i
].restart_barrier
= stat_task
->restart_barrier
;
1020 static void status_pre_select(struct sched
*s
, struct task
*t
)
1022 struct status_task
*st
= t
->private_data
;
1025 t
->ret
= 1; /* always successful */
1026 if (st
->pcd
|| audiod_status
== AUDIOD_OFF
)
1028 if (!st
->clock_diff_count
&& tv_diff(now
, &st
->restart_barrier
, NULL
)
1031 if (st
->clock_diff_count
) {
1032 char *argv
[] = {"audiod", "stat", "1", NULL
};
1034 if (tv_diff(now
, &st
->clock_diff_barrier
, NULL
) < 0)
1036 PARA_INFO_LOG("clock diff count: %d\n", st
->clock_diff_count
);
1037 ret
= client_open(argc
, argv
, &st
->pcd
);
1040 char *argv
[] = {"audiod", "stat", NULL
};
1042 ret
= client_open(argc
, argv
, &st
->pcd
);
1044 set_stat_task_restart_barrier();
1047 st
->pcd
->task
.event_handler
= client_task_event_handler
;
1048 s
->timeout
.tv_sec
= 0;
1049 s
->timeout
.tv_usec
= 1;
1052 static void status_post_select(__a_unused
struct sched
*s
, struct task
*t
)
1054 struct status_task
*st
= t
->private_data
;
1055 unsigned bytes_left
;
1058 if (!st
->pcd
|| st
->pcd
->status
!= CL_RECEIVING
)
1060 if (st
->pcd
&& audiod_status
== AUDIOD_OFF
) {
1061 unregister_task(&st
->pcd
->task
);
1063 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1066 bytes_left
= for_each_line(st
->pcd
->buf
, st
->pcd
->loaded
,
1067 &check_stat_line
, NULL
);
1068 if (st
->pcd
->loaded
!= bytes_left
) {
1069 st
->last_status_read
= *now
;
1070 st
->pcd
->loaded
= bytes_left
;
1072 struct timeval diff
;
1073 tv_diff(now
, &st
->last_status_read
, &diff
);
1074 if (diff
.tv_sec
> 61)
1079 static void init_status_task(struct status_task
*st
)
1081 memset(st
, 0, sizeof(struct status_task
));
1082 st
->task
.pre_select
= status_pre_select
;
1083 st
->task
.post_select
= status_post_select
;
1084 st
->task
.private_data
= st
;
1085 st
->sa_time_diff_sign
= 1;
1086 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1087 sprintf(st
->task
.status
, "status task");
1090 static void set_initial_status(void)
1092 audiod_status
= AUDIOD_ON
;
1093 if (!conf
.mode_given
)
1095 if (!strcmp(conf
.mode_arg
, "sb")) {
1096 audiod_status
= AUDIOD_STANDBY
;
1099 if (!strcmp(conf
.mode_arg
, "off")) {
1100 audiod_status
= AUDIOD_OFF
;
1103 if (strcmp(conf
.mode_arg
, "on"))
1104 PARA_WARNING_LOG("%s", "invalid mode\n");
1108 * the main function of para_audiod
1110 * \param argc usual argument count
1111 * \param argv usual argument vector
1113 * \return EXIT_SUCCESS or EXIT_FAILURE
1115 * \sa para_audiod(1)
1117 int main(int argc
, char *argv
[])
1122 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1123 struct task audiod_task_struct
, *audiod_task
= &audiod_task_struct
;
1126 audiod_cmdline_parser(argc
, argv
, &conf
);
1127 HANDLE_VERSION_FLAG("audiod", conf
);
1128 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1129 config_file
= configfile_exists();
1131 struct audiod_cmdline_parser_params params
= {
1134 .check_required
= 0,
1135 .check_ambiguity
= 0
1138 if (audiod_cmdline_parser_config_file(config_file
, &conf
, ¶ms
)) {
1139 PARA_EMERG_LOG("%s", "parse error in config file\n");
1144 if (conf
.logfile_given
)
1145 logfile
= open_log(conf
.logfile_arg
);
1146 log_welcome("para_audiod", conf
.loglevel_arg
);
1147 i
= init_stream_io();
1149 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1152 server_uptime(UPTIME_SET
);
1153 set_initial_status();
1157 setup_signal_handling();
1158 signal_setup_default(sig_task
);
1159 sig_task
->task
.event_handler
= signal_event_handler
;
1161 init_status_task(stat_task
);
1162 init_command_task(cmd_task
);
1163 init_audiod_task(audiod_task
);
1165 if (conf
.daemon_given
)
1168 register_task(&sig_task
->task
);
1169 register_task(&cmd_task
->task
);
1170 register_task(&stat_task
->task
);
1171 register_task(audiod_task
);
1172 s
.default_timeout
.tv_sec
= 0;
1173 s
.default_timeout
.tv_usec
= 99 * 1000;
1176 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
1177 return EXIT_FAILURE
;