fix negotiation timing issue, potential deadlock in ICE restart
This commit is contained in:
+17
-10
@@ -96,7 +96,8 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
|||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
defer t.lock.Unlock()
|
defer t.lock.Unlock()
|
||||||
if t.restartAfterGathering {
|
if t.restartAfterGathering {
|
||||||
if err := t.CreateAndSendOffer(&webrtc.OfferOptions{ICERestart: true}); err != nil {
|
logger.Debugw("restarting ICE after ICE gathering")
|
||||||
|
if err := t.createAndSendOffer(&webrtc.OfferOptions{ICERestart: true}); err != nil {
|
||||||
logger.Warnw("could not restart ICE", err)
|
logger.Warnw("could not restart ICE", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -133,6 +134,10 @@ func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// negotiated, reset flag
|
||||||
|
lastState := t.negotiationState
|
||||||
|
t.negotiationState = negotiationStateNone
|
||||||
|
|
||||||
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
|
||||||
@@ -140,16 +145,13 @@ func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error {
|
|||||||
}
|
}
|
||||||
t.pendingCandidates = nil
|
t.pendingCandidates = nil
|
||||||
|
|
||||||
// negotiated, reset flag
|
// only initiate when we are the offerer
|
||||||
state := t.negotiationState
|
if lastState == negotiationRetry && sd.Type == webrtc.SDPTypeAnswer {
|
||||||
t.negotiationState = negotiationStateNone
|
logger.Debugw("re-negotiate after answering")
|
||||||
if state == negotiationRetry {
|
if err := t.createAndSendOffer(nil); err != nil {
|
||||||
// need to Negotiate again, do it immediately
|
|
||||||
if err := t.CreateAndSendOffer(nil); err != nil {
|
|
||||||
logger.Errorw("could not negotiate", err)
|
logger.Errorw("could not negotiate", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,6 +169,13 @@ func (t *PCTransport) Negotiate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *PCTransport) CreateAndSendOffer(options *webrtc.OfferOptions) error {
|
func (t *PCTransport) CreateAndSendOffer(options *webrtc.OfferOptions) error {
|
||||||
|
t.lock.Lock()
|
||||||
|
defer t.lock.Unlock()
|
||||||
|
return t.createAndSendOffer(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// creates and sends offer assuming lock has been acquired
|
||||||
|
func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
|
||||||
if t.onOffer == nil {
|
if t.onOffer == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -174,8 +183,6 @@ func (t *PCTransport) CreateAndSendOffer(options *webrtc.OfferOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.lock.Lock()
|
|
||||||
defer t.lock.Unlock()
|
|
||||||
iceRestart := options != nil && options.ICERestart
|
iceRestart := options != nil && options.ICERestart
|
||||||
|
|
||||||
// if restart is requested, and we are not ready, then continue afterwards
|
// if restart is requested, and we are not ready, then continue afterwards
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package rtc
|
|||||||
import (
|
import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/testutils"
|
"github.com/livekit/livekit-server/pkg/testutils"
|
||||||
livekit "github.com/livekit/livekit-server/proto"
|
livekit "github.com/livekit/livekit-server/proto"
|
||||||
@@ -76,7 +77,7 @@ func TestNegotiationTiming(t *testing.T) {
|
|||||||
handleICEExchange(t, transportA, transportB)
|
handleICEExchange(t, transportA, transportB)
|
||||||
offer := atomic.Value{}
|
offer := atomic.Value{}
|
||||||
transportA.OnOffer(func(sd webrtc.SessionDescription) {
|
transportA.OnOffer(func(sd webrtc.SessionDescription) {
|
||||||
offer.Store(sd)
|
offer.Store(&sd)
|
||||||
})
|
})
|
||||||
|
|
||||||
// initial offer
|
// initial offer
|
||||||
@@ -90,6 +91,23 @@ func TestNegotiationTiming(t *testing.T) {
|
|||||||
// third try, should've stayed at retry
|
// third try, should've stayed at retry
|
||||||
require.NoError(t, transportA.CreateAndSendOffer(nil))
|
require.NoError(t, transportA.CreateAndSendOffer(nil))
|
||||||
require.Equal(t, negotiationRetry, transportA.negotiationState)
|
require.Equal(t, negotiationRetry, transportA.negotiationState)
|
||||||
|
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
actualOffer, ok := offer.Load().(*webrtc.SessionDescription)
|
||||||
|
|
||||||
|
require.True(t, ok)
|
||||||
|
require.NoError(t, transportB.SetRemoteDescription(*actualOffer))
|
||||||
|
answer, err := transportB.pc.CreateAnswer(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, transportB.pc.SetLocalDescription(answer))
|
||||||
|
require.NoError(t, transportA.SetRemoteDescription(answer))
|
||||||
|
time.Sleep(5 * time.Millisecond)
|
||||||
|
|
||||||
|
// it should still be negotiating again
|
||||||
|
require.Equal(t, negotiationStateClient, transportA.negotiationState)
|
||||||
|
offer2, ok := offer.Load().(*webrtc.SessionDescription)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.False(t, offer2 == actualOffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
func handleOfferFunc(t *testing.T, current, other *PCTransport) func(sd webrtc.SessionDescription) {
|
||||||
|
|||||||
+1
-4
@@ -72,7 +72,7 @@ func scenarioPublishingUponJoining(t *testing.T, ports ...int) {
|
|||||||
writers = publishTracksForClients(t, c2)
|
writers = publishTracksForClients(t, c2)
|
||||||
defer stopWriters(writers...)
|
defer stopWriters(writers...)
|
||||||
|
|
||||||
success = testutils.WithTimeout(t, "new c2 tracks should be published again", func() bool {
|
testutils.WithTimeout(t, "new c2 tracks should be published again", func() bool {
|
||||||
tracks := c3.SubscribedTracks()
|
tracks := c3.SubscribedTracks()
|
||||||
if len(tracks[c2.ID()]) != 2 {
|
if len(tracks[c2.ID()]) != 2 {
|
||||||
return false
|
return false
|
||||||
@@ -82,9 +82,6 @@ func scenarioPublishingUponJoining(t *testing.T, ports ...int) {
|
|||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
if !success {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func scenarioReceiveBeforePublish(t *testing.T) {
|
func scenarioReceiveBeforePublish(t *testing.T) {
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
package version
|
package version
|
||||||
|
|
||||||
const Version = "0.9.9"
|
const Version = "0.9.10-SNAPSHOT"
|
||||||
|
|||||||
Reference in New Issue
Block a user