sdp
Advanced tools
Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "sdp", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "SDP parsing and serialization utilities", | ||
@@ -11,3 +11,4 @@ "main": "sdp.js", | ||
"scripts": { | ||
"test": "eslint sdp.js test/sdp.js && node test/sdp.js" | ||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"test": "eslint sdp.js test/sdp.js && nyc --reporter html mocha test/sdp.js" | ||
}, | ||
@@ -21,5 +22,10 @@ "keywords": [ | ||
"devDependencies": { | ||
"chai": "^4.0.0", | ||
"codecov": "^2.2.0", | ||
"eslint": "^2.8.0", | ||
"tape": "^4.0.0" | ||
"mocha": "^3.4.2", | ||
"nyc": "^10.3.2", | ||
"sinon": "^2.3.2", | ||
"sinon-chai": "^2.10.0" | ||
} | ||
} |
31
sdp.js
@@ -103,2 +103,6 @@ /* eslint-env node */ | ||
} | ||
if (candidate.ufrag) { | ||
sdp.push('ufrag'); | ||
sdp.push(candidate.ufrag); | ||
} | ||
return 'candidate:' + sdp.join(' '); | ||
@@ -449,3 +453,7 @@ }; | ||
} else if (bandwidth[0].indexOf('b=AS:') === 0) { | ||
bandwidth = parseInt(bandwidth[0].substr(5), 10); | ||
// use formula from JSEP to convert b=AS to TIAS value. | ||
bandwidth = parseInt(bandwidth[0].substr(5), 10) * 1000 * 0.95 | ||
- (50 * 40 * 8); | ||
} else { | ||
bandwidth = undefined; | ||
} | ||
@@ -514,6 +522,23 @@ encodingParameters.forEach(function(params) { | ||
SDPUtils.writeSessionBoilerplate = function() { | ||
// Generate a session ID for SDP. | ||
// https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-20#section-5.2.1 | ||
// recommends using a cryptographically random +ve 64-bit value | ||
// but right now this should be acceptable and within the right range | ||
SDPUtils.generateSessionId = function() { | ||
return Math.random().toString().substr(2, 21); | ||
}; | ||
// Write boilder plate for start of SDP | ||
// sessId argument is optional - if not supplied it will | ||
// be generated randomly | ||
SDPUtils.writeSessionBoilerplate = function(sessId) { | ||
var sessionId; | ||
if (sessId) { | ||
sessionId = sessId; | ||
} else { | ||
sessionId = SDPUtils.generateSessionId(); | ||
} | ||
// FIXME: sess-id should be an NTP timestamp. | ||
return 'v=0\r\n' + | ||
'o=thisisadapterortc 8169639915646943137 2 IN IP4 127.0.0.1\r\n' + | ||
'o=thisisadapterortc ' + sessionId + ' 2 IN IP4 127.0.0.1\r\n' + | ||
's=-\r\n' + | ||
@@ -520,0 +545,0 @@ 't=0 0\r\n'; |
807
test/sdp.js
@@ -5,6 +5,10 @@ /* jshint node: true */ | ||
// tests for the Edge SDP parser. Tests plain JS so can be run in node. | ||
var test = require('tape'); | ||
var SDPUtils = require('../sdp.js'); | ||
const SDPUtils = require('../sdp.js'); | ||
var videoSDP = | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
const sinon = require('sinon'); | ||
chai.use(require('sinon-chai')); | ||
const videoSDP = | ||
'v=0\r\no=- 1376706046264470145 3 IN IP4 127.0.0.1\r\ns=-\r\n' + | ||
@@ -47,3 +51,3 @@ 't=0 0\r\na=group:BUNDLE video\r\n' + | ||
// Firefox offer | ||
var videoSDP2 = | ||
const videoSDP2 = | ||
'v=0\r\n' + | ||
@@ -72,226 +76,677 @@ 'o=mozilla...THIS_IS_SDPARTA-45.0 5508396880163053452 0 IN IP4 0.0.0.0\r\n' + | ||
test('splitSections', function(t) { | ||
var parsed = SDPUtils.splitSections(videoSDP.replace(/\r\n/g, '\n')); | ||
t.ok(parsed.length === 2, | ||
'split video-only SDP with only LF into two sections'); | ||
describe('splitSections', () => { | ||
let parsed; | ||
it('splits video-only SDP with only LF into two sections', () => { | ||
parsed = SDPUtils.splitSections(videoSDP.replace(/\r\n/g, '\n')); | ||
expect(parsed.length).to.equal(2); | ||
}); | ||
parsed = SDPUtils.splitSections(videoSDP); | ||
t.ok(parsed.length === 2, 'split video-only SDP into two sections'); | ||
it('splits video-only SDP into two sections', () => { | ||
parsed = SDPUtils.splitSections(videoSDP); | ||
expect(parsed.length).to.equal(2); | ||
}); | ||
it('every section ends with CRLF', () => { | ||
expect(parsed.every(function(section) { | ||
return section.substr(-2) === '\r\n'; | ||
})).to.equal(true); | ||
}); | ||
t.ok(parsed.every(function(section) { | ||
return section.substr(-2) === '\r\n'; | ||
}), 'every section ends with CRLF'); | ||
it('joining sections without separator recreates SDP', () => { | ||
expect(parsed.join('')).to.equal(videoSDP); | ||
}); | ||
}); | ||
t.ok(parsed.join('') === videoSDP, | ||
'joining sections without separator recreates SDP'); | ||
t.end(); | ||
describe('parseRtpParameters with the video sdp example', () => { | ||
const sections = SDPUtils.splitSections(videoSDP); | ||
const parsed = SDPUtils.parseRtpParameters(sections[1]); | ||
it('parses 9 codecs', () => { | ||
expect(parsed.codecs.length).to.equal(9); | ||
}); | ||
describe('fecMechanisms', () => { | ||
it('parses 2 fecMechanisms', () => { | ||
expect(parsed.fecMechanisms.length).to.equal(2); | ||
}); | ||
it('parses RED as FEC mechanism', () => { | ||
expect(parsed.fecMechanisms).to.contain('RED'); | ||
}); | ||
it('parses ULPFEC as FEC mechanism', () => { | ||
expect(parsed.fecMechanisms).to.contain('ULPFEC'); | ||
}); | ||
}); | ||
it('parses 3 headerExtensions', () => { | ||
expect(parsed.headerExtensions.length).to.equal(3); | ||
}); | ||
}); | ||
test('parseRtpParameters', function(t) { | ||
var sections = SDPUtils.splitSections(videoSDP); | ||
var parsed = SDPUtils.parseRtpParameters(sections[1]); | ||
t.ok(parsed.codecs.length === 9, 'parsed 9 codecs'); | ||
t.ok(parsed.fecMechanisms.length === 2, 'parsed FEC mechanisms'); | ||
t.ok(parsed.fecMechanisms.indexOf('RED') !== -1, | ||
'parsed RED as FEC mechanism'); | ||
t.ok(parsed.fecMechanisms.indexOf('ULPFEC') !== -1, | ||
'parsed ULPFEC as FEC mechanism'); | ||
t.ok(parsed.headerExtensions.length === 3, 'parsed 3 header extensions'); | ||
t.end(); | ||
describe('fmtp', () => { | ||
const line = 'a=fmtp:111 minptime=10; useinbandfec=1'; | ||
const parsed = SDPUtils.parseFmtp(line); | ||
describe('parsing', () => { | ||
it('parses 2 parameters', () => { | ||
expect(Object.keys(parsed).length).to.equal(2); | ||
}); | ||
it('parses minptime', () => { | ||
expect(parsed.minptime).to.equal('10'); | ||
}); | ||
it('parses useinbandfec', () => { | ||
expect(parsed.useinbandfec).to.equal('1'); | ||
}); | ||
}); | ||
describe('serialization', () => { | ||
it('uses preferredPayloadType', () => { | ||
let out = SDPUtils.writeFmtp({ | ||
preferredPayloadType: 111, | ||
parameters: {minptime: '10'} | ||
}).trim(); | ||
expect(out).to.equal('a=fmtp:111 minptime=10'); | ||
}); | ||
it('returns an empty string if there are no parameters', () => { | ||
let out = SDPUtils.writeFmtp({ | ||
preferredPayloadType: 111, | ||
parameters: {} | ||
}).trim(); | ||
expect(out).to.equal(''); | ||
}); | ||
// TODO: is this safe or can the order change? | ||
// serialization strings the extra whitespace after ';' | ||
it('does not add extra spaces between parameters', () => { | ||
let out = SDPUtils.writeFmtp({ | ||
payloadType: 111, | ||
parameters: parsed | ||
}).trim(); | ||
expect(out).to.equal(line.replace('; ', ';')); | ||
}); | ||
}); | ||
}); | ||
test('fmtp parsing and serialization', function(t) { | ||
var line = 'a=fmtp:111 minptime=10; useinbandfec=1'; | ||
var parsed = SDPUtils.parseFmtp(line); | ||
t.ok(Object.keys(parsed).length === 2, 'parsed 2 parameters'); | ||
t.ok(parsed.minptime === '10', 'parsed minptime'); | ||
t.ok(parsed.useinbandfec === '1', 'parsed useinbandfec'); | ||
describe('rtpmap', () => { | ||
const line = 'a=rtpmap:111 opus/48000/2'; | ||
const parsed = SDPUtils.parseRtpMap(line); | ||
describe('parsing', () => { | ||
it('parses codec name', () => { | ||
expect(parsed.name).to.equal('opus'); | ||
}); | ||
// TODO: is this safe or can the order change? | ||
// serialization strings the extra whitespace after ';' | ||
t.ok(SDPUtils.writeFmtp({payloadType: 111, parameters: parsed}) | ||
=== line.replace('; ', ';') + '\r\n', | ||
'serialization does not add extra spaces between parameters'); | ||
t.end(); | ||
it('parses payloadType as integer', () => { | ||
expect(parsed.payloadType).to.equal(111); | ||
}); | ||
it('parses clockRate as an integer', () => { | ||
expect(parsed.clockRate).to.equal(48000); | ||
}); | ||
it('parses numChannels as an integer', () => { | ||
expect(parsed.numChannels).to.equal(2); | ||
}); | ||
it('parses numChannels and defaults to 1 if not present', () => { | ||
expect(SDPUtils.parseRtpMap('a=rtpmap:0 PCMU/8000').numChannels) | ||
.to.equal(1); | ||
}); | ||
}); | ||
describe('serialization', () => { | ||
it('generates the expected output', () => { | ||
let out = SDPUtils.writeRtpMap({ | ||
payloadType: 111, | ||
name: 'opus', | ||
clockRate: 48000, | ||
numChannels: 2 | ||
}).trim(); | ||
expect(out).to.equal(line); | ||
}); | ||
it('uses preferredPayloadType', () => { | ||
let out = SDPUtils.writeRtpMap({ | ||
preferredPayloadType: 111, | ||
name: 'opus', | ||
clockRate: 48000, | ||
numChannels: 2 | ||
}).trim(); | ||
expect(out).to.equal(line); | ||
}); | ||
it('does not append numChannels when there is only one channel', () => { | ||
let out = SDPUtils.writeRtpMap({ | ||
payloadType: 0, | ||
name: 'pcmu', | ||
clockRate: 8000, | ||
numChannels: 1 | ||
}).trim(); | ||
expect(out).to.equal('a=rtpmap:0 pcmu/8000'); | ||
}); | ||
}); | ||
}); | ||
test('rtpmap parsing and serialization', function(t) { | ||
var line = 'a=rtpmap:111 opus/48000/2'; | ||
var parsed = SDPUtils.parseRtpMap(line); | ||
t.ok(parsed.name === 'opus', 'parsed codec name'); | ||
t.ok(parsed.payloadType === 111, 'parsed payloadType as integer'); | ||
t.ok(parsed.clockRate === 48000, 'parsed clockRate as integer'); | ||
t.ok(parsed.numChannels === 2, 'parsed numChannels'); | ||
describe('parseRtpEncodingParameters', () => { | ||
let sections = SDPUtils.splitSections(videoSDP); | ||
let data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
it('parses 8 encoding parameters for four codecs with fec', () => { | ||
expect(data.length).to.equal(8); | ||
}); | ||
parsed = SDPUtils.parseRtpMap('a=rtpmap:0 PCMU/8000'); | ||
t.ok(parsed.numChannels === 1, 'numChannels defaults to 1 if not present'); | ||
it('parses primary ssrc', () => { | ||
expect(data[0].ssrc).to.equal(1734522595); | ||
}); | ||
t.ok(SDPUtils.writeRtpMap({ | ||
payloadType: 111, | ||
name: 'opus', | ||
clockRate: 48000, | ||
numChannels: 2 | ||
}).trim() === line, 'serialized rtpmap'); | ||
it('parses RTX encoding and ssrc', () => { | ||
expect(data[0].rtx); | ||
expect(data[0].rtx.ssrc).to.equal(2715962409); | ||
}); | ||
t.end(); | ||
it('parses ssrc from cname as a fallback', () => { | ||
sections = SDPUtils.splitSections(videoSDP2); | ||
data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
expect(data.length).to.equal(1); | ||
expect(data[0].ssrc).to.equal(98927270); | ||
}); | ||
describe('bandwidth modifier', () => { | ||
it('of type AS is parsed', () => { | ||
sections = SDPUtils.splitSections( | ||
videoSDP.replace('c=IN IP4 0.0.0.0\r\n', | ||
'c=IN IP4 0.0.0.0\r\nb=AS:512\r\n') | ||
); | ||
data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
// conversion formula from jsep. | ||
expect(data[0].maxBitrate).to.equal(512 * 1000 * 0.95 - (50 * 40 * 8)) | ||
}); | ||
it('of type TIAS is parsed', () => { | ||
sections = SDPUtils.splitSections( | ||
videoSDP.replace('c=IN IP4 0.0.0.0\r\n', | ||
'c=IN IP4 0.0.0.0\r\nb=TIAS:512000\r\n') | ||
); | ||
data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
expect(data[0].maxBitrate).to.equal(512000); | ||
}); | ||
it('of unknown type is ignored', () => { | ||
sections = SDPUtils.splitSections( | ||
videoSDP.replace('c=IN IP4 0.0.0.0\r\n', | ||
'c=IN IP4 0.0.0.0\r\nb=something:1\r\n') | ||
); | ||
data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
expect(data[0].maxBitrate).to.equal(undefined); | ||
}); | ||
}); | ||
}); | ||
test('parseRtpEncodingParameters', function(t) { | ||
var sections = SDPUtils.splitSections(videoSDP); | ||
var data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
t.ok(data.length === 8, 'parsed encoding parameters for four codecs'); | ||
describe('rtcp feedback', () => { | ||
describe('serialization', () => { | ||
it('serializes', () => { | ||
const codec = { payloadType: 100, | ||
rtcpFeedback: [ | ||
{ type: 'nack', parameter: 'pli' }, | ||
{ type: 'nack' } | ||
] | ||
}; | ||
const expected = 'a=rtcp-fb:100 nack pli\r\n' + | ||
'a=rtcp-fb:100 nack\r\n'; | ||
expect(SDPUtils.writeRtcpFb(codec)).to.equal(expected); | ||
}); | ||
t.ok(data[0].ssrc === 1734522595, 'parsed primary SSRC'); | ||
t.ok(data[0].rtx, 'has RTX encoding'); | ||
t.ok(data[0].rtx.ssrc === 2715962409, 'parsed secondary SSRC for RTX'); | ||
t.end(); | ||
it('serialized preferredPayloadType', () => { | ||
const codec = { preferredPayloadType: 100, | ||
rtcpFeedback: [ | ||
{ type: 'nack' } | ||
] | ||
}; | ||
const expected = 'a=rtcp-fb:100 nack\r\n'; | ||
expect(SDPUtils.writeRtcpFb(codec)).to.equal(expected); | ||
}); | ||
it('does nothing if there is no rtcp feedback', () => { | ||
const codec = { payloadType: 100, | ||
rtcpFeedback: [] | ||
}; | ||
expect(SDPUtils.writeRtcpFb(codec)).to.equal(''); | ||
}); | ||
}) | ||
}); | ||
test('parseRtpEncodingParameters fallback', function(t) { | ||
var sections = SDPUtils.splitSections(videoSDP2); | ||
var data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
it('getKind', () => { | ||
const mediaSection = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n' + | ||
'c=IN IP4 0.0.0.0\r\na=sendrecv\r\n'; | ||
expect(SDPUtils.getKind(mediaSection)).to.equal('video'); | ||
}); | ||
t.ok(data.length === 1 && data[0].ssrc === 98927270, 'parsed single SSRC'); | ||
t.end(); | ||
describe('getDirection', () => { | ||
const mediaSection = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n' + | ||
'c=IN IP4 0.0.0.0\r\na=sendonly\r\n'; | ||
it('parses the direction from the mediaSection', () => { | ||
expect(SDPUtils.getDirection(mediaSection)).to.equal('sendonly'); | ||
}); | ||
it('falls back to sendrecv', () => { | ||
expect(SDPUtils.getDirection('')).to.equal('sendrecv'); | ||
}); | ||
}); | ||
test('parseRtpEncodingParameters with b=AS', function(t) { | ||
var sections = SDPUtils.splitSections( | ||
videoSDP.replace('c=IN IP4 0.0.0.0\r\n', | ||
'c=IN IP4 0.0.0.0\r\nb=AS:512\r\n') | ||
); | ||
var data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
describe('isRejected', () => { | ||
it('returns true if the m-lines port is 0', () => { | ||
const rej = 'm=video 0 UDP/TLS/RTP/SAVPF 120 126 97\r\n'; | ||
expect(SDPUtils.isRejected(rej)).to.equal(true); | ||
}); | ||
t.ok(data[0].maxBitrate === 512, 'parsed b=AS:512'); | ||
t.end(); | ||
it('returns false for a non-zero port', () => { | ||
const ok = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n'; | ||
expect(SDPUtils.isRejected(ok)).to.equal(false); | ||
}); | ||
}); | ||
test('parseRtpEncodingParameters with b=TIAS', function(t) { | ||
var sections = SDPUtils.splitSections( | ||
videoSDP.replace('c=IN IP4 0.0.0.0\r\n', | ||
'c=IN IP4 0.0.0.0\r\nb=TIAS:512\r\n') | ||
); | ||
var data = SDPUtils.parseRtpEncodingParameters(sections[1]); | ||
describe('parseMsid', () => { | ||
const spec = 'a=msid:stream track\r\n'; | ||
const planB = 'a=ssrc:1 msid:stream track\r\n'; | ||
t.ok(data[1].maxBitrate === 512, 'parsed b=TIAS:512'); | ||
t.end(); | ||
const parsed = SDPUtils.parseMsid(spec); | ||
it('returns undefined if no msid is found', () => { | ||
expect(SDPUtils.parseMsid('')).to.equal(undefined); | ||
}); | ||
it('returns an object if a msid is found', () => { | ||
expect(parsed).to.be.an('Object'); | ||
}); | ||
it('parses the stream id', () => { | ||
expect(parsed.stream).to.equal('stream'); | ||
}); | ||
it('parses the track id', () => { | ||
expect(parsed.track).to.equal('track'); | ||
}); | ||
it('parses legacy plan B stuff', () => { | ||
let legacy = SDPUtils.parseMsid(planB); | ||
expect(legacy).to.be.an('Object'); | ||
expect(legacy.stream).to.equal('stream'); | ||
expect(legacy.track).to.equal('track'); | ||
}); | ||
}); | ||
test('writeRtcpFb', function(t) { | ||
var codec = { payloadType: 100, | ||
rtcpFeedback: [ | ||
{ type: 'nack', parameter: 'pli' }, | ||
{ type: 'nack' } | ||
] | ||
}; | ||
var expected = 'a=rtcp-fb:100 nack pli\r\n' + | ||
'a=rtcp-fb:100 nack\r\n'; | ||
t.ok(SDPUtils.writeRtcpFb(codec) === expected, 'wrote rtcp-fb'); | ||
t.end(); | ||
describe('parseRtcpParameters', () => { | ||
const rtcp = SDPUtils.parseRtcpParameters(videoSDP); | ||
it('parses cname', () => { | ||
expect(rtcp.cname).to.equal('VrveQctHgkwqDKj6'); | ||
}); | ||
it('parses ssrc', () => { | ||
expect(rtcp.ssrc).to.equal(1734522595); | ||
}); | ||
it('parses reduced size', () => { | ||
expect(rtcp.reducedSize).to.equal(true); | ||
}); | ||
it('parses compoind', () => { | ||
expect(rtcp.compound).to.equal(false); | ||
}); | ||
it('parses mux', () => { | ||
expect(rtcp.mux).to.equal(true); | ||
}); | ||
}); | ||
test('getKind', function(t) { | ||
var mediaSection = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n' + | ||
'c=IN IP4 0.0.0.0\r\na=sendrecv\r\n'; | ||
t.ok(SDPUtils.getKind(mediaSection) === 'video', | ||
'extracted mediaSection kind'); | ||
t.end(); | ||
describe('parseFingerprint', () => { | ||
const res = SDPUtils.parseFingerprint('a=fingerprint:ALG fp'); | ||
it('parses and lowercaseѕ the algorithm', () => { | ||
expect(res.algorithm).to.equal('alg'); | ||
}); | ||
it('parses the fingerprint value', () => { | ||
expect(res.value).to.equal('fp'); | ||
}); | ||
}); | ||
test('isRejected', function(t) { | ||
var ok = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n'; | ||
var rej = 'm=video 0 UDP/TLS/RTP/SAVPF 120 126 97\r\n'; | ||
t.ok(SDPUtils.isRejected(ok) === false, 'port 9 is not rejected'); | ||
t.ok(SDPUtils.isRejected(rej) === true, 'port 0 is rejected'); | ||
t.end(); | ||
describe('getDtlsParameters', () => { | ||
const fp = 'a=fingerprint:sha-256 so:me:th:in:g1\r\n' + | ||
'a=fingerprint:SHA-1 somethingelse'; | ||
const dtlsParameters = SDPUtils.getDtlsParameters(fp, ''); | ||
it('sets the role to auto', () => { | ||
expect(dtlsParameters.role).to.equal('auto'); | ||
}) | ||
it('parses two fingerprints', () => { | ||
expect(dtlsParameters.fingerprints.length).to.equal(2); | ||
}); | ||
it('extracts the algorithm', () => { | ||
expect(dtlsParameters.fingerprints[0].algorithm).to.equal('sha-256'); | ||
expect(dtlsParameters.fingerprints[1].algorithm).to.equal('sha-1'); | ||
}); | ||
it('extracts the fingerprints', () => { | ||
expect(dtlsParameters.fingerprints[0].value).to.equal('so:me:th:in:g1'); | ||
expect(dtlsParameters.fingerprints[1].value).to.equal('somethingelse'); | ||
}); | ||
}); | ||
test('parseRtcpParameters', function(t) { | ||
var rtcp = SDPUtils.parseRtcpParameters(videoSDP); | ||
t.ok(rtcp.cname === 'VrveQctHgkwqDKj6', 'parsed RTCP cname'); | ||
t.ok(rtcp.ssrc === 1734522595, 'parsed RTCP ssrc'); | ||
t.ok(rtcp.reducedSize === true, 'parsed RTCP reducedSize'); | ||
t.ok(rtcp.compound === false, 'parsed RTCP compound'); | ||
t.ok(rtcp.mux === true, 'parse RTCP mux'); | ||
t.end(); | ||
describe('writeDtlsParameters', () => { | ||
const type = 'actpass'; | ||
const parameters = {fingerprints: [ | ||
{algorithm: 'sha-256', value: 'so:me:th:in:g1'}, | ||
{algorithm: 'SHA-1', value: 'somethingelse'} | ||
]}; | ||
const serialized = SDPUtils.writeDtlsParameters(parameters, type); | ||
it('serializes the fingerprints', () => { | ||
expect(serialized).to.contain('a=fingerprint:sha-256 so:me:th:in:g1'); | ||
expect(serialized).to.contain('a=fingerprint:SHA-1 somethingelse'); | ||
}); | ||
it('serializes the type', () => { | ||
expect(serialized).to.contain('a=setup:' + type); | ||
}); | ||
}); | ||
describe('getIceParameters', () => { | ||
const sections = SDPUtils.splitSections(videoSDP); | ||
const ice = SDPUtils.getIceParameters(sections[1], sections[0]); | ||
it('returns an object', () => { | ||
expect(ice).to.be.an('Object'); | ||
}); | ||
test('parseFingerprint', function(t) { | ||
var res = SDPUtils.parseFingerprint('a=fingerprint:ALG fp'); | ||
t.ok(res.algorithm === 'alg', 'algorithm is parsed and lowercased'); | ||
t.ok(res.value === 'fp', 'value is parsed'); | ||
t.end(); | ||
it('parses the ufrag', () => { | ||
expect(ice.usernameFragment).to.equal('npaLWmWDg3Yp6vJt'); | ||
}); | ||
it('parses the password', () => { | ||
expect(ice.password).to.equal('pdfQZAiFbcsFmUKWw55g4TD5'); | ||
}); | ||
}); | ||
test('getDtlsParameters', function(t) { | ||
var fp = 'a=fingerprint:sha-256 so:me:th:in:g1\r\na=fingerprint:SHA-1 somethingelse'; | ||
var dtlsParameters = SDPUtils.getDtlsParameters(fp, ''); | ||
t.ok(dtlsParameters.role === 'auto', 'set role to "auto"'); | ||
t.ok(dtlsParameters.fingerprints.length === 2, 'parsed two fingerprints'); | ||
t.ok(dtlsParameters.fingerprints[0].algorithm === 'sha-256', 'extracted algorithm'); | ||
t.ok(dtlsParameters.fingerprints[0].value === 'so:me:th:in:g1', 'extracted value'); | ||
t.ok(dtlsParameters.fingerprints[1].algorithm === 'sha-1', 'extracted second algorithm (lowercased)'); | ||
t.ok(dtlsParameters.fingerprints[1].value === 'somethingelse', 'extracted second value'); | ||
t.end(); | ||
describe('writeIceParameters', () => { | ||
const serialized= SDPUtils.writeIceParameters({ | ||
usernameFragment: 'foo', | ||
password: 'bar' | ||
}); | ||
it('serializes the usernameFragment', () => { | ||
expect(serialized).to.contain('a=ice-ufrag:foo'); | ||
}); | ||
it('serializes the password', () => { | ||
expect(serialized).to.contain('a=ice-pwd:bar'); | ||
}); | ||
}); | ||
test('getMid', function(t) { | ||
var mediaSection = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n' + | ||
describe('getMid', () => { | ||
const mediaSection = 'm=video 9 UDP/TLS/RTP/SAVPF 120 126 97\r\n' + | ||
'c=IN IP4 0.0.0.0\r\na=sendrecv\r\n'; | ||
t.ok(SDPUtils.getMid(mediaSection + 'a=mid:foo\r\n') === 'foo', 'returns the mid'); | ||
t.ok(SDPUtils.getMid(mediaSection) === undefined, 'returns undefined when no a=mid is present'); | ||
t.end(); | ||
it('returns undefined if no mid attribute is found', () => { | ||
expect(SDPUtils.getMid(mediaSection)).to.equal(undefined); | ||
}); | ||
it('returns the mid attribute', () => { | ||
expect(SDPUtils.getMid(mediaSection + 'a=mid:foo\r\n')).to.equal('foo'); | ||
}); | ||
}); | ||
test('parseIceOptions', function(t) { | ||
var result = SDPUtils.parseIceOptions('a=ice-options:trickle something'); | ||
t.ok(Array.isArray(result), 'returns an array of options'); | ||
t.ok(result.length === 2, 'returns two items for the given data'); | ||
t.ok(result[0] === 'trickle', 'first option equals "trickle"'); | ||
t.ok(result[1] === 'something', 'first option equals "something"'); | ||
t.end() | ||
describe('parseIceOptions', () => { | ||
const result = SDPUtils.parseIceOptions('a=ice-options:trickle something'); | ||
it('returns an array of options', () => { | ||
expect(result).to.be.an('Array'); | ||
expect(result.length).to.equal(2); | ||
expect(result[0]).to.equal('trickle'); | ||
expect(result[1]).to.equal('something'); | ||
}); | ||
}); | ||
test('parseExtmap', function(t) { | ||
var res = SDPUtils.parseExtmap('a=extmap:2 uri'); | ||
t.ok(res.id === 2, 'parses extmap id'); | ||
t.ok(res.uri === 'uri', 'parses extmap uri'); | ||
t.ok(res.direction === 'sendrecv', 'direction defaults to sendrecv'); | ||
describe('extmap', () => { | ||
describe('parseExtmap', () => { | ||
let res = SDPUtils.parseExtmap('a=extmap:2 uri'); | ||
res = SDPUtils.parseExtmap('a=extmap:2/sendonly uri'); | ||
t.ok(res.id === 2, 'parses extmap id when direction is present'); | ||
t.ok(res.direction === 'sendonly', 'parses extmap direction'); | ||
it('parses the extmap id', () => { | ||
expect(res.id).to.equal(2); | ||
}); | ||
t.end(); | ||
it('parses the extmap uri', () => { | ||
expect(res.uri).to.equal('uri'); | ||
}); | ||
it('parses the direction defaulting to sendrecv', () => { | ||
expect(res.direction).to.equal('sendrecv'); | ||
}); | ||
it('parses id and direction when direction is present', () => { | ||
res = SDPUtils.parseExtmap('a=extmap:2/sendonly uri'); | ||
expect(res.id === 2, 'parses extmap id when direction is present'); | ||
expect(res.direction === 'sendonly', 'parses extmap direction'); | ||
}); | ||
}); | ||
describe('writeExtmap', () => { | ||
it('writes extmap without direction', () => { | ||
expect(SDPUtils.writeExtmap({id: 1, uri: 'uri'})) | ||
.to.equal('a=extmap:1 uri\r\n'); | ||
}); | ||
it('writes extmap without direction when direction is sendrecv (default)', () => { | ||
expect(SDPUtils.writeExtmap({id: 1, uri: 'uri', direction: 'sendrecv'})) | ||
.to.equal('a=extmap:1 uri\r\n'); | ||
}); | ||
it('writes extmap with direction when direction is not sendrecv', () => { | ||
expect(SDPUtils.writeExtmap({id: 1, uri: 'uri', direction: 'sendonly'})) | ||
.to.equal('a=extmap:1/sendonly uri\r\n'); | ||
}); | ||
}); | ||
}); | ||
test('writeExtmap', function(t) { | ||
t.ok(SDPUtils.writeExtmap({id: 1, uri: 'uri'}) === 'a=extmap:1 uri\r\n', | ||
'writes extmap without direction'); | ||
t.ok(SDPUtils.writeExtmap({id: 1, uri: 'uri', direction: 'sendrecv'}) === 'a=extmap:1 uri\r\n', | ||
'writes extmap without direction when direction is sendrecv (default)'); | ||
t.ok(SDPUtils.writeExtmap({id: 1, uri: 'uri', direction: 'sendonly'}) === 'a=extmap:1/sendonly uri\r\n', | ||
'writes extmap with direction when direction is not sendrecv'); | ||
t.end(); | ||
describe('ice candidate', () => { | ||
describe('parsing', () => { | ||
const candidateString = 'candidate:702786350 2 udp 41819902 8.8.8.8 60769 ' + | ||
'typ relay raddr 8.8.8.8 rport 1234 ' + | ||
'tcptype active ' + | ||
'ufrag abc'; | ||
const candidate = SDPUtils.parseCandidate(candidateString); | ||
it('parses foundation', () => { | ||
expect(candidate.foundation).to.equal('702786350'); | ||
}); | ||
it('parses component', () => { | ||
expect(candidate.component).to.equal(2); | ||
}); | ||
it('parses priority', () => { | ||
expect(candidate.priority).to.equal(41819902, 'parses priority'); | ||
}); | ||
it('parses ip', () => { | ||
expect(candidate.ip).to.equal('8.8.8.8'); | ||
}); | ||
it('parses protocol', () => { | ||
expect(candidate.protocol).to.equal('udp'); | ||
}); | ||
it('parses port', () => { | ||
expect(candidate.port).to.equal(60769); | ||
}); | ||
it('parses type', () => { | ||
expect(candidate.type).to.equal('relay'); | ||
}); | ||
it('parses tcpType', () => { | ||
expect(candidate.tcpType).to.equal('active'); | ||
}); | ||
it('parses relatedAddress', () => { | ||
expect(candidate.relatedAddress).to.equal('8.8.8.8'); | ||
}); | ||
it('parses relatedPort', () => { | ||
expect(candidate.relatedPort).to.equal(1234); | ||
}); | ||
it('parses ufrag', () => { | ||
expect(candidate.ufrag).to.equal('abc'); | ||
}); | ||
it('parses the candidate with the legacy a= prefix', () => { | ||
expect(SDPUtils.parseCandidate('a=' + candidateString).foundation) | ||
.to.equal('702786350'); | ||
}); | ||
}); | ||
describe('serialization', () => { | ||
let serialized; | ||
let candidate; | ||
beforeEach(() => { | ||
candidate = { | ||
foundation: '702786350', | ||
component: 2, | ||
protocol: 'udp', | ||
priority: 4189902, | ||
ip: '8.8.8.8', | ||
port: 60769, | ||
type: 'host', | ||
}; | ||
}); | ||
it('serializes a candidate with everything', () => { | ||
candidate.protocol = 'tcp'; | ||
candidate.tcpType = 'active'; | ||
candidate.type = 'relay'; | ||
candidate.relatedAddress = '8.8.8.8'; | ||
candidate.relatedPort = 1234; | ||
serialized = SDPUtils.writeCandidate(candidate).trim(); | ||
expect(serialized).to.equal('candidate:702786350 2 TCP 4189902 8.8.8.8 ' + | ||
'60769 typ relay raddr 8.8.8.8 rport 1234 tcptype active'); | ||
}); | ||
it('adds ufrag if present', () => { | ||
candidate.ufrag = 'abc'; | ||
serialized = SDPUtils.writeCandidate(candidate).trim(); | ||
expect(serialized).to.equal('candidate:702786350 2 UDP 4189902 8.8.8.8 ' + | ||
'60769 typ host ufrag abc'); | ||
}); | ||
it('does not add relatedAddress and relatedPort for host candidates', () => { | ||
candidate.relatedAddress = '8.8.8.8'; | ||
candidate.relatedPort = 1234; | ||
serialized = SDPUtils.writeCandidate(candidate).trim(); | ||
expect(serialized).to.equal('candidate:702786350 2 UDP 4189902 8.8.8.8 ' + | ||
'60769 typ host'); | ||
}); | ||
it('ignores tcpType for udp candidates', () => { | ||
candidate.tcpType = 'active'; | ||
serialized = SDPUtils.writeCandidate(candidate).trim(); | ||
expect(serialized).to.equal('candidate:702786350 2 UDP 4189902 8.8.8.8 ' + | ||
'60769 typ host'); | ||
}); | ||
}); | ||
}); | ||
test('parseCandidate', function(t) { | ||
var candidateString = 'candidate:702786350 2 udp 41819902 8.8.8.8 60769 ' + | ||
'typ relay raddr 8.8.8.8 rport 1234 ' + | ||
'tcptype active ' + | ||
'ufrag abc'; | ||
var candidate = SDPUtils.parseCandidate(candidateString); | ||
describe('writeRtpDescription', () => { | ||
const kind = 'audio'; | ||
let parameters; | ||
beforeEach(() => { | ||
parameters = { | ||
codecs: [{ | ||
payloadType: 111, | ||
name: 'opus', | ||
clockRate: 48000, | ||
numChannels: 2, | ||
parameters: { | ||
minptime: '10' | ||
} | ||
}], | ||
headerExtensions: [{ | ||
id: 2, | ||
uri: 'urn:ietf:params:rtp-hdrext:toffset' | ||
}], | ||
}; | ||
}); | ||
t.ok(candidate.foundation === '702786350', 'parsed foundation'); | ||
t.ok(candidate.component === 2, 'parsed component'); | ||
t.ok(candidate.priority === 41819902, 'parsed priority'); | ||
t.ok(candidate.ip === '8.8.8.8', 'parsed ip'); | ||
t.ok(candidate.protocol === 'udp', 'parsed protocol'); | ||
t.ok(candidate.port === 60769, 'parsed port'); | ||
t.ok(candidate.tcpType === 'active', 'parsed tcpType'); | ||
t.ok(candidate.relatedAddress === '8.8.8.8', 'parsed relatedAddress'); | ||
t.ok(candidate.relatedPort === 1234, 'parsed relatedPort'); | ||
t.ok(candidate.ufrag === 'abc', 'parsed ufrag'); | ||
t.end(); | ||
it('generates a rejected m-line if the codecs are empty', () => { | ||
parameters.codecs = []; | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('m=' + kind + ' 0 '); | ||
}); | ||
it('generates rtpmap lines', () => { | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('a=rtpmap:111'); | ||
}); | ||
it('generates rtpmap lines using preferredPayloadType', () => { | ||
delete parameters.codecs[0].payloadType; | ||
parameters.codecs[0].preferredPayloadType = 111; | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('a=rtpmap:111'); | ||
}); | ||
it('generates fmtp lines for codecs with parameters', () => { | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('a=fmtp:'); | ||
}); | ||
it('does not generate fmtp lines for codecs with no parameters', () => { | ||
delete parameters.codecs[0].parameters; | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).not.to.contain('a=fmtp:'); | ||
}); | ||
it('generates rtcp-fb lines for codecs with rtcp-feedback', () => { | ||
parameters.codecs[0].rtcpFeedback = [{type: 'nack'}]; | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('a=rtcp-fb:'); | ||
}); | ||
it('does not generate rtcp-fb lines for codecs with no feedback', () => { | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).not.to.contain('a=rtcp-fb:'); | ||
}); | ||
it('generates extmap lines for headerExtensions', () => { | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).to.contain('a=extmap:2 urn:ietf:params:rtp-hdrext:toffset\r\n'); | ||
}); | ||
it('does not generate extmap lines if there are no headerExtensions', () => { | ||
parameters.headerExtensions = []; | ||
const serialized = SDPUtils.writeRtpDescription(kind, parameters); | ||
expect(serialized).not.to.contain('a=extmap:'); | ||
}); | ||
}); | ||
describe('writeBoilerPlate', () => { | ||
let sdp; | ||
beforeEach(() => { | ||
sdp = SDPUtils.writeSessionBoilerplate(); | ||
}); | ||
it('returns a string', () => { | ||
expect(sdp).to.be.a('String'); | ||
}); | ||
it('generates unique id', () => { | ||
expect(sdp).to.not.equal(SDPUtils.writeSessionBoilerplate()); | ||
}); | ||
it('uses passed in session id', () => { | ||
let sessionId = 'uniqueTestSessionId1'; | ||
let sdpWithSession = SDPUtils.writeSessionBoilerplate(sessionId); | ||
expect(sdpWithSession).to.include(' ' + sessionId + ' '); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
46577
9
1207
7