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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. * A flag will make {@link transportToJingle} and {@link jingle2media} replace
  20. * ICE candidates IPs with invalid value of '1.1.1.1' which will cause ICE
  21. * failure. The flag is used in the automated testing.
  22. * @type {boolean}
  23. */
  24. SDP.prototype.failICE = false;
  25. /**
  26. * Whether or not to remove TCP ice candidates when translating from/to jingle.
  27. * @type {boolean}
  28. */
  29. SDP.prototype.removeTcpCandidates = false;
  30. /**
  31. * Whether or not to remove UDP ice candidates when translating from/to jingle.
  32. * @type {boolean}
  33. */
  34. SDP.prototype.removeUdpCandidates = false;
  35. /**
  36. * Returns map of MediaChannel mapped per channel idx.
  37. */
  38. SDP.prototype.getMediaSsrcMap = function() {
  39. var self = this;
  40. var media_ssrcs = {};
  41. var tmp;
  42. for (var mediaindex = 0; mediaindex < self.media.length; mediaindex++) {
  43. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc:');
  44. var mid = SDPUtil.parse_mid(SDPUtil.find_line(self.media[mediaindex], 'a=mid:'));
  45. var media = {
  46. mediaindex: mediaindex,
  47. mid: mid,
  48. ssrcs: {},
  49. ssrcGroups: []
  50. };
  51. media_ssrcs[mediaindex] = media;
  52. tmp.forEach(function (line) {
  53. var linessrc = line.substring(7).split(' ')[0];
  54. // allocate new ChannelSsrc
  55. if(!media.ssrcs[linessrc]) {
  56. media.ssrcs[linessrc] = {
  57. ssrc: linessrc,
  58. lines: []
  59. };
  60. }
  61. media.ssrcs[linessrc].lines.push(line);
  62. });
  63. tmp = SDPUtil.find_lines(self.media[mediaindex], 'a=ssrc-group:');
  64. tmp.forEach(function(line) {
  65. var idx = line.indexOf(' ');
  66. var semantics = line.substr(0, idx).substr(13);
  67. var ssrcs = line.substr(14 + semantics.length).split(' ');
  68. if (ssrcs.length) {
  69. media.ssrcGroups.push({
  70. semantics: semantics,
  71. ssrcs: ssrcs
  72. });
  73. }
  74. });
  75. }
  76. return media_ssrcs;
  77. };
  78. /**
  79. * Returns <tt>true</tt> if this SDP contains given SSRC.
  80. * @param ssrc the ssrc to check.
  81. * @returns {boolean} <tt>true</tt> if this SDP contains given SSRC.
  82. */
  83. SDP.prototype.containsSSRC = function (ssrc) {
  84. // FIXME this code is really strange - improve it if you can
  85. var medias = this.getMediaSsrcMap();
  86. var result = false;
  87. Object.keys(medias).forEach(function (mediaindex) {
  88. if (result)
  89. return;
  90. if (medias[mediaindex].ssrcs[ssrc]) {
  91. result = true;
  92. }
  93. });
  94. return result;
  95. };
  96. // remove iSAC and CN from SDP
  97. SDP.prototype.mangle = function () {
  98. var i, j, mline, lines, rtpmap, newdesc;
  99. for (i = 0; i < this.media.length; i++) {
  100. lines = this.media[i].split('\r\n');
  101. lines.pop(); // remove empty last element
  102. mline = SDPUtil.parse_mline(lines.shift());
  103. if (mline.media != 'audio')
  104. continue;
  105. newdesc = '';
  106. mline.fmt.length = 0;
  107. for (j = 0; j < lines.length; j++) {
  108. if (lines[j].substr(0, 9) == 'a=rtpmap:') {
  109. rtpmap = SDPUtil.parse_rtpmap(lines[j]);
  110. if (rtpmap.name == 'CN' || rtpmap.name == 'ISAC')
  111. continue;
  112. mline.fmt.push(rtpmap.id);
  113. }
  114. newdesc += lines[j] + '\r\n';
  115. }
  116. this.media[i] = SDPUtil.build_mline(mline) + '\r\n' + newdesc;
  117. }
  118. this.raw = this.session + this.media.join('');
  119. };
  120. // remove lines matching prefix from session section
  121. SDP.prototype.removeSessionLines = function(prefix) {
  122. var self = this;
  123. var lines = SDPUtil.find_lines(this.session, prefix);
  124. lines.forEach(function(line) {
  125. self.session = self.session.replace(line + '\r\n', '');
  126. });
  127. this.raw = this.session + this.media.join('');
  128. return lines;
  129. };
  130. // remove lines matching prefix from a media section specified by mediaindex
  131. // TODO: non-numeric mediaindex could match mid
  132. SDP.prototype.removeMediaLines = function(mediaindex, prefix) {
  133. var self = this;
  134. var lines = SDPUtil.find_lines(this.media[mediaindex], prefix);
  135. lines.forEach(function(line) {
  136. self.media[mediaindex] = self.media[mediaindex].replace(line + '\r\n', '');
  137. });
  138. this.raw = this.session + this.media.join('');
  139. return lines;
  140. };
  141. // add content's to a jingle element
  142. SDP.prototype.toJingle = function (elem, thecreator) {
  143. var i, j, k, mline, ssrc, rtpmap, tmp, lines;
  144. // new bundle plan
  145. lines = SDPUtil.find_lines(this.session, 'a=group:');
  146. if (lines.length) {
  147. for (i = 0; i < lines.length; i++) {
  148. tmp = lines[i].split(' ');
  149. var semantics = tmp.shift().substr(8);
  150. elem.c('group', {xmlns: 'urn:xmpp:jingle:apps:grouping:0', semantics:semantics});
  151. for (j = 0; j < tmp.length; j++) {
  152. elem.c('content', {name: tmp[j]}).up();
  153. }
  154. elem.up();
  155. }
  156. }
  157. for (i = 0; i < this.media.length; i++) {
  158. mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
  159. if (!(mline.media === 'audio' ||
  160. mline.media === 'video' ||
  161. mline.media === 'application')) {
  162. continue;
  163. }
  164. var assrcline = SDPUtil.find_line(this.media[i], 'a=ssrc:');
  165. if (assrcline) {
  166. ssrc = assrcline.substring(7).split(' ')[0]; // take the first
  167. } else {
  168. ssrc = false;
  169. }
  170. elem.c('content', {creator: thecreator, name: mline.media});
  171. var amidline = SDPUtil.find_line(this.media[i], 'a=mid:');
  172. if (amidline) {
  173. // prefer identifier from a=mid if present
  174. var mid = SDPUtil.parse_mid(amidline);
  175. elem.attrs({ name: mid });
  176. }
  177. if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length) {
  178. elem.c('description',
  179. {xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  180. media: mline.media });
  181. if (ssrc) {
  182. elem.attrs({ssrc: ssrc});
  183. }
  184. for (j = 0; j < mline.fmt.length; j++) {
  185. rtpmap = SDPUtil.find_line(this.media[i], 'a=rtpmap:' + mline.fmt[j]);
  186. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  187. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
  188. var afmtpline = SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j]);
  189. if (afmtpline) {
  190. tmp = SDPUtil.parse_fmtp(afmtpline);
  191. for (k = 0; k < tmp.length; k++) {
  192. elem.c('parameter', tmp[k]).up();
  193. }
  194. }
  195. this.rtcpFbToJingle(i, elem, mline.fmt[j]); // XEP-0293 -- map a=rtcp-fb
  196. elem.up();
  197. }
  198. var crypto = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
  199. if (crypto.length) {
  200. elem.c('encryption', {required: 1});
  201. crypto.forEach(function(line) {
  202. elem.c('crypto', SDPUtil.parse_crypto(line)).up();
  203. });
  204. elem.up(); // end of encryption
  205. }
  206. if (ssrc) {
  207. // new style mapping
  208. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  209. // FIXME: group by ssrc and support multiple different ssrcs
  210. var ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
  211. if(ssrclines.length > 0) {
  212. ssrclines.forEach(function (line) {
  213. var idx = line.indexOf(' ');
  214. var linessrc = line.substr(0, idx).substr(7);
  215. if (linessrc != ssrc) {
  216. elem.up();
  217. ssrc = linessrc;
  218. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  219. }
  220. var kv = line.substr(idx + 1);
  221. elem.c('parameter');
  222. if (kv.indexOf(':') == -1) {
  223. elem.attrs({ name: kv });
  224. } else {
  225. var k = kv.split(':', 2)[0];
  226. elem.attrs({ name: k });
  227. var v = kv.split(':', 2)[1];
  228. v = SDPUtil.filter_special_chars(v);
  229. elem.attrs({ value: v });
  230. }
  231. elem.up();
  232. });
  233. } else {
  234. elem.up();
  235. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  236. elem.c('parameter');
  237. elem.attrs({name: "cname", value:Math.random().toString(36).substring(7)});
  238. elem.up();
  239. var msid = null;
  240. if(mline.media == "audio") {
  241. // FIXME what is this ? global APP.RTC in SDP ?
  242. msid = APP.RTC.localAudio._getId();
  243. } else {
  244. msid = APP.RTC.localVideo._getId();
  245. }
  246. if(msid != null) {
  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. if (self.failICE) {
  374. candidate.ip = "1.1.1.1";
  375. }
  376. var protocol = (candidate &&
  377. typeof candidate.protocol === 'string')
  378. ? candidate.protocol.toLowerCase() : '';
  379. if ((self.removeTcpCandidates && protocol === 'tcp') ||
  380. (self.removeUdpCandidates && protocol === 'udp')) {
  381. return;
  382. }
  383. elem.c('candidate', candidate).up();
  384. });
  385. }
  386. }
  387. elem.up(); // end of transport
  388. };
  389. SDP.prototype.rtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
  390. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
  391. lines.forEach(function (line) {
  392. var tmp = SDPUtil.parse_rtcpfb(line);
  393. if (tmp.type == 'trr-int') {
  394. elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
  395. elem.up();
  396. } else {
  397. elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
  398. if (tmp.params.length > 0) {
  399. elem.attrs({'subtype': tmp.params[0]});
  400. }
  401. elem.up();
  402. }
  403. });
  404. };
  405. SDP.prototype.rtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
  406. var media = '';
  407. var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  408. if (tmp.length) {
  409. media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
  410. if (tmp.attr('value')) {
  411. media += tmp.attr('value');
  412. } else {
  413. media += '0';
  414. }
  415. media += '\r\n';
  416. }
  417. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  418. tmp.each(function () {
  419. media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
  420. if ($(this).attr('subtype')) {
  421. media += ' ' + $(this).attr('subtype');
  422. }
  423. media += '\r\n';
  424. });
  425. return media;
  426. };
  427. // construct an SDP from a jingle stanza
  428. SDP.prototype.fromJingle = function (jingle) {
  429. var self = this;
  430. this.raw = 'v=0\r\n' +
  431. 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
  432. 's=-\r\n' +
  433. 't=0 0\r\n';
  434. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
  435. if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
  436. $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
  437. var contents = $(group).find('>content').map(function (idx, content) {
  438. return content.getAttribute('name');
  439. }).get();
  440. if (contents.length > 0) {
  441. self.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
  442. }
  443. });
  444. }
  445. this.session = this.raw;
  446. jingle.find('>content').each(function () {
  447. var m = self.jingle2media($(this));
  448. self.media.push(m);
  449. });
  450. // reconstruct msid-semantic -- apparently not necessary
  451. /*
  452. var msid = SDPUtil.parse_ssrc(this.raw);
  453. if (msid.hasOwnProperty('mslabel')) {
  454. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  455. }
  456. */
  457. this.raw = this.session + this.media.join('');
  458. };
  459. // translate a jingle content element into an an SDP media part
  460. SDP.prototype.jingle2media = function (content) {
  461. var media = '',
  462. desc = content.find('description'),
  463. self = this,
  464. tmp;
  465. var sctp = content.find(
  466. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  467. tmp = { media: desc.attr('media') };
  468. tmp.port = '1';
  469. if (content.attr('senders') == 'rejected') {
  470. // estos hack to reject an m-line.
  471. tmp.port = '0';
  472. }
  473. if (content.find('>transport>fingerprint').length
  474. || desc.find('encryption').length) {
  475. tmp.proto = sctp.length ? 'DTLS/SCTP' : '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. } else if (self.failICE) {
  574. this.setAttribute('ip', '1.1.1.1');
  575. }
  576. media += SDPUtil.candidateFromJingle(this);
  577. });
  578. // XEP-0339 handle ssrc-group attributes
  579. content.find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  580. var semantics = this.getAttribute('semantics');
  581. var ssrcs = $(this).find('>source').map(function() {
  582. return this.getAttribute('ssrc');
  583. }).get();
  584. if (ssrcs.length) {
  585. media += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  586. }
  587. });
  588. tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  589. tmp.each(function () {
  590. var ssrc = this.getAttribute('ssrc');
  591. $(this).find('>parameter').each(function () {
  592. var name = this.getAttribute('name');
  593. var value = this.getAttribute('value');
  594. value = SDPUtil.filter_special_chars(value);
  595. media += 'a=ssrc:' + ssrc + ' ' + name;
  596. if (value && value.length)
  597. media += ':' + value;
  598. media += '\r\n';
  599. });
  600. });
  601. return media;
  602. };
  603. module.exports = SDP;