modified lib-jitsi-meet dev repo
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 24KB

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