audio: refactoring

This commit is contained in:
Hajime Hoshi 2024-02-01 14:24:07 +09:00
parent 061d8617ce
commit b9f0db955c
3 changed files with 20 additions and 20 deletions

View File

@ -70,7 +70,7 @@ type Context struct {
ready bool ready bool
readyOnce sync.Once readyOnce sync.Once
players map[*playerImpl]struct{} playingPlayers map[*playerImpl]struct{}
m sync.Mutex m sync.Mutex
semaphore chan struct{} semaphore chan struct{}
@ -97,11 +97,11 @@ func NewContext(sampleRate int) *Context {
} }
c := &Context{ c := &Context{
sampleRate: sampleRate, sampleRate: sampleRate,
playerFactory: newPlayerFactory(sampleRate), playerFactory: newPlayerFactory(sampleRate),
players: map[*playerImpl]struct{}{}, playingPlayers: map[*playerImpl]struct{}{},
inited: make(chan struct{}), inited: make(chan struct{}),
semaphore: make(chan struct{}, 1), semaphore: make(chan struct{}, 1),
} }
theContext = c theContext = c
@ -175,14 +175,14 @@ func (c *Context) setReady() {
c.m.Unlock() c.m.Unlock()
} }
func (c *Context) addPlayer(p *playerImpl) { func (c *Context) addPlayingPlayer(p *playerImpl) {
c.m.Lock() c.m.Lock()
defer c.m.Unlock() defer c.m.Unlock()
c.players[p] = struct{}{} c.playingPlayers[p] = struct{}{}
// Check the source duplication // Check the source duplication
srcs := map[io.Reader]struct{}{} srcs := map[io.Reader]struct{}{}
for p := range c.players { for p := range c.playingPlayers {
if _, ok := srcs[p.source()]; ok { if _, ok := srcs[p.source()]; ok {
c.err = errors.New("audio: a same source is used by multiple Player") c.err = errors.New("audio: a same source is used by multiple Player")
return return
@ -191,9 +191,9 @@ func (c *Context) addPlayer(p *playerImpl) {
} }
} }
func (c *Context) removePlayer(p *playerImpl) { func (c *Context) removePlayingPlayer(p *playerImpl) {
c.m.Lock() c.m.Lock()
delete(c.players, p) delete(c.playingPlayers, p)
c.m.Unlock() c.m.Unlock()
} }
@ -202,8 +202,8 @@ func (c *Context) gcPlayers() error {
// Copy the playerImpls and iterate them without a lock. // Copy the playerImpls and iterate them without a lock.
var players []*playerImpl var players []*playerImpl
c.m.Lock() c.m.Lock()
players = make([]*playerImpl, 0, len(c.players)) players = make([]*playerImpl, 0, len(c.playingPlayers))
for p := range c.players { for p := range c.playingPlayers {
players = append(players, p) players = append(players, p)
} }
c.m.Unlock() c.m.Unlock()
@ -225,7 +225,7 @@ func (c *Context) gcPlayers() error {
c.m.Lock() c.m.Lock()
for _, p := range playersToRemove { for _, p := range playersToRemove {
delete(c.players, p) delete(c.playingPlayers, p)
} }
c.m.Unlock() c.m.Unlock()
@ -242,7 +242,7 @@ func (c *Context) IsReady() bool {
if c.ready { if c.ready {
return true return true
} }
if len(c.players) != 0 { if len(c.playingPlayers) != 0 {
return false return false
} }

View File

@ -142,7 +142,7 @@ func UpdateForTesting() error {
func PlayersCountForTesting() int { func PlayersCountForTesting() int {
c := CurrentContext() c := CurrentContext()
c.m.Lock() c.m.Lock()
n := len(c.players) n := len(c.playingPlayers)
c.m.Unlock() c.m.Unlock()
return n return n
} }

View File

@ -177,7 +177,7 @@ func (p *playerImpl) Play() {
return return
} }
p.player.Play() p.player.Play()
p.context.addPlayer(p) p.context.addPlayingPlayer(p)
} }
func (p *playerImpl) Pause() { func (p *playerImpl) Pause() {
@ -192,7 +192,7 @@ func (p *playerImpl) Pause() {
} }
p.player.Pause() p.player.Pause()
p.context.removePlayer(p) p.context.removePlayingPlayer(p)
} }
func (p *playerImpl) IsPlaying() bool { func (p *playerImpl) IsPlaying() bool {
@ -250,7 +250,7 @@ func (p *playerImpl) Position() time.Duration {
return 0 return 0
} }
samples := (p.stream.Current() - int64(p.player.BufferedSize())) / bytesPerSampleInt16 samples := (p.stream.position() - int64(p.player.BufferedSize())) / bytesPerSampleInt16
return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate) return time.Duration(samples) * time.Second / time.Duration(p.factory.sampleRate)
} }
@ -366,7 +366,7 @@ func (s *timeStream) timeDurationToPos(offset time.Duration) int64 {
return o return o
} }
func (s *timeStream) Current() int64 { func (s *timeStream) position() int64 {
s.m.Lock() s.m.Lock()
defer s.m.Unlock() defer s.m.Unlock()