audio: Bug fix: sources were not reused properly

This commit is contained in:
Hajime Hoshi 2016-02-13 06:44:49 +09:00
parent 3638ac6ad8
commit 0f81a421b8

View File

@ -29,11 +29,12 @@ import (
type alSourceCacheEntry struct { type alSourceCacheEntry struct {
source al.Source source al.Source
sampleRate int sampleRate int
isClosed bool
} }
const maxSourceNum = 32 const maxSourceNum = 32
var alSourceCache = []alSourceCacheEntry{} var alSourceCache = []*alSourceCacheEntry{}
type player struct { type player struct {
alSource al.Source alSource al.Source
@ -49,10 +50,10 @@ func newAlSource(sampleRate int) (al.Source, error) {
if e.sampleRate != sampleRate { if e.sampleRate != sampleRate {
continue continue
} }
s := e.source.State() if !e.isClosed {
if s != al.Initial && s != al.Stopped {
continue continue
} }
e.isClosed = false
return e.source, nil return e.source, nil
} }
if maxSourceNum <= len(alSourceCache) { if maxSourceNum <= len(alSourceCache) {
@ -62,7 +63,7 @@ func newAlSource(sampleRate int) (al.Source, error) {
if err := al.Error(); err != 0 { if err := al.Error(); err != 0 {
panic(fmt.Sprintf("audio: al.GenSources error: %d", err)) panic(fmt.Sprintf("audio: al.GenSources error: %d", err))
} }
e := alSourceCacheEntry{ e := &alSourceCacheEntry{
source: s[0], source: s[0],
sampleRate: sampleRate, sampleRate: sampleRate,
} }
@ -167,7 +168,8 @@ func (p *player) play() error {
func (p *player) close() error { func (p *player) close() error {
m.Lock() m.Lock()
var bs []al.Buffer var bs []al.Buffer
if p.alSource != 0 { s := p.alSource
if p.alSource == 0 {
al.RewindSources(p.alSource) al.RewindSources(p.alSource)
al.StopSources(p.alSource) al.StopSources(p.alSource)
n := p.alSource.BuffersQueued() n := p.alSource.BuffersQueued()
@ -177,6 +179,7 @@ func (p *player) close() error {
} }
p.alSource = 0 p.alSource = 0
} }
// TODO: Is this needed?
if 0 < len(p.alBuffers) { if 0 < len(p.alBuffers) {
al.DeleteBuffers(p.alBuffers...) al.DeleteBuffers(p.alBuffers...)
p.alBuffers = []al.Buffer{} p.alBuffers = []al.Buffer{}
@ -187,9 +190,24 @@ func (p *player) close() error {
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))
} }
if s != 0 {
found := false
for _, e := range alSourceCache {
if e.source != s {
continue
}
if e.isClosed {
panic("audio: cache state is invalid: source is already closed?")
}
e.isClosed = true
found = true
break
}
if !found {
panic("audio: cache state is invalid: source is not cached?")
}
}
m.Unlock() m.Unlock()
runtime.SetFinalizer(p, nil) runtime.SetFinalizer(p, nil)
return nil return nil
} }
// TODO: Implement Close method