Add the ability to send data to specific participants (#34)

* UserPacket destination filter

* Updated protocol to v0.5.2

* Added DataChannel tests
This commit is contained in:
Théo Monnom
2021-06-09 12:14:14 -07:00
committed by GitHub
parent bbf7f83784
commit 321ebab99b
4 changed files with 78 additions and 1 deletions
+14
View File
@@ -391,6 +391,8 @@ func (r *Room) onParticipantMetadataUpdate(p types.Participant) {
}
func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
dest := dp.GetUser().GetDestinationSids()
for _, op := range r.GetParticipants() {
if op.State() != livekit.ParticipantInfo_ACTIVE {
continue
@@ -398,6 +400,18 @@ func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
if op.ID() == source.ID() {
continue
}
if len(dest) > 0 {
found := false
for _, dSid := range dest {
if op.ID() == dSid {
found = true
break
}
}
if !found {
continue
}
}
_ = op.SendDataPacket(dp)
}
}
+61
View File
@@ -335,6 +335,67 @@ func TestActiveSpeakers(t *testing.T) {
})
}
func TestDataChannel(t *testing.T) {
t.Parallel()
t.Run("participants should receive data", func(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: 3})
participants := rm.GetParticipants()
p := participants[0].(*typesfakes.FakeParticipant)
packet := livekit.DataPacket{
Kind: livekit.DataPacket_RELIABLE,
Value: &livekit.DataPacket_User{
User: &livekit.UserPacket{
ParticipantSid: p.ID(),
Payload: []byte("message.."),
},
},
}
p.OnDataPacketArgsForCall(0)(p, &packet)
// ensure everyone has received the packet
for _, op := range participants {
fp := op.(*typesfakes.FakeParticipant)
if fp == p {
require.Zero(t, fp.SendDataPacketCallCount())
continue
}
require.Equal(t, 1, fp.SendDataPacketCallCount())
require.Equal(t, packet.Value, fp.SendDataPacketArgsForCall(0).Value)
}
})
t.Run("only one participant should receive the data", func(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: 4})
participants := rm.GetParticipants()
p := participants[0].(*typesfakes.FakeParticipant)
p1 := participants[1].(*typesfakes.FakeParticipant)
packet := livekit.DataPacket{
Kind: livekit.DataPacket_RELIABLE,
Value: &livekit.DataPacket_User{
User: &livekit.UserPacket{
ParticipantSid: p.ID(),
Payload: []byte("message to p1.."),
DestinationSids: []string{p1.ID()},
},
},
}
p.OnDataPacketArgsForCall(0)(p, &packet)
// only p1 should receive the data
for _, op := range participants {
fp := op.(*typesfakes.FakeParticipant)
if fp != p1 {
require.Zero(t, fp.SendDataPacketCallCount())
}
}
require.Equal(t, 1, p1.SendDataPacketCallCount())
require.Equal(t, packet.Value, p1.SendDataPacketArgsForCall(0).Value)
})
}
type testRoomOpts struct {
num int
protocol types.ProtocolVersion