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.

SDP.js 26KB

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