[webrtc-pc] setRemoteDescription and ontrack order of events: SRD before ontrack?

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

== setRemoteDescription and ontrack order of events: SRD before ontrack? ==
TL;DR: Should `setRemoteDescription` be resolved before the resulting `ontrack` fires for remote tracks or after in the same task? @jan-ivar @taylor-b 

`setRemoteDescription` does, in parallel to returning a promise, a number of things. On success this includes:
```
1. Process remote tracks.
   1a. Queue a task to fire an event named track.
2. Resolve the promise.
```
As described, the promise would be resolved before `ontrack` fires. Invoking `then()` of a resolved promise means the resolver is not executed immediately, but [at the end of the current run of the JavaScript event loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Timing), which is still before any tasks queued for the next JavaScript event loop. We get this order of events:
```
1. Synchronous code after our setRemoteDescription() call is executed as part of
   the current loop.
2. resolve is called (setRemoteDescription(...).then(resolve)) at the end of
   the current loop.
3. ontrack fires in the next loop.
```

Question: Is this on purpose? setRemoteDescription happens before tracks are fired.

If this isn't on purpose it's not obvious how to make the ontrack events fire first. If we don't queue them but fire them immediately, they're fired before we've completed all steps of setRemoteDescription, so we may be in an incomplete state. If we queue a task to resolve the promise then it would be possible to run a task in-between calling setRemoteDescription having had its effect and before the promise being resolved, which might yield unexpected behavior when examining the pc.

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

Received on Tuesday, 25 July 2017 09:29:30 UTC