modified lib-jitsi-meet dev repo
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. var logger = require("jitsi-meet-logger").getLogger(__filename);
  2. var RTCBrowserType = require("../RTC/RTCBrowserType");
  3. SDPUtil = {
  4. filter_special_chars: function (text) {
  5. return text.replace(/[\\\/\{,\}\+]/g, "");
  6. },
  7. iceparams: function (mediadesc, sessiondesc) {
  8. var data = null;
  9. var ufrag, pwd;
  10. if ((ufrag = SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc))
  11. && (pwd = SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))) {
  12. data = {
  13. ufrag: SDPUtil.parse_iceufrag(ufrag),
  14. pwd: SDPUtil.parse_icepwd(pwd)
  15. };
  16. }
  17. return data;
  18. },
  19. parse_iceufrag: function (line) {
  20. return line.substring(12);
  21. },
  22. build_iceufrag: function (frag) {
  23. return 'a=ice-ufrag:' + frag;
  24. },
  25. parse_icepwd: function (line) {
  26. return line.substring(10);
  27. },
  28. build_icepwd: function (pwd) {
  29. return 'a=ice-pwd:' + pwd;
  30. },
  31. parse_mid: function (line) {
  32. return line.substring(6);
  33. },
  34. parse_mline: function (line) {
  35. var parts = line.substring(2).split(' '),
  36. data = {};
  37. data.media = parts.shift();
  38. data.port = parts.shift();
  39. data.proto = parts.shift();
  40. if (parts[parts.length - 1] === '') { // trailing whitespace
  41. parts.pop();
  42. }
  43. data.fmt = parts;
  44. return data;
  45. },
  46. build_mline: function (mline) {
  47. return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
  48. },
  49. parse_rtpmap: function (line) {
  50. var parts = line.substring(9).split(' '),
  51. data = {};
  52. data.id = parts.shift();
  53. parts = parts[0].split('/');
  54. data.name = parts.shift();
  55. data.clockrate = parts.shift();
  56. data.channels = parts.length ? parts.shift() : '1';
  57. return data;
  58. },
  59. /**
  60. * Parses SDP line "a=sctpmap:..." and extracts SCTP port from it.
  61. * @param line eg. "a=sctpmap:5000 webrtc-datachannel"
  62. * @returns [SCTP port number, protocol, streams]
  63. */
  64. parse_sctpmap: function (line)
  65. {
  66. var parts = line.substring(10).split(' ');
  67. var sctpPort = parts[0];
  68. var protocol = parts[1];
  69. // Stream count is optional
  70. var streamCount = parts.length > 2 ? parts[2] : null;
  71. return [sctpPort, protocol, streamCount];// SCTP port
  72. },
  73. build_rtpmap: function (el) {
  74. var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
  75. if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
  76. line += '/' + el.getAttribute('channels');
  77. }
  78. return line;
  79. },
  80. parse_crypto: function (line) {
  81. var parts = line.substring(9).split(' '),
  82. data = {};
  83. data.tag = parts.shift();
  84. data['crypto-suite'] = parts.shift();
  85. data['key-params'] = parts.shift();
  86. if (parts.length) {
  87. data['session-params'] = parts.join(' ');
  88. }
  89. return data;
  90. },
  91. parse_fingerprint: function (line) { // RFC 4572
  92. var parts = line.substring(14).split(' '),
  93. data = {};
  94. data.hash = parts.shift();
  95. data.fingerprint = parts.shift();
  96. // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
  97. return data;
  98. },
  99. parse_fmtp: function (line) {
  100. var parts = line.split(' '),
  101. i, key, value,
  102. data = [];
  103. parts.shift();
  104. parts = parts.join(' ').split(';');
  105. for (i = 0; i < parts.length; i++) {
  106. key = parts[i].split('=')[0];
  107. while (key.length && key[0] == ' ') {
  108. key = key.substring(1);
  109. }
  110. value = parts[i].split('=')[1];
  111. if (key && value) {
  112. data.push({name: key, value: value});
  113. } else if (key) {
  114. // rfc 4733 (DTMF) style stuff
  115. data.push({name: '', value: key});
  116. }
  117. }
  118. return data;
  119. },
  120. parse_icecandidate: function (line) {
  121. var candidate = {},
  122. elems = line.split(' ');
  123. candidate.foundation = elems[0].substring(12);
  124. candidate.component = elems[1];
  125. candidate.protocol = elems[2].toLowerCase();
  126. candidate.priority = elems[3];
  127. candidate.ip = elems[4];
  128. candidate.port = elems[5];
  129. // elems[6] => "typ"
  130. candidate.type = elems[7];
  131. candidate.generation = 0; // default value, may be overwritten below
  132. for (var i = 8; i < elems.length; i += 2) {
  133. switch (elems[i]) {
  134. case 'raddr':
  135. candidate['rel-addr'] = elems[i + 1];
  136. break;
  137. case 'rport':
  138. candidate['rel-port'] = elems[i + 1];
  139. break;
  140. case 'generation':
  141. candidate.generation = elems[i + 1];
  142. break;
  143. case 'tcptype':
  144. candidate.tcptype = elems[i + 1];
  145. break;
  146. default: // TODO
  147. logger.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  148. }
  149. }
  150. candidate.network = '1';
  151. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  152. return candidate;
  153. },
  154. build_icecandidate: function (cand) {
  155. var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
  156. line += ' ';
  157. switch (cand.type) {
  158. case 'srflx':
  159. case 'prflx':
  160. case 'relay':
  161. if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
  162. line += 'raddr';
  163. line += ' ';
  164. line += cand['rel-addr'];
  165. line += ' ';
  166. line += 'rport';
  167. line += ' ';
  168. line += cand['rel-port'];
  169. line += ' ';
  170. }
  171. break;
  172. }
  173. if (cand.hasOwnAttribute('tcptype')) {
  174. line += 'tcptype';
  175. line += ' ';
  176. line += cand.tcptype;
  177. line += ' ';
  178. }
  179. line += 'generation';
  180. line += ' ';
  181. line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
  182. return line;
  183. },
  184. parse_ssrc: function (desc) {
  185. // proprietary mapping of a=ssrc lines
  186. // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
  187. // and parse according to that
  188. var lines = desc.split('\r\n'),
  189. data = {};
  190. for (var i = 0; i < lines.length; i++) {
  191. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  192. var idx = lines[i].indexOf(' ');
  193. data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
  194. }
  195. }
  196. return data;
  197. },
  198. parse_rtcpfb: function (line) {
  199. var parts = line.substr(10).split(' ');
  200. var data = {};
  201. data.pt = parts.shift();
  202. data.type = parts.shift();
  203. data.params = parts;
  204. return data;
  205. },
  206. parse_extmap: function (line) {
  207. var parts = line.substr(9).split(' ');
  208. var data = {};
  209. data.value = parts.shift();
  210. if (data.value.indexOf('/') != -1) {
  211. data.direction = data.value.substr(data.value.indexOf('/') + 1);
  212. data.value = data.value.substr(0, data.value.indexOf('/'));
  213. } else {
  214. data.direction = 'both';
  215. }
  216. data.uri = parts.shift();
  217. data.params = parts;
  218. return data;
  219. },
  220. find_line: function (haystack, needle, sessionpart) {
  221. var lines = haystack.split('\r\n');
  222. for (var i = 0; i < lines.length; i++) {
  223. if (lines[i].substring(0, needle.length) == needle) {
  224. return lines[i];
  225. }
  226. }
  227. if (!sessionpart) {
  228. return false;
  229. }
  230. // search session part
  231. lines = sessionpart.split('\r\n');
  232. for (var j = 0; j < lines.length; j++) {
  233. if (lines[j].substring(0, needle.length) == needle) {
  234. return lines[j];
  235. }
  236. }
  237. return false;
  238. },
  239. find_lines: function (haystack, needle, sessionpart) {
  240. var lines = haystack.split('\r\n'),
  241. needles = [];
  242. for (var i = 0; i < lines.length; i++) {
  243. if (lines[i].substring(0, needle.length) == needle)
  244. needles.push(lines[i]);
  245. }
  246. if (needles.length || !sessionpart) {
  247. return needles;
  248. }
  249. // search session part
  250. lines = sessionpart.split('\r\n');
  251. for (var j = 0; j < lines.length; j++) {
  252. if (lines[j].substring(0, needle.length) == needle) {
  253. needles.push(lines[j]);
  254. }
  255. }
  256. return needles;
  257. },
  258. candidateToJingle: function (line) {
  259. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  260. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  261. if (line.indexOf('candidate:') === 0) {
  262. line = 'a=' + line;
  263. } else if (line.substring(0, 12) != 'a=candidate:') {
  264. logger.log('parseCandidate called with a line that is not a candidate line');
  265. logger.log(line);
  266. return null;
  267. }
  268. if (line.substring(line.length - 2) == '\r\n') // chomp it
  269. line = line.substring(0, line.length - 2);
  270. var candidate = {},
  271. elems = line.split(' '),
  272. i;
  273. if (elems[6] != 'typ') {
  274. logger.log('did not find typ in the right place');
  275. logger.log(line);
  276. return null;
  277. }
  278. candidate.foundation = elems[0].substring(12);
  279. candidate.component = elems[1];
  280. candidate.protocol = elems[2].toLowerCase();
  281. candidate.priority = elems[3];
  282. candidate.ip = elems[4];
  283. candidate.port = elems[5];
  284. // elems[6] => "typ"
  285. candidate.type = elems[7];
  286. candidate.generation = '0'; // default, may be overwritten below
  287. for (i = 8; i < elems.length; i += 2) {
  288. switch (elems[i]) {
  289. case 'raddr':
  290. candidate['rel-addr'] = elems[i + 1];
  291. break;
  292. case 'rport':
  293. candidate['rel-port'] = elems[i + 1];
  294. break;
  295. case 'generation':
  296. candidate.generation = elems[i + 1];
  297. break;
  298. case 'tcptype':
  299. candidate.tcptype = elems[i + 1];
  300. break;
  301. default: // TODO
  302. logger.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  303. }
  304. }
  305. candidate.network = '1';
  306. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  307. return candidate;
  308. },
  309. candidateFromJingle: function (cand) {
  310. var line = 'a=candidate:';
  311. line += cand.getAttribute('foundation');
  312. line += ' ';
  313. line += cand.getAttribute('component');
  314. line += ' ';
  315. var protocol = cand.getAttribute('protocol');
  316. // use tcp candidates for FF
  317. if (RTCBrowserType.isFirefox() && protocol.toLowerCase() == 'ssltcp') {
  318. protocol = 'tcp';
  319. }
  320. line += protocol; //.toUpperCase(); // chrome M23 doesn't like this
  321. line += ' ';
  322. line += cand.getAttribute('priority');
  323. line += ' ';
  324. line += cand.getAttribute('ip');
  325. line += ' ';
  326. line += cand.getAttribute('port');
  327. line += ' ';
  328. line += 'typ';
  329. line += ' ' + cand.getAttribute('type');
  330. line += ' ';
  331. switch (cand.getAttribute('type')) {
  332. case 'srflx':
  333. case 'prflx':
  334. case 'relay':
  335. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  336. line += 'raddr';
  337. line += ' ';
  338. line += cand.getAttribute('rel-addr');
  339. line += ' ';
  340. line += 'rport';
  341. line += ' ';
  342. line += cand.getAttribute('rel-port');
  343. line += ' ';
  344. }
  345. break;
  346. }
  347. if (protocol.toLowerCase() == 'tcp') {
  348. line += 'tcptype';
  349. line += ' ';
  350. line += cand.getAttribute('tcptype');
  351. line += ' ';
  352. }
  353. line += 'generation';
  354. line += ' ';
  355. line += cand.getAttribute('generation') || '0';
  356. return line + '\r\n';
  357. }
  358. };
  359. module.exports = SDPUtil;