選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

strophe.jingle.sdp.js 24KB

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