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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. import $ from 'jquery';
  2. import { cloneDeep } from 'lodash-es';
  3. import transform from 'sdp-transform';
  4. import { Strophe } from 'strophe.js';
  5. import { MediaDirection } from '../../service/RTC/MediaDirection';
  6. import { MediaType } from '../../service/RTC/MediaType';
  7. import { SSRC_GROUP_SEMANTICS } from '../../service/RTC/StandardVideoQualitySettings';
  8. import { XEP } from '../../service/xmpp/XMPPExtensioProtocols';
  9. import browser from '../browser';
  10. import SDPUtil from './SDPUtil';
  11. /**
  12. * A class that translates the Jingle messages received from the signaling server into SDP format that the
  13. * browser understands and vice versa. This is needed for media session establishment and for signaling local and
  14. * remote sources across peers.
  15. */
  16. export default class SDP {
  17. /**
  18. * Constructor.
  19. *
  20. * @param {string} sdp - The SDP generated by the browser when SDP->Jingle conversion is needed, an empty string
  21. * when Jingle->SDP conversion is needed.
  22. * @param {boolean} isP2P - Whether this SDP belongs to a p2p peerconnection.
  23. */
  24. constructor(sdp, isP2P = false) {
  25. this._updateSessionAndMediaSections(sdp);
  26. this.isP2P = isP2P;
  27. this.raw = this.session + this.media.join('');
  28. // This flag will make {@link transportToJingle} and {@link jingle2media} replace ICE candidates IPs with
  29. // invalid value of '1.1.1.1' which will cause ICE failure. The flag is used in the automated testing.
  30. this.failICE = false;
  31. // Whether or not to remove TCP ice candidates when translating from/to jingle.
  32. this.removeTcpCandidates = false;
  33. // Whether or not to remove UDP ice candidates when translating from/to jingle.
  34. this.removeUdpCandidates = false;
  35. }
  36. /**
  37. * Updates the media and session sections of the SDP based on the raw SDP string.
  38. *
  39. * @param {string} sdp - The SDP generated by the browser.
  40. * @returns {void}
  41. * @private
  42. */
  43. _updateSessionAndMediaSections(sdp) {
  44. const media = typeof sdp === 'string' ? sdp.split('\r\nm=') : this.raw.split('\r\nm=');
  45. for (let i = 1, length = media.length; i < length; i++) {
  46. let mediaI = `m=${media[i]}`;
  47. if (i !== length - 1) {
  48. mediaI += '\r\n';
  49. }
  50. media[i] = mediaI;
  51. }
  52. this.session = `${media.shift()}\r\n`;
  53. this.media = media;
  54. }
  55. /**
  56. * Adds or removes the sources from the SDP.
  57. *
  58. * @param {Object} sourceMap - The map of the sources that are being added/removed.
  59. * @param {boolean} isAdd - Whether the sources are being added or removed.
  60. * @returns {Array<number>} - The indices of the new m-lines that were added/modifed in the SDP.
  61. */
  62. updateRemoteSources(sourceMap, isAdd = true) {
  63. const updatedMidIndices = [];
  64. for (const source of sourceMap.values()) {
  65. const { mediaType, msid, ssrcList, groups } = source;
  66. let idx;
  67. if (isAdd) {
  68. // For P2P, check if there is an m-line with the matching mediaType that doesn't have any ssrc lines.
  69. // Update the existing m-line if it exists, otherwise create a new m-line and add the sources.
  70. idx = this.media.findIndex(mLine => mLine.includes(`m=${mediaType}`) && !mLine.includes('a=ssrc'));
  71. if (!this.isP2P || idx === -1) {
  72. this.addMlineForNewSource(mediaType, true);
  73. idx = this.media.length - 1;
  74. }
  75. } else {
  76. idx = this.media.findIndex(mLine => mLine.includes(`a=ssrc:${ssrcList[0]}`));
  77. if (idx === -1) {
  78. continue; // eslint-disable-line no-continue
  79. }
  80. }
  81. updatedMidIndices.push(idx);
  82. if (isAdd) {
  83. ssrcList.forEach(ssrc => {
  84. this.media[idx] += `a=ssrc:${ssrc} msid:${msid}\r\n`;
  85. });
  86. groups?.forEach(group => {
  87. this.media[idx] += `a=ssrc-group:${group.semantics} ${group.ssrcs.join(' ')}\r\n`;
  88. });
  89. } else {
  90. ssrcList.forEach(ssrc => {
  91. this.media[idx] = this.media[idx].replace(new RegExp(`a=ssrc:${ssrc}.*\r\n`, 'g'), '');
  92. });
  93. groups?.forEach(group => {
  94. this.media[idx] = this.media[idx]
  95. .replace(new RegExp(`a=ssrc-group:${group.semantics}.*\r\n`, 'g'), '');
  96. });
  97. if (!this.isP2P) {
  98. // Reject the m-line so that the browser removes the associated transceiver from the list of
  99. // available transceivers. This will prevent the client from trying to re-use these inactive
  100. // transceivers when additional video sources are added to the peerconnection.
  101. const { media, port } = SDPUtil.parseMLine(this.media[idx].split('\r\n')[0]);
  102. this.media[idx] = this.media[idx]
  103. .replace(`a=${MediaDirection.SENDONLY}`, `a=${MediaDirection.INACTIVE}`);
  104. this.media[idx] = this.media[idx].replace(`m=${media} ${port}`, `m=${media} 0`);
  105. }
  106. }
  107. this.raw = this.session + this.media.join('');
  108. }
  109. return updatedMidIndices;
  110. }
  111. /**
  112. * Adds a new m-line to the description so that a new local or remote source can be added to the conference.
  113. *
  114. * @param {MediaType} mediaType media type of the new source that is being added.
  115. * @returns {void}
  116. */
  117. addMlineForNewSource(mediaType, isRemote = false) {
  118. const mid = this.media.length;
  119. const sdp = transform.parse(this.raw);
  120. const mline = cloneDeep(sdp.media.find(m => m.type === mediaType));
  121. // Edit media direction, mid and remove the existing ssrc lines in the m-line.
  122. mline.mid = mid;
  123. mline.direction = isRemote ? MediaDirection.SENDONLY : MediaDirection.RECVONLY;
  124. mline.msid = undefined;
  125. mline.ssrcs = undefined;
  126. mline.ssrcGroups = undefined;
  127. sdp.media = [ ...sdp.media, mline ];
  128. // We regenerate the BUNDLE group (since we added a new m-line).
  129. sdp.groups.forEach(group => {
  130. if (group.type === 'BUNDLE') {
  131. group.mids = [ ...group.mids.split(' '), mid ].join(' ');
  132. }
  133. });
  134. this.raw = transform.write(sdp);
  135. this._updateSessionAndMediaSections();
  136. }
  137. /**
  138. * Converts the Jingle message element to SDP.
  139. *
  140. * @param {*} jingle - The Jingle message element.
  141. * @returns {void}
  142. */
  143. fromJingle(jingle) {
  144. const sessionId = Date.now();
  145. // Use a unique session id for every TPC.
  146. this.raw = 'v=0\r\n'
  147. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  148. + 's=-\r\n'
  149. + 't=0 0\r\n';
  150. const groups = $(jingle).find(`>group[xmlns='${XEP.BUNDLE_MEDIA}']`);
  151. if (this.isP2P && groups.length) {
  152. groups.each((idx, group) => {
  153. const contents = $(group)
  154. .find('>content')
  155. .map((_, content) => content.getAttribute('name'))
  156. .get();
  157. if (contents.length > 0) {
  158. this.raw
  159. += `a=group:${
  160. group.getAttribute('semantics')
  161. || group.getAttribute('type')} ${
  162. contents.join(' ')}\r\n`;
  163. }
  164. });
  165. }
  166. this.session = this.raw;
  167. jingle.find('>content').each((_, content) => {
  168. const m = this.jingle2media($(content));
  169. this.media.push(m);
  170. });
  171. this.raw = this.session + this.media.join('');
  172. if (this.isP2P) {
  173. return;
  174. }
  175. // For offers from Jicofo, a new m-line needs to be created for each new remote source that is added to the
  176. // conference.
  177. const newSession = transform.parse(this.raw);
  178. const newMedia = [];
  179. newSession.media.forEach(mLine => {
  180. const type = mLine.type;
  181. if (type === MediaType.APPLICATION) {
  182. const newMline = cloneDeep(mLine);
  183. newMline.mid = newMedia.length.toString();
  184. newMedia.push(newMline);
  185. return;
  186. }
  187. if (!mLine.ssrcs?.length) {
  188. const newMline = cloneDeep(mLine);
  189. newMline.mid = newMedia.length.toString();
  190. newMedia.push(newMline);
  191. return;
  192. }
  193. mLine.ssrcs.forEach((ssrc, idx) => {
  194. // Do nothing if the m-line with the given SSRC already exists.
  195. if (newMedia.some(mline => mline.ssrcs?.some(source => source.id === ssrc.id))) {
  196. return;
  197. }
  198. const newMline = cloneDeep(mLine);
  199. newMline.ssrcs = [];
  200. newMline.ssrcGroups = [];
  201. newMline.mid = newMedia.length.toString();
  202. newMline.bundleOnly = undefined;
  203. newMline.direction = idx ? 'sendonly' : 'sendrecv';
  204. // Add the sources and the related FID source group to the new m-line.
  205. const ssrcId = ssrc.id.toString();
  206. const group = mLine.ssrcGroups?.find(g => g.ssrcs.includes(ssrcId));
  207. if (group) {
  208. newMline.ssrcs.push(ssrc);
  209. const otherSsrc = group.ssrcs.split(' ').find(s => s !== ssrcId);
  210. if (otherSsrc) {
  211. const otherSource = mLine.ssrcs.find(source => source.id.toString() === otherSsrc);
  212. newMline.ssrcs.push(otherSource);
  213. }
  214. newMline.ssrcGroups.push(group);
  215. } else {
  216. newMline.ssrcs.push(ssrc);
  217. }
  218. newMedia.push(newMline);
  219. });
  220. });
  221. newSession.media = newMedia;
  222. const mids = [];
  223. newMedia.forEach(mLine => {
  224. mids.push(mLine.mid);
  225. });
  226. if (groups.length) {
  227. // We regenerate the BUNDLE group (since we regenerated the mids)
  228. newSession.groups = [ {
  229. type: 'BUNDLE',
  230. mids: mids.join(' ')
  231. } ];
  232. }
  233. // msid semantic
  234. newSession.msidSemantic = {
  235. semantic: 'WMS',
  236. token: '*'
  237. };
  238. // Increment the session version every time.
  239. newSession.origin.sessionVersion++;
  240. this.raw = transform.write(newSession);
  241. this._updateSessionAndMediaSections();
  242. }
  243. /**
  244. * Returns an SSRC Map by extracting SSRCs and SSRC groups from all the m-lines in the SDP.
  245. *
  246. * @returns {*}
  247. */
  248. getMediaSsrcMap() {
  249. const sourceInfo = new Map();
  250. this.media.forEach((mediaItem, mediaindex) => {
  251. const mid = SDPUtil.parseMID(SDPUtil.findLine(mediaItem, 'a=mid:'));
  252. const mline = SDPUtil.parseMLine(mediaItem.split('\r\n')[0]);
  253. const isRecvOnly = SDPUtil.findLine(mediaItem, `a=${MediaDirection.RECVONLY}`);
  254. // Do not process recvonly m-lines. Firefox generates recvonly SSRCs for all remote sources.
  255. if (isRecvOnly && browser.isFirefox()) {
  256. return;
  257. }
  258. const media = {
  259. mediaindex,
  260. mediaType: mline.media,
  261. mid,
  262. ssrcs: {},
  263. ssrcGroups: []
  264. };
  265. SDPUtil.findLines(mediaItem, 'a=ssrc:').forEach(line => {
  266. const linessrc = line.substring(7).split(' ')[0];
  267. // Allocate new ChannelSsrc.
  268. if (!media.ssrcs[linessrc]) {
  269. media.ssrcs[linessrc] = {
  270. ssrc: linessrc,
  271. lines: []
  272. };
  273. }
  274. media.ssrcs[linessrc].lines.push(line);
  275. });
  276. SDPUtil.findLines(mediaItem, 'a=ssrc-group:').forEach(line => {
  277. const idx = line.indexOf(' ');
  278. const semantics = line.substr(0, idx).substr(13);
  279. const ssrcs = line.substr(14 + semantics.length).split(' ');
  280. if (ssrcs.length) {
  281. media.ssrcGroups.push({
  282. semantics,
  283. ssrcs
  284. });
  285. }
  286. });
  287. sourceInfo.set(mediaindex, media);
  288. });
  289. return sourceInfo;
  290. }
  291. /**
  292. * Converts the content section from Jingle to a media section that can be appended to the SDP.
  293. *
  294. * @param {*} content - The content section from the Jingle message element.
  295. * @returns {*} - The constructed media sections.
  296. */
  297. jingle2media(content) {
  298. const desc = content.find('>description');
  299. const transport = content.find(`>transport[xmlns='${XEP.ICE_UDP_TRANSPORT}']`);
  300. let sdp = '';
  301. const sctp = transport.find(`>sctpmap[xmlns='${XEP.SCTP_DATA_CHANNEL}']`);
  302. const media = { media: desc.attr('media') };
  303. media.port = '9';
  304. if (content.attr('senders') === 'rejected') {
  305. media.port = '0';
  306. }
  307. if (transport.find(`>fingerprint[xmlns='${XEP.DTLS_SRTP}']`).length) {
  308. media.proto = sctp.length ? 'UDP/DTLS/SCTP' : 'UDP/TLS/RTP/SAVPF';
  309. } else {
  310. media.proto = 'UDP/TLS/RTP/SAVPF';
  311. }
  312. if (sctp.length) {
  313. sdp += `m=application ${media.port} UDP/DTLS/SCTP webrtc-datachannel\r\n`;
  314. sdp += `a=sctp-port:${sctp.attr('number')}\r\n`;
  315. sdp += 'a=max-message-size:262144\r\n';
  316. } else {
  317. media.fmt
  318. = desc
  319. .find('>payload-type')
  320. .map((_, payloadType) => payloadType.getAttribute('id'))
  321. .get();
  322. sdp += `${SDPUtil.buildMLine(media)}\r\n`;
  323. }
  324. sdp += 'c=IN IP4 0.0.0.0\r\n';
  325. if (!sctp.length) {
  326. sdp += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  327. }
  328. if (transport.length) {
  329. if (transport.attr('ufrag')) {
  330. sdp += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
  331. }
  332. if (transport.attr('pwd')) {
  333. sdp += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
  334. }
  335. transport.find(`>fingerprint[xmlns='${XEP.DTLS_SRTP}']`).each((_, fingerprint) => {
  336. sdp += `a=fingerprint:${fingerprint.getAttribute('hash')} ${$(fingerprint).text()}\r\n`;
  337. if (fingerprint.hasAttribute('setup')) {
  338. sdp += `a=setup:${fingerprint.getAttribute('setup')}\r\n`;
  339. }
  340. });
  341. }
  342. transport.find('>candidate').each((_, candidate) => {
  343. let protocol = candidate.getAttribute('protocol');
  344. protocol = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  345. if ((this.removeTcpCandidates && (protocol === 'tcp' || protocol === 'ssltcp'))
  346. || (this.removeUdpCandidates && protocol === 'udp')) {
  347. return;
  348. } else if (this.failICE) {
  349. candidate.setAttribute('ip', '1.1.1.1');
  350. }
  351. sdp += SDPUtil.candidateFromJingle(candidate);
  352. });
  353. switch (content.attr('senders')) {
  354. case 'initiator':
  355. sdp += `a=${MediaDirection.SENDONLY}\r\n`;
  356. break;
  357. case 'responder':
  358. sdp += `a=${MediaDirection.RECVONLY}\r\n`;
  359. break;
  360. case 'none':
  361. sdp += `a=${MediaDirection.INACTIVE}\r\n`;
  362. break;
  363. case 'both':
  364. sdp += `a=${MediaDirection.SENDRECV}\r\n`;
  365. break;
  366. }
  367. sdp += `a=mid:${content.attr('name')}\r\n`;
  368. // <description><rtcp-mux/></description>
  369. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
  370. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  371. if (desc.find('>rtcp-mux').length) {
  372. sdp += 'a=rtcp-mux\r\n';
  373. }
  374. desc.find('>payload-type').each((_, payloadType) => {
  375. sdp += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
  376. if ($(payloadType).find('>parameter').length) {
  377. sdp += `a=fmtp:${payloadType.getAttribute('id')} `;
  378. sdp += $(payloadType)
  379. .find('>parameter')
  380. .map((__, parameter) => {
  381. const name = parameter.getAttribute('name');
  382. return (name ? `${name}=` : '') + parameter.getAttribute('value');
  383. })
  384. .get()
  385. .join(';');
  386. sdp += '\r\n';
  387. }
  388. sdp += this.rtcpFbFromJingle($(payloadType), payloadType.getAttribute('id'));
  389. });
  390. sdp += this.rtcpFbFromJingle(desc, '*');
  391. desc.find(`>rtp-hdrext[xmlns='${XEP.RTP_HEADER_EXTENSIONS}']`).each((_, hdrExt) => {
  392. sdp += `a=extmap:${hdrExt.getAttribute('id')} ${hdrExt.getAttribute('uri')}\r\n`;
  393. });
  394. if (desc.find(`>extmap-allow-mixed[xmlns='${XEP.RTP_HEADER_EXTENSIONS}']`).length > 0) {
  395. sdp += 'a=extmap-allow-mixed\r\n';
  396. }
  397. desc
  398. .find(`>ssrc-group[xmlns='${XEP.SOURCE_ATTRIBUTES}']`)
  399. .each((_, ssrcGroup) => {
  400. const semantics = ssrcGroup.getAttribute('semantics');
  401. const ssrcs
  402. = $(ssrcGroup)
  403. .find('>source')
  404. .map((__, source) => source.getAttribute('ssrc'))
  405. .get();
  406. if (ssrcs.length) {
  407. sdp += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  408. }
  409. });
  410. let userSources = '';
  411. let nonUserSources = '';
  412. desc
  413. .find(`>source[xmlns='${XEP.SOURCE_ATTRIBUTES}']`)
  414. .each((_, source) => {
  415. const ssrc = source.getAttribute('ssrc');
  416. let isUserSource = true;
  417. let sourceStr = '';
  418. $(source)
  419. .find('>parameter')
  420. .each((__, parameter) => {
  421. const name = parameter.getAttribute('name');
  422. let value = parameter.getAttribute('value');
  423. value = SDPUtil.filterSpecialChars(value);
  424. sourceStr += `a=ssrc:${ssrc} ${name}`;
  425. if (value && value.length) {
  426. sourceStr += `:${value}`;
  427. }
  428. sourceStr += '\r\n';
  429. if (value?.includes('mixedmslabel')) {
  430. isUserSource = false;
  431. }
  432. });
  433. if (isUserSource) {
  434. userSources += sourceStr;
  435. } else {
  436. nonUserSources += sourceStr;
  437. }
  438. });
  439. // Append sources in the correct order, the mixedmslable m-line which has the JVB's SSRC for RTCP termination
  440. // is expected to be in the first m-line.
  441. sdp += nonUserSources + userSources;
  442. return sdp;
  443. }
  444. /**
  445. * Coverts the RTCP attributes for the session from XMPP format to SDP.
  446. * https://xmpp.org/extensions/xep-0293.html
  447. *
  448. * @param {*} elem - Jingle message element.
  449. * @param {*} payloadtype - Payload type for the codec.
  450. * @returns {string}
  451. */
  452. rtcpFbFromJingle(elem, payloadtype) {
  453. let sdp = '';
  454. const feedbackElementTrrInt = elem.find(`>rtcp-fb-trr-int[xmlns='${XEP.RTP_FEEDBACK}']`);
  455. if (feedbackElementTrrInt.length) {
  456. sdp += 'a=rtcp-fb:* trr-int ';
  457. sdp += feedbackElementTrrInt.attr('value') || '0';
  458. sdp += '\r\n';
  459. }
  460. const feedbackElements = elem.find(`>rtcp-fb[xmlns='${XEP.RTP_FEEDBACK}']`);
  461. feedbackElements.each((_, fb) => {
  462. sdp += `a=rtcp-fb:${payloadtype} ${fb.getAttribute('type')}`;
  463. if (fb.hasAttribute('subtype')) {
  464. sdp += ` ${fb.getAttribute('subtype')}`;
  465. }
  466. sdp += '\r\n';
  467. });
  468. return sdp;
  469. }
  470. /**
  471. * Converts the RTCP attributes for the session from SDP to XMPP format.
  472. * https://xmpp.org/extensions/xep-0293.html
  473. *
  474. * @param {*} mediaIndex - The index of the media section in the SDP.
  475. * @param {*} elem - The Jingle message element.
  476. * @param {*} payloadtype - payload type for the codec.
  477. */
  478. rtcpFbToJingle(mediaIndex, elem, payloadtype) {
  479. const lines = SDPUtil.findLines(this.media[mediaIndex], `a=rtcp-fb:${payloadtype}`);
  480. lines.forEach(line => {
  481. const feedback = SDPUtil.parseRTCPFB(line);
  482. if (feedback.type === 'trr-int') {
  483. elem.c('rtcp-fb-trr-int', {
  484. xmlns: XEP.RTP_FEEDBACK,
  485. value: feedback.params[0]
  486. });
  487. elem.up();
  488. } else {
  489. elem.c('rtcp-fb', {
  490. xmlns: XEP.RTP_FEEDBACK,
  491. type: feedback.type
  492. });
  493. if (feedback.params.length > 0) {
  494. elem.attrs({ 'subtype': feedback.params[0] });
  495. }
  496. elem.up();
  497. }
  498. });
  499. }
  500. /**
  501. * Converts the current SDP to a Jingle message that can be sent over the wire to a signaling server.
  502. *
  503. * @param {*} elem - The Jingle message element.
  504. * @param {*} thecreator - Sender role, whether it is an 'initiator' or 'responder'.
  505. * @returns - The updated Jingle message element.
  506. */
  507. toJingle(elem, thecreator) {
  508. SDPUtil.findLines(this.session, 'a=group:').forEach(line => {
  509. const parts = line.split(' ');
  510. const semantics = parts.shift().substr(8);
  511. elem.c('group', {
  512. xmlns: XEP.BUNDLE_MEDIA,
  513. semantics
  514. });
  515. // Bundle all the media types. Jicofo expects the 'application' media type to be signaled as 'data'.
  516. let mediaTypes = [ MediaType.AUDIO, MediaType.VIDEO, 'data' ];
  517. // For p2p connection, 'mid' will be used in the bundle group.
  518. if (this.isP2P) {
  519. mediaTypes = this.media.map(mediaItem => SDPUtil.parseMID(SDPUtil.findLine(mediaItem, 'a=mid:')));
  520. }
  521. mediaTypes.forEach(type => elem.c('content', { name: type }).up());
  522. elem.up();
  523. });
  524. this.media.forEach((mediaItem, i) => {
  525. const mline = SDPUtil.parseMLine(mediaItem.split('\r\n')[0]);
  526. const mediaType = mline.media === MediaType.APPLICATION ? 'data' : mline.media;
  527. let ssrc = false;
  528. const assrcline = SDPUtil.findLine(mediaItem, 'a=ssrc:');
  529. const isRecvOnly = SDPUtil.findLine(mediaItem, `a=${MediaDirection.RECVONLY}`);
  530. if (assrcline) {
  531. ssrc = assrcline.substring(7).split(' ')[0];
  532. }
  533. const contents = $(elem.tree()).find(`content[name='${mediaType}']`);
  534. // Append source groups from the new m-lines to the existing media description. The SDP will have multiple
  535. // m-lines for audio and video including the recv-only ones for remote sources but there needs to be only
  536. // one media description for a given media type that should include all the sources, i.e., both the camera
  537. // and screenshare sources should be added to the 'video' description.
  538. for (const content of contents) {
  539. if (!content.hasAttribute('creator')) {
  540. // eslint-disable-next-line no-continue
  541. continue;
  542. }
  543. if (ssrc && !(isRecvOnly && browser.isFirefox())) {
  544. const description = $(content).find('description');
  545. const ssrcMap = SDPUtil.parseSSRC(mediaItem);
  546. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  547. const sourceName = SDPUtil.parseSourceNameLine(ssrcParameters);
  548. const videoType = SDPUtil.parseVideoTypeLine(ssrcParameters);
  549. const source = Strophe.xmlElement('source', {
  550. ssrc: availableSsrc,
  551. name: sourceName,
  552. videoType,
  553. xmlns: XEP.SOURCE_ATTRIBUTES
  554. });
  555. const msid = SDPUtil.parseMSIDAttribute(ssrcParameters);
  556. if (msid) {
  557. const param = Strophe.xmlElement('parameter', {
  558. name: 'msid',
  559. value: msid
  560. });
  561. source.append(param);
  562. }
  563. description.append(source);
  564. }
  565. const ssrcGroupLines = SDPUtil.findLines(mediaItem, 'a=ssrc-group:');
  566. ssrcGroupLines.forEach(line => {
  567. const { semantics, ssrcs } = SDPUtil.parseSSRCGroupLine(line);
  568. if (ssrcs.length) {
  569. const group = Strophe.xmlElement('ssrc-group', {
  570. semantics,
  571. xmlns: XEP.SOURCE_ATTRIBUTES
  572. });
  573. for (const val of ssrcs) {
  574. const src = Strophe.xmlElement('source', {
  575. ssrc: val
  576. });
  577. group.append(src);
  578. }
  579. description.append(group);
  580. }
  581. });
  582. }
  583. return;
  584. }
  585. const mid = SDPUtil.parseMID(SDPUtil.findLine(mediaItem, 'a=mid:'));
  586. elem.c('content', {
  587. creator: thecreator,
  588. name: this.isP2P ? mid : mediaType
  589. });
  590. if (mediaType === MediaType.VIDEO && typeof this.initialLastN === 'number') {
  591. elem.c('initial-last-n', {
  592. xmlns: 'jitsi:colibri2',
  593. value: this.initialLastN
  594. }).up();
  595. }
  596. if ([ MediaType.AUDIO, MediaType.VIDEO ].includes(mediaType)) {
  597. elem.c('description', {
  598. xmlns: XEP.RTP_MEDIA,
  599. media: mediaType
  600. });
  601. mline.fmt.forEach(format => {
  602. const rtpmap = SDPUtil.findLine(mediaItem, `a=rtpmap:${format}`);
  603. elem.c('payload-type', SDPUtil.parseRTPMap(rtpmap));
  604. const afmtpline = SDPUtil.findLine(mediaItem, `a=fmtp:${format}`);
  605. if (afmtpline) {
  606. const fmtpParameters = SDPUtil.parseFmtp(afmtpline);
  607. fmtpParameters.forEach(param => elem.c('parameter', param).up());
  608. }
  609. this.rtcpFbToJingle(i, elem, format);
  610. elem.up();
  611. });
  612. if (ssrc && !(isRecvOnly && browser.isFirefox())) {
  613. const ssrcMap = SDPUtil.parseSSRC(mediaItem);
  614. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  615. const sourceName = SDPUtil.parseSourceNameLine(ssrcParameters);
  616. const videoType = SDPUtil.parseVideoTypeLine(ssrcParameters);
  617. elem.c('source', {
  618. ssrc: availableSsrc,
  619. name: sourceName,
  620. videoType,
  621. xmlns: XEP.SOURCE_ATTRIBUTES
  622. });
  623. const msid = SDPUtil.parseMSIDAttribute(ssrcParameters);
  624. if (msid) {
  625. elem.c('parameter').attrs({
  626. name: 'msid',
  627. value: msid
  628. });
  629. elem.up();
  630. }
  631. elem.up();
  632. }
  633. const ssrcGroupLines = SDPUtil.findLines(mediaItem, 'a=ssrc-group:');
  634. ssrcGroupLines.forEach(line => {
  635. const { semantics, ssrcs } = SDPUtil.parseSSRCGroupLine(line);
  636. if (ssrcs.length) {
  637. elem.c('ssrc-group', {
  638. semantics,
  639. xmlns: XEP.SOURCE_ATTRIBUTES
  640. });
  641. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  642. elem.up();
  643. }
  644. });
  645. }
  646. const ridLines = SDPUtil.findLines(mediaItem, 'a=rid:');
  647. if (ridLines.length && browser.usesRidsForSimulcast()) {
  648. // Map a line which looks like "a=rid:2 send" to just the rid ("2").
  649. const rids = ridLines.map(ridLine => ridLine.split(':')[1].split(' ')[0]);
  650. rids.forEach(rid => {
  651. elem.c('source', {
  652. rid,
  653. xmlns: XEP.SOURCE_ATTRIBUTES
  654. });
  655. elem.up();
  656. });
  657. const unifiedSimulcast = SDPUtil.findLine(mediaItem, 'a=simulcast:');
  658. if (unifiedSimulcast) {
  659. elem.c('rid-group', {
  660. semantics: SSRC_GROUP_SEMANTICS.SIM,
  661. xmlns: XEP.SOURCE_ATTRIBUTES
  662. });
  663. rids.forEach(rid => elem.c('source', { rid }).up());
  664. elem.up();
  665. }
  666. }
  667. if (SDPUtil.findLine(mediaItem, 'a=rtcp-mux')) {
  668. elem.c('rtcp-mux').up();
  669. }
  670. this.rtcpFbToJingle(i, elem, '*');
  671. const extmapLines = SDPUtil.findLines(mediaItem, 'a=extmap:', this.session);
  672. extmapLines.forEach(extmapLine => {
  673. const extmap = SDPUtil.parseExtmap(extmapLine);
  674. elem.c('rtp-hdrext', {
  675. xmlns: XEP.RTP_HEADER_EXTENSIONS,
  676. uri: extmap.uri,
  677. id: extmap.value
  678. });
  679. if (extmap.hasOwnProperty('direction')) {
  680. switch (extmap.direction) {
  681. case MediaDirection.SENDONLY:
  682. elem.attrs({ senders: 'responder' });
  683. break;
  684. case MediaDirection.RECVONLY:
  685. elem.attrs({ senders: 'initiator' });
  686. break;
  687. case MediaDirection.SENDRECV:
  688. elem.attrs({ senders: 'both' });
  689. break;
  690. case MediaDirection.INACTIVE:
  691. elem.attrs({ senders: 'none' });
  692. break;
  693. }
  694. }
  695. elem.up();
  696. });
  697. if (SDPUtil.findLine(mediaItem, 'a=extmap-allow-mixed', this.session)) {
  698. elem.c('extmap-allow-mixed', {
  699. xmlns: XEP.RTP_HEADER_EXTENSIONS
  700. });
  701. elem.up();
  702. }
  703. elem.up(); // end of description
  704. }
  705. // Map ice-ufrag/pwd, dtls fingerprint, candidates.
  706. this.transportToJingle(i, elem);
  707. // Set senders attribute based on media direction
  708. if (SDPUtil.findLine(mediaItem, `a=${MediaDirection.SENDRECV}`)) {
  709. elem.attrs({ senders: 'both' });
  710. } else if (SDPUtil.findLine(mediaItem, `a=${MediaDirection.SENDONLY}`)) {
  711. elem.attrs({ senders: 'initiator' });
  712. } else if (SDPUtil.findLine(mediaItem, `a=${MediaDirection.RECVONLY}`)) {
  713. elem.attrs({ senders: 'responder' });
  714. } else if (SDPUtil.findLine(mediaItem, `a=${MediaDirection.INACTIVE}`)) {
  715. elem.attrs({ senders: 'none' });
  716. }
  717. // Reject an m-line only when port is 0 and a=bundle-only is not present in the section.
  718. // The port is automatically set to 0 when bundle-only is used.
  719. if (mline.port === '0' && !SDPUtil.findLine(mediaItem, 'a=bundle-only', this.session)) {
  720. elem.attrs({ senders: 'rejected' });
  721. }
  722. elem.up(); // end of content
  723. });
  724. elem.up();
  725. return elem;
  726. }
  727. /**
  728. * Converts the session transport information from SDP to XMPP format.
  729. *
  730. * @param {*} mediaIndex The index for the m-line in the SDP.
  731. * @param {*} elem The transport element.
  732. */
  733. transportToJingle(mediaIndex, elem) {
  734. elem.c('transport');
  735. const sctpport = SDPUtil.findLine(this.media[mediaIndex], 'a=sctp-port:', this.session);
  736. const sctpmap = SDPUtil.findLine(this.media[mediaIndex], 'a=sctpmap:', this.session);
  737. if (sctpport) {
  738. const sctpAttrs = SDPUtil.parseSCTPPort(sctpport);
  739. elem.c('sctpmap', {
  740. xmlns: XEP.SCTP_DATA_CHANNEL,
  741. number: sctpAttrs, // SCTP port
  742. protocol: 'webrtc-datachannel' // protocol
  743. });
  744. // The parser currently requires streams to be present.
  745. elem.attrs({ streams: 0 });
  746. elem.up();
  747. } else if (sctpmap) {
  748. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  749. elem.c('sctpmap', {
  750. xmlns: XEP.SCTP_DATA_CHANNEL,
  751. number: sctpAttrs[0], // SCTP port
  752. protocol: sctpAttrs[1] // protocol
  753. });
  754. // Optional stream count attribute.
  755. elem.attrs({ streams: sctpAttrs.length > 2 ? sctpAttrs[2] : 0 });
  756. elem.up();
  757. }
  758. const fingerprints = SDPUtil.findLines(this.media[mediaIndex], 'a=fingerprint:', this.session);
  759. fingerprints.forEach(line => {
  760. const fingerprint = SDPUtil.parseFingerprint(line);
  761. fingerprint.xmlns = XEP.DTLS_SRTP;
  762. elem.c('fingerprint').t(fingerprint.fingerprint);
  763. delete fingerprint.fingerprint;
  764. const setupLine = SDPUtil.findLine(this.media[mediaIndex], 'a=setup:', this.session);
  765. if (setupLine) {
  766. fingerprint.setup = setupLine.substr(8);
  767. }
  768. elem.attrs(fingerprint);
  769. elem.up(); // end of fingerprint
  770. });
  771. const iceParameters = SDPUtil.iceparams(this.media[mediaIndex], this.session);
  772. if (iceParameters) {
  773. iceParameters.xmlns = XEP.ICE_UDP_TRANSPORT;
  774. elem.attrs(iceParameters);
  775. const candidateLines = SDPUtil.findLines(this.media[mediaIndex], 'a=candidate:', this.session);
  776. candidateLines.forEach(line => { // add any a=candidate lines
  777. const candidate = SDPUtil.candidateToJingle(line);
  778. if (this.failICE) {
  779. candidate.ip = '1.1.1.1';
  780. }
  781. const protocol = candidate && typeof candidate.protocol === 'string'
  782. ? candidate.protocol.toLowerCase() : '';
  783. if ((this.removeTcpCandidates && (protocol === 'tcp' || protocol === 'ssltcp'))
  784. || (this.removeUdpCandidates && protocol === 'udp')) {
  785. return;
  786. }
  787. elem.c('candidate', candidate).up();
  788. });
  789. }
  790. elem.up(); // end of transport
  791. }
  792. }