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

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