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