【发布时间】:2017-08-05 20:03:20
【问题描述】:
我对此进行了很多研究,但我感到很沮丧,因为我觉得解决方案应该简单,尽管我知道不会。理想情况下,我只想使用 node 来托管服务器,webrtc getusermedia 在本地客户端上获取实时流,并使用 socket.io 之类的东西将流发送到服务器,然后服务器会将流广播到远程客户;就好像它是一个简单的消息聊天应用程序一样。
再想一想,这种简单的方法似乎是不可能的,因为直播视频需要连续发送大量数据,这并不等同于在事件之后发送单个消息甚至文件(发送按钮按下)。
也许我错了,直播视频流应用程序可以遵循 node/socket.io Messenger 应用程序的相同结构吗?你会以某种方式发送从 getUserMedia 返回的媒体对象、blob、一些二进制数据(我已经尝试了所有这些,但可能不正确)。
理想的目标是应用尽可能少地使用额外的绒毛,尽可能少地安装 npm,尽可能少地使用额外的 javascript 库,或者不用担心编码/解码或任何地狱 ICE 或STUN都是。有什么办法可以做到吗?还是我要求太多了?
理想客户
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);
});
理想服务器
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");
});
});
这是运行中的损坏应用程序:https://nodejs-videochat.herokuapp.com/
这是github上的破代码:https://github.com/joshydotpoo/nodejs-videochat
【问题讨论】:
-
@Profstyle 从我在您发布的 github 链接上看到的用于从视频中捕获单个帧并将其转换为视频?甚至使用 cpp 文件,但也许这只是为了让它与本机相机一起使用,而不仅仅是使用 webrtc 的东西......我真的试图保持这个简单
标签: javascript node.js socket.io webrtc live-streaming