diff options
author | metamuffin <metamuffin@disroot.org> | 2023-10-02 21:35:31 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-10-02 21:35:31 +0200 |
commit | 7450134996a94c4629f1e112e90ad458a84c0c04 (patch) | |
tree | 0d5e80cc66ccb7da53bf936e2cc063b90a219a1d /web/script/player/download.ts | |
parent | f783143b4adf22be662db1af2ca00b34a868cf72 (diff) | |
download | jellything-7450134996a94c4629f1e112e90ad458a84c0c04.tar jellything-7450134996a94c4629f1e112e90ad458a84c0c04.tar.bz2 jellything-7450134996a94c4629f1e112e90ad458a84c0c04.tar.zst |
estimate bandwidth
Diffstat (limited to 'web/script/player/download.ts')
-rw-r--r-- | web/script/player/download.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/web/script/player/download.ts b/web/script/player/download.ts new file mode 100644 index 0000000..63085b8 --- /dev/null +++ b/web/script/player/download.ts @@ -0,0 +1,42 @@ +import { OVar } from "../jshelper/mod.ts"; + +interface Measurement { time: number, duration: number, size: number } +export class SegmentDownloader { + private measurements: Measurement[] = [] + + public bandwidth = new OVar(Infinity) + + constructor() { } + + async download(url: string): Promise<ArrayBuffer> { + const dl_start = performance.now(); + const res = await fetch(url) + const dl_header = performance.now(); + if (!res.ok) throw new Error("aaaaa"); + const buf = await res.arrayBuffer() + const dl_body = performance.now(); + + if (buf.byteLength > 500 * 1000) { + const m = { + time: dl_start, + duration: (dl_body - dl_header) / 1000, + size: buf.byteLength + } + console.log(m); + this.measurements.push(m) + this.update_bandwidth() + } + return buf; + } + + update_bandwidth() { + while (this.measurements.length > 32) + this.measurements.splice(0, 1) + const total_size = this.measurements.reduce((a, v) => v.size + a, 0) + const total_duration = this.measurements.reduce((a, v) => v.duration + a, 0) + const average = total_size / total_duration + console.log(total_size, average, this.measurements); + + this.bandwidth.value = average + } +} |