Skip to content

Commit 8e2c868

Browse files
cnderrauberSean-Der
authored andcommitted
Add option to disable close by dtls
Close peerconnection on DTLS.CloseNotify could break ice restart with dtls restart, when the dtls finger-print changed, the browser could teardown the old dtlstransport and establish new one then pion could close the peerconnection and restart failed. So browser don't do this and spec also don't say peerconnection should close when dtls is closed.
1 parent 2553783 commit 8e2c868

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

peerconnection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ func (pc *PeerConnection) startTransports(iceRole ICERole, dtlsRole DTLSRole, re
24172417
}
24182418

24192419
pc.dtlsTransport.internalOnCloseHandler = func() {
2420-
if pc.isClosed.get() {
2420+
if pc.isClosed.get() || pc.api.settingEngine.disableCloseByDTLS {
24212421
return
24222422
}
24232423

settingengine.go

+8
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type SettingEngine struct {
102102
receiveMTU uint
103103
iceMaxBindingRequests *uint16
104104
fireOnTrackBeforeFirstRTP bool
105+
disableCloseByDTLS bool
105106
}
106107

107108
// getReceiveMTU returns the configured MTU. If SettingEngine's MTU is configured to 0 it returns the default
@@ -501,3 +502,10 @@ func (e *SettingEngine) SetICEBindingRequestHandler(bindingRequestHandler func(m
501502
func (e *SettingEngine) SetFireOnTrackBeforeFirstRTP(fireOnTrackBeforeFirstRTP bool) {
502503
e.fireOnTrackBeforeFirstRTP = fireOnTrackBeforeFirstRTP
503504
}
505+
506+
// DisableCloseByDTLS sets if the connection should be closed when dtls transport is closed.
507+
// Setting this to true will keep the connection open when dtls transport is closed
508+
// and relies on the ice failed state to detect the connection is interrupted.
509+
func (e *SettingEngine) DisableCloseByDTLS(isEnabled bool) {
510+
e.disableCloseByDTLS = isEnabled
511+
}

settingengine_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,26 @@ func TestSetFireOnTrackBeforeFirstRTP(t *testing.T) {
394394

395395
closePairNow(t, offerer, answerer)
396396
}
397+
398+
func TestDisableCloseByDTLS(t *testing.T) {
399+
lim := test.TimeOut(time.Second * 30)
400+
defer lim.Stop()
401+
402+
report := test.CheckRoutines(t)
403+
defer report()
404+
405+
s := SettingEngine{}
406+
s.DisableCloseByDTLS(true)
407+
408+
offer, answer, err := NewAPI(WithSettingEngine(s)).newPair(Configuration{})
409+
assert.NoError(t, err)
410+
411+
assert.NoError(t, signalPair(offer, answer))
412+
413+
untilConnectionState(PeerConnectionStateConnected, offer, answer).Wait()
414+
assert.NoError(t, answer.Close())
415+
416+
time.Sleep(time.Second)
417+
assert.True(t, offer.ConnectionState() == PeerConnectionStateConnected)
418+
assert.NoError(t, offer.Close())
419+
}

0 commit comments

Comments
 (0)