Skip to content

Commit

Permalink
Enable renegotation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
farao committed Jun 10, 2020
1 parent d03388b commit 7ce4342
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions coffee/remote_peer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class palava.RemotePeer extends palava.Peer
@setupPeerConnection(offers)
@setupDistributor()

@offers = offers
if offers
@sendOffer()

Expand Down Expand Up @@ -84,6 +85,10 @@ class palava.RemotePeer extends palava.Peer
@ready = false
@emit 'stream_removed'

@peerConnection.onnegotiationneeded = () =>
@negotiationInProcess = true
@sendOffer()

@peerConnection.oniceconnectionstatechange = (event) =>
connectionState = event.target.iceConnectionState

Expand Down Expand Up @@ -156,12 +161,19 @@ class palava.RemotePeer extends palava.Peer
@peerConnection.addIceCandidate(candidate)

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

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

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

0 comments on commit 7ce4342

Please sign in to comment.