audio/internal/readdriver: Bug fix: Implement Suspend/Resume for browsers

Closes #1630
This commit is contained in:
Hajime Hoshi 2021-05-04 18:49:34 +09:00
parent 44e96f2377
commit 29eade9b4a
4 changed files with 38 additions and 0 deletions

View File

@ -127,9 +127,15 @@ func NewContext(sampleRate int) *Context {
h := getHook()
h.OnSuspendAudio(func() {
c.semaphore <- struct{}{}
if s, ok := np.(interface{ suspend() }); ok {
s.suspend()
}
})
h.OnResumeAudio(func() {
<-c.semaphore
if s, ok := np.(interface{ resume() }); ok {
s.resume()
}
})
h.AppendHookOnBeforeUpdate(func() error {

View File

@ -21,6 +21,8 @@ import (
type Context interface {
NewPlayer(io.Reader) Player
MaxBufferSize() int
Suspend()
Resume()
io.Closer
}

View File

@ -139,6 +139,14 @@ func (c *context) MaxBufferSize() int {
return c.oneBufferSize() * 2
}
func (c *context) Suspend() {
c.audioContext.Call("suspend")
}
func (c *context) Resume() {
c.audioContext.Call("resume")
}
func (p *player) Pause() {
if p.state != playerPlay {
return
@ -299,6 +307,14 @@ func (w *go2cppDriverWrapper) MaxBufferSize() int {
return w.c.MaxBufferSize()
}
func (w *go2cppDriverWrapper) Suspend() {
// Do nothing so far.
}
func (w *go2cppDriverWrapper) Resume() {
// Do nothing so far.
}
func (w *go2cppDriverWrapper) Close() error {
return w.c.Close()
}

View File

@ -60,6 +60,20 @@ func (f *readerPlayerFactory) newPlayerImpl(context *Context, src io.Reader) (pl
return p, nil
}
func (f *readerPlayerFactory) suspend() {
if f.context == nil {
return
}
f.context.Suspend()
}
func (f *readerPlayerFactory) resume() {
if f.context == nil {
return
}
f.context.Resume()
}
func (p *readerPlayer) ensurePlayer() error {
// Initialize the underlying player lazily to enable calling NewContext in an 'init' function.
// Accessing the underlying player functions requires the environment to be already initialized,