SendData API & publishData permission (#88)
* SendData Server API * SendData Server-API adjustments based on suggestions * Update proto version * enforce publishData permission * go mod tidy * fix go.mod * go mod tidy Co-authored-by: ChesterMing <89124853+ChesterMing@users.noreply.github.com>
This commit is contained in:
co-authored by
ChesterMing
parent
2ab37d8fcc
commit
bcf6e15e07
@@ -16,6 +16,7 @@ func newMockParticipant(identity string, protocol types.ProtocolVersion, hidden
|
||||
p.ProtocolVersionReturns(protocol)
|
||||
p.CanSubscribeReturns(true)
|
||||
p.CanPublishReturns(!hidden)
|
||||
p.CanPublishDataReturns(!hidden)
|
||||
p.HiddenReturns(hidden)
|
||||
|
||||
p.SetMetadataStub = func(m string) {
|
||||
|
||||
@@ -598,6 +598,10 @@ func (p *ParticipantImpl) CanSubscribe() bool {
|
||||
return p.permission == nil || p.permission.CanSubscribe
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) CanPublishData() bool {
|
||||
return p.permission == nil || p.permission.CanPublishData
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) Hidden() bool {
|
||||
return p.params.Hidden
|
||||
}
|
||||
|
||||
+15
-1
@@ -352,6 +352,16 @@ func (r *Room) OnParticipantChanged(f func(participant types.Participant)) {
|
||||
r.onParticipantChanged = f
|
||||
}
|
||||
|
||||
func (r *Room) SendDataPacket(up *livekit.UserPacket, kind livekit.DataPacket_Kind) {
|
||||
dp := &livekit.DataPacket{
|
||||
Kind: kind,
|
||||
Value: &livekit.DataPacket_User{
|
||||
User: up,
|
||||
},
|
||||
}
|
||||
r.onDataPacket(nil, dp)
|
||||
}
|
||||
|
||||
// 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() {
|
||||
@@ -422,13 +432,17 @@ func (r *Room) onParticipantMetadataUpdate(p types.Participant) {
|
||||
}
|
||||
|
||||
func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
|
||||
// don't forward if source isn't allowed to publish data
|
||||
if source != nil && !source.CanPublishData() {
|
||||
return
|
||||
}
|
||||
dest := dp.GetUser().GetDestinationSids()
|
||||
|
||||
for _, op := range r.GetParticipants() {
|
||||
if op.State() != livekit.ParticipantInfo_ACTIVE {
|
||||
continue
|
||||
}
|
||||
if op.ID() == source.ID() {
|
||||
if source != nil && op.ID() == source.ID() {
|
||||
continue
|
||||
}
|
||||
if len(dest) > 0 {
|
||||
|
||||
@@ -454,6 +454,30 @@ func TestDataChannel(t *testing.T) {
|
||||
require.Equal(t, 1, p1.SendDataPacketCallCount())
|
||||
require.Equal(t, packet.Value, p1.SendDataPacketArgsForCall(0).Value)
|
||||
})
|
||||
|
||||
t.Run("publishing disallowed", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, testRoomOpts{num: 2})
|
||||
defer rm.Close()
|
||||
participants := rm.GetParticipants()
|
||||
p := participants[0].(*typesfakes.FakeParticipant)
|
||||
p.CanPublishDataReturns(false)
|
||||
|
||||
packet := livekit.DataPacket{
|
||||
Kind: livekit.DataPacket_RELIABLE,
|
||||
Value: &livekit.DataPacket_User{
|
||||
User: &livekit.UserPacket{
|
||||
Payload: []byte{},
|
||||
},
|
||||
},
|
||||
}
|
||||
p.OnDataPacketArgsForCall(0)(p, &packet)
|
||||
|
||||
// no one should've been sent packet
|
||||
for _, op := range participants {
|
||||
fp := op.(*typesfakes.FakeParticipant)
|
||||
require.Zero(t, fp.SendDataPacketCallCount())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestHiddenParticipants(t *testing.T) {
|
||||
|
||||
@@ -57,6 +57,7 @@ type Participant interface {
|
||||
|
||||
CanPublish() bool
|
||||
CanSubscribe() bool
|
||||
CanPublishData() bool
|
||||
Hidden() bool
|
||||
|
||||
Start()
|
||||
|
||||
@@ -59,6 +59,16 @@ type FakeParticipant struct {
|
||||
canPublishReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
CanPublishDataStub func() bool
|
||||
canPublishDataMutex sync.RWMutex
|
||||
canPublishDataArgsForCall []struct {
|
||||
}
|
||||
canPublishDataReturns struct {
|
||||
result1 bool
|
||||
}
|
||||
canPublishDataReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
CanSubscribeStub func() bool
|
||||
canSubscribeMutex sync.RWMutex
|
||||
canSubscribeArgsForCall []struct {
|
||||
@@ -649,6 +659,59 @@ func (fake *FakeParticipant) CanPublishReturnsOnCall(i int, result1 bool) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanPublishData() bool {
|
||||
fake.canPublishDataMutex.Lock()
|
||||
ret, specificReturn := fake.canPublishDataReturnsOnCall[len(fake.canPublishDataArgsForCall)]
|
||||
fake.canPublishDataArgsForCall = append(fake.canPublishDataArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.CanPublishDataStub
|
||||
fakeReturns := fake.canPublishDataReturns
|
||||
fake.recordInvocation("CanPublishData", []interface{}{})
|
||||
fake.canPublishDataMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub()
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanPublishDataCallCount() int {
|
||||
fake.canPublishDataMutex.RLock()
|
||||
defer fake.canPublishDataMutex.RUnlock()
|
||||
return len(fake.canPublishDataArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanPublishDataCalls(stub func() bool) {
|
||||
fake.canPublishDataMutex.Lock()
|
||||
defer fake.canPublishDataMutex.Unlock()
|
||||
fake.CanPublishDataStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanPublishDataReturns(result1 bool) {
|
||||
fake.canPublishDataMutex.Lock()
|
||||
defer fake.canPublishDataMutex.Unlock()
|
||||
fake.CanPublishDataStub = nil
|
||||
fake.canPublishDataReturns = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanPublishDataReturnsOnCall(i int, result1 bool) {
|
||||
fake.canPublishDataMutex.Lock()
|
||||
defer fake.canPublishDataMutex.Unlock()
|
||||
fake.CanPublishDataStub = nil
|
||||
if fake.canPublishDataReturnsOnCall == nil {
|
||||
fake.canPublishDataReturnsOnCall = make(map[int]struct {
|
||||
result1 bool
|
||||
})
|
||||
}
|
||||
fake.canPublishDataReturnsOnCall[i] = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) CanSubscribe() bool {
|
||||
fake.canSubscribeMutex.Lock()
|
||||
ret, specificReturn := fake.canSubscribeReturnsOnCall[len(fake.canSubscribeArgsForCall)]
|
||||
@@ -2550,6 +2613,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.addTrackMutex.RUnlock()
|
||||
fake.canPublishMutex.RLock()
|
||||
defer fake.canPublishMutex.RUnlock()
|
||||
fake.canPublishDataMutex.RLock()
|
||||
defer fake.canPublishDataMutex.RUnlock()
|
||||
fake.canSubscribeMutex.RLock()
|
||||
defer fake.canSubscribeMutex.RUnlock()
|
||||
fake.closeMutex.RLock()
|
||||
|
||||
@@ -90,11 +90,6 @@ type FakePublishedTrack struct {
|
||||
setMutedArgsForCall []struct {
|
||||
arg1 bool
|
||||
}
|
||||
SetSimulcastLayersStub func([]livekit.VideoQuality)
|
||||
setSimulcastLayersMutex sync.RWMutex
|
||||
setSimulcastLayersArgsForCall []struct {
|
||||
arg1 []livekit.VideoQuality
|
||||
}
|
||||
StartStub func()
|
||||
startMutex sync.RWMutex
|
||||
startArgsForCall []struct {
|
||||
@@ -567,43 +562,6 @@ func (fake *FakePublishedTrack) SetMutedArgsForCall(i int) bool {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetSimulcastLayers(arg1 []livekit.VideoQuality) {
|
||||
var arg1Copy []livekit.VideoQuality
|
||||
if arg1 != nil {
|
||||
arg1Copy = make([]livekit.VideoQuality, len(arg1))
|
||||
copy(arg1Copy, arg1)
|
||||
}
|
||||
fake.setSimulcastLayersMutex.Lock()
|
||||
fake.setSimulcastLayersArgsForCall = append(fake.setSimulcastLayersArgsForCall, struct {
|
||||
arg1 []livekit.VideoQuality
|
||||
}{arg1Copy})
|
||||
stub := fake.SetSimulcastLayersStub
|
||||
fake.recordInvocation("SetSimulcastLayers", []interface{}{arg1Copy})
|
||||
fake.setSimulcastLayersMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.SetSimulcastLayersStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetSimulcastLayersCallCount() int {
|
||||
fake.setSimulcastLayersMutex.RLock()
|
||||
defer fake.setSimulcastLayersMutex.RUnlock()
|
||||
return len(fake.setSimulcastLayersArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetSimulcastLayersCalls(stub func([]livekit.VideoQuality)) {
|
||||
fake.setSimulcastLayersMutex.Lock()
|
||||
defer fake.setSimulcastLayersMutex.Unlock()
|
||||
fake.SetSimulcastLayersStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetSimulcastLayersArgsForCall(i int) []livekit.VideoQuality {
|
||||
fake.setSimulcastLayersMutex.RLock()
|
||||
defer fake.setSimulcastLayersMutex.RUnlock()
|
||||
argsForCall := fake.setSimulcastLayersArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) Start() {
|
||||
fake.startMutex.Lock()
|
||||
fake.startArgsForCall = append(fake.startArgsForCall, struct {
|
||||
@@ -704,8 +662,6 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
|
||||
defer fake.removeSubscriberMutex.RUnlock()
|
||||
fake.setMutedMutex.RLock()
|
||||
defer fake.setMutedMutex.RUnlock()
|
||||
fake.setSimulcastLayersMutex.RLock()
|
||||
defer fake.setSimulcastLayersMutex.RUnlock()
|
||||
fake.startMutex.RLock()
|
||||
defer fake.startMutex.RUnlock()
|
||||
fake.toProtoMutex.RLock()
|
||||
|
||||
Reference in New Issue
Block a user