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;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-02-12 00:01:38 -07:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public DirectRecorder(ILogger logger, IHttpClient httpClient, IFileSystem fileSystem)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 22:15:06 -07:00
|
|
|
|
public string GetOutputPath(MediaSourceInfo mediaSource, string targetFile)
|
|
|
|
|
{
|
|
|
|
|
return targetFile;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 21:24:42 -07:00
|
|
|
|
public async Task Record(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))
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Opened recording stream from tuner provider");
|
|
|
|
|
|
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();
|
|
|
|
|
|
2016-04-17 13:57:57 -07:00
|
|
|
|
_logger.Info("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
|
|
|
|
|
2016-09-14 09:21:33 -07:00
|
|
|
|
await CopyUntilCancelled(response.Content, output, cancellationToken).ConfigureAwait(false);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-17 13:57:57 -07:00
|
|
|
|
|
|
|
|
|
_logger.Info("Recording completed to file {0}", targetFile);
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
2016-09-14 09:21:33 -07:00
|
|
|
|
|
|
|
|
|
private const int BufferSize = 81920;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
public static Task CopyUntilCancelled(Stream source, Stream target, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return CopyUntilCancelled(source, target, null, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
public static async Task CopyUntilCancelled(Stream source, Stream target, Action onStarted, CancellationToken cancellationToken)
|
2016-09-14 09:21:33 -07:00
|
|
|
|
{
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2016-09-25 11:39:13 -07:00
|
|
|
|
var bytesRead = await CopyToAsyncInternal(source, target, BufferSize, onStarted, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
onStarted = null;
|
2016-09-14 09:21:33 -07:00
|
|
|
|
|
|
|
|
|
//var position = fs.Position;
|
|
|
|
|
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
|
|
|
|
|
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(100).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-25 11:39:13 -07:00
|
|
|
|
private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, Action onStarted, CancellationToken cancellationToken)
|
2016-09-14 09:21:33 -07:00
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[bufferSize];
|
|
|
|
|
int bytesRead;
|
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
|
|
|
|
|
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
|
|
|
|
|
{
|
|
|
|
|
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
totalBytesRead += bytesRead;
|
2016-09-25 11:39:13 -07:00
|
|
|
|
|
|
|
|
|
if (onStarted != null)
|
|
|
|
|
{
|
|
|
|
|
onStarted();
|
|
|
|
|
}
|
|
|
|
|
onStarted = null;
|
2016-09-14 09:21:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return totalBytesRead;
|
|
|
|
|
}
|
2016-02-12 00:01:38 -07:00
|
|
|
|
}
|
|
|
|
|
}
|