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

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