ICE config using protocol model (#1233)

* ICE config using protocol model

* use pointers consistently

* protocol pointer

* mage generate
This commit is contained in:
Raja Subramanian
2022-12-19 10:25:08 +05:30
committed by GitHub
parent 33902a9f2a
commit 241a7120f5
8 changed files with 64 additions and 87 deletions
+5 -5
View File
@@ -154,7 +154,7 @@ type ParticipantImpl struct {
onClose func(types.LocalParticipant, map[livekit.TrackID]livekit.ParticipantID)
onClaimsChanged func(participant types.LocalParticipant)
onICEConfigChanged func(participant types.LocalParticipant, iceConfig types.IceConfig)
onICEConfigChanged func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)
cachedDownTracks map[livekit.TrackID]*downTrackState
@@ -759,7 +759,7 @@ func (p *ParticipantImpl) MigrateState() types.MigrateState {
}
// ICERestart restarts subscriber ICE connections
func (p *ParticipantImpl) ICERestart(iceConfig *types.IceConfig) {
func (p *ParticipantImpl) ICERestart(iceConfig *livekit.ICEConfig) {
p.clearDisconnectTimer()
p.clearMigrationTimer()
@@ -770,7 +770,7 @@ func (p *ParticipantImpl) ICERestart(iceConfig *types.IceConfig) {
p.TransportManager.ICERestart(iceConfig)
}
func (p *ParticipantImpl) OnICEConfigChanged(f func(participant types.LocalParticipant, iceConfig types.IceConfig)) {
func (p *ParticipantImpl) OnICEConfigChanged(f func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)) {
p.lock.Lock()
p.onICEConfigChanged = f
p.lock.Unlock()
@@ -1097,14 +1097,14 @@ func (p *ParticipantImpl) setupTransportManager() error {
return err
}
tm.OnICEConfigChanged(func(iceConfig types.IceConfig) {
tm.OnICEConfigChanged(func(iceConfig *livekit.ICEConfig) {
p.lock.Lock()
onICEConfigChanged := p.onICEConfigChanged
if p.params.ClientConf == nil {
p.params.ClientConf = &livekit.ClientConfiguration{}
}
if iceConfig.PreferSub == types.PreferTls {
if iceConfig.PreferenceSubscriber == livekit.ICECandidateType_ICT_TLS {
p.params.ClientConf.ForceRelay = livekit.ClientConfigSetting_ENABLED
} else {
// UNSET indicates that clients could override RTCConfiguration to forceRelay
+3 -3
View File
@@ -651,9 +651,9 @@ 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{
PreferSub: types.PreferCandidateType(scenario.SwitchCandidateProtocol),
PreferPub: types.PreferCandidateType(scenario.SwitchCandidateProtocol),
participant.ICERestart(&livekit.ICEConfig{
PreferenceSubscriber: livekit.ICECandidateType(scenario.SwitchCandidateProtocol),
PreferencePublisher: livekit.ICECandidateType(scenario.SwitchCandidateProtocol),
})
}
return nil
+27 -26
View File
@@ -10,6 +10,7 @@ import (
"github.com/pion/webrtc/v3"
"github.com/pkg/errors"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/rtc/types"
@@ -61,7 +62,7 @@ type TransportManager struct {
pendingDataChannelsPublisher []*livekit.DataChannelInfo
lastPublisherAnswer atomic.Value
lastPublisherOffer atomic.Value
iceConfig types.IceConfig
iceConfig *livekit.ICEConfig
mediaLossProxy *MediaLossProxy
udpLossUnstableCount uint32
@@ -72,7 +73,7 @@ type TransportManager struct {
onPrimaryTransportInitialConnected func()
onAnyTransportFailed func()
onICEConfigChanged func(iceConfig types.IceConfig)
onICEConfigChanged func(iceConfig *livekit.ICEConfig)
}
func NewTransportManager(params TransportManagerParams) (*TransportManager, error) {
@@ -433,27 +434,27 @@ func (t *TransportManager) NegotiateSubscriber(force bool) {
t.subscriber.Negotiate(force)
}
func (t *TransportManager) ICERestart(iceConfig *types.IceConfig) {
func (t *TransportManager) ICERestart(iceConfig *livekit.ICEConfig) {
if iceConfig != nil {
t.SetICEConfig(*iceConfig)
t.SetICEConfig(iceConfig)
}
t.subscriber.ICERestart()
}
func (t *TransportManager) OnICEConfigChanged(f func(iceConfig types.IceConfig)) {
func (t *TransportManager) OnICEConfigChanged(f func(iceConfig *livekit.ICEConfig)) {
t.lock.Lock()
t.onICEConfigChanged = f
t.lock.Unlock()
}
func (t *TransportManager) SetICEConfig(iceConfig types.IceConfig) {
func (t *TransportManager) SetICEConfig(iceConfig *livekit.ICEConfig) {
t.configureICE(iceConfig, true)
}
func (t *TransportManager) configureICE(iceConfig types.IceConfig, reset bool) {
func (t *TransportManager) configureICE(iceConfig *livekit.ICEConfig, reset bool) {
t.lock.Lock()
if t.iceConfig == iceConfig {
if proto.Equal(t.iceConfig, iceConfig) {
t.lock.Unlock()
return
}
@@ -465,12 +466,12 @@ func (t *TransportManager) configureICE(iceConfig types.IceConfig, reset bool) {
t.isTransportReconfigured = !reset
t.lock.Unlock()
if iceConfig.PreferSub != types.PreferNone {
if iceConfig.PreferenceSubscriber != livekit.ICECandidateType_ICT_NONE {
t.mediaLossProxy.OnMediaLossUpdate(nil)
}
t.publisher.SetPreferTCP(iceConfig.PreferPub == types.PreferTcp)
t.subscriber.SetPreferTCP(iceConfig.PreferSub == types.PreferTcp)
t.publisher.SetPreferTCP(iceConfig.PreferencePublisher == livekit.ICECandidateType_ICT_TCP)
t.subscriber.SetPreferTCP(iceConfig.PreferenceSubscriber == livekit.ICECandidateType_ICT_TCP)
if onICEConfigChanged != nil {
onICEConfigChanged(iceConfig)
@@ -506,22 +507,22 @@ func (t *TransportManager) handleConnectionFailed(isShortLived bool) {
}
//
// Checking only `PreferSub` field although any connection failure (PUBLISHER OR SUBSCRIBER) will
// Checking only `PreferenceSubcriber` field although any connection failure (PUBLISHER OR SUBSCRIBER) will
// flow through here.
//
// As both transports are switched to the same type on any failure, checking just subscriber should be fine.
//
getNext := func(ic types.IceConfig) types.PreferCandidateType {
if ic.PreferSub == types.PreferNone && t.params.ClientInfo.SupportsICETCP() {
return types.PreferTcp
} else if ic.PreferSub != types.PreferTls && t.params.TURNSEnabled {
return types.PreferTls
getNext := func(ic *livekit.ICEConfig) livekit.ICECandidateType {
if ic.PreferenceSubscriber == livekit.ICECandidateType_ICT_NONE && t.params.ClientInfo.SupportsICETCP() {
return livekit.ICECandidateType_ICT_TCP
} else if ic.PreferenceSubscriber != livekit.ICECandidateType_ICT_TLS && t.params.TURNSEnabled {
return livekit.ICECandidateType_ICT_TLS
} else {
return types.PreferNone
return livekit.ICECandidateType_ICT_NONE
}
}
var preferNext types.PreferCandidateType
var preferNext livekit.ICECandidateType
if isShortLived {
preferNext = getNext(t.iceConfig)
} else {
@@ -534,7 +535,7 @@ func (t *TransportManager) handleConnectionFailed(isShortLived bool) {
preferNext = getNext(t.iceConfig)
}
if preferNext == t.iceConfig.PreferSub {
if preferNext == t.iceConfig.PreferenceSubscriber {
t.lock.Unlock()
return
}
@@ -543,21 +544,21 @@ func (t *TransportManager) handleConnectionFailed(isShortLived bool) {
t.lock.Unlock()
switch preferNext {
case types.PreferTcp:
case livekit.ICECandidateType_ICT_TCP:
t.params.Logger.Infow("prefer TCP transport on both peer connections")
case types.PreferTls:
case livekit.ICECandidateType_ICT_TLS:
t.params.Logger.Infow("prefer TLS transport both peer connections")
case types.PreferNone:
case livekit.ICECandidateType_ICT_NONE:
t.params.Logger.Infow("allowing all transports on both peer connections")
}
// 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.configureICE(types.IceConfig{
PreferPub: preferNext,
PreferSub: preferNext,
t.configureICE(&livekit.ICEConfig{
PreferenceSubscriber: preferNext,
PreferencePublisher: preferNext,
}, false)
}
+3 -16
View File
@@ -207,19 +207,6 @@ type Participant interface {
DebugInfo() map[string]interface{}
}
type PreferCandidateType int
const (
PreferNone PreferCandidateType = iota
PreferTcp
PreferTls
)
type IceConfig struct {
PreferSub PreferCandidateType
PreferPub PreferCandidateType
}
// -------------------------------------------------------
type ICEConnectionType string
@@ -269,7 +256,7 @@ type LocalParticipant interface {
HandleAnswer(sdp webrtc.SessionDescription)
Negotiate(force bool)
ICERestart(iceConfig *IceConfig)
ICERestart(iceConfig *livekit.ICEConfig)
AddTrackToSubscriber(trackLocal webrtc.TrackLocal, params AddTrackParams) (*webrtc.RTPSender, *webrtc.RTPTransceiver, error)
AddTransceiverFromTrackToSubscriber(trackLocal webrtc.TrackLocal, params AddTrackParams) (*webrtc.RTPSender, *webrtc.RTPTransceiver, error)
RemoveTrackFromSubscriber(sender *webrtc.RTPSender) error
@@ -335,8 +322,8 @@ type LocalParticipant interface {
ProcessSubscriptionRequestsQueue(trackID livekit.TrackID)
ClearInProgressAndProcessSubscriptionRequestsQueue(trackID livekit.TrackID)
SetICEConfig(iceConfig IceConfig)
OnICEConfigChanged(callback func(participant LocalParticipant, iceConfig IceConfig))
SetICEConfig(iceConfig *livekit.ICEConfig)
OnICEConfigChanged(callback func(participant LocalParticipant, iceConfig *livekit.ICEConfig))
UpdateSubscribedQuality(nodeID livekit.NodeID, trackID livekit.TrackID, maxQualities []SubscribedCodecQuality) error
UpdateMediaLoss(nodeID livekit.NodeID, trackID livekit.TrackID, fractionalLoss uint32) error
@@ -340,10 +340,10 @@ type FakeLocalParticipant struct {
hiddenReturnsOnCall map[int]struct {
result1 bool
}
ICERestartStub func(*types.IceConfig)
ICERestartStub func(*livekit.ICEConfig)
iCERestartMutex sync.RWMutex
iCERestartArgsForCall []struct {
arg1 *types.IceConfig
arg1 *livekit.ICEConfig
}
IDStub func() livekit.ParticipantID
iDMutex sync.RWMutex
@@ -448,10 +448,10 @@ type FakeLocalParticipant struct {
onDataPacketArgsForCall []struct {
arg1 func(types.LocalParticipant, *livekit.DataPacket)
}
OnICEConfigChangedStub func(func(participant types.LocalParticipant, iceConfig types.IceConfig))
OnICEConfigChangedStub func(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig))
onICEConfigChangedMutex sync.RWMutex
onICEConfigChangedArgsForCall []struct {
arg1 func(participant types.LocalParticipant, iceConfig types.IceConfig)
arg1 func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)
}
OnParticipantUpdateStub func(func(types.LocalParticipant))
onParticipantUpdateMutex sync.RWMutex
@@ -607,10 +607,10 @@ type FakeLocalParticipant struct {
sendSpeakerUpdateReturnsOnCall map[int]struct {
result1 error
}
SetICEConfigStub func(types.IceConfig)
SetICEConfigStub func(*livekit.ICEConfig)
setICEConfigMutex sync.RWMutex
setICEConfigArgsForCall []struct {
arg1 types.IceConfig
arg1 *livekit.ICEConfig
}
SetMetadataStub func(string)
setMetadataMutex sync.RWMutex
@@ -2464,10 +2464,10 @@ func (fake *FakeLocalParticipant) HiddenReturnsOnCall(i int, result1 bool) {
}{result1}
}
func (fake *FakeLocalParticipant) ICERestart(arg1 *types.IceConfig) {
func (fake *FakeLocalParticipant) ICERestart(arg1 *livekit.ICEConfig) {
fake.iCERestartMutex.Lock()
fake.iCERestartArgsForCall = append(fake.iCERestartArgsForCall, struct {
arg1 *types.IceConfig
arg1 *livekit.ICEConfig
}{arg1})
stub := fake.ICERestartStub
fake.recordInvocation("ICERestart", []interface{}{arg1})
@@ -2483,13 +2483,13 @@ func (fake *FakeLocalParticipant) ICERestartCallCount() int {
return len(fake.iCERestartArgsForCall)
}
func (fake *FakeLocalParticipant) ICERestartCalls(stub func(*types.IceConfig)) {
func (fake *FakeLocalParticipant) ICERestartCalls(stub func(*livekit.ICEConfig)) {
fake.iCERestartMutex.Lock()
defer fake.iCERestartMutex.Unlock()
fake.ICERestartStub = stub
}
func (fake *FakeLocalParticipant) ICERestartArgsForCall(i int) *types.IceConfig {
func (fake *FakeLocalParticipant) ICERestartArgsForCall(i int) *livekit.ICEConfig {
fake.iCERestartMutex.RLock()
defer fake.iCERestartMutex.RUnlock()
argsForCall := fake.iCERestartArgsForCall[i]
@@ -3065,10 +3065,10 @@ func (fake *FakeLocalParticipant) OnDataPacketArgsForCall(i int) func(types.Loca
return argsForCall.arg1
}
func (fake *FakeLocalParticipant) OnICEConfigChanged(arg1 func(participant types.LocalParticipant, iceConfig types.IceConfig)) {
func (fake *FakeLocalParticipant) OnICEConfigChanged(arg1 func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)) {
fake.onICEConfigChangedMutex.Lock()
fake.onICEConfigChangedArgsForCall = append(fake.onICEConfigChangedArgsForCall, struct {
arg1 func(participant types.LocalParticipant, iceConfig types.IceConfig)
arg1 func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)
}{arg1})
stub := fake.OnICEConfigChangedStub
fake.recordInvocation("OnICEConfigChanged", []interface{}{arg1})
@@ -3084,13 +3084,13 @@ func (fake *FakeLocalParticipant) OnICEConfigChangedCallCount() int {
return len(fake.onICEConfigChangedArgsForCall)
}
func (fake *FakeLocalParticipant) OnICEConfigChangedCalls(stub func(func(participant types.LocalParticipant, iceConfig types.IceConfig))) {
func (fake *FakeLocalParticipant) OnICEConfigChangedCalls(stub func(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig))) {
fake.onICEConfigChangedMutex.Lock()
defer fake.onICEConfigChangedMutex.Unlock()
fake.OnICEConfigChangedStub = stub
}
func (fake *FakeLocalParticipant) OnICEConfigChangedArgsForCall(i int) func(participant types.LocalParticipant, iceConfig types.IceConfig) {
func (fake *FakeLocalParticipant) OnICEConfigChangedArgsForCall(i int) func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) {
fake.onICEConfigChangedMutex.RLock()
defer fake.onICEConfigChangedMutex.RUnlock()
argsForCall := fake.onICEConfigChangedArgsForCall[i]
@@ -3979,10 +3979,10 @@ func (fake *FakeLocalParticipant) SendSpeakerUpdateReturnsOnCall(i int, result1
}{result1}
}
func (fake *FakeLocalParticipant) SetICEConfig(arg1 types.IceConfig) {
func (fake *FakeLocalParticipant) SetICEConfig(arg1 *livekit.ICEConfig) {
fake.setICEConfigMutex.Lock()
fake.setICEConfigArgsForCall = append(fake.setICEConfigArgsForCall, struct {
arg1 types.IceConfig
arg1 *livekit.ICEConfig
}{arg1})
stub := fake.SetICEConfigStub
fake.recordInvocation("SetICEConfig", []interface{}{arg1})
@@ -3998,13 +3998,13 @@ func (fake *FakeLocalParticipant) SetICEConfigCallCount() int {
return len(fake.setICEConfigArgsForCall)
}
func (fake *FakeLocalParticipant) SetICEConfigCalls(stub func(types.IceConfig)) {
func (fake *FakeLocalParticipant) SetICEConfigCalls(stub func(*livekit.ICEConfig)) {
fake.setICEConfigMutex.Lock()
defer fake.setICEConfigMutex.Unlock()
fake.SetICEConfigStub = stub
}
func (fake *FakeLocalParticipant) SetICEConfigArgsForCall(i int) types.IceConfig {
func (fake *FakeLocalParticipant) SetICEConfigArgsForCall(i int) *livekit.ICEConfig {
fake.setICEConfigMutex.RLock()
defer fake.setICEConfigMutex.RUnlock()
argsForCall := fake.setICEConfigArgsForCall[i]
+5 -5
View File
@@ -31,7 +31,7 @@ const (
)
type iceConfigCacheEntry struct {
iceConfig types.IceConfig
iceConfig *livekit.ICEConfig
modifiedAt time.Time
}
@@ -323,7 +323,7 @@ func (r *RoomManager) StartSession(
opts := rtc.ParticipantOptions{
AutoSubscribe: pi.AutoSubscribe,
}
if err = room.Join(participant, &opts, r.iceServersForRoom(protoRoom, iceConfig.PreferSub == types.PreferTls)); err != nil {
if err = room.Join(participant, &opts, r.iceServersForRoom(protoRoom, iceConfig.PreferenceSubscriber == livekit.ICECandidateType_ICT_TLS)); err != nil {
pLogger.Errorw("could not join room", err)
_ = participant.Close(true, types.ParticipantCloseReasonJoinFailed)
return err
@@ -364,7 +364,7 @@ func (r *RoomManager) StartSession(
logger.Errorw("could not refresh token", err)
}
})
participant.OnICEConfigChanged(func(participant types.LocalParticipant, iceConfig types.IceConfig) {
participant.OnICEConfigChanged(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) {
r.lock.Lock()
r.iceConfigCache[participant.Identity()] = &iceConfigCacheEntry{
iceConfig: iceConfig,
@@ -687,13 +687,13 @@ func (r *RoomManager) refreshToken(participant types.LocalParticipant) error {
return nil
}
func (r *RoomManager) setIceConfig(participant types.LocalParticipant) types.IceConfig {
func (r *RoomManager) setIceConfig(participant types.LocalParticipant) *livekit.ICEConfig {
r.lock.Lock()
iceConfigCacheEntry, ok := r.iceConfigCache[participant.Identity()]
if !ok || time.Since(iceConfigCacheEntry.modifiedAt) > iceConfigTTL {
delete(r.iceConfigCache, participant.Identity())
r.lock.Unlock()
return types.IceConfig{}
return &livekit.ICEConfig{}
}
r.lock.Unlock()