From 81f41aca20e32be1c672bb625df85e8568222794 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Sat, 24 Jun 2023 19:18:05 +0530 Subject: [PATCH] Full reconnect on publication mismatch on resume. (#1823) * Full reconnect on publication mismatch on resume. It is possible that publications mismatch on resume. An example sequence - Client sends `AddTrack` for `trackA` - Server never receives it due to signalling connection breakage. - Client could do a resume (reconnect=1) noticing signalling connection breakage. - Client's view thinks that `trackA` is known to server, but server does not know about it. - A subsequence offer containing `trackA` triggers `trackInfo not available before track publish` and the track does not get published. Detect the case of missing track and issue a full reconnect. * UpdateSubscriptions from sync state a la cloud * add missing shouldReconnect --- pkg/rtc/room.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index df9db3764..dcd5a6b1c 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -526,6 +526,46 @@ func (r *Room) UpdateSubscriptions( } func (r *Room) SyncState(participant types.LocalParticipant, state *livekit.SyncState) error { + pLogger := participant.GetLogger() + pLogger.Infow("setting sync state", "state", state) + + shouldReconnect := false + pubTracks := state.GetPublishTracks() + existingPubTracks := participant.GetPublishedTracks() + for _, pubTrack := range pubTracks { + // client may not have sent TrackInfo for each published track + ti := pubTrack.Track + if ti == nil { + pLogger.Warnw("TrackInfo not sent during resume", nil) + shouldReconnect = true + break + } + + found := false + for _, existingPubTrack := range existingPubTracks { + if existingPubTrack.ID() == livekit.TrackID(ti.Sid) { + found = true + break + } + } + if !found { + pLogger.Warnw("unknown track during resume", nil, "trackID", ti.Sid) + shouldReconnect = true + break + } + } + if shouldReconnect { + pLogger.Warnw("unable to resume due to missing published tracks, starting full reconnect", nil) + participant.IssueFullReconnect(types.ParticipantCloseReasonPublicationError) + return nil + } + + r.UpdateSubscriptions( + participant, + livekit.StringsAsTrackIDs(state.Subscription.TrackSids), + state.Subscription.ParticipantTracks, + state.Subscription.Subscribe, + ) return nil }