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