fallback to turn over tls when tcp short connection happen (#950)

* fallback to tls when tcp failed

* go mod

* magefile
This commit is contained in:
cnderrauber
2022-08-24 20:42:56 +08:00
committed by GitHub
parent 00b131da50
commit 1350400c3a
8 changed files with 68 additions and 32 deletions
+10 -2
View File
@@ -233,6 +233,8 @@ func (p *ParticipantImpl) ConnectedAt() time.Time {
}
func (p *ParticipantImpl) GetClientConfiguration() *livekit.ClientConfiguration {
p.lock.RLock()
defer p.lock.RUnlock()
return p.params.ClientConf
}
@@ -1035,9 +1037,15 @@ func (p *ParticipantImpl) setupTransportManager() error {
}
tm.OnICEConfigChanged(func(iceConfig types.IceConfig) {
p.lock.RLock()
p.lock.Lock()
onICEConfigChanged := p.onICEConfigChanged
p.lock.RUnlock()
if iceConfig.PreferSub == types.PreferTls {
if p.params.ClientConf == nil {
p.params.ClientConf = &livekit.ClientConfiguration{}
}
p.params.ClientConf.ForceRelay = livekit.ClientConfigSetting_ENABLED
}
p.lock.Unlock()
if onICEConfigChanged != nil {
onICEConfigChanged(p, iceConfig)
+2 -2
View File
@@ -637,8 +637,8 @@ func (r *Room) SimulateScenario(participant types.LocalParticipant, simulateScen
case *livekit.SimulateScenario_SwitchCandidateProtocol:
r.Logger.Infow("simulating switch candidate protocol", "participant", participant.Identity())
participant.ICERestart(&types.IceConfig{
PreferSubTcp: scenario.SwitchCandidateProtocol == livekit.CandidateProtocol_TCP,
PreferPubTcp: scenario.SwitchCandidateProtocol == livekit.CandidateProtocol_TCP,
PreferSub: types.PreferCandidateType(scenario.SwitchCandidateProtocol),
PreferPub: types.PreferCandidateType(scenario.SwitchCandidateProtocol),
})
}
return nil
-2
View File
@@ -473,8 +473,6 @@ func (t *PCTransport) onICEConnectionStateChange(state webrtc.ICEConnectionState
} else {
t.params.Logger.Infow("selected ICE candidate pair", "pair", pair)
}
case webrtc.ICEConnectionStateFailed:
t.handleConnectionFailed()
}
}
+31 -15
View File
@@ -46,6 +46,7 @@ type TransportManager struct {
pendingOfferPublisher *webrtc.SessionDescription
pendingDataChannelsPublisher []*livekit.DataChannelInfo
lastPublisherAnswer atomic.Value
iceConfig types.IceConfig
onPublisherInitialConnected func()
onSubscriberInitialConnected func()
@@ -101,7 +102,6 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
}
})
t.publisher.OnFailed(func(isShortLived bool) {
t.handleConnectionFailed(isShortLived)
if t.onAnyTransportFailed != nil {
t.onAnyTransportFailed()
}
@@ -422,17 +422,14 @@ func (t *TransportManager) OnICEConfigChanged(f func(iceConfig types.IceConfig))
func (t *TransportManager) SetICEConfig(iceConfig types.IceConfig) {
t.params.Logger.Infow("setting ICE config", "iceConfig", iceConfig)
if iceConfig.PreferPubTcp {
t.publisher.SetPreferTCP(true)
}
if iceConfig.PreferSubTcp {
t.subscriber.SetPreferTCP(true)
}
t.publisher.SetPreferTCP(iceConfig.PreferPub == types.PreferTcp)
t.subscriber.SetPreferTCP(iceConfig.PreferSub == types.PreferTcp)
t.lock.RLock()
t.lock.Lock()
onICEConfigChanged := t.onICEConfigChanged
t.lock.RUnlock()
t.iceConfig = iceConfig
t.lock.Unlock()
if onICEConfigChanged != nil {
onICEConfigChanged(iceConfig)
@@ -456,14 +453,33 @@ func (t *TransportManager) handleConnectionFailed(isShortLived bool) {
if !t.params.AllowTCPFallback || !isShortLived {
return
}
t.lock.RLock()
iceConfig := t.iceConfig
t.lock.RUnlock()
// irrespective of which one fails, force TCP on both as the other one might
var nextConfig types.IceConfig
// irrespective of which one fails, force prefer candidate on both as the other one might
// fail at a different time and cause another disruption
t.params.Logger.Infow("restricting transport to TCP on both peer connections")
t.SetICEConfig(types.IceConfig{
PreferPubTcp: true,
PreferSubTcp: true,
})
switch iceConfig.PreferSub {
case types.PreferNone:
t.params.Logger.Infow("restricting transport to TCP on both peer connections")
nextConfig = types.IceConfig{
PreferPub: types.PreferTcp,
PreferSub: types.PreferTcp,
}
case types.PreferTcp:
t.params.Logger.Infow("prefer transport to TLS on both peer connections")
nextConfig = types.IceConfig{
PreferPub: types.PreferTls,
PreferSub: types.PreferTls,
}
default:
return
}
t.SetICEConfig(nextConfig)
}
func (t *TransportManager) SetMigrateInfo(previousAnswer *webrtc.SessionDescription, dataChannels []*livekit.DataChannelInfo) {
+10 -2
View File
@@ -195,9 +195,17 @@ type Participant interface {
DebugInfo() map[string]interface{}
}
type PreferCandidateType int
const (
PreferNone PreferCandidateType = iota
PreferTcp
PreferTls
)
type IceConfig struct {
PreferSubTcp bool
PreferPubTcp bool
PreferSub PreferCandidateType
PreferPub PreferCandidateType
}
//counterfeiter:generate . LocalParticipant