-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsdp.js
308 lines (279 loc) · 9.51 KB
/
sdp.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* globals RTCPeerConnection, RTCRtpTransceiver */
'use strict';
var flatMap = require('./').flatMap;
var guessBrowser = require('./').guessBrowser;
// NOTE(mmalavalli): We cache Chrome's sdpSemantics support in order to prevent
// instantiation of more than one RTCPeerConnection.
var isSdpSemanticsSupported = null;
/**
* Check if Chrome supports specifying sdpSemantics for an RTCPeerConnection.
* @return {boolean}
*/
function checkIfSdpSemanticsIsSupported() {
if (typeof isSdpSemanticsSupported === 'boolean') {
return isSdpSemanticsSupported;
}
if (typeof RTCPeerConnection === 'undefined') {
isSdpSemanticsSupported = false;
return isSdpSemanticsSupported;
}
try {
new RTCPeerConnection({ sdpSemantics: 'foo' });
isSdpSemanticsSupported = false;
} catch (e) {
isSdpSemanticsSupported = true;
}
return isSdpSemanticsSupported;
}
// NOTE(mmalavalli): We cache Chrome's SDP format in order to prevent
// instantiation of more than one RTCPeerConnection.
var chromeSdpFormat = null;
/**
* Get Chrome's default SDP format.
* @returns {'planb'|'unified'}
*/
function getChromeDefaultSdpFormat() {
if (!chromeSdpFormat) {
if (typeof RTCPeerConnection !== 'undefined'
&& 'addTransceiver' in RTCPeerConnection.prototype) {
try {
new RTCPeerConnection().addTransceiver('audio');
chromeSdpFormat = 'unified';
} catch (e) {
chromeSdpFormat = 'planb';
}
} else {
chromeSdpFormat = 'planb';
}
}
return chromeSdpFormat;
}
/**
* Get Chrome's SDP format.
* @param {'plan-b'|'unified-plan'} [sdpSemantics]
* @returns {'planb'|'unified'}
*/
function getChromeSdpFormat(sdpSemantics) {
if (!sdpSemantics || !checkIfSdpSemanticsIsSupported()) {
return getChromeDefaultSdpFormat();
}
return {
'plan-b': 'planb',
'unified-plan': 'unified'
}[sdpSemantics];
}
/**
* Get Safari's default SDP format.
* @returns {'planb'|'unified'}
*/
function getSafariSdpFormat() {
return typeof RTCRtpTransceiver !== 'undefined'
&& 'currentDirection' in RTCRtpTransceiver.prototype
? 'unified'
: 'planb';
}
/**
* Get the browser's default SDP format.
* @param {'plan-b'|'unified-plan'} [sdpSemantics]
* @returns {'planb'|'unified'}
*/
function getSdpFormat(sdpSemantics) {
return {
chrome: getChromeSdpFormat(sdpSemantics),
firefox: 'unified',
safari: getSafariSdpFormat()
}[guessBrowser()] || null;
}
/**
* Match a pattern across lines, returning the first capture group for any
* matches.
* @param {string} pattern
* @param {string} lines
* @returns {Set<string>} matches
*/
function getMatches(pattern, lines) {
var matches = lines.match(new RegExp(pattern, 'gm')) || [];
return matches.reduce(function(results, line) {
var match = line.match(new RegExp(pattern));
return match ? results.add(match[1]) : results;
}, new Set());
}
/**
* Get a Set of MediaStreamTrack IDs from an SDP.
* @param {string} pattern
* @param {string} sdp
* @returns {Set<string>}
*/
function getTrackIds(pattern, sdp) {
return getMatches(pattern, sdp);
}
/**
* Get a Set of MediaStreamTrack IDs from a Plan B SDP.
* @param {string} sdp - Plan B SDP
* @returns {Set<string>} trackIds
*/
function getPlanBTrackIds(sdp) {
return getTrackIds('^a=ssrc:[0-9]+ +msid:.+ +(.+) *$', sdp);
}
/**
* Get a Set of MediaStreamTrack IDs from a Unified Plan SDP.
* @param {string} sdp - Unified Plan SDP
* @returns {Set<string>} trackIds
*/
function getUnifiedPlanTrackIds(sdp) {
return getTrackIds('^a=msid:.+ +(.+) *$', sdp);
}
/**
* Get a Set of SSRCs for a MediaStreamTrack from a Plan B SDP.
* @param {string} sdp - Plan B SDP
* @param {string} trackId - MediaStreamTrack ID
* @returns {Set<string>}
*/
function getPlanBSSRCs(sdp, trackId) {
var pattern = '^a=ssrc:([0-9]+) +msid:[^ ]+ +' + trackId + ' *$';
return getMatches(pattern, sdp);
}
/**
* Get the m= sections of a particular kind and direction from an sdp.
* @param {string} sdp - sdp string
* @param {string} [kind] - Pattern for matching kind
* @param {string} [direction] - Pattern for matching direction
* @returns {Array<string>} mediaSections
*/
function getMediaSections(sdp, kind, direction) {
kind = kind || '.*';
direction = direction || '.*';
return sdp.split('\r\nm=').slice(1).map(function(mediaSection) {
return 'm=' + mediaSection;
}).filter(function(mediaSection) {
var kindPattern = new RegExp('m=' + kind, 'gm');
var directionPattern = new RegExp('a=' + direction, 'gm');
return kindPattern.test(mediaSection) && directionPattern.test(mediaSection);
});
}
/**
* Get the Set of SSRCs announced in a MediaSection.
* @param {string} mediaSection
* @returns {Array<string>} ssrcs
*/
function getMediaSectionSSRCs(mediaSection) {
return Array.from(getMatches('^a=ssrc:([0-9]+) +.*$', mediaSection));
}
/**
* Get a Set of SSRCs for a MediaStreamTrack from a Unified Plan SDP.
* @param {string} sdp - Unified Plan SDP
* @param {string} trackId - MediaStreamTrack ID
* @returns {Set<string>}
*/
function getUnifiedPlanSSRCs(sdp, trackId) {
var mediaSections = getMediaSections(sdp);
var msidAttrRegExp = new RegExp('^a=msid:[^ ]+ +' + trackId + ' *$', 'gm');
var matchingMediaSections = mediaSections.filter(function(mediaSection) {
return mediaSection.match(msidAttrRegExp);
});
return new Set(flatMap(matchingMediaSections, getMediaSectionSSRCs));
}
/**
* Get a Map from MediaStreamTrack IDs to SSRCs from an SDP.
* @param {function(string): Set<string>} getTrackIds
* @param {function(string, string): Set<string>} getSSRCs
* @param {string} sdp - SDP
* @returns {Map<string, Set<string>>} trackIdsToSSRCs
*/
function getTrackIdsToSSRCs(getTrackIds, getSSRCs, sdp) {
return new Map(Array.from(getTrackIds(sdp)).map(function(trackId) {
return [trackId, getSSRCs(sdp, trackId)];
}));
}
/**
* Get a Map from MediaStreamTrack IDs to SSRCs from a Plan B SDP.
* @param {string} sdp - Plan B SDP
* @returns {Map<string, Set<string>>} trackIdsToSSRCs
*/
function getPlanBTrackIdsToSSRCs(sdp) {
return getTrackIdsToSSRCs(getPlanBTrackIds, getPlanBSSRCs, sdp);
}
/**
* Get a Map from MediaStreamTrack IDs to SSRCs from a Plan B SDP.
* @param {string} sdp - Plan B SDP
* @returns {Map<string, Set<string>>} trackIdsToSSRCs
*/
function getUnifiedPlanTrackIdsToSSRCs(sdp) {
return getTrackIdsToSSRCs(getUnifiedPlanTrackIds, getUnifiedPlanSSRCs, sdp);
}
/**
* Update the mappings from MediaStreamTrack IDs to SSRCs as indicated by both
* the Map from MediaStreamTrack IDs to SSRCs and the SDP itself. This method
* ensures that SSRCs never change once announced.
* @param {function(string): Map<string, Set<string>>} getTrackIdsToSSRCs
* @param {Map<string, Set<string>>} trackIdsToSSRCs
* @param {string} sdp - SDP
* @returns {strinng} updatedSdp - updated SDP
*/
function updateTrackIdsToSSRCs(getTrackIdsToSSRCs, trackIdsToSSRCs, sdp) {
var newTrackIdsToSSRCs = getTrackIdsToSSRCs(sdp);
var newSSRCsToOldSSRCs = new Map();
// NOTE(mroberts): First, update a=ssrc attributes.
newTrackIdsToSSRCs.forEach(function(ssrcs, trackId) {
if (!trackIdsToSSRCs.has(trackId)) {
trackIdsToSSRCs.set(trackId, ssrcs);
return;
}
var oldSSRCs = Array.from(trackIdsToSSRCs.get(trackId));
var newSSRCs = Array.from(ssrcs);
oldSSRCs.forEach(function(oldSSRC, i) {
var newSSRC = newSSRCs[i];
newSSRCsToOldSSRCs.set(newSSRC, oldSSRC);
var pattern = '^a=ssrc:' + newSSRC + ' (.*)$';
var replacement = 'a=ssrc:' + oldSSRC + ' $1';
sdp = sdp.replace(new RegExp(pattern, 'gm'), replacement);
});
});
// NOTE(mroberts): Then, update a=ssrc-group attributes.
var pattern = '^(a=ssrc-group:[^ ]+ +)(.*)$';
var matches = sdp.match(new RegExp(pattern, 'gm')) || [];
matches.forEach(function(line) {
var match = line.match(new RegExp(pattern));
if (!match) {
return;
}
var prefix = match[1];
var newSSRCs = match[2];
var oldSSRCs = newSSRCs.split(' ').map(function(newSSRC) {
var oldSSRC = newSSRCsToOldSSRCs.get(newSSRC);
return oldSSRC ? oldSSRC : newSSRC;
}).join(' ');
sdp = sdp.replace(match[0], prefix + oldSSRCs);
});
return sdp;
}
/**
* Update the mappings from MediaStreamTrack IDs to SSRCs as indicated by both
* the Map from MediaStreamTrack IDs to SSRCs and the Plan B SDP itself. This
* method ensures that SSRCs never change once announced.
* @param {Map<string, Set<string>>} trackIdsToSSRCs
* @param {string} sdp - Plan B SDP
* @returns {string} updatedSdp - updated Plan B SDP
*/
function updatePlanBTrackIdsToSSRCs(trackIdsToSSRCs, sdp) {
return updateTrackIdsToSSRCs(getPlanBTrackIdsToSSRCs, trackIdsToSSRCs, sdp);
}
/**
* Update the mappings from MediaStreamTrack IDs to SSRCs as indicated by both
* the Map from MediaStreamTrack IDs to SSRCs and the Plan B SDP itself. This
* method ensures that SSRCs never change once announced.
* @param {Map<string, Set<string>>} trackIdsToSSRCs
* @param {string} sdp - Plan B SDP
* @returns {string} updatedSdp - updated Plan B SDP
*/
function updateUnifiedPlanTrackIdsToSSRCs(trackIdsToSSRCs, sdp) {
return updateTrackIdsToSSRCs(getUnifiedPlanTrackIdsToSSRCs, trackIdsToSSRCs, sdp);
}
exports.getSdpFormat = getSdpFormat;
exports.getMediaSections = getMediaSections;
exports.getPlanBTrackIds = getPlanBTrackIds;
exports.getUnifiedPlanTrackIds = getUnifiedPlanTrackIds;
exports.getPlanBSSRCs = getPlanBSSRCs;
exports.getUnifiedPlanSSRCs = getUnifiedPlanSSRCs;
exports.updatePlanBTrackIdsToSSRCs = updatePlanBTrackIdsToSSRCs;
exports.updateUnifiedPlanTrackIdsToSSRCs = updateUnifiedPlanTrackIdsToSSRCs;