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.

SDP.js 23KB

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