forked from j62/ctbrec
Fix race condition causing orphaned FFmpeg processes
The problem was, that an error occured before FFmpeg was completely launched. ctbrec called internalStop, but the FFmpeg fields still pointed to null. ctbrec then finished the recording. In the meantime FFmpeg fired up and was abandoned by the recording.
This commit is contained in:
parent
b820021aee
commit
ef3c036f49
|
@ -51,6 +51,7 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload {
|
||||||
private transient Process ffmpeg;
|
private transient Process ffmpeg;
|
||||||
private transient OutputStream ffmpegStdIn;
|
private transient OutputStream ffmpegStdIn;
|
||||||
private transient Thread ffmpegThread;
|
private transient Thread ffmpegThread;
|
||||||
|
private transient Object ffmpegStartMonitor = new Object();
|
||||||
|
|
||||||
public MergedFfmpegHlsDownload(HttpClient client) {
|
public MergedFfmpegHlsDownload(HttpClient client) {
|
||||||
super(client);
|
super(client);
|
||||||
|
@ -85,9 +86,21 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload {
|
||||||
|
|
||||||
Files.createDirectories(targetFile.getParentFile().toPath());
|
Files.createDirectories(targetFile.getParentFile().toPath());
|
||||||
startFfmpegProcess(targetFile);
|
startFfmpegProcess(targetFile);
|
||||||
|
synchronized (ffmpegStartMonitor) {
|
||||||
|
int tries = 0;
|
||||||
|
while (ffmpeg == null && tries++ < 15) {
|
||||||
|
LOG.debug("Waiting for FFmpeg to spawn to record {}", model.getName());
|
||||||
|
ffmpegStartMonitor.wait(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
downloadSegments(segments, true);
|
if (ffmpeg == null) {
|
||||||
ffmpegThread.join();
|
throw new ProcessExitedUncleanException("Couldn't spawn FFmpeg");
|
||||||
|
} else {
|
||||||
|
LOG.debug("Starting to download segments");
|
||||||
|
downloadSegments(segments, true);
|
||||||
|
ffmpegThread.join();
|
||||||
|
}
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new IOException("Couldn't parse stream information", e);
|
throw new IOException("Couldn't parse stream information", e);
|
||||||
} catch (PlaylistException e) {
|
} catch (PlaylistException e) {
|
||||||
|
@ -125,6 +138,9 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload {
|
||||||
|
|
||||||
LOG.debug("Command line: {}", Arrays.toString(cmdline));
|
LOG.debug("Command line: {}", Arrays.toString(cmdline));
|
||||||
ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], target.getParentFile());
|
ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], target.getParentFile());
|
||||||
|
synchronized (ffmpegStartMonitor) {
|
||||||
|
ffmpegStartMonitor.notifyAll();
|
||||||
|
}
|
||||||
ffmpegStdIn = ffmpeg.getOutputStream();
|
ffmpegStdIn = ffmpeg.getOutputStream();
|
||||||
int exitCode = 1;
|
int exitCode = 1;
|
||||||
File ffmpegLog = File.createTempFile(target.getName(), ".log");
|
File ffmpegLog = File.createTempFile(target.getName(), ".log");
|
||||||
|
|
Loading…
Reference in New Issue