简体   繁体   English

使用 WebRTC 从画布元素流式传输视频

[英]Stream video from a canvas element using WebRTC

I am using WebRTC for peer-to-peer video communication, and I would like to apply video filters to local webcam video before sending it to a remote peer.我正在使用 WebRTC 进行点对点视频通信,并且我想在将视频过滤器发送到远程对等点之前将视频过滤器应用于本地网络摄像头视频。

The approach that I am considering is to send the local webcam video to a canvas element, where I will apply javascript filters to the video.我正在考虑的方法是将本地网络摄像头视频发送到画布元素,在那里我将 javascript 过滤器应用于视频。 Then I would like to stream the video from the canvas element to the peer using WebRTC.然后我想使用 WebRTC 将视频从画布元素流式传输到对等方。 However, it is not clear to me if this is possible.但是,我不清楚这是否可能。

Is it possible to stream video from a canvas element using WebRTC?是否可以使用 WebRTC 从画布元素流式传输视频? If so, how can this be done?如果是这样,如何做到这一点? Alternatively, are there any other approaches that I might consider to accomplish my objective?或者,我是否可以考虑其他任何方法来实现我的目标?

It's April 2020;现在是 2020 年 4 月; you can achieve this with the canvas.captureStream() method.您可以使用canvas.captureStream()方法来实现这一点。

There is an excellent article on how to use it, along with several demos on github.有一篇关于如何使用它的优秀文章,以及 github 上的几个演示。 See the following links:请参阅以下链接:

Capture Stream 捕获流

Stream from a canvas element to peer connection 从画布元素流式传输到对等连接

So, basically, you can apply all the transformations on the canvas and stream from the canvas to remote peer.因此,基本上,您可以在画布上应用所有转换,并从画布流式传输到远程对等点。

mozCaptureStreamUntilEnded is supported on firefox but resulting stream can't be attached to peer connection. Firefox 支持mozCaptureStreamUntilEnded ,但生成的流无法附加到对等连接。

Playing over <canvas> is easier however streaming media from a <video> element requires Media Processing API (capture-stream-until-ended) along with RTCPeerConnection (with all features support).通过<canvas>播放更容易,但是来自<video>元素的流媒体需要媒体处理 API(capture-stream-until-ended)和 RTCPeerConnection(支持所有功能)。

We can get images from <canvas> however I'm not sure if we can generate MediaStream from <canvas> .我们可以从<canvas>获取图像,但是我不确定我们是否可以从<canvas>生成 MediaStream。

So, mozCaptureStreamUntilEnded is useful only wth pre-recorded media streaming .因此, mozCaptureStreamUntilEnded 仅对预先录制的媒体流有用。

我的解决方案是,将普通流发送到对等方,也传输,它必须如何修改,所以在另一方面,而不是直接在视频元素中显示(播放视频 n 隐藏元素),你会保持使用 settimeout/requestAnimationFrame 在画布中绘制(处理后)。

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

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