|
|
|
@ -23,7 +23,6 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
{
|
|
|
|
|
public class HdHomerunUdpStream : LiveStream, IDirectStreamProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
private readonly ISocketFactory _socketFactory;
|
|
|
|
|
|
|
|
|
@ -33,22 +32,18 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
private readonly int _numTuners;
|
|
|
|
|
private readonly INetworkManager _networkManager;
|
|
|
|
|
|
|
|
|
|
private readonly string _tempFilePath;
|
|
|
|
|
|
|
|
|
|
public HdHomerunUdpStream(MediaSourceInfo mediaSource, string originalStreamId, IHdHomerunChannelCommands channelCommands, int numTuners, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost, ISocketFactory socketFactory, INetworkManager networkManager, IEnvironmentInfo environment)
|
|
|
|
|
: base(mediaSource, environment, fileSystem)
|
|
|
|
|
: base(mediaSource, environment, fileSystem, logger, appPaths)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_socketFactory = socketFactory;
|
|
|
|
|
_networkManager = networkManager;
|
|
|
|
|
OriginalStreamId = originalStreamId;
|
|
|
|
|
_channelCommands = channelCommands;
|
|
|
|
|
_numTuners = numTuners;
|
|
|
|
|
_tempFilePath = Path.Combine(appPaths.TranscodingTempPath, UniqueId + ".ts");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task OpenInternal(CancellationToken openCancellationToken)
|
|
|
|
|
protected override Task OpenInternal(CancellationToken openCancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_liveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
@ -57,7 +52,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
var uri = new Uri(mediaSource.Path);
|
|
|
|
|
var localPort = _networkManager.GetRandomUnusedUdpPort();
|
|
|
|
|
|
|
|
|
|
_logger.Info("Opening HDHR UDP Live stream from {0}", uri.Host);
|
|
|
|
|
Logger.Info("Opening HDHR UDP Live stream from {0}", uri.Host);
|
|
|
|
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<bool>();
|
|
|
|
|
|
|
|
|
@ -73,14 +68,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
//OpenedMediaSource.SupportsDirectStream = true;
|
|
|
|
|
//OpenedMediaSource.SupportsTranscoding = true;
|
|
|
|
|
|
|
|
|
|
await taskCompletionSource.Task.ConfigureAwait(false);
|
|
|
|
|
return taskCompletionSource.Task;
|
|
|
|
|
|
|
|
|
|
//await Task.Delay(5000).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task Close()
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Closing HDHR UDP live stream");
|
|
|
|
|
Logger.Info("Closing HDHR UDP live stream");
|
|
|
|
|
_liveStreamCancellationTokenSource.Cancel();
|
|
|
|
|
|
|
|
|
|
return _liveStreamTaskCompletionSource.Task;
|
|
|
|
@ -107,7 +102,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
_logger.Error("Unable to determine local ip address for Legacy HDHomerun stream.");
|
|
|
|
|
Logger.Error("Unable to determine local ip address for Legacy HDHomerun stream.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -119,12 +114,12 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
// send url to start streaming
|
|
|
|
|
await hdHomerunManager.StartStreaming(remoteAddress, localAddress, localPort, _channelCommands, _numTuners, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
_logger.Info("Opened HDHR UDP stream from {0}", remoteAddress);
|
|
|
|
|
Logger.Info("Opened HDHR UDP stream from {0}", remoteAddress);
|
|
|
|
|
|
|
|
|
|
if (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(_tempFilePath));
|
|
|
|
|
using (var fileStream = FileSystem.GetFileStream(_tempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
|
|
|
|
|
FileSystem.CreateDirectory(FileSystem.GetDirectoryName(TempFilePath));
|
|
|
|
|
using (var fileStream = FileSystem.GetFileStream(TempFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.None))
|
|
|
|
|
{
|
|
|
|
|
CopyTo(udpClient, fileStream, openTaskCompletionSource, cancellationToken);
|
|
|
|
|
}
|
|
|
|
@ -132,7 +127,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("HDHR UDP stream cancelled or timed out from {0}", remoteAddress);
|
|
|
|
|
Logger.Info("HDHR UDP stream cancelled or timed out from {0}", remoteAddress);
|
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
@ -140,12 +135,12 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
{
|
|
|
|
|
if (isFirstAttempt)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error opening live stream:", ex);
|
|
|
|
|
Logger.ErrorException("Error opening live stream:", ex);
|
|
|
|
|
openTaskCompletionSource.TrySetException(ex);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.ErrorException("Error copying live stream, will reopen", ex);
|
|
|
|
|
Logger.ErrorException("Error copying live stream, will reopen", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isFirstAttempt = false;
|
|
|
|
@ -156,7 +151,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await DeleteTempFile(_tempFilePath).ConfigureAwait(false);
|
|
|
|
|
await DeleteTempFile(TempFilePath).ConfigureAwait(false);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -168,37 +163,6 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task CopyToAsync(Stream stream, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var allowAsync = false;//Environment.OperatingSystem != MediaBrowser.Model.System.OperatingSystem.Windows;
|
|
|
|
|
// use non-async filestream along with read due to https://github.com/dotnet/corefx/issues/6039
|
|
|
|
|
|
|
|
|
|
using (var inputStream = (FileStream)GetInputStream(_tempFilePath, allowAsync))
|
|
|
|
|
{
|
|
|
|
|
TrySeek(inputStream, -20000);
|
|
|
|
|
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
StreamHelper.CopyTo(inputStream, stream, 81920, cancellationToken);
|
|
|
|
|
|
|
|
|
|
//var position = fs.Position;
|
|
|
|
|
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TrySeek(FileStream stream, long offset)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(offset, SeekOrigin.End);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error seeking stream", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int RtpHeaderBytes = 12;
|
|
|
|
|
private void CopyTo(ISocket udpClient, Stream target, TaskCompletionSource<bool> openTaskCompletionSource, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
@ -226,30 +190,6 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
|
|
|
|
|
Resolve(openTaskCompletionSource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//var copier = new AsyncStreamCopier(source, target, 0, cancellationToken, false, bufferSize, bufferCount);
|
|
|
|
|
//copier.IndividualReadOffset = RtpHeaderBytes;
|
|
|
|
|
|
|
|
|
|
//var taskCompletion = new TaskCompletionSource<long>();
|
|
|
|
|
|
|
|
|
|
//copier.TaskCompletionSource = taskCompletion;
|
|
|
|
|
|
|
|
|
|
//var result = copier.BeginCopy(StreamCopyCallback, copier);
|
|
|
|
|
|
|
|
|
|
//if (openTaskCompletionSource != null)
|
|
|
|
|
//{
|
|
|
|
|
// Resolve(openTaskCompletionSource);
|
|
|
|
|
// openTaskCompletionSource = null;
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//if (result.CompletedSynchronously)
|
|
|
|
|
//{
|
|
|
|
|
// StreamCopyCallback(result);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//cancellationToken.Register(() => taskCompletion.TrySetCanceled());
|
|
|
|
|
|
|
|
|
|
//return taskCompletion.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class UdpClientStream : Stream
|
|
|
|
|