Skip to content

Commit

Permalink
Merge pull request #2492 from jan-ivar/collision
Browse files Browse the repository at this point in the history
Refactor collision example to avoid assignment in if statement.
  • Loading branch information
jan-ivar committed Mar 22, 2020
2 parents b8e07b8 + 1c3fc89 commit 544a656
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions webrtc.html
Original file line number Diff line number Diff line change
Expand Up @@ -11497,29 +11497,32 @@ <h3>Perfect Negotiation Example</h3>
// - The perfect negotiation logic, separated from the rest of the application ---

// keep track of some negotiation state to prevent races and errors
let offering = false, ignoredOffer = false;
let makingOffer = false, ignoreOffer = false;

// send any ice candidates to the other peer
pc.onicecandidate = ({candidate}) =&gt; signaling.send({candidate});

// let the "negotiationneeded" event trigger offer generation
pc.onnegotiationneeded = async () => {
try {
offering = true;
makingOffer = true;
await pc.setLocalDescription();
signaling.send({description: pc.localDescription});
} catch (err) {
console.error(err);
} finally {
offering = false;
makingOffer = false;
}
};

signaling.onmessage = async ({data: {description, candidate}}) => {
try {
if (description) {
const collision = pc.signalingState != "stable" || offering;
if (ignoredOffer = !polite && description.type == "offer" && collision) {
const offerCollision = description.type == "offer" &&
(makingOffer || pc.signalingState != "stable");

ignoreOffer = !polite && offerCollision;
if (ignoreOffer) {
return;
}
await pc.setRemoteDescription(description); // SRD rolls back as needed
Expand All @@ -11531,7 +11534,7 @@ <h3>Perfect Negotiation Example</h3>
try {
await pc.addIceCandidate(candidate);
} catch (err) {
if (!ignoredOffer) throw err; // Suppress ignored offer's candidates
if (!ignoreOffer) throw err; // Suppress ignored offer's candidates
}
}
} catch (err) {
Expand All @@ -11543,7 +11546,7 @@ <h3>Perfect Negotiation Example</h3>
of {{RTCPeerConnection/setLocalDescription}} (without arguments) and
{{RTCPeerConnection/setRemoteDescription}} (with implicit rollback) to avoid
races with other signaling messages being serviced.</p>
<p>The <var>ignoredOffer</var> variable is needed, because
<p>The <var>ignoreOffer</var> variable is needed, because
the {{RTCPeerConnection}} object on the impolite side is never
told about ignored offers. We must therefore suppress errors from
incoming candidates belonging to such offers.</p>
Expand Down

0 comments on commit 544a656

Please sign in to comment.