最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to control monostereo in WebRTC audio call? - Stack Overflow

programmeradmin5浏览0评论

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:

.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!

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./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!

Share Improve this question asked Feb 8, 2017 at 16:57 Alejandro AlhamaAlejandro Alhama 551 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

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./beradrian/sdpparser and then modify the entire SDP payload as a JSON object. Disclaimer: I'm the author of sdpparser.

发布评论

评论列表(0)

  1. 暂无评论