0

I am trying to work out with first demo MediaSource try out but I am unable to play video. Source I am using is a webm video "big-buck-bunny_trailer.webm". I keep getting "Uncaught InvalidStateError: Failed to execute 'endOfStream' on 'MediaSource': The 'updating' attribute is true on one or more of this MediaSource's SourceBuffers.". I have handled the sourcebuffer updating flag as well but still it doesn't seem to work.

window.URL = window.URL || window.webkitURL;
window.MediaSource = window.MediaSource || window.WebKitMediaSource;
if (!!!window.MediaSource) {
    //handle fallback
    alert('MediaSource API is not available');
}
var mediaSource = new MediaSource();
var sourceBuffer;
var audio;
var playRange = 0;
var queue = [];
var playing = false;
var contentLength = undefined;

function ajaxMediaGET(url, range, async, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url);
    xhr.responseType = "arraybuffer";
    xhr.setRequestHeader("Range", range);
    xhr.addEventListener('load', onLoad, false);
    xhr.send();

    function onLoad() {
        playRange += Number(xhr.getResponseHeader("Content-Length"));
        if(!contentLength) {
            var contentLengthString = xhr.getResponseHeader("Content-Range").split("/");
            contentLength = Number(contentLengthString[1]);
        }
        //callback(new Uint8Array(xhr.response));

        sourceBuffer.addEventListener("update", function() {
            if(!sourceBuffer.updating && queue.length > 0) {// && mediaSource.readyState == "open"
                sourceBuffer.appendBuffer(queue.shift());
                sourceBuffer.removeEventListener("update");
            }
        }, false);

        if (!sourceBuffer.updating) {
            sourceBuffer.appendBuffer(new Uint8Array(xhr.response));
        } else {
            queue.push(new Uint8Array(xhr.response));
        }

        if(playRange < contentLength - 1) {
            ajaxMediaGET("path-to-server", loadNextRange(playRange), true, undefined);
        } else {
            mediaSource.endOfStream();
        }
    }
}

function mediaSourceOpen(e) {
    sourceBuffer = e.target.addSourceBuffer('video/webm;codecs="vp8,vorbis"');
    ajaxMediaGET("path-to-server", loadNextRange(playRange), true, undefined);
    /*if(!playing) {
        audio.play();
        playing = true;
    }*/
}

function initPlayer() {
    audio = document.querySelector('video');
    audio.src = window.URL.createObjectURL(mediaSource);
    mediaSource.addEventListener('sourceopen', mediaSourceOpen, false);
    mediaSource.addEventListener('webkitsourceopen', mediaSourceOpen, false);
}

initPlayer();
rohitpal
  • 455
  • 1
  • 6
  • 21
  • Tried chrome://media-internals/ got error, Unexpected element ID 0x45df 00:00:05 667 error media::SourceState::Append: stream parsing failed. Data size=61441 append_window_start=0 append_window_end=1.#INF. Any ideas – rohitpal Mar 02 '15 at 17:57
  • I don't know about .webm, but if you want to use MSE with `h264` in an `mp4` container, you have to give the `sourceBuffer` specific parts of the video (namely an initialization segment with a `moov` box and video segments with `moof+mdat` boxes). Surely there is a special format for .webm content that you need to follow in order to be able to use MSE correctly. – Pablo Montilla Mar 03 '15 at 12:45

1 Answers1

-2

You need to wait until the sourceBuffer has completed updating. Replace mediaSource.endOfStream(); with

if(playRange < contentLength - 1) {
    ajaxMediaGET("path-to-server", loadNextRange(playRange), true, undefined);
} else {
    sourceBuffer.addEventListener('updateend', function(){
        mediaSource.endOfStream();
    });
}

Have a look at the docs and sample code for MediaSource.endOfStream()