2 * Copyright (C) 2005-2009 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 */
14 #include "audiod.cmdline.h"
20 #include "grab_client.cmdline.h"
21 #include "grab_client.h"
22 #include "client.cmdline.h"
30 #include "write_common.h"
33 /** define the array of error lists needed by para_audiod */
35 /** define the array containing all supported audio formats */
36 const char *audio_formats
[] = {AUDIOD_AUDIO_FORMAT_ARRAY NULL
};
38 /** Defines how audiod handles one supported audio format. */
39 struct audio_format_info
{
40 /** pointer to the receiver for this audio format */
41 struct receiver
*receiver
;
42 /** the receiver configuration */
44 /** the number of filters that should be activated for this audio format */
45 unsigned int num_filters
;
46 /** Array of filter numbers to be activated. */
47 unsigned *filter_nums
;
48 /** Pointer to the array of filter configurations. */
50 /** the number of filters that should be activated for this audio format */
51 unsigned int num_writers
;
52 /** pointer to the array of writers to be activated */
53 struct writer
**writers
;
54 /** pointer to the array of writer configurations */
56 /** do not start receiver/filters/writer before this time */
57 struct timeval restart_barrier
;
61 * para_audiod uses \p MAX_STREAM_SLOTS different slots, each of which may
62 * be associated with a receiver/filter/writer triple. This array holds all
63 * information on the status of these slots.
65 * \sa struct slot_info
67 struct slot_info slot
[MAX_STREAM_SLOTS
];
69 /** The vss status flags audiod is interested in. */
70 enum vss_status_flags
{
71 /** Whether the 'N' flag is set. */
72 VSS_STATUS_FLAG_NEXT
= 1,
73 /** The 'P' flag is set. */
74 VSS_STATUS_FLAG_PLAYING
= 2,
78 * The task for obtaining para_server's status (para_client stat).
80 * \sa struct task, struct sched.
83 /** The associated task structure of audiod. */
85 /** Client data associated with the stat task. */
86 struct client_task
*ct
;
87 /** Do not restart client command until this time. */
88 struct timeval restart_barrier
;
89 /** Last time we received status data from para_server. */
90 struct timeval last_status_read
;
91 /** The offset value announced by para_server. */
93 /** The length of the current audio file as announced by para_server. */
95 /** The start of the current stream from the view of para_server. */
96 struct timeval server_stream_start
;
97 /** The average time deviation between para_server and para_audiod. */
98 struct timeval sa_time_diff
;
99 /** Whether client time is ahead of server time. */
100 int sa_time_diff_sign
;
101 /** The 'P' and the 'N' flags as announced by para_server. */
102 enum vss_status_flags vss_status
;
103 /** Number of times the clock difference is to be checked. */
104 unsigned clock_diff_count
;
105 /** When to start the next check for clock difference. */
106 struct timeval clock_diff_barrier
;
107 /** Number of the audio format as announced by para_server. */
108 int current_audio_format_num
;
111 /** The array of status items sent by para_server. */
112 char *stat_item_values
[NUM_STAT_ITEMS
] = {NULL
};
115 * the current mode of operation of which can be changed by the on/off/cycle
116 * commands. It is either, AUDIOD_OFF, AUDIOD_ON or AUDIOD_STANDBY.
118 int audiod_status
= AUDIOD_ON
;
121 * the gengetopt args_info struct that holds information on all command line
124 struct audiod_args_info conf
;
126 static char *socket_name
;
127 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
129 static struct signal_task signal_task_struct
, *sig_task
= &signal_task_struct
;
131 static struct status_task status_task_struct
;
134 * the task that calls the status command of para_server
136 * \sa struct status_task
138 static struct status_task
*stat_task
= &status_task_struct
;
139 static struct timeval initial_delay_barrier
;
142 * the task for handling audiod commands
144 * \sa struct task, struct sched
146 struct command_task
{
147 /** the local listening socket */
149 /** the associated task structure */
153 /** iterate over all supported audio formats */
154 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
157 * get the audio format number
158 * \param name the name of the audio format
160 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
161 * \a name is not a supported audio format.
163 int get_audio_format_num(char *name
)
167 while (para_isspace(*name
))
169 FOR_EACH_AUDIO_FORMAT(i
)
170 if (!strcmp(name
, audio_formats
[i
]))
172 return -E_UNSUPPORTED_AUDIO_FORMAT
;
175 char *get_time_string(int slot_num
)
177 int ret
, seconds
= 0, length
;
178 struct timeval
*tmp
, sum
, sss
, /* server stream start */
179 wtime
, /* now - writer start */
180 rskip
; /* receiver start - sss */
181 struct slot_info
*s
= slot_num
< 0? NULL
: &slot
[slot_num
];
183 if (audiod_status
== AUDIOD_OFF
)
185 if (!(stat_task
->vss_status
& VSS_STATUS_FLAG_PLAYING
)) {
186 if (stat_task
->length_seconds
) /* paused */
188 goto empty
; /* stopped */
190 if (audiod_status
== AUDIOD_ON
&& !s
)
192 /* valid status items and playing */
193 if (s
) { /* writer active in this slot */
194 length
= s
->seconds_total
;
195 tmp
= &s
->server_stream_start
;
196 } else { /* standby mode, rely on status items */
197 length
= stat_task
->length_seconds
;
198 tmp
= &stat_task
->server_stream_start
;
200 if (stat_task
->sa_time_diff_sign
> 0)
201 tv_diff(tmp
, &stat_task
->sa_time_diff
, &sss
);
203 tv_add(tmp
, &stat_task
->sa_time_diff
, &sss
);
206 tv_diff(now
, &sss
, &diff
);
207 seconds
= diff
.tv_sec
+ stat_task
->offset_seconds
;
210 tv_diff(now
, &s
->wstime
, &wtime
);
211 seconds
= s
->offset_seconds
;
212 ret
= tv_diff(&s
->rstime
, &sss
, &rskip
);
213 if (ret
> 0) { /* audiod was started in the middle of the stream */
214 tv_add(&wtime
, &rskip
, &sum
);
215 seconds
+= sum
.tv_sec
;
217 seconds
+= wtime
.tv_sec
;
219 seconds
= PARA_MIN(seconds
, length
);
220 seconds
= PARA_MAX(seconds
, 0);
222 "%s: %s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
223 status_item_list
[SI_PLAY_TIME
],
227 (length
- seconds
) / 60,
228 (length
- seconds
) % 60,
229 length
? (seconds
* 100 + length
/ 2) / length
: 0,
234 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
237 static int want_colors(void)
239 if (conf
.color_arg
== color_arg_no
)
241 if (conf
.color_arg
== color_arg_yes
)
243 if (conf
.logfile_given
)
245 return isatty(STDERR_FILENO
);
248 static void parse_config_or_die(void)
252 struct audiod_cmdline_parser_params params
= {
256 .check_ambiguity
= 0,
260 if (conf
.config_file_given
)
261 config_file
= para_strdup(conf
.config_file_arg
);
263 char *home
= para_homedir();
264 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
267 ret
= file_exists(config_file
);
268 if (conf
.config_file_given
&& !ret
) {
269 PARA_EMERG_LOG("can not read config file %s\n", config_file
);
273 audiod_cmdline_parser_config_file(config_file
, &conf
, ¶ms
);
275 daemon_set_loglevel(conf
.loglevel_arg
);
282 static void setup_signal_handling(void)
284 sig_task
->fd
= para_signal_init();
285 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
286 para_install_sighandler(SIGINT
);
287 para_install_sighandler(SIGTERM
);
288 para_install_sighandler(SIGHUP
);
289 para_sigaction(SIGPIPE
, SIG_IGN
);
292 static void clear_slot(int slot_num
)
294 struct slot_info
*s
= &slot
[slot_num
];
296 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
297 memset(s
, 0, sizeof(struct slot_info
));
301 static void close_receiver(int slot_num
)
303 struct slot_info
*s
= &slot
[slot_num
];
304 struct audio_format_info
*a
;
306 if (s
->format
< 0 || !s
->receiver_node
)
309 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
310 audio_formats
[s
->format
], slot_num
);
311 a
->receiver
->close(s
->receiver_node
);
312 free(s
->receiver_node
);
313 s
->receiver_node
= NULL
;
316 static void kill_all_decoders(int error
)
321 struct slot_info
*s
= &slot
[i
];
322 if (s
->wng
&& s
->wng
->task
.error
>= 0) {
323 PARA_INFO_LOG("deactivating wng in slot %d\n",
325 s
->wng
->task
.error
= error
;
327 if (s
->fc
&& s
->fc
->task
.error
>= 0) {
328 PARA_INFO_LOG("deactivatimg filter chain in slot %d\n", i
);
329 s
->fc
->task
.error
= error
;
331 if (s
->receiver_node
&& s
->receiver_node
->task
.error
>= 0) {
332 PARA_INFO_LOG("deactivating receiver_node in slot %d\n", i
);
333 s
->receiver_node
->task
.error
= error
;
338 static int get_empty_slot(void)
349 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
354 return -E_NO_MORE_SLOTS
;
358 * get the number of filters
360 * \param audio_format_num the number identifying the audio format
362 * \return the number of filters for the given audio format
366 int num_filters(int audio_format_num
)
368 return afi
[audio_format_num
].num_filters
;
371 static void open_filters(int slot_num
)
373 struct slot_info
*s
= &slot
[slot_num
];
374 struct audio_format_info
*a
= &afi
[s
->format
];
375 struct filter_node
*fn
;
376 int nf
= a
->num_filters
;
382 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
383 s
->fc
= para_calloc(sizeof(struct filter_chain
));
384 s
->fc
->filter_nodes
= para_malloc(nf
* sizeof(struct filter_node
));
385 s
->fc
->inbufp
= &s
->receiver_node
->buf
;
386 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
387 s
->fc
->input_error
= &s
->receiver_node
->task
.error
;
388 s
->fc
->task
.pre_select
= filter_pre_select
;
389 s
->fc
->task
.post_select
= NULL
;
390 s
->fc
->task
.error
= 0;
391 s
->fc
->num_filters
= nf
;
393 s
->receiver_node
->output_error
= &s
->fc
->task
.error
;
394 sprintf(s
->fc
->task
.status
, "filter chain");
395 FOR_EACH_FILTER_NODE(fn
, s
->fc
, i
) {
396 struct filter
*f
= filters
+ a
->filter_nums
[i
];
397 fn
->filter_num
= a
->filter_nums
[i
];
398 fn
->conf
= a
->filter_conf
[i
];
401 INIT_LIST_HEAD(&fn
->callbacks
);
403 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
404 audio_formats
[s
->format
], i
, nf
, f
->name
, slot_num
);
405 s
->fc
->outbufp
= &fn
->buf
;
406 s
->fc
->out_loaded
= &fn
->loaded
;
408 register_task(&s
->fc
->task
);
411 static void open_writers(int slot_num
)
414 struct slot_info
*s
= &slot
[slot_num
];
415 struct audio_format_info
*a
= &afi
[s
->format
];
417 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
419 s
->wng
= setup_default_wng();
421 s
->wng
= wng_new(a
->num_writers
);
423 s
->wng
->bufp
= s
->fc
->outbufp
;
424 s
->wng
->loaded
= s
->fc
->out_loaded
;
425 s
->wng
->input_error
= &s
->fc
->task
.error
;
426 s
->wng
->channels
= &s
->fc
->channels
;
427 s
->wng
->samplerate
= &s
->fc
->samplerate
;
428 s
->fc
->output_error
= &s
->wng
->task
.error
;
429 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
431 s
->wng
->bufp
= &s
->receiver_node
->buf
;
432 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
433 s
->wng
->input_error
= &s
->receiver_node
->task
.error
;
435 for (i
= 0; i
< a
->num_writers
; i
++) {
436 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
437 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
439 ret
= wng_open(s
->wng
);
443 s
->server_stream_start
= stat_task
->server_stream_start
.tv_sec
?
444 stat_task
->server_stream_start
: *now
;
445 s
->offset_seconds
= stat_task
->offset_seconds
;
446 s
->seconds_total
= stat_task
->length_seconds
;
447 activate_inactive_grab_clients(slot_num
, s
->format
, s
->fc
);
450 static int open_receiver(int format
)
452 struct audio_format_info
*a
= &afi
[format
];
455 struct receiver_node
*rn
;
456 const struct timeval restart_delay
= {2, 0};
458 ret
= get_empty_slot();
464 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
465 rn
= s
->receiver_node
;
466 rn
->receiver
= a
->receiver
;
467 rn
->conf
= a
->receiver_conf
;
468 ret
= a
->receiver
->open(s
->receiver_node
);
470 free(s
->receiver_node
);
471 s
->receiver_node
= NULL
;
474 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
475 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
476 rn
->task
.pre_select
= a
->receiver
->pre_select
;
477 rn
->task
.post_select
= a
->receiver
->post_select
;
479 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
480 register_task(&rn
->task
);
484 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
485 tv_add(now
, &restart_delay
, &afi
[format
].restart_barrier
);
489 /* return: 0: Not running, 1: Running, -1: Running but eof (or error) */
490 static int receiver_running(int format
)
495 struct slot_info
*s
= &slot
[i
];
496 if (s
->format
!= format
)
498 if (!s
->receiver_node
)
500 if (s
->receiver_node
->task
.error
>= 0)
507 static void open_current_receiver(struct sched
*s
)
510 int ret
, cafn
= stat_task
->current_audio_format_num
;
512 if (cafn
< 0 || !stat_task
->ct
)
514 /* Do nothing if the 'N' flag is set or the 'P' flag is unset */
515 if (stat_task
->vss_status
!= VSS_STATUS_FLAG_PLAYING
)
517 ret
= receiver_running(cafn
);
518 if (ret
> 0) /* already running and not eof */
520 if (ret
< 0) { /* eof */
522 * para_server uses a zero start time during the announcement
523 * period, i.e. before it sends the first chunk. Wait until
524 * this period begins to avoid restarting the receiver that
525 * belongs to the file just completed.
527 if (stat_task
->server_stream_start
.tv_sec
)
530 if (tv_diff(now
, &afi
[cafn
].restart_barrier
, &diff
) < 0) {
531 /* avoid busy loop */
535 /* start a new receiver */
539 static unsigned compute_time_diff(const struct timeval
*status_time
)
541 struct timeval tmp
, diff
;
542 static unsigned count
;
543 int sign
, sa_time_diff_sign
= stat_task
->sa_time_diff_sign
;
544 const struct timeval max_deviation
= {0, 500 * 1000};
545 const int time_smooth
= 5;
549 sign
= tv_diff(status_time
, now
, &diff
);
550 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
551 // sign, sa_time_diff_sign);
553 sa_time_diff_sign
= sign
;
554 stat_task
->sa_time_diff
= diff
;
559 int s
= tv_diff(&diff
, &stat_task
->sa_time_diff
, &tmp
);
560 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
561 PARA_WARNING_LOG("time diff jump: %lims\n",
565 sa_time_diff_sign
= tv_convex_combination(
566 sa_time_diff_sign
* time_smooth
, &stat_task
->sa_time_diff
,
567 count
> 10? sign
: sign
* time_smooth
, &diff
,
569 stat_task
->sa_time_diff
= tmp
;
570 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
573 sa_time_diff_sign
? "+" : "-",
574 tv2ms(&stat_task
->sa_time_diff
)
577 stat_task
->sa_time_diff_sign
= sa_time_diff_sign
;
581 static int check_stat_line(char *line
, __a_unused
void *data
)
585 long unsigned sec
, usec
;
588 //PARA_INFO_LOG("line: %s\n", line);
591 itemnum
= stat_line_valid(line
);
593 PARA_WARNING_LOG("invalid status line: %s\n", line
);
596 if (stat_task
->clock_diff_count
&& itemnum
!= SI_CURRENT_TIME
)
598 tmp
= make_message("%s\n", line
);
599 stat_client_write(tmp
, itemnum
);
601 free(stat_item_values
[itemnum
]);
602 stat_item_values
[itemnum
] = para_strdup(line
);
603 ilen
= strlen(status_item_list
[itemnum
]);
605 case SI_STATUS_FLAGS
:
606 stat_task
->vss_status
= 0;
607 if (strchr(line
, 'N'))
608 stat_task
->vss_status
|= VSS_STATUS_FLAG_NEXT
;
609 if (strchr(line
, 'P'))
610 stat_task
->vss_status
|= VSS_STATUS_FLAG_PLAYING
;
613 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
615 case SI_SECONDS_TOTAL
:
616 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
618 case SI_STREAM_START
:
619 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
620 struct timeval a_start
, delay
;
621 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
622 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
623 stat_task
->server_stream_start
.tv_sec
= sec
;
624 stat_task
->server_stream_start
.tv_usec
= usec
;
625 if (compute_time_diff(NULL
) > 2) {
626 if (stat_task
->sa_time_diff_sign
< 0)
627 tv_add(&stat_task
->server_stream_start
,
628 &stat_task
->sa_time_diff
, &a_start
);
630 tv_diff(&stat_task
->server_stream_start
,
631 &stat_task
->sa_time_diff
, &a_start
);
632 tv_add(&a_start
, &delay
, &initial_delay_barrier
);
636 case SI_CURRENT_TIME
:
637 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
638 struct timeval tv
= {sec
, usec
};
639 compute_time_diff(&tv
);
643 stat_task
->current_audio_format_num
= get_audio_format_num(
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
->filter_nums
[nf
] = filter_num
;
676 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
,
677 filters
[filter_num
].name
);
681 static int parse_writer_args(void)
685 struct audio_format_info
*a
;
687 nw
= PARA_MAX(1U, conf
.writer_given
);
688 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
689 FOR_EACH_AUDIO_FORMAT(i
) {
691 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
692 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
695 for (i
= 0; i
< conf
.writer_given
; i
++) {
698 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
703 wconf
= check_writer_arg(cmd
, &writer_num
);
708 a
->writers
[nw
] = &writers
[writer_num
];
709 a
->writer_conf
[nw
] = wconf
;
710 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
711 nw
, writer_names
[writer_num
]);
719 static int parse_receiver_args(void)
721 int i
, ret
, receiver_num
;
723 struct audio_format_info
*a
;
725 for (i
= conf
.receiver_given
- 1; i
>= 0; i
--) {
726 char *arg
= conf
.receiver_arg
[i
];
727 char *recv_arg
= strchr(arg
, ':');
728 ret
= -E_MISSING_COLON
;
733 ret
= get_audio_format_num(arg
);
736 afi
[ret
].receiver_conf
= check_receiver_arg(recv_arg
, &receiver_num
);
737 if (!afi
[ret
].receiver_conf
) {
738 ret
= -E_RECV_SYNTAX
;
741 afi
[ret
].receiver
= &receivers
[receiver_num
];
743 /* use the first available receiver with no arguments
744 * for those audio formats for which no receiver
747 cmd
= para_strdup(receivers
[0].name
);
748 FOR_EACH_AUDIO_FORMAT(i
) {
750 if (a
->receiver_conf
)
752 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
753 if (!a
->receiver_conf
)
754 return -E_RECV_SYNTAX
;
755 a
->receiver
= &receivers
[receiver_num
];
763 static int init_default_filters(void)
767 FOR_EACH_AUDIO_FORMAT(i
) {
768 struct audio_format_info
*a
= &afi
[i
];
773 continue; /* no default -- nothing to to */
774 /* add "dec" to audio format name */
775 tmp
= make_message("%sdec", audio_formats
[i
]);
776 for (j
= 0; filters
[j
].name
; j
++)
777 if (!strcmp(tmp
, filters
[j
].name
))
780 ret
= -E_UNSUPPORTED_FILTER
;
781 if (!filters
[j
].name
)
783 tmp
= para_strdup(filters
[j
].name
);
784 ret
= add_filter(i
, tmp
);
788 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
795 static int parse_filter_args(void)
799 nf
= PARA_MAX(1U, conf
.filter_given
);
800 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
801 FOR_EACH_AUDIO_FORMAT(i
) {
802 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
803 afi
[i
].filter_nums
= para_malloc(nf
* sizeof(unsigned));
805 if (!conf
.no_default_filters_given
)
806 return init_default_filters();
807 for (i
= 0; i
< conf
.filter_given
; i
++) {
808 char *arg
= conf
.filter_arg
[i
];
809 char *filter_name
= strchr(arg
, ':');
810 ret
= -E_MISSING_COLON
;
815 ret
= get_audio_format_num(arg
);
818 ret
= add_filter(ret
, filter_name
);
822 ret
= init_default_filters(); /* use default values for the rest */
827 static int parse_stream_args(void)
831 ret
= parse_receiver_args();
834 ret
= parse_filter_args();
837 ret
= parse_writer_args();
843 /* does not unlink socket on errors */
844 static int audiod_get_socket(void)
846 struct sockaddr_un unix_addr
;
849 if (conf
.socket_given
)
850 socket_name
= para_strdup(conf
.socket_arg
);
852 char *hn
= para_hostname();
853 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
857 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
858 if (conf
.force_given
)
860 ret
= create_local_socket(socket_name
, &unix_addr
,
861 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
865 if (listen(fd
, 5) < 0) {
866 ret
= -ERRNO_TO_PARA_ERROR(errno
);
869 ret
= mark_fd_nonblocking(fd
);
874 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
878 static void signal_pre_select(struct sched
*s
, struct task
*t
)
880 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
881 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
884 static void signal_post_select(struct sched
*s
, struct task
*t
)
886 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
888 if (!FD_ISSET(st
->fd
, &s
->rfds
))
891 st
->signum
= para_next_signal();
892 switch (st
->signum
) {
896 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
897 clean_exit(EXIT_FAILURE
, "caught deadly 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 sprintf(st
->task
.status
, "signal task");
908 static void command_pre_select(struct sched
*s
, struct task
*t
)
910 struct command_task
*ct
= container_of(t
, struct command_task
, task
);
911 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
914 static void command_post_select(struct sched
*s
, struct task
*t
)
917 struct command_task
*ct
= container_of(t
, struct command_task
, task
);
919 audiod_status_dump();
920 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
922 ret
= handle_connect(ct
->fd
);
924 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
927 static void init_command_task(struct command_task
*ct
)
929 ct
->task
.pre_select
= command_pre_select
;
930 ct
->task
.post_select
= command_post_select
;
932 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
933 sprintf(ct
->task
.status
, "command task");
936 static void close_stat_pipe(void)
942 client_close(stat_task
->ct
);
943 stat_task
->ct
= NULL
;
944 FOR_EACH_STATUS_ITEM(i
) {
945 free(stat_item_values
[i
]);
946 stat_item_values
[i
] = NULL
;
949 stat_task
->length_seconds
= 0;
950 stat_task
->offset_seconds
= 0;
951 stat_task
->vss_status
= 0;
952 audiod_status_dump();
956 * close the connection to para_server and exit
958 * \param status the exit status which is passed to exit(3)
959 * \param msg the log message
961 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
962 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
967 void __noreturn
clean_exit(int status
, const char *msg
)
969 PARA_EMERG_LOG("%s\n", msg
);
976 /* avoid busy loop if server is down */
977 static void set_stat_task_restart_barrier(unsigned seconds
)
979 struct timeval delay
= {seconds
, 0};
980 tv_add(now
, &delay
, &stat_task
->restart_barrier
);
983 static void try_to_close_slot(int slot_num
)
985 struct slot_info
*s
= &slot
[slot_num
];
989 if (s
->receiver_node
&& s
->receiver_node
->task
.error
!= -E_TASK_UNREGISTERED
)
991 if (s
->fc
&& s
->fc
->task
.error
!= -E_TASK_UNREGISTERED
)
993 if (s
->wng
&& s
->wng
->task
.error
!= -E_TASK_UNREGISTERED
)
995 PARA_INFO_LOG("closing slot %d\n", slot_num
);
997 close_filters(s
->fc
);
999 close_receiver(slot_num
);
1000 clear_slot(slot_num
);
1004 * Check if any receivers/filters/writers need to be started and do so if
1007 static void start_stop_decoders(struct sched
*s
)
1012 try_to_close_slot(i
);
1013 if (audiod_status
!= AUDIOD_ON
||
1014 !(stat_task
->vss_status
& VSS_STATUS_FLAG_PLAYING
))
1015 return kill_all_decoders(-E_NOT_PLAYING
);
1016 open_current_receiver(s
);
1018 struct slot_info
*sl
= &slot
[i
];
1019 struct audio_format_info
*a
;
1020 struct timeval diff
;
1024 a
= &afi
[sl
->format
];
1025 if (!sl
->receiver_node
)
1027 if ((!a
->num_filters
|| sl
->fc
) && sl
->wng
)
1028 continue; /* everything already started */
1029 if (!a
->num_filters
) {
1030 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
1035 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
1039 if (sl
->wng
|| !sl
->fc
|| !*sl
->fc
->out_loaded
)
1041 if (tv_diff(now
, &initial_delay_barrier
, &diff
) > 0) {
1045 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff
));
1046 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0) {
1053 /* restart the client task if necessary */
1054 static void status_pre_select(struct sched
*s
, struct task
*t
)
1056 struct status_task
*st
= container_of(t
, struct status_task
, task
);
1058 if (audiod_status
== AUDIOD_OFF
) {
1061 if (st
->ct
->task
.error
>= 0) {
1062 st
->ct
->task
.error
= -E_AUDIOD_OFF
;
1065 if (st
->ct
->task
.error
!= -E_TASK_UNREGISTERED
)
1068 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1072 unsigned bytes_left
;
1073 if (st
->ct
->task
.error
< 0) {
1074 if (st
->ct
->task
.error
!= -E_TASK_UNREGISTERED
)
1079 if (st
->ct
->status
!= CL_RECEIVING
)
1081 bytes_left
= for_each_line(st
->ct
->buf
, st
->ct
->loaded
,
1082 &check_stat_line
, NULL
);
1083 if (st
->ct
->loaded
!= bytes_left
) {
1084 st
->last_status_read
= *now
;
1085 st
->ct
->loaded
= bytes_left
;
1087 struct timeval diff
;
1088 tv_diff(now
, &st
->last_status_read
, &diff
);
1089 if (diff
.tv_sec
> 61)
1090 st
->ct
->task
.error
= -E_STATUS_TIMEOUT
;
1094 if (tv_diff(now
, &st
->restart_barrier
, NULL
) < 0)
1096 if (st
->clock_diff_count
) { /* get status only one time */
1097 char *argv
[] = {"audiod", "stat", "1", NULL
};
1099 PARA_INFO_LOG("clock diff count: %d\n", st
->clock_diff_count
);
1100 st
->clock_diff_count
--;
1101 client_open(argc
, argv
, &st
->ct
, NULL
);
1102 set_stat_task_restart_barrier(2);
1105 char *argv
[] = {"audiod", "stat", NULL
};
1107 client_open(argc
, argv
, &st
->ct
, NULL
);
1108 set_stat_task_restart_barrier(5);
1110 free(stat_item_values
[SI_BASENAME
]);
1111 stat_item_values
[SI_BASENAME
] = make_message(
1112 "%s: no connection to para_server\n",
1113 status_item_list
[SI_BASENAME
]);
1114 stat_client_write(stat_item_values
[SI_BASENAME
],
1116 st
->last_status_read
= *now
;
1118 start_stop_decoders(s
);
1121 static void init_status_task(struct status_task
*st
)
1123 memset(st
, 0, sizeof(struct status_task
));
1124 st
->task
.pre_select
= status_pre_select
;
1125 st
->sa_time_diff_sign
= 1;
1126 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1127 st
->current_audio_format_num
= -1;
1128 sprintf(st
->task
.status
, "status task");
1131 static void set_initial_status(void)
1133 audiod_status
= AUDIOD_ON
;
1134 if (!conf
.mode_given
)
1136 if (!strcmp(conf
.mode_arg
, "sb")) {
1137 audiod_status
= AUDIOD_STANDBY
;
1140 if (!strcmp(conf
.mode_arg
, "off")) {
1141 audiod_status
= AUDIOD_OFF
;
1144 if (strcmp(conf
.mode_arg
, "on"))
1145 PARA_WARNING_LOG("invalid mode\n");
1148 __noreturn
static void print_help_and_die(void)
1150 int d
= conf
.detailed_help_given
;
1151 const char **p
= d
? audiod_args_info_detailed_help
1152 : audiod_args_info_help
;
1154 printf_or_die("%s\n\n", AUDIOD_CMDLINE_PARSER_PACKAGE
"-"
1155 AUDIOD_CMDLINE_PARSER_VERSION
);
1156 printf_or_die("%s\n\n", audiod_args_info_usage
);
1158 printf_or_die("%s\n", *p
);
1159 print_receiver_helps(d
);
1160 print_filter_helps(d
);
1161 print_writer_helps(d
);
1165 static void init_colors_or_die(void)
1171 daemon_set_default_log_colors();
1172 daemon_set_flag(DF_COLOR_LOG
);
1173 for (i
= 0; i
< conf
.log_color_given
; i
++) {
1174 ret
= daemon_set_log_color(conf
.log_color_arg
[i
]);
1181 * the main function of para_audiod
1183 * \param argc usual argument count
1184 * \param argv usual argument vector
1186 * \return EXIT_SUCCESS or EXIT_FAILURE
1188 * \sa para_audiod(1)
1190 int main(int argc
, char *argv
[])
1193 static struct sched s
;
1194 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1195 struct audiod_cmdline_parser_params params
= {
1198 .check_required
= 0,
1199 .check_ambiguity
= 0,
1204 if (audiod_cmdline_parser_ext(argc
, argv
, &conf
, ¶ms
))
1206 HANDLE_VERSION_FLAG("audiod", conf
);
1207 /* init receivers/filters/writers early to make help work */
1211 if (conf
.help_given
|| conf
.detailed_help_given
)
1212 print_help_and_die();
1213 drop_privileges_or_die(conf
.user_arg
, conf
.group_arg
);
1214 parse_config_or_die();
1215 init_colors_or_die();
1216 daemon_set_flag(DF_LOG_TIME
);
1217 daemon_set_flag(DF_LOG_HOSTNAME
);
1218 daemon_set_flag(DF_LOG_LL
);
1219 if (conf
.logfile_given
) {
1220 daemon_set_logfile(conf
.logfile_arg
);
1221 daemon_open_log_or_die();
1223 ret
= parse_stream_args();
1225 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
1228 log_welcome("para_audiod");
1229 server_uptime(UPTIME_SET
);
1230 set_initial_status();
1234 setup_signal_handling();
1235 signal_setup_default(sig_task
);
1237 init_status_task(stat_task
);
1238 init_command_task(cmd_task
);
1240 if (conf
.daemon_given
)
1243 register_task(&sig_task
->task
);
1244 register_task(&cmd_task
->task
);
1245 register_task(&stat_task
->task
);
1246 s
.default_timeout
.tv_sec
= 0;
1247 s
.default_timeout
.tv_usec
= 99 * 1000;
1250 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
1251 return EXIT_FAILURE
;