Skip to content

Commit

Permalink
Merge pull request #3 from elbecita/get-candidates-from-sdp
Browse files Browse the repository at this point in the history
getCandidatesFromSDP added & gulp tasks deleted, moved to npm scripts
  • Loading branch information
marifehe committed Sep 4, 2018
2 parents badeaba + 07a2241 commit 5ec6eff
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 91 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
"strict": 0,
"comma-dangle": 0,
"spaced-comment": 0,
"no-underscore-dangle": 0
"no-underscore-dangle": 0,
"linebreak-style": 0
}
};
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ console.log('>>> My filtered SDP: ', filteredSdp);
*/
```

#### `getCandidatesFromSDP`: get an array of the ICE candidates present in an SDP file

```javascript
var IceBreaker = require('ice-breaker');

// Please notice this is just a section of an SDP file
var sdp = 'a=sendonly\r\n' +
'a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n' +
'a=candidate:2 1 TCP 1019217151 1111::222:3aff:1111:4983 9 typ host tcptype active\r\n';

var iceCandidates = IceBreaker.getCandidatesFromSDP(sdp);

console.log('>>> ICE Candidates in my SDP file: ', iceCandidates);
/* Should print:
>>> ICE Candidates in my SDP file: [ { foundation: '1',
componentId: '1',
transport: 'UDP',
priority: '2013266431',
connectionAddress: '1111::222:3aff:1111:4983',
port: '50791',
candidateType: 'host' },
{ foundation: '2',
componentId: '1',
transport: 'TCP',
priority: '1019217151',
connectionAddress: '1111::222:3aff:1111:4983',
port: '9',
candidateType: 'host' } ]
*/
```

---
Thank you for using this module! Feel free to contribute :)

License
Expand Down
29 changes: 0 additions & 29 deletions gulp/index.js

This file was deleted.

17 changes: 0 additions & 17 deletions gulp/tasks/lint.js

This file was deleted.

3 changes: 0 additions & 3 deletions gulpfile.js

This file was deleted.

9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "ice-breaker",
"version": "0.0.7",
"description": "Parse WebRTC ICE Candidates string",
"version": "1.0.0",
"description": "Set of helper methods to ease the WebRTC Media connection process.",
"main": "lib/ice-breaker.js",
"repository": {
"type": "git",
"url": "ssh://git@github.com:elbecita/ice-breaker.git"
},
"scripts": {
"lint": "eslint **/*.js",
"test": "nyc mocha --reporter spec",
"compile": "babel --presets env -d lib/ src/",
"prepublish": "npm run compile"
Expand Down Expand Up @@ -39,10 +40,6 @@
"eslint-plugin-promise": "^3.5.0",
"eslint-plugin-react": "^7.0.1",
"eslint-plugin-standard": "^3.0.1",
"gulp": "^3.9.1",
"gulp-eslint": "^3.0.1",
"gulp-help": "^1.6.1",
"gulp-if": "^2.0.2",
"mocha": "^3.4.2",
"nyc": "^11.0.2",
"sinon": "^4.5.0",
Expand Down
51 changes: 31 additions & 20 deletions src/ice-breaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function _validateSDPTransportFilter(_filter) {
}
if (Array.isArray(filter)) {
const validTransports = Object.keys(Constants.transport)
.map((k) => (Constants.transport[k]));
const isValid = filter.every((f) => (validTransports.indexOf(f) > -1));
.map(k => (Constants.transport[k]));
const isValid = filter.every(f => (validTransports.indexOf(f) > -1));
return isValid ? filter : false;
}
return false;
Expand All @@ -28,18 +28,6 @@ function _getTransportFilterRegexp(filter) {
}

class IceBreaker {

/**
* @deprecated Use candidateToJson instead.
* Parses the received ICE candidate string into a JSON object
* @param {string} iceCandidate
* @returns {object} ICE candidate parsed
*/
static toJson(iceCandidate) {
console.warn('ice-breaker WARN `toJson` method is deprecated since version 0.0.6. Use `candidateToJson` instead.');
return this.candidateToJson(iceCandidate);
}

/**
* Parses the received ICE candidate string into a JSON object
* @param {string} iceCandidate
Expand Down Expand Up @@ -86,29 +74,52 @@ class IceBreaker {
/**
* Returns a new SDP containing only the ICE candidates that match the
* transport protocol provided in the filter.
* - The SDP must be a string, with every field in a new line. (see RFC-4566 -
* - The SDP must be a string, with every field in a new line. (See RFC-4566 -
* https://tools.ietf.org/html/rfc4566 for SDP details).
* - The SDP will remain the same if it is not a string or no filter
* is provided.
*
* @param {string} SDP to filter, with every field in a new line
* @param {string|Array} filter IceBreaker.transport (UDP|TCP)
* @returns {string} SDP with only the filtered ICE candidates
*/
static filterSDPCandidatesByTransport(sdp, _filter) {
let filteredSdp = sdp;
const filter = _validateSDPTransportFilter(_filter);
if (typeof sdp === 'string' && filter) {
let transportRegex = _getTransportFilterRegexp(filter);
const transportRegex = _getTransportFilterRegexp(filter);
const sdpLines = sdp.split('\n');
const filteredSdpLines = sdpLines.filter((l) => (
l.indexOf('a=candidate') > -1 &&
l.search(transportRegex) > -1 ||
l.indexOf('a=candidate') < 0
const filteredSdpLines = sdpLines.filter(l => (
(l.indexOf('a=candidate') > -1 && l.search(transportRegex) > -1)
|| l.indexOf('a=candidate') < 0
));
filteredSdp = filteredSdpLines.join('\n');
}
return filteredSdp;
}

/**
* Returns an array of the ICE candidates present in the provided sdp.
* - The SDP must be a string, with every field in a new line. (See RFC-4566 -
* https://tools.ietf.org/html/rfc4566 for SDP details).
*
* @param {string} SDP, with every field in a new line
* @returns {Array} ICE candidates present in the provided sdp (returned as objects)
*/
static getCandidatesFromSDP(sdp) {
let iceCandidates = [];
if (typeof sdp === 'string') {
const sdpLines = sdp.split('\n');
const iceCandidatesLines = sdpLines.filter(l => (l.indexOf('a=candidate') > -1));
iceCandidates = iceCandidatesLines.map((l) => {
// remove 'a='
const candidate = l.substr(2);
return IceBreaker.candidateToJson(candidate);
});
}

return iceCandidates;
}
}

IceBreaker.transport = Constants.transport;
Expand Down
50 changes: 35 additions & 15 deletions test/ice-breaker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ describe('IceBreaker', () => {
sinonSandbox.restore();
});

describe('toJson()', () => {
it('should call candidateToJson', () => {
const candidateToJsonStub = sinonSandbox.stub(IceBreaker, 'candidateToJson').callsFake(() => {});
IceBreaker.toJson(1)
expect(candidateToJsonStub).to.have.been.calledWith(1);
});
});

describe('candidateToJson()', () => {
it('should return null if no ice candidate is received', () => {
expect(IceBreaker.candidateToJson()).to.be.null;
Expand Down Expand Up @@ -131,10 +123,10 @@ describe('IceBreaker', () => {
'a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n' +
'a=candidate:2 1 TCP 1019217151 1111::222:3aff:1111:4983 9 typ host tcptype active\r\n';
const validFilter = IceBreaker.transport.TCP;

// Act
const filteredSdp = IceBreaker.filterSDPCandidatesByTransport(sdp, validFilter);

// Assert
expect(filteredSdp).to.contain('a=sendonly');
expect(filteredSdp).to.not.contain('a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n');
Expand All @@ -147,10 +139,10 @@ describe('IceBreaker', () => {
'a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n' +
'a=candidate:2 1 TCP 1019217151 1111::222:3aff:1111:4983 9 typ host tcptype active\r\n';
const validFilter = IceBreaker.transport.UDP;

// Act
const filteredSdp = IceBreaker.filterSDPCandidatesByTransport(sdp, validFilter);

// Assert
expect(filteredSdp).to.contain('a=sendonly\r\n');
expect(filteredSdp).to.contain('a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n');
Expand All @@ -163,12 +155,40 @@ describe('IceBreaker', () => {
'a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n' +
'a=candidate:2 1 TCP 1019217151 1111::222:3aff:1111:4983 9 typ host tcptype active\r\n';
const validFilter = [IceBreaker.transport.UDP, IceBreaker.transport.TCP];

// Act
const filteredSdp = IceBreaker.filterSDPCandidatesByTransport(sdp, validFilter);

// Assert
expect(filteredSdp).to.equal(sdp);
});
})
});

describe('getCandidatesFromSDP()', () => {
it('should return an array with all the ICE candidates present in the provided SDP', () => {
// Arrange
const sdp = 'a=sendonly\r\n' +
'a=candidate:1 1 UDP 2013266431 1111::222:3aff:1111:4983 50791 typ host\r\n' +
'a=candidate:2 1 TCP 1019217151 1111::222:3aff:1111:4983 9 typ host tcptype active\r\n';

// Act
const iceCandidates = IceBreaker.getCandidatesFromSDP(sdp);

// Assert
expect(iceCandidates.length).to.equal(2);
expect(iceCandidates[0].transport).to.equal('UDP');
expect(iceCandidates[1].transport).to.equal('TCP');
});

it('should return an empty array if the sdp received is not a string', () => {
// Arrange
const sdp = { id: 'not a string' };

// Act
const iceCandidates = IceBreaker.getCandidatesFromSDP(sdp);

// Assert
expect(iceCandidates).to.be.empty;
});
});
});

0 comments on commit 5ec6eff

Please sign in to comment.