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.

SDPUtil.js 13KB

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