Files
jellyfin-jellyfin-1/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs

82 lines
2.7 KiB
C#
Raw Normal View History

2016-03-27 23:11:27 +02:00
using MediaBrowser.Model.Logging;
2013-12-07 10:52:38 -05:00
using ServiceStack.Web;
2014-09-02 22:30:24 -04:00
using System;
using System.Collections.Generic;
2013-02-26 23:19:05 -05:00
using System.IO;
2016-07-24 12:46:17 -04:00
using System.Threading;
2013-02-26 23:19:05 -05:00
using System.Threading.Tasks;
2015-10-04 00:23:11 -04:00
using CommonIO;
2013-02-26 23:19:05 -05:00
namespace MediaBrowser.Api.Playback.Progressive
{
2014-06-26 13:04:11 -04:00
public class ProgressiveFileCopier
{
private readonly IFileSystem _fileSystem;
2014-07-17 18:21:35 -04:00
private readonly TranscodingJob _job;
2016-03-21 23:27:46 -04:00
private readonly ILogger _logger;
2014-06-26 13:04:11 -04:00
2015-07-31 16:38:08 -04:00
// 256k
private const int BufferSize = 262144;
2016-03-21 23:27:46 -04:00
2014-09-02 22:30:24 -04:00
private long _bytesWritten = 0;
2016-03-21 23:27:46 -04:00
public ProgressiveFileCopier(IFileSystem fileSystem, TranscodingJob job, ILogger logger)
2014-06-26 13:04:11 -04:00
{
_fileSystem = fileSystem;
2014-07-17 18:21:35 -04:00
_job = job;
2016-03-21 23:27:46 -04:00
_logger = logger;
2014-06-26 13:04:11 -04:00
}
2016-07-24 12:46:17 -04:00
public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken)
2013-02-26 23:19:05 -05:00
{
var eofCount = 0;
using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
2013-02-26 23:19:05 -05:00
{
while (eofCount < 15)
{
2016-07-24 12:46:17 -04:00
var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, cancellationToken).ConfigureAwait(false);
2013-02-26 23:19:05 -05:00
2016-07-24 12:46:17 -04:00
//var position = fs.Position;
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
2013-02-26 23:19:05 -05:00
if (bytesRead == 0)
{
2014-07-17 18:21:35 -04:00
if (_job == null || _job.HasExited)
{
eofCount++;
}
2016-07-24 12:46:17 -04:00
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
2013-02-26 23:19:05 -05:00
}
else
{
eofCount = 0;
}
}
}
}
2014-09-02 22:30:24 -04:00
2016-07-24 12:46:17 -04:00
private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
2014-09-02 22:30:24 -04:00
{
2016-07-24 12:46:17 -04:00
byte[] buffer = new byte[bufferSize];
int bytesRead;
int totalBytesRead = 0;
2016-03-21 23:27:46 -04:00
2016-07-24 12:46:17 -04:00
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
2014-09-02 22:30:24 -04:00
2016-07-24 12:46:17 -04:00
_bytesWritten += bytesRead;
totalBytesRead += bytesRead;
2014-09-02 22:30:24 -04:00
if (_job != null)
{
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
}
}
2016-07-24 12:46:17 -04:00
return totalBytesRead;
2014-09-02 22:30:24 -04:00
}
2013-02-26 23:19:05 -05:00
}
}