Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaSource appending to SourceBuffer does not work after the first time

I am currently trying to stream a .webm video file via socket.io to my client (currently using Chrome as client).

Appending the first Uint8Array to the SourceBuffer works fine but appending further ones does not work and throws the following error: Uncaught DOMException: Failed to execute 'appendBuffer' on 'SourceBuffer': The HTMLMediaElement.error attribute is not null.

My current code:

'use strict';

let socket = io.connect('http://localhost:1337');

let mediaSource = new MediaSource();
let video = document.getElementById("player");
let queue = [];
let sourceBuffer;

video.src = window.URL.createObjectURL(mediaSource);

mediaSource.addEventListener('sourceopen', function() {
    sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vorbis,vp8"');

    socket.on("video", function(data) {
        let uIntArray = new Uint8Array(data);

        if (!sourceBuffer.updating) {
            sourceBuffer.appendBuffer(uIntArray);
        } else {
            queue.push(data);
        }
    });
});

Server side code (snippet)

io.on('connection', function(socket) {
            console.log("Client connected");

            let readStream = fs.createReadStream("bunny.webm");
            readStream.addListener('data', function(data) {
                socket.emit('video', data);
            });
        });

I also removed the webkit checks since this will only run on Chromium browsers.

like image 534
Jannis Lehmann Avatar asked Dec 02 '15 18:12

Jannis Lehmann


People also ask

What is a sourcebuffer in a MediaSource?

A MediaSource represents a source of media for an audio or video element. Once a MediaSource object is instantiated and its open event has fired, SourceBuffer s can be added to it. These act as buffers for media segments: // Get video segments and append them to sourceBuffer.

Why can't I create a new sourcebuffer from A mimetype?

The user agent can't handle any more SourceBuffer objects, or creating a new SourceBuffer using the given mimeType would result in an unsupported configuration of SourceBuffer s. The following snippet is from a simple example written by Nick Desaulniers ( view the full demo live, or download the source for further investigation.) Experimental.

What is the mimetype of the MediaSource?

The value specified for mimeType is an empty string rather than a valid MIME type. The MediaSource is not in the "open" readyState. The specified mimeType isn't supported by the user agent, or is not compatible with the MIME types of other SourceBuffer objects that are already included in the media source's sourceBuffers list.

What is media source extensions API (MSE)?

That works well in simple use cases, but for techniques such as adaptive streaming, the Media Source Extensions API (MSE) provides more control. MSE enables streams to be built in JavaScript from segments of audio or video. The code below is from that example. A MediaSource represents a source of media for an audio or video element.


1 Answers

I think you have to free the buffer, see the remove() function http://w3c.github.io/media-source/#widl-SourceBuffer-remove-void-double-start-unrestricted-double-end

Let me know if it helped.

like image 169
François Richard Avatar answered Oct 18 '22 11:10

François Richard