jellyfin/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs

191 lines
7.3 KiB
C#
Raw Normal View History

2020-06-23 10:48:37 -07:00
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
2020-07-27 12:42:40 -07:00
using Jellyfin.Api.Models.PlaybackDtos;
2020-06-23 10:48:37 -07:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.IO;
namespace Jellyfin.Api.Helpers
{
/// <summary>
/// Progressive file copier.
/// </summary>
public class ProgressiveFileCopier
{
2020-07-27 12:42:40 -07:00
private readonly TranscodingJobDto? _job;
2020-06-23 10:48:37 -07:00
private readonly string? _path;
2020-07-27 12:42:40 -07:00
private readonly CancellationToken _cancellationToken;
2020-06-23 10:48:37 -07:00
private readonly IDirectStreamProvider? _directStreamProvider;
2020-07-27 12:42:40 -07:00
private readonly TranscodingJobHelper _transcodingJobHelper;
private long _bytesWritten;
2020-06-23 10:48:37 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="ProgressiveFileCopier"/> class.
/// </summary>
2020-07-27 12:42:40 -07:00
/// <param name="path">The path to copy from.</param>
/// <param name="job">The transcoding job.</param>
/// <param name="transcodingJobHelper">Instance of the <see cref="TranscodingJobHelper"/>.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public ProgressiveFileCopier(string path, TranscodingJobDto? job, TranscodingJobHelper transcodingJobHelper, CancellationToken cancellationToken)
2020-06-23 10:48:37 -07:00
{
_path = path;
2020-07-27 12:42:40 -07:00
_job = job;
_cancellationToken = cancellationToken;
_transcodingJobHelper = transcodingJobHelper;
2020-06-23 10:48:37 -07:00
}
/// <summary>
/// Initializes a new instance of the <see cref="ProgressiveFileCopier"/> class.
/// </summary>
/// <param name="directStreamProvider">Instance of the <see cref="IDirectStreamProvider"/> interface.</param>
2020-07-27 12:42:40 -07:00
/// <param name="job">The transcoding job.</param>
/// <param name="transcodingJobHelper">Instance of the <see cref="TranscodingJobHelper"/>.</param>
/// <param name="cancellationToken">The cancellation token.</param>
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, TranscodingJobDto? job, TranscodingJobHelper transcodingJobHelper, CancellationToken cancellationToken)
2020-06-23 10:48:37 -07:00
{
_directStreamProvider = directStreamProvider;
2020-07-27 12:42:40 -07:00
_job = job;
_cancellationToken = cancellationToken;
_transcodingJobHelper = transcodingJobHelper;
2020-06-23 10:48:37 -07:00
}
2020-07-27 12:42:40 -07:00
/// <summary>
/// Gets or sets a value indicating whether allow read end of file.
/// </summary>
public bool AllowEndOfFile { get; set; } = true;
/// <summary>
/// Gets or sets copy start position.
/// </summary>
public long StartPosition { get; set; }
2020-06-23 10:48:37 -07:00
/// <summary>
/// Write source stream to output.
/// </summary>
/// <param name="outputStream">Output stream.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A <see cref="Task"/>.</returns>
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
{
2020-07-27 12:42:40 -07:00
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token;
try
2020-06-23 10:48:37 -07:00
{
2020-07-27 12:42:40 -07:00
if (_directStreamProvider != null)
{
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
return;
}
var fileOptions = FileOptions.SequentialScan;
var allowAsyncFileRead = false;
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
fileOptions |= FileOptions.Asynchronous;
allowAsyncFileRead = true;
}
await using var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, fileOptions);
var eofCount = 0;
const int emptyReadLimit = 20;
if (StartPosition > 0)
{
inputStream.Position = StartPosition;
}
while (eofCount < emptyReadLimit || !AllowEndOfFile)
{
int bytesRead;
if (allowAsyncFileRead)
{
bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
}
else
{
bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
}
2020-06-23 10:48:37 -07:00
2020-07-27 12:42:40 -07:00
if (bytesRead == 0)
{
if (_job == null || _job.HasExited)
{
eofCount++;
}
2020-06-23 10:48:37 -07:00
2020-07-27 12:42:40 -07:00
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
else
{
eofCount = 0;
}
}
}
finally
2020-06-23 10:48:37 -07:00
{
2020-07-27 12:42:40 -07:00
if (_job != null)
{
_transcodingJobHelper.OnTranscodeEndRequest(_job);
}
2020-06-23 10:48:37 -07:00
}
2020-07-27 12:42:40 -07:00
}
2020-06-23 10:48:37 -07:00
2020-07-27 12:42:40 -07:00
private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
{
var array = new byte[IODefaults.CopyToBufferSize];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
2020-06-23 10:48:37 -07:00
{
2020-07-27 12:42:40 -07:00
var bytesToWrite = bytesRead;
2020-06-23 10:48:37 -07:00
2020-07-27 12:42:40 -07:00
if (bytesToWrite > 0)
2020-06-23 10:48:37 -07:00
{
2020-07-27 12:42:40 -07:00
await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
_bytesWritten += bytesRead;
totalBytesRead += bytesRead;
if (_job != null)
{
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
}
2020-06-23 10:48:37 -07:00
}
2020-07-27 12:42:40 -07:00
}
return totalBytesRead;
}
private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
{
var array = new byte[IODefaults.CopyToBufferSize];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
var bytesToWrite = bytesRead;
if (bytesToWrite > 0)
2020-06-23 10:48:37 -07:00
{
2020-07-27 12:42:40 -07:00
await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
_bytesWritten += bytesRead;
totalBytesRead += bytesRead;
if (_job != null)
{
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
}
2020-06-23 10:48:37 -07:00
}
}
2020-07-27 12:42:40 -07:00
return totalBytesRead;
2020-06-23 10:48:37 -07:00
}
}
}