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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. msid = APP.RTC.localAudio._getId();
  238. } else {
  239. msid = APP.RTC.localVideo._getId();
  240. }
  241. if(msid != null) {
  242. msid = SDPUtil.filter_special_chars(msid);
  243. elem.c('parameter');
  244. elem.attrs({name: "msid", value:msid});
  245. elem.up();
  246. elem.c('parameter');
  247. elem.attrs({name: "mslabel", value:msid});
  248. elem.up();
  249. elem.c('parameter');
  250. elem.attrs({name: "label", value:msid});
  251. elem.up();
  252. }
  253. }
  254. elem.up();
  255. // XEP-0339 handle ssrc-group attributes
  256. var ssrc_group_lines = SDPUtil.find_lines(this.media[i], 'a=ssrc-group:');
  257. ssrc_group_lines.forEach(function(line) {
  258. var idx = line.indexOf(' ');
  259. var semantics = line.substr(0, idx).substr(13);
  260. var ssrcs = line.substr(14 + semantics.length).split(' ');
  261. if (ssrcs.length) {
  262. elem.c('ssrc-group', { semantics: semantics, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  263. ssrcs.forEach(function(ssrc) {
  264. elem.c('source', { ssrc: ssrc })
  265. .up();
  266. });
  267. elem.up();
  268. }
  269. });
  270. }
  271. if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
  272. elem.c('rtcp-mux').up();
  273. }
  274. // XEP-0293 -- map a=rtcp-fb:*
  275. this.rtcpFbToJingle(i, elem, '*');
  276. // XEP-0294
  277. lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
  278. if (lines.length) {
  279. for (j = 0; j < lines.length; j++) {
  280. tmp = SDPUtil.parse_extmap(lines[j]);
  281. elem.c('rtp-hdrext', { xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  282. uri: tmp.uri,
  283. id: tmp.value });
  284. if (tmp.hasOwnProperty('direction')) {
  285. switch (tmp.direction) {
  286. case 'sendonly':
  287. elem.attrs({senders: 'responder'});
  288. break;
  289. case 'recvonly':
  290. elem.attrs({senders: 'initiator'});
  291. break;
  292. case 'sendrecv':
  293. elem.attrs({senders: 'both'});
  294. break;
  295. case 'inactive':
  296. elem.attrs({senders: 'none'});
  297. break;
  298. }
  299. }
  300. // TODO: handle params
  301. elem.up();
  302. }
  303. }
  304. elem.up(); // end of description
  305. }
  306. // map ice-ufrag/pwd, dtls fingerprint, candidates
  307. this.transportToJingle(i, elem);
  308. if (SDPUtil.find_line(this.media[i], 'a=sendrecv', this.session)) {
  309. elem.attrs({senders: 'both'});
  310. } else if (SDPUtil.find_line(this.media[i], 'a=sendonly', this.session)) {
  311. elem.attrs({senders: 'initiator'});
  312. } else if (SDPUtil.find_line(this.media[i], 'a=recvonly', this.session)) {
  313. elem.attrs({senders: 'responder'});
  314. } else if (SDPUtil.find_line(this.media[i], 'a=inactive', this.session)) {
  315. elem.attrs({senders: 'none'});
  316. }
  317. if (mline.port == '0') {
  318. // estos hack to reject an m-line
  319. elem.attrs({senders: 'rejected'});
  320. }
  321. elem.up(); // end of content
  322. }
  323. elem.up();
  324. return elem;
  325. };
  326. SDP.prototype.transportToJingle = function (mediaindex, elem) {
  327. var tmp, sctpmap, sctpAttrs, fingerprints;
  328. var self = this;
  329. elem.c('transport');
  330. // XEP-0343 DTLS/SCTP
  331. sctpmap
  332. = SDPUtil.find_line(this.media[mediaindex], 'a=sctpmap:', self.session);
  333. if (sctpmap) {
  334. sctpAttrs = SDPUtil.parse_sctpmap(sctpmap);
  335. elem.c('sctpmap', {
  336. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  337. number: sctpAttrs[0], /* SCTP port */
  338. protocol: sctpAttrs[1] /* protocol */
  339. });
  340. // Optional stream count attribute
  341. if (sctpAttrs.length > 2)
  342. elem.attrs({ streams: sctpAttrs[2]});
  343. elem.up();
  344. }
  345. // XEP-0320
  346. fingerprints = SDPUtil.find_lines(this.media[mediaindex], 'a=fingerprint:', this.session);
  347. fingerprints.forEach(function(line) {
  348. tmp = SDPUtil.parse_fingerprint(line);
  349. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  350. elem.c('fingerprint').t(tmp.fingerprint);
  351. delete tmp.fingerprint;
  352. line = SDPUtil.find_line(self.media[mediaindex], 'a=setup:', self.session);
  353. if (line) {
  354. tmp.setup = line.substr(8);
  355. }
  356. elem.attrs(tmp);
  357. elem.up(); // end of fingerprint
  358. });
  359. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  360. if (tmp) {
  361. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  362. elem.attrs(tmp);
  363. // XEP-0176
  364. if (SDPUtil.find_line(this.media[mediaindex], 'a=candidate:', this.session)) { // add any a=candidate lines
  365. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=candidate:', this.session);
  366. lines.forEach(function (line) {
  367. var candidate = SDPUtil.candidateToJingle(line);
  368. var protocol = (candidate &&
  369. typeof candidate.protocol === 'string')
  370. ? candidate.protocol.toLowerCase() : '';
  371. if ((self.removeTcpCandidates && protocol === 'tcp') ||
  372. (self.removeUdpCandidates && protocol === 'udp')) {
  373. return;
  374. }
  375. elem.c('candidate', candidate).up();
  376. });
  377. }
  378. }
  379. elem.up(); // end of transport
  380. }
  381. SDP.prototype.rtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
  382. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
  383. lines.forEach(function (line) {
  384. var tmp = SDPUtil.parse_rtcpfb(line);
  385. if (tmp.type == 'trr-int') {
  386. elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
  387. elem.up();
  388. } else {
  389. elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
  390. if (tmp.params.length > 0) {
  391. elem.attrs({'subtype': tmp.params[0]});
  392. }
  393. elem.up();
  394. }
  395. });
  396. };
  397. SDP.prototype.rtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
  398. var media = '';
  399. var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  400. if (tmp.length) {
  401. media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
  402. if (tmp.attr('value')) {
  403. media += tmp.attr('value');
  404. } else {
  405. media += '0';
  406. }
  407. media += '\r\n';
  408. }
  409. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  410. tmp.each(function () {
  411. media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
  412. if ($(this).attr('subtype')) {
  413. media += ' ' + $(this).attr('subtype');
  414. }
  415. media += '\r\n';
  416. });
  417. return media;
  418. };
  419. // construct an SDP from a jingle stanza
  420. SDP.prototype.fromJingle = function (jingle) {
  421. var self = this;
  422. this.raw = 'v=0\r\n' +
  423. 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
  424. 's=-\r\n' +
  425. 't=0 0\r\n';
  426. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
  427. if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
  428. $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
  429. var contents = $(group).find('>content').map(function (idx, content) {
  430. return content.getAttribute('name');
  431. }).get();
  432. if (contents.length > 0) {
  433. self.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
  434. }
  435. });
  436. }
  437. this.session = this.raw;
  438. jingle.find('>content').each(function () {
  439. var m = self.jingle2media($(this));
  440. self.media.push(m);
  441. });
  442. // reconstruct msid-semantic -- apparently not necessary
  443. /*
  444. var msid = SDPUtil.parse_ssrc(this.raw);
  445. if (msid.hasOwnProperty('mslabel')) {
  446. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  447. }
  448. */
  449. this.raw = this.session + this.media.join('');
  450. };
  451. // translate a jingle content element into an an SDP media part
  452. SDP.prototype.jingle2media = function (content) {
  453. var media = '',
  454. desc = content.find('description'),
  455. ssrc = desc.attr('ssrc'),
  456. self = this,
  457. tmp;
  458. var sctp = content.find(
  459. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  460. tmp = { media: desc.attr('media') };
  461. tmp.port = '1';
  462. if (content.attr('senders') == 'rejected') {
  463. // estos hack to reject an m-line.
  464. tmp.port = '0';
  465. }
  466. if (content.find('>transport>fingerprint').length
  467. || desc.find('encryption').length) {
  468. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  469. } else {
  470. tmp.proto = 'RTP/AVPF';
  471. }
  472. if (!sctp.length) {
  473. tmp.fmt = desc.find('payload-type').map(
  474. function () { return this.getAttribute('id'); }).get();
  475. media += SDPUtil.build_mline(tmp) + '\r\n';
  476. } else {
  477. media += 'm=application 1 DTLS/SCTP ' + sctp.attr('number') + '\r\n';
  478. media += 'a=sctpmap:' + sctp.attr('number') +
  479. ' ' + sctp.attr('protocol');
  480. var streamCount = sctp.attr('streams');
  481. if (streamCount)
  482. media += ' ' + streamCount + '\r\n';
  483. else
  484. media += '\r\n';
  485. }
  486. media += 'c=IN IP4 0.0.0.0\r\n';
  487. if (!sctp.length)
  488. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  489. tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  490. if (tmp.length) {
  491. if (tmp.attr('ufrag')) {
  492. media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
  493. }
  494. if (tmp.attr('pwd')) {
  495. media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
  496. }
  497. tmp.find('>fingerprint').each(function () {
  498. // FIXME: check namespace at some point
  499. media += 'a=fingerprint:' + this.getAttribute('hash');
  500. media += ' ' + $(this).text();
  501. media += '\r\n';
  502. if (this.getAttribute('setup')) {
  503. media += 'a=setup:' + this.getAttribute('setup') + '\r\n';
  504. }
  505. });
  506. }
  507. switch (content.attr('senders')) {
  508. case 'initiator':
  509. media += 'a=sendonly\r\n';
  510. break;
  511. case 'responder':
  512. media += 'a=recvonly\r\n';
  513. break;
  514. case 'none':
  515. media += 'a=inactive\r\n';
  516. break;
  517. case 'both':
  518. media += 'a=sendrecv\r\n';
  519. break;
  520. }
  521. media += 'a=mid:' + content.attr('name') + '\r\n';
  522. // <description><rtcp-mux/></description>
  523. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
  524. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  525. if (desc.find('rtcp-mux').length) {
  526. media += 'a=rtcp-mux\r\n';
  527. }
  528. if (desc.find('encryption').length) {
  529. desc.find('encryption>crypto').each(function () {
  530. media += 'a=crypto:' + this.getAttribute('tag');
  531. media += ' ' + this.getAttribute('crypto-suite');
  532. media += ' ' + this.getAttribute('key-params');
  533. if (this.getAttribute('session-params')) {
  534. media += ' ' + this.getAttribute('session-params');
  535. }
  536. media += '\r\n';
  537. });
  538. }
  539. desc.find('payload-type').each(function () {
  540. media += SDPUtil.build_rtpmap(this) + '\r\n';
  541. if ($(this).find('>parameter').length) {
  542. media += 'a=fmtp:' + this.getAttribute('id') + ' ';
  543. media += $(this).find('parameter').map(function () {
  544. return (this.getAttribute('name')
  545. ? (this.getAttribute('name') + '=') : '') +
  546. this.getAttribute('value');
  547. }).get().join('; ');
  548. media += '\r\n';
  549. }
  550. // xep-0293
  551. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  552. });
  553. // xep-0293
  554. media += self.rtcpFbFromJingle(desc, '*');
  555. // xep-0294
  556. tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  557. tmp.each(function () {
  558. media += 'a=extmap:' + this.getAttribute('id') + ' ' + this.getAttribute('uri') + '\r\n';
  559. });
  560. content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
  561. var protocol = this.getAttribute('protocol');
  562. protocol = (typeof protocol === 'string') ? protocol.toLowerCase(): '';
  563. if ((self.removeTcpCandidates && protocol === 'tcp') ||
  564. (self.removeUdpCandidates && protocol === 'udp')) {
  565. return;
  566. }
  567. media += SDPUtil.candidateFromJingle(this);
  568. });
  569. // XEP-0339 handle ssrc-group attributes
  570. content.find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  571. var semantics = this.getAttribute('semantics');
  572. var ssrcs = $(this).find('>source').map(function() {
  573. return this.getAttribute('ssrc');
  574. }).get();
  575. if (ssrcs.length) {
  576. media += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  577. }
  578. });
  579. tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  580. tmp.each(function () {
  581. var ssrc = this.getAttribute('ssrc');
  582. $(this).find('>parameter').each(function () {
  583. var name = this.getAttribute('name');
  584. var value = this.getAttribute('value');
  585. value = SDPUtil.filter_special_chars(value);
  586. media += 'a=ssrc:' + ssrc + ' ' + name;
  587. if (value && value.length)
  588. media += ':' + value;
  589. media += '\r\n';
  590. });
  591. });
  592. return media;
  593. };
  594. module.exports = SDP;