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