From e82d50d717f074386af79260f18091e317a12bf1 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 1 Sep 2021 21:50:02 -0700 Subject: [PATCH] Send explicit message to clients for mute/unmute. (#107) * Send explicit message to clients for mute/unmute. * documentation in config --- config-sample.yaml | 3 +++ go.mod | 2 +- go.sum | 4 ++-- pkg/config/config.go | 7 ++++--- pkg/routing/interfaces.go | 1 - pkg/routing/redisrouter.go | 2 -- pkg/rtc/helper_test.go | 2 +- pkg/rtc/participant.go | 14 +++++++++++++- pkg/rtc/participant_internal_test.go | 2 +- pkg/rtc/room_test.go | 2 +- pkg/rtc/types/interfaces.go | 2 +- pkg/rtc/types/typesfakes/fake_participant.go | 18 ++++++++++-------- pkg/service/roommanager.go | 14 +++++++------- pkg/service/rtcservice.go | 3 --- 14 files changed, 44 insertions(+), 32 deletions(-) diff --git a/config-sample.yaml b/config-sample.yaml index 760a74bd..a5d604f4 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -74,6 +74,9 @@ keys: # enabled_codecs: # - mime: audio/opus # - mime: video/vp8 +# # allow tracks to be unmuted remotely, defaults to false +# # tracks can always be muted from the Room Service APIs +# enable_remote_unmute: true # Webhooks # when configured, LiveKit notifies your URL handler with room events diff --git a/go.mod b/go.mod index d9d6c96d..e3d026ca 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,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.8.1 + github.com/livekit/protocol v0.8.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 d0db9f5f..299d84c4 100644 --- a/go.sum +++ b/go.sum @@ -241,8 +241,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.9 h1:Ih+tE5CXqdL1o6iXfZn+uBoddBXWceaOKVIun6aVuZM= github.com/livekit/ion-sfu v1.20.9/go.mod h1:g8hwobZI5fvX1RXvayf4ZXkgP7spV5YGE4yTSsumpB4= -github.com/livekit/protocol v0.8.1 h1:J4mz1C2rUM6696xc1UQJ+qpi7DJXLWl9oTR68xYTGX8= -github.com/livekit/protocol v0.8.1/go.mod h1:YKcyBbqH0WmNL35i7c5jxr1L2Az13oT10oGNDOijI20= +github.com/livekit/protocol v0.8.2 h1:GvB4fDaIOkDUtYe+G/AMm7mPE19U4zv/55XL7LrrqpY= +github.com/livekit/protocol v0.8.2/go.mod h1:YKcyBbqH0WmNL35i7c5jxr1L2Az13oT10oGNDOijI20= 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/config/config.go b/pkg/config/config.go index 9d242beb..2383d74a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -82,9 +82,10 @@ type RedisConfig struct { } type RoomConfig struct { - EnabledCodecs []CodecSpec `yaml:"enabled_codecs"` - MaxParticipants uint32 `yaml:"max_participants"` - EmptyTimeout uint32 `yaml:"empty_timeout"` + EnabledCodecs []CodecSpec `yaml:"enabled_codecs"` + MaxParticipants uint32 `yaml:"max_participants"` + EmptyTimeout uint32 `yaml:"empty_timeout"` + EnableRemoteUnmute bool `yaml:"enable_remote_unmute"` } type CodecSpec struct { diff --git a/pkg/routing/interfaces.go b/pkg/routing/interfaces.go index c94c9d94..bfbaf57a 100644 --- a/pkg/routing/interfaces.go +++ b/pkg/routing/interfaces.go @@ -30,7 +30,6 @@ type ParticipantInit struct { Reconnect bool Permission *livekit.ParticipantPermission ProtocolVersion int32 - UsePlanB bool AutoSubscribe bool Hidden bool } diff --git a/pkg/routing/redisrouter.go b/pkg/routing/redisrouter.go index a212caee..e666d43a 100644 --- a/pkg/routing/redisrouter.go +++ b/pkg/routing/redisrouter.go @@ -154,7 +154,6 @@ func (r *RedisRouter) StartParticipantSignal(ctx context.Context, roomName strin Reconnect: pi.Reconnect, Permission: pi.Permission, ProtocolVersion: pi.ProtocolVersion, - UsePlanB: pi.UsePlanB, AutoSubscribe: pi.AutoSubscribe, Hidden: pi.Hidden, }) @@ -225,7 +224,6 @@ func (r *RedisRouter) startParticipantRTC(ss *livekit.StartSession, participantK Reconnect: ss.Reconnect, Permission: ss.Permission, ProtocolVersion: ss.ProtocolVersion, - UsePlanB: ss.UsePlanB, AutoSubscribe: ss.AutoSubscribe, Hidden: ss.Hidden, } diff --git a/pkg/rtc/helper_test.go b/pkg/rtc/helper_test.go index 05d569aa..6e08648f 100644 --- a/pkg/rtc/helper_test.go +++ b/pkg/rtc/helper_test.go @@ -38,7 +38,7 @@ func newMockParticipant(identity string, protocol types.ProtocolVersion, hidden } } - p.SetTrackMutedStub = func(sid string, muted bool) { + p.SetTrackMutedStub = func(sid string, muted bool, fromServer bool) { updateTrack() } p.AddTrackStub = func(req *livekit.AddTrackRequest) { diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index b1696715..fd9fea1d 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -536,7 +536,7 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket) error { } } -func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool) { +func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool, fromAdmin bool) { isPending := false p.lock.RLock() for _, ti := range p.pendingTracks { @@ -559,6 +559,18 @@ func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool) { currentMuted := track.IsMuted() track.SetMuted(muted) + // when request is coming from admin, send message to current participant + if fromAdmin { + _ = p.writeMessage(&livekit.SignalResponse{ + Message: &livekit.SignalResponse_Mute{ + Mute: &livekit.MuteTrackRequest{ + Sid: trackId, + Muted: muted, + }, + }, + }) + } + if currentMuted != track.IsMuted() && p.onTrackUpdated != nil { logger.Debugw("mute status changed", "participant", p.Identity(), diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 5e62abc3..82ff33a4 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -165,7 +165,7 @@ func TestMuteSetting(t *testing.T) { ti := &livekit.TrackInfo{Sid: "testTrack"} p.pendingTracks["cid"] = ti - p.SetTrackMuted(ti.Sid, true) + p.SetTrackMuted(ti.Sid, true, false) require.True(t, ti.Muted) }) diff --git a/pkg/rtc/room_test.go b/pkg/rtc/room_test.go index d1f22f4a..365a430e 100644 --- a/pkg/rtc/room_test.go +++ b/pkg/rtc/room_test.go @@ -146,7 +146,7 @@ func TestParticipantUpdate(t *testing.T) { "track mutes are sent to everyone", true, func(p types.Participant) { - p.SetTrackMuted("", true) + p.SetTrackMuted("", true, false) }, }, { diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index bfdc6ed2..830dd50f 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -50,7 +50,7 @@ type Participant interface { SendParticipantUpdate(participants []*livekit.ParticipantInfo) error SendActiveSpeakers(speakers []*livekit.SpeakerInfo) error SendDataPacket(packet *livekit.DataPacket) error - SetTrackMuted(trackId string, muted bool) + SetTrackMuted(trackId string, muted bool, fromAdmin bool) GetAudioLevel() (level uint8, active bool) // permissions diff --git a/pkg/rtc/types/typesfakes/fake_participant.go b/pkg/rtc/types/typesfakes/fake_participant.go index 55a7d603..0e06d6b1 100644 --- a/pkg/rtc/types/typesfakes/fake_participant.go +++ b/pkg/rtc/types/typesfakes/fake_participant.go @@ -351,11 +351,12 @@ type FakeParticipant struct { setResponseSinkArgsForCall []struct { arg1 routing.MessageSink } - SetTrackMutedStub func(string, bool) + SetTrackMutedStub func(string, bool, bool) setTrackMutedMutex sync.RWMutex setTrackMutedArgsForCall []struct { arg1 string arg2 bool + arg3 bool } StartStub func() startMutex sync.RWMutex @@ -2278,17 +2279,18 @@ func (fake *FakeParticipant) SetResponseSinkArgsForCall(i int) routing.MessageSi return argsForCall.arg1 } -func (fake *FakeParticipant) SetTrackMuted(arg1 string, arg2 bool) { +func (fake *FakeParticipant) SetTrackMuted(arg1 string, arg2 bool, arg3 bool) { fake.setTrackMutedMutex.Lock() fake.setTrackMutedArgsForCall = append(fake.setTrackMutedArgsForCall, struct { arg1 string arg2 bool - }{arg1, arg2}) + arg3 bool + }{arg1, arg2, arg3}) stub := fake.SetTrackMutedStub - fake.recordInvocation("SetTrackMuted", []interface{}{arg1, arg2}) + fake.recordInvocation("SetTrackMuted", []interface{}{arg1, arg2, arg3}) fake.setTrackMutedMutex.Unlock() if stub != nil { - fake.SetTrackMutedStub(arg1, arg2) + fake.SetTrackMutedStub(arg1, arg2, arg3) } } @@ -2298,17 +2300,17 @@ func (fake *FakeParticipant) SetTrackMutedCallCount() int { return len(fake.setTrackMutedArgsForCall) } -func (fake *FakeParticipant) SetTrackMutedCalls(stub func(string, bool)) { +func (fake *FakeParticipant) SetTrackMutedCalls(stub func(string, bool, bool)) { fake.setTrackMutedMutex.Lock() defer fake.setTrackMutedMutex.Unlock() fake.SetTrackMutedStub = stub } -func (fake *FakeParticipant) SetTrackMutedArgsForCall(i int) (string, bool) { +func (fake *FakeParticipant) SetTrackMutedArgsForCall(i int) (string, bool, bool) { fake.setTrackMutedMutex.RLock() defer fake.setTrackMutedMutex.RUnlock() argsForCall := fake.setTrackMutedArgsForCall[i] - return argsForCall.arg1, argsForCall.arg2 + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 } func (fake *FakeParticipant) Start() { diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 70250eba..9901af80 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -10,7 +10,6 @@ import ( livekit "github.com/livekit/protocol/proto" "github.com/livekit/protocol/utils" "github.com/livekit/protocol/webhook" - "github.com/pion/webrtc/v3" "github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/logger" @@ -287,16 +286,12 @@ func (r *LocalRoomManager) StartSession(ctx context.Context, roomName string, pi "room", roomName, "nodeID", r.currentNode.Id, "participant", pi.Identity, - "planB", pi.UsePlanB, "protocol", pi.ProtocolVersion, ) pv := types.ProtocolVersion(pi.ProtocolVersion) rtcConf := *r.rtcConfig rtcConf.SetBufferFactory(room.GetBufferFactor()) - if pi.UsePlanB { - rtcConf.Configuration.SDPSemantics = webrtc.SDPSemanticsPlanB - } participant, err = rtc.NewParticipant(rtc.ParticipantParams{ Identity: pi.Identity, Config: &rtcConf, @@ -459,7 +454,7 @@ func (r *LocalRoomManager) rtcSessionWorker(room *rtc.Room, participant types.Pa logger.Errorw("could not handle trickle", err, "participant", participant.Identity(), "pID", participant.ID()) } case *livekit.SignalRequest_Mute: - participant.SetTrackMuted(msg.Mute.Sid, msg.Mute.Muted) + participant.SetTrackMuted(msg.Mute.Sid, msg.Mute.Muted, false) case *livekit.SignalRequest_Subscription: if err := room.UpdateSubscriptions(participant, msg.Subscription.TrackSids, msg.Subscription.Subscribe); err != nil { logger.Warnw("could not update subscription", err, @@ -490,6 +485,7 @@ func (r *LocalRoomManager) rtcSessionWorker(room *rtc.Room, participant types.Pa } } +// handles RTC messages resulted from Room API calls func (r *LocalRoomManager) handleRTCMessage(ctx context.Context, roomName, identity string, msg *livekit.RTCNodeMessage) { r.lock.RLock() room := r.rooms[roomName] @@ -512,7 +508,11 @@ func (r *LocalRoomManager) handleRTCMessage(ctx context.Context, roomName, ident case *livekit.RTCNodeMessage_MuteTrack: logger.Debugw("setting track muted", "room", roomName, "participant", identity, "track", rm.MuteTrack.TrackSid, "muted", rm.MuteTrack.Muted) - participant.SetTrackMuted(rm.MuteTrack.TrackSid, rm.MuteTrack.Muted) + if !rm.MuteTrack.Muted && !r.config.Room.EnableRemoteUnmute { + logger.Errorw("cannot unmute track, remote unmute is disabled", nil) + return + } + participant.SetTrackMuted(rm.MuteTrack.TrackSid, rm.MuteTrack.Muted, true) case *livekit.RTCNodeMessage_UpdateParticipant: logger.Debugw("updating participant", "room", roomName, "participant", identity) if rm.UpdateParticipant.Metadata != "" { diff --git a/pkg/service/rtcservice.go b/pkg/service/rtcservice.go index 12cdd265..e5b4d726 100644 --- a/pkg/service/rtcservice.go +++ b/pkg/service/rtcservice.go @@ -68,8 +68,6 @@ func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit, reconnectParam := r.FormValue("reconnect") protocolParam := r.FormValue("protocol") autoSubParam := r.FormValue("auto_subscribe") - // plan b does not work fully at the moment. - planBParam := r.FormValue("planb") if onlyName != "" { roomName = onlyName @@ -78,7 +76,6 @@ func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit, pi := routing.ParticipantInit{ Reconnect: boolValue(reconnectParam), Identity: claims.Identity, - UsePlanB: boolValue(planBParam), AutoSubscribe: true, Metadata: claims.Metadata, Hidden: claims.Video.Hidden,