您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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