harii66 commited on
Commit
fb22294
·
verified ·
1 Parent(s): b4edbc0

Update static/js/downloader.js

Browse files
Files changed (1) hide show
  1. 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.buffer = [];
11
- this.downloaded = 0;
12
- this.totalSize = 0;
13
- this.isRunning = false;
14
- this.isStopped = false;
15
- this.controllers = [];
16
- this.currentIndex = 0;
17
- }
18
-
19
- async start() {
20
- if (this.isRunning) return;
21
-
22
- this.isRunning = true;
23
- this.isStopped = false;
24
-
25
- const workers = [];
26
- for (let i = 0; i < Math.min(this.thread, this.fragments.length); i++) {
27
- workers.push(this.downloadWorker());
28
- }
29
-
30
- await Promise.all(workers);
31
-
32
- if (!this.isStopped) {
33
- this.onComplete(this.buffer);
34
- }
35
- }
36
-
37
- async downloadWorker() {
38
- while (this.currentIndex < this.fragments.length && !this.isStopped) {
39
- const index = this.currentIndex++;
40
- const fragment = this.fragments[index];
41
-
42
- try {
43
- await this.downloadFragment(fragment);
44
- } catch (error) {
45
- this.onError(fragment, error);
46
- }
47
- }
48
- }
49
-
50
- async downloadFragment(fragment) {
51
- const controller = new AbortController();
52
- this.controllers[fragment.index] = controller;
53
-
54
- try {
55
- const response = await fetch(fragment.url, {
56
- signal: controller.signal
57
- });
58
-
59
- if (!response.ok) {
60
- throw new Error(`HTTP ${response.status}`);
61
- }
62
-
63
- const reader = response.body.getReader();
64
- const contentLength = parseInt(response.headers.get('content-length')) || 0;
65
- let receivedLength = 0;
66
- const chunks = [];
67
-
68
- while (true) {
69
- const { done, value } = await reader.read();
70
-
71
- if (done) break;
72
-
73
- chunks.push(value);
74
- receivedLength += value.length;
75
-
76
- this.onProgress({
77
- index: fragment.index,
78
- current: receivedLength,
79
- total: contentLength,
80
- percentage: contentLength ? (receivedLength / contentLength * 100).toFixed(2) : 0
81
- });
82
- }
83
-
84
- const buffer = new Uint8Array(receivedLength);
85
- let position = 0;
86
- for (const chunk of chunks) {
87
- buffer.set(chunk, position);
88
- position += chunk.length;
89
- }
90
-
91
- this.buffer[fragment.index] = buffer.buffer;
92
- this.downloaded++;
93
- this.totalSize += buffer.length;
94
-
95
- this.onItemComplete(fragment, buffer.buffer);
96
-
97
- } catch (error) {
98
- if (error.name !== 'AbortError') {
99
- throw error;
100
- }
101
- }
102
- }
103
-
104
- stop() {
105
- this.isStopped = true;
106
- this.isRunning = false;
107
- this.controllers.forEach(controller => {
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
  }