jellyfin/Jellyfin.Api/Helpers/ProgressiveFileStream.cs

184 lines
6.1 KiB
C#
Raw Normal View History

2020-09-25 21:59:17 +00:00
using System;
2021-08-17 17:18:26 +00:00
using System.Diagnostics;
2020-09-25 21:59:17 +00:00
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Api.Models.PlaybackDtos;
using MediaBrowser.Model.IO;
namespace Jellyfin.Api.Helpers
{
/// <summary>
/// A progressive file stream for transferring transcoded files as they are written to.
/// </summary>
public class ProgressiveFileStream : Stream
{
private readonly Stream _stream;
2020-09-25 21:59:17 +00:00
private readonly TranscodingJobDto? _job;
private readonly TranscodingJobHelper? _transcodingJobHelper;
2021-08-17 17:18:26 +00:00
private readonly int _timeoutMs;
2020-09-25 21:59:17 +00:00
private readonly bool _allowAsyncFileRead;
private int _bytesWritten;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="ProgressiveFileStream"/> class.
/// </summary>
/// <param name="filePath">The path to the transcoded file.</param>
/// <param name="job">The transcoding job information.</param>
/// <param name="transcodingJobHelper">The transcoding job helper.</param>
2021-08-17 17:18:26 +00:00
/// <param name="timeoutMs">The timeout duration in milliseconds.</param>
public ProgressiveFileStream(string filePath, TranscodingJobDto? job, TranscodingJobHelper transcodingJobHelper, int timeoutMs = 30000)
2020-09-25 21:59:17 +00:00
{
_job = job;
_transcodingJobHelper = transcodingJobHelper;
2021-08-17 17:18:26 +00:00
_timeoutMs = timeoutMs;
2020-09-25 21:59:17 +00:00
var fileOptions = FileOptions.SequentialScan;
_allowAsyncFileRead = false;
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
if (AsyncFile.UseAsyncIO)
2020-09-25 21:59:17 +00:00
{
fileOptions |= FileOptions.Asynchronous;
_allowAsyncFileRead = true;
}
_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="timeoutMs">The timeout duration in milliseconds.</param>
2021-09-10 08:03:42 +00:00
public ProgressiveFileStream(Stream stream, int timeoutMs = 30000)
{
2021-09-10 08:03:42 +00:00
_job = null;
_transcodingJobHelper = null;
_timeoutMs = timeoutMs;
_allowAsyncFileRead = AsyncFile.UseAsyncIO;
_stream = stream;
2020-09-25 21:59:17 +00:00
}
/// <inheritdoc />
public override bool CanRead => _stream.CanRead;
2020-09-25 21:59:17 +00:00
/// <inheritdoc />
public override bool CanSeek => false;
/// <inheritdoc />
public override bool CanWrite => false;
/// <inheritdoc />
public override long Length => throw new NotSupportedException();
/// <inheritdoc />
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
/// <inheritdoc />
public override void Flush()
{
_stream.Flush();
2020-09-25 21:59:17 +00:00
}
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
return _stream.Read(buffer, offset, count);
2020-09-25 21:59:17 +00:00
}
/// <inheritdoc />
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
int totalBytesRead = 0;
int remainingBytesToRead = count;
2021-08-17 17:18:26 +00:00
var stopwatch = Stopwatch.StartNew();
2020-09-25 21:59:17 +00:00
2020-11-03 23:38:47 +00:00
int newOffset = offset;
2020-09-26 17:03:23 +00:00
while (remainingBytesToRead > 0)
2020-09-25 21:59:17 +00:00
{
cancellationToken.ThrowIfCancellationRequested();
int bytesRead;
if (_allowAsyncFileRead)
{
bytesRead = await _stream.ReadAsync(buffer, newOffset, remainingBytesToRead, cancellationToken).ConfigureAwait(false);
2020-09-25 21:59:17 +00:00
}
else
{
bytesRead = _stream.Read(buffer, newOffset, remainingBytesToRead);
2020-09-25 21:59:17 +00:00
}
remainingBytesToRead -= bytesRead;
2020-11-03 23:38:47 +00:00
newOffset += bytesRead;
2020-09-25 21:59:17 +00:00
if (bytesRead > 0)
{
_bytesWritten += bytesRead;
totalBytesRead += bytesRead;
if (_job != null)
{
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
}
}
2020-09-26 17:03:23 +00:00
else
2020-09-25 21:59:17 +00:00
{
2021-08-17 17:18:26 +00:00
// If the job is null it's a live stream and will require user action to close, but don't keep it open indefinitely
if (_job?.HasExited ?? stopwatch.ElapsedMilliseconds > _timeoutMs)
2020-09-25 21:59:17 +00:00
{
2020-09-26 17:03:23 +00:00
break;
2020-09-25 21:59:17 +00:00
}
2020-11-03 23:38:47 +00:00
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
2020-09-25 21:59:17 +00:00
}
}
return totalBytesRead;
}
/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();
/// <inheritdoc />
public override void SetLength(long value)
=> throw new NotSupportedException();
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
2020-09-26 17:03:23 +00:00
try
2020-09-25 21:59:17 +00:00
{
2020-09-26 17:03:23 +00:00
if (disposing)
2020-09-25 21:59:17 +00:00
{
_stream.Dispose();
2020-09-26 17:03:23 +00:00
if (_job != null)
{
_transcodingJobHelper?.OnTranscodeEndRequest(_job);
2020-09-26 17:03:23 +00:00
}
2020-09-25 21:59:17 +00:00
}
}
2020-09-26 17:03:23 +00:00
finally
{
_disposed = true;
base.Dispose(disposing);
}
2020-09-25 21:59:17 +00:00
}
}
}