Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

strophe.jingle.sdp.js 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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.semantics
  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, ssrcs) {
  185. // console.log("SSRC" + ssrcs["audio"] + " - " + ssrcs["video"]);
  186. var i, j, k, mline, ssrc, rtpmap, tmp, line, lines;
  187. var self = this;
  188. // new bundle plan
  189. if (SDPUtil.find_line(this.session, 'a=group:')) {
  190. lines = SDPUtil.find_lines(this.session, 'a=group:');
  191. for (i = 0; i < lines.length; i++) {
  192. tmp = lines[i].split(' ');
  193. var semantics = tmp.shift().substr(8);
  194. elem.c('group', {xmlns: 'urn:xmpp:jingle:apps:grouping:0', semantics:semantics});
  195. for (j = 0; j < tmp.length; j++) {
  196. elem.c('content', {name: tmp[j]}).up();
  197. }
  198. elem.up();
  199. }
  200. }
  201. for (i = 0; i < this.media.length; i++) {
  202. mline = SDPUtil.parse_mline(this.media[i].split('\r\n')[0]);
  203. if (!(mline.media === 'audio' ||
  204. mline.media === 'video' ||
  205. mline.media === 'application'))
  206. {
  207. continue;
  208. }
  209. if (SDPUtil.find_line(this.media[i], 'a=ssrc:')) {
  210. ssrc = SDPUtil.find_line(this.media[i], 'a=ssrc:').substring(7).split(' ')[0]; // take the first
  211. } else {
  212. if(ssrcs && ssrcs[mline.media])
  213. {
  214. ssrc = ssrcs[mline.media];
  215. }
  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. }
  225. if (SDPUtil.find_line(this.media[i], 'a=rtpmap:').length)
  226. {
  227. elem.c('description',
  228. {xmlns: 'urn:xmpp:jingle:apps:rtp:1',
  229. media: mline.media });
  230. if (ssrc) {
  231. elem.attrs({ssrc: ssrc});
  232. }
  233. for (j = 0; j < mline.fmt.length; j++) {
  234. rtpmap = SDPUtil.find_line(this.media[i], 'a=rtpmap:' + mline.fmt[j]);
  235. elem.c('payload-type', SDPUtil.parse_rtpmap(rtpmap));
  236. // put any 'a=fmtp:' + mline.fmt[j] lines into <param name=foo value=bar/>
  237. if (SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j])) {
  238. tmp = SDPUtil.parse_fmtp(SDPUtil.find_line(this.media[i], 'a=fmtp:' + mline.fmt[j]));
  239. for (k = 0; k < tmp.length; k++) {
  240. elem.c('parameter', tmp[k]).up();
  241. }
  242. }
  243. this.RtcpFbToJingle(i, elem, mline.fmt[j]); // XEP-0293 -- map a=rtcp-fb
  244. elem.up();
  245. }
  246. if (SDPUtil.find_line(this.media[i], 'a=crypto:', this.session)) {
  247. elem.c('encryption', {required: 1});
  248. var crypto = SDPUtil.find_lines(this.media[i], 'a=crypto:', this.session);
  249. crypto.forEach(function(line) {
  250. elem.c('crypto', SDPUtil.parse_crypto(line)).up();
  251. });
  252. elem.up(); // end of encryption
  253. }
  254. if (ssrc) {
  255. // new style mapping
  256. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  257. // FIXME: group by ssrc and support multiple different ssrcs
  258. var ssrclines = SDPUtil.find_lines(this.media[i], 'a=ssrc:');
  259. if(ssrclines.length > 0) {
  260. ssrclines.forEach(function (line) {
  261. idx = line.indexOf(' ');
  262. var linessrc = line.substr(0, idx).substr(7);
  263. if (linessrc != ssrc) {
  264. elem.up();
  265. ssrc = linessrc;
  266. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  267. }
  268. var kv = line.substr(idx + 1);
  269. elem.c('parameter');
  270. if (kv.indexOf(':') == -1) {
  271. elem.attrs({ name: kv });
  272. } else {
  273. elem.attrs({ name: kv.split(':', 2)[0] });
  274. elem.attrs({ value: kv.split(':', 2)[1] });
  275. }
  276. elem.up();
  277. });
  278. elem.up();
  279. }
  280. else
  281. {
  282. elem.up();
  283. elem.c('source', { ssrc: ssrc, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  284. elem.c('parameter');
  285. elem.attrs({name: "cname", value:Math.random().toString(36).substring(7)});
  286. elem.up();
  287. var msid = null;
  288. if(mline.media == "audio")
  289. {
  290. msid = connection.jingle.localAudio.getAudioTracks()[0].id;
  291. }
  292. else
  293. {
  294. msid = connection.jingle.localVideo.getVideoTracks()[0].id;
  295. }
  296. if(msid != null)
  297. {
  298. msid = msid.replace(/[\{,\}]/g,"");
  299. elem.c('parameter');
  300. elem.attrs({name: "msid", value:msid});
  301. elem.up();
  302. elem.c('parameter');
  303. elem.attrs({name: "mslabel", value:msid});
  304. elem.up();
  305. elem.c('parameter');
  306. elem.attrs({name: "label", value:msid});
  307. elem.up();
  308. elem.up();
  309. }
  310. }
  311. // XEP-0339 handle ssrc-group attributes
  312. var ssrc_group_lines = SDPUtil.find_lines(this.media[i], 'a=ssrc-group:');
  313. ssrc_group_lines.forEach(function(line) {
  314. idx = line.indexOf(' ');
  315. var semantics = line.substr(0, idx).substr(13);
  316. var ssrcs = line.substr(14 + semantics.length).split(' ');
  317. if (ssrcs.length != 0) {
  318. elem.c('ssrc-group', { semantics: semantics, xmlns: 'urn:xmpp:jingle:apps:rtp:ssma:0' });
  319. ssrcs.forEach(function(ssrc) {
  320. elem.c('source', { ssrc: ssrc })
  321. .up();
  322. });
  323. elem.up();
  324. }
  325. });
  326. }
  327. if (SDPUtil.find_line(this.media[i], 'a=rtcp-mux')) {
  328. elem.c('rtcp-mux').up();
  329. }
  330. // XEP-0293 -- map a=rtcp-fb:*
  331. this.RtcpFbToJingle(i, elem, '*');
  332. // XEP-0294
  333. if (SDPUtil.find_line(this.media[i], 'a=extmap:')) {
  334. lines = SDPUtil.find_lines(this.media[i], 'a=extmap:');
  335. for (j = 0; j < lines.length; j++) {
  336. tmp = SDPUtil.parse_extmap(lines[j]);
  337. elem.c('rtp-hdrext', { xmlns: 'urn:xmpp:jingle:apps:rtp:rtp-hdrext:0',
  338. uri: tmp.uri,
  339. id: tmp.value });
  340. if (tmp.hasOwnProperty('direction')) {
  341. switch (tmp.direction) {
  342. case 'sendonly':
  343. elem.attrs({senders: 'responder'});
  344. break;
  345. case 'recvonly':
  346. elem.attrs({senders: 'initiator'});
  347. break;
  348. case 'sendrecv':
  349. elem.attrs({senders: 'both'});
  350. break;
  351. case 'inactive':
  352. elem.attrs({senders: 'none'});
  353. break;
  354. }
  355. }
  356. // TODO: handle params
  357. elem.up();
  358. }
  359. }
  360. elem.up(); // end of description
  361. }
  362. // map ice-ufrag/pwd, dtls fingerprint, candidates
  363. this.TransportToJingle(i, elem);
  364. if (SDPUtil.find_line(this.media[i], 'a=sendrecv', this.session)) {
  365. elem.attrs({senders: 'both'});
  366. } else if (SDPUtil.find_line(this.media[i], 'a=sendonly', this.session)) {
  367. elem.attrs({senders: 'initiator'});
  368. } else if (SDPUtil.find_line(this.media[i], 'a=recvonly', this.session)) {
  369. elem.attrs({senders: 'responder'});
  370. } else if (SDPUtil.find_line(this.media[i], 'a=inactive', this.session)) {
  371. elem.attrs({senders: 'none'});
  372. }
  373. if (mline.port == '0') {
  374. // estos hack to reject an m-line
  375. elem.attrs({senders: 'rejected'});
  376. }
  377. elem.up(); // end of content
  378. }
  379. elem.up();
  380. return elem;
  381. };
  382. SDP.prototype.TransportToJingle = function (mediaindex, elem) {
  383. var i = mediaindex;
  384. var tmp;
  385. var self = this;
  386. elem.c('transport');
  387. // XEP-0343 DTLS/SCTP
  388. if (SDPUtil.find_line(this.media[mediaindex], 'a=sctpmap:').length)
  389. {
  390. var sctpmap = SDPUtil.find_line(
  391. this.media[i], 'a=sctpmap:', self.session);
  392. if (sctpmap)
  393. {
  394. var sctpAttrs = SDPUtil.parse_sctpmap(sctpmap);
  395. elem.c('sctpmap',
  396. {
  397. xmlns: 'urn:xmpp:jingle:transports:dtls-sctp:1',
  398. number: sctpAttrs[0], /* SCTP port */
  399. protocol: sctpAttrs[1], /* protocol */
  400. });
  401. // Optional stream count attribute
  402. if (sctpAttrs.length > 2)
  403. elem.attrs({ streams: sctpAttrs[2]});
  404. elem.up();
  405. }
  406. }
  407. // XEP-0320
  408. var fingerprints = SDPUtil.find_lines(this.media[mediaindex], 'a=fingerprint:', this.session);
  409. fingerprints.forEach(function(line) {
  410. tmp = SDPUtil.parse_fingerprint(line);
  411. tmp.xmlns = 'urn:xmpp:jingle:apps:dtls:0';
  412. elem.c('fingerprint').t(tmp.fingerprint);
  413. delete tmp.fingerprint;
  414. line = SDPUtil.find_line(self.media[mediaindex], 'a=setup:', self.session);
  415. if (line) {
  416. tmp.setup = line.substr(8);
  417. }
  418. elem.attrs(tmp);
  419. elem.up(); // end of fingerprint
  420. });
  421. tmp = SDPUtil.iceparams(this.media[mediaindex], this.session);
  422. if (tmp) {
  423. tmp.xmlns = 'urn:xmpp:jingle:transports:ice-udp:1';
  424. elem.attrs(tmp);
  425. // XEP-0176
  426. if (SDPUtil.find_line(this.media[mediaindex], 'a=candidate:', this.session)) { // add any a=candidate lines
  427. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=candidate:', this.session);
  428. lines.forEach(function (line) {
  429. elem.c('candidate', SDPUtil.candidateToJingle(line)).up();
  430. });
  431. }
  432. }
  433. elem.up(); // end of transport
  434. }
  435. SDP.prototype.RtcpFbToJingle = function (mediaindex, elem, payloadtype) { // XEP-0293
  436. var lines = SDPUtil.find_lines(this.media[mediaindex], 'a=rtcp-fb:' + payloadtype);
  437. lines.forEach(function (line) {
  438. var tmp = SDPUtil.parse_rtcpfb(line);
  439. if (tmp.type == 'trr-int') {
  440. elem.c('rtcp-fb-trr-int', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', value: tmp.params[0]});
  441. elem.up();
  442. } else {
  443. elem.c('rtcp-fb', {xmlns: 'urn:xmpp:jingle:apps:rtp:rtcp-fb:0', type: tmp.type});
  444. if (tmp.params.length > 0) {
  445. elem.attrs({'subtype': tmp.params[0]});
  446. }
  447. elem.up();
  448. }
  449. });
  450. };
  451. SDP.prototype.RtcpFbFromJingle = function (elem, payloadtype) { // XEP-0293
  452. var media = '';
  453. var tmp = elem.find('>rtcp-fb-trr-int[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  454. if (tmp.length) {
  455. media += 'a=rtcp-fb:' + '*' + ' ' + 'trr-int' + ' ';
  456. if (tmp.attr('value')) {
  457. media += tmp.attr('value');
  458. } else {
  459. media += '0';
  460. }
  461. media += '\r\n';
  462. }
  463. tmp = elem.find('>rtcp-fb[xmlns="urn:xmpp:jingle:apps:rtp:rtcp-fb:0"]');
  464. tmp.each(function () {
  465. media += 'a=rtcp-fb:' + payloadtype + ' ' + $(this).attr('type');
  466. if ($(this).attr('subtype')) {
  467. media += ' ' + $(this).attr('subtype');
  468. }
  469. media += '\r\n';
  470. });
  471. return media;
  472. };
  473. // construct an SDP from a jingle stanza
  474. SDP.prototype.fromJingle = function (jingle) {
  475. var self = this;
  476. this.raw = 'v=0\r\n' +
  477. 'o=- ' + '1923518516' + ' 2 IN IP4 0.0.0.0\r\n' +// FIXME
  478. 's=-\r\n' +
  479. 't=0 0\r\n';
  480. // http://tools.ietf.org/html/draft-ietf-mmusic-sdp-bundle-negotiation-04#section-8
  481. if ($(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').length) {
  482. $(jingle).find('>group[xmlns="urn:xmpp:jingle:apps:grouping:0"]').each(function (idx, group) {
  483. var contents = $(group).find('>content').map(function (idx, content) {
  484. return content.getAttribute('name');
  485. }).get();
  486. if (contents.length > 0) {
  487. self.raw += 'a=group:' + (group.getAttribute('semantics') || group.getAttribute('type')) + ' ' + contents.join(' ') + '\r\n';
  488. }
  489. });
  490. }
  491. this.session = this.raw;
  492. jingle.find('>content').each(function () {
  493. var m = self.jingle2media($(this));
  494. self.media.push(m);
  495. });
  496. // reconstruct msid-semantic -- apparently not necessary
  497. /*
  498. var msid = SDPUtil.parse_ssrc(this.raw);
  499. if (msid.hasOwnProperty('mslabel')) {
  500. this.session += "a=msid-semantic: WMS " + msid.mslabel + "\r\n";
  501. }
  502. */
  503. this.raw = this.session + this.media.join('');
  504. };
  505. // translate a jingle content element into an an SDP media part
  506. SDP.prototype.jingle2media = function (content) {
  507. var media = '',
  508. desc = content.find('description'),
  509. ssrc = desc.attr('ssrc'),
  510. self = this,
  511. tmp;
  512. var sctp = content.find(
  513. '>transport>sctpmap[xmlns="urn:xmpp:jingle:transports:dtls-sctp:1"]');
  514. tmp = { media: desc.attr('media') };
  515. tmp.port = '1';
  516. if (content.attr('senders') == 'rejected') {
  517. // estos hack to reject an m-line.
  518. tmp.port = '0';
  519. }
  520. if (content.find('>transport>fingerprint').length || desc.find('encryption').length) {
  521. if (sctp.length)
  522. tmp.proto = 'DTLS/SCTP';
  523. else
  524. tmp.proto = 'RTP/SAVPF';
  525. } else {
  526. tmp.proto = 'RTP/AVPF';
  527. }
  528. if (!sctp.length)
  529. {
  530. tmp.fmt = desc.find('payload-type').map(
  531. function () { return this.getAttribute('id'); }).get();
  532. media += SDPUtil.build_mline(tmp) + '\r\n';
  533. }
  534. else
  535. {
  536. media += 'm=application 1 DTLS/SCTP ' + sctp.attr('number') + '\r\n';
  537. media += 'a=sctpmap:' + sctp.attr('number') +
  538. ' ' + sctp.attr('protocol');
  539. var streamCount = sctp.attr('streams');
  540. if (streamCount)
  541. media += ' ' + streamCount + '\r\n';
  542. else
  543. media += '\r\n';
  544. }
  545. media += 'c=IN IP4 0.0.0.0\r\n';
  546. if (!sctp.length)
  547. media += 'a=rtcp:1 IN IP4 0.0.0.0\r\n';
  548. tmp = content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]');
  549. if (tmp.length) {
  550. if (tmp.attr('ufrag')) {
  551. media += SDPUtil.build_iceufrag(tmp.attr('ufrag')) + '\r\n';
  552. }
  553. if (tmp.attr('pwd')) {
  554. media += SDPUtil.build_icepwd(tmp.attr('pwd')) + '\r\n';
  555. }
  556. tmp.find('>fingerprint').each(function () {
  557. // FIXME: check namespace at some point
  558. media += 'a=fingerprint:' + this.getAttribute('hash');
  559. media += ' ' + $(this).text();
  560. media += '\r\n';
  561. if (this.getAttribute('setup')) {
  562. media += 'a=setup:' + this.getAttribute('setup') + '\r\n';
  563. }
  564. });
  565. }
  566. switch (content.attr('senders')) {
  567. case 'initiator':
  568. media += 'a=sendonly\r\n';
  569. break;
  570. case 'responder':
  571. media += 'a=recvonly\r\n';
  572. break;
  573. case 'none':
  574. media += 'a=inactive\r\n';
  575. break;
  576. case 'both':
  577. media += 'a=sendrecv\r\n';
  578. break;
  579. }
  580. media += 'a=mid:' + content.attr('name') + '\r\n';
  581. // <description><rtcp-mux/></description>
  582. // see http://code.google.com/p/libjingle/issues/detail?id=309 -- no spec though
  583. // and http://mail.jabber.org/pipermail/jingle/2011-December/001761.html
  584. if (desc.find('rtcp-mux').length) {
  585. media += 'a=rtcp-mux\r\n';
  586. }
  587. if (desc.find('encryption').length) {
  588. desc.find('encryption>crypto').each(function () {
  589. media += 'a=crypto:' + this.getAttribute('tag');
  590. media += ' ' + this.getAttribute('crypto-suite');
  591. media += ' ' + this.getAttribute('key-params');
  592. if (this.getAttribute('session-params')) {
  593. media += ' ' + this.getAttribute('session-params');
  594. }
  595. media += '\r\n';
  596. });
  597. }
  598. desc.find('payload-type').each(function () {
  599. media += SDPUtil.build_rtpmap(this) + '\r\n';
  600. if ($(this).find('>parameter').length) {
  601. media += 'a=fmtp:' + this.getAttribute('id') + ' ';
  602. media += $(this).find('parameter').map(function () { return (this.getAttribute('name') ? (this.getAttribute('name') + '=') : '') + this.getAttribute('value'); }).get().join('; ');
  603. media += '\r\n';
  604. }
  605. // xep-0293
  606. media += self.RtcpFbFromJingle($(this), this.getAttribute('id'));
  607. });
  608. // xep-0293
  609. media += self.RtcpFbFromJingle(desc, '*');
  610. // xep-0294
  611. tmp = desc.find('>rtp-hdrext[xmlns="urn:xmpp:jingle:apps:rtp:rtp-hdrext:0"]');
  612. tmp.each(function () {
  613. media += 'a=extmap:' + this.getAttribute('id') + ' ' + this.getAttribute('uri') + '\r\n';
  614. });
  615. content.find('>transport[xmlns="urn:xmpp:jingle:transports:ice-udp:1"]>candidate').each(function () {
  616. media += SDPUtil.candidateFromJingle(this);
  617. });
  618. // XEP-0339 handle ssrc-group attributes
  619. tmp = content.find('description>ssrc-group[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]').each(function() {
  620. var semantics = this.getAttribute('semantics');
  621. var ssrcs = $(this).find('>source').map(function() {
  622. return this.getAttribute('ssrc');
  623. }).get();
  624. if (ssrcs.length != 0) {
  625. media += 'a=ssrc-group:' + semantics + ' ' + ssrcs.join(' ') + '\r\n';
  626. }
  627. });
  628. tmp = content.find('description>source[xmlns="urn:xmpp:jingle:apps:rtp:ssma:0"]');
  629. tmp.each(function () {
  630. var ssrc = this.getAttribute('ssrc');
  631. $(this).find('>parameter').each(function () {
  632. media += 'a=ssrc:' + ssrc + ' ' + this.getAttribute('name');
  633. if (this.getAttribute('value') && this.getAttribute('value').length)
  634. media += ':' + this.getAttribute('value');
  635. media += '\r\n';
  636. });
  637. });
  638. return media;
  639. };