2016-03-27 14:11:27 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-09-02 19:30:24 -07:00
|
|
|
|
using System;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
using System.IO;
|
2016-07-24 09:46:17 -07:00
|
|
|
|
using System.Threading;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-08-04 21:08:11 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using System.Collections.Generic;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
using MediaBrowser.Model.System;
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
|
|
|
|
{
|
2016-10-25 12:02:04 -07:00
|
|
|
|
public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
|
2014-06-26 10:04:11 -07:00
|
|
|
|
{
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-07-17 15:21:35 -07:00
|
|
|
|
private readonly TranscodingJob _job;
|
2016-03-21 20:27:46 -07:00
|
|
|
|
private readonly ILogger _logger;
|
2016-08-04 21:08:11 -07:00
|
|
|
|
private readonly string _path;
|
|
|
|
|
private readonly CancellationToken _cancellationToken;
|
|
|
|
|
private readonly Dictionary<string, string> _outputHeaders;
|
2014-06-26 10:04:11 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
const int StreamCopyToBufferSize = 81920;
|
2016-03-21 20:27:46 -07:00
|
|
|
|
|
2014-09-02 19:30:24 -07:00
|
|
|
|
private long _bytesWritten = 0;
|
2016-09-29 19:21:24 -07:00
|
|
|
|
public long StartPosition { get; set; }
|
2016-09-25 11:39:13 -07:00
|
|
|
|
public bool AllowEndOfFile = true;
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
private readonly IDirectStreamProvider _directStreamProvider;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
private readonly IEnvironmentInfo _environment;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, IEnvironmentInfo environment, CancellationToken cancellationToken)
|
2014-06-26 10:04:11 -07:00
|
|
|
|
{
|
|
|
|
|
_fileSystem = fileSystem;
|
2016-08-04 21:08:11 -07:00
|
|
|
|
_path = path;
|
|
|
|
|
_outputHeaders = outputHeaders;
|
2014-07-17 15:21:35 -07:00
|
|
|
|
_job = job;
|
2016-03-21 20:27:46 -07:00
|
|
|
|
_logger = logger;
|
2016-08-04 21:08:11 -07:00
|
|
|
|
_cancellationToken = cancellationToken;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
_environment = environment;
|
2014-06-26 10:04:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, IEnvironmentInfo environment, CancellationToken cancellationToken)
|
2016-10-05 00:15:29 -07:00
|
|
|
|
{
|
|
|
|
|
_directStreamProvider = directStreamProvider;
|
|
|
|
|
_outputHeaders = outputHeaders;
|
|
|
|
|
_job = job;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_cancellationToken = cancellationToken;
|
2017-05-21 21:54:02 -07:00
|
|
|
|
_environment = environment;
|
2016-10-05 00:15:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
public IDictionary<string, string> Headers
|
2016-08-04 21:08:11 -07:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _outputHeaders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
private Stream GetInputStream(bool allowAsyncFileRead)
|
2016-10-05 00:15:29 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
var fileOpenOptions = StartPosition > 0
|
|
|
|
|
? FileOpenOptions.RandomAccess
|
|
|
|
|
: FileOpenOptions.SequentialScan;
|
|
|
|
|
|
|
|
|
|
if (allowAsyncFileRead)
|
|
|
|
|
{
|
|
|
|
|
fileOpenOptions |= FileOpenOptions.Asynchronous;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
|
2016-10-05 00:15:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cancellationToken).Token;
|
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
try
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2016-10-05 00:15:29 -07:00
|
|
|
|
if (_directStreamProvider != null)
|
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
|
2016-10-05 00:15:29 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
var eofCount = 0;
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
|
|
|
|
var allowAsyncFileRead = _environment.OperatingSystem != OperatingSystem.Windows;
|
|
|
|
|
|
|
|
|
|
using (var inputStream = GetInputStream(allowAsyncFileRead))
|
2013-02-26 21:19:05 -07:00
|
|
|
|
{
|
2016-09-29 19:21:24 -07:00
|
|
|
|
if (StartPosition > 0)
|
|
|
|
|
{
|
2016-10-05 00:15:29 -07:00
|
|
|
|
inputStream.Position = StartPosition;
|
2016-09-29 19:21:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
while (eofCount < 20 || !AllowEndOfFile)
|
2016-08-06 07:08:38 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
int bytesRead;
|
|
|
|
|
if (allowAsyncFileRead)
|
|
|
|
|
{
|
|
|
|
|
bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
//var position = fs.Position;
|
|
|
|
|
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
|
2013-02-26 21:19:05 -07:00
|
|
|
|
|
2016-08-06 07:08:38 -07:00
|
|
|
|
if (bytesRead == 0)
|
2014-07-17 15:21:35 -07:00
|
|
|
|
{
|
2016-08-06 07:08:38 -07:00
|
|
|
|
if (_job == null || _job.HasExited)
|
|
|
|
|
{
|
|
|
|
|
eofCount++;
|
|
|
|
|
}
|
2017-05-21 21:54:02 -07:00
|
|
|
|
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
2016-08-06 07:08:38 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
eofCount = 0;
|
2014-07-17 15:21:35 -07:00
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-06 07:08:38 -07:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_job != null)
|
|
|
|
|
{
|
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
2014-09-02 19:30:24 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
private async Task<int> CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken)
|
2014-09-02 19:30:24 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
var array = new byte[StreamCopyToBufferSize];
|
2016-07-24 09:46:17 -07:00
|
|
|
|
int bytesRead;
|
|
|
|
|
int totalBytesRead = 0;
|
2016-03-21 20:27:46 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
while ((bytesRead = source.Read(array, 0, array.Length)) != 0)
|
2016-07-24 09:46:17 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -07:00
|
|
|
|
var bytesToWrite = bytesRead;
|
|
|
|
|
|
|
|
|
|
if (bytesToWrite > 0)
|
|
|
|
|
{
|
|
|
|
|
await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
|
2014-09-02 19:30:24 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
_bytesWritten += bytesRead;
|
|
|
|
|
totalBytesRead += bytesRead;
|
2014-09-02 19:30:24 -07:00
|
|
|
|
|
2017-05-21 21:54:02 -07:00
|
|
|
|
if (_job != null)
|
|
|
|
|
{
|
|
|
|
|
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return totalBytesRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<int> CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var array = new byte[StreamCopyToBufferSize];
|
|
|
|
|
int bytesRead;
|
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
|
|
|
|
|
while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
|
|
|
|
{
|
|
|
|
|
var bytesToWrite = bytesRead;
|
|
|
|
|
|
|
|
|
|
if (bytesToWrite > 0)
|
2014-09-02 19:30:24 -07:00
|
|
|
|
{
|
2017-05-21 21:54:02 -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);
|
|
|
|
|
}
|
2014-09-02 19:30:24 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-24 09:46:17 -07:00
|
|
|
|
|
|
|
|
|
return totalBytesRead;
|
2014-09-02 19:30:24 -07:00
|
|
|
|
}
|
2013-02-26 21:19:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|