migrate to IHttpClientFactory in DirectRecorder

This commit is contained in:
crobibero 2020-08-31 11:38:47 -06:00
parent 6d19adbecf
commit 804b0fc034

View File

@ -16,13 +16,13 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
public class DirectRecorder : IRecorder public class DirectRecorder : IRecorder
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IHttpClient _httpClient; private readonly IHttpClientFactory _httpClientFactory;
private readonly IStreamHelper _streamHelper; private readonly IStreamHelper _streamHelper;
public DirectRecorder(ILogger logger, IHttpClient httpClient, IStreamHelper streamHelper) public DirectRecorder(ILogger logger, IHttpClientFactory httpClientFactory, IStreamHelper streamHelper)
{ {
_logger = logger; _logger = logger;
_httpClient = httpClient; _httpClientFactory = httpClientFactory;
_streamHelper = streamHelper; _streamHelper = streamHelper;
} }
@ -63,36 +63,24 @@ namespace Emby.Server.Implementations.LiveTv.EmbyTV
private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken) private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
{ {
var httpRequestOptions = new HttpRequestOptions using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(mediaSource.Path, cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Opened recording stream from tuner provider");
Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{ {
Url = mediaSource.Path, onStarted();
BufferContent = false,
// Some remote urls will expect a user agent to be supplied _logger.LogInformation("Copying recording stream to file {0}", targetFile);
UserAgent = "Emby/3.0",
// Shouldn't matter but may cause issues // The media source if infinite so we need to handle stopping ourselves
DecompressionMethod = CompressionMethods.None var durationToken = new CancellationTokenSource(duration);
}; cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
using (var response = await _httpClient.SendAsync(httpRequestOptions, HttpMethod.Get).ConfigureAwait(false)) await _streamHelper.CopyUntilCancelled(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), output, 81920, cancellationToken).ConfigureAwait(false);
{
_logger.LogInformation("Opened recording stream from tuner provider");
Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
onStarted();
_logger.LogInformation("Copying recording stream to file {0}", targetFile);
// The media source if infinite so we need to handle stopping ourselves
var durationToken = new CancellationTokenSource(duration);
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
await _streamHelper.CopyUntilCancelled(response.Content, output, 81920, cancellationToken).ConfigureAwait(false);
}
} }
_logger.LogInformation("Recording completed to file {0}", targetFile); _logger.LogInformation("Recording completed to file {0}", targetFile);