[webrtc-pc] Better API for rollback that's glare-proof (#2166)

jan-ivar has just created a new issue for https://github.com/w3c/webrtc-pc:

== Better API for rollback that's glare-proof ==
In [my latest blog](https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/) on *"Perfect negotiation in WebRTC"*, I cover using `negotiationneeded` and `"rollback"` in combination to abstract away negotiation. I describe implementing something I call "the polite peer" (a peer that rolls back in the face of an incoming offer):
```js
io.onmessage = async ({data: {description, candidate}}) => {
  if (description) {
    if (description.type == "offer" && pc.signalingState == "have-local-offer") {
      if (!polite) return;
      await Promise.all([
        pc.setLocalDescription({type: "rollback"}),
        pc.setRemoteDescription(description)
      ]);
    } else {
      await pc.setRemoteDescription(description);
    }
    if (description.type == "offer") {
      await pc.setLocalDescription(await pc.createAnswer());
      send({description: pc.localDescription});
    }
  } else if (candidate) await pc.addIceCandidate(candidate);
}
```
In the blog I cover in depth why the `Promise.all` is needed, but in short, it's there to cover a timing-window where we'd otherwise be vulnerable to *addIceCandidate* getting queued ahead of *setRemoteDescription* and failing.

To make this less of a footgun I think we should come up with a safer rollback API that does the rollback and sets the new description on the same run of the JS event loop. E.g.
```js
pc.rollback();
await pc.setRemoteDescription(description);
```
or perhaps
```js
await pc.setRemoteDescription(description, {rollback: true});
```
Open to other ideas.

Please view or discuss this issue at https://github.com/w3c/webrtc-pc/issues/2166 using your GitHub account

Received on Sunday, 7 April 2019 21:31:46 UTC