lbry-desktop/ui/util/detect-user-bandwidth.js
mayeaux e3791aefdc
Send video bitrate and user bandwidth to Watchman (#7145)
* adding functionality to detect user download speed

* calculating bandwidth speed more intelligently

* saving download speed and updating it every 30s

* all the functionality should be done needs testing

* fix linting

* use a 1mb file for calculating bandwidth

* add optional chaining plugin to babel and get bitrate from texttrack

* allow optional chaining for flow

* ignore flow error

* disable bandwidth checking functionality

* fix flow error
2021-10-06 14:59:33 -04:00

21 lines
740 B
JavaScript

const imageAddr = 'https://upload.wikimedia.org/wikipedia/commons/b/b9/Pizigani_1367_Chart_1MB.jpg';
const downloadSize = 1093957; // this must match with the image above
let startTime, endTime;
async function measureConnectionSpeed() {
startTime = (new Date()).getTime();
const cacheBuster = '?nnn=' + startTime;
const download = new Image();
download.src = imageAddr + cacheBuster;
// this returns when the image is finished downloading
await download.decode();
endTime = (new Date()).getTime();
const duration = (endTime - startTime) / 1000;
const bitsLoaded = downloadSize * 8;
const speedBps = (bitsLoaded / duration).toFixed(2);
return Math.round(Number(speedBps));
}
module.exports = measureConnectionSpeed;