-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
153 lines (121 loc) · 3.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* jshint node: true */
'use strict';
var nub = require('whisk/nub');
var pluck = require('whisk/pluck');
var flatten = require('whisk/flatten');
var reLineBreak = /\r?\n/;
var reTrailingNewlines = /\r?\n$/;
// list sdp line types that are not "significant"
var nonHeaderLines = [ 'a', 'c', 'b', 'k' ];
var parsers = require('./parsers');
/**
# rtc-sdp
This is a utility module for intepreting and patching sdp.
## Usage
The `rtc-sdp` main module exposes a single function that is capable of
parsing lines of SDP, and providing an object allowing you to perform
operations on those parsed lines:
```js
var sdp = require('rtc-sdp')(lines);
```
The currently supported operations are listed below:
**/
module.exports = function(sdp) {
var ops = {};
var parsed = [];
var activeCollector;
// initialise the lines
var lines = sdp.split(reLineBreak).filter(Boolean).map(function(line) {
return line.split('=');
});
var inputOrder = nub(lines.filter(function(line) {
return line[0] && nonHeaderLines.indexOf(line[0]) < 0;
}).map(pluck(0)));
var findLine = ops.findLine = function(type, index) {
var lineData = parsed.filter(function(line) {
return line[0] === type;
})[index || 0];
return lineData && lineData[1];
};
// push into parsed sections
lines.forEach(function(line) {
var customParser = parsers[line[0]];
if (customParser) {
activeCollector = customParser(parsed, line);
}
else if (activeCollector) {
activeCollector = activeCollector(line);
}
else {
parsed.push(line);
}
});
/**
### `sdp.addIceCandidate(data)`
Modify the sdp to include candidates as denoted by the data.
**/
ops.addIceCandidate = function(data) {
var lineIndex = (data || {}).lineIndex || (data || {}).sdpMLineIndex;
var mLine = typeof lineIndex != 'undefined' && findLine('m', lineIndex);
var candidate = (data || {}).candidate;
// if we have the mLine add the new candidate
if (mLine && candidate) {
mLine.childlines.push(candidate.replace(reTrailingNewlines, '').split('='));
}
};
/**
### `sdp.getMediaTypes() => []`
Retrieve the list of media types that have been defined in the sdp via
`m=` lines.
**/
ops.getMediaTypes = function() {
function getMediaType(data) {
return data[1].def.split(/\s/)[0];
}
return parsed.filter(function(parts) {
return parts[0] === 'm' && parts[1] && parts[1].def;
}).map(getMediaType);
};
/**
### `sdp.getMediaIDs() => []`
Returns the list of unique media line IDs that have been defined in the sdp
via `a=mid:` lines.
**/
ops.getMediaIDs = function() {
return parsed.filter(function(parts) {
return parts[0] === 'm' && parts[1] && parts[1].childlines && parts[1].childlines.length > 0;
}).map(function(mediaLine) {
var lines = mediaLine[1].childlines;
// Default ID to the media type
var mediaId = mediaLine[1].def.split(/\s/)[0];
// Look for the media ID
for (var i = 0; i < lines.length; i++) {
var tokens = lines[i][1].split(':');
if (tokens.length > 0 && tokens[0] === 'mid') {
mediaId = tokens[1];
break;
}
}
return mediaId;
});
};
/**
### `sdp.toString()`
Convert the SDP structure that is currently retained in memory, into a string
that can be provided to a `setLocalDescription` (or `setRemoteDescription`)
WebRTC call.
**/
ops.toString = function() {
return parsed.map(function(line) {
return typeof line[1].toArray == 'function' ? line[1].toArray() : [ line ];
}).reduce(flatten).map(function(line) {
return line.join('=');
}).join('\r\n') + '\r\n';
};
/**
## SDP Filtering / Munging Functions
There are additional functions included in the module to assign with
performing "single-shot" SDP filtering (or munging) operations:
**/
return ops;
};