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.

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