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.

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