Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

strophe.jingle.sdp.js 27KB

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