fix merge conflicts

This commit is contained in:
Luke Pulverenti 2017-10-01 23:35:31 -04:00
parent 27868803d8
commit f651fd6ffb
3 changed files with 26 additions and 123 deletions

View File

@ -19,27 +19,21 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
{
public class HdHomerunHttpStream : LiveStream, IDirectStreamProvider
{
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private readonly IServerApplicationHost _appHost;
private readonly CancellationTokenSource _liveStreamCancellationTokenSource = new CancellationTokenSource();
private readonly TaskCompletionSource<bool> _liveStreamTaskCompletionSource = new TaskCompletionSource<bool>();
private readonly string _tempFilePath;
public HdHomerunHttpStream(MediaSourceInfo mediaSource, string originalStreamId, IFileSystem fileSystem, IHttpClient httpClient, ILogger logger, IServerApplicationPaths appPaths, IServerApplicationHost appHost, IEnvironmentInfo environment)
: base(mediaSource, environment, fileSystem)
: base(mediaSource, environment, fileSystem, logger, appPaths)
{
_httpClient = httpClient;
_logger = logger;
_appHost = appHost;
OriginalStreamId = originalStreamId;
_tempFilePath = Path.Combine(appPaths.TranscodingTempPath, UniqueId + ".ts");
}
protected override async Task OpenInternal(CancellationToken openCancellationToken)
protected override Task OpenInternal(CancellationToken openCancellationToken)
{
_liveStreamCancellationTokenSource.Token.ThrowIfCancellationRequested();
@ -47,7 +41,7 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
var url = mediaSource.Path;
_logger.Info("Opening HDHR Live stream from {0}", url);
Logger.Info("Opening HDHR Live stream from {0}", url);
var taskCompletionSource = new TaskCompletionSource<bool>();
@ -66,14 +60,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 live stream");
Logger.Info("Closing HDHR live stream");
_liveStreamCancellationTokenSource.Cancel();
return _liveStreamTaskCompletionSource.Task;
@ -102,14 +96,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
}, "GET").ConfigureAwait(false))
{
_logger.Info("Opened HDHR stream from {0}", url);
Logger.Info("Opened HDHR stream from {0}", url);
if (!cancellationToken.IsCancellationRequested)
{
_logger.Info("Beginning multicastStream.CopyUntilCancelled");
Logger.Info("Beginning multicastStream.CopyUntilCancelled");
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))
{
StreamHelper.CopyTo(response.Content, fileStream, 81920, () => Resolve(openTaskCompletionSource), cancellationToken);
}
@ -124,19 +118,19 @@ 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;
}
_liveStreamTaskCompletionSource.TrySetResult(true);
await DeleteTempFile(_tempFilePath).ConfigureAwait(false);
await DeleteTempFile(TempFilePath).ConfigureAwait(false);
});
}
@ -147,36 +141,5 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
openTaskCompletionSource.TrySetResult(true);
});
}
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);
}
}
}
}

View File

@ -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

View File

@ -1,3 +1,3 @@
using System.Reflection;
[assembly: AssemblyVersion("3.2.32")]
[assembly: AssemblyVersion("3.2.32.16")]