選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SDP.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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 === 'audio' || mline.media === 'video') {
  166. elem.c('description',
  167. { xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  168. media: mline.media });
  169. if (ssrc) {
  170. elem.attrs({ ssrc });
  171. }
  172. for (let j = 0; j < mline.fmt.length; j++) {
  173. const rtpmap
  174. = SDPUtil.findLine(
  175. this.media[i],
  176. `a=rtpmap:${mline.fmt[j]}`);
  177. elem.c('payload-type', SDPUtil.parseRTPMap(rtpmap));
  178. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo
  179. // value=bar/>
  180. const afmtpline
  181. = SDPUtil.findLine(
  182. this.media[i],
  183. `a=fmtp:${mline.fmt[j]}`);
  184. if (afmtpline) {
  185. const fmtpParameters = SDPUtil.parseFmtp(afmtpline);
  186. // eslint-disable-next-line max-depth
  187. for (let k = 0; k < fmtpParameters.length; k++) {
  188. elem.c('parameter', fmtpParameters[k]).up();
  189. }
  190. }
  191. // XEP-0293 -- map a=rtcp-fb
  192. this.rtcpFbToJingle(i, elem, mline.fmt[j]);
  193. elem.up();
  194. }
  195. if (ssrc) {
  196. const ssrcMap = SDPUtil.parseSSRC(this.media[i]);
  197. for (const [ availableSsrc, ssrcParameters ] of ssrcMap) {
  198. const sourceName = SDPUtil.parseSourceNameLine(ssrcParameters);
  199. const videoType = SDPUtil.parseVideoTypeLine(ssrcParameters);
  200. elem.c('source', {
  201. ssrc: availableSsrc,
  202. name: sourceName,
  203. videoType,
  204. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  205. });
  206. const msid = SDPUtil.parseMSIDAttribute(ssrcParameters);
  207. // eslint-disable-next-line max-depth
  208. if (msid) {
  209. elem.c('parameter');
  210. elem.attrs({ name: 'msid' });
  211. elem.attrs({ value: msid });
  212. elem.up();
  213. }
  214. elem.up();
  215. }
  216. // XEP-0339 handle ssrc-group attributes
  217. const ssrcGroupLines
  218. = SDPUtil.findLines(this.media[i], 'a=ssrc-group:');
  219. ssrcGroupLines.forEach(line => {
  220. const idx = line.indexOf(' ');
  221. const semantics = line.substr(0, idx).substr(13);
  222. const ssrcs = line.substr(14 + semantics.length).split(' ');
  223. if (ssrcs.length) {
  224. elem.c('ssrc-group', { semantics,
  225. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  226. ssrcs.forEach(s => elem.c('source', { ssrc: s }).up());
  227. elem.up();
  228. }
  229. });
  230. }
  231. const ridLines = SDPUtil.findLines(this.media[i], 'a=rid:');
  232. if (ridLines.length && browser.usesRidsForSimulcast()) {
  233. // Map a line which looks like "a=rid:2 send" to just
  234. // the rid ("2")
  235. const rids = ridLines
  236. .map(ridLine => ridLine.split(':')[1])
  237. .map(ridInfo => ridInfo.split(' ')[0]);
  238. rids.forEach(rid => {
  239. elem.c('source', {
  240. rid,
  241. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  242. });
  243. elem.up();
  244. });
  245. const unifiedSimulcast
  246. = SDPUtil.findLine(this.media[i], 'a=simulcast:');
  247. if (unifiedSimulcast) {
  248. elem.c('rid-group', {
  249. semantics: 'SIM',
  250. xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0'
  251. });
  252. rids.forEach(rid => {
  253. elem.c('source', { rid }).up();
  254. });
  255. elem.up();
  256. }
  257. }
  258. if (SDPUtil.findLine(this.media[i], 'a=rtcp-mux')) {
  259. elem.c('rtcp-mux').up();
  260. }
  261. // XEP-0293 -- map a=rtcp-fb:*
  262. this.rtcpFbToJingle(i, elem, '*');
  263. // XEP-0294
  264. const extmapLines = SDPUtil.findLines(this.media[i], 'a=extmap:');
  265. for (let j = 0; j < extmapLines.length; j++) {
  266. const extmap = SDPUtil.parseExtmap(extmapLines[j]);
  267. elem.c('rtp-hdrext', {
  268. xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  269. uri: extmap.uri,
  270. id: extmap.value
  271. });
  272. // eslint-disable-next-line max-depth
  273. if (extmap.hasOwnProperty('direction')) {
  274. // eslint-disable-next-line max-depth
  275. switch (extmap.direction) {
  276. case MediaDirection.SENDONLY:
  277. elem.attrs({ senders: 'responder' });
  278. break;
  279. case MediaDirection.RECVONLY:
  280. elem.attrs({ senders: 'initiator' });
  281. break;
  282. case MediaDirection.SENDRECV:
  283. elem.attrs({ senders: 'both' });
  284. break;
  285. case MediaDirection.INACTIVE:
  286. elem.attrs({ senders: 'none' });
  287. break;
  288. }
  289. }
  290. // TODO: handle params
  291. elem.up();
  292. }
  293. elem.up(); // end of description
  294. }
  295. // map ice-ufrag/pwd, dtls fingerprint, candidates
  296. this.transportToJingle(i, elem);
  297. const m = this.media[i];
  298. if (SDPUtil.findLine(m, `a=${MediaDirection.SENDRECV}`, this.session)) {
  299. elem.attrs({ senders: 'both' });
  300. } else if (SDPUtil.findLine(m, `a=${MediaDirection.SENDONLY}`, this.session)) {
  301. elem.attrs({ senders: 'initiator' });
  302. } else if (SDPUtil.findLine(m, `a=${MediaDirection.RECVONLY}`, this.session)) {
  303. elem.attrs({ senders: 'responder' });
  304. } else if (SDPUtil.findLine(m, `a=${MediaDirection.INACTIVE}`, this.session)) {
  305. elem.attrs({ senders: 'none' });
  306. }
  307. // Reject an m-line only when port is 0 and a=bundle-only is not present in the section.
  308. // The port is automatically set to 0 when bundle-only is used.
  309. if (mline.port === '0' && !SDPUtil.findLine(m, 'a=bundle-only', this.session)) {
  310. // estos hack to reject an m-line
  311. elem.attrs({ senders: 'rejected' });
  312. }
  313. elem.up(); // end of content
  314. }
  315. elem.up();
  316. return elem;
  317. };
  318. SDP.prototype.transportToJingle = function(mediaindex, elem) {
  319. elem.c('transport');
  320. // XEP-0343 DTLS/SCTP
  321. const sctpport
  322. = SDPUtil.findLine(this.media[mediaindex], 'a=sctp-port:', this.session);
  323. const sctpmap
  324. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', this.session);
  325. if (sctpport) {
  326. const sctpAttrs = SDPUtil.parseSCTPPort(sctpport);
  327. elem.c('sctpmap', {
  328. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  329. number: sctpAttrs, /* SCTP port */
  330. protocol: 'webrtc-datachannel' /* protocol */
  331. });
  332. // The parser currently requires streams to be present
  333. elem.attrs({ streams: 0 });
  334. elem.up();
  335. } else if (sctpmap) {
  336. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  337. elem.c('sctpmap', {
  338. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  339. number: sctpAttrs[0], /* SCTP port */
  340. protocol: sctpAttrs[1] /* protocol */
  341. });
  342. // Optional stream count attribute
  343. if (sctpAttrs.length > 2) {
  344. elem.attrs({ streams: sctpAttrs[2] });
  345. } else {
  346. elem.attrs({ streams: 0 });
  347. }
  348. elem.up();
  349. }
  350. // XEP-0320
  351. const fingerprints
  352. = SDPUtil.findLines(
  353. this.media[mediaindex],
  354. 'a=fingerprint:',
  355. this.session);
  356. fingerprints.forEach(line => {
  357. const fingerprint = SDPUtil.parseFingerprint(line);
  358. fingerprint.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  359. elem.c('fingerprint').t(fingerprint.fingerprint);
  360. delete fingerprint.fingerprint;
  361. const setupLine
  362. = SDPUtil.findLine(
  363. this.media[mediaindex],
  364. 'a=setup:',
  365. this.session);
  366. if (setupLine) {
  367. fingerprint.setup = setupLine.substr(8);
  368. }
  369. elem.attrs(fingerprint);
  370. elem.up(); // end of fingerprint
  371. });
  372. const iceParameters = SDPUtil.iceparams(this.media[mediaindex], this.session);
  373. if (iceParameters) {
  374. iceParameters.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  375. elem.attrs(iceParameters);
  376. // XEP-0176
  377. const candidateLines
  378. = SDPUtil.findLines(
  379. this.media[mediaindex],
  380. 'a=candidate:',
  381. this.session);
  382. candidateLines.forEach(line => { // add any a=candidate lines
  383. const candidate = SDPUtil.candidateToJingle(line);
  384. if (this.failICE) {
  385. candidate.ip = '1.1.1.1';
  386. }
  387. const protocol
  388. = candidate && typeof candidate.protocol === 'string'
  389. ? candidate.protocol.toLowerCase()
  390. : '';
  391. if ((this.removeTcpCandidates
  392. && (protocol === 'tcp' || protocol === 'ssltcp'))
  393. || (this.removeUdpCandidates && protocol === 'udp')) {
  394. return;
  395. }
  396. elem.c('candidate', candidate).up();
  397. });
  398. }
  399. elem.up(); // end of transport
  400. };
  401. // XEP-0293
  402. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  403. const lines
  404. = SDPUtil.findLines(
  405. this.media[mediaindex],
  406. `a=rtcp-fb:${payloadtype}`);
  407. lines.forEach(line => {
  408. const feedback = SDPUtil.parseRTCPFB(line);
  409. if (feedback.type === 'trr-int') {
  410. elem.c('rtcp-fb-trr-int', {
  411. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  412. value: feedback.params[0]
  413. });
  414. elem.up();
  415. } else {
  416. elem.c('rtcp-fb', {
  417. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  418. type: feedback.type
  419. });
  420. if (feedback.params.length > 0) {
  421. elem.attrs({ 'subtype': feedback.params[0] });
  422. }
  423. elem.up();
  424. }
  425. });
  426. };
  427. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  428. let sdp = '';
  429. const feedbackElementTrrInt
  430. = elem.find(
  431. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  432. if (feedbackElementTrrInt.length) {
  433. sdp += 'a=rtcp-fb:* trr-int ';
  434. if (feedbackElementTrrInt.attr('value')) {
  435. sdp += feedbackElementTrrInt.attr('value');
  436. } else {
  437. sdp += '0';
  438. }
  439. sdp += '\r\n';
  440. }
  441. const feedbackElements = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  442. feedbackElements.each((_, fb) => {
  443. sdp += `a=rtcp-fb:${payloadtype} ${fb.getAttribute('type')}`;
  444. if (fb.hasAttribute('subtype')) {
  445. sdp += ` ${fb.getAttribute('subtype')}`;
  446. }
  447. sdp += '\r\n';
  448. });
  449. return sdp;
  450. };
  451. // construct an SDP from a jingle stanza
  452. SDP.prototype.fromJingle = function(jingle) {
  453. const sessionId = Date.now();
  454. // Use a unique session id for every TPC.
  455. this.raw = 'v=0\r\n'
  456. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  457. + 's=-\r\n'
  458. + 't=0 0\r\n';
  459. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  460. // #section-8
  461. const groups
  462. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  463. if (groups.length) {
  464. groups.each((idx, group) => {
  465. const contents
  466. = $(group)
  467. .find('>content')
  468. .map((_, content) => content.getAttribute('name'))
  469. .get();
  470. if (contents.length > 0) {
  471. this.raw
  472. += `a=group:${
  473. group.getAttribute('semantics')
  474. || group.getAttribute('type')} ${
  475. contents.join(' ')}\r\n`;
  476. }
  477. });
  478. }
  479. this.session = this.raw;
  480. jingle.find('>content').each((_, content) => {
  481. const m = this.jingle2media($(content));
  482. this.media.push(m);
  483. });
  484. // reconstruct msid-semantic -- apparently not necessary
  485. /*
  486. var msid = SDPUtil.parseSSRC(this.raw);
  487. if (msid.hasOwnProperty('mslabel')) {
  488. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  489. }
  490. */
  491. this.raw = this.session + this.media.join('');
  492. };
  493. // translate a jingle content element into an an SDP media part
  494. SDP.prototype.jingle2media = function(content) {
  495. const desc = content.find('>description');
  496. const transport = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  497. let sdp = '';
  498. const sctp = transport.find(
  499. '>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  500. const media = { media: desc.attr('media') };
  501. media.port = '9';
  502. if (content.attr('senders') === 'rejected') {
  503. // estos hack to reject an m-line.
  504. media.port = '0';
  505. }
  506. if (transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
  507. media.proto = sctp.length ? 'UDP/DTLS/SCTP' : 'UDP/TLS/RTP/SAVPF';
  508. } else {
  509. media.proto = 'UDP/TLS/RTP/SAVPF';
  510. }
  511. if (sctp.length) {
  512. sdp += `m=application ${media.port} UDP/DTLS/SCTP webrtc-datachannel\r\n`;
  513. sdp += `a=sctp-port:${sctp.attr('number')}\r\n`;
  514. sdp += 'a=max-message-size:262144\r\n';
  515. } else {
  516. media.fmt
  517. = desc
  518. .find('>payload-type')
  519. .map((_, payloadType) => payloadType.getAttribute('id'))
  520. .get();
  521. sdp += `${SDPUtil.buildMLine(media)}\r\n`;
  522. }
  523. sdp += 'c=IN IP4 0.0.0.0\r\n';
  524. if (!sctp.length) {
  525. sdp += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  526. }
  527. // XEP-0176 ICE parameters
  528. if (transport.length) {
  529. if (transport.attr('ufrag')) {
  530. sdp += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
  531. }
  532. if (transport.attr('pwd')) {
  533. sdp += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
  534. }
  535. transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
  536. sdp += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
  537. sdp += ` ${$(fingerprint).text()}`;
  538. sdp += '\r\n';
  539. if (fingerprint.hasAttribute('setup')) {
  540. sdp += `a=setup:${fingerprint.getAttribute('setup')}\r\n`;
  541. }
  542. });
  543. }
  544. // XEP-0176 ICE candidates
  545. transport.find('>candidate')
  546. .each((_, candidate) => {
  547. let protocol = candidate.getAttribute('protocol');
  548. protocol
  549. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  550. if ((this.removeTcpCandidates
  551. && (protocol === 'tcp' || protocol === 'ssltcp'))
  552. || (this.removeUdpCandidates && protocol === 'udp')) {
  553. return;
  554. } else if (this.failICE) {
  555. candidate.setAttribute('ip', '1.1.1.1');
  556. }
  557. sdp += SDPUtil.candidateFromJingle(candidate);
  558. });
  559. switch (content.attr('senders')) {
  560. case 'initiator':
  561. sdp += `a=${MediaDirection.SENDONLY}\r\n`;
  562. break;
  563. case 'responder':
  564. sdp += `a=${MediaDirection.RECVONLY}\r\n`;
  565. break;
  566. case 'none':
  567. sdp += `a=${MediaDirection.INACTIVE}\r\n`;
  568. break;
  569. case 'both':
  570. sdp += `a=${MediaDirection.SENDRECV}\r\n`;
  571. break;
  572. }
  573. sdp += `a=mid:${content.attr('name')}\r\n`;
  574. // <description><rtcp-mux/></description>
  575. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  576. // though
  577. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  578. if (desc.find('>rtcp-mux').length) {
  579. sdp += 'a=rtcp-mux\r\n';
  580. }
  581. desc.find('>payload-type').each((_, payloadType) => {
  582. sdp += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
  583. if ($(payloadType).find('>parameter').length) {
  584. sdp += `a=fmtp:${payloadType.getAttribute('id')} `;
  585. sdp
  586. += $(payloadType)
  587. .find('>parameter')
  588. .map((__, parameter) => {
  589. const name = parameter.getAttribute('name');
  590. return (
  591. (name ? `${name}=` : '')
  592. + parameter.getAttribute('value'));
  593. })
  594. .get()
  595. .join(';');
  596. sdp += '\r\n';
  597. }
  598. // xep-0293
  599. sdp += this.rtcpFbFromJingle($(payloadType), payloadType.getAttribute('id'));
  600. });
  601. // xep-0293
  602. sdp += this.rtcpFbFromJingle(desc, '*');
  603. // xep-0294
  604. desc
  605. .find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]')
  606. .each((_, hdrExt) => {
  607. sdp
  608. += `a=extmap:${hdrExt.getAttribute('id')} ${
  609. hdrExt.getAttribute('uri')}\r\n`;
  610. });
  611. // XEP-0339 handle ssrc-group attributes
  612. desc
  613. .find('>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  614. .each((_, ssrcGroup) => {
  615. const semantics = ssrcGroup.getAttribute('semantics');
  616. const ssrcs
  617. = $(ssrcGroup)
  618. .find('>source')
  619. .map((__, source) => source.getAttribute('ssrc'))
  620. .get();
  621. if (ssrcs.length) {
  622. sdp += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  623. }
  624. });
  625. // XEP-0339 handle source attributes
  626. let userSources = '';
  627. let nonUserSources = '';
  628. desc
  629. .find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  630. .each((_, source) => {
  631. const ssrc = source.getAttribute('ssrc');
  632. let isUserSource = true;
  633. let sourceStr = '';
  634. $(source)
  635. .find('>parameter')
  636. .each((__, parameter) => {
  637. const name = parameter.getAttribute('name');
  638. let value = parameter.getAttribute('value');
  639. value = SDPUtil.filterSpecialChars(value);
  640. sourceStr += `a=ssrc:${ssrc} ${name}`;
  641. if (value && value.length) {
  642. sourceStr += `:${value}`;
  643. }
  644. sourceStr += '\r\n';
  645. if (value?.includes('mixedmslabel')) {
  646. isUserSource = false;
  647. }
  648. });
  649. if (isUserSource) {
  650. userSources += sourceStr;
  651. } else {
  652. nonUserSources += sourceStr;
  653. }
  654. });
  655. // The sdp-interop package is relying the mixedmslabel m line to be the first one in order to set the direction
  656. // to sendrecv.
  657. sdp += nonUserSources + userSources;
  658. return sdp;
  659. };