MenuMenu

RTCMultiConnection.processSdp

Change SDP before creating any WebRTC connection

This method can be used to modify SDP yourself!

You can modify SDP to force h264, vp9 or vp8 codecs.

You can even modify SDP for application-level bandwidth and many other SDP-attributes.

Usage

connection.processSdp = function(sdp) {
    // modify SDP here
    sdp = remove_vp8_codecs(sdp);
    sdp = prefer_opus(sdp);
    sdp = use_maxaveragebitrate(sdp);

    // return the modified SDP
    return sdp;
};

Default implementation

connection.processSdp = function(sdp) {
    if (DetectRTC.browser.name === 'Safari') {
        return sdp;
    }

    if (connection.codecs.video.toUpperCase() === 'VP8') {
        sdp = CodecsHandler.preferCodec(sdp, 'vp8');
    }

    if (connection.codecs.video.toUpperCase() === 'VP9') {
        sdp = CodecsHandler.preferCodec(sdp, 'vp9');
    }

    if (connection.codecs.video.toUpperCase() === 'H264') {
        sdp = CodecsHandler.preferCodec(sdp, 'h264');
    }

    if (connection.codecs.audio === 'G722') {
        sdp = CodecsHandler.removeNonG722(sdp);
    }

    if (DetectRTC.browser.name === 'Firefox') {
        return sdp;
    }

    if (connection.bandwidth.video || connection.bandwidth.screen) {
        sdp = CodecsHandler.setApplicationSpecificBandwidth(sdp, connection.bandwidth, !!connection.session.screen);
    }

    if (connection.bandwidth.video) {
        sdp = CodecsHandler.setVideoBitrates(sdp, {
            min: connection.bandwidth.video * 8 * 1024,
            max: connection.bandwidth.video * 8 * 1024
        });
    }

    if (connection.bandwidth.audio) {
        sdp = CodecsHandler.setOpusAttributes(sdp, {
            maxaveragebitrate: connection.bandwidth.audio * 8 * 1024,
            maxplaybackrate: connection.bandwidth.audio * 8 * 1024,
            stereo: 1,
            maxptime: 3
        });
    }

    return sdp;
};

Description

parameterdescription
sdp SDP string is passed as first parameter

Force ISAC audio; and remove all other codecs

connection.processSdp = function (sdp) {
    sdp = forceIsac(sdp);
    return sdp;
};

function forceIsac(sdp) {
    // Remove all other codecs (not the video codecs though).
    sdp = sdp.replace(/m=audio (\d+) RTP\/SAVPF.*\r\n/g,
        'm=audio $1 RTP/SAVPF 104\r\n');
    sdp = sdp.replace('a=fmtp:111 minptime=10', 'a=fmtp:104 minptime=10');
    sdp = sdp.replace(/a=rtpmap:(?!104)\d{1,3} (?!VP8|red|ulpfec).*\r\n/g, '');
    return sdp;
}

Disable Video NACK

<script src="/dev/CodecsHandler.js"></script>
<script>
// in your HTML file
connection.processSdp = function(sdp) {
    // Disable NACK to test IDR recovery
    sdp = CodecsHandler.disableNACK(sdp);
    return sdp;
};
</script>

Use BandwidthHandler

var BandwidthHandler = connection.BandwidthHandler;
connection.bandwidth = {
    audio: 128,
    video: 256,
    screen: 300
};
connection.processSdp = function(sdp) {
    sdp = BandwidthHandler.setApplicationSpecificBandwidth(sdp, connection.bandwidth, !!connection.session.screen);
    sdp = BandwidthHandler.setVideoBitrates(sdp, {
        min: connection.bandwidth.video,
        max: connection.bandwidth.video
    });

    sdp = BandwidthHandler.setOpusAttributes(sdp);

    sdp = BandwidthHandler.setOpusAttributes(sdp, {
        'stereo': 1,
        //'sprop-stereo': 1,
        'maxaveragebitrate': connection.bandwidth.audio * 1000 * 8,
        'maxplaybackrate': connection.bandwidth.audio * 1000 * 8,
        //'cbr': 1,
        //'useinbandfec': 1,
        // 'usedtx': 1,
        'maxptime': 3
    });

    return sdp;
};

Use SdpSerializer

// https://cdn.webrtc-experiment.com/SdpSerializer.js
connection.processSdp = function (sdp) {
    var serializer = new SdpSerializer(sdp);
    
    // remove entire audio m-line
    serializer.audio.remove();

    // change order of a payload type in video m-line
    serializer.video.payload(117).order(0);

    // inject new-line after a specific payload type; under video m-line
    serializer.video.payload(117).newLine('a=ptime:10');

    // remove a specific payload type; under video m-line
    serializer.video.payload(100).remove();

    // want to add/replace a crypto line?
    serializer.video.crypto().newLine('a=crypto:0 AES_CM_128_HMAC_SHA1_80 inline:AAAAAAAAAAAAAAAAAAAAAAAAA');

    // want to remove a crypto line?
    serializer.video.crypto(80).remove();

    // want to set direction?
    serializer.video.direction.set('sendonly');

    // want to get direction?
    serializer.video.direction.get();

    // want to remove entire audio or video track?
    // usually, in video m-line:
    // 0-track is always "video" stream
    // 1-track will be screen sharing stream (if attached)
    serializer.video.track(0).remove()

    // get serialized sdp
    sdp = serializer.deserialize();
    
    return sdp;
};

Demo

<script src="https://rtcmulticonnection.herokuapp.com/dist/RTCMultiConnection.min.js"></script>
<script src="https://rtcmulticonnection.herokuapp.com/socket.io/socket.io.js"></script>

<script>
var connection = new RTCMultiConnection();

// this line is VERY_important
connection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/';

// if you want audio+video conferencing
connection.session = {
    audio: true,
    video: true
};

var BandwidthHandler = connection.BandwidthHandler;
connection.bandwidth = {
    audio: 128,
    video: 256,
    screen: 300
};
connection.processSdp = function(sdp) {
    sdp = BandwidthHandler.setApplicationSpecificBandwidth(sdp, connection.bandwidth, !!connection.session.screen);
    sdp = BandwidthHandler.setVideoBitrates(sdp, {
        min: connection.bandwidth.video,
        max: connection.bandwidth.video
    });

    sdp = BandwidthHandler.setOpusAttributes(sdp);

    sdp = BandwidthHandler.setOpusAttributes(sdp, {
        'stereo': 1,
        //'sprop-stereo': 1,
        'maxaveragebitrate': connection.bandwidth.audio * 1000 * 8,
        'maxplaybackrate': connection.bandwidth.audio * 1000 * 8,
        //'cbr': 1,
        //'useinbandfec': 1,
        // 'usedtx': 1,
        'maxptime': 3
    });

    return sdp;
};

connection.openOrJoin('your-room-id');
</script>