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
|
|
|
|
|
{
|
2021-09-10 07:29:14 +00:00
|
|
|
|
private readonly Stream _stream;
|
2020-09-25 21:59:17 +00:00
|
|
|
|
private readonly TranscodingJobDto? _job;
|
2021-09-10 07:29:14 +00:00
|
|
|
|
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 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
|
|
|
|
|
2021-09-25 17:44:40 +00:00
|
|
|
|
_stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan);
|
2021-09-10 07:29:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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 07:29:14 +00:00
|
|
|
|
{
|
2021-09-10 08:03:42 +00:00
|
|
|
|
_job = null;
|
|
|
|
|
_transcodingJobHelper = null;
|
2021-09-10 07:29:14 +00:00
|
|
|
|
_timeoutMs = timeoutMs;
|
|
|
|
|
_stream = stream;
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-09-10 07:29:14 +00:00
|
|
|
|
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()
|
|
|
|
|
{
|
2021-11-27 02:23:46 +00:00
|
|
|
|
// Not supported
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
2021-11-27 02:23:46 +00:00
|
|
|
|
=> Read(buffer.AsSpan(offset, count));
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override int Read(Span<byte> buffer)
|
2020-09-25 21:59:17 +00:00
|
|
|
|
{
|
2021-11-27 02:23:46 +00:00
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
|
|
|
|
|
2022-02-12 08:51:47 +00:00
|
|
|
|
while (true)
|
2021-11-27 02:23:46 +00:00
|
|
|
|
{
|
|
|
|
|
totalBytesRead += _stream.Read(buffer);
|
2022-02-12 08:51:47 +00:00
|
|
|
|
if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
|
2021-11-27 02:23:46 +00:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateBytesWritten(totalBytesRead);
|
|
|
|
|
|
|
|
|
|
return totalBytesRead;
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
2021-11-27 02:23:46 +00:00
|
|
|
|
=> await ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
2020-09-25 21:59:17 +00:00
|
|
|
|
{
|
|
|
|
|
int totalBytesRead = 0;
|
2021-08-17 17:18:26 +00:00
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
2020-09-25 21:59:17 +00:00
|
|
|
|
|
2022-02-12 08:51:47 +00:00
|
|
|
|
while (true)
|
2020-09-25 21:59:17 +00:00
|
|
|
|
{
|
2021-11-27 02:23:46 +00:00
|
|
|
|
totalBytesRead += await _stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
|
2022-02-12 08:51:47 +00:00
|
|
|
|
if (StopReading(totalBytesRead, stopwatch.ElapsedMilliseconds))
|
2020-09-25 21:59:17 +00:00
|
|
|
|
{
|
2021-11-27 02:23:46 +00:00
|
|
|
|
break;
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 02:23:46 +00:00
|
|
|
|
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 02:23:46 +00:00
|
|
|
|
UpdateBytesWritten(totalBytesRead);
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-09-10 07:29:14 +00:00
|
|
|
|
_stream.Dispose();
|
2020-09-26 17:03:23 +00:00
|
|
|
|
|
2022-12-05 14:01:13 +00:00
|
|
|
|
if (_job is not null)
|
2020-09-26 17:03:23 +00:00
|
|
|
|
{
|
2021-09-10 07:29:14 +00:00
|
|
|
|
_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
|
|
|
|
}
|
2021-11-27 02:23:46 +00:00
|
|
|
|
|
|
|
|
|
private void UpdateBytesWritten(int totalBytesRead)
|
|
|
|
|
{
|
2022-12-05 14:01:13 +00:00
|
|
|
|
if (_job is not null)
|
2021-11-27 02:23:46 +00:00
|
|
|
|
{
|
|
|
|
|
_job.BytesDownloaded += totalBytesRead;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 08:51:47 +00:00
|
|
|
|
private bool StopReading(int bytesRead, long elapsed)
|
2021-11-27 02:23:46 +00:00
|
|
|
|
{
|
2022-02-12 08:51:47 +00:00
|
|
|
|
// It should stop reading when anything has been successfully read or if the job has exited
|
|
|
|
|
// If the job is null, however, it's a live stream and will require user action to close,
|
|
|
|
|
// but don't keep it open indefinitely if it isn't reading anything
|
|
|
|
|
return bytesRead > 0 || (_job?.HasExited ?? elapsed >= _timeoutMs);
|
2021-11-27 02:23:46 +00:00
|
|
|
|
}
|
2020-09-25 21:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|