简体   繁体   English

JavaScript XMLHttpRequest.responseType as "arraybuffer" -- 如何获取当前的数组缓冲区块

[英]JavaScript XMLHttpRequest.responseType as "arraybuffer" -- how to get current arraybuffer chunk

I'm trying to stream a from the client side to a server side, to later stream the video back to another client.我正在尝试将 a 从客户端流式传输到服务器端,稍后将视频流式传输另一个客户端。

The point is:重点是:

How can I get chunks of a video from client-side JavaScript (that can be sent to a server)?如何从客户端 JavaScript 获取视频块(可以发送到服务器)?

using this code, for example:使用此代码,例如:

var x = new XMLHttpRequest();
        var url = location.href;
        x.onreadystatechange = function(){
            if(x.readyState == 200) {
                console.log("done");
            } else {
                console.log("chunk",x.response); //this is null until readyState is 200 anyway
            }

        }
        x.onprogress = e => {
            console.log("EE",e.target.response); //also null if resposneType is arraybuffer
        };
        x.responseType="arraybuffer";
        x.open("GET","http://localhost:88/videoplayback.mp4",true);
        x.send("");

When I try to print the response before its finished loading (to get it by chunks) then its simply null;当我尝试在完成加载之前打印响应(按块获取它)时,它只是 null; the arraybuffer only returns as the respnse when its finished loading. arraybuffer 仅在加载完成后作为响应返回。

If I take out the responsetype and just leave it as plain text, then indeed some unicode-characters get printed to the screen for each readystatechange even before its finished, only an arraybuffer doesn't.如果我取出 responsetype 并将其保留为纯文本,那么即使在完成之前,每个 readystatechange 的某些 unicode 字符确实会打印到屏幕上,只有 arraybuffer 不会。

So: is this the best way to stream a video from the client to server, and if so, how can I actually do it?那么:这是将视频从客户端流式传输到服务器的最佳方式吗,如果是这样,我该怎么做? and if not, what's a better way?如果没有,什么是更好的方法?

Indeed, arraybuffer or blob responseTypes do not allow access of chunk of data until the download is complete.实际上,在下载完成之前, arraybufferblob响应类型不允许访问数据块。

Now, there are ways... For instance in newest browsers, you could start a ReadableStream from the fetch API , or from Firefox you could set the responseType to "moz-chunked-buffer" , but that's not what you need at all here.现在,有一些方法......例如在最新的浏览器中,您可以从 fetch API启动ReadableStream ,或者从 Firefox 您可以将responseType设置为"moz-chunked-buffer" ,但这根本不是您在这里需要的.

What you are describing is exactly what WebRTC has been made for.您所描述的正是WebRTC 的用途。 So the best way is to run a STUN/TURN server, and to stream your media through it using the MediaStream API.所以最好的方法是运行一个 STUN/TURN 服务器,并使用 MediaStream API 通过它流式传输你的媒体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM