audio/internal/go2cpp: Bug fix: Writing data must be concurrent-safe

This commit is contained in:
Hajime Hoshi 2021-01-13 02:06:40 +09:00
parent 78789a511c
commit aeaa443bf2

View File

@ -186,6 +186,21 @@ 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()
if p.state == playerStateClosed {
return
}
if !p.v.Truthy() {
return
}
js.CopyBytesToJS(dst, src)
p.v.Call("write", dst, len(src))
}
func (p *Player) loop() { func (p *Player) loop() {
const size = 4096 const size = 4096
@ -203,8 +218,7 @@ func (p *Player) loop() {
return return
} }
if n > 0 { if n > 0 {
js.CopyBytesToJS(dst, buf[:n]) p.write(dst, buf[:n])
p.v.Call("write", dst, n)
} }
if err == io.EOF { if err == io.EOF {