Ok so the question is a bit much, but it actually mentions every part of the "bug" I'm experiencing. I'm not sure if it is really a bug or if there is something I don't understand yet. I created a demo on codepen to reproduce the weird behavior.
https://codepen.io/rroyerrivard/pen/jOwBLbB
Like I wrote in the codepen, there seems to be a bug in Chrome for which the stream of an HTMLCanvasElement
does not get refreshed on every image draw when it is hidden. But for that to happen, there needs to be these 4 conditions all at once.
- We must feed an
HTMLVideoElement
with aMediaStream
gotten from a call toHTMLCanvasElement.captureStream()
(instead of directly showing theHTMLCanvasElement
). - The
HTMLCanvasElement
from which we get theMediaStream
must be hidden (either not in the DOM or having it hidden with css). - We must draw on the
HTMLCanvasElement
from anOffscreenCanvas
that we get from a call toHTMLCanvasElement.transferControlToOffscreen()
. - The draw on the
OffscreenCanvas
must be done in a web worker that got theOffscreenCanvas
transferred to.
I was unfortunate enough to hit all these conditions at once in a web app at work. I can avoid the bug by not using the transferControlToOffscreen()
call and draw an ImageBitmap
in the main thread after receiving it from the web worker, but that reduces the FPS by roughly 18%.
Is this a known bug? Is there a way to force the MediaStream
to refresh on every draw of the OffscreenCanvas
?