javascriptmp4mp4boxfmp4

Using MP4box.js and onSegment callback is not called


Base problem: display a H264 live stream in a browser.

Solution: let's just convert it to fragmented mp4 and load chunk-by-chunk via websocket (or XHR) into MSE.

Sounds too easy. But I want to do the fragmentation on client side with pure JS.

So I'm trying to use MP4Box.js. On its readme page it states: it has a demo: "A player that performs on-the-fly fragmentation".

That's the thing I need!

However the onSegment callbacks which should feed MSE are not called at all:

var ws;      //for websocket
var mp4box;  //for fragmentation


function startVideo() {
  mp4box = MP4Box.createFile();

  mp4box.onError = function(e) {
    console.log("mp4box failed to parse data.");
  };

  mp4box.onMoovStart = function () {
    console.log("Starting to receive File Information");
  };

  mp4box.onReady = function(info) {
    console.log(info.mime);

    mp4box.onSegment = function (id, user, buffer, sampleNum) {
      console.log("Received segment on track "+id+" for object "+user+" with a length of "+buffer.byteLength+",sampleNum="+sampleNum);
    }

    var options = { nbSamples: 1000 };
    mp4box.setSegmentOptions(info.tracks[0].id, null, options); // I don't need user object this time
    var initSegs = mp4box.initializeSegmentation();
    mp4box.start();
  };

  ws = new WebSocket("ws://a_websocket_server_which_serves_h264_file");
  ws.binaryType = "arraybuffer";
  ws.onmessage = function (event) {
    event.data.fileStart = 0; //tried also with event.data.byteOffset, but resulted error.
    var nextBufferStart = mp4box.appendBuffer(event.data);
    mp4box.flush(); //tried commenting out - unclear documentation!
  };
} 


window.onload = function() {
  startVideo();
}

Now putting this into an HTML file would result this in the JavaScript console:

Starting to receive File Information
video/mp4; codecs="avc1.4d4028"; profiles="isom,iso2,avc1,iso6,mp41"

But nothing happens afterwards. Why is the onSegment not called here? (the h264 file which the websocket-server serves is playable in VLC - however it is not fragmented)


Solution

  • The problem was using the nextBufferStart in a wrong way.

    This should be the correct one:

    var nextBufferStart = 0;
    ...
    ws.onmessage = function (event) {
        event.data.fileStart = nextBufferStart;
        nextBufferStart = mp4box.appendBuffer(event.data);
        mp4box.flush();
    };