enable explicit reconnect to distinguish between WS disconnect vs PeerConnection disconnect

This commit is contained in:
David Zhao
2021-01-30 23:16:11 -08:00
parent 2cf2c448be
commit 320a6c35bf
9 changed files with 87 additions and 69 deletions
+19 -13
View File
@@ -125,28 +125,34 @@ func (r *RoomManager) Cleanup() error {
}
// starts WebRTC session when a new participant is connected, takes place on RTC node
func (r *RoomManager) StartSession(roomName, identity string, requestSource routing.MessageSource, responseSink routing.MessageSink) {
func (r *RoomManager) StartSession(roomName, identity string, reconnect bool, requestSource routing.MessageSource, responseSink routing.MessageSink) {
room, err := r.getOrCreateRoom(roomName)
if err != nil {
logger.Errorw("could not create room", "error", err)
return
}
// Use existing peer connection if it's already connected, perhaps from a different signal connection
participant := room.GetParticipant(identity)
if participant != nil {
logger.Debugw("resuming RTC session",
"room", roomName,
"node", r.currentNode.Id,
"participant", identity,
)
// close previous sink, and link to new one
prevSink := participant.GetResponseSink()
if prevSink != nil {
prevSink.Close()
// When reconnecting, it means WS has interrupted by underlying peer connection is still ok
// in this mode, we'll keep the participant SID, and just swap the sink for the underlying connection
if reconnect {
logger.Debugw("resuming RTC session",
"room", roomName,
"node", r.currentNode.Id,
"participant", identity,
)
// close previous sink, and link to new one
prevSink := participant.GetResponseSink()
if prevSink != nil {
prevSink.Close()
}
participant.SetResponseSink(responseSink)
return
} else {
// we need to clean up the existing participant, so a new one can join
room.RemoveParticipant(participant.Identity())
}
participant.SetResponseSink(responseSink)
return
}
logger.Debugw("starting RTC session",
+4 -3
View File
@@ -43,6 +43,8 @@ func NewRTCService(conf *config.Config, roomStore RoomStore, roomManager *RoomMa
func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
roomName := r.FormValue("room")
reconnectParam := r.FormValue("reconnect")
isReconnect := reconnectParam == "1" || reconnectParam == "true"
claims := GetGrants(r.Context())
// require a claim
if claims == nil || claims.Video == nil {
@@ -68,12 +70,11 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
// this needs to be started first *before* using router functions on this node
reqSink, resSource, err := s.router.StartParticipantSignal(roomName, identity)
reqSink, resSource, err := s.router.StartParticipantSignal(roomName, identity, isReconnect)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
return
}
logger.Debugw("started participant signal", "sink", reqSink, "source", resSource)
done := make(chan bool, 1)
defer func() {
@@ -93,7 +94,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
sigConn := NewWSSignalConnection(conn)
logger.Infow("new client connected",
logger.Infow("new client WS connected",
"room", rm.Sid,
"roomName", rm.Name,
"name", identity,