Update static/js/downloader.js
Browse files- static/js/downloader.js +107 -123
static/js/downloader.js
CHANGED
|
@@ -1,124 +1,108 @@
|
|
| 1 |
-
class HLSDownloader {
|
| 2 |
-
constructor(fragments = [], options = {}) {
|
| 3 |
-
this.fragments = fragments.map((f, i) => ({ ...f, index: i }));
|
| 4 |
-
this.thread = options.thread || 6;
|
| 5 |
-
this.onProgress = options.onProgress || (() => {});
|
| 6 |
-
this.onError = options.onError || (() => {});
|
| 7 |
-
this.onComplete = options.onComplete || (() => {});
|
| 8 |
-
this.onItemComplete = options.onItemComplete || (() => {});
|
| 9 |
-
|
| 10 |
-
this.
|
| 11 |
-
this.
|
| 12 |
-
this.
|
| 13 |
-
this.
|
| 14 |
-
this.
|
| 15 |
-
this.
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
if (controller) controller.abort();
|
| 109 |
-
});
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
getProgress() {
|
| 113 |
-
return {
|
| 114 |
-
downloaded: this.downloaded,
|
| 115 |
-
total: this.fragments.length,
|
| 116 |
-
percentage: (this.downloaded / this.fragments.length * 100).toFixed(2),
|
| 117 |
-
size: this.totalSize
|
| 118 |
-
};
|
| 119 |
-
}
|
| 120 |
-
}
|
| 121 |
-
|
| 122 |
-
if (typeof window !== 'undefined') {
|
| 123 |
-
window.HLSDownloader = HLSDownloader;
|
| 124 |
}
|
|
|
|
| 1 |
+
class HLSDownloader {
|
| 2 |
+
constructor(fragments = [], options = {}) {
|
| 3 |
+
this.fragments = fragments.map((f, i) => ({ ...f, index: i }));
|
| 4 |
+
this.thread = options.thread || 6;
|
| 5 |
+
this.onProgress = options.onProgress || (() => {});
|
| 6 |
+
this.onError = options.onError || (() => {});
|
| 7 |
+
this.onComplete = options.onComplete || (() => {});
|
| 8 |
+
this.onItemComplete = options.onItemComplete || (() => {});
|
| 9 |
+
this.buffer = [];
|
| 10 |
+
this.downloaded = 0;
|
| 11 |
+
this.totalSize = 0;
|
| 12 |
+
this.isRunning = false;
|
| 13 |
+
this.isStopped = false;
|
| 14 |
+
this.controllers = [];
|
| 15 |
+
this.currentIndex = 0;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
async start() {
|
| 19 |
+
if (this.isRunning) return;
|
| 20 |
+
this.isRunning = true;
|
| 21 |
+
this.isStopped = false;
|
| 22 |
+
const workers = [];
|
| 23 |
+
for (let i = 0; i < Math.min(this.thread, this.fragments.length); i++) {
|
| 24 |
+
workers.push(this.downloadWorker());
|
| 25 |
+
}
|
| 26 |
+
await Promise.all(workers);
|
| 27 |
+
if (!this.isStopped) {
|
| 28 |
+
this.onComplete(this.buffer);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
async downloadWorker() {
|
| 33 |
+
while (this.currentIndex < this.fragments.length && !this.isStopped) {
|
| 34 |
+
const index = this.currentIndex++;
|
| 35 |
+
const fragment = this.fragments[index];
|
| 36 |
+
let downloadedSuccessfully = false;
|
| 37 |
+
|
| 38 |
+
while (!downloadedSuccessfully && !this.isStopped) {
|
| 39 |
+
try {
|
| 40 |
+
const controller = new AbortController();
|
| 41 |
+
this.controllers.push(controller);
|
| 42 |
+
const response = await fetch(fragment.url, { signal: controller.signal });
|
| 43 |
+
|
| 44 |
+
if (!response.ok) throw new Error(response.status);
|
| 45 |
+
|
| 46 |
+
const reader = response.body.getReader();
|
| 47 |
+
const contentLength = +response.headers.get('Content-Length') || 0;
|
| 48 |
+
let receivedLength = 0;
|
| 49 |
+
const chunks = [];
|
| 50 |
+
|
| 51 |
+
while (true) {
|
| 52 |
+
const { done, value } = await reader.read();
|
| 53 |
+
if (done) break;
|
| 54 |
+
chunks.push(value);
|
| 55 |
+
receivedLength += value.length;
|
| 56 |
+
if (this.onProgress) {
|
| 57 |
+
this.onProgress({
|
| 58 |
+
index: fragment.index,
|
| 59 |
+
current: receivedLength,
|
| 60 |
+
total: contentLength,
|
| 61 |
+
percentage: contentLength ? ((receivedLength / contentLength) * 100).toFixed(2) : 0
|
| 62 |
+
});
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
const buffer = new Uint8Array(receivedLength);
|
| 67 |
+
let position = 0;
|
| 68 |
+
for (const chunk of chunks) {
|
| 69 |
+
buffer.set(chunk, position);
|
| 70 |
+
position += chunk.length;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
this.buffer[fragment.index] = buffer.buffer;
|
| 74 |
+
this.downloaded++;
|
| 75 |
+
this.totalSize += buffer.length;
|
| 76 |
+
|
| 77 |
+
if (this.onItemComplete) {
|
| 78 |
+
this.onItemComplete(fragment, buffer.buffer);
|
| 79 |
+
}
|
| 80 |
+
downloadedSuccessfully = true;
|
| 81 |
+
|
| 82 |
+
} catch (error) {
|
| 83 |
+
if (error.name === 'AbortError') break;
|
| 84 |
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
stop() {
|
| 91 |
+
this.isStopped = true;
|
| 92 |
+
this.isRunning = false;
|
| 93 |
+
this.controllers.forEach(c => c && c.abort());
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
getProgress() {
|
| 97 |
+
return {
|
| 98 |
+
downloaded: this.downloaded,
|
| 99 |
+
total: this.fragments.length,
|
| 100 |
+
percentage: (this.downloaded / this.fragments.length * 100).toFixed(2),
|
| 101 |
+
size: this.totalSize
|
| 102 |
+
};
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
if (typeof window !== 'undefined') {
|
| 107 |
+
window.HLSDownloader = HLSDownloader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
}
|