automatically cleanup participant redis data
This commit is contained in:
+7
-6
@@ -3,10 +3,11 @@ package rtc
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrRoomIdMissing = errors.New("room is not passed in")
|
||||
ErrInvalidRoomName = errors.New("room must have a unique name")
|
||||
ErrRoomNotFound = errors.New("requested room does not exist")
|
||||
ErrPermissionDenied = errors.New("no permissions to access the room")
|
||||
ErrUnexpectedOffer = errors.New("expected answer SDP, received offer")
|
||||
ErrUnexpectedNegotiation = errors.New("client negotiation has not been granted")
|
||||
ErrInvalidRoomName = errors.New("room must have a unique name")
|
||||
ErrRoomNotFound = errors.New("requested room does not exist")
|
||||
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")
|
||||
ErrUnexpectedOffer = errors.New("expected answer SDP, received offer")
|
||||
ErrUnexpectedNegotiation = errors.New("client negotiation has not been granted")
|
||||
)
|
||||
|
||||
@@ -172,9 +172,11 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
|
||||
State: p.state,
|
||||
}
|
||||
|
||||
p.lock.RLock()
|
||||
for _, t := range p.publishedTracks {
|
||||
info.Tracks = append(info.Tracks, ToProtoTrack(t))
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
return info
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
@@ -16,6 +17,9 @@ type Room struct {
|
||||
lock sync.RWMutex
|
||||
// map of participantId -> Participant
|
||||
participants map[string]types.Participant
|
||||
hasJoined bool
|
||||
isClosed bool
|
||||
onClose func()
|
||||
}
|
||||
|
||||
func NewRoom(room *livekit.Room, config WebRTCConfig) *Room {
|
||||
@@ -40,9 +44,19 @@ func (r *Room) GetParticipants() []types.Participant {
|
||||
}
|
||||
|
||||
func (r *Room) Join(participant types.Participant) error {
|
||||
if r.isClosed {
|
||||
return ErrRoomClosed
|
||||
}
|
||||
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
if r.MaxParticipants > 0 && int(r.MaxParticipants) == len(r.participants) {
|
||||
return ErrMaxParticipantsExceeded
|
||||
}
|
||||
|
||||
r.hasJoined = true
|
||||
|
||||
// it's important to set this before connection, we don't want to miss out on any publishedTracks
|
||||
participant.OnTrackPublished(r.onTrackAdded)
|
||||
participant.OnStateChange(func(p types.Participant, oldState livekit.ParticipantInfo_State) {
|
||||
@@ -102,6 +116,36 @@ func (r *Room) RemoveParticipant(id string) {
|
||||
}
|
||||
|
||||
delete(r.participants, id)
|
||||
|
||||
go r.CloseIfEmpty()
|
||||
}
|
||||
|
||||
// Close the room if all participants had left, or it's still empty past timeout
|
||||
func (r *Room) CloseIfEmpty() {
|
||||
if r.isClosed {
|
||||
return
|
||||
}
|
||||
|
||||
r.lock.RLock()
|
||||
numParticipants := len(r.participants)
|
||||
r.lock.RUnlock()
|
||||
|
||||
if numParticipants > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
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) {
|
||||
r.isClosed = true
|
||||
if r.onClose != nil {
|
||||
r.onClose()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) OnClose(f func()) {
|
||||
r.onClose = f
|
||||
}
|
||||
|
||||
// a ParticipantImpl in the room added a new remoteTrack, subscribe other participants to it
|
||||
|
||||
+54
-1
@@ -13,6 +13,7 @@ import (
|
||||
|
||||
const (
|
||||
numParticipants = 3
|
||||
defaultDelay = 10 * time.Millisecond
|
||||
)
|
||||
|
||||
func TestRoomJoin(t *testing.T) {
|
||||
@@ -61,7 +62,7 @@ func TestRoomJoin(t *testing.T) {
|
||||
|
||||
rm.RemoveParticipant(p.ID())
|
||||
p.OnStateChangeArgsForCall(0)(p, livekit.ParticipantInfo_ACTIVE)
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
time.Sleep(defaultDelay)
|
||||
|
||||
for _, op := range participants {
|
||||
if op == p {
|
||||
@@ -72,6 +73,58 @@ func TestRoomJoin(t *testing.T) {
|
||||
assert.Equal(t, 1, fakeP.SendParticipantUpdateCallCount())
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("cannot exceed max participants", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, 1)
|
||||
rm.MaxParticipants = 1
|
||||
p := newMockParticipant("second")
|
||||
|
||||
err := rm.Join(p)
|
||||
assert.Equal(t, rtc.ErrMaxParticipantsExceeded, err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRoomClosure(t *testing.T) {
|
||||
t.Run("room closes after participant leaves", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, 1)
|
||||
isClosed := false
|
||||
rm.OnClose(func() {
|
||||
isClosed = true
|
||||
})
|
||||
p := rm.GetParticipants()[0]
|
||||
rm.RemoveParticipant(p.ID())
|
||||
|
||||
time.Sleep(defaultDelay)
|
||||
|
||||
assert.Len(t, rm.GetParticipants(), 0)
|
||||
assert.True(t, isClosed)
|
||||
|
||||
assert.Equal(t, rtc.ErrRoomClosed, rm.Join(p))
|
||||
})
|
||||
|
||||
t.Run("room does not close before empty timeout", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, 0)
|
||||
isClosed := false
|
||||
rm.OnClose(func() {
|
||||
isClosed = true
|
||||
})
|
||||
|
||||
rm.CloseIfEmpty()
|
||||
assert.False(t, isClosed)
|
||||
})
|
||||
|
||||
t.Run("room closes after empty timeout", func(t *testing.T) {
|
||||
rm := newRoomWithParticipants(t, 0)
|
||||
isClosed := false
|
||||
rm.OnClose(func() {
|
||||
isClosed = true
|
||||
})
|
||||
rm.EmptyTimeout = 1
|
||||
|
||||
time.Sleep(1010 * time.Millisecond)
|
||||
rm.CloseIfEmpty()
|
||||
assert.True(t, isClosed)
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewTrack(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user