ability to disable auto subscribe per participant

This commit is contained in:
David Zhao
2021-05-22 22:54:47 -07:00
parent 55d801d30b
commit 0900054c24
29 changed files with 236 additions and 7683 deletions
+4 -4
View File
@@ -3,13 +3,13 @@ package config
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig_UnmarshalKeys(t *testing.T) {
conf, err := NewConfig("")
assert.NoError(t, err)
require.NoError(t, err)
assert.NoError(t, conf.unmarshalKeys("key1: secret1"))
assert.Equal(t, "secret1", conf.Keys["key1"])
require.NoError(t, conf.unmarshalKeys("key1: secret1"))
require.Equal(t, "secret1", conf.Keys["key1"])
}
+1
View File
@@ -30,6 +30,7 @@ type ParticipantInit struct {
Permission *livekit.ParticipantPermission
ProtocolVersion int32
UsePlanB bool
AutoSubscribe bool
}
type NewParticipantCallback func(roomName string, pi ParticipantInit, requestSource MessageSource, responseSink MessageSink)
+2
View File
@@ -158,6 +158,7 @@ func (r *RedisRouter) StartParticipantSignal(roomName string, pi ParticipantInit
Permission: pi.Permission,
ProtocolVersion: pi.ProtocolVersion,
UsePlanB: pi.UsePlanB,
AutoSubscribe: pi.AutoSubscribe,
})
if err != nil {
return
@@ -225,6 +226,7 @@ func (r *RedisRouter) startParticipantRTC(ss *livekit.StartSession, participantK
Permission: ss.Permission,
ProtocolVersion: ss.ProtocolVersion,
UsePlanB: ss.UsePlanB,
AutoSubscribe: ss.AutoSubscribe,
}
reqChan := r.getOrCreateMessageChannel(r.requestChannels, participantKey)
+3 -4
View File
@@ -4,10 +4,9 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/routing"
livekit "github.com/livekit/livekit-server/proto"
"github.com/stretchr/testify/require"
)
func TestIsAvailable(t *testing.T) {
@@ -17,7 +16,7 @@ func TestIsAvailable(t *testing.T) {
UpdatedAt: time.Now().Unix() - 3,
},
}
assert.True(t, routing.IsAvailable(n))
require.True(t, routing.IsAvailable(n))
})
t.Run("expired", func(t *testing.T) {
@@ -26,6 +25,6 @@ func TestIsAvailable(t *testing.T) {
UpdatedAt: time.Now().Unix() - 20,
},
}
assert.False(t, routing.IsAvailable(n))
require.False(t, routing.IsAvailable(n))
})
}
+8 -9
View File
@@ -3,9 +3,8 @@ package rtc_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/stretchr/testify/require"
)
const (
@@ -19,11 +18,11 @@ func TestAudioLevel(t *testing.T) {
t.Run("initially to return not noisy, within a few samples", func(t *testing.T) {
a := rtc.NewAudioLevel(defaultActiveLevel, defaultPercentile)
_, noisy := a.GetLevel()
assert.False(t, noisy)
require.False(t, noisy)
observeSamples(a, 28, 5)
_, noisy = a.GetLevel()
assert.False(t, noisy)
require.False(t, noisy)
})
t.Run("not noisy when all samples are below threshold", func(t *testing.T) {
@@ -31,7 +30,7 @@ func TestAudioLevel(t *testing.T) {
observeSamples(a, 35, 100)
_, noisy := a.GetLevel()
assert.False(t, noisy)
require.False(t, noisy)
})
t.Run("not noisy when less than percentile samples are above threshold", func(t *testing.T) {
@@ -42,7 +41,7 @@ func TestAudioLevel(t *testing.T) {
observeSamples(a, 35, 1)
_, noisy := a.GetLevel()
assert.False(t, noisy)
require.False(t, noisy)
})
t.Run("noisy when higher than percentile samples are above threshold", func(t *testing.T) {
@@ -53,9 +52,9 @@ func TestAudioLevel(t *testing.T) {
observeSamples(a, 29, 2)
level, noisy := a.GetLevel()
assert.True(t, noisy)
assert.Less(t, level, uint8(defaultActiveLevel))
assert.Greater(t, level, uint8(25))
require.True(t, noisy)
require.Less(t, level, uint8(defaultActiveLevel))
require.Greater(t, level, uint8(25))
})
}
+2
View File
@@ -13,6 +13,8 @@ func newMockParticipant(identity string, protocol types.ProtocolVersion) *typesf
p.IdentityReturns(identity)
p.StateReturns(livekit.ParticipantInfo_JOINED)
p.ProtocolVersionReturns(protocol)
p.CanSubscribeReturns(true)
p.CanPublishReturns(true)
p.SetMetadataStub = func(m string) {
var f func(participant types.Participant)
+4
View File
@@ -115,6 +115,10 @@ func (t *MediaTrack) IsSubscriber(subId string) bool {
// AddSubscriber subscribes sub to current mediaTrack
func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
if !sub.CanSubscribe() {
return ErrPermissionDenied
}
t.lock.Lock()
defer t.lock.Unlock()
existingSt := t.subscribedTracks[sub.ID()]
+12 -13
View File
@@ -5,7 +5,6 @@ import (
"time"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/config"
@@ -43,7 +42,7 @@ func TestIsReady(t *testing.T) {
t.Run(test.state.String(), func(t *testing.T) {
p := &ParticipantImpl{}
p.state.Store(test.state)
assert.Equal(t, test.ready, p.IsReady())
require.Equal(t, test.ready, p.IsReady())
})
}
}
@@ -82,13 +81,13 @@ func TestTrackPublishing(t *testing.T) {
})
p.handleTrackPublished(track)
assert.True(t, published)
assert.False(t, updated)
assert.Len(t, p.publishedTracks, 1)
require.True(t, published)
require.False(t, updated)
require.Len(t, p.publishedTracks, 1)
track.OnCloseArgsForCall(0)()
assert.Len(t, p.publishedTracks, 0)
assert.True(t, updated)
require.Len(t, p.publishedTracks, 0)
require.True(t, updated)
})
t.Run("sends back trackPublished event", func(t *testing.T) {
@@ -97,13 +96,13 @@ func TestTrackPublishing(t *testing.T) {
//track.IDReturns("id")
sink := p.params.Sink.(*routingfakes.FakeMessageSink)
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
assert.Equal(t, 1, sink.WriteMessageCallCount())
require.Equal(t, 1, sink.WriteMessageCallCount())
res := sink.WriteMessageArgsForCall(0).(*livekit.SignalResponse)
assert.IsType(t, &livekit.SignalResponse_TrackPublished{}, res.Message)
require.IsType(t, &livekit.SignalResponse_TrackPublished{}, res.Message)
published := res.Message.(*livekit.SignalResponse_TrackPublished).TrackPublished
assert.Equal(t, "cid", published.Cid)
assert.Equal(t, "webcam", published.Track.Name)
assert.Equal(t, livekit.TrackType_VIDEO, published.Track.Type)
require.Equal(t, "cid", published.Cid)
require.Equal(t, "webcam", published.Track.Name)
require.Equal(t, livekit.TrackType_VIDEO, published.Track.Type)
})
t.Run("should not allow adding of duplicate tracks", func(t *testing.T) {
@@ -114,7 +113,7 @@ func TestTrackPublishing(t *testing.T) {
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
p.AddTrack("cid", "duplicate", livekit.TrackType_AUDIO)
assert.Equal(t, 1, sink.WriteMessageCallCount())
require.Equal(t, 1, sink.WriteMessageCallCount())
})
}
+34 -2
View File
@@ -23,7 +23,9 @@ type Room struct {
iceServers []*livekit.ICEServer
lock sync.RWMutex
// map of identity -> Participant
participants map[string]types.Participant
participants map[string]types.Participant
participantOpts map[string]*ParticipantOptions
// time the first participant joined the room
joinedAt atomic.Value
// time that the last participant left the room
@@ -40,6 +42,10 @@ type Room struct {
onClose func()
}
type ParticipantOptions struct {
AutoSubscribe bool
}
func NewRoom(room *livekit.Room, config WebRTCConfig, iceServers []*livekit.ICEServer, audioUpdateInterval uint32) *Room {
r := &Room{
Room: *room,
@@ -49,6 +55,7 @@ func NewRoom(room *livekit.Room, config WebRTCConfig, iceServers []*livekit.ICES
lock: sync.RWMutex{},
statsReporter: NewRoomStatsReporter(room.Name),
participants: make(map[string]types.Participant),
participantOpts: make(map[string]*ParticipantOptions),
}
if r.EmptyTimeout == 0 {
r.EmptyTimeout = DefaultEmptyTimeout
@@ -118,7 +125,7 @@ func (r *Room) LastLeftAt() int64 {
return 0
}
func (r *Room) Join(participant types.Participant) error {
func (r *Room) Join(participant types.Participant, opts *ParticipantOptions) error {
if r.isClosed.Get() {
return ErrRoomClosed
}
@@ -171,6 +178,7 @@ func (r *Room) Join(participant types.Participant) error {
"roomId", r.Sid)
r.participants[participant.Identity()] = participant
r.participantOpts[participant.Identity()] = opts
// gather other participants and send join response
otherParticipants := make([]types.Participant, 0, len(r.participants))
@@ -192,6 +200,7 @@ func (r *Room) RemoveParticipant(identity string) {
p, ok := r.participants[identity]
if ok {
delete(r.participants, identity)
delete(r.participantOpts, identity)
}
r.lock.Unlock()
if !ok {
@@ -309,6 +318,22 @@ func (r *Room) OnParticipantChanged(f func(participant types.Participant)) {
r.onParticipantChanged = f
}
func (r *Room) autoSubscribe(participant types.Participant) bool {
if !participant.CanSubscribe() {
return false
}
r.lock.RLock()
defer r.lock.RUnlock()
opts := r.participantOpts[participant.Identity()]
// default to true if no options are set
if opts != nil && !opts.AutoSubscribe {
return false
}
return true
}
// a ParticipantImpl in the room added a new remoteTrack, subscribe other participants to it
func (r *Room) onTrackAdded(participant types.Participant, track types.PublishedTrack) {
// publish participant update, since track state is changed
@@ -328,6 +353,9 @@ func (r *Room) onTrackAdded(participant types.Participant, track types.Published
// not fully joined. don't subscribe yet
continue
}
if !r.autoSubscribe(existingParticipant) {
continue
}
logger.Debugw("subscribing to new track",
"source", participant.Identity(),
@@ -374,6 +402,10 @@ func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
}
func (r *Room) subscribeToExistingTracks(p types.Participant) {
if !r.autoSubscribe(p) {
return
}
tracksAdded := 0
for _, op := range r.GetParticipants() {
if p.ID() == op.ID() {
+38 -39
View File
@@ -5,7 +5,6 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/logger"
@@ -28,15 +27,15 @@ func init() {
func TestJoinedState(t *testing.T) {
t.Run("new room should return joinedAt 0", func(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: 0})
assert.Equal(t, int64(0), rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
require.Equal(t, int64(0), rm.FirstJoinedAt())
require.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be current time when a participant joins", func(t *testing.T) {
s := time.Now().Unix()
rm := newRoomWithParticipants(t, testRoomOpts{num: 1})
assert.Equal(t, s, rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
require.Equal(t, s, rm.FirstJoinedAt())
require.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be set when a participant leaves", func(t *testing.T) {
@@ -44,14 +43,14 @@ func TestJoinedState(t *testing.T) {
p0 := rm.GetParticipants()[0]
s := time.Now().Unix()
rm.RemoveParticipant(p0.Identity())
assert.Equal(t, s, rm.LastLeftAt())
require.Equal(t, s, rm.LastLeftAt())
})
t.Run("LastLeftAt should not be set when there are still participants in the room", func(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: 2})
p0 := rm.GetParticipants()[0]
rm.RemoveParticipant(p0.Identity())
assert.EqualValues(t, 0, rm.LastLeftAt())
require.EqualValues(t, 0, rm.LastLeftAt())
})
}
@@ -60,13 +59,13 @@ func TestRoomJoin(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: numParticipants})
pNew := newMockParticipant("new", types.DefaultProtocol)
rm.Join(pNew)
rm.Join(pNew, nil)
// expect new participant to get a JoinReply
info, participants, iceServers := pNew.SendJoinResponseArgsForCall(0)
assert.Equal(t, info.Sid, rm.Sid)
assert.Len(t, participants, numParticipants)
assert.Len(t, rm.GetParticipants(), numParticipants+1)
require.Equal(t, info.Sid, rm.Sid)
require.Len(t, participants, numParticipants)
require.Len(t, rm.GetParticipants(), numParticipants+1)
require.NotEmpty(t, iceServers)
})
@@ -75,11 +74,11 @@ func TestRoomJoin(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: numExisting})
p := newMockParticipant("new", types.DefaultProtocol)
err := rm.Join(p)
assert.NoError(t, err)
err := rm.Join(p, &rtc.ParticipantOptions{AutoSubscribe: true})
require.NoError(t, err)
stateChangeCB := p.OnStateChangeArgsForCall(0)
assert.NotNil(t, stateChangeCB)
require.NotNil(t, stateChangeCB)
p.StateReturns(livekit.ParticipantInfo_ACTIVE)
stateChangeCB(p, livekit.ParticipantInfo_JOINED)
@@ -89,9 +88,9 @@ func TestRoomJoin(t *testing.T) {
continue
}
mockP := op.(*typesfakes.FakeParticipant)
assert.NotZero(t, mockP.AddSubscriberCallCount())
require.NotZero(t, mockP.AddSubscriberCallCount())
// last call should be to add the newest participant
assert.Equal(t, p, mockP.AddSubscriberArgsForCall(mockP.AddSubscriberCallCount()-1))
require.Equal(t, p, mockP.AddSubscriberArgsForCall(mockP.AddSubscriberCallCount()-1))
}
})
@@ -109,19 +108,19 @@ func TestRoomJoin(t *testing.T) {
rm.RemoveParticipant(p.Identity())
time.Sleep(defaultDelay)
assert.Equal(t, p, changedParticipant)
require.Equal(t, p, changedParticipant)
numUpdates := 0
for _, op := range participants {
if op == p || op == disconnectedParticipant {
assert.Zero(t, p.SendParticipantUpdateCallCount())
require.Zero(t, p.SendParticipantUpdateCallCount())
continue
}
fakeP := op.(*typesfakes.FakeParticipant)
assert.Equal(t, 1, fakeP.SendParticipantUpdateCallCount())
require.Equal(t, 1, fakeP.SendParticipantUpdateCallCount())
numUpdates += 1
}
assert.Equal(t, numParticipants-2, numUpdates)
require.Equal(t, numParticipants-2, numUpdates)
})
t.Run("cannot exceed max participants", func(t *testing.T) {
@@ -129,8 +128,8 @@ func TestRoomJoin(t *testing.T) {
rm.MaxParticipants = 1
p := newMockParticipant("second", types.ProtocolVersion(0))
err := rm.Join(p)
assert.Equal(t, rtc.ErrMaxParticipantsExceeded, err)
err := rm.Join(p, nil)
require.Equal(t, rtc.ErrMaxParticipantsExceeded, err)
})
}
@@ -205,10 +204,10 @@ func TestRoomClosure(t *testing.T) {
time.Sleep(defaultDelay)
rm.CloseIfEmpty()
assert.Len(t, rm.GetParticipants(), 0)
assert.True(t, isClosed)
require.Len(t, rm.GetParticipants(), 0)
require.True(t, isClosed)
assert.Equal(t, rtc.ErrRoomClosed, rm.Join(p))
require.Equal(t, rtc.ErrRoomClosed, rm.Join(p, nil))
})
t.Run("room does not close before empty timeout", func(t *testing.T) {
@@ -217,9 +216,9 @@ func TestRoomClosure(t *testing.T) {
rm.OnClose(func() {
isClosed = true
})
assert.NotZero(t, rm.EmptyTimeout)
require.NotZero(t, rm.EmptyTimeout)
rm.CloseIfEmpty()
assert.False(t, isClosed)
require.False(t, isClosed)
})
t.Run("room closes after empty timeout", func(t *testing.T) {
@@ -232,7 +231,7 @@ func TestRoomClosure(t *testing.T) {
time.Sleep(1010 * time.Millisecond)
rm.CloseIfEmpty()
assert.True(t, isClosed)
require.True(t, isClosed)
})
}
@@ -250,11 +249,11 @@ func TestNewTrack(t *testing.T) {
// p3 adds track
track := newMockTrack(livekit.TrackType_VIDEO, "webcam")
trackCB := pub.OnTrackPublishedArgsForCall(0)
assert.NotNil(t, trackCB)
require.NotNil(t, trackCB)
trackCB(pub, track)
// only p2 should've been called
assert.Equal(t, 1, track.AddSubscriberCallCount())
assert.Equal(t, p1, track.AddSubscriberArgsForCall(0))
require.Equal(t, 1, track.AddSubscriberCallCount())
require.Equal(t, p1, track.AddSubscriberArgsForCall(0))
})
}
@@ -273,16 +272,16 @@ func TestActiveSpeakers(t *testing.T) {
return updates
}
audioUpdateDuration := (audioUpdateInterval + 2) * time.Millisecond
audioUpdateDuration := (audioUpdateInterval + 10) * time.Millisecond
t.Run("participant should not be getting audio updates (protocol 2)", func(t *testing.T) {
rm := newRoomWithParticipants(t, testRoomOpts{num: 1, protocol: types.DefaultProtocol})
p := rm.GetParticipants()[0].(*typesfakes.FakeParticipant)
assert.Empty(t, rm.GetActiveSpeakers())
require.Empty(t, rm.GetActiveSpeakers())
time.Sleep(audioUpdateDuration)
updates := getActiveSpeakerUpdates(p)
assert.Empty(t, updates)
require.Empty(t, updates)
})
t.Run("speakers should be sorted by loudness (protocol 0)", func(t *testing.T) {
@@ -294,9 +293,9 @@ func TestActiveSpeakers(t *testing.T) {
p2.GetAudioLevelReturns(20, true)
speakers := rm.GetActiveSpeakers()
assert.Len(t, speakers, 2)
assert.Equal(t, p.ID(), speakers[0].Sid)
assert.Equal(t, p2.ID(), speakers[1].Sid)
require.Len(t, speakers, 2)
require.Equal(t, p.ID(), speakers[0].Sid)
require.Equal(t, p2.ID(), speakers[1].Sid)
})
t.Run("participants are getting audio updates (protocol 2)", func(t *testing.T) {
@@ -357,9 +356,9 @@ func newRoomWithParticipants(t *testing.T, opts testRoomOpts) *rtc.Room {
for i := 0; i < opts.num; i++ {
identity := fmt.Sprintf("p%d", i)
participant := newMockParticipant(identity, opts.protocol)
err := rm.Join(participant)
err := rm.Join(participant, &rtc.ParticipantOptions{AutoSubscribe: true})
participant.StateReturns(livekit.ParticipantInfo_ACTIVE)
assert.NoError(t, err)
require.NoError(t, err)
}
return rm
}
+1 -1
View File
@@ -62,7 +62,7 @@ func TestMissingAnswerDuringICERestart(t *testing.T) {
ICERestart: true,
}))
time.Sleep(50 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
require.Equal(t, webrtc.ICEConnectionStateConnected, transportA.pc.ICEConnectionState())
require.Equal(t, webrtc.ICEConnectionStateConnected, transportB.pc.ICEConnectionState())
}
+3
View File
@@ -54,6 +54,7 @@ type Participant interface {
GetAudioLevel() (level uint8, noisy bool)
// permissions
CanPublish() bool
CanSubscribe() bool
@@ -61,6 +62,7 @@ type Participant interface {
Close() error
// callbacks
OnStateChange(func(p Participant, oldState livekit.ParticipantInfo_State))
// OnTrackPublished - remote added a remoteTrack
OnTrackPublished(func(Participant, PublishedTrack))
@@ -71,6 +73,7 @@ type Participant interface {
OnClose(func(Participant))
// package methods
AddSubscribedTrack(participantId string, st SubscribedTrack)
RemoveSubscribedTrack(participantId string, st SubscribedTrack)
SubscriberPC() *webrtc.PeerConnection
+8 -8
View File
@@ -3,16 +3,16 @@ package rtc
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPackStreamId(t *testing.T) {
packed := "PA_123abc|uuid-id"
pId, trackId := UnpackStreamID(packed)
assert.Equal(t, "PA_123abc", pId)
assert.Equal(t, "uuid-id", trackId)
require.Equal(t, "PA_123abc", pId)
require.Equal(t, "uuid-id", trackId)
assert.Equal(t, packed, PackStreamID(pId, trackId))
require.Equal(t, packed, PackStreamID(pId, trackId))
}
func TestPackDataTrackLabel(t *testing.T) {
@@ -20,10 +20,10 @@ func TestPackDataTrackLabel(t *testing.T) {
trackId := "TR_b3da25"
label := "trackLabel"
packed := "PA_123abc|TR_b3da25|trackLabel"
assert.Equal(t, packed, PackDataTrackLabel(pId, trackId, label))
require.Equal(t, packed, PackDataTrackLabel(pId, trackId, label))
p, tr, l := UnpackDataTrackLabel(packed)
assert.Equal(t, pId, p)
assert.Equal(t, trackId, tr)
assert.Equal(t, label, l)
require.Equal(t, pId, p)
require.Equal(t, trackId, tr)
require.Equal(t, label, l)
}
+8 -9
View File
@@ -5,11 +5,10 @@ import (
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/service"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/auth/authfakes"
"github.com/stretchr/testify/require"
)
func TestAuthMiddleware(t *testing.T) {
@@ -30,23 +29,23 @@ func TestAuthMiddleware(t *testing.T) {
at := auth.NewAccessToken(api, secret).
AddGrant(orig)
token, err := at.ToJWT()
assert.NoError(t, err)
require.NoError(t, err)
r := &http.Request{Header: http.Header{}}
w := httptest.NewRecorder()
service.SetAuthorizationToken(r, token)
m.ServeHTTP(w, r, handler)
assert.NotNil(t, grants)
assert.EqualValues(t, orig, grants.Video)
require.NotNil(t, grants)
require.EqualValues(t, orig, grants.Video)
// no authorization == no claims
grants = nil
w = httptest.NewRecorder()
r = &http.Request{Header: http.Header{}}
m.ServeHTTP(w, r, handler)
assert.Nil(t, grants)
assert.Equal(t, http.StatusOK, w.Code)
require.Nil(t, grants)
require.Equal(t, http.StatusOK, w.Code)
// incorrect authorization: error
grants = nil
@@ -54,6 +53,6 @@ func TestAuthMiddleware(t *testing.T) {
r = &http.Request{Header: http.Header{}}
service.SetAuthorizationToken(r, "invalid token")
m.ServeHTTP(w, r, handler)
assert.Nil(t, grants)
assert.Equal(t, http.StatusUnauthorized, w.Code)
require.Nil(t, grants)
require.Equal(t, http.StatusUnauthorized, w.Code)
}
+12 -13
View File
@@ -3,10 +3,9 @@ package service_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/service"
livekit "github.com/livekit/livekit-server/proto"
"github.com/stretchr/testify/require"
)
func TestParticipantPersisence(t *testing.T) {
@@ -33,28 +32,28 @@ func TestParticipantPersisence(t *testing.T) {
}
// create the participant
assert.NoError(t, rs.PersistParticipant(roomName, p))
require.NoError(t, rs.PersistParticipant(roomName, p))
// result should match
pGet, err := rs.GetParticipant(roomName, p.Identity)
assert.NoError(t, err)
assert.Equal(t, p.Identity, pGet.Identity)
assert.Equal(t, len(p.Tracks), len(pGet.Tracks))
assert.Equal(t, p.Tracks[0].Sid, pGet.Tracks[0].Sid)
require.NoError(t, err)
require.Equal(t, p.Identity, pGet.Identity)
require.Equal(t, len(p.Tracks), len(pGet.Tracks))
require.Equal(t, p.Tracks[0].Sid, pGet.Tracks[0].Sid)
// list should return one participant
participants, err := rs.ListParticipants(roomName)
assert.NoError(t, err)
assert.Len(t, participants, 1)
require.NoError(t, err)
require.Len(t, participants, 1)
// deleting participant should return back to normal
assert.NoError(t, rs.DeleteParticipant(roomName, p.Identity))
require.NoError(t, rs.DeleteParticipant(roomName, p.Identity))
participants, err = rs.ListParticipants(roomName)
assert.NoError(t, err)
assert.Len(t, participants, 0)
require.NoError(t, err)
require.Len(t, participants, 0)
// shouldn't be able to get it
_, err = rs.GetParticipant(roomName, p.Identity)
assert.Equal(t, err, service.ErrParticipantNotFound)
require.Equal(t, err, service.ErrParticipantNotFound)
}
+4 -1
View File
@@ -260,7 +260,10 @@ func (r *RoomManager) StartSession(roomName string, pi routing.ParticipantInit,
}
// join room
if err := room.Join(participant); err != nil {
opts := rtc.ParticipantOptions{
AutoSubscribe: pi.AutoSubscribe,
}
if err := room.Join(participant, &opts); err != nil {
logger.Errorw("could not join room", "error", err)
return
}
+12 -6
View File
@@ -57,6 +57,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
roomName := r.FormValue("room")
reconnectParam := r.FormValue("reconnect")
protocolParam := r.FormValue("protocol")
autoSubParam := r.FormValue("auto_subscribe")
planBParam := r.FormValue("planb")
claims := GetGrants(r.Context())
@@ -66,10 +67,18 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
pi := routing.ParticipantInit{
Reconnect: boolValue(reconnectParam),
Identity: claims.Identity,
UsePlanB: boolValue(planBParam),
Reconnect: boolValue(reconnectParam),
Identity: claims.Identity,
UsePlanB: boolValue(planBParam),
AutoSubscribe: true,
}
if autoSubParam != "" {
pi.AutoSubscribe = boolValue(autoSubParam)
}
if pv, err := strconv.Atoi(protocolParam); err == nil {
pi.ProtocolVersion = int32(pv)
}
// only use permissions if any of them are set, default permissive
if claims.Video.CanPublish || claims.Video.CanSubscribe {
pi.Permission = &livekit.ParticipantPermission{
@@ -77,9 +86,6 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
CanPublish: claims.Video.CanPublish,
}
}
if pv, err := strconv.Atoi(protocolParam); err == nil {
pi.ProtocolVersion = int32(pv)
}
onlyName, err := EnsureJoinPermission(r.Context())
if err != nil {