Backport pull request #18186 from jellyfin/release-12.z

Fix segment cleaner deleting segments ahead of playback when remuxing MKV

Original-merge: 3acc9c8443

Merged-by: crobibero <cody@robibe.ro>

Backported-by: Cody Robibero <cody@robibe.ro>
This commit is contained in:
klizas authored and Cody Robibero committed 2026-10-05 19:18:45 -04:00
1 parent 69e131eaa5
commit 7dc6544d34
5 files changed
+102 -10

No files matched your search

@@ -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<TranscodingJob> _logger;
private readonly Lock _processLock = new();
private readonly Lock _timerLock = new();
private readonly ConcurrentDictionary<int, long> _segmentEndPositionTicks = new();
private int _activeRequestCount;
private Timer? _killTimer;
@@ -156,6 +158,40 @@ public sealed class TranscodingJob : IDisposable
/// </summary>
public int PingTimeout { get; set; }
/// <summary>
/// Records the end position of a served segment.
/// </summary>
/// <param name="segmentIndex">The segment index.</param>
/// <param name="segmentEndPositionTicks">The segment end position ticks.</param>
public void ReportSegmentDownloaded(int segmentIndex, long segmentEndPositionTicks)
{
if (segmentIndex < 0)
{
return;
}
_segmentEndPositionTicks[segmentIndex] = segmentEndPositionTicks;
}
/// <summary>
/// Gets the highest index among served segments whose end position is at or before the given position.
/// </summary>
/// <param name="positionTicks">The position ticks.</param>
/// <returns>The segment index, or <c>null</c> if there is none.</returns>
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;
}
/// <summary>
/// Increments the active request count.
/// </summary>
@@ -22,7 +22,6 @@ public class TranscodingSegmentCleaner : IDisposable
private readonly IFileSystem _fileSystem;
private readonly IMediaEncoder _mediaEncoder;
private Timer? _timer;
private int _segmentLength;
/// <summary>
/// Initializes a new instance of the <see cref="TranscodingSegmentCleaner"/> class.
@@ -32,7 +31,7 @@ public class TranscodingSegmentCleaner : IDisposable
/// <param name="config">Instance of the <see cref="IConfigurationManager"/> interface.</param>
/// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
/// <param name="mediaEncoder">Instance of the <see cref="IMediaEncoder"/> interface.</param>
/// <param name="segmentLength">The segment length of this transcoding job.</param>
/// <param name="segmentLength">The segment length of this transcoding job. Unused.</param>
public TranscodingSegmentCleaner(TranscodingJob job, ILogger<TranscodingSegmentCleaner> logger, IConfigurationManager config, IFileSystem fileSystem, IMediaEncoder mediaEncoder, int segmentLength)
{
_job = job;
@@ -40,7 +39,6 @@ public class TranscodingSegmentCleaner : IDisposable
_config = config;
_fileSystem = fileSystem;
_mediaEncoder = mediaEncoder;
_segmentLength = segmentLength;
}
/// <summary>
@@ -104,11 +102,11 @@ public class TranscodingSegmentCleaner : IDisposable
if (downloadPositionSeconds > 0 && segmentKeepSeconds > 0 && downloadPositionSeconds > segmentKeepSeconds)
{
var idxMaxToDelete = (downloadPositionSeconds - segmentKeepSeconds) / _segmentLength;
var idxMaxToDelete = _job.GetHighestServedSegmentIndexEndingAtOrBefore(downloadPositionTicks - TimeSpan.FromSeconds(segmentKeepSeconds).Ticks);
if (idxMaxToDelete > 0)
{
await DeleteSegmentFiles(_job, 0, idxMaxToDelete, 1500).ConfigureAwait(false);
await DeleteSegmentFiles(_job, 0, idxMaxToDelete.Value, 1500).ConfigureAwait(false);
}
}
}