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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /* global $ */
  2. import browser from '../browser';
  3. import SDPUtil from './SDPUtil';
  4. /**
  5. *
  6. * @param sdp
  7. */
  8. export default function SDP(sdp) {
  9. const media = sdp.split('\r\nm=');
  10. for (let i = 1, length = media.length; i < length; i++) {
  11. let mediaI = `m=${media[i]}`;
  12. if (i !== length - 1) {
  13. mediaI += '\r\n';
  14. }
  15. media[i] = mediaI;
  16. }
  17. const session = `${media.shift()}\r\n`;
  18. this.media = media;
  19. this.raw = session + media.join('');
  20. this.session = session;
  21. }
  22. /**
  23. * A flag will make {@link transportToJingle} and {@link jingle2media} replace
  24. * ICE candidates IPs with invalid value of '1.1.1.1' which will cause ICE
  25. * failure. The flag is used in the automated testing.
  26. * @type {boolean}
  27. */
  28. SDP.prototype.failICE = false;
  29. /**
  30. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  31. * @type {boolean}
  32. */
  33. SDP.prototype.removeTcpCandidates = false;
  34. /**
  35. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  36. * @type {boolean}
  37. */
  38. SDP.prototype.removeUdpCandidates = false;
  39. /**
  40. * Returns map of MediaChannel mapped per channel idx.
  41. */
  42. SDP.prototype.getMediaSsrcMap = function() {
  43. const self = this;
  44. const mediaSSRCs = {};
  45. let tmp;
  46. for (let mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
  47. tmp = SDPUtil.findLines(self.media[mediaindex], 'a=ssrc:');
  48. const mid
  49. = SDPUtil.parseMID(
  50. SDPUtil.findLine(self.media[mediaindex], '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. const ssrcMap = SDPUtil.parseSSRC(this.media[i]);
  229. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  230. elem.c('source', {
  231. ssrc: availableSsrc,
  232. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  233. });
  234. ssrcParameters.forEach(ssrcSdpLine => {
  235. // get everything after first space
  236. const idx = ssrcSdpLine.indexOf(' ');
  237. const kv = ssrcSdpLine.substr(idx + 1);
  238. elem.c('parameter');
  239. if (kv.indexOf(':') === -1) {
  240. elem.attrs({ name: kv });
  241. } else {
  242. const name = kv.split(':', 2)[0];
  243. elem.attrs({ name });
  244. let v = kv.split(':', 2)[1];
  245. v = SDPUtil.filterSpecialChars(v);
  246. elem.attrs({ value: v });
  247. }
  248. elem.up();
  249. });
  250. elem.up();
  251. }
  252. // XEP-0339 handle ssrc-group attributes
  253. const ssrcGroupLines
  254. = SDPUtil.findLines(this.media[i], 'a=ssrc-group:');
  255. ssrcGroupLines.forEach(line => {
  256. const idx = line.indexOf(' ');
  257. const semantics = line.substr(0, idx).substr(13);
  258. const ssrcs = line.substr(14 + semantics.length).split(' ');
  259. if (ssrcs.length) {
  260. elem.c('ssrc-group', { semantics,
  261. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  262. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  263. elem.up();
  264. }
  265. });
  266. }
  267. const ridLines = SDPUtil.findLines(this.media[i], 'a=rid');
  268. if (ridLines.length && browser.usesRidsForSimulcast()) {
  269. // Map a line which looks like "a=rid:2 send" to just
  270. // the rid ("2")
  271. const rids = ridLines
  272. .map(ridLine => ridLine.split(':')[1])
  273. .map(ridInfo => ridInfo.split(' ')[0]);
  274. rids.forEach(rid => {
  275. elem.c('source', {
  276. rid,
  277. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  278. });
  279. elem.up();
  280. });
  281. const unifiedSimulcast
  282. = SDPUtil.findLine(this.media[i], 'a=simulcast');
  283. if (unifiedSimulcast) {
  284. elem.c('rid-group', {
  285. semantics: 'SIM',
  286. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  287. });
  288. rids.forEach(rid => {
  289. elem.c('source', { rid }).up();
  290. });
  291. elem.up();
  292. }
  293. }
  294. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  295. elem.c('rtcp-mux').up();
  296. }
  297. // XEP-0293 -- map a=rtcp-fb:*
  298. this.rtcpFbToJingle(i, elem, '*');
  299. // XEP-0294
  300. lines = SDPUtil.findLines(this.media[i], 'a=extmap:');
  301. if (lines.length) {
  302. for (j = 0; j < lines.length; j++) {
  303. tmp = SDPUtil.parseExtmap(lines[j]);
  304. elem.c('rtp-hdrext', {
  305. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  306. uri: tmp.uri,
  307. id: tmp.value
  308. });
  309. // eslint-disable-next-line max-depth
  310. if (tmp.hasOwnProperty('direction')) {
  311. // eslint-disable-next-line max-depth
  312. switch (tmp.direction) {
  313. case 'sendonly':
  314. elem.attrs({ senders: 'responder' });
  315. break;
  316. case 'recvonly':
  317. elem.attrs({ senders: 'initiator' });
  318. break;
  319. case 'sendrecv':
  320. elem.attrs({ senders: 'both' });
  321. break;
  322. case 'inactive':
  323. elem.attrs({ senders: 'none' });
  324. break;
  325. }
  326. }
  327. // TODO: handle params
  328. elem.up();
  329. }
  330. }
  331. elem.up(); // end of description
  332. }
  333. // map ice-ufrag/pwd, dtls fingerprint, candidates
  334. this.transportToJingle(i, elem);
  335. const m = this.media[i];
  336. if (SDPUtil.findLine(m, 'a=sendrecv', this.session)) {
  337. elem.attrs({ senders: 'both' });
  338. } else if (SDPUtil.findLine(m, 'a=sendonly', this.session)) {
  339. elem.attrs({ senders: 'initiator' });
  340. } else if (SDPUtil.findLine(m, 'a=recvonly', this.session)) {
  341. elem.attrs({ senders: 'responder' });
  342. } else if (SDPUtil.findLine(m, 'a=inactive', this.session)) {
  343. elem.attrs({ senders: 'none' });
  344. }
  345. // Reject an m-line only when port is 0 and a=bundle-only is not present in the section.
  346. // The port is automatically set to 0 when bundle-only is used.
  347. if (mline.port === '0' && !SDPUtil.findLine(m, 'a=bundle-only', this.session)) {
  348. // estos hack to reject an m-line
  349. elem.attrs({ senders: 'rejected' });
  350. }
  351. elem.up(); // end of content
  352. }
  353. elem.up();
  354. return elem;
  355. };
  356. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  357. let tmp;
  358. const self = this;
  359. elem.c('transport');
  360. // XEP-0343 DTLS/SCTP
  361. const sctpmap
  362. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', self.session);
  363. if (sctpmap) {
  364. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  365. elem.c('sctpmap', {
  366. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  367. number: sctpAttrs[0], /* SCTP port */
  368. protocol: sctpAttrs[1] /* protocol */
  369. });
  370. // Optional stream count attribute
  371. if (sctpAttrs.length > 2) {
  372. elem.attrs({ streams: sctpAttrs[2] });
  373. }
  374. elem.up();
  375. }
  376. // XEP-0320
  377. const fingerprints
  378. = SDPUtil.findLines(
  379. this.media[mediaindex],
  380. 'a=fingerprint:',
  381. this.session);
  382. fingerprints.forEach(line => {
  383. tmp = SDPUtil.parseFingerprint(line);
  384. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  385. elem.c('fingerprint').t(tmp.fingerprint);
  386. delete tmp.fingerprint;
  387. // eslint-disable-next-line no-param-reassign
  388. line
  389. = SDPUtil.findLine(
  390. self.media[mediaindex],
  391. 'a=setup:',
  392. self.session);
  393. if (line) {
  394. tmp.setup = line.substr(8);
  395. }
  396. elem.attrs(tmp);
  397. elem.up(); // end of fingerprint
  398. });
  399. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  400. if (tmp) {
  401. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  402. elem.attrs(tmp);
  403. // XEP-0176
  404. const lines
  405. = SDPUtil.findLines(
  406. this.media[mediaindex],
  407. 'a=candidate:',
  408. this.session);
  409. if (lines.length) { // add any a=candidate lines
  410. lines.forEach(line => {
  411. const candidate = SDPUtil.candidateToJingle(line);
  412. if (self.failICE) {
  413. candidate.ip = '1.1.1.1';
  414. }
  415. const protocol
  416. = candidate && typeof candidate.protocol === 'string'
  417. ? candidate.protocol.toLowerCase()
  418. : '';
  419. if ((self.removeTcpCandidates
  420. && (protocol === 'tcp' || protocol === 'ssltcp'))
  421. || (self.removeUdpCandidates && protocol === 'udp')) {
  422. return;
  423. }
  424. elem.c('candidate', candidate).up();
  425. });
  426. }
  427. }
  428. elem.up(); // end of transport
  429. };
  430. // XEP-0293
  431. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  432. const lines
  433. = SDPUtil.findLines(
  434. this.media[mediaindex],
  435. `a=rtcp-fb:${payloadtype}`);
  436. lines.forEach(line => {
  437. const tmp = SDPUtil.parseRTCPFB(line);
  438. if (tmp.type === 'trr-int') {
  439. elem.c('rtcp-fb-trr-int', {
  440. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  441. value: tmp.params[0]
  442. });
  443. elem.up();
  444. } else {
  445. elem.c('rtcp-fb', {
  446. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  447. type: tmp.type
  448. });
  449. if (tmp.params.length > 0) {
  450. elem.attrs({ 'subtype': tmp.params[0] });
  451. }
  452. elem.up();
  453. }
  454. });
  455. };
  456. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  457. let media = '';
  458. let tmp
  459. = elem.find(
  460. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  461. if (tmp.length) {
  462. media += 'a=rtcp-fb:* trr-int ';
  463. if (tmp.attr('value')) {
  464. media += tmp.attr('value');
  465. } else {
  466. media += '0';
  467. }
  468. media += '\r\n';
  469. }
  470. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  471. tmp.each(function() {
  472. /* eslint-disable no-invalid-this */
  473. media += `a=rtcp-fb:${payloadtype} ${$(this).attr('type')}`;
  474. if ($(this).attr('subtype')) {
  475. media += ` ${$(this).attr('subtype')}`;
  476. }
  477. media += '\r\n';
  478. /* eslint-enable no-invalid-this */
  479. });
  480. return media;
  481. };
  482. // construct an SDP from a jingle stanza
  483. SDP.prototype.fromJingle = function(jingle) {
  484. const self = this;
  485. const sessionId = Date.now();
  486. // Use a unique session id for every TPC.
  487. this.raw = 'v=0\r\n'
  488. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  489. + 's=-\r\n'
  490. + 't=0 0\r\n';
  491. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  492. // #section-8
  493. const groups
  494. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  495. if (groups.length) {
  496. groups.each((idx, group) => {
  497. const contents
  498. = $(group)
  499. .find('>content')
  500. .map((_, content) => content.getAttribute('name'))
  501. .get();
  502. if (contents.length > 0) {
  503. self.raw
  504. += `a=group:${
  505. group.getAttribute('semantics')
  506. || group.getAttribute('type')} ${
  507. contents.join(' ')}\r\n`;
  508. }
  509. });
  510. }
  511. this.session = this.raw;
  512. jingle.find('>content').each(function() {
  513. // eslint-disable-next-line no-invalid-this
  514. const m = self.jingle2media($(this));
  515. self.media.push(m);
  516. });
  517. // reconstruct msid-semantic -- apparently not necessary
  518. /*
  519. var msid = SDPUtil.parseSSRC(this.raw);
  520. if (msid.hasOwnProperty('mslabel')) {
  521. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  522. }
  523. */
  524. this.raw = this.session + this.media.join('');
  525. };
  526. // translate a jingle content element into an an SDP media part
  527. SDP.prototype.jingle2media = function(content) {
  528. const desc = content.find('description');
  529. let media = '';
  530. const self = this;
  531. const sctp = content.find(
  532. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  533. let tmp = { media: desc.attr('media') };
  534. tmp.port = '1';
  535. if (content.attr('senders') === 'rejected') {
  536. // estos hack to reject an m-line.
  537. tmp.port = '0';
  538. }
  539. if (content.find('>transport>fingerprint').length
  540. || desc.find('encryption').length) {
  541. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  542. } else {
  543. tmp.proto = 'RTP/AVPF';
  544. }
  545. if (sctp.length) {
  546. media += `m=application ${tmp.port} DTLS/SCTP ${
  547. sctp.attr('number')}\r\n`;
  548. media += `a=sctpmap:${sctp.attr('number')} ${sctp.attr('protocol')}`;
  549. const streamCount = sctp.attr('streams');
  550. if (streamCount) {
  551. media += ` ${streamCount}\r\n`;
  552. } else {
  553. media += '\r\n';
  554. }
  555. } else {
  556. tmp.fmt
  557. = desc
  558. .find('payload-type')
  559. .map(function() {
  560. // eslint-disable-next-line no-invalid-this
  561. return this.getAttribute('id');
  562. })
  563. .get();
  564. media += `${SDPUtil.buildMLine(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.buildICEUfrag(tmp.attr('ufrag'))}\r\n`;
  576. }
  577. if (tmp.attr('pwd')) {
  578. media += `${SDPUtil.buildICEPwd(tmp.attr('pwd'))}\r\n`;
  579. }
  580. tmp.find('>fingerprint').each(function() {
  581. /* eslint-disable no-invalid-this */
  582. // FIXME: check namespace at some point
  583. media += `a=fingerprint:${this.getAttribute('hash')}`;
  584. media += ` ${$(this).text()}`;
  585. media += '\r\n';
  586. if (this.getAttribute('setup')) {
  587. media += `a=setup:${this.getAttribute('setup')}\r\n`;
  588. }
  589. /* eslint-enable no-invalid-this */
  590. });
  591. }
  592. switch (content.attr('senders')) {
  593. case 'initiator':
  594. media += 'a=sendonly\r\n';
  595. break;
  596. case 'responder':
  597. media += 'a=recvonly\r\n';
  598. break;
  599. case 'none':
  600. media += 'a=inactive\r\n';
  601. break;
  602. case 'both':
  603. media += 'a=sendrecv\r\n';
  604. break;
  605. }
  606. media += `a=mid:${content.attr('name')}\r\n`;
  607. // <description><rtcp-mux/></description>
  608. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  609. // though
  610. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  611. if (desc.find('rtcp-mux').length) {
  612. media += 'a=rtcp-mux\r\n';
  613. }
  614. if (desc.find('encryption').length) {
  615. desc.find('encryption>crypto').each(function() {
  616. /* eslint-disable no-invalid-this */
  617. media += `a=crypto:${this.getAttribute('tag')}`;
  618. media += ` ${this.getAttribute('crypto-suite')}`;
  619. media += ` ${this.getAttribute('key-params')}`;
  620. if (this.getAttribute('session-params')) {
  621. media += ` ${this.getAttribute('session-params')}`;
  622. }
  623. media += '\r\n';
  624. /* eslint-enable no-invalid-this */
  625. });
  626. }
  627. desc.find('payload-type').each(function() {
  628. /* eslint-disable no-invalid-this */
  629. media += `${SDPUtil.buildRTPMap(this)}\r\n`;
  630. if ($(this).find('>parameter').length) {
  631. media += `a=fmtp:${this.getAttribute('id')} `;
  632. media
  633. += $(this)
  634. .find('parameter')
  635. .map(function() {
  636. const name = this.getAttribute('name');
  637. return (
  638. (name ? `${name}=` : '')
  639. + this.getAttribute('value'));
  640. })
  641. .get()
  642. .join('; ');
  643. media += '\r\n';
  644. }
  645. // xep-0293
  646. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  647. /* eslint-enable no-invalid-this */
  648. });
  649. // xep-0293
  650. media += self.rtcpFbFromJingle(desc, '*');
  651. // xep-0294
  652. tmp
  653. = desc.find(
  654. '>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  655. tmp.each(function() {
  656. /* eslint-disable no-invalid-this */
  657. media
  658. += `a=extmap:${this.getAttribute('id')} ${
  659. this.getAttribute('uri')}\r\n`;
  660. /* eslint-enable no-invalid-this */
  661. });
  662. content
  663. .find(
  664. '>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]'
  665. + '>candidate')
  666. .each(function() {
  667. /* eslint-disable no-invalid-this */
  668. let protocol = this.getAttribute('protocol');
  669. protocol
  670. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  671. if ((self.removeTcpCandidates
  672. && (protocol === 'tcp' || protocol === 'ssltcp'))
  673. || (self.removeUdpCandidates && protocol === 'udp')) {
  674. return;
  675. } else if (self.failICE) {
  676. this.setAttribute('ip', '1.1.1.1');
  677. }
  678. media += SDPUtil.candidateFromJingle(this);
  679. /* eslint-enable no-invalid-this */
  680. });
  681. // XEP-0339 handle ssrc-group attributes
  682. content
  683. .find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  684. .each(function() {
  685. /* eslint-disable no-invalid-this */
  686. const semantics = this.getAttribute('semantics');
  687. const ssrcs
  688. = $(this)
  689. .find('>source')
  690. .map(function() {
  691. return this.getAttribute('ssrc');
  692. })
  693. .get();
  694. if (ssrcs.length) {
  695. media += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  696. }
  697. /* eslint-enable no-invalid-this */
  698. });
  699. tmp
  700. = content.find(
  701. 'description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  702. tmp.each(function() {
  703. /* eslint-disable no-invalid-this */
  704. const ssrc = this.getAttribute('ssrc');
  705. // eslint-disable-next-line newline-per-chained-call
  706. $(this).find('>parameter').each(function() {
  707. const name = this.getAttribute('name');
  708. let value = this.getAttribute('value');
  709. value = SDPUtil.filterSpecialChars(value);
  710. media += `a=ssrc:${ssrc} ${name}`;
  711. if (value && value.length) {
  712. media += `:${value}`;
  713. }
  714. media += '\r\n';
  715. });
  716. /* eslint-enable no-invalid-this */
  717. });
  718. return media;
  719. };