Merged in alsa-pool (pull request #471)

alsa: handle pool buffer error and properly close chunks

Approved-by: Saxon Milton
This commit is contained in:
Trek Hopton 2022-02-09 06:27:46 +00:00
commit 54edadbe13
1 changed files with 27 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"sync" "sync"
"time" "time"
@ -405,7 +406,6 @@ func (d *ALSA) input() {
d.l.Log(logger.Warning, "old audio data overwritten") d.l.Log(logger.Warning, "old audio data overwritten")
default: default:
d.l.Log(logger.Error, "unexpected ringbuffer error", "error", err.Error()) 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. // 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.
d.l.Log(logger.Debug, pkg+"getting next chunk ready")
_, err := d.buf.Next(rbNextTimeout) _, 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+"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. // formatBuffer returns audio that has been converted to the desired format.