2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Collections.Generic;
|
2018-12-14 09:40:55 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
using MediaBrowser.Model.System;
|
2018-12-14 23:48:06 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-03-07 16:39:40 +00:00
|
|
|
using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
|
|
|
{
|
|
|
|
public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
|
|
|
|
{
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly TranscodingJob _job;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly string _path;
|
|
|
|
private readonly CancellationToken _cancellationToken;
|
|
|
|
private readonly Dictionary<string, string> _outputHeaders;
|
|
|
|
|
|
|
|
private long _bytesWritten = 0;
|
|
|
|
public long StartPosition { get; set; }
|
|
|
|
public bool AllowEndOfFile = true;
|
|
|
|
|
|
|
|
private readonly IDirectStreamProvider _directStreamProvider;
|
|
|
|
|
2019-03-07 16:39:40 +00:00
|
|
|
public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_path = path;
|
|
|
|
_outputHeaders = outputHeaders;
|
|
|
|
_job = job;
|
|
|
|
_logger = logger;
|
|
|
|
_cancellationToken = cancellationToken;
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:39:40 +00:00
|
|
|
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
_directStreamProvider = directStreamProvider;
|
|
|
|
_outputHeaders = outputHeaders;
|
|
|
|
_job = job;
|
|
|
|
_logger = logger;
|
|
|
|
_cancellationToken = cancellationToken;
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public IDictionary<string, string> Headers => _outputHeaders;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
private Stream GetInputStream(bool allowAsyncFileRead)
|
|
|
|
{
|
2020-01-08 16:52:50 +00:00
|
|
|
var fileOptions = FileOptions.SequentialScan;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (allowAsyncFileRead)
|
|
|
|
{
|
2020-01-08 16:52:50 +00:00
|
|
|
fileOptions |= FileOptions.Asynchronous;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-01-08 16:52:50 +00:00
|
|
|
return new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, fileOptions);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_directStreamProvider != null)
|
|
|
|
{
|
|
|
|
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var eofCount = 0;
|
|
|
|
|
|
|
|
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
2019-03-07 16:39:40 +00:00
|
|
|
var allowAsyncFileRead = OperatingSystem.Id != OperatingSystemId.Windows;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
using (var inputStream = GetInputStream(allowAsyncFileRead))
|
|
|
|
{
|
|
|
|
if (StartPosition > 0)
|
|
|
|
{
|
|
|
|
inputStream.Position = StartPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (eofCount < 20 || !AllowEndOfFile)
|
|
|
|
{
|
|
|
|
int bytesRead;
|
|
|
|
if (allowAsyncFileRead)
|
|
|
|
{
|
|
|
|
bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//var position = fs.Position;
|
2018-12-14 23:48:06 +00:00
|
|
|
//_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
{
|
|
|
|
if (_job == null || _job.HasExited)
|
|
|
|
{
|
|
|
|
eofCount++;
|
|
|
|
}
|
|
|
|
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eofCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (_job != null)
|
|
|
|
{
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-01-08 16:52:50 +00:00
|
|
|
var array = new byte[IODefaults.CopyToBufferSize];
|
2018-12-14 09:40:55 +00:00
|
|
|
int bytesRead;
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
|
|
|
while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
|
|
|
|
{
|
|
|
|
var bytesToWrite = bytesRead;
|
|
|
|
|
|
|
|
if (bytesToWrite > 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalBytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-01-08 16:52:50 +00:00
|
|
|
var array = new byte[IODefaults.CopyToBufferSize];
|
2018-12-14 09:40:55 +00:00
|
|
|
int bytesRead;
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
|
|
|
while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
|
|
|
{
|
|
|
|
var bytesToWrite = bytesRead;
|
|
|
|
|
|
|
|
if (bytesToWrite > 0)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalBytesRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|