From 5713731363d2cfad39a312d9b990ba25a7716b7f Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 10 Aug 2021 21:09:23 -0700 Subject: [PATCH] allow user to mute before track is fully published (#82) fixes https://github.com/livekit/client-sdk-js/issues/28 --- go.mod | 2 +- go.sum | 6 ++---- pkg/rtc/mediatrack.go | 2 +- pkg/rtc/participant.go | 15 +++++++++++++++ pkg/rtc/participant_internal_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index e9f32667..0a87b48f 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/google/wire v0.5.0 github.com/gorilla/websocket v1.4.2 github.com/jxskiss/base62 v0.0.0-20191017122030-4f11678b909b - github.com/livekit/protocol v0.7.1 + github.com/livekit/protocol v0.7.2 github.com/magefile/mage v1.11.0 github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index b3c5c428..e371d068 100644 --- a/go.sum +++ b/go.sum @@ -237,10 +237,8 @@ github.com/lithammer/shortuuid/v3 v3.0.6 h1:pr15YQyvhiSX/qPxncFtqk+v4xLEpOZObbsY github.com/lithammer/shortuuid/v3 v3.0.6/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts= github.com/livekit/ion-sfu v1.20.6 h1:vA98RfuW3sSidV1rfK+/szGWgHFgki4Q4pomxsJS0i0= github.com/livekit/ion-sfu v1.20.6/go.mod h1:dEdOG4KSqIftr5HxxqciNKBIdu0v3OD0ZYL7A3J09KA= -github.com/livekit/protocol v0.7.0 h1:p1HjTwlFdWalJAHs0zDFXy3Nyou0z+WqD9bernAoGT0= -github.com/livekit/protocol v0.7.0/go.mod h1:Vk04t1uIJa+U2L5SeANEmDl6ebjc9tKVi4kk3CpqW74= -github.com/livekit/protocol v0.7.1 h1:KO6n7SYrwJfnHekX0GrFY1BfC+fCXYHa5yIQ099rgsU= -github.com/livekit/protocol v0.7.1/go.mod h1:Vk04t1uIJa+U2L5SeANEmDl6ebjc9tKVi4kk3CpqW74= +github.com/livekit/protocol v0.7.2 h1:4qwCkIFKhDYeyzp79lwb09/nwkjyjql3/o/Viifnyig= +github.com/livekit/protocol v0.7.2/go.mod h1:Vk04t1uIJa+U2L5SeANEmDl6ebjc9tKVi4kk3CpqW74= github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ= github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= diff --git a/pkg/rtc/mediatrack.go b/pkg/rtc/mediatrack.go index 75fed104..ea864d94 100644 --- a/pkg/rtc/mediatrack.go +++ b/pkg/rtc/mediatrack.go @@ -182,7 +182,6 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error { downTrack.SetTransceiver(transceiver) // when outtrack is bound, start loop to send reports downTrack.OnBind(func() { - subTrack.SetPublisherMuted(t.IsMuted()) go t.sendDownTrackBindingReports(sub) }) @@ -227,6 +226,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error { }) t.subscribedTracks[sub.ID()] = subTrack + subTrack.SetPublisherMuted(t.IsMuted()) t.receiver.AddDownTrack(downTrack, t.shouldStartWithBestQuality()) // since sub will lock, run it in a goroutine to avoid deadlocks diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index e750a014..727e8d29 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -303,6 +303,7 @@ func (p *ParticipantImpl) AddTrack(req *livekit.AddTrackRequest) { Sid: utils.NewGuid(utils.TrackPrefix), Width: req.Width, Height: req.Height, + Muted: req.Muted, } p.pendingTracks[req.Cid] = ti @@ -536,9 +537,21 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket) error { } func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool) { + isPending := false p.lock.RLock() + for _, ti := range p.pendingTracks { + if ti.Sid == trackId { + ti.Muted = muted + isPending = true + } + } track := p.publishedTracks[trackId] p.lock.RUnlock() + + // already handled + if isPending { + return + } if track == nil { logger.Warnw("could not locate track", nil, "track", trackId) return @@ -719,6 +732,7 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w } // delete pending track if it's not simulcasting + // TODO: we should delete it after adding three tracks ti := p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()), track.RID() == "") if ti == nil { return @@ -745,6 +759,7 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w Height: ti.Height, }) mt.name = ti.Name + mt.SetMuted(ti.Muted) newTrack = true } diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 12ed14a0..44bdd3a5 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -159,6 +159,31 @@ func TestCorrectJoinedAt(t *testing.T) { require.True(t, time.Now().Unix()-info.JoinedAt <= 1) } +func TestMuteSetting(t *testing.T) { + t.Run("can set mute when track is pending", func(t *testing.T) { + p := newParticipantForTest("test") + ti := &livekit.TrackInfo{Sid: "testTrack"} + p.pendingTracks["cid"] = ti + + p.SetTrackMuted(ti.Sid, true) + require.True(t, ti.Muted) + + }) + + t.Run("can publish a muted track", func(t *testing.T) { + p := newParticipantForTest("test") + p.AddTrack(&livekit.AddTrackRequest{ + Cid: "cid", + Type: livekit.TrackType_AUDIO, + Muted: true, + }) + + ti := p.getPendingTrack("cid", livekit.TrackType_AUDIO, false) + require.NotNil(t, ti) + require.True(t, ti.Muted) + }) +} + func newParticipantForTest(identity string) *ParticipantImpl { conf, _ := config.NewConfig("", nil) // disable mux, it doesn't play too well with unit test