您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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