尝试修复无法获取下载进度的问题
This commit is contained in:
40
server.js
40
server.js
@@ -205,12 +205,15 @@ const broadcastWs = (key, payload) => {
|
||||
|
||||
// If this is a base key (not a sub-key), also broadcast to all its sub-keys
|
||||
if (!key.includes('-sub')) {
|
||||
for (const [subKey, subClients] of wsSubscriptions.entries()) {
|
||||
if (subKey.startsWith(`${key}-sub`)) {
|
||||
// Find the original raw key from the payload to reconstruct sub-keys
|
||||
const rawKey = payload.key;
|
||||
for (const [subRoomId, subClients] of wsSubscriptions.entries()) {
|
||||
if (subRoomId.startsWith(`${key}-sub`)) {
|
||||
// Extract the suffix from the room ID
|
||||
const suffix = subRoomId.substring(key.length); // e.g., "-sub1"
|
||||
for (const client of subClients) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
// We need to keep the payload key matching the room key for filtering on the client side
|
||||
const subPayload = { ...payload, key: subKey };
|
||||
const subPayload = { ...payload, key: `${rawKey}${suffix}` };
|
||||
client.send(JSON.stringify(subPayload));
|
||||
}
|
||||
}
|
||||
@@ -410,7 +413,13 @@ wss.on('connection', (ws) => {
|
||||
}
|
||||
addWsClient(progressKey, ws);
|
||||
|
||||
const currentProgress = progressMap[progressKey];
|
||||
// Check for current progress, fallback to base key if this is a sub-key and no specific progress exists
|
||||
let currentProgress = progressMap[progressKey];
|
||||
if (!currentProgress && progressKey.includes('-sub')) {
|
||||
const baseKey = progressKey.split('-sub')[0];
|
||||
currentProgress = progressMap[baseKey];
|
||||
}
|
||||
|
||||
if (currentProgress) {
|
||||
if (typeof currentProgress.duration === 'number' && currentProgress.duration > 0) {
|
||||
ws.send(JSON.stringify({ type: 'duration', key: message.key, duration: currentProgress.duration }));
|
||||
@@ -496,9 +505,14 @@ const ensureS3Downloaded = async (s3Client, bucket, key, tmpInputPath, progressK
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const writeStream = fs.createWriteStream(downloadingPath);
|
||||
let lastBroadcastTime = 0;
|
||||
|
||||
s3Stream.on('data', (chunk) => {
|
||||
downloadedBytes += chunk.length;
|
||||
const now = Date.now();
|
||||
|
||||
// Throttle broadcasts to max once every 100ms
|
||||
if (now - lastBroadcastTime > 100 || downloadedBytes === totalBytes) {
|
||||
const percent = totalBytes ? Math.min(100, Math.round((downloadedBytes / totalBytes) * 100)) : 0;
|
||||
const downloadState = {
|
||||
status: 'downloading',
|
||||
@@ -506,11 +520,13 @@ const ensureS3Downloaded = async (s3Client, bucket, key, tmpInputPath, progressK
|
||||
downloadedBytes,
|
||||
totalBytes,
|
||||
streamSessionId,
|
||||
details: totalBytes ? `Downloading source ${percent}%` : 'Downloading source...',
|
||||
details: totalBytes ? `正在从S3下载源文件 ${percent}%` : '正在从S3下载源文件...',
|
||||
mp4Url: null
|
||||
};
|
||||
progressMap[progressKey] = downloadState;
|
||||
broadcastWs(progressKey, { type: 'progress', key, progress: downloadState });
|
||||
lastBroadcastTime = now;
|
||||
}
|
||||
});
|
||||
|
||||
s3Stream.on('error', reject);
|
||||
@@ -876,6 +892,18 @@ app.get('/api/hls/segment.ts', async (req, res) => {
|
||||
|
||||
const tmpInputPath = getInputCachePath(bucket, key);
|
||||
|
||||
// Ensure the video is fully downloaded before attempting to transcode segments
|
||||
const auth = await extractS3Credentials(req);
|
||||
const s3Client = createS3Client(auth);
|
||||
const baseProgressKeyForDownload = getProgressKey(key);
|
||||
const streamSessionId = createStreamSessionId();
|
||||
try {
|
||||
await ensureS3Downloaded(s3Client, bucket, key, tmpInputPath, baseProgressKeyForDownload, streamSessionId);
|
||||
} catch (err) {
|
||||
console.error('S3 Download Failed for segment:', err);
|
||||
return res.status(500).send('S3 Download Failed');
|
||||
}
|
||||
|
||||
const baseProgressKey = getProgressKey(key);
|
||||
const subtitleSuffix = (subtitleIndex !== null && subtitleIndex !== undefined && subtitleIndex !== '-1') ? `-sub${subtitleIndex}` : '';
|
||||
const progressKey = `${baseProgressKey}${subtitleSuffix}`;
|
||||
|
||||
Reference in New Issue
Block a user