modified lib-jitsi-meet dev repo
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

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