Skip to content

Commit 7ce4342

Browse files
committed
Enable renegotation
Roughly following https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/ but using @negotiationInProcess instead of @peerConnection.signalingState to make the process a bit more readable/understandable. Disadvantage: Rollbacks are quite new (half a year in chrome, bit more in Firefox). But: Races should be rare and all other solutions would require a protocol change and would make the process slower.
1 parent d03388b commit 7ce4342

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

coffee/remote_peer.coffee

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class palava.RemotePeer extends palava.Peer
2929
@setupPeerConnection(offers)
3030
@setupDistributor()
3131

32+
@offers = offers
3233
if offers
3334
@sendOffer()
3435

@@ -84,6 +85,10 @@ class palava.RemotePeer extends palava.Peer
8485
@ready = false
8586
@emit 'stream_removed'
8687

88+
@peerConnection.onnegotiationneeded = () =>
89+
@negotiationInProcess = true
90+
@sendOffer()
91+
8792
@peerConnection.oniceconnectionstatechange = (event) =>
8893
connectionState = event.target.iceConnectionState
8994

@@ -156,12 +161,19 @@ class palava.RemotePeer extends palava.Peer
156161
@peerConnection.addIceCandidate(candidate)
157162

158163
@distributor.on 'offer', (msg) =>
164+
if @negotiationInProcess
165+
# we're in a race (both peers sent an offer at the same time)
166+
# case 1: we have offer precedence, thus we throw away the other offer
167+
return if @offers
168+
# case 2: the peer has offer precende, thus we back off and roll back
169+
@peerConnection.setLocalDescription({type: "rollback"})
159170
@peerConnection.setRemoteDescription(new RTCSessionDescription(msg.sdp))
160171
@emit 'offer' # ignored so far
161172
@sendAnswer()
162173

163174
@distributor.on 'answer', (msg) =>
164175
@peerConnection.setRemoteDescription(new RTCSessionDescription(msg.sdp))
176+
@negotiationInProcess = false
165177
@emit 'answer' # ignored so far
166178

167179
@distributor.on 'peer_updated_status', (msg) =>

0 commit comments

Comments
 (0)