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.

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