muaz-khan / RecordRTC

RecordRTC is WebRTC JavaScript library for audio/video as well as screen activity recording. It supports Chrome, Firefox, Opera, Android, and Microsoft Edge. Platforms: Linux, Mac and Windows.

Home Page:https://www.webrtc-experiment.com/RecordRTC/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

StereoAudioRecorder gobbles memory; how to release?

darrellsilver opened this issue · comments

Long recordings eat up a lot of memory, how to release it? Just using Chrome / Mac the below simple code, and have tried several methods implied by their names to clear any stored data.

Shutting off the recording stops the accumulation but that won't work in my application. Any ideas?

<html>
    <head>
        <script src="https://www.WebRTC-Experiment.com/RecordRTC.js"></script>
        <script>
        let mediaRecorder;
        function startRecording() {
            navigator.mediaDevices.getUserMedia({audio: true}).then((stream) => {
                // audioStream = stream
                mediaRecorder = new RecordRTC(stream, {
                    type: "audio",
                    mimeType: "audio/wav;codecs=pcm", // endpoint requires 16bit PCM audio
                    recorderType: StereoAudioRecorder,
                    timeSlice: 250, // set 250 ms intervals of data that sends to AAI
                });
                mediaRecorder.startRecording();
            });
        }
        </script>
    </head>
    <body onload="startRecording()">
        <h1>leaky memories</h1>
    </body>
</html>

Trying these either break the recording or have no apaprent effect:

  • clearRecordedData()
  • clearRecordedDataCB()
  • resetVariables() <-- doesn't work
  • reset() <-- stops recording

Got it working by manually freeing the data stored by StereoAudioRecorder. Seems like a hack so going to leave this issue open w/ my code.

Add a freeMemory func to RecordRTC.js:

    function resetVariables(stopRecording) {
        leftchannel = [];
        rightchannel = [];
        recordingLength = 0;
        if (stopRecording === undefined) {
            console.log('stopRecording all');
            isAudioProcessStarted = false;
            recording = false;
            isPaused = false;
            context = null;
        }

        self.leftchannel = leftchannel;
        self.rightchannel = rightchannel;
        self.numberOfAudioChannels = numberOfAudioChannels;
        self.desiredSampRate = desiredSampRate;
        self.sampleRate = sampleRate;
        self.recordingLength = recordingLength;

        intervalsBasedBuffers = {
            left: [],
            right: [],
            recordingLength: 0
        };
    }

    this.freeMemory = function() {
        resetVariables(false);
    }

Then in my code, as part of ondataavailable I call
mediaRecorder.getInternalRecorder().freeMemory()

According to Chrome Memory tool it works & in Safari too (Silicon Mac 13.4.1).

I was struggling to deal with this memory consumption. I ended up removing RecordRTC.js lines 3136, 3141 and 3144. This class absolutely save me, but this "memory leak" almost ruins everything.

Maybe some config in constructor do control wheter to keep this buffer or not, or the maximum number of bytes to save, can be very helpful.

Got it working by manually freeing the data stored by StereoAudioRecorder. Seems like a hack so going to leave this issue open w/ my code.

Add a freeMemory func to RecordRTC.js:

    function resetVariables(stopRecording) {
        leftchannel = [];
        rightchannel = [];
        recordingLength = 0;
        if (stopRecording === undefined) {
            console.log('stopRecording all');
            isAudioProcessStarted = false;
            recording = false;
            isPaused = false;
            context = null;
        }

        self.leftchannel = leftchannel;
        self.rightchannel = rightchannel;
        self.numberOfAudioChannels = numberOfAudioChannels;
        self.desiredSampRate = desiredSampRate;
        self.sampleRate = sampleRate;
        self.recordingLength = recordingLength;

        intervalsBasedBuffers = {
            left: [],
            right: [],
            recordingLength: 0
        };
    }

    this.freeMemory = function() {
        resetVariables(false);
    }

Then in my code, as part of ondataavailable I call mediaRecorder.getInternalRecorder().freeMemory()

According to Chrome Memory tool it works & in Safari too (Silicon Mac 13.4.1).

Today I was dealing with the same problem, I could see in the chrome memory how RecordRTC was allocating memory non-stop, for now adding this function solved the problem.