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