--- play-ac3.c.orig Tue Sep 11 01:55:29 2001 +++ play-ac3.c Tue Sep 11 04:12:08 2001 @@ -31,6 +31,7 @@ #endif #define FRAMES 4 +#define CHUNK_SIZE 512 /* FRAMES * frame_size % CHUNK_SIZE == 0 */ void usage() { @@ -41,11 +42,14 @@ int check_ac3_stream(int in, int *frame_size) { - int i, skipped; - unsigned char buffer[4096]; + int i = 0, j, skipped; + unsigned char buffer[3072]; struct ac3info ai; - i = read(in, buffer, sizeof(buffer)); + while (i <= (sizeof(buffer) - CHUNK_SIZE)) { + j = read(in, buffer + i, CHUNK_SIZE); + i += j; + } if (i != sizeof(buffer)) { fprintf(stderr, "Error reading AC3 stream.\n"); return -1; @@ -65,8 +69,7 @@ i = read(in, buffer, ai.framesize-(sizeof(buffer)%ai.framesize)+skipped); } if (i < 0) { - fprintf(stderr, "Error seeking in AC3 stream.\n"); - return -1; + fprintf(stderr, "Error seeking in AC3 stream. Ignoring.\n"); } *frame_size = ai.framesize; @@ -76,7 +79,7 @@ int main(int argc, char *argv[]) { int fd, in; - int tmp, i, c = 1, bytes_read, data_type = 1, frame_size; + int tmp, i, c = 1, bytes_read, extra_bytes, data_type = 1, frame_size; unsigned char *ac3buf, *iecbuf; if (argc < 2) @@ -105,6 +108,7 @@ exit(1); break; } + fprintf(stderr, "frame size = %i", frame_size); fd = open("/dev/dsp", O_WRONLY); if (fd < 0) { fprintf(stderr, "Error opening DSP.\n"); @@ -117,7 +121,12 @@ } ac3buf = malloc(FRAMES * frame_size); iecbuf = malloc(FRAMES * 6144); - bytes_read = read(in, ac3buf, FRAMES * frame_size); + + bytes_read = read(in, ac3buf, CHUNK_SIZE); + while (bytes_read != FRAMES * frame_size) { + extra_bytes = read(in, ac3buf + bytes_read, CHUNK_SIZE); + bytes_read += extra_bytes; + } while (bytes_read == FRAMES * frame_size) { for (i = 0; i < FRAMES; i++) { ac3_iec958_build_burst(frame_size, data_type, 1, @@ -125,7 +134,11 @@ iecbuf + i * 6144); } write(fd, iecbuf, FRAMES * 6144); - bytes_read = read(in, ac3buf, FRAMES * frame_size); + bytes_read = read(in, ac3buf, CHUNK_SIZE); + while (bytes_read != FRAMES * frame_size) { + extra_bytes = read(in, ac3buf + bytes_read, CHUNK_SIZE); + bytes_read += extra_bytes; + } } return 0; }