2 * Copyright (C) 2005-2006 Andre Noll <noll@mathematik.tu-darmstadt.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file audiod.c the paraslash's audio daemon */
21 #include <sys/time.h> /* gettimeofday */
24 #include "audiod.cmdline.h"
26 #include "close_on_fork.h"
30 #include "grab_client.cmdline.h"
31 #include "grab_client.h"
40 /** define the array of error lists needed by para_audiod */
42 /** define the array containing all supported audio formats */
43 DEFINE_AUDIO_FORMAT_ARRAY
;
46 * the possible modes of operation
48 * - off: disconnect from para_server
49 * - on: receive status information from para_server and play the audio stream
50 * - sb: only receive status information but not the audio stream
52 enum {AUDIOD_OFF
, AUDIOD_ON
, AUDIOD_STANDBY
};
54 /** defines how to handle one supported audio format */
55 struct audio_format_info
{
56 /** pointer to the receiver for this audio format */
57 struct receiver
*receiver
;
58 /** the receiver configuration */
60 /** the number of filters that should be activated for this audio format */
61 unsigned int num_filters
;
62 /** pointer to the array of filters to be activated */
63 struct filter
**filters
;
64 /** pointer to the array of filter configurations */
66 /** output of the last filter is written to stdin of this command */
68 /** do not start receiver/filters/writer before this time */
69 struct timeval restart_barrier
;
73 * describes one instance of a receiver-filter-writer chain
75 * \sa receier_node, receiver, filter, filter_node, filter_chain_info
78 /** number of the audio format in this slot */
80 /** the file descriptor of the writer */
82 /** the process id of the writer */
84 /** time of the last successful read from the receiver */
86 /** time the last write to the write fd happend */
88 /** writer start time */
89 struct timeval wstime
;
90 /** did we include \a write_fd in the fdset */
92 /** set to one if we have sent the TERM signal to \a wpid */
94 /** the receiver info associated with this slot */
95 struct receiver_node
*receiver_node
;
96 /** the active filter chain */
97 struct filter_chain_info
*fci
;
100 static struct slot_info slot
[MAX_STREAM_SLOTS
];
102 /** defines one command of para_audiod */
103 struct audiod_command
{
104 /** the name of the command */
106 /** pointer to the function that handles the command */
107 int (*handler
)(int, int, char**);
108 int (*line_handler
)(int, char*);
109 /** one-line description of the command */
110 const char *description
;
111 /** summary of the command line options */
112 const char *synopsis
;
113 /** the long help text */
117 extern const char *status_item_list
[NUM_STAT_ITEMS
];
119 static int com_grab(int, char *);
120 static int com_cycle(int, int, char **);
121 static int com_help(int, int, char **);
122 static int com_off(int, int, char **);
123 static int com_on(int, int, char **);
124 static int com_sb(int, int, char **);
125 static int com_stat(int, int, char **);
126 static int com_term(int, int, char **);
127 static int stat_pipe
= -1, signal_pipe
;
129 static struct gengetopt_args_info conf
;
130 static struct timeval server_stream_start
, sa_time_diff
;
131 static int playing
, current_decoder
= -1,
132 audiod_status
= AUDIOD_ON
, offset_seconds
, length_seconds
,
133 sa_time_diff_sign
= 1, audiod_socket
= -1;
134 static char *af_status
, /* the audio format announced in server status */
135 *socket_name
, *hostname
;
136 static char *stat_item_values
[NUM_STAT_ITEMS
];
137 static FILE *logfile
;
138 static const struct timeval restart_delay
= {0, 300 * 1000};
140 static struct audio_format_info afi
[NUM_AUDIO_FORMATS
];
142 static struct audiod_command cmds
[] = {
145 .handler
= com_cycle
,
146 .description
= "switch to next mode",
150 "on -> standby -> off -> on\n"
155 .line_handler
= com_grab
,
156 .description
= "grab the audio stream",
157 .synopsis
= "-- grab [grab_options]",
160 "grab ('splice') the audio stream at any position in the filter \n"
161 "chain and send that data back to the client. Try\n"
162 "\t para_audioc -- grab -h\n"
163 "for the list of available options.\n"
169 .description
= "display command list or help for given command",
170 .synopsis
= "help [command]",
173 "When I was younger, so much younger than today, I never needed\n"
174 "anybody's help in any way. But now these days are gone, I'm not so\n"
175 "self assured. Now I find I've changed my mind and opened up the doors.\n"
177 " -- Beatles: Help\n"
183 .description
= "deactivate para_audiod",
187 "Close connection to para_server and stop all decoders.\n"
193 .description
= "activate para_audiod",
197 "Establish connection to para_server, retrieve para_server's current\n"
198 "status. If playing, start corresponding decoder. Otherwise stop\n"
205 .description
= "enter standby mode",
209 "Stop all decoders but leave connection to para_server open.\n"
215 .description
= "print status information",
216 .synopsis
= "stat [item1 ...]",
219 "Dump given status items (all if none given) to stdout.\n"
225 .description
= "terminate audiod",
229 "Stop all decoders, shut down connection to para_server and exit.\n"
237 /** iterate over all slots */
238 #define FOR_EACH_SLOT(slot) for (slot = 0; slot < MAX_STREAM_SLOTS; slot++)
239 /** iterate over all supported audio formats */
240 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
241 /** iterate over the array of all audiod commands */
242 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
245 * get the audio format number
246 * \param name the name of the audio format
248 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
249 * \a name is not a supported audio format.
251 int get_audio_format_num(char *name
)
254 FOR_EACH_AUDIO_FORMAT(i
)
255 if (!strcmp(name
, audio_formats
[i
]))
257 return -E_UNSUPPORTED_AUDIO_FORMAT
;
261 * log function. first argument is loglevel.
263 void para_log(int ll
, const char* fmt
,...)
269 char str
[MAXLINE
] = "";
271 if (ll
< conf
.loglevel_arg
)
273 outfd
= logfile
? logfile
: stderr
;
276 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
277 fprintf(outfd
, "%s %s ", str
, hostname
);
278 if (conf
.loglevel_arg
<= INFO
)
279 fprintf(outfd
, "%i ", ll
);
281 vfprintf(outfd
, fmt
, argp
);
285 static int client_write(int fd
, const char *buf
)
287 size_t len
= strlen(buf
);
288 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
291 static char *get_time_string(struct timeval
*newest_stime
)
293 struct timeval now
, diff
, adj_stream_start
, tmp
;
294 int total
= 0, use_server_time
= 1;
299 return make_message("%s:\n", status_item_list
[SI_PLAY_TIME
]);
301 if (audiod_status
== AUDIOD_OFF
)
303 if (sa_time_diff_sign
> 0)
304 tv_diff(&server_stream_start
, &sa_time_diff
,
307 tv_add(&server_stream_start
, &sa_time_diff
,
309 tmp
= adj_stream_start
;
310 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
311 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
312 if (tv2ms(&diff
) < 5000) {
317 gettimeofday(&now
, NULL
);
318 tv_diff(&now
, &tmp
, &diff
);
319 total
= diff
.tv_sec
+ offset_seconds
;
320 if (total
> length_seconds
)
321 total
= length_seconds
;
326 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)\n",
327 status_item_list
[SI_PLAY_TIME
],
328 use_server_time
? "~" : "",
331 (length_seconds
- total
) / 60,
332 (length_seconds
- total
) % 60,
333 length_seconds
? (total
* 100 + length_seconds
/ 2) /
340 __malloc
static char *audiod_status_string(void)
342 const char *status
= (audiod_status
== AUDIOD_ON
)?
343 "on" : (audiod_status
== AUDIOD_OFF
)? "off": "sb";
344 return make_message("%s:%s\n", status_item_list
[SI_AUDIOD_STATUS
], status
);
347 static struct timeval
*wstime(void)
350 struct timeval
*max
= NULL
;
352 struct slot_info
*s
= &slot
[i
];
355 if (max
&& tv_diff(&s
->wstime
, max
, NULL
) <= 0)
361 __malloc
static char *decoder_flags(void)
364 char decoder_flags
[MAX_STREAM_SLOTS
+ 1];
367 struct slot_info
*s
= &slot
[i
];
369 if (s
->receiver_node
)
373 decoder_flags
[i
] = flag
;
375 decoder_flags
[MAX_STREAM_SLOTS
] = '\0';
376 return make_message("%s:%s\n", status_item_list
[SI_DECODER_FLAGS
],
380 static char *configfile_exists(void)
382 static char *config_file
;
385 char *home
= para_homedir();
386 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
389 return file_exists(config_file
)? config_file
: NULL
;
392 static void setup_signal_handling(void)
394 signal_pipe
= para_signal_init();
395 PARA_INFO_LOG("signal pipe: fd %d\n", signal_pipe
);
396 para_install_sighandler(SIGINT
);
397 para_install_sighandler(SIGTERM
);
398 para_install_sighandler(SIGCHLD
);
399 para_install_sighandler(SIGHUP
);
400 signal(SIGPIPE
, SIG_IGN
);
403 static void audiod_status_dump(void)
405 static char *p_ts
, *p_us
, *p_as
, *p_df
;
406 struct timeval
*t
= wstime();
407 char *us
, *tmp
= get_time_string(t
);
409 if (tmp
&& (!p_ts
|| strcmp(tmp
, p_ts
)))
410 stat_client_write(tmp
, SI_PLAY_TIME
);
415 tmp
= make_message("%s:%s\n", status_item_list
[SI_AUDIOD_UPTIME
], us
);
417 if (!p_us
|| strcmp(p_us
, tmp
))
418 stat_client_write(tmp
, SI_AUDIOD_UPTIME
);
422 tmp
= audiod_status_string();
423 if (!p_as
|| strcmp(p_as
, tmp
))
424 stat_client_write(tmp
, SI_AUDIOD_STATUS
);
428 tmp
= decoder_flags();
429 if (!p_df
|| strcmp(p_df
, tmp
))
430 stat_client_write(tmp
, SI_DECODER_FLAGS
);
435 static void clear_slot(int slot_num
)
437 struct slot_info
*s
= &slot
[slot_num
];
439 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
440 memset(s
, 0, sizeof(struct slot_info
));
444 static void kill_stream_writer(int slot_num
)
446 struct slot_info
*s
= &slot
[slot_num
];
448 if (s
->format
< 0 || s
->wkilled
|| s
->wpid
<= 0)
450 PARA_DEBUG_LOG("kill -TERM %d (%s stream writer in slot %d)\n",
451 s
->wpid
, audio_formats
[s
->format
], slot_num
);
452 kill(s
->wpid
, SIGTERM
);
456 static void set_restart_barrier(int format
, struct timeval
*now
)
463 gettimeofday(&tmp
, NULL
);
464 tv_add(&tmp
, &restart_delay
, &afi
[format
].restart_barrier
);
467 static void close_receiver(int slot_num
)
469 struct slot_info
*s
= &slot
[slot_num
];
470 struct audio_format_info
*a
;
472 if (s
->format
< 0 || !s
->receiver_node
)
475 PARA_NOTICE_LOG("closing %s recevier in slot %d\n",
476 audio_formats
[s
->format
] , slot_num
);
477 a
->receiver
->close(s
->receiver_node
);
478 free(s
->receiver_node
);
479 s
->receiver_node
= NULL
;
480 set_restart_barrier(s
->format
, NULL
);
483 static void kill_all_decoders(void)
488 if (slot
[i
].format
>= 0) {
489 PARA_INFO_LOG("stopping decoder in slot %d\n", i
);
490 kill_stream_writer(i
);
494 static void check_sigchld(void)
499 gettimeofday(&now
, NULL
);
502 pid
= para_reap_child();
506 struct slot_info
*s
= &slot
[i
];
510 if (pid
== s
->wpid
) {
512 lifetime
= now
.tv_sec
- s
->wstime
.tv_sec
;
513 PARA_INFO_LOG("%s stream writer in slot %d died "
515 audio_formats
[s
->format
], i
, lifetime
);
516 set_restart_barrier(s
->format
, &now
);
517 goto reap_next_child
;
520 PARA_CRIT_LOG("para_client died (pid %d)\n", pid
);
521 goto reap_next_child
;
524 static int get_empty_slot(void)
535 if (s
->write_fd
> 0 || s
->wpid
> 0)
537 if (s
->receiver_node
)
544 return -E_NO_MORE_SLOTS
;
547 static int decoder_running(int format
)
554 if (s
->format
== format
&& s
->receiver_node
)
556 if (s
->format
== format
&& s
->wpid
> 0)
562 static void close_stat_pipe(void)
568 PARA_NOTICE_LOG("%s", "closing status pipe\n");
570 del_close_on_fork_list(stat_pipe
);
573 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
574 free(stat_item_values
[i
]);
575 stat_item_values
[i
] = NULL
;
580 audiod_status_dump();
582 stat_item_values
[SI_STATUS_BAR
] = make_message("%s:no connection to para_server\n",
583 status_item_list
[SI_STATUS_BAR
]);
584 stat_client_write(stat_item_values
[SI_STATUS_BAR
], SI_STATUS_BAR
);
587 static void __noreturn
clean_exit(int status
, const char *msg
)
589 PARA_EMERG_LOG("%s\n", msg
);
598 __malloc
static char *glob_cmd(char *cmd
)
600 char *ret
, *replacement
;
601 struct timeval tmp
, delay
, rss
; /* real stream start */
603 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
604 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
605 // PARA_INFO_LOG("delay: %lu:%lu\n", delay.tv_sec, delay.tv_usec);
606 if (sa_time_diff_sign
< 0)
607 tv_add(&server_stream_start
, &sa_time_diff
, &rss
);
609 tv_diff(&server_stream_start
, &sa_time_diff
, &rss
);
610 tv_add(&rss
, &delay
, &tmp
);
611 replacement
= make_message("%lu:%lu",
612 (long unsigned)tmp
.tv_sec
,
613 (long unsigned)tmp
.tv_usec
);
614 ret
= s_a_r(cmd
, "STREAM_START", replacement
);
618 PARA_INFO_LOG("cmd: %s, repl: %s\n", cmd
, ret
);
623 /** get the number of filters for the given audio format */
624 int num_filters(int audio_format_num
)
626 return afi
[audio_format_num
].num_filters
;
629 static void open_filters(int slot_num
)
631 struct slot_info
*s
= &slot
[slot_num
];
632 struct audio_format_info
*a
= &afi
[s
->format
];
633 int nf
= a
->num_filters
;
636 s
->fci
= para_calloc(sizeof(struct filter_chain_info
));
637 INIT_LIST_HEAD(&s
->fci
->filters
);
640 s
->fci
->inbuf
= s
->receiver_node
->buf
;
641 s
->fci
->in_loaded
= &s
->receiver_node
->loaded
;
642 s
->fci
->outbuf
= s
->receiver_node
->buf
;
643 s
->fci
->out_loaded
= &s
->receiver_node
->loaded
;
644 s
->fci
->eof
= &s
->receiver_node
->eof
;
645 for (i
= 0; i
< nf
; i
++) {
646 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
647 fn
->conf
= a
->filter_conf
[i
];
649 fn
->filter
= a
->filters
[i
];
650 INIT_LIST_HEAD(&fn
->callbacks
);
651 list_add_tail(&fn
->node
, &s
->fci
->filters
);
652 fn
->filter
->open(fn
);
653 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
654 audio_formats
[s
->format
], i
+ 1, nf
,
655 fn
->filter
->name
, slot_num
);
656 s
->fci
->outbuf
= fn
->buf
;
657 s
->fci
->out_loaded
= &fn
->loaded
;
659 PARA_DEBUG_LOG("output buffer for filter chain %p: %p\n", s
->fci
,
663 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
665 struct filter_node
*fn
;
669 struct slot_info
*s
= &slot
[i
];
670 if (s
->format
< 0 || !s
->fci
)
672 if (slot_num
>= 0 && slot_num
!= i
)
674 if (format
>= 0 && s
->format
!= format
)
676 if (num_filters(i
) < filternum
)
680 list_for_each_entry(fn
, &s
->fci
->filters
, node
)
681 if (filternum
<= 0 || j
++ == filternum
)
688 static void start_stream_writer(int slot_num
)
690 int ret
, fds
[3] = {1, -1, -1};
691 struct slot_info
*s
= &slot
[slot_num
];
692 struct audio_format_info
*a
= &afi
[s
->format
];
696 glob
= glob_cmd(a
->write_cmd
);
698 glob
= para_strdup("para_write -w alsa");
699 PARA_INFO_LOG("starting stream writer: %s\n", glob
);
700 open_filters(slot_num
);
701 ret
= para_exec_cmdline_pid(&s
->wpid
, glob
, fds
);
704 PARA_ERROR_LOG("exec failed (%d)\n", ret
);
707 s
->write_fd
= fds
[0];
708 add_close_on_fork_list(s
->write_fd
);
709 /* we write to this fd in do_select, so we need non-blocking */
710 mark_fd_nonblock(s
->write_fd
);
711 gettimeofday(&s
->wstime
, NULL
);
712 current_decoder
= slot_num
;
713 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fci
->filters
);
716 static void open_receiver(int format
)
718 struct audio_format_info
*a
= &afi
[format
];
722 slot_num
= get_empty_slot();
724 clean_exit(EXIT_FAILURE
, PARA_STRERROR(-slot_num
));
727 gettimeofday(&s
->rtime
, NULL
);
729 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
730 s
->receiver_node
->conf
= a
->receiver_conf
;
731 ret
= a
->receiver
->open(s
->receiver_node
);
733 PARA_ERROR_LOG("failed to open receiver (%s)\n",
734 PARA_STRERROR(-ret
));
735 free(s
->receiver_node
);
736 s
->receiver_node
= NULL
;
739 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
740 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
743 static int is_frozen(int format
)
746 struct audio_format_info
*a
= &afi
[format
];
748 gettimeofday(&now
, NULL
);
749 return (tv_diff(&now
, &a
->restart_barrier
, NULL
) > 0)? 0 : 1;
752 static void start_current_receiver(void)
758 i
= get_audio_format_num(af_status
);
761 if ((decoder_running(i
) & 1) || is_frozen(i
))
766 static void compute_time_diff(const struct timeval
*status_time
)
768 struct timeval now
, tmp
, diff
;
771 const struct timeval max_deviation
= {0, 500 * 1000};
772 const int time_smooth
= 5;
774 gettimeofday(&now
, NULL
);
775 sign
= tv_diff(status_time
, &now
, &diff
);
776 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
777 // sign, sa_time_diff_sign);
779 sa_time_diff_sign
= sign
;
785 int s
= tv_diff(&diff
, &sa_time_diff
, &tmp
);
786 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
787 PARA_WARNING_LOG("time diff jump: %lims\n",
791 sa_time_diff_sign
= tv_convex_combination(
792 sa_time_diff_sign
* time_smooth
, &sa_time_diff
,
793 count
> 10? sign
: sign
* time_smooth
, &diff
,
796 PARA_INFO_LOG("time diff (cur/avg): %s%lums/%s%lums\n",
799 sa_time_diff_sign
? "+" : "-",
804 static void check_stat_line(char *line
)
808 long unsigned sec
, usec
;
811 // PARA_INFO_LOG("line: %s\n", line);
814 itemnum
= stat_line_valid(line
);
816 PARA_WARNING_LOG("invalid status line: %s\n", line
);
819 tmp
= make_message("%s\n", line
);
820 stat_client_write(tmp
, itemnum
);
822 free(stat_item_values
[itemnum
]);
823 stat_item_values
[itemnum
] = para_strdup(line
);
824 ilen
= strlen(status_item_list
[itemnum
]);
827 playing
= strstr(line
, "playing")? 1 : 0;
831 af_status
= para_strdup(line
+ ilen
+ 1);
834 offset_seconds
= atoi(line
+ ilen
+ 1);
837 length_seconds
= atoi(line
+ ilen
+ 1);
839 case SI_STREAM_START
:
840 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
841 server_stream_start
.tv_sec
= sec
;
842 server_stream_start
.tv_usec
= usec
;
845 case SI_CURRENT_TIME
:
846 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &sec
, &usec
) == 2) {
847 struct timeval tv
= {sec
, usec
};
848 compute_time_diff(&tv
);
854 static void handle_signal(int sig
)
858 return check_sigchld();
862 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
863 clean_exit(EXIT_FAILURE
, "caught deadly signal");
868 static void check_timeouts(void)
871 int slot_num
, timeout
= conf
.stream_timeout_arg
;
873 gettimeofday(&now
, NULL
);
874 FOR_EACH_SLOT(slot_num
) {
875 struct slot_info
*s
= &slot
[slot_num
];
878 /* check read time */
879 if (s
->receiver_node
&&
880 now
.tv_sec
> s
->rtime
.tv_sec
+ timeout
) {
881 PARA_INFO_LOG("%s input buffer (slot %d) not ready\n",
882 audio_formats
[s
->format
], slot_num
);
886 close_receiver(slot_num
);
888 /* check write time */
889 if (s
->wpid
> 0 && !s
->wkilled
&&
890 now
.tv_sec
> s
->wtime
.tv_sec
+ timeout
) {
891 PARA_INFO_LOG("%s output buffer (slot %d) not ready\n",
892 audio_formats
[s
->format
], slot_num
);
895 kill_stream_writer(slot_num
);
900 static size_t get_loaded_bytes(int slot_num
)
903 struct slot_info
*s
= &slot
[slot_num
];
904 struct receiver_node
*rn
= s
->receiver_node
;
909 if (afi
[s
->format
].num_filters
) {
911 loaded
= *s
->fci
->out_loaded
;
921 static void close_decoder_if_idle(int slot_num
)
923 struct slot_info
*s
= &slot
[slot_num
];
924 struct receiver_node
*rn
= s
->receiver_node
;
930 if (!rn
->eof
&& !s
->fc
->eof
&& s
->wpid
> 0)
932 if (!s
->fci
->eof
&& s
->wpid
> 0) { /* eof */
933 if (filter_io(s
->fci
) > 0)
935 if (get_loaded_bytes(slot_num
))
938 if (s
->write_fd
> 0) {
939 PARA_INFO_LOG("slot %d: closing write fd %d\n", slot_num
,
942 del_close_on_fork_list(s
->write_fd
);
946 return; /* wait until writer dies before closing filters */
947 PARA_INFO_LOG("closing all filters in slot %d (filter_chain %p)\n",
949 close_filters(s
->fci
);
951 close_receiver(slot_num
);
952 clear_slot(slot_num
);
955 static void set_stream_fds(fd_set
*wfds
, int *max_fileno
)
961 struct slot_info
*s
= &slot
[i
];
962 struct audio_format_info
*a
;
963 struct receiver_node
*rn
;
965 close_decoder_if_idle(i
);
970 rn
= s
->receiver_node
;
971 if (rn
&& rn
->loaded
&& !s
->wpid
) {
972 PARA_INFO_LOG("no writer in slot %d\n", i
);
973 start_stream_writer(i
);
975 if (s
->write_fd
<= 0)
977 if (!get_loaded_bytes(i
))
979 para_fd_set(s
->write_fd
, wfds
, max_fileno
);
984 static int write_audio_data(int slot_num
)
986 struct slot_info
*s
= &slot
[slot_num
];
987 struct audio_format_info
*a
= &afi
[s
->format
];
988 struct receiver_node
*rn
= s
->receiver_node
;
993 if (a
->num_filters
) {
994 buf
= &s
->fci
->outbuf
;
995 len
= s
->fci
->out_loaded
;
1000 PARA_DEBUG_LOG("writing %p (%zd bytes)\n", *buf
, *len
);
1001 rv
= write(s
->write_fd
, *buf
, *len
);
1002 PARA_DEBUG_LOG("wrote %d/%zd\n", rv
, *len
);
1004 PARA_WARNING_LOG("write error in slot %d (fd %d): %s\n",
1005 slot_num
, s
->write_fd
, strerror(errno
));
1007 s
->fci
->error
= E_WRITE_AUDIO_DATA
;
1008 } else if (rv
!= *len
) {
1009 PARA_DEBUG_LOG("partial %s write (%i/%zd) for slot %d\n",
1010 audio_formats
[s
->format
], rv
, *len
, slot_num
);
1012 memmove(*buf
, *buf
+ rv
, *len
);
1016 gettimeofday(&s
->wtime
, NULL
);
1020 static void slot_io(fd_set
*wfds
)
1025 struct slot_info
*s
= &slot
[i
];
1026 struct receiver_node
*rn
= s
->receiver_node
;
1028 if (rn
&& rn
->loaded
)
1029 gettimeofday(&s
->rtime
, NULL
);
1030 if (s
->format
>= 0 && s
->write_fd
> 0 && s
->fci
) {
1031 ret
= filter_io(s
->fci
);
1033 s
->fci
->error
= -ret
;
1034 // PARA_DEBUG_LOG("slot %d, filter io %d bytes, check write: %d, loaded: %d/%d, eof: %d\n",
1035 // i, ret, s->wcheck, rn->loaded, *s->fci->out_loaded, rn->eof);
1037 if (s
->write_fd
<= 0 || !s
->wcheck
|| !FD_ISSET(s
->write_fd
, wfds
))
1039 write_audio_data(i
);
1043 static int parse_stream_command(const char *txt
, char **cmd
)
1045 char *p
= strchr(txt
, ':');
1049 return -E_MISSING_COLON
;
1051 FOR_EACH_AUDIO_FORMAT(i
) {
1052 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
1057 return -E_UNSUPPORTED_AUDIO_FORMAT
;
1060 static int add_filter(int format
, char *cmdline
)
1062 struct audio_format_info
*a
= &afi
[format
];
1063 int filter_num
, nf
= a
->num_filters
;
1065 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
1068 a
->filters
[nf
] = &filters
[filter_num
];
1070 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
1071 a
->filters
[nf
]->name
);
1075 static int setup_default_filters(void)
1079 FOR_EACH_AUDIO_FORMAT(i
) {
1080 struct audio_format_info
*a
= &afi
[i
];
1085 /* add "dec" to audio format name */
1086 tmp
= make_message("%sdec", audio_formats
[i
]);
1087 for (j
= 0; filters
[j
].name
; j
++)
1088 if (!strcmp(tmp
, filters
[j
].name
))
1091 ret
= -E_UNSUPPORTED_FILTER
;
1092 if (!filters
[j
].name
)
1094 tmp
= para_strdup(filters
[j
].name
);
1095 ret
= add_filter(i
, tmp
);
1099 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
],
1101 ret
= add_filter(i
, "wav");
1104 PARA_INFO_LOG("%s -> default filter: wav\n", audio_formats
[i
]);
1110 static int init_stream_io(void)
1112 int i
, ret
, receiver_num
, nf
;
1115 for (i
= 0; i
< conf
.stream_write_cmd_given
; i
++) {
1116 ret
= parse_stream_command(conf
.stream_write_cmd_arg
[i
], &cmd
);
1119 afi
[ret
].write_cmd
= para_strdup(cmd
);
1120 PARA_INFO_LOG("%s write command: %s\n", audio_formats
[ret
], afi
[ret
].write_cmd
);
1122 for (i
= 0; receivers
[i
].name
; i
++) {
1123 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
1124 receivers
[i
].init(&receivers
[i
]);
1126 for (i
= 0; i
< conf
.receiver_given
; i
++) {
1127 char *arg
= conf
.receiver_arg
[i
];
1128 char *recv
= strchr(arg
, ':');
1129 ret
= -E_MISSING_COLON
;
1134 ret
= get_audio_format_num(arg
);
1137 afi
[ret
].receiver_conf
= check_receiver_arg(recv
, &receiver_num
);
1138 if (!afi
[ret
].receiver_conf
) {
1139 ret
= -E_RECV_SYNTAX
;
1142 afi
[ret
].receiver
= &receivers
[receiver_num
];
1144 /* use the first available receiver with no arguments
1145 * for those audio formats for which no receiver
1148 cmd
= para_strdup(receivers
[0].name
);
1149 FOR_EACH_AUDIO_FORMAT(i
) {
1150 struct audio_format_info
*a
= &afi
[i
];
1151 if (a
->receiver_conf
)
1153 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
1154 if (!a
->receiver_conf
)
1155 return -E_RECV_SYNTAX
;
1156 a
->receiver
= &receivers
[receiver_num
];
1160 filter_init(filters
);
1161 nf
= PARA_MAX(2, conf
.filter_given
) + 1;
1162 PARA_INFO_LOG("allocating space for %d filters\n", nf
);
1163 FOR_EACH_AUDIO_FORMAT(i
) {
1164 afi
[i
].filter_conf
= para_malloc(nf
* sizeof(char *));
1165 afi
[i
].filters
= para_malloc(nf
* sizeof(struct filter
*));
1167 if (!conf
.no_default_filters_given
)
1168 return setup_default_filters();
1169 for (i
= 0; i
< conf
.filter_given
; i
++) {
1170 char *arg
= conf
.filter_arg
[i
];
1171 char *filter_name
= strchr(arg
, ':');
1172 ret
= -E_MISSING_COLON
;
1175 *filter_name
= '\0';
1177 ret
= get_audio_format_num(arg
);
1180 ret
= add_filter(ret
, filter_name
);
1189 static int dump_commands(int fd
)
1191 char *buf
= para_strdup(""), *tmp
= NULL
;
1195 FOR_EACH_COMMAND(i
) {
1196 tmp
= make_message("%s%s\t%s\n", buf
, cmds
[i
].name
,
1197 cmds
[i
].description
);
1201 ret
= client_write(fd
, buf
);
1207 * command handlers don't close their fd on errors (ret < 0) so that
1208 * its caller can send an error message. Otherwise (ret >= 0) it's up
1209 * to each individual command to close the fd if necessary.
1212 static int com_help(int fd
, int argc
, char **argv
)
1216 const char *dflt
= "No such command. Available commands:\n";
1219 ret
= dump_commands(fd
);
1222 FOR_EACH_COMMAND(i
) {
1223 if (strcmp(cmds
[i
].name
, argv
[1]))
1226 "NAME\n\t%s -- %s\n"
1227 "SYNOPSIS\n\tpara_audioc %s\n"
1228 "DESCRIPTION\n%s\n",
1230 cmds
[i
].description
,
1234 ret
= client_write(fd
, buf
);
1238 ret
= client_write(fd
, dflt
);
1240 ret
= dump_commands(fd
);
1247 static int com_stat(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1251 long unsigned mask
= ~0LU;
1255 for (i
= 1; i
< argc
; i
++) {
1256 ret
= stat_item_valid(argv
[i
]);
1262 PARA_INFO_LOG("mask: 0x%lx\n", mask
);
1263 if (mask
& (1 << SI_PLAY_TIME
)) {
1264 struct timeval
*t
= wstime();
1265 char *ts
= get_time_string(t
);
1267 ret
= client_write(fd
, ts
);
1273 if (mask
& (1 << SI_AUDIOD_UPTIME
)) {
1274 char *tmp
, *us
= uptime_str();
1275 tmp
= make_message("%s:%s\n",
1276 status_item_list
[SI_AUDIOD_UPTIME
], us
);
1278 ret
= client_write(fd
, tmp
);
1283 if (mask
& (1 << SI_AUDIOD_STATUS
)) {
1284 char *s
= audiod_status_string();
1285 ret
= client_write(fd
, s
);
1290 if (mask
& (1 << SI_DECODER_FLAGS
)) {
1291 char *df
=decoder_flags();
1292 ret
= client_write(fd
, df
);
1298 for (i
= 0; i
< NUM_STAT_ITEMS
; i
++) {
1300 if (!((1 << i
) & mask
))
1302 v
= stat_item_values
[i
];
1303 tmp
= make_message("%s%s%s", buf
? buf
: "",
1304 v
? v
: "", v
? "\n" : "");
1308 ret
= client_write(fd
, buf
);
1311 ret
= stat_client_add(fd
, mask
);
1317 static char *list_filters(void)
1320 char *tmp
, *msg
= make_message("format\tnum\tcmd\n");
1322 FOR_EACH_AUDIO_FORMAT(i
) {
1323 for (j
= 0; j
< afi
[i
].num_filters
; j
++) {
1324 tmp
= make_message("%s\t%i\t%s\n",
1325 afi
[i
].name
, j
, afi
[i
].filter_cmds
[j
]);
1326 msg
= para_strcat(msg
, tmp
);
1329 tmp
= make_message("%s\t%i\t%s\n", afi
[i
].name
,
1330 j
, afi
[i
].write_cmd
);
1331 msg
= para_strcat(msg
, tmp
);
1339 static int com_grab(int fd
, char *cmdline
)
1341 struct grab_client
*gc
;
1342 struct filter_node
*fn
;
1346 gc
= grab_client_new(fd
, cmdline
, &err
);
1349 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
1351 activate_grab_client(gc
, fn
);
1354 if (err
!= -E_GC_HELP_GIVEN
&& err
!= -E_GC_VERSION_GIVEN
)
1356 if (err
== -E_GC_HELP_GIVEN
) {
1357 msg
= make_message("%s\n\n", grab_client_args_info_usage
);
1358 for (i
= 0; grab_client_args_info_help
[i
]; i
++) {
1359 char *tmp
= make_message("%s%s\n", msg
,
1360 grab_client_args_info_help
[i
]);
1365 msg
= make_message("%s %s\n",
1366 GRAB_CLIENT_CMDLINE_PARSER_PACKAGE
,
1367 GRAB_CLIENT_CMDLINE_PARSER_VERSION
);
1368 err
= client_write(fd
, msg
);
1376 static int __noreturn
com_term(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1379 clean_exit(EXIT_SUCCESS
, "terminating on user request");
1382 static int com_on(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1384 audiod_status
= AUDIOD_ON
;
1389 static int com_off(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1391 audiod_status
= AUDIOD_OFF
;
1396 static int com_sb(int fd
, __a_unused
int argc
, __a_unused
char **argv
)
1398 audiod_status
= AUDIOD_STANDBY
;
1403 static int com_cycle(int fd
, int argc
, char **argv
)
1405 switch (audiod_status
) {
1407 return com_sb(fd
, argc
, argv
);
1410 return com_on(fd
, argc
, argv
);
1412 case AUDIOD_STANDBY
:
1413 return com_off(fd
, argc
, argv
);
1420 static int check_perms(uid_t uid
)
1424 if (!conf
.user_allow_given
)
1426 for (i
= 0; i
< conf
.user_allow_given
; i
++)
1427 if (uid
== conf
.user_allow_arg
[i
])
1429 return -E_UCRED_PERM
;
1432 static int handle_connect(void)
1434 int i
, argc
, ret
, clifd
= -1;
1435 char *cmd
= NULL
, *p
, *buf
= para_calloc(MAXLINE
), **argv
= NULL
;
1436 struct sockaddr_un unix_addr
;
1438 ret
= para_accept(audiod_socket
, &unix_addr
, sizeof(struct sockaddr_un
));
1442 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1);
1445 PARA_INFO_LOG("connection from user %i, buf: %s\n", ret
, buf
);
1446 ret
= check_perms(ret
);
1449 ret
= -E_INVALID_AUDIOD_CMD
;
1450 cmd
= para_strdup(buf
);
1451 p
= strchr(cmd
, '\n');
1458 for (i
= 0; cmds
[i
].name
; i
++) {
1460 if (strcmp(cmds
[i
].name
, cmd
))
1462 if (cmds
[i
].handler
) {
1463 argc
= split_args(buf
, &argv
, "\n");
1464 PARA_INFO_LOG("argv[0]: %s, argc= %d\n", argv
[0], argc
);
1465 ret
= cmds
[i
].handler(clifd
, argc
, argv
);
1468 for (j
= 0; p
[j
]; j
++)
1471 PARA_INFO_LOG("cmd: %s, options: %s\n", cmd
, p
);
1472 ret
= cmds
[i
].line_handler(clifd
, p
);
1475 ret
= -E_INVALID_AUDIOD_CMD
; /* cmd not found */
1480 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
1481 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
1482 client_write(clifd
, tmp
);
1489 static void audiod_get_socket(void)
1491 struct sockaddr_un unix_addr
;
1493 if (conf
.socket_given
)
1494 socket_name
= para_strdup(conf
.socket_arg
);
1496 char *hn
= para_hostname();
1497 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
1501 PARA_NOTICE_LOG("connecting to local socket %s\n", socket_name
);
1502 if (conf
.force_given
)
1503 unlink(socket_name
);
1504 audiod_socket
= create_pf_socket(socket_name
, &unix_addr
,
1505 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
1506 if (audiod_socket
< 0) {
1507 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1508 exit(EXIT_FAILURE
); /* do not unlink socket */
1510 if (listen(audiod_socket
, 5) < 0) {
1511 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1512 exit(EXIT_FAILURE
); /* do not unlink socket */
1514 add_close_on_fork_list(audiod_socket
);
1517 static int open_stat_pipe(void)
1519 int ret
, fd
[3] = {-1, 1, 0};
1521 ret
= para_exec_cmdline_pid(&pid
, BINDIR
"/para_client stat", fd
);
1524 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret
);
1525 add_close_on_fork_list(ret
);
1527 clean_exit(EXIT_FAILURE
, "failed to open status pipe");
1531 static void audiod_pre_select(fd_set
*rfds
, fd_set
*wfds
, struct timeval
*tv
,
1537 struct slot_info
*s
= &slot
[i
];
1538 struct audio_format_info
*a
;
1539 struct receiver_node
*rn
= s
->receiver_node
;
1540 if (s
->format
< 0 || !rn
)
1542 a
= &afi
[s
->format
];
1543 ret
= a
->receiver
->pre_select(rn
, rfds
, wfds
, tv
);
1544 // PARA_NOTICE_LOG("%s preselect: %d\n", a->receiver->name, ret);
1545 *max_fileno
= PARA_MAX(*max_fileno
, ret
);
1548 static void audiod_post_select(int select_ret
, fd_set
*rfds
, fd_set
*wfds
)
1553 struct slot_info
*s
= &slot
[i
];
1554 struct audio_format_info
*a
;
1555 struct receiver_node
*rn
= s
->receiver_node
;
1556 if (s
->format
< 0 || !rn
|| rn
->eof
)
1558 a
= &afi
[s
->format
];
1559 ret
= a
->receiver
->post_select(rn
, select_ret
, rfds
, wfds
);
1562 PARA_ERROR_LOG("%s post select failed: %s (slot %d)\n",
1563 a
->receiver
->name
, PARA_STRERROR(-ret
), i
);
1565 PARA_INFO_LOG("eof in slot %d\n", i
);
1568 if (ret
< 0 && s
->fci
)
1569 s
->fci
->error
= ret
;
1573 /* TODO: move everything before the select call to pre_select() */
1574 static void __noreturn
audiod_mainloop(void)
1577 int ret
, max_fileno
, sbo
= 0;
1578 char status_buf
[STRINGSIZE
] = "";
1584 /* always check signal pipe and the local socket */
1585 para_fd_set(signal_pipe
, &rfds
, &max_fileno
);
1586 para_fd_set(audiod_socket
, &rfds
, &max_fileno
);
1588 if (audiod_status
!= AUDIOD_ON
)
1589 kill_all_decoders();
1591 start_current_receiver();
1593 set_stream_fds(&wfds
, &max_fileno
);
1595 if (stat_pipe
>= 0 && audiod_status
== AUDIOD_OFF
)
1597 if (stat_pipe
< 0 && audiod_status
!= AUDIOD_OFF
) {
1598 stat_pipe
= open_stat_pipe();
1600 status_buf
[0] = '\0';
1602 if (stat_pipe
>= 0 && audiod_status
!= AUDIOD_OFF
)
1603 para_fd_set(stat_pipe
, &rfds
, &max_fileno
);
1606 tv
.tv_usec
= 200 * 1000;
1607 audiod_pre_select(&rfds
, &wfds
, &tv
, &max_fileno
);
1608 ret
= para_select(max_fileno
+ 1, &rfds
, &wfds
, &tv
);
1611 if (audiod_status
!= AUDIOD_OFF
)
1612 audiod_status_dump();
1613 audiod_post_select(ret
, &rfds
, &wfds
);
1614 /* read status pipe */
1615 if (stat_pipe
>=0 && FD_ISSET(stat_pipe
, &rfds
)) {
1616 ret
= read(stat_pipe
, status_buf
+ sbo
, STRINGSIZE
- 1 - sbo
);
1619 /* avoid busy loop if server is down */
1620 while (sleep(1) > 0)
1623 status_buf
[ret
+ sbo
] = '\0';
1624 sbo
= for_each_line(status_buf
, ret
+ sbo
,
1629 if (FD_ISSET(audiod_socket
, &rfds
)) {
1630 ret
= handle_connect();
1632 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
1636 if (FD_ISSET(signal_pipe
, &rfds
)) {
1637 int sig_nr
= para_next_signal();
1639 handle_signal(sig_nr
);
1644 static void set_initial_status(void)
1646 audiod_status
= AUDIOD_ON
;
1647 if (!conf
.mode_given
)
1649 if (!strcmp(conf
.mode_arg
, "sb")) {
1650 audiod_status
= AUDIOD_STANDBY
;
1653 if (!strcmp(conf
.mode_arg
, "off")) {
1654 audiod_status
= AUDIOD_OFF
;
1657 if (strcmp(conf
.mode_arg
, "on"))
1658 PARA_WARNING_LOG("%s", "invalid mode\n");
1661 int __noreturn
main(int argc
, char *argv
[])
1667 hostname
= para_hostname();
1668 cmdline_parser(argc
, argv
, &conf
);
1669 para_drop_privileges(conf
.user_arg
, conf
.group_arg
);
1670 cf
= configfile_exists();
1672 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
1673 PARA_EMERG_LOG("%s", "parse error in config file\n");
1677 if (conf
.logfile_given
)
1678 logfile
= open_log(conf
.logfile_arg
);
1679 log_welcome("para_audiod", conf
.loglevel_arg
);
1680 i
= init_stream_io();
1682 PARA_EMERG_LOG("init stream io error: %s\n", PARA_STRERROR(-i
));
1685 server_uptime(UPTIME_SET
);
1686 set_initial_status();
1690 setup_signal_handling();
1691 if (conf
.daemon_given
)
1693 audiod_get_socket(); /* doesn't return on errors */