strophe.jingle.sdp.js 27 KB

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