2020-02-06 07:20:23 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2019-01-26 13:47:11 -07:00
|
|
|
using System.IO;
|
2019-12-13 12:11:37 -07:00
|
|
|
using System.Net.Http;
|
2016-02-12 00:01:38 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2021-09-10 00:29:14 -07:00
|
|
|
using Jellyfin.Api.Helpers;
|
2016-02-12 00:01:38 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2017-05-15 12:45:39 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
2016-02-12 00:01:38 -07:00
|
|
|
using MediaBrowser.Model.Dto;
|
2019-01-13 12:22:00 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2018-12-13 06:18:25 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
2016-11-03 16:35:19 -07:00
|
|
|
namespace Emby.Server.Implementations.LiveTv.EmbyTV
|
2016-02-12 00:01:38 -07:00
|
|
|
{
|
|
|
|
public class DirectRecorder : IRecorder
|
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
2020-08-31 10:38:47 -07:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2018-09-12 10:26:21 -07:00
|
|
|
private readonly IStreamHelper _streamHelper;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
2020-08-31 10:38:47 -07:00
|
|
|
public DirectRecorder(ILogger logger, IHttpClientFactory httpClientFactory, IStreamHelper streamHelper)
|
2016-02-12 00:01:38 -07:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2020-08-31 10:38:47 -07:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2018-09-12 10:26:21 -07:00
|
|
|
_streamHelper = streamHelper;
|
2016-02-12 00:01:38 -07:00
|
|
|
}
|
|
|
|
|
2016-05-09 22:15:06 -07:00
|
|
|
public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
|
|
|
|
{
|
|
|
|
return targetFile;
|
|
|
|
}
|
|
|
|
|
2021-08-28 08:32:09 -07:00
|
|
|
public Task Record(IDirectStreamProvider? directStreamProvider, MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
|
2017-05-15 12:45:39 -07:00
|
|
|
{
|
2022-12-05 07:01:13 -07:00
|
|
|
if (directStreamProvider is not null)
|
2017-05-15 12:45:39 -07:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2021-08-28 08:32:09 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
|
2017-09-18 09:52:22 -07:00
|
|
|
|
2022-01-22 15:36:42 -07:00
|
|
|
await using (var output = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, FileOptions.Asynchronous))
|
2017-05-15 12:45:39 -07:00
|
|
|
{
|
|
|
|
onStarted();
|
|
|
|
|
2021-09-10 00:29:14 -07:00
|
|
|
_logger.LogInformation("Copying recording to file {FilePath}", targetFile);
|
2017-05-15 12:45:39 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
// The media source is infinite so we need to handle stopping ourselves
|
2020-08-31 13:20:19 -07:00
|
|
|
using var durationToken = new CancellationTokenSource(duration);
|
|
|
|
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
|
2021-09-10 00:29:14 -07:00
|
|
|
var linkedCancellationToken = cancellationTokenSource.Token;
|
2022-01-22 15:36:42 -07:00
|
|
|
var fileStream = new ProgressiveFileStream(directStreamProvider.GetStream());
|
|
|
|
await using (fileStream.ConfigureAwait(false))
|
|
|
|
{
|
|
|
|
await _streamHelper.CopyToAsync(
|
|
|
|
fileStream,
|
|
|
|
output,
|
|
|
|
IODefaults.CopyToBufferSize,
|
|
|
|
1000,
|
|
|
|
linkedCancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
2017-05-15 12:45:39 -07:00
|
|
|
}
|
|
|
|
|
2021-09-10 00:29:14 -07:00
|
|
|
_logger.LogInformation("Recording completed: {FilePath}", targetFile);
|
2017-05-15 12:45:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task RecordFromMediaSource(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
|
2016-02-12 00:01:38 -07:00
|
|
|
{
|
2020-08-31 10:38:47 -07:00
|
|
|
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
|
2020-09-01 06:51:06 -07:00
|
|
|
.GetAsync(mediaSource.Path, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
|
2016-12-27 13:42:02 -07:00
|
|
|
|
2020-08-31 10:38:47 -07:00
|
|
|
_logger.LogInformation("Opened recording stream from tuner provider");
|
2016-12-27 13:42:02 -07:00
|
|
|
|
2021-08-28 08:32:09 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(targetFile) ?? throw new ArgumentException("Path can't be a root directory.", nameof(targetFile)));
|
2016-02-12 00:01:38 -07:00
|
|
|
|
2021-10-03 10:52:38 -07:00
|
|
|
await using var output = new FileStream(targetFile, FileMode.CreateNew, FileAccess.Write, FileShare.Read, IODefaults.CopyToBufferSize, FileOptions.Asynchronous);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
2020-08-31 11:46:42 -07:00
|
|
|
onStarted();
|
2016-02-12 00:01:38 -07:00
|
|
|
|
2020-08-31 11:46:42 -07:00
|
|
|
_logger.LogInformation("Copying recording stream to file {0}", targetFile);
|
2016-02-29 21:24:42 -07:00
|
|
|
|
2020-08-31 11:46:42 -07:00
|
|
|
// The media source if infinite so we need to handle stopping ourselves
|
2020-11-17 11:43:00 -07:00
|
|
|
using var durationToken = new CancellationTokenSource(duration);
|
|
|
|
using var linkedCancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token);
|
|
|
|
cancellationToken = linkedCancellationToken.Token;
|
2020-08-31 11:46:42 -07:00
|
|
|
|
2020-09-04 07:16:49 -07:00
|
|
|
await _streamHelper.CopyUntilCancelled(
|
2020-11-17 11:43:00 -07:00
|
|
|
await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false),
|
2020-09-04 07:16:49 -07:00
|
|
|
output,
|
2020-09-04 07:24:21 -07:00
|
|
|
IODefaults.CopyToBufferSize,
|
2020-09-04 07:16:49 -07:00
|
|
|
cancellationToken).ConfigureAwait(false);
|
2016-04-17 13:57:57 -07:00
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
_logger.LogInformation("Recording completed to file {0}", targetFile);
|
2016-02-12 00:01:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|