audio/internal/readerdriver: Refactoring: unify add and play (Windows)

This commit is contained in:
Hajime Hoshi 2021-05-28 04:40:50 +09:00
parent 72d8d77173
commit f334eee1e4

View File

@ -110,45 +110,14 @@ func (p *players) setContext(context *context) {
p.context = context p.context = context
} }
func (p *players) add(player *playerImpl) { func (p *players) add(player *playerImpl) error {
p.cond.L.Lock() p.cond.L.Lock()
defer p.cond.L.Unlock() defer p.cond.L.Unlock()
if p.players == nil { if p.players == nil {
p.players = map[*playerImpl]struct{}{} p.players = map[*playerImpl]struct{}{}
} }
p.players[player] = struct{}{} p.players[player] = struct{}{}
}
func (p *players) remove(player *playerImpl) error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
delete(p.players, player)
if len(p.players) > 0 {
return nil
}
if p.waveOut == 0 {
return nil
}
for _, h := range p.headers {
if err := h.Close(); err != nil {
return err
}
}
p.headers = p.headers[:0]
if err := waveOutClose(p.waveOut); err != nil {
return err
}
p.waveOut = 0
p.cond.Signal()
return nil
}
func (p *players) play() error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
if p.waveOut != 0 { if p.waveOut != 0 {
return nil return nil
@ -192,6 +161,33 @@ func (p *players) play() error {
return nil return nil
} }
func (p *players) remove(player *playerImpl) error {
p.cond.L.Lock()
defer p.cond.L.Unlock()
delete(p.players, player)
if len(p.players) > 0 {
return nil
}
if p.waveOut == 0 {
return nil
}
for _, h := range p.headers {
if err := h.Close(); err != nil {
return err
}
}
p.headers = p.headers[:0]
if err := waveOutClose(p.waveOut); err != nil {
return err
}
p.waveOut = 0
p.cond.Signal()
return nil
}
func (p *players) shouldWait() bool { func (p *players) shouldWait() bool {
if p.waveOut == 0 { if p.waveOut == 0 {
return false return false
@ -442,11 +438,6 @@ func (p *playerImpl) playImpl() {
return return
} }
// thePlayers can has another mutex, and double mutex might introduce a deadlock.
p.m.Unlock()
thePlayers.add(p)
p.m.Lock()
buf := make([]byte, p.context.maxBufferSize()) buf := make([]byte, p.context.maxBufferSize())
for len(p.buf) < p.context.maxBufferSize() { for len(p.buf) < p.context.maxBufferSize() {
n, err := p.src.Read(buf) n, err := p.src.Read(buf)
@ -465,8 +456,9 @@ func (p *playerImpl) playImpl() {
return return
} }
// thePlayers can has another mutex, and double mutex might introduce a deadlock.
p.m.Unlock() p.m.Unlock()
err := thePlayers.play() err := thePlayers.add(p)
p.m.Lock() p.m.Lock()
if err != nil { if err != nil {