jellyfin/MediaBrowser.Api/LiveTv/ProgressiveFileCopier.cs

106 lines
3.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2013-02-27 05:19:05 +01:00
using System.IO;
2016-07-24 18:46:17 +02:00
using System.Threading;
2013-02-27 05:19:05 +01:00
using System.Threading.Tasks;
2016-10-05 09:15:29 +02:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.IO;
2016-10-25 21:02:04 +02:00
using MediaBrowser.Model.Services;
2017-05-22 06:54:02 +02:00
using MediaBrowser.Model.System;
2018-12-15 00:48:06 +01:00
using Microsoft.Extensions.Logging;
2013-02-27 05:19:05 +01:00
namespace MediaBrowser.Api.LiveTv
2013-02-27 05:19:05 +01:00
{
2016-10-25 21:02:04 +02:00
public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
2014-06-26 19:04:11 +02:00
{
private readonly IFileSystem _fileSystem;
2016-03-22 04:27:46 +01:00
private readonly ILogger _logger;
private readonly string _path;
private readonly Dictionary<string, string> _outputHeaders;
2014-06-26 19:04:11 +02:00
2017-05-22 06:54:02 +02:00
const int StreamCopyToBufferSize = 81920;
2016-03-22 04:27:46 +01:00
2016-09-30 04:21:24 +02:00
public long StartPosition { get; set; }
2016-09-25 20:39:13 +02:00
public bool AllowEndOfFile = true;
2016-10-25 21:02:04 +02:00
private readonly IDirectStreamProvider _directStreamProvider;
2017-05-22 06:54:02 +02:00
private readonly IEnvironmentInfo _environment;
2018-09-12 19:26:21 +02:00
private IStreamHelper _streamHelper;
2016-10-05 09:15:29 +02:00
2018-09-12 19:26:21 +02:00
public ProgressiveFileCopier(IFileSystem fileSystem, IStreamHelper streamHelper, string path, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
2014-06-26 19:04:11 +02:00
{
_fileSystem = fileSystem;
_path = path;
_outputHeaders = outputHeaders;
2016-03-22 04:27:46 +01:00
_logger = logger;
2017-05-22 06:54:02 +02:00
_environment = environment;
2018-09-12 19:26:21 +02:00
_streamHelper = streamHelper;
2014-06-26 19:04:11 +02:00
}
2018-09-12 19:26:21 +02:00
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, IStreamHelper streamHelper, Dictionary<string, string> outputHeaders, ILogger logger, IEnvironmentInfo environment)
2016-10-05 09:15:29 +02:00
{
_directStreamProvider = directStreamProvider;
_outputHeaders = outputHeaders;
_logger = logger;
2017-05-22 06:54:02 +02:00
_environment = environment;
2018-09-12 19:26:21 +02:00
_streamHelper = streamHelper;
2016-10-05 09:15:29 +02:00
}
public IDictionary<string, string> Headers => _outputHeaders;
2017-05-22 06:54:02 +02:00
private Stream GetInputStream(bool allowAsyncFileRead)
2016-10-05 09:15:29 +02:00
{
2017-06-01 06:27:17 +02:00
var fileOpenOptions = FileOpenOptions.SequentialScan;
2017-05-22 06:54:02 +02:00
if (allowAsyncFileRead)
{
fileOpenOptions |= FileOpenOptions.Asynchronous;
}
return _fileSystem.GetFileStream(_path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite, fileOpenOptions);
2016-10-05 09:15:29 +02:00
}
2016-10-25 21:02:04 +02:00
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
2013-02-27 05:19:05 +01:00
{
if (_directStreamProvider != null)
2013-02-27 05:19:05 +01:00
{
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
return;
}
2016-10-05 09:15:29 +02:00
var eofCount = 0;
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
2018-09-12 19:26:21 +02:00
var allowAsyncFileRead = true;
2017-05-22 06:54:02 +02:00
using (var inputStream = GetInputStream(allowAsyncFileRead))
{
if (StartPosition > 0)
2013-02-27 05:19:05 +01:00
{
inputStream.Position = StartPosition;
}
2017-10-14 08:52:56 +02:00
var emptyReadLimit = AllowEndOfFile ? 20 : 100;
while (eofCount < emptyReadLimit)
{
int bytesRead;
2018-09-12 19:26:21 +02:00
bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
2016-09-30 04:21:24 +02:00
//var position = fs.Position;
//_logger.LogDebug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
if (bytesRead == 0)
{
eofCount++;
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
else
{
eofCount = 0;
2013-02-27 05:19:05 +01:00
}
}
}
2013-02-27 05:19:05 +01:00
}
}
}