Room Metadata Implementation (#126)
This commit is contained in:
@@ -578,6 +578,16 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket) error {
|
||||
return dc.Send(data)
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) SendRoomUpdate(room *livekit.Room) error {
|
||||
return p.writeMessage(&livekit.SignalResponse{
|
||||
Message: &livekit.SignalResponse_RoomUpdate{
|
||||
RoomUpdate: &livekit.RoomUpdate{
|
||||
Room: room,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool, fromAdmin bool) {
|
||||
isPending := false
|
||||
p.lock.RLock()
|
||||
|
||||
@@ -47,6 +47,7 @@ type Room struct {
|
||||
statsReporter *stats.RoomStatsReporter
|
||||
|
||||
onParticipantChanged func(p types.Participant)
|
||||
onMetadataUpdate func(metadata string)
|
||||
onClose func()
|
||||
}
|
||||
|
||||
@@ -73,6 +74,7 @@ func NewRoom(room *livekit.Room, config WebRTCConfig, iceServers []*livekit.ICES
|
||||
}
|
||||
r.statsReporter.RoomStarted()
|
||||
go r.audioUpdateWorker()
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -374,6 +376,30 @@ func (r *Room) SendDataPacket(up *livekit.UserPacket, kind livekit.DataPacket_Ki
|
||||
r.onDataPacket(nil, dp)
|
||||
}
|
||||
|
||||
func (r *Room) SetMetadata(metadata string) {
|
||||
r.Room.Metadata = metadata
|
||||
|
||||
// Send update to participants
|
||||
for _, p := range r.GetParticipants() {
|
||||
if !p.IsReady() {
|
||||
continue
|
||||
}
|
||||
|
||||
err := p.SendRoomUpdate(r.Room)
|
||||
if err != nil {
|
||||
logger.Warnw("failed to send room update", err, "room", r.Room.Name, "participant", p.Identity())
|
||||
}
|
||||
}
|
||||
|
||||
if r.onMetadataUpdate != nil {
|
||||
r.onMetadataUpdate(metadata)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) OnMetadataUpdate(f func(metadata string)) {
|
||||
r.onMetadataUpdate = f
|
||||
}
|
||||
|
||||
// checks if participant should be autosubscribed to new tracks, assumes lock is already acquired
|
||||
func (r *Room) autoSubscribe(participant types.Participant) bool {
|
||||
if !participant.CanSubscribe() {
|
||||
|
||||
@@ -520,6 +520,20 @@ func TestHiddenParticipants(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestRoomUpdate(t *testing.T) {
|
||||
t.Run("participants should receive metadata update", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, testRoomOpts{num: 2})
|
||||
defer rm.Close()
|
||||
|
||||
rm.SetMetadata("test metadata...")
|
||||
|
||||
for _, op := range rm.GetParticipants() {
|
||||
fp := op.(*typesfakes.FakeParticipant)
|
||||
require.Equal(t, 1, fp.SendRoomUpdateCallCount())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type testRoomOpts struct {
|
||||
num int
|
||||
numHidden int
|
||||
@@ -548,6 +562,7 @@ func newRoomWithParticipants(t *testing.T, opts testRoomOpts) *rtc.Room {
|
||||
participant := newMockParticipant(identity, opts.protocol, i >= opts.num)
|
||||
err := rm.Join(participant, &rtc.ParticipantOptions{AutoSubscribe: true})
|
||||
participant.StateReturns(livekit.ParticipantInfo_ACTIVE)
|
||||
participant.IsReadyReturns(true)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
return rm
|
||||
|
||||
@@ -50,6 +50,7 @@ type Participant interface {
|
||||
SendParticipantUpdate(participants []*livekit.ParticipantInfo) error
|
||||
SendSpeakerUpdate(speakers []*livekit.SpeakerInfo) error
|
||||
SendDataPacket(packet *livekit.DataPacket) error
|
||||
SendRoomUpdate(room *livekit.Room) error
|
||||
SetTrackMuted(trackId string, muted bool, fromAdmin bool)
|
||||
GetAudioLevel() (level uint8, active bool)
|
||||
|
||||
|
||||
@@ -325,6 +325,17 @@ type FakeParticipant struct {
|
||||
sendParticipantUpdateReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
SendRoomUpdateStub func(*livekit.Room) error
|
||||
sendRoomUpdateMutex sync.RWMutex
|
||||
sendRoomUpdateArgsForCall []struct {
|
||||
arg1 *livekit.Room
|
||||
}
|
||||
sendRoomUpdateReturns struct {
|
||||
result1 error
|
||||
}
|
||||
sendRoomUpdateReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
SendSpeakerUpdateStub func([]*livekit.SpeakerInfo) error
|
||||
sendSpeakerUpdateMutex sync.RWMutex
|
||||
sendSpeakerUpdateArgsForCall []struct {
|
||||
@@ -2117,6 +2128,67 @@ func (fake *FakeParticipant) SendParticipantUpdateReturnsOnCall(i int, result1 e
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdate(arg1 *livekit.Room) error {
|
||||
fake.sendRoomUpdateMutex.Lock()
|
||||
ret, specificReturn := fake.sendRoomUpdateReturnsOnCall[len(fake.sendRoomUpdateArgsForCall)]
|
||||
fake.sendRoomUpdateArgsForCall = append(fake.sendRoomUpdateArgsForCall, struct {
|
||||
arg1 *livekit.Room
|
||||
}{arg1})
|
||||
stub := fake.SendRoomUpdateStub
|
||||
fakeReturns := fake.sendRoomUpdateReturns
|
||||
fake.recordInvocation("SendRoomUpdate", []interface{}{arg1})
|
||||
fake.sendRoomUpdateMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub(arg1)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdateCallCount() int {
|
||||
fake.sendRoomUpdateMutex.RLock()
|
||||
defer fake.sendRoomUpdateMutex.RUnlock()
|
||||
return len(fake.sendRoomUpdateArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdateCalls(stub func(*livekit.Room) error) {
|
||||
fake.sendRoomUpdateMutex.Lock()
|
||||
defer fake.sendRoomUpdateMutex.Unlock()
|
||||
fake.SendRoomUpdateStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdateArgsForCall(i int) *livekit.Room {
|
||||
fake.sendRoomUpdateMutex.RLock()
|
||||
defer fake.sendRoomUpdateMutex.RUnlock()
|
||||
argsForCall := fake.sendRoomUpdateArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdateReturns(result1 error) {
|
||||
fake.sendRoomUpdateMutex.Lock()
|
||||
defer fake.sendRoomUpdateMutex.Unlock()
|
||||
fake.SendRoomUpdateStub = nil
|
||||
fake.sendRoomUpdateReturns = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendRoomUpdateReturnsOnCall(i int, result1 error) {
|
||||
fake.sendRoomUpdateMutex.Lock()
|
||||
defer fake.sendRoomUpdateMutex.Unlock()
|
||||
fake.SendRoomUpdateStub = nil
|
||||
if fake.sendRoomUpdateReturnsOnCall == nil {
|
||||
fake.sendRoomUpdateReturnsOnCall = make(map[int]struct {
|
||||
result1 error
|
||||
})
|
||||
}
|
||||
fake.sendRoomUpdateReturnsOnCall[i] = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SendSpeakerUpdate(arg1 []*livekit.SpeakerInfo) error {
|
||||
var arg1Copy []*livekit.SpeakerInfo
|
||||
if arg1 != nil {
|
||||
@@ -2675,6 +2747,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.sendJoinResponseMutex.RUnlock()
|
||||
fake.sendParticipantUpdateMutex.RLock()
|
||||
defer fake.sendParticipantUpdateMutex.RUnlock()
|
||||
fake.sendRoomUpdateMutex.RLock()
|
||||
defer fake.sendRoomUpdateMutex.RUnlock()
|
||||
fake.sendSpeakerUpdateMutex.RLock()
|
||||
defer fake.sendSpeakerUpdateMutex.RUnlock()
|
||||
fake.setMetadataMutex.RLock()
|
||||
|
||||
Reference in New Issue
Block a user