rework multi-node routing, cleaner design for reconnection
This commit is contained in:
@@ -6,6 +6,7 @@ var (
|
||||
ErrRoomClosed = errors.New("room has already closed")
|
||||
ErrPermissionDenied = errors.New("no permissions to access the room")
|
||||
ErrMaxParticipantsExceeded = errors.New("room has exceeded its max participants")
|
||||
ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room")
|
||||
ErrUnexpectedOffer = errors.New("expected answer SDP, received offer")
|
||||
ErrUnexpectedNegotiation = errors.New("client negotiation has not been granted")
|
||||
)
|
||||
|
||||
+10
-2
@@ -75,14 +75,14 @@ func NewPeerConnection(conf *WebRTCConfig) (*webrtc.PeerConnection, error) {
|
||||
return pc, err
|
||||
}
|
||||
|
||||
func NewParticipant(participantId, identity string, pc types.PeerConnection, rs routing.MessageSink, receiverConfig ReceiverConfig) (*ParticipantImpl, error) {
|
||||
func NewParticipant(identity string, pc types.PeerConnection, rs routing.MessageSink, receiverConfig ReceiverConfig) (*ParticipantImpl, error) {
|
||||
// TODO: check to ensure params are valid, id and identity can't be empty
|
||||
me := &webrtc.MediaEngine{}
|
||||
me.RegisterDefaultCodecs()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
participant := &ParticipantImpl{
|
||||
id: participantId,
|
||||
id: utils.NewGuid(utils.ParticipantPrefix),
|
||||
identity: identity,
|
||||
peerConn: pc,
|
||||
responseSink: rs,
|
||||
@@ -184,6 +184,14 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
|
||||
return info
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) GetResponseSink() routing.MessageSink {
|
||||
return p.responseSink
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) SetResponseSink(sink routing.MessageSink) {
|
||||
p.responseSink = sink
|
||||
}
|
||||
|
||||
// callbacks for clients
|
||||
func (p *ParticipantImpl) OnTrackPublished(callback func(types.Participant, types.PublishedTrack)) {
|
||||
p.onTrackPublished = callback
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/routing/routingfakes"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -117,10 +116,9 @@ func TestDisconnectTiming(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func newParticipantForTest(name string) *ParticipantImpl {
|
||||
func newParticipantForTest(identity string) *ParticipantImpl {
|
||||
p, _ := NewParticipant(
|
||||
utils.NewGuid(utils.ParticipantPrefix),
|
||||
name,
|
||||
identity,
|
||||
&typesfakes.FakePeerConnection{},
|
||||
&routingfakes.FakeMessageSink{},
|
||||
ReceiverConfig{})
|
||||
|
||||
+28
-19
@@ -16,7 +16,7 @@ type Room struct {
|
||||
livekit.Room
|
||||
config WebRTCConfig
|
||||
lock sync.RWMutex
|
||||
// map of participantId -> Participant
|
||||
// map of identity -> Participant
|
||||
participants map[string]types.Participant
|
||||
hasJoined bool
|
||||
isClosed utils.AtomicFlag
|
||||
@@ -32,10 +32,10 @@ func NewRoom(room *livekit.Room, config WebRTCConfig) *Room {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) GetParticipant(id string) types.Participant {
|
||||
func (r *Room) GetParticipant(identity string) types.Participant {
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
return r.participants[id]
|
||||
return r.participants[identity]
|
||||
}
|
||||
|
||||
func (r *Room) GetParticipants() []types.Participant {
|
||||
@@ -52,6 +52,11 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if r.participants[participant.Identity()] != nil {
|
||||
return ErrAlreadyJoined
|
||||
|
||||
}
|
||||
|
||||
if r.MaxParticipants > 0 && int(r.MaxParticipants) == len(r.participants) {
|
||||
return ErrMaxParticipantsExceeded
|
||||
}
|
||||
@@ -75,15 +80,15 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
if err := op.AddSubscriber(p); err != nil {
|
||||
// TODO: log error? or disconnect?
|
||||
logger.Errorw("could not subscribe to participant",
|
||||
"dstParticipant", p.ID(),
|
||||
"srcParticipant", op.ID())
|
||||
"dest", p.Identity(),
|
||||
"source", op.Identity())
|
||||
}
|
||||
}
|
||||
// start the workers once connectivity is established
|
||||
p.Start()
|
||||
} else if p.State() == livekit.ParticipantInfo_DISCONNECTED {
|
||||
// remove participant from room
|
||||
go r.RemoveParticipant(p.ID())
|
||||
go r.RemoveParticipant(p.Identity())
|
||||
}
|
||||
})
|
||||
participant.OnTrackUpdated(r.onTrackUpdated)
|
||||
@@ -93,7 +98,7 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
"identity", participant.Identity(),
|
||||
"roomId", r.Sid)
|
||||
|
||||
r.participants[participant.ID()] = participant
|
||||
r.participants[participant.Identity()] = participant
|
||||
|
||||
// gather other participants and send join response
|
||||
otherParticipants := make([]types.Participant, 0, len(r.participants))
|
||||
@@ -106,11 +111,11 @@ func (r *Room) Join(participant types.Participant) error {
|
||||
return participant.SendJoinResponse(&r.Room, otherParticipants)
|
||||
}
|
||||
|
||||
func (r *Room) RemoveParticipant(id string) {
|
||||
func (r *Room) RemoveParticipant(identity string) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if p, ok := r.participants[id]; ok {
|
||||
if p, ok := r.participants[identity]; ok {
|
||||
// avoid blocking lock
|
||||
go func() {
|
||||
Recover()
|
||||
@@ -119,7 +124,7 @@ func (r *Room) RemoveParticipant(id string) {
|
||||
}()
|
||||
}
|
||||
|
||||
delete(r.participants, id)
|
||||
delete(r.participants, identity)
|
||||
|
||||
go r.CloseIfEmpty()
|
||||
}
|
||||
@@ -139,11 +144,15 @@ func (r *Room) CloseIfEmpty() {
|
||||
}
|
||||
|
||||
elapsed := uint32(time.Now().Unix() - r.CreationTime)
|
||||
logger.Infow("comparing elapsed", "elapsed", elapsed, "timeout", r.EmptyTimeout)
|
||||
if r.hasJoined || (r.EmptyTimeout > 0 && elapsed >= r.EmptyTimeout) {
|
||||
if r.isClosed.TrySet(true) && r.onClose != nil {
|
||||
r.onClose()
|
||||
}
|
||||
r.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) Close() {
|
||||
logger.Infow("closing room", "room", r.Sid, "name", r.Name)
|
||||
if r.isClosed.TrySet(true) && r.onClose != nil {
|
||||
r.onClose()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,14 +180,14 @@ func (r *Room) onTrackAdded(participant types.Participant, track types.Published
|
||||
continue
|
||||
}
|
||||
logger.Debugw("subscribing to new track",
|
||||
"srcParticipant", participant.ID(),
|
||||
"source", participant.Identity(),
|
||||
"remoteTrack", track.ID(),
|
||||
"dstParticipant", existingParticipant.ID())
|
||||
"dest", existingParticipant.Identity())
|
||||
if err := track.AddSubscriber(existingParticipant); err != nil {
|
||||
logger.Errorw("could not subscribe to remoteTrack",
|
||||
"srcParticipant", participant.ID(),
|
||||
"source", participant.Identity(),
|
||||
"remoteTrack", track.ID(),
|
||||
"dstParticipant", existingParticipant.ID())
|
||||
"dest", existingParticipant.Identity())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,7 +211,7 @@ func (r *Room) broadcastParticipantState(p types.Participant) {
|
||||
err := op.SendParticipantUpdate(updates)
|
||||
if err != nil {
|
||||
logger.Errorw("could not send update to participant",
|
||||
"participant", p.ID(),
|
||||
"participant", p.Identity(),
|
||||
"err", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package rtc_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -62,7 +63,7 @@ func TestRoomJoin(t *testing.T) {
|
||||
disconnectedParticipant := participants[1].(*typesfakes.FakeParticipant)
|
||||
disconnectedParticipant.StateReturns(livekit.ParticipantInfo_DISCONNECTED)
|
||||
|
||||
rm.RemoveParticipant(p.ID())
|
||||
rm.RemoveParticipant(p.Identity())
|
||||
p.OnStateChangeArgsForCall(0)(p, livekit.ParticipantInfo_ACTIVE)
|
||||
time.Sleep(defaultDelay)
|
||||
|
||||
@@ -97,7 +98,7 @@ func TestRoomClosure(t *testing.T) {
|
||||
isClosed = true
|
||||
})
|
||||
p := rm.GetParticipants()[0]
|
||||
rm.RemoveParticipant(p.ID())
|
||||
rm.RemoveParticipant(p.Identity())
|
||||
|
||||
time.Sleep(defaultDelay)
|
||||
|
||||
@@ -155,12 +156,14 @@ func TestNewTrack(t *testing.T) {
|
||||
}
|
||||
|
||||
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
|
||||
|
||||
rm := rtc.NewRoom(
|
||||
&livekit.Room{Name: "identity"},
|
||||
&livekit.Room{Name: "room"},
|
||||
rtc.WebRTCConfig{},
|
||||
)
|
||||
for i := 0; i < num; i++ {
|
||||
participant := newMockParticipant("")
|
||||
identity := fmt.Sprintf("p%d", i)
|
||||
participant := newMockParticipant(identity)
|
||||
err := rm.Join(participant)
|
||||
assert.NoError(t, err)
|
||||
//rm.participants[participant.ID()] = participant
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
@@ -53,6 +54,8 @@ type Participant interface {
|
||||
IsReady() bool
|
||||
ToProto() *livekit.ParticipantInfo
|
||||
RTCPChan() *utils.CalmChannel
|
||||
GetResponseSink() routing.MessageSink
|
||||
SetResponseSink(sink routing.MessageSink)
|
||||
|
||||
AddTrack(clientId, name string, trackType livekit.TrackType)
|
||||
Answer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
|
||||
|
||||
@@ -4,6 +4,7 @@ package typesfakes
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
@@ -70,6 +71,16 @@ type FakeParticipant struct {
|
||||
closeReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
GetResponseSinkStub func() routing.MessageSink
|
||||
getResponseSinkMutex sync.RWMutex
|
||||
getResponseSinkArgsForCall []struct {
|
||||
}
|
||||
getResponseSinkReturns struct {
|
||||
result1 routing.MessageSink
|
||||
}
|
||||
getResponseSinkReturnsOnCall map[int]struct {
|
||||
result1 routing.MessageSink
|
||||
}
|
||||
HandleAnswerStub func(webrtc.SessionDescription) error
|
||||
handleAnswerMutex sync.RWMutex
|
||||
handleAnswerArgsForCall []struct {
|
||||
@@ -194,6 +205,11 @@ type FakeParticipant struct {
|
||||
sendParticipantUpdateReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
SetResponseSinkStub func(routing.MessageSink)
|
||||
setResponseSinkMutex sync.RWMutex
|
||||
setResponseSinkArgsForCall []struct {
|
||||
arg1 routing.MessageSink
|
||||
}
|
||||
SetTrackMutedStub func(string, bool)
|
||||
setTrackMutedMutex sync.RWMutex
|
||||
setTrackMutedArgsForCall []struct {
|
||||
@@ -534,6 +550,59 @@ func (fake *FakeParticipant) CloseReturnsOnCall(i int, result1 error) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) GetResponseSink() routing.MessageSink {
|
||||
fake.getResponseSinkMutex.Lock()
|
||||
ret, specificReturn := fake.getResponseSinkReturnsOnCall[len(fake.getResponseSinkArgsForCall)]
|
||||
fake.getResponseSinkArgsForCall = append(fake.getResponseSinkArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.GetResponseSinkStub
|
||||
fakeReturns := fake.getResponseSinkReturns
|
||||
fake.recordInvocation("GetResponseSink", []interface{}{})
|
||||
fake.getResponseSinkMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub()
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) GetResponseSinkCallCount() int {
|
||||
fake.getResponseSinkMutex.RLock()
|
||||
defer fake.getResponseSinkMutex.RUnlock()
|
||||
return len(fake.getResponseSinkArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) GetResponseSinkCalls(stub func() routing.MessageSink) {
|
||||
fake.getResponseSinkMutex.Lock()
|
||||
defer fake.getResponseSinkMutex.Unlock()
|
||||
fake.GetResponseSinkStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) GetResponseSinkReturns(result1 routing.MessageSink) {
|
||||
fake.getResponseSinkMutex.Lock()
|
||||
defer fake.getResponseSinkMutex.Unlock()
|
||||
fake.GetResponseSinkStub = nil
|
||||
fake.getResponseSinkReturns = struct {
|
||||
result1 routing.MessageSink
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) GetResponseSinkReturnsOnCall(i int, result1 routing.MessageSink) {
|
||||
fake.getResponseSinkMutex.Lock()
|
||||
defer fake.getResponseSinkMutex.Unlock()
|
||||
fake.GetResponseSinkStub = nil
|
||||
if fake.getResponseSinkReturnsOnCall == nil {
|
||||
fake.getResponseSinkReturnsOnCall = make(map[int]struct {
|
||||
result1 routing.MessageSink
|
||||
})
|
||||
}
|
||||
fake.getResponseSinkReturnsOnCall[i] = struct {
|
||||
result1 routing.MessageSink
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) HandleAnswer(arg1 webrtc.SessionDescription) error {
|
||||
fake.handleAnswerMutex.Lock()
|
||||
ret, specificReturn := fake.handleAnswerReturnsOnCall[len(fake.handleAnswerArgsForCall)]
|
||||
@@ -1242,6 +1311,38 @@ func (fake *FakeParticipant) SendParticipantUpdateReturnsOnCall(i int, result1 e
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SetResponseSink(arg1 routing.MessageSink) {
|
||||
fake.setResponseSinkMutex.Lock()
|
||||
fake.setResponseSinkArgsForCall = append(fake.setResponseSinkArgsForCall, struct {
|
||||
arg1 routing.MessageSink
|
||||
}{arg1})
|
||||
stub := fake.SetResponseSinkStub
|
||||
fake.recordInvocation("SetResponseSink", []interface{}{arg1})
|
||||
fake.setResponseSinkMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.SetResponseSinkStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SetResponseSinkCallCount() int {
|
||||
fake.setResponseSinkMutex.RLock()
|
||||
defer fake.setResponseSinkMutex.RUnlock()
|
||||
return len(fake.setResponseSinkArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SetResponseSinkCalls(stub func(routing.MessageSink)) {
|
||||
fake.setResponseSinkMutex.Lock()
|
||||
defer fake.setResponseSinkMutex.Unlock()
|
||||
fake.SetResponseSinkStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SetResponseSinkArgsForCall(i int) routing.MessageSink {
|
||||
fake.setResponseSinkMutex.RLock()
|
||||
defer fake.setResponseSinkMutex.RUnlock()
|
||||
argsForCall := fake.setResponseSinkArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) SetTrackMuted(arg1 string, arg2 bool) {
|
||||
fake.setTrackMutedMutex.Lock()
|
||||
fake.setTrackMutedArgsForCall = append(fake.setTrackMutedArgsForCall, struct {
|
||||
@@ -1420,6 +1521,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.answerMutex.RUnlock()
|
||||
fake.closeMutex.RLock()
|
||||
defer fake.closeMutex.RUnlock()
|
||||
fake.getResponseSinkMutex.RLock()
|
||||
defer fake.getResponseSinkMutex.RUnlock()
|
||||
fake.handleAnswerMutex.RLock()
|
||||
defer fake.handleAnswerMutex.RUnlock()
|
||||
fake.handleClientNegotiationMutex.RLock()
|
||||
@@ -1452,6 +1555,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.sendJoinResponseMutex.RUnlock()
|
||||
fake.sendParticipantUpdateMutex.RLock()
|
||||
defer fake.sendParticipantUpdateMutex.RUnlock()
|
||||
fake.setResponseSinkMutex.RLock()
|
||||
defer fake.setResponseSinkMutex.RUnlock()
|
||||
fake.setTrackMutedMutex.RLock()
|
||||
defer fake.setTrackMutedMutex.RUnlock()
|
||||
fake.startMutex.RLock()
|
||||
|
||||
Reference in New Issue
Block a user