cleanup room state, proper participant termination

This commit is contained in:
David Zhao
2021-01-18 23:44:59 -08:00
parent d83a1b1af3
commit 71b4673a0a
22 changed files with 374 additions and 101 deletions
-2
View File
@@ -3,8 +3,6 @@ package rtc
import "errors"
var (
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")
+8 -1
View File
@@ -128,9 +128,15 @@ func NewParticipant(participantId, name string, pc types.PeerConnection, rs rout
logger.Debugw("ICE connection state changed", "state", state.String())
if state == webrtc.ICEConnectionStateConnected {
participant.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected {
go participant.Close()
}
})
//pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
// logger.Debugw("PeerConnection state changed", "state", state.String())
//})
pc.OnDataChannel(participant.onDataChannel)
// only set after answered
@@ -320,6 +326,7 @@ func (p *ParticipantImpl) Close() error {
}
close(p.rtcpCh)
p.updateState(livekit.ParticipantInfo_DISCONNECTED)
p.responseSink.Close()
if p.onClose != nil {
p.onClose(p)
}
@@ -470,7 +477,7 @@ func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
}
oldState := p.state
p.state = state
logger.Debugw("updating participant state", "state", state.String())
if p.onStateChange != nil {
go func() {
defer Recover()
+1
View File
@@ -24,6 +24,7 @@ type WebsocketClient interface {
type PeerConnection interface {
OnICECandidate(f func(*webrtc.ICECandidate))
OnICEConnectionStateChange(func(webrtc.ICEConnectionState))
OnConnectionStateChange(f func(webrtc.PeerConnectionState))
OnTrack(f func(*webrtc.TrackRemote, *webrtc.RTPReceiver))
OnDataChannel(func(d *webrtc.DataChannel))
OnNegotiationNeeded(f func())
@@ -95,6 +95,11 @@ type FakePeerConnection struct {
result1 webrtc.SessionDescription
result2 error
}
OnConnectionStateChangeStub func(func(webrtc.PeerConnectionState))
onConnectionStateChangeMutex sync.RWMutex
onConnectionStateChangeArgsForCall []struct {
arg1 func(webrtc.PeerConnectionState)
}
OnDataChannelStub func(func(d *webrtc.DataChannel))
onDataChannelMutex sync.RWMutex
onDataChannelArgsForCall []struct {
@@ -603,6 +608,38 @@ func (fake *FakePeerConnection) CreateOfferReturnsOnCall(i int, result1 webrtc.S
}{result1, result2}
}
func (fake *FakePeerConnection) OnConnectionStateChange(arg1 func(webrtc.PeerConnectionState)) {
fake.onConnectionStateChangeMutex.Lock()
fake.onConnectionStateChangeArgsForCall = append(fake.onConnectionStateChangeArgsForCall, struct {
arg1 func(webrtc.PeerConnectionState)
}{arg1})
stub := fake.OnConnectionStateChangeStub
fake.recordInvocation("OnConnectionStateChange", []interface{}{arg1})
fake.onConnectionStateChangeMutex.Unlock()
if stub != nil {
fake.OnConnectionStateChangeStub(arg1)
}
}
func (fake *FakePeerConnection) OnConnectionStateChangeCallCount() int {
fake.onConnectionStateChangeMutex.RLock()
defer fake.onConnectionStateChangeMutex.RUnlock()
return len(fake.onConnectionStateChangeArgsForCall)
}
func (fake *FakePeerConnection) OnConnectionStateChangeCalls(stub func(func(webrtc.PeerConnectionState))) {
fake.onConnectionStateChangeMutex.Lock()
defer fake.onConnectionStateChangeMutex.Unlock()
fake.OnConnectionStateChangeStub = stub
}
func (fake *FakePeerConnection) OnConnectionStateChangeArgsForCall(i int) func(webrtc.PeerConnectionState) {
fake.onConnectionStateChangeMutex.RLock()
defer fake.onConnectionStateChangeMutex.RUnlock()
argsForCall := fake.onConnectionStateChangeArgsForCall[i]
return argsForCall.arg1
}
func (fake *FakePeerConnection) OnDataChannel(arg1 func(d *webrtc.DataChannel)) {
fake.onDataChannelMutex.Lock()
fake.onDataChannelArgsForCall = append(fake.onDataChannelArgsForCall, struct {
@@ -1082,6 +1119,8 @@ func (fake *FakePeerConnection) Invocations() map[string][][]interface{} {
defer fake.createDataChannelMutex.RUnlock()
fake.createOfferMutex.RLock()
defer fake.createOfferMutex.RUnlock()
fake.onConnectionStateChangeMutex.RLock()
defer fake.onConnectionStateChangeMutex.RUnlock()
fake.onDataChannelMutex.RLock()
defer fake.onDataChannelMutex.RUnlock()
fake.onICECandidateMutex.RLock()