简体   繁体   English

WebRTC:如何将网络摄像头数据作为数据流?

[英]WebRTC: how to get the webcam data as a stream of data?

I have a simple webpage where you can stream your webcam. 我有一个简单的网页,您可以在其中流式传输网络摄像头。 I would like to take this stream and send it somewhere, but apparently I can't really access the stream itself. 我想把这个流发送到某个地方,但显然我无法真正访问流本身。 I have this code to run the stream: 我有这个代码来运行流:

navigator.webkitGetUserMedia({video: true}, gotStream, noStream);

And in gotStream, I tried many things to "redirect" this stream somewhere else, for example: 在gotStream中,我尝试了很多东西来将这个流“重定向”到其他地方,例如:

function gotStream(stream) {   
    stream_handler(stream)
    //other stuff to show webcam output on the webpage
}

or 要么

function gotStream(stream) {   
    stream.videoTracks.onaddtrack = function(track){
        console.log("in onaddtrack");
        stream_handler(track);
    }
    //other stuff to show webcam output on the webpage
}

But apparently the gotStream function gets called only once at the beginning, when the user grants permissions to the webcam to stream. 但显然,当用户向网络摄像头授予流的权限时, gotStream函数在开始时只被调用一次。 Moreover the stream variable is not the stream itself but an object with some properties inside. 此外, stream变量不是流本身,而是内部具有一些属性的对象。 How am I supposed to access the stream itself and redirect it wherever I want? 我应该如何访问流本身并将其重定向到我想要的任何地方?

EDIT: You may be familiar with webglmeeting, a sort of face2face conversation apparently developed on top of WebRTC. 编辑:您可能熟悉webglmeeting,这是一种显然是在WebRTC之上开发的face2face对话。 I think that script is sending somehow the stream of data from one point to the other. 我认为该脚本以某种方式将数据流从一个点发送到另一个点。 I would like to achieve the same by understanding how to get the stream of data in the first place. 我想通过了解如何首先获取数据流来实现同样的目标。

RE-EDIT: I don't want a conversion to image and sending the latter, I would like to work with the stream of data itself. 重新编辑:我不希望转换为图像并发送后者,我想使用数据流本身。

If you mean to steam your camera to somewhere in PNG or JPG so I will use canvas like this 如果你的意思是将你的相机蒸到PNG或JPG的某个地方,那么我将使用这样的canvas

HTML HTML

<video id="live" width="320" height="240" autoplay></video>
<canvas width="320" id="canvas" height="240" style="display:none;"></canvas>

JS ( jQuery ) JS(jQuery)

var video = $("#live").get()[0];
var canvas = $("#canvas");
var ctx = canvas.get()[0].getContext('2d');

navigator.webkitGetUserMedia("video",
        function(stream) {
            video.src = webkitURL.createObjectURL(stream);
        }
)
     setInterval(
        function () {
            ctx.drawImage(video, 0, 0, 320,240);
                var data = canvas[0].toDataURL("image/jpeg");   
  },1000);

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

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