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 27KB

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