pass client defined metadata from JWT to participant (#17)

This commit is contained in:
David Zhao
2021-02-10 23:13:36 -08:00
committed by GitHub
parent 38140debdf
commit 538461d5dc
23 changed files with 286 additions and 81 deletions
+23 -2
View File
@@ -1,6 +1,7 @@
package rtc
import (
"encoding/json"
"io"
"sync"
"sync/atomic"
@@ -39,8 +40,10 @@ type ParticipantImpl struct {
isClosed utils.AtomicFlag
mediaEngine *webrtc.MediaEngine
identity string
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
// JSON encoded metadata to pass to clients
metadata string
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
// tracks the current participant is subscribed to, map of otherParticipantId => []DownTrack
subscribedTracks map[string][]types.SubscribedTrack
// publishedTracks that participant is publishing
@@ -162,6 +165,22 @@ func (p *ParticipantImpl) IsReady() bool {
return state == livekit.ParticipantInfo_JOINED || state == livekit.ParticipantInfo_ACTIVE
}
// attach metadata to the participant
func (p *ParticipantImpl) SetMetadata(metadata map[string]interface{}) error {
if metadata == nil {
p.metadata = ""
return nil
}
if data, err := json.Marshal(metadata); err != nil {
return err
} else {
p.metadata = string(data)
}
return nil
}
func (p *ParticipantImpl) RTCPChan() chan []rtcp.Packet {
return p.rtcpCh
}
@@ -170,6 +189,7 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
info := &livekit.ParticipantInfo{
Sid: p.id,
Identity: p.identity,
Metadata: p.metadata,
State: p.State(),
}
@@ -433,6 +453,7 @@ func (p *ParticipantImpl) SendParticipantUpdate(participants []*livekit.Particip
if !p.IsReady() {
return nil
}
return p.responseSink.WriteMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_Update{
Update: &livekit.ParticipantUpdate{
+1 -1
View File
@@ -181,7 +181,7 @@ func (r *Room) RemoveParticipant(identity string) {
if r.onParticipantChanged != nil {
r.onParticipantChanged(p)
}
go r.broadcastParticipantState(p)
r.broadcastParticipantState(p)
}
}
+7 -1
View File
@@ -9,6 +9,7 @@ import (
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -93,15 +94,20 @@ func TestRoomJoin(t *testing.T) {
t.Run("participant state change is broadcasted to others", func(t *testing.T) {
rm := newRoomWithParticipants(t, numParticipants)
var changedParticipant types.Participant
rm.OnParticipantChanged(func(participant types.Participant) {
changedParticipant = participant
})
participants := rm.GetParticipants()
p := participants[0].(*typesfakes.FakeParticipant)
disconnectedParticipant := participants[1].(*typesfakes.FakeParticipant)
disconnectedParticipant.StateReturns(livekit.ParticipantInfo_DISCONNECTED)
rm.RemoveParticipant(p.Identity())
p.OnStateChangeArgsForCall(0)(p, livekit.ParticipantInfo_ACTIVE)
time.Sleep(defaultDelay)
assert.Equal(t, p, changedParticipant)
numUpdates := 0
for _, op := range participants {
if op == p || op == disconnectedParticipant {
+1
View File
@@ -52,6 +52,7 @@ type Participant interface {
IsReady() bool
ToProto() *livekit.ParticipantInfo
RTCPChan() chan []rtcp.Packet
SetMetadata(metadata map[string]interface{}) error
GetResponseSink() routing.MessageSink
SetResponseSink(sink routing.MessageSink)
@@ -204,6 +204,17 @@ type FakeParticipant struct {
sendParticipantUpdateReturnsOnCall map[int]struct {
result1 error
}
SetMetadataStub func(map[string]interface{}) error
setMetadataMutex sync.RWMutex
setMetadataArgsForCall []struct {
arg1 map[string]interface{}
}
setMetadataReturns struct {
result1 error
}
setMetadataReturnsOnCall map[int]struct {
result1 error
}
SetResponseSinkStub func(routing.MessageSink)
setResponseSinkMutex sync.RWMutex
setResponseSinkArgsForCall []struct {
@@ -1310,6 +1321,67 @@ func (fake *FakeParticipant) SendParticipantUpdateReturnsOnCall(i int, result1 e
}{result1}
}
func (fake *FakeParticipant) SetMetadata(arg1 map[string]interface{}) error {
fake.setMetadataMutex.Lock()
ret, specificReturn := fake.setMetadataReturnsOnCall[len(fake.setMetadataArgsForCall)]
fake.setMetadataArgsForCall = append(fake.setMetadataArgsForCall, struct {
arg1 map[string]interface{}
}{arg1})
stub := fake.SetMetadataStub
fakeReturns := fake.setMetadataReturns
fake.recordInvocation("SetMetadata", []interface{}{arg1})
fake.setMetadataMutex.Unlock()
if stub != nil {
return stub(arg1)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) SetMetadataCallCount() int {
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
return len(fake.setMetadataArgsForCall)
}
func (fake *FakeParticipant) SetMetadataCalls(stub func(map[string]interface{}) error) {
fake.setMetadataMutex.Lock()
defer fake.setMetadataMutex.Unlock()
fake.SetMetadataStub = stub
}
func (fake *FakeParticipant) SetMetadataArgsForCall(i int) map[string]interface{} {
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
argsForCall := fake.setMetadataArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakeParticipant) SetMetadataReturns(result1 error) {
fake.setMetadataMutex.Lock()
defer fake.setMetadataMutex.Unlock()
fake.SetMetadataStub = nil
fake.setMetadataReturns = struct {
result1 error
}{result1}
}
func (fake *FakeParticipant) SetMetadataReturnsOnCall(i int, result1 error) {
fake.setMetadataMutex.Lock()
defer fake.setMetadataMutex.Unlock()
fake.SetMetadataStub = nil
if fake.setMetadataReturnsOnCall == nil {
fake.setMetadataReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.setMetadataReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeParticipant) SetResponseSink(arg1 routing.MessageSink) {
fake.setResponseSinkMutex.Lock()
fake.setResponseSinkArgsForCall = append(fake.setResponseSinkArgsForCall, struct {
@@ -1554,6 +1626,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.sendJoinResponseMutex.RUnlock()
fake.sendParticipantUpdateMutex.RLock()
defer fake.sendParticipantUpdateMutex.RUnlock()
fake.setMetadataMutex.RLock()
defer fake.setMetadataMutex.RUnlock()
fake.setResponseSinkMutex.RLock()
defer fake.setResponseSinkMutex.RUnlock()
fake.setTrackMutedMutex.RLock()