我们有一种情况试图提供视频流。
由于 HTML5 视频标签不支持 udp 到多播,我们正在尝试重新使用已转换的 ffmpeg 流并将其发送到多个响应。但这不起作用。
第一个响应使流正常,但第二个响应没有。似乎无法将流传输到另一个响应,也无法克隆它。
有没有人这样做过?有任何想法吗?
提前致谢!
这是代码:
var request = require('request');
var http = require('http');
var child_process = require("child_process");
var n = 1;
var stdouts = {};
http.createServer(function (req, resp) {
console.log("***** url ["+req.url+"], call "+n);
if (req.url != "/favicon.ico" && req.url != "/")
{
var params = req.url.substring(1).split("/");
switch (params[0])
{
case "VIEW":
if (params[1] == "C2FLOOR1" || params[1] == "C2FLOOR2" || params[1] == "C2PORFUN" || params[1] == "C2TESTCAM")
var camera = "rtsp://192.168.16.19:554/Inter/Cameras/Stream?Camera="+params[1];
else
var camera = "http://192.168.16.19:8609/Inter/Cameras/GetStream?Camera="+params[1];
// Write header
resp.writeHead(200, {'Content-Type': 'video/ogg', 'Connection': 'keep-alive'});
if (stdouts.hasOwnProperty(params[1]))
{
console.log("Getting stream already created for camera "+params[1]);
var newStdout = Object.create(stdouts[params[1]]);
newStdout.pipe(resp);
}
else
{
// Start ffmpeg
var ffmpeg = child_process.spawn("ffmpeg",[
"-i",camera,
"-vcodec","libtheora",
"-qscale:v","7", // video quality
"-f","ogg", // File format
"-g","1", // GOP (Group Of Pictures) size
"-" // Output to STDOUT
]);
stdouts[params[1]] = ffmpeg.stdout;
// Pipe the video output to the client response
ffmpeg.stdout.pipe(resp);
console.log("Initializing camera at "+camera);
}
// Kill the subprocesses when client disconnects
/*
resp.on("close",function(){
ffmpegs[params[1]].kill();
console.log("FIM!");
});
*/
break;
}
}
else
{
resp.writeHeader(200, {"Content-Type": "text/html"});
resp.write("WRONG CALL");
resp.end();
}
n++;
}).listen(8088);
console.log('Server running at port 8088');