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.

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