audio/internal/go2cpp: Simplify reading the source

This commit is contained in:
Hajime Hoshi 2021-05-10 04:00:58 +09:00
parent 9585c1e09a
commit 9b965a6308

View File

@ -213,7 +213,10 @@ func (p *Player) Close() error {
func (p *Player) close(remove bool) error { func (p *Player) close(remove bool) error {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
return p.closeImpl(remove)
}
func (p *Player) closeImpl(remove bool) error {
if p.state == playerStateClosed { if p.state == playerStateClosed {
return p.err return p.err
} }
@ -271,12 +274,6 @@ func (p *Player) waitUntilUnpaused() bool {
return p.v.Truthy() && p.state == playerStatePlaying return p.v.Truthy() && p.state == playerStatePlaying
} }
func (p *Player) write(dst js.Value, src []byte) {
p.cond.L.Lock()
defer p.cond.L.Unlock()
p.writeImpl(dst, src)
}
func (p *Player) writeImpl(dst js.Value, src []byte) { func (p *Player) writeImpl(dst js.Value, src []byte) {
if p.state == playerStateClosed { if p.state == playerStateClosed {
return return
@ -300,22 +297,22 @@ func (p *Player) loop() {
return return
} }
n := readChunkSize p.cond.L.Lock()
if max := p.context.MaxBufferSize() - p.UnplayedBufferSize(); n > max { n, err := p.src.Read(buf)
n = max
}
n2, err := p.src.Read(buf[:n])
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
p.setError(err) p.setErrorImpl(err)
p.cond.L.Unlock()
return return
} }
if n > 0 { if n > 0 {
p.write(dst, buf[:n2]) p.writeImpl(dst, buf[:n])
} }
if err == io.EOF { if err == io.EOF {
p.close(false) p.closeImpl(false)
p.cond.L.Unlock()
return return
} }
p.cond.L.Unlock()
} }
} }