2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file server/speaker.h
21 * @brief Speaker process
26 #ifdef WORDS_BIGENDIAN
27 # define MACHINE_AO_FMT AO_FMT_BIG
29 # define MACHINE_AO_FMT AO_FMT_LITTLE
32 /** @brief How many seconds of input to buffer
34 * While any given connection has this much audio buffered, no more reads will
35 * be issued for that connection. The decoder will have to wait.
37 #define BUFFER_SECONDS 5
39 /** @brief Frame batch size
41 * This controls how many frames are written in one go.
43 * For ALSA we request a buffer of three times this size and set the low
44 * watermark to this amount. The goal is then to keep between 1 and 3 times
45 * this many frames in play.
47 * For all backends we attempt to play up to three times this many frames per
48 * shot. In practice we will often only send much less than this.
52 /** @brief Bytes to send per network packet
54 * Don't make this too big or arithmetic will start to overflow.
56 #define NETWORK_BYTES (1024+sizeof(struct rtp_header))
58 /** @brief Maximum RTP playahead (ms) */
59 #define RTP_AHEAD_MS 1000
61 /** @brief Maximum number of FDs to poll for */
64 /** @brief Track structure
66 * Known tracks are kept in a linked list. Usually there will be at most two
67 * of these but rearranging the queue can cause there to be more.
70 struct track *next; /* next track */
71 int fd; /* input FD */
73 size_t start, used; /* start + bytes used */
74 int eof; /* input is at EOF */
75 int got_format; /* got format yet? */
76 ao_sample_format format; /* sample format */
77 unsigned long long played; /* number of frames played */
78 char *buffer; /* sample buffer */
79 size_t size; /* sample buffer size */
80 int slot; /* poll array slot */
83 /** @brief Structure of a backend */
84 struct speaker_backend {
85 /** @brief Which backend this is
87 * @c -1 terminates the list.
97 /** @brief Lock to configured sample format */
98 #define FIXED_FORMAT 0x0001
100 /** @brief Initialization
102 * Called once at startup. This is responsible for one-time setup
103 * operations, for instance opening a network socket to transmit to.
105 * When writing to a native sound API this might @b not imply opening the
106 * native sound device - that might be done by @c activate below.
110 /** @brief Activation
111 * @return 0 on success, non-0 on error
113 * Called to activate the output device.
115 * After this function succeeds, @ref ready should be non-0. As well as
116 * opening the audio device, this function is responsible for reconfiguring
117 * if it necessary to cope with different samples formats (for backends that
118 * don't demand a single fixed sample format for the lifetime of the server).
120 int (*activate)(void);
122 /** @brief Play sound
123 * @param frames Number of frames to play
124 * @return Number of frames actually played
126 size_t (*play)(size_t frames);
128 /** @brief Deactivation
130 * Called to deactivate the sound device. This is the inverse of
133 void (*deactivate)(void);
135 /** @brief Called before poll()
137 * Called before the call to poll(). Should call addfd() to update the FD
138 * array and stash the slot number somewhere safe.
140 void (*beforepoll)(void);
142 /** @brief Called after poll()
143 * @return 0 if we could play, non-0 if not
145 * Called after the call to poll(). Should arrange to play some audio if the
146 * output device is ready.
148 * The return value should be 0 if the device was ready to play, or nonzero
151 int (*afterpoll)(void);
154 /** @brief Linked list of all prepared tracks */
155 extern struct track *tracks;
157 /** @brief Playing track, or NULL */
158 extern struct track *playing;
160 #endif /* SPEAKER_H */