mirror of
https://github.com/smittix/intercept.git
synced 2026-07-19 23:08:10 -07:00
Retry listening audio stream fetch
This commit is contained in:
@@ -2218,34 +2218,48 @@ async function startFetchAudioStream(streamUrl, audioPlayer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(streamUrl, {
|
let attempts = 0;
|
||||||
cache: 'no-store',
|
while (attempts < 5) {
|
||||||
signal: audioFetchController.signal
|
attempts += 1;
|
||||||
});
|
const response = await fetch(streamUrl, {
|
||||||
if (!response.ok || !response.body) {
|
cache: 'no-store',
|
||||||
console.warn('[LISTEN] Fetch stream response invalid', response.status);
|
signal: audioFetchController.signal
|
||||||
resolve(false);
|
});
|
||||||
|
|
||||||
|
if (response.status === 204) {
|
||||||
|
console.warn('[LISTEN] Stream not ready (204), retrying...', attempts);
|
||||||
|
await new Promise(r => setTimeout(r, 500));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok || !response.body) {
|
||||||
|
console.warn('[LISTEN] Fetch stream response invalid', response.status);
|
||||||
|
resolve(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
const appendChunk = async (chunk) => {
|
||||||
|
if (!chunk || chunk.length === 0) return;
|
||||||
|
if (!sourceBuffer.updating) {
|
||||||
|
sourceBuffer.appendBuffer(chunk);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await new Promise(r => sourceBuffer.addEventListener('updateend', r, { once: true }));
|
||||||
|
sourceBuffer.appendBuffer(chunk);
|
||||||
|
};
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
await appendChunk(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reader = response.body.getReader();
|
resolve(false);
|
||||||
const appendChunk = async (chunk) => {
|
|
||||||
if (!chunk || chunk.length === 0) return;
|
|
||||||
if (!sourceBuffer.updating) {
|
|
||||||
sourceBuffer.appendBuffer(chunk);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
await new Promise(r => sourceBuffer.addEventListener('updateend', r, { once: true }));
|
|
||||||
sourceBuffer.appendBuffer(chunk);
|
|
||||||
};
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) break;
|
|
||||||
await appendChunk(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(true);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.name !== 'AbortError') {
|
if (e.name !== 'AbortError') {
|
||||||
console.error('[LISTEN] Fetch stream error:', e);
|
console.error('[LISTEN] Fetch stream error:', e);
|
||||||
|
|||||||
Reference in New Issue
Block a user