diff --git a/common/src/main/java/ctbrec/recorder/download/hls/MergedFfmpegHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/MergedFfmpegHlsDownload.java index 4343e52e..0647682e 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/MergedFfmpegHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/MergedFfmpegHlsDownload.java @@ -51,6 +51,7 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload { private transient Process ffmpeg; private transient OutputStream ffmpegStdIn; private transient Thread ffmpegThread; + private transient Object ffmpegStartMonitor = new Object(); public MergedFfmpegHlsDownload(HttpClient client) { super(client); @@ -85,9 +86,21 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload { Files.createDirectories(targetFile.getParentFile().toPath()); 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); - ffmpegThread.join(); + if (ffmpeg == null) { + throw new ProcessExitedUncleanException("Couldn't spawn FFmpeg"); + } else { + LOG.debug("Starting to download segments"); + downloadSegments(segments, true); + ffmpegThread.join(); + } } catch (ParseException e) { throw new IOException("Couldn't parse stream information", e); } catch (PlaylistException e) { @@ -125,6 +138,9 @@ public class MergedFfmpegHlsDownload extends AbstractHlsDownload { LOG.debug("Command line: {}", Arrays.toString(cmdline)); ffmpeg = Runtime.getRuntime().exec(cmdline, new String[0], target.getParentFile()); + synchronized (ffmpegStartMonitor) { + ffmpegStartMonitor.notifyAll(); + } ffmpegStdIn = ffmpeg.getOutputStream(); int exitCode = 1; File ffmpegLog = File.createTempFile(target.getName(), ".log");