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