How to reduce Web RTC noise, clicking sound during conversation

Viewed 505

I have created an app with a live conversation module in javascript. I am using WebRTC to set up the Peer connection. Signaling and candidates all seem to work correctly. The peers are not on the same network.

At some point, the audio starts to add clicks to the stream. The quality starts to fail quite fast.

I have tried to reduce bitrates but to no avail. The code to reduce bitrate looks like this:

function setMediaBitrates(sdp, videoRate = 250, audioRate = 50) {
    let f = navigator.userAgent.search("Firefox");
    let modifier = 'AS';
    // if (f > -1) {
    //     videoRate = (videoRate >>> 0) * 1000;
    //     audioRate = (audioRate >>> 0) * 1000;
    //     modifier = 'TIAS';
    // }

    //find the correct m lines (video and audio)
    var lines = sdp.sdp.split("\n");
    let i = 0;
    while (i < lines.length) {
        //if we find an m-line
        if (lines[i].indexOf("m=") === 0) {
            let video = lines[i].indexOf("m=video") === 0;
            let audio = lines[i].indexOf("m=audio") === 0;

            if (video || audio) {
                while(i < lines.length && (lines[i].indexOf("i=") === 0 || lines[i].indexOf("c=") === 0)) {
                    i++;
                }

                // If we're on a b line, replace it
                if (i < lines.length && lines[i].indexOf("b") === 0) {
                    lines[i] = `b=${modifier}:${video ? videoRate : audioRate}`;
                } else if (i == lines.length) {
                    lines.push(`b=${modifier}:${video ? videoRate : audioRate}`);
                    i++;
                } else {
                    i++;
                    lines.splice(i, 0, `b=${modifier}:${video ? videoRate : audioRate}`);
                }
            }
        }
        i++;
    }
    
    let s = lines.join("\n");    
    return { type : sdp.type, sdp : s };
}   

With rate limiting, I have about 15 minutes of good audio, and after that static starts to appear.

I am starting the peer connection using the following ReactJS code:

pc
.createOffer({ offerToReceiveVideo : useVideo ? 1 : 0 })
.then(sdp => {
  pc.setLocalDescription(sdp);

  let newSdp = setMediaBitrates(sdp);
  dispatch(sendWebRTCSdp(activeConversationId, newSdp));
})
.catch(e => {});
1 Answers

you could use MediaTrackSettings.noiseSuppression but it still has compatibility issues with safari. you could also disable echoCancellation because sometimes hardware noise suppression can have weird effects on slow internet. I remember I had a problem with the clicking sound but it was just these parameters that should be given to getUserMedia() and I made sure to set myVideo.muted = true; in which myVideo is the video element on the page it should be muted so that your voice and the other person's voice don't echo repeatedly through both of your audio output devices.

some sources that helped me: https://developers.google.com/web/updates/2017/12/disabling-hardware-noise-suppression

https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings/noiseSuppression

Related