Merge pull request #3405 from crobibero/api-livetv
Move LiveTvService.cs to Jellyfin.Apipull/3507/head
commit
c675a9bec9
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.IO;
|
||||
|
||||
namespace Jellyfin.Api.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Progressive file copier.
|
||||
/// </summary>
|
||||
public class ProgressiveFileCopier
|
||||
{
|
||||
private readonly string? _path;
|
||||
private readonly IDirectStreamProvider? _directStreamProvider;
|
||||
private readonly IStreamHelper _streamHelper;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ProgressiveFileCopier"/> class.
|
||||
/// </summary>
|
||||
/// <param name="streamHelper">Instance of the <see cref="IStreamHelper"/> interface.</param>
|
||||
/// <param name="path">Filepath to stream from.</param>
|
||||
public ProgressiveFileCopier(IStreamHelper streamHelper, string path)
|
||||
{
|
||||
_path = path;
|
||||
_streamHelper = streamHelper;
|
||||
_directStreamProvider = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ProgressiveFileCopier"/> class.
|
||||
/// </summary>
|
||||
/// <param name="streamHelper">Instance of the <see cref="IStreamHelper"/> interface.</param>
|
||||
/// <param name="directStreamProvider">Instance of the <see cref="IDirectStreamProvider"/> interface.</param>
|
||||
public ProgressiveFileCopier(IStreamHelper streamHelper, IDirectStreamProvider directStreamProvider)
|
||||
{
|
||||
_directStreamProvider = directStreamProvider;
|
||||
_streamHelper = streamHelper;
|
||||
_path = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write source stream to output.
|
||||
/// </summary>
|
||||
/// <param name="outputStream">Output stream.</param>
|
||||
/// <param name="cancellationToken">Cancellation token.</param>
|
||||
/// <returns>A <see cref="Task"/>.</returns>
|
||||
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_directStreamProvider != null)
|
||||
{
|
||||
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var fileOptions = FileOptions.SequentialScan;
|
||||
|
||||
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
||||
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
|
||||
{
|
||||
fileOptions |= FileOptions.Asynchronous;
|
||||
}
|
||||
|
||||
await using var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, fileOptions);
|
||||
const int emptyReadLimit = 100;
|
||||
var eofCount = 0;
|
||||
while (eofCount < emptyReadLimit)
|
||||
{
|
||||
var bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
eofCount++;
|
||||
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
eofCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,80 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Model.IO;
|
||||
using MediaBrowser.Model.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace MediaBrowser.Api.LiveTv
|
||||
{
|
||||
public class ProgressiveFileCopier : IAsyncStreamWriter, IHasHeaders
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly string _path;
|
||||
private readonly Dictionary<string, string> _outputHeaders;
|
||||
|
||||
public bool AllowEndOfFile = true;
|
||||
|
||||
private readonly IDirectStreamProvider _directStreamProvider;
|
||||
private IStreamHelper _streamHelper;
|
||||
|
||||
public ProgressiveFileCopier(IStreamHelper streamHelper, string path, Dictionary<string, string> outputHeaders, ILogger logger)
|
||||
{
|
||||
_path = path;
|
||||
_outputHeaders = outputHeaders;
|
||||
_logger = logger;
|
||||
_streamHelper = streamHelper;
|
||||
}
|
||||
|
||||
public ProgressiveFileCopier(IDirectStreamProvider directStreamProvider, IStreamHelper streamHelper, Dictionary<string, string> outputHeaders, ILogger logger)
|
||||
{
|
||||
_directStreamProvider = directStreamProvider;
|
||||
_outputHeaders = outputHeaders;
|
||||
_logger = logger;
|
||||
_streamHelper = streamHelper;
|
||||
}
|
||||
|
||||
public IDictionary<string, string> Headers => _outputHeaders;
|
||||
|
||||
public async Task WriteToAsync(Stream outputStream, CancellationToken cancellationToken)
|
||||
{
|
||||
if (_directStreamProvider != null)
|
||||
{
|
||||
await _directStreamProvider.CopyToAsync(outputStream, cancellationToken).ConfigureAwait(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var fileOptions = FileOptions.SequentialScan;
|
||||
|
||||
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
||||
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
|
||||
{
|
||||
fileOptions |= FileOptions.Asynchronous;
|
||||
}
|
||||
|
||||
using (var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, fileOptions))
|
||||
{
|
||||
var emptyReadLimit = AllowEndOfFile ? 20 : 100;
|
||||
var eofCount = 0;
|
||||
while (eofCount < emptyReadLimit)
|
||||
{
|
||||
int bytesRead;
|
||||
bytesRead = await _streamHelper.CopyToAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
eofCount++;
|
||||
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
eofCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue