aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Jellyfin.Api/Helpers/ProgressiveFileStream.cs')
-rw-r--r--Jellyfin.Api/Helpers/ProgressiveFileStream.cs37
1 files changed, 26 insertions, 11 deletions
diff --git a/Jellyfin.Api/Helpers/ProgressiveFileStream.cs b/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
index d4cc0172d..993973417 100644
--- a/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
+++ b/Jellyfin.Api/Helpers/ProgressiveFileStream.cs
@@ -13,9 +13,9 @@ namespace Jellyfin.Api.Helpers
/// </summary>
public class ProgressiveFileStream : Stream
{
- private readonly FileStream _fileStream;
+ private readonly Stream _stream;
private readonly TranscodingJobDto? _job;
- private readonly TranscodingJobHelper _transcodingJobHelper;
+ private readonly TranscodingJobHelper? _transcodingJobHelper;
private readonly int _timeoutMs;
private readonly bool _allowAsyncFileRead;
private int _bytesWritten;
@@ -33,7 +33,6 @@ namespace Jellyfin.Api.Helpers
_job = job;
_transcodingJobHelper = transcodingJobHelper;
_timeoutMs = timeoutMs;
- _bytesWritten = 0;
var fileOptions = FileOptions.SequentialScan;
_allowAsyncFileRead = false;
@@ -45,11 +44,27 @@ namespace Jellyfin.Api.Helpers
_allowAsyncFileRead = true;
}
- _fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, fileOptions);
+ _stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, fileOptions);
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class.
+ /// </summary>
+ /// <param name="stream">The stream to progressively copy.</param>
+ /// <param name="job">The transcoding job information.</param>
+ /// <param name="transcodingJobHelper">The transcoding job helper.</param>
+ /// <param name="timeoutMs">The timeout duration in milliseconds.</param>
+ public ProgressiveFileStream(Stream stream, TranscodingJobDto? job, TranscodingJobHelper? transcodingJobHelper, int timeoutMs = 30000)
+ {
+ _job = job;
+ _transcodingJobHelper = transcodingJobHelper;
+ _timeoutMs = timeoutMs;
+ _allowAsyncFileRead = AsyncFile.UseAsyncIO;
+ _stream = stream;
}
/// <inheritdoc />
- public override bool CanRead => _fileStream.CanRead;
+ public override bool CanRead => _stream.CanRead;
/// <inheritdoc />
public override bool CanSeek => false;
@@ -70,13 +85,13 @@ namespace Jellyfin.Api.Helpers
/// <inheritdoc />
public override void Flush()
{
- _fileStream.Flush();
+ _stream.Flush();
}
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
- return _fileStream.Read(buffer, offset, count);
+ return _stream.Read(buffer, offset, count);
}
/// <inheritdoc />
@@ -93,11 +108,11 @@ namespace Jellyfin.Api.Helpers
int bytesRead;
if (_allowAsyncFileRead)
{
- bytesRead = await _fileStream.ReadAsync(buffer, newOffset, remainingBytesToRead, cancellationToken).ConfigureAwait(false);
+ bytesRead = await _stream.ReadAsync(buffer, newOffset, remainingBytesToRead, cancellationToken).ConfigureAwait(false);
}
else
{
- bytesRead = _fileStream.Read(buffer, newOffset, remainingBytesToRead);
+ bytesRead = _stream.Read(buffer, newOffset, remainingBytesToRead);
}
remainingBytesToRead -= bytesRead;
@@ -152,11 +167,11 @@ namespace Jellyfin.Api.Helpers
{
if (disposing)
{
- _fileStream.Dispose();
+ _stream.Dispose();
if (_job != null)
{
- _transcodingJobHelper.OnTranscodeEndRequest(_job);
+ _transcodingJobHelper?.OnTranscodeEndRequest(_job);
}
}
}