X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/cf714d856f8e57ec300704b665f0bbf33a4a317d..237413906dcdf4b1ba1e5809bb7510bae71b1956:/server/speaker.h?ds=sidebyside diff --git a/server/speaker.h b/server/speaker.h index 81c2ff4..2af2c6b 100644 --- a/server/speaker.h +++ b/server/speaker.h @@ -29,34 +29,31 @@ # define MACHINE_AO_FMT AO_FMT_LITTLE #endif -/** @brief How many seconds of input to buffer +/** @brief Minimum number of frames to try to play at once * - * 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 - -/** @brief Frame batch size - * - * This controls how many frames are written in one go. + * The main loop will only attempt to play any audio when this many + * frames are available (or the current track has reached the end). + * The actual number of frames it attempts to play will often be + * larger than this (up to three times). * * For ALSA we request a buffer of three times this size and set the low * watermark to this amount. The goal is then to keep between 1 and 3 times * this many frames in play. * - * For all backends we attempt to play up to three times this many frames per + * For other we attempt to play up to three times this many frames per * shot. In practice we will often only send much less than this. */ #define FRAMES 4096 /** @brief Bytes to send per network packet + * + * This is the maximum number of bytes we pass to write(2); to determine actual + * packet sizes, add a UDP header and an IP header (and a link layer header if + * it's the link layer size you care about). * * 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 +#define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/) /** @brief Maximum number of FDs to poll for */ #define NFDS 256 @@ -67,17 +64,43 @@ * of these but rearranging the queue can cause there to be more. */ struct track { - struct track *next; /* next track */ + /** @brief Next track */ + struct track *next; + + /** @brief Input file descriptor */ 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 */ + + /** @brief Track ID */ + char id[24]; + + /** @brief Start position of data in buffer */ + size_t start; + + /** @brief Number of bytes of data in buffer */ + size_t used; + + /** @brief Set @c fd is at EOF */ + int eof; + + /** @brief Total number of frames played */ + unsigned long long played; + + /** @brief Slot in @ref fds */ + int slot; + + /** @brief Set when playable + * + * A track becomes playable whenever it fills its buffer or reaches EOF; it + * stops being playable when it entirely empties its buffer. Tracks start + * out life not playable. + */ + int playable; + + /** @brief Input buffer + * + * 1Mbyte is enough for nearly 6s of 44100Hz 16-bit stereo + */ + char buffer[1048576]; }; /** @brief Structure of a backend */ @@ -90,12 +113,9 @@ struct speaker_backend { /** @brief Flags * - * Possible values - * - @ref FIXED_FORMAT + * This field is currently not used and must be 0. */ unsigned flags; -/** @brief Lock to configured sample format */ -#define FIXED_FORMAT 0x0001 /** @brief Initialization * @@ -112,51 +132,114 @@ struct speaker_backend { * * 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). + * On input @ref device_state may be anything. If it is @ref + * device_open then the device is already open but might be using + * the wrong sample format. The device should be reconfigured to + * use the right sample format. + * + * If it is @ref device_error then a retry is underway and an + * attempt to recover or re-open the device (with the right sample + * format) should be made. + * + * If it is @ref device_closed then the device should be opened with + * the right sample format. + * + * Some devices are effectively always open and have no error state, in which + * case this callback can be NULL. Note that @ref device_state still + * switches between @ref device_open and @ref device_closed in this case. */ - int (*activate)(void); + void (*activate)(void); /** @brief Play sound * @param frames Number of frames to play * @return Number of frames actually played + * + * If an error occurs (and it is not immediately recovered) this + * should set @ref device_state to @ref device_error. */ size_t (*play)(size_t frames); /** @brief Deactivation * - * Called to deactivate the sound device. This is the inverse of - * @c activate above. + * Called to deactivate the sound device. This is the inverse of @c + * activate above. + * + * For sound devices that are open all the time and have no error + * state, this callback can be NULL. Note that @ref device_state + * still switches between @ref device_open and @ref device_closed in + * this case. */ void (*deactivate)(void); /** @brief Called before poll() + * @param timeoutp Pointer to timeout + * + * Called before the call to poll(). + * + * If desirable, should call addfd() to update the FD array and stash the + * slot number somewhere safe. This will only be called if @ref device_state + * is @ref device_open. * - * Called before the call to poll(). Should call addfd() to update the FD - * array and stash the slot number somewhere safe. + * @p timeoutp points to the poll timeout value in milliseconds. It may be + * reduced, but never increased. + * + * NB you can NOT assume that @c beforepoll is always called before @c play. */ - void (*beforepoll)(void); + void (*beforepoll)(int *timeoutp); /** @brief Called after poll() - * @return 0 if we could play, non-0 if not + * @return 1 if output device ready for play, 0 otherwise * - * Called after the call to poll(). Should arrange to play some audio if the - * output device is ready. + * Called after the call to poll(). This will only be called if + * @ref device_state = @ref device_open. * - * The return value should be 0 if the device was ready to play, or nonzero - * if it was not. + * The return value should be 1 if the device was ready to play, or + * 0 if it was not. */ - int (*afterpoll)(void); + int (*ready)(void); }; -/** @brief Linked list of all prepared tracks */ -extern struct track *tracks; +/** @brief Possible device states */ +enum device_states { + /** @brief The device is closed */ + device_closed, -/** @brief Playing track, or NULL */ + /** @brief The device is open and ready to receive sound + * + * The current device sample format is potentially part of this state. + */ + device_open, + + /** @brief An error has occurred on the device + * + * This state is used to ensure that a small interval is left + * between retrying the device. If errors just set @ref + * device_closed then the main loop would busy-wait on broken output + * devices. + * + * The current device sample format is potentially part of this state. + */ + device_error +}; + +extern enum device_states device_state; +extern struct track *tracks; extern struct track *playing; +extern const struct speaker_backend network_backend; +extern const struct speaker_backend alsa_backend; +extern const struct speaker_backend command_backend; +extern const struct speaker_backend coreaudio_backend; +extern const struct speaker_backend oss_backend; + +extern struct pollfd fds[NFDS]; +extern int fdno; +extern size_t bpf; +extern int idled; + +int addfd(int fd, int events); +void abandon(void); + #endif /* SPEAKER_H */ /*