Skip to content

Instantly share code, notes, and snippets.

@xlphs
Created February 16, 2016 17:29
Show Gist options
  • Save xlphs/96fed27f1be4842fed24 to your computer and use it in GitHub Desktop.
Save xlphs/96fed27f1be4842fed24 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>SDP Rewrite Test</title>
</head>
<body>
<textarea rows="20" cols="80" id="sdp">
v=0
o=- 9085987410276364131 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE audio video
a=msid-semantic: WMS
m=audio 9 UDP/TLS/RTP/SAVPF 111 103 104 9 0 8 106 105 13 126
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-ufrag:kpij9ZBZDz4eSOuL
a=ice-pwd:0IvC2V32/ojX6rWax4SFZPSY
a=fingerprint:sha-256 3D:C3:B4:92:93:FF:C2:ED:E9:5D:65:DF:05:9A:99:3C:A9:2C:9F:8F:41:33:02:02:71:08:01:60:8E:14:C7:D8
a=setup:actpass
a=mid:audio
a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=recvonly
a=rtcp-mux
a=rtpmap:111 opus/48000/2
a=fmtp:111 minptime=10; useinbandfec=1
a=rtpmap:103 ISAC/16000
a=rtpmap:104 ISAC/32000
a=rtpmap:9 G722/8000
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:106 CN/32000
a=rtpmap:105 CN/16000
a=rtpmap:13 CN/8000
a=rtpmap:126 telephone-event/8000
a=maxptime:60
m=video 9 UDP/TLS/RTP/SAVPF 100 101 116 117 96 126
c=IN IP4 0.0.0.0
a=rtcp:9 IN IP4 0.0.0.0
a=ice-ufrag:kpij9ZBZDz4eSOuL
a=ice-pwd:0IvC2V32/ojX6rWax4SFZPSY
a=fingerprint:sha-256 3D:C3:B4:92:93:FF:C2:ED:E9:5D:65:DF:05:9A:99:3C:A9:2C:9F:8F:41:33:02:02:71:08:01:60:8E:14:C7:D8
a=setup:actpass
a=mid:video
a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
a=extmap:4 urn:3gpp:video-orientation
a=recvonly
a=rtcp-mux
a=rtpmap:100 VP8/90000
a=rtcp-fb:100 ccm fir
a=rtcp-fb:100 nack
a=rtcp-fb:100 nack pli
a=rtcp-fb:100 goog-remb
a=rtpmap:101 VP9/90000
a=rtcp-fb:101 ccm fir
a=rtcp-fb:101 nack
a=rtcp-fb:101 nack pli
a=rtcp-fb:101 goog-remb
a=rtpmap:126 H264/90000
a=rtpmap:116 red/90000
a=rtpmap:117 ulpfec/90000
a=rtpmap:96 rtx/90000
a=fmtp:96 apt=100</textarea>
<br>
<button onclick="updateSdp()">Rewrite</button>
<script type="text/javascript">
function updateSdp(sdpOffer) {
document.getElementById('sdp').value = rewriteSdpOffer(document.getElementById('sdp').value);
}
function rewriteSdpOffer(sdpOffer) {
// find the line starting with 'm=video'
var lines = sdpOffer.split("\n");
var mvideoIdx = -1;
for (var i=0; i<lines.length; i++) {
if (lines[i].indexOf("m=video") != -1) {
mvideoIdx = i;
break;
}
}
if (mvideoIdx == -1) return;
var rtpmapTokens = lines[mvideoIdx].split(" ");
if (rtpmapTokens.length < 4) return;
var getRtpMapLine = function(token) {
for (var i=mvideoIdx+1; i<lines.length; i++) {
if (lines[i].indexOf(token) != -1) {
return lines[i];
}
}
};
var allowedRtpmapTokens = [];
var unwantedRtpmapToken = [];
for (var i=3; i<rtpmapTokens.length; i++) {
var line = getRtpMapLine(rtpmapTokens[i]);
if (line.indexOf("H264/9000") != -1) {
allowedRtpmapTokens.push(rtpmapTokens[i]);
} else {
unwantedRtpmapToken.push(rtpmapTokens[i]);
}
}
if (allowedRtpmapTokens.length == 0) return;
// remove unwanted codec from m=video line
for (var i=3; i<rtpmapTokens.length; i++) {
if (allowedRtpmapTokens.indexOf(rtpmapTokens[i]) == -1) {
rtpmapTokens.splice(i, 1);
i = 3;
}
}
if (allowedRtpmapTokens.indexOf(rtpmapTokens[3]) == -1) {
rtpmapTokens.splice(3, 1);
}
lines[mvideoIdx] = rtpmapTokens.join(" ");
// delete unwanted rtpmap lines
var isUnwanted = function(rtpmapLine) {
if ((rtpmapLine.substr(0, 9) == "a=rtpmap:" ||
rtpmapLine.substr(0, 10) == "a=rtcp-fb:"))
{
for (var i=0; i<unwantedRtpmapToken.length; i++) {
if (rtpmapLine.indexOf(unwantedRtpmapToken[i]) != -1) return true;
}
}
return false;
};
for (var i=mvideoIdx+1; i<lines.length; i++) {
if (isUnwanted(lines[i])) {
lines.splice(i, 1);
i = mvideoIdx + 1;
}
}
return lines.join("\n");
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment