#178: always send something on stream failure

merge pull request #178 from dumbmoron/stream-error-handling
This commit is contained in:
wukko 2023-08-20 15:43:28 +06:00 committed by GitHub
commit c870c03271
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,11 @@ import got from "got";
import { ffmpegArgs, genericUserAgent } from "../config.js"; import { ffmpegArgs, genericUserAgent } from "../config.js";
import { getThreads, metadataManager, msToTime } from "../sub/utils.js"; import { getThreads, metadataManager, msToTime } from "../sub/utils.js";
function fail(res) {
if (!res.headersSent) res.sendStatus(500);
return res.destroy();
}
export function streamDefault(streamInfo, res) { export function streamDefault(streamInfo, res) {
try { try {
let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1];
@ -15,25 +20,17 @@ export function streamDefault(streamInfo, res) {
}, },
isStream: true isStream: true
}); });
stream.pipe(res).on('error', () => { stream.pipe(res).on('error', () => fail(res));
res.destroy(); stream.on('error', () => fail(res));
}); stream.on('aborted', () => fail(res));
stream.on('error', () => {
res.destroy();
});
stream.on('aborted', () => {
res.destroy();
});
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamLiveRender(streamInfo, res) { export function streamLiveRender(streamInfo, res) {
try { try {
if (streamInfo.urls.length !== 2) { if (streamInfo.urls.length !== 2) return fail(res);
res.destroy();
return;
}
let audio = got.get(streamInfo.urls[1], { isStream: true }); let audio = got.get(streamInfo.urls[1], { isStream: true });
let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [ let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [
'-loglevel', '-8', '-loglevel', '-8',
@ -57,24 +54,24 @@ export function streamLiveRender(streamInfo, res) {
res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`);
res.on('error', () => { res.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
ffmpegProcess.stdio[4].pipe(res).on('error', () => { ffmpegProcess.stdio[4].pipe(res).on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.pipe(ffmpegProcess.stdio[3]).on('error', () => { audio.pipe(ffmpegProcess.stdio[3]).on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.on('error', () => { audio.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
audio.on('aborted', () => { audio.on('aborted', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
ffmpegProcess.on('disconnect', () => ffmpegProcess.kill()); ffmpegProcess.on('disconnect', () => ffmpegProcess.kill());
@ -84,11 +81,11 @@ export function streamLiveRender(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamAudioOnly(streamInfo, res) { export function streamAudioOnly(streamInfo, res) {
@ -132,10 +129,10 @@ export function streamAudioOnly(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }
export function streamVideoOnly(streamInfo, res) { export function streamVideoOnly(streamInfo, res) {
@ -168,9 +165,9 @@ export function streamVideoOnly(streamInfo, res) {
res.on('close', () => ffmpegProcess.kill()); res.on('close', () => ffmpegProcess.kill());
ffmpegProcess.on('error', () => { ffmpegProcess.on('error', () => {
ffmpegProcess.kill(); ffmpegProcess.kill();
res.destroy(); fail(res);
}); });
} catch (e) { } catch (e) {
res.destroy(); fail(res);
} }
} }