[1/2] pq_alsa.c: handle output buffer underrun

Message ID 20170902143403.14185-1-axilirator@gmail.com
State New
Headers show
Series
  • [1/2] pq_alsa.c: handle output buffer underrun
Related show

Commit Message

Vadim Yanitskiy Sept. 2, 2017, 2:34 p.m.
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(+)

Comments

Harald Welte Sept. 2, 2017, 3:04 p.m. | #1
Thanks,

both applied.

Patch

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;
 }