refine negotiation process (#807)
* refine negotiation process * not restart during ice gathering * fix test case
This commit is contained in:
@@ -225,6 +225,8 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.subscriber.OnNegotiationFailed(p.handleNegotiationFailed)
|
||||||
|
|
||||||
p.publisher.pc.OnICECandidate(func(c *webrtc.ICECandidate) {
|
p.publisher.pc.OnICECandidate(func(c *webrtc.ICECandidate) {
|
||||||
if c == nil || p.State() == livekit.ParticipantInfo_DISCONNECTED {
|
if c == nil || p.State() == livekit.ParticipantInfo_DISCONNECTED {
|
||||||
return
|
return
|
||||||
@@ -2055,3 +2057,16 @@ func (p *ParticipantImpl) GetCachedDownTrack(trackID livekit.TrackID) (*webrtc.R
|
|||||||
|
|
||||||
return nil, sfu.ForwarderState{}
|
return nil, sfu.ForwarderState{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ParticipantImpl) handleNegotiationFailed() {
|
||||||
|
p.params.Logger.Infow("negotiation failed, notify client do full reconnect")
|
||||||
|
_ = p.writeMessage(&livekit.SignalResponse{
|
||||||
|
Message: &livekit.SignalResponse_Leave{
|
||||||
|
Leave: &livekit.LeaveRequest{
|
||||||
|
CanReconnect: true,
|
||||||
|
Reason: types.ParticipantCloseReasonNegotiateFailed.ToDisconnectReason(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
p.closeSignalConnection()
|
||||||
|
}
|
||||||
|
|||||||
+3
-1
@@ -964,7 +964,9 @@ func (r *Room) connectionQualityWorker() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
nowConnectionInfos[p.ID()] = p.GetConnectionQuality()
|
if q := p.GetConnectionQuality(); q != nil {
|
||||||
|
nowConnectionInfos[p.ID()] = q
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// send an update if there is a change
|
// send an update if there is a change
|
||||||
|
|||||||
+58
-11
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/pion/interceptor/pkg/twcc"
|
"github.com/pion/interceptor/pkg/twcc"
|
||||||
"github.com/pion/sdp/v3"
|
"github.com/pion/sdp/v3"
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
|
"go.uber.org/atomic"
|
||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
"github.com/livekit/protocol/logger"
|
"github.com/livekit/protocol/logger"
|
||||||
@@ -28,6 +29,7 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
negotiationFrequency = 150 * time.Millisecond
|
negotiationFrequency = 150 * time.Millisecond
|
||||||
|
negotiationFailedTimout = 15 * time.Second
|
||||||
dtlsRetransmissionInterval = 100 * time.Millisecond
|
dtlsRetransmissionInterval = 100 * time.Millisecond
|
||||||
|
|
||||||
iceDisconnectedTimeout = 10 * time.Second // compatible for ice-lite with firefox client
|
iceDisconnectedTimeout = 10 * time.Second // compatible for ice-lite with firefox client
|
||||||
@@ -35,6 +37,10 @@ const (
|
|||||||
iceKeepaliveInterval = 2 * time.Second // pion's default
|
iceKeepaliveInterval = 2 * time.Second // pion's default
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrIceRestartWithoutLocalSDP = errors.New("ICE restart without local SDP settled")
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
negotiationStateNone = iota
|
negotiationStateNone = iota
|
||||||
// waiting for client answer
|
// waiting for client answer
|
||||||
@@ -54,12 +60,16 @@ type PCTransport struct {
|
|||||||
pc *webrtc.PeerConnection
|
pc *webrtc.PeerConnection
|
||||||
me *webrtc.MediaEngine
|
me *webrtc.MediaEngine
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.RWMutex
|
||||||
pendingCandidates []webrtc.ICECandidateInit
|
pendingCandidates []webrtc.ICECandidateInit
|
||||||
debouncedNegotiate func(func())
|
debouncedNegotiate func(func())
|
||||||
onOffer func(offer webrtc.SessionDescription)
|
onOffer func(offer webrtc.SessionDescription)
|
||||||
restartAfterGathering bool
|
restartAfterGathering bool
|
||||||
|
restartAtNextOffer bool
|
||||||
negotiationState int
|
negotiationState int
|
||||||
|
negotiateCounter atomic.Int32
|
||||||
|
signalStateCheckTimer *time.Timer
|
||||||
|
onNegotiationFailed func()
|
||||||
|
|
||||||
// stream allocator for subscriber PC
|
// stream allocator for subscriber PC
|
||||||
streamAllocator *sfu.StreamAllocator
|
streamAllocator *sfu.StreamAllocator
|
||||||
@@ -276,6 +286,10 @@ func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
|
|||||||
lastState := t.negotiationState
|
lastState := t.negotiationState
|
||||||
t.negotiationState = negotiationStateNone
|
t.negotiationState = negotiationStateNone
|
||||||
|
|
||||||
|
if t.signalStateCheckTimer != nil {
|
||||||
|
t.signalStateCheckTimer.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
for _, c := range t.pendingCandidates {
|
for _, c := range t.pendingCandidates {
|
||||||
if err := t.pc.AddICECandidate(c); err != nil {
|
if err := t.pc.AddICECandidate(c); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -298,6 +312,10 @@ func (t *PCTransport) OnOffer(f func(sd webrtc.SessionDescription)) {
|
|||||||
t.onOffer = f
|
t.onOffer = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *PCTransport) OnNegotiationFailed(f func()) {
|
||||||
|
t.onNegotiationFailed = f
|
||||||
|
}
|
||||||
|
|
||||||
func (t *PCTransport) Negotiate(force bool) {
|
func (t *PCTransport) Negotiate(force bool) {
|
||||||
if force {
|
if force {
|
||||||
t.debouncedNegotiate(func() {
|
t.debouncedNegotiate(func() {
|
||||||
@@ -330,7 +348,7 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
iceRestart := options != nil && options.ICERestart
|
iceRestart := (options != nil && options.ICERestart) || t.restartAtNextOffer
|
||||||
|
|
||||||
// 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 {
|
||||||
@@ -345,14 +363,17 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
if iceRestart && t.negotiationState != negotiationStateNone {
|
if iceRestart && t.negotiationState != negotiationStateNone {
|
||||||
currentSD := t.pc.CurrentRemoteDescription()
|
currentSD := t.pc.CurrentRemoteDescription()
|
||||||
if currentSD == nil {
|
if currentSD == nil {
|
||||||
// never received an answer from client, create a new peer connection
|
// restart without current remote description, sent current local description again to try recover
|
||||||
t.params.Logger.Infow("no previous answer from client on ICE restart, creating a new PC")
|
offer := t.pc.LocalDescription()
|
||||||
t.pc.Close()
|
if offer == nil {
|
||||||
if err := t.createPeerConnection(); err != nil {
|
// it should not happend, check for safety
|
||||||
t.params.Logger.Errorw("could not create new PC on ICE restart", err)
|
t.params.Logger.Infow("ice restart without local offer")
|
||||||
return err
|
return ErrIceRestartWithoutLocalSDP
|
||||||
} else {
|
} else {
|
||||||
options.ICERestart = false
|
t.negotiationState = negotiationRetry
|
||||||
|
t.restartAtNextOffer = true
|
||||||
|
go t.onOffer(*offer)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// recover by re-applying the last answer
|
// recover by re-applying the last answer
|
||||||
@@ -374,12 +395,22 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.previousAnswer != nil {
|
ensureICERestart := func(options *webrtc.OfferOptions) *webrtc.OfferOptions {
|
||||||
t.previousAnswer = nil
|
|
||||||
if options == nil {
|
if options == nil {
|
||||||
options = &webrtc.OfferOptions{}
|
options = &webrtc.OfferOptions{}
|
||||||
}
|
}
|
||||||
options.ICERestart = true
|
options.ICERestart = true
|
||||||
|
return options
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.previousAnswer != nil {
|
||||||
|
t.previousAnswer = nil
|
||||||
|
options = ensureICERestart(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
if t.restartAtNextOffer {
|
||||||
|
t.restartAtNextOffer = false
|
||||||
|
options = ensureICERestart(options)
|
||||||
}
|
}
|
||||||
|
|
||||||
offer, err := t.pc.CreateOffer(options)
|
offer, err := t.pc.CreateOffer(options)
|
||||||
@@ -400,6 +431,22 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
t.negotiationState = negotiationStateClient
|
t.negotiationState = negotiationStateClient
|
||||||
t.restartAfterGathering = false
|
t.restartAfterGathering = false
|
||||||
|
|
||||||
|
negotiateVersion := t.negotiateCounter.Inc()
|
||||||
|
if t.signalStateCheckTimer != nil {
|
||||||
|
t.signalStateCheckTimer.Stop()
|
||||||
|
}
|
||||||
|
t.signalStateCheckTimer = time.AfterFunc(negotiationFailedTimout, func() {
|
||||||
|
t.lock.RLock()
|
||||||
|
failed := t.negotiationState != negotiationStateNone
|
||||||
|
t.lock.RUnlock()
|
||||||
|
if t.negotiateCounter.Load() == negotiateVersion && failed {
|
||||||
|
t.params.Logger.Infow("negotiation timeout")
|
||||||
|
if t.onNegotiationFailed != nil {
|
||||||
|
t.onNegotiationFailed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
go t.onOffer(offer)
|
go t.onOffer(offer)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+134
-1
@@ -1,12 +1,12 @@
|
|||||||
package rtc
|
package rtc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v3"
|
"github.com/pion/webrtc/v3"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/atomic"
|
|
||||||
|
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
|
|
||||||
@@ -136,6 +136,139 @@ func TestNegotiationTiming(t *testing.T) {
|
|||||||
require.False(t, offer2 == actualOffer)
|
require.False(t, offer2 == actualOffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFirstOfferMissedDuringICERestart(t *testing.T) {
|
||||||
|
params := TransportParams{
|
||||||
|
ParticipantID: "id",
|
||||||
|
ParticipantIdentity: "identity",
|
||||||
|
Target: livekit.SignalTarget_PUBLISHER,
|
||||||
|
Config: &WebRTCConfig{},
|
||||||
|
}
|
||||||
|
transportA, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = transportA.pc.CreateDataChannel("test", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
transportB, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
//first offer missed
|
||||||
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {})
|
||||||
|
require.NoError(t, transportA.CreateAndSendOffer(nil))
|
||||||
|
|
||||||
|
// exchange ICE
|
||||||
|
handleICEExchange(t, transportA, transportB)
|
||||||
|
|
||||||
|
// set offer/answer with restart ICE, will negotiate twice,
|
||||||
|
// first one is recover from missed offer
|
||||||
|
// second one is restartICE
|
||||||
|
handleOffer := handleOfferFunc(t, transportA, transportB)
|
||||||
|
var offerCount int32
|
||||||
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {
|
||||||
|
atomic.AddInt32(&offerCount, 1)
|
||||||
|
// the second offer is a ice restart offer, so we wait transportB complete the ice gathering
|
||||||
|
if transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateComplete
|
||||||
|
}, 10*time.Second, time.Millisecond*10)
|
||||||
|
}
|
||||||
|
handleOffer(sd)
|
||||||
|
})
|
||||||
|
|
||||||
|
// first establish connection
|
||||||
|
require.NoError(t, transportA.CreateAndSendOffer(&webrtc.OfferOptions{
|
||||||
|
ICERestart: true,
|
||||||
|
}))
|
||||||
|
|
||||||
|
// ensure we are connected
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return transportA.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||||
|
transportB.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||||
|
atomic.LoadInt32(&offerCount) == 2
|
||||||
|
}, testutils.ConnectTimeout, 10*time.Millisecond, "transport did not connect")
|
||||||
|
transportA.Close()
|
||||||
|
transportB.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFirstAnwserMissedDuringICERestart(t *testing.T) {
|
||||||
|
params := TransportParams{
|
||||||
|
ParticipantID: "id",
|
||||||
|
ParticipantIdentity: "identity",
|
||||||
|
Target: livekit.SignalTarget_PUBLISHER,
|
||||||
|
Config: &WebRTCConfig{},
|
||||||
|
}
|
||||||
|
transportA, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = transportA.pc.CreateDataChannel("test", nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
transportB, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
//first anwser missed
|
||||||
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {
|
||||||
|
require.NoError(t, transportB.SetRemoteDescription(sd))
|
||||||
|
answer, err := transportB.pc.CreateAnswer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, transportB.pc.SetLocalDescription(answer))
|
||||||
|
})
|
||||||
|
// exchange ICE
|
||||||
|
handleICEExchange(t, transportA, transportB)
|
||||||
|
|
||||||
|
require.NoError(t, transportA.CreateAndSendOffer(nil))
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return transportB.pc.SignalingState() == webrtc.SignalingStateStable
|
||||||
|
}, time.Second, 10*time.Millisecond)
|
||||||
|
|
||||||
|
// set offer/answer with restart ICE, will negotiate twice,
|
||||||
|
// first one is recover from missed offer
|
||||||
|
// second one is restartICE
|
||||||
|
handleOffer := handleOfferFunc(t, transportA, transportB)
|
||||||
|
var offerCount int32
|
||||||
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {
|
||||||
|
atomic.AddInt32(&offerCount, 1)
|
||||||
|
// the second offer is a ice restart offer, so we wait transportB complete the ice gathering
|
||||||
|
if transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering {
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateComplete
|
||||||
|
}, 10*time.Second, time.Millisecond*10)
|
||||||
|
}
|
||||||
|
handleOffer(sd)
|
||||||
|
})
|
||||||
|
|
||||||
|
// first establish connection
|
||||||
|
require.NoError(t, transportA.CreateAndSendOffer(&webrtc.OfferOptions{
|
||||||
|
ICERestart: true,
|
||||||
|
}))
|
||||||
|
|
||||||
|
// ensure we are connected
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return transportA.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||||
|
transportB.pc.ICEConnectionState() == webrtc.ICEConnectionStateConnected &&
|
||||||
|
atomic.LoadInt32(&offerCount) == 2
|
||||||
|
}, testutils.ConnectTimeout, 10*time.Millisecond, "transport did not connect")
|
||||||
|
transportA.Close()
|
||||||
|
transportB.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNegotiationFailed(t *testing.T) {
|
||||||
|
params := TransportParams{
|
||||||
|
ParticipantID: "id",
|
||||||
|
ParticipantIdentity: "identity",
|
||||||
|
Target: livekit.SignalTarget_PUBLISHER,
|
||||||
|
Config: &WebRTCConfig{},
|
||||||
|
}
|
||||||
|
transportA, err := NewPCTransport(params)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {})
|
||||||
|
var failed int32
|
||||||
|
transportA.OnNegotiationFailed(func() {
|
||||||
|
atomic.AddInt32(&failed, 1)
|
||||||
|
})
|
||||||
|
transportA.CreateAndSendOffer(nil)
|
||||||
|
require.Eventually(t, func() bool {
|
||||||
|
return atomic.LoadInt32(&failed) == 1
|
||||||
|
}, negotiationFailedTimout+time.Second, 10*time.Millisecond, "negotiation failed")
|
||||||
|
}
|
||||||
|
|
||||||
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
||||||
return func(sd webrtc.SessionDescription) {
|
return func(sd webrtc.SessionDescription) {
|
||||||
t.Logf("handling offer")
|
t.Logf("handling offer")
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ const (
|
|||||||
ParticipantCloseReasonSimulateMigration
|
ParticipantCloseReasonSimulateMigration
|
||||||
ParticipantCloseReasonSimulateNodeFailure
|
ParticipantCloseReasonSimulateNodeFailure
|
||||||
ParticipantCloseReasonSimulateServerLeave
|
ParticipantCloseReasonSimulateServerLeave
|
||||||
|
ParticipantCloseReasonNegotiateFailed
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p ParticipantCloseReason) String() string {
|
func (p ParticipantCloseReason) String() string {
|
||||||
@@ -118,6 +119,8 @@ func (p ParticipantCloseReason) String() string {
|
|||||||
return "SIMULATE_NODE_FAILURE"
|
return "SIMULATE_NODE_FAILURE"
|
||||||
case ParticipantCloseReasonSimulateServerLeave:
|
case ParticipantCloseReasonSimulateServerLeave:
|
||||||
return "SIMULATE_SERVER_LEAVE"
|
return "SIMULATE_SERVER_LEAVE"
|
||||||
|
case ParticipantCloseReasonNegotiateFailed:
|
||||||
|
return "NEGOTIATE_FAILED"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d", int(p))
|
return fmt.Sprintf("%d", int(p))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user