comparison libao2/ao_esd.c @ 11619:179138947307

This patch contains bugfixes for the esd audio output driver that I uncovered while trying to send sound to a remote esd server over a wireless (11 mbs, just enough to handle to sound) link. First, the sound was full "ticking" sounds. I found a bug that prevented the "send the remainder of this block" code from ever being called - so large chunks of audio were simply being ignored. Fixing this bug removed the "ticking" from audio streams. Fixing this bug, however, uncovered another problem - when the socket buffer was full, doing a blocking write to finish the buffer would take far too long and would turn video into a chunky mess. I'd imagine this blocking write would be fine for an audio-only stream, but it turns out to hold up the video far too much. The solution in this patch is to write as much data as possible to the socket, and then return as soon as possible, reporting the number of bytes actually written accurately back to mplayer. I've tested it on both local and remote esd servers, and it works well. Patch by Benjamin Osheroff <ben@gimbo.net>
author attila
date Wed, 10 Dec 2003 12:19:13 +0000
parents 5e15ff3261ff
children 99798c3cdb93
comparison
equal deleted inserted replaced
11618:0d5cfe5358bc 11619:179138947307
313 313
314 #define SINGLE_WRITE 0 314 #define SINGLE_WRITE 0
315 #if SINGLE_WRITE 315 #if SINGLE_WRITE
316 nwritten = write(esd_play_fd, data, len); 316 nwritten = write(esd_play_fd, data, len);
317 #else 317 #else
318 for (offs = 0; offs + ESD_BUF_SIZE <= len; offs += ESD_BUF_SIZE) { 318 for (offs = 0, nwritten=0; offs + ESD_BUF_SIZE <= len; offs += ESD_BUF_SIZE) {
319 /* 319 /*
320 * note: we're writing to a non-blocking socket here. 320 * note: we're writing to a non-blocking socket here.
321 * A partial write means, that the socket buffer is full. 321 * A partial write means, that the socket buffer is full.
322 */ 322 */
323 nwritten = write(esd_play_fd, (char*)data + offs, ESD_BUF_SIZE); 323 n = write(esd_play_fd, (char*)data + offs, ESD_BUF_SIZE);
324 if (nwritten != ESD_BUF_SIZE) { 324 if ( n < 0 ) {
325 if (nwritten < 0 && errno != EAGAIN) { 325 if ( errno != EAGAIN )
326 dprintf("esd play: write failed: %s\n", strerror(errno)); 326 dprintf("esd play: write failed: %s\n", strerror(errno));
327 }
328 break; 327 break;
329 } 328 } else if ( n != ESD_BUF_SIZE ) {
330 } 329 nwritten += n;
331 nwritten = offs; 330 break;
332 #endif
333
334 if (nwritten > 0 && nwritten % ESD_BUF_SIZE != 0) {
335
336 /*
337 * partial write of an audio block of ESD_BUF_SIZE bytes.
338 *
339 * Send the remainder of that block as well; this avoids a busy
340 * polling loop in the esd daemon, which waits for the rest of
341 * the incomplete block using reads from a non-blocking
342 * socket. This busy polling loop wastes CPU cycles on the
343 * esd server machine, and we're trying to avoid that.
344 * (esd 0.2.28+ has the busy polling read loop, 0.2.22 inserts
345 * 0 samples which is bad as well)
346 *
347 * Let's hope the blocking write does not consume too much time.
348 *
349 * (fortunatelly, this piece of code is not used when playing
350 * sound on the local machine - on solaris at least)
351 */
352 remainder = ESD_BUF_SIZE - nwritten % ESD_BUF_SIZE;
353 dprintf("esd play: partial audio block written, remainder %d \n",
354 remainder);
355
356 /* blocking write of remaining bytes for the partial audio block */
357 saved_fl = fcntl(esd_play_fd, F_GETFL);
358 fcntl(esd_play_fd, F_SETFL, saved_fl & ~O_NDELAY);
359 n = write(esd_play_fd, (char *)data + nwritten, remainder);
360 fcntl(esd_play_fd, F_SETFL, saved_fl);
361
362 if (n != remainder) {
363 mp_msg(MSGT_AO, MSGL_ERR,
364 "AO: [esd] send remainer of audio block failed, %d/%d\n",
365 n, remainder);
366 } else 331 } else
367 nwritten += n; 332 nwritten += n;
368 } 333 }
369 334 #endif
335
370 if (nwritten > 0) { 336 if (nwritten > 0) {
371 if (!esd_play_start.tv_sec) 337 if (!esd_play_start.tv_sec)
372 gettimeofday(&esd_play_start, NULL); 338 gettimeofday(&esd_play_start, NULL);
373 nsamples = nwritten / esd_bytes_per_sample; 339 nsamples = nwritten / esd_bytes_per_sample;
374 esd_samples_written += nsamples; 340 esd_samples_written += nsamples;