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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 mediaI = `m=${media[i]}`;
  8. if (i !== length - 1) {
  9. mediaI += '\r\n';
  10. }
  11. media[i] = mediaI;
  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 mediaSSRCs = {};
  41. let tmp;
  42. for (let mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
  43. tmp = SDPUtil.findLines(self.media[mediaindex], 'a=ssrc:');
  44. const mid
  45. = SDPUtil.parseMID(
  46. SDPUtil.findLine(self.media[mediaindex],
  47. 'a=mid:'));
  48. const media = {
  49. mediaindex,
  50. mid,
  51. ssrcs: {},
  52. ssrcGroups: []
  53. };
  54. mediaSSRCs[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.findLines(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 mediaSSRCs;
  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.parseMLine(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.parseRTPMap(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.buildMLine(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.findLines(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.findLines(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.findLines(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.parseMLine(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.findLine(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.findLine(this.media[i], 'a=mid:');
  181. if (amidline) {
  182. // prefer identifier from a=mid if present
  183. const mid = SDPUtil.parseMID(amidline);
  184. elem.attrs({ name: mid });
  185. }
  186. if (SDPUtil.findLine(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.findLine(
  196. this.media[i],
  197. `a=rtpmap:${mline.fmt[j]}`);
  198. elem.c('payload-type', SDPUtil.parseRTPMap(rtpmap));
  199. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo
  200. // value=bar/>
  201. const afmtpline
  202. = SDPUtil.findLine(
  203. this.media[i],
  204. `a=fmtp:${mline.fmt[j]}`);
  205. if (afmtpline) {
  206. tmp = SDPUtil.parseFmtp(afmtpline);
  207. // eslint-disable-next-line max-depth
  208. for (k = 0; k < tmp.length; k++) {
  209. elem.c('parameter', tmp[k]).up();
  210. }
  211. }
  212. // XEP-0293 -- map a=rtcp-fb
  213. this.rtcpFbToJingle(i, elem, mline.fmt[j]);
  214. elem.up();
  215. }
  216. const crypto
  217. = SDPUtil.findLines(this.media[i], 'a=crypto:', this.session);
  218. if (crypto.length) {
  219. elem.c('encryption', { required: 1 });
  220. crypto.forEach(
  221. line => elem.c('crypto', SDPUtil.parseCrypto(line)).up());
  222. elem.up(); // end of encryption
  223. }
  224. if (ssrc) {
  225. // new style mapping
  226. elem.c('source', { ssrc,
  227. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  228. // FIXME: group by ssrc and support multiple different ssrcs
  229. const ssrclines = SDPUtil.findLines(this.media[i], 'a=ssrc:');
  230. if (ssrclines.length > 0) {
  231. // eslint-disable-next-line no-loop-func
  232. ssrclines.forEach(line => {
  233. const idx = line.indexOf(' ');
  234. const linessrc = line.substr(0, idx).substr(7);
  235. if (linessrc !== ssrc) {
  236. elem.up();
  237. ssrc = linessrc;
  238. elem.c('source', { ssrc,
  239. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  240. }
  241. const kv = line.substr(idx + 1);
  242. elem.c('parameter');
  243. if (kv.indexOf(':') === -1) {
  244. elem.attrs({ name: kv });
  245. } else {
  246. const name = kv.split(':', 2)[0];
  247. elem.attrs({ name });
  248. let v = kv.split(':', 2)[1];
  249. v = SDPUtil.filterSpecialChars(v);
  250. elem.attrs({ value: v });
  251. }
  252. elem.up();
  253. });
  254. } else {
  255. elem.up();
  256. elem.c('source', { ssrc,
  257. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  258. elem.c('parameter');
  259. elem.attrs({
  260. name: 'cname',
  261. // eslint-disable-next-line newline-per-chained-call
  262. value: Math.random().toString(36).substring(7)
  263. });
  264. elem.up();
  265. // FIXME what case does this code handle ? remove ???
  266. let msid = null;
  267. // FIXME what is this ? global APP.RTC in SDP ?
  268. const localTrack = APP.RTC.getLocalTracks(mline.media);
  269. // eslint-disable-next-line max-depth
  270. if (localTrack) {
  271. // FIXME before this changes the track id was accessed,
  272. // but msid stands for the stream id, makes no sense ?
  273. msid = localTrack.getTrackId();
  274. }
  275. // eslint-disable-next-line max-depth
  276. if (msid !== null) {
  277. msid = SDPUtil.filterSpecialChars(msid);
  278. elem.c('parameter');
  279. elem.attrs({ name: 'msid',
  280. value: msid });
  281. elem.up();
  282. elem.c('parameter');
  283. elem.attrs({ name: 'mslabel',
  284. value: msid });
  285. elem.up();
  286. elem.c('parameter');
  287. elem.attrs({ name: 'label',
  288. value: msid });
  289. elem.up();
  290. }
  291. }
  292. elem.up();
  293. // XEP-0339 handle ssrc-group attributes
  294. const ssrcGroupLines
  295. = SDPUtil.findLines(this.media[i], 'a=ssrc-group:');
  296. ssrcGroupLines.forEach(line => {
  297. const idx = line.indexOf(' ');
  298. const semantics = line.substr(0, idx).substr(13);
  299. const ssrcs = line.substr(14 + semantics.length).split(' ');
  300. if (ssrcs.length) {
  301. elem.c('ssrc-group', { semantics,
  302. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  303. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  304. elem.up();
  305. }
  306. });
  307. }
  308. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  309. elem.c('rtcp-mux').up();
  310. }
  311. // XEP-0293 -- map a=rtcp-fb:*
  312. this.rtcpFbToJingle(i, elem, '*');
  313. // XEP-0294
  314. lines = SDPUtil.findLines(this.media[i], 'a=extmap:');
  315. if (lines.length) {
  316. for (j = 0; j < lines.length; j++) {
  317. tmp = SDPUtil.parseExtmap(lines[j]);
  318. elem.c('rtp-hdrext', {
  319. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  320. uri: tmp.uri,
  321. id: tmp.value
  322. });
  323. // eslint-disable-next-line max-depth
  324. if (tmp.hasOwnProperty('direction')) {
  325. // eslint-disable-next-line max-depth
  326. switch (tmp.direction) {
  327. case 'sendonly':
  328. elem.attrs({ senders: 'responder' });
  329. break;
  330. case 'recvonly':
  331. elem.attrs({ senders: 'initiator' });
  332. break;
  333. case 'sendrecv':
  334. elem.attrs({ senders: 'both' });
  335. break;
  336. case 'inactive':
  337. elem.attrs({ senders: 'none' });
  338. break;
  339. }
  340. }
  341. // TODO: handle params
  342. elem.up();
  343. }
  344. }
  345. elem.up(); // end of description
  346. }
  347. // map ice-ufrag/pwd, dtls fingerprint, candidates
  348. this.transportToJingle(i, elem);
  349. const m = this.media[i];
  350. if (SDPUtil.findLine(m, 'a=sendrecv', this.session)) {
  351. elem.attrs({ senders: 'both' });
  352. } else if (SDPUtil.findLine(m, 'a=sendonly', this.session)) {
  353. elem.attrs({ senders: 'initiator' });
  354. } else if (SDPUtil.findLine(m, 'a=recvonly', this.session)) {
  355. elem.attrs({ senders: 'responder' });
  356. } else if (SDPUtil.findLine(m, 'a=inactive', this.session)) {
  357. elem.attrs({ senders: 'none' });
  358. }
  359. if (mline.port === '0') {
  360. // estos hack to reject an m-line
  361. elem.attrs({ senders: 'rejected' });
  362. }
  363. elem.up(); // end of content
  364. }
  365. elem.up();
  366. return elem;
  367. };
  368. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  369. let tmp;
  370. const self = this;
  371. elem.c('transport');
  372. // XEP-0343 DTLS/SCTP
  373. const sctpmap
  374. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', self.session);
  375. if (sctpmap) {
  376. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  377. elem.c('sctpmap', {
  378. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  379. number: sctpAttrs[0], /* SCTP port */
  380. protocol: sctpAttrs[1] /* protocol */
  381. });
  382. // Optional stream count attribute
  383. if (sctpAttrs.length > 2) {
  384. elem.attrs({ streams: sctpAttrs[2] });
  385. }
  386. elem.up();
  387. }
  388. // XEP-0320
  389. const fingerprints
  390. = SDPUtil.findLines(
  391. this.media[mediaindex],
  392. 'a=fingerprint:',
  393. this.session);
  394. fingerprints.forEach(line => {
  395. tmp = SDPUtil.parseFingerprint(line);
  396. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  397. elem.c('fingerprint').t(tmp.fingerprint);
  398. delete tmp.fingerprint;
  399. // eslint-disable-next-line no-param-reassign
  400. line
  401. = SDPUtil.findLine(
  402. self.media[mediaindex],
  403. 'a=setup:',
  404. self.session);
  405. if (line) {
  406. tmp.setup = line.substr(8);
  407. }
  408. elem.attrs(tmp);
  409. elem.up(); // end of fingerprint
  410. });
  411. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  412. if (tmp) {
  413. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  414. elem.attrs(tmp);
  415. // XEP-0176
  416. const lines
  417. = SDPUtil.findLines(
  418. this.media[mediaindex],
  419. 'a=candidate:',
  420. this.session);
  421. if (lines.length) { // add any a=candidate lines
  422. lines.forEach(line => {
  423. const candidate = SDPUtil.candidateToJingle(line);
  424. if (self.failICE) {
  425. candidate.ip = '1.1.1.1';
  426. }
  427. const protocol
  428. = candidate && typeof candidate.protocol === 'string'
  429. ? candidate.protocol.toLowerCase()
  430. : '';
  431. if ((self.removeTcpCandidates
  432. && (protocol === 'tcp' || protocol === 'ssltcp'))
  433. || (self.removeUdpCandidates && protocol === 'udp')) {
  434. return;
  435. }
  436. elem.c('candidate', candidate).up();
  437. });
  438. }
  439. }
  440. elem.up(); // end of transport
  441. };
  442. // XEP-0293
  443. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  444. const lines
  445. = SDPUtil.findLines(
  446. this.media[mediaindex],
  447. `a=rtcp-fb:${payloadtype}`);
  448. lines.forEach(line => {
  449. const tmp = SDPUtil.parseRTCPFB(line);
  450. if (tmp.type === 'trr-int') {
  451. elem.c('rtcp-fb-trr-int', {
  452. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  453. value: tmp.params[0]
  454. });
  455. elem.up();
  456. } else {
  457. elem.c('rtcp-fb', {
  458. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  459. type: tmp.type
  460. });
  461. if (tmp.params.length > 0) {
  462. elem.attrs({ 'subtype': tmp.params[0] });
  463. }
  464. elem.up();
  465. }
  466. });
  467. };
  468. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  469. let media = '';
  470. let tmp
  471. = elem.find(
  472. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  473. if (tmp.length) {
  474. media += 'a=rtcp-fb:* trr-int ';
  475. if (tmp.attr('value')) {
  476. media += tmp.attr('value');
  477. } else {
  478. media += '0';
  479. }
  480. media += '\r\n';
  481. }
  482. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  483. tmp.each(function() {
  484. media += `a=rtcp-fb:${payloadtype} ${$(this).attr('type')}`;
  485. if ($(this).attr('subtype')) {
  486. media += ` ${$(this).attr('subtype')}`;
  487. }
  488. media += '\r\n';
  489. });
  490. return media;
  491. };
  492. // construct an SDP from a jingle stanza
  493. SDP.prototype.fromJingle = function(jingle) {
  494. const self = this;
  495. this.raw = 'v=0\r\n'
  496. + 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n'// FIXME
  497. + 's=-\r\n'
  498. + 't=0 0\r\n';
  499. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  500. // #section-8
  501. const groups
  502. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  503. if (groups.length) {
  504. groups.each((idx, group) => {
  505. const contents
  506. = $(group)
  507. .find('>content')
  508. .map((_, content) => content.getAttribute('name'))
  509. .get();
  510. if (contents.length > 0) {
  511. self.raw
  512. += `a=group:${
  513. group.getAttribute('semantics')
  514. || group.getAttribute('type')} ${
  515. contents.join(' ')}\r\n`;
  516. }
  517. });
  518. }
  519. this.session = this.raw;
  520. jingle.find('>content').each(function() {
  521. const m = self.jingle2media($(this));
  522. self.media.push(m);
  523. });
  524. // reconstruct msid-semantic -- apparently not necessary
  525. /*
  526. var msid = SDPUtil.parseSSRC(this.raw);
  527. if (msid.hasOwnProperty('mslabel')) {
  528. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  529. }
  530. */
  531. this.raw = this.session + this.media.join('');
  532. };
  533. // translate a jingle content element into an an SDP media part
  534. SDP.prototype.jingle2media = function(content) {
  535. const desc = content.find('description');
  536. let media = '';
  537. const self = this;
  538. const sctp = content.find(
  539. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  540. let tmp = { media: desc.attr('media') };
  541. tmp.port = '1';
  542. if (content.attr('senders') === 'rejected') {
  543. // estos hack to reject an m-line.
  544. tmp.port = '0';
  545. }
  546. if (content.find('>transport>fingerprint').length
  547. || desc.find('encryption').length) {
  548. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  549. } else {
  550. tmp.proto = 'RTP/AVPF';
  551. }
  552. if (sctp.length) {
  553. media += `m=application 1 DTLS/SCTP ${sctp.attr('number')}\r\n`;
  554. media += `a=sctpmap:${sctp.attr('number')} ${sctp.attr('protocol')}`;
  555. const streamCount = sctp.attr('streams');
  556. if (streamCount) {
  557. media += ` ${streamCount}\r\n`;
  558. } else {
  559. media += '\r\n';
  560. }
  561. } else {
  562. tmp.fmt
  563. = desc
  564. .find('payload-type')
  565. .map(function() {
  566. return this.getAttribute('id');
  567. })
  568. .get();
  569. media += `${SDPUtil.buildMLine(tmp)}\r\n`;
  570. }
  571. media += 'c=IN IP4 0.0.0.0\r\n';
  572. if (!sctp.length) {
  573. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  574. }
  575. tmp
  576. = content.find(
  577. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  578. if (tmp.length) {
  579. if (tmp.attr('ufrag')) {
  580. media += `${SDPUtil.buildICEUfrag(tmp.attr('ufrag'))}\r\n`;
  581. }
  582. if (tmp.attr('pwd')) {
  583. media += `${SDPUtil.buildICEPwd(tmp.attr('pwd'))}\r\n`;
  584. }
  585. tmp.find('>fingerprint').each(function() {
  586. // FIXME: check namespace at some point
  587. media += `a=fingerprint:${this.getAttribute('hash')}`;
  588. media += ` ${$(this).text()}`;
  589. media += '\r\n';
  590. if (this.getAttribute('setup')) {
  591. media += `a=setup:${this.getAttribute('setup')}\r\n`;
  592. }
  593. });
  594. }
  595. switch (content.attr('senders')) {
  596. case 'initiator':
  597. media += 'a=sendonly\r\n';
  598. break;
  599. case 'responder':
  600. media += 'a=recvonly\r\n';
  601. break;
  602. case 'none':
  603. media += 'a=inactive\r\n';
  604. break;
  605. case 'both':
  606. media += 'a=sendrecv\r\n';
  607. break;
  608. }
  609. media += `a=mid:${content.attr('name')}\r\n`;
  610. // <description><rtcp-mux/></description>
  611. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  612. // though
  613. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  614. if (desc.find('rtcp-mux').length) {
  615. media += 'a=rtcp-mux\r\n';
  616. }
  617. if (desc.find('encryption').length) {
  618. desc.find('encryption>crypto').each(function() {
  619. media += `a=crypto:${this.getAttribute('tag')}`;
  620. media += ` ${this.getAttribute('crypto-suite')}`;
  621. media += ` ${this.getAttribute('key-params')}`;
  622. if (this.getAttribute('session-params')) {
  623. media += ` ${this.getAttribute('session-params')}`;
  624. }
  625. media += '\r\n';
  626. });
  627. }
  628. desc.find('payload-type').each(function() {
  629. media += `${SDPUtil.buildRTPMap(this)}\r\n`;
  630. if ($(this).find('>parameter').length) {
  631. media += `a=fmtp:${this.getAttribute('id')} `;
  632. media
  633. += $(this)
  634. .find('parameter')
  635. .map(function() {
  636. return (this.getAttribute('name')
  637. ? `${this.getAttribute('name')}=` : '')
  638. + this.getAttribute('value');
  639. })
  640. .get()
  641. .join('; ');
  642. media += '\r\n';
  643. }
  644. // xep-0293
  645. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  646. });
  647. // xep-0293
  648. media += self.rtcpFbFromJingle(desc, '*');
  649. // xep-0294
  650. tmp
  651. = desc.find(
  652. '>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  653. tmp.each(function() {
  654. media
  655. += `a=extmap:${this.getAttribute('id')} ${
  656. this.getAttribute('uri')}\r\n`;
  657. });
  658. content
  659. .find(
  660. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]'
  661. + '>candidate')
  662. .each(function() {
  663. let protocol = this.getAttribute('protocol');
  664. protocol
  665. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  666. if ((self.removeTcpCandidates
  667. && (protocol === 'tcp' || protocol === 'ssltcp'))
  668. || (self.removeUdpCandidates && protocol === 'udp')) {
  669. return;
  670. } else if (self.failICE) {
  671. this.setAttribute('ip', '1.1.1.1');
  672. }
  673. media += SDPUtil.candidateFromJingle(this);
  674. });
  675. // XEP-0339 handle ssrc-group attributes
  676. content
  677. .find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  678. .each(function() {
  679. const semantics = this.getAttribute('semantics');
  680. const ssrcs
  681. = $(this)
  682. .find('>source')
  683. .map(function() {
  684. return this.getAttribute('ssrc');
  685. })
  686. .get();
  687. if (ssrcs.length) {
  688. media += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  689. }
  690. });
  691. tmp
  692. = content.find(
  693. 'description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  694. tmp.each(function() {
  695. const ssrc = this.getAttribute('ssrc');
  696. // eslint-disable-next-line newline-per-chained-call
  697. $(this).find('>parameter').each(function() {
  698. const name = this.getAttribute('name');
  699. let value = this.getAttribute('value');
  700. value = SDPUtil.filterSpecialChars(value);
  701. media += `a=ssrc:${ssrc} ${name}`;
  702. if (value && value.length) {
  703. media += `:${value}`;
  704. }
  705. media += '\r\n';
  706. });
  707. });
  708. return media;
  709. };
  710. module.exports = SDP;