Mercurial > mplayer.hg
annotate libao2/ao_esd.c @ 9233:ba7041fbcfda
ffmpeg's DV audio decoder added
author | arpi |
---|---|
date | Mon, 03 Feb 2003 00:20:46 +0000 |
parents | 440301fef3fe |
children | 12b1790038b0 |
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" | |
40 | |
41 | |
42 #undef ESD_DEBUG | |
43 | |
44 #if ESD_DEBUG | |
45 #define dprintf(...) printf(__VA_ARGS__) | |
46 #else | |
47 #define dprintf(...) /**/ | |
48 #endif | |
49 | |
50 | |
51 #define ESD_CLIENT_NAME "MPlayer" | |
52 #define ESD_MAX_DELAY (1.0f) /* max amount of data buffered in esd (#sec) */ | |
53 | |
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; | |
72 | |
73 | |
74 /* | |
75 * to set/get/query special features/parameters | |
76 */ | |
77 static int control(int cmd, int arg) | |
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; | |
149 | |
150 if (esd_fd < 0) { | |
151 esd_fd = esd_open_sound(NULL); | |
152 if (esd_fd < 0) { | |
153 mp_msg(MSGT_AO, MSGL_ERR, "AO: [esd] esd_open_sound failed: %s\n", | |
154 strerror(errno)); | |
155 return 0; | |
156 } | |
157 | |
158 esd_svinfo = esd_get_server_info(esd_fd); | |
159 /* | |
160 if (esd_svinfo) { | |
161 mp_msg(MSGT_AO, MSGL_INFO, "AO: [esd] server info:\n"); | |
162 esd_print_server_info(esd_svinfo); | |
163 } | |
164 */ | |
165 | |
166 esd_latency = esd_get_latency(esd_fd); | |
167 /* mp_msg(MSGT_AO, MSGL_INFO, "AO: [esd] latency: %d\n", esd_latency); */ | |
168 } | |
169 | |
170 esd_fmt = ESD_STREAM | ESD_PLAY; | |
171 | |
172 #if ESD_RESAMPLES | |
173 /* let the esd daemon convert sample rate */ | |
174 #else | |
175 /* let mplayer's audio filter convert the sample rate */ | |
176 if (esd_svinfo != NULL) | |
177 rate_hz = esd_svinfo->rate; | |
178 #endif | |
179 ao_data.samplerate = rate_hz; | |
180 | |
181 | |
182 /* EsounD can play mono or stereo */ | |
183 switch (channels) { | |
184 case 1: | |
185 esd_fmt |= ESD_MONO; | |
186 ao_data.channels = bytes_per_sample = 1; | |
187 break; | |
188 default: | |
189 esd_fmt |= ESD_STEREO; | |
190 ao_data.channels = bytes_per_sample = 2; | |
191 break; | |
192 } | |
193 | |
194 /* EsounD can play 8bit unsigned and 16bit signed native */ | |
195 switch (format) { | |
196 case AFMT_S8: | |
197 case AFMT_U8: | |
198 esd_fmt |= ESD_BITS8; | |
199 ao_data.format = AFMT_U8; | |
200 break; | |
201 default: | |
202 esd_fmt |= ESD_BITS16; | |
203 ao_data.format = AFMT_S16_NE; | |
204 bytes_per_sample *= 2; | |
205 break; | |
206 } | |
207 | |
208 esd_play_fd = esd_play_stream_fallback(esd_fmt, rate_hz, | |
209 NULL, ESD_CLIENT_NAME); | |
210 if (esd_play_fd < 0) { | |
211 mp_msg(MSGT_AO, MSGL_ERR, | |
212 "AO: [esd] failed to open esd playback stream: %s\n", | |
213 strerror(errno)); | |
214 return 0; | |
215 } | |
216 | |
217 /* enable non-blocking i/o on the socket connection to the esd server */ | |
218 if ((fl = fcntl(esd_play_fd, F_GETFL)) >= 0) | |
219 fcntl(esd_play_fd, F_SETFL, O_NDELAY|fl); | |
220 | |
221 #if ESD_DEBUG | |
222 { | |
223 int sbuf, rbuf, len; | |
224 len = sizeof(sbuf); | |
225 getsockopt(esd_play_fd, SOL_SOCKET, SO_SNDBUF, &sbuf, &len); | |
226 len = sizeof(rbuf); | |
227 getsockopt(esd_play_fd, SOL_SOCKET, SO_RCVBUF, &rbuf, &len); | |
228 dprintf("esd: send/receive socket buffer space %d/%d bytes\n", | |
229 sbuf, rbuf); | |
230 } | |
231 #endif | |
232 | |
233 ao_data.bps = bytes_per_sample * rate_hz; | |
234 ao_data.outburst = ao_data.bps > 100000 ? 4*ESD_BUF_SIZE : 2*ESD_BUF_SIZE; | |
235 | |
236 esd_play_start.tv_sec = 0; | |
237 esd_samples_written = 0; | |
238 esd_bytes_per_sample = bytes_per_sample; | |
239 | |
240 return 1; | |
241 } | |
242 | |
243 | |
244 /* | |
245 * close audio device | |
246 */ | |
247 static void uninit() | |
248 { | |
249 if (esd_play_fd >= 0) { | |
250 esd_close(esd_play_fd); | |
251 esd_play_fd = -1; | |
252 } | |
253 | |
254 if (esd_svinfo) { | |
255 esd_free_server_info(esd_svinfo); | |
256 esd_svinfo = NULL; | |
257 } | |
258 | |
259 if (esd_fd >= 0) { | |
260 esd_close(esd_fd); | |
261 esd_fd = -1; | |
262 } | |
263 } | |
264 | |
265 | |
266 /* | |
267 * plays 'len' bytes of 'data' | |
268 * it should round it down to outburst*n | |
269 * return: number of bytes played | |
270 */ | |
271 static int play(void* data, int len, int flags) | |
272 { | |
273 int offs; | |
274 int nwritten; | |
275 int nsamples; | |
276 int remainder, n; | |
277 int saved_fl; | |
278 | |
279 /* round down buffersize to a multiple of ESD_BUF_SIZE bytes */ | |
280 len = len / ESD_BUF_SIZE * ESD_BUF_SIZE; | |
281 if (len <= 0) | |
282 return 0; | |
283 | |
284 #define SINGLE_WRITE 0 | |
285 #if SINGLE_WRITE | |
286 nwritten = write(esd_play_fd, data, len); | |
287 #else | |
288 for (offs = 0; offs + ESD_BUF_SIZE <= len; offs += ESD_BUF_SIZE) { | |
289 /* | |
290 * note: we're writing to a non-blocking socket here. | |
291 * A partial write means, that the socket buffer is full. | |
292 */ | |
293 nwritten = write(esd_play_fd, (char*)data + offs, ESD_BUF_SIZE); | |
294 if (nwritten != ESD_BUF_SIZE) { | |
295 if (nwritten < 0 && errno != EAGAIN) { | |
296 dprintf("esd play: write failed: %s\n", strerror(errno)); | |
297 } | |
298 break; | |
299 } | |
300 } | |
301 nwritten = offs; | |
302 #endif | |
303 | |
304 if (nwritten > 0 && nwritten % ESD_BUF_SIZE != 0) { | |
305 | |
306 /* | |
307 * partial write of an audio block of ESD_BUF_SIZE bytes. | |
308 * | |
309 * Send the remainder of that block as well; this avoids a busy | |
310 * polling loop in the esd daemon, which waits for the rest of | |
311 * the incomplete block using reads from a non-blocking | |
312 * socket. This busy polling loop wastes CPU cycles on the | |
313 * esd server machine, and we're trying to avoid that. | |
314 * (esd 0.2.28+ has the busy polling read loop, 0.2.22 inserts | |
315 * 0 samples which is bad as well) | |
316 * | |
317 * Let's hope the blocking write does not consume too much time. | |
318 * | |
319 * (fortunatelly, this piece of code is not used when playing | |
320 * sound on the local machine - on solaris at least) | |
321 */ | |
322 remainder = ESD_BUF_SIZE - nwritten % ESD_BUF_SIZE; | |
323 dprintf("esd play: partial audio block written, remainder %d \n", | |
324 remainder); | |
325 | |
326 /* blocking write of remaining bytes for the partial audio block */ | |
327 saved_fl = fcntl(esd_play_fd, F_GETFL); | |
328 fcntl(esd_play_fd, F_SETFL, saved_fl & ~O_NDELAY); | |
329 n = write(esd_play_fd, (char *)data + nwritten, remainder); | |
330 fcntl(esd_play_fd, F_SETFL, saved_fl); | |
331 | |
332 if (n != remainder) { | |
333 mp_msg(MSGT_AO, MSGL_ERR, | |
334 "AO: [esd] send remainer of audio block failed, %d/%d\n", | |
335 n, remainder); | |
336 } else | |
337 nwritten += n; | |
338 } | |
339 | |
340 if (nwritten > 0) { | |
341 if (!esd_play_start.tv_sec) | |
342 gettimeofday(&esd_play_start, NULL); | |
343 nsamples = nwritten / esd_bytes_per_sample; | |
344 esd_samples_written += nsamples; | |
345 | |
346 dprintf("esd play: %d %lu\n", nsamples, esd_samples_written); | |
347 } else { | |
348 dprintf("esd play: blocked / %lu\n", esd_samples_written); | |
349 } | |
350 | |
351 return nwritten; | |
352 } | |
353 | |
354 | |
355 /* | |
356 * stop playing, keep buffers (for pause) | |
357 */ | |
358 static void audio_pause() | |
359 { | |
360 /* | |
361 * not possible with esd. the esd daemom will continue playing | |
362 * buffered data (not more than ESD_MAX_DELAY seconds of samples) | |
363 */ | |
364 } | |
365 | |
366 | |
367 /* | |
368 * resume playing, after audio_pause() | |
369 */ | |
370 static void audio_resume() | |
371 { | |
372 /* | |
373 * not possible with esd. | |
374 * | |
375 * Let's hope the pause was long enough that the esd ran out of | |
376 * buffered data; we restart our time based delay computation | |
377 * for an audio resume. | |
378 */ | |
379 esd_play_start.tv_sec = 0; | |
380 esd_samples_written = 0; | |
381 } | |
382 | |
383 | |
384 /* | |
385 * stop playing and empty buffers (for seeking/pause) | |
386 */ | |
387 static void reset() | |
388 { | |
389 #ifdef __svr4__ | |
390 /* throw away data buffered in the esd connection */ | |
391 if (ioctl(esd_play_fd, I_FLUSH, FLUSHW)) | |
392 perror("I_FLUSH"); | |
393 #endif | |
394 } | |
395 | |
396 | |
397 /* | |
398 * return: how many bytes can be played without blocking | |
399 */ | |
400 static int get_space() | |
401 { | |
402 struct timeval tmout; | |
403 fd_set wfds; | |
404 float current_delay; | |
405 int space; | |
406 | |
407 /* | |
408 * Don't buffer too much data in the esd daemon. | |
409 * | |
410 * If we send too much, esd will block in write()s to the sound | |
411 * device, and the consequence is a huge slow down for things like | |
412 * esd_get_all_info(). | |
413 */ | |
414 if ((current_delay = get_delay()) >= ESD_MAX_DELAY) { | |
415 dprintf("esd get_space: too much data buffered\n"); | |
416 return 0; | |
417 } | |
418 | |
419 FD_ZERO(&wfds); | |
420 FD_SET(esd_play_fd, &wfds); | |
421 tmout.tv_sec = 0; | |
422 tmout.tv_usec = 0; | |
423 | |
424 if (select(esd_play_fd + 1, NULL, &wfds, NULL, &tmout) != 1) | |
425 return 0; | |
426 | |
427 if (!FD_ISSET(esd_play_fd, &wfds)) | |
428 return 0; | |
429 | |
430 /* try to fill 50% of the remaining "free" buffer space */ | |
431 space = (ESD_MAX_DELAY - current_delay) * ao_data.bps * 0.5f; | |
432 | |
433 /* round up to next multiple of ESD_BUF_SIZE */ | |
434 space = (space + ESD_BUF_SIZE-1) / ESD_BUF_SIZE * ESD_BUF_SIZE; | |
435 | |
436 dprintf("esd get_space: %d\n", space); | |
437 return space; | |
438 } | |
439 | |
440 | |
441 /* | |
442 * return: delay in seconds between first and last sample in buffer | |
443 */ | |
444 static float get_delay() | |
445 { | |
446 struct timeval now; | |
447 double buffered_samples_time; | |
448 double play_time; | |
449 | |
450 if (!esd_play_start.tv_sec) | |
451 return 0; | |
452 | |
453 buffered_samples_time = (float)esd_samples_written / ao_data.samplerate; | |
454 gettimeofday(&now, NULL); | |
455 play_time = now.tv_sec - esd_play_start.tv_sec; | |
456 play_time += (now.tv_usec - esd_play_start.tv_usec) / 1000000.; | |
457 | |
458 /* dprintf("esd delay: %f %f\n", play_time, buffered_samples_time); */ | |
459 | |
460 if (play_time > buffered_samples_time) { | |
461 dprintf("esd: underflow\n"); | |
462 esd_play_start.tv_sec = 0; | |
463 esd_samples_written = 0; | |
464 return 0; | |
465 } | |
466 | |
467 dprintf("esd: get_delay %f\n", buffered_samples_time - play_time); | |
468 return buffered_samples_time - play_time; | |
469 } |