通过Websocket视频流到< video>标签 [英] Video stream through Websocket to <video> tag

查看:4188
本文介绍了通过Websocket视频流到< video>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node.js通过Websocket将实时webm视频流式传输到将在标签中播放的网页。
以下是服务器和客户端的代码:

I use Node.js to stream via Websocket a realtime webm video to a webpage which will play it in a tag. The following is the code from both the server and the client:

服务器:

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中转换它,但没有运气。

I'm using Chrome 26 and i get this error: "Uncaught Error: InvalidAccessError: DOM Exception 15". It seems like the type of the buffer fed to the append method is wrong. I already tried converting it in a Blob, Array and Uint8Array, but with no luck.

推荐答案

您的示例仅包含显示在渲染页面上: http://html5-demos.appspot.com/static/media -source.html

Your example only contains the code that is shown on the rendered page from here: http://html5-demos.appspot.com/static/media-source.html

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

Check the source code, line 155 is what you are missing:

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

所以,你需要告诉Blob的内容类型,然后用Uint8Array提供缓冲区(见行171):

So, you need to tell the Blob the content type and then feed the buffer with Uint8Array (see line 171):

sourceBuffer.append(new Uint8Array(e.target.result));

这篇关于通过Websocket视频流到&lt; video&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆