Javascript Node.js 服务器上的实时视频流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42803724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Live Video Stream on a Node.js Server
提问by joshy.poo
I have been researching this a lot but am frustrated as I feel like the solution should besimple though I know wont be. Ideally i'd just want to use node to host the server, webrtc getusermedia to get the live stream on the local client and use something like socket.io to send the stream to the server and then the server would broadcast the stream to the remote client; as if it was a simple messaging chat app.
我一直在研究这个,但很沮丧,因为我觉得解决方案应该很简单,尽管我知道不会。理想情况下,我只想使用 node 来托管服务器,webrtc getusermedia 在本地客户端上获取实时流,并使用 socket.io 之类的东西将流发送到服务器,然后服务器将流广播到远程客户; 就好像它是一个简单的消息聊天应用程序。
Just thinking about this some more it seems as an approach this simple would be impossible because a live video requires continuous large amounts of data to be sent, which does not equate to sending a single message or even file after an event (send button pressed).
再考虑一下,这种简单的方法似乎是不可能的,因为实时视频需要连续发送大量数据,这并不等同于在事件发生后发送单个消息甚至文件(按下发送按钮) .
Maybe I am wrong however, can a live video stream app follow the same structure of a node/socket.io messenger app? Would you send the media object returned from getUserMedia, the blob, some binary data some how (I've tried all of these but perhaps not correctly).
然而,也许我错了,实时视频流应用程序可以遵循 node/socket.io 信使应用程序的相同结构吗?您是否会发送从 getUserMedia 返回的媒体对象、blob、一些二进制数据(我已经尝试了所有这些,但可能不正确)。
The ideal goal would be an app that uses as little extra fluffas necessary, as little npm installs, as little extra javascript libraries, or little worrying about encoding/decoding or whatever the hell ICE or STUN are. Is there any way this is possible or am I asking for too much?
理想的目标是应用尽可能少地使用额外的功能,尽可能少地安装 npm,尽可能少地安装额外的 javascript 库,或者不用担心编码/解码或 ICE 或 STUN 是什么鬼东西。有什么办法可以做到,还是我要求太多?
Ideal Client
理想客户
var socket = io();
var local = document.getElementById("local_video");
var remote = document.getElementById("remote_video");
// display local video
navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(function(stream) {
local.src = window.URL.createObjectURL(stream);
socket.emit("stream", stream);
}).catch(function(err){console.log(err);});
// displays remote video
socket.on("stream", function(stream){
remote.src = window.URL.createObjectURL(stream);
});
Ideal Server
理想服务器
var app = require("express")();
var http = require("http").Server(app);
var fs = require("fs");
var io = require("socket.io")(http);
app.get('/', onRequest);
http.listen(process.env.PORT || 3000, function() {
console.log('server started');
})
//404 response
function send404(response) {
response.writeHead(404, {"Content-Type" : "text/plain"});
response.write("Error 404: Page not found");
response.end();
}
function onRequest(request, response) {
if(request.method == 'GET' && request.url == '/') {
response.writeHead(200, {"Content-Type" : "text/html"});
fs.createReadStream("./index.html").pipe(response);
} else {
send404(response);
}
}
io.on('connection', function(socket) {
console.log("a user connected");
socket.on('stream', function(stream) {
socket.broadcast.emit("stream", stream);
});
socket.on('disconnect', function () {
console.log("user disconnected");
});
});
This is the broken app in action : https://nodejs-videochat.herokuapp.com/
这是运行中的损坏应用程序:https: //nodejs-videochat.herokuapp.com/
This is the broken code on github: https://github.com/joshydotpoo/nodejs-videochat
这是github上的损坏代码:https: //github.com/joshydotpoo/nodejs-videochat
采纳答案by Gaurav Chaudhary
Try to be clear and specific. First, you are not using WebRTC here. getUserMedia()is a part of navigator WebAPIwhich you are using to get media stream from the camera.
尽量清楚和具体。首先,您在这里没有使用 WebRTC。getUserMedia()是navigator WebAPI的一部分,您可以使用它从相机获取媒体流。
Using WebRTC means you are using ICE and STUN/TURN servers for the purpose of signaling. You will use your hostserver(Node) for specifying ICE configuration, identify each user and provide a way to call each other.
使用 WebRTC 意味着您正在使用 ICE 和 STUN/TURN 服务器来发送信号。您将使用您的主机服务器(节点)来指定 ICE 配置,识别每个用户并提供一种相互调用的方式。
If you want to stream it through your host, probably you should stream it in chunks and set up your own signaling infrastructure. You can use Stream API with socket io to stream data in chunks(packets). See here Stream API(socket.io)
如果您想通过您的主机流式传输它,您可能应该将其分块流式传输并设置您自己的信令基础设施。您可以使用带有套接字 io 的 Stream API 以块(数据包)的形式流式传输数据。看这里Stream API(socket.io)
Also, you can check out the live example of WebRTC + Socket.io here: Socket.io | WebRTC Video Chat
此外,您可以在此处查看 WebRTC + Socket.io 的实时示例:Socket.io | WebRTC 视频聊天
You can find out more information here: sending a media stream to Host server
您可以在此处找到更多信息:将媒体流发送到主机服务器

