-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathindex.tsx
executable file
·316 lines (253 loc) · 13.5 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import React, { useRef, useEffect, useState } from 'react';
import Hls from 'hls.js'; // @types/hls.js
import apiUrls from '@/config/apiUrls';
interface VideoPlayerProps {
url: string;
id: number;
poster?: string;
}
const formatSpeed = (bytesPerSecond: number): string => {
if (bytesPerSecond >= 1024 * 1024) {
return `${(bytesPerSecond / (1024 * 1024)).toFixed(2)} MB/s`;
} else if (bytesPerSecond >= 1024) {
return `${(bytesPerSecond / 1024).toFixed(2)} KB/s`;
}
return `${bytesPerSecond.toFixed(2)} B/s`;
};
const VideoPlayer: React.FC<VideoPlayerProps> = ({ url, id, poster }) => {
const videoRef = useRef<HTMLVideoElement>(null);
const hlsRef = useRef<Hls | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const [progress, setProgress] = useState<number>(0);
const [totalSize, setTotalSize] = useState<number>(0);
const [downloadSpeed, setDownloadSpeed] = useState<number>(0);
// speed
const lastTime = useRef<number>(0);
const lastReceivedLength = useRef<number>(0);
// Create Stream Cache
const urlObj = new URL(url);
const filePath = urlObj.pathname;
const reqUrl = `${apiUrls.VIDEO_URL}?filePath=${encodeURIComponent(filePath)}&id=${id}`;
// Add a ref to track accumulated size
const accumulatedSizeRef = useRef<number>(0);
// Add new ref for tracking segments
const segmentsRef = useRef<Set<string>>(new Set());
const fetchM3U8 = async (url: string) => {
try {
const response = await fetch(url);
const text = await response.text();
// console.log("M3U8 Content:", text);
// Get the total size information from the m3u8 file
const totalSizeMatch = text.match(/#EXT-X-TOTAL-SIZE:(\d+)/);
if (totalSizeMatch) {
setTotalSize(parseInt(totalSizeMatch[1]));
}
} catch (error) {
console.error("Failed to fetch M3U8:", error);
}
};
useEffect(() => {
// Create Stream Cache
fetch(reqUrl)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
const _totalSize = data.data.totalSize; // DO NOT USE "Content-Length" from cloud server;
setTotalSize(_totalSize);
const setupHls = async () => {
if (!videoRef.current) return;
try {
const hlsUrl = `${apiUrls.HLS_VIDEO_URL.replace('{id}', id as never).replace('{slug}', 'playlist.m3u8')}`;
// Check for native HLS support first (Safari)
if (videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
console.log('--> Using native HLS support');
videoRef.current.src = hlsUrl;
// Modified progress tracking for Safari
const trackProgressSafari = async () => {
try {
// First fetch the m3u8 playlist
const playlistResponse = await fetch(hlsUrl);
const playlist = await playlistResponse.text();
// Parse the playlist to get ts segments
const tsSegments = playlist.split('\n')
.filter(line => line.endsWith('.ts'))
.map(segment => segment.trim());
// Track each segment
for (const segment of tsSegments) {
if (!segmentsRef.current.has(segment)) {
const segmentUrl = `${apiUrls.HLS_VIDEO_URL.replace('{id}', id as never).replace('{slug}', segment)}`;
const xhr = new XMLHttpRequest();
xhr.open('GET', segmentUrl, true);
xhr.onprogress = (event) => {
if (!segmentsRef.current.has(segment)) {
accumulatedSizeRef.current += event.loaded;
// Speed update
const currentTime = Date.now();
const timeDiff = (currentTime - lastTime.current) / 1000;
if (timeDiff >= 1) {
const speed = (accumulatedSizeRef.current - lastReceivedLength.current) / timeDiff;
setDownloadSpeed(speed);
lastTime.current = currentTime;
lastReceivedLength.current = accumulatedSizeRef.current;
}
// Progress update
if (_totalSize > 0) {
const newProgress = (accumulatedSizeRef.current / _totalSize) * 100;
setProgress(Math.min(newProgress, 100));
}
}
};
xhr.onload = () => {
segmentsRef.current.add(segment);
};
xhr.send();
}
}
// Continue tracking if not all segments are loaded
if (accumulatedSizeRef.current < _totalSize) {
setTimeout(trackProgressSafari, 1000);
}
} catch (error) {
console.error('Error tracking progress:', error);
}
};
// Start tracking progress
trackProgressSafari();
videoRef.current.addEventListener('loadedmetadata', () => {
setIsLoading(false);
videoRef.current?.play().catch(console.error);
});
} else if (Hls.isSupported()) {
if (hlsRef.current) {
hlsRef.current.destroy();
}
const hls = new Hls({
enableWorker: true,
lowLatencyMode: true,
backBufferLength: 90,
xhrSetup: (xhr, url) => {
// Listen to the download progress
xhr.addEventListener('progress', (event) => {
// Update accumulated size for this segment
accumulatedSizeRef.current += event.loaded;
// Speed update
const currentTime = Date.now();
const timeDiff = (currentTime - lastTime.current) / 1000; // seconds
if (timeDiff >= 1) {
const speed = (accumulatedSizeRef.current - lastReceivedLength.current) / timeDiff; // bytes per second
setDownloadSpeed(speed);
lastTime.current = currentTime;
lastReceivedLength.current = accumulatedSizeRef.current;
}
// Progress update
if (_totalSize > 0) {
const newProgress = (accumulatedSizeRef.current / _totalSize) * 100;
setProgress(Math.min(newProgress, 100));
}
});
// Rewrite URLs to use our API endpoints
if (url.includes('.m3u8')) {
xhr.open('GET', `${apiUrls.HLS_VIDEO_URL.replace('{id}', id as never).replace('{slug}', 'playlist.m3u8')}`, true);
} else if (url.includes('.key')) {
xhr.open('GET', `${apiUrls.HLS_VIDEO_URL.replace('{id}', id as never).replace('{slug}', 'enc.key')}`, true);
} else if (url.includes('.ts')) {
const segmentName = url.split('/').pop();
if (segmentName) {
xhr.open('GET', `${apiUrls.HLS_VIDEO_URL.replace('{id}', id as never).replace('{slug}', segmentName)}`, true);
}
}
}
});
hlsRef.current = hls;
// Attach media and load source
hls.attachMedia(videoRef.current);
hls.on(Hls.Events.MEDIA_ATTACHED, () => {
console.log('--> HLS Media attached');
hls.loadSource(hlsUrl);
});
hls.on(Hls.Events.MANIFEST_PARSED, (event, data: any) => {
console.log('--> HLS Manifest parsed');
setIsLoading(false);
videoRef.current?.play().catch(console.error);
// Scrape M3U8 file content directly
// if (hls.url) {
// fetchM3U8(hls.url);
// }
});
hls.on(Hls.Events.FRAG_LOADED, (event, data: any) => {
// Fragment loaded event handling is now optional
// since we're tracking progress in xhr progress event
});
hls.on(Hls.Events.ERROR, (event, data) => {
console.error('HLS Error:', data);
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
console.log('Fatal network error encountered');
hls.startLoad();
break;
case Hls.ErrorTypes.MEDIA_ERROR:
console.log('Fatal media error encountered');
hls.recoverMediaError();
break;
default:
hls.destroy();
setError('Fatal HLS error');
break;
}
}
});
} else if (videoRef.current.canPlayType('application/vnd.apple.mpegurl')) {
videoRef.current.src = hlsUrl;
videoRef.current.addEventListener('loadedmetadata', () => {
setIsLoading(false);
videoRef.current?.play().catch(console.error);
});
} else {
setError('HLS is not supported in this browser');
}
} catch (err) {
console.error('Error setting up HLS:', err);
setError('Failed to load video');
}
};
setupHls();
});
return () => {
// Clear segments set on cleanup
segmentsRef.current.clear();
if (hlsRef.current) {
hlsRef.current.destroy();
hlsRef.current = null;
}
};
}, [url, id]);
return (
<div className="relative">
{progress < 100 ? (
<div className="text-center">
<small>(Video) Loading... {progress.toFixed(2)}%, Speed:{formatSpeed(downloadSpeed)} </small>
<div className="absolute bottom-0 left-0 right-0 bg-primary" style={{ height: '5px', width: `${progress}%` }}>
</div>
</div>
) : null}
<div className="app-ratio app-ratio-16x9" style={{display: progress < 100 ? 'none' : ''}}>
<video
ref={videoRef}
autoPlay
muted
loop
playsInline
preload="auto"
poster={poster}
></video>
</div>
</div>
);
};
export default VideoPlayer;