common: improve ffmpeg kill func, add queue end promise

This commit is contained in:
Koushik Dutta
2023-11-27 19:04:09 -08:00
parent 83c9d9a4a6
commit fde3c47d8c
2 changed files with 22 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ class EndError extends Error {
export function createAsyncQueue<T>() {
let ended: Error | undefined;
const endDeferred = new Deferred<void>();
const waiting: Deferred<T>[] = [];
const queued: { item: T, dequeued?: Deferred<void> }[] = [];
@@ -75,7 +76,8 @@ export function createAsyncQueue<T>() {
if (ended)
return false;
// catch to prevent unhandled rejection.
ended = e || new EndError()
ended = e || new EndError();
endDeferred.resolve();
while (waiting.length) {
waiting.shift().reject(ended);
}
@@ -124,6 +126,7 @@ export function createAsyncQueue<T>() {
get ended() {
return ended;
},
endPromise: endDeferred.promise,
take,
clear() {
return clear();

View File

@@ -1,5 +1,6 @@
import { ChildProcess } from "child_process";
import process from 'process';
import { sleep } from "./sleep";
const filtered = [
'decode_slice_header error',
@@ -7,17 +8,22 @@ const filtered = [
'non-existing PPS',
];
export function safeKillFFmpeg(cp: ChildProcess) {
export async function safeKillFFmpeg(cp: ChildProcess) {
if (!cp)
return;
// this will allow ffmpeg to send rtsp TEARDOWN etc
try {
cp.stdin.on('error', () => {});
cp.stdin.write('q\n');
}
catch (e) {
}
setTimeout(() => {
if (cp.exitCode != null)
return;
await new Promise(async resolve => {
cp.on('exit', resolve);
// this will allow ffmpeg to send rtsp TEARDOWN etc
try {
cp.stdin.on('error', () => { });
cp.stdin.write('q\n');
}
catch (e) {
}
await sleep(2000);
for (const f of cp.stdio) {
try {
f?.destroy();
@@ -25,11 +31,9 @@ export function safeKillFFmpeg(cp: ChildProcess) {
catch (e) {
}
}
cp.kill();
setTimeout(() => {
cp.kill('SIGKILL');
}, 2000);
}, 2000);
await sleep(2000);
cp.kill('SIGKILL');
});
}
export function ffmpegLogInitialOutput(console: Console, cp: ChildProcess, forever?: boolean, storage?: Storage) {