modified lib-jitsi-meet dev repo
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SDP.js 23KB

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