audio/internal/readerdriver: unqueuedBufs should be reset when the player is reused later

This commit is contained in:
Hajime Hoshi 2021-05-09 19:54:34 +09:00
parent f04230da08
commit d9972822e2

View File

@ -517,6 +517,8 @@ func (p *playerImpl) closeForReuse() error {
func (p *playerImpl) closeImpl(reuseLater bool) error { func (p *playerImpl) closeImpl(reuseLater bool) error {
if p.audioQueue != nil { if p.audioQueue != nil {
// Even if reuseLater is true, AudioQueuePause is not efficent for reusing.
// AudioQueueStart takes long if the AudioQueueStop is not called.
if osstatus := C.AudioQueueStop(p.audioQueue, C.true); osstatus != C.noErr && p.err != nil { if osstatus := C.AudioQueueStop(p.audioQueue, C.true); osstatus != C.noErr && p.err != nil {
// setErrorImpl calls closeImpl. Do not call this. // setErrorImpl calls closeImpl. Do not call this.
p.err = fmt.Errorf("readerdriver: AudioQueueStop failed: %d", osstatus) p.err = fmt.Errorf("readerdriver: AudioQueueStop failed: %d", osstatus)
@ -527,12 +529,14 @@ func (p *playerImpl) closeImpl(reuseLater bool) error {
thePlayers.remove(p.audioQueue) thePlayers.remove(p.audioQueue)
p.audioQueue = nil p.audioQueue = nil
} }
// When reuseLater is true, this playerImpl can be reused later even though the AudioQueue is removed.
if reuseLater { if reuseLater {
p.state = playerPaused p.state = playerPaused
p.buf = p.buf[:0] p.buf = p.buf[:0]
p.eof = false p.eof = false
} else {
p.unqueuedBufs = nil p.unqueuedBufs = nil
} else {
p.state = playerClosed p.state = playerClosed
} }
p.cond.Signal() p.cond.Signal()