From 2014b60cade67222e6b499f7a04fc92a561a6749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Karl=20Roland=20W=C3=A4rting?= Date: Wed, 16 Mar 2016 09:56:48 +0100 Subject: [PATCH 1/3] Updated SDP to use es6 --- lib/sdp.js | 363 +++++++++++++++++++++++++++-------------------------- 1 file changed, 185 insertions(+), 178 deletions(-) diff --git a/lib/sdp.js b/lib/sdp.js index 624f87b..9e2cc2f 100644 --- a/lib/sdp.js +++ b/lib/sdp.js @@ -1,195 +1,202 @@ -var sdpTransform = require('sdp-transform'); -var uFragPass = require('./ice-ufrag-pwd'); -var BigNum = require('bignumber.js'); - - -function Candidate (candidate) { - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.1 - this.foundation = candidate.foundation; - this.component = 1; - this.transport = candidate.protocol.toUpperCase(); - this.priority = candidate.priority; - - if (candidate.type === 'host') { - this.ip = candidate.ip; - this.port = candidate.port; - this.type = 'host'; - } else if (candidate.type === 'srflx') { - this.ip = candidate.ip; - this.port = candidate.port; - this.type = 'srflx'; - this.raddr = candidate.relatedAddress; - this.rport = candidate.relatedPort; - } +"use strict" - // TODO: there's probably also a turn type and turn specific things - // and peer reflexive - - //console.log(candidate); -}; - -function Media (candidates) { - //this.rtp = []; - //this.fmtp = []; - this.type = 'application'; - this.protocol = 'DTLS/SCTP'; - this.payloads = 5000; - // https://tools.ietf.org/html/rfc5245#section-21.1.1 - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.1 - this.candidates = []; - this.direction = 'sendrecv'; - // We don't want to send this for trickle - //this.endOfCandidates = 'end-of-candidates'; - // https://tools.ietf.org/html/rfc5245#section-21.1.5 - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.5 - this.icePwd = uFragPass.password(); - // https://tools.ietf.org/html/rfc5245#section-21.1.6 - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.6 - // https://tools.ietf.org/html/draft-ietf-ice-rfc5245bis-00#appendix-B.4 - this.iceUfrag = uFragPass.ufrag(); - // https://tools.ietf.org/html/rfc5888#section-6 - // has something to do with SDP "grouping" ??? - //this.mid = ''; - // Note sure what this is for, yet? - this.invalid = [ - { - value: 'sctpmap:5000 webrtc-datachannel 256', - } - ]; - // Used in DTLS - // https://tools.ietf.org/html/rfc4572#section-6.2 - //this.setup = '';// 'actpass' ? - //this.ssrcs = [];// has an object { id: 0, attribute: '', value: '' } - - this.port = 0; - this.connection = { - version: 4, - ip: '0.0.0.0', - }; - - for (var i = 0, len = candidates.length; i < len; ++i) { - var candidate = candidates[i]; - this.candidates.push(new Candidate(candidate)); - if (i === 0) { +const sdpTransform = require('sdp-transform'); +const uFragPass = require('./ice-ufrag-pwd'); +const BigNum = require('bignumber.js'); + +// returns a string representing a random 62b Integer. +const random62bInt = (twoToSixtyFour => + () => twoToSixtyFour.mul(BigNum.random()).floor().toString() +)((new BigNum(2)).toPower(62)); + +class Candidate { + constructor(candidate) { + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.1 + this.foundation = candidate.foundation; + this.component = 1; + this.transport = candidate.protocol.toUpperCase(); + this.priority = candidate.priority; + + if (candidate.type === 'host') { + this.ip = candidate.ip; + this.port = candidate.port; + this.type = 'host'; + } else if (candidate.type === 'srflx') { + this.ip = candidate.ip; this.port = candidate.port; + this.type = 'srflx'; + this.raddr = candidate.relatedAddress; + this.rport = candidate.relatedPort; } + + // TODO: there's probably also a turn type and turn specific things + // and peer reflexive + + //console.log(candidate); } -}; +} + +class Media { + constructor(candidates) { + //this.rtp = []; + //this.fmtp = []; + this.type = 'application'; + this.protocol = 'DTLS/SCTP'; + this.payloads = 5000; + // https://tools.ietf.org/html/rfc5245#section-21.1.1 + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.1 + this.candidates = []; + this.direction = 'sendrecv'; + // We don't want to send this for trickle + //this.endOfCandidates = 'end-of-candidates'; + // https://tools.ietf.org/html/rfc5245#section-21.1.5 + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.5 + this.icePwd = uFragPass.password(); + // https://tools.ietf.org/html/rfc5245#section-21.1.6 + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.6 + // https://tools.ietf.org/html/draft-ietf-ice-rfc5245bis-00#appendix-B.4 + this.iceUfrag = uFragPass.ufrag(); + // https://tools.ietf.org/html/rfc5888#section-6 + // has something to do with SDP "grouping" ??? + //this.mid = ''; + // Note sure what this is for, yet? + this.invalid = [ + { + value: 'sctpmap:5000 webrtc-datachannel 256', + } + ]; + // Used in DTLS + // https://tools.ietf.org/html/rfc4572#section-6.2 + //this.setup = '';// 'actpass' ? + //this.ssrcs = [];// has an object { id: 0, attribute: '', value: '' } + + this.port = 0; + this.connection = { + version: 4, + ip: '0.0.0.0', + }; + + for (var i = 0, len = candidates.length; i < len; ++i) { + var candidate = candidates[i]; + this.candidates.push(new Candidate(candidate)); + if (i === 0) { + this.port = candidate.port; + } + } + } +} // takes a list of candidates, and can generate an SDP string. While numerous // RFCs and Specs talk about what should be included in offers, the info is // highly fragmented, and the best source of truth seems to be // https://rtcweb-wg.github.io/jsep/#rfc.section.5.2.1 . -function SDP (candidates, fingerprint) { - // In the form for sdp-transform to write to a string. These variables refer - // to the session, as opposed to individual pieces of media. - this.version = 0; - this.origin = { - username: 'node-rtc-peer-connection', +class SDP { + constructor(candidates, fingerprint) { + // In the form for sdp-transform to write to a string. These variables refer + // to the session, as opposed to individual pieces of media. + this.version = 0; + this.origin = { + username: 'node-rtc-peer-connection', + // https://rtcweb-wg.github.io/jsep/#rfc.section.5.2.1 + // https://tools.ietf.org/html/rfc3264#section-5 + sessionId: random62bInt(), + sessionVersion: 0, + netType: 'IN', + ipVer: 4, + address: '0.0.0.0', + }; + this.name = '-'; + this.timing = { + start: 0, + stop: 0, + }; + this.direction = 'sendrecv'; + // https://tools.ietf.org/html/rfc4572#section-5 + this.fingerprint = fingerprint; + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.6 + // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.8 + this.iceOptions = 'trickle'; + // Used in JSEP // https://rtcweb-wg.github.io/jsep/#rfc.section.5.2.1 - // https://tools.ietf.org/html/rfc3264#section-5 - sessionId: random62bInt(), - sessionVersion: 0, - netType: 'IN', - ipVer: 4, - address: '0.0.0.0', - }; - this.name = '-'; - this.timing = { - start: 0, - stop: 0, - }; - this.direction = 'sendrecv'; - // https://tools.ietf.org/html/rfc4572#section-5 - this.fingerprint = fingerprint; - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-9.6 - // https://tools.ietf.org/html/draft-ietf-mmusic-ice-sip-sdp-07#section-17.1.8 - this.iceOptions = 'trickle'; - // Used in JSEP - // https://rtcweb-wg.github.io/jsep/#rfc.section.5.2.1 - //this.msidSemantic = { - //semantic: 'WMS', - //token: '*', - //}; - this.media = [ - new Media(candidates), - ]; -}; - -SDP.prototype.getExternalAddr = function () { - return this.media[0].connection.ip; -}; - -SDP.prototype.setExternalAddr = function (addr) { - this.media[0].connection.ip = addr; -}; - -SDP.prototype.getExternalPort = function () { - return this.media[0].port; -}; - -SDP.prototype.setExternalPort = function (port) { - this.media[0].port = port; -}; - -SDP.prototype.getUsername = function () { - return this.media[0].iceUfrag; -}; - -SDP.prototype.setUsername = function (username) { - this.media[0].iceUfrag = username; -}; - -SDP.prototype.getPassword = function () { - return this.media[0].icePwd; -}; - -SDP.prototype.setPassword = function (password) { - this.media[0].icePwd = password; -}; - -SDP.prototype.toString = function () { - return sdpTransform.write(this); -}; - -SDP.fromString = function (str) { - return Object.assign(Object.create(SDP.prototype), sdpTransform.parse(str)); -}; - -SDP.candidateStr = function (candidate) { - // candidate should be a RTCIceCandidate - // TODO: enforce with instanceof or duck typing? - - // TODO: this is largely duplicated from sdp-transform/lib/grammar.js - var str = 'candidate:' + [ - candidate.foundation, - '1', // component - candidate.protocol, - candidate.priority, - candidate.ip, - candidate.port, - 'typ', - candidate.type - ].join(' '); - - if (candidate.type === 'srflx') { - str += ' raddr ' + candidate.relatedAddress + ' rport ' + - candidate.relatedPort; + //this.msidSemantic = { + //semantic: 'WMS', + //token: '*', + //}; + this.media = [ + new Media(candidates), + ]; } - return str; -}; + getExternalAddr () { + return this.media[0].connection.ip; + } + + getExternalAddr () { + // TODO + } + + setExternalAddr (addr) { + this.media[0].connection.ip = addr; + } + + getExternalPort () { + return this.media[0].port; + } + + setExternalPort (port) { + this.media[0].port = port; + } + + getUsername () { + return this.media[0].iceUfrag; + } + + setUsername (username) { + this.media[0].iceUfrag = username; + } + + getPassword () { + return this.media[0].icePwd; + } + + setPassword (password) { + this.media[0].icePwd = password; + } + + toString () { + return sdpTransform.write(this); + } + + fromString (str) { + return Object.assign(Object.create(SDP.prototype), sdpTransform.parse(str)); + } + + static candidateStr (candidate) { + // candidate should be a RTCIceCandidate + // TODO: enforce with instanceof or duck typing? + + // TODO: this is largely duplicated from sdp-transform/lib/grammar.js + var str = 'candidate:' + [ + candidate.foundation, + '1', // component + candidate.protocol, + candidate.priority, + candidate.ip, + candidate.port, + 'typ', + candidate.type + ].join(' '); + + if (candidate.type === 'srflx') { + str += ' raddr ' + candidate.relatedAddress + ' rport ' + + candidate.relatedPort; + } + + return str; + } +} -// returns a string representing a random 62b Integer. -var random62bInt = (function () { - var twoToSixtyFour = (new BigNum(2)).toPower(62); - return function () { - return twoToSixtyFour.mul(BigNum.random()).floor().toString(); - }; -})(); // Mozilla's SDP implementation: // https://dxr.mozilla.org/mozilla-central/source/media/webrtc/signaling/src/sdp - module.exports = SDP; - From 1dabbd83d7d1a2b5fffd5a18719149ec2068ad35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Karl=20Roland=20W=C3=A4rting?= Date: Wed, 16 Mar 2016 10:18:15 +0100 Subject: [PATCH 2/3] Update sdp.js --- lib/sdp.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/sdp.js b/lib/sdp.js index 9e2cc2f..2aa8879 100644 --- a/lib/sdp.js +++ b/lib/sdp.js @@ -131,10 +131,6 @@ class SDP { return this.media[0].connection.ip; } - getExternalAddr () { - // TODO - } - setExternalAddr (addr) { this.media[0].connection.ip = addr; } From bc0c870432ac3f09edf8cc080698e6f1e899d07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Karl=20Roland=20W=C3=A4rting?= Date: Wed, 16 Mar 2016 16:26:58 +0100 Subject: [PATCH 3/3] Added engines --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 7bd4a64..707b2fa 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "lint": "node_modules/.bin/jshint server lib || true", "style": "node_modules/.bin/jscs server lib || true" }, + "engines" : { + "node" : ">=5.0.0" + }, "dependencies": { "bignumber.js": "^2.1.4", "chalk": "^1.1.1",