From be35600571ffd42992576006f158d97dedd14338 Mon Sep 17 00:00:00 2001 From: Trek H Date: Thu, 27 Jan 2022 11:50:45 +1030 Subject: [PATCH] alsa: check and handle common errors with pool buffer --- device/alsa/alsa.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 90002589..62df0d5a 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -30,6 +30,7 @@ import ( "bytes" "errors" "fmt" + "io" "sync" "time" @@ -413,13 +414,39 @@ func (d *ALSA) input() { // Read reads from the ringbuffer, returning the number of bytes read upon success. func (d *ALSA) Read(p []byte) (int, error) { // Ready ringbuffer for read. - _, err := d.buf.Next(rbNextTimeout) + d.l.Log(logger.Debug, pkg+"getting next chunk ready") + chunk, err := d.buf.Next(rbNextTimeout) if err != nil { - return 0, err + switch err { + case nil, io.EOF: + d.l.Log(logger.Debug, pkg+"EOF") + return 0, nil + case pool.ErrTimeout: + d.l.Log(logger.Debug, pkg+"pool buffer timeout") + return 0, nil + default: + d.l.Log(logger.Error, pkg+"unexpected error from Next", "error", err.Error()) + return 0, nil + } } // Read from pool buffer. - return d.buf.Read(p) + d.l.Log(logger.Debug, pkg+"getting next chunk ready") + n, err := d.buf.Read(p) + if err != nil { + switch err { + case nil, io.EOF: + d.l.Log(logger.Debug, pkg+"EOF") + chunk.Close() + return 0, nil + default: + d.l.Log(logger.Error, pkg+"unexpected error from Read", "error", err.Error()) + chunk.Close() + return 0, nil + } + } + chunk.Close() + return n, nil } // formatBuffer returns audio that has been converted to the desired format.