-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Out-of-main-thread processing by default #23
Comments
The option of using w3c/mediacapture-extensions#16 also would allow getting access on the main thread. |
It'd let us limit new MST access methods to workers, so users have to transfer them to a worker to do such processing.
I think it's generally understood that the main-thread is overworked and underpaid. It's a particularly poor environment for realtime (low-latency) processing due to the open-ended number of tasks that may be queued on it at certain times, which can lead to ms delays even to get on it. This is why "The main-thread is completely unpredictable". Newer devices have increasingly tighter FPS deadlines to meet, so we shouldn't design something that cannot meet it reliably.
I don't see how it would be. Worst-case, streams can be transferred from workers to main-thread, no worse than what was proposed the other direction. |
True. But is the any precedent for such an API design?.
Yes. And developers know that, so they are in a good position to weigh the tradeoffs. I don't think we should make the decision for them. Moreover, the reality today is that many different types of applications, from simple examples to industrial-strength VC systems are using the main thread today to do this type of processing and it works.
I agree that transferring tracks would not result in a particularly complex API. My comment was before we had decided to move in that direction. |
Audio worklet is one example. |
I was referring specifically to APIs exposed on DedicatedWorker, but not on Window.
How would a worklet support the use cases that require generating a new track in JS?
Nothing is mandatory, but going against how all Web APIs are exposed requires in my opinion a stronger argument than the ones I've seen so far. The existence of examples ranging from simple demos to critical enterprise products using the main thread shows that it's technically viable to do it. It's true that a lot of those cases would benefit from moving the processing off the main thread, but I think that's a tradeoff developers should be able to decide. |
I think this is a perfect example, and a strong corollary to video:
There's clear and relevant precedent here for controlling exposure of realtime data away from main thread, possibly even away from workers.
@guidou Is the argument we shouldn't expose methods to workers without also exposing them to main-thread, because that would be unexpected somehow? What's the worry there exactly? If symmetry is of concern, we can do VideoWorklet. 😉
It can be done with audio today, so I don't think whether it is possible or not is the discussion. Instead, worker vs. worklet I think comes down to what benefits there may be from controlling the environment of exposure. I don't know what those are atm, but if we plan to expose |
I'm saying that going against the established pattern of exposing in both places requires a stronger argument than "the user might do something wrong in certain circumstances" which is the only argument given so far. This could be said basically about any API, including the audio worklet.
I was asking just in case you had something concrete in mind.
I'm not very familiar with the history of the audio worklet, but I think a stronger argument than controlled exposure is the ability to run on a real-time thread, which you can't do with workers. A real-time thread is ideal for very low-latency, relatively low-CPU applications, which is very common for audio applications that need to render audio locally. It's not needed for the use cases we intend to support (it would be a negative in some cases).
We have control. The application can run processing on a worker if the main-thread is a concern. What other controls do you envision? |
I'm hearing no reason other than pattern, and I suspect those patterns are largely historical. I.e. workers are still a relatively young concept compared to the size of the platform, so that might explain why many APIs were exposed to main thread first. WebIDL lets us specify exposure discretely, and I see no established rationale to back up why exposure must follow any pattern. If you have one, please point me to it, otherwise I see no reason to follow a pattern not backed by rationale. Also, AudioWorklet is a clear example that breaks that pattern: we had main thread exposure, and removed it on purpose for good reasons, which I gave. That this ended up being a worklet rather than a worker, I don't think had anything to do with any rule that APIs cannot be exposed in workers only. I've also seen no proposals to transfer I also disagree that being conservative requires arguments. I think being cautious with these low-level APIs is the right approach, which means requiring arguments to be anything but conservative. It'll be hard to put the genie back in the bottle.
What use case is that? "Funny hats" and "Virtual Reality Gaming" both seem real-time to me. |
The argument is that there are use cases where allowing the main thread is justified IMO:
I don't think ignoring these legitimate use cases because the main thread is bad in other situations is enough justification.
My point is that there are some legitimate use cases that we shouldn't ignore and the historical precedent backs supporting those use cases. Now, what is actually the issue? the concern originally raised by this spec issue is main thread by default, but we're mainly discussing allowing or not allowing running on the main thread, which is a different discussion. We can address the concern of main thread by default with an extra parameter in the constructor (e.g., allowStreamOnWindow) that is false by default. Any attempt to use the streams on Window would fail unless the application explicitly passes this parameter as true, therefore disabling processing on main thread by default. Would this address the concern in your opinion?
The fact that it is a "real-time" application does not mean a real-time priority thread at the OS level is a good idea. In fact, it can be a way to shoot yourself in the foot. If you do processing that requires a lot of CPU on a real-time priority thread you run the risk of starving everything else, including UI and other things running on the main thread. |
My guess is that applications complexity mostly lies in the processing of the data, not in how to get the data: WebGL shaders, WASM libraries...
Demos should show the preferred way of using the API. Toys are product applications. As an example, ScriptProcessorNode is probably fine for some applications on some devices.
Agreed and that is precisely the point: isolate potentially perf sensitive tasks in their own context to let the UA do the prioritisation, potentially dynamically. |
My guess is that In a complex codebase with a lot of interconnected layers, introducing off-main-thread processing can be a challenging architecture change, and having the possibility of a migration in multiple stages can be very important for such a project. For example, they could first migrate the main-thread logic from canvas to the new API and evaluate the correctness of that change, in a second stage they could better isolate the part of the processing that will move off the main thread, and in a third stage actually they could move the logic off the main thread.
I presented a proposal to have main-thread processing disabled by default, but to make it easily available if the application explicitly needs it. Do you think it addresses the concern originally raised by this spec issue, even if you disagree with the general approach taken by the spec? |
@guidou In my experience (Plan-B, getDisplayMedia etc.) migration is never helped, only hurt, by making it optional. Those same arguments ("challenging architecture change") are reasons not to support main-thread access, because sites may otherwise never move away from main thread, leaving us in an undesirable place where sites suck and users blame browsers. I believe good API design makes desirable things easy and undesirable things hard, leading users down the right path. |
Unless we plan to remove canvas capture, etc., migration will always be optional. |
If people were happy with canvas capture we wouldn't be here, so I don't feel we have to compete with status quo. Sites want something better, we have their attention, so we're at a crossroads of what to offer next. Is it transferable ScriptProcessorNode, AudioWorklet, or something else? |
We have not heard from anyone who is not affiliated with a browser vendor arguing this point one way or the other. |
Not really. AudioWorkletProcessor is an interface that cannot be instantiated on main thread, yet has full WPT coverage. Debugging web workers is trivial these days. People also don't seem to have complaints debugging and testing AudioWorklets, even though I assume that gets a little trickier. @alvestrand if your argument is we haven't heard from enough people that think the status quo for capturing raw video data is fine, I'm willing to listen. |
Some additional API examples:
|
Additional thoughts on existing API vs. what we are trying to achieve:
|
w3ctag/design-principles#360 which was opened in response to this was closed by w3ctag/design-principles#404 a year ago. The TAG's guidance here is now enshrined in § 10.2.1. Some APIs should only be exposed to dedicated workers. It seems to support the spec's existing decision to expose in DedicatedWorker only. Out-of-main-thread processing has been by default since adoption #66. Can this be closed, or is there more to discuss here? |
Current API allows getting access to raw media in main thread which has known issues in terms of robustness and implementation.
We should envision an API that does processing by default where it is safe to do so.
A few options come to mind:
The text was updated successfully, but these errors were encountered: