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