modified lib-jitsi-meet dev repo
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* global $, APP */
  2. const SDPUtil = require('./SDPUtil');
  3. // SDP STUFF
  4. function SDP(sdp) {
  5. const media = sdp.split('\r\nm=');
  6. for (let i = 1, length = media.length; i < length; i++) {
  7. let media_i = `m=${media[i]}`;
  8. if (i != length - 1) {
  9. media_i += '\r\n';
  10. }
  11. media[i] = media_i;
  12. }
  13. const session = `${media.shift()}\r\n`;
  14. this.media = media;
  15. this.raw = session + media.join('');
  16. this.session = session;
  17. }
  18. /**
  19. * A flag will make {@link transportToJingle} and {@link jingle2media} replace
  20. * ICE candidates IPs with invalid value of '1.1.1.1' which will cause ICE
  21. * failure. The flag is used in the automated testing.
  22. * @type {boolean}
  23. */
  24. SDP.prototype.failICE = false;
  25. /**
  26. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  27. * @type {boolean}
  28. */
  29. SDP.prototype.removeTcpCandidates = false;
  30. /**
  31. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  32. * @type {boolean}
  33. */
  34. SDP.prototype.removeUdpCandidates = false;
  35. /**
  36. * Returns map of MediaChannel mapped per channel idx.
  37. */
  38. SDP.prototype.getMediaSsrcMap = function() {
  39. const self = this;
  40. const media_ssrcs = {};
  41. let tmp;
  42. for (let mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
  43. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc:');
  44. const mid
  45. = SDPUtil.parse_mid(
  46. SDPUtil.find_line(self.media[mediaindex],
  47. 'a=mid:'));
  48. const media = {
  49. mediaindex,
  50. mid,
  51. ssrcs: {},
  52. ssrcGroups: []
  53. };
  54. media_ssrcs[mediaindex] = media;
  55. tmp.forEach(line => {
  56. const linessrc = line.substring(7).split(' ')[0];
  57. // allocate new ChannelSsrc
  58. if (!media.ssrcs[linessrc]) {
  59. media.ssrcs[linessrc] = {
  60. ssrc: linessrc,
  61. lines: []
  62. };
  63. }
  64. media.ssrcs[linessrc].lines.push(line);
  65. });
  66. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc-group:');
  67. tmp.forEach(line => {
  68. const idx = line.indexOf(' ');
  69. const semantics = line.substr(0, idx).substr(13);
  70. const ssrcs = line.substr(14 + semantics.length).split(' ');
  71. if (ssrcs.length) {
  72. media.ssrcGroups.push({
  73. semantics,
  74. ssrcs
  75. });
  76. }
  77. });
  78. }
  79. return media_ssrcs;
  80. };
  81. /**
  82. * Returns <tt>true</tt> if this SDP contains given SSRC.
  83. * @param ssrc the ssrc to check.
  84. * @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
  85. */
  86. SDP.prototype.containsSSRC = function(ssrc) {
  87. // FIXME this code is really strange - improve it if you can
  88. const medias = this.getMediaSsrcMap();
  89. let result = false;
  90. Object.keys(medias).forEach(mediaindex => {
  91. if (result) {
  92. return;
  93. }
  94. if (medias[mediaindex].ssrcs[ssrc]) {
  95. result = true;
  96. }
  97. });
  98. return result;
  99. };
  100. // remove iSAC and CN from SDP
  101. SDP.prototype.mangle = function() {
  102. let i, j, lines, mline, newdesc, rtpmap;
  103. for (i = 0; i < this.media.length; i++) {
  104. lines = this.media[i].split('\r\n');
  105. lines.pop(); // remove empty last element
  106. mline = SDPUtil.parse_mline(lines.shift());
  107. if (mline.media != 'audio') {
  108. continue; // eslint-disable-line no-continue
  109. }
  110. newdesc = '';
  111. mline.fmt.length = 0;
  112. for (j = 0; j < lines.length; j++) {
  113. if (lines[j].substr(0, 9) == 'a=rtpmap:') {
  114. rtpmap = SDPUtil.parse_rtpmap(lines[j]);
  115. if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC') {
  116. continue; // eslint-disable-line no-continue
  117. }
  118. mline.fmt.push(rtpmap.id);
  119. }
  120. newdesc += `${lines[j]}\r\n`;
  121. }
  122. this.media[i] = `${SDPUtil.build_mline(mline)}\r\n${newdesc}`;
  123. }
  124. this.raw = this.session + this.media.join('');
  125. };
  126. // remove lines matching prefix from session section
  127. SDP.prototype.removeSessionLines = function(prefix) {
  128. const self = this;
  129. const lines = SDPUtil.find_lines(this.session, prefix);
  130. lines.forEach(line => {
  131. self.session = self.session.replace(`${line}\r\n`, '');
  132. });
  133. this.raw = this.session + this.media.join('');
  134. return lines;
  135. };
  136. // remove lines matching prefix from a media section specified by mediaindex
  137. // TODO: non-numeric mediaindex could match mid
  138. SDP.prototype.removeMediaLines = function(mediaindex, prefix) {
  139. const self = this;
  140. const lines = SDPUtil.find_lines(this.media[mediaindex], prefix);
  141. lines.forEach(line => {
  142. self.media[mediaindex]
  143. = self.media[mediaindex].replace(`${line}\r\n`, '');
  144. });
  145. this.raw = this.session + this.media.join('');
  146. return lines;
  147. };
  148. // add content's to a jingle element
  149. SDP.prototype.toJingle = function(elem, thecreator) {
  150. let i, j, k, lines, mline, rtpmap, ssrc, tmp;
  151. // new bundle plan
  152. lines = SDPUtil.find_lines(this.session, 'a=group:');
  153. if (lines.length) {
  154. for (i = 0; i < lines.length; i++) {
  155. tmp = lines[i].split(' ');
  156. const semantics = tmp.shift().substr(8);
  157. elem.c('group', { xmlns: 'urn:xmpp:jingle:apps:grouping:0',
  158. semantics });
  159. for (j = 0; j < tmp.length; j++) {
  160. elem.c('content', { name: tmp[j] }).up();
  161. }
  162. elem.up();
  163. }
  164. }
  165. for (i = 0; i < this.media.length; i++) {
  166. mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
  167. if (!(mline.media === 'audio'
  168. || mline.media === 'video'
  169. || mline.media === 'application')) {
  170. continue; // eslint-disable-line no-continue
  171. }
  172. const assrcline = SDPUtil.find_line(this.media[i], 'a=ssrc:');
  173. if (assrcline) {
  174. ssrc = assrcline.substring(7).split(' ')[0]; // take the first
  175. } else {
  176. ssrc = false;
  177. }
  178. elem.c('content', { creator: thecreator,
  179. name: mline.media });
  180. const amidline = SDPUtil.find_line(this.media[i], 'a=mid:');
  181. if (amidline) {
  182. // prefer identifier from a=mid if present
  183. const mid = SDPUtil.parse_mid(amidline);
  184. elem.attrs({ name: mid });
  185. }
  186. if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length) {
  187. elem.c('description',
  188. { xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  189. media: mline.media });
  190. if (ssrc) {
  191. elem.attrs({ ssrc });
  192. }
  193. for (j = 0; j < mline.fmt.length; j++) {
  194. rtpmap
  195. = SDPUtil.find_line(
  196. this.media[i],
  197. `a=rtpmap:${mline.fmt[j]}`);
  198. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  199. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo
  200. // value=bar/>
  201. const afmtpline
  202. = SDPUtil.find_line(
  203. this.media[i],
  204. `a=fmtp:${mline.fmt[j]}`);
  205. if (afmtpline) {
  206. tmp = SDPUtil.parse_fmtp(afmtpline);
  207. for (k = 0; k < tmp.length; k++) {
  208. elem.c('parameter', tmp[k]).up();
  209. }
  210. }
  211. // XEP-0293 -- map a=rtcp-fb
  212. this.rtcpFbToJingle(i, elem, mline.fmt[j]);
  213. elem.up();
  214. }
  215. const crypto
  216. = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
  217. if (crypto.length) {
  218. elem.c('encryption', { required: 1 });
  219. crypto.forEach(
  220. line => elem.c('crypto', SDPUtil.parse_crypto(line)).up());
  221. elem.up(); // end of encryption
  222. }
  223. if (ssrc) {
  224. // new style mapping
  225. elem.c('source', { ssrc,
  226. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  227. // FIXME: group by ssrc and support multiple different ssrcs
  228. const ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
  229. if (ssrclines.length > 0) {
  230. ssrclines.forEach(line => {
  231. const idx = line.indexOf(' ');
  232. const linessrc = line.substr(0, idx).substr(7);
  233. if (linessrc != ssrc) {
  234. elem.up();
  235. ssrc = linessrc;
  236. elem.c('source', { ssrc,
  237. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  238. }
  239. const kv = line.substr(idx + 1);
  240. elem.c('parameter');
  241. if (kv.indexOf(':') == -1) {
  242. elem.attrs({ name: kv });
  243. } else {
  244. const k = kv.split(':', 2)[0];
  245. elem.attrs({ name: k });
  246. let v = kv.split(':', 2)[1];
  247. v = SDPUtil.filter_special_chars(v);
  248. elem.attrs({ value: v });
  249. }
  250. elem.up();
  251. });
  252. } else {
  253. elem.up();
  254. elem.c('source', { ssrc,
  255. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  256. elem.c('parameter');
  257. elem.attrs({
  258. name: 'cname',
  259. // eslint-disable-next-line newline-per-chained-call
  260. value: Math.random().toString(36).substring(7)
  261. });
  262. elem.up();
  263. // FIXME what case does this code handle ? remove ???
  264. let msid = null;
  265. // FIXME what is this ? global APP.RTC in SDP ?
  266. const localTrack = APP.RTC.getLocalTracks(mline.media);
  267. if (localTrack) {
  268. // FIXME before this changes the track id was accessed,
  269. // but msid stands for the stream id, makes no sense ?
  270. msid = localTrack.getTrackId();
  271. }
  272. if (msid !== null) {
  273. msid = SDPUtil.filter_special_chars(msid);
  274. elem.c('parameter');
  275. elem.attrs({ name: 'msid',
  276. value: msid });
  277. elem.up();
  278. elem.c('parameter');
  279. elem.attrs({ name: 'mslabel',
  280. value: msid });
  281. elem.up();
  282. elem.c('parameter');
  283. elem.attrs({ name: 'label',
  284. value: msid });
  285. elem.up();
  286. }
  287. }
  288. elem.up();
  289. // XEP-0339 handle ssrc-group attributes
  290. const ssrc_group_lines
  291. = SDPUtil.find_lines(this.media[i], 'a=ssrc-group:');
  292. ssrc_group_lines.forEach(line => {
  293. const idx = line.indexOf(' ');
  294. const semantics = line.substr(0, idx).substr(13);
  295. const ssrcs = line.substr(14 + semantics.length).split(' ');
  296. if (ssrcs.length) {
  297. elem.c('ssrc-group', { semantics,
  298. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  299. ssrcs.forEach(ssrc => elem.c('source', { ssrc }).up());
  300. elem.up();
  301. }
  302. });
  303. }
  304. if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
  305. elem.c('rtcp-mux').up();
  306. }
  307. // XEP-0293 -- map a=rtcp-fb:*
  308. this.rtcpFbToJingle(i, elem, '*');
  309. // XEP-0294
  310. lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
  311. if (lines.length) {
  312. for (j = 0; j < lines.length; j++) {
  313. tmp = SDPUtil.parse_extmap(lines[j]);
  314. elem.c('rtp-hdrext', {
  315. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  316. uri: tmp.uri,
  317. id: tmp.value
  318. });
  319. if (tmp.hasOwnProperty('direction')) {
  320. switch (tmp.direction) {
  321. case 'sendonly':
  322. elem.attrs({ senders: 'responder' });
  323. break;
  324. case 'recvonly':
  325. elem.attrs({ senders: 'initiator' });
  326. break;
  327. case 'sendrecv':
  328. elem.attrs({ senders: 'both' });
  329. break;
  330. case 'inactive':
  331. elem.attrs({ senders: 'none' });
  332. break;
  333. }
  334. }
  335. // TODO: handle params
  336. elem.up();
  337. }
  338. }
  339. elem.up(); // end of description
  340. }
  341. // map ice-ufrag/pwd, dtls fingerprint, candidates
  342. this.transportToJingle(i, elem);
  343. const m = this.media[i];
  344. if (SDPUtil.find_line(m, 'a=sendrecv', this.session)) {
  345. elem.attrs({ senders: 'both' });
  346. } else if (SDPUtil.find_line(m, 'a=sendonly', this.session)) {
  347. elem.attrs({ senders: 'initiator' });
  348. } else if (SDPUtil.find_line(m, 'a=recvonly', this.session)) {
  349. elem.attrs({ senders: 'responder' });
  350. } else if (SDPUtil.find_line(m, 'a=inactive', this.session)) {
  351. elem.attrs({ senders: 'none' });
  352. }
  353. if (mline.port == '0') {
  354. // estos hack to reject an m-line
  355. elem.attrs({ senders: 'rejected' });
  356. }
  357. elem.up(); // end of content
  358. }
  359. elem.up();
  360. return elem;
  361. };
  362. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  363. let tmp;
  364. const self = this;
  365. elem.c('transport');
  366. // XEP-0343 DTLS/SCTP
  367. const sctpmap
  368. = SDPUtil.find_line(this.media[mediaindex], 'a=sctpmap:', self.session);
  369. if (sctpmap) {
  370. const sctpAttrs = SDPUtil.parse_sctpmap(sctpmap);
  371. elem.c('sctpmap', {
  372. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  373. number: sctpAttrs[0], /* SCTP port */
  374. protocol: sctpAttrs[1] /* protocol */
  375. });
  376. // Optional stream count attribute
  377. if (sctpAttrs.length > 2) {
  378. elem.attrs({ streams: sctpAttrs[2] });
  379. }
  380. elem.up();
  381. }
  382. // XEP-0320
  383. const fingerprints
  384. = SDPUtil.find_lines(
  385. this.media[mediaindex],
  386. 'a=fingerprint:',
  387. this.session);
  388. fingerprints.forEach(line => {
  389. tmp = SDPUtil.parse_fingerprint(line);
  390. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  391. elem.c('fingerprint').t(tmp.fingerprint);
  392. delete tmp.fingerprint;
  393. line
  394. = SDPUtil.find_line(
  395. self.media[mediaindex],
  396. 'a=setup:',
  397. self.session);
  398. if (line) {
  399. tmp.setup = line.substr(8);
  400. }
  401. elem.attrs(tmp);
  402. elem.up(); // end of fingerprint
  403. });
  404. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  405. if (tmp) {
  406. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  407. elem.attrs(tmp);
  408. // XEP-0176
  409. const lines
  410. = SDPUtil.find_lines(
  411. this.media[mediaindex],
  412. 'a=candidate:',
  413. this.session);
  414. if (lines.length) { // add any a=candidate lines
  415. lines.forEach(line => {
  416. const candidate = SDPUtil.candidateToJingle(line);
  417. if (self.failICE) {
  418. candidate.ip = '1.1.1.1';
  419. }
  420. const protocol
  421. = candidate && typeof candidate.protocol === 'string'
  422. ? candidate.protocol.toLowerCase()
  423. : '';
  424. if ((self.removeTcpCandidates
  425. && (protocol === 'tcp' || protocol === 'ssltcp'))
  426. || (self.removeUdpCandidates && protocol === 'udp')) {
  427. return;
  428. }
  429. elem.c('candidate', candidate).up();
  430. });
  431. }
  432. }
  433. elem.up(); // end of transport
  434. };
  435. // XEP-0293
  436. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  437. const lines
  438. = SDPUtil.find_lines(
  439. this.media[mediaindex],
  440. `a=rtcp-fb:${payloadtype}`);
  441. lines.forEach(line => {
  442. const tmp = SDPUtil.parse_rtcpfb(line);
  443. if (tmp.type == 'trr-int') {
  444. elem.c('rtcp-fb-trr-int', {
  445. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  446. value: tmp.params[0]
  447. });
  448. elem.up();
  449. } else {
  450. elem.c('rtcp-fb', {
  451. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  452. type: tmp.type
  453. });
  454. if (tmp.params.length > 0) {
  455. elem.attrs({ 'subtype': tmp.params[0] });
  456. }
  457. elem.up();
  458. }
  459. });
  460. };
  461. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  462. let media = '';
  463. let tmp
  464. = elem.find(
  465. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  466. if (tmp.length) {
  467. media += 'a=rtcp-fb:* trr-int ';
  468. if (tmp.attr('value')) {
  469. media += tmp.attr('value');
  470. } else {
  471. media += '0';
  472. }
  473. media += '\r\n';
  474. }
  475. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  476. tmp.each(function() {
  477. media += `a=rtcp-fb:${payloadtype} ${$(this).attr('type')}`;
  478. if ($(this).attr('subtype')) {
  479. media += ` ${$(this).attr('subtype')}`;
  480. }
  481. media += '\r\n';
  482. });
  483. return media;
  484. };
  485. // construct an SDP from a jingle stanza
  486. SDP.prototype.fromJingle = function(jingle) {
  487. const self = this;
  488. this.raw = 'v=0\r\n'
  489. + 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n'// FIXME
  490. + 's=-\r\n'
  491. + 't=0 0\r\n';
  492. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  493. // #section-8
  494. const groups
  495. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  496. if (groups.length) {
  497. groups.each((idx, group) => {
  498. const contents
  499. = $(group)
  500. .find('>content')
  501. .map((idx, content) => content.getAttribute('name'))
  502. .get();
  503. if (contents.length > 0) {
  504. self.raw
  505. += `a=group:${
  506. group.getAttribute('semantics')
  507. || group.getAttribute('type')} ${
  508. contents.join(' ')}\r\n`;
  509. }
  510. });
  511. }
  512. this.session = this.raw;
  513. jingle.find('>content').each(function() {
  514. const m = self.jingle2media($(this));
  515. self.media.push(m);
  516. });
  517. // reconstruct msid-semantic -- apparently not necessary
  518. /*
  519. var msid = SDPUtil.parse_ssrc(this.raw);
  520. if (msid.hasOwnProperty('mslabel')) {
  521. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  522. }
  523. */
  524. this.raw = this.session + this.media.join('');
  525. };
  526. // translate a jingle content element into an an SDP media part
  527. SDP.prototype.jingle2media = function(content) {
  528. const desc = content.find('description');
  529. let media = '';
  530. const self = this;
  531. const sctp = content.find(
  532. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  533. let tmp = { media: desc.attr('media') };
  534. tmp.port = '1';
  535. if (content.attr('senders') == 'rejected') {
  536. // estos hack to reject an m-line.
  537. tmp.port = '0';
  538. }
  539. if (content.find('>transport>fingerprint').length
  540. || desc.find('encryption').length) {
  541. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  542. } else {
  543. tmp.proto = 'RTP/AVPF';
  544. }
  545. if (sctp.length) {
  546. media += `m=application 1 DTLS/SCTP ${sctp.attr('number')}\r\n`;
  547. media += `a=sctpmap:${sctp.attr('number')} ${sctp.attr('protocol')}`;
  548. const streamCount = sctp.attr('streams');
  549. if (streamCount) {
  550. media += ` ${streamCount}\r\n`;
  551. } else {
  552. media += '\r\n';
  553. }
  554. } else {
  555. tmp.fmt
  556. = desc
  557. .find('payload-type')
  558. .map(function() {
  559. return this.getAttribute('id');
  560. })
  561. .get();
  562. media += `${SDPUtil.build_mline(tmp)}\r\n`;
  563. }
  564. media += 'c=IN IP4 0.0.0.0\r\n';
  565. if (!sctp.length) {
  566. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  567. }
  568. tmp
  569. = content.find(
  570. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  571. if (tmp.length) {
  572. if (tmp.attr('ufrag')) {
  573. media += `${SDPUtil.build_iceufrag(tmp.attr('ufrag'))}\r\n`;
  574. }
  575. if (tmp.attr('pwd')) {
  576. media += `${SDPUtil.build_icepwd(tmp.attr('pwd'))}\r\n`;
  577. }
  578. tmp.find('>fingerprint').each(function() {
  579. // FIXME: check namespace at some point
  580. media += `a=fingerprint:${this.getAttribute('hash')}`;
  581. media += ` ${$(this).text()}`;
  582. media += '\r\n';
  583. if (this.getAttribute('setup')) {
  584. media += `a=setup:${this.getAttribute('setup')}\r\n`;
  585. }
  586. });
  587. }
  588. switch (content.attr('senders')) {
  589. case 'initiator':
  590. media += 'a=sendonly\r\n';
  591. break;
  592. case 'responder':
  593. media += 'a=recvonly\r\n';
  594. break;
  595. case 'none':
  596. media += 'a=inactive\r\n';
  597. break;
  598. case 'both':
  599. media += 'a=sendrecv\r\n';
  600. break;
  601. }
  602. media += `a=mid:${content.attr('name')}\r\n`;
  603. // <description><rtcp-mux/></description>
  604. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  605. // though
  606. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  607. if (desc.find('rtcp-mux').length) {
  608. media += 'a=rtcp-mux\r\n';
  609. }
  610. if (desc.find('encryption').length) {
  611. desc.find('encryption>crypto').each(function() {
  612. media += `a=crypto:${this.getAttribute('tag')}`;
  613. media += ` ${this.getAttribute('crypto-suite')}`;
  614. media += ` ${this.getAttribute('key-params')}`;
  615. if (this.getAttribute('session-params')) {
  616. media += ` ${this.getAttribute('session-params')}`;
  617. }
  618. media += '\r\n';
  619. });
  620. }
  621. desc.find('payload-type').each(function() {
  622. media += `${SDPUtil.build_rtpmap(this)}\r\n`;
  623. if ($(this).find('>parameter').length) {
  624. media += `a=fmtp:${this.getAttribute('id')} `;
  625. media
  626. += $(this)
  627. .find('parameter')
  628. .map(function() {
  629. return (this.getAttribute('name')
  630. ? `${this.getAttribute('name')}=` : '')
  631. + this.getAttribute('value');
  632. })
  633. .get()
  634. .join('; ');
  635. media += '\r\n';
  636. }
  637. // xep-0293
  638. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  639. });
  640. // xep-0293
  641. media += self.rtcpFbFromJingle(desc, '*');
  642. // xep-0294
  643. tmp
  644. = desc.find(
  645. '>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  646. tmp.each(function() {
  647. media
  648. += `a=extmap:${this.getAttribute('id')} ${
  649. this.getAttribute('uri')}\r\n`;
  650. });
  651. content
  652. .find(
  653. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]'
  654. + '>candidate')
  655. .each(function() {
  656. let protocol = this.getAttribute('protocol');
  657. protocol
  658. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  659. if ((self.removeTcpCandidates
  660. && (protocol === 'tcp' || protocol === 'ssltcp'))
  661. || (self.removeUdpCandidates && protocol === 'udp')) {
  662. return;
  663. } else if (self.failICE) {
  664. this.setAttribute('ip', '1.1.1.1');
  665. }
  666. media += SDPUtil.candidateFromJingle(this);
  667. });
  668. // XEP-0339 handle ssrc-group attributes
  669. content
  670. .find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  671. .each(function() {
  672. const semantics = this.getAttribute('semantics');
  673. const ssrcs
  674. = $(this)
  675. .find('>source')
  676. .map(function() {
  677. return this.getAttribute('ssrc');
  678. })
  679. .get();
  680. if (ssrcs.length) {
  681. media += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  682. }
  683. });
  684. tmp
  685. = content.find(
  686. 'description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  687. tmp.each(function() {
  688. const ssrc = this.getAttribute('ssrc');
  689. // eslint-disable-next-line newline-per-chained-call
  690. $(this).find('>parameter').each(function() {
  691. const name = this.getAttribute('name');
  692. let value = this.getAttribute('value');
  693. value = SDPUtil.filter_special_chars(value);
  694. media += `a=ssrc:${ssrc} ${name}`;
  695. if (value && value.length) {
  696. media += `:${value}`;
  697. }
  698. media += '\r\n';
  699. });
  700. });
  701. return media;
  702. };
  703. module.exports = SDP;