Mercurial > mplayer.hg
annotate libao2/ao_esd.c @ 13987:bde510e335cd
synced to 1.13
author | gabrov |
---|---|
date | Fri, 19 Nov 2004 20:34:56 +0000 |
parents | c1955840883d |
children | a92101a7eb49 |
rev | line source |
---|---|
8572 | 1 /* |
2 * ao_esd - EsounD audio output driver for MPlayer | |
3 * | |
4 * Juergen Keil <jk@tools.de> | |
5 * | |
6 * This driver is distributed under the terms of the GPL | |
7 * | |
8 * TODO / known problems: | |
9 * - does not work well when the esd daemon has autostandby disabled | |
10 * (workaround: run esd with option "-as 2" - fortunatelly this is | |
11 * the default) | |
12 * - plays noise on a linux 2.4.4 kernel with a SB16PCI card, when using | |
13 * a local tcp connection to the esd daemon; there is no noise when using | |
14 * a unix domain socket connection. | |
15 * (there are EIO errors reported by the sound card driver, so this is | |
16 * most likely a linux sound card driver problem) | |
17 */ | |
18 | |
19 #include "../config.h" | |
20 | |
21 #include <sys/types.h> | |
22 #include <sys/time.h> | |
23 #include <sys/socket.h> | |
24 #include <stdio.h> | |
25 #include <string.h> | |
26 #include <unistd.h> | |
27 #include <errno.h> | |
28 #include <fcntl.h> | |
8623
440301fef3fe
Added/reordered #includes to silence warnings about "implicit declaration".
rathann
parents:
8572
diff
changeset
|
29 #include <time.h> |
8572 | 30 #ifdef __svr4__ |
31 #include <stropts.h> | |
32 #endif | |
33 #include <esd.h> | |
34 | |
35 #include "audio_out.h" | |
36 #include "audio_out_internal.h" | |
37 #include "afmt.h" | |
38 #include "../config.h" | |
39 #include "../mp_msg.h" | |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
40 #include "../help_mp.h" |
8572 | 41 |
42 | |
43 #undef ESD_DEBUG | |
44 | |
45 #if ESD_DEBUG | |
46 #define dprintf(...) printf(__VA_ARGS__) | |
47 #else | |
48 #define dprintf(...) /**/ | |
49 #endif | |
50 | |
51 | |
52 #define ESD_CLIENT_NAME "MPlayer" | |
53 #define ESD_MAX_DELAY (1.0f) /* max amount of data buffered in esd (#sec) */ | |
54 | |
55 static ao_info_t info = | |
56 { | |
57 "EsounD audio output", | |
58 "esd", | |
59 "Juergen Keil <jk@tools.de>", | |
60 "" | |
61 }; | |
62 | |
63 LIBAO_EXTERN(esd) | |
64 | |
65 static int esd_fd = -1; | |
66 static int esd_play_fd = -1; | |
67 static esd_server_info_t *esd_svinfo; | |
68 static int esd_latency; | |
69 static int esd_bytes_per_sample; | |
70 static unsigned long esd_samples_written; | |
71 static struct timeval esd_play_start; | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
72 extern float audio_delay; |
8572 | 73 |
74 /* | |
75 * to set/get/query special features/parameters | |
76 */ | |
9633
12b1790038b0
64bit libao2 fix by Jens Axboe <mplayer-dev@kernel.dk>
alex
parents:
8623
diff
changeset
|
77 static int control(int cmd, void *arg) |
8572 | 78 { |
79 esd_player_info_t *esd_pi; | |
80 esd_info_t *esd_i; | |
81 time_t now; | |
82 static time_t vol_cache_time; | |
83 static ao_control_vol_t vol_cache; | |
84 | |
85 switch (cmd) { | |
86 case AOCONTROL_GET_VOLUME: | |
87 time(&now); | |
88 if (now == vol_cache_time) { | |
89 *(ao_control_vol_t *)arg = vol_cache; | |
90 return CONTROL_OK; | |
91 } | |
92 | |
93 dprintf("esd: get vol\n"); | |
94 if ((esd_i = esd_get_all_info(esd_fd)) == NULL) | |
95 return CONTROL_ERROR; | |
96 | |
97 for (esd_pi = esd_i->player_list; esd_pi != NULL; esd_pi = esd_pi->next) | |
98 if (strcmp(esd_pi->name, ESD_CLIENT_NAME) == 0) | |
99 break; | |
100 | |
101 if (esd_pi != NULL) { | |
102 ao_control_vol_t *vol = (ao_control_vol_t *)arg; | |
103 vol->left = esd_pi->left_vol_scale * 100 / ESD_VOLUME_BASE; | |
104 vol->right = esd_pi->right_vol_scale * 100 / ESD_VOLUME_BASE; | |
105 | |
106 vol_cache = *vol; | |
107 vol_cache_time = now; | |
108 } | |
109 esd_free_all_info(esd_i); | |
110 | |
111 return CONTROL_OK; | |
112 | |
113 case AOCONTROL_SET_VOLUME: | |
114 dprintf("esd: set vol\n"); | |
115 if ((esd_i = esd_get_all_info(esd_fd)) == NULL) | |
116 return CONTROL_ERROR; | |
117 | |
118 for (esd_pi = esd_i->player_list; esd_pi != NULL; esd_pi = esd_pi->next) | |
119 if (strcmp(esd_pi->name, ESD_CLIENT_NAME) == 0) | |
120 break; | |
121 | |
122 if (esd_pi != NULL) { | |
123 ao_control_vol_t *vol = (ao_control_vol_t *)arg; | |
124 esd_set_stream_pan(esd_fd, esd_pi->source_id, | |
125 vol->left * ESD_VOLUME_BASE / 100, | |
126 vol->right * ESD_VOLUME_BASE / 100); | |
127 | |
128 vol_cache = *vol; | |
129 time(&vol_cache_time); | |
130 } | |
131 esd_free_all_info(esd_i); | |
132 return CONTROL_OK; | |
133 | |
134 default: | |
135 return CONTROL_UNKNOWN; | |
136 } | |
137 } | |
138 | |
139 | |
140 /* | |
141 * open & setup audio device | |
142 * return: 1=success 0=fail | |
143 */ | |
144 static int init(int rate_hz, int channels, int format, int flags) | |
145 { | |
146 esd_format_t esd_fmt; | |
147 int bytes_per_sample; | |
148 int fl; | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
149 char *server = ao_subdevice; /* NULL for localhost */ |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
150 float lag_seconds, lag_net, lag_serv; |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
151 struct timeval proto_start, proto_end; |
8572 | 152 |
153 if (esd_fd < 0) { | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
154 esd_fd = esd_open_sound(server); |
8572 | 155 if (esd_fd < 0) { |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
156 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_ESD_CantOpenSound, |
8572 | 157 strerror(errno)); |
158 return 0; | |
159 } | |
160 | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
161 /* get server info, and measure network latency */ |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
162 gettimeofday(&proto_start, NULL); |
8572 | 163 esd_svinfo = esd_get_server_info(esd_fd); |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
164 if(server) { |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
165 gettimeofday(&proto_end, NULL); |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
166 lag_net = (proto_end.tv_sec - proto_start.tv_sec) + |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
167 (proto_end.tv_usec - proto_start.tv_usec) / 1000000.0; |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
168 lag_net /= 2.0; /* round trip -> one way */ |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
169 } else |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
170 lag_net = 0.0; /* no network lag */ |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
171 |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
172 /* |
8572 | 173 if (esd_svinfo) { |
174 mp_msg(MSGT_AO, MSGL_INFO, "AO: [esd] server info:\n"); | |
175 esd_print_server_info(esd_svinfo); | |
176 } | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
177 */ |
8572 | 178 } |
179 | |
180 esd_fmt = ESD_STREAM | ESD_PLAY; | |
181 | |
182 #if ESD_RESAMPLES | |
183 /* let the esd daemon convert sample rate */ | |
184 #else | |
185 /* let mplayer's audio filter convert the sample rate */ | |
186 if (esd_svinfo != NULL) | |
187 rate_hz = esd_svinfo->rate; | |
188 #endif | |
189 ao_data.samplerate = rate_hz; | |
190 | |
191 /* EsounD can play mono or stereo */ | |
192 switch (channels) { | |
193 case 1: | |
194 esd_fmt |= ESD_MONO; | |
195 ao_data.channels = bytes_per_sample = 1; | |
196 break; | |
197 default: | |
198 esd_fmt |= ESD_STEREO; | |
199 ao_data.channels = bytes_per_sample = 2; | |
200 break; | |
201 } | |
202 | |
203 /* EsounD can play 8bit unsigned and 16bit signed native */ | |
204 switch (format) { | |
205 case AFMT_S8: | |
206 case AFMT_U8: | |
207 esd_fmt |= ESD_BITS8; | |
208 ao_data.format = AFMT_U8; | |
209 break; | |
210 default: | |
211 esd_fmt |= ESD_BITS16; | |
212 ao_data.format = AFMT_S16_NE; | |
213 bytes_per_sample *= 2; | |
214 break; | |
215 } | |
216 | |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
217 /* modify audio_delay depending on esd_latency |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
218 * latency is number of samples @ 44.1khz stereo 16 bit |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
219 * adjust according to rate_hz & bytes_per_sample |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
220 */ |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
221 #ifdef HAVE_ESD_LATENCY |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
222 esd_latency = esd_get_latency(esd_fd); |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
223 #else |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
224 esd_latency = ((channels == 1 ? 2 : 1) * ESD_DEFAULT_RATE * |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
225 (ESD_BUF_SIZE + 64 * (4.0f / bytes_per_sample)) |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
226 ) / rate_hz; |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
227 esd_latency += ESD_BUF_SIZE * 2; |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
228 #endif |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
229 if(esd_latency > 0) { |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
230 lag_serv = (esd_latency * 4.0f) / (bytes_per_sample * rate_hz); |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
231 lag_seconds = lag_net + lag_serv; |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
232 audio_delay += lag_seconds; |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
233 mp_msg(MSGT_AO, MSGL_INFO,MSGTR_AO_ESD_LatencyInfo, |
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
234 lag_serv, lag_net, lag_seconds); |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
235 } |
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
236 |
8572 | 237 esd_play_fd = esd_play_stream_fallback(esd_fmt, rate_hz, |
10213
5e15ff3261ff
esd:server and esd latency support by Andrew Williams <andrew.s.williams@adelaide.edu.au>
alex
parents:
9633
diff
changeset
|
238 server, ESD_CLIENT_NAME); |
8572 | 239 if (esd_play_fd < 0) { |
13383
c1955840883d
mp_msg transition of unmaintained audio output drivers.
ivo
parents:
12145
diff
changeset
|
240 mp_msg(MSGT_AO, MSGL_ERR, MSGTR_AO_ESD_CantOpenPBStream, strerror(errno)); |
8572 | 241 return 0; |
242 } | |
243 | |
244 /* enable non-blocking i/o on the socket connection to the esd server */ | |
245 if ((fl = fcntl(esd_play_fd, F_GETFL)) >= 0) | |
246 fcntl(esd_play_fd, F_SETFL, O_NDELAY|fl); | |
247 | |
248 #if ESD_DEBUG | |
249 { | |
250 int sbuf, rbuf, len; | |
251 len = sizeof(sbuf); | |
252 getsockopt(esd_play_fd, SOL_SOCKET, SO_SNDBUF, &sbuf, &len); | |
253 len = sizeof(rbuf); | |
254 getsockopt(esd_play_fd, SOL_SOCKET, SO_RCVBUF, &rbuf, &len); | |
255 dprintf("esd: send/receive socket buffer space %d/%d bytes\n", | |
256 sbuf, rbuf); | |
257 } | |
258 #endif | |
259 | |
260 ao_data.bps = bytes_per_sample * rate_hz; | |
261 ao_data.outburst = ao_data.bps > 100000 ? 4*ESD_BUF_SIZE : 2*ESD_BUF_SIZE; | |
262 | |
263 esd_play_start.tv_sec = 0; | |
264 esd_samples_written = 0; | |
265 esd_bytes_per_sample = bytes_per_sample; | |
266 | |
267 return 1; | |
268 } | |
269 | |
270 | |
271 /* | |
272 * close audio device | |
273 */ | |
12145 | 274 static void uninit(int immed) |
8572 | 275 { |
276 if (esd_play_fd >= 0) { | |
277 esd_close(esd_play_fd); | |
278 esd_play_fd = -1; | |
279 } | |
280 | |
281 if (esd_svinfo) { | |
282 esd_free_server_info(esd_svinfo); | |
283 esd_svinfo = NULL; | |
284 } | |
285 | |
286 if (esd_fd >= 0) { | |
287 esd_close(esd_fd); | |
288 esd_fd = -1; | |
289 } | |
290 } | |
291 | |
292 | |
293 /* | |
294 * plays 'len' bytes of 'data' | |
295 * it should round it down to outburst*n | |
296 * return: number of bytes played | |
297 */ | |
298 static int play(void* data, int len, int flags) | |
299 { | |
300 int offs; | |
301 int nwritten; | |
302 int nsamples; | |
303 int remainder, n; | |
304 int saved_fl; | |
305 | |
306 /* round down buffersize to a multiple of ESD_BUF_SIZE bytes */ | |
307 len = len / ESD_BUF_SIZE * ESD_BUF_SIZE; | |
308 if (len <= 0) | |
309 return 0; | |
310 | |
311 #define SINGLE_WRITE 0 | |
312 #if SINGLE_WRITE | |
313 nwritten = write(esd_play_fd, data, len); | |
314 #else | |
11619
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
315 for (offs = 0, nwritten=0; offs + ESD_BUF_SIZE <= len; offs += ESD_BUF_SIZE) { |
8572 | 316 /* |
317 * note: we're writing to a non-blocking socket here. | |
318 * A partial write means, that the socket buffer is full. | |
319 */ | |
11619
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
320 n = write(esd_play_fd, (char*)data + offs, ESD_BUF_SIZE); |
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
321 if ( n < 0 ) { |
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
322 if ( errno != EAGAIN ) |
8572 | 323 dprintf("esd play: write failed: %s\n", strerror(errno)); |
324 break; | |
11619
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
325 } else if ( n != ESD_BUF_SIZE ) { |
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
326 nwritten += n; |
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
327 break; |
8572 | 328 } else |
329 nwritten += n; | |
330 } | |
11619
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
331 #endif |
179138947307
This patch contains bugfixes for the esd audio output driver that I
attila
parents:
10213
diff
changeset
|
332 |
8572 | 333 if (nwritten > 0) { |
334 if (!esd_play_start.tv_sec) | |
335 gettimeofday(&esd_play_start, NULL); | |
336 nsamples = nwritten / esd_bytes_per_sample; | |
337 esd_samples_written += nsamples; | |
338 | |
339 dprintf("esd play: %d %lu\n", nsamples, esd_samples_written); | |
340 } else { | |
341 dprintf("esd play: blocked / %lu\n", esd_samples_written); | |
342 } | |
343 | |
344 return nwritten; | |
345 } | |
346 | |
347 | |
348 /* | |
349 * stop playing, keep buffers (for pause) | |
350 */ | |
351 static void audio_pause() | |
352 { | |
353 /* | |
354 * not possible with esd. the esd daemom will continue playing | |
355 * buffered data (not more than ESD_MAX_DELAY seconds of samples) | |
356 */ | |
357 } | |
358 | |
359 | |
360 /* | |
361 * resume playing, after audio_pause() | |
362 */ | |
363 static void audio_resume() | |
364 { | |
365 /* | |
366 * not possible with esd. | |
367 * | |
368 * Let's hope the pause was long enough that the esd ran out of | |
369 * buffered data; we restart our time based delay computation | |
370 * for an audio resume. | |
371 */ | |
372 esd_play_start.tv_sec = 0; | |
373 esd_samples_written = 0; | |
374 } | |
375 | |
376 | |
377 /* | |
378 * stop playing and empty buffers (for seeking/pause) | |
379 */ | |
380 static void reset() | |
381 { | |
382 #ifdef __svr4__ | |
383 /* throw away data buffered in the esd connection */ | |
384 if (ioctl(esd_play_fd, I_FLUSH, FLUSHW)) | |
385 perror("I_FLUSH"); | |
386 #endif | |
387 } | |
388 | |
389 | |
390 /* | |
391 * return: how many bytes can be played without blocking | |
392 */ | |
393 static int get_space() | |
394 { | |
395 struct timeval tmout; | |
396 fd_set wfds; | |
397 float current_delay; | |
398 int space; | |
399 | |
400 /* | |
401 * Don't buffer too much data in the esd daemon. | |
402 * | |
403 * If we send too much, esd will block in write()s to the sound | |
404 * device, and the consequence is a huge slow down for things like | |
405 * esd_get_all_info(). | |
406 */ | |
407 if ((current_delay = get_delay()) >= ESD_MAX_DELAY) { | |
408 dprintf("esd get_space: too much data buffered\n"); | |
409 return 0; | |
410 } | |
411 | |
412 FD_ZERO(&wfds); | |
413 FD_SET(esd_play_fd, &wfds); | |
414 tmout.tv_sec = 0; | |
415 tmout.tv_usec = 0; | |
416 | |
417 if (select(esd_play_fd + 1, NULL, &wfds, NULL, &tmout) != 1) | |
418 return 0; | |
419 | |
420 if (!FD_ISSET(esd_play_fd, &wfds)) | |
421 return 0; | |
422 | |
423 /* try to fill 50% of the remaining "free" buffer space */ | |
424 space = (ESD_MAX_DELAY - current_delay) * ao_data.bps * 0.5f; | |
425 | |
426 /* round up to next multiple of ESD_BUF_SIZE */ | |
427 space = (space + ESD_BUF_SIZE-1) / ESD_BUF_SIZE * ESD_BUF_SIZE; | |
428 | |
429 dprintf("esd get_space: %d\n", space); | |
430 return space; | |
431 } | |
432 | |
433 | |
434 /* | |
435 * return: delay in seconds between first and last sample in buffer | |
436 */ | |
437 static float get_delay() | |
438 { | |
439 struct timeval now; | |
440 double buffered_samples_time; | |
441 double play_time; | |
442 | |
443 if (!esd_play_start.tv_sec) | |
444 return 0; | |
445 | |
446 buffered_samples_time = (float)esd_samples_written / ao_data.samplerate; | |
447 gettimeofday(&now, NULL); | |
448 play_time = now.tv_sec - esd_play_start.tv_sec; | |
449 play_time += (now.tv_usec - esd_play_start.tv_usec) / 1000000.; | |
450 | |
451 /* dprintf("esd delay: %f %f\n", play_time, buffered_samples_time); */ | |
452 | |
453 if (play_time > buffered_samples_time) { | |
454 dprintf("esd: underflow\n"); | |
455 esd_play_start.tv_sec = 0; | |
456 esd_samples_written = 0; | |
457 return 0; | |
458 } | |
459 | |
460 dprintf("esd: get_delay %f\n", buffered_samples_time - play_time); | |
461 return buffered_samples_time - play_time; | |
462 } |