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

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