jellyfin-server/Emby.Server.Implementations/LiveTv/EmbyTV/DirectRecorder.cs

94 lines
4.0 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.IO;
using System.Net.Http;
2016-02-12 07:01:38 +00:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
2017-05-15 19:45:39 +00:00
using MediaBrowser.Controller.Library;
2016-02-12 07:01:38 +00:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.IO;
using Microsoft.Extensions.Logging;
2016-02-12 07:01:38 +00:00
2016-11-03 23:35:19 +00:00
namespace Emby.Server.Implementations.LiveTv.EmbyTV
2016-02-12 07:01:38 +00:00
{
public class DirectRecorder : IRecorder
{
private readonly ILogger _logger;
private readonly IHttpClientFactory _httpClientFactory;
2018-09-12 17:26:21 +00:00
private readonly IStreamHelper _streamHelper;
2016-02-12 07:01:38 +00:00
public DirectRecorder(ILogger logger, IHttpClientFactory httpClientFactory, IStreamHelper streamHelper)
2016-02-12 07:01:38 +00:00
{
_logger = logger;
_httpClientFactory = httpClientFactory;
2018-09-12 17:26:21 +00:00
_streamHelper = streamHelper;
2016-02-12 07:01:38 +00:00
}
2016-05-10 05:15:06 +00:00
public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
{
return targetFile;
}
2017-05-15 19:45:39 +00:00
public Task Record(IDirectStreamProvider directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
{
if (directStreamProvider != null)
{
return RecordFromDirectStreamProvider(directStreamProvider, targetFile, duration, onStarted, cancellationToken);
}
return RecordFromMediaSource(mediaSource, targetFile, duration, onStarted, cancellationToken);
}
private async Task RecordFromDirectStreamProvider(IDirectStreamProvider directStreamProvider, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
{
Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
2017-09-18 16:52:22 +00:00
2020-01-08 16:52:50 +00:00
using (var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
2017-05-15 19:45:39 +00:00
{
onStarted();
_logger.LogInformation("Copying recording stream to file {0}", targetFile);
2017-05-15 19:45:39 +00:00
2018-09-12 17:26:21 +00:00
// The media source is infinite so we need to handle stopping ourselves
2020-08-31 20:20:19 +00:00
using var durationToken = new CancellationTokenSource(duration);
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
2017-05-15 19:45:39 +00:00
2020-08-31 20:20:19 +00:00
await directStreamProvider.CopyToAsync(output, cancellationTokenSource.Token).ConfigureAwait(false);
2017-05-15 19:45:39 +00:00
}
_logger.LogInformation("Recording completed to file {0}", targetFile);
2017-05-15 19:45:39 +00:00
}
private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
2016-02-12 07:01:38 +00:00
{
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Opened recording stream from tuner provider");
Directory.CreateDirectory(Path.GetDirectoryName(targetFile));
2016-02-12 07:01:38 +00:00
2020-08-31 18:46:42 +00:00
await using var output = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read);
2016-02-12 07:01:38 +00:00
2020-08-31 18:46:42 +00:00
onStarted();
2016-02-12 07:01:38 +00:00
2020-08-31 18:46:42 +00:00
_logger.LogInformation("Copying recording stream to file {0}", targetFile);
2016-03-01 04:24:42 +00:00
2020-08-31 18:46:42 +00:00
// The media source if infinite so we need to handle stopping ourselves
2020-11-17 18:43:00 +00:00
using var durationToken = new CancellationTokenSource(duration);
using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
cancellationToken = linkedCancellationToken.Token;
2020-08-31 18:46:42 +00:00
await _streamHelper.CopyUntilCancelled(
2020-11-17 18:43:00 +00:00
await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
output,
2020-09-04 14:24:21 +00:00
IODefaults.CopyToBufferSize,
cancellationToken).ConfigureAwait(false);
2016-04-17 20:57:57 +00:00
_logger.LogInformation("Recording completed to file {0}", targetFile);
2016-02-12 07:01:38 +00:00
}
}
}