X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/795192f4c5b972ddcb3b4bdcba0037567cf9c4c4..cf714d856f8e57ec300704b665f0bbf33a4a317d:/server/speaker.c diff --git a/server/speaker.c b/server/speaker.c index 4b3f7dc..172671b 100644 --- a/server/speaker.c +++ b/server/speaker.c @@ -18,7 +18,7 @@ * USA */ /** @file server/speaker.c - * @brief Speaker processs + * @brief Speaker process * * This program is responsible for transmitting a single coherent audio stream * to its destination (over the network, to some sound API, to some @@ -82,61 +82,22 @@ #include "log.h" #include "defs.h" #include "mem.h" -#include "speaker.h" +#include "speaker-protocol.h" #include "user.h" #include "addr.h" #include "timeval.h" #include "rtp.h" +#include "speaker.h" #if API_ALSA #include #endif -#ifdef WORDS_BIGENDIAN -# define MACHINE_AO_FMT AO_FMT_BIG -#else -# define MACHINE_AO_FMT AO_FMT_LITTLE -#endif - -/** @brief How many seconds of input to buffer - * - * While any given connection has this much audio buffered, no more reads will - * be issued for that connection. The decoder will have to wait. - */ -#define BUFFER_SECONDS 5 - -#define FRAMES 4096 /* Frame batch size */ - -/** @brief Bytes to send per network packet - * - * Don't make this too big or arithmetic will start to overflow. - */ -#define NETWORK_BYTES (1024+sizeof(struct rtp_header)) - -/** @brief Maximum RTP playahead (ms) */ -#define RTP_AHEAD_MS 1000 +/** @brief Linked list of all prepared tracks */ +struct track *tracks; -/** @brief Maximum number of FDs to poll for */ -#define NFDS 256 - -/** @brief Track structure - * - * Known tracks are kept in a linked list. Usually there will be at most two - * of these but rearranging the queue can cause there to be more. - */ -static struct track { - struct track *next; /* next track */ - int fd; /* input FD */ - char id[24]; /* ID */ - size_t start, used; /* start + bytes used */ - int eof; /* input is at EOF */ - int got_format; /* got format yet? */ - ao_sample_format format; /* sample format */ - unsigned long long played; /* number of frames played */ - char *buffer; /* sample buffer */ - size_t size; /* sample buffer size */ - int slot; /* poll array slot */ -} *tracks, *playing; /* all tracks + playing track */ +/** @brief Playing track, or NULL */ +struct track *playing; static time_t last_report; /* when we last reported */ static int paused; /* pause status */ @@ -159,9 +120,25 @@ static ao_sample_format pcm_format; /* current format if aodev != 0 */ */ static int ready; -static int forceplay; /* frames to force play */ -static int cmdfd = -1; /* child process input */ -static int bfd = -1; /* broadcast FD */ +/** @brief Frames to force-play + * + * If this is nonzero, and playing is enabled, then the main loop will attempt + * to play this many frames without checking whether the output device is + * ready. + */ +static int forceplay; + +/** @brief Pipe to subprocess + * + * This is the file descriptor to write to for @ref BACKEND_COMMAND. + */ +static int cmdfd = -1; + +/** @brief Network socket + * + * This is the file descriptor to write to for @ref BACKEND_NETWORK. + */ +static int bfd = -1; /** @brief RTP timestamp * @@ -183,63 +160,21 @@ static uint64_t rtp_time; */ static struct timeval rtp_time_0; -static uint16_t rtp_seq; /* frame sequence number */ -static uint32_t rtp_id; /* RTP SSRC */ -static int idled; /* set when idled */ -static int audio_errors; /* audio error counter */ +/** @brief RTP packet sequence number */ +static uint16_t rtp_seq; -/** @brief Structure of a backend */ -struct speaker_backend { - /** @brief Which backend this is - * - * @c -1 terminates the list. - */ - int backend; +/** @brief RTP SSRC */ +static uint32_t rtp_id; - /** @brief Flags - * - * Possible values - * - @ref FIXED_FORMAT - */ - unsigned flags; -/** @brief Lock to configured sample format */ -#define FIXED_FORMAT 0x0001 - - /** @brief Initialization - * - * Called once at startup. This is responsible for one-time setup - * operations, for instance opening a network socket to transmit to. - * - * When writing to a native sound API this might @b not imply opening the - * native sound device - that might be done by @c activate below. - */ - void (*init)(void); - - /** @brief Activation - * @return 0 on success, non-0 on error - * - * Called to activate the output device. - * - * After this function succeeds, @ref ready should be non-0. As well as - * opening the audio device, this function is responsible for reconfiguring - * if it necessary to cope with different samples formats (for backends that - * don't demand a single fixed sample format for the lifetime of the server). - */ - int (*activate)(void); +/** @brief Set when idled + * + * This is set when the sound device is deliberately closed by idle(). + * @ref ready is set to 0 at the same time. + */ +static int idled; /* set when idled */ - /** @brief Play sound - * @param frames Number of frames to play - * @return Number of frames actually played - */ - size_t (*play)(size_t frames); - - /** @brief Deactivation - * - * Called to deactivate the sound device. This is the inverse of - * @c activate above. - */ - void (*deactivate)(void); -}; +/** @brief Error counter */ +static int audio_errors; /** @brief Selected backend */ static const struct speaker_backend *backend; @@ -424,7 +359,9 @@ static void enable_translation(struct track *t) { * @param t Pointer to track * @return 0 on success, -1 on EOF * - * This is effectively the read callback on @c t->fd. + * This is effectively the read callback on @c t->fd. It is called from the + * main loop whenever the track's file descriptor is readable, assuming the + * buffer has not reached the maximum allowed occupancy. */ static int fill(struct track *t) { size_t where, left; @@ -474,7 +411,12 @@ static int fill(struct track *t) { return 0; } -/** @brief Close the sound device */ +/** @brief Close the sound device + * + * This is called to deactivate the output device when pausing, and also by the + * ALSA backend when changing encoding (in which case the sound device will be + * immediately reactivated). + */ static void idle(void) { D(("idle")); if(backend->deactivate) @@ -540,7 +482,13 @@ static int activate(void) { return backend->activate(); } -/* Check to see whether the current track has finished playing */ +/** @brief Check whether the current track has finished + * + * The current track is determined to have finished either if the input stream + * eded before the format could be determined (i.e. it is malformed) or the + * input is at end of file and there is less than a frame left unplayed. (So + * it copes with decoders that crash mid-frame.) + */ static void maybe_finished(void) { if(playing && playing->eof @@ -549,6 +497,7 @@ static void maybe_finished(void) { abandon(); } +/** @brief Start the subprocess for @ref BACKEND_COMMAND */ static void fork_cmd(void) { pid_t cmdpid; int pfd[2]; @@ -568,6 +517,7 @@ static void fork_cmd(void) { D(("forked cmd %d, fd = %d", cmdpid, cmdfd)); } +/** @brief Play up to @p frames frames of audio */ static void play(size_t frames) { size_t avail_frames, avail_bytes, written_frames; ssize_t written_bytes; @@ -791,6 +741,51 @@ static size_t alsa_play(size_t frames) { return pcm_written_frames; } +static int alsa_slots, alsa_nslots = -1; + +/** @brief Fill in poll fd array for ALSA */ +static void alsa_beforepoll(void) { + /* We send sample data to ALSA as fast as it can accept it, relying on + * the fact that it has a relatively small buffer to minimize pause + * latency. */ + int retry = 3, err; + + alsa_slots = fdno; + do { + retry = 0; + alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno); + if((alsa_nslots <= 0 + || !(fds[alsa_slots].events & POLLOUT)) + && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) { + error(0, "underrun detected after call to snd_pcm_poll_descriptors()"); + if((err = snd_pcm_prepare(pcm))) + fatal(0, "error calling snd_pcm_prepare: %d", err); + } else + break; + } while(retry-- > 0); + if(alsa_nslots >= 0) + fdno += alsa_nslots; +} + +/** @brief Process poll() results for ALSA */ +static int alsa_afterpoll(void) { + int err; + + if(alsa_slots != -1) { + unsigned short alsa_revents; + + if((err = snd_pcm_poll_descriptors_revents(pcm, + &fds[alsa_slots], + alsa_nslots, + &alsa_revents)) < 0) + fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err); + if(alsa_revents & (POLLOUT | POLLERR)) + play(3 * FRAMES); + return 0; + } else + return 1; +} + /** @brief ALSA deactivation */ static void alsa_deactivate(void) { if(pcm) { @@ -838,6 +833,26 @@ static size_t command_play(size_t frames) { return written_bytes / bpf; } +static int cmdfd_slot; + +/** @brief Update poll array for writing to subprocess */ +static void command_beforepoll(void) { + /* We send sample data to the subprocess as fast as it can accept it. + * This isn't ideal as pause latency can be very high as a result. */ + if(cmdfd >= 0) + cmdfd_slot = addfd(cmdfd, POLLOUT); +} + +/** @brief Process poll() results for subprocess play */ +static int command_afterpoll(void) { + if(cmdfd_slot != -1) { + if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR)) + play(3 * FRAMES); + return 0; + } else + return -1; +} + /** @brief Command/network backend activation */ static int generic_activate(void) { if(!ready) { @@ -1030,6 +1045,43 @@ static size_t network_play(size_t frames) { return written_frames; } +static int bfd_slot; + +/** @brief Set up poll array for network play */ +static void network_beforepoll(void) { + struct timeval now; + uint64_t target_us; + uint64_t target_rtp_time; + const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS + * config->sample_format.rate + * config->sample_format.channels + / 1000); + + /* If we're starting then initialize the base time */ + if(!rtp_time) + xgettimeofday(&rtp_time_0, 0); + /* We send audio data whenever we get RTP_AHEAD seconds or more + * behind */ + xgettimeofday(&now, 0); + target_us = tvsub_us(now, rtp_time_0); + assert(target_us <= UINT64_MAX / 88200); + target_rtp_time = (target_us * config->sample_format.rate + * config->sample_format.channels) + / 1000000; + if((int64_t)(rtp_time - target_rtp_time) < samples_ahead) + bfd_slot = addfd(bfd, POLLOUT); +} + +/** @brief Process poll() results for network play */ +static int network_afterpoll(void) { + if(bfd_slot != -1) { + if(fds[bfd_slot].revents & (POLLOUT | POLLERR)) + play(3 * FRAMES); + return 0; + } else + return 1; +} + /** @brief Table of speaker backends */ static const struct speaker_backend backends[] = { #if API_ALSA @@ -1039,7 +1091,9 @@ static const struct speaker_backend backends[] = { alsa_init, alsa_activate, alsa_play, - alsa_deactivate + alsa_deactivate, + alsa_beforepoll, + alsa_afterpoll }, #endif { @@ -1048,7 +1102,9 @@ static const struct speaker_backend backends[] = { command_init, generic_activate, command_play, - 0 /* deactivate */ + 0, /* deactivate */ + command_beforepoll, + command_afterpoll }, { BACKEND_NETWORK, @@ -1056,57 +1112,60 @@ static const struct speaker_backend backends[] = { network_init, generic_activate, network_play, - 0 /* deactivate */ + 0, /* deactivate */ + network_beforepoll, + network_afterpoll }, - { -1, 0, 0, 0, 0, 0 } + { -1, 0, 0, 0, 0, 0, 0, 0 } /* end of list */ }; -int main(int argc, char **argv) { - int n, fd, stdin_slot, alsa_slots, cmdfd_slot, bfd_slot, poke, timeout; +/** @brief Main event loop + * + * This has grown in a rather bizarre and ad-hoc way is very sensitive to + * changes... + * + * Firstly the loop is terminated when the parent process exits. Therefore the + * speaker process has the same lifetime as the main server. This and the + * reading of data from decoders is comprehensible enough. + * + * The playing of audio is more complicated however. + * + * On the first run through when a track is ready to be played, @ref ready and + * @ref forceplay will both be zero. Therefore @c beforepoll is not called. + * + * @c afterpoll on the other hand @b is called and will return nonzero. The + * result is that we call @c play(0). This will call activate(), setting + * @ref ready nonzero, but otherwise has no immediate effect. + * + * We then deal with stdin and the decoders. + * + * We then reach the second place we might play some audio. @ref forceplay is + * 0 so nothing happens here again. + * + * On the next iteration through however @ref ready is nonzero, and @ref + * forceplay is 0, so we call @c beforepoll. After the @c poll() we call @c + * afterpoll and actually get some audio played. + * + * This is surely @b far more complicated than it needs to be! + * + * If at any call to play(), activate() fails, or if there aren't enough bytes + * in the buffer to satisfy the request, then @ref forceplay is set non-0. On + * the next pass through the event loop @c beforepoll is not called. This + * means that (if none of the other FDs trigger) the @c poll() call will block + * for up to a second. @c afterpoll will return nonzero, since @c beforepoll + * wasn't called, and consequently play() is called with @ref forceplay as its + * argument. + * + * The effect is to attempt to restart playing audio - including the activate() + * step, which may have failed at the previous attempt - at least once a second + * after an error has disabled it. The delay prevents busy-waiting on whatever + * condition has rendered the audio device uncooperative. + */ +static void mainloop(void) { struct track *t; struct speaker_message sm; -#if API_ALSA - int alsa_nslots = -1, err; -#endif + int n, fd, stdin_slot, poke, timeout; - set_progname(argv); - if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); - while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { - switch(n) { - case 'h': help(); - case 'V': version(); - case 'c': configfile = optarg; break; - case 'd': debugging = 1; break; - case 'D': debugging = 0; break; - default: fatal(0, "invalid option"); - } - } - if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1; - /* If stderr is a TTY then log there, otherwise to syslog. */ - if(!isatty(2)) { - openlog(progname, LOG_PID, LOG_DAEMON); - log_default = &log_syslog; - } - if(config_read()) fatal(0, "cannot read configuration"); - /* ignore SIGPIPE */ - signal(SIGPIPE, SIG_IGN); - /* reap kids */ - signal(SIGCHLD, reap); - /* set nice value */ - xnice(config->nice_speaker); - /* change user */ - become_mortal(); - /* make sure we're not root, whatever the config says */ - if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); - /* identify the backend used to play */ - for(n = 0; backends[n].backend != -1; ++n) - if(backends[n].backend == config->speaker_backend) - break; - if(backends[n].backend == -1) - fatal(0, "unsupported backend %d", config->speaker_backend); - backend = &backends[n]; - /* backend-specific initialization */ - backend->init(); while(getppid() != 1) { fdno = 0; /* Always ready for commands from the main server. */ @@ -1119,85 +1178,18 @@ int main(int argc, char **argv) { playing->slot = -1; /* If forceplay is set then wait until it succeeds before waiting on the * sound device. */ +#if API_ALSA alsa_slots = -1; +#endif cmdfd_slot = -1; bfd_slot = -1; /* By default we will wait up to a second before thinking about current * state. */ timeout = 1000; - if(ready && !forceplay) { - switch(config->speaker_backend) { - case BACKEND_COMMAND: - /* We send sample data to the subprocess as fast as it can accept it. - * This isn't ideal as pause latency can be very high as a result. */ - if(cmdfd >= 0) - cmdfd_slot = addfd(cmdfd, POLLOUT); - break; - case BACKEND_NETWORK: { - struct timeval now; - uint64_t target_us; - uint64_t target_rtp_time; - const int64_t samples_ahead = ((uint64_t)RTP_AHEAD_MS - * config->sample_format.rate - * config->sample_format.channels - / 1000); -#if 0 - static unsigned logit; -#endif - - /* If we're starting then initialize the base time */ - if(!rtp_time) - xgettimeofday(&rtp_time_0, 0); - /* We send audio data whenever we get RTP_AHEAD seconds or more - * behind */ - xgettimeofday(&now, 0); - target_us = tvsub_us(now, rtp_time_0); - assert(target_us <= UINT64_MAX / 88200); - target_rtp_time = (target_us * config->sample_format.rate - * config->sample_format.channels) - - / 1000000; -#if 0 - /* TODO remove logging guff */ - if(!(logit++ & 1023)) - info("rtp_time %llu target %llu difference %lld [%lld]", - rtp_time, target_rtp_time, - rtp_time - target_rtp_time, - samples_ahead); -#endif - if((int64_t)(rtp_time - target_rtp_time) < samples_ahead) - bfd_slot = addfd(bfd, POLLOUT); - break; - } -#if API_ALSA - case BACKEND_ALSA: { - /* We send sample data to ALSA as fast as it can accept it, relying on - * the fact that it has a relatively small buffer to minimize pause - * latency. */ - int retry = 3; - - alsa_slots = fdno; - do { - retry = 0; - alsa_nslots = snd_pcm_poll_descriptors(pcm, &fds[fdno], NFDS - fdno); - if((alsa_nslots <= 0 - || !(fds[alsa_slots].events & POLLOUT)) - && snd_pcm_state(pcm) == SND_PCM_STATE_XRUN) { - error(0, "underrun detected after call to snd_pcm_poll_descriptors()"); - if((err = snd_pcm_prepare(pcm))) - fatal(0, "error calling snd_pcm_prepare: %d", err); - } else - break; - } while(retry-- > 0); - if(alsa_nslots >= 0) - fdno += alsa_nslots; - break; - } -#endif - default: - assert(!"unknown backend"); - } - } + /* We'll break the poll as soon as the underlying sound device is ready for + * more data */ + if(ready && !forceplay) + backend->beforepoll(); /* If any other tracks don't have a full buffer, try to read sample data * from them. */ for(t = tracks; t; t = t->next) @@ -1214,39 +1206,7 @@ int main(int argc, char **argv) { fatal(errno, "error calling poll"); } /* Play some sound before doing anything else */ - poke = 0; - switch(config->speaker_backend) { -#if API_ALSA - case BACKEND_ALSA: - if(alsa_slots != -1) { - unsigned short alsa_revents; - - if((err = snd_pcm_poll_descriptors_revents(pcm, - &fds[alsa_slots], - alsa_nslots, - &alsa_revents)) < 0) - fatal(0, "error calling snd_pcm_poll_descriptors_revents: %d", err); - if(alsa_revents & (POLLOUT | POLLERR)) - play(3 * FRAMES); - } else - poke = 1; - break; -#endif - case BACKEND_COMMAND: - if(cmdfd_slot != -1) { - if(fds[cmdfd_slot].revents & (POLLOUT | POLLERR)) - play(3 * FRAMES); - } else - poke = 1; - break; - case BACKEND_NETWORK: - if(bfd_slot != -1) { - if(fds[bfd_slot].revents & (POLLOUT | POLLERR)) - play(3 * FRAMES); - } else - poke = 1; - break; - } + poke = backend->afterpoll(); if(poke) { /* Some attempt to play must have failed */ if(playing && !paused) @@ -1329,6 +1289,50 @@ int main(int argc, char **argv) { if(time(0) > last_report) report(); } +} + +int main(int argc, char **argv) { + int n; + + set_progname(argv); + if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); + while((n = getopt_long(argc, argv, "hVc:dD", options, 0)) >= 0) { + switch(n) { + case 'h': help(); + case 'V': version(); + case 'c': configfile = optarg; break; + case 'd': debugging = 1; break; + case 'D': debugging = 0; break; + default: fatal(0, "invalid option"); + } + } + if(getenv("DISORDER_DEBUG_SPEAKER")) debugging = 1; + /* If stderr is a TTY then log there, otherwise to syslog. */ + if(!isatty(2)) { + openlog(progname, LOG_PID, LOG_DAEMON); + log_default = &log_syslog; + } + if(config_read()) fatal(0, "cannot read configuration"); + /* ignore SIGPIPE */ + signal(SIGPIPE, SIG_IGN); + /* reap kids */ + signal(SIGCHLD, reap); + /* set nice value */ + xnice(config->nice_speaker); + /* change user */ + become_mortal(); + /* make sure we're not root, whatever the config says */ + if(getuid() == 0 || geteuid() == 0) fatal(0, "do not run as root"); + /* identify the backend used to play */ + for(n = 0; backends[n].backend != -1; ++n) + if(backends[n].backend == config->speaker_backend) + break; + if(backends[n].backend == -1) + fatal(0, "unsupported backend %d", config->speaker_backend); + backend = &backends[n]; + /* backend-specific initialization */ + backend->init(); + mainloop(); info("stopped (parent terminated)"); exit(0); }