Skip to content

Instantly share code, notes, and snippets.

@tnoho
Last active April 29, 2024 16:41
Show Gist options
  • Save tnoho/948be984f9981b59df43 to your computer and use it in GitHub Desktop.
Save tnoho/948be984f9981b59df43 to your computer and use it in GitHub Desktop.
Particular video codec remover from WebRTC SDP

What's this?

Javascript function snippet. It can use for remove video codec from your WebRTC SDP. When you want to test particular video codec, it's very useful!

WebRTCのSDPから特定のコーデックを削除する関数です。 特定のコーデックで試験を行いたい場合、非常に便利にご利用になれます。

Tested Browser

  • Firefox 44.0.2
  • Chrome 48.0.2564.116
  • Chrome 51.0.2665.0 canary with H.264 Option!

How to use

Like this!

peer_connection.createOffer(function(offer) {
    offer.sdp = removeCodec(offer.sdp, "H264");
    offer.sdp = removeCodec(offer.sdp, "VP9");
    peer_connection.setLocalDescription(offer);
}, function(e) {}, media_constraints);

# removeCodec function

Sorry! multistream IS NOT supported.

function removeCodec(orgsdp, codec) {
    var internalFunc = function(sdp) {
        var codecre = new RegExp('(a=rtpmap:(\\d*) ' + codec + '\/90000\\r\\n)');
        var rtpmaps = sdp.match(codecre);
        if(rtpmaps == null || rtpmaps.length <= 2) {
            return sdp;
        }
        var rtpmap = rtpmaps[2];
        var modsdp = sdp.replace(codecre, "");var rtcpre = new RegExp('(a=rtcp-fb:' + rtpmap + '.*\r\n)', 'g');
        modsdp = modsdp.replace(rtcpre, "");var fmtpre = new RegExp('(a=fmtp:' + rtpmap + '.*\r\n)', 'g');
        modsdp = modsdp.replace(fmtpre, "");var aptpre = new RegExp('(a=fmtp:(\\d*) apt=' + rtpmap + '\\r\\n)');
        var aptmaps = modsdp.match(aptpre);
        var fmtpmap = "";
        if(aptmaps != null && aptmaps.length >= 3) {
            fmtpmap = aptmaps[2];
            modsdp = modsdp.replace(aptpre, "");var rtppre = new RegExp('(a=rtpmap:' + fmtpmap + '.*\r\n)', 'g');
            modsdp = modsdp.replace(rtppre, "");
        }var videore = /(m=video.*\r\n)/;
        var videolines = modsdp.match(videore);
        if(videolines != null) {
            //If many m=video are found in SDP, this program doesn't work.
            var videoline = videolines[0].substring(0, videolines[0].length - 2);
            var videoelem = videoline.split(" ");
            var modvideoline = videoelem[0];
            for(var i = 1; i < videoelem.length; i++) {
                if(videoelem[i] == rtpmap || videoelem[i] == fmtpmap) {
                    continue;
                }
                modvideoline += " " + videoelem[i];
            }
            modvideoline += "\r\n";
            modsdp = modsdp.replace(videore, modvideoline);
        }
        return internalFunc(modsdp);
    };
    return internalFunc(orgsdp);
}

License

CC0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment