diff --git a/pkg/rtc/transport.go b/pkg/rtc/transport.go index de4f47dbf..860cc3551 100644 --- a/pkg/rtc/transport.go +++ b/pkg/rtc/transport.go @@ -1790,8 +1790,15 @@ func (t *PCTransport) doICERestart() error { return nil } + // if restart is requested, but negotiation never started + iceGatheringState := t.pc.ICEGatheringState() + if iceGatheringState == webrtc.ICEGatheringStateNew { + t.params.Logger.Debugw("skipping ICE restart on not yet started peer connection") + return nil + } + // if restart is requested, and we are not ready, then continue afterwards - if t.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering { + if iceGatheringState == webrtc.ICEGatheringStateGathering { t.params.Logger.Debugw("deferring ICE restart to after gathering") t.restartAfterGathering = true return nil diff --git a/pkg/rtc/transport_test.go b/pkg/rtc/transport_test.go index ab5cd5a88..c80b206ef 100644 --- a/pkg/rtc/transport_test.go +++ b/pkg/rtc/transport_test.go @@ -315,7 +315,7 @@ func TestFirstAnswerMissedDuringICERestart(t *testing.T) { handlerA.OnOfferCalls(func(sd webrtc.SessionDescription) error { offerCount.Inc() - // the second offer is a ice restart offer, so we wait transportB complete the ice gathering + // the second offer is a ice restart offer, so we wait for transportB to complete ICE gathering if transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateGathering { require.Eventually(t, func() bool { return transportB.pc.ICEGatheringState() == webrtc.ICEGatheringStateComplete