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

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