Hi everybody. I will first ask my question and then provide details on why I am doing that.
Question:
Is there an efficient / fast way to get the video frame data from WebRTC to a web worker (at 20hz, without blocking too much the main thread)?
More details:
I use Webrtc native to stitch 7 cameras feed together and send them as one video track through Webrtc. Then on the browser, the video track gets rendered into a hidden <video> element and is then un-stitched into 7 canvas using <canvas>.getContext('2d').drawImage(<video>, ....).
The stitching is for video synchronization.
The problem is that the 7 call to canvas.drawImage(width=~400, width=~320) takes too much time to keep a 60fps browser rendering and slows down my javascript main event loop. Which delays some data channel operations.
So instead of drawing into a regular canvas, I tried drawing into an offscreenCanvas, then call imageData = offscreenCanvas.getImageData() and send imageData to a worker so that the worker can put back the image data into a transferred "onscreen" canvas.
mainThread.js
const transferedOnscreenCanvas = document.getElementById('onscreen-canvas').transferControlToOffscreen();
const offscreenCanvas =new OffscreenCanvas(width, height);
offscreenCanvas.drawImage(...);
const imageData = offscreenCanvas.getContext('2d').getImageData()
worker.postMessage({imageData:imageData, canvas: transferedOnscreenCanvas}, [transferedOnscreenCanvas]);
worker.js
onMessage(event) {
const onscreenCanvas = event.data.canvas;
const imageData = event.data.imageData;
onscreenCanvas.putImageData(imageData, 0, 0);
}
The problem is that getImageData() takes as much time as drawing on an onscreen canvas and is done on the main thread.
Another idea would be to use 7 video track and 7 video elements, would that be more efficient and synchronized?
All ideas are welcomed.
Thanks.