2016-03-27 21:11:27 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-09-03 02:30:24 +00:00
|
|
|
|
using System;
|
2013-02-27 04:19:05 +00:00
|
|
|
|
using System.IO;
|
2016-07-24 16:46:17 +00:00
|
|
|
|
using System.Threading;
|
2013-02-27 04:19:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2015-10-04 04:23:11 +00:00
|
|
|
|
using CommonIO;
|
2016-08-05 04:08:11 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using ServiceStack.Web;
|
2013-02-27 04:19:05 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
|
|
|
|
{
|
2016-08-05 04:08:11 +00:00
|
|
|
|
public class ProgressiveFileCopier : IAsyncStreamSource, IHasOptions
|
2014-06-26 17:04:11 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-07-17 22:21:35 +00:00
|
|
|
|
private readonly TranscodingJob _job;
|
2016-03-22 03:27:46 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2016-08-05 04:08:11 +00:00
|
|
|
|
private readonly string _path;
|
|
|
|
|
private readonly CancellationToken _cancellationToken;
|
|
|
|
|
private readonly Dictionary<string, string> _outputHeaders;
|
2014-06-26 17:04:11 +00:00
|
|
|
|
|
2015-07-31 20:38:08 +00:00
|
|
|
|
// 256k
|
2016-07-27 17:11:25 +00:00
|
|
|
|
private const int BufferSize = 81920;
|
2016-03-22 03:27:46 +00:00
|
|
|
|
|
2014-09-03 02:30:24 +00:00
|
|
|
|
private long _bytesWritten = 0;
|
2016-09-30 02:21:24 +00:00
|
|
|
|
public long StartPosition { get; set; }
|
2016-09-25 18:39:13 +00:00
|
|
|
|
public bool AllowEndOfFile = true;
|
|
|
|
|
|
2016-08-05 04:08:11 +00:00
|
|
|
|
public ProgressiveFileCopier(IFileSystem fileSystem, string path, Dictionary<string, string> outputHeaders, TranscodingJob job, ILogger logger, CancellationToken cancellationToken)
|
2014-06-26 17:04:11 +00:00
|
|
|
|
{
|
|
|
|
|
_fileSystem = fileSystem;
|
2016-08-05 04:08:11 +00:00
|
|
|
|
_path = path;
|
|
|
|
|
_outputHeaders = outputHeaders;
|
2014-07-17 22:21:35 +00:00
|
|
|
|
_job = job;
|
2016-03-22 03:27:46 +00:00
|
|
|
|
_logger = logger;
|
2016-08-05 04:08:11 +00:00
|
|
|
|
_cancellationToken = cancellationToken;
|
2014-06-26 17:04:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-05 04:08:11 +00:00
|
|
|
|
public IDictionary<string, string> Options
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _outputHeaders;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task WriteToAsync(Stream outputStream)
|
2013-02-27 04:19:05 +00:00
|
|
|
|
{
|
2016-08-06 14:08:38 +00:00
|
|
|
|
try
|
2013-02-27 04:19:05 +00:00
|
|
|
|
{
|
2016-08-06 14:08:38 +00:00
|
|
|
|
var eofCount = 0;
|
|
|
|
|
|
2016-08-16 00:22:59 +00:00
|
|
|
|
using (var fs = _fileSystem.GetFileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
|
2013-02-27 04:19:05 +00:00
|
|
|
|
{
|
2016-09-30 02:21:24 +00:00
|
|
|
|
if (StartPosition > 0)
|
|
|
|
|
{
|
|
|
|
|
fs.Position = StartPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 18:39:13 +00:00
|
|
|
|
while (eofCount < 15 || !AllowEndOfFile)
|
2016-08-06 14:08:38 +00:00
|
|
|
|
{
|
2016-08-16 00:22:59 +00:00
|
|
|
|
var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, _cancellationToken).ConfigureAwait(false);
|
2013-02-27 04:19:05 +00:00
|
|
|
|
|
2016-08-06 14:08:38 +00:00
|
|
|
|
//var position = fs.Position;
|
|
|
|
|
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
|
2013-02-27 04:19:05 +00:00
|
|
|
|
|
2016-08-06 14:08:38 +00:00
|
|
|
|
if (bytesRead == 0)
|
2014-07-17 22:21:35 +00:00
|
|
|
|
{
|
2016-08-06 14:08:38 +00:00
|
|
|
|
if (_job == null || _job.HasExited)
|
|
|
|
|
{
|
|
|
|
|
eofCount++;
|
|
|
|
|
}
|
2016-08-16 00:22:59 +00:00
|
|
|
|
await Task.Delay(100, _cancellationToken).ConfigureAwait(false);
|
2016-08-06 14:08:38 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
eofCount = 0;
|
2014-07-17 22:21:35 +00:00
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-06 14:08:38 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_job != null)
|
|
|
|
|
{
|
|
|
|
|
ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
}
|
2014-09-03 02:30:24 +00:00
|
|
|
|
|
2016-07-24 16:46:17 +00:00
|
|
|
|
private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
|
2014-09-03 02:30:24 +00:00
|
|
|
|
{
|
2016-07-24 16:46:17 +00:00
|
|
|
|
byte[] buffer = new byte[bufferSize];
|
|
|
|
|
int bytesRead;
|
|
|
|
|
int totalBytesRead = 0;
|
2016-03-22 03:27:46 +00:00
|
|
|
|
|
2016-07-24 16:46:17 +00: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-03 02:30:24 +00:00
|
|
|
|
|
2016-07-24 16:46:17 +00:00
|
|
|
|
_bytesWritten += bytesRead;
|
|
|
|
|
totalBytesRead += bytesRead;
|
2014-09-03 02:30:24 +00:00
|
|
|
|
|
|
|
|
|
if (_job != null)
|
|
|
|
|
{
|
|
|
|
|
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-24 16:46:17 +00:00
|
|
|
|
|
|
|
|
|
return totalBytesRead;
|
2014-09-03 02:30:24 +00:00
|
|
|
|
}
|
2013-02-27 04:19:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|