Skip to content

Commit

Permalink
common: improve ffmpeg kill func, add queue end promise
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Nov 28, 2023
1 parent 83c9d9a commit fde3c47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
5 changes: 4 additions & 1 deletion common/src/async-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> }[] = [];

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -124,6 +126,7 @@ export function createAsyncQueue<T>() {
get ended() {
return ended;
},
endPromise: endDeferred.promise,
take,
clear() {
return clear();
Expand Down
32 changes: 18 additions & 14 deletions server/src/media-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { ChildProcess } from "child_process";
import process from 'process';
import { sleep } from "./sleep";

const filtered = [
'decode_slice_header error',
'no frame!',
'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();
}
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) {
Expand Down

0 comments on commit fde3c47

Please sign in to comment.