Subject: Re: /dev/sound, non-blocking IO, and select
To: Alan Post <apost@recalcitrant.org>
From: Jared D. McNeill <jmcneill@invisible.ca>
List: tech-kern
Date: 04/07/2005 09:30:23
On Thu, 2005-04-07 at 02:44 +0000, Alan Post wrote:
> In article <1112834454.538.3.camel@localhost>, Jared D. McNeill wrote:
> > 
> > The problem: select on the /dev/sound fd is never saying that data
> > is available. Why is this the case? Isn't there always sound
> > available to read?
> 
> On my system, I have to do a read to get the audio to start, before
> calling select the first time.

The read is never returning for me (even if I turn off O_NONBLOCK).
Something strange is going on here.

I can 'dd if=/dev/sound1 of=/dev/null ...' and 'cat /dev/sound1' without
a problem, but reads in this application are hanging.

Here's basically what I'm doing for setup:
  dev->fd = open(dev_name, O_RDWR, O_NONBLOCK);
  [...]
  if (ioctl(dev->fd, AUDIO_GETINFO, &oinfo) < 0) {
    [...]
  }
  AUDIO_INITINFO(&dev->info);
  dev->info.play.sample_rate = dev->info.record.sample_rate = 8000;
  dev->info.play.channels = dev->info.record.channels = 1;
  dev->info.play.precision = dev->info.record.precision = 16;
  dev->info.play.encoding = dev->info.record.encoding =
AUDIO_ENCODING_SLINEAR_LE;
  dev->info.play.gain = dev->info.record.gain = oinfo.play.gain;
  dev->info.play.port = dev->info.record.port = oinfo.play.port;
  dev->info.play.balance = dev->info.record.balance =
oinfo.play.balance;
  dev->info.monitor_gain = oinfo.monitor_gain;
  dev->info.mode = AUMODE_PLAY | AUMODE_RECORD;
  if (ioctl(dev->fd, AUDIO_SETINFO, &dev->info) < 0) {
    [...]
  }

I then try to read from the device (before the select, as you
suggested):
  while (len > 0)
    x = read(dev->fd, buf, len);
    if (x == -1 && errno == EAGAIN) {
      [...]
      continue;
    }
    if (x != -1) {
      buf += x;
      len -= x;
    }
  }

The read call never returns, even with len=1.

Cheers,
Jared