blob: 0dd24726694387edfc7be20a2074ffac3ebc70ae [file] [log] [blame]
James Kuszmaul4a42b182021-01-17 11:32:46 -08001/*
2 * Copyright Philipp Hancke
3 * License: MIT
4 * Source: https://github.com/fippo/sdp
5 *
6 * Extended by Lennart Grahl
7 */
8
9/* eslint-env node */
10'use strict';
11
12// SDP helpers.
13var SDPUtils = {};
14
15// Generate an alphanumeric identifier for cname or mids.
16// TODO: use UUIDs instead? https://gist.github.com/jed/982883
17SDPUtils.generateIdentifier = function() {
18 return Math.random().toString(36).substr(2, 10);
19};
20
21// The RTCP CNAME used by all peerconnections from the same JS.
22SDPUtils.localCName = SDPUtils.generateIdentifier();
23
24// Splits SDP into lines, dealing with both CRLF and LF.
25SDPUtils.splitLines = function(blob) {
26 return blob.trim().split('\n').map(function(line) {
27 return line.trim();
28 });
29};
30// Splits SDP into sessionpart and mediasections. Ensures CRLF.
31SDPUtils.splitSections = function(blob) {
32 var parts = blob.split('\nm=');
33 return parts.map(function(part, index) {
34 return (index > 0 ? 'm=' + part : part).trim() + '\r\n';
35 });
36};
37
38// Returns lines that start with a certain prefix.
39SDPUtils.matchPrefix = function(blob, prefix) {
40 return SDPUtils.splitLines(blob).filter(function(line) {
41 return line.indexOf(prefix) === 0;
42 });
43};
44
45// Parses an ICE candidate line. Sample input:
46// candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8
47// rport 55996"
48SDPUtils.parseCandidate = function(line) {
49 var parts;
50 // Parse both variants.
51 if (line.indexOf('a=candidate:') === 0) {
52 parts = line.substring(12).split(' ');
53 } else {
54 parts = line.substring(10).split(' ');
55 }
56
57 var candidate = {
58 foundation: parts[0],
59 component: parts[1],
60 protocol: parts[2].toLowerCase(),
61 priority: parseInt(parts[3], 10),
62 ip: parts[4],
63 port: parseInt(parts[5], 10),
64 // skip parts[6] == 'typ'
65 type: parts[7]
66 };
67
68 for (var i = 8; i < parts.length; i += 2) {
69 switch (parts[i]) {
70 case 'raddr':
71 candidate.relatedAddress = parts[i + 1];
72 break;
73 case 'rport':
74 candidate.relatedPort = parseInt(parts[i + 1], 10);
75 break;
76 case 'tcptype':
77 candidate.tcpType = parts[i + 1];
78 break;
79 default: // Unknown extensions are silently ignored.
80 break;
81 }
82 }
83 return candidate;
84};
85
86// Translates a candidate object into SDP candidate attribute.
87SDPUtils.writeCandidate = function(candidate) {
88 var sdp = [];
89 sdp.push(candidate.foundation);
90 sdp.push(candidate.component);
91 sdp.push(candidate.protocol.toUpperCase());
92 sdp.push(candidate.priority);
93 sdp.push(candidate.ip);
94 sdp.push(candidate.port);
95
96 var type = candidate.type;
97 sdp.push('typ');
98 sdp.push(type);
99 if (type !== 'host' && candidate.relatedAddress &&
100 candidate.relatedPort) {
101 sdp.push('raddr');
102 sdp.push(candidate.relatedAddress); // was: relAddr
103 sdp.push('rport');
104 sdp.push(candidate.relatedPort); // was: relPort
105 }
106 if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') {
107 sdp.push('tcptype');
108 sdp.push(candidate.tcpType);
109 }
110 return 'candidate:' + sdp.join(' ');
111};
112
113// Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input:
114// a=rtpmap:111 opus/48000/2
115SDPUtils.parseRtpMap = function(line) {
116 var parts = line.substr(9).split(' ');
117 var parsed = {
118 payloadType: parseInt(parts.shift(), 10) // was: id
119 };
120
121 parts = parts[0].split('/');
122
123 parsed.name = parts[0];
124 parsed.clockRate = parseInt(parts[1], 10); // was: clockrate
125 // was: channels
126 parsed.numChannels = parts.length === 3 ? parseInt(parts[2], 10) : 1;
127 return parsed;
128};
129
130// Generate an a=rtpmap line from RTCRtpCodecCapability or
131// RTCRtpCodecParameters.
132SDPUtils.writeRtpMap = function(codec) {
133 var pt = codec.payloadType;
134 if (codec.preferredPayloadType !== undefined) {
135 pt = codec.preferredPayloadType;
136 }
137 return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate +
138 (codec.numChannels !== 1 ? '/' + codec.numChannels : '') + '\r\n';
139};
140
141// Parses an a=extmap line (headerextension from RFC 5285). Sample input:
142// a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
143SDPUtils.parseExtmap = function(line) {
144 var parts = line.substr(9).split(' ');
145 return {
146 id: parseInt(parts[0], 10),
147 uri: parts[1]
148 };
149};
150
151// Generates a=extmap line from RTCRtpHeaderExtensionParameters or
152// RTCRtpHeaderExtension.
153SDPUtils.writeExtmap = function(headerExtension) {
154 return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) +
155 ' ' + headerExtension.uri + '\r\n';
156};
157
158// Parses an ftmp line, returns dictionary. Sample input:
159// a=fmtp:96 vbr=on;cng=on
160// Also deals with vbr=on; cng=on
161SDPUtils.parseFmtp = function(line) {
162 var parsed = {};
163 var kv;
164 var parts = line.substr(line.indexOf(' ') + 1).split(';');
165 for (var j = 0; j < parts.length; j++) {
166 kv = parts[j].trim().split('=');
167 parsed[kv[0].trim()] = kv[1];
168 }
169 return parsed;
170};
171
172// Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters.
173SDPUtils.writeFmtp = function(codec) {
174 var line = '';
175 var pt = codec.payloadType;
176 if (codec.preferredPayloadType !== undefined) {
177 pt = codec.preferredPayloadType;
178 }
179 if (codec.parameters && Object.keys(codec.parameters).length) {
180 var params = [];
181 Object.keys(codec.parameters).forEach(function(param) {
182 params.push(param + '=' + codec.parameters[param]);
183 });
184 line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n';
185 }
186 return line;
187};
188
189// Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input:
190// a=rtcp-fb:98 nack rpsi
191SDPUtils.parseRtcpFb = function(line) {
192 var parts = line.substr(line.indexOf(' ') + 1).split(' ');
193 return {
194 type: parts.shift(),
195 parameter: parts.join(' ')
196 };
197};
198// Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters.
199SDPUtils.writeRtcpFb = function(codec) {
200 var lines = '';
201 var pt = codec.payloadType;
202 if (codec.preferredPayloadType !== undefined) {
203 pt = codec.preferredPayloadType;
204 }
205 if (codec.rtcpFeedback && codec.rtcpFeedback.length) {
206 // FIXME: special handling for trr-int?
207 codec.rtcpFeedback.forEach(function(fb) {
208 lines += 'a=rtcp-fb:' + pt + ' ' + fb.type +
209 (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') +
210 '\r\n';
211 });
212 }
213 return lines;
214};
215
216// Parses an RFC 5576 ssrc media attribute. Sample input:
217// a=ssrc:3735928559 cname:something
218SDPUtils.parseSsrcMedia = function(line) {
219 var sp = line.indexOf(' ');
220 var parts = {
221 ssrc: parseInt(line.substr(7, sp - 7), 10)
222 };
223 var colon = line.indexOf(':', sp);
224 if (colon > -1) {
225 parts.attribute = line.substr(sp + 1, colon - sp - 1);
226 parts.value = line.substr(colon + 1);
227 } else {
228 parts.attribute = line.substr(sp + 1);
229 }
230 return parts;
231};
232
233// Extracts SCTP capabilities from SDP media section or sessionpart.
234SDPUtils.getSctpCapabilities = function(mediaSection, sessionpart) {
235 var lines = SDPUtils.splitLines(mediaSection);
236 // Search in session part, too.
237 lines = lines.concat(SDPUtils.splitLines(sessionpart));
238 var maxMessageSize = lines.filter(function(line) {
239 return line.indexOf('a=max-message-size:') === 0;
240 });
241 // TODO: Use 65536 once Firefox has disabled PPID-based fragmentation
242 // see: https://lgrahl.de/articles/demystifying-webrtc-dc-size-limit.html
243 maxMessageSize = maxMessageSize.length ? parseInt(maxMessageSize[0].substr(19)) : 16384;
244 return {
245 maxMessageSize: maxMessageSize
246 };
247};
248
249// Serializes SCTP capabilities to SDP.
250SDPUtils.writeSctpCapabilities = function(capabilities) {
251 return 'a=max-message-size:' + capabilities.maxMessageSize + '\r\n'; // (03)
252};
253
254// Extracts SCTP port from SDP media section or sessionpart.
255SDPUtils.getSctpPort = function(mediaSection, sessionpart) {
256 var lines = SDPUtils.splitLines(mediaSection);
257 // Search in session part, too.
258 lines = lines.concat(SDPUtils.splitLines(sessionpart));
259 var port = lines.filter(function(line) {
260 return line.indexOf('a=sctp-port:') === 0;
261 });
262 port = port.length ? parseInt(port[0].substr(12), 10) : 5000;
263 return port;
264};
265
266// Serializes SCTP port to SDP.
267SDPUtils.writeSctpPort = function(port) {
268 // TODO: Enable (chromium can't cope with it)
269 // return 'a=sctp-port:' + (port ? port : 5000) + '\r\n'; // (03)
270 return '';
271};
272
273// Extracts DTLS parameters from SDP media section or sessionpart.
274// FIXME: for consistency with other functions this should only
275// get the fingerprint line as input. See also getIceParameters.
276SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) {
277 var lines = SDPUtils.splitLines(mediaSection);
278 // Search in session part, too.
279 lines = lines.concat(SDPUtils.splitLines(sessionpart));
280 var fpLine = lines.filter(function(line) {
281 return line.indexOf('a=fingerprint:') === 0;
282 })[0].substr(14);
283 // Note: a=setup line is ignored since we use the 'auto' role.
284 var dtlsParameters = {
285 role: 'auto',
286 fingerprints: [{
287 algorithm: fpLine.split(' ')[0],
288 value: fpLine.split(' ')[1]
289 }]
290 };
291 return dtlsParameters;
292};
293
294// Serializes DTLS parameters to SDP.
295SDPUtils.writeDtlsParameters = function(params, setupType) {
296 var sdp = 'a=setup:' + setupType + '\r\n';
297 params.fingerprints.forEach(function(fp) {
298 sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n';
299 });
300 return sdp;
301};
302// Parses ICE information from SDP media section or sessionpart.
303// FIXME: for consistency with other functions this should only
304// get the ice-ufrag and ice-pwd lines as input.
305SDPUtils.getIceParameters = function(mediaSection, sessionpart) {
306 var lines = SDPUtils.splitLines(mediaSection);
307 // Search in session part, too.
308 lines = lines.concat(SDPUtils.splitLines(sessionpart));
309 var iceParameters = {
310 usernameFragment: lines.filter(function(line) {
311 return line.indexOf('a=ice-ufrag:') === 0;
312 })[0].substr(12),
313 password: lines.filter(function(line) {
314 return line.indexOf('a=ice-pwd:') === 0;
315 })[0].substr(10)
316 };
317 return iceParameters;
318};
319
320// Serializes ICE parameters to SDP.
321SDPUtils.writeIceParameters = function(params) {
322 return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' +
323 'a=ice-pwd:' + params.password + '\r\n';
324};
325
326// Parses the SDP media section and returns RTCRtpParameters.
327SDPUtils.parseRtpParameters = function(mediaSection) {
328 var description = {
329 codecs: [],
330 headerExtensions: [],
331 fecMechanisms: [],
332 rtcp: []
333 };
334 var lines = SDPUtils.splitLines(mediaSection);
335 var mline = lines[0].split(' ');
336 for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..]
337 var pt = mline[i];
338 var rtpmapline = SDPUtils.matchPrefix(
339 mediaSection, 'a=rtpmap:' + pt + ' ')[0];
340 if (rtpmapline) {
341 var codec = SDPUtils.parseRtpMap(rtpmapline);
342 var fmtps = SDPUtils.matchPrefix(
343 mediaSection, 'a=fmtp:' + pt + ' ');
344 // Only the first a=fmtp:<pt> is considered.
345 codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {};
346 codec.rtcpFeedback = SDPUtils.matchPrefix(
347 mediaSection, 'a=rtcp-fb:' + pt + ' ')
348 .map(SDPUtils.parseRtcpFb);
349 description.codecs.push(codec);
350 // parse FEC mechanisms from rtpmap lines.
351 switch (codec.name.toUpperCase()) {
352 case 'RED':
353 case 'ULPFEC':
354 description.fecMechanisms.push(codec.name.toUpperCase());
355 break;
356 default: // only RED and ULPFEC are recognized as FEC mechanisms.
357 break;
358 }
359 }
360 }
361 SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) {
362 description.headerExtensions.push(SDPUtils.parseExtmap(line));
363 });
364 // FIXME: parse rtcp.
365 return description;
366};
367
368// Generates parts of the SDP media section describing the capabilities /
369// parameters.
370SDPUtils.writeRtpDescription = function(kind, caps) {
371 var sdp = '';
372
373 // Build the mline.
374 sdp += 'm=' + kind + ' ';
375 sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs.
376 sdp += ' UDP/TLS/RTP/SAVPF ';
377 sdp += caps.codecs.map(function(codec) {
378 if (codec.preferredPayloadType !== undefined) {
379 return codec.preferredPayloadType;
380 }
381 return codec.payloadType;
382 }).join(' ') + '\r\n';
383
384 sdp += 'c=IN IP4 0.0.0.0\r\n';
385 sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n';
386
387 // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb.
388 caps.codecs.forEach(function(codec) {
389 sdp += SDPUtils.writeRtpMap(codec);
390 sdp += SDPUtils.writeFmtp(codec);
391 sdp += SDPUtils.writeRtcpFb(codec);
392 });
393 var maxptime = 0;
394 caps.codecs.forEach(function(codec) {
395 if (codec.maxptime > maxptime) {
396 maxptime = codec.maxptime;
397 }
398 });
399 if (maxptime > 0) {
400 sdp += 'a=maxptime:' + maxptime + '\r\n';
401 }
402 sdp += 'a=rtcp-mux\r\n';
403
404 caps.headerExtensions.forEach(function(extension) {
405 sdp += SDPUtils.writeExtmap(extension);
406 });
407 // FIXME: write fecMechanisms.
408 return sdp;
409};
410
411// Parses the SDP media section and returns an array of
412// RTCRtpEncodingParameters.
413SDPUtils.parseRtpEncodingParameters = function(mediaSection) {
414 var encodingParameters = [];
415 var description = SDPUtils.parseRtpParameters(mediaSection);
416 var hasRed = description.fecMechanisms.indexOf('RED') !== -1;
417 var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1;
418
419 // filter a=ssrc:... cname:, ignore PlanB-msid
420 var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
421 .map(function(line) {
422 return SDPUtils.parseSsrcMedia(line);
423 })
424 .filter(function(parts) {
425 return parts.attribute === 'cname';
426 });
427 var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc;
428 var secondarySsrc;
429
430 var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID')
431 .map(function(line) {
432 var parts = line.split(' ');
433 parts.shift();
434 return parts.map(function(part) {
435 return parseInt(part, 10);
436 });
437 });
438 if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) {
439 secondarySsrc = flows[0][1];
440 }
441
442 description.codecs.forEach(function(codec) {
443 if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) {
444 var encParam = {
445 ssrc: primarySsrc,
446 codecPayloadType: parseInt(codec.parameters.apt, 10),
447 rtx: {
448 payloadType: codec.payloadType,
449 ssrc: secondarySsrc
450 }
451 };
452 encodingParameters.push(encParam);
453 if (hasRed) {
454 encParam = JSON.parse(JSON.stringify(encParam));
455 encParam.fec = {
456 ssrc: secondarySsrc,
457 mechanism: hasUlpfec ? 'red+ulpfec' : 'red'
458 };
459 encodingParameters.push(encParam);
460 }
461 }
462 });
463 if (encodingParameters.length === 0 && primarySsrc) {
464 encodingParameters.push({
465 ssrc: primarySsrc
466 });
467 }
468
469 // we support both b=AS and b=TIAS but interpret AS as TIAS.
470 var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b=');
471 if (bandwidth.length) {
472 if (bandwidth[0].indexOf('b=TIAS:') === 0) {
473 bandwidth = parseInt(bandwidth[0].substr(7), 10);
474 } else if (bandwidth[0].indexOf('b=AS:') === 0) {
475 bandwidth = parseInt(bandwidth[0].substr(5), 10);
476 }
477 encodingParameters.forEach(function(params) {
478 params.maxBitrate = bandwidth;
479 });
480 }
481 return encodingParameters;
482};
483
484SDPUtils.writeSessionBoilerplate = function() {
485 // FIXME: sess-id should be an NTP timestamp.
486 return 'v=0\r\n' +
487 'o=thisisadapterortc 8169639915646943137 2 IN IP4 127.0.0.1\r\n' +
488 's=-\r\n' +
489 't=0 0\r\n';
490};
491
492SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) {
493 var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps);
494
495 // Map ICE parameters (ufrag, pwd) to SDP.
496 sdp += SDPUtils.writeIceParameters(
497 transceiver.iceGatherer.getLocalParameters());
498
499 // Map DTLS parameters to SDP.
500 sdp += SDPUtils.writeDtlsParameters(
501 transceiver.dtlsTransport.getLocalParameters(),
502 type === 'offer' ? 'actpass' : 'active');
503
504 sdp += 'a=mid:' + transceiver.mid + '\r\n';
505
506 if (transceiver.rtpSender && transceiver.rtpReceiver) {
507 sdp += 'a=sendrecv\r\n';
508 } else if (transceiver.rtpSender) {
509 sdp += 'a=sendonly\r\n';
510 } else if (transceiver.rtpReceiver) {
511 sdp += 'a=recvonly\r\n';
512 } else {
513 sdp += 'a=inactive\r\n';
514 }
515
516 // FIXME: for RTX there might be multiple SSRCs. Not implemented in Edge yet.
517 if (transceiver.rtpSender) {
518 var msid = 'msid:' + stream.id + ' ' +
519 transceiver.rtpSender.track.id + '\r\n';
520 sdp += 'a=' + msid;
521 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
522 ' ' + msid;
523 }
524 // FIXME: this should be written by writeRtpDescription.
525 sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
526 ' cname:' + SDPUtils.localCName + '\r\n';
527 return sdp;
528};
529
530// Gets the direction from the mediaSection or the sessionpart.
531SDPUtils.getDirection = function(mediaSection, sessionpart) {
532 // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv.
533 var lines = SDPUtils.splitLines(mediaSection);
534 for (var i = 0; i < lines.length; i++) {
535 switch (lines[i]) {
536 case 'a=sendrecv':
537 case 'a=sendonly':
538 case 'a=recvonly':
539 case 'a=inactive':
540 return lines[i].substr(2);
541 default:
542 // FIXME: What should happen here?
543 }
544 }
545 if (sessionpart) {
546 return SDPUtils.getDirection(sessionpart);
547 }
548 return 'sendrecv';
549};