Skip to content

Commit

Permalink
Timeout trigger _iceComplete if candidates seen.
Browse files Browse the repository at this point in the history
On some networks the iceStateChange=complete will not fire until the
browser times out adding tens of seconds of latency even though all
valid candidates have been recieved. This patch makes _iceComplete fire
early and current offer return after some timeout if we've received
valid candidates.

See webtorrent/bittorrent-tracker#199 for further discussion.
  • Loading branch information
chr15m committed Sep 11, 2018
1 parent 21d85de commit b97979d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var randombytes = require('randombytes')
var stream = require('readable-stream')

var MAX_BUFFERED_AMOUNT = 64 * 1024
var ICECOMPLETE_TIMEOUT = 5 * 1000

inherits(Peer, stream.Duplex)

Expand Down Expand Up @@ -44,6 +45,7 @@ function Peer (opts) {
self.sdpTransform = opts.sdpTransform || function (sdp) { return sdp }
self.streams = opts.streams || (opts.stream ? [opts.stream] : []) // support old "stream" option
self.trickle = opts.trickle !== undefined ? opts.trickle : true
self.iceCompleteTimeout = opts.iceCompleteTimeout || ICECOMPLETE_TIMEOUT

self.destroyed = false
self.connected = false
Expand All @@ -69,6 +71,7 @@ function Peer (opts) {
self._pcReady = false
self._channelReady = false
self._iceComplete = false // ice candidate trickle done (got null candidate)
self._iceCompleteTimer = null // send an offer/answer anyway after some timeout
self._channel = null
self._pendingCandidates = []

Expand Down Expand Up @@ -489,6 +492,20 @@ Peer.prototype._onFinish = function () {
}
}

Peer.prototype._startIceCompleteTimeout = function () {
debug('started iceComplete timeout')
var self = this
if (self.destroyed) return
if (self._iceCompleteTimer) return
self._iceCompleteTimer = setTimeout(function () {
if (!self._iceComplete) {
self._iceComplete = true
self.emit('iceTimeout')
self.emit('_iceComplete')
}
}, this.iceCompleteTimeout)
}

Peer.prototype._createOffer = function () {
var self = this
if (self.destroyed) return
Expand Down Expand Up @@ -806,10 +823,14 @@ Peer.prototype._onIceCandidate = function (event) {
sdpMid: event.candidate.sdpMid
}
})
} else if (!event.candidate) {
} else if (!event.candidate && !self._iceComplete) {
self._iceComplete = true
self.emit('_iceComplete')
}
// as soon as we've received one valid candidate start timeout
if (event.candidate) {
self._startIceCompleteTimeout()
}
}

Peer.prototype._onChannelMessage = function (event) {
Expand Down

0 comments on commit b97979d

Please sign in to comment.