Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SDP.js 24KB

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