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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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
  380. && (protocol === 'tcp' || protocol === 'ssltcp')) ||
  381. (self.removeUdpCandidates && protocol === 'udp')) {
  382. return;
  383. }
  384. elem.c('candidate', candidate).up();
  385. });
  386. }
  387. }
  388. elem.up(); // end of transport
  389. };
  390. SDP.prototype.rtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
  391. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
  392. lines.forEach(function (line) {
  393. var tmp = SDPUtil.parse_rtcpfb(line);
  394. if (tmp.type == 'trr-int') {
  395. elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
  396. elem.up();
  397. } else {
  398. elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
  399. if (tmp.params.length > 0) {
  400. elem.attrs({'subtype': tmp.params[0]});
  401. }
  402. elem.up();
  403. }
  404. });
  405. };
  406. SDP.prototype.rtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
  407. var media = '';
  408. var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  409. if (tmp.length) {
  410. media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
  411. if (tmp.attr('value')) {
  412. media += tmp.attr('value');
  413. } else {
  414. media += '0';
  415. }
  416. media += '\r\n';
  417. }
  418. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  419. tmp.each(function () {
  420. media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
  421. if ($(this).attr('subtype')) {
  422. media += ' ' + $(this).attr('subtype');
  423. }
  424. media += '\r\n';
  425. });
  426. return media;
  427. };
  428. // construct an SDP from a jingle stanza
  429. SDP.prototype.fromJingle = function (jingle) {
  430. var self = this;
  431. this.raw = 'v=0\r\n' +
  432. 'o=- 1923518516 2 IN IP4 0.0.0.0\r\n' +// FIXME
  433. 's=-\r\n' +
  434. 't=0 0\r\n';
  435. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
  436. if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
  437. $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
  438. var contents = $(group).find('>content').map(function (idx, content) {
  439. return content.getAttribute('name');
  440. }).get();
  441. if (contents.length > 0) {
  442. self.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
  443. }
  444. });
  445. }
  446. this.session = this.raw;
  447. jingle.find('>content').each(function () {
  448. var m = self.jingle2media($(this));
  449. self.media.push(m);
  450. });
  451. // reconstruct msid-semantic -- apparently not necessary
  452. /*
  453. var msid = SDPUtil.parse_ssrc(this.raw);
  454. if (msid.hasOwnProperty('mslabel')) {
  455. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  456. }
  457. */
  458. this.raw = this.session + this.media.join('');
  459. };
  460. // translate a jingle content element into an an SDP media part
  461. SDP.prototype.jingle2media = function (content) {
  462. var media = '',
  463. desc = content.find('description'),
  464. self = this,
  465. tmp;
  466. var sctp = content.find(
  467. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  468. tmp = { media: desc.attr('media') };
  469. tmp.port = '1';
  470. if (content.attr('senders') == 'rejected') {
  471. // estos hack to reject an m-line.
  472. tmp.port = '0';
  473. }
  474. if (content.find('>transport>fingerprint').length
  475. || desc.find('encryption').length) {
  476. tmp.proto = sctp.length ? 'DTLS/SCTP' : 'RTP/SAVPF';
  477. } else {
  478. tmp.proto = 'RTP/AVPF';
  479. }
  480. if (!sctp.length) {
  481. tmp.fmt = desc.find('payload-type').map(
  482. function () { return this.getAttribute('id'); }).get();
  483. media += SDPUtil.build_mline(tmp) + '\r\n';
  484. } else {
  485. media += 'm=application 1 DTLS/SCTP ' + sctp.attr('number') + '\r\n';
  486. media += 'a=sctpmap:' + sctp.attr('number') +
  487. ' ' + sctp.attr('protocol');
  488. var streamCount = sctp.attr('streams');
  489. if (streamCount)
  490. media += ' ' + streamCount + '\r\n';
  491. else
  492. media += '\r\n';
  493. }
  494. media += 'c=IN IP4 0.0.0.0\r\n';
  495. if (!sctp.length)
  496. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  497. tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  498. if (tmp.length) {
  499. if (tmp.attr('ufrag')) {
  500. media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
  501. }
  502. if (tmp.attr('pwd')) {
  503. media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
  504. }
  505. tmp.find('>fingerprint').each(function () {
  506. // FIXME: check namespace at some point
  507. media += 'a=fingerprint:' + this.getAttribute('hash');
  508. media += ' ' + $(this).text();
  509. media += '\r\n';
  510. if (this.getAttribute('setup')) {
  511. media += 'a=setup:' + this.getAttribute('setup') + '\r\n';
  512. }
  513. });
  514. }
  515. switch (content.attr('senders')) {
  516. case 'initiator':
  517. media += 'a=sendonly\r\n';
  518. break;
  519. case 'responder':
  520. media += 'a=recvonly\r\n';
  521. break;
  522. case 'none':
  523. media += 'a=inactive\r\n';
  524. break;
  525. case 'both':
  526. media += 'a=sendrecv\r\n';
  527. break;
  528. }
  529. media += 'a=mid:' + content.attr('name') + '\r\n';
  530. // <description><rtcp-mux/></description>
  531. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
  532. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  533. if (desc.find('rtcp-mux').length) {
  534. media += 'a=rtcp-mux\r\n';
  535. }
  536. if (desc.find('encryption').length) {
  537. desc.find('encryption>crypto').each(function () {
  538. media += 'a=crypto:' + this.getAttribute('tag');
  539. media += ' ' + this.getAttribute('crypto-suite');
  540. media += ' ' + this.getAttribute('key-params');
  541. if (this.getAttribute('session-params')) {
  542. media += ' ' + this.getAttribute('session-params');
  543. }
  544. media += '\r\n';
  545. });
  546. }
  547. desc.find('payload-type').each(function () {
  548. media += SDPUtil.build_rtpmap(this) + '\r\n';
  549. if ($(this).find('>parameter').length) {
  550. media += 'a=fmtp:' + this.getAttribute('id') + ' ';
  551. media += $(this).find('parameter').map(function () {
  552. return (this.getAttribute('name')
  553. ? (this.getAttribute('name') + '=') : '') +
  554. this.getAttribute('value');
  555. }).get().join('; ');
  556. media += '\r\n';
  557. }
  558. // xep-0293
  559. media += self.rtcpFbFromJingle($(this), this.getAttribute('id'));
  560. });
  561. // xep-0293
  562. media += self.rtcpFbFromJingle(desc, '*');
  563. // xep-0294
  564. tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  565. tmp.each(function () {
  566. media += 'a=extmap:' + this.getAttribute('id') + ' ' + this.getAttribute('uri') + '\r\n';
  567. });
  568. content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
  569. var protocol = this.getAttribute('protocol');
  570. protocol = (typeof protocol === 'string') ? protocol.toLowerCase(): '';
  571. if ((self.removeTcpCandidates
  572. && (protocol === 'tcp' || protocol === 'ssltcp')) ||
  573. (self.removeUdpCandidates && protocol === 'udp')) {
  574. return;
  575. } else if (self.failICE) {
  576. this.setAttribute('ip', '1.1.1.1');
  577. }
  578. media += SDPUtil.candidateFromJingle(this);
  579. });
  580. // XEP-0339 handle ssrc-group attributes
  581. content.find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  582. var semantics = this.getAttribute('semantics');
  583. var ssrcs = $(this).find('>source').map(function() {
  584. return this.getAttribute('ssrc');
  585. }).get();
  586. if (ssrcs.length) {
  587. media += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  588. }
  589. });
  590. tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  591. tmp.each(function () {
  592. var ssrc = this.getAttribute('ssrc');
  593. $(this).find('>parameter').each(function () {
  594. var name = this.getAttribute('name');
  595. var value = this.getAttribute('value');
  596. value = SDPUtil.filter_special_chars(value);
  597. media += 'a=ssrc:' + ssrc + ' ' + name;
  598. if (value && value.length)
  599. media += ':' + value;
  600. media += '\r\n';
  601. });
  602. });
  603. return media;
  604. };
  605. module.exports = SDP;