简体   繁体   中英

How to control mono/stereo in WebRTC audio call?

I'm trying to force my audio calls to mono-only, I'm willing to use PCMU, G.729, OPUS and SpeeX as my codecs for this calls.

Right now I'm using the following code to search for the chosen codec in my sdp message:

function maybePreferCodec(sdp, type, dir, codec) {
    var str = type + ' ' + dir + ' codec';
    if (codec === '') {
        return sdp;
    }

    var sdpLines = sdp.split('\r\n');

     // Search for m line.
    var mLineIndex = findLine(sdpLines, 'm=', type);
    if (mLineIndex === null) {
        return sdp;
    }

    // If the codec is available, set it as the default in m line.
    var codecIndex = findLine(sdpLines, 'a=rtpmap', codec);
    console.log('codecIndex', codecIndex);
    if (codecIndex) {
        var payload = getCodecPayloadType(sdpLines[codecIndex]);
        if (payload) {
             sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex],       payload);
        }
    }

    sdp = sdpLines.join('\r\n');
    return sdp;
}

The other functions can be found here:

http://www.codeforge.com/read/252733/sdputils.js__html

There are many other functions on the link but I don't know if they'll properly work on my selected codecs.

Thanks in advance!

For audio, the format of "a=rtpmap" lines is:

a=rtpmap:<payload type> <encoding name>/<clock rate>[/<number of channels>]

For example:

a=rtpmap:111 opus/48000/2

So, you can scan for those lines, and remove any codec with 2 channels. Note that to remove a codec, you'll also need to remove the payload type (in this case, 111) from the "m=" line, and remove "a=fmtp" lines for it. I believe sdputils.js has code to do this sort of thing.

Opus is a bit of a special case though, because it always appears as having 2 channels, which allows it to switch between mono and stereo in-band without doing a new offer/answer. So with Opus, stereo vs. mono preference is indicated by a "stereo" parameter which is set to 0 or 1:

a=fmtp:111 stereo=0

You can use https://github.com/beradrian/sdpparser and then modify the entire SDP payload as a JSON object. Disclaimer: I'm the author of sdpparser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM