audio: Bug fix: Reuse buffers properly

This commit is contained in:
Hajime Hoshi 2016-02-13 20:43:27 +09:00
parent 0f81a421b8
commit 16412aab30

View File

@ -26,8 +26,12 @@ import (
"golang.org/x/mobile/exp/audio/al" "golang.org/x/mobile/exp/audio/al"
) )
// alSourceCacheEntry represents a source.
// In some environments, too many calls of alGenSources and alGenBuffers could cause errors.
// To avoid this error, al.Source and al.Buffer are reused.
type alSourceCacheEntry struct { type alSourceCacheEntry struct {
source al.Source source al.Source
buffers []al.Buffer
sampleRate int sampleRate int
isClosed bool isClosed bool
} }
@ -45,7 +49,7 @@ type player struct {
var m sync.Mutex var m sync.Mutex
func newAlSource(sampleRate int) (al.Source, error) { func newAlSource(sampleRate int) (al.Source, []al.Buffer, error) {
for _, e := range alSourceCache { for _, e := range alSourceCache {
if e.sampleRate != sampleRate { if e.sampleRate != sampleRate {
continue continue
@ -54,10 +58,10 @@ func newAlSource(sampleRate int) (al.Source, error) {
continue continue
} }
e.isClosed = false e.isClosed = false
return e.source, nil return e.source, e.buffers, nil
} }
if maxSourceNum <= len(alSourceCache) { if maxSourceNum <= len(alSourceCache) {
return 0, ErrTooManyPlayers return 0, nil, ErrTooManyPlayers
} }
s := al.GenSources(1) s := al.GenSources(1)
if err := al.Error(); err != 0 { if err := al.Error(); err != 0 {
@ -65,10 +69,11 @@ func newAlSource(sampleRate int) (al.Source, error) {
} }
e := &alSourceCacheEntry{ e := &alSourceCacheEntry{
source: s[0], source: s[0],
buffers: []al.Buffer{},
sampleRate: sampleRate, sampleRate: sampleRate,
} }
alSourceCache = append(alSourceCache, e) alSourceCache = append(alSourceCache, e)
return s[0], nil return s[0], e.buffers, nil
} }
func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) { func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
@ -77,7 +82,7 @@ func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
m.Unlock() m.Unlock()
return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e) return nil, fmt.Errorf("audio: OpenAL initialization failed: %v", e)
} }
s, err := newAlSource(sampleRate) s, b, err := newAlSource(sampleRate)
if err != nil { if err != nil {
m.Unlock() m.Unlock()
return nil, err return nil, err
@ -85,7 +90,7 @@ func newPlayer(src io.ReadSeeker, sampleRate int) (*Player, error) {
m.Unlock() m.Unlock()
p := &player{ p := &player{
alSource: s, alSource: s,
alBuffers: []al.Buffer{}, alBuffers: b,
source: src, source: src,
sampleRate: sampleRate, sampleRate: sampleRate,
} }
@ -97,10 +102,16 @@ const bufferSize = 1024
func (p *player) proceed() error { func (p *player) proceed() error {
m.Lock() m.Lock()
if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: before proceed: %d", err))
}
processedNum := p.alSource.BuffersProcessed() processedNum := p.alSource.BuffersProcessed()
if 0 < processedNum { if 0 < processedNum {
bufs := make([]al.Buffer, processedNum) bufs := make([]al.Buffer, processedNum)
p.alSource.UnqueueBuffers(bufs...) p.alSource.UnqueueBuffers(bufs...)
if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: Unqueue in process: %d", err))
}
p.alBuffers = append(p.alBuffers, bufs...) p.alBuffers = append(p.alBuffers, bufs...)
} }
m.Unlock() m.Unlock()
@ -113,6 +124,9 @@ func (p *player) proceed() error {
p.alBuffers = p.alBuffers[1:] p.alBuffers = p.alBuffers[1:]
buf.BufferData(al.FormatStereo16, b[:n], int32(p.sampleRate)) buf.BufferData(al.FormatStereo16, b[:n], int32(p.sampleRate))
p.alSource.QueueBuffers(buf) p.alSource.QueueBuffers(buf)
if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: Queue in process: %d", err))
}
m.Unlock() m.Unlock()
} }
if err != nil { if err != nil {
@ -123,6 +137,9 @@ func (p *player) proceed() error {
if p.alSource.State() == al.Stopped { if p.alSource.State() == al.Stopped {
al.RewindSources(p.alSource) al.RewindSources(p.alSource)
al.PlaySources(p.alSource) al.PlaySources(p.alSource)
if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: PlaySource in process: %d", err))
}
} }
m.Unlock() m.Unlock()
@ -167,26 +184,22 @@ func (p *player) play() error {
func (p *player) close() error { func (p *player) close() error {
m.Lock() m.Lock()
var bs []al.Buffer if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: error before closing: %d", err))
}
s := p.alSource s := p.alSource
if p.alSource == 0 { if p.alSource != 0 {
var bs []al.Buffer
al.RewindSources(p.alSource) al.RewindSources(p.alSource)
al.StopSources(p.alSource) al.StopSources(p.alSource)
n := p.alSource.BuffersQueued() n := p.alSource.BuffersQueued()
if 0 < n { if 0 < n {
bs = make([]al.Buffer, n) bs = make([]al.Buffer, n)
p.alSource.UnqueueBuffers(bs...) p.alSource.UnqueueBuffers(bs...)
p.alBuffers = append(p.alBuffers, bs...)
} }
p.alSource = 0 p.alSource = 0
} }
// TODO: Is this needed?
if 0 < len(p.alBuffers) {
al.DeleteBuffers(p.alBuffers...)
p.alBuffers = []al.Buffer{}
}
if bs != nil {
p.alBuffers = append(p.alBuffers, bs...)
}
if err := al.Error(); err != 0 { if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: closing error: %d", err)) panic(fmt.Sprintf("audio: closing error: %d", err))
} }
@ -199,6 +212,7 @@ func (p *player) close() error {
if e.isClosed { if e.isClosed {
panic("audio: cache state is invalid: source is already closed?") panic("audio: cache state is invalid: source is already closed?")
} }
e.buffers = p.alBuffers
e.isClosed = true e.isClosed = true
found = true found = true
break break