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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. const ridLines = SDPUtil.findLines(this.media[i], 'a=rid');
  312. if (ridLines.length) {
  313. // Map a line which looks like "a=rid:2 send" to just
  314. // the rid ("2")
  315. const rids = ridLines
  316. .map(ridLine => ridLine.split(':')[1])
  317. .map(ridInfo => ridInfo.split(' ')[0]);
  318. rids.forEach(rid => {
  319. elem.c('source', {
  320. rid,
  321. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  322. });
  323. elem.up();
  324. });
  325. const unifiedSimulcast
  326. = SDPUtil.findLine(this.media[i], 'a=simulcast');
  327. if (unifiedSimulcast) {
  328. elem.c('rid-group', {
  329. semantics: 'SIM',
  330. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  331. });
  332. rids.forEach(rid => {
  333. elem.c('source', { rid }).up();
  334. });
  335. elem.up();
  336. }
  337. }
  338. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  339. elem.c('rtcp-mux').up();
  340. }
  341. // XEP-0293 -- map a=rtcp-fb:*
  342. this.rtcpFbToJingle(i, elem, '*');
  343. // XEP-0294
  344. lines = SDPUtil.findLines(this.media[i], 'a=extmap:');
  345. if (lines.length) {
  346. for (j = 0; j < lines.length; j++) {
  347. tmp = SDPUtil.parseExtmap(lines[j]);
  348. elem.c('rtp-hdrext', {
  349. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  350. uri: tmp.uri,
  351. id: tmp.value
  352. });
  353. // eslint-disable-next-line max-depth
  354. if (tmp.hasOwnProperty('direction')) {
  355. // eslint-disable-next-line max-depth
  356. switch (tmp.direction) {
  357. case 'sendonly':
  358. elem.attrs({ senders: 'responder' });
  359. break;
  360. case 'recvonly':
  361. elem.attrs({ senders: 'initiator' });
  362. break;
  363. case 'sendrecv':
  364. elem.attrs({ senders: 'both' });
  365. break;
  366. case 'inactive':
  367. elem.attrs({ senders: 'none' });
  368. break;
  369. }
  370. }
  371. // TODO: handle params
  372. elem.up();
  373. }
  374. }
  375. elem.up(); // end of description
  376. }
  377. // map ice-ufrag/pwd, dtls fingerprint, candidates
  378. this.transportToJingle(i, elem);
  379. const m = this.media[i];
  380. if (SDPUtil.findLine(m, 'a=sendrecv', this.session)) {
  381. elem.attrs({ senders: 'both' });
  382. } else if (SDPUtil.findLine(m, 'a=sendonly', this.session)) {
  383. elem.attrs({ senders: 'initiator' });
  384. } else if (SDPUtil.findLine(m, 'a=recvonly', this.session)) {
  385. elem.attrs({ senders: 'responder' });
  386. } else if (SDPUtil.findLine(m, 'a=inactive', this.session)) {
  387. elem.attrs({ senders: 'none' });
  388. }
  389. if (mline.port === '0') {
  390. // estos hack to reject an m-line
  391. elem.attrs({ senders: 'rejected' });
  392. }
  393. elem.up(); // end of content
  394. }
  395. elem.up();
  396. return elem;
  397. };
  398. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  399. let tmp;
  400. const self = this;
  401. elem.c('transport');
  402. // XEP-0343 DTLS/SCTP
  403. const sctpmap
  404. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', self.session);
  405. if (sctpmap) {
  406. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  407. elem.c('sctpmap', {
  408. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  409. number: sctpAttrs[0], /* SCTP port */
  410. protocol: sctpAttrs[1] /* protocol */
  411. });
  412. // Optional stream count attribute
  413. if (sctpAttrs.length > 2) {
  414. elem.attrs({ streams: sctpAttrs[2] });
  415. }
  416. elem.up();
  417. }
  418. // XEP-0320
  419. const fingerprints
  420. = SDPUtil.findLines(
  421. this.media[mediaindex],
  422. 'a=fingerprint:',
  423. this.session);
  424. fingerprints.forEach(line => {
  425. tmp = SDPUtil.parseFingerprint(line);
  426. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  427. elem.c('fingerprint').t(tmp.fingerprint);
  428. delete tmp.fingerprint;
  429. // eslint-disable-next-line no-param-reassign
  430. line
  431. = SDPUtil.findLine(
  432. self.media[mediaindex],
  433. 'a=setup:',
  434. self.session);
  435. if (line) {
  436. tmp.setup = line.substr(8);
  437. }
  438. elem.attrs(tmp);
  439. elem.up(); // end of fingerprint
  440. });
  441. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  442. if (tmp) {
  443. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  444. elem.attrs(tmp);
  445. // XEP-0176
  446. const lines
  447. = SDPUtil.findLines(
  448. this.media[mediaindex],
  449. 'a=candidate:',
  450. this.session);
  451. if (lines.length) { // add any a=candidate lines
  452. lines.forEach(line => {
  453. const candidate = SDPUtil.candidateToJingle(line);
  454. if (self.failICE) {
  455. candidate.ip = '1.1.1.1';
  456. }
  457. const protocol
  458. = candidate && typeof candidate.protocol === 'string'
  459. ? candidate.protocol.toLowerCase()
  460. : '';
  461. if ((self.removeTcpCandidates
  462. && (protocol === 'tcp' || protocol === 'ssltcp'))
  463. || (self.removeUdpCandidates && protocol === 'udp')) {
  464. return;
  465. }
  466. elem.c('candidate', candidate).up();
  467. });
  468. }
  469. }
  470. elem.up(); // end of transport
  471. };
  472. // XEP-0293
  473. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  474. const lines
  475. = SDPUtil.findLines(
  476. this.media[mediaindex],
  477. `a=rtcp-fb:${payloadtype}`);
  478. lines.forEach(line => {
  479. const tmp = SDPUtil.parseRTCPFB(line);
  480. if (tmp.type === 'trr-int') {
  481. elem.c('rtcp-fb-trr-int', {
  482. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  483. value: tmp.params[0]
  484. });
  485. elem.up();
  486. } else {
  487. elem.c('rtcp-fb', {
  488. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  489. type: tmp.type
  490. });
  491. if (tmp.params.length > 0) {
  492. elem.attrs({ 'subtype': tmp.params[0] });
  493. }
  494. elem.up();
  495. }
  496. });
  497. };
  498. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  499. let media = '';
  500. let tmp
  501. = elem.find(
  502. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  503. if (tmp.length) {
  504. media += 'a=rtcp-fb:* trr-int ';
  505. if (tmp.attr('value')) {
  506. media += tmp.attr('value');
  507. } else {
  508. media += '0';
  509. }
  510. media += '\r\n';
  511. }
  512. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  513. tmp.each(function() {
  514. /* eslint-disable no-invalid-this */
  515. media += `a=rtcp-fb:${payloadtype} ${$(this).attr('type')}`;
  516. if ($(this).attr('subtype')) {
  517. media += ` ${$(this).attr('subtype')}`;
  518. }
  519. media += '\r\n';
  520. /* eslint-enable no-invalid-this */
  521. });
  522. return media;
  523. };
  524. // construct an SDP from a jingle stanza
  525. SDP.prototype.fromJingle = function(jingle) {
  526. const self = this;
  527. this.raw = 'v=0\r\n'
  528. + 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n'// FIXME
  529. + 's=-\r\n'
  530. + 't=0 0\r\n';
  531. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  532. // #section-8
  533. const groups
  534. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  535. if (groups.length) {
  536. groups.each((idx, group) => {
  537. const contents
  538. = $(group)
  539. .find('>content')
  540. .map((_, content) => content.getAttribute('name'))
  541. .get();
  542. if (contents.length > 0) {
  543. self.raw
  544. += `a=group:${
  545. group.getAttribute('semantics')
  546. || group.getAttribute('type')} ${
  547. contents.join(' ')}\r\n`;
  548. }
  549. });
  550. }
  551. this.session = this.raw;
  552. jingle.find('>content').each(function() {
  553. // eslint-disable-next-line no-invalid-this
  554. const m = self.jingle2media($(this));
  555. self.media.push(m);
  556. });
  557. // reconstruct msid-semantic -- apparently not necessary
  558. /*
  559. var msid = SDPUtil.parseSSRC(this.raw);
  560. if (msid.hasOwnProperty('mslabel')) {
  561. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  562. }
  563. */
  564. this.raw = this.session + this.media.join('');
  565. };
  566. // translate a jingle content element into an an SDP media part
  567. SDP.prototype.jingle2media = function(content) {
  568. const desc = content.find('description');
  569. let media = '';
  570. const self = this;
  571. const sctp = content.find(
  572. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  573. let tmp = { media: desc.attr('media') };
  574. tmp.port = '1';
  575. if (content.attr('senders') === 'rejected') {
  576. // estos hack to reject an m-line.
  577. tmp.port = '0';
  578. }
  579. if (content.find('>transport>fingerprint').length
  580. || desc.find('encryption').length) {
  581. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  582. } else {
  583. tmp.proto = 'RTP/AVPF';
  584. }
  585. if (sctp.length) {
  586. media += `m=application 1 DTLS/SCTP ${sctp.attr('number')}\r\n`;
  587. media += `a=sctpmap:${sctp.attr('number')} ${sctp.attr('protocol')}`;
  588. const streamCount = sctp.attr('streams');
  589. if (streamCount) {
  590. media += ` ${streamCount}\r\n`;
  591. } else {
  592. media += '\r\n';
  593. }
  594. } else {
  595. tmp.fmt
  596. = desc
  597. .find('payload-type')
  598. .map(function() {
  599. // eslint-disable-next-line no-invalid-this
  600. return this.getAttribute('id');
  601. })
  602. .get();
  603. media += `${SDPUtil.buildMLine(tmp)}\r\n`;
  604. }
  605. media += 'c=IN IP4 0.0.0.0\r\n';
  606. if (!sctp.length) {
  607. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  608. }
  609. tmp
  610. = content.find(
  611. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  612. if (tmp.length) {
  613. if (tmp.attr('ufrag')) {
  614. media += `${SDPUtil.buildICEUfrag(tmp.attr('ufrag'))}\r\n`;
  615. }
  616. if (tmp.attr('pwd')) {
  617. media += `${SDPUtil.buildICEPwd(tmp.attr('pwd'))}\r\n`;
  618. }
  619. tmp.find('>fingerprint').each(function() {
  620. /* eslint-disable no-invalid-this */
  621. // FIXME: check namespace at some point
  622. media += `a=fingerprint:${this.getAttribute('hash')}`;
  623. media += ` ${$(this).text()}`;
  624. media += '\r\n';
  625. if (this.getAttribute('setup')) {
  626. media += `a=setup:${this.getAttribute('setup')}\r\n`;
  627. }
  628. /* eslint-enable no-invalid-this */
  629. });
  630. }
  631. switch (content.attr('senders')) {
  632. case 'initiator':
  633. media += 'a=sendonly\r\n';
  634. break;
  635. case 'responder':
  636. media += 'a=recvonly\r\n';
  637. break;
  638. case 'none':
  639. media += 'a=inactive\r\n';
  640. break;
  641. case 'both':
  642. media += 'a=sendrecv\r\n';
  643. break;
  644. }
  645. media += `a=mid:${content.attr('name')}\r\n`;
  646. // <description><rtcp-mux/></description>
  647. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  648. // though
  649. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  650. if (desc.find('rtcp-mux').length) {
  651. media += 'a=rtcp-mux\r\n';
  652. }
  653. if (desc.find('encryption').length) {
  654. desc.find('encryption>crypto').each(function() {
  655. /* eslint-disable no-invalid-this */
  656. media += `a=crypto:${this.getAttribute('tag')}`;
  657. media += ` ${this.getAttribute('crypto-suite')}`;
  658. media += ` ${this.getAttribute('key-params')}`;
  659. if (this.getAttribute('session-params')) {
  660. media += ` ${this.getAttribute('session-params')}`;
  661. }
  662. media += '\r\n';
  663. /* eslint-enable no-invalid-this */
  664. });
  665. }
  666. desc.find('payload-type').each(function() {
  667. /* eslint-disable no-invalid-this */
  668. media += `${SDPUtil.buildRTPMap(this)}\r\n`;
  669. if ($(this).find('>parameter').length) {
  670. media += `a=fmtp:${this.getAttribute('id')} `;
  671. media
  672. += $(this)
  673. .find('parameter')
  674. .map(function() {
  675. return (this.getAttribute('name')
  676. ? `${this.getAttribute('name')}=` : '')
  677. + this.getAttribute('value');
  678. })
  679. .get()
  680. .join('; ');
  681. media += '\r\n';
  682. }
  683. // xep-0293
  684. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  685. /* eslint-enable no-invalid-this */
  686. });
  687. // xep-0293
  688. media += self.rtcpFbFromJingle(desc, '*');
  689. // xep-0294
  690. tmp
  691. = desc.find(
  692. '>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  693. tmp.each(function() {
  694. /* eslint-disable no-invalid-this */
  695. media
  696. += `a=extmap:${this.getAttribute('id')} ${
  697. this.getAttribute('uri')}\r\n`;
  698. /* eslint-enable no-invalid-this */
  699. });
  700. content
  701. .find(
  702. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]'
  703. + '>candidate')
  704. .each(function() {
  705. /* eslint-disable no-invalid-this */
  706. let protocol = this.getAttribute('protocol');
  707. protocol
  708. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  709. if ((self.removeTcpCandidates
  710. && (protocol === 'tcp' || protocol === 'ssltcp'))
  711. || (self.removeUdpCandidates && protocol === 'udp')) {
  712. return;
  713. } else if (self.failICE) {
  714. this.setAttribute('ip', '1.1.1.1');
  715. }
  716. media += SDPUtil.candidateFromJingle(this);
  717. /* eslint-enable no-invalid-this */
  718. });
  719. // XEP-0339 handle ssrc-group attributes
  720. content
  721. .find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  722. .each(function() {
  723. /* eslint-disable no-invalid-this */
  724. const semantics = this.getAttribute('semantics');
  725. const ssrcs
  726. = $(this)
  727. .find('>source')
  728. .map(function() {
  729. return this.getAttribute('ssrc');
  730. })
  731. .get();
  732. if (ssrcs.length) {
  733. media += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  734. }
  735. /* eslint-enable no-invalid-this */
  736. });
  737. tmp
  738. = content.find(
  739. 'description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  740. tmp.each(function() {
  741. /* eslint-disable no-invalid-this */
  742. const ssrc = this.getAttribute('ssrc');
  743. // eslint-disable-next-line newline-per-chained-call
  744. $(this).find('>parameter').each(function() {
  745. const name = this.getAttribute('name');
  746. let value = this.getAttribute('value');
  747. value = SDPUtil.filterSpecialChars(value);
  748. media += `a=ssrc:${ssrc} ${name}`;
  749. if (value && value.length) {
  750. media += `:${value}`;
  751. }
  752. media += '\r\n';
  753. });
  754. /* eslint-enable no-invalid-this */
  755. });
  756. return media;
  757. };