Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import {getLogger} from 'jitsi-meet-logger';
  2. const logger = getLogger(__filename);
  3. import RandomUtil from '../util/RandomUtil';
  4. const RTCBrowserType = require('../RTC/RTCBrowserType');
  5. const 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. let data = null;
  13. let 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. const data = {};
  40. const 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. const data = {};
  55. let 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. const parts = line.substring(10).split(' ');
  70. const sctpPort = parts[0];
  71. const protocol = parts[1];
  72. // Stream count is optional
  73. const streamCount = parts.length > 2 ? parts[2] : null;
  74. return [sctpPort, protocol, streamCount];// SCTP port
  75. },
  76. build_rtpmap(el) {
  77. let 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. const data = {};
  85. const 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. const data = {};
  96. const 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. const data = [];
  104. let parts = line.split(' ');
  105. parts.shift();
  106. parts = parts.join(' ').split(';');
  107. for (let i = 0; i < parts.length; i++) {
  108. let key = parts[i].split('=')[0];
  109. while (key.length && key[0] == ' ') {
  110. key = key.substring(1);
  111. }
  112. const value = parts[i].split('=')[1];
  113. if (key && value) {
  114. data.push({name: key, 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(line) {
  123. const candidate = {};
  124. const 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 (let 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(cand) {
  157. let 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(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. const data = {};
  191. const lines = desc.split('\r\n');
  192. for (let i = 0; i < lines.length; i++) {
  193. if (lines[i].substring(0, 7) == 'a=ssrc:') {
  194. const 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(line) {
  201. const parts = line.substr(10).split(' ');
  202. const data = {};
  203. data.pt = parts.shift();
  204. data.type = parts.shift();
  205. data.params = parts;
  206. return data;
  207. },
  208. parse_extmap(line) {
  209. const parts = line.substr(9).split(' ');
  210. const 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(haystack, needle, sessionpart) {
  223. let lines = haystack.split('\r\n');
  224. for (let 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 (let 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(haystack, needle, sessionpart) {
  242. let lines = haystack.split('\r\n');
  243. const needles = [];
  244. for (let i = 0; i < lines.length; i++) {
  245. if (lines[i].substring(0, needle.length) == needle) {
  246. needles.push(lines[i]);
  247. }
  248. }
  249. if (needles.length || !sessionpart) {
  250. return needles;
  251. }
  252. // search session part
  253. lines = sessionpart.split('\r\n');
  254. for (let j = 0; j < lines.length; j++) {
  255. if (lines[j].substring(0, needle.length) == needle) {
  256. needles.push(lines[j]);
  257. }
  258. }
  259. return needles;
  260. },
  261. candidateToJingle(line) {
  262. // a=candidate:2979166662 1 udp 2113937151 192.168.2.100 57698 typ host generation 0
  263. // <candidate component=... foundation=... generation=... id=... ip=... network=... port=... priority=... protocol=... type=.../>
  264. if (line.indexOf('candidate:') === 0) {
  265. line = 'a=' + line;
  266. } else if (line.substring(0, 12) != 'a=candidate:') {
  267. logger.log('parseCandidate called with a line that is not a candidate line');
  268. logger.log(line);
  269. return null;
  270. }
  271. if (line.substring(line.length - 2) == '\r\n') {// chomp it
  272. line = line.substring(0, line.length - 2);
  273. }
  274. const candidate = {};
  275. const elems = line.split(' ');
  276. if (elems[6] != 'typ') {
  277. logger.log('did not find typ in the right place');
  278. logger.log(line);
  279. return null;
  280. }
  281. candidate.foundation = elems[0].substring(12);
  282. candidate.component = elems[1];
  283. candidate.protocol = elems[2].toLowerCase();
  284. candidate.priority = elems[3];
  285. candidate.ip = elems[4];
  286. candidate.port = elems[5];
  287. // elems[6] => "typ"
  288. candidate.type = elems[7];
  289. candidate.generation = '0'; // default, may be overwritten below
  290. for (let i = 8; i < elems.length; i += 2) {
  291. switch (elems[i]) {
  292. case 'raddr':
  293. candidate['rel-addr'] = elems[i + 1];
  294. break;
  295. case 'rport':
  296. candidate['rel-port'] = elems[i + 1];
  297. break;
  298. case 'generation':
  299. candidate.generation = elems[i + 1];
  300. break;
  301. case 'tcptype':
  302. candidate.tcptype = elems[i + 1];
  303. break;
  304. default: // TODO
  305. logger.log('not translating "' + elems[i] + '" = "' + elems[i + 1] + '"');
  306. }
  307. }
  308. candidate.network = '1';
  309. candidate.id = Math.random().toString(36).substr(2, 10); // not applicable to SDP -- FIXME: should be unique, not just random
  310. return candidate;
  311. },
  312. candidateFromJingle(cand) {
  313. let line = 'a=candidate:';
  314. line += cand.getAttribute('foundation');
  315. line += ' ';
  316. line += cand.getAttribute('component');
  317. line += ' ';
  318. let protocol = cand.getAttribute('protocol');
  319. // use tcp candidates for FF
  320. if (RTCBrowserType.isFirefox() && protocol.toLowerCase() == 'ssltcp') {
  321. protocol = 'tcp';
  322. }
  323. line += protocol; // .toUpperCase(); // chrome M23 doesn't like this
  324. line += ' ';
  325. line += cand.getAttribute('priority');
  326. line += ' ';
  327. line += cand.getAttribute('ip');
  328. line += ' ';
  329. line += cand.getAttribute('port');
  330. line += ' ';
  331. line += 'typ';
  332. line += ' ' + cand.getAttribute('type');
  333. line += ' ';
  334. switch (cand.getAttribute('type')) {
  335. case 'srflx':
  336. case 'prflx':
  337. case 'relay':
  338. if (cand.getAttribute('rel-addr') && cand.getAttribute('rel-port')) {
  339. line += 'raddr';
  340. line += ' ';
  341. line += cand.getAttribute('rel-addr');
  342. line += ' ';
  343. line += 'rport';
  344. line += ' ';
  345. line += cand.getAttribute('rel-port');
  346. line += ' ';
  347. }
  348. break;
  349. }
  350. if (protocol.toLowerCase() == 'tcp') {
  351. line += 'tcptype';
  352. line += ' ';
  353. line += cand.getAttribute('tcptype');
  354. line += ' ';
  355. }
  356. line += 'generation';
  357. line += ' ';
  358. line += cand.getAttribute('generation') || '0';
  359. return line + '\r\n';
  360. },
  361. /**
  362. * Parse the 'most' primary video ssrc from the given m line
  363. * @param {object} mLine object as parsed from transform.parse
  364. * @return {number} the primary video ssrc from the given m line
  365. */
  366. parsePrimaryVideoSsrc(videoMLine) {
  367. const numSsrcs = videoMLine.ssrcs
  368. .map(ssrcInfo => ssrcInfo.id)
  369. .filter((ssrc, index, array) => array.indexOf(ssrc) === index)
  370. .length;
  371. const numGroups = (videoMLine.ssrcGroups && videoMLine.ssrcGroups.length) || 0;
  372. if (numSsrcs > 1 && numGroups === 0) {
  373. // Ambiguous, can't figure out the primary
  374. return;
  375. }
  376. let primarySsrc = null;
  377. if (numSsrcs === 1) {
  378. primarySsrc = videoMLine.ssrcs[0].id;
  379. } else {
  380. if (numSsrcs === 2) {
  381. // Can figure it out if there's an FID group
  382. const fidGroup = videoMLine.ssrcGroups
  383. .find(group => group.semantics === 'FID');
  384. if (fidGroup) {
  385. primarySsrc = fidGroup.ssrcs.split(' ')[0];
  386. }
  387. } else if (numSsrcs >= 3) {
  388. // Can figure it out if there's a sim group
  389. const simGroup = videoMLine.ssrcGroups
  390. .find(group => group.semantics === 'SIM');
  391. if (simGroup) {
  392. primarySsrc = simGroup.ssrcs.split(' ')[0];
  393. }
  394. }
  395. }
  396. return primarySsrc;
  397. },
  398. /**
  399. * Generate an ssrc
  400. * @returns {number} an ssrc
  401. */
  402. generateSsrc() {
  403. return RandomUtil.randomInt(1, 0xffffffff);
  404. },
  405. /**
  406. * Get an attribute for the given ssrc with the given attributeName
  407. * from the given mline
  408. * @param {object} mLine an mLine object as parsed from transform.parse
  409. * @param {number} ssrc the ssrc for which an attribtue is desired
  410. * @param {string} attributeName the name of the desired attribute
  411. * @returns {string} the value corresponding to the given ssrc
  412. * and attributeName
  413. */
  414. getSsrcAttribute(mLine, ssrc, attributeName) {
  415. for (let i = 0; i < mLine.ssrcs.length; ++i) {
  416. const ssrcLine = mLine.ssrcs[i];
  417. if (ssrcLine.id === ssrc
  418. && ssrcLine.attribute === attributeName) {
  419. return ssrcLine.value;
  420. }
  421. }
  422. },
  423. /**
  424. * Parses the ssrcs from the group sdp line and
  425. * returns them as a list of numbers
  426. * @param {object} the ssrcGroup object as parsed from
  427. * sdp-transform
  428. * @returns {list<number>} a list of the ssrcs in the group
  429. * parsed as numbers
  430. */
  431. parseGroupSsrcs(ssrcGroup) {
  432. return ssrcGroup
  433. .ssrcs
  434. .split(' ')
  435. .map(ssrcStr => parseInt(ssrcStr));
  436. },
  437. /**
  438. * Get the mline of the given type from the given sdp
  439. * @param {object} sdp sdp as parsed from transform.parse
  440. * @param {string} type the type of the desired mline (e.g. "video")
  441. * @returns {object} a media object
  442. */
  443. getMedia(sdp, type) {
  444. return sdp.media.find(m => m.type === type);
  445. },
  446. /**
  447. * Sets the given codecName as the preferred codec by
  448. * moving it to the beginning of the payload types
  449. * list (modifies the given mline in place). If there
  450. * are multiple options within the same codec (multiple h264
  451. * profiles, for instance), this will prefer the first one
  452. * that is found.
  453. * @param {object} videoMLine the video mline object from
  454. * an sdp as parsed by transform.parse
  455. * @param {string} the name of the preferred codec
  456. */
  457. preferVideoCodec(videoMLine, codecName) {
  458. let payloadType = null;
  459. for (let i = 0; i < videoMLine.rtp.length; ++i) {
  460. const rtp = videoMLine.rtp[i];
  461. if (rtp.codec === codecName) {
  462. payloadType = rtp.payload;
  463. break;
  464. }
  465. }
  466. if (payloadType) {
  467. const payloadTypes = videoMLine.payloads.split(' ').map(p => parseInt(p));
  468. const payloadIndex = payloadTypes.indexOf(payloadType);
  469. payloadTypes.splice(payloadIndex, 1);
  470. payloadTypes.unshift(payloadType);
  471. videoMLine.payloads = payloadTypes.join(' ');
  472. }
  473. },
  474. };
  475. module.exports = SDPUtil;