diff --git a/localparticipant.go b/localparticipant.go index b1c60fb5..937ba257 100644 --- a/localparticipant.go +++ b/localparticipant.go @@ -58,6 +58,7 @@ func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, opts *TrackPubl pub.OnRttUpdate(func(rtt uint32) { p.engine.setRTT(rtt) }) + pub.onMuteChanged = p.onTrackMuted req := &livekit.AddTrackRequest{ Cid: track.ID(), @@ -149,6 +150,7 @@ func (p *LocalParticipant) PublishSimulcastTrack(tracks []*LocalSampleTrack, opt mainTrack := tracks[len(tracks)-1] pub := NewLocalTrackPublication(KindFromRTPType(mainTrack.Kind()), nil, *opts, p.engine.client) + pub.onMuteChanged = p.onTrackMuted var layers []*livekit.VideoLayer for _, st := range tracks { @@ -359,16 +361,7 @@ func (p *LocalParticipant) updateInfo(info *livekit.ParticipantInfo) { continue } if pub.IsMuted() != ti.Muted { - pub.SetMuted(ti.Muted) - - // trigger callback - if ti.Muted { - p.Callback.OnTrackMuted(pub, p) - p.roomCallback.OnTrackMuted(pub, p) - } else if !ti.Muted { - p.Callback.OnTrackUnmuted(pub, p) - p.roomCallback.OnTrackUnmuted(pub, p) - } + _ = p.engine.client.SendMuteTrack(pub.SID(), pub.IsMuted()) } } } @@ -379,3 +372,13 @@ func (p *LocalParticipant) getLocalPublication(sid string) *LocalTrackPublicatio } return nil } + +func (p *LocalParticipant) onTrackMuted(pub *LocalTrackPublication, muted bool) { + if muted { + p.Callback.OnTrackMuted(pub, p) + p.roomCallback.OnTrackMuted(pub, p) + } else { + p.Callback.OnTrackUnmuted(pub, p) + p.roomCallback.OnTrackUnmuted(pub, p) + } +} diff --git a/publication.go b/publication.go index 65b0f803..36265747 100644 --- a/publication.go +++ b/publication.go @@ -242,6 +242,7 @@ type LocalTrackPublication struct { simulcastTracks map[livekit.VideoQuality]*LocalSampleTrack onRttUpdate func(uint32) opts TrackPublicationOptions + onMuteChanged func(*LocalTrackPublication, bool) } func NewLocalTrackPublication(kind TrackKind, track Track, opts TrackPublicationOptions, client *SignalClient) *LocalTrackPublication { @@ -280,17 +281,27 @@ func (p *LocalTrackPublication) GetSimulcastTrack(quality livekit.VideoQuality) } func (p *LocalTrackPublication) SetMuted(muted bool) { + p.setMuted(muted, false) +} + +func (p *LocalTrackPublication) setMuted(muted bool, byRemote bool) { if p.isMuted.Swap(muted) == muted { return } - _ = p.client.SendMuteTrack(p.sid.Load(), muted) + if !byRemote { + _ = p.client.SendMuteTrack(p.sid.Load(), muted) + } if track := p.track; track != nil { switch t := track.(type) { case *LocalSampleTrack: t.setMuted(muted) } } + + if p.onMuteChanged != nil { + p.onMuteChanged(p, muted) + } } func (p *LocalTrackPublication) addSimulcastTrack(st *LocalSampleTrack) { diff --git a/room.go b/room.go index 69796e56..8ac401cc 100644 --- a/room.go +++ b/room.go @@ -127,7 +127,7 @@ func CreateRoom(callback *RoomCallback) *Room { engine.OnResuming = r.handleResuming engine.OnResumed = r.handleResumed engine.client.OnLocalTrackUnpublished = r.handleLocalTrackUnpublished - engine.client.OnTrackMuted = r.handleTrackMuted + engine.client.OnTrackRemoteMuted = r.handleTrackRemoteMuted return r } @@ -484,12 +484,12 @@ func (r *Room) handleRoomUpdate(room *livekit.Room) { go r.callback.OnRoomMetadataChanged(room.Metadata) } -func (r *Room) handleTrackMuted(msg *livekit.MuteTrackRequest) { +func (r *Room) handleTrackRemoteMuted(msg *livekit.MuteTrackRequest) { for _, pub := range r.LocalParticipant.Tracks() { if pub.SID() == msg.Sid { localPub := pub.(*LocalTrackPublication) // TODO: pause sending data because it'll be dropped by SFU - localPub.SetMuted(msg.Muted) + localPub.setMuted(msg.Muted, true) } } } diff --git a/signalclient.go b/signalclient.go index 12ca817e..cf58da43 100644 --- a/signalclient.go +++ b/signalclient.go @@ -51,7 +51,7 @@ type SignalClient struct { OnSpeakersChanged func([]*livekit.SpeakerInfo) OnConnectionQuality func([]*livekit.ConnectionQualityInfo) OnRoomUpdate func(room *livekit.Room) - OnTrackMuted func(request *livekit.MuteTrackRequest) + OnTrackRemoteMuted func(request *livekit.MuteTrackRequest) OnLocalTrackUnpublished func(response *livekit.TrackUnpublishedResponse) OnTokenRefresh func(refreshToken string) OnLeave func(*livekit.LeaveRequest) @@ -319,8 +319,8 @@ func (c *SignalClient) handleResponse(res *livekit.SignalResponse) { c.OnLocalTrackPublished(msg.TrackPublished) } case *livekit.SignalResponse_Mute: - if c.OnTrackMuted != nil { - c.OnTrackMuted(msg.Mute) + if c.OnTrackRemoteMuted != nil { + c.OnTrackRemoteMuted(msg.Mute) } case *livekit.SignalResponse_ConnectionQuality: if c.OnConnectionQuality != nil {