diff --git a/device/alsa/alsa.go b/device/alsa/alsa.go index 90002589..73a56a83 100644 --- a/device/alsa/alsa.go +++ b/device/alsa/alsa.go @@ -30,6 +30,7 @@ import ( "bytes" "errors" "fmt" + "io" "sync" "time" @@ -405,7 +406,6 @@ func (d *ALSA) input() { d.l.Log(logger.Warning, "old audio data overwritten") default: d.l.Log(logger.Error, "unexpected ringbuffer error", "error", err.Error()) - return } } } @@ -413,13 +413,37 @@ 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. + d.l.Log(logger.Debug, pkg+"getting next chunk ready") _, 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+"reading from buffer") + n, err := d.buf.Read(p) + if err != nil { + switch err { + case nil, io.EOF: + d.l.Log(logger.Debug, pkg+"EOF from Read, stopping") + return n, err + default: + d.l.Log(logger.Error, pkg+"unexpected error from Read", "error", err.Error()) + return n, nil + } + } + d.l.Log(logger.Debug, fmt.Sprintf("%v read %v bytes", pkg, n)) + return n, nil } // formatBuffer returns audio that has been converted to the desired format.