You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SDP.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /* global $ */
  2. import clonedeep from 'lodash.clonedeep';
  3. import transform from 'sdp-transform';
  4. import { MediaDirection } from '../../service/RTC/MediaDirection';
  5. import browser from '../browser';
  6. import FeatureFlags from '../flags/FeatureFlags';
  7. import SDPUtil from './SDPUtil';
  8. /**
  9. *
  10. * @param sdp
  11. */
  12. export default function SDP(sdp) {
  13. const media = sdp.split('\r\nm=');
  14. for (let i = 1, length = media.length; i < length; i++) {
  15. let mediaI = `m=${media[i]}`;
  16. if (i !== length - 1) {
  17. mediaI += '\r\n';
  18. }
  19. media[i] = mediaI;
  20. }
  21. const session = `${media.shift()}\r\n`;
  22. this.media = media;
  23. this.raw = session + media.join('');
  24. this.session = session;
  25. }
  26. /**
  27. * A flag will make {@link transportToJingle} and {@link jingle2media} replace
  28. * ICE candidates IPs with invalid value of '1.1.1.1' which will cause ICE
  29. * failure. The flag is used in the automated testing.
  30. * @type {boolean}
  31. */
  32. SDP.prototype.failICE = false;
  33. /**
  34. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  35. * @type {boolean}
  36. */
  37. SDP.prototype.removeTcpCandidates = false;
  38. /**
  39. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  40. * @type {boolean}
  41. */
  42. SDP.prototype.removeUdpCandidates = false;
  43. /**
  44. * Adds a new m-line to the description so that a new local source can then be attached to the transceiver that gets
  45. * added after a reneogtiation cycle.
  46. *
  47. * @param {Mediatype} mediaType media type of the new source that is being added.
  48. */
  49. SDP.prototype.addMlineForNewLocalSource = function(mediaType) {
  50. const mid = this.media.length;
  51. const sdp = transform.parse(this.raw);
  52. const mline = clonedeep(sdp.media.find(m => m.type === mediaType));
  53. // Edit media direction, mid and remove the existing ssrc lines in the m-line.
  54. mline.mid = mid;
  55. mline.direction = MediaDirection.RECVONLY;
  56. // Remove the ssrcs and source groups.
  57. mline.msid = undefined;
  58. mline.ssrcs = undefined;
  59. mline.ssrcGroups = undefined;
  60. sdp.media = sdp.media.concat(mline);
  61. // We regenerate the BUNDLE group (since we added a new m-line)
  62. sdp.groups.forEach(group => {
  63. if (group.type === 'BUNDLE') {
  64. const mids = group.mids.split(' ');
  65. mids.push(mid);
  66. group.mids = mids.join(' ');
  67. }
  68. });
  69. this.raw = transform.write(sdp);
  70. };
  71. /**
  72. * Returns map of MediaChannel mapped per channel idx.
  73. */
  74. SDP.prototype.getMediaSsrcMap = function() {
  75. const mediaSSRCs = {};
  76. for (let mediaindex = 0; mediaindex < this.media.length; mediaindex++) {
  77. const mid
  78. = SDPUtil.parseMID(
  79. SDPUtil.findLine(this.media[mediaindex], 'a=mid:'));
  80. const media = {
  81. mediaindex,
  82. mid,
  83. ssrcs: {},
  84. ssrcGroups: []
  85. };
  86. mediaSSRCs[mediaindex] = media;
  87. SDPUtil.findLines(this.media[mediaindex], 'a=ssrc:').forEach(line => {
  88. const linessrc = line.substring(7).split(' ')[0];
  89. // allocate new ChannelSsrc
  90. if (!media.ssrcs[linessrc]) {
  91. media.ssrcs[linessrc] = {
  92. ssrc: linessrc,
  93. lines: []
  94. };
  95. }
  96. media.ssrcs[linessrc].lines.push(line);
  97. });
  98. SDPUtil.findLines(this.media[mediaindex], 'a=ssrc-group:').forEach(line => {
  99. const idx = line.indexOf(' ');
  100. const semantics = line.substr(0, idx).substr(13);
  101. const ssrcs = line.substr(14 + semantics.length).split(' ');
  102. if (ssrcs.length) {
  103. media.ssrcGroups.push({
  104. semantics,
  105. ssrcs
  106. });
  107. }
  108. });
  109. }
  110. return mediaSSRCs;
  111. };
  112. /**
  113. * Returns <tt>true</tt> if this SDP contains given SSRC.
  114. * @param ssrc the ssrc to check.
  115. * @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
  116. */
  117. SDP.prototype.containsSSRC = function(ssrc) {
  118. // FIXME this code is really strange - improve it if you can
  119. const medias = this.getMediaSsrcMap();
  120. let result = false;
  121. Object.keys(medias).forEach(mediaindex => {
  122. if (result) {
  123. return;
  124. }
  125. if (medias[mediaindex].ssrcs[ssrc]) {
  126. result = true;
  127. }
  128. });
  129. return result;
  130. };
  131. // add content's to a jingle element
  132. SDP.prototype.toJingle = function(elem, thecreator) {
  133. // https://xmpp.org/extensions/xep-0338.html
  134. SDPUtil.findLines(this.session, 'a=group:').forEach(line => {
  135. const parts = line.split(' ');
  136. const semantics = parts.shift().substr(8);
  137. elem.c('group', { xmlns: 'urn:xmpp:jingle:apps:grouping:0',
  138. semantics });
  139. for (let j = 0; j < parts.length; j++) {
  140. elem.c('content', { name: parts[j] }).up();
  141. }
  142. elem.up();
  143. });
  144. for (let i = 0; i < this.media.length; i++) {
  145. const mline = SDPUtil.parseMLine(this.media[i].split('\r\n')[0]);
  146. if (!(mline.media === 'audio'
  147. || mline.media === 'video'
  148. || mline.media === 'application')) {
  149. continue; // eslint-disable-line no-continue
  150. }
  151. let ssrc;
  152. const assrcline = SDPUtil.findLine(this.media[i], 'a=ssrc:');
  153. if (assrcline) {
  154. ssrc = assrcline.substring(7).split(' ')[0]; // take the first
  155. } else {
  156. ssrc = false;
  157. }
  158. elem.c('content', { creator: thecreator,
  159. name: mline.media });
  160. const amidline = SDPUtil.findLine(this.media[i], 'a=mid:');
  161. if (amidline) {
  162. // prefer identifier from a=mid if present
  163. const mid = SDPUtil.parseMID(amidline);
  164. elem.attrs({ name: mid });
  165. }
  166. if (mline.media === 'audio' || mline.media === 'video') {
  167. elem.c('description',
  168. { xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  169. media: mline.media });
  170. if (ssrc) {
  171. elem.attrs({ ssrc });
  172. }
  173. for (let j = 0; j < mline.fmt.length; j++) {
  174. const rtpmap
  175. = SDPUtil.findLine(
  176. this.media[i],
  177. `a=rtpmap:${mline.fmt[j]}`);
  178. elem.c('payload-type', SDPUtil.parseRTPMap(rtpmap));
  179. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo
  180. // value=bar/>
  181. const afmtpline
  182. = SDPUtil.findLine(
  183. this.media[i],
  184. `a=fmtp:${mline.fmt[j]}`);
  185. if (afmtpline) {
  186. const fmtpParameters = SDPUtil.parseFmtp(afmtpline);
  187. // eslint-disable-next-line max-depth
  188. for (let k = 0; k < fmtpParameters.length; k++) {
  189. elem.c('parameter', fmtpParameters[k]).up();
  190. }
  191. }
  192. // XEP-0293 -- map a=rtcp-fb
  193. this.rtcpFbToJingle(i, elem, mline.fmt[j]);
  194. elem.up();
  195. }
  196. if (ssrc) {
  197. const ssrcMap = SDPUtil.parseSSRC(this.media[i]);
  198. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  199. const sourceName = SDPUtil.parseSourceNameLine(ssrcParameters);
  200. elem.c('source', {
  201. ssrc: availableSsrc,
  202. name: FeatureFlags.isSourceNameSignalingEnabled() ? sourceName : undefined,
  203. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  204. });
  205. const msid = SDPUtil.parseMSIDAttribute(ssrcParameters);
  206. // eslint-disable-next-line max-depth
  207. if (msid) {
  208. elem.c('parameter');
  209. elem.attrs({ name: 'msid' });
  210. elem.attrs({ value: msid });
  211. elem.up();
  212. }
  213. elem.up();
  214. }
  215. // XEP-0339 handle ssrc-group attributes
  216. const ssrcGroupLines
  217. = SDPUtil.findLines(this.media[i], 'a=ssrc-group:');
  218. ssrcGroupLines.forEach(line => {
  219. const idx = line.indexOf(' ');
  220. const semantics = line.substr(0, idx).substr(13);
  221. const ssrcs = line.substr(14 + semantics.length).split(' ');
  222. if (ssrcs.length) {
  223. elem.c('ssrc-group', { semantics,
  224. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  225. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  226. elem.up();
  227. }
  228. });
  229. }
  230. const ridLines = SDPUtil.findLines(this.media[i], 'a=rid:');
  231. if (ridLines.length && browser.usesRidsForSimulcast()) {
  232. // Map a line which looks like "a=rid:2 send" to just
  233. // the rid ("2")
  234. const rids = ridLines
  235. .map(ridLine => ridLine.split(':')[1])
  236. .map(ridInfo => ridInfo.split(' ')[0]);
  237. rids.forEach(rid => {
  238. elem.c('source', {
  239. rid,
  240. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  241. });
  242. elem.up();
  243. });
  244. const unifiedSimulcast
  245. = SDPUtil.findLine(this.media[i], 'a=simulcast:');
  246. if (unifiedSimulcast) {
  247. elem.c('rid-group', {
  248. semantics: 'SIM',
  249. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  250. });
  251. rids.forEach(rid => {
  252. elem.c('source', { rid }).up();
  253. });
  254. elem.up();
  255. }
  256. }
  257. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  258. elem.c('rtcp-mux').up();
  259. }
  260. // XEP-0293 -- map a=rtcp-fb:*
  261. this.rtcpFbToJingle(i, elem, '*');
  262. // XEP-0294
  263. const extmapLines = SDPUtil.findLines(this.media[i], 'a=extmap:');
  264. for (let j = 0; j < extmapLines.length; j++) {
  265. const extmap = SDPUtil.parseExtmap(extmapLines[j]);
  266. elem.c('rtp-hdrext', {
  267. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  268. uri: extmap.uri,
  269. id: extmap.value
  270. });
  271. // eslint-disable-next-line max-depth
  272. if (extmap.hasOwnProperty('direction')) {
  273. // eslint-disable-next-line max-depth
  274. switch (extmap.direction) {
  275. case MediaDirection.SENDONLY:
  276. elem.attrs({ senders: 'responder' });
  277. break;
  278. case MediaDirection.RECVONLY:
  279. elem.attrs({ senders: 'initiator' });
  280. break;
  281. case MediaDirection.SENDRECV:
  282. elem.attrs({ senders: 'both' });
  283. break;
  284. case MediaDirection.INACTIVE:
  285. elem.attrs({ senders: 'none' });
  286. break;
  287. }
  288. }
  289. // TODO: handle params
  290. elem.up();
  291. }
  292. elem.up(); // end of description
  293. }
  294. // map ice-ufrag/pwd, dtls fingerprint, candidates
  295. this.transportToJingle(i, elem);
  296. const m = this.media[i];
  297. if (SDPUtil.findLine(m, `a=${MediaDirection.SENDRECV}`, this.session)) {
  298. elem.attrs({ senders: 'both' });
  299. } else if (SDPUtil.findLine(m, `a=${MediaDirection.SENDONLY}`, this.session)) {
  300. elem.attrs({ senders: 'initiator' });
  301. } else if (SDPUtil.findLine(m, `a=${MediaDirection.RECVONLY}`, this.session)) {
  302. elem.attrs({ senders: 'responder' });
  303. } else if (SDPUtil.findLine(m, `a=${MediaDirection.INACTIVE}`, this.session)) {
  304. elem.attrs({ senders: 'none' });
  305. }
  306. // Reject an m-line only when port is 0 and a=bundle-only is not present in the section.
  307. // The port is automatically set to 0 when bundle-only is used.
  308. if (mline.port === '0' && !SDPUtil.findLine(m, 'a=bundle-only', this.session)) {
  309. // estos hack to reject an m-line
  310. elem.attrs({ senders: 'rejected' });
  311. }
  312. elem.up(); // end of content
  313. }
  314. elem.up();
  315. return elem;
  316. };
  317. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  318. elem.c('transport');
  319. // XEP-0343 DTLS/SCTP
  320. const sctpport
  321. = SDPUtil.findLine(this.media[mediaindex], 'a=sctp-port:', this.session);
  322. const sctpmap
  323. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', this.session);
  324. if (sctpport) {
  325. const sctpAttrs = SDPUtil.parseSCTPPort(sctpport);
  326. elem.c('sctpmap', {
  327. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  328. number: sctpAttrs, /* SCTP port */
  329. protocol: 'webrtc-datachannel' /* protocol */
  330. });
  331. // The parser currently requires streams to be present
  332. elem.attrs({ streams: 0 });
  333. elem.up();
  334. } else if (sctpmap) {
  335. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  336. elem.c('sctpmap', {
  337. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  338. number: sctpAttrs[0], /* SCTP port */
  339. protocol: sctpAttrs[1] /* protocol */
  340. });
  341. // Optional stream count attribute
  342. if (sctpAttrs.length > 2) {
  343. elem.attrs({ streams: sctpAttrs[2] });
  344. } else {
  345. elem.attrs({ streams: 0 });
  346. }
  347. elem.up();
  348. }
  349. // XEP-0320
  350. const fingerprints
  351. = SDPUtil.findLines(
  352. this.media[mediaindex],
  353. 'a=fingerprint:',
  354. this.session);
  355. fingerprints.forEach(line => {
  356. const fingerprint = SDPUtil.parseFingerprint(line);
  357. fingerprint.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  358. elem.c('fingerprint').t(fingerprint.fingerprint);
  359. delete fingerprint.fingerprint;
  360. const setupLine
  361. = SDPUtil.findLine(
  362. this.media[mediaindex],
  363. 'a=setup:',
  364. this.session);
  365. if (setupLine) {
  366. fingerprint.setup = setupLine.substr(8);
  367. }
  368. elem.attrs(fingerprint);
  369. elem.up(); // end of fingerprint
  370. });
  371. const iceParameters = SDPUtil.iceparams(this.media[mediaindex], this.session);
  372. if (iceParameters) {
  373. iceParameters.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  374. elem.attrs(iceParameters);
  375. // XEP-0176
  376. const candidateLines
  377. = SDPUtil.findLines(
  378. this.media[mediaindex],
  379. 'a=candidate:',
  380. this.session);
  381. candidateLines.forEach(line => { // add any a=candidate lines
  382. const candidate = SDPUtil.candidateToJingle(line);
  383. if (this.failICE) {
  384. candidate.ip = '1.1.1.1';
  385. }
  386. const protocol
  387. = candidate && typeof candidate.protocol === 'string'
  388. ? candidate.protocol.toLowerCase()
  389. : '';
  390. if ((this.removeTcpCandidates
  391. && (protocol === 'tcp' || protocol === 'ssltcp'))
  392. || (this.removeUdpCandidates && protocol === 'udp')) {
  393. return;
  394. }
  395. elem.c('candidate', candidate).up();
  396. });
  397. }
  398. elem.up(); // end of transport
  399. };
  400. // XEP-0293
  401. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  402. const lines
  403. = SDPUtil.findLines(
  404. this.media[mediaindex],
  405. `a=rtcp-fb:${payloadtype}`);
  406. lines.forEach(line => {
  407. const feedback = SDPUtil.parseRTCPFB(line);
  408. if (feedback.type === 'trr-int') {
  409. elem.c('rtcp-fb-trr-int', {
  410. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  411. value: feedback.params[0]
  412. });
  413. elem.up();
  414. } else {
  415. elem.c('rtcp-fb', {
  416. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  417. type: feedback.type
  418. });
  419. if (feedback.params.length > 0) {
  420. elem.attrs({ 'subtype': feedback.params[0] });
  421. }
  422. elem.up();
  423. }
  424. });
  425. };
  426. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  427. let sdp = '';
  428. const feedbackElementTrrInt
  429. = elem.find(
  430. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  431. if (feedbackElementTrrInt.length) {
  432. sdp += 'a=rtcp-fb:* trr-int ';
  433. if (feedbackElementTrrInt.attr('value')) {
  434. sdp += feedbackElementTrrInt.attr('value');
  435. } else {
  436. sdp += '0';
  437. }
  438. sdp += '\r\n';
  439. }
  440. const feedbackElements = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  441. feedbackElements.each((_, fb) => {
  442. sdp += `a=rtcp-fb:${payloadtype} ${fb.getAttribute('type')}`;
  443. if (fb.hasAttribute('subtype')) {
  444. sdp += ` ${fb.getAttribute('subtype')}`;
  445. }
  446. sdp += '\r\n';
  447. });
  448. return sdp;
  449. };
  450. // construct an SDP from a jingle stanza
  451. SDP.prototype.fromJingle = function(jingle) {
  452. const sessionId = Date.now();
  453. // Use a unique session id for every TPC.
  454. this.raw = 'v=0\r\n'
  455. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  456. + 's=-\r\n'
  457. + 't=0 0\r\n';
  458. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  459. // #section-8
  460. const groups
  461. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  462. if (groups.length) {
  463. groups.each((idx, group) => {
  464. const contents
  465. = $(group)
  466. .find('>content')
  467. .map((_, content) => content.getAttribute('name'))
  468. .get();
  469. if (contents.length > 0) {
  470. this.raw
  471. += `a=group:${
  472. group.getAttribute('semantics')
  473. || group.getAttribute('type')} ${
  474. contents.join(' ')}\r\n`;
  475. }
  476. });
  477. }
  478. this.session = this.raw;
  479. jingle.find('>content').each((_, content) => {
  480. const m = this.jingle2media($(content));
  481. this.media.push(m);
  482. });
  483. // reconstruct msid-semantic -- apparently not necessary
  484. /*
  485. var msid = SDPUtil.parseSSRC(this.raw);
  486. if (msid.hasOwnProperty('mslabel')) {
  487. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  488. }
  489. */
  490. this.raw = this.session + this.media.join('');
  491. };
  492. // translate a jingle content element into an an SDP media part
  493. SDP.prototype.jingle2media = function(content) {
  494. const desc = content.find('>description');
  495. const transport = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  496. let sdp = '';
  497. const sctp = transport.find(
  498. '>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  499. const media = { media: desc.attr('media') };
  500. media.port = '9';
  501. if (content.attr('senders') === 'rejected') {
  502. // estos hack to reject an m-line.
  503. media.port = '0';
  504. }
  505. if (transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
  506. media.proto = sctp.length ? 'UDP/DTLS/SCTP' : 'UDP/TLS/RTP/SAVPF';
  507. } else {
  508. media.proto = 'UDP/TLS/RTP/SAVPF';
  509. }
  510. if (sctp.length) {
  511. sdp += `m=application ${media.port} UDP/DTLS/SCTP webrtc-datachannel\r\n`;
  512. sdp += `a=sctp-port:${sctp.attr('number')}\r\n`;
  513. sdp += 'a=max-message-size:262144\r\n';
  514. } else {
  515. media.fmt
  516. = desc
  517. .find('>payload-type')
  518. .map((_, payloadType) => payloadType.getAttribute('id'))
  519. .get();
  520. sdp += `${SDPUtil.buildMLine(media)}\r\n`;
  521. }
  522. sdp += 'c=IN IP4 0.0.0.0\r\n';
  523. if (!sctp.length) {
  524. sdp += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  525. }
  526. // XEP-0176 ICE parameters
  527. if (transport.length) {
  528. if (transport.attr('ufrag')) {
  529. sdp += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
  530. }
  531. if (transport.attr('pwd')) {
  532. sdp += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
  533. }
  534. transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
  535. sdp += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
  536. sdp += ` ${$(fingerprint).text()}`;
  537. sdp += '\r\n';
  538. if (fingerprint.hasAttribute('setup')) {
  539. sdp += `a=setup:${fingerprint.getAttribute('setup')}\r\n`;
  540. }
  541. });
  542. }
  543. // XEP-0176 ICE candidates
  544. transport.find('>candidate')
  545. .each((_, candidate) => {
  546. let protocol = candidate.getAttribute('protocol');
  547. protocol
  548. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  549. if ((this.removeTcpCandidates
  550. && (protocol === 'tcp' || protocol === 'ssltcp'))
  551. || (this.removeUdpCandidates && protocol === 'udp')) {
  552. return;
  553. } else if (this.failICE) {
  554. candidate.setAttribute('ip', '1.1.1.1');
  555. }
  556. sdp += SDPUtil.candidateFromJingle(candidate);
  557. });
  558. switch (content.attr('senders')) {
  559. case 'initiator':
  560. sdp += `a=${MediaDirection.SENDONLY}\r\n`;
  561. break;
  562. case 'responder':
  563. sdp += `a=${MediaDirection.RECVONLY}\r\n`;
  564. break;
  565. case 'none':
  566. sdp += `a=${MediaDirection.INACTIVE}\r\n`;
  567. break;
  568. case 'both':
  569. sdp += `a=${MediaDirection.SENDRECV}\r\n`;
  570. break;
  571. }
  572. sdp += `a=mid:${content.attr('name')}\r\n`;
  573. // <description><rtcp-mux/></description>
  574. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  575. // though
  576. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  577. if (desc.find('>rtcp-mux').length) {
  578. sdp += 'a=rtcp-mux\r\n';
  579. }
  580. desc.find('>payload-type').each((_, payloadType) => {
  581. sdp += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
  582. if ($(payloadType).find('>parameter').length) {
  583. sdp += `a=fmtp:${payloadType.getAttribute('id')} `;
  584. sdp
  585. += $(payloadType)
  586. .find('>parameter')
  587. .map((__, parameter) => {
  588. const name = parameter.getAttribute('name');
  589. return (
  590. (name ? `${name}=` : '')
  591. + parameter.getAttribute('value'));
  592. })
  593. .get()
  594. .join(';');
  595. sdp += '\r\n';
  596. }
  597. // xep-0293
  598. sdp += this.rtcpFbFromJingle($(payloadType), payloadType.getAttribute('id'));
  599. });
  600. // xep-0293
  601. sdp += this.rtcpFbFromJingle(desc, '*');
  602. // xep-0294
  603. desc
  604. .find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]')
  605. .each((_, hdrExt) => {
  606. sdp
  607. += `a=extmap:${hdrExt.getAttribute('id')} ${
  608. hdrExt.getAttribute('uri')}\r\n`;
  609. });
  610. // XEP-0339 handle ssrc-group attributes
  611. desc
  612. .find('>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  613. .each((_, ssrcGroup) => {
  614. const semantics = ssrcGroup.getAttribute('semantics');
  615. const ssrcs
  616. = $(ssrcGroup)
  617. .find('>source')
  618. .map((__, source) => source.getAttribute('ssrc'))
  619. .get();
  620. if (ssrcs.length) {
  621. sdp += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  622. }
  623. });
  624. // XEP-0339 handle source attributes
  625. let userSources = '';
  626. let nonUserSources = '';
  627. desc
  628. .find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  629. .each((_, source) => {
  630. const ssrc = source.getAttribute('ssrc');
  631. let isUserSource = true;
  632. let sourceStr = '';
  633. $(source)
  634. .find('>parameter')
  635. .each((__, parameter) => {
  636. const name = parameter.getAttribute('name');
  637. let value = parameter.getAttribute('value');
  638. value = SDPUtil.filterSpecialChars(value);
  639. sourceStr += `a=ssrc:${ssrc} ${name}`;
  640. if (value && value.length) {
  641. sourceStr += `:${value}`;
  642. }
  643. sourceStr += '\r\n';
  644. if (value?.includes('mixedmslabel')) {
  645. isUserSource = false;
  646. }
  647. });
  648. if (isUserSource) {
  649. userSources += sourceStr;
  650. } else {
  651. nonUserSources += sourceStr;
  652. }
  653. });
  654. // The sdp-interop package is relying the mixedmslabel m line to be the first one in order to set the direction
  655. // to sendrecv.
  656. sdp += nonUserSources + userSources;
  657. return sdp;
  658. };