From b206254c77b383072d507d80b0cf46437e10cc1b Mon Sep 17 00:00:00 2001 From: Max Isom Date: Thu, 17 Mar 2022 19:17:22 -0400 Subject: [PATCH] Kill FFMPEG after stream is closed (#582) --- CHANGELOG.md | 2 ++ src/services/player.ts | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63fa116..ad547ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- There are no longer FFMPEG orphan processes after listening to a livestream ## [1.6.1] - 2022-03-15 ### Fixed diff --git a/src/services/player.ts b/src/services/player.ts index b3ed34d..020c501 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -527,14 +527,18 @@ export default class { capacitor.createReadStream().pipe(cacheStream); } + const returnedStream = capacitor.createReadStream(); + let hasReturnedStreamClosed = false; + const stream = ffmpeg(url) .inputOptions(options?.ffmpegInputOptions ?? ['-re']) .noVideo() .audioCodec('libopus') .outputFormat('webm') .on('error', error => { - console.error(error); - reject(error); + if (!hasReturnedStreamClosed) { + reject(error); + } }) .on('start', command => { debug(`Spawned ffmpeg with ${command as string}`); @@ -542,7 +546,12 @@ export default class { stream.pipe(capacitor); - resolve(capacitor.createReadStream()); + returnedStream.on('close', () => { + stream.kill('SIGKILL'); + hasReturnedStreamClosed = true; + }); + + resolve(returnedStream); }); } }