15

我使用 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,但没有成功。

4

1 回答 1

8

您的示例仅包含此处呈现的页面上显示的代码:http: //html5-demos.appspot.com/static/media-source.html

检查源代码,第 155 行是您所缺少的:

var file = new Blob([uInt8Array], {type: 'video/webm'});

因此,您需要告诉 Blob 内容类型,然后使用 Uint8Array 提供缓冲区(参见第 171 行):

sourceBuffer.append(new Uint8Array(e.target.result));
于 2013-05-17T18:07:10.397 回答