Update static/js/downloader.js
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108class HLSDownloader {
constructor(fragments = [], options = {}) {
this.fragments = fragments.map((f, i) => ({ ...f, index: i }));
this.thread = options.thread || 6;
this.onProgress = options.onProgress || (() => {});
this.onError = options.onError || (() => {});
this.onComplete = options.onComplete || (() => {});
this.onItemComplete = options.onItemComplete || (() => {});
this.buffer = [];
this.downloaded = 0;
this.totalSize = 0;
this.isRunning = false;
this.isStopped = false;
this.controllers = [];
this.currentIndex = 0;
}
async start() {
if (this.isRunning) return;
this.isRunning = true;
this.isStopped = false;
const workers = [];
for (let i = 0; i < Math.min(this.thread, this.fragments.length); i++) {
workers.push(this.downloadWorker());
}
await Promise.all(workers);
if (!this.isStopped) {
this.onComplete(this.buffer);
}
}
async downloadWorker() {
while (this.currentIndex < this.fragments.length && !this.isStopped) {
const index = this.currentIndex++;
const fragment = this.fragments[index];
try {
await this.downloadFragment(fragment);
} catch (error) {
this.onError(fragment, error);
}
}
}
async downloadFragment(fragment) {
const controller = new AbortController();
this.controllers[fragment.index] = controller;
try {
const response = await fetch(fragment.url, {
signal: controller.signal
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const reader = response.body.getReader();
const contentLength = parseInt(response.headers.get('content-length')) || 0;
let receivedLength = 0;
const chunks = [];
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
receivedLength += value.length;
this.onProgress({
index: fragment.index,
current: receivedLength,
total: contentLength,
percentage: contentLength ? (receivedLength / contentLength * 100).toFixed(2) : 0
});
}
const buffer = new Uint8Array(receivedLength);
let position = 0;
for (const chunk of chunks) {
buffer.set(chunk, position);
position += chunk.length;
}
this.buffer[fragment.index] = buffer.buffer;
this.downloaded++;
this.totalSize += buffer.length;
this.onItemComplete(fragment, buffer.buffer);
} catch (error) {
if (error.name !== 'AbortError') {
throw error;
}
}
}
stop() {
this.isStopped = true;
this.isRunning = false;
this.controllers.forEach(controller => {
if (controller) controller.abort();
});
}
getProgress() {
return {
downloaded: this.downloaded,
total: this.fragments.length,
percentage: (this.downloaded / this.fragments.length * 100).toFixed(2),
size: this.totalSize
};
}
}
if (typeof window !== 'undefined') {
window.HLSDownloader = HLSDownloader;
class HLSDownloader { constructor(fragments = [], options = {}) { this.fragments = fragments.map((f, i) => ({ ...f, index: i })); this.thread = options.thread || 6; this.onProgress = options.onProgress || (() => {}); this.onError = options.onError || (() => {}); this.onComplete = options.onComplete || (() => {}); this.onItemComplete = options.onItemComplete || (() => {}); this.buffer = []; this.downloaded = 0; this.totalSize = 0; this.isRunning = false; this.isStopped = false; this.controllers = []; this.currentIndex = 0; }
async start() { if (this.isRunning) return; this.isRunning = true; this.isStopped = false; const workers = []; for (let i = 0; i < Math.min(this.thread, this.fragments.length); i++) { workers.push(this.downloadWorker()); } await Promise.all(workers); if (!this.isStopped) { this.onComplete(this.buffer); } }
async downloadWorker() { while (this.currentIndex < this.fragments.length && !this.isStopped) { const index = this.currentIndex++; const fragment = this.fragments[index]; let downloadedSuccessfully = false;
while (!downloadedSuccessfully && !this.isStopped) { try { const controller = new AbortController(); this.controllers.push(controller); const response = await fetch(fragment.url, { signal: controller.signal }); if (!response.ok) throw new Error(response.status);
const reader = response.body.getReader(); const contentLength = +response.headers.get('Content-Length') || 0; let receivedLength = 0; const chunks = [];
while (true) { const { done, value } = await reader.read(); if (done) break; chunks.push(value); receivedLength += value.length; if (this.onProgress) { this.onProgress({ index: fragment.index, current: receivedLength, total: contentLength, percentage: contentLength ? ((receivedLength / contentLength) * 100).toFixed(2) : 0 }); } }
const buffer = new Uint8Array(receivedLength); let position = 0; for (const chunk of chunks) { buffer.set(chunk, position); position += chunk.length; }
this.buffer[fragment.index] = buffer.buffer; this.downloaded++; this.totalSize += buffer.length; if (this.onItemComplete) { this.onItemComplete(fragment, buffer.buffer); } downloadedSuccessfully = true;
} catch (error) { if (error.name === 'AbortError') break; await new Promise(resolve => setTimeout(resolve, 1500)); } } } }
stop() { this.isStopped = true; this.isRunning = false; this.controllers.forEach(c => c && c.abort()); }
getProgress() { return { downloaded: this.downloaded, total: this.fragments.length, percentage: (this.downloaded / this.fragments.length * 100).toFixed(2), size: this.totalSize }; }}
if (typeof window !== 'undefined') { window.HLSDownloader = HLSDownloader;}static/js/downloader.js+107-123