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.
Starting.
<video>.Show the code frames in, DOM snapshots in, a track out
- The markup. One
drawableelement holds the overlays. It's clear where there is no overlay. - The page hands over the canvas and sends a snapshot on every paint.
transferControlToOffscreen(), thencaptureElementImage()in thepainthandler. - Video frames to the worker. The track is transferred where the browser allows it. Otherwise the
ReadableStreamof aMediaStreamTrackProcessoris transferred. - A track out of the worker.
VideoTrackGeneratorin the worker where it exists. Otherwise theWritableStreamof aMediaStreamTrackGeneratoris transferred. - The worker's read loop.
- A file decoded in the worker. A WebM reader and
VideoDecoder. - YUV to RGB without the main thread.
copyTo()and a loop. - One output frame. The video frame,
drawElementImage()of the snapshot,new VideoFrame(canvas), one write. - The looks. 2D canvas calls on the frame and on the boxes of the panels.
- The whole worker.
How it works
- An
ElementImagecan be drawn only into the canvas it was captured from. So the worker doesn't make its own canvas. The page callstransferControlToOffscreen()on the drawable canvas and the worker draws into that. The canvas on the page shows what the worker draws. - The page listens for
paint. Each time, it callscanvas.captureElementImage(scene)and transfers the snapshot to the worker. The worker keeps the newest one and closes the one before. - Video frames reach the worker as
VideoFrameobjects. The first source reads the WebM clip in the worker and decodes it there withVideoDecoder. The camera and the second clip source use aMediaStreamTrackProcessoron a track fromgetUserMedia()orvideo.captureStream(). - A 2D context draws RGB and the frames are YUV. "Frame to RGB" picks how the worker converts them:
copyTo()of the planes and a loop in script,createImageBitmap(), ordrawImage()of the frame itself. Auto usescopyTo()for I420 and NV12 frames andcreateImageBitmap()for the rest. - For each frame the worker draws the frame, draws the snapshot, wraps the canvas in a
VideoFramewith the input frame's timestamp and writes it to the track. - The worker's
drawElementImage()also sets where the element is for hit testing on the main thread. The field and the buttons in the picture take input where they are drawn. - While the main thread is blocked no
paintruns, so the worker draws the last snapshot it has. The video frames keep changing.
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.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157, headless, with software rendering and software video decoding. The rows above say what your browser does.
- Works: a worker composes video frames and DOM into a track. The frames come from a transferred
ReadableStream, the DOM from transferredElementImagesnapshots, and the output goes to a transferredWritableStream. No call tocaptureStream()on the canvas is needed. - Not in Chromium 157: the two calls the specification gives a worker.
MediaStreamTrackProcessorandVideoTrackGeneratoraren't defined in a worker, and aMediaStreamTrackcan't be transferred (DataCloneError). The window hasMediaStreamTrackProcessorand the olderMediaStreamTrackGenerator, and their streams can be transferred. The page tries the standard route first and reports which one it used. - Limit: the snapshot must be drawn into its source canvas. Drawing it into an
OffscreenCanvasmade in the worker throws. The output frame is therefore the size of the canvas on the page, and that canvas shows the composition whether or not it is wanted there. - Works: the main thread can stop. With the clip decoded in the worker, the worker read and wrote 23 frames during a 0.8 s block and the track delivered 22. The HTML in them is the last snapshot. Input to the overlays waits until the block ends.
- Fails during a block:
drawImage(VideoFrame),createImageBitmap(VideoFrame)andtexImage2D(VideoFrame)of an I420 frame in a worker. Each one waited until the main thread was free, so the worker composed one frame or none.copyTo()in the frame's own format didn't wait. The worker therefore reads the planes and converts them to RGB in a loop, which took about 4 ms for 640 by 360 here. Frames the worker made from a canvas (BGRA) convert withcreateImageBitmap()during a block. This was measured with software rendering. A device with a GPU may not have the wait. - Fails during a block:
video.captureStream()as the source. It delivered no frame while the main thread was blocked. A camera track delivered 20 frames a second through the same block (Chromium's fake camera). - Fails during a block:
captureStream()of the canvas on the page as the way out. It still works aftertransferControlToOffscreen(), and delivered 10 frames in a second, but none while the main thread was blocked. The track written by the worker didn't stop. - Works: WebGL 2 in the worker.
texElementSubImage2D()takes the transferred snapshot andtexImage2D()takes aVideoFrameon the transferred canvas, so the looks could be shaders. The page uses the 2D context, which is the lighter route. - Works: hit testing. The canvas buffer is 640 px wide and the canvas is shown narrower. The worker passes the buffer size to
drawElementImage(), and the buttons are hit where they are drawn. - Not verified here: a real camera, hardware-decoded frames (which can arrive as NV12 or with no format, not I420), which conversions wait for the main thread on a device with a GPU, the frame rate on a phone, and whether Chrome 159 has the worker calls. The live checks answer these on your device.