Close subscriber PC after a wait to aid in migration. (#979)

* Close subscriber PC after a wait to aid in migration.

* mage generate
This commit is contained in:
Raja Subramanian
2022-09-03 01:16:51 +05:30
committed by GitHub
parent c75f38bce6
commit d13c4be923
5 changed files with 138 additions and 2 deletions
+51
View File
@@ -34,6 +34,7 @@ const (
rttUpdateInterval = 5 * time.Second
disconnectCleanupDuration = 15 * time.Second
migrationWaitDuration = 3 * time.Second
)
type pendingTrackInfo struct {
@@ -100,6 +101,7 @@ type ParticipantImpl struct {
connectedAt time.Time
// timer that's set when disconnect is detected on primary PC
disconnectTimer *time.Timer
migrationTimer *time.Timer
rtcpCh chan []rtcp.Packet
@@ -697,6 +699,54 @@ func (p *ParticipantImpl) Negotiate(force bool) {
}
}
func (p *ParticipantImpl) clearMigrationTimer() {
p.lock.Lock()
if p.migrationTimer != nil {
p.migrationTimer.Stop()
p.migrationTimer = nil
}
p.lock.Unlock()
}
func (p *ParticipantImpl) MaybeStartMigration(force bool, onStart func()) bool {
if !force && p.TransportManager.HaveAllTransportEverConnected() {
return false
}
if onStart != nil {
onStart()
}
p.CloseSignalConnection()
//
// On subscriber peer connection, remote side will try ICE on both
// pre- and post-migration ICE candidates as the migrating out
// peer connection leaves itself open to enable transition of
// media with as less disruption as possible.
//
// But, sometimes clients could delay the migration because of
// pinging the incorrect ICE candidates. Give the remote some time
// to try and succeed. If not, close the subscriber peer connection
// and help the remote side to narrow down its ICE candidate pool.
//
p.clearMigrationTimer()
p.lock.Lock()
p.migrationTimer = time.AfterFunc(migrationWaitDuration, func() {
p.clearMigrationTimer()
if p.isClosed.Load() || p.State() == livekit.ParticipantInfo_DISCONNECTED {
return
}
p.params.Logger.Infow("closing subscriber peer connection to aid migration")
p.TransportManager.SubscriberClose()
})
p.lock.Unlock()
return true
}
func (p *ParticipantImpl) SetMigrateState(s types.MigrateState) {
preState := p.MigrateState()
if preState == types.MigrateStateComplete || preState == s {
@@ -726,6 +776,7 @@ func (p *ParticipantImpl) MigrateState() types.MigrateState {
// ICERestart restarts subscriber ICE connections
func (p *ParticipantImpl) ICERestart(iceConfig *types.IceConfig) {
p.clearDisconnectTimer()
p.clearMigrationTimer()
for _, t := range p.GetPublishedTracks() {
t.(types.LocalMediaTrack).Restart()
+1 -1
View File
@@ -1031,7 +1031,7 @@ func (t *PCTransport) RemoveTrackFromStreamAllocator(subTrack types.SubscribedTr
t.streamAllocator.RemoveTrack(subTrack.DownTrack())
}
func (t *PCTransport) getICEConnectionType() types.ICEConnectionType {
func (t *PCTransport) GetICEConnectionType() types.ICEConnectionType {
unknown := types.ICEConnectionTypeUnknown
if t.pc == nil {
return unknown
+9 -1
View File
@@ -153,6 +153,14 @@ func (t *TransportManager) Close() {
t.subscriber.Close()
}
func (t *TransportManager) HaveAllTransportEverConnected() bool {
return t.publisher.HasEverConnected() && t.subscriber.HasEverConnected()
}
func (t *TransportManager) SubscriberClose() {
t.subscriber.Close()
}
func (t *TransportManager) OnPublisherICECandidate(f func(c *webrtc.ICECandidate) error) {
t.publisher.OnICECandidate(f)
}
@@ -443,7 +451,7 @@ func (t *TransportManager) SubscriberAsPrimary() bool {
}
func (t *TransportManager) GetICEConnectionType() types.ICEConnectionType {
return t.getTransport(true).getICEConnectionType()
return t.getTransport(true).GetICEConnectionType()
}
func (t *TransportManager) getTransport(isPrimary bool) *PCTransport {
+1
View File
@@ -306,6 +306,7 @@ type LocalParticipant interface {
OnClaimsChanged(callback func(LocalParticipant))
// session migration
MaybeStartMigration(force bool, onStart func()) bool
SetMigrateState(s MigrateState)
MigrateState() MigrateState
SetMigrateInfo(previousAnswer *webrtc.SessionDescription, mediaTracks []*livekit.TrackPublishedResponse, dataChannels []*livekit.DataChannelInfo)
@@ -394,6 +394,18 @@ type FakeLocalParticipant struct {
isSubscribedToReturnsOnCall map[int]struct {
result1 bool
}
MaybeStartMigrationStub func(bool, func()) bool
maybeStartMigrationMutex sync.RWMutex
maybeStartMigrationArgsForCall []struct {
arg1 bool
arg2 func()
}
maybeStartMigrationReturns struct {
result1 bool
}
maybeStartMigrationReturnsOnCall map[int]struct {
result1 bool
}
MigrateStateStub func() types.MigrateState
migrateStateMutex sync.RWMutex
migrateStateArgsForCall []struct {
@@ -2756,6 +2768,68 @@ func (fake *FakeLocalParticipant) IsSubscribedToReturnsOnCall(i int, result1 boo
}{result1}
}
func (fake *FakeLocalParticipant) MaybeStartMigration(arg1 bool, arg2 func()) bool {
fake.maybeStartMigrationMutex.Lock()
ret, specificReturn := fake.maybeStartMigrationReturnsOnCall[len(fake.maybeStartMigrationArgsForCall)]
fake.maybeStartMigrationArgsForCall = append(fake.maybeStartMigrationArgsForCall, struct {
arg1 bool
arg2 func()
}{arg1, arg2})
stub := fake.MaybeStartMigrationStub
fakeReturns := fake.maybeStartMigrationReturns
fake.recordInvocation("MaybeStartMigration", []interface{}{arg1, arg2})
fake.maybeStartMigrationMutex.Unlock()
if stub != nil {
return stub(arg1, arg2)
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) MaybeStartMigrationCallCount() int {
fake.maybeStartMigrationMutex.RLock()
defer fake.maybeStartMigrationMutex.RUnlock()
return len(fake.maybeStartMigrationArgsForCall)
}
func (fake *FakeLocalParticipant) MaybeStartMigrationCalls(stub func(bool, func()) bool) {
fake.maybeStartMigrationMutex.Lock()
defer fake.maybeStartMigrationMutex.Unlock()
fake.MaybeStartMigrationStub = stub
}
func (fake *FakeLocalParticipant) MaybeStartMigrationArgsForCall(i int) (bool, func()) {
fake.maybeStartMigrationMutex.RLock()
defer fake.maybeStartMigrationMutex.RUnlock()
argsForCall := fake.maybeStartMigrationArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2
}
func (fake *FakeLocalParticipant) MaybeStartMigrationReturns(result1 bool) {
fake.maybeStartMigrationMutex.Lock()
defer fake.maybeStartMigrationMutex.Unlock()
fake.MaybeStartMigrationStub = nil
fake.maybeStartMigrationReturns = struct {
result1 bool
}{result1}
}
func (fake *FakeLocalParticipant) MaybeStartMigrationReturnsOnCall(i int, result1 bool) {
fake.maybeStartMigrationMutex.Lock()
defer fake.maybeStartMigrationMutex.Unlock()
fake.MaybeStartMigrationStub = nil
if fake.maybeStartMigrationReturnsOnCall == nil {
fake.maybeStartMigrationReturnsOnCall = make(map[int]struct {
result1 bool
})
}
fake.maybeStartMigrationReturnsOnCall[i] = struct {
result1 bool
}{result1}
}
func (fake *FakeLocalParticipant) MigrateState() types.MigrateState {
fake.migrateStateMutex.Lock()
ret, specificReturn := fake.migrateStateReturnsOnCall[len(fake.migrateStateArgsForCall)]
@@ -4818,6 +4892,8 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
defer fake.isRecorderMutex.RUnlock()
fake.isSubscribedToMutex.RLock()
defer fake.isSubscribedToMutex.RUnlock()
fake.maybeStartMigrationMutex.RLock()
defer fake.maybeStartMigrationMutex.RUnlock()
fake.migrateStateMutex.RLock()
defer fake.migrateStateMutex.RUnlock()
fake.negotiateMutex.RLock()