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

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