我正在尝试创建一个应用程序,可以在其中通过Websocket将浏览器MediaRecorder中的Webcam视频通过Websocket发送到节点服务器,该节点服务器会将分块的数据保存在内存中,并通过终结点处理程序进行处理。
我可以接收数据,创建Passthrough流并为端点提供服务,但是出现的html5视频播放器却什么也不显示。我不确定我哪里出错了。
客户端:
serverVideoStreamConnection = new WebSocket('wss://' + window.location.hostname + ':8443/');
serverVideoStreamConnection.binaryType = "arraybuffer";
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(constraints).then(recordStream).catch(errorHandler);
} else {
alert('Your browser does not support getUserMedia API');
}
}
function recordStream(stream) {
console.log("Started recording")
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = handleDataAvailable;
mediaRecorder.start(10);
function handleDataAvailable(event) {
if (event.data.size > 0) {
serverVideoStreamConnection.send(event.data);
} else {
// ...
console.log("Data not available")
}
}
服务器:
let video = new PassThrough();
wss.on('connection', function (ws) {
ws.on('message', function (message) {
// Broadcast any received message to all clients
if (typeof message !== 'string') {
video.write(new Buffer.from(message));
} else {
wss.broadcast(message);
}
}
server.get('/video', handleVideo)
function handleVideo(request, response) {
const header = {
'Content-Type': 'video/mp4',
"Accept-Ranges": "bytes",
};
response.writeHead(206, header);
video.pipe(response);
}