Skip to content

Commit

Permalink
audio: Bug fix: Fill empty data even when audio is suspended
Browse files Browse the repository at this point in the history
When writing is stopped on Oto's players, unexpected delaying
happens.

The ideal solution is to have APIs to suspend and resume Oto's
player, but this is not easy. For a temporary solution, write
zero values on the players when audio is suspended.

Fixes #975
  • Loading branch information
hajimehoshi committed Nov 6, 2019
1 parent ddba7f0 commit 529dddd
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions audio/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,20 @@ type Context struct {
sampleRate int
err error
ready bool
suspended bool

players map[*playerImpl]struct{}

m sync.Mutex
semaphore chan struct{}
m sync.Mutex
}

var (
theContext *Context
theContextLock sync.Mutex
)

var emptyBytes = make([]byte, 256)

// NewContext creates a new audio context with the given sample rate.
//
// The sample rate is also used for decoding MP3 with audio/mp3 package
Expand All @@ -99,16 +101,19 @@ func NewContext(sampleRate int) (*Context, error) {
c: newContext(sampleRate),
players: map[*playerImpl]struct{}{},
inited: make(chan struct{}),
semaphore: make(chan struct{}, 1),
}
theContext = c

h := getHook()
h.OnSuspendAudio(func() {
c.semaphore <- struct{}{}
c.m.Lock()
c.suspended = true
c.m.Unlock()
})
h.OnResumeAudio(func() {
<-c.semaphore
c.m.Lock()
c.suspended = false
c.m.Unlock()
})

h.AppendHookOnBeforeUpdate(func() error {
Expand Down Expand Up @@ -455,10 +460,14 @@ func (p *playerImpl) read() ([]byte, bool) {

const bufSize = 2048

p.context.semaphore <- struct{}{}
defer func() {
<-p.context.semaphore
}()
// If audio is suspended, fill zero values not to cause delay (#975).
// TODO: Oto's players should be able to be suspended and resumed.
p.context.m.Lock()
s := p.context.suspended
p.context.m.Unlock()
if s {
return emptyBytes, true
}

newBuf := make([]byte, bufSize-len(p.buf))
n, err := p.src.Read(newBuf)
Expand Down

0 comments on commit 529dddd

Please sign in to comment.