From 7dc6544d3441187f73393dac54dc358b3ceb98ed Mon Sep 17 00:00:00 2001 From: klizas Date: Mon, 5 Oct 2026 19:18:45 -0400 Subject: Backport pull request #18186 from jellyfin/release-12.z Fix segment cleaner deleting segments ahead of playback when remuxing MKV Original-merge: 3acc9c8443d7ee445c4fe015f2977a47d4ea57ac Merged-by: crobibero Backported-by: Cody Robibero --- .../MediaEncoding/TranscodingJob.cs | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'MediaBrowser.Controller/MediaEncoding/TranscodingJob.cs') diff --git a/MediaBrowser.Controller/MediaEncoding/TranscodingJob.cs b/MediaBrowser.Controller/MediaEncoding/TranscodingJob.cs index 5045030b9b..12600af384 100644 --- a/MediaBrowser.Controller/MediaEncoding/TranscodingJob.cs +++ b/MediaBrowser.Controller/MediaEncoding/TranscodingJob.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Diagnostics; using System.Threading; using MediaBrowser.Model.Dto; @@ -14,6 +15,7 @@ public sealed class TranscodingJob : IDisposable private readonly ILogger _logger; private readonly Lock _processLock = new(); private readonly Lock _timerLock = new(); + private readonly ConcurrentDictionary _segmentEndPositionTicks = new(); private int _activeRequestCount; private Timer? _killTimer; @@ -156,6 +158,40 @@ public sealed class TranscodingJob : IDisposable /// public int PingTimeout { get; set; } + /// + /// Records the end position of a served segment. + /// + /// The segment index. + /// The segment end position ticks. + public void ReportSegmentDownloaded(int segmentIndex, long segmentEndPositionTicks) + { + if (segmentIndex < 0) + { + return; + } + + _segmentEndPositionTicks[segmentIndex] = segmentEndPositionTicks; + } + + /// + /// Gets the highest index among served segments whose end position is at or before the given position. + /// + /// The position ticks. + /// The segment index, or null if there is none. + public int? GetHighestServedSegmentIndexEndingAtOrBefore(long positionTicks) + { + int? result = null; + foreach (var (index, endPositionTicks) in _segmentEndPositionTicks) + { + if (endPositionTicks <= positionTicks && (result is null || index > result)) + { + result = index; + } + } + + return result; + } + /// /// Increments the active request count. /// -- cgit v1.2.3