audio: Bug fix: Race condition on accessing the set of players

Closes #1548
This commit is contained in:
Hajime Hoshi 2021-03-24 23:41:14 +09:00
parent 922b1f163a
commit 7212cd7999

View File

@ -148,19 +148,7 @@ func NewContext(sampleRate int) *Context {
return err
}
// Now reader players cannot call removePlayers from themselves in the current implementation.
// Underlying playering can be the pause state after fishing its playing,
// but there is no way to notify this to readerPlayers so far.
// Instead, let's check the states proactively every frame.
for p := range c.players {
rp, ok := p.(*readerPlayer)
if !ok {
break
}
if !rp.IsPlaying() {
delete(c.players, p)
}
}
c.gcPlayers()
return nil
})
@ -217,6 +205,25 @@ func (c *Context) removePlayer(p playerImpl) {
c.m.Unlock()
}
func (c *Context) gcPlayers() {
c.m.Lock()
defer c.m.Unlock()
// Now reader players cannot call removePlayers from themselves in the current implementation.
// Underlying playering can be the pause state after fishing its playing,
// but there is no way to notify this to readerPlayers so far.
// Instead, let's check the states proactively every frame.
for p := range c.players {
rp, ok := p.(*readerPlayer)
if !ok {
return
}
if !rp.IsPlaying() {
delete(c.players, p)
}
}
}
// IsReady returns a boolean value indicating whether the audio is ready or not.
//
// On some browsers, user interaction like click or pressing keys is required to start audio.