Recover from retry on ICE restart (#798)
This commit is contained in:
+64
-41
@@ -50,8 +50,9 @@ type SimulcastTrackInfo struct {
|
|||||||
|
|
||||||
// PCTransport is a wrapper around PeerConnection, with some helper methods
|
// PCTransport is a wrapper around PeerConnection, with some helper methods
|
||||||
type PCTransport struct {
|
type PCTransport struct {
|
||||||
pc *webrtc.PeerConnection
|
params TransportParams
|
||||||
me *webrtc.MediaEngine
|
pc *webrtc.PeerConnection
|
||||||
|
me *webrtc.MediaEngine
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
pendingCandidates []webrtc.ICECandidateInit
|
pendingCandidates []webrtc.ICECandidateInit
|
||||||
@@ -63,8 +64,6 @@ type PCTransport struct {
|
|||||||
// stream allocator for subscriber PC
|
// stream allocator for subscriber PC
|
||||||
streamAllocator *sfu.StreamAllocator
|
streamAllocator *sfu.StreamAllocator
|
||||||
|
|
||||||
logger logger.Logger
|
|
||||||
|
|
||||||
previousAnswer *webrtc.SessionDescription
|
previousAnswer *webrtc.SessionDescription
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,20 +180,10 @@ func newPeerConnection(params TransportParams, onBandwidthEstimator func(estimat
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
||||||
var bwe cc.BandwidthEstimator
|
|
||||||
pc, me, err := newPeerConnection(params, func(estimator cc.BandwidthEstimator) {
|
|
||||||
bwe = estimator
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
t := &PCTransport{
|
t := &PCTransport{
|
||||||
pc: pc,
|
params: params,
|
||||||
me: me,
|
|
||||||
debouncedNegotiate: debounce.New(negotiationFrequency),
|
debouncedNegotiate: debounce.New(negotiationFrequency),
|
||||||
negotiationState: negotiationStateNone,
|
negotiationState: negotiationStateNone,
|
||||||
logger: params.Logger,
|
|
||||||
}
|
}
|
||||||
if params.Target == livekit.SignalTarget_SUBSCRIBER {
|
if params.Target == livekit.SignalTarget_SUBSCRIBER {
|
||||||
t.streamAllocator = sfu.NewStreamAllocator(sfu.StreamAllocatorParams{
|
t.streamAllocator = sfu.NewStreamAllocator(sfu.StreamAllocatorParams{
|
||||||
@@ -202,26 +191,47 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
|||||||
Logger: params.Logger,
|
Logger: params.Logger,
|
||||||
})
|
})
|
||||||
t.streamAllocator.Start()
|
t.streamAllocator.Start()
|
||||||
if bwe != nil {
|
|
||||||
t.streamAllocator.SetBandwidthEstimator(bwe)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := t.createPeerConnection(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *PCTransport) createPeerConnection() error {
|
||||||
|
var bwe cc.BandwidthEstimator
|
||||||
|
pc, me, err := newPeerConnection(t.params, func(estimator cc.BandwidthEstimator) {
|
||||||
|
bwe = estimator
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
t.pc = pc
|
||||||
t.pc.OnICEGatheringStateChange(func(state webrtc.ICEGathererState) {
|
t.pc.OnICEGatheringStateChange(func(state webrtc.ICEGathererState) {
|
||||||
if state == webrtc.ICEGathererStateComplete {
|
if state == webrtc.ICEGathererStateComplete {
|
||||||
go func() {
|
go func() {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
if t.restartAfterGathering {
|
if t.restartAfterGathering {
|
||||||
params.Logger.Debugw("restarting ICE after ICE gathering")
|
t.params.Logger.Debugw("restarting ICE after ICE gathering")
|
||||||
if err := t.createAndSendOffer(&webrtc.OfferOptions{ICERestart: true}); err != nil {
|
if err := t.createAndSendOffer(&webrtc.OfferOptions{ICERestart: true}); err != nil {
|
||||||
params.Logger.Warnw("could not restart ICE", err)
|
t.params.Logger.Warnw("could not restart ICE", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return t, nil
|
t.me = me
|
||||||
|
|
||||||
|
if bwe != nil && t.streamAllocator != nil {
|
||||||
|
t.streamAllocator.SetBandwidthEstimator(bwe)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error {
|
func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error {
|
||||||
@@ -232,7 +242,7 @@ func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.logger.Debugw("add candidate ", "candidate", candidate.Candidate)
|
t.params.Logger.Debugw("add candidate ", "candidate", candidate.Candidate)
|
||||||
|
|
||||||
return t.pc.AddICECandidate(candidate)
|
return t.pc.AddICECandidate(candidate)
|
||||||
}
|
}
|
||||||
@@ -275,9 +285,9 @@ func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
|
|||||||
|
|
||||||
// only initiate when we are the offerer
|
// only initiate when we are the offerer
|
||||||
if lastState == negotiationRetry && sd.Type == webrtc.SDPTypeAnswer {
|
if lastState == negotiationRetry && sd.Type == webrtc.SDPTypeAnswer {
|
||||||
t.logger.Debugw("re-negotiate after answering")
|
t.params.Logger.Debugw("re-negotiate after receiving answer")
|
||||||
if err := t.createAndSendOffer(nil); err != nil {
|
if err := t.createAndSendOffer(nil); err != nil {
|
||||||
t.logger.Errorw("could not negotiate", err)
|
t.params.Logger.Errorw("could not negotiate", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -294,12 +304,12 @@ func (t *PCTransport) Negotiate(force bool) {
|
|||||||
// no op to cancel pending negotiation
|
// no op to cancel pending negotiation
|
||||||
})
|
})
|
||||||
if err := t.CreateAndSendOffer(nil); err != nil {
|
if err := t.CreateAndSendOffer(nil); err != nil {
|
||||||
t.logger.Errorw("could not negotiate", err)
|
t.params.Logger.Errorw("could not negotiate", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
t.debouncedNegotiate(func() {
|
t.debouncedNegotiate(func() {
|
||||||
if err := t.CreateAndSendOffer(nil); err != nil {
|
if err := t.CreateAndSendOffer(nil); err != nil {
|
||||||
t.logger.Errorw("could not negotiate", err)
|
t.params.Logger.Errorw("could not negotiate", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -325,30 +335,43 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
// if restart is requested, and we are not ready, then continue afterwards
|
// if restart is requested, and we are not ready, then continue afterwards
|
||||||
if iceRestart {
|
if iceRestart {
|
||||||
if t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {
|
if t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {
|
||||||
t.logger.Debugw("restart ICE after gathering")
|
t.params.Logger.Debugw("restart ICE after gathering")
|
||||||
t.restartAfterGathering = true
|
t.restartAfterGathering = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
t.logger.Debugw("restarting ICE")
|
t.params.Logger.Debugw("restarting ICE")
|
||||||
}
|
}
|
||||||
|
|
||||||
// when there's an ongoing negotiation, let it finish and not disrupt its state
|
if iceRestart && t.negotiationState != negotiationStateNone {
|
||||||
if t.negotiationState == negotiationStateClient {
|
|
||||||
currentSD := t.pc.CurrentRemoteDescription()
|
currentSD := t.pc.CurrentRemoteDescription()
|
||||||
if iceRestart && currentSD != nil {
|
if currentSD == nil {
|
||||||
t.logger.Debugw("recovering from client negotiation state")
|
// never received an answer from client, create a new peer connection
|
||||||
|
t.params.Logger.Infow("no previous answer from client on ICE restart, creating a new PC")
|
||||||
|
t.pc.Close()
|
||||||
|
if err := t.createPeerConnection(); err != nil {
|
||||||
|
t.params.Logger.Errorw("could not create new PC on ICE restart", err)
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
options.ICERestart = false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// recover by re-applying the last answer
|
||||||
|
t.params.Logger.Infow("recovering from client negotiation state on ICE restart")
|
||||||
if err := t.pc.SetRemoteDescription(*currentSD); err != nil {
|
if err := t.pc.SetRemoteDescription(*currentSD); err != nil {
|
||||||
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "remote_description").Add(1)
|
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "remote_description").Add(1)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
t.logger.Debugw("skipping negotiation, trying again later")
|
} else {
|
||||||
|
// when there's an ongoing negotiation, let it finish and not disrupt its state
|
||||||
|
if t.negotiationState == negotiationStateClient {
|
||||||
|
t.params.Logger.Infow("skipping negotiation, trying again later")
|
||||||
t.negotiationState = negotiationRetry
|
t.negotiationState = negotiationRetry
|
||||||
return nil
|
return nil
|
||||||
|
} else if t.negotiationState == negotiationRetry {
|
||||||
|
// already set to retry, we can safely skip this attempt
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else if t.negotiationState == negotiationRetry {
|
|
||||||
// already set to retry, we can safely skip this attempt
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.previousAnswer != nil {
|
if t.previousAnswer != nil {
|
||||||
@@ -362,14 +385,14 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
offer, err := t.pc.CreateOffer(options)
|
offer, err := t.pc.CreateOffer(options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "create").Add(1)
|
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "create").Add(1)
|
||||||
t.logger.Errorw("could not create offer", err)
|
t.params.Logger.Errorw("could not create offer", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = t.pc.SetLocalDescription(offer)
|
err = t.pc.SetLocalDescription(offer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "local_description").Add(1)
|
prometheus.ServiceOperationCounter.WithLabelValues("offer", "error", "local_description").Add(1)
|
||||||
t.logger.Errorw("could not set local description", err)
|
t.params.Logger.Errorw("could not set local description", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,7 +494,7 @@ func (t *PCTransport) initPCWithPreviousAnswer(previousAnswer webrtc.SessionDesc
|
|||||||
// because sdp can negotiate multi times before migration.(it will sticky to the last m-line atfirst negotiate)
|
// because sdp can negotiate multi times before migration.(it will sticky to the last m-line atfirst negotiate)
|
||||||
// so use a dumb pc to negotiate sdp to fixed the datachannel's mid at same position with previous answer
|
// so use a dumb pc to negotiate sdp to fixed the datachannel's mid at same position with previous answer
|
||||||
if err := t.preparePC(previousAnswer); err != nil {
|
if err := t.preparePC(previousAnswer); err != nil {
|
||||||
t.logger.Errorw("prepare pc for migration failed", err)
|
t.params.Logger.Errorw("prepare pc for migration failed", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@@ -526,7 +549,7 @@ func (t *PCTransport) SetPreviousAnswer(answer *webrtc.SessionDescription) {
|
|||||||
if t.pc.RemoteDescription() == nil && t.previousAnswer == nil {
|
if t.pc.RemoteDescription() == nil && t.previousAnswer == nil {
|
||||||
t.previousAnswer = answer
|
t.previousAnswer = answer
|
||||||
if err := t.initPCWithPreviousAnswer(*t.previousAnswer); err != nil {
|
if err := t.initPCWithPreviousAnswer(*t.previousAnswer); err != nil {
|
||||||
t.logger.Errorw("initPCWithPreviousAnswer failed", err)
|
t.params.Logger.Errorw("initPCWithPreviousAnswer failed", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user