Support for participant attributes (#2806)

* Support for participant attributes

* move metadata setters to LocalParticipant

* address feedback

* forward error

* update go mod

* update attributes first
This commit is contained in:
David Zhao
2024-06-19 23:14:19 -07:00
committed by GitHub
parent d4e50b633f
commit 7a774cc82a
24 changed files with 388 additions and 142 deletions
+4 -1
View File
@@ -14,7 +14,9 @@
package rtc
import "errors"
import (
"errors"
)
var (
ErrRoomClosed = errors.New("room has already closed")
@@ -29,6 +31,7 @@ var (
ErrEmptyParticipantID = errors.New("participant ID cannot be empty")
ErrMissingGrants = errors.New("VideoGrant is missing")
ErrInternalError = errors.New("internal error")
ErrAttributeExceedsLimits = errors.New("attribute size exceeds limits")
// Track subscription related
ErrNoTrackPermission = errors.New("participant is not allowed to subscribe to this track")
+48
View File
@@ -136,6 +136,7 @@ type ParticipantParams struct {
VersionGenerator utils.TimedVersionGenerator
TrackResolver types.MediaTrackResolver
DisableDynacast bool
MaxAttributesSize uint32
SubscriberAllowPause bool
SubscriptionLimitAudio int32
SubscriptionLimitVideo int32
@@ -459,6 +460,52 @@ func (p *ParticipantImpl) SetMetadata(metadata string) {
}
}
func (p *ParticipantImpl) SetAttributes(attrs map[string]string) error {
p.lock.Lock()
grants := p.grants.Load().Clone()
if grants.Attributes == nil {
grants.Attributes = make(map[string]string)
}
var keysToDelete []string
for k, v := range attrs {
if v == "" {
keysToDelete = append(keysToDelete, k)
} else {
grants.Attributes[k] = v
}
}
for _, k := range keysToDelete {
delete(grants.Attributes, k)
}
maxAttributesSize := p.params.MaxAttributesSize
if maxAttributesSize > 0 {
total := 0
for k, v := range grants.Attributes {
total += len(k) + len(v)
}
if uint32(total) > maxAttributesSize {
p.lock.Unlock()
return ErrAttributeExceedsLimits
}
}
p.grants.Store(grants)
p.dirty.Store(true)
onParticipantUpdate := p.onParticipantUpdate
onClaimsChanged := p.onClaimsChanged
p.lock.Unlock()
if onParticipantUpdate != nil {
onParticipantUpdate(p)
}
if onClaimsChanged != nil {
onClaimsChanged(p)
}
return nil
}
func (p *ParticipantImpl) ClaimGrants() *auth.ClaimGrants {
return p.grants.Load()
}
@@ -551,6 +598,7 @@ func (p *ParticipantImpl) ToProtoWithVersion() (*livekit.ParticipantInfo, utils.
Version: v,
Permission: grants.Video.ToPermission(),
Metadata: grants.Metadata,
Attributes: grants.Attributes,
Region: p.params.Region,
IsPublisher: p.IsPublisher(),
Kind: grants.GetParticipantKind(),
+12 -1
View File
@@ -819,13 +819,24 @@ func (r *Room) SetMetadata(metadata string) <-chan struct{} {
return r.protoProxy.MarkDirty(true)
}
func (r *Room) UpdateParticipantMetadata(participant types.LocalParticipant, name string, metadata string) {
func (r *Room) UpdateParticipantMetadata(
participant types.LocalParticipant,
name string,
metadata string,
attributes map[string]string,
) error {
if attributes != nil && len(attributes) > 0 {
if err := participant.SetAttributes(attributes); err != nil {
return err
}
}
if metadata != "" {
participant.SetMetadata(metadata)
}
if name != "" {
participant.SetName(name)
}
return nil
}
func (r *Room) sendRoomUpdate() {
+9 -1
View File
@@ -93,7 +93,15 @@ func HandleParticipantSignal(room types.Room, participant types.LocalParticipant
case *livekit.SignalRequest_UpdateMetadata:
if participant.ClaimGrants().Video.GetCanUpdateOwnMetadata() {
room.UpdateParticipantMetadata(participant, msg.UpdateMetadata.Name, msg.UpdateMetadata.Metadata)
err := room.UpdateParticipantMetadata(
participant,
msg.UpdateMetadata.Name,
msg.UpdateMetadata.Metadata,
msg.UpdateMetadata.Attributes,
)
if err != nil {
pLogger.Warnw("could not update metadata", err)
}
}
case *livekit.SignalRequest_UpdateAudioTrack:
+6 -4
View File
@@ -255,9 +255,6 @@ type Participant interface {
CanSkipBroadcast() bool
ToProto() *livekit.ParticipantInfo
SetName(name string)
SetMetadata(metadata string)
IsPublisher() bool
GetPublishedTrack(trackID livekit.TrackID) MediaTrack
GetPublishedTracks() []MediaTrack
@@ -329,6 +326,11 @@ type LocalParticipant interface {
SetSignalSourceValid(valid bool)
HandleSignalSourceClose()
// updates
SetName(name string)
SetMetadata(metadata string)
SetAttributes(attributes map[string]string) error
// permissions
ClaimGrants() *auth.ClaimGrants
SetPermission(permission *livekit.ParticipantPermission) bool
@@ -437,7 +439,7 @@ type Room interface {
SimulateScenario(participant LocalParticipant, scenario *livekit.SimulateScenario) error
ResolveMediaTrackForSubscriber(subIdentity livekit.ParticipantIdentity, trackID livekit.TrackID) MediaResolverResult
GetLocalParticipants() []LocalParticipant
UpdateParticipantMetadata(participant LocalParticipant, name string, metadata string)
UpdateParticipantMetadata(participant LocalParticipant, name string, metadata string, attributes map[string]string) error
}
// MediaTrack represents a media track
@@ -733,6 +733,17 @@ type FakeLocalParticipant struct {
sendSpeakerUpdateReturnsOnCall map[int]struct {
result1 error
}
SetAttributesStub func(map[string]string) error
setAttributesMutex sync.RWMutex
setAttributesArgsForCall []struct {
arg1 map[string]string
}
setAttributesReturns struct {
result1 error
}
setAttributesReturnsOnCall map[int]struct {
result1 error
}
SetICEConfigStub func(*livekit.ICEConfig)
setICEConfigMutex sync.RWMutex
setICEConfigArgsForCall []struct {
@@ -4881,6 +4892,67 @@ func (fake *FakeLocalParticipant) SendSpeakerUpdateReturnsOnCall(i int, result1
}{result1}
}
func (fake *FakeLocalParticipant) SetAttributes(arg1 map[string]string) error {
fake.setAttributesMutex.Lock()
ret, specificReturn := fake.setAttributesReturnsOnCall[len(fake.setAttributesArgsForCall)]
fake.setAttributesArgsForCall = append(fake.setAttributesArgsForCall, struct {
arg1 map[string]string
}{arg1})
stub := fake.SetAttributesStub
fakeReturns := fake.setAttributesReturns
fake.recordInvocation("SetAttributes", []interface{}{arg1})
fake.setAttributesMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) SetAttributesCallCount() int {
fake.setAttributesMutex.RLock()
defer fake.setAttributesMutex.RUnlock()
return len(fake.setAttributesArgsForCall)
}
func (fake *FakeLocalParticipant) SetAttributesCalls(stub func(map[string]string) error) {
fake.setAttributesMutex.Lock()
defer fake.setAttributesMutex.Unlock()
fake.SetAttributesStub = stub
}
func (fake *FakeLocalParticipant) SetAttributesArgsForCall(i int) map[string]string {
fake.setAttributesMutex.RLock()
defer fake.setAttributesMutex.RUnlock()
argsForCall := fake.setAttributesArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeLocalParticipant) SetAttributesReturns(result1 error) {
fake.setAttributesMutex.Lock()
defer fake.setAttributesMutex.Unlock()
fake.SetAttributesStub = nil
fake.setAttributesReturns = struct {
result1 error
}{result1}
}
func (fake *FakeLocalParticipant) SetAttributesReturnsOnCall(i int, result1 error) {
fake.setAttributesMutex.Lock()
defer fake.setAttributesMutex.Unlock()
fake.SetAttributesStub = nil
if fake.setAttributesReturnsOnCall == nil {
fake.setAttributesReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.setAttributesReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeLocalParticipant) SetICEConfig(arg1 *livekit.ICEConfig) {
fake.setICEConfigMutex.Lock()
fake.setICEConfigArgsForCall = append(fake.setICEConfigArgsForCall, struct {
@@ -6571,6 +6643,8 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
defer fake.sendRoomUpdateMutex.RUnlock()
fake.sendSpeakerUpdateMutex.RLock()
defer fake.sendSpeakerUpdateMutex.RUnlock()
fake.setAttributesMutex.RLock()
defer fake.setAttributesMutex.RUnlock()
fake.setICEConfigMutex.RLock()
defer fake.setICEConfigMutex.RUnlock()
fake.setMetadataMutex.RLock()
@@ -175,16 +175,6 @@ type FakeParticipant struct {
arg2 bool
arg3 bool
}
SetMetadataStub func(string)
setMetadataMutex sync.RWMutex
setMetadataArgsForCall []struct {
arg1 string
}
SetNameStub func(string)
setNameMutex sync.RWMutex
setNameArgsForCall []struct {
arg1 string
}
StateStub func() livekit.ParticipantInfo_State
stateMutex sync.RWMutex
stateArgsForCall []struct {
@@ -1115,70 +1105,6 @@ func (fake *FakeParticipant) RemovePublishedTrackArgsForCall(i int) (types.Media
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
}
func (fake *FakeParticipant) SetMetadata(arg1 string) {
fake.setMetadataMutex.Lock()
fake.setMetadataArgsForCall = append(fake.setMetadataArgsForCall, struct {
arg1 string
}{arg1})
stub := fake.SetMetadataStub
fake.recordInvocation("SetMetadata", []interface{}{arg1})
fake.setMetadataMutex.Unlock()
if stub != nil {
fake.SetMetadataStub(arg1)
}
}
func (fake *FakeParticipant) SetMetadataCallCount() int {
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
return len(fake.setMetadataArgsForCall)
}
func (fake *FakeParticipant) SetMetadataCalls(stub func(string)) {
fake.setMetadataMutex.Lock()
defer fake.setMetadataMutex.Unlock()
fake.SetMetadataStub = stub
}
func (fake *FakeParticipant) SetMetadataArgsForCall(i int) string {
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
argsForCall := fake.setMetadataArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) SetName(arg1 string) {
fake.setNameMutex.Lock()
fake.setNameArgsForCall = append(fake.setNameArgsForCall, struct {
arg1 string
}{arg1})
stub := fake.SetNameStub
fake.recordInvocation("SetName", []interface{}{arg1})
fake.setNameMutex.Unlock()
if stub != nil {
fake.SetNameStub(arg1)
}
}
func (fake *FakeParticipant) SetNameCallCount() int {
fake.setNameMutex.RLock()
defer fake.setNameMutex.RUnlock()
return len(fake.setNameArgsForCall)
}
func (fake *FakeParticipant) SetNameCalls(stub func(string)) {
fake.setNameMutex.Lock()
defer fake.setNameMutex.Unlock()
fake.SetNameStub = stub
}
func (fake *FakeParticipant) SetNameArgsForCall(i int) string {
fake.setNameMutex.RLock()
defer fake.setNameMutex.RUnlock()
argsForCall := fake.setNameArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) State() livekit.ParticipantInfo_State {
fake.stateMutex.Lock()
ret, specificReturn := fake.stateReturnsOnCall[len(fake.stateArgsForCall)]
@@ -1561,10 +1487,6 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.kindMutex.RUnlock()
fake.removePublishedTrackMutex.RLock()
defer fake.removePublishedTrackMutex.RUnlock()
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
fake.setNameMutex.RLock()
defer fake.setNameMutex.RUnlock()
fake.stateMutex.RLock()
defer fake.stateMutex.RUnlock()
fake.subscriptionPermissionMutex.RLock()
+45 -8
View File
@@ -82,12 +82,19 @@ type FakeRoom struct {
syncStateReturnsOnCall map[int]struct {
result1 error
}
UpdateParticipantMetadataStub func(types.LocalParticipant, string, string)
UpdateParticipantMetadataStub func(types.LocalParticipant, string, string, map[string]string) error
updateParticipantMetadataMutex sync.RWMutex
updateParticipantMetadataArgsForCall []struct {
arg1 types.LocalParticipant
arg2 string
arg3 string
arg4 map[string]string
}
updateParticipantMetadataReturns struct {
result1 error
}
updateParticipantMetadataReturnsOnCall map[int]struct {
result1 error
}
UpdateSubscriptionPermissionStub func(types.LocalParticipant, *livekit.SubscriptionPermission) error
updateSubscriptionPermissionMutex sync.RWMutex
@@ -492,19 +499,26 @@ func (fake *FakeRoom) SyncStateReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeRoom) UpdateParticipantMetadata(arg1 types.LocalParticipant, arg2 string, arg3 string) {
func (fake *FakeRoom) UpdateParticipantMetadata(arg1 types.LocalParticipant, arg2 string, arg3 string, arg4 map[string]string) error {
fake.updateParticipantMetadataMutex.Lock()
ret, specificReturn := fake.updateParticipantMetadataReturnsOnCall[len(fake.updateParticipantMetadataArgsForCall)]
fake.updateParticipantMetadataArgsForCall = append(fake.updateParticipantMetadataArgsForCall, struct {
arg1 types.LocalParticipant
arg2 string
arg3 string
}{arg1, arg2, arg3})
arg4 map[string]string
}{arg1, arg2, arg3, arg4})
stub := fake.UpdateParticipantMetadataStub
fake.recordInvocation("UpdateParticipantMetadata", []interface{}{arg1, arg2, arg3})
fakeReturns := fake.updateParticipantMetadataReturns
fake.recordInvocation("UpdateParticipantMetadata", []interface{}{arg1, arg2, arg3, arg4})
fake.updateParticipantMetadataMutex.Unlock()
if stub != nil {
fake.UpdateParticipantMetadataStub(arg1, arg2, arg3)
return stub(arg1, arg2, arg3, arg4)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeRoom) UpdateParticipantMetadataCallCount() int {
@@ -513,17 +527,40 @@ func (fake *FakeRoom) UpdateParticipantMetadataCallCount() int {
return len(fake.updateParticipantMetadataArgsForCall)
}
func (fake *FakeRoom) UpdateParticipantMetadataCalls(stub func(types.LocalParticipant, string, string)) {
func (fake *FakeRoom) UpdateParticipantMetadataCalls(stub func(types.LocalParticipant, string, string, map[string]string) error) {
fake.updateParticipantMetadataMutex.Lock()
defer fake.updateParticipantMetadataMutex.Unlock()
fake.UpdateParticipantMetadataStub = stub
}
func (fake *FakeRoom) UpdateParticipantMetadataArgsForCall(i int) (types.LocalParticipant, string, string) {
func (fake *FakeRoom) UpdateParticipantMetadataArgsForCall(i int) (types.LocalParticipant, string, string, map[string]string) {
fake.updateParticipantMetadataMutex.RLock()
defer fake.updateParticipantMetadataMutex.RUnlock()
argsForCall := fake.updateParticipantMetadataArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
}
func (fake *FakeRoom) UpdateParticipantMetadataReturns(result1 error) {
fake.updateParticipantMetadataMutex.Lock()
defer fake.updateParticipantMetadataMutex.Unlock()
fake.UpdateParticipantMetadataStub = nil
fake.updateParticipantMetadataReturns = struct {
result1 error
}{result1}
}
func (fake *FakeRoom) UpdateParticipantMetadataReturnsOnCall(i int, result1 error) {
fake.updateParticipantMetadataMutex.Lock()
defer fake.updateParticipantMetadataMutex.Unlock()
fake.UpdateParticipantMetadataStub = nil
if fake.updateParticipantMetadataReturnsOnCall == nil {
fake.updateParticipantMetadataReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.updateParticipantMetadataReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeRoom) UpdateSubscriptionPermission(arg1 types.LocalParticipant, arg2 *livekit.SubscriptionPermission) error {