user6369958
user6369958

Reputation: 367

ALSA: snd_pcm_writei returns EAGAIN

I'm struggling to understand what I'm doing wrong in my audio playback routine. I've a thread that takes buffers from other threads and plays them the same way this alsa example program does: https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_8c-example.html I'm referring to the write_loop() function. This is the device configuration setup up of the pcm.c example program (output of snd_pcm_dump()):

ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 1
  rate         : 44100
  exact rate   : 44100 (44100/1)
  msbits       : 16
  buffer_size  : 22050
  period_size  : 4410
  period_time  : 100000
  tstamp_mode  : NONE
  tstamp_type  : GETTIMEOFDAY
  period_step  : 1
  avail_min    : 4410
  period_event : 0
  start_threshold  : 22050
  stop_threshold   : 22050
  silence_threshold: 0
  silence_size : 0
  boundary     : 6206523236469964800

What I see placing some printf() around snd_pcm_writei() is that it gets executed 5 times straight and every next loop snd_pcm_writei() takes 100ms to complete. This is exactly what I was expecting to see.

This is device setup of my program:

ALSA <-> PulseAudio PCM I/O Plugin
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : FLOAT_LE
  subformat    : STD
  channels     : 1
  rate         : 44100
  exact rate   : 44100 (44100/1)
  msbits       : 32
  buffer_size  : 13230
  period_size  : 4410
  period_time  : 100000
  tstamp_mode  : NONE
  tstamp_type  : GETTIMEOFDAY
  period_step  : 1
  avail_min    : 4410
  period_event : 0
  start_threshold  : 4410
  stop_threshold   : 13230
  silence_threshold: 0
  silence_size : 0
  boundary     : 7447827883763957760

What happens is snd_pcm_writei() runs 5 times (and this is ok) but after that every new loop it returns immediately with -EAGAIN. Retrying continuously for 100ms (100% cpu usage) to play the same buffer eventually it gets played, snd_pcm_writei() returns a positive number and for next audio buffer I get immediately -EAGAIN, for 100ms; and so on. The audio playback, however, is fine.

What I don't understand is why it doesn't wait 100ms to play the new buffer instead of returning immediately -EAGAIN (cannot find anything in ALSA docs about snd_pcm_writei() returning -EAGAIN).

Thanks in advance for any help!

Upvotes: 1

Views: 1656

Answers (1)

CL.
CL.

Reputation: 180060

A PCM device can be in blocking mode (waiting) or in non-blocking mode (returning -EAGAIN).

This mode can be set with a flag when calling snd_pcm_open(), or with snd_pcm_nonblock().

Upvotes: 3

Related Questions