您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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. /**
  7. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  8. * @type {boolean}
  9. */
  10. this.removeTcpCandidates = false;
  11. /**
  12. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  13. * @type {boolean}
  14. */
  15. this.removeUdpCandidates = false;
  16. this.media = sdp.split('\r\nm=');
  17. for (var i = 1; i < this.media.length; i++) {
  18. this.media[i] = 'm=' + this.media[i];
  19. if (i != this.media.length - 1) {
  20. this.media[i] += '\r\n';
  21. }
  22. }
  23. this.session = this.media.shift() + '\r\n';
  24. this.raw = this.session + this.media.join('');
  25. }
  26. /**
  27. * Returns map of MediaChannel mapped per channel idx.
  28. */
  29. SDP.prototype.getMediaSsrcMap = function() {
  30. var self = this;
  31. var media_ssrcs = {};
  32. var tmp;
  33. for (var mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
  34. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc:');
  35. var mid = SDPUtil.parse_mid(SDPUtil.find_line(self.media[mediaindex], 'a=mid:'));
  36. var media = {
  37. mediaindex: mediaindex,
  38. mid: mid,
  39. ssrcs: {},
  40. ssrcGroups: []
  41. };
  42. media_ssrcs[mediaindex] = media;
  43. tmp.forEach(function (line) {
  44. var linessrc = line.substring(7).split(' ')[0];
  45. // allocate new ChannelSsrc
  46. if(!media.ssrcs[linessrc]) {
  47. media.ssrcs[linessrc] = {
  48. ssrc: linessrc,
  49. lines: []
  50. };
  51. }
  52. media.ssrcs[linessrc].lines.push(line);
  53. });
  54. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc-group:');
  55. tmp.forEach(function(line) {
  56. var idx = line.indexOf(' ');
  57. var semantics = line.substr(0, idx).substr(13);
  58. var ssrcs = line.substr(14 + semantics.length).split(' ');
  59. if (ssrcs.length) {
  60. media.ssrcGroups.push({
  61. semantics: semantics,
  62. ssrcs: ssrcs
  63. });
  64. }
  65. });
  66. }
  67. return media_ssrcs;
  68. };
  69. /**
  70. * Returns <tt>true</tt> if this SDP contains given SSRC.
  71. * @param ssrc the ssrc to check.
  72. * @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
  73. */
  74. SDP.prototype.containsSSRC = function (ssrc) {
  75. // FIXME this code is really strange - improve it if you can
  76. var medias = this.getMediaSsrcMap();
  77. var result = false;
  78. Object.keys(medias).forEach(function (mediaindex) {
  79. if (result)
  80. return;
  81. if (medias[mediaindex].ssrcs[ssrc]) {
  82. result = true;
  83. }
  84. });
  85. return result;
  86. };
  87. // remove iSAC and CN from SDP
  88. SDP.prototype.mangle = function () {
  89. var i, j, mline, lines, rtpmap, newdesc;
  90. for (i = 0; i < this.media.length; i++) {
  91. lines = this.media[i].split('\r\n');
  92. lines.pop(); // remove empty last element
  93. mline = SDPUtil.parse_mline(lines.shift());
  94. if (mline.media != 'audio')
  95. continue;
  96. newdesc = '';
  97. mline.fmt.length = 0;
  98. for (j = 0; j < lines.length; j++) {
  99. if (lines[j].substr(0, 9) == 'a=rtpmap:') {
  100. rtpmap = SDPUtil.parse_rtpmap(lines[j]);
  101. if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC')
  102. continue;
  103. mline.fmt.push(rtpmap.id);
  104. newdesc += lines[j] + '\r\n';
  105. } else {
  106. newdesc += lines[j] + '\r\n';
  107. }
  108. }
  109. this.media[i] = SDPUtil.build_mline(mline) + '\r\n';
  110. this.media[i] += 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 || desc.find('encryption').length) {
  467. if (sctp.length)
  468. tmp.proto = 'DTLS/SCTP';
  469. else
  470. tmp.proto = '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;