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