From b13c4f162289f52e2a5a7665f3750a41638e53f0 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Fri, 13 Aug 2021 16:43:12 +0200 Subject: [PATCH] Add stalled download detection If no segments have been transfered for 30 seconds, the download will be stopped. --- .../recorder/download/hls/AbstractHlsDownload.java | 12 +++++++++++- .../ctbrec/recorder/download/hls/HlsDownload.java | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java index bcaa89fb..25c92864 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/AbstractHlsDownload.java @@ -72,6 +72,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload { private static final transient Logger LOG = LoggerFactory.getLogger(AbstractHlsDownload.class); private static final int A_FEW_SECONDS = 10_000; + private static final int MAX_SECONDS_WITHOUT_TRANSFER = 30; private transient NumberFormat nf = new DecimalFormat("000000"); private transient int playlistEmptyCount = 0; @@ -87,6 +88,7 @@ public abstract class AbstractHlsDownload extends AbstractDownload { private transient Instant beforeLastPlaylistRequest= Instant.EPOCH; private transient int consecutivePlaylistTimeouts = 0; private transient int consecutivePlaylistErrors = 0; + private transient Instant lastSegmentDownload = Instant.MAX; private transient List recordingEvents = new LinkedList<>(); @@ -95,7 +97,15 @@ public abstract class AbstractHlsDownload extends AbstractDownload { } protected abstract OutputStream getSegmentOutputStream(String prefix, String fileName) throws IOException; - protected void segmentDownloadFinished(SegmentDownload segmentDownload) {} + + protected void segmentDownloadFinished(SegmentDownload segmentDownload) { // NOSONAR + if (Duration.between(lastSegmentDownload, Instant.now()).getSeconds() > MAX_SECONDS_WITHOUT_TRANSFER) { + LOG.info("No video data received for {} seconds. Stopping recording for model {}", MAX_SECONDS_WITHOUT_TRANSFER, model); + stop(); + } + lastSegmentDownload = Instant.now(); + } + protected abstract void internalStop(); @Override diff --git a/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java b/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java index fbb8fa9c..cc1d4a8c 100644 --- a/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/hls/HlsDownload.java @@ -138,6 +138,7 @@ public class HlsDownload extends AbstractHlsDownload { @Override protected void segmentDownloadFinished(SegmentDownload segmentDownload) { + super.segmentDownloadFinished(segmentDownload); IoUtils.close(segmentDownload.getOutputStream(), "Couldn't close segment file"); } }