From c4c6acacf992ddcebd5464828c9b2dbe253fa370 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 29 Apr 2019 01:01:17 +0900 Subject: [PATCH] audio: Remove (*mux).hasPlayer --- audio/audio.go | 27 ++++++++++++++++++--------- audio/mux.go | 7 ------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/audio/audio.go b/audio/audio.go index 6c4005b62..55c9dea35 100644 --- a/audio/audio.go +++ b/audio/audio.go @@ -240,6 +240,7 @@ type playerImpl struct { src io.ReadCloser srcEOF bool sampleRate int + playing bool closedExplicitly bool runningReadLoop bool @@ -351,22 +352,20 @@ func (p *Player) Close() error { } func (p *playerImpl) Close() error { - p.mux.removePlayer(p) - p.m.Lock() - c := p.closedExplicitly - p.m.Unlock() - if c { + p.playing = false + p.mux.removePlayer(p) + if p.closedExplicitly { + p.m.Unlock() return fmt.Errorf("audio: the player is already closed") } - - p.m.Lock() p.closedExplicitly = true - p.m.Unlock() // src.Close is called only when Player's Close is called. if err := p.src.Close(); err != nil { + p.m.Unlock() return err } + p.m.Unlock() return p.closeImpl() } @@ -388,6 +387,7 @@ func (p *playerImpl) ensureReadLoop() error { func (p *playerImpl) closeImpl() error { p.m.Lock() defer p.m.Unlock() + p.playing = false if !p.runningReadLoop { return nil } @@ -416,7 +416,10 @@ func (p *Player) Play() error { } func (p *playerImpl) Play() { + p.m.Lock() + p.playing = true p.mux.addPlayer(p) + p.m.Unlock() } func (p *playerImpl) readLoop() { @@ -584,7 +587,10 @@ func (p *Player) IsPlaying() bool { } func (p *playerImpl) IsPlaying() bool { - return p.mux.hasPlayer(p) + p.m.Lock() + r := p.playing + p.m.Unlock() + return r } // Rewind rewinds the current position to the start. @@ -635,7 +641,10 @@ func (p *Player) Pause() error { } func (p *playerImpl) Pause() { + p.m.Lock() + p.playing = false p.mux.removePlayer(p) + p.m.Unlock() } // Current returns the current position. diff --git a/audio/mux.go b/audio/mux.go index 2d8983680..69a783545 100644 --- a/audio/mux.go +++ b/audio/mux.go @@ -143,10 +143,3 @@ func (m *mux) removePlayer(player *playerImpl) { delete(m.ps, player) m.m.Unlock() } - -func (m *mux) hasPlayer(player *playerImpl) bool { - m.m.RLock() - _, ok := m.ps[player] - m.m.RUnlock() - return ok -}