2 * Copyright (C) 2005-2008 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 /** Array of filter numbers to be activated. */
45 unsigned *filter_nums
;
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
)
147 if (!logfile
&& conf
.daemon_given
)
150 hostname
= para_hostname();
151 outfd
= logfile
? logfile
: stderr
;
154 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
155 fprintf(outfd
, "%s %s ", str
, hostname
);
156 if (conf
.loglevel_arg
<= INFO
)
157 fprintf(outfd
, "%i ", ll
);
159 vfprintf(outfd
, fmt
, argp
);
163 static char *configfile_exists(void)
165 char *home
= para_homedir();
166 char *config_file
= make_message("%s/.paraslash/audiod.conf",
169 if (file_exists(config_file
))
175 static void setup_signal_handling(void)
177 sig_task
->fd
= para_signal_init();
178 PARA_INFO_LOG("signal pipe: fd %d\n", sig_task
->fd
);
179 para_install_sighandler(SIGINT
);
180 para_install_sighandler(SIGTERM
);
181 para_install_sighandler(SIGHUP
);
182 signal(SIGPIPE
, SIG_IGN
);
185 static void clear_slot(int slot_num
)
187 struct slot_info
*s
= &slot
[slot_num
];
189 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
190 memset(s
, 0, sizeof(struct slot_info
));
194 static void close_receiver(int slot_num
)
196 struct slot_info
*s
= &slot
[slot_num
];
197 struct audio_format_info
*a
;
199 if (s
->format
< 0 || !s
->receiver_node
)
202 PARA_NOTICE_LOG("closing %s receiver in slot %d\n",
203 audio_formats
[s
->format
], slot_num
);
204 a
->receiver
->close(s
->receiver_node
);
205 free(s
->receiver_node
);
206 s
->receiver_node
= NULL
;
209 static void kill_all_decoders(int error
)
214 struct slot_info
*s
= &slot
[i
];
215 if (s
->wng
&& s
->wng
->task
.error
>= 0) {
216 PARA_INFO_LOG("deactivating wng in slot %d\n",
218 s
->wng
->task
.error
= error
;
220 if (s
->fc
&& s
->fc
->task
.error
>= 0) {
221 PARA_INFO_LOG("deactivatimg filter chain in slot %d\n", i
);
222 s
->fc
->task
.error
= error
;
224 if (s
->receiver_node
&& s
->receiver_node
->task
.error
>= 0) {
225 PARA_INFO_LOG("deactivating receiver_node in slot %d\n", i
);
226 s
->receiver_node
->task
.error
= error
;
231 static int get_empty_slot(void)
242 if (s
->wng
|| s
->receiver_node
|| s
->fc
)
247 return -E_NO_MORE_SLOTS
;
251 * get the number of filters
253 * \param audio_format_num the number identifying the audio format
255 * \return the number of filters for the given audio format
259 int num_filters(int audio_format_num
)
261 return afi
[audio_format_num
].num_filters
;
264 static void open_filters(int slot_num
)
266 struct slot_info
*s
= &slot
[slot_num
];
267 struct audio_format_info
*a
= &afi
[s
->format
];
268 struct filter_node
*fn
;
269 int nf
= a
->num_filters
;
275 PARA_INFO_LOG("opening %s filters\n", audio_formats
[s
->format
]);
276 s
->fc
= para_calloc(sizeof(struct filter_chain
));
277 s
->fc
->filter_nodes
= para_malloc(nf
* sizeof(struct filter_chain
));
278 s
->fc
->inbuf
= s
->receiver_node
->buf
;
279 s
->fc
->in_loaded
= &s
->receiver_node
->loaded
;
280 s
->fc
->input_error
= &s
->receiver_node
->task
.error
;
281 s
->fc
->task
.pre_select
= filter_pre_select
;
282 s
->fc
->task
.post_select
= NULL
;
283 s
->fc
->task
.error
= 0;
284 s
->fc
->num_filters
= nf
;
286 s
->receiver_node
->output_error
= &s
->fc
->task
.error
;
287 sprintf(s
->fc
->task
.status
, "filter chain");
288 FOR_EACH_FILTER_NODE(fn
, s
->fc
, i
) {
289 struct filter
*f
= filters
+ a
->filter_nums
[i
];
290 fn
->filter_num
= a
->filter_nums
[i
];
291 fn
->conf
= a
->filter_conf
[i
];
294 INIT_LIST_HEAD(&fn
->callbacks
);
296 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
297 audio_formats
[s
->format
], i
+ 1, nf
, f
->name
, slot_num
);
298 s
->fc
->outbuf
= fn
->buf
;
299 s
->fc
->out_loaded
= &fn
->loaded
;
301 register_task(&s
->fc
->task
);
304 static void open_writers(int slot_num
)
307 struct slot_info
*s
= &slot
[slot_num
];
308 struct audio_format_info
*a
= &afi
[s
->format
];
310 PARA_INFO_LOG("opening %s writers\n", audio_formats
[s
->format
]);
312 s
->wng
= setup_default_wng();
314 s
->wng
= wng_new(a
->num_writers
);
316 s
->wng
->buf
= s
->fc
->outbuf
;
317 s
->wng
->loaded
= s
->fc
->out_loaded
;
318 s
->wng
->input_error
= &s
->fc
->task
.error
;
319 s
->wng
->channels
= &s
->fc
->channels
;
320 s
->wng
->samplerate
= &s
->fc
->samplerate
;
321 s
->fc
->output_error
= &s
->wng
->task
.error
;
322 PARA_INFO_LOG("samplerate: %d\n", *s
->wng
->samplerate
);
324 s
->wng
->buf
= s
->receiver_node
->buf
;
325 s
->wng
->loaded
= &s
->receiver_node
->loaded
;
326 s
->wng
->input_error
= &s
->receiver_node
->task
.error
;
328 for (i
= 0; i
< a
->num_writers
; i
++) {
329 s
->wng
->writer_nodes
[i
].conf
= a
->writer_conf
[i
];
330 s
->wng
->writer_nodes
[i
].writer
= a
->writers
[i
];
332 ret
= wng_open(s
->wng
);
334 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
338 activate_inactive_grab_clients(slot_num
, s
->format
, s
->fc
);
342 static void rn_event_handler(struct task
*t
)
344 struct receiver_node
*rn
= t
->private_data
;
347 PARA_NOTICE_LOG("%s\n", para_strerror(-t
->ret
));
350 /* set restart barrier */
352 struct timeval restart_delay
= {0, 10 * 1000};
353 if (slot
[i
].receiver_node
!= rn
)
355 if (rn
->error
!= -E_RECV_EOF
)
356 /* don't reconnect immediately on errors */
357 restart_delay
.tv_sec
= 5;
358 tv_add(now
, &restart_delay
, &afi
[slot
[i
].format
].restart_barrier
);
363 static int open_receiver(int format
)
365 struct audio_format_info
*a
= &afi
[format
];
368 struct receiver_node
*rn
;
369 const struct timeval restart_delay
= {1, 0};
371 ret
= get_empty_slot();
377 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
378 rn
= s
->receiver_node
;
379 rn
->receiver
= a
->receiver
;
380 rn
->conf
= a
->receiver_conf
;
381 ret
= a
->receiver
->open(s
->receiver_node
);
383 free(s
->receiver_node
);
384 s
->receiver_node
= NULL
;
387 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
388 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
389 rn
->task
.pre_select
= a
->receiver
->pre_select
;
390 rn
->task
.post_select
= a
->receiver
->post_select
;
391 sprintf(rn
->task
.status
, "%s receiver node", rn
->receiver
->name
);
392 register_task(&rn
->task
);
395 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
396 tv_add(now
, &restart_delay
, &afi
[format
].restart_barrier
);
400 static int receiver_running(int format
)
405 struct slot_info
*s
= &slot
[i
];
406 if (s
->format
== format
&& s
->receiver_node
407 && s
->receiver_node
->task
.error
>= 0)
413 static int open_current_receiver(struct sched
*s
)
416 int cafn
= stat_task
->current_audio_format_num
;
418 if (cafn
< 0 || !stat_task
->ct
)
420 if (receiver_running(cafn
))
422 if (tv_diff(now
, &afi
[cafn
].restart_barrier
, &diff
) < 0) {
426 return open_receiver(cafn
) < 0? 0 : 1;
429 static unsigned compute_time_diff(const struct timeval
*status_time
)
431 struct timeval tmp
, diff
;
432 static unsigned count
;
433 int sign
, sa_time_diff_sign
= stat_task
->sa_time_diff_sign
;
434 const struct timeval max_deviation
= {0, 500 * 1000};
435 const int time_smooth
= 5;
439 sign
= tv_diff(status_time
, now
, &diff
);
440 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
441 // sign, sa_time_diff_sign);
443 sa_time_diff_sign
= sign
;
444 stat_task
->sa_time_diff
= diff
;
449 int s
= tv_diff(&diff
, &stat_task
->sa_time_diff
, &tmp
);
450 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
451 PARA_WARNING_LOG("time diff jump: %lims\n",
455 sa_time_diff_sign
= tv_convex_combination(
456 sa_time_diff_sign
* time_smooth
, &stat_task
->sa_time_diff
,
457 count
> 10? sign
: sign
* time_smooth
, &diff
,
459 stat_task
->sa_time_diff
= tmp
;
460 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
463 sa_time_diff_sign
? "+" : "-",
464 tv2ms(&stat_task
->sa_time_diff
)
467 stat_task
->sa_time_diff_sign
= sa_time_diff_sign
;
471 static int check_stat_line(char *line
, __a_unused
void *data
)
475 long unsigned sec
, usec
;
478 //PARA_INFO_LOG("line: %s\n", line);
481 itemnum
= stat_line_valid(line
);
483 PARA_WARNING_LOG("invalid status line: %s\n", line
);
486 if (stat_task
->clock_diff_count
&& itemnum
!= SI_CURRENT_TIME
)
488 tmp
= make_message("%s\n", line
);
489 stat_client_write(tmp
, itemnum
);
491 free(stat_task
->stat_item_values
[itemnum
]);
492 stat_task
->stat_item_values
[itemnum
] = para_strdup(line
);
493 ilen
= strlen(status_item_list
[itemnum
]);
496 stat_task
->playing
= strstr(line
, "playing")? 1 : 0;
497 PARA_INFO_LOG("stat task playing: %d\n", stat_task
->playing
);
500 stat_task
->offset_seconds
= atoi(line
+ ilen
+ 1);
502 case SI_SECONDS_TOTAL
:
503 stat_task
->length_seconds
= atoi(line
+ ilen
+ 1);
505 case SI_STREAM_START
:
506 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
507 struct timeval a_start
, delay
;
508 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
509 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
510 stat_task
->server_stream_start
.tv_sec
= sec
;
511 stat_task
->server_stream_start
.tv_usec
= usec
;
512 if (compute_time_diff(NULL
) > 2) {
513 if (stat_task
->sa_time_diff_sign
< 0)
514 tv_add(&stat_task
->server_stream_start
,
515 &stat_task
->sa_time_diff
, &a_start
);
517 tv_diff(&stat_task
->server_stream_start
,
518 &stat_task
->sa_time_diff
, &a_start
);
519 tv_add(&a_start
, &delay
, &initial_delay_barrier
);
523 case SI_CURRENT_TIME
:
524 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
525 struct timeval tv
= {sec
, usec
};
526 compute_time_diff(&tv
);
530 stat_task
->current_audio_format_num
= get_audio_format_num(
536 static int parse_stream_command(const char *txt
, char **cmd
)
538 char *p
= strchr(txt
, ':');
542 return -E_MISSING_COLON
;
544 FOR_EACH_AUDIO_FORMAT(i
) {
545 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
550 return -E_UNSUPPORTED_AUDIO_FORMAT
;
553 static int add_filter(int format
, char *cmdline
)
555 struct audio_format_info
*a
= &afi
[format
];
556 int filter_num
, nf
= a
->num_filters
;
558 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
561 a
->filter_nums
[nf
] = filter_num
;
563 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
564 filters
[filter_num
].name
);
568 static int init_writers(void)
572 struct audio_format_info
*a
;
574 init_supported_writers();
575 nw
= PARA_MAX(1, conf
.writer_given
);
576 PARA_INFO_LOG("maximal number of writers: %d\n", nw
);
577 FOR_EACH_AUDIO_FORMAT(i
) {
579 a
->writer_conf
= para_malloc(nw
* sizeof(void *));
580 a
->writers
= para_malloc(nw
* sizeof(struct writer
*));
583 for (i
= 0; i
< conf
.writer_given
; i
++) {
586 ret
= parse_stream_command(conf
.writer_arg
[i
], &cmd
);
591 wconf
= check_writer_arg(cmd
, &writer_num
);
596 a
->writers
[nw
] = &writers
[writer_num
];
597 a
->writer_conf
[nw
] = wconf
;
598 PARA_INFO_LOG("%s writer #%d: %s\n", audio_formats
[ret
],
599 nw
, writer_names
[writer_num
]);
607 static int init_receivers(void)
609 int i
, ret
, receiver_num
;
611 struct audio_format_info
*a
;
613 for (i
= 0; receivers
[i
].name
; i
++) {
614 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
615 receivers
[i
].init(&receivers
[i
]);
617 for (i
= conf
.receiver_given
- 1; i
>= 0; i
--) {
618 char *arg
= conf
.receiver_arg
[i
];
619 char *recv_arg
= strchr(arg
, ':');
620 ret
= -E_MISSING_COLON
;
625 ret
= get_audio_format_num(arg
);
628 afi
[ret
].receiver_conf
= check_receiver_arg(recv_arg
, &receiver_num
);
629 if (!afi
[ret
].receiver_conf
) {
630 ret
= -E_RECV_SYNTAX
;
633 afi
[ret
].receiver
= &receivers
[receiver_num
];
635 /* use the first available receiver with no arguments
636 * for those audio formats for which no receiver
639 cmd
= para_strdup(receivers
[0].name
);
640 FOR_EACH_AUDIO_FORMAT(i
) {
642 if (a
->receiver_conf
)
644 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
645 if (!a
->receiver_conf
)
646 return -E_RECV_SYNTAX
;
647 a
->receiver
= &receivers
[receiver_num
];
655 static int init_default_filters(void)
659 FOR_EACH_AUDIO_FORMAT(i
) {
660 struct audio_format_info
*a
= &afi
[i
];
665 continue; /* no default -- nothing to to */
666 /* add "dec" to audio format name */
667 tmp
= make_message("%sdec", audio_formats
[i
]);
668 for (j
= 0; filters
[j
].name
; j
++)
669 if (!strcmp(tmp
, filters
[j
].name
))
672 ret
= -E_UNSUPPORTED_FILTER
;
673 if (!filters
[j
].name
)
675 tmp
= para_strdup(filters
[j
].name
);
676 ret
= add_filter(i
, tmp
);
680 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
687 static int init_filters(void)
691 filter_init(filters
);
692 nf
= PARA_MAX(1, conf
.filter_given
);
693 PARA_INFO_LOG("maximal number of filters: %d\n", nf
);
694 FOR_EACH_AUDIO_FORMAT(i
) {
695 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(void *));
696 afi
[i
].filter_nums
= para_malloc(nf
* sizeof(unsigned));
698 if (!conf
.no_default_filters_given
)
699 return init_default_filters();
700 for (i
= 0; i
< conf
.filter_given
; i
++) {
701 char *arg
= conf
.filter_arg
[i
];
702 char *filter_name
= strchr(arg
, ':');
703 ret
= -E_MISSING_COLON
;
708 ret
= get_audio_format_num(arg
);
711 ret
= add_filter(ret
, filter_name
);
715 ret
= init_default_filters(); /* use default values for the rest */
720 static int init_stream_io(void)
724 ret
= init_writers();
727 ret
= init_receivers();
730 ret
= init_filters();
736 /* does not unlink socket on errors */
737 static int audiod_get_socket(void)
739 struct sockaddr_un unix_addr
;
742 if (conf
.socket_given
)
743 socket_name
= para_strdup(conf
.socket_arg
);
745 char *hn
= para_hostname();
746 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
750 PARA_NOTICE_LOG("local socket: %s\n", socket_name
);
751 if (conf
.force_given
)
753 ret
= create_local_socket(socket_name
, &unix_addr
,
754 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
758 if (listen(fd
, 5) < 0) {
759 ret
= -ERRNO_TO_PARA_ERROR(errno
);
762 ret
= mark_fd_nonblocking(fd
);
767 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
771 static void signal_pre_select(struct sched
*s
, struct task
*t
)
773 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
774 para_fd_set(st
->fd
, &s
->rfds
, &s
->max_fileno
);
777 static void signal_post_select(struct sched
*s
, struct task
*t
)
779 struct signal_task
*st
= container_of(t
, struct signal_task
, task
);
782 if (!FD_ISSET(st
->fd
, &s
->rfds
))
785 signum
= para_next_signal();
790 PARA_EMERG_LOG("terminating on signal %d\n", st
->signum
);
791 clean_exit(EXIT_FAILURE
, "caught deadly signal");
795 static void signal_setup_default(struct signal_task
*st
)
797 st
->task
.pre_select
= signal_pre_select
;
798 st
->task
.post_select
= signal_post_select
;
799 sprintf(st
->task
.status
, "signal task");
802 static void command_pre_select(struct sched
*s
, struct task
*t
)
804 struct command_task
*ct
= container_of(t
, struct command_task
, task
);
805 para_fd_set(ct
->fd
, &s
->rfds
, &s
->max_fileno
);
808 static void command_post_select(struct sched
*s
, struct task
*t
)
811 struct command_task
*ct
= container_of(t
, struct command_task
, task
);
813 audiod_status_dump();
814 if (!FD_ISSET(ct
->fd
, &s
->rfds
))
816 ret
= handle_connect(ct
->fd
);
818 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
821 static void init_command_task(struct command_task
*ct
)
823 ct
->task
.pre_select
= command_pre_select
;
824 ct
->task
.post_select
= command_post_select
;
826 ct
->fd
= audiod_get_socket(); /* doesn't return on errors */
827 sprintf(ct
->task
.status
, "command task");
830 static void close_stat_pipe(void)
836 client_close(stat_task
->ct
);
837 stat_task
->ct
= NULL
;
838 FOR_EACH_STATUS_ITEM(i
) {
839 free(stat_task
->stat_item_values
[i
]);
840 stat_task
->stat_item_values
[i
] = NULL
;
843 stat_task
->length_seconds
= 0;
844 stat_task
->offset_seconds
= 0;
845 audiod_status_dump();
846 stat_task
->playing
= 0;
847 stat_task
->stat_item_values
[SI_BASENAME
] = make_message(
848 "%s: no connection to para_server\n",
849 status_item_list
[SI_BASENAME
]);
850 stat_client_write(stat_task
->stat_item_values
[SI_BASENAME
],
855 * close the connection to para_server and exit
857 * \param status the exit status which is passed to exit(3)
858 * \param msg the log message
860 * Log \a msg with loglevel \p EMERG, close the connection to para_server if
861 * open, and call \p exit(status). \a status should be either EXIT_SUCCESS or
866 void __noreturn
clean_exit(int status
, const char *msg
)
868 PARA_EMERG_LOG("%s\n", msg
);
875 /* avoid busy loop if server is down */
876 static void set_stat_task_restart_barrier(unsigned seconds
)
878 struct timeval delay
= {seconds
, 0};
879 tv_add(now
, &delay
, &stat_task
->restart_barrier
);
882 static void try_to_close_slot(int slot_num
)
884 struct slot_info
*s
= &slot
[slot_num
];
888 if (s
->receiver_node
&& s
->receiver_node
->task
.error
!= -E_TASK_UNREGISTERED
)
890 if (s
->fc
&& s
->fc
->task
.error
!= -E_TASK_UNREGISTERED
)
892 if (s
->wng
&& s
->wng
->task
.error
!= -E_TASK_UNREGISTERED
)
894 PARA_INFO_LOG("closing slot %d\n", slot_num
);
896 close_filters(s
->fc
);
898 close_receiver(slot_num
);
899 clear_slot(slot_num
);
903 * Check if any receivers/filters/writers need to be started and do so if
906 static void start_stop_decoders(struct sched
*s
)
911 try_to_close_slot(i
);
912 if (audiod_status
!= AUDIOD_ON
|| !stat_task
->playing
)
913 return kill_all_decoders(-E_NOT_PLAYING
);
914 open_current_receiver(s
);
916 struct slot_info
*sl
= &slot
[i
];
917 struct audio_format_info
*a
;
922 a
= &afi
[sl
->format
];
923 if (!sl
->receiver_node
)
925 if ((!a
->num_filters
|| sl
->fc
) && sl
->wng
)
926 continue; /* everything already started */
927 if (!a
->num_filters
) {
928 if (sl
->receiver_node
->loaded
&& !sl
->wng
) {
933 if (sl
->receiver_node
->loaded
&& !sl
->fc
) {
937 if (sl
->wng
|| !sl
->fc
|| !*sl
->fc
->out_loaded
)
939 if (tv_diff(now
, &initial_delay_barrier
, &diff
) > 0) {
943 PARA_INFO_LOG("initial delay: %lu ms left\n", tv2ms(&diff
));
944 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0) {
951 /* restart the client task if necessary */
952 static void status_pre_select(struct sched
*s
, struct task
*t
)
954 struct status_task
*st
= container_of(t
, struct status_task
, task
);
956 if (audiod_status
== AUDIOD_OFF
) {
959 if (st
->ct
->task
.error
>= 0) {
960 st
->ct
->task
.error
= -E_AUDIOD_OFF
;
963 if (st
->ct
->task
.error
!= -E_TASK_UNREGISTERED
)
966 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
971 if (st
->ct
->task
.error
< 0) {
972 if (st
->ct
->task
.error
!= -E_TASK_UNREGISTERED
)
977 if (st
->ct
->status
!= CL_RECEIVING
)
979 bytes_left
= for_each_line(st
->ct
->buf
, st
->ct
->loaded
,
980 &check_stat_line
, NULL
);
981 if (st
->ct
->loaded
!= bytes_left
) {
982 st
->last_status_read
= *now
;
983 st
->ct
->loaded
= bytes_left
;
986 tv_diff(now
, &st
->last_status_read
, &diff
);
987 if (diff
.tv_sec
> 61)
988 st
->ct
->task
.error
= -E_STATUS_TIMEOUT
;
992 if (tv_diff(now
, &st
->restart_barrier
, NULL
) < 0)
994 if (st
->clock_diff_count
) { /* get status only one time */
995 char *argv
[] = {"audiod", "stat", "1", NULL
};
997 PARA_INFO_LOG("clock diff count: %d\n", st
->clock_diff_count
);
998 st
->clock_diff_count
--;
999 client_open(argc
, argv
, &st
->ct
);
1000 set_stat_task_restart_barrier(2);
1003 char *argv
[] = {"audiod", "stat", NULL
};
1005 client_open(argc
, argv
, &st
->ct
);
1006 set_stat_task_restart_barrier(5);
1008 st
->last_status_read
= *now
;
1010 start_stop_decoders(s
);
1013 static void init_status_task(struct status_task
*st
)
1015 memset(st
, 0, sizeof(struct status_task
));
1016 st
->task
.pre_select
= status_pre_select
;
1017 st
->sa_time_diff_sign
= 1;
1018 st
->clock_diff_count
= conf
.clock_diff_count_arg
;
1019 st
->current_audio_format_num
= -1;
1020 sprintf(st
->task
.status
, "status task");
1023 static void set_initial_status(void)
1025 audiod_status
= AUDIOD_ON
;
1026 if (!conf
.mode_given
)
1028 if (!strcmp(conf
.mode_arg
, "sb")) {
1029 audiod_status
= AUDIOD_STANDBY
;
1032 if (!strcmp(conf
.mode_arg
, "off")) {
1033 audiod_status
= AUDIOD_OFF
;
1036 if (strcmp(conf
.mode_arg
, "on"))
1037 PARA_WARNING_LOG("invalid mode\n");
1041 * the main function of para_audiod
1043 * \param argc usual argument count
1044 * \param argv usual argument vector
1046 * \return EXIT_SUCCESS or EXIT_FAILURE
1048 * \sa para_audiod(1)
1050 int main(int argc
, char *argv
[])
1055 struct command_task command_task_struct
, *cmd_task
= &command_task_struct
;
1058 audiod_cmdline_parser(argc
, argv
, &conf
);
1059 HANDLE_VERSION_FLAG("audiod", conf
);
1060 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1061 config_file
= configfile_exists();
1063 struct audiod_cmdline_parser_params params
= {
1066 .check_required
= 0,
1067 .check_ambiguity
= 0
1070 if (audiod_cmdline_parser_config_file(config_file
, &conf
, ¶ms
)) {
1071 PARA_EMERG_LOG("parse error in config file\n");
1076 if (conf
.logfile_given
)
1077 logfile
= open_log(conf
.logfile_arg
);
1078 log_welcome("para_audiod", conf
.loglevel_arg
);
1079 i
= init_stream_io();
1081 PARA_EMERG_LOG("init stream io error: %s\n", para_strerror(-i
));
1084 server_uptime(UPTIME_SET
);
1085 set_initial_status();
1089 setup_signal_handling();
1090 signal_setup_default(sig_task
);
1092 init_status_task(stat_task
);
1093 init_command_task(cmd_task
);
1095 if (conf
.daemon_given
)
1098 register_task(&sig_task
->task
);
1099 register_task(&cmd_task
->task
);
1100 register_task(&stat_task
->task
);
1101 s
.default_timeout
.tv_sec
= 0;
1102 s
.default_timeout
.tv_usec
= 99 * 1000;
1105 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));
1106 return EXIT_FAILURE
;