5

我正在尝试将 webm 视频文件流式传输到客户端。

第一个块(大约 4466 字节长)“工作”(使用 appendBuffer),因为我可以看到<video>调整到视频分辨率的大小。

这是我第三次附加后得到的错误:

Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.

的属性有这个错误SourceBufferbuffered

[Exception: DOMException]

这是客户端代码:

var client = new BinaryClient('ws://127.0.0.1:3080/binary-endpoint');
var ms = new MediaSource();
var sourceBuffer;
var video = document.querySelector('video');
var needUpdate = true;
var paused = true;
var busy = false;
var initial = true;

ms.addEventListener('sourceopen', function(e) {

    console.log('The MediaSource has been opened: ', e)

    sourceBuffer = ms.addSourceBuffer('video/webm; codecs="vorbis,vp8"');
    ms.duration = 1000;

    sourceBuffer.addEventListener('updatestart', function(e) {
        busy = true;
    });

    sourceBuffer.addEventListener('updateend', function(e) {
        console.log(e);
        busy = false;
    });

    sourceBuffer.addEventListener('update', function(e) {
        console.log('Update succeeded', e)
    });

    console.log('The SourceBuffer has been created', sourceBuffer);
}, false);

client.on('stream', function(stream, meta){

    stream.on('data', function(data) {

        var DATA = new Uint8Array(data);

        if (needUpdate && !busy) {
            sourceBuffer.appendBuffer(DATA);
        }

        if (!initial && paused) {
            video.play();
            paused = false;
        }

        initial = false;
    });
});

video.src = window.URL.createObjectURL(ms);
4

0 回答 0