2016-02-12 07:01:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommonIO;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.LiveTv.EmbyTV
|
|
|
|
|
{
|
|
|
|
|
public class DirectRecorder : IRecorder
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
|
|
|
|
|
public DirectRecorder(ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 05:15:06 +00:00
|
|
|
|
public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
|
|
|
|
|
{
|
|
|
|
|
return targetFile;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-01 04:24:42 +00:00
|
|
|
|
public async Task Record(MediaSourceInfo mediaSource, string targetFile, TimeSpan duration, Action onStarted, CancellationToken cancellationToken)
|
2016-02-12 07:01:38 +00:00
|
|
|
|
{
|
|
|
|
|
var httpRequestOptions = new HttpRequestOptions()
|
|
|
|
|
{
|
|
|
|
|
Url = mediaSource.Path
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
httpRequestOptions.BufferContent = false;
|
|
|
|
|
|
|
|
|
|
using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Opened recording stream from tuner provider");
|
|
|
|
|
|
|
|
|
|
using (var output = _fileSystem.GetFileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.Read))
|
|
|
|
|
{
|
|
|
|
|
onStarted();
|
|
|
|
|
|
2016-04-17 20:57:57 +00:00
|
|
|
|
_logger.Info("Copying recording stream to file {0}", targetFile);
|
2016-02-12 07:01:38 +00:00
|
|
|
|
|
2016-04-30 04:00:28 +00:00
|
|
|
|
if (mediaSource.RunTimeTicks.HasValue)
|
2016-04-26 02:16:46 +00:00
|
|
|
|
{
|
2016-04-30 04:00:28 +00:00
|
|
|
|
// The media source already has a fixed duration
|
|
|
|
|
// But add another stop 1 minute later just in case the recording gets stuck for any reason
|
|
|
|
|
var durationToken = new CancellationTokenSource(duration.Add(TimeSpan.FromMinutes(1)));
|
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The media source if infinite so we need to handle stopping ourselves
|
2016-04-26 02:16:46 +00:00
|
|
|
|
var durationToken = new CancellationTokenSource(duration);
|
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
|
|
|
|
|
}
|
2016-03-01 04:24:42 +00:00
|
|
|
|
|
2016-04-26 02:16:46 +00:00
|
|
|
|
await response.Content.CopyToAsync(output, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
|
2016-02-12 07:01:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-17 20:57:57 +00:00
|
|
|
|
|
|
|
|
|
_logger.Info("Recording completed to file {0}", targetFile);
|
2016-02-12 07:01:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|