Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
t-mullen committed Aug 8, 2020
1 parent c07d23b commit 9627c2a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 17 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Peer extends stream.Duplex {

this.initiator = opts.initiator || false
this.channelConfig = opts.channelConfig || Peer.channelConfig
this.negotiated = this.channelConfig.negotiated
this.channelNegotiated = this.channelConfig.negotiated
this.config = Object.assign({}, Peer.config, opts.config)
this.offerOptions = opts.offerOptions || {}
this.answerOptions = opts.answerOptions || {}
Expand Down Expand Up @@ -86,12 +86,12 @@ class Peer extends stream.Duplex {
this._channel = null
this._pendingCandidates = []

this._isNegotiating = this.negotiated ? false : !this.initiator // is this peer waiting for negotiation to complete?
this._isNegotiating = false // is this peer waiting for negotiation to complete?
this._firstNegotiation = true
this._batchedNegotiation = false // batch synchronous negotiations
this._queuedNegotiation = false // is there a queued negotiation request?
this._sendersAwaitingStable = []
this._senderMap = new Map()
this._firstStable = true
this._closingInterval = null

this._remoteTracks = []
Expand Down Expand Up @@ -134,7 +134,7 @@ class Peer extends stream.Duplex {
// - onfingerprintfailure
// - onnegotiationneeded

if (this.initiator || this.negotiated) {
if (this.initiator || this.channelNegotiated) {
this._setupData({
channel: this._pc.createDataChannel(this.channelName, this.channelConfig)
})
Expand All @@ -153,9 +153,8 @@ class Peer extends stream.Duplex {
this._onTrack(event)
}

if (this.initiator) {
this._needsNegotiation()
}
this._debug('initial negotiation')
this._needsNegotiation()

this._onFinishBound = () => {
this._onFinish()
Expand Down Expand Up @@ -367,8 +366,13 @@ class Peer extends stream.Duplex {
this._batchedNegotiation = true
queueMicrotask(() => {
this._batchedNegotiation = false
this._debug('starting batched negotiation')
this.negotiate()
if (this.initiator || !this._firstNegotiation) {
this._debug('starting batched negotiation')
this.negotiate()
} else {
this._debug('non-initiator initial negotiation request discarded')
}
this._firstNegotiation = false
})
}

Expand Down Expand Up @@ -881,7 +885,7 @@ class Peer extends stream.Duplex {
_onSignalingStateChange () {
if (this.destroyed) return

if (this._pc.signalingState === 'stable' && !this._firstStable) {
if (this._pc.signalingState === 'stable') {
this._isNegotiating = false

// HACK: Firefox doesn't yet support removing tracks when signalingState !== 'stable'
Expand All @@ -896,12 +900,11 @@ class Peer extends stream.Duplex {
this._debug('flushing negotiation queue')
this._queuedNegotiation = false
this._needsNegotiation() // negotiate again
} else {
this._debug('negotiate')
this.emit('negotiate')
}

this._debug('negotiate')
this.emit('negotiate')
}
this._firstStable = false

this._debug('signalingStateChange %s', this._pc.signalingState)
this.emit('signalingStateChange', this._pc.signalingState)
Expand Down
10 changes: 8 additions & 2 deletions test/negotiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ test('single negotiation', function (t) {
var peer1 = new Peer({ config, initiator: true, stream: common.getMediaStream(), wrtc: common.wrtc })
var peer2 = new Peer({ config, stream: common.getMediaStream(), wrtc: common.wrtc })

peer1.on('signal', function (data) { if (!peer2.destroyed) peer2.signal(data) })
peer2.on('signal', function (data) { if (!peer1.destroyed) peer1.signal(data) })
peer1.on('signal', function (data) {
if (data.renegotiate) t.fail('got unexpected request to renegotiate')
if (!peer2.destroyed) peer2.signal(data)
})
peer2.on('signal', function (data) {
if (data.renegotiate) t.fail('got unexpected request to renegotiate')
if (!peer1.destroyed) peer1.signal(data)
})

peer1.on('connect', function () {
t.pass('peer1 connected')
Expand Down

0 comments on commit 9627c2a

Please sign in to comment.