You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strophe.jingle.sdp.js 23KB

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