From 83aa99e7a9a1ff0d6949b4c71cd8817e2668b341 Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Sat, 2 Sep 2017 21:34:02 +0700 Subject: [PATCH] pq_alsa.c: handle output buffer underrun On some systems the ALSA output buffer is pretty big, and if the audio samples are not being passed into the buffer quickly enough, it becomes starved for data, resulting in an error called underrun. Previously, when it happenned, GAPK used to stop processing with the following message (where X is a random number): [+] PQ: Adding ALSA output (dev='default', blk_len=320) [!] pq_execute(): abort, item returned -1 [+] Processed X frames According to the ALSA documentation, the pcm_handle changes its state when the problem happens, and should be recovered using the snd_pcm_prepare() call. This change actually does that. --- src/pq_alsa.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/pq_alsa.c b/src/pq_alsa.c index 9cee426..a3435dd 100644 --- a/src/pq_alsa.c +++ b/src/pq_alsa.c @@ -57,7 +57,15 @@ pq_cb_alsa_output(void *_state, uint8_t *out, const uint8_t *in, unsigned int in struct pq_state_alsa *state = _state; unsigned int num_samples = in_len/2; int rv; + rv = snd_pcm_writei(state->pcm_handle, in, num_samples); + if (rv == -EPIPE) { + /* Recover from buffer underrun */ + snd_pcm_prepare(state->pcm_handle); + /* Send a new sample again */ + rv = snd_pcm_writei(state->pcm_handle, in, num_samples); + } + return rv == num_samples ? 0 : -1; }