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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import {getLogger} from "jitsi-meet-logger";
  2. const logger = getLogger(__filename);
  3. import RandomUtil from "../util/RandomUtil";
  4. var RTCBrowserType = require("../RTC/RTCBrowserType");
  5. var SDPUtil = {
  6. filter_special_chars(text) {
  7. // XXX Neither one of the falsy values (e.g. null, undefined, false,
  8. // "", etc.) "contain" special chars.
  9. return text ? text.replace(/[\\\/\{,\}\+]/g, "") : text;
  10. },
  11. iceparams(mediadesc, sessiondesc) {
  12. var data = null;
  13. var pwd, ufrag;
  14. if ((ufrag = SDPUtil.find_line(mediadesc, 'a=ice-ufrag:', sessiondesc))
  15. && (pwd = SDPUtil.find_line(mediadesc, 'a=ice-pwd:', sessiondesc))) {
  16. data = {
  17. ufrag: SDPUtil.parse_iceufrag(ufrag),
  18. pwd: SDPUtil.parse_icepwd(pwd)
  19. };
  20. }
  21. return data;
  22. },
  23. parse_iceufrag(line) {
  24. return line.substring(12);
  25. },
  26. build_iceufrag(frag) {
  27. return 'a=ice-ufrag:' + frag;
  28. },
  29. parse_icepwd(line) {
  30. return line.substring(10);
  31. },
  32. build_icepwd(pwd) {
  33. return 'a=ice-pwd:' + pwd;
  34. },
  35. parse_mid(line) {
  36. return line.substring(6);
  37. },
  38. parse_mline(line) {
  39. var data = {},
  40. parts = line.substring(2).split(' ');
  41. data.media = parts.shift();
  42. data.port = parts.shift();
  43. data.proto = parts.shift();
  44. if (parts[parts.length - 1] === '') { // trailing whitespace
  45. parts.pop();
  46. }
  47. data.fmt = parts;
  48. return data;
  49. },
  50. build_mline(mline) {
  51. return 'm=' + mline.media + ' ' + mline.port + ' ' + mline.proto + ' ' + mline.fmt.join(' ');
  52. },
  53. parse_rtpmap(line) {
  54. var data = {},
  55. parts = line.substring(9).split(' ');
  56. data.id = parts.shift();
  57. parts = parts[0].split('/');
  58. data.name = parts.shift();
  59. data.clockrate = parts.shift();
  60. data.channels = parts.length ? parts.shift() : '1';
  61. return data;
  62. },
  63. /**
  64. * Parses SDP line "a=sctpmap:..." and extracts SCTP port from it.
  65. * @param line eg. "a=sctpmap:5000 webrtc-datachannel"
  66. * @returns [SCTP port number, protocol, streams]
  67. */
  68. parse_sctpmap(line) {
  69. var parts = line.substring(10).split(' ');
  70. var sctpPort = parts[0];
  71. var protocol = parts[1];
  72. // Stream count is optional
  73. var streamCount = parts.length > 2 ? parts[2] : null;
  74. return [sctpPort, protocol, streamCount];// SCTP port
  75. },
  76. build_rtpmap(el) {
  77. var line = 'a=rtpmap:' + el.getAttribute('id') + ' ' + el.getAttribute('name') + '/' + el.getAttribute('clockrate');
  78. if (el.getAttribute('channels') && el.getAttribute('channels') != '1') {
  79. line += '/' + el.getAttribute('channels');
  80. }
  81. return line;
  82. },
  83. parse_crypto(line) {
  84. var data = {},
  85. parts = line.substring(9).split(' ');
  86. data.tag = parts.shift();
  87. data['crypto-suite'] = parts.shift();
  88. data['key-params'] = parts.shift();
  89. if (parts.length) {
  90. data['session-params'] = parts.join(' ');
  91. }
  92. return data;
  93. },
  94. parse_fingerprint(line) { // RFC 4572
  95. var data = {},
  96. parts = line.substring(14).split(' ');
  97. data.hash = parts.shift();
  98. data.fingerprint = parts.shift();
  99. // TODO assert that fingerprint satisfies 2UHEX *(":" 2UHEX) ?
  100. return data;
  101. },
  102. parse_fmtp(line) {
  103. var data = [],
  104. i,
  105. key,
  106. parts = line.split(' '),
  107. value;
  108. parts.shift();
  109. parts = parts.join(' ').split(';');
  110. for (i = 0; i < parts.length; i++) {
  111. key = parts[i].split('=')[0];
  112. while (key.length && key[0] == ' ') {
  113. key = key.substring(1);
  114. }
  115. value = parts[i].split('=')[1];
  116. if (key && value) {
  117. data.push({name: key, value});
  118. } else if (key) {
  119. // rfc 4733 (DTMF) style stuff
  120. data.push({name: '', value: key});
  121. }
  122. }
  123. return data;
  124. },
  125. parse_icecandidate(line) {
  126. var candidate = {},
  127. elems = line.split(' ');
  128. candidate.foundation = elems[0].substring(12);
  129. candidate.component = elems[1];
  130. candidate.protocol = elems[2].toLowerCase();
  131. candidate.priority = elems[3];
  132. candidate.ip = elems[4];
  133. candidate.port = elems[5];
  134. // elems[6] => "typ"
  135. candidate.type = elems[7];
  136. candidate.generation = 0; // default value, may be overwritten below
  137. for (var i = 8; i < elems.length; i += 2) {
  138. switch (elems[i]) {
  139. case 'raddr':
  140. candidate['rel-addr'] = elems[i + 1];
  141. break;
  142. case 'rport':
  143. candidate['rel-port'] = elems[i + 1];
  144. break;
  145. case 'generation':
  146. candidate.generation = elems[i + 1];
  147. break;
  148. case 'tcptype':
  149. candidate.tcptype = elems[i + 1];
  150. break;
  151. default: // TODO
  152. logger.log('parse_icecandidate not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  153. }
  154. }
  155. candidate.network = '1';
  156. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  157. return candidate;
  158. },
  159. build_icecandidate(cand) {
  160. var line = ['a=candidate:' + cand.foundation, cand.component, cand.protocol, cand.priority, cand.ip, cand.port, 'typ', cand.type].join(' ');
  161. line += ' ';
  162. switch (cand.type) {
  163. case 'srflx':
  164. case 'prflx':
  165. case 'relay':
  166. if (cand.hasOwnAttribute('rel-addr') && cand.hasOwnAttribute('rel-port')) {
  167. line += 'raddr';
  168. line += ' ';
  169. line += cand['rel-addr'];
  170. line += ' ';
  171. line += 'rport';
  172. line += ' ';
  173. line += cand['rel-port'];
  174. line += ' ';
  175. }
  176. break;
  177. }
  178. if (cand.hasOwnAttribute('tcptype')) {
  179. line += 'tcptype';
  180. line += ' ';
  181. line += cand.tcptype;
  182. line += ' ';
  183. }
  184. line += 'generation';
  185. line += ' ';
  186. line += cand.hasOwnAttribute('generation') ? cand.generation : '0';
  187. return line;
  188. },
  189. parse_ssrc(desc) {
  190. // proprietary mapping of a=ssrc lines
  191. // TODO: see "Jingle RTP Source Description" by Juberti and P. Thatcher on google docs
  192. // and parse according to that
  193. var data = {},
  194. lines = desc.split('\r\n');
  195. for (var i = 0; i < lines.length; i++) {
  196. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  197. var idx = lines[i].indexOf(' ');
  198. data[lines[i].substr(idx + 1).split(':', 2)[0]] = lines[i].substr(idx + 1).split(':', 2)[1];
  199. }
  200. }
  201. return data;
  202. },
  203. parse_rtcpfb(line) {
  204. var parts = line.substr(10).split(' ');
  205. var data = {};
  206. data.pt = parts.shift();
  207. data.type = parts.shift();
  208. data.params = parts;
  209. return data;
  210. },
  211. parse_extmap(line) {
  212. var parts = line.substr(9).split(' ');
  213. var data = {};
  214. data.value = parts.shift();
  215. if (data.value.indexOf('/') != -1) {
  216. data.direction = data.value.substr(data.value.indexOf('/') + 1);
  217. data.value = data.value.substr(0, data.value.indexOf('/'));
  218. } else {
  219. data.direction = 'both';
  220. }
  221. data.uri = parts.shift();
  222. data.params = parts;
  223. return data;
  224. },
  225. find_line(haystack, needle, sessionpart) {
  226. var lines = haystack.split('\r\n');
  227. for (var i = 0; i < lines.length; i++) {
  228. if (lines[i].substring(0, needle.length) == needle) {
  229. return lines[i];
  230. }
  231. }
  232. if (!sessionpart) {
  233. return false;
  234. }
  235. // search session part
  236. lines = sessionpart.split('\r\n');
  237. for (var j = 0; j < lines.length; j++) {
  238. if (lines[j].substring(0, needle.length) == needle) {
  239. return lines[j];
  240. }
  241. }
  242. return false;
  243. },
  244. find_lines(haystack, needle, sessionpart) {
  245. var lines = haystack.split('\r\n'),
  246. needles = [];
  247. for (var i = 0; i < lines.length; i++) {
  248. if (lines[i].substring(0, needle.length) == needle) {
  249. needles.push(lines[i]);
  250. }
  251. }
  252. if (needles.length || !sessionpart) {
  253. return needles;
  254. }
  255. // search session part
  256. lines = sessionpart.split('\r\n');
  257. for (var j = 0; j < lines.length; j++) {
  258. if (lines[j].substring(0, needle.length) == needle) {
  259. needles.push(lines[j]);
  260. }
  261. }
  262. return needles;
  263. },
  264. candidateToJingle(line) {
  265. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  266. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  267. if (line.indexOf('candidate:') === 0) {
  268. line = 'a=' + line;
  269. } else if (line.substring(0, 12) != 'a=candidate:') {
  270. logger.log('parseCandidate called with a line that is not a candidate line');
  271. logger.log(line);
  272. return null;
  273. }
  274. if (line.substring(line.length - 2) == '\r\n') {// chomp it
  275. line = line.substring(0, line.length - 2);
  276. }
  277. var candidate = {},
  278. elems = line.split(' '),
  279. i;
  280. if (elems[6] != 'typ') {
  281. logger.log('did not find typ in the right place');
  282. logger.log(line);
  283. return null;
  284. }
  285. candidate.foundation = elems[0].substring(12);
  286. candidate.component = elems[1];
  287. candidate.protocol = elems[2].toLowerCase();
  288. candidate.priority = elems[3];
  289. candidate.ip = elems[4];
  290. candidate.port = elems[5];
  291. // elems[6] => "typ"
  292. candidate.type = elems[7];
  293. candidate.generation = '0'; // default, may be overwritten below
  294. for (i = 8; i < elems.length; i += 2) {
  295. switch (elems[i]) {
  296. case 'raddr':
  297. candidate['rel-addr'] = elems[i + 1];
  298. break;
  299. case 'rport':
  300. candidate['rel-port'] = elems[i + 1];
  301. break;
  302. case 'generation':
  303. candidate.generation = elems[i + 1];
  304. break;
  305. case 'tcptype':
  306. candidate.tcptype = elems[i + 1];
  307. break;
  308. default: // TODO
  309. logger.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  310. }
  311. }
  312. candidate.network = '1';
  313. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  314. return candidate;
  315. },
  316. candidateFromJingle(cand) {
  317. var line = 'a=candidate:';
  318. line += cand.getAttribute('foundation');
  319. line += ' ';
  320. line += cand.getAttribute('component');
  321. line += ' ';
  322. var protocol = cand.getAttribute('protocol');
  323. // use tcp candidates for FF
  324. if (RTCBrowserType.isFirefox() && protocol.toLowerCase() == 'ssltcp') {
  325. protocol = 'tcp';
  326. }
  327. line += protocol; // .toUpperCase(); // chrome M23 doesn't like this
  328. line += ' ';
  329. line += cand.getAttribute('priority');
  330. line += ' ';
  331. line += cand.getAttribute('ip');
  332. line += ' ';
  333. line += cand.getAttribute('port');
  334. line += ' ';
  335. line += 'typ';
  336. line += ' ' + cand.getAttribute('type');
  337. line += ' ';
  338. switch (cand.getAttribute('type')) {
  339. case 'srflx':
  340. case 'prflx':
  341. case 'relay':
  342. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  343. line += 'raddr';
  344. line += ' ';
  345. line += cand.getAttribute('rel-addr');
  346. line += ' ';
  347. line += 'rport';
  348. line += ' ';
  349. line += cand.getAttribute('rel-port');
  350. line += ' ';
  351. }
  352. break;
  353. }
  354. if (protocol.toLowerCase() == 'tcp') {
  355. line += 'tcptype';
  356. line += ' ';
  357. line += cand.getAttribute('tcptype');
  358. line += ' ';
  359. }
  360. line += 'generation';
  361. line += ' ';
  362. line += cand.getAttribute('generation') || '0';
  363. return line + '\r\n';
  364. },
  365. /**
  366. * Parse the 'most' primary video ssrc from the given m line
  367. * @param {object} mLine object as parsed from transform.parse
  368. * @return {number} the primary video ssrc from the given m line
  369. */
  370. parsePrimaryVideoSsrc(videoMLine) {
  371. const numSsrcs = videoMLine.ssrcs
  372. .map(ssrcInfo => ssrcInfo.id)
  373. .filter((ssrc, index, array) => array.indexOf(ssrc) === index)
  374. .length;
  375. const numGroups = (videoMLine.ssrcGroups && videoMLine.ssrcGroups.length) || 0;
  376. if (numSsrcs > 1 && numGroups === 0) {
  377. // Ambiguous, can't figure out the primary
  378. return;
  379. }
  380. let primarySsrc = null;
  381. if (numSsrcs === 1) {
  382. primarySsrc = videoMLine.ssrcs[0].id;
  383. } else {
  384. if (numSsrcs === 2) {
  385. // Can figure it out if there's an FID group
  386. const fidGroup = videoMLine.ssrcGroups
  387. .find(group => group.semantics === "FID");
  388. if (fidGroup) {
  389. primarySsrc = fidGroup.ssrcs.split(" ")[0];
  390. }
  391. } else if (numSsrcs >= 3) {
  392. // Can figure it out if there's a sim group
  393. const simGroup = videoMLine.ssrcGroups
  394. .find(group => group.semantics === "SIM");
  395. if (simGroup) {
  396. primarySsrc = simGroup.ssrcs.split(" ")[0];
  397. }
  398. }
  399. }
  400. return primarySsrc;
  401. },
  402. /**
  403. * Generate an ssrc
  404. * @returns {number} an ssrc
  405. */
  406. generateSsrc() {
  407. return RandomUtil.randomInt(1, 0xffffffff);
  408. },
  409. /**
  410. * Get an attribute for the given ssrc with the given attributeName
  411. * from the given mline
  412. * @param {object} mLine an mLine object as parsed from transform.parse
  413. * @param {number} ssrc the ssrc for which an attribtue is desired
  414. * @param {string} attributeName the name of the desired attribute
  415. * @returns {string} the value corresponding to the given ssrc
  416. * and attributeName
  417. */
  418. getSsrcAttribute(mLine, ssrc, attributeName) {
  419. for (let i = 0; i < mLine.ssrcs.length; ++i) {
  420. const ssrcLine = mLine.ssrcs[i];
  421. if (ssrcLine.id === ssrc &&
  422. ssrcLine.attribute === attributeName) {
  423. return ssrcLine.value;
  424. }
  425. }
  426. },
  427. /**
  428. * Parses the ssrcs from the group sdp line and
  429. * returns them as a list of numbers
  430. * @param {object} the ssrcGroup object as parsed from
  431. * sdp-transform
  432. * @returns {list<number>} a list of the ssrcs in the group
  433. * parsed as numbers
  434. */
  435. parseGroupSsrcs(ssrcGroup) {
  436. return ssrcGroup
  437. .ssrcs
  438. .split(" ")
  439. .map(ssrcStr => parseInt(ssrcStr));
  440. },
  441. /**
  442. * Get the mline of the given type from the given sdp
  443. * @param {object} sdp sdp as parsed from transform.parse
  444. * @param {string} type the type of the desired mline (e.g. "video")
  445. * @returns {object} a media object
  446. */
  447. getMedia(sdp, type) {
  448. return sdp.media.find(m => m.type === type);
  449. },
  450. /**
  451. * Sets the given codecName as the preferred codec by
  452. * moving it to the beginning of the payload types
  453. * list (modifies the given mline in place). If there
  454. * are multiple options within the same codec (multiple h264
  455. * profiles, for instance), this will prefer the first one
  456. * that is found.
  457. * @param {object} videoMLine the video mline object from
  458. * an sdp as parsed by transform.parse
  459. * @param {string} the name of the preferred codec
  460. */
  461. preferVideoCodec(videoMLine, codecName) {
  462. let payloadType = null;
  463. for (let i = 0; i < videoMLine.rtp.length; ++i) {
  464. const rtp = videoMLine.rtp[i];
  465. if (rtp.codec === codecName) {
  466. payloadType = rtp.payload;
  467. break;
  468. }
  469. }
  470. if (payloadType) {
  471. const payloadTypes = videoMLine.payloads.split(" ").map(p => parseInt(p));
  472. const payloadIndex = payloadTypes.indexOf(payloadType);
  473. payloadTypes.splice(payloadIndex, 1);
  474. payloadTypes.unshift(payloadType);
  475. videoMLine.payloads = payloadTypes.join(" ");
  476. }
  477. },
  478. };
  479. module.exports = SDPUtil;