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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With