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"
29 #include "grab_client.cmdline.h"
30 #include "grab_client.h"
31 #include "ringbuffer.h"
39 /** define the array of error lists needed by para_audiod */
41 /** define the array containing all supported audio formats */
42 DEFINE_AUDIO_FORMAT_ARRAY
;
45 * the possible modes of operation
47 * - off: disconnect from para_server
48 * - on: receive status information from para_server and play the audio stream
49 * - sb: only receive status information but not the audio stream
51 enum {AUDIOD_OFF
, AUDIOD_ON
, AUDIOD_STANDBY
};
53 /** defines how to handle one supported audio format */
54 struct audio_format_info
{
55 /** pointer to the receiver for this audio format */
56 struct receiver
*receiver
;
57 /** the receiver configuration */
59 /** the number of filters that should be activated for this audio format */
60 unsigned int num_filters
;
61 /** pointer to the array of filters to be activated */
62 struct filter
**filters
;
63 /** pointer to the array of filter configurations */
65 /** output of the last filter is written to stdin of this command */
67 /** do not start receiver/filters/writer before this time */
68 struct timeval restart_barrier
;
72 * describes one instance of a receiver-filter-writer chain
74 * \sa receier_node, receiver, filter, filter_node, filter_chain_info
77 /** number of the audio format in this slot */
79 /** the file descriptor of the writer */
81 /** the process id of the writer */
83 /** time of the last successful read from the receiver */
85 /** time the last write to the write fd happend */
87 /** writer start time */
88 struct timeval wstime
;
89 /** did we include \a write_fd in the fdset */
91 /** set to one if we have sent the TERM signal to \a wpid */
93 /** the receiver info associated with this slot */
94 struct receiver_node
*receiver_node
;
95 /** the active filter chain */
96 struct filter_chain_info
*fci
;
99 static struct slot_info slot
[MAX_STREAM_SLOTS
];
101 /** defines one command of para_audiod */
102 struct audiod_command
{
103 /** the name of the command */
105 /** pointer to the function that handles the command */
106 int (*handler
)(int, int, char**);
107 /** one-line description of the command */
108 const char *description
;
109 /** summary of the command line options */
110 const char *synopsis
;
111 /** the long help text */
115 extern const char *status_item_list
[NUM_STAT_ITEMS
];
117 static int com_grab(int, int, char **);
118 static int com_cycle(int, int, char **);
119 static int com_help(int, int, char **);
120 static int com_off(int, int, char **);
121 static int com_on(int, int, char **);
122 static int com_sb(int, int, char **);
123 static int com_stat(int, int, char **);
124 static int com_term(int, int, char **);
125 static int stat_pipe
= -1, signal_pipe
;
127 static struct gengetopt_args_info conf
;
128 static struct timeval server_stream_start
, sa_time_diff
;
129 static int playing
, current_decoder
= -1,
130 audiod_status
= AUDIOD_ON
, offset_seconds
, length_seconds
,
131 sa_time_diff_sign
= 1, audiod_socket
= -1;
132 static char *af_status
, /* the audio format announced in server status */
133 *socket_name
, *hostname
;
134 /** how many status items to remember */
135 #define RINGBUFFER_SIZE 32
136 static void *stat_item_ringbuf
;
137 static FILE *logfile
;
138 static const struct timeval restart_delay
= {0, 300 * 1000};
140 static struct audio_format_info afi
[] = {
144 .write_cmd
= "para_play",
148 .write_cmd
= "para_play",
152 static struct audiod_command cmds
[] = {
155 .handler
= com_cycle
,
156 .description
= "switch to next mode",
160 "on -> standby -> off -> on\n"
166 .description
= "grab the audio stream",
167 .synopsis
= "-- grab [grab_options]",
169 "grab ('splice') the audio stream at any position in the filter \n"
170 "chain and send that data back to the client. \n"
171 "Available options:\n\n"
177 .description
= "display command list or help for given command",
178 .synopsis
= "help [command]",
181 "When I was younger, so much younger than today, I never needed\n"
182 "anybody's help in any way. But now these days are gone, I'm not so\n"
183 "self assured. Now I find I've changed my mind and opened up the doors.\n"
185 " -- Beatles: Help\n"
191 .description
= "deactivate para_audiod",
195 "Close connection to para_server and stop all decoders.\n"
201 .description
= "activate para_audiod",
205 "Establish connection to para_server, retrieve para_server's current\n"
206 "status. If playing, start corresponding decoder. Otherwise stop\n"
213 .description
= "enter standby mode",
217 "Stop all decoders but leave connection to para_server open.\n"
223 .description
= "print status information",
227 "Add para_audiod status information to para_server's status information\n"
228 "and dump everything to stdout.\n"
234 .description
= "terminate audiod",
238 "Stop all decoders, shut down connection to para_server and exit.\n"
246 /** iterate over all slots */
247 #define FOR_EACH_SLOT(slot) for (slot = 0; slot < MAX_STREAM_SLOTS; slot++)
248 /** iterate over all supported audio formats */
249 #define FOR_EACH_AUDIO_FORMAT(af) for (af = 0; af < NUM_AUDIO_FORMATS; af++)
250 /** iterate over the array of all audiod commands */
251 #define FOR_EACH_COMMAND(c) for (c = 0; cmds[c].name; c++)
254 * get the audio format number
255 * \param name the name of the audio format
257 * \return The audio format number on success, -E_UNSUPPORTED_AUDIO_FORMAT if
258 * \a name is not a supported audio format.
260 int get_audio_format_num(char *name
)
263 FOR_EACH_AUDIO_FORMAT(i
)
264 if (!strcmp(name
, audio_formats
[i
]))
266 return -E_UNSUPPORTED_AUDIO_FORMAT
;
270 * log function. first argument is loglevel.
272 void para_log(int ll
, char* fmt
,...)
278 char str
[MAXLINE
] = "";
280 if (ll
< conf
.loglevel_arg
)
282 if (!logfile
&& conf
.logfile_given
)
283 logfile
= open_log(conf
.logfile_arg
);
284 if (!logfile
&& conf
.daemon_given
)
295 strftime(str
, MAXLINE
, "%b %d %H:%M:%S", tm
);
296 fprintf(outfd
, "%s %s ", str
, hostname
);
297 if (conf
.loglevel_arg
<= INFO
)
298 fprintf(outfd
, "%i ", ll
);
300 vfprintf(outfd
, fmt
, argp
);
304 static int client_write(int fd
, const char *buf
)
306 size_t len
= strlen(buf
);
307 return write(fd
, buf
, len
) != len
? -E_CLIENT_WRITE
: 1;
310 static char *get_time_string(struct timeval
*newest_stime
)
312 struct timeval now
, diff
, adj_stream_start
, tmp
;
313 int total
= 0, use_server_time
= 1;
316 return make_message("%s:", length_seconds
?
317 "" : status_item_list
[SI_PLAY_TIME
]);
318 if (audiod_status
== AUDIOD_OFF
)
320 if (sa_time_diff_sign
> 0)
321 tv_diff(&server_stream_start
, &sa_time_diff
,
324 tv_add(&server_stream_start
, &sa_time_diff
,
326 tmp
= adj_stream_start
;
327 if (newest_stime
&& audiod_status
== AUDIOD_ON
) {
328 tv_diff(newest_stime
, &adj_stream_start
, &diff
);
329 if (tv2ms(&diff
) < 5000) {
334 gettimeofday(&now
, NULL
);
335 tv_diff(&now
, &tmp
, &diff
);
336 total
= diff
.tv_sec
+ offset_seconds
;
337 if (total
> length_seconds
)
338 total
= length_seconds
;
343 "%s:%s%d:%02d [%d:%02d] (%d%%/%d:%02d)",
344 status_item_list
[SI_PLAY_TIME
],
345 use_server_time
? "~" : "",
348 (length_seconds
- total
) / 60,
349 (length_seconds
- total
) % 60,
350 length_seconds
? (total
* 100 + length_seconds
/ 2) /
357 static char *audiod_status_string(void)
360 struct timeval
*newest_stime
= NULL
;
361 char *ret
, *time_string
, *uptime_string
, *decoder_flags
=
362 para_malloc((MAX_STREAM_SLOTS
+ 1) * sizeof(char));
365 struct slot_info
*s
= &slot
[i
];
367 if (s
->receiver_node
)
372 flag
+= s
->format
* 4;
373 decoder_flags
[i
] = flag
;
376 if (newest_stime
&& tv_diff(&s
->wstime
, newest_stime
, NULL
) <= 0)
378 newest_stime
= &s
->wstime
;
380 decoder_flags
[MAX_STREAM_SLOTS
] = '\0';
381 time_string
= get_time_string(newest_stime
);
382 uptime_string
= uptime_str();
383 ret
= make_message("%s:%s\n%s:%s\n%s:%s\n%s",
384 status_item_list
[SI_AUDIOD_UPTIME
], uptime_string
,
385 status_item_list
[SI_DECODER_FLAGS
], decoder_flags
,
386 status_item_list
[SI_AUDIOD_STATUS
], audiod_status
== AUDIOD_ON
?
387 "on" : (audiod_status
== AUDIOD_OFF
? "off": "sb"),
395 static char *configfile_exists(void)
397 static char *config_file
;
400 char *home
= para_homedir();
401 config_file
= make_message("%s/.paraslash/audiod.conf", home
);
404 return file_exists(config_file
)? config_file
: NULL
;
407 static void setup_signal_handling(void)
409 signal_pipe
= para_signal_init();
410 PARA_INFO_LOG("signal pipe: fd %d\n", signal_pipe
);
411 para_install_sighandler(SIGINT
);
412 para_install_sighandler(SIGTERM
);
413 para_install_sighandler(SIGCHLD
);
414 para_install_sighandler(SIGHUP
);
415 signal(SIGPIPE
, SIG_IGN
);
418 static void audiod_status_dump(void)
420 static char *prev_status
;
421 char *tmp
= audiod_status_string();
423 if (!prev_status
|| strcmp(tmp
, prev_status
))
424 stat_client_write(tmp
);
429 static void clear_slot(int slot_num
)
431 struct slot_info
*s
= &slot
[slot_num
];
433 PARA_INFO_LOG("clearing slot %d\n", slot_num
);
434 memset(s
, 0, sizeof(struct slot_info
));
438 static void kill_stream_writer(int slot_num
)
440 struct slot_info
*s
= &slot
[slot_num
];
442 if (s
->format
< 0 || s
->wkilled
|| s
->wpid
<= 0)
444 PARA_DEBUG_LOG("kill -TERM %d (%s stream writer in slot %d)\n",
445 s
->wpid
, audio_formats
[s
->format
], slot_num
);
446 kill(s
->wpid
, SIGTERM
);
451 static void close_receiver(int slot_num
)
453 struct slot_info
*s
= &slot
[slot_num
];
454 struct audio_format_info
*a
;
457 if (s
->format
< 0 || !s
->receiver_node
)
460 PARA_NOTICE_LOG("closing %s recevier in slot %d\n",
461 audio_formats
[s
->format
] , slot_num
);
462 a
->receiver
->close(s
->receiver_node
);
463 free(s
->receiver_node
);
464 s
->receiver_node
= NULL
;
465 gettimeofday(&now
, NULL
);
466 tv_add(&now
, &restart_delay
, &a
->restart_barrier
); /* FIXME: Use set_restart_barrier() */
469 static void kill_all_decoders(void)
474 if (slot
[i
].format
>= 0) {
475 PARA_INFO_LOG("stopping decoder in slot %d\n", i
);
476 kill_stream_writer(i
);
480 static void set_restart_barrier(int format
, struct timeval
*now
)
487 gettimeofday(&tmp
, NULL
);
488 tv_add(&tmp
, &restart_delay
, &afi
[format
].restart_barrier
);
491 static void check_sigchld(void)
496 gettimeofday(&now
, NULL
);
499 pid
= para_reap_child();
503 struct slot_info
*s
= &slot
[i
];
507 if (pid
== s
->wpid
) {
509 lifetime
= now
.tv_sec
- s
->wstime
.tv_sec
;
510 PARA_INFO_LOG("%s stream writer in slot %d died "
512 audio_formats
[s
->format
], i
, lifetime
);
513 set_restart_barrier(s
->format
, &now
);
514 goto reap_next_child
;
517 PARA_CRIT_LOG("para_client died (pid %d)\n", pid
);
518 goto reap_next_child
;
521 static int get_empty_slot(void)
532 if (s
->write_fd
> 0 || s
->wpid
> 0)
534 if (s
->receiver_node
)
541 return -E_NO_MORE_SLOTS
;
544 static int decoder_running(int format
)
551 if (s
->format
== format
&& s
->receiver_node
)
553 if (s
->format
== format
&& s
->wpid
> 0)
559 static void close_stat_pipe(void)
566 PARA_NOTICE_LOG("%s", "closing status pipe\n");
568 del_close_on_fork_list(stat_pipe
);
571 for (i
= 0; i
< RINGBUFFER_SIZE
; i
++)
572 ringbuffer_add(stat_item_ringbuf
, para_strdup(NULL
));
576 audiod_status_dump();
578 msg
= make_message("%s:no connection to para_server\n",
579 status_item_list
[SI_STATUS_BAR
]);
580 ringbuffer_add(stat_item_ringbuf
, msg
);
581 stat_client_write(msg
);
585 static void __noreturn
clean_exit(int status
, const char *msg
)
587 PARA_EMERG_LOG("%s\n", msg
);
596 static char *glob_cmd(char *cmd
)
598 char *ret
, *replacement
;
599 struct timeval tmp
, delay
, rss
; /* real stream start */
601 delay
.tv_sec
= conf
.stream_delay_arg
/ 1000;
602 delay
.tv_usec
= (conf
.stream_delay_arg
% 1000) * 1000;
603 // PARA_INFO_LOG("delay: %lu:%lu\n", delay.tv_sec, delay.tv_usec);
604 if (sa_time_diff_sign
< 0)
605 tv_add(&server_stream_start
, &sa_time_diff
, &rss
);
607 tv_diff(&server_stream_start
, &sa_time_diff
, &rss
);
608 tv_add(&rss
, &delay
, &tmp
);
609 replacement
= make_message("%lu:%lu", tmp
.tv_sec
, tmp
.tv_usec
);
610 ret
= s_a_r(cmd
, "STREAM_START", replacement
);
614 PARA_INFO_LOG("cmd: %s, repl: %s\n", cmd
, ret
);
617 gettimeofday(&now
, NULL
);
618 PARA_INFO_LOG("now: %lu:%lu\n", now
.tv_sec
, now
.tv_usec
);
624 /** get the number of filters for the given audio format */
625 int num_filters(int audio_format_num
)
627 return afi
[audio_format_num
].num_filters
;
630 static void open_filters(int slot_num
)
632 struct slot_info
*s
= &slot
[slot_num
];
633 struct audio_format_info
*a
= &afi
[s
->format
];
634 int nf
= a
->num_filters
;
637 s
->fci
= para_calloc(sizeof(struct filter_chain_info
));
638 INIT_LIST_HEAD(&s
->fci
->filters
);
641 s
->fci
->inbuf
= s
->receiver_node
->buf
;
642 s
->fci
->in_loaded
= &s
->receiver_node
->loaded
;
643 s
->fci
->outbuf
= s
->receiver_node
->buf
;
644 s
->fci
->out_loaded
= &s
->receiver_node
->loaded
;
645 s
->fci
->eof
= &s
->receiver_node
->eof
;
646 for (i
= 0; i
< nf
; i
++) {
647 struct filter_node
*fn
= para_calloc(sizeof(struct filter_node
));
648 fn
->conf
= a
->filter_conf
[i
];
650 fn
->filter
= a
->filters
[i
];
651 INIT_LIST_HEAD(&fn
->callbacks
);
652 list_add_tail(&fn
->node
, &s
->fci
->filters
);
653 fn
->filter
->open(fn
);
654 PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
655 audio_formats
[s
->format
], i
+ 1, nf
,
656 fn
->filter
->name
, slot_num
);
657 s
->fci
->outbuf
= fn
->buf
;
658 s
->fci
->out_loaded
= &fn
->loaded
;
660 PARA_DEBUG_LOG("output buffer for filter chain %p: %p\n", s
->fci
,
664 static struct filter_node
*find_filter_node(int slot_num
, int format
, int filternum
)
666 struct filter_node
*fn
;
670 struct slot_info
*s
= &slot
[i
];
671 if (s
->format
< 0 || !s
->fci
)
673 if (slot_num
>= 0 && slot_num
!= i
)
675 if (format
>= 0 && s
->format
!= format
)
677 if (num_filters(i
) < filternum
)
681 list_for_each_entry(fn
, &s
->fci
->filters
, node
)
682 if (filternum
<= 0 || j
++ == filternum
)
689 static void start_stream_writer(int slot_num
)
691 int ret
, fds
[3] = {1, -1, -1};
692 struct slot_info
*s
= &slot
[slot_num
];
693 struct audio_format_info
*a
= &afi
[s
->format
];
694 char *glob
= glob_cmd(a
->write_cmd
);
696 PARA_INFO_LOG("starting stream writer: %s\n", glob
? glob
: a
->write_cmd
);
697 open_filters(slot_num
);
699 ret
= para_exec_cmdline_pid(&s
->wpid
, glob
? glob
: a
->write_cmd
, fds
);
702 PARA_ERROR_LOG("exec failed (%d)\n", ret
);
705 s
->write_fd
= fds
[0];
706 add_close_on_fork_list(s
->write_fd
);
707 /* we write to this fd in do_select, so we need non-blocking */
708 fcntl(s
->write_fd
, F_SETFL
, O_NONBLOCK
);
709 gettimeofday(&s
->wstime
, NULL
);
710 current_decoder
= slot_num
;
711 activate_inactive_grab_clients(slot_num
, s
->format
, &s
->fci
->filters
);
714 static void open_receiver(int format
)
716 struct audio_format_info
*a
= &afi
[format
];
720 slot_num
= get_empty_slot();
722 clean_exit(EXIT_FAILURE
, PARA_STRERROR(-slot_num
));
725 gettimeofday(&s
->rtime
, NULL
);
727 s
->receiver_node
= para_calloc(sizeof(struct receiver_node
));
728 s
->receiver_node
->conf
= a
->receiver_conf
;
729 ret
= a
->receiver
->open(s
->receiver_node
);
731 PARA_ERROR_LOG("failed to open receiver (%s)\n",
732 PARA_STRERROR(-ret
));
733 free(s
->receiver_node
);
734 s
->receiver_node
= NULL
;
737 PARA_NOTICE_LOG("started %s: %s receiver in slot %d\n",
738 audio_formats
[s
->format
], a
->receiver
->name
, slot_num
);
741 static int is_frozen(int format
)
744 struct audio_format_info
*a
= &afi
[format
];
746 gettimeofday(&now
, NULL
);
747 return (tv_diff(&now
, &a
->restart_barrier
, NULL
) > 0)? 0 : 1;
750 static void start_current_receiver(void)
756 i
= get_audio_format_num(af_status
);
759 if ((decoder_running(i
) & 1) || is_frozen(i
))
764 static void compute_time_diff(const struct timeval
*status_time
)
766 struct timeval now
, tmp
, diff
;
769 const struct timeval max_deviation
= {0, 500 * 1000};
770 const int time_smooth
= 5;
772 gettimeofday(&now
, NULL
);
773 sign
= tv_diff(status_time
, &now
, &diff
);
774 // PARA_NOTICE_LOG("%s: sign = %i, sa_time_diff_sign = %i\n", __func__,
775 // sign, sa_time_diff_sign);
777 sa_time_diff_sign
= sign
;
783 int s
= tv_diff(&diff
, &sa_time_diff
, &tmp
);
784 if (tv_diff(&max_deviation
, &tmp
, NULL
) < 0)
785 PARA_WARNING_LOG("time diff jump: %lims\n",
789 sa_time_diff_sign
= tv_convex_combination(
790 sa_time_diff_sign
* time_smooth
, &sa_time_diff
,
791 count
> 10? sign
: sign
* time_smooth
, &diff
,
794 PARA_INFO_LOG("time diff (cur/avg): "
796 sign
* diff
.tv_sec
, (diff
.tv_usec
+ 500) / 1000,
797 sa_time_diff_sign
* sa_time_diff
.tv_sec
,
798 (sa_time_diff
.tv_usec
+ 500)/ 1000);
801 static void check_stat_line(char *line
)
809 ringbuffer_add(stat_item_ringbuf
, line
);
810 stat_client_write(line
);
811 itemnum
= stat_line_valid(line
);
814 ilen
= strlen(status_item_list
[itemnum
]);
817 playing
= strstr(line
, "playing")? 1 : 0;
821 af_status
= para_strdup(line
+ ilen
+ 1);
824 offset_seconds
= atoi(line
+ ilen
+ 1);
827 length_seconds
= atoi(line
+ ilen
+ 1);
829 case SI_STREAM_START
:
830 if (sscanf(line
+ ilen
+ 1, "%lu.%lu",
831 &tv
.tv_sec
, &tv
.tv_usec
) == 2)
832 server_stream_start
= tv
;
834 case SI_CURRENT_TIME
:
835 if (sscanf(line
+ ilen
+ 1, "%lu.%lu", &tv
.tv_sec
,
837 compute_time_diff(&tv
);
842 static void handle_signal(int sig
)
846 return check_sigchld();
850 PARA_EMERG_LOG("terminating on signal %d\n", sig
);
851 clean_exit(EXIT_FAILURE
, "caught deadly signal");
856 static void check_timeouts(void)
859 int slot_num
, timeout
= conf
.stream_timeout_arg
;
861 gettimeofday(&now
, NULL
);
862 FOR_EACH_SLOT(slot_num
) {
863 struct slot_info
*s
= &slot
[slot_num
];
866 /* check read time */
867 if (s
->receiver_node
&&
868 now
.tv_sec
> s
->rtime
.tv_sec
+ timeout
) {
869 PARA_INFO_LOG("%s input buffer (slot %d) not ready\n",
870 audio_formats
[s
->format
], slot_num
);
874 close_receiver(slot_num
);
876 /* check write time */
877 if (s
->wpid
> 0 && !s
->wkilled
&&
878 now
.tv_sec
> s
->wtime
.tv_sec
+ timeout
) {
879 PARA_INFO_LOG("%s output buffer (slot %d) not ready\n",
880 audio_formats
[s
->format
], slot_num
);
887 static size_t get_loaded_bytes(int slot_num
)
890 struct slot_info
*s
= &slot
[slot_num
];
891 struct receiver_node
*rn
= s
->receiver_node
;
896 if (afi
[s
->format
].num_filters
) {
898 loaded
= *s
->fci
->out_loaded
;
908 static void close_decoder_if_idle(int slot_num
)
910 struct slot_info
*s
= &slot
[slot_num
];
911 struct receiver_node
*rn
= s
->receiver_node
;
917 if (!rn
->eof
&& !s
->fci
->error
&& s
->wpid
> 0)
919 if (!s
->fci
->error
&& s
->wpid
> 0) { /* eof */
920 if (filter_io(s
->fci
) > 0)
922 if (get_loaded_bytes(slot_num
))
925 if (s
->write_fd
> 0) {
926 PARA_INFO_LOG("slot %d: closing write fd %d\n", slot_num
,
929 del_close_on_fork_list(s
->write_fd
);
933 return; /* wait until writer dies before closing filters */
934 PARA_INFO_LOG("closing all filters in slot %d (filter_chain %p)\n",
936 close_filters(s
->fci
);
938 close_receiver(slot_num
);
939 clear_slot(slot_num
);
942 static int set_stream_fds(fd_set
*wfds
)
944 int i
, max_fileno
= -1;
948 struct slot_info
*s
= &slot
[i
];
949 struct audio_format_info
*a
;
950 struct receiver_node
*rn
;
952 close_decoder_if_idle(i
);
957 rn
= s
->receiver_node
;
958 if (rn
&& rn
->loaded
&& !s
->wpid
) {
959 PARA_INFO_LOG("no writer in slot %d\n", i
);
960 start_stream_writer(i
);
962 if (s
->write_fd
<= 0)
964 if (!get_loaded_bytes(i
))
966 FD_SET(s
->write_fd
, wfds
);
968 max_fileno
= MAX(s
->write_fd
, max_fileno
);
970 // PARA_INFO_LOG("return %d\n", max_fileno);
974 static int write_audio_data(int slot_num
)
976 struct slot_info
*s
= &slot
[slot_num
];
977 struct audio_format_info
*a
= &afi
[s
->format
];
978 struct receiver_node
*rn
= s
->receiver_node
;
983 if (a
->num_filters
) {
984 buf
= &s
->fci
->outbuf
;
985 len
= s
->fci
->out_loaded
;
990 PARA_DEBUG_LOG("writing %p (%d bytes)\n", *buf
, *len
);
991 rv
= write(s
->write_fd
, *buf
, *len
);
992 PARA_DEBUG_LOG("wrote %d/%d\n", rv
, *len
);
994 PARA_WARNING_LOG("write error in slot %d (fd %d): %s\n",
995 slot_num
, s
->write_fd
, strerror(errno
));
997 s
->fci
->error
= E_WRITE_AUDIO_DATA
;
998 } else if (rv
!= *len
) {
999 PARA_DEBUG_LOG("partial %s write (%i/%i) for slot %d\n",
1000 audio_formats
[s
->format
], rv
, *len
, slot_num
);
1002 memmove(*buf
, *buf
+ rv
, *len
);
1006 gettimeofday(&s
->wtime
, NULL
);
1010 static void slot_io(fd_set
*wfds
)
1015 struct slot_info
*s
= &slot
[i
];
1016 struct receiver_node
*rn
= s
->receiver_node
;
1018 if (rn
&& rn
->loaded
)
1019 gettimeofday(&s
->rtime
, NULL
);
1020 if (s
->format
>= 0 && s
->write_fd
> 0 && s
->fci
) {
1021 ret
= filter_io(s
->fci
);
1023 s
->fci
->error
= -ret
;
1024 // PARA_DEBUG_LOG("slot %d, filter io %d bytes, check write: %d, loaded: %d/%d, eof: %d\n",
1025 // i, ret, s->wcheck, rn->loaded, *s->fci->out_loaded, rn->eof);
1027 if (s
->write_fd
<= 0 || !s
->wcheck
|| !FD_ISSET(s
->write_fd
, wfds
))
1029 write_audio_data(i
);
1033 static int parse_stream_command(const char *txt
, char **cmd
)
1035 char *p
= strchr(txt
, ':');
1039 return -E_MISSING_COLON
;
1041 FOR_EACH_AUDIO_FORMAT(i
) {
1042 if (strncmp(txt
, audio_formats
[i
], strlen(audio_formats
[i
])))
1047 return -E_UNSUPPORTED_AUDIO_FORMAT
;
1050 static int add_filter(int format
, char *cmdline
)
1052 struct audio_format_info
*a
= &afi
[format
];
1053 int filter_num
, nf
= a
->num_filters
;
1055 filter_num
= check_filter_arg(cmdline
, &a
->filter_conf
[nf
]);
1058 a
->filters
[nf
] = &filters
[filter_num
];
1060 PARA_INFO_LOG("%s filter %d: %s\n", audio_formats
[format
], nf
+ 1,
1061 a
->filters
[nf
]->name
);
1065 static int setup_default_filters(void)
1069 FOR_EACH_AUDIO_FORMAT(i
) {
1070 struct audio_format_info
*a
= &afi
[i
];
1075 /* add "dec" to audio format name */
1076 tmp
= make_message("%sdec", audio_formats
[i
]);
1077 for (j
= 0; filters
[j
].name
; j
++)
1078 if (!strcmp(tmp
, filters
[j
].name
))
1081 ret
= -E_UNSUPPORTED_FILTER
;
1082 if (!filters
[j
].name
)
1084 tmp
= para_strdup(filters
[j
].name
);
1085 ret
= add_filter(i
, tmp
);
1089 PARA_INFO_LOG("%s -> default filter: %s\n", audio_formats
[i
], filters
[j
].name
);
1090 ret
= add_filter(i
, "wav");
1093 PARA_INFO_LOG("%s -> default filter: wav\n", audio_formats
[i
]);
1099 static int init_stream_io(void)
1101 int i
, ret
, receiver_num
;
1104 for (i
= 0; i
< conf
.stream_write_cmd_given
; i
++) {
1105 ret
= parse_stream_command(conf
.stream_write_cmd_arg
[i
], &cmd
);
1108 afi
[ret
].write_cmd
= para_strdup(cmd
);
1109 PARA_INFO_LOG("%s write command: %s\n", audio_formats
[ret
], afi
[ret
].write_cmd
);
1111 for (i
= 0; receivers
[i
].name
; i
++) {
1112 PARA_INFO_LOG("initializing %s receiver\n", receivers
[i
].name
);
1113 receivers
[i
].init(&receivers
[i
]);
1115 for (i
= 0; i
< conf
.receiver_given
; i
++) {
1116 char *arg
= conf
.receiver_arg
[i
];
1117 char *recv
= strchr(arg
, ':');
1118 ret
= -E_MISSING_COLON
;
1123 ret
= get_audio_format_num(arg
);
1126 afi
[ret
].receiver_conf
= check_receiver_arg(recv
, &receiver_num
);
1127 if (!afi
[ret
].receiver_conf
) {
1128 ret
= -E_RECV_SYNTAX
;
1131 afi
[ret
].receiver
= &receivers
[receiver_num
];
1133 /* use the first available receiver with no arguments
1134 * for those audio formats for which no receiver
1137 cmd
= para_strdup(receivers
[0].name
);
1138 FOR_EACH_AUDIO_FORMAT(i
) {
1139 struct audio_format_info
*a
= &afi
[i
];
1140 if (a
->receiver_conf
)
1142 a
->receiver_conf
= check_receiver_arg(cmd
, &receiver_num
);
1143 if (!a
->receiver_conf
)
1144 return -E_RECV_SYNTAX
;
1145 a
->receiver
= &receivers
[receiver_num
];
1149 filter_init(filters
);
1150 FOR_EACH_AUDIO_FORMAT(i
) {
1151 afi
[i
].filter_conf
= para_malloc((conf
.filter_given
+ 1) * sizeof(char *));
1152 afi
[i
].filters
= para_malloc((conf
.filter_given
+ 1) * sizeof(struct filter
*));
1154 if (!conf
.no_default_filters_given
)
1155 return setup_default_filters();
1156 for (i
= 0; i
< conf
.filter_given
; i
++) {
1157 char *arg
= conf
.filter_arg
[i
];
1158 char *filter_name
= strchr(arg
, ':');
1159 ret
= -E_MISSING_COLON
;
1162 *filter_name
= '\0';
1164 ret
= get_audio_format_num(arg
);
1167 ret
= add_filter(ret
, filter_name
);
1176 static int dump_commands(int fd
)
1178 char *buf
= para_strdup(""), *tmp
= NULL
;
1182 FOR_EACH_COMMAND(i
) {
1183 tmp
= make_message("%s%s\t%s\n", buf
, cmds
[i
].name
,
1184 cmds
[i
].description
);
1188 ret
= client_write(fd
, buf
);
1194 * command handlers don't close their fd on errors (ret < 0) so that
1195 * its caller can send an error message. Otherwise (ret >= 0) it's up
1196 * to each individual command to close the fd if necessary.
1199 static int com_help(int fd
, int argc
, char **argv
)
1203 const char *dflt
= "No such command. Available commands:\n";
1206 ret
= dump_commands(fd
);
1209 FOR_EACH_COMMAND(i
) {
1210 if (strcmp(cmds
[i
].name
, argv
[1]))
1213 "NAME\n\t%s -- %s\n"
1214 "SYNOPSIS\n\tpara_audioc %s\n"
1215 "DESCRIPTION\n%s\n",
1217 cmds
[i
].description
,
1221 ret
= client_write(fd
, buf
);
1225 ret
= client_write(fd
, dflt
);
1227 ret
= dump_commands(fd
);
1234 static int com_stat(int fd
, __unused
int argc
, __unused
char **argv
)
1237 char *buf
= audiod_status_string();
1239 buf
= para_strcat(buf
, "\n");
1240 for (i
= RINGBUFFER_SIZE
- 1; i
>= 0; i
--) {
1241 char *tmp
, *line
= ringbuffer_get(stat_item_ringbuf
, i
);
1244 tmp
= make_message("%s\n", line
);
1245 buf
= para_strcat(buf
, tmp
);
1248 ret
= client_write(fd
, buf
);
1250 ret
= stat_client_add(fd
);
1256 static char *list_filters(void)
1259 char *tmp
, *msg
= make_message("format\tnum\tcmd\n");
1261 FOR_EACH_AUDIO_FORMAT(i
) {
1262 for (j
= 0; j
< afi
[i
].num_filters
; j
++) {
1263 tmp
= make_message("%s\t%i\t%s\n",
1264 afi
[i
].name
, j
, afi
[i
].filter_cmds
[j
]);
1265 msg
= para_strcat(msg
, tmp
);
1268 tmp
= make_message("%s\t%i\t%s\n", afi
[i
].name
,
1269 j
, afi
[i
].write_cmd
);
1270 msg
= para_strcat(msg
, tmp
);
1277 static int com_grab(int fd
, int argc
, char **argv
)
1279 struct grab_client
*gc
;
1280 struct filter_node
*fn
;
1283 PARA_INFO_LOG("argc: %d, argv[0]: %s, optind: %d\n", argc
, argv
[0], optind
);
1284 gc
= grab_client_new(fd
, argc
, argv
, &err
);
1285 PARA_INFO_LOG("argc: %d, argv[0]: %s, optind: %d\n", argc
, argv
[0], optind
);
1288 fn
= find_filter_node(gc
->conf
->slot_arg
, gc
->audio_format_num
, gc
->conf
->filter_num_arg
);
1290 activate_grab_client(gc
, fn
);
1293 if (err
!= -E_GC_HELP_GIVEN
)
1295 err
= client_write(fd
, "Usage: para_audioc [audioc_options] -- "
1296 "grab [grab_options]\nAvailable options:\n");
1299 err
= client_write(fd
, GRAB_HELP_TXT
);
1306 static int __noreturn
com_term(int fd
, __unused
int argc
, __unused
char **argv
)
1309 clean_exit(EXIT_SUCCESS
, "terminating on user request");
1312 static int com_on(int fd
, __unused
int argc
, __unused
char **argv
)
1314 audiod_status
= AUDIOD_ON
;
1319 static int com_off(int fd
, __unused
int argc
, __unused
char **argv
)
1321 audiod_status
= AUDIOD_OFF
;
1326 static int com_sb(int fd
, __unused
int argc
, __unused
char **argv
)
1328 audiod_status
= AUDIOD_STANDBY
;
1333 static int com_cycle(int fd
, int argc
, char **argv
)
1335 switch (audiod_status
) {
1337 return com_sb(fd
, argc
, argv
);
1340 return com_on(fd
, argc
, argv
);
1342 case AUDIOD_STANDBY
:
1343 return com_off(fd
, argc
, argv
);
1350 static int check_perms(struct ucred
*c
)
1354 if (!conf
.user_allow_given
)
1356 for (i
= 0; i
< conf
.user_allow_given
; i
++)
1357 if (c
->uid
== conf
.user_allow_arg
[i
])
1359 return -E_UCRED_PERM
;
1362 static int handle_connect(void)
1364 int i
, argc
, ret
, clifd
= -1;
1366 char *buf
= para_malloc(MAXLINE
), **argv
= NULL
;
1367 struct sockaddr_un unix_addr
;
1369 ret
= para_accept(audiod_socket
, &unix_addr
, sizeof(struct sockaddr_un
));
1373 ret
= recv_cred_buffer(clifd
, buf
, MAXLINE
- 1, &c
);
1376 PARA_INFO_LOG("pid: %i, uid: %i, gid: %i, ret: %i, buf: %s\n", c
.pid
, c
.uid
, c
.gid
, ret
, buf
);
1378 ret
= check_perms(&c
);
1381 argc
= split_args(buf
, &argv
, '\n');
1382 PARA_INFO_LOG("argv[0]: %s\n", argv
[0]);
1383 for (i
= 0; cmds
[i
].name
; i
++) {
1384 if (strcmp(cmds
[i
].name
, argv
[0]))
1386 ret
= cmds
[i
].handler(clifd
, argc
+ 1, argv
);
1389 ret
= -E_INVALID_AUDIOD_CMD
; /* cmd not found */
1393 if (clifd
> 0 && ret
< 0 && ret
!= -E_CLIENT_WRITE
) {
1394 char *tmp
= make_message("%s\n", PARA_STRERROR(-ret
));
1395 client_write(clifd
, tmp
);
1402 static void audiod_get_socket(void)
1404 struct sockaddr_un unix_addr
;
1406 if (conf
.socket_given
)
1407 socket_name
= para_strdup(conf
.socket_arg
);
1409 char *hn
= para_hostname();
1410 socket_name
= make_message("/var/paraslash/audiod_socket.%s",
1414 PARA_NOTICE_LOG("connecting to local socket %s\n", socket_name
);
1415 if (conf
.force_given
)
1416 unlink(socket_name
);
1417 audiod_socket
= create_pf_socket(socket_name
, &unix_addr
,
1418 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IWOTH
);
1419 if (audiod_socket
< 0) {
1420 PARA_EMERG_LOG("%s", "can not connect to socket\n");
1421 exit(EXIT_FAILURE
); /* do not unlink socket */
1423 if (listen(audiod_socket
, 5) < 0) {
1424 PARA_EMERG_LOG("%s", "can not listen on socket\n");
1425 exit(EXIT_FAILURE
); /* do not unlink socket */
1427 add_close_on_fork_list(audiod_socket
);
1430 static int open_stat_pipe(void)
1432 int ret
, fd
[3] = {-1, 1, 0};
1433 char *argv
[] = {BINDIR
"/para_client", "stat", NULL
};
1435 ret
= para_exec(&pid
, BINDIR
"/para_client", argv
, fd
);
1438 PARA_NOTICE_LOG("stat pipe opened, fd %d\n", ret
);
1439 add_close_on_fork_list(ret
);
1441 clean_exit(EXIT_FAILURE
, "failed to open status pipe");
1445 static int pre_select(fd_set
*rfds
, fd_set
*wfds
, struct timeval
*tv
)
1447 int i
, ret
, max
= -1;
1450 struct slot_info
*s
= &slot
[i
];
1451 struct audio_format_info
*a
;
1452 struct receiver_node
*rn
= s
->receiver_node
;
1453 if (s
->format
< 0 || !rn
)
1455 a
= &afi
[s
->format
];
1456 ret
= a
->receiver
->pre_select(rn
, rfds
, wfds
, tv
);
1457 // PARA_NOTICE_LOG("%s preselect: %d\n", a->receiver->name, ret);
1458 max
= MAX(max
, ret
);
1463 static void audiod_post_select(int select_ret
, fd_set
*rfds
, fd_set
*wfds
)
1468 struct slot_info
*s
= &slot
[i
];
1469 struct audio_format_info
*a
;
1470 struct receiver_node
*rn
= s
->receiver_node
;
1471 if (s
->format
< 0 || !rn
|| rn
->eof
)
1473 a
= &afi
[s
->format
];
1474 ret
= a
->receiver
->post_select(rn
, select_ret
, rfds
, wfds
);
1477 PARA_ERROR_LOG("%s post select failed: %s (slot %d)\n",
1478 a
->receiver
->name
, PARA_STRERROR(-ret
), i
);
1480 PARA_INFO_LOG("eof in slot %d\n", i
);
1483 if (ret
< 0 && s
->fci
)
1484 s
->fci
->error
= ret
;
1488 /* TODO: move everything before the select call to pre_select() */
1489 static void __noreturn
audiod_mainloop(void)
1492 int ret
, max_fileno
, sbo
= 0;
1493 char status_buf
[STRINGSIZE
] = "";
1499 if (audiod_status
!= AUDIOD_ON
)
1500 kill_all_decoders();
1502 start_current_receiver();
1503 max_fileno
= set_stream_fds(&wfds
);
1504 /* stat pipe (read) */
1505 if (stat_pipe
>= 0 && audiod_status
== AUDIOD_OFF
)
1507 if (stat_pipe
< 0 && audiod_status
!= AUDIOD_OFF
) {
1508 stat_pipe
= open_stat_pipe();
1510 status_buf
[0] = '\0';
1512 if (stat_pipe
>= 0 && audiod_status
!= AUDIOD_OFF
) {
1513 FD_SET(stat_pipe
, &rfds
);
1514 max_fileno
= MAX(max_fileno
, stat_pipe
);
1516 /* always check signal pipe */
1517 FD_SET(signal_pipe
, &rfds
);
1518 max_fileno
= MAX(max_fileno
, signal_pipe
);
1520 if (audiod_socket
< 0)
1521 audiod_get_socket(); /* doesn't return on errors */
1522 FD_SET(audiod_socket
, &rfds
);
1523 max_fileno
= MAX(max_fileno
, audiod_socket
);
1525 tv
.tv_usec
= 200 * 1000;
1526 ret
= pre_select(&rfds
, &wfds
, &tv
);
1527 max_fileno
= MAX(max_fileno
, ret
);
1528 ret
= select(max_fileno
+ 1, &rfds
, &wfds
, NULL
, &tv
);
1529 if (ret
< 0 && errno
!= EINTR
)
1530 PARA_ERROR_LOG("select returned %d (%s)\n", ret
,
1532 if (audiod_status
!= AUDIOD_OFF
)
1533 audiod_status_dump();
1536 audiod_post_select(ret
, &rfds
, &wfds
);
1537 /* read status pipe */
1538 if (stat_pipe
>=0 && FD_ISSET(stat_pipe
, &rfds
)) {
1539 ret
= read(stat_pipe
, status_buf
+ sbo
, STRINGSIZE
- 1 - sbo
);
1542 /* avoid busy loop if server is down */
1543 while (sleep(1) > 0)
1546 status_buf
[ret
+ sbo
] = '\0';
1547 sbo
= for_each_line(status_buf
, ret
+ sbo
,
1552 if (FD_ISSET(audiod_socket
, &rfds
)) {
1553 ret
= handle_connect();
1555 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
1559 if (FD_ISSET(signal_pipe
, &rfds
)) {
1560 int sig_nr
= para_next_signal();
1562 handle_signal(sig_nr
);
1567 static void set_initial_status(void)
1569 audiod_status
= AUDIOD_ON
;
1570 if (!conf
.mode_given
)
1572 if (!strcmp(conf
.mode_arg
, "sb")) {
1573 audiod_status
= AUDIOD_STANDBY
;
1576 if (!strcmp(conf
.mode_arg
, "off")) {
1577 audiod_status
= AUDIOD_OFF
;
1580 if (strcmp(conf
.mode_arg
, "on"))
1581 PARA_WARNING_LOG("%s", "invalid mode\n");
1584 int __noreturn
main(int argc
, char *argv
[])
1590 hostname
= para_hostname();
1591 cmdline_parser(argc
, argv
, &conf
);
1592 para_drop_privileges(conf
.user_arg
);
1593 cf
= configfile_exists();
1595 if (cmdline_parser_configfile(cf
, &conf
, 0, 0, 0)) {
1596 fprintf(stderr
, "parse error in config file\n");
1600 log_welcome("para_audiod", conf
.loglevel_arg
);
1601 i
= init_stream_io();
1603 fprintf(stderr
, "init stream io error: %s\n",
1607 server_uptime(UPTIME_SET
);
1608 set_initial_status();
1611 stat_item_ringbuf
= ringbuffer_new(RINGBUFFER_SIZE
);
1613 setup_signal_handling();
1614 if (conf
.daemon_given
)