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

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