Pj Rigor
Pj Rigor

Reputation: 401

starting/stopping MediaRecorder API causes Chrome to crash

I am implementing the MediaRecorder API as a way to record webm blobs for use as segments in a livestream. I have gotten the functionality I need but ran into a problem with Chrome crashing when calling MediaRecorder.stop() and MediaRecorder.start() multiple times in regular intervals.

Here is the recording code:

let Recorder = null;
let segmentBuffer = [];
let recordInterval = null;
let times = 0;    //limiter for crashes

function startRecording() {
    Recorder = new MediaRecorder(LocalStream, { mimeType: 'video/webm;codecs=opus, vp8', audioBitsPerSecond: 50000, videoBitsPerSecond: 1000000, });
    //error evt
    Recorder.onerror = (evt) => {
        console.error(evt.error);
    }
    //push blob data to segments buffer
    Recorder.ondataavailable = (evt) => {
        segmentBuffer.push(evt.data);
    }
    //start initial recording
    Recorder.start();

    //set stop/start delivery interval every 5 seconds
    recordInterval = setInterval(() => {

        //stop recording
        Recorder.stop();

        //here to prevent crash
        if (times > 5) {
            Recorder = null;
            console.log('end')
            return;
        }
        times++;

        //check if has segments
        if (segmentBuffer.length) {
            //produce segment, this segment is playable and not just a byte-stream due to start/stop
            let webm = segmentBuffer.reduce((a, b) => new Blob([a, b], { type: "video/webm;codecs=opus, vp8" }));
            //unset buffer
            segmentBuffer = [];
            //handle blob ie. send to server
            handleBlob(webm)
        }
        //restart recorder
        Recorder.start();
    }, 5000);
}

I've also gone into the performance and discovered that a new audio and video encoder thread is started for each start/stop. I think this is the major problem as setting the interval to 10s vs. 5s creates fewer encoding threads. The buildup of multiple encoding threads causes chrome to lag and then finally crash afer a few passes.

How do I prevent multiple encoding threads from occurring while still being able to start/stop MediaRecorder (start/stop is the only way I found to achieve webm files that can be playable separately, otherwise each subsequent blob is missing the webm header part).

Upvotes: 2

Views: 1386

Answers (2)

Oleg Yanusik
Oleg Yanusik

Reputation: 1

In my case the problem is that stream has too high resolution. I'm calcuting best resolution using getCapabilities on each camera and the starting the stream.

Upvotes: 0

Jontonsoup
Jontonsoup

Reputation: 171

It appears that this is a bug in chrome:

https://bugs.chromium.org/p/chromium/issues/detail?id=1012378&q=mediaRecorder%20thread&can=2

I'm not sure there is anything you can do to fix it.

Upvotes: 0

Related Questions