Commit | Line | Data |
---|---|---|
cf714d85 | 1 | /* |
2 | * This file is part of DisOrder | |
5aff007d | 3 | * Copyright (C) 2005-2008 Richard Kettlewell |
cf714d85 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
cf714d85 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
cf714d85 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
cf714d85 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
cf714d85 | 17 | */ |
18 | /** @file server/speaker.h | |
19 | * @brief Speaker process | |
20 | */ | |
21 | #ifndef SPEAKER_H | |
22 | #define SPEAKER_H | |
23 | ||
24 | #ifdef WORDS_BIGENDIAN | |
25 | # define MACHINE_AO_FMT AO_FMT_BIG | |
26 | #else | |
27 | # define MACHINE_AO_FMT AO_FMT_LITTLE | |
28 | #endif | |
29 | ||
5a7c42a8 | 30 | /** @brief Minimum number of frames to try to play at once |
cf714d85 | 31 | * |
5a7c42a8 | 32 | * The main loop will only attempt to play any audio when this many |
33 | * frames are available (or the current track has reached the end). | |
34 | * The actual number of frames it attempts to play will often be | |
35 | * larger than this (up to three times). | |
cf714d85 | 36 | * |
37 | * For ALSA we request a buffer of three times this size and set the low | |
38 | * watermark to this amount. The goal is then to keep between 1 and 3 times | |
39 | * this many frames in play. | |
40 | * | |
5a7c42a8 | 41 | * For other we attempt to play up to three times this many frames per |
cf714d85 | 42 | * shot. In practice we will often only send much less than this. |
43 | */ | |
44 | #define FRAMES 4096 | |
45 | ||
46 | /** @brief Bytes to send per network packet | |
a684c959 RK |
47 | * |
48 | * This is the maximum number of bytes we pass to write(2); to determine actual | |
49 | * packet sizes, add a UDP header and an IP header (and a link layer header if | |
50 | * it's the link layer size you care about). | |
cf714d85 | 51 | * |
52 | * Don't make this too big or arithmetic will start to overflow. | |
53 | */ | |
a684c959 | 54 | #define NETWORK_BYTES (1500-8/*UDP*/-40/*IP*/-8/*conservatism*/) |
cf714d85 | 55 | |
cf714d85 | 56 | /** @brief Maximum number of FDs to poll for */ |
57 | #define NFDS 256 | |
58 | ||
59 | /** @brief Track structure | |
60 | * | |
61 | * Known tracks are kept in a linked list. Usually there will be at most two | |
62 | * of these but rearranging the queue can cause there to be more. | |
63 | */ | |
64 | struct track { | |
6d2d327c RK |
65 | /** @brief Next track */ |
66 | struct track *next; | |
67 | ||
68 | /** @brief Input file descriptor */ | |
cf714d85 | 69 | int fd; /* input FD */ |
6d2d327c RK |
70 | |
71 | /** @brief Track ID */ | |
72 | char id[24]; | |
73 | ||
74 | /** @brief Start position of data in buffer */ | |
75 | size_t start; | |
76 | ||
77 | /** @brief Number of bytes of data in buffer */ | |
78 | size_t used; | |
79 | ||
80 | /** @brief Set @c fd is at EOF */ | |
81 | int eof; | |
82 | ||
83 | /** @brief Total number of frames played */ | |
84 | unsigned long long played; | |
85 | ||
86 | /** @brief Slot in @ref fds */ | |
87 | int slot; | |
88 | ||
f5a03f58 RK |
89 | /** @brief Set when playable |
90 | * | |
91 | * A track becomes playable whenever it fills its buffer or reaches EOF; it | |
92 | * stops being playable when it entirely empties its buffer. Tracks start | |
93 | * out life not playable. | |
94 | */ | |
95 | int playable; | |
96 | ||
6d2d327c RK |
97 | /** @brief Input buffer |
98 | * | |
99 | * 1Mbyte is enough for nearly 6s of 44100Hz 16-bit stereo | |
100 | */ | |
101 | char buffer[1048576]; | |
cf714d85 | 102 | }; |
103 | ||
104 | /** @brief Structure of a backend */ | |
105 | struct speaker_backend { | |
106 | /** @brief Which backend this is | |
107 | * | |
108 | * @c -1 terminates the list. | |
109 | */ | |
110 | int backend; | |
111 | ||
112 | /** @brief Flags | |
113 | * | |
6d2d327c | 114 | * This field is currently not used and must be 0. |
cf714d85 | 115 | */ |
116 | unsigned flags; | |
cf714d85 | 117 | |
118 | /** @brief Initialization | |
119 | * | |
120 | * Called once at startup. This is responsible for one-time setup | |
121 | * operations, for instance opening a network socket to transmit to. | |
122 | * | |
123 | * When writing to a native sound API this might @b not imply opening the | |
124 | * native sound device - that might be done by @c activate below. | |
125 | */ | |
126 | void (*init)(void); | |
127 | ||
128 | /** @brief Activation | |
129 | * @return 0 on success, non-0 on error | |
130 | * | |
131 | * Called to activate the output device. | |
132 | * | |
5a7c42a8 | 133 | * On input @ref device_state may be anything. If it is @ref |
134 | * device_open then the device is already open but might be using | |
135 | * the wrong sample format. The device should be reconfigured to | |
136 | * use the right sample format. | |
137 | * | |
138 | * If it is @ref device_error then a retry is underway and an | |
139 | * attempt to recover or re-open the device (with the right sample | |
140 | * format) should be made. | |
141 | * | |
142 | * If it is @ref device_closed then the device should be opened with | |
143 | * the right sample format. | |
144 | * | |
717ba987 RK |
145 | * Some devices are effectively always open and have no error state, in which |
146 | * case this callback can be NULL. Note that @ref device_state still | |
147 | * switches between @ref device_open and @ref device_closed in this case. | |
cf714d85 | 148 | */ |
5a7c42a8 | 149 | void (*activate)(void); |
cf714d85 | 150 | |
151 | /** @brief Play sound | |
152 | * @param frames Number of frames to play | |
153 | * @return Number of frames actually played | |
5a7c42a8 | 154 | * |
155 | * If an error occurs (and it is not immediately recovered) this | |
156 | * should set @ref device_state to @ref device_error. | |
cf714d85 | 157 | */ |
158 | size_t (*play)(size_t frames); | |
159 | ||
160 | /** @brief Deactivation | |
161 | * | |
5a7c42a8 | 162 | * Called to deactivate the sound device. This is the inverse of @c |
163 | * activate above. | |
164 | * | |
165 | * For sound devices that are open all the time and have no error | |
166 | * state, this callback can be NULL. Note that @ref device_state | |
0e4472a0 | 167 | * still switches between @ref device_open and @ref device_closed in |
5a7c42a8 | 168 | * this case. |
cf714d85 | 169 | */ |
170 | void (*deactivate)(void); | |
171 | ||
172 | /** @brief Called before poll() | |
e84fb5f0 | 173 | * @param timeoutp Pointer to timeout |
cf714d85 | 174 | * |
e84fb5f0 RK |
175 | * Called before the call to poll(). |
176 | * | |
177 | * If desirable, should call addfd() to update the FD array and stash the | |
178 | * slot number somewhere safe. This will only be called if @ref device_state | |
179 | * is @ref device_open. | |
180 | * | |
181 | * @p timeoutp points to the poll timeout value in milliseconds. It may be | |
182 | * reduced, but never increased. | |
23741390 RK |
183 | * |
184 | * NB you can NOT assume that @c beforepoll is always called before @c play. | |
cf714d85 | 185 | */ |
e84fb5f0 | 186 | void (*beforepoll)(int *timeoutp); |
cf714d85 | 187 | |
188 | /** @brief Called after poll() | |
5a7c42a8 | 189 | * @return 1 if output device ready for play, 0 otherwise |
cf714d85 | 190 | * |
5a7c42a8 | 191 | * Called after the call to poll(). This will only be called if |
192 | * @ref device_state = @ref device_open. | |
cf714d85 | 193 | * |
5a7c42a8 | 194 | * The return value should be 1 if the device was ready to play, or |
195 | * 0 if it was not. | |
cf714d85 | 196 | */ |
5a7c42a8 | 197 | int (*ready)(void); |
cf714d85 | 198 | }; |
199 | ||
5a7c42a8 | 200 | /** @brief Possible device states */ |
201 | enum device_states { | |
202 | /** @brief The device is closed */ | |
203 | device_closed, | |
204 | ||
205 | /** @brief The device is open and ready to receive sound | |
206 | * | |
207 | * The current device sample format is potentially part of this state. | |
208 | */ | |
209 | device_open, | |
210 | ||
211 | /** @brief An error has occurred on the device | |
212 | * | |
213 | * This state is used to ensure that a small interval is left | |
214 | * between retrying the device. If errors just set @ref | |
215 | * device_closed then the main loop would busy-wait on broken output | |
216 | * devices. | |
217 | * | |
218 | * The current device sample format is potentially part of this state. | |
219 | */ | |
220 | device_error | |
221 | }; | |
cf714d85 | 222 | |
5a7c42a8 | 223 | extern enum device_states device_state; |
5a7c42a8 | 224 | extern struct track *tracks; |
cf714d85 | 225 | extern struct track *playing; |
226 | ||
1c3f1e73 | 227 | extern const struct speaker_backend network_backend; |
228 | extern const struct speaker_backend alsa_backend; | |
229 | extern const struct speaker_backend command_backend; | |
937be4c0 | 230 | extern const struct speaker_backend coreaudio_backend; |
e99d42b1 | 231 | extern const struct speaker_backend oss_backend; |
1c3f1e73 | 232 | |
233 | extern struct pollfd fds[NFDS]; | |
234 | extern int fdno; | |
6d2d327c | 235 | extern size_t bpf; |
1c3f1e73 | 236 | extern int idled; |
237 | ||
238 | int addfd(int fd, int events); | |
1c3f1e73 | 239 | void abandon(void); |
240 | ||
cf714d85 | 241 | #endif /* SPEAKER_H */ |
242 | ||
243 | /* | |
244 | Local Variables: | |
245 | c-basic-offset:2 | |
246 | comment-column:40 | |
247 | fill-column:79 | |
248 | indent-tabs-mode:nil | |
249 | End: | |
250 | */ |