我使用 Node.js 通过 Websocket 将实时 webm 视频流式传输到将在标签中播放的网页。以下是来自服务器和客户端的代码:
服务器:
var io = require('./libs/socket.io').listen(8080, {log:false});
var fs = require('fs');
io.sockets.on('connection', function (socket)
{
console.log('sono entrato in connection');
var readStream = fs.createReadStream("video.webm");
socket.on('VIDEO_STREAM_REQ', function (req)
{
console.log(req);
readStream.addListener('data', function(data)
{
socket.emit('VS',data);
});
});
});
客户:
<html>
<body>
<video id="v" autoplay> </video>
<script src='https://localhost/socket.io/socket.io.js'></script>
<script>
window.URL = window.URL || window.webkitURL;
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if(!!! window.MediaSource)
{
alert('MediaSource API is not available!');
return;
}
var mediaSource = new MediaSource();
var video = document.getElementById('v');
video.src = window.URL.createObjectURL(mediaSource);
mediaSource.addEventListener('webkitsourceopen', function(e)
{
var sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
var socket = io.connect('http://localhost:8080');
if(socket)
console.log('Library retrieved!');
socket.emit('VIDEO_STREAM_REQ','REQUEST');
socket.on('VS', function (data)
{
console.log(data);
sourceBuffer.append(data);
});
});
</script>
</body>
</html>
我正在使用 Chrome 26,但出现此错误:“未捕获错误:InvalidAccessError:DOM 异常 15”。似乎提供给 append 方法的缓冲区类型是错误的。我已经尝试将它转换为 Blob、Array 和 Uint8Array,但没有成功。