简体   繁体   English

使用WebRTC流HTML5画布活动

[英]Stream HTML5 canvas activity using WebRTC

I'd like to stream the activity of an HTML5 canvas using WebRTC. 我想使用WebRTC流式传输HTML5画布的活动。 One user broadcast his canvas and the connected peer see what the remote peer is doing on his canvas, if he is drawing a line or anything, i'd like the connected peer to see the changes on his own canvas. 一个用户广播了他的画布,并且连接的对等方看到远程对等方在其画布上正在做什么,如果他正在画一条线或其他任何东西,我希望连接的对等方在自己的画布上看到更改。

My question, is this possible out of the box with WebRTC? 我的问题是,WebRTC是否可以立即使用? After my searches, i would say that it's not the case. 搜索之后,我会说不是这样。

If not, how would you approach such a system? 如果没有,您将如何处理这样的系统?

Thanks. 谢谢。

Yes there is; 就在这里; the CANVAS element comes with a captureStream method that allows you to capture the canvas as a RTC Peer Channel compatible MediaStream. CANVAS元素带有captureStream方法,该方法允许您将画布捕获为与RTC对等通道兼容的MediaStream。 Just as you send a camera stream to another peer you can send this captured stream to the peer to get it working. 就像将摄像机流发送给另一个对等方一样,您也可以将捕获的流发送给对等方以使其正常工作。

Ref: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/captureStream 参考: https : //developer.mozilla.org/zh-CN/docs/Web/API/HTMLCanvasElement/captureStream

Granted it's not supported in all browsers; 当然,并非所有浏览器都支持它; but its a useful technology as it can be used for CANVAS streaming the way you've mentioned. 但它是一项有用的技术,因为它可以按照您提到的方式用于CANVAS流式传输。

I'm surprised to see there's aa 2015 answer saying this isn't possible - the captureStream method on canvas has been around since 2014-2015... There's quite a few demos available on it today maybe that'll help you get started. 我很惊讶地看到一个2015年的回答说这是不可能的-画布上的captureStream方法自2014-2015年就出现了……今天有很多演示可用,也许可以帮助您入门。

It can't be done out of the box unfortunately, as canvas cannot be used as a video source. 不幸的是,由于画布不能用作视频源,因此无法开箱即用。

This has to be solved locally on each client by installing a pseudo-camera (driver emulating a camera) which records the screen instead. 必须通过在每个客户端上本地安装伪摄像机(模拟摄像机的驱动程序)来解决此问题,该伪摄像机记录屏幕。 Then this can be used as source for WebRTC. 然后,可以将其用作WebRTC的源。

You can use something like WebSocket to emulate streaming by sending draw commands by the server and replay them on receiver end, but it won't be an integrated part of WebRTC of course (or use its data channel). 您可以使用WebSocket之类的方法通过服务器发送绘制命令并在接收器端重播它们来模拟流传输,但是它当然不会成为WebRTC的集成部分(或使用其数据通道)。

Update 更新
This answer was written 1.5+ years ago and although there existed initial drafts for a captureStream() interface it was in its very infant state. 这个答案是1.5年前写的,尽管有一个captureStream()接口的初稿,但它还处于初期状态。 The technology has matured since then and can be used for experimental usage. 从那时起,该技术已经成熟,可以用于实验用途。

However, be warned as the page also states: 但是,请注意,页面也会指出:

This is an experimental technology 这是一项实验技术

as well as 以及

Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes. 还要注意,随着规范的更改,实验技术的语法和行为可能会在将来的浏览器版本中发生变化。

The feature is only supported in Chrome (and therefor also in the new Opera) and Firefox, and must currently be enabled by flags. 此功能仅在Chrome(以及新的Opera)中和Firefox中受支持,并且当前必须通过标志启用。

In other words, it's not something to consider for production quite yet. 换句话说,尚未考虑将其用于生产。

Very few things are "out of the box" with WebRTC; WebRTC几乎没有“开箱即用”的东西。 What you want to do isn't the exception. 您想要做的也不例外。 Yet, it could be achieved, all depending on the complexity of your canvas application. 但是,可以实现它,这完全取决于画布应用程序的复杂性。

An idea can be to render a local canvas representing the remote canvas. 一个想法可以是渲染代表远程画布的本地画布。 Once the peers are connected, the remote peer can send the information needed for you to duplicate the contents of the remote canvas on your own. 一旦连接了对等方,远程对等方就可以发送您所需的信息,以便您自己复制远程画布的内容。

If that is possible, then a way to send this information is over WebRTC's DataChannel, which exposes an API similar to WebSockets. 如果可能的话,则通过WebRTC的DataChannel发送此信息的方法,该方法公开类似于WebSockets的API。 It is certainly faster than WebSockets because of the nature of the peer connection, and the ability to send packets with out-of-order delivery and variable amount of re transmit. 由于对等连接的性质以及以乱序发送和可变数量的重新发送来发送数据包的能力,它肯定比WebSockets更快。

Relevant information here: http://www.html5rocks.com/en/tutorials/webrtc/datachannels/ 此处的相关信息: http : //www.html5rocks.com/en/tutorials/webrtc/datachannels/

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

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