A decoder worker feeding a renderer worker, while the page is busy
A decoder worker feeding a renderer worker directly. Keeping compute or sync pipelines responsive during main-thread layout or GC stalls.
One worker makes video frames. Another paints them into a canvas it was handed. This page introduces the two with a channel and steps away, so frames go from one worker straight to the other and never touch the page's thread. Freeze the page for three seconds, as a long layout or a garbage collection would, and the picture keeps moving.
Code
const { port1, port2 } = new MessageChannel();
decoder.postMessage({ out: port1 }, [port1]);
renderer.postMessage({ in: port2, surface }, [port2, surface]);
// the decoder: each frame's memory is moved to the renderer
out.postMessage({ pixels }, [pixels.buffer]);
Step by step
What each press of Next does in this demo.
- Connect the decoder to the renderer. Each worker gets one end of a channel. Frames start to flow between them.
- Freeze this page for 3 seconds. The page's counter stops. The picture does not.