简体   繁体   English

以部分透明的方式流式传输 HTML5 canvas 内容?

[英]Streaming HTML5 canvas content with partial transparency?

I'm currently sending a canvas stream over WebRTC, using canvas.captureStream() .我目前正在通过 WebRTC 发送 canvas stream,使用canvas.captureStream() This works as intended, but my canvas is an overlay over another video and therefore has transparent pixels.这按预期工作,但我的 canvas 是另一个视频的叠加层,因此具有透明像素。

I'm aware that the usual H26x or VPx formats don't support transparency (see also Streaming video with transparent pixels using webrtc ), so I decided to go with good old chroma-keying (ie 100% green == transparent).我知道通常的 H26x 或 VPx 格式不支持透明度(另请参阅使用 webrtc 使用透明像素流式传输视频),因此我决定使用具有良好旧色键的 go (即 100% 绿色 == 透明)。

To get there, I'm currently filling the canvas with transparent green at startup:为了到达那里,我目前在启动时用透明绿色填充 canvas:

// already tried various composite ops here, none seem to work
// context.globalCompositeOperation = "destination-out";
context.fillStyle = "rgba(0,255,0,0)";
context.fillRect(0, 0, canvas.width, canvas.height);

This looks correct on the client side (the canvas is transparent, whatever I draw on top is not), but in the resulting stream that's going out over WebRTC, the background that's transparent in the browser is apparently just black, rather than green.这在客户端看起来是正确的(canvas 是透明的,无论我在上面画什么都不是),但是在生成的 stream 中,WebRTC 中的透明显然只是黑色,而不是绿色。 I would expect the alpha value to just get dropped?我希望alpha值会被丢弃?

When I change the alpha value to anything other than 0 (eg 1 or 128), then the result isn't transparent anymore, but fully opaque bright green, both in the outgoing stream (good) and in the browser (not good).当我将 alpha 值更改为 0 以外的任何值(例如 1 或 128)时,结果不再透明,而是完全不透明的亮绿色,无论是在传出的 stream (好)和浏览器中(不好)。

I'd rather avoid having to manually do RGBA -> RGB conversion in Javascript for every frame on a hidden canvas, which is the only alternative I can think of right now.我宁愿避免在 Javascript 中为隐藏的 canvas 上的每一帧手动执行 RGBA -> RGB 转换,这是我现在能想到的唯一选择。 Other ideas very welcome:-)非常欢迎其他想法:-)

EDIT: tested with both Chrome 96 and Firefox 94, on Ubuntu 20.04.编辑:在 Ubuntu 20.04 上使用 Chrome 96 和 Firefox 94 进行了测试。 For reference, here's the description of the compositing ops: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Compositing/Example作为参考,这里是合成操作的描述: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Compositing/Example

Based on the comments by Kaiido (thanks,)?根据Kaiido的评论(谢谢)? I figured out a relatively (:) efficient solution, I now have two canvases, one visible and one hidden.我想出了一个相对 (:) 有效的解决方案,我现在有两个画布,一个可见,一个隐藏。 and all drawing commands are just replicated on both of them, The visible one uses a transparent black background and is overlaid over the video.并且所有绘图命令都只是在它们两个上复制,可见的使用透明的黑色背景并覆盖在视频上。 the hidden one has a bright green background and is used as stream source.隐藏的具有亮绿色背景,用作 stream 源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM