alsa: check and handle common errors with pool buffer

This commit is contained in:
Trek H 2022-01-27 11:50:45 +10:30
parent 8bfebec9df
commit be35600571
1 changed files with 30 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"sync" "sync"
"time" "time"
@ -413,13 +414,39 @@ func (d *ALSA) input() {
// Read reads from the ringbuffer, returning the number of bytes read upon success. // Read reads from the ringbuffer, returning the number of bytes read upon success.
func (d *ALSA) Read(p []byte) (int, error) { func (d *ALSA) Read(p []byte) (int, error) {
// Ready ringbuffer for read. // 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 { 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. // 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. // formatBuffer returns audio that has been converted to the desired format.