[alsa-devel] Choppy audio with non-blocking writes
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
Hello,
I'm using non-blocking writes to play an audio stream. When snd_pcm_writei() return -EGAIN I put the thread to sleep for the time it would take to play the packet that I'm trying to write and the packet size it's capped at 1/4 the ALSA buffer so that should ensure that the buffer never gets more than half empty. And it works as I don't get underruns. But sometimes the audio gets choppy and it sounds like it's playing out of time. Like periodically for a fraction of a second it plays a bit of audio that should've been played about a second ago.
Triggering an underrun or a call to snd_pcm_drain(), either by pausing the stream or overloading the CPU corrects the problem so I think the internal ALSA ring buffer pointers are getting out of sync somehow. Also it only happens with non-blocking writes. If I use blocking writes it works fine but there are other problems with that. So I think either I'm handling -EAGAIN incorrectly, or not configuring it properly?
You can look at the code here: https://bitbucket.org/frodzdev/mediabox/src/84dced5b84eba4e6e639615dd3d30fcc...
Any help will be appreciated.
- --
Fernando Rodriguez
participants (1)
-
Fernando Rodriguez