Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SDP.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. import $ from 'jquery';
  2. import clonedeep from 'lodash.clonedeep';
  3. import transform from 'sdp-transform';
  4. import { MediaDirection } from '../../service/RTC/MediaDirection';
  5. import browser from '../browser';
  6. import SDPUtil from './SDPUtil';
  7. /**
  8. *
  9. * @param sdp
  10. */
  11. export default function SDP(sdp) {
  12. const media = sdp.split('\r\nm=');
  13. for (let i = 1, length = media.length; i < length; i++) {
  14. let mediaI = `m=${media[i]}`;
  15. if (i !== length - 1) {
  16. mediaI += '\r\n';
  17. }
  18. media[i] = mediaI;
  19. }
  20. const session = `${media.shift()}\r\n`;
  21. this.media = media;
  22. this.raw = session + media.join('');
  23. this.session = session;
  24. }
  25. /**
  26. * A flag will make {@link transportToJingle} and {@link jingle2media} replace
  27. * ICE candidates IPs with invalid value of '1.1.1.1' which will cause ICE
  28. * failure. The flag is used in the automated testing.
  29. * @type {boolean}
  30. */
  31. SDP.prototype.failICE = false;
  32. /**
  33. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  34. * @type {boolean}
  35. */
  36. SDP.prototype.removeTcpCandidates = false;
  37. /**
  38. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  39. * @type {boolean}
  40. */
  41. SDP.prototype.removeUdpCandidates = false;
  42. /**
  43. * Adds a new m-line to the description so that a new local source can then be attached to the transceiver that gets
  44. * added after a reneogtiation cycle.
  45. *
  46. * @param {MediaType} mediaType media type of the new source that is being added.
  47. */
  48. SDP.prototype.addMlineForNewLocalSource = function(mediaType) {
  49. const mid = this.media.length;
  50. const sdp = transform.parse(this.raw);
  51. const mline = clonedeep(sdp.media.find(m => m.type === mediaType));
  52. // Edit media direction, mid and remove the existing ssrc lines in the m-line.
  53. mline.mid = mid;
  54. mline.direction = MediaDirection.RECVONLY;
  55. // Remove the ssrcs and source groups.
  56. mline.msid = undefined;
  57. mline.ssrcs = undefined;
  58. mline.ssrcGroups = undefined;
  59. sdp.media = sdp.media.concat(mline);
  60. // We regenerate the BUNDLE group (since we added a new m-line)
  61. sdp.groups.forEach(group => {
  62. if (group.type === 'BUNDLE') {
  63. const mids = group.mids.split(' ');
  64. mids.push(mid);
  65. group.mids = mids.join(' ');
  66. }
  67. });
  68. this.raw = transform.write(sdp);
  69. };
  70. /**
  71. * Returns map of MediaChannel mapped per channel idx.
  72. */
  73. SDP.prototype.getMediaSsrcMap = function() {
  74. const mediaSSRCs = {};
  75. for (let mediaindex = 0; mediaindex < this.media.length; mediaindex++) {
  76. const mid
  77. = SDPUtil.parseMID(
  78. SDPUtil.findLine(this.media[mediaindex], 'a=mid:'));
  79. const media = {
  80. mediaindex,
  81. mid,
  82. ssrcs: {},
  83. ssrcGroups: []
  84. };
  85. mediaSSRCs[mediaindex] = media;
  86. SDPUtil.findLines(this.media[mediaindex], 'a=ssrc:').forEach(line => {
  87. const linessrc = line.substring(7).split(' ')[0];
  88. // allocate new ChannelSsrc
  89. if (!media.ssrcs[linessrc]) {
  90. media.ssrcs[linessrc] = {
  91. ssrc: linessrc,
  92. lines: []
  93. };
  94. }
  95. media.ssrcs[linessrc].lines.push(line);
  96. });
  97. SDPUtil.findLines(this.media[mediaindex], 'a=ssrc-group:').forEach(line => {
  98. const idx = line.indexOf(' ');
  99. const semantics = line.substr(0, idx).substr(13);
  100. const ssrcs = line.substr(14 + semantics.length).split(' ');
  101. if (ssrcs.length) {
  102. media.ssrcGroups.push({
  103. semantics,
  104. ssrcs
  105. });
  106. }
  107. });
  108. }
  109. return mediaSSRCs;
  110. };
  111. /**
  112. * Returns <tt>true</tt> if this SDP contains given SSRC.
  113. * @param ssrc the ssrc to check.
  114. * @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
  115. */
  116. SDP.prototype.containsSSRC = function(ssrc) {
  117. // FIXME this code is really strange - improve it if you can
  118. const medias = this.getMediaSsrcMap();
  119. let result = false;
  120. Object.keys(medias).forEach(mediaindex => {
  121. if (result) {
  122. return;
  123. }
  124. if (medias[mediaindex].ssrcs[ssrc]) {
  125. result = true;
  126. }
  127. });
  128. return result;
  129. };
  130. // add content's to a jingle element
  131. SDP.prototype.toJingle = function(elem, thecreator) {
  132. // https://xmpp.org/extensions/xep-0338.html
  133. SDPUtil.findLines(this.session, 'a=group:').forEach(line => {
  134. const parts = line.split(' ');
  135. const semantics = parts.shift().substr(8);
  136. elem.c('group', { xmlns: 'urn:xmpp:jingle:apps:grouping:0',
  137. semantics });
  138. for (let j = 0; j < parts.length; j++) {
  139. elem.c('content', { name: parts[j] }).up();
  140. }
  141. elem.up();
  142. });
  143. for (let i = 0; i < this.media.length; i++) {
  144. const mline = SDPUtil.parseMLine(this.media[i].split('\r\n')[0]);
  145. if (!(mline.media === 'audio'
  146. || mline.media === 'video'
  147. || mline.media === 'application')) {
  148. continue; // eslint-disable-line no-continue
  149. }
  150. let ssrc;
  151. const assrcline = SDPUtil.findLine(this.media[i], 'a=ssrc:');
  152. if (assrcline) {
  153. ssrc = assrcline.substring(7).split(' ')[0]; // take the first
  154. } else {
  155. ssrc = false;
  156. }
  157. elem.c('content', { creator: thecreator,
  158. name: mline.media });
  159. const amidline = SDPUtil.findLine(this.media[i], 'a=mid:');
  160. if (amidline) {
  161. // prefer identifier from a=mid if present
  162. const mid = SDPUtil.parseMID(amidline);
  163. elem.attrs({ name: mid });
  164. }
  165. if (mline.media === 'video' && typeof this.initialLastN === 'number') {
  166. elem.c('initial-last-n',
  167. { xmlns: 'jitsi:colibri2',
  168. value: this.initialLastN }).up();
  169. }
  170. if (mline.media === 'audio' || mline.media === 'video') {
  171. elem.c('description',
  172. { xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  173. media: mline.media });
  174. if (ssrc) {
  175. elem.attrs({ ssrc });
  176. }
  177. for (let j = 0; j < mline.fmt.length; j++) {
  178. const rtpmap
  179. = SDPUtil.findLine(
  180. this.media[i],
  181. `a=rtpmap:${mline.fmt[j]}`);
  182. elem.c('payload-type', SDPUtil.parseRTPMap(rtpmap));
  183. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo
  184. // value=bar/>
  185. const afmtpline
  186. = SDPUtil.findLine(
  187. this.media[i],
  188. `a=fmtp:${mline.fmt[j]}`);
  189. if (afmtpline) {
  190. const fmtpParameters = SDPUtil.parseFmtp(afmtpline);
  191. // eslint-disable-next-line max-depth
  192. for (let k = 0; k < fmtpParameters.length; k++) {
  193. elem.c('parameter', fmtpParameters[k]).up();
  194. }
  195. }
  196. // XEP-0293 -- map a=rtcp-fb
  197. this.rtcpFbToJingle(i, elem, mline.fmt[j]);
  198. elem.up();
  199. }
  200. if (ssrc) {
  201. const ssrcMap = SDPUtil.parseSSRC(this.media[i]);
  202. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  203. const sourceName = SDPUtil.parseSourceNameLine(ssrcParameters);
  204. const videoType = SDPUtil.parseVideoTypeLine(ssrcParameters);
  205. elem.c('source', {
  206. ssrc: availableSsrc,
  207. name: sourceName,
  208. videoType,
  209. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  210. });
  211. const msid = SDPUtil.parseMSIDAttribute(ssrcParameters);
  212. // eslint-disable-next-line max-depth
  213. if (msid) {
  214. elem.c('parameter');
  215. elem.attrs({ name: 'msid' });
  216. elem.attrs({ value: msid });
  217. elem.up();
  218. }
  219. elem.up();
  220. }
  221. // XEP-0339 handle ssrc-group attributes
  222. const ssrcGroupLines
  223. = SDPUtil.findLines(this.media[i], 'a=ssrc-group:');
  224. ssrcGroupLines.forEach(line => {
  225. const idx = line.indexOf(' ');
  226. const semantics = line.substr(0, idx).substr(13);
  227. const ssrcs = line.substr(14 + semantics.length).split(' ');
  228. if (ssrcs.length) {
  229. elem.c('ssrc-group', { semantics,
  230. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  231. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  232. elem.up();
  233. }
  234. });
  235. }
  236. const ridLines = SDPUtil.findLines(this.media[i], 'a=rid:');
  237. if (ridLines.length && browser.usesRidsForSimulcast()) {
  238. // Map a line which looks like "a=rid:2 send" to just
  239. // the rid ("2")
  240. const rids = ridLines
  241. .map(ridLine => ridLine.split(':')[1])
  242. .map(ridInfo => ridInfo.split(' ')[0]);
  243. rids.forEach(rid => {
  244. elem.c('source', {
  245. rid,
  246. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  247. });
  248. elem.up();
  249. });
  250. const unifiedSimulcast
  251. = SDPUtil.findLine(this.media[i], 'a=simulcast:');
  252. if (unifiedSimulcast) {
  253. elem.c('rid-group', {
  254. semantics: 'SIM',
  255. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  256. });
  257. rids.forEach(rid => {
  258. elem.c('source', { rid }).up();
  259. });
  260. elem.up();
  261. }
  262. }
  263. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  264. elem.c('rtcp-mux').up();
  265. }
  266. // XEP-0293 -- map a=rtcp-fb:*
  267. this.rtcpFbToJingle(i, elem, '*');
  268. // XEP-0294
  269. const extmapLines = SDPUtil.findLines(this.media[i], 'a=extmap:', this.session);
  270. for (let j = 0; j < extmapLines.length; j++) {
  271. const extmap = SDPUtil.parseExtmap(extmapLines[j]);
  272. elem.c('rtp-hdrext', {
  273. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  274. uri: extmap.uri,
  275. id: extmap.value
  276. });
  277. // eslint-disable-next-line max-depth
  278. if (extmap.hasOwnProperty('direction')) {
  279. // eslint-disable-next-line max-depth
  280. switch (extmap.direction) {
  281. case MediaDirection.SENDONLY:
  282. elem.attrs({ senders: 'responder' });
  283. break;
  284. case MediaDirection.RECVONLY:
  285. elem.attrs({ senders: 'initiator' });
  286. break;
  287. case MediaDirection.SENDRECV:
  288. elem.attrs({ senders: 'both' });
  289. break;
  290. case MediaDirection.INACTIVE:
  291. elem.attrs({ senders: 'none' });
  292. break;
  293. }
  294. }
  295. // TODO: handle params
  296. elem.up();
  297. }
  298. if (SDPUtil.findLine(this.media[i], 'a=extmap-allow-mixed', this.session)) {
  299. elem.c('extmap-allow-mixed', {
  300. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0'
  301. });
  302. elem.up();
  303. }
  304. elem.up(); // end of description
  305. }
  306. // map ice-ufrag/pwd, dtls fingerprint, candidates
  307. this.transportToJingle(i, elem);
  308. const m = this.media[i];
  309. if (SDPUtil.findLine(m, `a=${MediaDirection.SENDRECV}`, this.session)) {
  310. elem.attrs({ senders: 'both' });
  311. } else if (SDPUtil.findLine(m, `a=${MediaDirection.SENDONLY}`, this.session)) {
  312. elem.attrs({ senders: 'initiator' });
  313. } else if (SDPUtil.findLine(m, `a=${MediaDirection.RECVONLY}`, this.session)) {
  314. elem.attrs({ senders: 'responder' });
  315. } else if (SDPUtil.findLine(m, `a=${MediaDirection.INACTIVE}`, this.session)) {
  316. elem.attrs({ senders: 'none' });
  317. }
  318. // Reject an m-line only when port is 0 and a=bundle-only is not present in the section.
  319. // The port is automatically set to 0 when bundle-only is used.
  320. if (mline.port === '0' && !SDPUtil.findLine(m, 'a=bundle-only', this.session)) {
  321. // estos hack to reject an m-line
  322. elem.attrs({ senders: 'rejected' });
  323. }
  324. elem.up(); // end of content
  325. }
  326. elem.up();
  327. return elem;
  328. };
  329. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  330. elem.c('transport');
  331. // XEP-0343 DTLS/SCTP
  332. const sctpport
  333. = SDPUtil.findLine(this.media[mediaindex], 'a=sctp-port:', this.session);
  334. const sctpmap
  335. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', this.session);
  336. if (sctpport) {
  337. const sctpAttrs = SDPUtil.parseSCTPPort(sctpport);
  338. elem.c('sctpmap', {
  339. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  340. number: sctpAttrs, /* SCTP port */
  341. protocol: 'webrtc-datachannel' /* protocol */
  342. });
  343. // The parser currently requires streams to be present
  344. elem.attrs({ streams: 0 });
  345. elem.up();
  346. } else if (sctpmap) {
  347. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  348. elem.c('sctpmap', {
  349. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  350. number: sctpAttrs[0], /* SCTP port */
  351. protocol: sctpAttrs[1] /* protocol */
  352. });
  353. // Optional stream count attribute
  354. if (sctpAttrs.length > 2) {
  355. elem.attrs({ streams: sctpAttrs[2] });
  356. } else {
  357. elem.attrs({ streams: 0 });
  358. }
  359. elem.up();
  360. }
  361. // XEP-0320
  362. const fingerprints
  363. = SDPUtil.findLines(
  364. this.media[mediaindex],
  365. 'a=fingerprint:',
  366. this.session);
  367. fingerprints.forEach(line => {
  368. const fingerprint = SDPUtil.parseFingerprint(line);
  369. fingerprint.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  370. elem.c('fingerprint').t(fingerprint.fingerprint);
  371. delete fingerprint.fingerprint;
  372. const setupLine
  373. = SDPUtil.findLine(
  374. this.media[mediaindex],
  375. 'a=setup:',
  376. this.session);
  377. if (setupLine) {
  378. fingerprint.setup = setupLine.substr(8);
  379. }
  380. elem.attrs(fingerprint);
  381. elem.up(); // end of fingerprint
  382. });
  383. const iceParameters = SDPUtil.iceparams(this.media[mediaindex], this.session);
  384. if (iceParameters) {
  385. iceParameters.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  386. elem.attrs(iceParameters);
  387. // XEP-0176
  388. const candidateLines
  389. = SDPUtil.findLines(
  390. this.media[mediaindex],
  391. 'a=candidate:',
  392. this.session);
  393. candidateLines.forEach(line => { // add any a=candidate lines
  394. const candidate = SDPUtil.candidateToJingle(line);
  395. if (this.failICE) {
  396. candidate.ip = '1.1.1.1';
  397. }
  398. const protocol
  399. = candidate && typeof candidate.protocol === 'string'
  400. ? candidate.protocol.toLowerCase()
  401. : '';
  402. if ((this.removeTcpCandidates
  403. && (protocol === 'tcp' || protocol === 'ssltcp'))
  404. || (this.removeUdpCandidates && protocol === 'udp')) {
  405. return;
  406. }
  407. elem.c('candidate', candidate).up();
  408. });
  409. }
  410. elem.up(); // end of transport
  411. };
  412. // XEP-0293
  413. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  414. const lines
  415. = SDPUtil.findLines(
  416. this.media[mediaindex],
  417. `a=rtcp-fb:${payloadtype}`);
  418. lines.forEach(line => {
  419. const feedback = SDPUtil.parseRTCPFB(line);
  420. if (feedback.type === 'trr-int') {
  421. elem.c('rtcp-fb-trr-int', {
  422. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  423. value: feedback.params[0]
  424. });
  425. elem.up();
  426. } else {
  427. elem.c('rtcp-fb', {
  428. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  429. type: feedback.type
  430. });
  431. if (feedback.params.length > 0) {
  432. elem.attrs({ 'subtype': feedback.params[0] });
  433. }
  434. elem.up();
  435. }
  436. });
  437. };
  438. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  439. let sdp = '';
  440. const feedbackElementTrrInt
  441. = elem.find(
  442. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  443. if (feedbackElementTrrInt.length) {
  444. sdp += 'a=rtcp-fb:* trr-int ';
  445. if (feedbackElementTrrInt.attr('value')) {
  446. sdp += feedbackElementTrrInt.attr('value');
  447. } else {
  448. sdp += '0';
  449. }
  450. sdp += '\r\n';
  451. }
  452. const feedbackElements = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  453. feedbackElements.each((_, fb) => {
  454. sdp += `a=rtcp-fb:${payloadtype} ${fb.getAttribute('type')}`;
  455. if (fb.hasAttribute('subtype')) {
  456. sdp += ` ${fb.getAttribute('subtype')}`;
  457. }
  458. sdp += '\r\n';
  459. });
  460. return sdp;
  461. };
  462. // construct an SDP from a jingle stanza
  463. SDP.prototype.fromJingle = function(jingle) {
  464. const sessionId = Date.now();
  465. // Use a unique session id for every TPC.
  466. this.raw = 'v=0\r\n'
  467. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  468. + 's=-\r\n'
  469. + 't=0 0\r\n';
  470. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  471. // #section-8
  472. const groups
  473. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  474. if (groups.length) {
  475. groups.each((idx, group) => {
  476. const contents
  477. = $(group)
  478. .find('>content')
  479. .map((_, content) => content.getAttribute('name'))
  480. .get();
  481. if (contents.length > 0) {
  482. this.raw
  483. += `a=group:${
  484. group.getAttribute('semantics')
  485. || group.getAttribute('type')} ${
  486. contents.join(' ')}\r\n`;
  487. }
  488. });
  489. }
  490. this.session = this.raw;
  491. jingle.find('>content').each((_, content) => {
  492. const m = this.jingle2media($(content));
  493. this.media.push(m);
  494. });
  495. // reconstruct msid-semantic -- apparently not necessary
  496. /*
  497. var msid = SDPUtil.parseSSRC(this.raw);
  498. if (msid.hasOwnProperty('mslabel')) {
  499. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  500. }
  501. */
  502. this.raw = this.session + this.media.join('');
  503. };
  504. // translate a jingle content element into an an SDP media part
  505. SDP.prototype.jingle2media = function(content) {
  506. const desc = content.find('>description');
  507. const transport = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  508. let sdp = '';
  509. const sctp = transport.find(
  510. '>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  511. const media = { media: desc.attr('media') };
  512. media.port = '9';
  513. if (content.attr('senders') === 'rejected') {
  514. // estos hack to reject an m-line.
  515. media.port = '0';
  516. }
  517. if (transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
  518. media.proto = sctp.length ? 'UDP/DTLS/SCTP' : 'UDP/TLS/RTP/SAVPF';
  519. } else {
  520. media.proto = 'UDP/TLS/RTP/SAVPF';
  521. }
  522. if (sctp.length) {
  523. sdp += `m=application ${media.port} UDP/DTLS/SCTP webrtc-datachannel\r\n`;
  524. sdp += `a=sctp-port:${sctp.attr('number')}\r\n`;
  525. sdp += 'a=max-message-size:262144\r\n';
  526. } else {
  527. media.fmt
  528. = desc
  529. .find('>payload-type')
  530. .map((_, payloadType) => payloadType.getAttribute('id'))
  531. .get();
  532. sdp += `${SDPUtil.buildMLine(media)}\r\n`;
  533. }
  534. sdp += 'c=IN IP4 0.0.0.0\r\n';
  535. if (!sctp.length) {
  536. sdp += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  537. }
  538. // XEP-0176 ICE parameters
  539. if (transport.length) {
  540. if (transport.attr('ufrag')) {
  541. sdp += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
  542. }
  543. if (transport.attr('pwd')) {
  544. sdp += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
  545. }
  546. transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
  547. sdp += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
  548. sdp += ` ${$(fingerprint).text()}`;
  549. sdp += '\r\n';
  550. if (fingerprint.hasAttribute('setup')) {
  551. sdp += `a=setup:${fingerprint.getAttribute('setup')}\r\n`;
  552. }
  553. });
  554. }
  555. // XEP-0176 ICE candidates
  556. transport.find('>candidate')
  557. .each((_, candidate) => {
  558. let protocol = candidate.getAttribute('protocol');
  559. protocol
  560. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  561. if ((this.removeTcpCandidates
  562. && (protocol === 'tcp' || protocol === 'ssltcp'))
  563. || (this.removeUdpCandidates && protocol === 'udp')) {
  564. return;
  565. } else if (this.failICE) {
  566. candidate.setAttribute('ip', '1.1.1.1');
  567. }
  568. sdp += SDPUtil.candidateFromJingle(candidate);
  569. });
  570. switch (content.attr('senders')) {
  571. case 'initiator':
  572. sdp += `a=${MediaDirection.SENDONLY}\r\n`;
  573. break;
  574. case 'responder':
  575. sdp += `a=${MediaDirection.RECVONLY}\r\n`;
  576. break;
  577. case 'none':
  578. sdp += `a=${MediaDirection.INACTIVE}\r\n`;
  579. break;
  580. case 'both':
  581. sdp += `a=${MediaDirection.SENDRECV}\r\n`;
  582. break;
  583. }
  584. sdp += `a=mid:${content.attr('name')}\r\n`;
  585. // <description><rtcp-mux/></description>
  586. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  587. // though
  588. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  589. if (desc.find('>rtcp-mux').length) {
  590. sdp += 'a=rtcp-mux\r\n';
  591. }
  592. desc.find('>payload-type').each((_, payloadType) => {
  593. sdp += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
  594. if ($(payloadType).find('>parameter').length) {
  595. sdp += `a=fmtp:${payloadType.getAttribute('id')} `;
  596. sdp
  597. += $(payloadType)
  598. .find('>parameter')
  599. .map((__, parameter) => {
  600. const name = parameter.getAttribute('name');
  601. return (
  602. (name ? `${name}=` : '')
  603. + parameter.getAttribute('value'));
  604. })
  605. .get()
  606. .join(';');
  607. sdp += '\r\n';
  608. }
  609. // xep-0293
  610. sdp += this.rtcpFbFromJingle($(payloadType), payloadType.getAttribute('id'));
  611. });
  612. // xep-0293
  613. sdp += this.rtcpFbFromJingle(desc, '*');
  614. // xep-0294
  615. desc
  616. .find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]')
  617. .each((_, hdrExt) => {
  618. sdp
  619. += `a=extmap:${hdrExt.getAttribute('id')} ${
  620. hdrExt.getAttribute('uri')}\r\n`;
  621. });
  622. if (desc.find('>extmap-allow-mixed[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]').length > 0) {
  623. sdp += 'a=extmap-allow-mixed\r\n';
  624. }
  625. // XEP-0339 handle ssrc-group attributes
  626. desc
  627. .find('>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  628. .each((_, ssrcGroup) => {
  629. const semantics = ssrcGroup.getAttribute('semantics');
  630. const ssrcs
  631. = $(ssrcGroup)
  632. .find('>source')
  633. .map((__, source) => source.getAttribute('ssrc'))
  634. .get();
  635. if (ssrcs.length) {
  636. sdp += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  637. }
  638. });
  639. // XEP-0339 handle source attributes
  640. let userSources = '';
  641. let nonUserSources = '';
  642. desc
  643. .find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  644. .each((_, source) => {
  645. const ssrc = source.getAttribute('ssrc');
  646. let isUserSource = true;
  647. let sourceStr = '';
  648. $(source)
  649. .find('>parameter')
  650. .each((__, parameter) => {
  651. const name = parameter.getAttribute('name');
  652. let value = parameter.getAttribute('value');
  653. value = SDPUtil.filterSpecialChars(value);
  654. sourceStr += `a=ssrc:${ssrc} ${name}`;
  655. if (value && value.length) {
  656. sourceStr += `:${value}`;
  657. }
  658. sourceStr += '\r\n';
  659. if (value?.includes('mixedmslabel')) {
  660. isUserSource = false;
  661. }
  662. });
  663. if (isUserSource) {
  664. userSources += sourceStr;
  665. } else {
  666. nonUserSources += sourceStr;
  667. }
  668. });
  669. // The sdp-interop package is relying the mixedmslabel m line to be the first one in order to set the direction
  670. // to sendrecv.
  671. sdp += nonUserSources + userSources;
  672. return sdp;
  673. };