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

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