From 5fcd682fb0c30e7a01dcf7d2da2a93997aee5ed1 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 3 May 2023 13:50:45 -0700 Subject: [PATCH] Refactor participant metadata updates to avoid duplication (#1679) * Refactor participant metadata updates to avoid duplication * generated fakes --- pkg/rtc/room.go | 9 ++++++ pkg/rtc/signalhandler.go | 7 +---- pkg/rtc/subscriptionmanager.go | 6 ++-- pkg/rtc/types/interfaces.go | 1 + pkg/rtc/types/typesfakes/fake_room.go | 43 +++++++++++++++++++++++++++ pkg/service/roommanager.go | 7 +---- 6 files changed, 58 insertions(+), 15 deletions(-) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 72cbb206f..6d339d83f 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -649,6 +649,15 @@ func (r *Room) SetMetadata(metadata string) { r.protoProxy.MarkDirty(true) } +func (r *Room) UpdateParticipantMetadata(participant types.LocalParticipant, name string, metadata string) { + if metadata != "" { + participant.SetMetadata(metadata) + } + if name != "" { + participant.SetName(name) + } +} + func (r *Room) sendRoomUpdate() { roomInfo := r.ToProto() // Send update to participants diff --git a/pkg/rtc/signalhandler.go b/pkg/rtc/signalhandler.go index ee349201f..c29baf10f 100644 --- a/pkg/rtc/signalhandler.go +++ b/pkg/rtc/signalhandler.go @@ -76,12 +76,7 @@ func HandleParticipantSignal(room types.Room, participant types.LocalParticipant case *livekit.SignalRequest_UpdateMetadata: if participant.ClaimGrants().Video.GetCanUpdateOwnMetadata() { - if msg.UpdateMetadata.Metadata != "" { - participant.SetMetadata(msg.UpdateMetadata.Metadata) - } - if msg.UpdateMetadata.Name != "" { - participant.SetName(msg.UpdateMetadata.Name) - } + room.UpdateParticipantMetadata(participant, msg.UpdateMetadata.Name, msg.UpdateMetadata.Metadata) } } return nil diff --git a/pkg/rtc/subscriptionmanager.go b/pkg/rtc/subscriptionmanager.go index 0e43f400f..329c067ab 100644 --- a/pkg/rtc/subscriptionmanager.go +++ b/pkg/rtc/subscriptionmanager.go @@ -391,7 +391,7 @@ func (m *SubscriptionManager) reconcileWorker() { } } -func (m *SubscriptionManager) hasCapcityForSubscription(kind livekit.TrackType) bool { +func (m *SubscriptionManager) hasCapacityForSubscription(kind livekit.TrackType) bool { switch kind { case livekit.TrackType_VIDEO: if m.params.SubscriptionLimitVideo > 0 && m.subscribedVideoCount.Load() >= m.params.SubscriptionLimitVideo { @@ -413,7 +413,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error { return ErrNoSubscribePermission } - if kind, ok := s.getKind(); ok && !m.hasCapcityForSubscription(kind) { + if kind, ok := s.getKind(); ok && !m.hasCapacityForSubscription(kind) { return ErrSubscriptionLimitExceeded } @@ -445,7 +445,7 @@ func (m *SubscriptionManager) subscribe(s *trackSubscription) error { return ErrTrackNotFound } s.trySetKind(track.Kind()) - if !m.hasCapcityForSubscription(track.Kind()) { + if !m.hasCapacityForSubscription(track.Kind()) { return ErrSubscriptionLimitExceeded } diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index 3c6299f9e..84989179a 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -357,6 +357,7 @@ type Room interface { UpdateVideoLayers(participant Participant, updateVideoLayers *livekit.UpdateVideoLayers) error ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIdentity, trackID livekit.TrackID) MediaResolverResult GetLocalParticipants() []LocalParticipant + UpdateParticipantMetadata(participant LocalParticipant, name string, metadata string) } // MediaTrack represents a media track diff --git a/pkg/rtc/types/typesfakes/fake_room.go b/pkg/rtc/types/typesfakes/fake_room.go index 84449cf56..67d68e8ca 100644 --- a/pkg/rtc/types/typesfakes/fake_room.go +++ b/pkg/rtc/types/typesfakes/fake_room.go @@ -82,6 +82,13 @@ type FakeRoom struct { syncStateReturnsOnCall map[int]struct { result1 error } + UpdateParticipantMetadataStub func(types.LocalParticipant, string, string) + updateParticipantMetadataMutex sync.RWMutex + updateParticipantMetadataArgsForCall []struct { + arg1 types.LocalParticipant + arg2 string + arg3 string + } UpdateSubscriptionPermissionStub func(types.LocalParticipant, *livekit.SubscriptionPermission) error updateSubscriptionPermissionMutex sync.RWMutex updateSubscriptionPermissionArgsForCall []struct { @@ -497,6 +504,40 @@ func (fake *FakeRoom) SyncStateReturnsOnCall(i int, result1 error) { }{result1} } +func (fake *FakeRoom) UpdateParticipantMetadata(arg1 types.LocalParticipant, arg2 string, arg3 string) { + fake.updateParticipantMetadataMutex.Lock() + fake.updateParticipantMetadataArgsForCall = append(fake.updateParticipantMetadataArgsForCall, struct { + arg1 types.LocalParticipant + arg2 string + arg3 string + }{arg1, arg2, arg3}) + stub := fake.UpdateParticipantMetadataStub + fake.recordInvocation("UpdateParticipantMetadata", []interface{}{arg1, arg2, arg3}) + fake.updateParticipantMetadataMutex.Unlock() + if stub != nil { + fake.UpdateParticipantMetadataStub(arg1, arg2, arg3) + } +} + +func (fake *FakeRoom) UpdateParticipantMetadataCallCount() int { + fake.updateParticipantMetadataMutex.RLock() + defer fake.updateParticipantMetadataMutex.RUnlock() + return len(fake.updateParticipantMetadataArgsForCall) +} + +func (fake *FakeRoom) UpdateParticipantMetadataCalls(stub func(types.LocalParticipant, string, string)) { + fake.updateParticipantMetadataMutex.Lock() + defer fake.updateParticipantMetadataMutex.Unlock() + fake.UpdateParticipantMetadataStub = stub +} + +func (fake *FakeRoom) UpdateParticipantMetadataArgsForCall(i int) (types.LocalParticipant, string, string) { + fake.updateParticipantMetadataMutex.RLock() + defer fake.updateParticipantMetadataMutex.RUnlock() + argsForCall := fake.updateParticipantMetadataArgsForCall[i] + return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3 +} + func (fake *FakeRoom) UpdateSubscriptionPermission(arg1 types.LocalParticipant, arg2 *livekit.SubscriptionPermission) error { fake.updateSubscriptionPermissionMutex.Lock() ret, specificReturn := fake.updateSubscriptionPermissionReturnsOnCall[len(fake.updateSubscriptionPermissionArgsForCall)] @@ -683,6 +724,8 @@ func (fake *FakeRoom) Invocations() map[string][][]interface{} { defer fake.simulateScenarioMutex.RUnlock() fake.syncStateMutex.RLock() defer fake.syncStateMutex.RUnlock() + fake.updateParticipantMetadataMutex.RLock() + defer fake.updateParticipantMetadataMutex.RUnlock() fake.updateSubscriptionPermissionMutex.RLock() defer fake.updateSubscriptionPermissionMutex.RUnlock() fake.updateSubscriptionsMutex.RLock() diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 3966d4f97..5b24c44bc 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -590,12 +590,7 @@ func (r *RoomManager) handleRTCMessage(ctx context.Context, roomName livekit.Roo } pLogger.Debugw("updating participant", "metadata", rm.UpdateParticipant.Metadata, "permission", rm.UpdateParticipant.Permission) - if rm.UpdateParticipant.Name != "" { - participant.SetName(rm.UpdateParticipant.Name) - } - if rm.UpdateParticipant.Metadata != "" { - participant.SetMetadata(rm.UpdateParticipant.Metadata) - } + room.UpdateParticipantMetadata(participant, rm.UpdateParticipant.Name, rm.UpdateParticipant.Metadata) if rm.UpdateParticipant.Permission != nil { participant.SetPermission(rm.UpdateParticipant.Permission) }