Skip to content

Commit

Permalink
Fix race when creating a mixer track
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jan 22, 2025
1 parent e74df72 commit ecd7532
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/mixer/mixer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,20 @@ func (m *Mixer) start() {
}

func (m *Mixer) Stop() {
m.mu.Lock()
defer m.mu.Unlock()
m.stopped.Break()
}

func (m *Mixer) NewInput() *Input {
if m == nil {
return nil
}
m.mu.Lock()
defer m.mu.Unlock()
if m.stopped.IsBroken() {
return nil
}

inp := &Input{
m: m,
Expand Down Expand Up @@ -222,6 +230,9 @@ func (i *Input) SampleRate() int {
}

func (i *Input) Close() error {
if i == nil {
return nil
}
i.m.RemoveInput(i)
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/sip/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,9 @@ func (c *inboundCall) joinRoom(ctx context.Context, rconf RoomConfig, status Cal

func (c *inboundCall) playAudio(ctx context.Context, frames []media.PCM16Sample) {
t := c.lkRoom.NewTrack()
if t == nil {
return // closed
}
defer t.Close()

sampleRate := res.SampleRate
Expand Down
7 changes: 6 additions & 1 deletion pkg/sip/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ func (r *Room) Connect(conf *config.Config, rconf RoomConfig) error {

go func() {
mTrack := r.NewTrack()
if mTrack == nil {
return // closed
}
defer mTrack.Close()

odec, err := opus.Decode(mTrack, channels, log)
Expand Down Expand Up @@ -352,7 +355,6 @@ func (r *Room) CloseWithReason(reason livekit.DisconnectReason) error {
}
if r.mix != nil {
r.mix.Stop()
r.mix = nil
}
return err
}
Expand Down Expand Up @@ -391,5 +393,8 @@ func (r *Room) SendData(data lksdk.DataPacket, opts ...lksdk.DataPublishOption)
}

func (r *Room) NewTrack() *mixer.Input {
if r == nil {
return nil
}
return r.mix.NewInput()
}

0 comments on commit ecd7532

Please sign in to comment.