Last active
September 1, 2015 22:51
-
-
Save ggarber/1e9bb9ea264c37aa05f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AUDIO_SDP_TEMPLATE = "v=0\r\no=- 3576587452431152789 2 IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\nm=audio 1 RTP/SAVPF 111 0 8\r\nc=IN IP4 0.0.0.0\r\na=rtcp:1 IN IP4 0.0.0.0\r\na=ice-ufrag:{2}\r\na=ice-pwd:{3}\r\na=fingerprint:{0} {1}\r\na=setup:actpass\r\na=mid:audio\r\na=sendrecv\r\na=rtcp-mux\r\na=rtpmap:111 opus/48000/2\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=ssrc:748822434 cname:OfO7E/zpleLM7chT\r\na=ssrc:748822434 msid:klYE03jD2B0DB3SriGc3gurGtX1PF8tUr5S8 1229f9a2-1418-4a47-8d9b-c94d356ce409\r\na=ssrc:748822434 mslabel:klYE03jD2B0DB3SriGc3gurGtX1PF8tUr5S8\r\na=ssrc:748822434 label:1229f9a2-1418-4a47-8d9b-c94d356ce409\r\n"; | |
if (!String.prototype.format) { | |
String.prototype.format = function() { | |
var args = arguments; | |
return this.replace(/{(\d+)}/g, function(match, number) { | |
return typeof args[number] != 'undefined' | |
? args[number] | |
: match | |
; | |
}); | |
}; | |
} | |
if (!String.prototype.format) { | |
String.prototype.format = function() { | |
var args = arguments; | |
return this.replace(/{(\d+)}/g, function(match, number) { | |
return typeof args[number] != 'undefined' | |
? args[number] | |
: match | |
; | |
}); | |
}; | |
} | |
function encodeSDP(params) { | |
return AUDIO_SDP_TEMPLATE.format( | |
params.fingerprint.algorithm, | |
params.fingerprint.value, | |
params.ice.ufrag, | |
params.ice.pwd); | |
} | |
function parseSDP(sdp) { | |
var candidate = /candidate:(.*) 1 (.*) (.*) (.*) (.*) typ (.*) generation/g.exec(sdp); | |
var params = { | |
fingerprint: { | |
algorithm: "sha-256", | |
value: /fingerprint:sha-256 (.*)\r\n/g.exec(sdp)[1] | |
}, | |
ice: { | |
ufrag: /ice-ufrag:(.*)\r\n/g.exec(sdp)[1], | |
pwd: /ice-pwd:(.*)\r\n/g.exec(sdp)[1], | |
candidates: [ { | |
component: 1, | |
foundation: candidate[1], | |
priority: candidate[3], | |
protocol: candidate[2], | |
ip: candidate[4], | |
port: candidate[5], | |
type: candidate[6] | |
} | |
] | |
} | |
}; | |
return params; | |
} | |
// Test encodeSDP | |
// console.log(encodeSDP({ | |
// fingerprint: { | |
// algorithm: "sha-256", | |
// value: "18:B1:2D:AD:11:FF:3D:3D:A5:F7:3C:E1:03:25:C7:4B:6D:15:11:1C:DB:2B:77:4D:C5:71:BB:A8:7A:63:6E:15" | |
// }, | |
// ice: { | |
// ufrag: "Gqy2a9xzp5ukk0AB", | |
// pwd: "124+uYPaafADxAQ0f6Bb7tH1" | |
// } | |
// }).replace(new RegExp('\r\n', 'g'), '\\r\\n')); | |
// Test parseSDP | |
var TEST_SDP = "v=0\r\n" + | |
"o=- 7919217494324810599 2 IN IP4 127.0.0.1\r\n" + | |
"s=-\r\n" + | |
"t=0 0\r\n" + | |
"a=group:BUNDLE audio video\r\n" + | |
"a=msid-semantic: WMS lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf\r\n" + | |
"m=audio 9 RTP/SAVPF 111 103 104 9 0 8 126\r\n" + | |
"c=IN IP4 0.0.0.0\r\n" + | |
"a=rtcp:9 IN IP4 0.0.0.0\r\n" + | |
"a=ice-ufrag:cBvQ1Cs3EW3Iz82r\r\n" + | |
"a=ice-pwd:o3F95RUbKfEqFUuKe8DiQ35a\r\n" + | |
"a=fingerprint:sha-256 C9:E0:27:41:4F:39:F2:7C:57:B8:14:37:60:0F:DA:90:18:09:C9:32:71:23:62:0C:CA:38:4E:6D:27:6E:9C:73\r\n" + | |
"a=setup:actpass\r\n" + | |
"a=mid:audio\r\n" + | |
"a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level\r\n" + | |
"a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\n" + | |
"a=sendrecv\r\n" + | |
"a=rtcp-mux\r\n" + | |
"a=rtpmap:111 opus/48000/2\r\n" + | |
"a=fmtp:111 minptime=10; useinbandfec=1\r\n" + | |
"a=rtpmap:103 ISAC/16000\r\n" + | |
"a=rtpmap:104 ISAC/32000\r\n" + | |
"a=rtpmap:9 G722/8000\r\n" + | |
"a=rtpmap:0 PCMU/8000\r\n" + | |
"a=rtpmap:8 PCMA/8000\r\n" + | |
"a=rtpmap:126 telephone-event/8000\r\n" + | |
"a=maxptime:60\r\n" + | |
"a=ssrc:666934306 cname:3XG98hRzkouimdbF\r\n" + | |
"a=ssrc:666934306 msid:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf 0b87e258-f61f-4462-a520-482f772f735c\r\n" + | |
"a=ssrc:666934306 mslabel:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf\r\n" + | |
"a=ssrc:666934306 label:0b87e258-f61f-4462-a520-482f772f735c\r\n" + | |
+ | |
"a=candidate:1796272311 1 UDP 2130706431 54.148.21.243 45618 typ host generation 0\r\n" + | |
"a=candidate:1796272311 2 UDP 2130706430 54.148.21.243 45618 typ host generation 0\r\n" + | |
"m=video 9 RTP/SAVPF 100 96\r\n" + | |
"c=IN IP4 0.0.0.0\r\n" + | |
"a=rtcp:9 IN IP4 0.0.0.0\r\n" + | |
"a=ice-ufrag:cBvQ1Cs3EW3Iz82r\r\n" + | |
"a=ice-pwd:o3F95RUbKfEqFUuKe8DiQ35a\r\n" + | |
"a=fingerprint:sha-256 C9:E0:27:41:4F:39:F2:7C:57:B8:14:37:60:0F:DA:90:18:09:C9:32:71:23:62:0C:CA:38:4E:6D:27:6E:9C:73\r\n" + | |
"a=setup:actpass\r\n" + | |
"a=mid:video\r\n" + | |
"a=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\n" + | |
"a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time\r\n" + | |
"a=extmap:4 urn:3gpp:video-orientation\r\n" + | |
"a=sendrecv\r\n" + | |
"a=rtcp-mux\r\n" + | |
"a=rtpmap:100 VP8/90000\r\n" + | |
"a=rtcp-fb:100 ccm fir\r\n" + | |
"a=rtcp-fb:100 nack\r\n" + | |
"a=rtcp-fb:100 nack pli\r\n" + | |
"a=rtcp-fb:100 goog-remb\r\n" + | |
"a=rtpmap:96 rtx/90000\r\n" + | |
"a=fmtp:96 apt=100\r\n" + | |
"a=ssrc-group:FID 1917303818 3760583944\r\n" + | |
"a=ssrc:1917303818 cname:3XG98hRzkouimdbF\r\n" + | |
"a=ssrc:1917303818 msid:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf 4c59ac63-d85d-4d65-a8f7-8f4ea0b9edc9\r\n" + | |
"a=ssrc:1917303818 mslabel:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf\r\n" + | |
"a=ssrc:1917303818 label:4c59ac63-d85d-4d65-a8f7-8f4ea0b9edc9\r\n" + | |
"a=ssrc:3760583944 cname:3XG98hRzkouimdbF\r\n" + | |
"a=ssrc:3760583944 msid:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf 4c59ac63-d85d-4d65-a8f7-8f4ea0b9edc9\r\n" + | |
"a=ssrc:3760583944 mslabel:lRLrH0K3Eaxn715rbIcSZ0jFL4bgNMFy56mf\r\n" + | |
"a=ssrc:3760583944 label:4c59ac63-d85d-4d65-a8f7-8f4ea0b9edc9\r\n" + | |
"a=candidate:1796272311 1 UDP 2130706431 54.148.21.243 45618 typ host generation 0\r\n" + | |
"a=candidate:1796272311 2 UDP 2130706430 54.148.21.243 45618 typ host generation 0\r\n" | |
console.log(JSON.stringify(parseSDP(TEST_SDP))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment