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

SDP.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 sctpport
  291. = SDPUtil.findLine(this.media[mediaindex], 'a=sctp-port:', this.session);
  292. const sctpmap
  293. = SDPUtil.findLine(this.media[mediaindex], 'a=sctpmap:', this.session);
  294. if (sctpport) {
  295. const sctpAttrs = SDPUtil.parseSCTPPort(sctpport);
  296. elem.c('sctpmap', {
  297. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  298. number: sctpAttrs, /* SCTP port */
  299. protocol: 'webrtc-datachannel' /* protocol */
  300. });
  301. // The parser currently requires streams to be present
  302. elem.attrs({ streams: 0 });
  303. elem.up();
  304. } else if (sctpmap) {
  305. const sctpAttrs = SDPUtil.parseSCTPMap(sctpmap);
  306. elem.c('sctpmap', {
  307. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  308. number: sctpAttrs[0], /* SCTP port */
  309. protocol: sctpAttrs[1] /* protocol */
  310. });
  311. // Optional stream count attribute
  312. if (sctpAttrs.length > 2) {
  313. elem.attrs({ streams: sctpAttrs[2] });
  314. } else {
  315. elem.attrs({ streams: 0 });
  316. }
  317. elem.up();
  318. }
  319. // XEP-0320
  320. const fingerprints
  321. = SDPUtil.findLines(
  322. this.media[mediaindex],
  323. 'a=fingerprint:',
  324. this.session);
  325. fingerprints.forEach(line => {
  326. const fingerprint = SDPUtil.parseFingerprint(line);
  327. fingerprint.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  328. elem.c('fingerprint').t(fingerprint.fingerprint);
  329. delete fingerprint.fingerprint;
  330. const setupLine
  331. = SDPUtil.findLine(
  332. this.media[mediaindex],
  333. 'a=setup:',
  334. this.session);
  335. if (setupLine) {
  336. fingerprint.setup = setupLine.substr(8);
  337. }
  338. elem.attrs(fingerprint);
  339. elem.up(); // end of fingerprint
  340. });
  341. const iceParameters = SDPUtil.iceparams(this.media[mediaindex], this.session);
  342. if (iceParameters) {
  343. iceParameters.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  344. elem.attrs(iceParameters);
  345. // XEP-0176
  346. const candidateLines
  347. = SDPUtil.findLines(
  348. this.media[mediaindex],
  349. 'a=candidate:',
  350. this.session);
  351. candidateLines.forEach(line => { // add any a=candidate lines
  352. const candidate = SDPUtil.candidateToJingle(line);
  353. if (this.failICE) {
  354. candidate.ip = '1.1.1.1';
  355. }
  356. const protocol
  357. = candidate && typeof candidate.protocol === 'string'
  358. ? candidate.protocol.toLowerCase()
  359. : '';
  360. if ((this.removeTcpCandidates
  361. && (protocol === 'tcp' || protocol === 'ssltcp'))
  362. || (this.removeUdpCandidates && protocol === 'udp')) {
  363. return;
  364. }
  365. elem.c('candidate', candidate).up();
  366. });
  367. }
  368. elem.up(); // end of transport
  369. };
  370. // XEP-0293
  371. SDP.prototype.rtcpFbToJingle = function(mediaindex, elem, payloadtype) {
  372. const lines
  373. = SDPUtil.findLines(
  374. this.media[mediaindex],
  375. `a=rtcp-fb:${payloadtype}`);
  376. lines.forEach(line => {
  377. const feedback = SDPUtil.parseRTCPFB(line);
  378. if (feedback.type === 'trr-int') {
  379. elem.c('rtcp-fb-trr-int', {
  380. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  381. value: feedback.params[0]
  382. });
  383. elem.up();
  384. } else {
  385. elem.c('rtcp-fb', {
  386. xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0',
  387. type: feedback.type
  388. });
  389. if (feedback.params.length > 0) {
  390. elem.attrs({ 'subtype': feedback.params[0] });
  391. }
  392. elem.up();
  393. }
  394. });
  395. };
  396. SDP.prototype.rtcpFbFromJingle = function(elem, payloadtype) { // XEP-0293
  397. let sdp = '';
  398. const feedbackElementTrrInt
  399. = elem.find(
  400. '>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  401. if (feedbackElementTrrInt.length) {
  402. sdp += 'a=rtcp-fb:* trr-int ';
  403. if (feedbackElementTrrInt.attr('value')) {
  404. sdp += feedbackElementTrrInt.attr('value');
  405. } else {
  406. sdp += '0';
  407. }
  408. sdp += '\r\n';
  409. }
  410. const feedbackElements = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  411. feedbackElements.each((_, fb) => {
  412. sdp += `a=rtcp-fb:${payloadtype} ${fb.getAttribute('type')}`;
  413. if (fb.hasAttribute('subtype')) {
  414. sdp += ` ${fb.getAttribute('subtype')}`;
  415. }
  416. sdp += '\r\n';
  417. });
  418. return sdp;
  419. };
  420. // construct an SDP from a jingle stanza
  421. SDP.prototype.fromJingle = function(jingle) {
  422. const sessionId = Date.now();
  423. // Use a unique session id for every TPC.
  424. this.raw = 'v=0\r\n'
  425. + `o=- ${sessionId} 2 IN IP4 0.0.0.0\r\n`
  426. + 's=-\r\n'
  427. + 't=0 0\r\n';
  428. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04
  429. // #section-8
  430. const groups
  431. = $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]');
  432. if (groups.length) {
  433. groups.each((idx, group) => {
  434. const contents
  435. = $(group)
  436. .find('>content')
  437. .map((_, content) => content.getAttribute('name'))
  438. .get();
  439. if (contents.length > 0) {
  440. this.raw
  441. += `a=group:${
  442. group.getAttribute('semantics')
  443. || group.getAttribute('type')} ${
  444. contents.join(' ')}\r\n`;
  445. }
  446. });
  447. }
  448. this.session = this.raw;
  449. jingle.find('>content').each((_, content) => {
  450. const m = this.jingle2media($(content));
  451. this.media.push(m);
  452. });
  453. // reconstruct msid-semantic -- apparently not necessary
  454. /*
  455. var msid = SDPUtil.parseSSRC(this.raw);
  456. if (msid.hasOwnProperty('mslabel')) {
  457. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  458. }
  459. */
  460. this.raw = this.session + this.media.join('');
  461. };
  462. // translate a jingle content element into an an SDP media part
  463. SDP.prototype.jingle2media = function(content) {
  464. const desc = content.find('>description');
  465. const transport = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  466. let sdp = '';
  467. const sctp = transport.find(
  468. '>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  469. const media = { media: desc.attr('media') };
  470. media.port = '9';
  471. if (content.attr('senders') === 'rejected') {
  472. // estos hack to reject an m-line.
  473. media.port = '0';
  474. }
  475. if (transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').length) {
  476. media.proto = sctp.length ? 'UDP/DTLS/SCTP' : 'UDP/TLS/RTP/SAVPF';
  477. } else {
  478. media.proto = 'UDP/TLS/RTP/SAVPF';
  479. }
  480. if (sctp.length) {
  481. sdp += `m=application ${media.port} UDP/DTLS/SCTP webrtc-datachannel\r\n`;
  482. sdp += `a=sctp-port:${sctp.attr('number')}\r\n`;
  483. sdp += 'a=max-message-size:262144\r\n';
  484. } else {
  485. media.fmt
  486. = desc
  487. .find('>payload-type')
  488. .map((_, payloadType) => payloadType.getAttribute('id'))
  489. .get();
  490. sdp += `${SDPUtil.buildMLine(media)}\r\n`;
  491. }
  492. sdp += 'c=IN IP4 0.0.0.0\r\n';
  493. if (!sctp.length) {
  494. sdp += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  495. }
  496. // XEP-0176 ICE parameters
  497. if (transport.length) {
  498. if (transport.attr('ufrag')) {
  499. sdp += `${SDPUtil.buildICEUfrag(transport.attr('ufrag'))}\r\n`;
  500. }
  501. if (transport.attr('pwd')) {
  502. sdp += `${SDPUtil.buildICEPwd(transport.attr('pwd'))}\r\n`;
  503. }
  504. transport.find('>fingerprint[xmlns="urn:xmpp:jingle:apps:dtls:0"]').each((_, fingerprint) => {
  505. sdp += `a=fingerprint:${fingerprint.getAttribute('hash')}`;
  506. sdp += ` ${$(fingerprint).text()}`;
  507. sdp += '\r\n';
  508. if (fingerprint.hasAttribute('setup')) {
  509. sdp += `a=setup:${fingerprint.getAttribute('setup')}\r\n`;
  510. }
  511. });
  512. }
  513. // XEP-0176 ICE candidates
  514. transport.find('>candidate')
  515. .each((_, candidate) => {
  516. let protocol = candidate.getAttribute('protocol');
  517. protocol
  518. = typeof protocol === 'string' ? protocol.toLowerCase() : '';
  519. if ((this.removeTcpCandidates
  520. && (protocol === 'tcp' || protocol === 'ssltcp'))
  521. || (this.removeUdpCandidates && protocol === 'udp')) {
  522. return;
  523. } else if (this.failICE) {
  524. candidate.setAttribute('ip', '1.1.1.1');
  525. }
  526. sdp += SDPUtil.candidateFromJingle(candidate);
  527. });
  528. switch (content.attr('senders')) {
  529. case 'initiator':
  530. sdp += `a=${MediaDirection.SENDONLY}\r\n`;
  531. break;
  532. case 'responder':
  533. sdp += `a=${MediaDirection.RECVONLY}\r\n`;
  534. break;
  535. case 'none':
  536. sdp += `a=${MediaDirection.INACTIVE}\r\n`;
  537. break;
  538. case 'both':
  539. sdp += `a=${MediaDirection.SENDRECV}\r\n`;
  540. break;
  541. }
  542. sdp += `a=mid:${content.attr('name')}\r\n`;
  543. // <description><rtcp-mux/></description>
  544. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec
  545. // though
  546. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  547. if (desc.find('>rtcp-mux').length) {
  548. sdp += 'a=rtcp-mux\r\n';
  549. }
  550. desc.find('>payload-type').each((_, payloadType) => {
  551. sdp += `${SDPUtil.buildRTPMap(payloadType)}\r\n`;
  552. if ($(payloadType).find('>parameter').length) {
  553. sdp += `a=fmtp:${payloadType.getAttribute('id')} `;
  554. sdp
  555. += $(payloadType)
  556. .find('>parameter')
  557. .map((__, parameter) => {
  558. const name = parameter.getAttribute('name');
  559. return (
  560. (name ? `${name}=` : '')
  561. + parameter.getAttribute('value'));
  562. })
  563. .get()
  564. .join('; ');
  565. sdp += '\r\n';
  566. }
  567. // xep-0293
  568. sdp += this.rtcpFbFromJingle($(payloadType), payloadType.getAttribute('id'));
  569. });
  570. // xep-0293
  571. sdp += this.rtcpFbFromJingle(desc, '*');
  572. // xep-0294
  573. desc
  574. .find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]')
  575. .each((_, hdrExt) => {
  576. sdp
  577. += `a=extmap:${hdrExt.getAttribute('id')} ${
  578. hdrExt.getAttribute('uri')}\r\n`;
  579. });
  580. // XEP-0339 handle ssrc-group attributes
  581. desc
  582. .find('>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  583. .each((_, ssrcGroup) => {
  584. const semantics = ssrcGroup.getAttribute('semantics');
  585. const ssrcs
  586. = $(ssrcGroup)
  587. .find('>source')
  588. .map((__, source) => source.getAttribute('ssrc'))
  589. .get();
  590. if (ssrcs.length) {
  591. sdp += `a=ssrc-group:${semantics} ${ssrcs.join(' ')}\r\n`;
  592. }
  593. });
  594. // XEP-0339 handle source attributes
  595. let userSources = '';
  596. let nonUserSources = '';
  597. desc
  598. .find('>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]')
  599. .each((_, source) => {
  600. const ssrc = source.getAttribute('ssrc');
  601. let isUserSource = true;
  602. let sourceStr = '';
  603. $(source)
  604. .find('>parameter')
  605. .each((__, parameter) => {
  606. const name = parameter.getAttribute('name');
  607. let value = parameter.getAttribute('value');
  608. value = SDPUtil.filterSpecialChars(value);
  609. sourceStr += `a=ssrc:${ssrc} ${name}`;
  610. if (value && value.length) {
  611. sourceStr += `:${value}`;
  612. }
  613. sourceStr += '\r\n';
  614. if (value?.includes('mixedmslabel')) {
  615. isUserSource = false;
  616. }
  617. });
  618. if (isUserSource) {
  619. userSources += sourceStr;
  620. } else {
  621. nonUserSources += sourceStr;
  622. }
  623. });
  624. // The sdp-interop package is relying the mixedmslabel m line to be the first one in order to set the direction
  625. // to sendrecv.
  626. sdp += nonUserSources + userSources;
  627. return sdp;
  628. };