Subject: Re: Nonblocking audio I/O
To: None <gson@araneus.fi>
From: Lennart Augustsson <augustss@cs.chalmers.se>
List: tech-kern
Date: 12/28/1998 00:21:16
> The way NetBSD's /dev/audio currently works is that if you simply open
> the device and then immediately select() on it for reading and/or
> writing, nothing will happen.  No playback or recording will start,
> and select() will never return.
Sorry, but I have to half disagree with you.  One of the programs in
my audio test suite sets the audio device in non-blocking mode
and then selects, it works fine.
Reading, on the other hand, does not start until you do a read
in blocking mode.  I'll change that, it looks like a bug.

   -- Lennart

Here's my test program:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>

char buf[200000];

int 
main(int argc, char **argv)
{
        int fd, fl, n, w, k;
	fd_set wfds;
	char *p;

        if ((fd = open(argv[1], O_WRONLY, 0)) == -1) {
                perror("open");
                exit(1);
        }
        if ((fl = fcntl(fd, F_GETFL, 0)) == -1) {
                perror("fcntl F_GETFL");
                exit(1);
        }
        if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
                perror("fcntl F_SETFL");
                exit(1);
        }
	n = read(0, buf, sizeof buf);
	p = buf;
	while (n > 0) {
		FD_ZERO(&wfds);
		FD_SET(fd, &wfds);
		k = select(fd+1, 0, &wfds, 0, 0);
		if (k <= 0) {
			printf("bad select k=%d, errno=%d\n", k, errno);
			exit(1);
		}
		printf("select returns... ");
		w = write(fd, p, n);
		printf("wrote %d\n", w);
		if (w > 0) {
			n -= w;
			p += w;
		}
	}
        exit(0);
}