2016-02-12 00:01:38 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2017-05-15 12:45:39 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
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;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
private readonly IStreamHelper _streamHelper;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
public DirectRecorder(ILogger logger, IHttpClient httpClient, IFileSystem fileSystem, IStreamHelper streamHelper)
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_fileSystem = fileSystem;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:45:39 -07: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)
|
|
|
|
|
{
|
2017-09-18 09:52:22 -07:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(targetFile));
|
|
|
|
|
|
2017-05-15 12:45:39 -07:00
|
|
|
|
using (var output = _fileSystem.GetFileStream(targetFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
|
|
|
|
|
{
|
|
|
|
|
onStarted();
|
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("Copying recording stream to file {0}", 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
|
2017-08-21 22:41:02 -07:00
|
|
|
|
var durationToken = new CancellationTokenSource(duration);
|
|
|
|
|
cancellationToken = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, durationToken.Token).Token;
|
2017-05-15 12:45:39 -07:00
|
|
|
|
|
|
|
|
|
await directStreamProvider.CopyToAsync(output, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("Recording completed to file {0}", 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
|
|
|
|
{
|
2016-11-14 12:26:51 -07:00
|
|
|
|
var httpRequestOptions = new HttpRequestOptions
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
2016-11-14 12:26:51 -07:00
|
|
|
|
Url = mediaSource.Path,
|
2016-12-27 13:42:02 -07:00
|
|
|
|
BufferContent = false,
|
|
|
|
|
|
|
|
|
|
// Some remote urls will expect a user agent to be supplied
|
|
|
|
|
UserAgent = "Emby/3.0",
|
|
|
|
|
|
|
|
|
|
// Shouldn't matter but may cause issues
|
|
|
|
|
EnableHttpCompression = false
|
2016-02-12 00:01:38 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var response = await _httpClient.SendAsync(httpRequestOptions, "GET").ConfigureAwait(false))
|
|
|
|
|
{
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("Opened recording stream from tuner provider");
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2017-09-18 09:52:22 -07:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(targetFile));
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using (var output = _fileSystem.GetFileStream(targetFile, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
|
2016-02-12 00:01:38 -07:00
|
|
|
|
{
|
|
|
|
|
onStarted();
|
|
|
|
|
|
2018-12-13 06:18:25 -07:00
|
|
|
|
_logger.LogInformation("Copying recording stream to file {0}", targetFile);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
|
2016-09-29 05:55:49 -07: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;
|
2016-02-29 21:24:42 -07:00
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
|
await _streamHelper.CopyUntilCancelled(response.Content, output, 81920, cancellationToken).ConfigureAwait(false);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|