A worker as the video compositor

A worker reads video frames, draws a snapshot of the HTML below over each one, and writes the result to a video track. The first picture is the worker's canvas. Type in the caption field and press the buttons in it. Then block the main thread: the picture and the track keep running.

ON AIR
Hand raised
Guest. This field takes typing.
The ticker is a CSS animation. Each paint sends one snapshot of this HTML to the worker. The worker draws it over every video frame and writes the frame to a track.

Starting.

The track the worker writes, in a <video>.
Show the code frames in, DOM snapshots in, a track out
  1. The markup. One drawable element holds the overlays. It's clear where there is no overlay.
  2. The page hands over the canvas and sends a snapshot on every paint. transferControlToOffscreen(), then captureElementImage() in the paint handler.
  3. Video frames to the worker. The track is transferred where the browser allows it. Otherwise the ReadableStream of a MediaStreamTrackProcessor is transferred.
  4. A track out of the worker. VideoTrackGenerator in the worker where it exists. Otherwise the WritableStream of a MediaStreamTrackGenerator is transferred.
  5. The worker's read loop.
  6. A file decoded in the worker. A WebM reader and VideoDecoder.
  7. YUV to RGB without the main thread. copyTo() and a loop.
  8. One output frame. The video frame, drawElementImage() of the snapshot, new VideoFrame(canvas), one write.
  9. The looks. 2D canvas calls on the frame and on the boxes of the panels.
  10. The whole worker.

How it works

Live checks

Each check builds its own small canvas and worker in a corner of the window. The picture above isn't touched. Four checks block the main thread for 0.8 s each.

ResultExperimentDetail

Findings

From Chromium 157, headless, with software rendering and software video decoding. The rows above say what your browser does.